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 <ohos_init.h>
17 #include <stdlib.h>
18 #include <securec.h>
19 #include <unistd.h>
20
21 #include "hctest.h"
22 #include "parameter.h"
23 #include "samgr_lite.h"
24
25 #ifdef INTER_ATTEST_MINI_MODULE
26 #include "devattest_interface.h"
27
28 #define DEVATTEST_SUCCESS 0
29 #endif
30
31 #define UDIDSIZE_LEN 64
32
setUp(void)33 void setUp(void) {}
tearDown(void)34 void tearDown(void) {}
suiteSetUp(void)35 void suiteSetUp(void) { }
suiteTearDown(int num_failures)36 int suiteTearDown(int num_failures) { return num_failures; }
37
38
39 static TestSuiteManager g_testSuiteManager;
40 static BOOL CompareInputType(const char *source, const char *input);
41 static void RunSingleTestCase(CTestCase* cTestCase,
42 const char *caseName, const int32 flag);
43 static int16 g_totalSuitesNum = 0;
44 static int16 g_doneSuitesNum = 0;
RunSingleTestSuite(CTestSuite * testSuite)45 static void RunSingleTestSuite(CTestSuite* testSuite)
46 {
47 if (testSuite == NULL) {
48 return;
49 }
50 int16 size = VECTOR_Size(&(testSuite->test_cases));
51 if (size < 0) {
52 return;
53 }
54 UnityBegin(testSuite->file);
55 int16 i;
56 CTestCase *testCase = (CTestCase *)(VECTOR_At(&(testSuite->test_cases), 0));
57 // when setup failed, skip test suites
58 if (testCase == NULL || !testCase->lite_setup()) {
59 printf("Setup failed, skip this test suite!\n");
60 UnityEnd();
61 return;
62 }
63 for (i = size - 1; i >= 0; i--) {
64 CTestCase *hcCase = (CTestCase *)(VECTOR_At(&(testSuite->test_cases), i));
65 if (hcCase != NULL) {
66 RunSingleTestCase(hcCase, hcCase->case_name, hcCase->flag);
67 }
68 }
69 testCase->lite_teardown();
70 UnityEnd();
71 }
72
GetTestSuite(const char * test_suite)73 static CTestSuite* GetTestSuite(const char *test_suite)
74 {
75 CTestSuite* suite = NULL;
76 TestSuiteManager* testMgr = GetTestMgrInstance();
77 if (testMgr == NULL || test_suite == NULL) {
78 return suite;
79 }
80 int16 size = VECTOR_Size(&(testMgr->test_suites));
81 int16 i;
82 for (i = 0; i < size; i++) {
83 CTestSuite* curSuite = (CTestSuite *)(VECTOR_At(&(testMgr->test_suites), i));
84 if (strcmp(curSuite->suite_name, test_suite) == 0) {
85 suite = curSuite;
86 break;
87 }
88 }
89
90 return suite;
91 }
92
RegisterTestSuite(CTestSuite * testSuite)93 static BOOL RegisterTestSuite(CTestSuite *testSuite)
94 {
95 VECTOR_Add(&(g_testSuiteManager.test_suites), testSuite);
96 g_totalSuitesNum++;
97 return TRUE;
98 }
99
RemoveTestSuite(CTestSuite * testSuite)100 static BOOL RemoveTestSuite(CTestSuite *testSuite)
101 {
102 VECTOR_Swap(&(g_testSuiteManager.test_suites),
103 VECTOR_Find(&(g_testSuiteManager.test_suites), testSuite),
104 testSuite);
105 return TRUE;
106 }
107
AddTestCase(CTestCase * testCase)108 static void AddTestCase(CTestCase *testCase)
109 {
110 if (testCase == NULL) {
111 return;
112 }
113 CTestSuite* suite = GetTestSuite(testCase->suite_name);
114 if (suite == NULL) {
115 CTestSuite suite_object;
116 suite_object.subsystem_name = NULL;
117 suite_object.module_name = NULL;
118 suite_object.suite_name = testCase->suite_name;
119 suite_object.test_cases = VECTOR_Make(NULL, NULL);
120 suite = &suite_object;
121 VECTOR_Add(&(g_testSuiteManager.test_suites), suite);
122 }
123 VECTOR_Add(&(suite->test_cases), testCase);
124 return;
125 }
126
CompareInputType(const char * source,const char * input)127 static BOOL CompareInputType(const char *source, const char *input)
128 {
129 if (strcmp(input, CONST_STRING_SPACE) != 0 && strcmp(input, source) != 0) {
130 return TRUE;
131 }
132 return FALSE;
133 }
134
135 static BOOL g_isBreak = FALSE;
RunSingleTestCase(CTestCase * cTestCase,const char * caseName,const int32 flag)136 static void RunSingleTestCase(CTestCase* cTestCase,
137 const char *caseName, const int32 flag)
138 {
139 if (cTestCase != NULL) {
140 if (CompareInputType(cTestCase->case_name, caseName)
141 || (cTestCase->flag != flag)) {
142 g_isBreak = TRUE;
143 return;
144 }
145 UnityDefaultTestRun(cTestCase->execute_func, cTestCase->case_name, cTestCase->line_num);
146 }
147 }
148
RunSpecialTestSuite(const char * subSystemName,const char * moduleName,const char * suiteName,const char * caseName,int caseLevel)149 static void RunSpecialTestSuite(const char *subSystemName,
150 const char *moduleName,
151 const char *suiteName,
152 const char *caseName,
153 int caseLevel)
154 {
155 int16 i;
156 int16 j;
157 int16 size = VECTOR_Size(&(g_testSuiteManager.test_suites));
158 UNITY_BEGIN();
159 for (i = 0; i < size; i++) {
160 if (g_isBreak) {
161 break;
162 }
163 CTestSuite* curSuite = (CTestSuite *)(VECTOR_At(&(g_testSuiteManager.test_suites), i));
164 if (curSuite != NULL) {
165 if (CompareInputType(curSuite->subsystem_name, subSystemName)
166 || CompareInputType(curSuite->module_name, moduleName)
167 || CompareInputType(curSuite->suite_name, suiteName)) {
168 continue;
169 }
170 for (j = 0; j < VECTOR_Size(&(curSuite->test_cases)); j++) {
171 CTestCase* cTestCase = (CTestCase *)(VECTOR_At(
172 &(curSuite->test_cases), j));
173 RunSingleTestCase(cTestCase, caseName, caseLevel);
174 }
175 }
176 }
177 UNITY_END();
178 }
179
RunTestSuite(const char * suite_name)180 static void RunTestSuite(const char* suite_name)
181 {
182 printf("Start to run test suite:%s\n", suite_name);
183 CTestSuite* curSuite = GetTestSuite(suite_name);
184 if (curSuite != NULL) {
185 g_doneSuitesNum++;
186 int16 times = curSuite->times;
187 int16 i;
188 for (i = 0; i < times; i++) {
189 sleep(1);
190 printf("Run test suite %d times\n", i+1);
191 RunSingleTestSuite(curSuite);
192 }
193 if (g_totalSuitesNum == g_doneSuitesNum) {
194 printf("All the test suites finished!\n");
195 }
196 }
197 }
198
LiteTestPrint(const char * fmt,...)199 void LiteTestPrint(const char *fmt, ...)
200 {
201 va_list ap;
202 va_start(ap, fmt);
203 printf(fmt, ap);
204 va_end(ap);
205 }
206
ObtainProductParams(void)207 void ObtainProductParams(void)
208 {
209 int sdkApiVersion = GetSdkApiVersion();
210 if (sdkApiVersion != 0) {
211 printf("SdkApiVersion = %d\n", sdkApiVersion);
212 }
213
214 int firstApiVersion = GetFirstApiVersion();
215 if (firstApiVersion != 0) {
216 printf("firstApiVersion = %d\n", firstApiVersion);
217 }
218
219 const char *bootloaderVersion = GetBootloaderVersion();
220 if (bootloaderVersion != NULL) {
221 printf("bootloaderVersion = %s\n", bootloaderVersion);
222 }
223
224 const char *incrementalVersion = GetIncrementalVersion();
225 if (incrementalVersion != NULL) {
226 printf("incrementalVersion = %s\n", incrementalVersion);
227 }
228
229 const char *buildType = GetBuildType();
230 if (buildType != NULL) {
231 printf("buildType = %s\n", buildType);
232 }
233
234 const char *buildUser = GetBuildUser();
235 if (buildUser != NULL) {
236 printf("buildUser = %s\n", buildUser);
237 }
238
239 const char *buildHost = GetBuildHost();
240 if (buildHost != NULL) {
241 printf("buildHost = %s\n", buildHost);
242 }
243
244 const char *buildTime = GetBuildTime();
245 if (buildTime != NULL) {
246 printf("buildTime = %s\n", buildTime);
247 }
248
249 const char *abiList = GetAbiList();
250 if (abiList != NULL) {
251 printf("AbiList = %s\n", abiList);
252 }
253
254 }
255
256 #ifdef INTER_ATTEST_MINI_MODULE
ObtainAttestResultParams(void)257 void ObtainAttestResultParams(void)
258 {
259 AttestResultInfo attestResultInfo = { 0 };
260 attestResultInfo.ticket = NULL;
261 int32_t retStatus = GetAttestStatus(&attestResultInfo);
262 if (retStatus != DEVATTEST_SUCCESS) {
263 printf("[CLIENT MAIN] wrong. retStatus:%d\n", retStatus);
264 }
265 printf("authResult = %d\n",attestResultInfo.authResult);
266 printf("softwareResult = %d\n",attestResultInfo.softwareResult);
267 }
268 #endif
269
ObtainSystemParams(void)270 void ObtainSystemParams(void)
271 {
272 printf("******To Obtain Product Params Start******\n");
273 const char *productType = GetDeviceType();
274 if (productType != NULL) {
275 printf("Device Type = %s\n", productType);
276 }
277
278 const char *securityPatchTag = GetSecurityPatchTag();
279 if (securityPatchTag != NULL) {
280 printf("Security Patch = %s\n", securityPatchTag);
281 }
282
283 const char *osName = GetOSFullName();
284 if (osName != NULL) {
285 printf("OsFullName = %s\n", osName);
286 }
287
288 const char *displayVersion = GetDisplayVersion();
289 if (displayVersion != NULL) {
290 printf("DisplayVersion = %s\n", displayVersion);
291 }
292
293 const char *versionId = GetVersionId();
294 if (versionId != NULL) {
295 printf("VersionID = %s\n", versionId);
296 }
297
298 #ifdef INTER_ATTEST_MINI_MODULE
299 ObtainAttestResultParams();
300 #endif
301
302 char udid[UDIDSIZE_LEN + 1] = { 0 };
303 int retUdid = GetDevUdid(udid, UDIDSIZE_LEN + 1);
304 if (retUdid == 0) {
305 printf("DevUdid = %s\n", udid);
306 }
307
308 const char *manuFacture = GetManufacture();
309 if (manuFacture != NULL) {
310 printf("manuFacture = %s\n", manuFacture);
311 }
312
313 const char *productModel = GetProductModel();
314 if (productModel != NULL) {
315 printf("productModel = %s\n", productModel);
316 }
317
318 const char *serial = GetSerial();
319 if (serial != NULL) {
320 printf("serial = %s\n", serial);
321 }
322
323 const char *brand = GetBrand();
324 if (brand != NULL) {
325 printf("brand = %s\n", brand);
326 }
327
328 const char *productSeries = GetProductSeries();
329 if (productSeries != NULL) {
330 printf("productSeries = %s\n", productSeries);
331 }
332
333 const char *softwareModel = GetSoftwareModel();
334 if (softwareModel != NULL) {
335 printf("softwareModel = %s\n", softwareModel);
336 }
337
338 const char *hardWareModel = GetHardwareModel();
339 if (hardWareModel != NULL) {
340 printf("HardwareModel = %s\n", hardWareModel);
341 }
342
343 const char *buildRootHash = GetBuildRootHash();
344 if (buildRootHash != NULL) {
345 printf("BuildRootHash = %s\n", buildRootHash);
346 }
347
348 const char *marketName = GetMarketName();
349 if (marketName != NULL) {
350 printf("marketName = %s\n", marketName);
351 }
352
353 ObtainProductParams();
354
355 printf("******To Obtain Product Params End ******\n");
356 return;
357 }
358
InitTestSuiteMgr(void)359 static void InitTestSuiteMgr(void)
360 {
361 g_testSuiteManager.test_suites = VECTOR_Make(NULL, NULL);
362 g_testSuiteManager.GetTestSuite = GetTestSuite;
363 g_testSuiteManager.RegisterTestSuite = RegisterTestSuite;
364 g_testSuiteManager.AddTestCase = AddTestCase;
365 g_testSuiteManager.RemoveTestSuite = RemoveTestSuite;
366 g_testSuiteManager.RunSpecialTestSuite = RunSpecialTestSuite;
367 g_testSuiteManager.RunTestSuite = RunTestSuite;
368 printf("[%10s] HCTest Framework inited.\n", "HCtest Service");
369 ObtainSystemParams();
370 }
371 CORE_INIT(InitTestSuiteMgr);
GetTestMgrInstance(void)372 TestSuiteManager* GetTestMgrInstance(void)
373 {
374 return &g_testSuiteManager;
375 }
376