• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022 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 "dslm_baselib_utils_test.h"
17 
18 #include <gtest/gtest.h>
19 
20 #include "file_ex.h"
21 #include "nativetoken_kit.h"
22 #include "securec.h"
23 #include "token_setproc.h"
24 
25 #include "utils_base64.h"
26 #include "utils_datetime.h"
27 #include "utils_hexstring.h"
28 #include "utils_json.h"
29 #include "utils_mem.h"
30 #include "utils_state_machine.h"
31 #include "utils_timer.h"
32 #include "utils_tlv.h"
33 
34 #define MAX_ENTRY 8
35 #define MAX_MALLOC_LEN (1 * 1024 * 1024)
36 
37 using namespace std;
38 using namespace std::chrono;
39 using namespace testing;
40 using namespace testing::ext;
41 
42 // for testing
43 extern "C" {
44 extern void DoTimerProcess(TimerProc callback, const void *context);
45 }
46 
47 namespace OHOS {
48 namespace Security {
49 namespace DslmUnitTest {
SetUpTestCase()50 void DslmBaselibUtilsTest::SetUpTestCase()
51 {
52 }
TearDownTestCase()53 void DslmBaselibUtilsTest::TearDownTestCase()
54 {
55 }
SetUp()56 void DslmBaselibUtilsTest::SetUp()
57 {
58 }
TearDown()59 void DslmBaselibUtilsTest::TearDown()
60 {
61 }
62 
63 /**
64  * @tc.name: Deserialize_case1
65  * @tc.desc: function Deserialize with malformed input
66  * @tc.type: FUNC
67  * @tc.require: issueNumber
68  */
69 HWTEST_F(DslmBaselibUtilsTest, Deserialize_case1, TestSize.Level0)
70 {
71     uint32_t cnt = 0;
72     TlvCommon tlvs[MAX_ENTRY];
73     // every entry has a sizeof(void *)-byte value
74     uint8_t buff[MAX_ENTRY * sizeof(TlvCommon)] = {0};
75 
76     {
77         cnt = 0;
78         (void)memset_s(&buff[0], sizeof(buff), 0, sizeof(buff));
79         (void)memset_s(&tlvs[0], sizeof(tlvs), 0, sizeof(tlvs));
80 
81         uint32_t ret = Deserialize(nullptr, sizeof(buff), &tlvs[0], MAX_ENTRY, &cnt);
82         EXPECT_EQ((uint32_t)TLV_ERR_INVALID_PARA, ret);
83     }
84 
85     {
86         // buff contains 10 entries which greater than MAX_ENTRY
87         int i;
88         // every entry has a sizeof(void *)-byte value
89         uint8_t buff[(MAX_ENTRY + 2) * sizeof(TlvCommon)] = {0};
90 
91         cnt = 0;
92         (void)memset_s(&tlvs[0], sizeof(tlvs), 0, sizeof(tlvs));
93 
94         for (i = 0; i < (MAX_ENTRY + 2); i++) {
95             TlvCommon *ptr = (TlvCommon *)buff + i;
96             ptr->tag = 0x105;
97             ptr->len = 4;
98         }
99 
100         uint32_t ret = Deserialize(buff, sizeof(buff), &tlvs[0], MAX_ENTRY, &cnt);
101         EXPECT_EQ((uint32_t)TLV_ERR_BUFF_NO_ENOUGH, ret);
102         EXPECT_EQ(0U, cnt);
103     }
104 }
105 
106 /**
107  * @tc.name: Deserialize_case2
108  * @tc.desc: function Deserialize with malformed input
109  * @tc.type: FUNC
110  * @tc.require: issueNumber
111  */
112 HWTEST_F(DslmBaselibUtilsTest, Deserialize_case2, TestSize.Level0)
113 {
114     int i;
115     uint32_t cnt = 0;
116     TlvCommon tlvs[MAX_ENTRY];
117     // every entry has a sizeof(void *)-byte value
118     uint8_t buff[MAX_ENTRY * sizeof(TlvCommon)] = {0};
119 
120     // malformed tlv entry's len
121     cnt = 0;
122     (void)memset_s(&buff[0], sizeof(buff), 0, sizeof(buff));
123     (void)memset_s(&tlvs[0], sizeof(tlvs), 0, sizeof(tlvs));
124 
125     for (i = 0; i < MAX_ENTRY; i++) {
126         TlvCommon *ptr = (TlvCommon *)buff + i;
127         ptr->tag = 0x105;
128         ptr->len = 0x100;
129     }
130 
131     uint32_t ret = Deserialize(buff, sizeof(buff), &tlvs[0], MAX_ENTRY, &cnt);
132     EXPECT_EQ(0U, ret);
133     EXPECT_EQ(0U, cnt);
134 }
135 
136 /**
137  * @tc.name: Serialize_case1
138  * @tc.desc: function Serialize with malformed input
139  * @tc.type: FUNC
140  * @tc.require: issueNumber
141  */
142 HWTEST_F(DslmBaselibUtilsTest, Serialize_case1, TestSize.Level0)
143 {
144     uint32_t size = 0;
145     int i = 0;
146     TlvCommon tlvs[MAX_ENTRY];
147     // every entry has a sizeof(void *)-byte value
148     uint8_t buff[MAX_ENTRY * sizeof(TlvCommon)] = {0};
149 
150     {
151         size = 0;
152         uint32_t ret = Serialize(nullptr, MAX_ENTRY, buff, sizeof(buff), &size);
153         EXPECT_EQ((uint32_t)TLV_ERR_INVALID_PARA, ret);
154     }
155 
156     {
157         // malformed max buffer size
158 
159         size = 0;
160         (void)memset_s(&buff[0], sizeof(buff), 0, sizeof(buff));
161         (void)memset_s(&tlvs[0], sizeof(tlvs), 0, sizeof(tlvs));
162 
163         for (i = 0; i < MAX_ENTRY; i++) {
164             TlvCommon *ptr = (TlvCommon *)tlvs + i;
165             ptr->tag = 0x105;
166             ptr->len = 4;
167         }
168 
169         uint32_t ret = Serialize(tlvs, MAX_ENTRY, buff, 1, &size);
170         EXPECT_EQ((uint32_t)TLV_ERR_BUFF_NO_ENOUGH, ret);
171         EXPECT_EQ(0U, size);
172     }
173 }
174 
175 /**
176  * @tc.name: Serialize_case1
177  * @tc.desc: function Serialize with malformed input
178  * @tc.type: FUNC
179  * @tc.require: issueNumber
180  */
181 HWTEST_F(DslmBaselibUtilsTest, Serialize_case2, TestSize.Level0)
182 {
183     uint32_t size = 0;
184     int i = 0;
185     TlvCommon tlvs[MAX_ENTRY];
186     // every entry has a sizeof(void *)-byte value
187     uint8_t buff[MAX_ENTRY * sizeof(TlvCommon)] = {0};
188     {
189         // malformed tvl.len
190         size = 0;
191         (void)memset_s(&buff[0], sizeof(buff), 0, sizeof(buff));
192         (void)memset_s(&tlvs[0], sizeof(tlvs), 0, sizeof(tlvs));
193 
194         for (i = 0; i < MAX_ENTRY; i++) {
195             TlvCommon *ptr = (TlvCommon *)tlvs + i;
196             ptr->tag = 0x105;
197             ptr->len = 0;
198         }
199 
200         uint32_t ret = Serialize(tlvs, MAX_ENTRY, buff, sizeof(buff), &size);
201         EXPECT_EQ(0U, ret);
202         // (TLV_TLV_HEAD_LEN + ptr->len) * MAX_ENTRY
203         EXPECT_EQ(32U, size);
204     }
205 
206     {
207         // malformed tvl.value
208         size = 0;
209         (void)memset_s(&buff[0], sizeof(buff), 0, sizeof(buff));
210         (void)memset_s(&tlvs[0], sizeof(tlvs), 0, sizeof(tlvs));
211 
212         for (i = 0; i < MAX_ENTRY; i++) {
213             TlvCommon *ptr = (TlvCommon *)tlvs + i;
214             ptr->tag = 0x105;
215             ptr->len = 4;
216             ptr->value = nullptr;
217         }
218 
219         uint32_t ret = Serialize(tlvs, MAX_ENTRY, buff, sizeof(buff), &size);
220         EXPECT_EQ(0U, ret);
221         // (TLV_TLV_HEAD_LEN + ptr->len) * MAX_ENTRY
222         EXPECT_EQ(64U, size);
223     }
224 }
225 
226 /**
227  * @tc.name: GetDateTimeByMillisecondSince1970_case1
228  * @tc.desc: function GetDateTimeByMillisecondSince1970 with malformed input
229  * @tc.type: FUNC
230  * @tc.require: issueNumber
231  */
232 HWTEST_F(DslmBaselibUtilsTest, GetDateTimeByMillisecondSince1970_case1, TestSize.Level0)
233 {
234     uint64_t input = 0;
235 
236     {
237         EXPECT_EQ(false, GetDateTimeByMillisecondSince1970(input, nullptr));
238         EXPECT_EQ(false, GetDateTimeByMillisecondSinceBoot(input, nullptr));
239     }
240 
241     {
242         DoTimerProcess(nullptr, nullptr);
243     }
244 }
245 
246 /**
247  * @tc.name: Base64UrlDecodeApp_case1
248  * @tc.desc: function Base64UrlDecodeApp with malformed input
249  * @tc.type: FUNC
250  * @tc.require: issueNumber
251  */
252 HWTEST_F(DslmBaselibUtilsTest, Base64UrlDecodeApp_case1, TestSize.Level0)
253 {
254     uint8_t src[] = {'a', '-', '_', 'd', '\0'};
255     uint8_t *to = nullptr;
256 
257     {
258         int32_t ret = Base64UrlDecodeApp(nullptr, &to);
259         EXPECT_EQ(0, ret);
260     }
261 
262     {
263         int32_t ret = Base64UrlDecodeApp(src, &to);
264         EXPECT_EQ(3, ret);
265         EXPECT_EQ('\xEF', to[1]);
266         EXPECT_EQ('\xDD', to[2]);
267         FREE(to);
268     }
269 }
270 
271 /**
272  * @tc.name: Base64DecodeApp_case1
273  * @tc.desc: function Base64DecodeApp with malformed input
274  * @tc.type: FUNC
275  * @tc.require: issueNumber
276  */
277 HWTEST_F(DslmBaselibUtilsTest, Base64DecodeApp_case1, TestSize.Level0)
278 {
279     uint8_t *to = nullptr;
280 
281     {
282         int32_t ret = Base64DecodeApp(nullptr, &to);
283         EXPECT_EQ(0, ret);
284     }
285 
286     {
287         uint32_t maxStrLen = MAX_MALLOC_LEN / 3 * 4 + 10;
288         uint8_t *maxStr = (uint8_t *)MALLOC(sizeof(uint8_t) * maxStrLen);
289         ASSERT_NE(nullptr, maxStr);
290 
291         memset_s(maxStr, maxStrLen - 1, 'c', maxStrLen - 1);
292 
293         int32_t ret = Base64DecodeApp(maxStr, &to);
294         EXPECT_EQ(0, ret);
295         FREE(maxStr);
296     }
297 }
298 
299 /**
300  * @tc.name: Base64EncodeApp_case1
301  * @tc.desc: function Base64EncodeApp with malformed input
302  * @tc.type: FUNC
303  * @tc.require: issueNumber
304  */
305 HWTEST_F(DslmBaselibUtilsTest, Base64EncodeApp_case1, TestSize.Level0)
306 {
307     uint8_t src[] = {'a', 'b', 'c', 'd', '\0'};
308 
309     EXPECT_EQ(nullptr, Base64EncodeApp(nullptr, sizeof(src)));
310     EXPECT_EQ(nullptr, Base64EncodeApp(src, 0));
311 
312     {
313         uint32_t maxStrLen = MAX_MALLOC_LEN / 4 * 3;
314         EXPECT_EQ(nullptr, Base64EncodeApp(src, maxStrLen));
315     }
316 }
317 
318 /**
319  * @tc.name: InitStateMachine_case1
320  * @tc.desc: function InitStateMachine with malformed input
321  * @tc.type: FUNC
322  * @tc.require: issueNumber
323  */
324 HWTEST_F(DslmBaselibUtilsTest, InitStateMachine_case1, TestSize.Level0)
325 {
326     {
327         StateMachine *machine = nullptr;
328         uint32_t machineId = 0;
329         uint32_t initState = 0;
330 
331         InitStateMachine(machine, machineId, initState);
332     }
333 
334     {
335         StateNode *node = nullptr;
336         uint32_t nodeCnt = 0;
337         StateMachine machine;
338         uint32_t event = 0;
339 
340         ScheduleMachine(node, nodeCnt, &machine, event, nullptr);
341     }
342 }
343 
344 /**
345  * @tc.name: DestroyJson_case1
346  * @tc.desc: function DestroyJson with malformed input
347  * @tc.type: FUNC
348  * @tc.require: issueNumber
349  */
350 HWTEST_F(DslmBaselibUtilsTest, DestroyJson_case1, TestSize.Level0)
351 {
352     int32_t arr[10] = {0};
353     const char *field = "test";
354     const char *value = "add";
355 
356     const char *str = "{\"version\":131072,\"challenge\":\"3C1F21EE53D3C4E2\",\"type\":2}";
357     JsonHandle handle = CreateJson(str);
358 
359     DestroyJson(nullptr);
360     AddFieldBoolToJson(nullptr, field, true);
361     AddFieldIntToJson(nullptr, field, 0);
362     AddFieldIntArrayToJson(nullptr, field, arr, sizeof(arr));
363     AddFieldStringToJson(nullptr, field, value);
364     AddFieldJsonToJson(handle, field, nullptr);
365 
366     {
367         int32_t ret = GetJsonFieldInt(nullptr, str);
368         EXPECT_EQ(-1, ret);
369 
370         ret = GetJsonFieldInt(handle, nullptr);
371         EXPECT_EQ(0, ret);
372     }
373 
374     {
375         uint32_t ret = GetJsonFieldIntArray(nullptr, str, arr, sizeof(arr));
376         EXPECT_EQ(0U, ret);
377     }
378 
379     {
380         const char *ret = GetJsonFieldString(nullptr, str);
381         EXPECT_EQ(nullptr, ret);
382     }
383 
384     {
385         const char *ret = GetJsonFieldString(handle, nullptr);
386         EXPECT_EQ(nullptr, ret);
387     }
388 
389     EXPECT_EQ(false, CompareJsonData(handle, nullptr, true));
390 
391     FREE(handle);
392 }
393 
394 /**
395  * @tc.name: GetJsonFieldInt_case1
396  * @tc.desc: function GetJsonFieldInt with malformed input
397  * @tc.type: FUNC
398  * @tc.require: issueNumber
399  */
400 HWTEST_F(DslmBaselibUtilsTest, GetJsonFieldInt_case1, TestSize.Level0)
401 {
402     const char *str = "{\"version\":131072,\"challenge\":\"3C1F21EE53D3C4E2\",\"type\":2}";
403     JsonHandle handle = CreateJson(str);
404 
405     int32_t ret = GetJsonFieldInt(handle, "challenge");
406     EXPECT_EQ(-1, ret);
407     FREE(handle);
408 }
409 
410 /**
411  * @tc.name: GetJsonFieldIntArray_case1
412  * @tc.desc: function GetJsonFieldIntArray with malformed input
413  * @tc.type: FUNC
414  * @tc.require: issueNumber
415  */
416 HWTEST_F(DslmBaselibUtilsTest, GetJsonFieldIntArray_case1, TestSize.Level0)
417 {
418     int32_t arr[5] = {0};
419     int32_t arrLen = sizeof(arr) / sizeof(arr[0]);
420     const char *str = "{\"version\":131072,\"challenge\":\"test challenge\",\"arr\":[\"3C1F21EE53D3C4E2\", \"elem2\", "
421                       "3, 4, 5],\"type\":2}";
422     JsonHandle handle = CreateJson(str);
423 
424     {
425         int32_t ret = GetJsonFieldIntArray(handle, "challenge", arr, arrLen);
426         EXPECT_EQ(0U, ret);
427     }
428 
429     {
430         int32_t ret = GetJsonFieldIntArray(handle, "arr", arr, arrLen - 2);
431         EXPECT_EQ(1U, ret);
432     }
433 
434     FREE(handle);
435 }
436 
437 /**
438  * @tc.name: ByteToHexString_case1
439  * @tc.desc: function ByteToHexString with malformed input
440  * @tc.type: FUNC
441  * @tc.require: issueNumber
442  */
443 HWTEST_F(DslmBaselibUtilsTest, ByteToHexString_case1, TestSize.Level0)
444 {
445     const uint8_t hex[] = {0x1, 0xF, 0xE, 0x8, 0xA};
446     uint32_t hexLen = sizeof(hex);
447     uint8_t str[10] = {0};
448     uint32_t strLen = 10;
449 
450     ByteToHexString(nullptr, 0, str, strLen);
451     EXPECT_EQ(0U, str[0]);
452 
453     ByteToHexString(hex, hexLen, nullptr, 0);
454     EXPECT_EQ(0U, str[0]);
455 
456     ByteToHexString(hex, hexLen, str, strLen - 1);
457     EXPECT_EQ(0U, str[0]);
458 }
459 
460 /**
461  * @tc.name: HexStringToByte_case1
462  * @tc.desc: function HexStringToByte with malformed input
463  * @tc.type: FUNC
464  * @tc.require: issueNumber
465  */
466 HWTEST_F(DslmBaselibUtilsTest, HexStringToByte_case1, TestSize.Level0)
467 {
468     uint32_t hexLen = 5;
469     uint32_t strLen = 10;
470     uint8_t hex[5] = {0};
471     char str[10] = {0};
472 
473     {
474         int32_t ret = HexStringToByte(nullptr, 0, hex, hexLen);
475         EXPECT_EQ(-1, ret);
476         EXPECT_EQ(0U, str[0]);
477     }
478 
479     {
480         int32_t ret = HexStringToByte(str, strLen, nullptr, 0);
481         EXPECT_EQ(-1, ret);
482         EXPECT_EQ(0U, str[0]);
483     }
484 
485     {
486         int32_t ret = HexStringToByte(str, strLen, hex, hexLen - 2);
487         EXPECT_EQ(-1, ret);
488         EXPECT_EQ(0U, str[0]);
489     }
490 }
491 } // namespace DslmUnitTest
492 } // namespace Security
493 } // namespace OHOS