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