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