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 "test.h"
17
18 #include "napi/native_api.h"
19 #include "napi/native_node_api.h"
20
21 #include "converter.h"
22 #include "js_blob.h"
23 #include "js_buffer.h"
24 #include "tools/log.h"
25
26 #include <limits>
27
28 #define ASSERT_CHECK_CALL(call) \
29 { \
30 ASSERT_EQ(call, napi_ok); \
31 }
32
33 #define ASSERT_CHECK_VALUE_TYPE(env, value, type) \
34 { \
35 napi_valuetype valueType = napi_undefined; \
36 ASSERT_TRUE(value != nullptr); \
37 ASSERT_CHECK_CALL(napi_typeof(env, value, &valueType)); \
38 ASSERT_EQ(valueType, type); \
39 }
FillZero(OHOS::buffer::Buffer * buf,size_t size)40 void FillZero(OHOS::buffer::Buffer *buf, size_t size)
41 {
42 for (size_t i = 0; i < size; i++) {
43 buf->Set(i, 0);
44 }
45 }
46
47 /**
48 * @tc.name: ConstructorTest001
49 * @tc.desc: Buffer Constructor.
50 * @tc.type: FUNC
51 * @tc.require:issueI5J5Z3
52 */
53 HWTEST_F(NativeEngineTest, ConstructorTest001, testing::ext::TestSize.Level0)
54 {
55 OHOS::buffer::Buffer *buf = new OHOS::buffer::Buffer();
56 buf->Init(10);
57 ASSERT_EQ(buf->GetLength(), 10);
58 }
59
60 /**
61 * @tc.name: ConstructorTest002
62 * @tc.desc: Buffer Constructor.
63 * @tc.type: FUNC
64 * @tc.require:issueI5J5Z3
65 */
66 HWTEST_F(NativeEngineTest, ConstructorTest002, testing::ext::TestSize.Level0)
67 {
68 OHOS::buffer::Buffer *buf1 = new OHOS::buffer::Buffer();
69 buf1->Init(10);
70 OHOS::buffer::Buffer *buf2 = new OHOS::buffer::Buffer();
71 buf2->Init(buf1);
72 ASSERT_EQ(buf2->GetLength(), 10);
73 }
74
75
76 /**
77 * @tc.name: ConstructorTest003
78 * @tc.desc: Buffer Constructor.
79 * @tc.type: FUNC
80 * @tc.require:issueI5J5Z3
81 */
82 HWTEST_F(NativeEngineTest, ConstructorTest003, testing::ext::TestSize.Level0)
83 {
84 OHOS::buffer::Buffer *poolBuffer = new OHOS::buffer::Buffer();
85 poolBuffer->Init(1024 * 8);
86 OHOS::buffer::Buffer *buf2 = new OHOS::buffer::Buffer();
87 buf2->Init(poolBuffer, 0, 5);
88 ASSERT_EQ(buf2->GetLength(), 5);
89 ASSERT_EQ(buf2->GetByteOffset(), 0);
90
91 OHOS::buffer::Buffer *buf3 = new OHOS::buffer::Buffer();
92 buf3->Init(poolBuffer, 5, 6);
93 ASSERT_EQ(buf3->GetLength(), 6);
94 ASSERT_EQ(buf3->GetByteOffset(), 5);
95 }
96
97 /**
98 * @tc.name: ConstructorTest004
99 * @tc.desc: Buffer Constructor.
100 * @tc.type: FUNC
101 * @tc.require:issueI5J5Z3
102 */
103 HWTEST_F(NativeEngineTest, ConstructorTest004, testing::ext::TestSize.Level0)
104 {
105 OHOS::buffer::Buffer *buf2 = new OHOS::buffer::Buffer();
106 uint8_t data[4] = {1, 2, 3, 4};
107 buf2->Init(data, 0, 4);
108 ASSERT_EQ(buf2->GetLength(), 4);
109 ASSERT_EQ(buf2->GetByteOffset(), 0);
110 }
111
112 /**
113 * @tc.name: ConstructorTest005
114 * @tc.desc: Buffer Constructor.
115 * @tc.type: FUNC
116 * @tc.require:issueI5J5Z3
117 */
118 HWTEST_F(NativeEngineTest, ConstructorTest005, testing::ext::TestSize.Level0)
119 {
120 OHOS::buffer::Buffer *buf = new OHOS::buffer::Buffer();
121 buf->Init(1);
122 ASSERT_EQ(buf->GetLength(), 1);
123 uint32_t res = buf->Copy(nullptr, 0, 0, 0);
124 ASSERT_EQ(res, 0);
125 int result = buf->Compare(nullptr, 0, 0, 0);
126 ASSERT_EQ(result, 0);
127 buf->ReadBytes(nullptr, 0, 0);
128 buf->FillString("abc", 1, 0, "utf16le");
129 buf->FillString("abc", 1, 0, "binary");
130 buf->FillString("abc", 1, 0, "sos");
131 buf->FillString("abc", 1, 0, "hex");
132 std::vector<uint8_t> array;
133 buf->FillNumber(array, 0, 0);
134 OHOS::buffer::Buffer *buffer = new OHOS::buffer::Buffer();
135 buf->FillBuffer(buffer, 0, 0);
136 buf->FillBuffer(nullptr, 1, 0);
137 buf->SetArray(array, 0);
138 result = buf->LastIndexOf(nullptr, 0, 0);
139 ASSERT_EQ(result, -1);
140 uint64_t resultIndex = 0;
141 result = buf->IndexOf(nullptr, 0, 0, resultIndex);
142 ASSERT_EQ(result, -1);
143 delete buf;
144 buf = nullptr;
145 delete buffer;
146 buffer = nullptr;
147 }
148
149 /**
150 * @tc.name: DestructorTest001
151 * @tc.desc: Buffer Destructor.
152 * @tc.type: FUNC
153 * @tc.require:issueI5J5Z3
154 */
155 HWTEST_F(NativeEngineTest, DestructorTest001, testing::ext::TestSize.Level0)
156 {
157 OHOS::buffer::Buffer *buf = new OHOS::buffer::Buffer();
158 uint8_t data[4] = {1, 2, 3, 4};
159 buf->Init(data, 0, 4);
160 delete buf;
161 }
162
163 /**
164 * @tc.name: GetLengthTest001
165 * @tc.desc: Get buffer Length.
166 * @tc.type: FUNC
167 * @tc.require:issueI5J5Z3
168 */
169 HWTEST_F(NativeEngineTest, GetLengthTest001, testing::ext::TestSize.Level0)
170 {
171 OHOS::buffer::Buffer *buf = new OHOS::buffer::Buffer();
172 ASSERT_EQ(buf->GetLength(), 0);
173 }
174
175 /**
176 * @tc.name: GetLengthTest002
177 * @tc.desc: Get buffer Length.
178 * @tc.type: FUNC
179 * @tc.require:issueI5J5Z3
180 */
181 HWTEST_F(NativeEngineTest, GetLengthTest002, testing::ext::TestSize.Level0)
182 {
183 OHOS::buffer::Buffer *buf = new OHOS::buffer::Buffer();
184 buf->Init(6);
185 ASSERT_EQ(buf->GetLength(), 6);
186 }
187
188 /**
189 * @tc.name: SetLengthTest001
190 * @tc.desc: Set buffer Length.
191 * @tc.type: FUNC
192 * @tc.require:issueI5J5Z3
193 */
194 HWTEST_F(NativeEngineTest, SetLengthTest001, testing::ext::TestSize.Level0)
195 {
196 OHOS::buffer::Buffer *buf = new OHOS::buffer::Buffer();
197 buf->Init(6);
198 buf->SetLength(7);
199 ASSERT_EQ(buf->GetLength(), 7);
200 }
201
202 /**
203 * @tc.name: GetByteOffsetTest001
204 * @tc.desc: Get buffer byteOffset.
205 * @tc.type: FUNC
206 * @tc.require:issueI5J5Z3
207 */
208 HWTEST_F(NativeEngineTest, GetByteOffsetTest001, testing::ext::TestSize.Level0)
209 {
210 OHOS::buffer::Buffer *poolBuffer = new OHOS::buffer::Buffer();
211 poolBuffer->Init(1024 * 8);
212 OHOS::buffer::Buffer *buf = new OHOS::buffer::Buffer();
213 buf->Init(poolBuffer, 2, 5);
214 ASSERT_EQ(buf->GetByteOffset(), 2);
215 }
216
217 /**
218 * @tc.name: GetAndSetTest001
219 * @tc.desc: Get And Set method.
220 * @tc.type: FUNC
221 * @tc.require:issueI5J5Z3
222 */
223 HWTEST_F(NativeEngineTest, GetAndSetTest001, testing::ext::TestSize.Level0)
224 {
225 OHOS::buffer::Buffer *buf = new OHOS::buffer::Buffer();
226 buf->Init(3);
227 buf->Set(0, 1);
228 int32_t value = buf->Get(0);
229 ASSERT_EQ(value, 1);
230 }
231
232 /**
233 * @tc.name: GetAndSetTest002
234 * @tc.desc: Get And Set method.
235 * @tc.type: FUNC
236 * @tc.require:issueI5J5Z3
237 */
238 HWTEST_F(NativeEngineTest, GetAndSetTest002, testing::ext::TestSize.Level0)
239 {
240 OHOS::buffer::Buffer *buf = new OHOS::buffer::Buffer();
241 buf->Init(3);
242 buf->Set(0, 1);
243 buf->Set(1, 2);
244 buf->Set(2, 3);
245 int32_t value = buf->Get(2);
246 ASSERT_EQ(value, 3);
247 }
248
249 /**
250 * @tc.name: WriteInt32BEAndReadInt32BETest001
251 * @tc.desc: Writes value to buf at the specified offset as big-endian. The value must be a valid signed 32-bit integer
252 * Reads a signed, big-endian 32-bit integer from buf at the specified offset.
253 * @tc.type: FUNC
254 * @tc.require:issueI5J5Z3
255 */
256 HWTEST_F(NativeEngineTest, WriteInt32BEAndReadInt32BETest001, testing::ext::TestSize.Level0)
257 {
258 OHOS::buffer::Buffer *buf = new OHOS::buffer::Buffer();
259 buf->Init(4);
260 FillZero(buf, 4);
261 buf->WriteInt32BE(0x12345678, 0);
262 int32_t res = buf->ReadInt32BE(0);
263 ASSERT_EQ(res, 0x12345678);
264 }
265
266 /**
267 * @tc.name: WriteInt32BEAndReadInt32BETest002
268 * @tc.desc: Writes value to buf at the specified offset as big-endian. The value must be a valid signed 32-bit integer
269 * Reads a signed, big-endian 32-bit integer from buf at the specified offset.
270 * @tc.type: FUNC
271 * @tc.require:issueI5J5Z3
272 */
273 HWTEST_F(NativeEngineTest, WriteInt32BEAndReadInt32BETest002, testing::ext::TestSize.Level0)
274 {
275 OHOS::buffer::Buffer *buf = new OHOS::buffer::Buffer();
276 buf->Init(5);
277 FillZero(buf, 5);
278 buf->WriteInt32BE(0x12345678, 1);
279 int32_t res = buf->ReadInt32BE(1);
280 ASSERT_EQ(res, 0x12345678);
281 }
282
283 /**
284 * @tc.name: WriteInt32BEAndReadInt32BETest003
285 * @tc.desc: Writes value to buf at the specified offset as big-endian. The value must be a valid signed 32-bit integer
286 * Reads a signed, big-endian 32-bit integer from buf at the specified offset.
287 * @tc.type: FUNC
288 * @tc.require:issueI5J5Z3
289 */
290 HWTEST_F(NativeEngineTest, WriteInt32BEAndReadInt32BETest003, testing::ext::TestSize.Level0)
291 {
292 OHOS::buffer::Buffer *buf = new OHOS::buffer::Buffer();
293 buf->Init(5);
294 FillZero(buf, 5);
295 buf->WriteInt32BE(0x12345678, 1);
296 int32_t res = buf->ReadInt32BE(1);
297 ASSERT_EQ(res, 0x12345678);
298 }
299
300 /**
301 * @tc.name: WriteInt32LEAndReadInt32LETest001
302 * @tc.desc: Writes value to buf at the specified offset as little-endian.
303 * The value must be a valid signed 32-bit integer.
304 * Reads a signed, little-endian 32-bit integer from buf at the specified offset.
305 * @tc.type: FUNC
306 * @tc.require:issueI5J5Z3
307 */
308 HWTEST_F(NativeEngineTest, WriteInt32LEAndReadInt32LETest001, testing::ext::TestSize.Level0)
309 {
310 OHOS::buffer::Buffer *buf = new OHOS::buffer::Buffer();
311 buf->Init(4);
312 FillZero(buf, 4);
313 buf->WriteInt32LE(0x12345678, 0);
314 int32_t res = buf->ReadInt32LE(0);
315 ASSERT_EQ(res, 0x12345678);
316 res = buf->ReadInt32BE(0);
317 ASSERT_EQ(res, 0x78563412);
318 }
319
320 /**
321 * @tc.name: WriteInt32LEAndReadInt32LETest002
322 * @tc.desc: Writes value to buf at the specified offset as little-endian.
323 * The value must be a valid signed 32-bit integer.
324 * Reads a signed, little-endian 32-bit integer from buf at the specified offset.
325 * @tc.type: FUNC
326 * @tc.require:issueI5J5Z3
327 */
328 HWTEST_F(NativeEngineTest, WriteInt32LEAndReadInt32LETest002, testing::ext::TestSize.Level0)
329 {
330 OHOS::buffer::Buffer *buf = new OHOS::buffer::Buffer();
331 buf->Init(5);
332 FillZero(buf, 5);
333 buf->WriteInt32LE(0x12345678, 1);
334 int32_t res = buf->ReadInt32LE(0);
335 ASSERT_EQ(res, 0x34567800);
336 }
337
338 /**
339 * @tc.name: WriteInt32LEAndReadInt32LETest003
340 * @tc.desc: Writes value to buf at the specified offset as little-endian.
341 * The value must be a valid signed 32-bit integer.
342 * Reads a signed, little-endian 32-bit integer from buf at the specified offset.
343 * @tc.type: FUNC
344 * @tc.require:issueI5J5Z3
345 */
346 HWTEST_F(NativeEngineTest, WriteInt32LEAndReadInt32LETest003, testing::ext::TestSize.Level0)
347 {
348 OHOS::buffer::Buffer *buf = new OHOS::buffer::Buffer();
349 buf->Init(5);
350 FillZero(buf, 5);
351 buf->WriteInt32LE(0x12345678, 1);
352 int32_t res = buf->ReadInt32LE(1);
353 ASSERT_EQ(res, 0x12345678);
354 }
355
356 /**
357 * @tc.name: WriteUInt32BEAndReadUInt32BETest001
358 * @tc.desc: Writes value to buf at the specified offset as big-endian.
359 * The value must be a valid unsigned 32-bit integer.
360 * Reads an unsigned, big-endian 32-bit integer from buf at the specified offset.
361 * @tc.type: FUNC
362 * @tc.require:issueI5J5Z3
363 */
364 HWTEST_F(NativeEngineTest, WriteUInt32BEAndReadUInt32BETest001, testing::ext::TestSize.Level0)
365 {
366 OHOS::buffer::Buffer *buf = new OHOS::buffer::Buffer();
367 buf->Init(4);
368 FillZero(buf, 4);
369 buf->WriteUInt32BE(0x12345678, 0);
370 int32_t res = buf->ReadUInt32BE(0);
371 ASSERT_EQ(res, 0x12345678);
372 }
373
374 /**
375 * @tc.name: WriteUInt32BEAndReadUInt32BETest002
376 * @tc.desc: Writes value to buf at the specified offset as big-endian.
377 * The value must be a valid unsigned 32-bit integer.
378 * Reads an unsigned, big-endian 32-bit integer from buf at the specified offset.
379 * @tc.type: FUNC
380 * @tc.require:issueI5J5Z3
381 */
382 HWTEST_F(NativeEngineTest, WriteUInt32BEAndReadUInt32BETest002, testing::ext::TestSize.Level0)
383 {
384 OHOS::buffer::Buffer *buf = new OHOS::buffer::Buffer();
385 buf->Init(5);
386 FillZero(buf, 5);
387 buf->WriteUInt32BE(0x12345678, 1);
388 int32_t res = buf->ReadUInt32BE(0);
389 ASSERT_EQ(res, 0x123456);
390 }
391
392 /**
393 * @tc.name: WriteUInt32BEAndReadUInt32BETest003
394 * @tc.desc: Writes value to buf at the specified offset as big-endian.
395 * The value must be a valid unsigned 32-bit integer.
396 * Reads an unsigned, big-endian 32-bit integer from buf at the specified offset.
397 * @tc.type: FUNC
398 * @tc.require:issueI5J5Z3
399 */
400 HWTEST_F(NativeEngineTest, WriteUInt32BEAndReadUInt32BETest003, testing::ext::TestSize.Level0)
401 {
402 OHOS::buffer::Buffer *buf = new OHOS::buffer::Buffer();
403 buf->Init(5);
404 FillZero(buf, 5);
405 buf->WriteUInt32BE(0x12345678, 1);
406 int32_t res = buf->ReadUInt32BE(1);
407 ASSERT_EQ(res, 0x12345678);
408 }
409
410 /**
411 * @tc.name: WriteUInt32LEAndReadUInt32LETest001
412 * @tc.desc: Writes value to buf at the specified offset as little-endian.
413 * The value must be a valid unsigned 32-bit integer.
414 * Reads an unsigned, little-endian 32-bit integer from buf at the specified offset.
415 * @tc.type: FUNC
416 * @tc.require:issueI5J5Z3
417 */
418 HWTEST_F(NativeEngineTest, WriteUInt32LEAndReadUInt32LETest001, testing::ext::TestSize.Level0)
419 {
420 OHOS::buffer::Buffer *buf = new OHOS::buffer::Buffer();
421 buf->Init(4);
422 FillZero(buf, 4);
423 buf->WriteUInt32LE(0x12345678, 0);
424 int32_t res = buf->ReadUInt32LE(0);
425 ASSERT_EQ(res, 0x12345678);
426 }
427
428 /**
429 * @tc.name: WriteUInt32LEAndReadUInt32LETest002
430 * @tc.desc: Writes value to buf at the specified offset as little-endian.
431 * The value must be a valid unsigned 32-bit integer.
432 * Reads an unsigned, little-endian 32-bit integer from buf at the specified offset.
433 * @tc.type: FUNC
434 * @tc.require:issueI5J5Z3
435 */
436 HWTEST_F(NativeEngineTest, WriteUInt32LEAndReadUInt32LETest002, testing::ext::TestSize.Level0)
437 {
438 OHOS::buffer::Buffer *buf = new OHOS::buffer::Buffer();
439 buf->Init(5);
440 FillZero(buf, 5);
441 buf->WriteUInt32LE(0x12345678, 1);
442 int32_t res = buf->ReadUInt32LE(0);
443 ASSERT_EQ(res, 0x34567800);
444 }
445
446 /**
447 * @tc.name: WriteUInt32LEAndReadUInt32LETest003
448 * @tc.desc: Writes value to buf at the specified offset as little-endian.
449 * The value must be a valid unsigned 32-bit integer.
450 * Reads an unsigned, little-endian 32-bit integer from buf at the specified offset.
451 * @tc.type: FUNC
452 * @tc.require:issueI5J5Z3
453 */
454 HWTEST_F(NativeEngineTest, WriteUInt32LEAndReadUInt32LETest003, testing::ext::TestSize.Level0)
455 {
456 OHOS::buffer::Buffer *buf = new OHOS::buffer::Buffer();
457 buf->Init(5);
458 FillZero(buf, 5);
459 buf->WriteUInt32LE(0x12345678, 1);
460 int32_t res = buf->ReadUInt32LE(1);
461 ASSERT_EQ(res, 0x12345678);
462 }
463
464 /**
465 * @tc.name: ReadBytesTest001
466 * @tc.desc: Read value from buffer.
467 * @tc.type: FUNC
468 * @tc.require:issueI5J5Z3
469 */
470 HWTEST_F(NativeEngineTest, ReadBytesTest001, testing::ext::TestSize.Level0)
471 {
472 OHOS::buffer::Buffer *buf = new OHOS::buffer::Buffer();
473 buf->Init(4);
474 FillZero(buf, 4);
475 buf->WriteUInt32BE(0x12345678, 0);
476 uint32_t length = buf->GetLength();
477 uint8_t data[length];
478 buf->ReadBytes(data, 0, length);
479 uint8_t res[4] = {0x12, 0x34, 0x56, 0x78};
480 for (size_t i = 0; i < length; i++) {
481 ASSERT_EQ(data[i], res[i]);
482 }
483 }
484
485 /**
486 * @tc.name: ReadBytesTest002
487 * @tc.desc: Read value error when length is zero.
488 * @tc.type: FUNC
489 * @tc.require:issueI5J5Z3
490 */
491 HWTEST_F(NativeEngineTest, ReadBytesTest002, testing::ext::TestSize.Level0)
492 {
493 OHOS::buffer::Buffer *buf = new OHOS::buffer::Buffer();
494 buf->Init(4);
495 FillZero(buf, 4);
496 buf->WriteUInt32BE(0x12345678, 0);
497 uint32_t length = buf->GetLength();
498 uint8_t data[length];
499 buf->ReadBytes(data, 0, 0);
500 uint8_t res[4] = {0x12, 0x34, 0x56, 0x78};
501 ASSERT_NE(data[0], res[0]);
502 }
503
504 /**
505 * @tc.name: WriteStringTest001
506 * @tc.desc: Write string to buffer.
507 * @tc.type: FUNC
508 * @tc.require:issueI5J5Z3
509 */
510 HWTEST_F(NativeEngineTest, WriteStringTest001, testing::ext::TestSize.Level0)
511 {
512 OHOS::buffer::Buffer *buf = new OHOS::buffer::Buffer();
513 buf->Init(10);
514 std::string str = "1234567890";
515 unsigned int size = buf->WriteString(str, 10);
516 ASSERT_EQ(size, 10);
517 }
518
519 /**
520 * @tc.name: WriteStringTest002
521 * @tc.desc: Write string to buffer.
522 * @tc.type: FUNC
523 * @tc.require:issueI5J5Z3
524 */
525 HWTEST_F(NativeEngineTest, WriteStringTest002, testing::ext::TestSize.Level0)
526 {
527 OHOS::buffer::Buffer *buf = new OHOS::buffer::Buffer();
528 buf->Init(9);
529 std::string str = "123456789";
530 unsigned int size = buf->WriteString(str, 9);
531 uint8_t data[size];
532 buf->ReadBytes(data, 0, size);
533 uint8_t value = 49;
534 for (size_t i = 0; i < size; i++) {
535 ASSERT_EQ(data[i], value);
536 value++;
537 }
538 }
539
540 /**
541 * @tc.name: WriteStringTest003
542 * @tc.desc: Write string to buffer.
543 * @tc.type: FUNC
544 * @tc.require:issueI5J5Z3
545 */
546 HWTEST_F(NativeEngineTest, WriteStringTest003, testing::ext::TestSize.Level0)
547 {
548 OHOS::buffer::Buffer *buf = new OHOS::buffer::Buffer();
549 buf->Init(9);
550 std::string str = "123456789";
551 unsigned int size = buf->WriteString(str, 0, 9);
552 uint8_t data[size];
553 buf->ReadBytes(data, 0, size);
554 uint8_t value = 49;
555 for (size_t i = 0; i < size; i++) {
556 ASSERT_EQ(data[i], value);
557 value++;
558 }
559 }
560
561 /**
562 * @tc.name: WriteStringTest004
563 * @tc.desc: Write string to buffer.
564 * @tc.type: FUNC
565 * @tc.require:issueI5J5Z3
566 */
567 HWTEST_F(NativeEngineTest, WriteStringTest004, testing::ext::TestSize.Level0)
568 {
569 OHOS::buffer::Buffer *buf = new OHOS::buffer::Buffer();
570 buf->Init(9);
571 std::string str = "123456789";
572 unsigned int size = buf->WriteString(str, 2, 7);
573 uint8_t data[size];
574 buf->ReadBytes(data, 0, size);
575 uint8_t value = 49;
576 for (size_t i = 2; i < size; i++) {
577 ASSERT_EQ(data[i], value);
578 value++;
579 }
580 }
581
582 /**
583 * @tc.name: WriteStringTest005
584 * @tc.desc: Write string to buffer.
585 * @tc.type: FUNC
586 * @tc.require:issueI5J5Z3
587 */
588 HWTEST_F(NativeEngineTest, WriteStringTest005, testing::ext::TestSize.Level0)
589 {
590 OHOS::buffer::Buffer *buf = new OHOS::buffer::Buffer();
591 buf->Init(9);
592 std::string str = "123456789";
593 unsigned int size = buf->WriteString(str, 0, 9, "utf8");
594 uint8_t data[size];
595 buf->ReadBytes(data, 0, size);
596 uint8_t value = 49;
597 for (size_t i = 0; i < size; i++) {
598 ASSERT_EQ(data[i], value);
599 value++;
600 }
601 }
602
603 /**
604 * @tc.name: WriteStringTest006
605 * @tc.desc: Write string to buffer.
606 * @tc.type: FUNC
607 * @tc.require:issueI5J5Z3
608 */
609 HWTEST_F(NativeEngineTest, WriteStringTest006, testing::ext::TestSize.Level0)
610 {
611 OHOS::buffer::Buffer *buf = new OHOS::buffer::Buffer();
612 buf->Init(16);
613 FillZero(buf, 16);
614 std::string str = "12345678";
615 unsigned int size = buf->WriteString(str, 0, 16, "utf16le");
616 ASSERT_EQ(size, 16);
617 uint8_t data[size];
618 buf->ReadBytes(data, 0, size);
619 uint8_t value = 49;
620 for (size_t i = 0; i < size; i++) {
621 if (i % 2 == 0) {
622 ASSERT_EQ(data[i], value);
623 value++;
624 } else {
625 ASSERT_EQ(data[i], 0);
626 }
627 }
628 }
629
630 /**
631 * @tc.name: SubBufferTest001
632 * @tc.desc: Returns a new Buffer that references the same memory as the original,
633 * but offset and cropped by the start and end indices.
634 * @tc.type: FUNC
635 * @tc.require:issueI5J5Z3
636 */
637 HWTEST_F(NativeEngineTest, SubBufferTest001, testing::ext::TestSize.Level0)
638 {
639 OHOS::buffer::Buffer *buf = new OHOS::buffer::Buffer();
640 buf->SubBuffer(nullptr, 0, 10);
641
642 OHOS::buffer::Buffer *buf1 = new OHOS::buffer::Buffer();
643 buf1->Init(10);
644 FillZero(buf1, 10);
645 std::string str = "1234567890";
646 buf1->WriteString(str, 0, 10);
647 OHOS::buffer::Buffer *buf2 = new OHOS::buffer::Buffer();
648 buf2->SubBuffer(buf1, 0, 10);
649 buf1->ReadBytesForArrayBuffer(NULL, 0);
650
651 ASSERT_EQ(buf2->GetLength(), 10);
652 uint8_t data[11];
653 buf2->ReadBytes(data, 0, 10);
654 data[10] = 0;
655 ASSERT_STREQ(reinterpret_cast<char*>(data), str.c_str());
656 }
657
658 /**
659 * @tc.name: SubBufferTest002
660 * @tc.desc: Returns a new Buffer that references the same memory as the original,
661 * but offset and cropped by the start and end indices.
662 * @tc.type: FUNC
663 * @tc.require:issueI5J5Z3
664 */
665 HWTEST_F(NativeEngineTest, SubBufferTest002, testing::ext::TestSize.Level0)
666 {
667 OHOS::buffer::Buffer *buf1 = new OHOS::buffer::Buffer();
668 buf1->Init(10);
669 FillZero(buf1, 10);
670 std::string str = "1234567890";
671 buf1->WriteString(str, 0, 10);
672 OHOS::buffer::Buffer *buf2 = new OHOS::buffer::Buffer();
673 buf2->SubBuffer(buf1, 2, 10);
674 ASSERT_EQ(buf2->GetLength(), 8);
675 uint8_t data[9];
676 buf2->ReadBytes(data, 0, 8);
677 data[8] = 0;
678 ASSERT_STREQ(reinterpret_cast<char*>(data), "34567890");
679 }
680
681 /**
682 * @tc.name: CopyTest001
683 * @tc.desc: Copies data from a region of buf to a region in target,
684 * even if the target memory region overlaps with buf.
685 * @tc.type: FUNC
686 * @tc.require:issueI5J5Z3
687 */
688 HWTEST_F(NativeEngineTest, CopyTest001, testing::ext::TestSize.Level0)
689 {
690 OHOS::buffer::Buffer *buffer = new OHOS::buffer::Buffer();
691 buffer->Init(20);
692
693 OHOS::buffer::Buffer *buf = new OHOS::buffer::Buffer();
694 buf->Init(20);
695 buf->WriteString("this is a string", 16);
696
697 unsigned int tOffset = 1;
698 unsigned int sOffset = 0;
699 unsigned int tEnd = 16;
700 unsigned int sEnd = 16;
701 buf->Copy(buffer, tOffset, sOffset, sEnd);
702 uint8_t data[20] = {0};
703 buffer->ReadBytes(data, tOffset, tEnd);
704 ASSERT_STREQ(reinterpret_cast<char*>(data), "this is a string");
705 }
706
707 /**
708 * @tc.name: CopyTest002
709 * @tc.desc: Copies data with len 0
710 * @tc.type: FUNC
711 * @tc.require:issueI5J5Z3
712 */
713 HWTEST_F(NativeEngineTest, CopyTest002, testing::ext::TestSize.Level0)
714 {
715 OHOS::buffer::Buffer *buf = new OHOS::buffer::Buffer();
716 buf->Init(20);
717 std::string value = "YWJjZA";
718 ASSERT_EQ(buf->Copy(buf, 0, 2, 2), 0);
719 }
720
721 /**
722 * @tc.name: CopyTest003
723 * @tc.desc: Buffer Copy.
724 * @tc.type: FUNC
725 * @tc.require:issueI5J5Z3
726 */
727 HWTEST_F(NativeEngineTest, CopyTest003, testing::ext::TestSize.Level0)
728 {
729 OHOS::buffer::Buffer *buf = new OHOS::buffer::Buffer();
730 uint8_t data[10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
731 buf->Init(data, 0, 10);
732 uint32_t res = buf->Copy(buf, 0, 4, 10);
733 ASSERT_EQ(res, 6);
734 res = buf->Copy(buf, 0, 0, 0);
735 ASSERT_EQ(res, 0);
736 delete buf;
737 OHOS::buffer::Buffer *buf1 = new OHOS::buffer::Buffer();
738 res = buf1->Copy(buf1, 0, 4, 10);
739 delete buf1;
740 ASSERT_EQ(res, 0);
741 }
742
743 /**
744 * @tc.name: CompareTest001
745 * @tc.desc: Compares buf with target and returns a number indicating whether buf comes before, after,
746 * or is the same as target in sort order. Comparison is based on the actual sequence of bytes in each Buffer
747 * @tc.type: FUNC
748 * @tc.require:issueI5J5Z3
749 */
750 HWTEST_F(NativeEngineTest, CompareTest001, testing::ext::TestSize.Level0)
751 {
752 OHOS::buffer::Buffer *buffer = new OHOS::buffer::Buffer();
753 buffer->Init(20);
754 buffer->WriteString("this is a string", 16);
755
756 OHOS::buffer::Buffer *buf = new OHOS::buffer::Buffer();
757 buf->Init(20);
758 buf->WriteString("this is a string", 1, 16);
759
760 int result = buf->Compare(buffer, 0, 1, 16);
761 ASSERT_EQ(result, 0);
762 }
763
764 /**
765 * @tc.name: IndexOfTest001
766 * @tc.desc: The index of the first occurrence of value in buf.
767 * @tc.type: FUNC
768 * @tc.require:issueI5J5Z3
769 */
770 HWTEST_F(NativeEngineTest, IndexOfTest001, testing::ext::TestSize.Level0)
771 {
772 OHOS::buffer::Buffer *buf = new OHOS::buffer::Buffer();
773 buf->Init(20);
774 buf->WriteString("this is a string", 16);
775 uint64_t resultIndex = 0;
776 int index = buf->IndexOf("is", 0, 2, resultIndex);
777 ASSERT_EQ(index, -2);
778 ASSERT_EQ(resultIndex, 2);
779 }
780
781 /**
782 * @tc.name: IndexOfTest002
783 * @tc.desc: The index of the first occurrence of value in buf.
784 * @tc.type: FUNC
785 * @tc.require:issueI5J5Z3
786 */
787 HWTEST_F(NativeEngineTest, IndexOfTest002, testing::ext::TestSize.Level0)
788 {
789 OHOS::buffer::Buffer *buf = new OHOS::buffer::Buffer();
790 buf->Init(7);
791 buf->WriteString("3363333", 7);
792 uint64_t resultIndex = 0;
793 int index = buf->IndexOf("36", 0, 2, resultIndex);
794 ASSERT_EQ(index, -2);
795 ASSERT_EQ(resultIndex, 1);
796 }
797
798 /**
799 * @tc.name: IndexOfTest003
800 * @tc.desc: The index of the first occurrence of value in buf.
801 * @tc.type: FUNC
802 * @tc.require:issueI5J5Z3
803 */
804 HWTEST_F(NativeEngineTest, IndexOfTest003, testing::ext::TestSize.Level0)
805 {
806 OHOS::buffer::Buffer *buf = new OHOS::buffer::Buffer();
807 buf->Init(12);
808 buf->WriteString("322362326233", 12);
809 uint64_t resultIndex = 0;
810 int index = buf->IndexOf("2623", 0, 4, resultIndex);
811 ASSERT_EQ(index, -2);
812 ASSERT_EQ(resultIndex, 7);
813 }
814
815 /**
816 * @tc.name: IndexOfTest004
817 * @tc.desc: Data is not in buf.
818 * @tc.type: FUNC
819 * @tc.require:issueI5J5Z3
820 */
821 HWTEST_F(NativeEngineTest, IndexOfTest004, testing::ext::TestSize.Level0)
822 {
823 OHOS::buffer::Buffer *buf = new OHOS::buffer::Buffer();
824 buf->Init(12);
825 buf->WriteString("322362326233", 12);
826 uint64_t resultIndex = 0;
827 int index = buf->IndexOf("99", 0, 2, resultIndex);
828 ASSERT_EQ(index, -1);
829 }
830
831 /**
832 * @tc.name: LastIndexOfTest001
833 * @tc.desc: The index of the last occurrence of value in buf.
834 * @tc.type: FUNC
835 * @tc.require:issueI5J5Z3
836 */
837 HWTEST_F(NativeEngineTest, LastIndexOfTest001, testing::ext::TestSize.Level0)
838 {
839 OHOS::buffer::Buffer *buf = new OHOS::buffer::Buffer();
840 buf->Init(20);
841 buf->WriteString("this is a string", 16);
842 int index = buf->LastIndexOf("is", 0, 2);
843 ASSERT_EQ(index, 5);
844 }
845
846 /**
847 * @tc.name: LastIndexOfTest002
848 * @tc.desc: The index of the last occurrence of value in buf.
849 * @tc.type: FUNC
850 * @tc.require:issueI5J5Z3
851 */
852 HWTEST_F(NativeEngineTest, LastIndexOfTest002, testing::ext::TestSize.Level0)
853 {
854 OHOS::buffer::Buffer *buf = new OHOS::buffer::Buffer();
855 buf->Init(7);
856 buf->WriteString("3363333", 7);
857 int index = buf->LastIndexOf("36", 0, 2);
858 ASSERT_EQ(index, 1);
859 }
860
861 /**
862 * @tc.name: LastIndexOfTest003
863 * @tc.desc: The index of the last occurrence of value in buf.
864 * @tc.type: FUNC
865 * @tc.require:issueI5J5Z3
866 */
867 HWTEST_F(NativeEngineTest, LastIndexOfTest003, testing::ext::TestSize.Level0)
868 {
869 OHOS::buffer::Buffer *buf = new OHOS::buffer::Buffer();
870 buf->Init(11);
871 buf->WriteString("32236326233", 11);
872 int index = buf->LastIndexOf("236", 0, 3);
873 ASSERT_EQ(index, 2);
874 }
875
876 /**
877 * @tc.name: LastIndexOfTest004
878 * @tc.desc: The index of the last occurrence of value in buf.
879 * @tc.type: FUNC
880 * @tc.require:issueI5J5Z3
881 */
882 HWTEST_F(NativeEngineTest, LastIndexOfTest004, testing::ext::TestSize.Level0)
883 {
884 OHOS::buffer::Buffer *buf = new OHOS::buffer::Buffer();
885 buf->Init(12);
886 buf->WriteString("322362326233", 12);
887 int index = buf->LastIndexOf("2236", 0, 4);
888 ASSERT_EQ(index, 1);
889 }
890
891 /**
892 * @tc.name: LastIndexOfTest005
893 * @tc.desc: The index of the last occurrence of value in buf.
894 * @tc.type: FUNC
895 * @tc.require:issueI5J5Z3
896 */
897 HWTEST_F(NativeEngineTest, LastIndexOfTest005, testing::ext::TestSize.Level0)
898 {
899 OHOS::buffer::Buffer *buf = new OHOS::buffer::Buffer();
900 buf->Init(12);
901 buf->WriteString("322362326233", 12);
902 int index = buf->LastIndexOf("136", 0, 3);
903 ASSERT_EQ(index, -1);
904 }
905
906
907 /**
908 * @tc.name: ToBase64Test001
909 * @tc.desc: Convert the contents of the buffer into a string in Base64 format.
910 * @tc.type: FUNC
911 * @tc.require:issueI5J5Z3
912 */
913 HWTEST_F(NativeEngineTest, ToBase64Test001, testing::ext::TestSize.Level0)
914 {
915 OHOS::buffer::Buffer *buf = new OHOS::buffer::Buffer();
916 buf->Init(20);
917 buf->WriteString("this is a string", 16);
918 std::string base64Str = buf->ToBase64(0, 16);
919 ASSERT_STREQ(base64Str.c_str(), "dGhpcyBpcyBhIHN0cmluZw==");
920 }
921
922 /**
923 * @tc.name: ToBase64Test002
924 * @tc.desc: Convert the contents of the buffer into a string in Base64 format.
925 * @tc.type: FUNC
926 * @tc.require:issueI5J5Z3
927 */
928 HWTEST_F(NativeEngineTest, ToBase64Test002, testing::ext::TestSize.Level0)
929 {
930 OHOS::buffer::Buffer *buf = new OHOS::buffer::Buffer();
931 buf->Init(30);
932 buf->WriteString("this is a big string", 20);
933 std::string base64Str = buf->ToBase64(0, 20);
934 ASSERT_STREQ(base64Str.c_str(), "dGhpcyBpcyBhIGJpZyBzdHJpbmc=");
935 }
936
937 /**
938 * @tc.name: ToBase64Test003
939 * @tc.desc: Convert to base64 with length 0.
940 * @tc.type: FUNC
941 * @tc.require:issueI5J5Z3
942 */
943 HWTEST_F(NativeEngineTest, ToBase64Test003, testing::ext::TestSize.Level0)
944 {
945 OHOS::buffer::Buffer *buf = new OHOS::buffer::Buffer();
946 buf->Init(30);
947 buf->WriteString("this is a big string", 20);
948 std::string base64Str = buf->ToBase64(0, 0);
949 ASSERT_STREQ(base64Str.c_str(), "");
950 }
951
952 /**
953 * @tc.name: ToBase64UrlTest001
954 * @tc.desc: Convert the contents of the buffer into a string in Base64 format.
955 * @tc.type: FUNC
956 * @tc.require:issueI5J5Z3
957 */
958 HWTEST_F(NativeEngineTest, ToBase64UrlTest001, testing::ext::TestSize.Level0)
959 {
960 OHOS::buffer::Buffer *buf = new OHOS::buffer::Buffer();
961 buf->Init(20);
962 buf->WriteString("this is a string", 16);
963 std::string base64Str = buf->ToBase64Url(0, 16);
964 std::string stra = OHOS::buffer::Base64Encode(nullptr, 10, OHOS::buffer::BASE64URL);
965 ASSERT_STREQ(base64Str.c_str(), "dGhpcyBpcyBhIHN0cmluZw");
966 }
967
968 /**
969 * @tc.name: GetEncodingTypeTest001
970 * @tc.desc: Get encoding type.
971 * @tc.type: FUNC
972 * @tc.require:issueI5J5Z3
973 */
974 HWTEST_F(NativeEngineTest, GetEncodingTypeTest001, testing::ext::TestSize.Level0)
975 {
976 std::map <std::string, int> _typeMap =
977 {
978 {"hex", OHOS::buffer::HEX},
979 {"base64url", OHOS::buffer::BASE64URL},
980 {"ascii", OHOS::buffer::ASCII},
981 {"base64", OHOS::buffer::BASE64},
982 {"latin1", OHOS::buffer::LATIN1},
983 {"binary", OHOS::buffer::BINARY},
984 {"utf16le", OHOS::buffer::UTF16LE},
985 {"utf8", OHOS::buffer::UTF8},
986 };
987
988 for (auto item =_typeMap.begin(); item != _typeMap.end(); item++)
989 {
990 std::string type = item->first;
991 OHOS::buffer::EncodingType et = OHOS::buffer::Buffer::GetEncodingType(type);
992 ASSERT_EQ(et, item->second);
993 }
994 }
995
996 /**
997 * @tc.name: SetArrayTest001
998 * @tc.desc: Put the contents of the array into the buffer.
999 * @tc.type: FUNC
1000 * @tc.require:issueI5J5Z3
1001 */
1002 HWTEST_F(NativeEngineTest, SetArrayTest001, testing::ext::TestSize.Level0)
1003 {
1004 OHOS::buffer::Buffer *buffer = new OHOS::buffer::Buffer();
1005 buffer->Init(20);
1006 std::vector<uint8_t> numbers;
1007 for (int i = 0; i < 10; i++) {
1008 numbers.push_back(i);
1009 }
1010 buffer->SetArray(numbers);
1011 unsigned int offset = 0;
1012 unsigned int end = 10;
1013 uint8_t data[20] = {0};
1014 buffer->ReadBytes(data, offset, end);
1015 for (int j = 0; j < 10; j++) {
1016 ASSERT_EQ(data[j], j);
1017 }
1018 }
1019
1020 /**
1021 * @tc.name: FillBufferTest001
1022 * @tc.desc: Fill the buffer with the buffer object
1023 * @tc.type: FUNC
1024 * @tc.require:issueI5J5Z3
1025 */
1026 HWTEST_F(NativeEngineTest, FillBufferTest001, testing::ext::TestSize.Level0)
1027 {
1028 OHOS::buffer::Buffer *buffer = new OHOS::buffer::Buffer();
1029 buffer->Init(10);
1030 std::vector<uint8_t> numbers;
1031 for (int i = 0; i < 10; i++) {
1032 numbers.push_back(i);
1033 }
1034 buffer->SetArray(numbers);
1035 unsigned int offset = 0;
1036 unsigned int end = 10;
1037 OHOS::buffer::Buffer *buf = new OHOS::buffer::Buffer();
1038 buf->Init(20);
1039 buf->FillBuffer(buffer, offset, end);
1040 uint8_t data[20] = {0};
1041 buf->ReadBytes(data, offset, end);
1042 for (int j = 0; j < 10; j++) {
1043 ASSERT_EQ(data[j], j);
1044 }
1045 }
1046
1047 /**
1048 * @tc.name: FillNumberTest001
1049 * @tc.desc: Fill the buffer with the number
1050 * @tc.type: FUNC
1051 * @tc.require:issueI5J5Z3
1052 */
1053 HWTEST_F(NativeEngineTest, FillNumberTest001, testing::ext::TestSize.Level0)
1054 {
1055 OHOS::buffer::Buffer *buf = new OHOS::buffer::Buffer();
1056 buf->Init(20);
1057 std::vector<uint8_t> numbers;
1058 for (int i = 0; i < 10; i++) {
1059 numbers.push_back(i);
1060 }
1061 unsigned int offset = 0;
1062 unsigned int end = 10;
1063 buf->FillNumber(numbers, offset, end);
1064 uint8_t data[20] = {0};
1065 buf->ReadBytes(data, offset, end);
1066 for (int j = 0; j < 10; j++) {
1067 ASSERT_EQ(data[j], j);
1068 }
1069 }
1070
1071 /**
1072 * @tc.name: FillStringTest001
1073 * @tc.desc: Fill the buffer with the string
1074 * @tc.type: FUNC
1075 * @tc.require:issueI5J5Z3
1076 */
1077 HWTEST_F(NativeEngineTest, FillStringTest001, testing::ext::TestSize.Level0)
1078 {
1079 OHOS::buffer::Buffer *buf = new OHOS::buffer::Buffer();
1080 buf->Init(20);
1081 std::string value = "abcd";
1082 std::string encoding = "ascii";
1083 buf->FillString(value, 1, 1, encoding);
1084
1085 unsigned int offset = 0;
1086 unsigned int end = 10;
1087 buf->FillString(value, offset, end, encoding);
1088 uint8_t data[20] = {0};
1089 buf->ReadBytes(data, offset, end);
1090 ASSERT_STREQ(reinterpret_cast<char*>(data), "abcdabcdab");
1091 }
1092
1093 /**
1094 * @tc.name: FillStringTest002
1095 * @tc.desc: Fill the buffer with the string
1096 * @tc.type: FUNC
1097 * @tc.require:issueI5J5Z3
1098 */
1099 HWTEST_F(NativeEngineTest, FillStringTest002, testing::ext::TestSize.Level0)
1100 {
1101 OHOS::buffer::Buffer *buf = new OHOS::buffer::Buffer();
1102 buf->Init(20);
1103 std::string value = "扡摣";
1104 unsigned int offset = 0;
1105 unsigned int end = 10;
1106 std::string encoding = "utf16le";
1107 buf->FillString(value, offset, end, encoding);
1108 uint8_t data[20] = {0};
1109 buf->ReadBytes(data, offset, end);
1110 ASSERT_STREQ(reinterpret_cast<char*>(data), "abcdabcdab");
1111 }
1112
1113 /**
1114 * @tc.name: FillStringTest003
1115 * @tc.desc: Fill the buffer with the string
1116 * @tc.type: FUNC
1117 * @tc.require:issueI5J5Z3
1118 */
1119 HWTEST_F(NativeEngineTest, FillStringTest003, testing::ext::TestSize.Level0)
1120 {
1121 OHOS::buffer::Buffer *buf = new OHOS::buffer::Buffer();
1122 buf->Init(20);
1123 std::string value = "YWJjZA";
1124 unsigned int offset = 0;
1125 unsigned int end = 10;
1126 std::string encoding = "base64";
1127 buf->FillString(value, offset, end, encoding);
1128 uint8_t data[20] = {0};
1129 buf->ReadBytes(data, offset, end);
1130 ASSERT_STREQ(reinterpret_cast<char*>(data), "abcdabcdab");
1131 }
1132
1133 /**
1134 * @tc.name: BlobConstructorTest001
1135 * @tc.desc: Blob Constructor
1136 * @tc.type: FUNC
1137 * @tc.require:issueI5J5Z3
1138 */
1139 HWTEST_F(NativeEngineTest, BlobConstructorTest001, testing::ext::TestSize.Level0)
1140 {
1141 OHOS::buffer::Blob *blob = new OHOS::buffer::Blob();
1142 uint8_t data[4] = {1, 2, 3, 4};
1143 blob->Init(data, 4);
1144 ASSERT_EQ(blob->GetLength(), 4);
1145 }
1146
1147 /**
1148 * @tc.name: BlobConstructorTest002
1149 * @tc.desc: Blob Constructor
1150 * @tc.type: FUNC
1151 * @tc.require:issueI5J5Z3
1152 */
1153 HWTEST_F(NativeEngineTest, BlobConstructorTest002, testing::ext::TestSize.Level0)
1154 {
1155 OHOS::buffer::Blob *blob = new OHOS::buffer::Blob();
1156 uint8_t data[4] = {1, 2, 3, 4};
1157 blob->Init(data, 4);
1158
1159 OHOS::buffer::Blob *blob2 = new OHOS::buffer::Blob();
1160 blob2->Init(blob, 0);
1161
1162 ASSERT_EQ(blob2->GetLength(), 4);
1163 }
1164
1165 /**
1166 * @tc.name: BlobConstructorTest003
1167 * @tc.desc: Blob Constructor
1168 * @tc.type: FUNC
1169 * @tc.require:issueI5J5Z3
1170 */
1171 HWTEST_F(NativeEngineTest, BlobConstructorTest003, testing::ext::TestSize.Level0)
1172 {
1173 OHOS::buffer::Blob *blob = new OHOS::buffer::Blob();
1174 uint8_t data[4] = {1, 2, 3, 4};
1175 blob->Init(data, 4);
1176
1177 OHOS::buffer::Blob *blob2 = new OHOS::buffer::Blob();
1178 blob2->Init(blob, 1, 4);
1179
1180 ASSERT_EQ(blob2->GetLength(), 3);
1181 }
1182
1183 /**
1184 * @tc.name: BlobConstructorTest004
1185 * @tc.desc: Blob Constructor
1186 * @tc.type: FUNC
1187 * @tc.require:issueI5J5Z3
1188 */
1189 HWTEST_F(NativeEngineTest, BlobConstructorTest004, testing::ext::TestSize.Level0)
1190 {
1191 OHOS::buffer::Blob *blob = new OHOS::buffer::Blob();
1192 uint8_t data[1] = {1};
1193 OHOS::buffer::Blob *blob1 = new OHOS::buffer::Blob();
1194 blob->Init(blob1, 1, 0);
1195 blob->Init(blob1, 1, -1);
1196 blob->Init(blob1, -1, 1);
1197 blob->Init(nullptr, 0, 1);
1198 blob->Init(data, 1);
1199 ASSERT_EQ(blob->GetLength(), 1);
1200 delete blob;
1201 blob = nullptr;
1202 delete blob1;
1203 blob1 = nullptr;
1204 }
1205
1206 /**
1207 * @tc.name: BlobDestructorTest001
1208 * @tc.desc: Blob Destructor.
1209 * @tc.type: FUNC
1210 * @tc.require:issueI5J5Z3
1211 */
1212 HWTEST_F(NativeEngineTest, BlobDestructorTest001, testing::ext::TestSize.Level0)
1213 {
1214 OHOS::buffer::Blob *blob = new OHOS::buffer::Blob();
1215 uint8_t data[4] = {1, 2, 3, 4};
1216 blob->Init(data, 4);
1217 delete blob;
1218 }
1219
1220 /**
1221 * @tc.name: BlobGetByteTest001
1222 * @tc.desc: Get a byte in blob
1223 * @tc.type: FUNC
1224 * @tc.require:issueI5J5Z3
1225 */
1226 HWTEST_F(NativeEngineTest, BlobGetByteTest001, testing::ext::TestSize.Level0)
1227 {
1228 OHOS::buffer::Blob *blob = new OHOS::buffer::Blob();
1229 uint8_t data[4] = {1, 2, 3, 4};
1230 blob->Init(data, 4);
1231
1232 uint8_t byte = blob->GetByte(2);
1233
1234 ASSERT_EQ(byte, 3);
1235 }
1236
1237 /**
1238 * @tc.name: BlobGetRawTest001
1239 * @tc.desc: Get the raw in blob
1240 * @tc.type: FUNC
1241 * @tc.require:issueI5J5Z3
1242 */
1243 HWTEST_F(NativeEngineTest, BlobGetRawTest001, testing::ext::TestSize.Level0)
1244 {
1245 OHOS::buffer::Blob *blob = new OHOS::buffer::Blob();
1246 uint8_t data[4] = {1, 2, 3, 4};
1247 blob->Init(data, 4);
1248
1249 uint8_t *raw = blob->GetRaw();
1250
1251 ASSERT_TRUE(raw != nullptr);
1252 }
1253
1254 /**
1255 * @tc.name: BlobGetLengthTest001
1256 * @tc.desc: Get the length in blob
1257 * @tc.type: FUNC
1258 * @tc.require:issueI5J5Z3
1259 */
1260 HWTEST_F(NativeEngineTest, BlobGetLengthTest001, testing::ext::TestSize.Level0)
1261 {
1262 OHOS::buffer::Blob *blob = new OHOS::buffer::Blob();
1263 uint8_t data[4] = {1, 2, 3, 4};
1264 blob->Init(data, 4);
1265
1266 unsigned int len = blob->GetLength();
1267
1268 ASSERT_EQ(len, 4);
1269 }
1270
1271 /**
1272 * @tc.name: BlobGetLengthTest001
1273 * @tc.desc: Read blob object bytes
1274 * @tc.type: FUNC
1275 * @tc.require:issueI5J5Z3
1276 */
1277 HWTEST_F(NativeEngineTest, BlobReadBytesTest001, testing::ext::TestSize.Level0)
1278 {
1279 OHOS::buffer::Blob *blob = new OHOS::buffer::Blob();
1280 uint8_t data[10] = {0};
1281 for (int i = 0; i < 10; i++) {
1282 data[i] = i;
1283 }
1284 blob->Init(data, 10);
1285
1286 uint8_t dat[10] = {0};
1287 blob->ReadBytes(dat, 10);
1288
1289 for (int i = 0; i < 10; i++) {
1290 ASSERT_EQ(dat[i], i);
1291 }
1292 }
1293
1294 /**
1295 * @tc.name: Utf8ToUtf16BETest001
1296 * @tc.desc: convert utf8 bytes to utf16 bytes
1297 * @tc.type: FUNC
1298 * @tc.require:issueI5J5Z3
1299 */
1300 HWTEST_F(NativeEngineTest, Utf8ToUtf16BETest001, testing::ext::TestSize.Level0)
1301 {
1302 std::string str8 = "";
1303 // one byte
1304 str8.append(1, 0x41);
1305 // two bytes
1306 str8.append(1, 0xC3);
1307 str8.append(1, 0x84);
1308 // three bytes
1309 str8.append(1, 0xE5);
1310 str8.append(1, 0x88);
1311 str8.append(1, 0x98);
1312 // four bytes
1313 str8.append(1, 0xf0);
1314 str8.append(1, 0x9f);
1315 str8.append(1, 0x90);
1316 str8.append(1, 0x85);
1317
1318 // another four bytes
1319 str8.append(1, 0xf0);
1320 str8.append(1, 0x8f);
1321 str8.append(1, 0x90);
1322 str8.append(1, 0x85);
1323
1324 bool isOk = false;
1325 std::u16string str16 = OHOS::buffer::Utf8ToUtf16BE(str8, &isOk);
1326
1327 char16_t results[] = {0x41, 0xc4, 0x5218, 0xd83d, 0xdc05, 0xf405};
1328 for (int i = 0; i < 6; i++) {
1329 ASSERT_EQ(results[i], str16[i]);
1330 }
1331 }
1332
1333 /**
1334 * @tc.name: HexDecodeTest001
1335 * @tc.desc: decode a hex string
1336 * @tc.type: FUNC
1337 * @tc.require:issueI5J5Z3
1338 */
1339 HWTEST_F(NativeEngineTest, HexDecodeTest001, testing::ext::TestSize.Level0)
1340 {
1341 std::string ret = OHOS::buffer::HexDecode("313g");
1342 ASSERT_EQ(ret, "1");
1343 }
1344
1345 /**
1346 * @tc.name: Utf16BEToLETest001
1347 * @tc.desc: Utf16BEToLE
1348 * @tc.type: FUNC
1349 * @tc.require:issueI5J5Z3
1350 */
1351 HWTEST_F(NativeEngineTest, Utf16BEToLETest001, testing::ext::TestSize.Level0)
1352 {
1353 std::u16string wstrBE = u"\x0041\x0042\x0043";
1354 std::u16string re = OHOS::buffer::Utf16BEToLE(wstrBE);
1355 char16_t results[] = {0x4100, 0x4200, 0x4300};
1356 for (int i = 0; i < 3; i++) {
1357 ASSERT_EQ(results[i], re[i]);
1358 }
1359 }
1360
1361 /**
1362 * @tc.name: Base64EncodeTest001
1363 * @tc.desc: Base64 encode with max size_t
1364 * @tc.type: FUNC
1365 * @tc.require:issueI5J5Z3
1366 */
1367 HWTEST_F(NativeEngineTest, Base64EncodeTest001, testing::ext::TestSize.Level0)
1368 {
1369 unsigned char data[] = {0x1A, 0x2B, 0x3C, 0x4D};
1370 size_t len = std::numeric_limits<size_t>::max();
1371 std::string stra = OHOS::buffer::Base64Encode(data, len, OHOS::buffer::BASE64URL);
1372 ASSERT_EQ(stra, "");
1373 }
1374
1375 /**
1376 * @tc.name: Base64EncodeTest002
1377 * @tc.desc: Base64 encode
1378 * @tc.type: FUNC
1379 * @tc.require:issueI5J5Z3
1380 */
1381 HWTEST_F(NativeEngineTest, Base64EncodeTest002, testing::ext::TestSize.Level0)
1382 {
1383 std::string value = "abc";
1384 std::string strb = OHOS::buffer::Base64Decode(value, OHOS::buffer::BASE64URL);
1385
1386 unsigned char data[] = {0x1A, 0x2B, 0x3C, 0x4D};
1387 std::string stra = OHOS::buffer::Base64Encode(data, 4, OHOS::buffer::BASE64URL);
1388 ASSERT_EQ(stra, "Gis8TQ");
1389 }
1390
1391 /**
1392 * @tc.name: GetGoodSuffixLengthByLastCharTest001
1393 * @tc.desc: Get good suffix length by last char
1394 * @tc.type: FUNC
1395 * @tc.require:issueI5J5Z3
1396 */
1397 HWTEST_F(NativeEngineTest, GetGoodSuffixLengthByLastCharTest001, testing::ext::TestSize.Level0)
1398 {
1399 uint8_t pat[] = "ababcab";
1400 int patLen = sizeof(pat) - 1;
1401 int patIndex = 4;
1402
1403 int length = OHOS::buffer::GetGoodSuffixLengthByLastChar(pat, patIndex, patLen);
1404 ASSERT_EQ(length, 3);
1405 }
1406
1407 /**
1408 * @tc.name: FindLastIndexTest001
1409 * @tc.desc: Find last index with error
1410 * @tc.type: FUNC
1411 * @tc.require:issueI5J5Z3
1412 */
1413 HWTEST_F(NativeEngineTest, FindLastIndexTest001, testing::ext::TestSize.Level0)
1414 {
1415 int rel1 = OHOS::buffer::FindLastIndex(nullptr, nullptr, 2, 1);
1416 ASSERT_EQ(rel1, -1);
1417
1418 uint8_t data[] = "abc";
1419 int rel2 = OHOS::buffer::FindLastIndex(data, data, 2, 0);
1420 ASSERT_EQ(rel2, -1);
1421 }
1422
1423 /**
1424 * @tc.name: FindIndex005
1425 * @tc.desc: Find index with error.
1426 * @tc.type: FUNC
1427 * @tc.require:issueI5J5Z3
1428 */
1429 HWTEST_F(NativeEngineTest, FindIndex005, testing::ext::TestSize.Level0)
1430 {
1431 int rel1 = OHOS::buffer::FindIndex(nullptr, nullptr, 2, 1);
1432 ASSERT_EQ(rel1, -1);
1433
1434 uint8_t data[] = "abc";
1435 int rel2 = OHOS::buffer::FindIndex(data, data, 2, 0);
1436 ASSERT_EQ(rel2, -1);
1437 }
1438
1439 /**
1440 * @tc.name: FindIndex001
1441 * @tc.desc: Buffer FindIndex.
1442 * @tc.type: FUNC
1443 * @tc.require:issueI5J5Z3
1444 */
1445 HWTEST_F(NativeEngineTest, FindIndex001, testing::ext::TestSize.Level0)
1446 {
1447 OHOS::buffer::Buffer *buf = new OHOS::buffer::Buffer();
1448 buf->Init(15);
1449 buf->WriteString("This is a buffer", 15);
1450 uint64_t resultIndex = 0;
1451 int index = buf->IndexOf("2623", 0, 4, resultIndex);
1452 ASSERT_EQ(index, -1);
1453 }
1454
1455 /**
1456 * @tc.name: FindIndex002
1457 * @tc.desc: Buffer FindIndex.
1458 * @tc.type: FUNC
1459 * @tc.require:issueI5J5Z3
1460 */
1461 HWTEST_F(NativeEngineTest, FindIndex002, testing::ext::TestSize.Level0)
1462 {
1463 OHOS::buffer::Buffer *buf = new OHOS::buffer::Buffer();
1464 buf->Init(15);
1465 buf->WriteString("This is a buffer", 15);
1466 uint64_t resultIndex = 0;
1467 int index = buf->IndexOf("f", 0, 1, resultIndex);
1468 ASSERT_EQ(index, -2);
1469 ASSERT_EQ(resultIndex, 12);
1470 }
1471
1472 /**
1473 * @tc.name: FindIndex003
1474 * @tc.desc: Buffer FindIndex.
1475 * @tc.type: FUNC
1476 * @tc.require:issueI5J5Z3
1477 */
1478 HWTEST_F(NativeEngineTest, FindIndex003, testing::ext::TestSize.Level0)
1479 {
1480 OHOS::buffer::Buffer *buf = new OHOS::buffer::Buffer();
1481 buf->Init(15);
1482 buf->WriteString("23456789abcdefg", 15);
1483 uint64_t resultIndex = 0;
1484 int index = buf->IndexOf("3", 0, 1, resultIndex);
1485 ASSERT_EQ(index, -2);
1486 ASSERT_EQ(resultIndex, 1);
1487 }
1488
1489 /**
1490 * @tc.name: FindIndex004
1491 * @tc.desc: Buffer FindIndex.
1492 * @tc.type: FUNC
1493 * @tc.require:issueI5J5Z3
1494 */
1495 HWTEST_F(NativeEngineTest, FindIndex004, testing::ext::TestSize.Level0)
1496 {
1497 OHOS::buffer::Buffer *buf = new OHOS::buffer::Buffer();
1498 buf->Init(15);
1499 buf->WriteString("23456789abcdefg", 15);
1500 uint64_t resultIndex = 0;
1501 int index = buf->IndexOf("3", 10, 1, resultIndex);
1502 ASSERT_EQ(index, -1);
1503 }