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 * @tc.name: CompareTest001
681 * @tc.desc: Compares buf with target and returns a number indicating whether buf comes before, after,
682 * or is the same as target in sort order. Comparison is based on the actual sequence of bytes in each Buffer
683 * @tc.type: FUNC
684 * @tc.require:issueI5J5Z3
685 */
686 HWTEST_F(NativeEngineTest, CompareTest001, testing::ext::TestSize.Level0)
687 {
688 OHOS::buffer::Buffer *buffer = new OHOS::buffer::Buffer();
689 buffer->Init(20);
690 buffer->WriteString("this is a string", 16);
691
692 OHOS::buffer::Buffer *buf = new OHOS::buffer::Buffer();
693 buf->Init(20);
694 buf->WriteString("this is a string", 1, 16);
695
696 int result = buf->Compare(buffer, 0, 1, 16);
697 ASSERT_EQ(result, 0);
698 }
699
700 /**
701 * @tc.name: IndexOfTest001
702 * @tc.desc: The index of the first occurrence of value in buf.
703 * @tc.type: FUNC
704 * @tc.require:issueI5J5Z3
705 */
706 HWTEST_F(NativeEngineTest, IndexOfTest001, testing::ext::TestSize.Level0)
707 {
708 OHOS::buffer::Buffer *buf = new OHOS::buffer::Buffer();
709 buf->Init(20);
710 buf->WriteString("this is a string", 16);
711 int index = buf->IndexOf("is", 0, 2);
712 ASSERT_EQ(index, 2);
713 }
714
715 /**
716 * @tc.name: IndexOfTest002
717 * @tc.desc: The index of the first occurrence of value in buf.
718 * @tc.type: FUNC
719 * @tc.require:issueI5J5Z3
720 */
721 HWTEST_F(NativeEngineTest, IndexOfTest002, testing::ext::TestSize.Level0)
722 {
723 OHOS::buffer::Buffer *buf = new OHOS::buffer::Buffer();
724 buf->Init(7);
725 buf->WriteString("3363333", 7);
726 int index = buf->IndexOf("36", 0, 2);
727 ASSERT_EQ(index, 1);
728 }
729
730 /**
731 * @tc.name: IndexOfTest003
732 * @tc.desc: The index of the first occurrence of value in buf.
733 * @tc.type: FUNC
734 * @tc.require:issueI5J5Z3
735 */
736 HWTEST_F(NativeEngineTest, IndexOfTest003, testing::ext::TestSize.Level0)
737 {
738 OHOS::buffer::Buffer *buf = new OHOS::buffer::Buffer();
739 buf->Init(12);
740 buf->WriteString("322362326233", 12);
741 int index = buf->IndexOf("2623", 0, 4);
742 ASSERT_EQ(index, 7);
743 }
744
745 /**
746 * @tc.name: LastIndexOfTest001
747 * @tc.desc: The index of the last occurrence of value in buf.
748 * @tc.type: FUNC
749 * @tc.require:issueI5J5Z3
750 */
751 HWTEST_F(NativeEngineTest, LastIndexOfTest001, testing::ext::TestSize.Level0)
752 {
753 OHOS::buffer::Buffer *buf = new OHOS::buffer::Buffer();
754 buf->Init(20);
755 buf->WriteString("this is a string", 16);
756 int index = buf->LastIndexOf("is", 0, 2);
757 ASSERT_EQ(index, 5);
758 }
759
760 /**
761 * @tc.name: LastIndexOfTest002
762 * @tc.desc: The index of the last occurrence of value in buf.
763 * @tc.type: FUNC
764 * @tc.require:issueI5J5Z3
765 */
766 HWTEST_F(NativeEngineTest, LastIndexOfTest002, testing::ext::TestSize.Level0)
767 {
768 OHOS::buffer::Buffer *buf = new OHOS::buffer::Buffer();
769 buf->Init(7);
770 buf->WriteString("3363333", 7);
771 int index = buf->LastIndexOf("36", 0, 2);
772 ASSERT_EQ(index, 1);
773 }
774
775 /**
776 * @tc.name: LastIndexOfTest003
777 * @tc.desc: The index of the last occurrence of value in buf.
778 * @tc.type: FUNC
779 * @tc.require:issueI5J5Z3
780 */
781 HWTEST_F(NativeEngineTest, LastIndexOfTest003, testing::ext::TestSize.Level0)
782 {
783 OHOS::buffer::Buffer *buf = new OHOS::buffer::Buffer();
784 buf->Init(11);
785 buf->WriteString("32236326233", 11);
786 int index = buf->LastIndexOf("236", 0, 3);
787 ASSERT_EQ(index, 2);
788 }
789
790 /**
791 * @tc.name: LastIndexOfTest004
792 * @tc.desc: The index of the last occurrence of value in buf.
793 * @tc.type: FUNC
794 * @tc.require:issueI5J5Z3
795 */
796 HWTEST_F(NativeEngineTest, LastIndexOfTest004, testing::ext::TestSize.Level0)
797 {
798 OHOS::buffer::Buffer *buf = new OHOS::buffer::Buffer();
799 buf->Init(12);
800 buf->WriteString("322362326233", 12);
801 int index = buf->LastIndexOf("2236", 0, 4);
802 ASSERT_EQ(index, 1);
803 }
804
805 /**
806 * @tc.name: LastIndexOfTest005
807 * @tc.desc: The index of the last occurrence of value in buf.
808 * @tc.type: FUNC
809 * @tc.require:issueI5J5Z3
810 */
811 HWTEST_F(NativeEngineTest, LastIndexOfTest005, testing::ext::TestSize.Level0)
812 {
813 OHOS::buffer::Buffer *buf = new OHOS::buffer::Buffer();
814 buf->Init(12);
815 buf->WriteString("322362326233", 12);
816 int index = buf->LastIndexOf("136", 0, 3);
817 ASSERT_EQ(index, -1);
818 }
819
820
821 /**
822 * @tc.name: ToBase64Test001
823 * @tc.desc: Convert the contents of the buffer into a string in Base64 format.
824 * @tc.type: FUNC
825 * @tc.require:issueI5J5Z3
826 */
827 HWTEST_F(NativeEngineTest, ToBase64Test001, testing::ext::TestSize.Level0)
828 {
829 OHOS::buffer::Buffer *buf = new OHOS::buffer::Buffer();
830 buf->Init(20);
831 buf->WriteString("this is a string", 16);
832 std::string base64Str = buf->ToBase64(0, 16);
833 ASSERT_STREQ(base64Str.c_str(), "dGhpcyBpcyBhIHN0cmluZw==");
834 }
835
836 /**
837 * @tc.name: ToBase64Test002
838 * @tc.desc: Convert the contents of the buffer into a string in Base64 format.
839 * @tc.type: FUNC
840 * @tc.require:issueI5J5Z3
841 */
842 HWTEST_F(NativeEngineTest, ToBase64Test002, testing::ext::TestSize.Level0)
843 {
844 OHOS::buffer::Buffer *buf = new OHOS::buffer::Buffer();
845 buf->Init(30);
846 buf->WriteString("this is a big string", 20);
847 std::string base64Str = buf->ToBase64(0, 20);
848 ASSERT_STREQ(base64Str.c_str(), "dGhpcyBpcyBhIGJpZyBzdHJpbmc=");
849 }
850
851 /**
852 * @tc.name: GetEncodingTypeTest001
853 * @tc.desc: Get encoding type.
854 * @tc.type: FUNC
855 * @tc.require:issueI5J5Z3
856 */
857 HWTEST_F(NativeEngineTest, GetEncodingTypeTest001, testing::ext::TestSize.Level0)
858 {
859 std::map <std::string, int> _typeMap =
860 {
861 {"hex", OHOS::buffer::HEX},
862 {"base64url", OHOS::buffer::BASE64URL},
863 {"ascii", OHOS::buffer::ASCII},
864 {"base64", OHOS::buffer::BASE64},
865 {"latin1", OHOS::buffer::LATIN1},
866 {"binary", OHOS::buffer::BINARY},
867 {"utf16le", OHOS::buffer::UTF16LE},
868 {"utf8", OHOS::buffer::UTF8},
869 };
870
871 for (auto item =_typeMap.begin(); item != _typeMap.end(); item++)
872 {
873 std::string type = item->first;
874 OHOS::buffer::EncodingType et = OHOS::buffer::Buffer::GetEncodingType(type);
875 ASSERT_EQ(et, item->second);
876 }
877 }
878
879 /**
880 * @tc.name: SetArrayTest001
881 * @tc.desc: Put the contents of the array into the buffer.
882 * @tc.type: FUNC
883 * @tc.require:issueI5J5Z3
884 */
885 HWTEST_F(NativeEngineTest, SetArrayTest001, testing::ext::TestSize.Level0)
886 {
887 OHOS::buffer::Buffer *buffer = new OHOS::buffer::Buffer();
888 buffer->Init(20);
889 std::vector<uint8_t> numbers;
890 for (int i = 0; i < 10; i++) {
891 numbers.push_back(i);
892 }
893 buffer->SetArray(numbers);
894 unsigned int offset = 0;
895 unsigned int end = 10;
896 uint8_t data[20] = {0};
897 buffer->ReadBytes(data, offset, end);
898 for (int j = 0; j < 10; j++) {
899 ASSERT_EQ(data[j], j);
900 }
901 }
902
903 /**
904 * @tc.name: FillBufferTest001
905 * @tc.desc: Fill the buffer with the buffer object
906 * @tc.type: FUNC
907 * @tc.require:issueI5J5Z3
908 */
909 HWTEST_F(NativeEngineTest, FillBufferTest001, testing::ext::TestSize.Level0)
910 {
911 OHOS::buffer::Buffer *buffer = new OHOS::buffer::Buffer();
912 buffer->Init(10);
913 std::vector<uint8_t> numbers;
914 for (int i = 0; i < 10; i++) {
915 numbers.push_back(i);
916 }
917 buffer->SetArray(numbers);
918 unsigned int offset = 0;
919 unsigned int end = 10;
920 OHOS::buffer::Buffer *buf = new OHOS::buffer::Buffer();
921 buf->Init(20);
922 buf->FillBuffer(buffer, offset, end);
923 uint8_t data[20] = {0};
924 buf->ReadBytes(data, offset, end);
925 for (int j = 0; j < 10; j++) {
926 ASSERT_EQ(data[j], j);
927 }
928 }
929
930 /**
931 * @tc.name: FillNumberTest001
932 * @tc.desc: Fill the buffer with the number
933 * @tc.type: FUNC
934 * @tc.require:issueI5J5Z3
935 */
936 HWTEST_F(NativeEngineTest, FillNumberTest001, testing::ext::TestSize.Level0)
937 {
938 OHOS::buffer::Buffer *buf = new OHOS::buffer::Buffer();
939 buf->Init(20);
940 std::vector<uint8_t> numbers;
941 for (int i = 0; i < 10; i++) {
942 numbers.push_back(i);
943 }
944 unsigned int offset = 0;
945 unsigned int end = 10;
946 buf->FillNumber(numbers, offset, end);
947 uint8_t data[20] = {0};
948 buf->ReadBytes(data, offset, end);
949 for (int j = 0; j < 10; j++) {
950 ASSERT_EQ(data[j], j);
951 }
952 }
953
954 /**
955 * @tc.name: FillStringTest001
956 * @tc.desc: Fill the buffer with the string
957 * @tc.type: FUNC
958 * @tc.require:issueI5J5Z3
959 */
960 HWTEST_F(NativeEngineTest, FillStringTest001, testing::ext::TestSize.Level0)
961 {
962 OHOS::buffer::Buffer *buf = new OHOS::buffer::Buffer();
963 buf->Init(20);
964 std::string value = "abcd";
965 unsigned int offset = 0;
966 unsigned int end = 10;
967 std::string encoding = "ascii";
968 buf->FillString(value, offset, end, encoding);
969 uint8_t data[20] = {0};
970 buf->ReadBytes(data, offset, end);
971 ASSERT_STREQ(reinterpret_cast<char*>(data), "abcdabcdab");
972 }
973
974 /**
975 * @tc.name: FillStringTest002
976 * @tc.desc: Fill the buffer with the string
977 * @tc.type: FUNC
978 * @tc.require:issueI5J5Z3
979 */
980 HWTEST_F(NativeEngineTest, FillStringTest002, testing::ext::TestSize.Level0)
981 {
982 OHOS::buffer::Buffer *buf = new OHOS::buffer::Buffer();
983 buf->Init(20);
984 std::string value = "扡摣";
985 unsigned int offset = 0;
986 unsigned int end = 10;
987 std::string encoding = "utf16le";
988 buf->FillString(value, offset, end, encoding);
989 uint8_t data[20] = {0};
990 buf->ReadBytes(data, offset, end);
991 ASSERT_STREQ(reinterpret_cast<char*>(data), "abcdabcdab");
992 }
993
994 /**
995 * @tc.name: FillStringTest003
996 * @tc.desc: Fill the buffer with the string
997 * @tc.type: FUNC
998 * @tc.require:issueI5J5Z3
999 */
1000 HWTEST_F(NativeEngineTest, FillStringTest003, testing::ext::TestSize.Level0)
1001 {
1002 OHOS::buffer::Buffer *buf = new OHOS::buffer::Buffer();
1003 buf->Init(20);
1004 std::string value = "YWJjZA";
1005 unsigned int offset = 0;
1006 unsigned int end = 10;
1007 std::string encoding = "base64";
1008 buf->FillString(value, offset, end, encoding);
1009 uint8_t data[20] = {0};
1010 buf->ReadBytes(data, offset, end);
1011 ASSERT_STREQ(reinterpret_cast<char*>(data), "abcdabcdab");
1012 }
1013
1014 /**
1015 * @tc.name: BlobConstructorTest001
1016 * @tc.desc: Blob Constructor
1017 * @tc.type: FUNC
1018 * @tc.require:issueI5J5Z3
1019 */
1020 HWTEST_F(NativeEngineTest, BlobConstructorTest001, testing::ext::TestSize.Level0)
1021 {
1022 OHOS::buffer::Blob *blob = new OHOS::buffer::Blob();
1023 uint8_t data[4] = {1, 2, 3, 4};
1024 blob->Init(data, 4);
1025 ASSERT_EQ(blob->GetLength(), 4);
1026 }
1027
1028 /**
1029 * @tc.name: BlobConstructorTest002
1030 * @tc.desc: Blob Constructor
1031 * @tc.type: FUNC
1032 * @tc.require:issueI5J5Z3
1033 */
1034 HWTEST_F(NativeEngineTest, BlobConstructorTest002, testing::ext::TestSize.Level0)
1035 {
1036 OHOS::buffer::Blob *blob = new OHOS::buffer::Blob();
1037 uint8_t data[4] = {1, 2, 3, 4};
1038 blob->Init(data, 4);
1039
1040 OHOS::buffer::Blob *blob2 = new OHOS::buffer::Blob();
1041 blob2->Init(blob, 0);
1042
1043 ASSERT_EQ(blob2->GetLength(), 4);
1044 }
1045
1046 /**
1047 * @tc.name: BlobConstructorTest003
1048 * @tc.desc: Blob Constructor
1049 * @tc.type: FUNC
1050 * @tc.require:issueI5J5Z3
1051 */
1052 HWTEST_F(NativeEngineTest, BlobConstructorTest003, testing::ext::TestSize.Level0)
1053 {
1054 OHOS::buffer::Blob *blob = new OHOS::buffer::Blob();
1055 uint8_t data[4] = {1, 2, 3, 4};
1056 blob->Init(data, 4);
1057
1058 OHOS::buffer::Blob *blob2 = new OHOS::buffer::Blob();
1059 blob2->Init(blob, 1, 4);
1060
1061 ASSERT_EQ(blob2->GetLength(), 3);
1062 }
1063
1064 /**
1065 * @tc.name: BlobConstructorTest004
1066 * @tc.desc: Blob Constructor
1067 * @tc.type: FUNC
1068 * @tc.require:issueI5J5Z3
1069 */
1070 HWTEST_F(NativeEngineTest, BlobConstructorTest004, testing::ext::TestSize.Level0)
1071 {
1072 OHOS::buffer::Blob *blob = new OHOS::buffer::Blob();
1073 uint8_t data[1] = {1};
1074 OHOS::buffer::Blob *blob1 = new OHOS::buffer::Blob();
1075 blob->Init(blob1, 1, 0);
1076 blob->Init(blob1, 1, -1);
1077 blob->Init(nullptr, 0, 1);
1078 blob->Init(data, 1);
1079 ASSERT_EQ(blob->GetLength(), 1);
1080 delete blob;
1081 blob = nullptr;
1082 delete blob1;
1083 blob1 = nullptr;
1084 }
1085
1086 /**
1087 * @tc.name: BlobDestructorTest001
1088 * @tc.desc: Blob Destructor.
1089 * @tc.type: FUNC
1090 * @tc.require:issueI5J5Z3
1091 */
1092 HWTEST_F(NativeEngineTest, BlobDestructorTest001, testing::ext::TestSize.Level0)
1093 {
1094 OHOS::buffer::Blob *blob = new OHOS::buffer::Blob();
1095 uint8_t data[4] = {1, 2, 3, 4};
1096 blob->Init(data, 4);
1097 delete blob;
1098 }
1099
1100 /**
1101 * @tc.name: BlobGetByteTest001
1102 * @tc.desc: Get a byte in blob
1103 * @tc.type: FUNC
1104 * @tc.require:issueI5J5Z3
1105 */
1106 HWTEST_F(NativeEngineTest, BlobGetByteTest001, testing::ext::TestSize.Level0)
1107 {
1108 OHOS::buffer::Blob *blob = new OHOS::buffer::Blob();
1109 uint8_t data[4] = {1, 2, 3, 4};
1110 blob->Init(data, 4);
1111
1112 uint8_t byte = blob->GetByte(2);
1113
1114 ASSERT_EQ(byte, 3);
1115 }
1116
1117 /**
1118 * @tc.name: BlobGetRawTest001
1119 * @tc.desc: Get the raw in blob
1120 * @tc.type: FUNC
1121 * @tc.require:issueI5J5Z3
1122 */
1123 HWTEST_F(NativeEngineTest, BlobGetRawTest001, testing::ext::TestSize.Level0)
1124 {
1125 OHOS::buffer::Blob *blob = new OHOS::buffer::Blob();
1126 uint8_t data[4] = {1, 2, 3, 4};
1127 blob->Init(data, 4);
1128
1129 uint8_t *raw = blob->GetRaw();
1130
1131 ASSERT_TRUE(raw != nullptr);
1132 }
1133
1134 /**
1135 * @tc.name: BlobGetLengthTest001
1136 * @tc.desc: Get the length in blob
1137 * @tc.type: FUNC
1138 * @tc.require:issueI5J5Z3
1139 */
1140 HWTEST_F(NativeEngineTest, BlobGetLengthTest001, testing::ext::TestSize.Level0)
1141 {
1142 OHOS::buffer::Blob *blob = new OHOS::buffer::Blob();
1143 uint8_t data[4] = {1, 2, 3, 4};
1144 blob->Init(data, 4);
1145
1146 unsigned int len = blob->GetLength();
1147
1148 ASSERT_EQ(len, 4);
1149 }
1150
1151 /**
1152 * @tc.name: BlobGetLengthTest001
1153 * @tc.desc: Read blob object bytes
1154 * @tc.type: FUNC
1155 * @tc.require:issueI5J5Z3
1156 */
1157 HWTEST_F(NativeEngineTest, BlobReadBytesTest001, testing::ext::TestSize.Level0)
1158 {
1159 OHOS::buffer::Blob *blob = new OHOS::buffer::Blob();
1160 uint8_t data[10] = {0};
1161 for (int i = 0; i < 10; i++) {
1162 data[i] = i;
1163 }
1164 blob->Init(data, 10);
1165
1166 uint8_t dat[10] = {0};
1167 blob->ReadBytes(dat, 10);
1168
1169 for (int i = 0; i < 10; i++) {
1170 ASSERT_EQ(dat[i], i);
1171 }
1172 }
1173
1174 /**
1175 * @tc.name: Utf8ToUtf16BETest001
1176 * @tc.desc: convert utf8 bytes to utf16 bytes
1177 * @tc.type: FUNC
1178 * @tc.require:issueI5J5Z3
1179 */
1180 HWTEST_F(NativeEngineTest, Utf8ToUtf16BETest001, testing::ext::TestSize.Level0)
1181 {
1182 std::string str8 = "";
1183 // one byte
1184 str8.append(1, 0x41);
1185 // two bytes
1186 str8.append(1, 0xC3);
1187 str8.append(1, 0x84);
1188 // three bytes
1189 str8.append(1, 0xE5);
1190 str8.append(1, 0x88);
1191 str8.append(1, 0x98);
1192 // four bytes
1193 str8.append(1, 0xf0);
1194 str8.append(1, 0x9f);
1195 str8.append(1, 0x90);
1196 str8.append(1, 0x85);
1197
1198 // another four bytes
1199 str8.append(1, 0xf0);
1200 str8.append(1, 0x8f);
1201 str8.append(1, 0x90);
1202 str8.append(1, 0x85);
1203
1204 bool isOk = false;
1205 std::u16string str16 = OHOS::buffer::Utf8ToUtf16BE(str8, &isOk);
1206
1207 char16_t results[] = {0x41, 0xc4, 0x5218, 0xd83d, 0xdc05, 0xf405};
1208 for (int i = 0; i < 6; i++) {
1209 ASSERT_EQ(results[i], str16[i]);
1210 }
1211 }
1212
1213 /**
1214 * @tc.name: HexDecodeTest001
1215 * @tc.desc: decode a hex string
1216 * @tc.type: FUNC
1217 * @tc.require:issueI5J5Z3
1218 */
1219 HWTEST_F(NativeEngineTest, HexDecodeTest001, testing::ext::TestSize.Level0)
1220 {
1221 std::string ret = OHOS::buffer::HexDecode("313g");
1222 ASSERT_EQ(ret, "1");
1223 }
1224