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 #include "gtest/gtest.h"
16 #include <securec.h>
17
18 #include "softbus_adapter_crypto.h"
19 #include "softbus_error_code.h"
20
21 using namespace std;
22 using namespace testing::ext;
23
24 namespace OHOS {
25 class AdaptorDsoftbusCryptTest : public testing::Test {
26 protected:
27 static void SetUpTestCase(void);
28 static void TearDownTestCase(void);
29 void SetUp();
30 void TearDown();
31 static unsigned char encodeStr[100];
32 static size_t encodeLen;
33 };
SetUpTestCase(void)34 void AdaptorDsoftbusCryptTest::SetUpTestCase(void) { }
TearDownTestCase(void)35 void AdaptorDsoftbusCryptTest::TearDownTestCase(void) { }
SetUp()36 void AdaptorDsoftbusCryptTest::SetUp() { }
TearDown()37 void AdaptorDsoftbusCryptTest::TearDown() { }
38 unsigned char AdaptorDsoftbusCryptTest::encodeStr[100];
39 size_t AdaptorDsoftbusCryptTest::encodeLen;
40 /*
41 * @tc.name: SoftBusBase64Encode001
42 * @tc.desc: parameters is Legal
43 * @tc.type: FUNC
44 * @tc.require: I5OHDE
45 */
46 HWTEST_F(AdaptorDsoftbusCryptTest, SoftBusBase64Encode001, TestSize.Level0)
47 {
48 char dst[100];
49 char src[] = "abcde";
50 size_t olen = 10;
51 int32_t ret = SoftBusBase64Encode((unsigned char *)dst, sizeof(dst), &olen, (unsigned char *)src, sizeof(src));
52 EXPECT_EQ(SOFTBUS_OK, ret);
53
54 encodeLen = olen;
55 memcpy_s(encodeStr, olen, dst, olen);
56 }
57
58 /*
59 * @tc.name: SoftBusBase64Encode002
60 * @tc.desc: parameter is nullptr
61 * @tc.type: FUNC
62 * @tc.require: I5OHDE
63 */
64 HWTEST_F(AdaptorDsoftbusCryptTest, SoftBusBase64Encode002, TestSize.Level0)
65 {
66 char dst[100];
67 char src[] = "abcde";
68 size_t olen = 10;
69 int32_t ret = SoftBusBase64Encode(nullptr, sizeof(dst), &olen, (unsigned char *)src, sizeof(src));
70 EXPECT_EQ(SOFTBUS_INVALID_PARAM, ret);
71
72 ret = SoftBusBase64Encode((unsigned char *)dst, sizeof(dst), nullptr, (unsigned char *)src, sizeof(src));
73 EXPECT_EQ(SOFTBUS_INVALID_PARAM, ret);
74
75 ret = SoftBusBase64Encode((unsigned char *)dst, sizeof(dst), &olen, nullptr, sizeof(src));
76 EXPECT_EQ(SOFTBUS_INVALID_PARAM, ret);
77 }
78
79 /*
80 * @tc.name: SoftBusBase64Encode003
81 * @tc.desc: dlen and slen is illegal
82 * @tc.type: FUNC
83 * @tc.require: I5OHDE
84 */
85 HWTEST_F(AdaptorDsoftbusCryptTest, SoftBusBase64Encode003, TestSize.Level0)
86 {
87 char dst[100];
88 char src[] = "abcde";
89 size_t olen = 10;
90 size_t dlen = 0;
91 int32_t ret = SoftBusBase64Encode((unsigned char *)dst, dlen, &olen, (unsigned char *)src, sizeof(src));
92 EXPECT_EQ(SOFTBUS_INVALID_PARAM, ret);
93
94 size_t slen = 0;
95 ret = SoftBusBase64Encode((unsigned char *)dst, sizeof(dst), &olen, (unsigned char *)src, slen);
96 EXPECT_EQ(SOFTBUS_INVALID_PARAM, ret);
97 }
98
99 /*
100 * @tc.name: SoftBusBase64Decode001
101 * @tc.desc: parameters is Legal
102 * @tc.type: FUNC
103 * @tc.require: I5OHDE
104 */
105 HWTEST_F(AdaptorDsoftbusCryptTest, SoftBusBase64Decode001, TestSize.Level0)
106 {
107 char dst[100];
108 size_t olen = 10;
109 int32_t ret = SoftBusBase64Decode((unsigned char *)dst, sizeof(dst), &olen, encodeStr, encodeLen);
110 EXPECT_EQ(SOFTBUS_OK, ret);
111 }
112
113 /*
114 * @tc.name: SoftBusBase64Decode002
115 * @tc.desc: parameters is nullptr
116 * @tc.type: FUNC
117 * @tc.require: I5OHDE
118 */
119 HWTEST_F(AdaptorDsoftbusCryptTest, SoftBusBase64Decode002, TestSize.Level0)
120 {
121 char dst[100];
122 char src[] = "abcde";
123 size_t dlen = 1;
124 size_t olen = 10;
125 size_t slen = 1;
126
127 int32_t ret = SoftBusBase64Decode(nullptr, dlen, &olen, (unsigned char *)src, slen);
128 EXPECT_EQ(SOFTBUS_INVALID_PARAM, ret);
129
130 ret = SoftBusBase64Decode((unsigned char *)dst, dlen, nullptr, (unsigned char *)src, slen);
131 EXPECT_EQ(SOFTBUS_INVALID_PARAM, ret);
132
133 ret = SoftBusBase64Decode((unsigned char *)dst, dlen, &olen, nullptr, slen);
134 EXPECT_EQ(SOFTBUS_INVALID_PARAM, ret);
135 }
136
137 /*
138 * @tc.name: SoftBusBase64Decode003
139 * @tc.desc: dlen and slen is illegal
140 * @tc.type: FUNC
141 * @tc.require: I5OHDE
142 */
143 HWTEST_F(AdaptorDsoftbusCryptTest, SoftBusBase64Decode003, TestSize.Level0)
144 {
145 char dst[100];
146 char src[] = "abcde";
147 size_t dlen = 1;
148 size_t olen = 10;
149 size_t slen = 1;
150 size_t dlen1 = 0;
151 int32_t ret = SoftBusBase64Decode((unsigned char *)dst, dlen1, &olen, (unsigned char *)src, slen);
152 EXPECT_EQ(SOFTBUS_INVALID_PARAM, ret);
153
154 size_t slen1 = 0;
155 SoftBusBase64Decode((unsigned char *)dst, dlen, &olen, (unsigned char *)src, slen1);
156 EXPECT_EQ(SOFTBUS_INVALID_PARAM, ret);
157 }
158
159 /*
160 * @tc.name: SoftBusGenerateSessionKey001
161 * @tc.desc: parameters is Legal
162 * @tc.type: FUNC
163 * @tc.require: I5OHDE
164 */
165 HWTEST_F(AdaptorDsoftbusCryptTest, SoftBusGenerateSessionKey001, TestSize.Level0)
166 {
167 char key[10];
168 int32_t ret = SoftBusGenerateSessionKey(key, sizeof(key));
169 EXPECT_EQ(SOFTBUS_OK, ret);
170 }
171
172 /*
173 * @tc.name: SoftBusGenerateSessionKey002
174 * @tc.desc: key is nullptr
175 * @tc.type: FUNC
176 * @tc.require: I5OHDE
177 */
178 HWTEST_F(AdaptorDsoftbusCryptTest, SoftBusGenerateSessionKey002, TestSize.Level0)
179 {
180 int32_t len = 10;
181 int32_t ret = SoftBusGenerateSessionKey(nullptr, len);
182 EXPECT_EQ(SOFTBUS_ENCRYPT_ERR, ret);
183 }
184
185 /*
186 * @tc.name: SoftBusGenerateSessionKey003
187 * @tc.desc: len is illegal
188 * @tc.type: FUNC
189 * @tc.require: I5OHDE
190 */
191 HWTEST_F(AdaptorDsoftbusCryptTest, SoftBusGenerateSessionKey003, TestSize.Level0)
192 {
193 char key[10];
194 int32_t len = 0;
195 int32_t ret = SoftBusGenerateSessionKey(key, len);
196 EXPECT_EQ(SOFTBUS_ENCRYPT_ERR, ret);
197 }
198
199 /*
200 * @tc.name: SoftBusGenerateStrHash001
201 * @tc.desc: parameters is Legal
202 * @tc.type: FUNC
203 * @tc.require: I5OHDE
204 */
205 HWTEST_F(AdaptorDsoftbusCryptTest, SoftBusGenerateStrHash001, TestSize.Level0)
206 {
207 char str[] = "abcde";
208 char hash[100];
209 int32_t ret = SoftBusGenerateStrHash((unsigned char *)str, sizeof(str), (unsigned char *)hash);
210 EXPECT_EQ(SOFTBUS_OK, ret);
211 }
212
213 /*
214 * @tc.name: SoftBusGenerateStrHash002
215 * @tc.desc: str and hash is nullptr
216 * @tc.type: FUNC
217 * @tc.require: I5OHDE
218 */
219 HWTEST_F(AdaptorDsoftbusCryptTest, SoftBusGenerateStrHash002, TestSize.Level0)
220 {
221 char str[] = "abcde";
222 char hash[100];
223 int32_t ret = SoftBusGenerateStrHash(nullptr, sizeof(str), (unsigned char *)hash);
224 EXPECT_EQ(SOFTBUS_INVALID_PARAM, ret);
225
226 ret = SoftBusGenerateStrHash((unsigned char *)str, sizeof(str), nullptr);
227 EXPECT_EQ(SOFTBUS_INVALID_PARAM, ret);
228
229 uint32_t len = 0;
230 ret = SoftBusGenerateStrHash((unsigned char *)str, len, (unsigned char *)hash);
231 EXPECT_EQ(SOFTBUS_INVALID_PARAM, ret);
232 }
233
234 /*
235 * @tc.name: SoftBusGenerateRandomArrayTest001
236 * @tc.desc: randStr and len is valid param
237 * @tc.type: FUNC
238 * @tc.require: I5OHDE
239 */
240 HWTEST_F(AdaptorDsoftbusCryptTest, SoftBusGenerateRandomArrayTest001, TestSize.Level0)
241 {
242 char randStr[64];
243 int32_t ret = SoftBusGenerateRandomArray((unsigned char *)randStr, sizeof(randStr));
244 EXPECT_EQ(SOFTBUS_OK, ret);
245 }
246
247 /*
248 * @tc.name: SoftBusGenerateRandomArrayTest002
249 * @tc.desc: randStr and len is invalid param
250 * @tc.type: FUNC
251 * @tc.require: I5OHDE
252 */
253 HWTEST_F(AdaptorDsoftbusCryptTest, SoftBusGenerateRandomArrayTest002, TestSize.Level0)
254 {
255 uint32_t len = 1;
256 char randStr[64];
257 uint32_t len1 = 0;
258 int32_t ret = SoftBusGenerateRandomArray(nullptr, len);
259 EXPECT_EQ(SOFTBUS_INVALID_PARAM, ret);
260 ret = SoftBusGenerateRandomArray((unsigned char *)randStr, len1);
261 EXPECT_EQ(SOFTBUS_INVALID_PARAM, ret);
262 }
263
264 /*
265 * @tc.name: SoftBusEncryptData001
266 * @tc.desc: all valid param
267 * @tc.type: FUNC
268 * @tc.require: I5OHDE
269 */
270 HWTEST_F(AdaptorDsoftbusCryptTest, SoftBusEncryptData001, TestSize.Level0)
271 {
272 AesGcmCipherKey cipherKey;
273 cipherKey.keyLen = SESSION_KEY_LENGTH;
274 int32_t ret = SoftBusGenerateRandomArray(cipherKey.key, SESSION_KEY_LENGTH);
275 EXPECT_EQ(SOFTBUS_OK, ret);
276
277 char input[32];
278 uint32_t inLen = 32;
279 char encryptData[32 + OVERHEAD_LEN];
280 uint32_t encryptLen = 32 + OVERHEAD_LEN;
281 ret = SoftBusEncryptData(&cipherKey, (unsigned char *)input, inLen, (unsigned char *)encryptData, &encryptLen);
282 EXPECT_EQ(SOFTBUS_OK, ret);
283 }
284
285 /*
286 * @tc.name: SoftBusEncryptData002
287 * @tc.desc: cipherKey、input and inLen is invalid param
288 * @tc.type: FUNC
289 * @tc.require: I5OHDE
290 */
291 HWTEST_F(AdaptorDsoftbusCryptTest, SoftBusEncryptData002, TestSize.Level0)
292 {
293 AesGcmCipherKey cipherKey;
294 cipherKey.keyLen = 32;
295 char input[32];
296 uint32_t inLen = 32;
297 uint32_t inLen1 = 0;
298 char encryptData[32];
299 uint32_t encryptLen = 32;
300 int32_t ret = SoftBusEncryptData(nullptr, (unsigned char *)input, inLen, (unsigned char *)encryptData, &encryptLen);
301 EXPECT_EQ(SOFTBUS_INVALID_PARAM, ret);
302 ret = SoftBusEncryptData(&cipherKey, nullptr, inLen, (unsigned char *)encryptData, &encryptLen);
303 EXPECT_EQ(SOFTBUS_INVALID_PARAM, ret);
304 ret = SoftBusEncryptData(&cipherKey, (unsigned char *)input, inLen1, (unsigned char *)encryptData, &encryptLen);
305 EXPECT_EQ(SOFTBUS_INVALID_PARAM, ret);
306 }
307
308 /*
309 * @tc.name: SoftBusEncryptData003
310 * @tc.desc: encryptData and encryptLen is invalid param
311 * @tc.type: FUNC
312 * @tc.require: I5OHDE
313 */
314 HWTEST_F(AdaptorDsoftbusCryptTest, SoftBusEncryptData003, TestSize.Level0)
315 {
316 AesGcmCipherKey cipherKey;
317 cipherKey.keyLen = 32;
318 char input[32];
319 uint32_t inLen = 32;
320 char encryptData[32];
321 uint32_t encryptLen = 32;
322 int32_t ret = SoftBusEncryptData(&cipherKey, (unsigned char *)input, inLen, nullptr, &encryptLen);
323 EXPECT_EQ(SOFTBUS_INVALID_PARAM, ret);
324 ret = SoftBusEncryptData(&cipherKey, (unsigned char *)input, inLen, (unsigned char *)encryptData, nullptr);
325 EXPECT_EQ(SOFTBUS_INVALID_PARAM, ret);
326 }
327
328 /*
329 * @tc.name: SoftBusEncryptDataWithSeq001
330 * @tc.desc: all valid param
331 * @tc.type: FUNC
332 * @tc.require: I5OHDE
333 */
334 HWTEST_F(AdaptorDsoftbusCryptTest, SoftBusEncryptDataWithSeq001, TestSize.Level0)
335 {
336 AesGcmCipherKey cipherKey;
337 cipherKey.keyLen = SESSION_KEY_LENGTH;
338 int32_t ret = SoftBusGenerateRandomArray(cipherKey.key, SESSION_KEY_LENGTH);
339 EXPECT_EQ(SOFTBUS_OK, ret);
340
341 char input[32];
342 uint32_t inLen = 32;
343 char encryptData[32 + OVERHEAD_LEN];
344 uint32_t encryptLen = 32 + OVERHEAD_LEN;
345 int32_t seqNum = 1;
346 ret = SoftBusEncryptDataWithSeq(
347 &cipherKey, (unsigned char *)input, inLen, (unsigned char *)encryptData, &encryptLen, seqNum);
348 EXPECT_EQ(SOFTBUS_OK, ret);
349 }
350
351 /*
352 * @tc.name: SoftBusEncryptDataWithSeq002
353 * @tc.desc: cipherKey and input is invalid param
354 * @tc.type: FUNC
355 * @tc.require: I5OHDE
356 */
357 HWTEST_F(AdaptorDsoftbusCryptTest, SoftBusEncryptDataWithSeq002, TestSize.Level0)
358 {
359 AesGcmCipherKey cipherKey;
360 cipherKey.keyLen = 32;
361 char input[32];
362 uint32_t inLen = 32;
363 char encryptData[32];
364 uint32_t encryptLen = 32;
365 int32_t seqNum = 1;
366 int32_t ret = SoftBusEncryptDataWithSeq(
367 nullptr, (unsigned char *)input, inLen, (unsigned char *)encryptData, &encryptLen, seqNum);
368 EXPECT_EQ(SOFTBUS_INVALID_PARAM, ret);
369 ret = SoftBusEncryptDataWithSeq(&cipherKey, nullptr, inLen, (unsigned char *)encryptData, &encryptLen, seqNum);
370 EXPECT_EQ(SOFTBUS_INVALID_PARAM, ret);
371 }
372
373 /*
374 * @tc.name: SoftBusEncryptDataWithSeq003
375 * @tc.desc: inLen is invalid param
376 * @tc.type: FUNC
377 * @tc.require: I5OHDE
378 */
379 HWTEST_F(AdaptorDsoftbusCryptTest, SoftBusEncryptDataWithSeq003, TestSize.Level0)
380 {
381 AesGcmCipherKey cipherKey;
382 cipherKey.keyLen = 32;
383 char input[32];
384 uint32_t inLen = 0;
385 char encryptData[32];
386 uint32_t encryptLen = 32;
387 int32_t seqNum = 1;
388 int32_t ret = SoftBusEncryptDataWithSeq(
389 &cipherKey, (unsigned char *)input, inLen, (unsigned char *)encryptData, &encryptLen, seqNum);
390 EXPECT_EQ(SOFTBUS_INVALID_PARAM, ret);
391 }
392
393 /*
394 * @tc.name: SoftBusEncryptDataWithSeq004
395 * @tc.desc: encryptData and encryptLen is invalid param
396 * @tc.type: FUNC
397 * @tc.require: I5OHDE
398 */
399 HWTEST_F(AdaptorDsoftbusCryptTest, SoftBusEncryptDataWithSeq004, TestSize.Level0)
400 {
401 AesGcmCipherKey cipherKey;
402 cipherKey.keyLen = 32;
403 char input[32];
404 uint32_t inLen = 32;
405 char encryptData[32];
406 uint32_t encryptLen = 32;
407 int32_t seqNum = 1;
408 int32_t ret = SoftBusEncryptDataWithSeq(&cipherKey, (unsigned char *)input, inLen, nullptr, &encryptLen, seqNum);
409 EXPECT_EQ(SOFTBUS_INVALID_PARAM, ret);
410 ret = SoftBusEncryptDataWithSeq(
411 &cipherKey, (unsigned char *)input, inLen, (unsigned char *)encryptData, nullptr, seqNum);
412 EXPECT_EQ(SOFTBUS_INVALID_PARAM, ret);
413 }
414
415 /*
416 * @tc.name: SoftBusDecryptData001
417 * @tc.desc: all valid param
418 * @tc.type: FUNC
419 * @tc.require: I5OHDE
420 */
421 HWTEST_F(AdaptorDsoftbusCryptTest, SoftBusDecryptData001, TestSize.Level0)
422 {
423 AesGcmCipherKey cipherKey;
424 cipherKey.keyLen = SESSION_KEY_LENGTH;
425 int32_t ret = SoftBusGenerateRandomArray(cipherKey.key, SESSION_KEY_LENGTH);
426 EXPECT_EQ(SOFTBUS_OK, ret);
427
428 char input[32];
429 uint32_t inLen = 32;
430 char encryptData[32 + OVERHEAD_LEN];
431 uint32_t encryptLen = 32 + OVERHEAD_LEN;
432 char decryptData[32];
433 uint32_t decryptLen = 32;
434 ret = SoftBusEncryptData(&cipherKey, (unsigned char *)input, inLen, (unsigned char *)encryptData, &encryptLen);
435 EXPECT_EQ(SOFTBUS_OK, ret);
436 ret = SoftBusDecryptData(
437 &cipherKey, (unsigned char *)encryptData, encryptLen, (unsigned char *)decryptData, &decryptLen);
438 EXPECT_EQ(SOFTBUS_OK, ret);
439 }
440
441 /*
442 * @tc.name: SoftBusDecryptData002
443 * @tc.desc: cipherKey、input and inLen is invalid param
444 * @tc.type: FUNC
445 * @tc.require: I5OHDE
446 */
447 HWTEST_F(AdaptorDsoftbusCryptTest, SoftBusDecryptData002, TestSize.Level0)
448 {
449 AesGcmCipherKey cipherKey;
450 cipherKey.keyLen = SESSION_KEY_LENGTH;
451 int32_t ret = SoftBusGenerateRandomArray(cipherKey.key, SESSION_KEY_LENGTH);
452 EXPECT_EQ(SOFTBUS_OK, ret);
453
454 char input[32];
455 uint32_t inLen = 32;
456 uint32_t inLen1 = 0;
457 char encryptData[32 + OVERHEAD_LEN];
458 uint32_t encryptLen = 32 + OVERHEAD_LEN;
459 char decryptData[32];
460 uint32_t decryptLen = 32;
461 ret = SoftBusEncryptData(&cipherKey, (unsigned char *)input, inLen, (unsigned char *)encryptData, &encryptLen);
462 EXPECT_EQ(SOFTBUS_OK, ret);
463 ret = SoftBusDecryptData(
464 nullptr, (unsigned char *)encryptData, encryptLen, (unsigned char *)decryptData, &decryptLen);
465 EXPECT_EQ(SOFTBUS_INVALID_PARAM, ret);
466 ret = SoftBusDecryptData(&cipherKey, nullptr, encryptLen, (unsigned char *)decryptData, &decryptLen);
467 EXPECT_EQ(SOFTBUS_INVALID_PARAM, ret);
468 ret =
469 SoftBusDecryptData(&cipherKey, (unsigned char *)encryptData, inLen1, (unsigned char *)decryptData, &decryptLen);
470 EXPECT_EQ(SOFTBUS_INVALID_PARAM, ret);
471 }
472
473 /*
474 * @tc.name: SoftBusDecryptData003
475 * @tc.desc: decryptData and decryptLen is invalid param
476 * @tc.type: FUNC
477 * @tc.require: I5OHDE
478 */
479 HWTEST_F(AdaptorDsoftbusCryptTest, SoftBusDecryptData003, TestSize.Level0)
480 {
481 AesGcmCipherKey cipherKey;
482 cipherKey.keyLen = SESSION_KEY_LENGTH;
483 int32_t ret = SoftBusGenerateRandomArray(cipherKey.key, SESSION_KEY_LENGTH);
484 EXPECT_EQ(SOFTBUS_OK, ret);
485
486 char input[32];
487 uint32_t inLen = 32;
488 char encryptData[32 + OVERHEAD_LEN];
489 uint32_t encryptLen = 32 + OVERHEAD_LEN;
490 char decryptData[32];
491 uint32_t decryptLen = 32;
492 ret = SoftBusEncryptData(&cipherKey, (unsigned char *)input, inLen, (unsigned char *)encryptData, &encryptLen);
493 EXPECT_EQ(SOFTBUS_OK, ret);
494 ret = SoftBusDecryptData(&cipherKey, (unsigned char *)encryptData, encryptLen, nullptr, &decryptLen);
495 EXPECT_EQ(SOFTBUS_INVALID_PARAM, ret);
496 ret =
497 SoftBusDecryptData(&cipherKey, (unsigned char *)encryptData, encryptLen, (unsigned char *)decryptData, nullptr);
498 EXPECT_EQ(SOFTBUS_INVALID_PARAM, ret);
499 }
500
501 /*
502 * @tc.name: SoftBusDecryptDataWithSeq001
503 * @tc.desc: all valid param
504 * @tc.type: FUNC
505 * @tc.require: I5OHDE
506 */
507 HWTEST_F(AdaptorDsoftbusCryptTest, SoftBusDecryptDataWithSeq001, TestSize.Level0)
508 {
509 AesGcmCipherKey cipherKey;
510 cipherKey.keyLen = SESSION_KEY_LENGTH;
511 int32_t ret = SoftBusGenerateRandomArray(cipherKey.key, SESSION_KEY_LENGTH);
512 EXPECT_EQ(SOFTBUS_OK, ret);
513
514 char input[32];
515 uint32_t inLen = 32;
516 char encryptData[32 + OVERHEAD_LEN];
517 uint32_t encryptLen = 32 + OVERHEAD_LEN;
518 char decryptData[32];
519 uint32_t decryptLen = 32;
520 int32_t seqNum = 1;
521 ret = SoftBusEncryptData(&cipherKey, (unsigned char *)input, inLen, (unsigned char *)encryptData, &encryptLen);
522 EXPECT_EQ(SOFTBUS_OK, ret);
523 ret = SoftBusDecryptDataWithSeq(
524 &cipherKey, (unsigned char *)encryptData, encryptLen, (unsigned char *)decryptData, &decryptLen, seqNum);
525 EXPECT_EQ(SOFTBUS_OK, ret);
526 }
527
528 /*
529 * @tc.name: SoftBusDecryptDataWithSeq002
530 * @tc.desc: cipherKey and input is invalid param
531 * @tc.type: FUNC
532 * @tc.require: I5OHDE
533 */
534 HWTEST_F(AdaptorDsoftbusCryptTest, SoftBusDecryptDataWithSeq002, TestSize.Level0)
535 {
536 AesGcmCipherKey cipherKey;
537 cipherKey.keyLen = SESSION_KEY_LENGTH;
538 int32_t ret = SoftBusGenerateRandomArray(cipherKey.key, SESSION_KEY_LENGTH);
539 EXPECT_EQ(SOFTBUS_OK, ret);
540
541 char input[32];
542 uint32_t inLen = 32;
543 char encryptData[32 + OVERHEAD_LEN];
544 uint32_t encryptLen = 32 + OVERHEAD_LEN;
545 char decryptData[32];
546 uint32_t decryptLen = 32;
547 int32_t seqNum = 1;
548 ret = SoftBusEncryptData(&cipherKey, (unsigned char *)input, inLen, (unsigned char *)encryptData, &encryptLen);
549 EXPECT_EQ(SOFTBUS_OK, ret);
550 ret = SoftBusDecryptDataWithSeq(
551 nullptr, (unsigned char *)encryptData, encryptLen, (unsigned char *)decryptData, &decryptLen, seqNum);
552 EXPECT_EQ(SOFTBUS_INVALID_PARAM, ret);
553 ret = SoftBusDecryptDataWithSeq(&cipherKey, nullptr, encryptLen, (unsigned char *)decryptData, &decryptLen, seqNum);
554 EXPECT_EQ(SOFTBUS_INVALID_PARAM, ret);
555 }
556
557 /*
558 * @tc.name: SoftBusDecryptDataWithSeq003
559 * @tc.desc: inLen is invalid param
560 * @tc.type: FUNC
561 * @tc.require: I5OHDE
562 */
563 HWTEST_F(AdaptorDsoftbusCryptTest, SoftBusDecryptDataWithSeq003, TestSize.Level0)
564 {
565 AesGcmCipherKey cipherKey;
566 cipherKey.keyLen = SESSION_KEY_LENGTH;
567 int32_t ret = SoftBusGenerateRandomArray(cipherKey.key, SESSION_KEY_LENGTH);
568 EXPECT_EQ(SOFTBUS_OK, ret);
569
570 char input[32];
571 uint32_t inLen = 32;
572 char encryptData[32 + OVERHEAD_LEN];
573 uint32_t encryptLen = 32 + OVERHEAD_LEN;
574 uint32_t encryptLen1 = 0;
575 char decryptData[32];
576 uint32_t decryptLen = 32;
577 int32_t seqNum = 1;
578 ret = SoftBusEncryptData(&cipherKey, (unsigned char *)input, inLen, (unsigned char *)encryptData, &encryptLen);
579 EXPECT_EQ(SOFTBUS_OK, ret);
580 ret = SoftBusDecryptDataWithSeq(
581 &cipherKey, (unsigned char *)encryptData, encryptLen1, (unsigned char *)decryptData, &decryptLen, seqNum);
582 EXPECT_EQ(SOFTBUS_INVALID_PARAM, ret);
583 }
584
585 /*
586 * @tc.name: SoftBusDecryptDataWithSeq004
587 * @tc.desc: encryptData and encryptLen is invalid param
588 * @tc.type: FUNC
589 * @tc.require: I5OHDE
590 */
591 HWTEST_F(AdaptorDsoftbusCryptTest, SoftBusDecryptDataWithSeq004, TestSize.Level0)
592 {
593 AesGcmCipherKey cipherKey;
594 cipherKey.keyLen = SESSION_KEY_LENGTH;
595 int32_t ret = SoftBusGenerateRandomArray(cipherKey.key, SESSION_KEY_LENGTH);
596 EXPECT_EQ(SOFTBUS_OK, ret);
597
598 char input[32];
599 uint32_t inLen = 32;
600 char encryptData[32 + OVERHEAD_LEN];
601 uint32_t encryptLen = 32 + OVERHEAD_LEN;
602 char decryptData[32];
603 uint32_t decryptLen = 32;
604 int32_t seqNum = 1;
605 ret = SoftBusEncryptData(&cipherKey, (unsigned char *)input, inLen, (unsigned char *)encryptData, &encryptLen);
606 EXPECT_EQ(SOFTBUS_OK, ret);
607 ret = SoftBusDecryptDataWithSeq(&cipherKey, (unsigned char *)encryptData, encryptLen, nullptr, &decryptLen, seqNum);
608 EXPECT_EQ(SOFTBUS_INVALID_PARAM, ret);
609 ret = SoftBusDecryptDataWithSeq(
610 &cipherKey, (unsigned char *)encryptData, encryptLen, (unsigned char *)decryptData, nullptr, seqNum);
611 EXPECT_EQ(SOFTBUS_INVALID_PARAM, ret);
612 }
613
614 /*
615 * @tc.name: SoftBusEncryptDataByGcm128001
616 * @tc.desc: encrypt invalid param
617 * @tc.type: FUNC
618 * @tc.require: I5OHDE
619 */
620 HWTEST_F(AdaptorDsoftbusCryptTest, SoftBusEncryptDataByGcm128001, TestSize.Level0)
621 {
622 AesGcm128CipherKey cipherKey;
623 cipherKey.keyLen = SHORT_SESSION_KEY_LENGTH;
624 int32_t ret = SoftBusGenerateRandomArray(cipherKey.key, SHORT_SESSION_KEY_LENGTH);
625 EXPECT_EQ(SOFTBUS_OK, ret);
626
627 char input[SESSION_KEY_LENGTH];
628 uint32_t inLen = SESSION_KEY_LENGTH;
629 char encryptData[SESSION_KEY_LENGTH + SHORT_TAG_LEN];
630 uint32_t encryptLen = SESSION_KEY_LENGTH + SHORT_TAG_LEN;
631 ret = SoftBusEncryptDataByGcm128(nullptr, (unsigned char *)input, inLen, (unsigned char *)encryptData, &encryptLen);
632 EXPECT_EQ(SOFTBUS_INVALID_PARAM, ret);
633
634 ret = SoftBusEncryptDataByGcm128(&cipherKey, nullptr, inLen, (unsigned char *)encryptData, &encryptLen);
635 EXPECT_EQ(SOFTBUS_INVALID_PARAM, ret);
636
637 ret = SoftBusEncryptDataByGcm128(&cipherKey, (unsigned char *)input, 0, (unsigned char *)encryptData, &encryptLen);
638 EXPECT_EQ(SOFTBUS_INVALID_PARAM, ret);
639
640 ret = SoftBusEncryptDataByGcm128(&cipherKey, (unsigned char *)input, inLen, nullptr, &encryptLen);
641 EXPECT_EQ(SOFTBUS_INVALID_PARAM, ret);
642
643 ret = SoftBusEncryptDataByGcm128(&cipherKey, (unsigned char *)input, inLen, (unsigned char *)encryptData, nullptr);
644 EXPECT_EQ(SOFTBUS_INVALID_PARAM, ret);
645
646 ret = SoftBusEncryptDataByGcm128(&cipherKey, (unsigned char *)input, inLen,
647 (unsigned char *)encryptData, &encryptLen);
648 EXPECT_EQ(SOFTBUS_OK, ret);
649 }
650
651 /*
652 * @tc.name: SoftBusDecryptDataByGcm128002
653 * @tc.desc: decrypt invalid param
654 * @tc.type: FUNC
655 * @tc.require: I5OHDE
656 */
657 HWTEST_F(AdaptorDsoftbusCryptTest, SoftBusDecryptDataByGcm128002, TestSize.Level0)
658 {
659 AesGcm128CipherKey cipherKey;
660 cipherKey.keyLen = SHORT_SESSION_KEY_LENGTH;
661 int32_t ret = SoftBusGenerateRandomArray(cipherKey.key, SHORT_SESSION_KEY_LENGTH);
662 EXPECT_EQ(SOFTBUS_OK, ret);
663
664 char input[SESSION_KEY_LENGTH];
665 uint32_t inLen = SESSION_KEY_LENGTH;
666 char decryptData[SESSION_KEY_LENGTH];
667 uint32_t decryptLen = SESSION_KEY_LENGTH;
668
669 ret = SoftBusDecryptDataByGcm128(nullptr, (unsigned char *)input, inLen, (unsigned char *)decryptData, &decryptLen);
670 EXPECT_EQ(SOFTBUS_INVALID_PARAM, ret);
671
672 ret = SoftBusDecryptDataByGcm128(&cipherKey, nullptr, inLen, (unsigned char *)decryptData, &decryptLen);
673 EXPECT_EQ(SOFTBUS_INVALID_PARAM, ret);
674
675 ret = SoftBusDecryptDataByGcm128(&cipherKey, (unsigned char *)input, 0, (unsigned char *)decryptData, &decryptLen);
676 EXPECT_EQ(SOFTBUS_INVALID_PARAM, ret);
677
678 ret = SoftBusDecryptDataByGcm128(&cipherKey, (unsigned char *)input, inLen, nullptr, &decryptLen);
679 EXPECT_EQ(SOFTBUS_INVALID_PARAM, ret);
680
681 ret = SoftBusDecryptDataByGcm128(&cipherKey, (unsigned char *)input, inLen, (unsigned char *)decryptData, nullptr);
682 EXPECT_EQ(SOFTBUS_INVALID_PARAM, ret);
683
684 ret = SoftBusEncryptDataByGcm128(&cipherKey, (unsigned char *)input, inLen,
685 (unsigned char *)decryptData, &decryptLen);
686 EXPECT_EQ(SOFTBUS_OK, ret);
687 }
688 } // namespace OHOS