1 /*
2 * Copyright (c) 2023 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include "code_signature_analysis_kit_test.h"
17
18 #include <securec.h>
19 #include "code_signature_analysis_kit.h"
20
21
22 using namespace testing::ext;
23 using namespace OHOS::Security;
24
SetUpTestCase()25 void CodeSignatureAnalysisKitTest::SetUpTestCase()
26 {
27 ModelApi *test = GetModelApi();
28 if (test != nullptr) {
29 test->release();
30 }
31 }
32
TearDownTestCase()33 void CodeSignatureAnalysisKitTest::TearDownTestCase()
34 {}
35
SetUp()36 void CodeSignatureAnalysisKitTest::SetUp()
37 {}
38
TearDown()39 void CodeSignatureAnalysisKitTest::TearDown()
40 {}
41
GetDataFromSqlMock(const char * sql,uint8_t * result,uint32_t resultLen)42 static int32_t GetDataFromSqlMock(const char* sql, uint8_t *result, uint32_t resultLen)
43 {
44 return 0;
45 }
46
47 static DbListener g_dbSubscribeMock;
48
49 static CodeSignatureReportedInfo g_reportInfo;
50
51 static int64_t g_timeStamp = 0;
52
53 typedef struct BundleInfo {
54 const char *name;
55 uint32_t tokenId;
56 } BundleInfo;
57
58 static BundleInfo g_bundleInfo[3] = {
59 {"test0", 12345}, // 12345 random tokenId.
60 {"test1", 123456}, // 123456 random tokenId.
61 {"test2", 1234567} // 1234567 random tokenId.
62 };
63
SubscribeDbMock(const int64_t * eventId,uint32_t eventIdLen,DbListener listener)64 static int32_t SubscribeDbMock(const int64_t *eventId, uint32_t eventIdLen, DbListener listener)
65 {
66 g_dbSubscribeMock = listener;
67 return 0;
68 }
69
70 static int32_t g_retListenerMockTime = 0;
71 /**
72 * @tc.name: GetModelApi001
73 * @tc.desc: GetModelApi test.
74 * @tc.type: FUNC
75 * @tc.require:
76 */
77 HWTEST_F(CodeSignatureAnalysisKitTest, GetModelApi001, TestSize.Level1)
78 {
79 ModelApi *test1 = GetModelApi();
80 ASSERT_NE(test1, nullptr);
81 ModelApi *test2 = GetModelApi();
82 ASSERT_EQ(test1, test2);
83 }
84
85
86 /**
87 * @tc.name: Init001
88 * @tc.desc: init with invalid processName.
89 * @tc.type: FUNC
90 * @tc.require:
91 */
92 HWTEST_F(CodeSignatureAnalysisKitTest, Init001, TestSize.Level1)
93 {
94 ModelApi *test = GetModelApi();
95 ASSERT_NE(test, nullptr);
96 ModelManagerApi *testApi = nullptr;
97 int32_t res = test->init(testApi);
98 ASSERT_EQ(res, INPUT_POINT_NULL);
99 }
100
101 /**
102 * @tc.name: Init002
103 * @tc.desc: init test.
104 * @tc.type: FUNC
105 * @tc.require:
106 */
107 HWTEST_F(CodeSignatureAnalysisKitTest, Init002, TestSize.Level1)
108 {
109 ModelApi *test = GetModelApi();
110 ASSERT_NE(test, nullptr);
111
112 ModelManagerApi testApi;
113 testApi.execSql = GetDataFromSqlMock;
114 testApi.subscribeDb = SubscribeDbMock;
115 int32_t res = test->init(&testApi);
116 ASSERT_EQ(res, OPER_SUCCESS);
117 test->release();
118 }
119
120 /**
121 * @tc.name: Init003
122 * @tc.desc: init test.
123 * @tc.type: FUNC
124 * @tc.require:
125 */
126 HWTEST_F(CodeSignatureAnalysisKitTest, Init003, TestSize.Level1)
127 {
128 ModelApi *test = GetModelApi();
129 ASSERT_NE(test, nullptr);
130
131 ModelManagerApi testApi;
132 testApi.execSql = GetDataFromSqlMock;
133 testApi.subscribeDb = SubscribeDbMock;
134 int32_t res = test->init(&testApi);
135 ASSERT_EQ(res, OPER_SUCCESS);
136
137 res = test->init(&testApi);
138 ASSERT_EQ(res, INIT_OPER_REPEAT);
139
140 test->release();
141 res = test->init(&testApi);
142 ASSERT_EQ(res, OPER_SUCCESS);
143 test->release();
144 }
145
146 /**
147 * @tc.name: GetResult001
148 * @tc.desc: GetResult test with null input.
149 * @tc.type: FUNC
150 * @tc.require:
151 */
152 HWTEST_F(CodeSignatureAnalysisKitTest, GetResult001, TestSize.Level1)
153 {
154 ModelApi *test = GetModelApi();
155 ASSERT_NE(test, nullptr);
156
157 ModelManagerApi testApi;
158 testApi.execSql = GetDataFromSqlMock;
159 testApi.subscribeDb = SubscribeDbMock;
160 int32_t res = test->init(&testApi);
161 ASSERT_EQ(res, OPER_SUCCESS);
162
163 uint32_t len = static_cast<uint32_t>(sizeof(NotifyRiskResultInfo));
164 NotifyRiskResultInfo *result = new (std::nothrow) NotifyRiskResultInfo;
165 ASSERT_NE(result, nullptr);
166
167 res = test->getResult(nullptr, &len);
168 EXPECT_EQ(res, INPUT_POINT_NULL);
169
170 test->release();
171 delete result;
172 }
173
174 /**
175 * @tc.name: GetResult002
176 * @tc.desc: GetResult test without init.
177 * @tc.type: FUNC
178 * @tc.require:
179 */
180 HWTEST_F(CodeSignatureAnalysisKitTest, GetResult002, TestSize.Level1)
181 {
182 ModelApi *test = GetModelApi();
183 ASSERT_NE(test, nullptr);
184
185 uint32_t len = static_cast<uint32_t>(sizeof(NotifyRiskResultInfo));
186 NotifyRiskResultInfo *result = new (std::nothrow) NotifyRiskResultInfo;
187 ASSERT_NE(result, nullptr);
188
189 int32_t res = test->getResult(reinterpret_cast<uint8_t *>(result), &len);
190 EXPECT_EQ(res, MODEL_INIT_NOT_COMPLETED);
191
192 delete result;
193 }
194
ReportErrorEventMock(DataChangeTypeCode optType,const char * bundleName,uint32_t tokenId)195 static void ReportErrorEventMock(DataChangeTypeCode optType, const char* bundleName, uint32_t tokenId)
196 {
197 g_reportInfo.bundleName[0] = '\0';
198 int32_t res = strcpy_s(g_reportInfo.bundleName, MAX_BUNDLE_NAME_LENGTH, bundleName);
199 ASSERT_EQ(res, 0);
200 g_reportInfo.errorType = SIGNATURE_MISSING;
201 g_reportInfo.timeStampMs = g_timeStamp++;
202 g_reportInfo.tokenId = tokenId;
203 g_dbSubscribeMock(optType,
204 reinterpret_cast<uint8_t *>(&g_reportInfo), static_cast<uint32_t>(sizeof(CodeSignatureReportedInfo)));
205 }
206
207 /**
208 * @tc.name: GetResult003
209 * @tc.desc: GetResult test.
210 * @tc.type: FUNC
211 * @tc.require:
212 */
213 HWTEST_F(CodeSignatureAnalysisKitTest, GetResult003, TestSize.Level1)
214 {
215 ModelApi *test = GetModelApi();
216 ASSERT_NE(test, nullptr);
217
218 ModelManagerApi testApi;
219 testApi.execSql = GetDataFromSqlMock;
220 testApi.subscribeDb = SubscribeDbMock;
221 int32_t res = test->init(&testApi);
222 ASSERT_EQ(res, OPER_SUCCESS);
223
224 uint32_t len = static_cast<uint32_t>(sizeof(NotifyRiskResultInfo));
225 NotifyRiskResultInfo *result = new (std::nothrow) NotifyRiskResultInfo;
226 ASSERT_NE(result, nullptr);
227
228 for (int i = 0; i < MAX_CODE_SIGNATURE_ERROR_NUM; i++) {
229 ReportErrorEventMock(EVENT_REPORTED, g_bundleInfo[0].name, g_bundleInfo[0].tokenId);
230 }
231
232 res = test->getResult(reinterpret_cast<uint8_t *>(result), &len);
233 ASSERT_EQ(res, OPER_SUCCESS);
234 ASSERT_EQ(len, 0);
235
236 ReportErrorEventMock(EVENT_REPORTED, g_bundleInfo[0].name, g_bundleInfo[0].tokenId);
237
238 // test the SHORT_OF_MEMORY error.
239 len = 0;
240 res = test->getResult(reinterpret_cast<uint8_t *>(result), &len);
241 EXPECT_EQ(res, SHORT_OF_MEMORY);
242
243 // test to get the right info.
244 len = static_cast<uint32_t>(sizeof(NotifyRiskResultInfo));
245 res = test->getResult(reinterpret_cast<uint8_t *>(result), &len);
246 ASSERT_EQ(res, OPER_SUCCESS);
247 ASSERT_EQ(len, sizeof(NotifyRiskResultInfo));
248 EXPECT_EQ(APPLICATION_RISK_EVENT_ID, result->eventId);
249 EXPECT_EQ(LOG_REPORT, result->policy);
250 EXPECT_EQ(g_bundleInfo[0].tokenId, result->tokenId);
251
252 // when the risk changed, the result is updated.
253 ReportErrorEventMock(OUT_OF_STORAGE_LIFE, g_bundleInfo[0].name, g_bundleInfo[0].tokenId);
254 len = static_cast<uint32_t>(sizeof(NotifyRiskResultInfo));
255 res = test->getResult(reinterpret_cast<uint8_t *>(result), &len);
256 EXPECT_EQ(res, OPER_SUCCESS);
257 EXPECT_EQ(len, 0);
258
259 test->release();
260 delete result;
261 }
262
263 /**
264 * @tc.name: GetResult004
265 * @tc.desc: GetResult test.
266 * @tc.type: FUNC
267 * @tc.require:
268 */
269 HWTEST_F(CodeSignatureAnalysisKitTest, GetResult004, TestSize.Level1)
270 {
271 ModelApi *test = GetModelApi();
272 ASSERT_NE(test, nullptr);
273
274 ModelManagerApi testApi;
275 testApi.execSql = GetDataFromSqlMock;
276 testApi.subscribeDb = SubscribeDbMock;
277 int32_t res = test->init(&testApi);
278 ASSERT_EQ(res, OPER_SUCCESS);
279
280 uint32_t len = static_cast<uint32_t>(sizeof(NotifyRiskResultInfo) * 3); // 3 means three info node.
281 NotifyRiskResultInfo *result = new (std::nothrow) NotifyRiskResultInfo[3]; // 3 means three info node.
282 ASSERT_NE(result, nullptr);
283
284 for (int i = 0; i < MAX_CODE_SIGNATURE_ERROR_NUM + 1; i++) {
285 ReportErrorEventMock(EVENT_REPORTED, g_bundleInfo[0].name, g_bundleInfo[0].tokenId);
286 }
287 uint32_t len1 = len;
288 res = test->getResult(reinterpret_cast<uint8_t *>(result), &len1);
289 ASSERT_EQ(res, OPER_SUCCESS);
290 ASSERT_EQ(len1, sizeof(NotifyRiskResultInfo));
291 EXPECT_EQ(g_bundleInfo[0].tokenId, result[0].tokenId);
292
293 for (int i = 0; i < MAX_CODE_SIGNATURE_ERROR_NUM + 1; i++) {
294 ReportErrorEventMock(EVENT_REPORTED, g_bundleInfo[1].name, g_bundleInfo[1].tokenId);
295 }
296 uint32_t len2 = len;
297 res = test->getResult(reinterpret_cast<uint8_t *>(result), &len2);
298 ASSERT_EQ(res, OPER_SUCCESS);
299 ASSERT_EQ(len2, sizeof(NotifyRiskResultInfo) * 2); // 2 include g_bundleInfo[0] and [1]
300 EXPECT_EQ(g_bundleInfo[0].tokenId, result[1].tokenId);
301 EXPECT_EQ(g_bundleInfo[1].tokenId, result[0].tokenId);
302
303 for (int i = 0; i < MAX_CODE_SIGNATURE_ERROR_NUM + 1; i++) {
304 ReportErrorEventMock(EVENT_REPORTED, g_bundleInfo[2].name, g_bundleInfo[2].tokenId); // 2 means index
305 }
306 uint32_t len3 = len;
307 res = test->getResult(reinterpret_cast<uint8_t *>(result), &len3);
308 ASSERT_EQ(res, OPER_SUCCESS);
309 ASSERT_EQ(len3, len);
310 EXPECT_EQ(g_bundleInfo[0].tokenId, result[2].tokenId);
311 EXPECT_EQ(g_bundleInfo[1].tokenId, result[1].tokenId);
312 EXPECT_EQ(g_bundleInfo[2].tokenId, result[0].tokenId);
313
314 g_timeStamp--;
315 ReportErrorEventMock(OUT_OF_STORAGE_LIFE, g_bundleInfo[2].name, g_bundleInfo[2].tokenId);
316 uint32_t len4 = len;
317 res = test->getResult(reinterpret_cast<uint8_t *>(result), &len4);
318 ASSERT_EQ(res, OPER_SUCCESS);
319 ASSERT_EQ(len4, sizeof(NotifyRiskResultInfo) * 2); // 2 include g_bundleInfo[0] and [1]
320 EXPECT_EQ(g_bundleInfo[0].tokenId, result[1].tokenId);
321 EXPECT_EQ(g_bundleInfo[1].tokenId, result[0].tokenId);
322
323 test->release();
324 delete[] result;
325 }
326
RetListenerMock(uint8_t * result,uint32_t resultLen)327 static void RetListenerMock(uint8_t *result, uint32_t resultLen)
328 {
329 g_retListenerMockTime++;
330 ASSERT_EQ(resultLen, sizeof(NotifyRiskResultInfo));
331 NotifyRiskResultInfo *res = reinterpret_cast<NotifyRiskResultInfo *>(result);
332 ASSERT_EQ(APPLICATION_RISK_EVENT_ID, res->eventId);
333 ASSERT_EQ(g_bundleInfo[1].tokenId, res->tokenId);
334
335 if (g_retListenerMockTime == 1) {
336 ASSERT_EQ(LOG_REPORT, res->policy);
337 } else if (g_retListenerMockTime == 2) { // 2 the second time.
338 ASSERT_EQ(NO_SECURITY_RISK, res->policy);
339 }
340 return;
341 }
342
343 /**
344 * @tc.name: SubscribeResult001
345 * @tc.desc: SubscribeResult test without init.
346 * @tc.type: FUNC
347 * @tc.require:
348 */
349 HWTEST_F(CodeSignatureAnalysisKitTest, SubscribeResult001, TestSize.Level1)
350 {
351 ModelApi *test = GetModelApi();
352 ASSERT_NE(test, nullptr);
353 int32_t res = test->subscribeResult(RetListenerMock);
354 ASSERT_EQ(res, MODEL_INIT_NOT_COMPLETED);
355 }
356
357 /**
358 * @tc.name: SubscribeResult002
359 * @tc.desc: SubscribeResult test with null input.
360 * @tc.type: FUNC
361 * @tc.require:
362 */
363 HWTEST_F(CodeSignatureAnalysisKitTest, SubscribeResult002, TestSize.Level1)
364 {
365 ModelApi *test = GetModelApi();
366 ASSERT_NE(test, nullptr);
367 int32_t res = test->subscribeResult(nullptr);
368 ASSERT_EQ(res, INPUT_POINT_NULL);
369 }
370
371 /**
372 * @tc.name: SubscribeResult003
373 * @tc.desc: SubscribeResult test.
374 * @tc.type: FUNC
375 * @tc.require:
376 */
377 HWTEST_F(CodeSignatureAnalysisKitTest, SubscribeResult003, TestSize.Level1)
378 {
379 ModelApi *test = GetModelApi();
380 ASSERT_NE(test, nullptr);
381
382 ModelManagerApi testApi;
383 testApi.execSql = GetDataFromSqlMock;
384 testApi.subscribeDb = SubscribeDbMock;
385 int32_t res = test->init(&testApi);
386 EXPECT_EQ(res, OPER_SUCCESS);
387 res = test->subscribeResult(RetListenerMock);
388 EXPECT_EQ(res, OPER_SUCCESS);
389
390 g_retListenerMockTime = 0;
391
392 for (int i = 0; i < MAX_CODE_SIGNATURE_ERROR_NUM + 1; i++) {
393 ReportErrorEventMock(EVENT_REPORTED, g_bundleInfo[1].name, g_bundleInfo[1].tokenId);
394 }
395 EXPECT_EQ(g_retListenerMockTime, 1);
396
397 g_timeStamp--;
398 ReportErrorEventMock(OUT_OF_STORAGE_LIFE, g_bundleInfo[1].name, g_bundleInfo[1].tokenId);
399
400 EXPECT_EQ(g_retListenerMockTime, 2);
401 test->release();
402 }
403
404 /**
405 * @tc.name: DataProcess001
406 * @tc.desc: DataProcess test.
407 * @tc.type: FUNC
408 * @tc.require:
409 */
410 HWTEST_F(CodeSignatureAnalysisKitTest, DataProcess001, TestSize.Level1)
411 {
412 ModelManagerApi testApi;
413 testApi.execSql = GetDataFromSqlMock;
414 testApi.subscribeDb = SubscribeDbMock;
415
416 ModelApi *test = GetModelApi();
417 ASSERT_NE(test, nullptr);
418 int32_t res = test->init(&testApi);
419 ASSERT_EQ(res, OPER_SUCCESS);
420
421 uint32_t len = static_cast<uint32_t>(sizeof(NotifyRiskResultInfo));
422 NotifyRiskResultInfo *result = new (std::nothrow) NotifyRiskResultInfo;
423 ASSERT_NE(result, nullptr);
424
425 g_dbSubscribeMock(EVENT_REPORTED, nullptr, 1);
426 g_dbSubscribeMock(EVENT_REPORTED, reinterpret_cast<uint8_t *>(&g_reportInfo), 0);
427 g_reportInfo.tokenId = 0;
428 g_dbSubscribeMock(EVENT_REPORTED,
429 reinterpret_cast<uint8_t *>(&g_reportInfo), static_cast<uint32_t>(sizeof(CodeSignatureReportedInfo)));
430
431 g_reportInfo.tokenId = 1;
432 g_reportInfo.errorType = CODE_SIGNATURE_ERROR_TYPE_BUFF;
433 g_dbSubscribeMock(EVENT_REPORTED,
434 reinterpret_cast<uint8_t *>(&g_reportInfo), static_cast<uint32_t>(sizeof(CodeSignatureReportedInfo)));
435
436 g_reportInfo.tokenId = 1;
437 g_reportInfo.errorType = ABC_FILE_TAMPERED;
438 g_dbSubscribeMock(DATA_CHANGE_TYPE_BUFF,
439 reinterpret_cast<uint8_t *>(&g_reportInfo), static_cast<uint32_t>(sizeof(CodeSignatureReportedInfo)));
440
441 for (int i = 0; i < MAX_CODE_SIGNATURE_ERROR_NUM; i++) {
442 ReportErrorEventMock(EVENT_REPORTED, g_bundleInfo[0].name, g_bundleInfo[0].tokenId);
443 }
444
445 res = test->getResult(reinterpret_cast<uint8_t *>(result), &len);
446 EXPECT_EQ(res, OPER_SUCCESS);
447 EXPECT_EQ(len, 0);
448
449 delete result;
450 test->release();
451 test->release();
452 }