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 "utils/log.h"
25
26
27 #define ASSERT_CHECK_CALL(call) \
28 { \
29 ASSERT_EQ(call, napi_ok); \
30 }
31
32 #define ASSERT_CHECK_VALUE_TYPE(env, value, type) \
33 { \
34 napi_valuetype valueType = napi_undefined; \
35 ASSERT_TRUE(value != nullptr); \
36 ASSERT_CHECK_CALL(napi_typeof(env, value, &valueType)); \
37 ASSERT_EQ(valueType, type); \
38 }
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 * @tc.name: ConstructorTest003
77 * @tc.desc: Buffer Constructor.
78 * @tc.type: FUNC
79 * @tc.require:issueI5J5Z3
80 */
81 HWTEST_F(NativeEngineTest, ConstructorTest003, testing::ext::TestSize.Level0)
82 {
83 OHOS::buffer::Buffer *poolBuffer = new OHOS::buffer::Buffer();
84 poolBuffer->Init(1024 * 8);
85 OHOS::buffer::Buffer *buf2 = new OHOS::buffer::Buffer();
86 buf2->Init(poolBuffer, 0, 5);
87 ASSERT_EQ(buf2->GetLength(), 5);
88 ASSERT_EQ(buf2->GetByteOffset(), 0);
89
90 OHOS::buffer::Buffer *buf3 = new OHOS::buffer::Buffer();
91 buf3->Init(poolBuffer, 5, 6);
92 ASSERT_EQ(buf3->GetLength(), 6);
93 ASSERT_EQ(buf3->GetByteOffset(), 5);
94 }
95
96 /**
97 * @tc.name: ConstructorTest004
98 * @tc.desc: Buffer Constructor.
99 * @tc.type: FUNC
100 * @tc.require:issueI5J5Z3
101 */
102 HWTEST_F(NativeEngineTest, ConstructorTest004, testing::ext::TestSize.Level0)
103 {
104 OHOS::buffer::Buffer *buf2 = new OHOS::buffer::Buffer();
105 uint8_t data[4] = {1, 2, 3, 4};
106 buf2->Init(data, 0, 4);
107 ASSERT_EQ(buf2->GetLength(), 4);
108 ASSERT_EQ(buf2->GetByteOffset(), 0);
109 }
110
111 /**
112 * @tc.name: DestructorTest001
113 * @tc.desc: Buffer Destructor.
114 * @tc.type: FUNC
115 * @tc.require:issueI5J5Z3
116 */
117 HWTEST_F(NativeEngineTest, DestructorTest001, testing::ext::TestSize.Level0)
118 {
119 OHOS::buffer::Buffer *buf = new OHOS::buffer::Buffer();
120 uint8_t data[4] = {1, 2, 3, 4};
121 buf->Init(data, 0, 4);
122 delete buf;
123 }
124
125 /**
126 * @tc.name: GetLengthTest001
127 * @tc.desc: Get buffer Length.
128 * @tc.type: FUNC
129 * @tc.require:issueI5J5Z3
130 */
131 HWTEST_F(NativeEngineTest, GetLengthTest001, testing::ext::TestSize.Level0)
132 {
133 OHOS::buffer::Buffer *buf = new OHOS::buffer::Buffer();
134 ASSERT_EQ(buf->GetLength(), 0);
135 }
136
137 /**
138 * @tc.name: GetLengthTest002
139 * @tc.desc: Get buffer Length.
140 * @tc.type: FUNC
141 * @tc.require:issueI5J5Z3
142 */
143 HWTEST_F(NativeEngineTest, GetLengthTest002, testing::ext::TestSize.Level0)
144 {
145 OHOS::buffer::Buffer *buf = new OHOS::buffer::Buffer();
146 buf->Init(6);
147 ASSERT_EQ(buf->GetLength(), 6);
148 }
149
150 /**
151 * @tc.name: SetLengthTest001
152 * @tc.desc: Set buffer Length.
153 * @tc.type: FUNC
154 * @tc.require:issueI5J5Z3
155 */
156 HWTEST_F(NativeEngineTest, SetLengthTest001, testing::ext::TestSize.Level0)
157 {
158 OHOS::buffer::Buffer *buf = new OHOS::buffer::Buffer();
159 buf->Init(6);
160 buf->SetLength(7);
161 ASSERT_EQ(buf->GetLength(), 7);
162 }
163
164 /**
165 * @tc.name: GetByteOffsetTest001
166 * @tc.desc: Get buffer byteOffset.
167 * @tc.type: FUNC
168 * @tc.require:issueI5J5Z3
169 */
170 HWTEST_F(NativeEngineTest, GetByteOffsetTest001, testing::ext::TestSize.Level0)
171 {
172 OHOS::buffer::Buffer *poolBuffer = new OHOS::buffer::Buffer();
173 poolBuffer->Init(1024 * 8);
174 OHOS::buffer::Buffer *buf = new OHOS::buffer::Buffer();
175 buf->Init(poolBuffer, 2, 5);
176 ASSERT_EQ(buf->GetByteOffset(), 2);
177 }
178
179 /**
180 * @tc.name: GetAndSetTest001
181 * @tc.desc: Get And Set method.
182 * @tc.type: FUNC
183 * @tc.require:issueI5J5Z3
184 */
185 HWTEST_F(NativeEngineTest, GetAndSetTest001, testing::ext::TestSize.Level0)
186 {
187 OHOS::buffer::Buffer *buf = new OHOS::buffer::Buffer();
188 buf->Init(3);
189 buf->Set(0, 1);
190 int32_t value = buf->Get(0);
191 ASSERT_EQ(value, 1);
192 }
193
194 /**
195 * @tc.name: GetAndSetTest002
196 * @tc.desc: Get And Set method.
197 * @tc.type: FUNC
198 * @tc.require:issueI5J5Z3
199 */
200 HWTEST_F(NativeEngineTest, GetAndSetTest002, testing::ext::TestSize.Level0)
201 {
202 OHOS::buffer::Buffer *buf = new OHOS::buffer::Buffer();
203 buf->Init(3);
204 buf->Set(0, 1);
205 buf->Set(1, 2);
206 buf->Set(2, 3);
207 int32_t value = buf->Get(2);
208 ASSERT_EQ(value, 3);
209 }
210
211 /**
212 * @tc.name: WriteInt32BEAndReadInt32BETest001
213 * @tc.desc: Writes value to buf at the specified offset as big-endian. The value must be a valid signed 32-bit integer
214 * Reads a signed, big-endian 32-bit integer from buf at the specified offset.
215 * @tc.type: FUNC
216 * @tc.require:issueI5J5Z3
217 */
218 HWTEST_F(NativeEngineTest, WriteInt32BEAndReadInt32BETest001, testing::ext::TestSize.Level0)
219 {
220 OHOS::buffer::Buffer *buf = new OHOS::buffer::Buffer();
221 buf->Init(4);
222 FillZero(buf, 4);
223 buf->WriteInt32BE(0x12345678, 0);
224 int32_t res = buf->ReadInt32BE(0);
225 ASSERT_EQ(res, 0x12345678);
226 }
227
228 /**
229 * @tc.name: WriteInt32BEAndReadInt32BETest002
230 * @tc.desc: Writes value to buf at the specified offset as big-endian. The value must be a valid signed 32-bit integer
231 * Reads a signed, big-endian 32-bit integer from buf at the specified offset.
232 * @tc.type: FUNC
233 * @tc.require:issueI5J5Z3
234 */
235 HWTEST_F(NativeEngineTest, WriteInt32BEAndReadInt32BETest002, testing::ext::TestSize.Level0)
236 {
237 OHOS::buffer::Buffer *buf = new OHOS::buffer::Buffer();
238 buf->Init(5);
239 FillZero(buf, 5);
240 buf->WriteInt32BE(0x12345678, 1);
241 int32_t res = buf->ReadInt32BE(1);
242 ASSERT_EQ(res, 0x12345678);
243 }
244
245 /**
246 * @tc.name: WriteInt32BEAndReadInt32BETest003
247 * @tc.desc: Writes value to buf at the specified offset as big-endian. The value must be a valid signed 32-bit integer
248 * Reads a signed, big-endian 32-bit integer from buf at the specified offset.
249 * @tc.type: FUNC
250 * @tc.require:issueI5J5Z3
251 */
252 HWTEST_F(NativeEngineTest, WriteInt32BEAndReadInt32BETest003, testing::ext::TestSize.Level0)
253 {
254 OHOS::buffer::Buffer *buf = new OHOS::buffer::Buffer();
255 buf->Init(5);
256 FillZero(buf, 5);
257 buf->WriteInt32BE(0x12345678, 1);
258 int32_t res = buf->ReadInt32BE(1);
259 ASSERT_EQ(res, 0x12345678);
260 }
261
262 /**
263 * @tc.name: WriteInt32LEAndReadInt32LETest001
264 * @tc.desc: Writes value to buf at the specified offset as little-endian.
265 * The value must be a valid signed 32-bit integer.
266 * Reads a signed, little-endian 32-bit integer from buf at the specified offset.
267 * @tc.type: FUNC
268 * @tc.require:issueI5J5Z3
269 */
270 HWTEST_F(NativeEngineTest, WriteInt32LEAndReadInt32LETest001, testing::ext::TestSize.Level0)
271 {
272 OHOS::buffer::Buffer *buf = new OHOS::buffer::Buffer();
273 buf->Init(4);
274 FillZero(buf, 4);
275 buf->WriteInt32LE(0x12345678, 0);
276 int32_t res = buf->ReadInt32LE(0);
277 ASSERT_EQ(res, 0x12345678);
278 res = buf->ReadInt32BE(0);
279 ASSERT_EQ(res, 0x78563412);
280 }
281
282 /**
283 * @tc.name: WriteInt32LEAndReadInt32LETest002
284 * @tc.desc: Writes value to buf at the specified offset as little-endian.
285 * The value must be a valid signed 32-bit integer.
286 * Reads a signed, little-endian 32-bit integer from buf at the specified offset.
287 * @tc.type: FUNC
288 * @tc.require:issueI5J5Z3
289 */
290 HWTEST_F(NativeEngineTest, WriteInt32LEAndReadInt32LETest002, testing::ext::TestSize.Level0)
291 {
292 OHOS::buffer::Buffer *buf = new OHOS::buffer::Buffer();
293 buf->Init(5);
294 FillZero(buf, 5);
295 buf->WriteInt32LE(0x12345678, 1);
296 int32_t res = buf->ReadInt32LE(0);
297 ASSERT_EQ(res, 0x34567800);
298 }
299
300 /**
301 * @tc.name: WriteInt32LEAndReadInt32LETest003
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, WriteInt32LEAndReadInt32LETest003, testing::ext::TestSize.Level0)
309 {
310 OHOS::buffer::Buffer *buf = new OHOS::buffer::Buffer();
311 buf->Init(5);
312 FillZero(buf, 5);
313 buf->WriteInt32LE(0x12345678, 1);
314 int32_t res = buf->ReadInt32LE(1);
315 ASSERT_EQ(res, 0x12345678);
316 }
317
318 /**
319 * @tc.name: WriteUInt32BEAndReadUInt32BETest001
320 * @tc.desc: Writes value to buf at the specified offset as big-endian.
321 * The value must be a valid unsigned 32-bit integer.
322 * Reads an unsigned, big-endian 32-bit integer from buf at the specified offset.
323 * @tc.type: FUNC
324 * @tc.require:issueI5J5Z3
325 */
326 HWTEST_F(NativeEngineTest, WriteUInt32BEAndReadUInt32BETest001, testing::ext::TestSize.Level0)
327 {
328 OHOS::buffer::Buffer *buf = new OHOS::buffer::Buffer();
329 buf->Init(4);
330 FillZero(buf, 4);
331 buf->WriteUInt32BE(0x12345678, 0);
332 int32_t res = buf->ReadUInt32BE(0);
333 ASSERT_EQ(res, 0x12345678);
334 }
335
336 /**
337 * @tc.name: WriteUInt32BEAndReadUInt32BETest002
338 * @tc.desc: Writes value to buf at the specified offset as big-endian.
339 * The value must be a valid unsigned 32-bit integer.
340 * Reads an unsigned, big-endian 32-bit integer from buf at the specified offset.
341 * @tc.type: FUNC
342 * @tc.require:issueI5J5Z3
343 */
344 HWTEST_F(NativeEngineTest, WriteUInt32BEAndReadUInt32BETest002, testing::ext::TestSize.Level0)
345 {
346 OHOS::buffer::Buffer *buf = new OHOS::buffer::Buffer();
347 buf->Init(5);
348 FillZero(buf, 5);
349 buf->WriteUInt32BE(0x12345678, 1);
350 int32_t res = buf->ReadUInt32BE(0);
351 ASSERT_EQ(res, 0x123456);
352 }
353
354 /**
355 * @tc.name: WriteUInt32BEAndReadUInt32BETest003
356 * @tc.desc: Writes value to buf at the specified offset as big-endian.
357 * The value must be a valid unsigned 32-bit integer.
358 * Reads an unsigned, big-endian 32-bit integer from buf at the specified offset.
359 * @tc.type: FUNC
360 * @tc.require:issueI5J5Z3
361 */
362 HWTEST_F(NativeEngineTest, WriteUInt32BEAndReadUInt32BETest003, testing::ext::TestSize.Level0)
363 {
364 OHOS::buffer::Buffer *buf = new OHOS::buffer::Buffer();
365 buf->Init(5);
366 FillZero(buf, 5);
367 buf->WriteUInt32BE(0x12345678, 1);
368 int32_t res = buf->ReadUInt32BE(1);
369 ASSERT_EQ(res, 0x12345678);
370 }
371
372 /**
373 * @tc.name: WriteUInt32LEAndReadUInt32LETest001
374 * @tc.desc: Writes value to buf at the specified offset as little-endian.
375 * The value must be a valid unsigned 32-bit integer.
376 * Reads an unsigned, little-endian 32-bit integer from buf at the specified offset.
377 * @tc.type: FUNC
378 * @tc.require:issueI5J5Z3
379 */
380 HWTEST_F(NativeEngineTest, WriteUInt32LEAndReadUInt32LETest001, testing::ext::TestSize.Level0)
381 {
382 OHOS::buffer::Buffer *buf = new OHOS::buffer::Buffer();
383 buf->Init(4);
384 FillZero(buf, 4);
385 buf->WriteUInt32LE(0x12345678, 0);
386 int32_t res = buf->ReadUInt32LE(0);
387 ASSERT_EQ(res, 0x12345678);
388 }
389
390 /**
391 * @tc.name: WriteUInt32LEAndReadUInt32LETest002
392 * @tc.desc: Writes value to buf at the specified offset as little-endian.
393 * The value must be a valid unsigned 32-bit integer.
394 * Reads an unsigned, little-endian 32-bit integer from buf at the specified offset.
395 * @tc.type: FUNC
396 * @tc.require:issueI5J5Z3
397 */
398 HWTEST_F(NativeEngineTest, WriteUInt32LEAndReadUInt32LETest002, testing::ext::TestSize.Level0)
399 {
400 OHOS::buffer::Buffer *buf = new OHOS::buffer::Buffer();
401 buf->Init(5);
402 FillZero(buf, 5);
403 buf->WriteUInt32LE(0x12345678, 1);
404 int32_t res = buf->ReadUInt32LE(0);
405 ASSERT_EQ(res, 0x34567800);
406 }
407
408 /**
409 * @tc.name: WriteUInt32LEAndReadUInt32LETest003
410 * @tc.desc: Writes value to buf at the specified offset as little-endian.
411 * The value must be a valid unsigned 32-bit integer.
412 * Reads an unsigned, little-endian 32-bit integer from buf at the specified offset.
413 * @tc.type: FUNC
414 * @tc.require:issueI5J5Z3
415 */
416 HWTEST_F(NativeEngineTest, WriteUInt32LEAndReadUInt32LETest003, testing::ext::TestSize.Level0)
417 {
418 OHOS::buffer::Buffer *buf = new OHOS::buffer::Buffer();
419 buf->Init(5);
420 FillZero(buf, 5);
421 buf->WriteUInt32LE(0x12345678, 1);
422 int32_t res = buf->ReadUInt32LE(1);
423 ASSERT_EQ(res, 0x12345678);
424 }
425
426 /**
427 * @tc.name: ReadBytesTest001
428 * @tc.desc: Read value from buffer.
429 * @tc.type: FUNC
430 * @tc.require:issueI5J5Z3
431 */
432 HWTEST_F(NativeEngineTest, ReadBytesTest001, testing::ext::TestSize.Level0)
433 {
434 OHOS::buffer::Buffer *buf = new OHOS::buffer::Buffer();
435 buf->Init(4);
436 FillZero(buf, 4);
437 buf->WriteUInt32BE(0x12345678, 0);
438 uint32_t length = buf->GetLength();
439 uint8_t data[length];
440 buf->ReadBytes(data, 0, length);
441 uint8_t res[4] = {0x12, 0x34, 0x56, 0x78};
442 for (size_t i = 0; i < length; i++) {
443 ASSERT_EQ(data[i], res[i]);
444 }
445 }
446
447 /**
448 * @tc.name: WriteStringTest001
449 * @tc.desc: Write string to buffer.
450 * @tc.type: FUNC
451 * @tc.require:issueI5J5Z3
452 */
453 HWTEST_F(NativeEngineTest, WriteStringTest001, testing::ext::TestSize.Level0)
454 {
455 OHOS::buffer::Buffer *buf = new OHOS::buffer::Buffer();
456 buf->Init(10);
457 std::string str = "1234567890";
458 unsigned int size = buf->WriteString(str, 10);
459 ASSERT_EQ(size, 10);
460 }
461
462 /**
463 * @tc.name: WriteStringTest002
464 * @tc.desc: Write string to buffer.
465 * @tc.type: FUNC
466 * @tc.require:issueI5J5Z3
467 */
468 HWTEST_F(NativeEngineTest, WriteStringTest002, testing::ext::TestSize.Level0)
469 {
470 OHOS::buffer::Buffer *buf = new OHOS::buffer::Buffer();
471 buf->Init(9);
472 std::string str = "123456789";
473 unsigned int size = buf->WriteString(str, 9);
474 uint8_t data[size];
475 buf->ReadBytes(data, 0, size);
476 uint8_t value = 49;
477 for (size_t i = 0; i < size; i++) {
478 ASSERT_EQ(data[i], value);
479 value++;
480 }
481 }
482
483 /**
484 * @tc.name: WriteStringTest003
485 * @tc.desc: Write string to buffer.
486 * @tc.type: FUNC
487 * @tc.require:issueI5J5Z3
488 */
489 HWTEST_F(NativeEngineTest, WriteStringTest003, testing::ext::TestSize.Level0)
490 {
491 OHOS::buffer::Buffer *buf = new OHOS::buffer::Buffer();
492 buf->Init(9);
493 std::string str = "123456789";
494 unsigned int size = buf->WriteString(str, 0, 9);
495 uint8_t data[size];
496 buf->ReadBytes(data, 0, size);
497 uint8_t value = 49;
498 for (size_t i = 0; i < size; i++) {
499 ASSERT_EQ(data[i], value);
500 value++;
501 }
502 }
503
504 /**
505 * @tc.name: WriteStringTest004
506 * @tc.desc: Write string to buffer.
507 * @tc.type: FUNC
508 * @tc.require:issueI5J5Z3
509 */
510 HWTEST_F(NativeEngineTest, WriteStringTest004, testing::ext::TestSize.Level0)
511 {
512 OHOS::buffer::Buffer *buf = new OHOS::buffer::Buffer();
513 buf->Init(9);
514 std::string str = "123456789";
515 unsigned int size = buf->WriteString(str, 2, 7);
516 uint8_t data[size];
517 buf->ReadBytes(data, 0, size);
518 uint8_t value = 49;
519 for (size_t i = 2; i < size; i++) {
520 ASSERT_EQ(data[i], value);
521 value++;
522 }
523 }
524
525 /**
526 * @tc.name: WriteStringTest005
527 * @tc.desc: Write string to buffer.
528 * @tc.type: FUNC
529 * @tc.require:issueI5J5Z3
530 */
531 HWTEST_F(NativeEngineTest, WriteStringTest005, testing::ext::TestSize.Level0)
532 {
533 OHOS::buffer::Buffer *buf = new OHOS::buffer::Buffer();
534 buf->Init(9);
535 std::string str = "123456789";
536 unsigned int size = buf->WriteString(str, 0, 9, "utf8");
537 uint8_t data[size];
538 buf->ReadBytes(data, 0, size);
539 uint8_t value = 49;
540 for (size_t i = 0; i < size; i++) {
541 ASSERT_EQ(data[i], value);
542 value++;
543 }
544 }
545
546 /**
547 * @tc.name: WriteStringTest006
548 * @tc.desc: Write string to buffer.
549 * @tc.type: FUNC
550 * @tc.require:issueI5J5Z3
551 */
552 HWTEST_F(NativeEngineTest, WriteStringTest006, testing::ext::TestSize.Level0)
553 {
554 OHOS::buffer::Buffer *buf = new OHOS::buffer::Buffer();
555 buf->Init(16);
556 FillZero(buf, 16);
557 std::string str = "12345678";
558 unsigned int size = buf->WriteString(str, 0, 16, "utf16le");
559 ASSERT_EQ(size, 16);
560 uint8_t data[size];
561 buf->ReadBytes(data, 0, size);
562 uint8_t value = 49;
563 for (size_t i = 0; i < size; i++) {
564 if (i % 2 == 0) {
565 ASSERT_EQ(data[i], value);
566 value++;
567 } else {
568 ASSERT_EQ(data[i], 0);
569 }
570 }
571 }
572
573 /**
574 * @tc.name: SubBufferTest001
575 * @tc.desc: Returns a new Buffer that references the same memory as the original,
576 * but offset and cropped by the start and end indices.
577 * @tc.type: FUNC
578 * @tc.require:issueI5J5Z3
579 */
580 HWTEST_F(NativeEngineTest, SubBufferTest001, testing::ext::TestSize.Level0)
581 {
582 OHOS::buffer::Buffer *buf1 = new OHOS::buffer::Buffer();
583 buf1->Init(10);
584 FillZero(buf1, 10);
585 std::string str = "1234567890";
586 buf1->WriteString(str, 0, 10);
587 OHOS::buffer::Buffer *buf2 = buf1->SubBuffer(0, 10);
588 ASSERT_EQ(buf2->GetLength(), 10);
589 uint8_t data[11];
590 buf2->ReadBytes(data, 0, 10);
591 data[10] = 0;
592 ASSERT_STREQ(reinterpret_cast<char*>(data), str.c_str());
593 }
594
595 /**
596 * @tc.name: SubBufferTest002
597 * @tc.desc: Returns a new Buffer that references the same memory as the original,
598 * but offset and cropped by the start and end indices.
599 * @tc.type: FUNC
600 * @tc.require:issueI5J5Z3
601 */
602 HWTEST_F(NativeEngineTest, SubBufferTest002, testing::ext::TestSize.Level0)
603 {
604 OHOS::buffer::Buffer *buf1 = new OHOS::buffer::Buffer();
605 buf1->Init(10);
606 FillZero(buf1, 10);
607 std::string str = "1234567890";
608 buf1->WriteString(str, 0, 10);
609 OHOS::buffer::Buffer *buf2 = buf1->SubBuffer(2, 10);
610 ASSERT_EQ(buf2->GetLength(), 8);
611 uint8_t data[9];
612 buf2->ReadBytes(data, 0, 8);
613 data[8] = 0;
614 ASSERT_STREQ(reinterpret_cast<char*>(data), "34567890");
615 }
616
617 /**
618 * @tc.name: CopyTest001
619 * @tc.desc: Copies data from a region of buf to a region in target,
620 * even if the target memory region overlaps with buf.
621 * @tc.type: FUNC
622 * @tc.require:issueI5J5Z3
623 */
624 HWTEST_F(NativeEngineTest, CopyTest001, testing::ext::TestSize.Level0)
625 {
626 OHOS::buffer::Buffer *buffer = new OHOS::buffer::Buffer();
627 buffer->Init(20);
628
629 OHOS::buffer::Buffer *buf = new OHOS::buffer::Buffer();
630 buf->Init(20);
631 buf->WriteString("this is a string", 16);
632
633 unsigned int tOffset = 1;
634 unsigned int sOffset = 0;
635 unsigned int tEnd = 16;
636 unsigned int sEnd = 16;
637 buf->Copy(buffer, tOffset, sOffset, sEnd);
638 uint8_t data[20] = {0};
639 buffer->ReadBytes(data, tOffset, tEnd);
640 ASSERT_STREQ(reinterpret_cast<char*>(data), "this is a string");
641
642 }
643
644 /**
645 * @tc.name: CompareTest001
646 * @tc.desc: Compares buf with target and returns a number indicating whether buf comes before, after,
647 * or is the same as target in sort order. Comparison is based on the actual sequence of bytes in each Buffer
648 * @tc.type: FUNC
649 * @tc.require:issueI5J5Z3
650 */
651 HWTEST_F(NativeEngineTest, CompareTest001, testing::ext::TestSize.Level0)
652 {
653 OHOS::buffer::Buffer *buffer = new OHOS::buffer::Buffer();
654 buffer->Init(20);
655 buffer->WriteString("this is a string", 16);
656
657 OHOS::buffer::Buffer *buf = new OHOS::buffer::Buffer();
658 buf->Init(20);
659 buf->WriteString("this is a string", 1, 16);
660
661 int result = buf->Compare(buffer, 0, 1, 16);
662 ASSERT_EQ(result, 0);
663 }
664
665 /**
666 * @tc.name: IndexOfTest001
667 * @tc.desc: The index of the first occurrence of value in buf.
668 * @tc.type: FUNC
669 * @tc.require:issueI5J5Z3
670 */
671 HWTEST_F(NativeEngineTest, IndexOfTest001, testing::ext::TestSize.Level0)
672 {
673 OHOS::buffer::Buffer *buf = new OHOS::buffer::Buffer();
674 buf->Init(20);
675 buf->WriteString("this is a string", 16);
676 int index = buf->IndexOf("is", 0, 2);
677 ASSERT_EQ(index, 2);
678 }
679
680 /**
681 * @tc.name: IndexOfTest002
682 * @tc.desc: The index of the first occurrence of value in buf.
683 * @tc.type: FUNC
684 * @tc.require:issueI5J5Z3
685 */
686 HWTEST_F(NativeEngineTest, IndexOfTest002, testing::ext::TestSize.Level0)
687 {
688 OHOS::buffer::Buffer *buf = new OHOS::buffer::Buffer();
689 buf->Init(7);
690 buf->WriteString("3363333", 7);
691 int index = buf->IndexOf("36", 0, 2);
692 ASSERT_EQ(index, 1);
693 }
694
695 /**
696 * @tc.name: IndexOfTest003
697 * @tc.desc: The index of the first occurrence of value in buf.
698 * @tc.type: FUNC
699 * @tc.require:issueI5J5Z3
700 */
701 HWTEST_F(NativeEngineTest, IndexOfTest003, testing::ext::TestSize.Level0)
702 {
703 OHOS::buffer::Buffer *buf = new OHOS::buffer::Buffer();
704 buf->Init(12);
705 buf->WriteString("322362326233", 12);
706 int index = buf->IndexOf("2623", 0, 4);
707 ASSERT_EQ(index, 7);
708 }
709
710 /**
711 * @tc.name: LastIndexOfTest001
712 * @tc.desc: The index of the last occurrence of value in buf.
713 * @tc.type: FUNC
714 * @tc.require:issueI5J5Z3
715 */
716 HWTEST_F(NativeEngineTest, LastIndexOfTest001, testing::ext::TestSize.Level0)
717 {
718 OHOS::buffer::Buffer *buf = new OHOS::buffer::Buffer();
719 buf->Init(20);
720 buf->WriteString("this is a string", 16);
721 int index = buf->LastIndexOf("is", 0, 2);
722 ASSERT_EQ(index, 5);
723 }
724
725 /**
726 * @tc.name: LastIndexOfTest002
727 * @tc.desc: The index of the last occurrence of value in buf.
728 * @tc.type: FUNC
729 * @tc.require:issueI5J5Z3
730 */
731 HWTEST_F(NativeEngineTest, LastIndexOfTest002, testing::ext::TestSize.Level0)
732 {
733 OHOS::buffer::Buffer *buf = new OHOS::buffer::Buffer();
734 buf->Init(7);
735 buf->WriteString("3363333", 7);
736 int index = buf->LastIndexOf("36", 0, 2);
737 ASSERT_EQ(index, 1);
738 }
739
740 /**
741 * @tc.name: LastIndexOfTest003
742 * @tc.desc: The index of the last occurrence of value in buf.
743 * @tc.type: FUNC
744 * @tc.require:issueI5J5Z3
745 */
746 HWTEST_F(NativeEngineTest, LastIndexOfTest003, testing::ext::TestSize.Level0)
747 {
748 OHOS::buffer::Buffer *buf = new OHOS::buffer::Buffer();
749 buf->Init(11);
750 buf->WriteString("32236326233", 11);
751 int index = buf->LastIndexOf("236", 0, 3);
752 ASSERT_EQ(index, 2);
753 }
754
755 /**
756 * @tc.name: LastIndexOfTest004
757 * @tc.desc: The index of the last occurrence of value in buf.
758 * @tc.type: FUNC
759 * @tc.require:issueI5J5Z3
760 */
761 HWTEST_F(NativeEngineTest, LastIndexOfTest004, testing::ext::TestSize.Level0)
762 {
763 OHOS::buffer::Buffer *buf = new OHOS::buffer::Buffer();
764 buf->Init(12);
765 buf->WriteString("322362326233", 12);
766 int index = buf->LastIndexOf("2236", 0, 4);
767 ASSERT_EQ(index, 1);
768 }
769
770 /**
771 * @tc.name: LastIndexOfTest005
772 * @tc.desc: The index of the last occurrence of value in buf.
773 * @tc.type: FUNC
774 * @tc.require:issueI5J5Z3
775 */
776 HWTEST_F(NativeEngineTest, LastIndexOfTest005, testing::ext::TestSize.Level0)
777 {
778 OHOS::buffer::Buffer *buf = new OHOS::buffer::Buffer();
779 buf->Init(12);
780 buf->WriteString("322362326233", 12);
781 int index = buf->LastIndexOf("136", 0, 3);
782 ASSERT_EQ(index, -1);
783 }
784
785
786 /**
787 * @tc.name: ToBase64Test001
788 * @tc.desc: Convert the contents of the buffer into a string in Base64 format.
789 * @tc.type: FUNC
790 * @tc.require:issueI5J5Z3
791 */
792 HWTEST_F(NativeEngineTest, ToBase64Test001, testing::ext::TestSize.Level0)
793 {
794 OHOS::buffer::Buffer *buf = new OHOS::buffer::Buffer();
795 buf->Init(20);
796 buf->WriteString("this is a string", 16);
797 std::string base64Str = buf->ToBase64(0, 16);
798 ASSERT_STREQ(base64Str.c_str(), "dGhpcyBpcyBhIHN0cmluZw==");
799 }
800
801 /**
802 * @tc.name: ToBase64Test002
803 * @tc.desc: Convert the contents of the buffer into a string in Base64 format.
804 * @tc.type: FUNC
805 * @tc.require:issueI5J5Z3
806 */
807 HWTEST_F(NativeEngineTest, ToBase64Test002, testing::ext::TestSize.Level0)
808 {
809 OHOS::buffer::Buffer *buf = new OHOS::buffer::Buffer();
810 buf->Init(30);
811 buf->WriteString("this is a big string", 20);
812 std::string base64Str = buf->ToBase64(0, 20);
813 ASSERT_STREQ(base64Str.c_str(), "dGhpcyBpcyBhIGJpZyBzdHJpbmc=");
814 }
815
816 /**
817 * @tc.name: GetEncodingTypeTest001
818 * @tc.desc: Get encoding type.
819 * @tc.type: FUNC
820 * @tc.require:issueI5J5Z3
821 */
822 HWTEST_F(NativeEngineTest, GetEncodingTypeTest001, testing::ext::TestSize.Level0)
823 {
824 std::map <std::string, int> _typeMap =
825 {
826 {"hex", OHOS::buffer::HEX},
827 {"base64url", OHOS::buffer::BASE64URL},
828 {"ascii", OHOS::buffer::ASCII},
829 {"base64", OHOS::buffer::BASE64},
830 {"latin1", OHOS::buffer::LATIN1},
831 {"binary", OHOS::buffer::BINARY},
832 {"utf16le", OHOS::buffer::UTF16LE},
833 {"utf8", OHOS::buffer::UTF8},
834 };
835
836 for (auto item =_typeMap.begin(); item != _typeMap.end(); item++)
837 {
838 std::string type = item->first;
839 OHOS::buffer::EncodingType et = OHOS::buffer::Buffer::GetEncodingType(type);
840 ASSERT_EQ(et, item->second);
841 }
842 }
843
844 /**
845 * @tc.name: SetArrayTest001
846 * @tc.desc: Put the contents of the array into the buffer.
847 * @tc.type: FUNC
848 * @tc.require:issueI5J5Z3
849 */
850 HWTEST_F(NativeEngineTest, SetArrayTest001, testing::ext::TestSize.Level0)
851 {
852 OHOS::buffer::Buffer *buffer = new OHOS::buffer::Buffer();
853 buffer->Init(20);
854 std::vector<uint8_t> numbers;
855 for (int i = 0; i < 10; i++) {
856 numbers.push_back(i);
857 }
858 buffer->SetArray(numbers);
859 unsigned int offset = 0;
860 unsigned int end = 10;
861 uint8_t data[20] = {0};
862 buffer->ReadBytes(data, offset, end);
863 for (int j = 0; j < 10; j++) {
864 ASSERT_EQ(data[j], j);
865 }
866 }
867
868 /**
869 * @tc.name: FillBufferTest001
870 * @tc.desc: Fill the buffer with the buffer object
871 * @tc.type: FUNC
872 * @tc.require:issueI5J5Z3
873 */
874 HWTEST_F(NativeEngineTest, FillBufferTest001, testing::ext::TestSize.Level0)
875 {
876 OHOS::buffer::Buffer *buffer = new OHOS::buffer::Buffer();
877 buffer->Init(10);
878 std::vector<uint8_t> numbers;
879 for (int i = 0; i < 10; i++) {
880 numbers.push_back(i);
881 }
882 buffer->SetArray(numbers);
883 unsigned int offset = 0;
884 unsigned int end = 10;
885 OHOS::buffer::Buffer *buf = new OHOS::buffer::Buffer();
886 buf->Init(20);
887 buf->FillBuffer(buffer, offset, end);
888 uint8_t data[20] = {0};
889 buf->ReadBytes(data, offset, end);
890 for (int j = 0; j < 10; j++) {
891 ASSERT_EQ(data[j], j);
892 }
893 }
894
895 /**
896 * @tc.name: FillNumberTest001
897 * @tc.desc: Fill the buffer with the number
898 * @tc.type: FUNC
899 * @tc.require:issueI5J5Z3
900 */
901 HWTEST_F(NativeEngineTest, FillNumberTest001, testing::ext::TestSize.Level0)
902 {
903 OHOS::buffer::Buffer *buf = new OHOS::buffer::Buffer();
904 buf->Init(20);
905 std::vector<uint8_t> numbers;
906 for (int i = 0; i < 10; i++) {
907 numbers.push_back(i);
908 }
909 unsigned int offset = 0;
910 unsigned int end = 10;
911 buf->FillNumber(numbers, offset, end);
912 uint8_t data[20] = {0};
913 buf->ReadBytes(data, offset, end);
914 for (int j = 0; j < 10; j++) {
915 ASSERT_EQ(data[j], j);
916 }
917 }
918
919 /**
920 * @tc.name: FillStringTest001
921 * @tc.desc: Fill the buffer with the string
922 * @tc.type: FUNC
923 * @tc.require:issueI5J5Z3
924 */
925 HWTEST_F(NativeEngineTest, FillStringTest001, testing::ext::TestSize.Level0)
926 {
927 OHOS::buffer::Buffer *buf = new OHOS::buffer::Buffer();
928 buf->Init(20);
929 std::string value = "abcd";
930 unsigned int offset = 0;
931 unsigned int end = 10;
932 std::string encoding = "ascii";
933 buf->FillString(value, offset, end, encoding);
934 uint8_t data[20] = {0};
935 buf->ReadBytes(data, offset, end);
936 ASSERT_STREQ(reinterpret_cast<char*>(data), "abcdabcdab");
937 }
938
939 /**
940 * @tc.name: FillStringTest002
941 * @tc.desc: Fill the buffer with the string
942 * @tc.type: FUNC
943 * @tc.require:issueI5J5Z3
944 */
945 HWTEST_F(NativeEngineTest, FillStringTest002, testing::ext::TestSize.Level0)
946 {
947 OHOS::buffer::Buffer *buf = new OHOS::buffer::Buffer();
948 buf->Init(20);
949 std::string value = "扡摣";
950 unsigned int offset = 0;
951 unsigned int end = 10;
952 std::string encoding = "utf16le";
953 buf->FillString(value, offset, end, encoding);
954 uint8_t data[20] = {0};
955 buf->ReadBytes(data, offset, end);
956 ASSERT_STREQ(reinterpret_cast<char*>(data), "abcdabcdab");
957 }
958
959 /**
960 * @tc.name: FillStringTest003
961 * @tc.desc: Fill the buffer with the string
962 * @tc.type: FUNC
963 * @tc.require:issueI5J5Z3
964 */
965 HWTEST_F(NativeEngineTest, FillStringTest003, testing::ext::TestSize.Level0)
966 {
967 OHOS::buffer::Buffer *buf = new OHOS::buffer::Buffer();
968 buf->Init(20);
969 std::string value = "YWJjZA";
970 unsigned int offset = 0;
971 unsigned int end = 10;
972 std::string encoding = "base64";
973 buf->FillString(value, offset, end, encoding);
974 uint8_t data[20] = {0};
975 buf->ReadBytes(data, offset, end);
976 ASSERT_STREQ(reinterpret_cast<char*>(data), "abcdabcdab");
977 }
978
979 /**
980 * @tc.name: BlobConstructorTest001
981 * @tc.desc: Blob Constructor
982 * @tc.type: FUNC
983 * @tc.require:issueI5J5Z3
984 */
985 HWTEST_F(NativeEngineTest, BlobConstructorTest001, testing::ext::TestSize.Level0)
986 {
987 OHOS::buffer::Blob *blob = new OHOS::buffer::Blob();
988 uint8_t data[4] = {1, 2, 3, 4};
989 blob->Init(data, 4);
990 ASSERT_EQ(blob->GetLength(), 4);
991 }
992
993 /**
994 * @tc.name: BlobConstructorTest002
995 * @tc.desc: Blob Constructor
996 * @tc.type: FUNC
997 * @tc.require:issueI5J5Z3
998 */
999 HWTEST_F(NativeEngineTest, BlobConstructorTest002, testing::ext::TestSize.Level0)
1000 {
1001 OHOS::buffer::Blob *blob = new OHOS::buffer::Blob();
1002 uint8_t data[4] = {1, 2, 3, 4};
1003 blob->Init(data, 4);
1004
1005 OHOS::buffer::Blob *blob2 = new OHOS::buffer::Blob();
1006 blob2->Init(blob, 0);
1007
1008 ASSERT_EQ(blob2->GetLength(), 4);
1009 }
1010
1011 /**
1012 * @tc.name: BlobConstructorTest003
1013 * @tc.desc: Blob Constructor
1014 * @tc.type: FUNC
1015 * @tc.require:issueI5J5Z3
1016 */
1017 HWTEST_F(NativeEngineTest, BlobConstructorTest003, testing::ext::TestSize.Level0)
1018 {
1019 OHOS::buffer::Blob *blob = new OHOS::buffer::Blob();
1020 uint8_t data[4] = {1, 2, 3, 4};
1021 blob->Init(data, 4);
1022
1023 OHOS::buffer::Blob *blob2 = new OHOS::buffer::Blob();
1024 blob2->Init(blob, 1, 4);
1025
1026 ASSERT_EQ(blob2->GetLength(), 3);
1027 }
1028
1029 /**
1030 * @tc.name: BlobDestructorTest001
1031 * @tc.desc: Blob Destructor.
1032 * @tc.type: FUNC
1033 * @tc.require:issueI5J5Z3
1034 */
1035 HWTEST_F(NativeEngineTest, BlobDestructorTest001, testing::ext::TestSize.Level0)
1036 {
1037 OHOS::buffer::Blob *blob = new OHOS::buffer::Blob();
1038 uint8_t data[4] = {1, 2, 3, 4};
1039 blob->Init(data, 4);
1040 delete blob;
1041 }
1042
1043 /**
1044 * @tc.name: BlobGetByteTest001
1045 * @tc.desc: Get a byte in blob
1046 * @tc.type: FUNC
1047 * @tc.require:issueI5J5Z3
1048 */
1049 HWTEST_F(NativeEngineTest, BlobGetByteTest001, testing::ext::TestSize.Level0)
1050 {
1051 OHOS::buffer::Blob *blob = new OHOS::buffer::Blob();
1052 uint8_t data[4] = {1, 2, 3, 4};
1053 blob->Init(data, 4);
1054
1055 uint8_t byte = blob->GetByte(2);
1056
1057 ASSERT_EQ(byte, 3);
1058 }
1059
1060 /**
1061 * @tc.name: BlobGetRawTest001
1062 * @tc.desc: Get the raw in blob
1063 * @tc.type: FUNC
1064 * @tc.require:issueI5J5Z3
1065 */
1066 HWTEST_F(NativeEngineTest, BlobGetRawTest001, testing::ext::TestSize.Level0)
1067 {
1068 OHOS::buffer::Blob *blob = new OHOS::buffer::Blob();
1069 uint8_t data[4] = {1, 2, 3, 4};
1070 blob->Init(data, 4);
1071
1072 uint8_t *raw = blob->GetRaw();
1073
1074 ASSERT_TRUE(raw != nullptr);
1075 }
1076
1077 /**
1078 * @tc.name: BlobGetLengthTest001
1079 * @tc.desc: Get the length in blob
1080 * @tc.type: FUNC
1081 * @tc.require:issueI5J5Z3
1082 */
1083 HWTEST_F(NativeEngineTest, BlobGetLengthTest001, testing::ext::TestSize.Level0)
1084 {
1085 OHOS::buffer::Blob *blob = new OHOS::buffer::Blob();
1086 uint8_t data[4] = {1, 2, 3, 4};
1087 blob->Init(data, 4);
1088
1089 unsigned int len = blob->GetLength();
1090
1091 ASSERT_EQ(len, 4);
1092 }
1093
1094 /**
1095 * @tc.name: BlobGetLengthTest001
1096 * @tc.desc: Read blob object bytes
1097 * @tc.type: FUNC
1098 * @tc.require:issueI5J5Z3
1099 */
1100 HWTEST_F(NativeEngineTest, BlobReadBytesTest001, testing::ext::TestSize.Level0)
1101 {
1102 OHOS::buffer::Blob *blob = new OHOS::buffer::Blob();
1103 uint8_t data[10] = {0};
1104 for (int i = 0; i < 10; i++) {
1105 data[i] = i;
1106 }
1107 blob->Init(data, 10);
1108
1109 uint8_t dat[10] = {0};
1110 blob->ReadBytes(dat, 10);
1111
1112 for (int i = 0; i < 10; i++) {
1113 ASSERT_EQ(dat[i], i);
1114 }
1115 }
1116
1117 /**
1118 * @tc.name: Utf8ToUtf16BETest001
1119 * @tc.desc: convert utf8 bytes to utf16 bytes
1120 * @tc.type: FUNC
1121 * @tc.require:issueI5J5Z3
1122 */
1123 HWTEST_F(NativeEngineTest, Utf8ToUtf16BETest001, testing::ext::TestSize.Level0)
1124 {
1125 std::string str8 = "";
1126 // one byte
1127 str8.append(1, 0x41);
1128 // two bytes
1129 str8.append(1, 0xC3);
1130 str8.append(1, 0x84);
1131 // three bytes
1132 str8.append(1, 0xE5);
1133 str8.append(1, 0x88);
1134 str8.append(1, 0x98);
1135 // four bytes
1136 str8.append(1, 0xf0);
1137 str8.append(1, 0x9f);
1138 str8.append(1, 0x90);
1139 str8.append(1, 0x85);
1140
1141 // another four bytes
1142 str8.append(1, 0xf0);
1143 str8.append(1, 0x8f);
1144 str8.append(1, 0x90);
1145 str8.append(1, 0x85);
1146
1147 bool isOk = false;
1148 std::u16string str16 = OHOS::buffer::Utf8ToUtf16BE(str8, &isOk);
1149
1150 char16_t results[] = {0x41, 0xc4, 0x5218, 0xd83d, 0xdc05, 0xf405};
1151 for (int i = 0; i < 6; i++) {
1152 ASSERT_EQ(results[i], str16[i]);
1153 }
1154 }
1155
1156 /**
1157 * @tc.name: HexDecodeTest001
1158 * @tc.desc: decode a hex string
1159 * @tc.type: FUNC
1160 * @tc.require:issueI5J5Z3
1161 */
1162 HWTEST_F(NativeEngineTest, HexDecodeTest001, testing::ext::TestSize.Level0)
1163 {
1164 std::string ret = OHOS::buffer::HexDecode("313g");
1165 ASSERT_EQ(ret, "1");
1166 }
1167