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