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