• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /**
2  * Copyright (c) 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 <string.h>
17 #include <stdbool.h>
18 #include "cJSON.h"
19 #include "hctest.h"
20 #include "oem_auth_config.h"
21 #include "oem_auth_result_storage.h"
22 #include "token.h"
23 
24 #define LOG(format, ...)  printf("%s:" format "\r\n", __FUNCTION__, ##__VA_ARGS__)
25 
26 #define TEST_TOKEN1 "TEST_TOKEN1abPA4o838zOuuq9R3HBiG2JoYk4X+FIfyYS5iV5DTFiyGuy84eZlr,\
27 qGPCt5czVOLcquVOs91rfwWT/ZiCCeN7+BXoj6R5ez2NC3JTf5y3wh0kx0twMwmN,0000000000000000,0000"
28 #define TEST_TOKEN2 "TEST_TOKEN2YisBwKb2M3rsytbhJrDlI348Ch0XHIahlG2CaJUTQyPQlAqRThHa0,\
29 MbucBf5K9uFnzJyUSj+1u6Ro1jX4xVM0JP4P7FngyAvro4DCmK1Pjq5btHrtceve,0000000000000000,0100"
30 #define TEST_TICKET "1234156456123134564646631315646465"
31 #define TEST_AUTH_STATUS ".abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ01234567890\
32 sdi73fabcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ01234567890abcdefghijklmnopqrstuvwxyz\
33 09uvuyabcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ01234567890abcdefghijklmnopqrstuvwxyz."
34 
35 #define MAX_SERVER_INFO_LEN  256
36 #define ENCRYPT_TOKEN_LEN 151
37 #define ENCRYPT_TICKET_LEN 64
38 #define STATUS_LEN 320
39 #define PRODUCT_ID_LEN 4
40 #define ACKEY_LEN 48
41 #define PRODUCT_KEY_LEN 32
42 
43 LITE_TEST_SUIT(applications, KitFwkOemApi, KitFwkOemApiTestSuite);
44 
45 bool g_isFirstRun = true;
46 
KitFwkOemApiTestSuiteSetUp(void)47 static BOOL KitFwkOemApiTestSuiteSetUp(void)
48 {
49     LOG("========setup========");
50     bool ret = OEMIsResetFlagExist();
51     if (ret) {
52         LOG("======== this is not the first run of test after flash ========\n");
53         g_isFirstRun = false;
54     } else {
55         LOG("======== this is the first run of test after flash ========\n");
56     }
57     return TRUE;
58 }
59 
KitFwkOemApiTestSuiteTearDown(void)60 static BOOL KitFwkOemApiTestSuiteTearDown(void)
61 {
62     LOG("========tearDown========");
63     int ret = OEMCreateResetFlag();
64     if (ret) {
65         LOG("======== OEMCreateResetFlag failed! ========");
66     } else {
67         LOG("======== OEMCreateResetFlag ok! ========");
68     }
69     return TRUE;
70 }
71 
72 #ifndef TOKEN_PERSIST_TEST
73 
74 /**
75  * @tc.name : TestReadAuthServerInfo
76  * @tc.desc : test case of OEMReadAuthServerInfo api.
77               no assert for this case, comiple ok means test pass.
78  */
79 LITE_TEST_CASE(KitFwkOemApiTestSuite, TestReadAuthServerInfo, Function | MediumTest | Level0)
80 {
81     LOG("----- OEMReadAuthServerInfo api test start -----");
82     char* serverInfo = malloc(MAX_SERVER_INFO_LEN + 1);
83     if (serverInfo == NULL) {
84         LOG("malloc fail, can't do test!");
85         TEST_FAIL();
86         return;
87     }
88     int ret = OEMReadAuthServerInfo(serverInfo, MAX_SERVER_INFO_LEN);
89     if (ret) {
90         LOG("no customized server info.");
91     } else {
92         LOG("customized server info: %s", serverInfo);
93     }
94 }
95 
96 /**
97  * @tc.name : TestLoadKitInfos
98  * @tc.desc : test that string return from OEMLoadKitInfos is a valid json string
99  */
100 LITE_TEST_CASE(KitFwkOemApiTestSuite, TestLoadKitInfos, Function | MediumTest | Level0)
101 {
102     LOG("----- OEMLoadKitInfos api test start -----");
103     char* kitInfoMsg = OEMLoadKitInfos();
104     if (kitInfoMsg == NULL) {
105         LOG("No other kit infos");
106         return;
107     }
108     LOG("kit info: %s", kitInfoMsg);
109     cJSON* root = cJSON_Parse(kitInfoMsg);
110     free(kitInfoMsg);
111 
112     TEST_ASSERT_NOT_NULL_MESSAGE(root, "Invalid json format!");
113 
114     cJSON* kitInfoArray = cJSON_GetObjectItem(root, KIT_INFO_JSON_KEY);
115     TEST_ASSERT_NOT_NULL_MESSAGE(kitInfoArray, "Invalid json format!");
116     cJSON_Delete(root);
117 }
118 
119 /**
120  * @tc.name : TestIsOverTemperatureLimit
121  * @tc.desc : test case of temperature api.
122  */
123 LITE_TEST_CASE(KitFwkOemApiTestSuite, TestIsOverTemperatureLimit, Function | MediumTest | Level0)
124 {
125     LOG("----- temperature api test start -----");
126     bool results = OEMIsOverTemperatureLimit();
127     TEST_ASSERT_EQUAL_INT_MESSAGE(0, results, "OEMIsOverTemperatureLimit should fail in normal state");
128 }
129 
130 /**
131  * @tc.name: TestResetFlagApi
132  * @tc.desc: test case of the reset flag related api: create, check
133  */
134 LITE_TEST_CASE(KitFwkOemApiTestSuite, TestResetFlagApi, Function | MediumTest | Level0)
135 {
136     LOG("----- reset flag api test start -----");
137     if (g_isFirstRun) {
138         int ret = OEMCreateResetFlag();
139         TEST_ASSERT_EQUAL_INT_MESSAGE(0, ret, "Failed to create the reset flag");
140         bool flag = OEMIsResetFlagExist();
141         TEST_ASSERT_EQUAL_INT_MESSAGE(1, flag, "the reset flag should exist.");
142     } else {
143         bool flag = OEMIsResetFlagExist();
144         TEST_ASSERT_EQUAL_INT_MESSAGE(1, flag, "the reset flag should exist.");
145         int ret = OEMDeleteResetFlag();
146         TEST_ASSERT_EQUAL_INT_MESSAGE(0, ret, "Failed to delete the Reset flag");
147         flag = OEMIsResetFlagExist();
148         TEST_ASSERT_EQUAL_INT_MESSAGE(0, flag, "the reset flag should not exist.");
149     }
150 }
151 
152 /**
153  * @tc.name: TestAuthStatusApi
154  * @tc.desc: test case of the authStatus related api: read, write, delete, check
155  */
156 LITE_TEST_CASE(KitFwkOemApiTestSuite, TestAuthStatusApi, Function | MediumTest | Level0)
157 {
158     LOG("----- auth status api test start -----");
159     uint32_t len = strlen(TEST_AUTH_STATUS);
160     char* statusRead = malloc(len + 1);
161     if (statusRead == NULL) {
162         LOG("malloc fail, can't do test!");
163         TEST_FAIL();
164         return;
165     }
166     if (g_isFirstRun) {
167         int ret = OEMWriteAuthStatus(TEST_AUTH_STATUS, len);
168         TEST_ASSERT_EQUAL_INT_MESSAGE(0, ret, "Failed to write auth data");
169         bool isExist = OEMIsAuthStatusExist();
170         TEST_ASSERT_EQUAL_INT_MESSAGE(1, isExist, "authStatus should exist!");
171         ret = OEMReadAuthStatus(statusRead, len);
172         LOG("statusRead:%s", statusRead);
173         TEST_ASSERT_EQUAL_INT_MESSAGE(0, ret, "Failed to read authStatus");
174         statusRead[len] = 0;
175         TEST_ASSERT_EQUAL_STRING_MESSAGE(TEST_AUTH_STATUS, statusRead, "authStatus not match!");
176         uint32_t newLen = 0;
177         ret = OEMGetAuthStatusFileSize(&newLen);
178         TEST_ASSERT_EQUAL_INT_MESSAGE(0, ret, "failed to get authStatus size");
179         // Check whether the obtained length is the same as the initial written length.
180         TEST_ASSERT_EQUAL_INT_MESSAGE(len, newLen, "Failed: length not match!");
181     } else {
182         bool isExist = OEMIsAuthStatusExist();
183         TEST_ASSERT_EQUAL_INT_MESSAGE(1, isExist, "authStatus should exist!");
184         int ret = OEMReadAuthStatus(statusRead, len);
185         LOG("statusRead:%s", statusRead);
186         TEST_ASSERT_EQUAL_INT_MESSAGE(0, ret, "Failed to read auth status");
187         statusRead[len] = 0;
188         TEST_ASSERT_EQUAL_STRING_MESSAGE(TEST_AUTH_STATUS, statusRead, "authStatus not match!");
189         uint32_t newLen = 0;
190         ret = OEMGetAuthStatusFileSize(&newLen);
191         TEST_ASSERT_EQUAL_INT_MESSAGE(0, ret, "failed to get authStatus size");
192         TEST_ASSERT_EQUAL_INT_MESSAGE(len, newLen, "Failed: length not match!");
193 
194         ret = OEMDeleteAuthStatus();
195         TEST_ASSERT_EQUAL_INT_MESSAGE(0, ret, "failed to delete authStatus");
196         isExist = OEMIsAuthStatusExist();
197         TEST_ASSERT_EQUAL_INT_MESSAGE(0, isExist, "file still exist!");
198     }
199     free(statusRead);
200 }
201 
202 /**
203  * @tc.name: TestTicketApi
204  * @tc.desc: test case of the ticket related api: read, write, delete, check
205  */
206 LITE_TEST_CASE(KitFwkOemApiTestSuite, TestTicketApi, Function | MediumTest | Level0)
207 {
208     LOG("----- ticket api test start -----");
209     uint32_t len = strlen(TEST_TICKET);
210     char* ticketRead = malloc(len + 1);
211     if (ticketRead == NULL) {
212         LOG("malloc fail, can't do test!");
213         TEST_FAIL();
214         return;
215     }
216     if (g_isFirstRun) {
217         int ret = OEMWriteTicket(TEST_TICKET, len);
218         TEST_ASSERT_EQUAL_INT_MESSAGE(0, ret, "Failed to write ticket");
219         bool isExist = OEMIsTicketExist();
220         TEST_ASSERT_EQUAL_INT_MESSAGE(1, isExist, "ticket should exist!");
221         ret = OEMReadTicket(ticketRead, len);
222         LOG("ticketRead:%s", ticketRead);
223         TEST_ASSERT_EQUAL_INT_MESSAGE(0, ret, "Failed to tead ticket");
224         ticketRead[len] = 0;
225         TEST_ASSERT_EQUAL_STRING_MESSAGE(TEST_TICKET, ticketRead, "ticket not match");
226         uint32_t newLen = 0;
227         ret = OEMGetTicketFileSize(&newLen);
228         TEST_ASSERT_EQUAL_INT_MESSAGE(0, ret, "Failed to get ticket size");
229         TEST_ASSERT_EQUAL_INT_MESSAGE(len, newLen, "length not match!");
230     } else {
231         bool isExist = OEMIsTicketExist();
232         TEST_ASSERT_EQUAL_INT_MESSAGE(1, isExist, "ticket not does exist!");
233         int ret = OEMReadTicket(ticketRead, len);
234         LOG("ticketRead:%s", ticketRead);
235         TEST_ASSERT_EQUAL_INT_MESSAGE(0, ret, "Failed to read ticket");
236         ticketRead[len] = 0;
237         TEST_ASSERT_EQUAL_STRING_MESSAGE(TEST_TICKET, ticketRead, "ticket not match");
238         uint32_t newLen = 0;
239         ret = OEMGetTicketFileSize(&newLen);
240         TEST_ASSERT_EQUAL_INT_MESSAGE(0, ret, "failed to get ticket size");
241         TEST_ASSERT_EQUAL_INT_MESSAGE(len, newLen, "length not match!");
242 
243         ret = OEMDeleteTicket();
244         TEST_ASSERT_EQUAL_INT_MESSAGE(0, ret, "Failed to delete ticket");
245         isExist = OEMIsTicketExist();
246         TEST_ASSERT_EQUAL_INT_MESSAGE(0, isExist, "file still exist!");
247     }
248     free(ticketRead);
249 }
250 
251 /**
252  * @tc.name: TestTokenApi
253  * @tc.desc: test case of the token related api: read, write
254  */
255 LITE_TEST_CASE(KitFwkOemApiTestSuite, TestTokenApi, Function | MediumTest | Level0)
256 {
257     LOG("----- token api test start -----");
258     uint32_t len = strlen(TEST_TOKEN1);
259     char* tokenRead = malloc(len + 1);
260     if (tokenRead == NULL) {
261         LOG("malloc fail, can't do test!");
262         TEST_FAIL();
263         return;
264     }
265     if (g_isFirstRun) {
266         int ret = ReadToken(tokenRead, len);
267         if (!ret) {
268             // print old token if existed, in case it's a valid 'true-token'
269             LOG("old token in device:%s", tokenRead);
270         }
271         // write then read token check
272         ret = WriteToken(TEST_TOKEN1, len);
273         TEST_ASSERT_EQUAL_INT_MESSAGE(0, ret, "Failed to write token");
274         ret = ReadToken(tokenRead, len);
275         TEST_ASSERT_EQUAL_INT_MESSAGE(0, ret, "Failed to read token");
276         tokenRead[len] = 0;
277         TEST_ASSERT_EQUAL_STRING_MESSAGE(TEST_TOKEN1, tokenRead, "token not match");
278 
279         // write then read token check, again. for A-B area check
280         ret = WriteToken(TEST_TOKEN2, len);
281         TEST_ASSERT_EQUAL_INT_MESSAGE(0, ret, "Failed to write token");
282         ret = ReadToken(tokenRead, len);
283         TEST_ASSERT_EQUAL_INT_MESSAGE(0, ret, "Failed to read token");
284         tokenRead[len] = 0;
285         TEST_ASSERT_EQUAL_STRING_MESSAGE(TEST_TOKEN2, tokenRead, "token not match");
286     } else {
287         int ret = ReadToken(tokenRead, len);
288         TEST_ASSERT_EQUAL_INT_MESSAGE(0, ret, "Failed to read token");
289         tokenRead[len] = 0;
290         TEST_ASSERT_EQUAL_STRING_MESSAGE(TEST_TOKEN2, tokenRead, "token not match");
291     }
292     free(tokenRead);
293 }
294 
295 /**
296  * @tc.name: TestGetAckeyApi
297  * @tc.desc: Check whether the Ackey can be obtained.
298  */
299 LITE_TEST_CASE(KitFwkOemApiTestSuite, TestGetAckeyApi, Function | MediumTest | Level0)
300 {
301     LOG("----- test get ackey start -----");
302     char acKey[ACKEY_LEN];
303     unsigned int len = sizeof(acKey);
304     int ret = GetAcKey(acKey, len);
305     TEST_ASSERT_EQUAL_INT_MESSAGE(0, ret, "Failed to get the ackey");
306 }
307 
308 /**
309  * @tc.name: TestGetProdIdApi
310  * @tc.desc: Check whether the ProdId can be obtained.
311  */
312 LITE_TEST_CASE(KitFwkOemApiTestSuite, TestGetProdIdApi, Function | MediumTest | Level0)
313 {
314     LOG("----- test getprodid start -----");
315     char productId[PRODUCT_ID_LEN];
316     int ret = GetProdId(productId, sizeof(productId));
317     TEST_ASSERT_EQUAL_INT_MESSAGE(0, ret, "Failed to get the productid");
318 }
319 
320 /**
321  * @tc.name: TestGetProdKeyApi
322  * @tc.desc: Check whether the Prodkey can be obtained.
323  */
324 LITE_TEST_CASE(KitFwkOemApiTestSuite, TestGetProdKeyApi, Function | MediumTest | Level0)
325 {
326     LOG("----- test getproductkey start -----");
327     char productKey[PRODUCT_KEY_LEN];
328     int ret = GetProdKey(productKey, sizeof(productKey));
329     TEST_ASSERT_EQUAL_INT_MESSAGE(0, ret, "Failed to get the productkey");
330 }
331 
332 #else  // defined TOKEN_PERSIST_TEST
333 
334 /**
335  * @tc.name: TestIfTokenPersist
336  * @tc.desc: test case of the token related api: read, write
337  */
338 LITE_TEST_CASE(KitFwkOemApiTestSuite, TestIfTokenPersist, Function | MediumTest | Level0)
339 {
340     LOG("----- token api test start -----");
341     uint32_t len = strlen(TEST_TOKEN2);
342     char* tokenRead = malloc(len + 1);
343     if (tokenRead == NULL) {
344         LOG("malloc fail, can't do test!");
345         TEST_FAIL();
346         return;
347     }
348     int ret = ReadToken(tokenRead, len);
349     TEST_ASSERT_EQUAL_INT_MESSAGE(0, ret, "Failed to read token");
350     tokenRead[len] = 0;
351     LOG("tokenRead:%s", tokenRead);
352     TEST_ASSERT_EQUAL_STRING_MESSAGE(TEST_TOKEN2, tokenRead, "token not match");
353 
354     free(tokenRead);
355 }
356 #endif
357 
358 RUN_TEST_SUITE(KitFwkOemApiTestSuite);
359