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