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 #include <codecvt>
18 #include "commonlibrary/ets_utils/js_util_module/util/js_base64.h"
19 #include "commonlibrary/ets_utils/js_util_module/util/js_uuid.h"
20 #include "commonlibrary/ets_utils/js_util_module/util/js_stringdecoder.h"
21 #include "commonlibrary/ets_utils/js_util_module/util/js_textencoder.h"
22 #include "commonlibrary/ets_utils/js_util_module/util/js_textdecoder.h"
23 #include "commonlibrary/ets_utils/js_util_module/util/js_types.h"
24 #include "ohos/init_data.h"
25 #include "tools/log.h"
26 #include "napi/native_api.h"
27 #include "napi/native_node_api.h"
28 #include "securec.h"
29
30
31 #define ASSERT_CHECK_CALL(call) \
32 { \
33 ASSERT_EQ(call, napi_ok); \
34 }
35
36 #define ASSERT_CHECK_VALUE_TYPE(env, value, type) \
37 { \
38 napi_valuetype valueType = napi_undefined; \
39 ASSERT_TRUE(value != nullptr); \
40 ASSERT_CHECK_CALL(napi_typeof(env, value, &valueType)); \
41 ASSERT_EQ(valueType, type); \
42 }
43
44 /* @tc.name: GetStringUUIDTest001
45 * @tc.desc: Test Generate a random RFC 4122 version 4 UUID.
46 * @tc.type: FUNC
47 */
48 HWTEST_F(NativeEngineTest, GetStringUUIDTest001, testing::ext::TestSize.Level0)
49 {
50 napi_env env = (napi_env)engine_;
51 std::string uuid = OHOS::Util::GetStringUUID(env, true);
52 ASSERT_EQ(uuid.length(), 36);
53 }
54
55 /* @tc.name: GetStringUUIDTest002
56 * @tc.desc: Test Generate a random RFC 4122 version 4 UUID.
57 * @tc.type: FUNC
58 */
59 HWTEST_F(NativeEngineTest, GetStringUUIDTest002, testing::ext::TestSize.Level0)
60 {
61 napi_env env = (napi_env)engine_;
62 std::string uuid = OHOS::Util::GetStringUUID(env, false);
63 ASSERT_EQ(uuid.length(), 36);
64 }
65
66 /* @tc.name: GetBinaryUUIDTest001
67 * @tc.desc: Test Generate a random RFC 4122 version 4 UUID.
68 * @tc.type: FUNC
69 */
70 HWTEST_F(NativeEngineTest, GetBinaryUUIDTest001, testing::ext::TestSize.Level0)
71 {
72 napi_env env = (napi_env)engine_;
73 napi_value arr = OHOS::Util::GetBinaryUUID(env, true);
74 napi_typedarray_type type = napi_int8_array;
75 size_t byteOffset = 0;
76 size_t length = 0;
77 void* resultData = nullptr;
78 napi_value resultBuffer = nullptr;
79 napi_get_typedarray_info(env, arr, &type, &length, &resultData, &resultBuffer, &byteOffset);
80 ASSERT_EQ(length, 16);
81 }
82
83 /* @tc.name: GetBinaryUUIDTest002
84 * @tc.desc: Test Generate a random RFC 4122 version 4 UUID.
85 * @tc.type: FUNC
86 */
87 HWTEST_F(NativeEngineTest, GetBinaryUUIDTest002, testing::ext::TestSize.Level0)
88 {
89 napi_env env = (napi_env)engine_;
90 napi_value arr = OHOS::Util::GetBinaryUUID(env, false);
91 napi_typedarray_type type = napi_int8_array;
92 size_t byteOffset = 0;
93 size_t length = 0;
94 void* resultData = nullptr;
95 napi_value resultBuffer = nullptr;
96 napi_get_typedarray_info(env, arr, &type, &length, &resultData, &resultBuffer, &byteOffset);
97 ASSERT_EQ(length, 16);
98 }
99
100 /* @tc.name: DoParseUUIDTest001
101 * @tc.desc: Parse a UUID from the string standard representation as described in the RFC 4122 version 4.
102 * @tc.type: FUNC
103 */
104 HWTEST_F(NativeEngineTest, DoParseUUIDTest001, testing::ext::TestSize.Level0)
105 {
106 napi_env env = (napi_env)engine_;
107 napi_value src = nullptr;
108 napi_create_string_utf8(env, "84bdf796-66cc-4655-9b89-d6218d100f9c", NAPI_AUTO_LENGTH, &src);
109 napi_value arr = OHOS::Util::DoParseUUID(env, src);
110 napi_typedarray_type type = napi_int8_array;
111 size_t byteOffset = 0;
112 size_t length = 0;
113 void* resultData = nullptr;
114 napi_value resultBuffer = nullptr;
115 napi_get_typedarray_info(env, arr, &type, &length, &resultData, &resultBuffer, &byteOffset);
116 ASSERT_EQ(length, 16);
117 }
118
119 /* @tc.name: DoParseUUIDTest002
120 * @tc.desc: Parse a UUID from the string standard representation as described in the RFC 4122 version 4.
121 * @tc.type: FUNC
122 */
123 HWTEST_F(NativeEngineTest, DoParseUUIDTest002, testing::ext::TestSize.Level0)
124 {
125 napi_env env = (napi_env)engine_;
126 napi_value src = nullptr;
127 std::string input = "abc123";
128 napi_create_string_utf8(env, input.c_str(), NAPI_AUTO_LENGTH, &src);
129 napi_value arr = OHOS::Util::DoParseUUID(env, src);
130 napi_typedarray_type type = napi_int8_array;
131 size_t byteOffset = 0;
132 size_t length = 0;
133 void* resultData = nullptr;
134 napi_value resultBuffer = nullptr;
135 napi_get_typedarray_info(env, arr, &type, &length, &resultData, &resultBuffer, &byteOffset);
136 ASSERT_EQ(length, 16);
137 }
138
139 /* @tc.name: DoParseUUIDTest003
140 * @tc.desc: Parse a UUID from the string standard representation as described in the RFC 4122 version 4.
141 * @tc.type: FUNC
142 */
143 HWTEST_F(NativeEngineTest, DoParseUUIDTest003, testing::ext::TestSize.Level0)
144 {
145 napi_env env = (napi_env)engine_;
146 napi_value src = nullptr;
147 std::string input = "abc123abc";
148 napi_create_string_utf8(env, input.c_str(), NAPI_AUTO_LENGTH, &src);
149 napi_value arr = OHOS::Util::DoParseUUID(env, src);
150 napi_typedarray_type type = napi_int8_array;
151 size_t byteOffset = 0;
152 size_t length = 0;
153 void* resultData = nullptr;
154 napi_value resultBuffer = nullptr;
155 napi_get_typedarray_info(env, arr, &type, &length, &resultData, &resultBuffer, &byteOffset);
156 ASSERT_EQ(length, 16);
157 }
158
159 /* @tc.name: DoParseUUIDTest004
160 * @tc.desc: Parse a UUID from the string standard representation as described in the RFC 4122 version 4.
161 * @tc.type: FUNC
162 */
163 HWTEST_F(NativeEngineTest, DoParseUUIDTest004, testing::ext::TestSize.Level0)
164 {
165 napi_env env = (napi_env)engine_;
166 napi_value src = nullptr;
167 napi_value arr = OHOS::Util::DoParseUUID(env, src);
168 ASSERT_EQ(arr, nullptr);
169 }
170
171 /* @tc.name: HexToCharUUIDTest001
172 * @tc.desc: Hex to char with g convert to x.
173 * @tc.type: FUNC
174 */
175 HWTEST_F(NativeEngineTest, HexToCharUUIDTest001, testing::ext::TestSize.Level0)
176 {
177 unsigned char input = 'g';
178 unsigned char res = OHOS::Util::HexToChar(input);
179 ASSERT_EQ(res, 'x');
180 }
181
182 /* @tc.name: getEncodingTest001
183 * @tc.desc: Test acquire encoding mode.
184 * @tc.type: FUNC
185 */
186 HWTEST_F(NativeEngineTest, getEncodingTest001, testing::ext::TestSize.Level0)
187 {
188 HILOG_INFO("getEncodingTest001 start");
189 napi_env env = (napi_env)engine_;
190
191 OHOS::Util::TextEncoder textEncoder("GBK");
192 textEncoder.SetOrgEncoding("GBK");
193 napi_value result = textEncoder.GetEncoding(env);
194
195 char *buffer = nullptr;
196 size_t bufferSize = 0;
197 napi_get_value_string_utf8(env, result, buffer, -1, &bufferSize);
198 if (bufferSize > 0) {
199 buffer = new char[bufferSize + 1];
200 napi_get_value_string_utf8(env, result, buffer, bufferSize + 1, &bufferSize);
201 }
202
203 ASSERT_STREQ(buffer, "GBK");
204 if (buffer != nullptr) {
205 delete []buffer;
206 buffer = nullptr;
207 }
208 }
209
210 /* @tc.name: getEncodingTest002
211 * @tc.desc: Test acquire encoding mode.
212 * @tc.type: FUNC
213 */
214 HWTEST_F(NativeEngineTest, getEncodingTest002, testing::ext::TestSize.Level0)
215 {
216 HILOG_INFO("getEncodingTest002 start");
217 napi_env env = (napi_env)engine_;
218
219 OHOS::Util::TextEncoder textEncoder("gb18030");
220 textEncoder.SetOrgEncoding("gb18030");
221 napi_value result = textEncoder.GetEncoding(env);
222
223 char *buffer = nullptr;
224 size_t bufferSize = 0;
225 napi_get_value_string_utf8(env, result, buffer, -1, &bufferSize);
226 if (bufferSize > 0) {
227 buffer = new char[bufferSize + 1];
228 napi_get_value_string_utf8(env, result, buffer, bufferSize + 1, &bufferSize);
229 }
230
231 ASSERT_STREQ(buffer, "gb18030");
232 if (buffer != nullptr) {
233 delete []buffer;
234 buffer = nullptr;
235 }
236 }
237
238 /* @tc.name: getEncodingTest003
239 * @tc.desc: Test acquire encoding mode.
240 * @tc.type: FUNC
241 */
242 HWTEST_F(NativeEngineTest, getEncodingTest003, testing::ext::TestSize.Level0)
243 {
244 HILOG_INFO("getEncodingTest003 start");
245 napi_env env = (napi_env)engine_;
246
247 OHOS::Util::TextEncoder textEncoder("GB18030");
248 textEncoder.SetOrgEncoding("GB18030");
249 napi_value result = textEncoder.GetEncoding(env);
250
251 char *buffer = nullptr;
252 size_t bufferSize = 0;
253 napi_get_value_string_utf8(env, result, buffer, -1, &bufferSize);
254 if (bufferSize > 0) {
255 buffer = new char[bufferSize + 1];
256 napi_get_value_string_utf8(env, result, buffer, bufferSize + 1, &bufferSize);
257 }
258
259 ASSERT_STREQ(buffer, "GB18030");
260 if (buffer != nullptr) {
261 delete []buffer;
262 buffer = nullptr;
263 }
264 }
265
266 /**
267 * @tc.name: textEncodeTest001
268 * @tc.desc: Test encode src.
269 * @tc.type: FUNC
270 */
271 HWTEST_F(NativeEngineTest, textEncodeTest001, testing::ext::TestSize.Level0)
272 {
273 HILOG_INFO("getEncodingTest001 start");
274 napi_env env = (napi_env)engine_;
275 OHOS::Util::TextEncoder textEncoder("utf-8");
276
277 std::string input = "abc123";
278 napi_value src = nullptr;
279 napi_create_string_utf8(env, input.c_str(), input.size(), &src);
280 napi_value result = textEncoder.Encode(env, src);
281
282 char excepted[7] = {0x61, 0x62, 0x63, 0x31, 0x32, 0x33, 0};
283
284 napi_typedarray_type type;
285 size_t srcLength = 0;
286 void* srcData = nullptr;
287 napi_value srcBuffer = nullptr;
288 size_t byteOffset = 0;
289
290 napi_get_typedarray_info(
291 env, result, &type, &srcLength, &srcData, &srcBuffer, &byteOffset);
292
293 ASSERT_EQ(srcLength, 6);
294 char* res = reinterpret_cast<char*>(srcData);
295
296 res[srcLength] = 0;
297 ASSERT_STREQ(res, excepted);
298 }
299
300 /**
301 * @tc.name: textEncodeTest002
302 * @tc.desc: Test encode src.
303 * @tc.type: FUNC
304 */
305 HWTEST_F(NativeEngineTest, textEncodeTest002, testing::ext::TestSize.Level0)
306 {
307 HILOG_INFO("getEncodingTest002 start");
308 napi_env env = (napi_env)engine_;
309 OHOS::Util::TextEncoder textEncoder("utf-8");
310
311 std::string input = "";
312 napi_value src = nullptr;
313 napi_create_string_utf8(env, input.c_str(), input.size(), &src);
314 napi_value result = textEncoder.Encode(env, src);
315
316 napi_typedarray_type type;
317 size_t srcLength = 0;
318 void* srcData = nullptr;
319 napi_value srcBuffer = nullptr;
320 size_t byteOffset = 0;
321
322 napi_get_typedarray_info(
323 env, result, &type, &srcLength, &srcData, &srcBuffer, &byteOffset);
324
325 ASSERT_STREQ((char*)srcData, nullptr);
326 }
327
328 /**
329 * @tc.name: textEncodeTest003
330 * @tc.desc: Test encode src.
331 * @tc.type: FUNC
332 */
333 HWTEST_F(NativeEngineTest, textEncodeTest003, testing::ext::TestSize.Level0)
334 {
335 HILOG_INFO("getEncodingTest003 start");
336 napi_env env = (napi_env)engine_;
337 OHOS::Util::TextEncoder textEncoder("utf-8");
338
339 std::string input = "text";
340 napi_value src = nullptr;
341 napi_create_string_utf8(env, input.c_str(), input.size(), &src);
342 napi_value result = textEncoder.Encode(env, src);
343
344 char excepted[7] = {0x74, 0x65, 0x78, 0x74, 0};
345
346 napi_typedarray_type type;
347 size_t srcLength = 0;
348 void* srcData = nullptr;
349 napi_value srcBuffer = nullptr;
350 size_t byteOffset = 0;
351
352 napi_get_typedarray_info(
353 env, result, &type, &srcLength, &srcData, &srcBuffer, &byteOffset);
354
355 ASSERT_EQ(srcLength, 4);
356 char* res = reinterpret_cast<char*>(srcData);
357
358 res[srcLength] = 0;
359 ASSERT_STREQ(res, excepted);
360 }
361
362 /**
363 * @tc.name: textEncodeTest004
364 * @tc.desc: Test encode src.
365 * @tc.type: FUNC
366 */
367 HWTEST_F(NativeEngineTest, textEncodeTest004, testing::ext::TestSize.Level0)
368 {
369 SetHwIcuDirectory();
370 napi_env env = (napi_env)engine_;
371 OHOS::Util::TextEncoder textEncoder("gbk");
372
373 std::string input = "abc123";
374 napi_value src = nullptr;
375 napi_create_string_utf8(env, input.c_str(), input.size(), &src);
376 napi_value result = textEncoder.Encode(env, src);
377
378 char excepted[7] = {0x61, 0x62, 0x63, 0x31, 0x32, 0x33, 0}; // 7:nums of args
379
380 napi_typedarray_type type;
381 size_t srcLength = 0;
382 void *srcData = nullptr;
383 napi_value srcBuffer = nullptr;
384 size_t byteOffset = 0;
385
386 napi_get_typedarray_info(
387 env, result, &type, &srcLength, &srcData, &srcBuffer, &byteOffset);
388
389 ASSERT_EQ(srcLength, 6); // 6:string length
390 char *res = reinterpret_cast<char*>(srcData);
391
392 res[srcLength] = 0;
393 ASSERT_STREQ(res, excepted);
394 }
395
396 /**
397 * @tc.name: textEncodeTest005
398 * @tc.desc: Test encode src.
399 * @tc.type: FUNC
400 */
401 HWTEST_F(NativeEngineTest, textEncodeTest005, testing::ext::TestSize.Level0)
402 {
403 napi_env env = (napi_env)engine_;
404 OHOS::Util::TextEncoder textEncoder("utf-8");
405 napi_value src = nullptr;
406 napi_value result = textEncoder.Encode(env, src);
407 ASSERT_TRUE(result == nullptr);
408 }
409
410 /**
411 * @tc.name: textEncodeTest006
412 * @tc.desc: Test encode src.
413 * @tc.type: FUNC
414 */
415 HWTEST_F(NativeEngineTest, textEncodeTest006, testing::ext::TestSize.Level0)
416 {
417 HILOG_INFO("textEncodeTest006 start");
418 SetHwIcuDirectory();
419 napi_env env = (napi_env)engine_;
420 OHOS::Util::TextEncoder textEncoder("big5");
421
422 std::string input = "abc哈熠";
423 napi_value src = nullptr;
424 napi_create_string_utf8(env, input.c_str(), input.size(), &src);
425 napi_value result = textEncoder.Encode(env, src);
426
427 char excepted[8] = {0x61, 0x62, 0x63, 0xAB, 0xA2, 0xE6, 0x66, 0}; // 8:nums of args
428
429 napi_typedarray_type type;
430 size_t srcLength = 0;
431 void *srcData = nullptr;
432 napi_value srcBuffer = nullptr;
433 size_t byteOffset = 0;
434
435 napi_get_typedarray_info(
436 env, result, &type, &srcLength, &srcData, &srcBuffer, &byteOffset);
437
438 ASSERT_EQ(srcLength, 7); // 7:string length
439 char *res = reinterpret_cast<char*>(srcData);
440
441 res[srcLength] = 0;
442 ASSERT_STREQ(res, excepted);
443 }
444
445 /**
446 * @tc.name: textEncodeTest007
447 * @tc.desc: Test encode src.
448 * @tc.type: FUNC
449 */
450 HWTEST_F(NativeEngineTest, textEncodeTest007, testing::ext::TestSize.Level0)
451 {
452 HILOG_INFO("textEncodeTest007 start");
453 SetHwIcuDirectory();
454 napi_env env = (napi_env)engine_;
455 OHOS::Util::TextEncoder textEncoder("shift_jis");
456
457 std::string input = "abc哈熠";
458 napi_value src = nullptr;
459 napi_create_string_utf8(env, input.c_str(), input.size(), &src);
460 napi_value result = textEncoder.Encode(env, src);
461
462 char excepted[8] = {0x61, 0x62, 0x63, 0x99, 0xFB, 0xFC, 0xFC, 0}; // 8:nums of args
463
464 napi_typedarray_type type;
465 size_t srcLength = 0;
466 void *srcData = nullptr;
467 napi_value srcBuffer = nullptr;
468 size_t byteOffset = 0;
469
470 napi_get_typedarray_info(
471 env, result, &type, &srcLength, &srcData, &srcBuffer, &byteOffset);
472
473 ASSERT_EQ(srcLength, 7); // 7:string length
474 char *res = reinterpret_cast<char*>(srcData);
475
476 res[srcLength] = 0;
477 ASSERT_STREQ(res, excepted);
478 }
479
480 /**
481 * @tc.name: textEncodeTest008
482 * @tc.desc: Test encode src.
483 * @tc.type: FUNC
484 */
485 HWTEST_F(NativeEngineTest, textEncodeTest008, testing::ext::TestSize.Level0)
486 {
487 HILOG_INFO("textEncodeTest008 start");
488 SetHwIcuDirectory();
489 napi_env env = (napi_env)engine_;
490 OHOS::Util::TextEncoder textEncoder("iso-2022-jp");
491
492 std::string input = "abc哈熠";
493 napi_value src = nullptr;
494 napi_create_string_utf8(env, input.c_str(), input.size(), &src);
495 napi_value result = textEncoder.Encode(env, src);
496
497 char excepted[13] = {0x61, 0x62, 0x63, 0x1B, 0x24, 0x42, 0x52, 0x7D, 0x1B, 0x28, 0x42, 0x1A, 0}; // 13:nums of args
498
499 napi_typedarray_type type;
500 size_t srcLength = 0;
501 void *srcData = nullptr;
502 napi_value srcBuffer = nullptr;
503 size_t byteOffset = 0;
504
505 napi_get_typedarray_info(
506 env, result, &type, &srcLength, &srcData, &srcBuffer, &byteOffset);
507
508 ASSERT_EQ(srcLength, 12); // 12:string length
509 char *res = reinterpret_cast<char*>(srcData);
510
511 res[srcLength] = 0;
512 ASSERT_STREQ(res, excepted);
513 }
514
515 /**
516 * @tc.name: textEncodeTest009
517 * @tc.desc: Test encode src.
518 * @tc.type: FUNC
519 */
520 HWTEST_F(NativeEngineTest, textEncodeTest009, testing::ext::TestSize.Level0)
521 {
522 HILOG_INFO("textEncodeTest009 start");
523 SetHwIcuDirectory();
524 napi_env env = (napi_env)engine_;
525 OHOS::Util::TextEncoder textEncoder("ibm866");
526
527 std::string input = "abc哈熠";
528 napi_value src = nullptr;
529 napi_create_string_utf8(env, input.c_str(), input.size(), &src);
530 napi_value result = textEncoder.Encode(env, src);
531
532 char excepted[6] = {0x61, 0x62, 0x63, 0x7F, 0x7F, 0}; // 6:nums of args
533
534 napi_typedarray_type type;
535 size_t srcLength = 0;
536 void *srcData = nullptr;
537 napi_value srcBuffer = nullptr;
538 size_t byteOffset = 0;
539
540 napi_get_typedarray_info(
541 env, result, &type, &srcLength, &srcData, &srcBuffer, &byteOffset);
542
543 ASSERT_EQ(srcLength, 5); // 5:string length
544 char *res = reinterpret_cast<char*>(srcData);
545
546 res[srcLength] = 0;
547 ASSERT_STREQ(res, excepted);
548 }
549
550 /**
551 * @tc.name: textEncodeTest010
552 * @tc.desc: Test encode src.
553 * @tc.type: FUNC
554 */
555 HWTEST_F(NativeEngineTest, textEncodeTest010, testing::ext::TestSize.Level0)
556 {
557 HILOG_INFO("textEncodeTest010 start");
558 SetHwIcuDirectory();
559 napi_env env = (napi_env)engine_;
560 OHOS::Util::TextEncoder textEncoder("macintosh");
561
562 std::string input = "abc哈熠";
563 napi_value src = nullptr;
564 napi_create_string_utf8(env, input.c_str(), input.size(), &src);
565 napi_value result = textEncoder.Encode(env, src);
566
567 char excepted[6] = {0x61, 0x62, 0x63, 0x3F, 0x3F, 0}; // 6:nums of args
568
569 napi_typedarray_type type;
570 size_t srcLength = 0;
571 void *srcData = nullptr;
572 napi_value srcBuffer = nullptr;
573 size_t byteOffset = 0;
574
575 napi_get_typedarray_info(
576 env, result, &type, &srcLength, &srcData, &srcBuffer, &byteOffset);
577
578 ASSERT_EQ(srcLength, 5); // 5:string length
579 char *res = reinterpret_cast<char*>(srcData);
580
581 res[srcLength] = 0;
582 ASSERT_STREQ(res, excepted);
583 }
584
585 /**
586 * @tc.name: textEncodeIntoTest001
587 * @tc.desc: Test returns a dictionary object indicating the progress of the encoding
588 * @tc.type: FUNC
589 */
590 HWTEST_F(NativeEngineTest, textEncodeIntoTest001, testing::ext::TestSize.Level0)
591 {
592 HILOG_INFO("textEncodeIntoTest001 start");
593 napi_env env = (napi_env)engine_;
594 OHOS::Util::TextEncoder textEncoder("utf-8");
595
596 std::string input = "abc123";
597 napi_value src = nullptr;
598 napi_create_string_utf8(env, input.c_str(), input.size(), &src);
599
600 napi_value arrayBuffer = nullptr;
601 void* arrayBufferPtr = nullptr;
602 size_t arrayBufferSize = 20;
603 napi_create_arraybuffer(env, arrayBufferSize, &arrayBufferPtr, &arrayBuffer);
604
605 napi_value dest = nullptr;
606 napi_create_typedarray(env, napi_int8_array, arrayBufferSize, arrayBuffer, 0, &dest);
607
608 napi_value result = textEncoder.EncodeInto(env, src, dest);
609
610 napi_value read = nullptr;
611 napi_get_named_property(env, result, "read", &read);
612
613 uint32_t resRead = 0;
614
615 napi_get_value_uint32(env, read, &resRead);
616
617 napi_value written = nullptr;
618 napi_get_named_property(env, result, "written", &written);
619
620 uint32_t resWritten = 0;
621 napi_get_value_uint32(env, read, &resWritten);
622
623 ASSERT_EQ(resRead, static_cast<uint32_t>(6));
624 ASSERT_EQ(resWritten, static_cast<uint32_t>(6));
625 }
626
627 /**
628 * @tc.name: textEncodeIntoTest002
629 * @tc.desc: Test returns a dictionary object indicating the progress of the encoding
630 * @tc.type: FUNC
631 */
632 HWTEST_F(NativeEngineTest, textEncodeIntoTest002, testing::ext::TestSize.Level0)
633 {
634 HILOG_INFO("textEncodeIntoTest002 start");
635 napi_env env = (napi_env)engine_;
636 OHOS::Util::TextEncoder textEncoder("utf-8");
637
638 std::string input = "text";
639 napi_value src = nullptr;
640 napi_create_string_utf8(env, input.c_str(), input.size(), &src);
641
642 napi_value arrayBuffer = nullptr;
643 void* arrayBufferPtr = nullptr;
644 size_t arrayBufferSize = 20;
645 napi_create_arraybuffer(env, arrayBufferSize, &arrayBufferPtr, &arrayBuffer);
646
647 napi_value dest = nullptr;
648 napi_create_typedarray(env, napi_int8_array, arrayBufferSize, arrayBuffer, 0, &dest);
649
650 napi_value result = textEncoder.EncodeInto(env, src, dest);
651
652 napi_value read = nullptr;
653 napi_get_named_property(env, result, "read", &read);
654
655 uint32_t resRead = 0;
656
657 napi_get_value_uint32(env, read, &resRead);
658
659 napi_value written = nullptr;
660 napi_get_named_property(env, result, "written", &written);
661
662 uint32_t resWritten = 0;
663 napi_get_value_uint32(env, read, &resWritten);
664
665 ASSERT_EQ(resRead, static_cast<uint32_t>(4));
666 ASSERT_EQ(resWritten, static_cast<uint32_t>(4));
667 }
668
669 /**
670 * @tc.name: textEncodeIntoTest003
671 * @tc.desc: Test returns a dictionary object indicating the progress of the encoding
672 * @tc.type: FUNC
673 */
674 HWTEST_F(NativeEngineTest, textEncodeIntoTest003, testing::ext::TestSize.Level0)
675 {
676 HILOG_INFO("textEncodeIntoTest003 start");
677 napi_env env = (napi_env)engine_;
678 OHOS::Util::TextEncoder textEncoder("utf-8");
679
680 std::string input = "12345";
681 napi_value src = nullptr;
682 napi_create_string_utf8(env, input.c_str(), input.size(), &src);
683
684 napi_value arrayBuffer = nullptr;
685 void* arrayBufferPtr = nullptr;
686 size_t arrayBufferSize = 20;
687 napi_create_arraybuffer(env, arrayBufferSize, &arrayBufferPtr, &arrayBuffer);
688
689 napi_value dest = nullptr;
690 napi_create_typedarray(env, napi_int8_array, arrayBufferSize, arrayBuffer, 0, &dest);
691
692 napi_value result = textEncoder.EncodeInto(env, src, dest);
693
694 napi_value read = nullptr;
695 napi_get_named_property(env, result, "read", &read);
696
697 uint32_t resRead = 0;
698
699 napi_get_value_uint32(env, read, &resRead);
700
701 napi_value written = nullptr;
702 napi_get_named_property(env, result, "written", &written);
703
704 uint32_t resWritten = 0;
705 napi_get_value_uint32(env, read, &resWritten);
706
707 ASSERT_EQ(resRead, static_cast<uint32_t>(5));
708 ASSERT_EQ(resWritten, static_cast<uint32_t>(5));
709 }
710
711 /**
712 * @tc.name: textEncodeIntoTest004
713 * @tc.desc: Test returns a dictionary object indicating the progress of the encoding
714 * @tc.type: FUNC
715 */
716 HWTEST_F(NativeEngineTest, textEncodeIntoTest004, testing::ext::TestSize.Level0)
717 {
718 HILOG_INFO("textEncodeIntoTest004 start");
719 SetHwIcuDirectory();
720 napi_env env = (napi_env)engine_;
721 OHOS::Util::TextEncoder textEncoder("big5");
722
723 std::string input = "abc123";
724 napi_value src = nullptr;
725 napi_create_string_utf8(env, input.c_str(), input.size(), &src);
726
727 napi_value arrayBuffer = nullptr;
728 void* arrayBufferPtr = nullptr;
729 size_t arrayBufferSize = 20;
730 napi_create_arraybuffer(env, arrayBufferSize, &arrayBufferPtr, &arrayBuffer);
731
732 napi_value dest = nullptr;
733 napi_create_typedarray(env, napi_uint8_array, arrayBufferSize, arrayBuffer, 0, &dest);
734
735 napi_value result = textEncoder.EncodeInto(env, src, dest);
736
737 napi_value read = nullptr;
738 napi_get_named_property(env, result, "read", &read);
739
740 uint32_t resRead = 0;
741
742 napi_get_value_uint32(env, read, &resRead);
743
744 napi_value written = nullptr;
745 napi_get_named_property(env, result, "written", &written);
746
747 uint32_t resWritten = 0;
748 napi_get_value_uint32(env, read, &resWritten);
749
750 ASSERT_EQ(resRead, static_cast<uint32_t>(6));
751 ASSERT_EQ(resWritten, static_cast<uint32_t>(6));
752 }
753
754 /**
755 * @tc.name: textEncodeIntoTest005
756 * @tc.desc: Test returns a dictionary object indicating the progress of the encoding
757 * @tc.type: FUNC
758 */
759 HWTEST_F(NativeEngineTest, textEncodeIntoTest005, testing::ext::TestSize.Level0)
760 {
761 HILOG_INFO("textEncodeIntoTest005 start");
762 SetHwIcuDirectory();
763 napi_env env = (napi_env)engine_;
764 OHOS::Util::TextEncoder textEncoder("shift_jis");
765
766 std::string input = "abc123哈";
767 napi_value src = nullptr;
768 napi_create_string_utf8(env, input.c_str(), input.size(), &src);
769
770 napi_value arrayBuffer = nullptr;
771 void* arrayBufferPtr = nullptr;
772 size_t arrayBufferSize = 20;
773 napi_create_arraybuffer(env, arrayBufferSize, &arrayBufferPtr, &arrayBuffer);
774
775 napi_value dest = nullptr;
776 napi_create_typedarray(env, napi_uint8_array, arrayBufferSize, arrayBuffer, 0, &dest);
777
778 napi_value result = textEncoder.EncodeInto(env, src, dest);
779
780 napi_value read = nullptr;
781 napi_get_named_property(env, result, "read", &read);
782
783 uint32_t resRead = 0;
784
785 napi_get_value_uint32(env, read, &resRead);
786
787 napi_value written = nullptr;
788 napi_get_named_property(env, result, "written", &written);
789
790 uint32_t resWritten = 0;
791 napi_get_value_uint32(env, read, &resWritten);
792
793 ASSERT_EQ(resRead, static_cast<uint32_t>(7));
794 ASSERT_EQ(resWritten, static_cast<uint32_t>(7));
795 }
796
797 /**
798 * @tc.name: textEncodeIntoTest006
799 * @tc.desc: Test returns a dictionary object indicating the progress of the encoding
800 * @tc.type: FUNC
801 */
802 HWTEST_F(NativeEngineTest, textEncodeIntoTest006, testing::ext::TestSize.Level0)
803 {
804 HILOG_INFO("textEncodeIntoTest006 start");
805 SetHwIcuDirectory();
806 napi_env env = (napi_env)engine_;
807 OHOS::Util::TextEncoder textEncoder("iso-2022-jp");
808
809 std::string input = "abc123哈";
810 napi_value src = nullptr;
811 napi_create_string_utf8(env, input.c_str(), input.size(), &src);
812
813 napi_value arrayBuffer = nullptr;
814 void* arrayBufferPtr = nullptr;
815 size_t arrayBufferSize = 20;
816 napi_create_arraybuffer(env, arrayBufferSize, &arrayBufferPtr, &arrayBuffer);
817
818 napi_value dest = nullptr;
819 napi_create_typedarray(env, napi_uint8_array, arrayBufferSize, arrayBuffer, 0, &dest);
820
821 napi_value result = textEncoder.EncodeInto(env, src, dest);
822
823 napi_value read = nullptr;
824 napi_get_named_property(env, result, "read", &read);
825
826 uint32_t resRead = 0;
827
828 napi_get_value_uint32(env, read, &resRead);
829
830 napi_value written = nullptr;
831 napi_get_named_property(env, result, "written", &written);
832
833 uint32_t resWritten = 0;
834 napi_get_value_uint32(env, read, &resWritten);
835
836 ASSERT_EQ(resRead, static_cast<uint32_t>(7));
837 ASSERT_EQ(resWritten, static_cast<uint32_t>(7));
838 }
839
840 /**
841 * @tc.name: textEncodeIntoTest007
842 * @tc.desc: Test returns a dictionary object indicating the progress of the encoding
843 * @tc.type: FUNC
844 */
845 HWTEST_F(NativeEngineTest, textEncodeIntoTest007, testing::ext::TestSize.Level0)
846 {
847 HILOG_INFO("textEncodeIntoTest007 start");
848 SetHwIcuDirectory();
849 napi_env env = (napi_env)engine_;
850 OHOS::Util::TextEncoder textEncoder("ibm866");
851
852 std::string input = "abc123哈";
853 napi_value src = nullptr;
854 napi_create_string_utf8(env, input.c_str(), input.size(), &src);
855
856 napi_value arrayBuffer = nullptr;
857 void* arrayBufferPtr = nullptr;
858 size_t arrayBufferSize = 20;
859 napi_create_arraybuffer(env, arrayBufferSize, &arrayBufferPtr, &arrayBuffer);
860
861 napi_value dest = nullptr;
862 napi_create_typedarray(env, napi_uint8_array, arrayBufferSize, arrayBuffer, 0, &dest);
863
864 napi_value result = textEncoder.EncodeInto(env, src, dest);
865
866 napi_value read = nullptr;
867 napi_get_named_property(env, result, "read", &read);
868
869 uint32_t resRead = 0;
870
871 napi_get_value_uint32(env, read, &resRead);
872
873 napi_value written = nullptr;
874 napi_get_named_property(env, result, "written", &written);
875
876 uint32_t resWritten = 0;
877 napi_get_value_uint32(env, read, &resWritten);
878
879 ASSERT_EQ(resRead, static_cast<uint32_t>(7));
880 ASSERT_EQ(resWritten, static_cast<uint32_t>(7));
881 }
882
883 /**
884 * @tc.name: decoderUtf8001 utf-8
885 * @tc.desc: Test date type.
886 * @tc.type: FUNC
887 */
888 HWTEST_F(NativeEngineTest, decoderUtf8001, testing::ext::TestSize.Level0)
889 {
890 HILOG_INFO("decoderUtf8001 start");
891 napi_env env = (napi_env)engine_;
892 int32_t flags = static_cast<int>(OHOS::Util::TextDecoder::ConverterFlags::FATAL_FLG) |
893 static_cast<int>(OHOS::Util::TextDecoder::ConverterFlags::IGNORE_BOM_FLG);
894 std::string str = "utf-8";
895 OHOS::Util::TextDecoder textDecoder(str, flags);
896 bool iflag = false;
897 size_t byteLength = 3;
898 void* data = nullptr;
899 napi_value resultBuff = nullptr;
900 napi_create_arraybuffer(env, byteLength, &data, &resultBuff);
901 unsigned char arr[3] = {0x61, 0x62, 0x63};
902 int ret = memcpy_s(data, sizeof(arr), reinterpret_cast<void*>(arr), sizeof(arr));
903 ASSERT_EQ(0, ret);
904 napi_value result2 = nullptr;
905 napi_create_typedarray(env, napi_int8_array, byteLength, resultBuff, 0, &result2);
906 napi_value testString = textDecoder.Decode(env, result2, iflag);
907 size_t bufferSize = 0;
908 napi_get_value_string_utf8(env, testString, nullptr, 0, &bufferSize);
909 size_t length = 0;
910 char* ch = nullptr;
911 if (bufferSize > 0) {
912 ch = new char[bufferSize + 1]();
913 napi_get_value_string_utf8(env, testString, ch, bufferSize + 1, &length);
914 }
915 ASSERT_STREQ("abc", ch);
916 if (ch != nullptr) {
917 delete []ch;
918 ch = nullptr;
919 }
920 }
921
922 /**
923 * @tc.name: decoderUtf8002 utf-8
924 * @tc.desc: Test date type.
925 * @tc.type: FUNC
926 */
927 HWTEST_F(NativeEngineTest, decoderUtf8002, testing::ext::TestSize.Level0)
928 {
929 HILOG_INFO("decoderUtf8002 start");
930 napi_env env = (napi_env)engine_;
931 int32_t flags = static_cast<int>(OHOS::Util::TextDecoder::ConverterFlags::FATAL_FLG);
932 std::string str = "utf-8";
933 OHOS::Util::TextDecoder textDecoder(str, flags);
934 bool iflag = true;
935 size_t byteLength = 5;
936 void* data = nullptr;
937 napi_value resultBuff = nullptr;
938 napi_create_arraybuffer(env, byteLength, &data, &resultBuff);
939 unsigned char arr[5] = {0x61, '\0', 0x62, 0x63, '\0'};
940 int ret = memcpy_s(data, sizeof(arr), reinterpret_cast<void*>(arr), sizeof(arr));
941 ASSERT_EQ(0, ret);
942 napi_value result2 = nullptr;
943 napi_create_typedarray(env, napi_int8_array, byteLength, resultBuff, 0, &result2);
944 napi_value testString = textDecoder.Decode(env, result2, iflag);
945 size_t bufferSize = 0;
946 size_t length = 0;
947 napi_get_value_string_utf8(env, testString, nullptr, 0, &bufferSize);
948 char* ch = nullptr;
949 if (bufferSize > 0) {
950 ch = new char[bufferSize + 1]();
951 napi_get_value_string_utf8(env, testString, ch, bufferSize + 1, &length);
952 }
953 ASSERT_STREQ("a bc ", ch);
954 if (ch != nullptr) {
955 delete []ch;
956 ch = nullptr;
957 }
958 }
959
960 /**
961 * @tc.name: decoderUtf8003 utf-8
962 * @tc.desc: Test date type.
963 * @tc.type: FUNC
964 */
965 HWTEST_F(NativeEngineTest, decoderUtf8003, testing::ext::TestSize.Level0)
966 {
967 napi_env env = (napi_env)engine_;
968 int32_t flags = static_cast<int>(OHOS::Util::TextDecoder::ConverterFlags::FATAL_FLG) |
969 static_cast<int>(OHOS::Util::TextDecoder::ConverterFlags::IGNORE_BOM_FLG);
970 std::string str = "utf-8";
971 OHOS::Util::TextDecoder textDecoder(str, flags);
972 bool iflag = false;
973 size_t byteLength = 0;
974 void *data = nullptr;
975 napi_value resultBuff = nullptr;
976 napi_create_arraybuffer(env, byteLength, &data, &resultBuff);
977 napi_value result2 = nullptr;
978 napi_create_typedarray(env, napi_int8_array, byteLength, resultBuff, 0, &result2);
979 napi_value testString = textDecoder.Decode(env, result2, iflag);
980 ASSERT_TRUE(testString == nullptr);
981 }
982
983 /**
984 * @tc.name: decoderUtf16le001 utf-16le
985 * @tc.desc: Test date type.
986 * @tc.type: FUNC
987 */
988 HWTEST_F(NativeEngineTest, decoderUtf16le001, testing::ext::TestSize.Level0)
989 {
990 HILOG_INFO("decoderUtf16le001 start");
991 napi_env env = (napi_env)engine_;
992 int32_t flags = 0;
993 std::string str = "utf-16le";
994 OHOS::Util::TextDecoder textDecoder(str, flags);
995 bool iflag = false;
996 size_t byteLength = 6;
997 void* data = nullptr;
998 napi_value resultBuff = nullptr;
999 napi_create_arraybuffer(env, byteLength, &data, &resultBuff);
1000 unsigned char arr[6] = {0x61, 0x00, 0x62, 0x00, 0x63, 0x00};
1001 int ret = memcpy_s(data, sizeof(arr), reinterpret_cast<void*>(arr), sizeof(arr));
1002 ASSERT_EQ(0, ret);
1003 napi_value result2 = nullptr;
1004 napi_create_typedarray(env, napi_int8_array, byteLength, resultBuff, 0, &result2);
1005 napi_value testString = textDecoder.Decode(env, result2, iflag);
1006 size_t bufferSize = 0;
1007 size_t length = 0;
1008 napi_get_value_string_utf8(env, testString, nullptr, 0, &bufferSize);
1009 char* ch = nullptr;
1010 if (bufferSize > 0) {
1011 ch = new char[bufferSize + 1]();
1012 napi_get_value_string_utf8(env, testString, ch, bufferSize + 1, &length);
1013 }
1014 ASSERT_STREQ("abc", ch);
1015 if (ch != nullptr) {
1016 delete []ch;
1017 ch = nullptr;
1018 }
1019 }
1020
1021 /**
1022 * @tc.name: decoderUtf16le002 utf-16le
1023 * @tc.desc: Test date type.
1024 * @tc.type: FUNC
1025 */
1026 HWTEST_F(NativeEngineTest, decoderUtf16le002, testing::ext::TestSize.Level0)
1027 {
1028 HILOG_INFO("decoderUtf16le002 start");
1029 napi_env env = (napi_env)engine_;
1030 int32_t flags = static_cast<int>(OHOS::Util::TextDecoder::ConverterFlags::IGNORE_BOM_FLG);
1031 std::string str = "utf-16le";
1032 OHOS::Util::TextDecoder textDecoder(str, flags);
1033 bool iflag = true;
1034 size_t byteLength = 6;
1035 void* data = nullptr;
1036 napi_value resultBuff = nullptr;
1037 napi_create_arraybuffer(env, byteLength, &data, &resultBuff);
1038 unsigned char arr[6] = {0x61, 0x00, 0x62, 0x00, 0x63, 0x00};
1039 int ret = memcpy_s(data, sizeof(arr), reinterpret_cast<void*>(arr), sizeof(arr));
1040 ASSERT_EQ(0, ret);
1041 napi_value result2 = nullptr;
1042 napi_create_typedarray(env, napi_int8_array, byteLength, resultBuff, 0, &result2);
1043 napi_value testString = textDecoder.Decode(env, result2, iflag);
1044 size_t bufferSize = 0;
1045 napi_get_value_string_utf8(env, testString, nullptr, 0, &bufferSize);
1046 char* ch = nullptr;
1047 size_t length = 0;
1048 if (bufferSize > 0) {
1049 ch = new char[bufferSize + 1]();
1050 napi_get_value_string_utf8(env, testString, ch, bufferSize + 1, &length);
1051 }
1052 ASSERT_STREQ("abc", ch);
1053 if (ch != nullptr) {
1054 delete []ch;
1055 ch = nullptr;
1056 }
1057 }
1058
1059 /**
1060 * @tc.name: decoderUtf16le003 utf-16le
1061 * @tc.desc: Test date type.
1062 * @tc.type: FUNC
1063 */
1064 HWTEST_F(NativeEngineTest, decoderUtf16le003, testing::ext::TestSize.Level0)
1065 {
1066 HILOG_INFO("decoderUtf16le003 start");
1067 napi_env env = (napi_env)engine_;
1068 int32_t flags = 0;
1069 std::string str = "utf-16le";
1070 OHOS::Util::TextDecoder textDecoder(str, flags);
1071 bool iflag = true;
1072 size_t byteLength = 8;
1073 void* data = nullptr;
1074 napi_value resultBuff = nullptr;
1075 napi_create_arraybuffer(env, byteLength, &data, &resultBuff);
1076 unsigned char arr[8] = {0xFF, 0xFE, 0x61, 0x00, 0x62, 0x00, 0x63, 0x00};
1077 int ret = memcpy_s(data, sizeof(arr), reinterpret_cast<void*>(arr), sizeof(arr));
1078 ASSERT_EQ(0, ret);
1079 napi_value result2 = nullptr;
1080 napi_create_typedarray(env, napi_int8_array, byteLength, resultBuff, 0, &result2);
1081 napi_value testString = textDecoder.Decode(env, result2, iflag);
1082 size_t bufferSize = 0;
1083 napi_get_value_string_utf8(env, testString, nullptr, 0, &bufferSize);
1084 char* ch = nullptr;
1085 size_t length = 0;
1086 std::string tempStr01 = "";
1087 if (bufferSize > 0) {
1088 ch = new char[bufferSize + 1]();
1089 napi_get_value_string_utf8(env, testString, ch, bufferSize + 1, &length);
1090 tempStr01 = ch;
1091 }
1092 std::u16string tempU16str02 =
1093 std::wstring_convert<std::codecvt_utf8_utf16<char16_t>, char16_t> {}.from_bytes(tempStr01);
1094 ASSERT_EQ(0xFEFF, static_cast<int>(tempU16str02[0]));
1095 ASSERT_EQ(0x61, static_cast<int>(tempU16str02[1]));
1096 ASSERT_EQ(0x62, static_cast<int>(tempU16str02[2]));
1097 ASSERT_EQ(0x63, static_cast<int>(tempU16str02[3]));
1098 if (ch != nullptr) {
1099 delete []ch;
1100 ch = nullptr;
1101 }
1102 }
1103
1104 /**
1105 * @tc.name: decoderUtf16le004 utf-16le
1106 * @tc.desc: Test date type.
1107 * @tc.type: FUNC
1108 */
1109 HWTEST_F(NativeEngineTest, decoderUtf16le004, testing::ext::TestSize.Level0)
1110 {
1111 HILOG_INFO("decoderUtf16le004 start");
1112 napi_env env = (napi_env)engine_;
1113 int32_t flags = static_cast<int>(OHOS::Util::TextDecoder::ConverterFlags::FATAL_FLG) |
1114 static_cast<int>(OHOS::Util::TextDecoder::ConverterFlags::IGNORE_BOM_FLG);
1115 std::string str = "utf-16le";
1116 OHOS::Util::TextDecoder textDecoder(str, flags);
1117 bool iflag = false;
1118 size_t byteLength = 8;
1119 void* data = nullptr;
1120 napi_value resultBuff = nullptr;
1121 napi_create_arraybuffer(env, byteLength, &data, &resultBuff);
1122 unsigned char arr[8] = {0xFF, 0xFE, 0x61, 0x00, 0x62, 0x00, 0x63, 0x00};
1123 int ret = memcpy_s(data, sizeof(arr), reinterpret_cast<void*>(arr), sizeof(arr));
1124 ASSERT_EQ(0, ret);
1125 napi_value result2 = nullptr;
1126 napi_create_typedarray(env, napi_int8_array, byteLength, resultBuff, 0, &result2);
1127 napi_value testString = textDecoder.Decode(env, result2, iflag);
1128 size_t bufferSize = 0;
1129 napi_get_value_string_utf8(env, testString, nullptr, 0, &bufferSize);
1130 char* ch = nullptr;
1131 size_t length = 0;
1132 std::string tempStr01 = "";
1133 if (bufferSize > 0) {
1134 ch = new char[bufferSize + 1]();
1135 napi_get_value_string_utf8(env, testString, ch, bufferSize + 1, &length);
1136 tempStr01 = ch;
1137 }
1138 std::u16string tempU16str02 =
1139 std::wstring_convert<std::codecvt_utf8_utf16<char16_t>, char16_t> {}.from_bytes(tempStr01);
1140 ASSERT_EQ(0xFEFF, static_cast<int>(tempU16str02[0]));
1141 ASSERT_EQ(0x61, static_cast<int>(tempU16str02[1]));
1142 ASSERT_EQ(0x62, static_cast<int>(tempU16str02[2]));
1143 ASSERT_EQ(0x63, static_cast<int>(tempU16str02[3]));
1144 if (ch != nullptr) {
1145 delete []ch;
1146 ch = nullptr;
1147 }
1148 }
1149
1150 /**
1151 * @tc.name: decoderUtf16be001 utf-16be
1152 * @tc.desc: Test date type.
1153 * @tc.type: FUNC
1154 */
1155 HWTEST_F(NativeEngineTest, decoderUtf16be001, testing::ext::TestSize.Level0)
1156 {
1157 HILOG_INFO("decoderUtf16be001 start");
1158 napi_env env = (napi_env)engine_;
1159 int32_t flags = 0;
1160 std::string str = "utf-16be";
1161 OHOS::Util::TextDecoder textDecoder(str, flags);
1162 bool iflag = false;
1163 size_t byteLength = 6;
1164 void* data = nullptr;
1165 napi_value resultBuff = nullptr;
1166 napi_create_arraybuffer(env, byteLength, &data, &resultBuff);
1167 unsigned char arr[6] = {0x00, 0x61, 0x00, 0x62, 0x00, 0x63};
1168 int ret = memcpy_s(data, sizeof(arr), reinterpret_cast<void*>(arr), sizeof(arr));
1169 ASSERT_EQ(0, ret);
1170 napi_value result2 = nullptr;
1171 napi_create_typedarray(env, napi_int8_array, byteLength, resultBuff, 0, &result2);
1172 napi_value testString = textDecoder.Decode(env, result2, iflag);
1173 size_t bufferSize = 0;
1174 napi_get_value_string_utf8(env, testString, nullptr, 0, &bufferSize);
1175 size_t length = 0;
1176 char* ch = nullptr;
1177 if (bufferSize > 0) {
1178 ch = new char[bufferSize + 1]();
1179 napi_get_value_string_utf8(env, testString, ch, bufferSize + 1, &length);
1180 }
1181 ASSERT_STREQ("abc", ch);
1182 if (ch != nullptr) {
1183 delete []ch;
1184 ch = nullptr;
1185 }
1186 }
1187
1188 /**
1189 * @tc.name: decoderUtf16be002 utf-16be
1190 * @tc.desc: Test date type.
1191 * @tc.type: FUNC
1192 */
1193 HWTEST_F(NativeEngineTest, decoderUtf16be002, testing::ext::TestSize.Level0)
1194 {
1195 HILOG_INFO("decoderUtf16be002 start");
1196 napi_env env = (napi_env)engine_;
1197 int32_t flags = 0;
1198 std::string str = "utf-16be";
1199 OHOS::Util::TextDecoder textDecoder(str, flags);
1200 bool iflag = false;
1201 size_t byteLength = 8;
1202 void* data = nullptr;
1203 napi_value resultBuff = nullptr;
1204 napi_create_arraybuffer(env, byteLength, &data, &resultBuff);
1205 unsigned char arr[8] = {0xFE, 0xFF, 0x00, 0x61, 0x00, 0x62, 0x00, 0x63};
1206 int ret = memcpy_s(data, sizeof(arr), reinterpret_cast<void*>(arr), sizeof(arr));
1207 ASSERT_EQ(0, ret);
1208 napi_value result2 = nullptr;
1209 napi_create_typedarray(env, napi_int8_array, byteLength, resultBuff, 0, &result2);
1210 napi_value testString = textDecoder.Decode(env, result2, iflag);
1211 size_t bufferSize = 0;
1212 napi_get_value_string_utf8(env, testString, nullptr, 0, &bufferSize);
1213 size_t length = 0;
1214 char* ch = nullptr;
1215 std::string tempStr01 = "";
1216 if (bufferSize > 0) {
1217 ch = new char[bufferSize + 1]();
1218 napi_get_value_string_utf8(env, testString, ch, bufferSize + 1, &length);
1219 tempStr01 = ch;
1220 }
1221 std::u16string tempU16str02 =
1222 std::wstring_convert<std::codecvt_utf8_utf16<char16_t>, char16_t> {}.from_bytes(tempStr01);
1223 ASSERT_EQ(0xFEFF, static_cast<int>(tempU16str02[0]));
1224 ASSERT_EQ(0x61, static_cast<int>(tempU16str02[1]));
1225 ASSERT_EQ(0x62, static_cast<int>(tempU16str02[2]));
1226 ASSERT_EQ(0x63, static_cast<int>(tempU16str02[3]));
1227 if (ch != nullptr) {
1228 delete []ch;
1229 ch = nullptr;
1230 }
1231 }
1232
1233 /**
1234 * @tc.name: decoderUtf16be003 utf-16be
1235 * @tc.desc: Test date type.
1236 * @tc.type: FUNC
1237 */
1238 HWTEST_F(NativeEngineTest, decoderUtf16be003, testing::ext::TestSize.Level0)
1239 {
1240 HILOG_INFO("decoderUtf16be003 start");
1241 napi_env env = (napi_env)engine_;
1242 int32_t flags = static_cast<int>(OHOS::Util::TextDecoder::ConverterFlags::IGNORE_BOM_FLG);
1243 std::string str = "utf-16be";
1244 OHOS::Util::TextDecoder textDecoder(str, flags);
1245 bool iflag = true;
1246 size_t byteLength = 8;
1247 void* data = nullptr;
1248 napi_value resultBuff = nullptr;
1249 napi_create_arraybuffer(env, byteLength, &data, &resultBuff);
1250 unsigned char arr[8] = {0xFE, 0xFF, 0x00, 0x61, 0x00, 0x62, 0x00, 0x63};
1251 int ret = memcpy_s(data, sizeof(arr), reinterpret_cast<void*>(arr), sizeof(arr));
1252 ASSERT_EQ(0, ret);
1253 napi_value result2 = nullptr;
1254 napi_create_typedarray(env, napi_int8_array, byteLength, resultBuff, 0, &result2);
1255 napi_value testString = textDecoder.Decode(env, result2, iflag);
1256 size_t bufferSize = 0;
1257 napi_get_value_string_utf8(env, testString, nullptr, 0, &bufferSize);
1258 size_t length = 0;
1259 char* ch = nullptr;
1260 std::string tempStr01 = "";
1261 if (bufferSize > 0) {
1262 ch = new char[bufferSize + 1]();
1263 napi_get_value_string_utf8(env, testString, ch, bufferSize + 1, &length);
1264 tempStr01 = ch;
1265 }
1266 std::u16string tempU16str02 =
1267 std::wstring_convert<std::codecvt_utf8_utf16<char16_t>, char16_t> {}.from_bytes(tempStr01);
1268 ASSERT_EQ(0xFEFF, static_cast<int>(tempU16str02[0]));
1269 ASSERT_EQ(0x61, static_cast<int>(tempU16str02[1]));
1270 ASSERT_EQ(0x62, static_cast<int>(tempU16str02[2]));
1271 ASSERT_EQ(0x63, static_cast<int>(tempU16str02[3]));
1272 if (ch != nullptr) {
1273 delete []ch;
1274 ch = nullptr;
1275 }
1276 }
1277
1278 /**
1279 * @tc.name: decoderUtf8-BOM001
1280 * @tc.desc: Testing the decoding result of UTF-8 data with BOM.
1281 * @tc.type: FUNC
1282 */
1283 HWTEST_F(NativeEngineTest, decoderUtf8BOM001, testing::ext::TestSize.Level0)
1284 {
1285 HILOG_INFO("decoderUtf8BOM001 start");
1286 napi_env env = (napi_env)engine_;
1287 int32_t flags = static_cast<int>(OHOS::Util::TextDecoder::ConverterFlags::IGNORE_BOM_FLG);
1288 std::string encoding = "utf-8";
1289 OHOS::Util::TextDecoder textDecoder(encoding, flags);
1290 bool iflag = true;
1291 size_t byteLength = 6;
1292 void* data = nullptr;
1293 napi_value resultBuff = nullptr;
1294 napi_create_arraybuffer(env, byteLength, &data, &resultBuff);
1295 unsigned char arr[8] = {0xEF, 0xBB, 0xBF, 0x41, 0x42, 0x43};
1296 int ret = memcpy_s(data, sizeof(arr), reinterpret_cast<void*>(arr), sizeof(arr));
1297 ASSERT_EQ(0, ret);
1298 napi_value result = nullptr;
1299 napi_create_typedarray(env, napi_int8_array, byteLength, resultBuff, 0, &result);
1300 napi_value testString = textDecoder.DecodeToString(env, result, iflag);
1301 size_t bufferSize = 0;
1302 napi_get_value_string_utf16(env, testString, nullptr, 0, &bufferSize);
1303 size_t length = 0;
1304 char16_t* ch = nullptr;
1305 if (bufferSize > 0) {
1306 ch = new char16_t[bufferSize + 1]();
1307 napi_get_value_string_utf16(env, testString, ch, bufferSize + 1, &length);
1308 }
1309 std::string str =
1310 std::wstring_convert<std::codecvt_utf8_utf16<char16_t>, char16_t> {}.to_bytes(ch);
1311 ASSERT_EQ(str, "ABC");
1312 if (ch != nullptr) {
1313 delete []ch;
1314 ch = nullptr;
1315 }
1316 }
1317
1318 /**
1319 * @tc.name: decoderUtf8-BOM002
1320 * @tc.desc: Testing the decoding result of UTF-8 data with BOM.
1321 * @tc.type: FUNC
1322 */
1323 HWTEST_F(NativeEngineTest, decoderUtf8BOM002, testing::ext::TestSize.Level0)
1324 {
1325 HILOG_INFO("decoderUtf8BOM002 start");
1326 napi_env env = (napi_env)engine_;
1327 int32_t flags = 0;
1328 std::string encoding = "utf-8";
1329 OHOS::Util::TextDecoder textDecoder(encoding, flags);
1330 bool iflag = true;
1331 size_t byteLength = 6;
1332 void* data = nullptr;
1333 napi_value resultBuff = nullptr;
1334 napi_create_arraybuffer(env, byteLength, &data, &resultBuff);
1335 unsigned char arr[8] = {0xEF, 0xBB, 0xBF, 0x41, 0x42, 0x43};
1336 int ret = memcpy_s(data, sizeof(arr), reinterpret_cast<void*>(arr), sizeof(arr));
1337 ASSERT_EQ(0, ret);
1338 napi_value result = nullptr;
1339 napi_create_typedarray(env, napi_int8_array, byteLength, resultBuff, 0, &result);
1340 napi_value testString = textDecoder.DecodeToString(env, result, iflag);
1341 size_t bufferSize = 0;
1342 napi_get_value_string_utf16(env, testString, nullptr, 0, &bufferSize);
1343 size_t length = 0;
1344 char16_t* ch = nullptr;
1345 if (bufferSize > 0) {
1346 ch = new char16_t[bufferSize + 1]();
1347 napi_get_value_string_utf16(env, testString, ch, bufferSize + 1, &length);
1348 }
1349 std::string str =
1350 std::wstring_convert<std::codecvt_utf8_utf16<char16_t>, char16_t> {}.to_bytes(ch);
1351 ASSERT_EQ(str, "\uFEFFABC");
1352 if (ch != nullptr) {
1353 delete []ch;
1354 ch = nullptr;
1355 }
1356 }
1357
1358 /**
1359 * @tc.name: decoderUtf8-BOM003
1360 * @tc.desc: Decoder utf8 BOM with limit err.
1361 * @tc.type: FUNC
1362 */
1363 HWTEST_F(NativeEngineTest, decoderUtf8BOM003, testing::ext::TestSize.Level0)
1364 {
1365 HILOG_INFO("decoderUtf8BOM003 start");
1366 napi_env env = (napi_env)engine_;
1367 int32_t flags = static_cast<int>(OHOS::Util::TextDecoder::ConverterFlags::FATAL_FLG) |
1368 static_cast<int>(OHOS::Util::TextDecoder::ConverterFlags::IGNORE_BOM_FLG);
1369 std::string str = "utf-8";
1370 OHOS::Util::TextDecoder textDecoder(str, flags);
1371 bool iflag = false;
1372 size_t byteLength = 0;
1373 void *data = nullptr;
1374 napi_value resultBuff = nullptr;
1375 napi_create_arraybuffer(env, byteLength, &data, &resultBuff);
1376 napi_value result2 = nullptr;
1377 napi_create_typedarray(env, napi_int8_array, byteLength, resultBuff, 0, &result2);
1378 napi_value testString = textDecoder.DecodeToString(env, result2, iflag);
1379 ASSERT_TRUE(testString == nullptr);
1380 }
1381
1382 /**
1383 * @tc.name: decoderUtf8-BOM004
1384 * @tc.desc: Testing the decoding result of UTF-8 data with BOM.
1385 * @tc.type: FUNC
1386 */
1387 HWTEST_F(NativeEngineTest, decoderUtf8BOM004, testing::ext::TestSize.Level0)
1388 {
1389 HILOG_INFO("decoderUtf8BOM004 start");
1390 napi_env env = (napi_env)engine_;
1391 int32_t flags = static_cast<int>(OHOS::Util::TextDecoder::ConverterFlags::IGNORE_BOM_FLG);
1392 std::string encoding = "utf-8";
1393 OHOS::Util::TextDecoder textDecoder(encoding, flags);
1394 bool iflag = false;
1395 size_t byteLength = 6;
1396 void* data = nullptr;
1397 napi_value resultBuff = nullptr;
1398 napi_create_arraybuffer(env, byteLength, &data, &resultBuff);
1399 unsigned char arr[8] = {0xEF, 0xBB, 0xBF, 0x41, 0x42, 0x43};
1400 int ret = memcpy_s(data, sizeof(arr), reinterpret_cast<void*>(arr), sizeof(arr));
1401 ASSERT_EQ(0, ret);
1402 napi_value result = nullptr;
1403 napi_create_typedarray(env, napi_int8_array, byteLength, resultBuff, 0, &result);
1404 napi_value testString = textDecoder.DecodeToString(env, result, iflag);
1405 size_t bufferSize = 0;
1406 napi_get_value_string_utf16(env, testString, nullptr, 0, &bufferSize);
1407 size_t length = 0;
1408 char16_t* ch = nullptr;
1409 if (bufferSize > 0) {
1410 ch = new char16_t[bufferSize + 1]();
1411 napi_get_value_string_utf16(env, testString, ch, bufferSize + 1, &length);
1412 }
1413 std::string str =
1414 std::wstring_convert<std::codecvt_utf8_utf16<char16_t>, char16_t> {}.to_bytes(ch);
1415 ASSERT_EQ(str, "ABC");
1416 if (ch != nullptr) {
1417 delete []ch;
1418 ch = nullptr;
1419 }
1420 }
1421
1422 /**
1423 * @tc.name: decoderUtf8-BOM005
1424 * @tc.desc: Testing the decoding result of UTF-8 data with BOM.
1425 * @tc.type: FUNC
1426 */
1427 HWTEST_F(NativeEngineTest, decoderUtf8BOM005, testing::ext::TestSize.Level0)
1428 {
1429 HILOG_INFO("decoderUtf8BOM005 start");
1430 napi_env env = (napi_env)engine_;
1431 int32_t flags = 0;
1432 std::string encoding = "utf-8";
1433 OHOS::Util::TextDecoder textDecoder(encoding, flags);
1434 bool iflag = true;
1435 size_t byteLength = 11;
1436 void* data = nullptr;
1437 napi_value resultBuff = nullptr;
1438 napi_create_arraybuffer(env, byteLength, &data, &resultBuff);
1439 unsigned char arr[11] = {0xEF, 0xBB, 0xBF, 0x41, 0x42, 0x43, 0x31, 0x32, 0x33, 0x34, 0x35};
1440 int ret = memcpy_s(data, sizeof(arr), reinterpret_cast<void*>(arr), sizeof(arr));
1441 ASSERT_EQ(0, ret);
1442 napi_value result = nullptr;
1443 napi_create_typedarray(env, napi_int8_array, byteLength, resultBuff, 0, &result);
1444 napi_value testString = textDecoder.DecodeToString(env, result, iflag);
1445 size_t bufferSize = 0;
1446 napi_get_value_string_utf16(env, testString, nullptr, 0, &bufferSize);
1447 size_t length = 0;
1448 char16_t* ch = nullptr;
1449 if (bufferSize > 0) {
1450 ch = new char16_t[bufferSize + 1]();
1451 napi_get_value_string_utf16(env, testString, ch, bufferSize + 1, &length);
1452 }
1453 std::string str =
1454 std::wstring_convert<std::codecvt_utf8_utf16<char16_t>, char16_t> {}.to_bytes(ch);
1455 ASSERT_EQ(str, "\uFEFFABC12345");
1456 if (ch != nullptr) {
1457 delete[] ch;
1458 ch = nullptr;
1459 }
1460 }
1461
1462 /**
1463 * @tc.name: decoderUtf8-BOM006
1464 * @tc.desc: Testing the decoding result of UTF-8 data with BOM.
1465 * @tc.type: FUNC
1466 */
1467 HWTEST_F(NativeEngineTest, decoderUtf8BOM006, testing::ext::TestSize.Level0)
1468 {
1469 HILOG_INFO("decoderUtf8BOM006 start");
1470 napi_env env = (napi_env)engine_;
1471 int32_t flags = 0;
1472 std::string encoding = "utf-8";
1473 OHOS::Util::TextDecoder textDecoder(encoding, flags);
1474 bool iflag = true;
1475 size_t byteLength = 12;
1476 void* data = nullptr;
1477 napi_value resultBuff = nullptr;
1478 napi_create_arraybuffer(env, byteLength, &data, &resultBuff);
1479 unsigned char arr[12] = {0xEF, 0xBB, 0xBF, 0x41, 0x42, 0x43, 0x61, 0x62, 0x63, 0x31, 0x32, 0x33};
1480 int ret = memcpy_s(data, sizeof(arr), reinterpret_cast<void*>(arr), sizeof(arr));
1481 ASSERT_EQ(0, ret);
1482 napi_value result = nullptr;
1483 napi_create_typedarray(env, napi_int8_array, byteLength, resultBuff, 0, &result);
1484 napi_value testString = textDecoder.DecodeToString(env, result, iflag);
1485 size_t bufferSize = 0;
1486 napi_get_value_string_utf16(env, testString, nullptr, 0, &bufferSize);
1487 size_t length = 0;
1488 char16_t* ch = nullptr;
1489 if (bufferSize > 0) {
1490 ch = new char16_t[bufferSize + 1]();
1491 napi_get_value_string_utf16(env, testString, ch, bufferSize + 1, &length);
1492 }
1493 std::string str =
1494 std::wstring_convert<std::codecvt_utf8_utf16<char16_t>, char16_t> {}.to_bytes(ch);
1495 ASSERT_EQ(str, "\uFEFFABCabc123");
1496 if (ch != nullptr) {
1497 delete[] ch;
1498 ch = nullptr;
1499 }
1500 }
1501
1502 /**
1503 * @tc.name: decoderUtf8-BOM007
1504 * @tc.desc: Testing the decoding result of UTF-8 data with BOM.
1505 * @tc.type: FUNC
1506 */
1507 HWTEST_F(NativeEngineTest, decoderUtf8BOM007, testing::ext::TestSize.Level0)
1508 {
1509 HILOG_INFO("decoderUtf8BOM007 start");
1510 napi_env env = (napi_env)engine_;
1511 int32_t flags = 0;
1512 std::string encoding = "utf-8";
1513 OHOS::Util::TextDecoder textDecoder(encoding, flags);
1514 bool iflag = true;
1515 size_t byteLength = 13;
1516 void* data = nullptr;
1517 napi_value resultBuff = nullptr;
1518 napi_create_arraybuffer(env, byteLength, &data, &resultBuff);
1519 unsigned char arr[13] = {0xEF, 0xBB, 0xBF, 0x41, 0x42, 0x43, 0x61, 0x62, 0x63, 0x31, 0x32, 0x33, 0x33};
1520 int ret = memcpy_s(data, sizeof(arr), reinterpret_cast<void*>(arr), sizeof(arr));
1521 ASSERT_EQ(0, ret);
1522 napi_value result = nullptr;
1523 napi_create_typedarray(env, napi_int8_array, byteLength, resultBuff, 0, &result);
1524 napi_value testString = textDecoder.DecodeToString(env, result, iflag);
1525 size_t bufferSize = 0;
1526 napi_get_value_string_utf16(env, testString, nullptr, 0, &bufferSize);
1527 size_t length = 0;
1528 char16_t* ch = nullptr;
1529 if (bufferSize > 0) {
1530 ch = new char16_t[bufferSize + 1]();
1531 napi_get_value_string_utf16(env, testString, ch, bufferSize + 1, &length);
1532 }
1533 std::string str =
1534 std::wstring_convert<std::codecvt_utf8_utf16<char16_t>, char16_t> {}.to_bytes(ch);
1535 ASSERT_EQ(str, "\uFEFFABCabc1233");
1536 if (ch != nullptr) {
1537 delete[] ch;
1538 ch = nullptr;
1539 }
1540 }
1541
1542 /**
1543 * @tc.name: decoderUtf8-BOM008
1544 * @tc.desc: Testing the decoding result of UTF-8 data with BOM.
1545 * @tc.type: FUNC
1546 */
1547 HWTEST_F(NativeEngineTest, decoderUtf8BOM008, testing::ext::TestSize.Level0)
1548 {
1549 HILOG_INFO("decoderUtf8BOM008 start");
1550 napi_env env = (napi_env)engine_;
1551 int32_t flags = static_cast<int>(OHOS::Util::TextDecoder::ConverterFlags::IGNORE_BOM_FLG);
1552 std::string encoding = "utf-8";
1553 OHOS::Util::TextDecoder textDecoder(encoding, flags);
1554 bool iflag = false;
1555 size_t byteLength = 200;
1556 void* data = nullptr;
1557 napi_value resultBuff = nullptr;
1558 napi_create_arraybuffer(env, byteLength, &data, &resultBuff);
1559 unsigned char arr[200] = {0xEF, 0xBB, 0xBF};
1560 for (size_t i = 3; i < 200; ++i) {
1561 arr[i] = static_cast<char>('A' + (i - 3) % 26);
1562 }
1563 int ret = memcpy_s(data, sizeof(arr), reinterpret_cast<void*>(arr), sizeof(arr));
1564 ASSERT_EQ(0, ret);
1565 napi_value result = nullptr;
1566 napi_create_typedarray(env, napi_int8_array, byteLength, resultBuff, 0, &result);
1567 napi_value testString = textDecoder.DecodeToString(env, result, iflag);
1568 size_t bufferSize = 0;
1569 napi_get_value_string_utf16(env, testString, nullptr, 0, &bufferSize);
1570 size_t length = 0;
1571 char16_t* ch = nullptr;
1572 if (bufferSize > 0) {
1573 ch = new char16_t[bufferSize + 1]();
1574 napi_get_value_string_utf16(env, testString, ch, bufferSize + 1, &length);
1575 }
1576 std::string str =
1577 std::wstring_convert<std::codecvt_utf8_utf16<char16_t>, char16_t> {}.to_bytes(ch);
1578 std::string expectedStr;
1579 for (size_t i = 3; i < 200; ++i) {
1580 expectedStr += static_cast<char>('A' + (i - 3) % 26);
1581 }
1582 ASSERT_EQ(str, expectedStr);
1583 if (ch != nullptr) {
1584 delete []ch;
1585 ch = nullptr;
1586 }
1587 }
1588
1589 /**
1590 * @tc.name: decoderUtf8-BOM009
1591 * @tc.desc: Testing the decoding result of UTF-8 data with BOM.
1592 * @tc.type: FUNC
1593 */
1594 HWTEST_F(NativeEngineTest, decoderUtf8BOM009, testing::ext::TestSize.Level0)
1595 {
1596 HILOG_INFO("decoderUtf8BOM009 start");
1597 napi_env env = (napi_env)engine_;
1598 int32_t flags = static_cast<int>(OHOS::Util::TextDecoder::ConverterFlags::IGNORE_BOM_FLG);
1599 std::string encoding = "utf-16";
1600 OHOS::Util::TextDecoder textDecoder(encoding, flags);
1601 bool iflag = true;
1602 size_t byteLength = 6;
1603 void* data = nullptr;
1604 napi_value resultBuff = nullptr;
1605 napi_create_arraybuffer(env, byteLength, &data, &resultBuff);
1606 unsigned char arr[8] = {0xEF, 0xBB, 0xBF, 0x41, 0x42, 0x43};
1607 int ret = memcpy_s(data, sizeof(arr), reinterpret_cast<void*>(arr), sizeof(arr));
1608 ASSERT_EQ(0, ret);
1609 napi_value result = nullptr;
1610 napi_create_typedarray(env, napi_int8_array, byteLength, resultBuff, 0, &result);
1611 napi_value testString = textDecoder.DecodeToString(env, result, iflag);
1612 size_t bufferSize = 0;
1613 napi_get_value_string_utf16(env, testString, nullptr, 0, &bufferSize);
1614 size_t length = 0;
1615 char16_t* ch = nullptr;
1616 if (bufferSize > 0) {
1617 ch = new char16_t[bufferSize + 1]();
1618 napi_get_value_string_utf16(env, testString, ch, bufferSize + 1, &length);
1619 }
1620 std::string str =
1621 std::wstring_convert<std::codecvt_utf8_utf16<char16_t>, char16_t> {}.to_bytes(ch);
1622 ASSERT_TRUE(str != "");
1623 if (ch != nullptr) {
1624 delete []ch;
1625 ch = nullptr;
1626 }
1627 }
1628
1629 /**
1630 * @tc.name: decoderUtf8-BOM010
1631 * @tc.desc: Testing the decoding result of UTF-8 data with BOM.
1632 * @tc.type: FUNC
1633 */
1634 HWTEST_F(NativeEngineTest, decoderUtf8BOM010, testing::ext::TestSize.Level0)
1635 {
1636 HILOG_INFO("decoderUtf8-BOM010 start");
1637 napi_env env = (napi_env)engine_;
1638 int32_t flags = 0;
1639 std::string encoding = "utf-8";
1640 OHOS::Util::TextDecoder textDecoder(encoding, flags);
1641 bool iflag = true;
1642 size_t byteLength = 4;
1643 void* data = nullptr;
1644 bool flag = false;
1645 size_t strLength = 2;
1646 void* strData = nullptr;
1647 napi_value resultBuff = nullptr;
1648 napi_value strBuffer = nullptr;
1649 napi_create_arraybuffer(env, byteLength, &data, &resultBuff);
1650 unsigned char arr[4] = {0xE4, 0xBD, 0xA0, 0xE5};
1651 int ret = memcpy_s(data, sizeof(arr), reinterpret_cast<void*>(arr), sizeof(arr));
1652 ASSERT_EQ(0, ret);
1653 napi_value result = nullptr;
1654 napi_create_typedarray(env, napi_int8_array, byteLength, resultBuff, 0, &result);
1655 napi_value testString = textDecoder.DecodeToString(env, result, iflag);
1656 size_t bufferSize = 0;
1657 napi_get_value_string_utf16(env, testString, nullptr, 0, &bufferSize);
1658 size_t length = 0;
1659 char16_t* ch = nullptr;
1660 if (bufferSize > 0) {
1661 ch = new char16_t[bufferSize + 1]();
1662 napi_get_value_string_utf16(env, testString, ch, bufferSize + 1, &length);
1663 }
1664 std::string str =
1665 std::wstring_convert<std::codecvt_utf8_utf16<char16_t>, char16_t> {}.to_bytes(ch);
1666 ASSERT_EQ(str, "你");
1667 if (ch != nullptr) {
1668 delete []ch;
1669 ch = nullptr;
1670 }
1671 napi_create_arraybuffer(env, strLength, &strData, &strBuffer);
1672 unsigned char arrayBuffer[2] = {0xA5, 0xBD};
1673 int strRet = memcpy_s(strData, sizeof(arrayBuffer), reinterpret_cast<void*>(arrayBuffer), sizeof(arrayBuffer));
1674 ASSERT_EQ(0, strRet);
1675 napi_value strResult = nullptr;
1676 napi_create_typedarray(env, napi_int8_array, strLength, strBuffer, 0, &strResult);
1677 napi_value strString = textDecoder.DecodeToString(env, strResult, flag);
1678 size_t strSize = 0;
1679 napi_get_value_string_utf16(env, strString, nullptr, 0, &strSize);
1680 size_t resultLength = 0;
1681 char16_t* temp = nullptr;
1682 if (strSize > 0) {
1683 temp = new char16_t[strSize + 1]();
1684 napi_get_value_string_utf16(env, strString, temp, strSize + 1, &resultLength);
1685 }
1686 std::string stringResult =
1687 std::wstring_convert<std::codecvt_utf8_utf16<char16_t>, char16_t> {}.to_bytes(temp);
1688 ASSERT_EQ(stringResult, "好");
1689 if (temp != nullptr) {
1690 delete []temp;
1691 temp = nullptr;
1692 }
1693 }
1694
1695 /**
1696 * @tc.name: getMinByteSizeTest001 utf-8
1697 * @tc.desc: get minbyte size with tranTool nullptr.
1698 * @tc.type: FUNC
1699 */
1700 HWTEST_F(NativeEngineTest, getMinByteSizeTest001, testing::ext::TestSize.Level0)
1701 {
1702 int32_t flags = static_cast<int>(OHOS::Util::TextDecoder::ConverterFlags::FATAL_FLG) |
1703 static_cast<int>(OHOS::Util::TextDecoder::ConverterFlags::IGNORE_BOM_FLG);
1704 std::string str = "XYZ123";
1705 OHOS::Util::TextDecoder textDecoder(str, flags);
1706 textDecoder.Reset();
1707 size_t rel1 = textDecoder.GetMinByteSize();
1708 ASSERT_EQ(rel1, 0);
1709 }
1710
1711 /* @tc.name: encodeTest001
1712 * @tc.desc: Encodes all bytes in the specified u8 array into
1713 the newly allocated u8 array using the Base64 encoding scheme.
1714 * @tc.type: FUNC
1715 */
1716 HWTEST_F(NativeEngineTest, encodeTest001, testing::ext::TestSize.Level0)
1717 {
1718 HILOG_INFO("encodeTest001 start");
1719 napi_env env = (napi_env)engine_;
1720 OHOS::Util::Base64 base64;
1721 unsigned char input[3] = {0x73, 0x31, 0x33};
1722 napi_value arrayBuffer = nullptr;
1723 void* data = nullptr;
1724 size_t arrayBufferSize = 3;
1725 napi_create_arraybuffer(env, arrayBufferSize, &data, &arrayBuffer);
1726 int ret = memcpy_s(data, sizeof(input), reinterpret_cast<void*>(input), sizeof(input));
1727 ASSERT_EQ(0, ret);
1728 napi_value src = nullptr;
1729 napi_create_typedarray(env, napi_uint8_array, arrayBufferSize, arrayBuffer, 0, &src);
1730
1731 napi_value result = base64.EncodeSync(env, src, OHOS::Util::Type::BASIC);
1732 char excepted[4] = {0x63, 0x7A, 0x45, 0x7A};
1733 napi_typedarray_type type;
1734 size_t srcLength = 0;
1735 void* srcData = nullptr;
1736 napi_value srcBuffer = nullptr;
1737 size_t byteOffset = 0;
1738 napi_get_typedarray_info(env, result, &type, &srcLength, &srcData, &srcBuffer, &byteOffset);
1739 char* res = (char*)srcData;
1740 ASSERT_EQ(res[0], excepted[0]);
1741 ASSERT_EQ(res[1], excepted[1]);
1742 ASSERT_EQ(res[2], excepted[2]);
1743 ASSERT_EQ(res[3], excepted[3]);
1744 }
1745
1746 /* @tc.name: encodeTest002
1747 * @tc.desc: Encodes all bytes in the specified u8 array
1748 into the newly allocated u8 array using the Base64 encoding scheme.
1749 * @tc.type: FUNC
1750 */
1751 HWTEST_F(NativeEngineTest, encodeTest002, testing::ext::TestSize.Level0)
1752 {
1753 HILOG_INFO("encodeTest002 start");
1754 napi_env env = (napi_env)engine_;
1755 OHOS::Util::Base64 base64;
1756 unsigned char input[14] = {66, 97, 115, 101, 54, 52, 32, 78, 111, 100, 101, 46, 106, 115};
1757 napi_value arrayBuffer = nullptr;
1758 void* data = nullptr;
1759 size_t arrayBufferSize = 14;
1760 napi_create_arraybuffer(env, arrayBufferSize, &data, &arrayBuffer);
1761 int ret = memcpy_s(data, sizeof(input), reinterpret_cast<void*>(input), sizeof(input));
1762 ASSERT_EQ(0, ret);
1763 napi_value src = nullptr;
1764 napi_create_typedarray(env, napi_uint8_array, arrayBufferSize, arrayBuffer, 0, &src);
1765
1766 napi_value result = base64.EncodeSync(env, src, OHOS::Util::Type::BASIC);
1767 char excepted[20] = {81, 109, 70, 122, 90, 84, 89, 48, 73, 69, 53, 118, 90, 71, 85, 117, 97, 110, 77, 61};
1768 napi_typedarray_type type;
1769 size_t srcLength = 0;
1770 void* srcData = nullptr;
1771 napi_value srcBuffer = nullptr;
1772 size_t byteOffset = 0;
1773 napi_get_typedarray_info(env, result, &type, &srcLength, &srcData, &srcBuffer, &byteOffset);
1774 char* res = (char*)srcData;
1775 for (size_t i = 0; i < 20; i++) {
1776 ASSERT_EQ(res[i], excepted[i]);
1777 }
1778 }
1779
1780 /* @tc.name: encodeTest003
1781 * @tc.desc: Encodes all bytes in the specified u8 array
1782 into the newly allocated u8 array using the Base64 encoding scheme.
1783 * @tc.type: FUNC
1784 */
1785 HWTEST_F(NativeEngineTest, encodeTest003, testing::ext::TestSize.Level0)
1786 {
1787 HILOG_INFO("encodeTest003 start");
1788 napi_env env = (napi_env)engine_;
1789 OHOS::Util::Base64 base64;
1790 unsigned char input[26] = {66, 97, 115, 101, 54, 52, 32, 69, 110,
1791 99, 111, 100, 105, 110, 103, 32, 105, 110, 32, 78, 111, 100, 101, 46, 106, 115};
1792 napi_value arrayBuffer = nullptr;
1793 void* data = nullptr;
1794 size_t arrayBufferSize = 26;
1795 napi_create_arraybuffer(env, arrayBufferSize, &data, &arrayBuffer);
1796 int ret = memcpy_s(data, sizeof(input), reinterpret_cast<void*>(input), sizeof(input));
1797 ASSERT_EQ(0, ret);
1798 napi_value src = nullptr;
1799 napi_create_typedarray(env, napi_uint8_array, arrayBufferSize, arrayBuffer, 0, &src);
1800
1801 napi_value result = base64.EncodeSync(env, src, OHOS::Util::Type::BASIC);
1802 char excepted[36] = {81, 109, 70, 122, 90, 84, 89, 48, 73, 69, 86, 117, 89, 50, 57, 107, 97, 87, 53,
1803 110, 73, 71, 108, 117, 73, 69, 53, 118, 90, 71, 85, 117, 97, 110, 77, 61};
1804 napi_typedarray_type type;
1805 size_t srcLength = 0;
1806 void* srcData = nullptr;
1807 napi_value srcBuffer = nullptr;
1808 size_t byteOffset = 0;
1809 napi_get_typedarray_info(env, result, &type, &srcLength, &srcData, &srcBuffer, &byteOffset);
1810 char* res = (char*)srcData;
1811 for (size_t i = 0; i < 36; i++) {
1812 ASSERT_EQ(res[i], excepted[i]);
1813 }
1814 }
1815
1816 /* @tc.name: encodeTest004
1817 * @tc.desc: Encodes all bytes in the specified u8 array into the
1818 newly allocated u8 array using the Base64 encoding scheme.
1819 * @tc.type: FUNC
1820 */
1821 HWTEST_F(NativeEngineTest, encodeTest004, testing::ext::TestSize.Level0)
1822 {
1823 HILOG_INFO("encodeTest004 start");
1824 napi_env env = (napi_env)engine_;
1825 OHOS::Util::Base64 base64;
1826 unsigned char input[4] = {168, 174, 155, 255};
1827 napi_value arrayBuffer = nullptr;
1828 void* data = nullptr;
1829 size_t arrayBufferSize = 4;
1830 napi_create_arraybuffer(env, arrayBufferSize, &data, &arrayBuffer);
1831 int ret = memcpy_s(data, sizeof(input), reinterpret_cast<void*>(input), sizeof(input));
1832 ASSERT_EQ(0, ret);
1833 napi_value src = nullptr;
1834 napi_create_typedarray(env, napi_uint8_array, arrayBufferSize, arrayBuffer, 0, &src);
1835
1836 napi_value result = base64.EncodeSync(env, src, OHOS::Util::Type::BASIC);
1837 char excepted[8] = {113, 75, 54, 98, 47, 119, 61, 61};
1838 napi_typedarray_type type;
1839 size_t srcLength = 0;
1840 void* srcData = nullptr;
1841 napi_value srcBuffer = nullptr;
1842 size_t byteOffset = 0;
1843 napi_get_typedarray_info(env, result, &type, &srcLength, &srcData, &srcBuffer, &byteOffset);
1844 char* res = (char*)srcData;
1845 for (size_t i = 0; i < 8; i++) {
1846 ASSERT_EQ(res[i], excepted[i]);
1847 }
1848 }
1849
1850 /* @tc.name: encodeTest005
1851 * @tc.desc: Encodes all bytes in the specified u8 array
1852 into the newly allocated u8 array using the Base64 encoding scheme.
1853 * @tc.type: FUNC
1854 */
1855 HWTEST_F(NativeEngineTest, encodeTest005, testing::ext::TestSize.Level0)
1856 {
1857 HILOG_INFO("encodeTest005 start");
1858 napi_env env = (napi_env)engine_;
1859 OHOS::Util::Base64 base64;
1860 unsigned char input[6] = {66, 97, 115, 101, 54, 52};
1861 napi_value arrayBuffer = nullptr;
1862 void* data = nullptr;
1863 size_t arrayBufferSize = 6;
1864 napi_create_arraybuffer(env, arrayBufferSize, &data, &arrayBuffer);
1865 int ret = memcpy_s(data, sizeof(input), reinterpret_cast<void*>(input), sizeof(input));
1866 ASSERT_EQ(0, ret);
1867 napi_value src = nullptr;
1868 napi_create_typedarray(env, napi_uint8_array, arrayBufferSize, arrayBuffer, 0, &src);
1869
1870 napi_value result = base64.EncodeSync(env, src, OHOS::Util::Type::BASIC);
1871 char excepted[8] = {81, 109, 70, 122, 90, 84, 89, 48};
1872 napi_typedarray_type type;
1873 size_t srcLength = 0;
1874 void* srcData = nullptr;
1875 napi_value srcBuffer = nullptr;
1876 size_t byteOffset = 0;
1877 napi_get_typedarray_info(env, result, &type, &srcLength, &srcData, &srcBuffer, &byteOffset);
1878 char* res = (char*)srcData;
1879 for (size_t i = 0; i < 8; i++) {
1880 ASSERT_EQ(res[i], excepted[i]);
1881 }
1882 }
1883
1884 /* @tc.name: encodeTest006
1885 * @tc.desc: Encode sync with napi_uint16_array.
1886 * @tc.type: FUNC
1887 */
1888 HWTEST_F(NativeEngineTest, encodeTest006, testing::ext::TestSize.Level0)
1889 {
1890 HILOG_INFO("encodeTest006 start");
1891 napi_env env = (napi_env)engine_;
1892 OHOS::Util::Base64 base64;
1893 unsigned char input[6] = {66, 97, 115, 101, 54, 51};
1894 napi_value arrayBuffer = nullptr;
1895 void* data = nullptr;
1896 size_t arrayBufferSize = 6;
1897 napi_create_arraybuffer(env, arrayBufferSize, &data, &arrayBuffer);
1898 int ret = memcpy_s(data, sizeof(input), reinterpret_cast<void*>(input), sizeof(input));
1899 ASSERT_EQ(0, ret);
1900 napi_value src = nullptr;
1901 napi_create_typedarray(env, napi_uint16_array, arrayBufferSize, arrayBuffer, 0, &src);
1902
1903 napi_value result = base64.EncodeSync(env, src, OHOS::Util::Type::BASIC);
1904 ASSERT_EQ(nullptr, result);
1905 napi_value result1 = base64.EncodeToStringSync(env, src, OHOS::Util::Type::BASIC);
1906 ASSERT_EQ(nullptr, result1);
1907 napi_value exception;
1908 napi_get_and_clear_last_exception(env, &exception);
1909 }
1910
1911 /* @tc.name: encodeTest007
1912 * @tc.desc: Encodes all bytes in the specified u8 array with type BASIC_URL_SAFE.
1913 * @tc.type: FUNC
1914 */
1915 HWTEST_F(NativeEngineTest, encodeTest007, testing::ext::TestSize.Level0)
1916 {
1917 HILOG_INFO("encodeTest007 start");
1918 napi_env env = (napi_env)engine_;
1919 OHOS::Util::Base64 base64;
1920 unsigned char input[4] = {168, 174, 155, 255};
1921 napi_value arrayBuffer = nullptr;
1922 void* data = nullptr;
1923 size_t arrayBufferSize = 4;
1924 napi_create_arraybuffer(env, arrayBufferSize, &data, &arrayBuffer);
1925 int ret = memcpy_s(data, sizeof(input), reinterpret_cast<void*>(input), sizeof(input));
1926 ASSERT_EQ(0, ret);
1927 napi_value src = nullptr;
1928 napi_create_typedarray(env, napi_uint8_array, arrayBufferSize, arrayBuffer, 0, &src);
1929
1930 napi_value result = base64.EncodeSync(env, src, OHOS::Util::Type::BASIC_URL_SAFE);
1931 char excepted[7] = {113, 75, 54, 98, 95, 119};
1932 napi_typedarray_type type;
1933 size_t srcLength = 0;
1934 void* srcData = nullptr;
1935 napi_value srcBuffer = nullptr;
1936 size_t byteOffset = 0;
1937 napi_get_typedarray_info(env, result, &type, &srcLength, &srcData, &srcBuffer, &byteOffset);
1938 char* res = (char*)srcData;
1939 for (size_t i = 0; i < 6; i++) {
1940 ASSERT_EQ(res[i], excepted[i]);
1941 }
1942 }
1943
1944 /* @tc.name: encodeToStringTest001
1945 * @tc.desc: Encodes the specified byte array as a String using the Base64 encoding scheme.
1946 * @tc.type: FUNC
1947 */
1948 HWTEST_F(NativeEngineTest, encodeToStringTest001, testing::ext::TestSize.Level0)
1949 {
1950 HILOG_INFO("encodeToStringTest001 start");
1951 napi_env env = (napi_env)engine_;
1952 OHOS::Util::Base64 base64;
1953
1954 unsigned char input[3] = {115, 49, 51};
1955 napi_value arrayBuffer = nullptr;
1956 size_t arrayBufferSize = 3;
1957 void* data = nullptr;
1958 napi_create_arraybuffer(env, arrayBufferSize, &data, &arrayBuffer);
1959 int ret = memcpy_s(data, sizeof(input), reinterpret_cast<void*>(input), sizeof(input));
1960 ASSERT_EQ(0, ret);
1961 napi_value src = nullptr;
1962 napi_create_typedarray(env, napi_uint8_array, arrayBufferSize, arrayBuffer, 0, &src);
1963 napi_value result = base64.EncodeToStringSync(env, src, OHOS::Util::Type::BASIC);
1964 size_t prolen = 0;
1965 char* inputString = nullptr;
1966 napi_get_value_string_utf8(env, result, nullptr, 0, &prolen);
1967 if (prolen > 0) {
1968 inputString = new char[prolen + 1];
1969 if (memset_s(inputString, prolen + 1, '\0', prolen + 1) != 0) {
1970 napi_throw_error(env, "-1", "decode inputString memset_s failed");
1971 }
1972 } else {
1973 napi_throw_error(env, "-2", "prolen is error !");
1974 }
1975 napi_get_value_string_utf8(env, result, inputString, prolen + 1, &prolen);
1976 ASSERT_STREQ("czEz", inputString);
1977 if (inputString != nullptr) {
1978 delete []inputString;
1979 inputString = nullptr;
1980 }
1981 }
1982
1983 /* @tc.name: encodeToStringTest002
1984 * @tc.desc: Encodes the specified byte array as a String using the Base64 encoding scheme.
1985 * @tc.type: FUNC
1986 */
1987 HWTEST_F(NativeEngineTest, encodeToStringTest002, testing::ext::TestSize.Level0)
1988 {
1989 HILOG_INFO("encodeToStringTest002 start");
1990 napi_env env = (napi_env)engine_;
1991 OHOS::Util::Base64 base64;
1992
1993 unsigned char input[14] = {66, 97, 115, 101, 54, 52, 32, 78, 111, 100, 101, 46, 106, 115};
1994 napi_value arrayBuffer = nullptr;
1995 size_t arrayBufferSize = 14;
1996 void* data = nullptr;
1997 napi_create_arraybuffer(env, arrayBufferSize, &data, &arrayBuffer);
1998 int ret = memcpy_s(data, sizeof(input), reinterpret_cast<void*>(input), sizeof(input));
1999 ASSERT_EQ(0, ret);
2000 napi_value src = nullptr;
2001 napi_create_typedarray(env, napi_uint8_array, arrayBufferSize, arrayBuffer, 0, &src);
2002 napi_value result = base64.EncodeToStringSync(env, src, OHOS::Util::Type::BASIC);
2003 size_t prolen = 0;
2004 char* inputString = nullptr;
2005 napi_get_value_string_utf8(env, result, nullptr, 0, &prolen);
2006 if (prolen > 0) {
2007 inputString = new char[prolen + 1];
2008 if (memset_s(inputString, prolen + 1, '\0', prolen + 1) != 0) {
2009 napi_throw_error(env, "-1", "decode inputString memset_s failed");
2010 }
2011 } else {
2012 napi_throw_error(env, "-2", "prolen is error !");
2013 }
2014 napi_get_value_string_utf8(env, result, inputString, prolen + 1, &prolen);
2015 ASSERT_STREQ("QmFzZTY0IE5vZGUuanM=", inputString);
2016 if (inputString != nullptr) {
2017 delete []inputString;
2018 inputString = nullptr;
2019 }
2020 }
2021
2022 /* @tc.name: encodeToStringTest003
2023 * @tc.desc: Encodes the specified byte array as a String using the Base64 encoding scheme.
2024 * @tc.type: FUNC
2025 */
2026 HWTEST_F(NativeEngineTest, encodeToStringTest003, testing::ext::TestSize.Level0)
2027 {
2028 HILOG_INFO("encodeToStringTest003 start");
2029 napi_env env = (napi_env)engine_;
2030 OHOS::Util::Base64 base64;
2031
2032 unsigned char input[26] = {66, 97, 115, 101, 54, 52, 32, 69, 110,
2033 99, 111, 100, 105, 110, 103, 32, 105, 110, 32, 78, 111, 100, 101, 46, 106, 115};
2034 napi_value arrayBuffer = nullptr;
2035 size_t arrayBufferSize = 26;
2036 void* data = nullptr;
2037 napi_create_arraybuffer(env, arrayBufferSize, &data, &arrayBuffer);
2038 int ret = memcpy_s(data, sizeof(input), reinterpret_cast<void*>(input), sizeof(input));
2039 ASSERT_EQ(0, ret);
2040 napi_value src = nullptr;
2041 napi_create_typedarray(env, napi_uint8_array, arrayBufferSize, arrayBuffer, 0, &src);
2042 napi_value result = base64.EncodeToStringSync(env, src, OHOS::Util::Type::BASIC);
2043 size_t prolen = 0;
2044 char* inputString = nullptr;
2045 napi_get_value_string_utf8(env, result, nullptr, 0, &prolen);
2046 if (prolen > 0) {
2047 inputString = new char[prolen + 1];
2048 if (memset_s(inputString, prolen + 1, '\0', prolen + 1) != 0) {
2049 napi_throw_error(env, "-1", "decode inputString memset_s failed");
2050 }
2051 } else {
2052 napi_throw_error(env, "-2", "prolen is error !");
2053 }
2054 napi_get_value_string_utf8(env, result, inputString, prolen + 1, &prolen);
2055 ASSERT_STREQ("QmFzZTY0IEVuY29kaW5nIGluIE5vZGUuanM=", inputString);
2056 if (inputString != nullptr) {
2057 delete []inputString;
2058 inputString = nullptr;
2059 }
2060 }
2061
2062 /* @tc.name: encodeToStringTest004
2063 * @tc.desc: Encodes the specified byte array as a String using the Base64 encoding scheme.
2064 * @tc.type: FUNC
2065 */
2066 HWTEST_F(NativeEngineTest, encodeToStringTest004, testing::ext::TestSize.Level0)
2067 {
2068 HILOG_INFO("encodeToStringTest004 start");
2069 napi_env env = (napi_env)engine_;
2070 OHOS::Util::Base64 base64;
2071
2072 unsigned char input[4] = {168, 174, 155, 255};
2073 napi_value arrayBuffer = nullptr;
2074 size_t arrayBufferSize = 4;
2075 void* data = nullptr;
2076 napi_create_arraybuffer(env, arrayBufferSize, &data, &arrayBuffer);
2077 int ret = memcpy_s(data, sizeof(input), reinterpret_cast<void*>(input), sizeof(input));
2078 ASSERT_EQ(0, ret);
2079 napi_value src = nullptr;
2080 napi_create_typedarray(env, napi_uint8_array, arrayBufferSize, arrayBuffer, 0, &src);
2081 napi_value result = base64.EncodeToStringSync(env, src, OHOS::Util::Type::BASIC);
2082 size_t prolen = 0;
2083 char* inputString = nullptr;
2084 napi_get_value_string_utf8(env, result, nullptr, 0, &prolen);
2085 if (prolen > 0) {
2086 inputString = new char[prolen + 1];
2087 if (memset_s(inputString, prolen + 1, '\0', prolen + 1) != 0) {
2088 napi_throw_error(env, "-1", "decode inputString memset_s failed");
2089 }
2090 } else {
2091 napi_throw_error(env, "-2", "prolen is error !");
2092 }
2093 napi_get_value_string_utf8(env, result, inputString, prolen + 1, &prolen);
2094 ASSERT_STREQ("qK6b/w==", inputString);
2095 if (inputString != nullptr) {
2096 delete []inputString;
2097 inputString = nullptr;
2098 }
2099 }
2100
2101 /* @tc.name: encodeToStringTest005
2102 * @tc.desc: Encodes the specified byte array as a String using the Base64 encoding scheme.
2103 * @tc.type: FUNC
2104 */
2105 HWTEST_F(NativeEngineTest, encodeToStringTest005, testing::ext::TestSize.Level0)
2106 {
2107 HILOG_INFO("encodeToStringTest005 start");
2108 napi_env env = (napi_env)engine_;
2109 OHOS::Util::Base64 base64;
2110
2111 unsigned char input[6] = {66, 97, 115, 101, 54, 52};
2112 napi_value arrayBuffer = nullptr;
2113 size_t arrayBufferSize = 6;
2114 void* data = nullptr;
2115 napi_create_arraybuffer(env, arrayBufferSize, &data, &arrayBuffer);
2116 int ret = memcpy_s(data, sizeof(input), reinterpret_cast<void*>(input), sizeof(input));
2117 ASSERT_EQ(0, ret);
2118 napi_value src = nullptr;
2119 napi_create_typedarray(env, napi_uint8_array, arrayBufferSize, arrayBuffer, 0, &src);
2120 napi_value result = base64.EncodeToStringSync(env, src, OHOS::Util::Type::BASIC);
2121 size_t prolen = 0;
2122 char* inputString = nullptr;
2123 napi_get_value_string_utf8(env, result, nullptr, 0, &prolen);
2124 if (prolen > 0) {
2125 inputString = new char[prolen + 1];
2126 if (memset_s(inputString, prolen + 1, '\0', prolen + 1) != 0) {
2127 napi_throw_error(env, "-1", "decode inputString memset_s failed");
2128 }
2129 } else {
2130 napi_throw_error(env, "-2", "prolen is error !");
2131 }
2132 napi_get_value_string_utf8(env, result, inputString, prolen + 1, &prolen);
2133 ASSERT_STREQ("QmFzZTY0", inputString);
2134 if (inputString != nullptr) {
2135 delete []inputString;
2136 inputString = nullptr;
2137 }
2138 }
2139
2140 /* @tc.name: decodeTest001
2141 * @tc.desc: Decodes the Base64-encoded string or input u8 array
2142 into the newly allocated u8 array using the Base64 encoding scheme.
2143 * @tc.type: FUNC
2144 */
2145 HWTEST_F(NativeEngineTest, decodeTest001, testing::ext::TestSize.Level0)
2146 {
2147 HILOG_INFO("decodeTest001 start");
2148 napi_env env = (napi_env)engine_;
2149 OHOS::Util::Base64 base64;
2150
2151 unsigned char input[4] = {99, 122, 69, 122};
2152 napi_value arrayBuffer = nullptr;
2153 size_t arrayBufferSize = 4;
2154 void* data = nullptr;
2155 napi_create_arraybuffer(env, arrayBufferSize, &data, &arrayBuffer);
2156 int ret = memcpy_s(data, sizeof(input), reinterpret_cast<void*>(input), sizeof(input));
2157 ASSERT_EQ(0, ret);
2158 napi_value src = nullptr;
2159 napi_create_typedarray(env, napi_uint8_array, arrayBufferSize, arrayBuffer, 0, &src);
2160 napi_value result = base64.DecodeSync(env, src, OHOS::Util::Type::BASIC);
2161 char excepted[3] = {115, 49, 51};
2162 napi_typedarray_type type;
2163 size_t srcLength = 0;
2164 void* srcData = nullptr;
2165 napi_value srcBuffer = nullptr;
2166 size_t byteOffset = 0;
2167 napi_get_typedarray_info(env, result, &type, &srcLength, &srcData, &srcBuffer, &byteOffset);
2168 char* res = (char*)srcData;
2169
2170 ASSERT_EQ(res[0], excepted[0]);
2171 ASSERT_EQ(res[1], excepted[1]);
2172 ASSERT_EQ(res[2], excepted[2]);
2173 }
2174
2175 /* @tc.name: decodeTest002
2176 * @tc.desc: Decodes the Base64-encoded string or input u8 array
2177 into the newly allocated u8 array using the Base64 encoding scheme.
2178 * @tc.type: FUNC
2179 */
2180 HWTEST_F(NativeEngineTest, decodeTest002, testing::ext::TestSize.Level0)
2181 {
2182 HILOG_INFO("decodeTest002 start");
2183 napi_env env = (napi_env)engine_;
2184 OHOS::Util::Base64 base64;
2185
2186 unsigned char input[20] = {81, 109, 70, 122, 90, 84, 89, 48, 73, 69, 53, 118, 90, 71, 85, 117, 97, 110, 77, 61};
2187 napi_value arrayBuffer = nullptr;
2188 size_t arrayBufferSize = 20;
2189 void* data = nullptr;
2190 napi_create_arraybuffer(env, arrayBufferSize, &data, &arrayBuffer);
2191 int ret = memcpy_s(data, sizeof(input), reinterpret_cast<void*>(input), sizeof(input));
2192 ASSERT_EQ(0, ret);
2193 napi_value src = nullptr;
2194 napi_create_typedarray(env, napi_uint8_array, arrayBufferSize, arrayBuffer, 0, &src);
2195 napi_value result = base64.DecodeSync(env, src, OHOS::Util::Type::BASIC);
2196 char excepted[14] = {66, 97, 115, 101, 54, 52, 32, 78, 111, 100, 101, 46, 106, 115};
2197 napi_typedarray_type type;
2198 size_t srcLength = 0;
2199 void* srcData = nullptr;
2200 napi_value srcBuffer = nullptr;
2201 size_t byteOffset = 0;
2202 napi_get_typedarray_info(env, result, &type, &srcLength, &srcData, &srcBuffer, &byteOffset);
2203 char* res = (char*)srcData;
2204
2205 for (size_t i = 0; i < 14; i++) {
2206 ASSERT_EQ(res[i], excepted[i]);
2207 }
2208 }
2209
2210 /* @tc.name: decodeTest003
2211 * @tc.desc: Decodes the Base64-encoded string or input u8 array
2212 into the newly allocated u8 array using the Base64 encoding scheme.
2213 * @tc.type: FUNC
2214 */
2215 HWTEST_F(NativeEngineTest, decodeTest003, testing::ext::TestSize.Level0)
2216 {
2217 HILOG_INFO("decodeTest003 start");
2218 napi_env env = (napi_env)engine_;
2219 OHOS::Util::Base64 base64;
2220
2221 std::string input = "czEz";
2222 napi_value src = nullptr;
2223 napi_create_string_utf8(env, input.c_str(), input.size(), &src);
2224 napi_value result = base64.DecodeSync(env, src, OHOS::Util::Type::BASIC);
2225 char excepted[3] = {115, 49, 51};
2226 napi_typedarray_type type;
2227 size_t srcLength = 0;
2228 void* srcData = nullptr;
2229 napi_value srcBuffer = nullptr;
2230 size_t byteOffset = 0;
2231 napi_get_typedarray_info(env, result, &type, &srcLength, &srcData, &srcBuffer, &byteOffset);
2232 char* res = (char*)srcData;
2233
2234 ASSERT_EQ(res[0], excepted[0]);
2235 ASSERT_EQ(res[1], excepted[1]);
2236 ASSERT_EQ(res[2], excepted[2]);
2237 }
2238
2239 /* @tc.name: decodeTest004
2240 * @tc.desc: Decodes the Base64-encoded string or input u8 array
2241 into the newly allocated u8 array using the Base64 encoding scheme.
2242 * @tc.type: FUNC
2243 */
2244 HWTEST_F(NativeEngineTest, decodeTest004, testing::ext::TestSize.Level0)
2245 {
2246 HILOG_INFO("decodeTest004 start");
2247 napi_env env = (napi_env)engine_;
2248 OHOS::Util::Base64 base64;
2249
2250 std::string input = "qK6b/w==";
2251 napi_value src = nullptr;
2252 napi_create_string_utf8(env, input.c_str(), input.size(), &src);
2253 napi_value result = base64.DecodeSync(env, src, OHOS::Util::Type::BASIC);
2254 char excepted[4] = {168, 174, 155, 255};
2255 napi_typedarray_type type;
2256 size_t srcLength = 0;
2257 void* srcData = nullptr;
2258 napi_value srcBuffer = nullptr;
2259 size_t byteOffset = 0;
2260 napi_get_typedarray_info(env, result, &type, &srcLength, &srcData, &srcBuffer, &byteOffset);
2261 char* res = (char*)srcData;
2262 for (size_t i = 0; i < 4; i++) {
2263 ASSERT_EQ(res[i], excepted[i]);
2264 }
2265 }
2266
2267 /* @tc.name: decodeTest005
2268 * @tc.desc: Decodes the Base64-encoded string or input u8 array
2269 into the newly allocated u8 array using the Base64 encoding scheme.
2270 * @tc.type: FUNC
2271 */
2272 HWTEST_F(NativeEngineTest, decodeTest005, testing::ext::TestSize.Level0)
2273 {
2274 HILOG_INFO("decodeTest005 start");
2275 napi_env env = (napi_env)engine_;
2276 OHOS::Util::Base64 base64;
2277
2278 std::string input = "QmFzZTY0";
2279 napi_value src = nullptr;
2280 napi_create_string_utf8(env, input.c_str(), input.size(), &src);
2281 napi_value result = base64.DecodeSync(env, src, OHOS::Util::Type::BASIC);
2282 char excepted[6] = {66, 97, 115, 101, 54, 52};
2283 napi_typedarray_type type;
2284 size_t srcLength = 0;
2285 void* srcData = nullptr;
2286 napi_value srcBuffer = nullptr;
2287 size_t byteOffset = 0;
2288 napi_get_typedarray_info(env, result, &type, &srcLength, &srcData, &srcBuffer, &byteOffset);
2289 char* res = (char*)srcData;
2290 for (size_t i = 0; i < 6; i++) {
2291 ASSERT_EQ(res[i], excepted[i]);
2292 }
2293 }
2294
2295 /* @tc.name: decodeTest006
2296 * @tc.desc: Decodes the Base64-encoded string or input unit32 array with return null.
2297 * @tc.type: FUNC
2298 */
2299 HWTEST_F(NativeEngineTest, decodeTest006, testing::ext::TestSize.Level0)
2300 {
2301 HILOG_INFO("decodeTest006 start");
2302 napi_env env = (napi_env)engine_;
2303 OHOS::Util::Base64 base64;
2304
2305 std::string input1 = "";
2306 napi_value src1 = nullptr;
2307 napi_create_string_utf8(env, input1.c_str(), input1.size(), &src1);
2308 base64.DecodeSync(env, src1, OHOS::Util::Type::BASIC_URL_SAFE);
2309 napi_value result1 = base64.DecodeSync(env, src1, OHOS::Util::Type::BASIC);
2310 ASSERT_EQ(result1, nullptr);
2311 napi_value exception;
2312 napi_get_and_clear_last_exception(env, &exception);
2313
2314 unsigned char input[20] = {81, 109, 70, 122, 90, 84, 89, 48, 73, 69, 53, 118, 90, 71, 85, 117, 97, 110, 77, 61};
2315 napi_value arrayBuffer = nullptr;
2316 size_t arrayBufferSize = 20;
2317 void* data = nullptr;
2318 napi_create_arraybuffer(env, arrayBufferSize, &data, &arrayBuffer);
2319 int ret = memcpy_s(data, sizeof(input), reinterpret_cast<void*>(input), sizeof(input));
2320 ASSERT_EQ(0, ret);
2321 napi_value src = nullptr;
2322 napi_create_typedarray(env, napi_uint32_array, arrayBufferSize, arrayBuffer, 0, &src);
2323 napi_value result = base64.DecodeSync(env, src, OHOS::Util::Type::BASIC);
2324 ASSERT_EQ(result, nullptr);
2325
2326 napi_get_and_clear_last_exception(env, &exception);
2327 }
2328
2329 /* @tc.name: decodeTest007
2330 * @tc.desc: Decodes the Base64-encoded string with type BASIC_URL_SAFE.
2331 * @tc.type: FUNC
2332 */
2333 HWTEST_F(NativeEngineTest, decodeTest007, testing::ext::TestSize.Level0)
2334 {
2335 HILOG_INFO("decodeTest007 start");
2336 napi_env env = (napi_env)engine_;
2337 OHOS::Util::Base64 base64;
2338 std::string input = "qK6b/w==";
2339 napi_value src = nullptr;
2340 napi_create_string_utf8(env, input.c_str(), input.size(), &src);
2341 napi_value result = base64.DecodeSync(env, src, OHOS::Util::Type::BASIC_URL_SAFE);
2342 ASSERT_EQ(nullptr, result);
2343
2344 std::string input1 = "qK6b/w";
2345 napi_value src1 = nullptr;
2346 napi_create_string_utf8(env, input1.c_str(), input1.size(), &src1);
2347 napi_value result1 = base64.DecodeSync(env, src1, OHOS::Util::Type::BASIC_URL_SAFE);
2348 ASSERT_EQ(nullptr, result1);
2349
2350 std::string input2 = "qK6b/w=";
2351 napi_value src2 = nullptr;
2352 napi_create_string_utf8(env, input2.c_str(), input2.size(), &src2);
2353 napi_value result2 = base64.DecodeSync(env, src2, OHOS::Util::Type::BASIC_URL_SAFE);
2354 ASSERT_EQ(nullptr, result2);
2355
2356 napi_value exception;
2357 napi_get_and_clear_last_exception(env, &exception);
2358 }
2359
2360 /* @tc.name: encodeAsyncTest001
2361 * @tc.desc: Asynchronously encodes all bytes in the specified u8 array
2362 into the newly allocated u8 array using the Base64 encoding scheme.
2363 * @tc.type: FUNC
2364 */
2365 HWTEST_F(NativeEngineTest, encodeAsyncTest001, testing::ext::TestSize.Level0)
2366 {
2367 HILOG_INFO("encodeAsyncTest001 start");
2368 napi_env env = (napi_env)engine_;
2369 OHOS::Util::Base64 base64;
2370 unsigned char input[3] = {0x73, 0x31, 0x33};
2371 napi_value arrayBuffer = nullptr;
2372 void* data = nullptr;
2373 size_t arrayBufferSize = 3;
2374 napi_create_arraybuffer(env, arrayBufferSize, &data, &arrayBuffer);
2375 int ret = memcpy_s(data, sizeof(input), reinterpret_cast<void*>(input), sizeof(input));
2376 ASSERT_EQ(0, ret);
2377 napi_value src = nullptr;
2378 napi_create_typedarray(env, napi_uint8_array, arrayBufferSize, arrayBuffer, 0, &src);
2379
2380 napi_value result = base64.Encode(env, src, OHOS::Util::Type::BASIC);
2381 bool res = false;
2382 napi_is_promise(env, result, &res);
2383 ASSERT_TRUE(res);
2384
2385 napi_value src1 = nullptr;
2386 napi_create_typedarray(env, napi_uint16_array, arrayBufferSize, arrayBuffer, 0, &src1);
2387 napi_value result1 = base64.Encode(env, src1, OHOS::Util::Type::BASIC);
2388 ASSERT_EQ(result1, nullptr);
2389
2390 napi_value exception;
2391 napi_get_and_clear_last_exception(env, &exception);
2392 }
2393
2394 /* @tc.name: encodeAsyncTest002
2395 * @tc.desc: Asynchronously encodes all bytes in the specified u8 array
2396 into the newly allocated u8 array using the Base64 encoding scheme.
2397 * @tc.type: FUNC
2398 */
2399 HWTEST_F(NativeEngineTest, encodeAsyncTest002, testing::ext::TestSize.Level0)
2400 {
2401 HILOG_INFO("encodeAsyncTest002 start");
2402 napi_env env = (napi_env)engine_;
2403 OHOS::Util::Base64 base64;
2404 unsigned char input[14] = {66, 97, 115, 101, 54, 52, 32, 78, 111, 100, 101, 46, 106, 115};
2405 napi_value arrayBuffer = nullptr;
2406 void* data = nullptr;
2407 size_t arrayBufferSize = 14;
2408 napi_create_arraybuffer(env, arrayBufferSize, &data, &arrayBuffer);
2409 int ret = memcpy_s(data, sizeof(input), reinterpret_cast<void*>(input), sizeof(input));
2410 ASSERT_EQ(0, ret);
2411 napi_value src = nullptr;
2412 napi_create_typedarray(env, napi_uint8_array, arrayBufferSize, arrayBuffer, 0, &src);
2413
2414 napi_value result = base64.Encode(env, src, OHOS::Util::Type::BASIC);
2415 bool res = false;
2416 napi_is_promise(env, result, &res);
2417 ASSERT_TRUE(res);
2418 }
2419
2420 /* @tc.name: encodeAsyncTest003
2421 * @tc.desc: Asynchronously encodes all bytes in the specified u8 array
2422 into the newly allocated u8 array using the Base64 encoding scheme.
2423 * @tc.type: FUNC
2424 */
2425 HWTEST_F(NativeEngineTest, encodeAsyncTest003, testing::ext::TestSize.Level0)
2426 {
2427 HILOG_INFO("encodeAsyncTest003 start");
2428 napi_env env = (napi_env)engine_;
2429 OHOS::Util::Base64 base64;
2430 unsigned char input[26] = {66, 97, 115, 101, 54, 52, 32, 69, 110,
2431 99, 111, 100, 105, 110, 103, 32, 105, 110, 32, 78, 111, 100, 101, 46, 106, 115};
2432 napi_value arrayBuffer = nullptr;
2433 void* data = nullptr;
2434 size_t arrayBufferSize = 26;
2435 napi_create_arraybuffer(env, arrayBufferSize, &data, &arrayBuffer);
2436 int ret = memcpy_s(data, sizeof(input), reinterpret_cast<void*>(input), sizeof(input));
2437 ASSERT_EQ(0, ret);
2438 napi_value src = nullptr;
2439 napi_create_typedarray(env, napi_uint8_array, arrayBufferSize, arrayBuffer, 0, &src);
2440
2441 napi_value result = base64.Encode(env, src, OHOS::Util::Type::BASIC);
2442 bool res = false;
2443 napi_is_promise(env, result, &res);
2444 ASSERT_TRUE(res);
2445 }
2446
2447 /* @tc.name: encodeAsyncTest004
2448 * @tc.desc: Asynchronously encodes all bytes in the specified u8 array
2449 into the newly allocated u8 array using the Base64 encoding scheme.
2450 * @tc.type: FUNC
2451 */
2452 HWTEST_F(NativeEngineTest, encodeAsyncTest004, testing::ext::TestSize.Level0)
2453 {
2454 HILOG_INFO("encodeAsyncTest004 start");
2455 napi_env env = (napi_env)engine_;
2456 OHOS::Util::Base64 base64;
2457 unsigned char input[4] = {168, 174, 155, 255};
2458 napi_value arrayBuffer = nullptr;
2459 void* data = nullptr;
2460 size_t arrayBufferSize = 4;
2461 napi_create_arraybuffer(env, arrayBufferSize, &data, &arrayBuffer);
2462 int ret = memcpy_s(data, sizeof(input), reinterpret_cast<void*>(input), sizeof(input));
2463 ASSERT_EQ(0, ret);
2464 napi_value src = nullptr;
2465 napi_create_typedarray(env, napi_uint8_array, arrayBufferSize, arrayBuffer, 0, &src);
2466
2467 napi_value result = base64.Encode(env, src, OHOS::Util::Type::BASIC);
2468 bool res = false;
2469 napi_is_promise(env, result, &res);
2470 ASSERT_TRUE(res);
2471 }
2472
2473 /* @tc.name: encodeAsyncTest005
2474 * @tc.desc: Asynchronously encodes all bytes in the specified u8 array
2475 into the newly allocated u8 array using the Base64 encoding scheme.
2476 * @tc.type: FUNC
2477 */
2478 HWTEST_F(NativeEngineTest, encodeAsyncTest005, testing::ext::TestSize.Level0)
2479 {
2480 HILOG_INFO("encodeAsyncTest005 start");
2481 napi_env env = (napi_env)engine_;
2482 OHOS::Util::Base64 base64;
2483 unsigned char input[6] = {66, 97, 115, 101, 54, 52};
2484 napi_value arrayBuffer = nullptr;
2485 void* data = nullptr;
2486 size_t arrayBufferSize = 6;
2487 napi_create_arraybuffer(env, arrayBufferSize, &data, &arrayBuffer);
2488 int ret = memcpy_s(data, sizeof(input), reinterpret_cast<void*>(input), sizeof(input));
2489 ASSERT_EQ(0, ret);
2490 napi_value src = nullptr;
2491 napi_create_typedarray(env, napi_uint8_array, arrayBufferSize, arrayBuffer, 0, &src);
2492
2493 napi_value result = base64.Encode(env, src, OHOS::Util::Type::BASIC);
2494 bool res = false;
2495 napi_is_promise(env, result, &res);
2496 ASSERT_TRUE(res);
2497 }
2498
2499 /* @tc.name: encodeToStringAsyncTest001
2500 * @tc.desc: Asynchronously encodes the specified byte array into a String using the Base64 encoding scheme.
2501 * @tc.type: FUNC
2502 */
2503 HWTEST_F(NativeEngineTest, encodeToStringAsyncTest001, testing::ext::TestSize.Level0)
2504 {
2505 HILOG_INFO("encodeToStringAsyncTest001 start");
2506 napi_env env = (napi_env)engine_;
2507 OHOS::Util::Base64 base64;
2508
2509 unsigned char input[3] = {115, 49, 51};
2510 napi_value arrayBuffer = nullptr;
2511 size_t arrayBufferSize = 3;
2512 void* data = nullptr;
2513 napi_create_arraybuffer(env, arrayBufferSize, &data, &arrayBuffer);
2514 int ret = memcpy_s(data, sizeof(input), reinterpret_cast<void*>(input), sizeof(input));
2515 ASSERT_EQ(0, ret);
2516 napi_value src = nullptr;
2517 napi_create_typedarray(env, napi_uint8_array, arrayBufferSize, arrayBuffer, 0, &src);
2518 napi_value result = base64.EncodeToString(env, src, OHOS::Util::Type::BASIC);
2519 bool res = false;
2520 napi_is_promise(env, result, &res);
2521 ASSERT_TRUE(res);
2522
2523 napi_value src1 = nullptr;
2524 napi_create_typedarray(env, napi_uint16_array, arrayBufferSize, arrayBuffer, 0, &src1);
2525 napi_value result1 = base64.EncodeToString(env, src1, OHOS::Util::Type::BASIC);
2526 ASSERT_EQ(result1, nullptr);
2527
2528 napi_value exception;
2529 napi_get_and_clear_last_exception(env, &exception);
2530 }
2531
2532 /* @tc.name: encodeToStringAsyncTest002
2533 * @tc.desc: Asynchronously encodes the specified byte array into a String using the Base64 encoding scheme.
2534 * @tc.type: FUNC
2535 */
2536 HWTEST_F(NativeEngineTest, encodeToStringAsyncTest002, testing::ext::TestSize.Level0)
2537 {
2538 HILOG_INFO("encodeToStringAsyncTest002 start");
2539 napi_env env = (napi_env)engine_;
2540 OHOS::Util::Base64 base64;
2541 unsigned char input[14] = {66, 97, 115, 101, 54, 52, 32, 78, 111, 100, 101, 46, 106, 115};
2542 napi_value arrayBuffer = nullptr;
2543 void* data = nullptr;
2544 size_t arrayBufferSize = 14;
2545 napi_create_arraybuffer(env, arrayBufferSize, &data, &arrayBuffer);
2546 int ret = memcpy_s(data, sizeof(input), reinterpret_cast<void*>(input), sizeof(input));
2547 ASSERT_EQ(0, ret);
2548 napi_value src = nullptr;
2549 napi_create_typedarray(env, napi_uint8_array, arrayBufferSize, arrayBuffer, 0, &src);
2550
2551 napi_value result = base64.EncodeToString(env, src, OHOS::Util::Type::BASIC);
2552 bool res = false;
2553 napi_is_promise(env, result, &res);
2554 ASSERT_TRUE(res);
2555 }
2556
2557 /* @tc.name: encodeToStringAsyncTest003
2558 * @tc.desc: Asynchronously encodes the specified byte array into a String using the Base64 encoding scheme.
2559 * @tc.type: FUNC
2560 */
2561 HWTEST_F(NativeEngineTest, encodeToStringAsyncTest003, testing::ext::TestSize.Level0)
2562 {
2563 HILOG_INFO("encodeToStringAsyncTest003 start");
2564 napi_env env = (napi_env)engine_;
2565 OHOS::Util::Base64 base64;
2566 unsigned char input[26] = {66, 97, 115, 101, 54, 52, 32, 69, 110,
2567 99, 111, 100, 105, 110, 103, 32, 105, 110, 32, 78, 111, 100, 101, 46, 106, 115};
2568 napi_value arrayBuffer = nullptr;
2569 void* data = nullptr;
2570 size_t arrayBufferSize = 26;
2571 napi_create_arraybuffer(env, arrayBufferSize, &data, &arrayBuffer);
2572 int ret = memcpy_s(data, sizeof(input), reinterpret_cast<void*>(input), sizeof(input));
2573 ASSERT_EQ(0, ret);
2574 napi_value src = nullptr;
2575 napi_create_typedarray(env, napi_uint8_array, arrayBufferSize, arrayBuffer, 0, &src);
2576
2577 napi_value result = base64.EncodeToString(env, src, OHOS::Util::Type::BASIC);
2578 bool res = false;
2579 napi_is_promise(env, result, &res);
2580 ASSERT_TRUE(res);
2581 }
2582
2583 /* @tc.name: encodeToStringAsyncTest004
2584 * @tc.desc: Asynchronously encodes the specified byte array into a String using the Base64 encoding scheme.
2585 * @tc.type: FUNC
2586 */
2587 HWTEST_F(NativeEngineTest, encodeToStringAsyncTest004, testing::ext::TestSize.Level0)
2588 {
2589 HILOG_INFO("encodeToStringAsyncTest004 start");
2590 napi_env env = (napi_env)engine_;
2591 OHOS::Util::Base64 base64;
2592 unsigned char input[4] = {168, 174, 155, 255};
2593 napi_value arrayBuffer = nullptr;
2594 void* data = nullptr;
2595 size_t arrayBufferSize = 4;
2596 napi_create_arraybuffer(env, arrayBufferSize, &data, &arrayBuffer);
2597 int ret = memcpy_s(data, sizeof(input), reinterpret_cast<void*>(input), sizeof(input));
2598 ASSERT_EQ(0, ret);
2599 napi_value src = nullptr;
2600 napi_create_typedarray(env, napi_uint8_array, arrayBufferSize, arrayBuffer, 0, &src);
2601
2602 napi_value result = base64.EncodeToString(env, src, OHOS::Util::Type::BASIC);
2603 bool res = false;
2604 napi_is_promise(env, result, &res);
2605 ASSERT_TRUE(res);
2606 }
2607
2608 /* @tc.name: encodeToStringAsyncTest005
2609 * @tc.desc: Asynchronously encodes the specified byte array into a String using the Base64 encoding scheme.
2610 * @tc.type: FUNC
2611 */
2612 HWTEST_F(NativeEngineTest, encodeToStringAsyncTest005, testing::ext::TestSize.Level0)
2613 {
2614 HILOG_INFO("encodeToStringAsyncTest005 start");
2615 napi_env env = (napi_env)engine_;
2616 OHOS::Util::Base64 base64;
2617 unsigned char input[6] = {66, 97, 115, 101, 54, 52};
2618 napi_value arrayBuffer = nullptr;
2619 void* data = nullptr;
2620 size_t arrayBufferSize = 6;
2621 napi_create_arraybuffer(env, arrayBufferSize, &data, &arrayBuffer);
2622 int ret = memcpy_s(data, sizeof(input), reinterpret_cast<void*>(input), sizeof(input));
2623 ASSERT_EQ(0, ret);
2624 napi_value src = nullptr;
2625 napi_create_typedarray(env, napi_uint8_array, arrayBufferSize, arrayBuffer, 0, &src);
2626
2627 napi_value result = base64.EncodeToString(env, src, OHOS::Util::Type::BASIC);
2628 bool res = false;
2629 napi_is_promise(env, result, &res);
2630 ASSERT_TRUE(res);
2631 }
2632
2633 /* @tc.name: decodeAsyncTest001
2634 * @tc.desc: Use the Base64 encoding scheme to asynchronously decode a
2635 Base64-encoded string or input u8 array into a newly allocated u8 array.
2636 * @tc.type: FUNC
2637 */
2638 HWTEST_F(NativeEngineTest, decodeAsyncTest001, testing::ext::TestSize.Level0)
2639 {
2640 HILOG_INFO("decodeAsyncTest001 start");
2641 napi_env env = (napi_env)engine_;
2642 OHOS::Util::Base64 base64;
2643
2644 unsigned char input[4] = {99, 122, 69, 122};
2645 napi_value arrayBuffer = nullptr;
2646 size_t arrayBufferSize = 4;
2647 void* data = nullptr;
2648 napi_create_arraybuffer(env, arrayBufferSize, &data, &arrayBuffer);
2649 int ret = memcpy_s(data, sizeof(input), reinterpret_cast<void*>(input), sizeof(input));
2650 ASSERT_EQ(0, ret);
2651 napi_value src = nullptr;
2652 napi_create_typedarray(env, napi_uint8_array, arrayBufferSize, arrayBuffer, 0, &src);
2653 napi_value result = base64.Decode(env, src, OHOS::Util::Type::BASIC);
2654 bool res = false;
2655 napi_is_promise(env, result, &res);
2656 ASSERT_TRUE(res);
2657 }
2658
2659 /* @tc.name: decodeAsyncTest002
2660 * @tc.desc: Use the Base64 encoding scheme to asynchronously decode a
2661 Base64-encoded string or input u8 array into a newly allocated u8 array.
2662 * @tc.type: FUNC
2663 */
2664 HWTEST_F(NativeEngineTest, decodeAsyncTest002, testing::ext::TestSize.Level0)
2665 {
2666 HILOG_INFO("decodeAsyncTest002 start");
2667 napi_env env = (napi_env)engine_;
2668 OHOS::Util::Base64 base64;
2669
2670 unsigned char input[8] = {113, 75, 54, 98, 47, 119, 61, 61};
2671 napi_value arrayBuffer = nullptr;
2672 size_t arrayBufferSize = 8;
2673 void* data = nullptr;
2674 napi_create_arraybuffer(env, arrayBufferSize, &data, &arrayBuffer);
2675 int ret = memcpy_s(data, sizeof(input), reinterpret_cast<void*>(input), sizeof(input));
2676 ASSERT_EQ(0, ret);
2677 napi_value src = nullptr;
2678 napi_create_typedarray(env, napi_uint8_array, arrayBufferSize, arrayBuffer, 0, &src);
2679 napi_value result = base64.Decode(env, src, OHOS::Util::Type::BASIC);
2680 bool res = false;
2681 napi_is_promise(env, result, &res);
2682 ASSERT_TRUE(res);
2683 }
2684
2685 /* @tc.name: decodeAsyncTest003
2686 * @tc.desc: Use the Base64 encoding scheme to asynchronously decode a
2687 Base64-encoded string or input u8 array into a newly allocated u8 array.
2688 * @tc.type: FUNC
2689 */
2690 HWTEST_F(NativeEngineTest, decodeAsyncTest003, testing::ext::TestSize.Level0)
2691 {
2692 HILOG_INFO("decodeAsyncTest003 start");
2693 napi_env env = (napi_env)engine_;
2694 OHOS::Util::Base64 base64;
2695
2696 std::string input = "czEz";
2697 napi_value src = nullptr;
2698 napi_create_string_utf8(env, input.c_str(), input.size(), &src);
2699 napi_value result = base64.Decode(env, src, OHOS::Util::Type::BASIC);
2700 bool res = false;
2701 napi_is_promise(env, result, &res);
2702 ASSERT_TRUE(res);
2703 }
2704
2705 /* @tc.name: decodeAsyncTest004
2706 * @tc.desc: Use the Base64 encoding scheme to asynchronously decode a
2707 Base64-encoded string or input u8 array into a newly allocated u8 array.
2708 * @tc.type: FUNC
2709 */
2710 HWTEST_F(NativeEngineTest, decodeAsyncTest004, testing::ext::TestSize.Level0)
2711 {
2712 HILOG_INFO("decodeAsyncTest004 start");
2713 napi_env env = (napi_env)engine_;
2714 OHOS::Util::Base64 base64;
2715
2716 std::string input = "QmFzZTY0IEVuY29kaW5nIGluIE5vZGUuanM=";
2717 napi_value src = nullptr;
2718 napi_create_string_utf8(env, input.c_str(), input.size(), &src);
2719 napi_value result = base64.Decode(env, src, OHOS::Util::Type::BASIC);
2720 bool res = false;
2721 napi_is_promise(env, result, &res);
2722 ASSERT_TRUE(res);
2723 }
2724
2725 /* @tc.name: decodeAsyncTest005
2726 * @tc.desc: Use the Base64 encoding scheme to asynchronously decode a
2727 Base64-encoded string or input u8 array into a newly allocated u8 array.
2728 * @tc.type: FUNC
2729 */
2730 HWTEST_F(NativeEngineTest, decodeAsyncTest005, testing::ext::TestSize.Level0)
2731 {
2732 HILOG_INFO("decodeAsyncTest005 start");
2733 napi_env env = (napi_env)engine_;
2734 OHOS::Util::Base64 base64;
2735
2736 std::string input = "qK6b/w==";
2737 napi_value src = nullptr;
2738 napi_create_string_utf8(env, input.c_str(), input.size(), &src);
2739 napi_value result = base64.Decode(env, src, OHOS::Util::Type::BASIC);
2740 bool res = false;
2741 napi_is_promise(env, result, &res);
2742 ASSERT_TRUE(res);
2743 }
2744
2745 /* @tc.name: decodeAsyncTest006
2746 * @tc.desc: Use the Base64 encoding scheme to asynchronously decode a
2747 Base64-encoded string or input u8 array into a newly allocated u8 array.
2748 * @tc.type: FUNC
2749 */
2750 HWTEST_F(NativeEngineTest, decodeAsyncTest006, testing::ext::TestSize.Level0)
2751 {
2752 HILOG_INFO("decodeAsyncTest006 start");
2753 napi_env env = (napi_env)engine_;
2754 OHOS::Util::Base64 base64;
2755
2756 napi_value src = nullptr;
2757 napi_value result = base64.Decode(env, src, OHOS::Util::Type::BASIC);
2758 ASSERT_EQ(nullptr, result);
2759
2760 std::string input1 = "";
2761 napi_value src1 = nullptr;
2762 napi_create_string_utf8(env, input1.c_str(), input1.size(), &src1);
2763 napi_value result1 = base64.Decode(env, src1, OHOS::Util::Type::BASIC);
2764 ASSERT_EQ(nullptr, result1);
2765 napi_value exception;
2766 napi_get_and_clear_last_exception(env, &exception);
2767
2768 napi_value arrayBuffer = nullptr;
2769 size_t arrayBufferSize = 0;
2770 void* data = nullptr;
2771 napi_create_arraybuffer(env, arrayBufferSize, &data, &arrayBuffer);
2772 napi_value src2 = nullptr;
2773 napi_create_typedarray(env, napi_uint8_array, arrayBufferSize, arrayBuffer, 0, &src2);
2774 napi_value result2 = base64.Decode(env, src2, OHOS::Util::Type::BASIC);
2775 ASSERT_EQ(nullptr, result2);
2776
2777 napi_get_and_clear_last_exception(env, &exception);
2778 }
2779
2780 /**
2781 * @tc.name: stringDecoderWrite001
2782 * @tc.desc: Test the write function with complete data.
2783 * @tc.type: FUNC
2784 */
2785 HWTEST_F(NativeEngineTest, stringDecoderWrite001, testing::ext::TestSize.Level0)
2786 {
2787 OHOS::Util::StringDecoder stringDecoder("utf-8");
2788 napi_env env = (napi_env)engine_;
2789 const int arrCount = 6;
2790 size_t byteLength = arrCount;
2791 void* data = nullptr;
2792 napi_value resultBuff = nullptr;
2793 unsigned char arr[arrCount] = {0xE4, 0xBD, 0xA0, 0xE5, 0xA5, 0xBD};
2794 napi_create_arraybuffer(env, byteLength, &data, &resultBuff);
2795 int ret = memcpy_s(data, sizeof(arr), reinterpret_cast<void*>(arr), sizeof(arr));
2796 ASSERT_EQ(0, ret);
2797 napi_value result = nullptr;
2798 napi_create_typedarray(env, napi_int8_array, byteLength, resultBuff, 0, &result);
2799 napi_value testRes = stringDecoder.Write(env, result);
2800 size_t bufferSize = 0;
2801 if (napi_get_value_string_utf8(env, testRes, nullptr, 0, &bufferSize) != napi_ok) {
2802 HILOG_ERROR("can not get arg size");
2803 }
2804 std::string buffer = "";
2805 buffer.reserve(bufferSize);
2806 buffer.resize(bufferSize);
2807 if (napi_get_value_string_utf8(env, testRes, buffer.data(), bufferSize + 1, &bufferSize) != napi_ok) {
2808 HILOG_ERROR("can not get arg value");
2809 }
2810 ASSERT_STREQ("你好", buffer.c_str());
2811
2812 napi_value testResEnd = stringDecoder.End(env);
2813 size_t bufferEndSizeEnd = 0;
2814 if (napi_get_value_string_utf8(env, testResEnd, nullptr, 0, &bufferEndSizeEnd) != napi_ok) {
2815 HILOG_ERROR("can not get arg size");
2816 }
2817 std::string bufferEnd = "";
2818 bufferEnd.reserve(bufferEndSizeEnd);
2819 bufferEnd.resize(bufferEndSizeEnd);
2820 if (napi_get_value_string_utf8(
2821 env, testResEnd, bufferEnd.data(), bufferEndSizeEnd + 1, &bufferEndSizeEnd) != napi_ok) {
2822 HILOG_ERROR("can not get arg value");
2823 }
2824 ASSERT_STREQ("", bufferEnd.c_str());
2825 }
2826
2827 /**
2828 * @tc.name: stringDecoderWrite002
2829 * @tc.desc: Test the write function by splitting the complete data into two parts.
2830 * @tc.type: FUNC
2831 */
2832 HWTEST_F(NativeEngineTest, stringDecoderWrite002, testing::ext::TestSize.Level0)
2833 {
2834 OHOS::Util::StringDecoder stringDecoder("utf-8");
2835 napi_env env = (napi_env)engine_;
2836 const int arrCount = 2;
2837 size_t byteLength = arrCount;
2838 void* data = nullptr;
2839 napi_value resultBuff = nullptr;
2840 unsigned char arr[arrCount] = {0xE4, 0xBD};
2841 napi_create_arraybuffer(env, byteLength, &data, &resultBuff);
2842 int ret = memcpy_s(data, sizeof(arr), reinterpret_cast<void*>(arr), sizeof(arr));
2843 ASSERT_EQ(0, ret);
2844 napi_value result = nullptr;
2845 napi_create_typedarray(env, napi_int8_array, byteLength, resultBuff, 0, &result);
2846 napi_value testRes = stringDecoder.Write(env, result);
2847 size_t bufferSize = 0;
2848 if (napi_get_value_string_utf8(env, testRes, nullptr, 0, &bufferSize) != napi_ok) {
2849 HILOG_ERROR("can not get arg size");
2850 }
2851 std::string buffer = "";
2852 buffer.reserve(bufferSize);
2853 buffer.resize(bufferSize);
2854 if (napi_get_value_string_utf8(env, testRes, buffer.data(), bufferSize + 1, &bufferSize) != napi_ok) {
2855 HILOG_ERROR("can not get arg value");
2856 }
2857 ASSERT_STREQ("", buffer.c_str());
2858
2859 const int count = 4;
2860 byteLength = count;
2861 data = nullptr;
2862 resultBuff = nullptr;
2863 unsigned char uint8[count] = {0xA0, 0xE5, 0xA5, 0xBD};
2864 napi_create_arraybuffer(env, byteLength, &data, &resultBuff);
2865 ret = memcpy_s(data, sizeof(uint8), reinterpret_cast<void*>(uint8), sizeof(uint8));
2866 ASSERT_EQ(0, ret);
2867 result = nullptr;
2868 napi_create_typedarray(env, napi_int8_array, byteLength, resultBuff, 0, &result);
2869 testRes = stringDecoder.Write(env, result);
2870 bufferSize = 0;
2871 if (napi_get_value_string_utf8(env, testRes, nullptr, 0, &bufferSize) != napi_ok) {
2872 HILOG_ERROR("can not get arg size");
2873 }
2874 buffer = "";
2875 buffer.reserve(bufferSize);
2876 buffer.resize(bufferSize);
2877 if (napi_get_value_string_utf8(env, testRes, buffer.data(), bufferSize + 1, &bufferSize) != napi_ok) {
2878 HILOG_ERROR("can not get arg value");
2879 }
2880 ASSERT_STREQ("你好", buffer.c_str());
2881 }
2882
2883 /**
2884 * @tc.name: stringDecoderWrite003
2885 * @tc.desc: Test the write function with not typedarray.
2886 * @tc.type: FUNC
2887 */
2888 HWTEST_F(NativeEngineTest, stringDecoderWrite003, testing::ext::TestSize.Level0)
2889 {
2890 OHOS::Util::StringDecoder stringDecoder("utf-8");
2891 napi_env env = (napi_env)engine_;
2892 napi_value testRes = stringDecoder.Write(env, nullptr);
2893 ASSERT_EQ(testRes, nullptr);
2894
2895 napi_value exception;
2896 napi_get_and_clear_last_exception(env, &exception);
2897 }
2898
2899 /**
2900 * @tc.name: stringDecoderEnd001
2901 * @tc.desc: Test the end function by splitting the complete data into two parts.
2902 * @tc.type: FUNC
2903 */
2904 HWTEST_F(NativeEngineTest, stringDecoderEnd001, testing::ext::TestSize.Level0)
2905 {
2906 OHOS::Util::StringDecoder stringDecoder("utf-8");
2907 napi_env env = (napi_env)engine_;
2908 const int arrCount = 2;
2909 size_t byteLength = arrCount;
2910 void* data = nullptr;
2911 napi_value resultBuff = nullptr;
2912 unsigned char arr[arrCount] = {0xE4, 0xBD};
2913 napi_create_arraybuffer(env, byteLength, &data, &resultBuff);
2914 int ret = memcpy_s(data, sizeof(arr), reinterpret_cast<void*>(arr), sizeof(arr));
2915 ASSERT_EQ(0, ret);
2916 napi_value result = nullptr;
2917 napi_create_typedarray(env, napi_int8_array, byteLength, resultBuff, 0, &result);
2918 napi_value testRes = stringDecoder.Write(env, result);
2919 size_t bufferSize = 0;
2920 if (napi_get_value_string_utf8(env, testRes, nullptr, 0, &bufferSize) != napi_ok) {
2921 HILOG_ERROR("can not get arg size");
2922 }
2923 std::string buffer = "";
2924 buffer.reserve(bufferSize);
2925 buffer.resize(bufferSize);
2926 if (napi_get_value_string_utf8(env, testRes, buffer.data(), bufferSize + 1, &bufferSize) != napi_ok) {
2927 HILOG_ERROR("can not get arg value");
2928 }
2929 ASSERT_STREQ("", buffer.c_str());
2930
2931 const int count = 4;
2932 byteLength = count;
2933 data = nullptr;
2934 resultBuff = nullptr;
2935 unsigned char uint8[count] = {0xA0, 0xE5, 0xA5, 0xBD};
2936 napi_create_arraybuffer(env, byteLength, &data, &resultBuff);
2937 ret = memcpy_s(data, sizeof(uint8), reinterpret_cast<void*>(uint8), sizeof(uint8));
2938 ASSERT_EQ(0, ret);
2939 result = nullptr;
2940 napi_create_typedarray(env, napi_int8_array, byteLength, resultBuff, 0, &result);
2941 testRes = stringDecoder.End(env, result);
2942 bufferSize = 0;
2943 if (napi_get_value_string_utf8(env, testRes, nullptr, 0, &bufferSize) != napi_ok) {
2944 HILOG_ERROR("can not get arg size");
2945 }
2946 buffer = "";
2947 buffer.reserve(bufferSize);
2948 buffer.resize(bufferSize);
2949 if (napi_get_value_string_utf8(env, testRes, buffer.data(), bufferSize + 1, &bufferSize) != napi_ok) {
2950 HILOG_ERROR("can not get arg value");
2951 }
2952 ASSERT_STREQ("你好", buffer.c_str());
2953 }
2954
2955 /**
2956 * @tc.name: stringDecoderEnd002
2957 * @tc.desc: Test the end function by splitting the complete data into two parts.
2958 * @tc.type: FUNC
2959 */
2960 HWTEST_F(NativeEngineTest, stringDecoderEnd002, testing::ext::TestSize.Level0)
2961 {
2962 OHOS::Util::StringDecoder stringDecoder("utf-8");
2963 napi_env env = (napi_env)engine_;
2964 const int arrCount = 3;
2965 size_t byteLength = arrCount;
2966 void* data = nullptr;
2967 napi_value resultBuff = nullptr;
2968 unsigned char arr[arrCount] = {0xE4, 0xBD, 0xA0};
2969 napi_create_arraybuffer(env, byteLength, &data, &resultBuff);
2970 int ret = memcpy_s(data, sizeof(arr), reinterpret_cast<void*>(arr), sizeof(arr));
2971 ASSERT_EQ(0, ret);
2972 napi_value result = nullptr;
2973 napi_create_typedarray(env, napi_int8_array, byteLength, resultBuff, 0, &result);
2974 napi_value testRes = stringDecoder.Write(env, result);
2975 size_t bufferSize = 0;
2976 if (napi_get_value_string_utf8(env, testRes, nullptr, 0, &bufferSize) != napi_ok) {
2977 HILOG_ERROR("can not get arg size");
2978 }
2979 std::string buffer = "";
2980 buffer.reserve(bufferSize);
2981 buffer.resize(bufferSize);
2982 if (napi_get_value_string_utf8(env, testRes, buffer.data(), bufferSize + 1, &bufferSize) != napi_ok) {
2983 HILOG_ERROR("can not get arg value");
2984 }
2985 ASSERT_STREQ("你", buffer.c_str());
2986 testRes = stringDecoder.End(env);
2987 bufferSize = 0;
2988 if (napi_get_value_string_utf8(env, testRes, nullptr, 0, &bufferSize) != napi_ok) {
2989 HILOG_ERROR("can not get arg size");
2990 }
2991 buffer = "";
2992 buffer.reserve(bufferSize);
2993 buffer.resize(bufferSize);
2994 if (napi_get_value_string_utf8(env, testRes, buffer.data(), bufferSize + 1, &bufferSize) != napi_ok) {
2995 HILOG_ERROR("can not get arg value");
2996 }
2997 ASSERT_STREQ("", buffer.c_str());
2998 }
2999
3000 /**
3001 * @tc.name: stringDecoderEnd003
3002 * @tc.desc: string decoder end with pending len 0.
3003 * @tc.type: FUNC
3004 */
3005 HWTEST_F(NativeEngineTest, stringDecoderEnd003, testing::ext::TestSize.Level0)
3006 {
3007 OHOS::Util::StringDecoder stringDecoder("utf-8");
3008 napi_env env = (napi_env)engine_;
3009 const int arrCount = 6;
3010 size_t byteLength = arrCount;
3011 void* data = nullptr;
3012 napi_value resultBuff = nullptr;
3013 unsigned char arr[arrCount] = {0xE4, 0xBD, 0xA0, 0xE5, 0xA5, 0xBD};
3014 napi_create_arraybuffer(env, byteLength, &data, &resultBuff);
3015 memcpy_s(data, sizeof(arr), reinterpret_cast<void*>(arr), sizeof(arr));
3016 napi_value result = nullptr;
3017 napi_create_typedarray(env, napi_int8_array, byteLength, resultBuff, 0, &result);
3018 stringDecoder.Write(env, result, false);
3019
3020 napi_value testResEnd = stringDecoder.End(env);
3021 size_t bufferEndSizeEnd = 0;
3022 if (napi_get_value_string_utf8(env, testResEnd, nullptr, 0, &bufferEndSizeEnd) != napi_ok) {
3023 HILOG_ERROR("can not get arg size");
3024 }
3025 std::string bufferEnd = "";
3026 bufferEnd.reserve(bufferEndSizeEnd);
3027 bufferEnd.resize(bufferEndSizeEnd);
3028 if (napi_get_value_string_utf8(
3029 env, testResEnd, bufferEnd.data(), bufferEndSizeEnd + 1, &bufferEndSizeEnd) != napi_ok) {
3030 HILOG_ERROR("can not get arg value");
3031 }
3032 ASSERT_STREQ("", bufferEnd.c_str());
3033 }
3034
3035 /**
3036 * @tc.name: stringDecoderEnd004
3037 * @tc.desc: Test the end function by splitting the complete data into two parts.
3038 * @tc.type: FUNC
3039 */
3040 HWTEST_F(NativeEngineTest, stringDecoderEnd004, testing::ext::TestSize.Level0)
3041 {
3042 OHOS::Util::StringDecoder stringDecoder("utf-8");
3043 napi_env env = (napi_env)engine_;
3044 const int arrCount = 2; // 2:nums of args
3045 size_t byteLength = arrCount;
3046 void* data = nullptr;
3047 napi_value resultBuff = nullptr;
3048 unsigned char arr[arrCount] = {0xE4, 0xBD};
3049 napi_create_arraybuffer(env, byteLength, &data, &resultBuff);
3050 int ret = memcpy_s(data, sizeof(arr), reinterpret_cast<void*>(arr), sizeof(arr));
3051 ASSERT_EQ(0, ret);
3052 napi_value res = nullptr;
3053 napi_create_typedarray(env, napi_int8_array, byteLength, resultBuff, 0, &res);
3054 napi_value testRes = stringDecoder.Write(env, res);
3055 size_t bufferSize = 0;
3056 if (napi_get_value_string_utf8(env, testRes, nullptr, 0, &bufferSize) != napi_ok) {
3057 HILOG_ERROR("can not get arg size");
3058 }
3059 std::string buffer = "";
3060 buffer.reserve(bufferSize);
3061 buffer.resize(bufferSize);
3062 if (napi_get_value_string_utf8(env, testRes, buffer.data(), bufferSize + 1, &bufferSize) != napi_ok) {
3063 HILOG_ERROR("can not get arg value");
3064 }
3065 ASSERT_STREQ("", buffer.c_str());
3066 napi_value result = stringDecoder.End(env);
3067 ASSERT_TRUE(result == nullptr);
3068 }
3069
3070 /**
3071 * @tc.name: charEncodeAchieves001
3072 * @tc.desc: char encode achieves with throw error.
3073 * @tc.type: FUNC
3074 */
3075 HWTEST_F(NativeEngineTest, charEncodeAchieves001, testing::ext::TestSize.Level0)
3076 {
3077 napi_env env = (napi_env)engine_;
3078 OHOS::Util::EncodeInfo* stdEncodeInfo = nullptr;
3079 stdEncodeInfo = new OHOS::Util::EncodeInfo();
3080 stdEncodeInfo->slength = 0;
3081 unsigned char* res = OHOS::Util::EncodeAchieves(env, stdEncodeInfo);
3082 ASSERT_EQ(nullptr, res);
3083
3084 OHOS::Util::EncodeInfo* stdEncodeInfo1 = nullptr;
3085 unsigned char arr[] = {0xE4, 0xBD, 0xA0, 0xE5, 0xA5, 0xBD};
3086 stdEncodeInfo1 = new OHOS::Util::EncodeInfo();
3087 stdEncodeInfo1->sinputEncode = arr;
3088 stdEncodeInfo1->slength = 1;
3089 stdEncodeInfo1->valueType = OHOS::Util::Type::BASIC_URL_SAFE;
3090 unsigned char* res1 = OHOS::Util::EncodeAchieves(env, stdEncodeInfo1);
3091 ASSERT_EQ(0x35, static_cast<unsigned char>(*res1));
3092
3093 napi_value exception;
3094 napi_get_and_clear_last_exception(env, &exception);
3095 }
3096
3097 /* @tc.name: testDecode001
3098 * @tc.desc: Test for abnormal situations in the decode function
3099 * @tc.type: FUNC
3100 */
3101 HWTEST_F(NativeEngineTest, testDecode001, testing::ext::TestSize.Level0)
3102 {
3103 HILOG_INFO("testDecode001 start");
3104 napi_env env = (napi_env)engine_;
3105 OHOS::Util::Base64 base64;
3106
3107 unsigned char input[4] = {99, 122, 69, 122};
3108 napi_value arrayBuffer = nullptr;
3109 size_t arrayBufferSize = 4;
3110 void* data = nullptr;
3111 napi_create_arraybuffer(env, arrayBufferSize, &data, &arrayBuffer);
3112 int ret = memcpy_s(data, sizeof(input), reinterpret_cast<void*>(input), sizeof(input));
3113 ASSERT_EQ(0, ret);
3114 napi_value src = nullptr;
3115 napi_create_typedarray(env, napi_int8_array, arrayBufferSize, arrayBuffer, 0, &src);
3116 napi_value result = base64.Decode(env, src, OHOS::Util::Type::BASIC);
3117 ASSERT_TRUE(result == nullptr);
3118 }
3119
3120 /* @tc.name: testDecode002
3121 * @tc.desc: Test for abnormal situations in the decode function
3122 * @tc.type: FUNC
3123 */
3124 HWTEST_F(NativeEngineTest, testDecode002, testing::ext::TestSize.Level0)
3125 {
3126 HILOG_INFO("testDecode002 start");
3127 napi_env env = (napi_env)engine_;
3128 OHOS::Util::Base64 base64;
3129 napi_value src = nullptr;
3130 napi_create_int32(env, 9, &src);
3131 napi_value result = base64.Decode(env, src, OHOS::Util::Type::BASIC);
3132 ASSERT_TRUE(result == nullptr);
3133 }
3134
3135 /* @tc.name: testDecodeSync001
3136 * @tc.desc: Test for abnormal situations in the DecodeSync function
3137 * @tc.type: FUNC
3138 */
3139 HWTEST_F(NativeEngineTest, testDecodeSync001, testing::ext::TestSize.Level0)
3140 {
3141 HILOG_INFO("testDecodeSync001 start");
3142 napi_env env = (napi_env)engine_;
3143 OHOS::Util::Base64 base64;
3144
3145 napi_value src = nullptr;
3146 napi_create_int32(env, 9, &src);
3147 napi_value result = base64.DecodeSync(env, src, OHOS::Util::Type::BASIC);
3148 ASSERT_TRUE(result == nullptr);
3149 }
3150
3151 /**
3152 * @tc.name: charDecodeAchieves001
3153 * @tc.desc: char dencode achieves with throw error.
3154 * @tc.type: FUNC
3155 */
3156 HWTEST_F(NativeEngineTest, charDencodeAchieves001, testing::ext::TestSize.Level0)
3157 {
3158 napi_env env = (napi_env)engine_;
3159 OHOS::Util::DecodeInfo* stdDecodeInfo2 = nullptr;
3160 char arr2[] = {0xE4, 0xBD, 0xA0, 0xE5, 0xA5, 0xBD};
3161 stdDecodeInfo2 = new OHOS::Util::DecodeInfo();
3162 stdDecodeInfo2->sinputDecode = arr2;
3163 stdDecodeInfo2->slength = 2;
3164 stdDecodeInfo2->valueType = OHOS::Util::Type::BASIC_URL_SAFE;
3165 unsigned char* res2 = OHOS::Util::DecodeAchieves(env, stdDecodeInfo2);
3166 ASSERT_EQ(0, static_cast<const char>(*res2));
3167
3168 OHOS::Util::DecodeInfo* stdDecodeInfo3 = nullptr;
3169 char arr3[] = {0xE4, 0xBD, 0xA0, 0xE5, 0xA5, 0xBD};
3170 stdDecodeInfo3 = new OHOS::Util::DecodeInfo();
3171 stdDecodeInfo3->sinputDecode = arr3;
3172 stdDecodeInfo3->slength = 3;
3173 stdDecodeInfo3->valueType = OHOS::Util::Type::BASIC_URL_SAFE;
3174 unsigned char* res3 = OHOS::Util::DecodeAchieves(env, stdDecodeInfo3);
3175 ASSERT_EQ(0, static_cast<unsigned char>(*res3));
3176
3177 napi_value exception;
3178 napi_get_and_clear_last_exception(env, &exception);
3179 }
3180
3181 /**
3182 * @tc.name: DecodeToStringNoStream001
3183 * @tc.desc: Testing the ignoreBOM of TextDecoder.
3184 * @tc.type: FUNC
3185 */
3186 HWTEST_F(NativeEngineTest, DecodeToStringNoStream001, testing::ext::TestSize.Level0)
3187 {
3188 HILOG_INFO("DecodeToStringNoStream start");
3189 napi_env env = (napi_env)engine_;
3190 int32_t flags = static_cast<int>(OHOS::Util::TextDecoder::ConverterFlags::FATAL_FLG) |
3191 static_cast<int>(OHOS::Util::TextDecoder::ConverterFlags::IGNORE_BOM_FLG);
3192 std::string encoding = "utf-8";
3193 OHOS::Util::TextDecoder textDecoder(encoding, flags);
3194 bool iflag = false;
3195 size_t byteLength = 6;
3196 void* data = nullptr;
3197 napi_value resultBuff = nullptr;
3198 napi_create_arraybuffer(env, byteLength, &data, &resultBuff);
3199 unsigned char arr[8] = {0xEF, 0xBB, 0xBF, 0x41, 0x42, 0x43};
3200 int ret = memcpy_s(data, sizeof(arr), reinterpret_cast<void*>(arr), sizeof(arr));
3201 ASSERT_EQ(0, ret);
3202 napi_value result = nullptr;
3203 napi_create_typedarray(env, napi_int8_array, byteLength, resultBuff, 0, &result);
3204 textDecoder.DecodeToString(env, result, iflag);
3205 }
3206
3207 /**
3208 * @tc.name: DecodeToStringWithStream001
3209 * @tc.desc: Testing the ignoreBOM of TextDecoder.
3210 * @tc.type: FUNC
3211 */
3212 HWTEST_F(NativeEngineTest, DecodeToStringWithStream001, testing::ext::TestSize.Level0)
3213 {
3214 HILOG_INFO("DecodeToStringWithStream start");
3215 napi_env env = (napi_env)engine_;
3216 int32_t flags = static_cast<int>(OHOS::Util::TextDecoder::ConverterFlags::FATAL_FLG) |
3217 static_cast<int>(OHOS::Util::TextDecoder::ConverterFlags::IGNORE_BOM_FLG);
3218 std::string encoding = "utf-8";
3219 OHOS::Util::TextDecoder textDecoder(encoding, flags);
3220 bool iflag = true;
3221 size_t byteLength = 6;
3222 void* data = nullptr;
3223 napi_value resultBuff = nullptr;
3224 napi_create_arraybuffer(env, byteLength, &data, &resultBuff);
3225 unsigned char arr[8] = {0xEF, 0xBB, 0xBF, 0x41, 0x42, 0x43};
3226 int ret = memcpy_s(data, sizeof(arr), reinterpret_cast<void*>(arr), sizeof(arr));
3227 ASSERT_EQ(0, ret);
3228 napi_value result = nullptr;
3229 napi_create_typedarray(env, napi_int8_array, byteLength, resultBuff, 0, &result);
3230 textDecoder.DecodeToString(env, result, iflag);
3231 }
3232
3233 /**
3234 * @tc.name: decoderGBK
3235 * @tc.desc: Testing the decoding result of GBK data with BOM.
3236 * @tc.type: FUNC
3237 */
3238 HWTEST_F(NativeEngineTest, decoderGBK001, testing::ext::TestSize.Level0)
3239 {
3240 HILOG_INFO("decoderGBK start");
3241 napi_env env = (napi_env)engine_;
3242 int32_t flags = 0;
3243 std::string encoding = "GBK";
3244 OHOS::Util::TextDecoder textDecoder(encoding, flags);
3245 bool iflag = true;
3246 size_t byteLength = 4;
3247 void* data = nullptr;
3248 napi_value resultBuff = nullptr;
3249 napi_create_arraybuffer(env, byteLength, &data, &resultBuff);
3250 unsigned char arr[4] = {196, 227, 186, 195};
3251 int ret = memcpy_s(data, sizeof(arr), reinterpret_cast<void*>(arr), sizeof(arr));
3252 ASSERT_EQ(0, ret);
3253 napi_value result = nullptr;
3254 napi_create_typedarray(env, napi_int8_array, byteLength, resultBuff, 0, &result);
3255 napi_value testString = textDecoder.DecodeToString(env, result, iflag);
3256 size_t bufferSize = 0;
3257 napi_get_value_string_utf16(env, testString, nullptr, 0, &bufferSize);
3258 size_t length = 0;
3259 char16_t* ch = nullptr;
3260 if (bufferSize > 0) {
3261 ch = new char16_t[bufferSize + 1]();
3262 napi_get_value_string_utf16(env, testString, ch, bufferSize + 1, &length);
3263 }
3264 std::string str =
3265 std::wstring_convert<std::codecvt_utf8_utf16<char16_t>, char16_t> {}.to_bytes(ch);
3266 ASSERT_EQ(str, "你好");
3267 if (ch != nullptr) {
3268 delete []ch;
3269 ch = nullptr;
3270 }
3271 }
3272
3273 /**
3274 * @tc.name: decoderbig5
3275 * @tc.desc: Testing the decoding result of big5 data with BOM.
3276 * @tc.type: FUNC
3277 */
3278 HWTEST_F(NativeEngineTest, decoderbig5001, testing::ext::TestSize.Level0)
3279 {
3280 HILOG_INFO("decoderbig5 start");
3281 napi_env env = (napi_env)engine_;
3282 int32_t flags = 0;
3283 std::string encoding = "big5";
3284 OHOS::Util::TextDecoder textDecoder(encoding, flags);
3285 bool iflag = true;
3286 size_t byteLength = 6;
3287 void* data = nullptr;
3288 napi_value resultBuff = nullptr;
3289 napi_create_arraybuffer(env, byteLength, &data, &resultBuff);
3290 unsigned char arr[6] = {166, 173, 164, 87, 166, 110};
3291 int ret = memcpy_s(data, sizeof(arr), reinterpret_cast<void*>(arr), sizeof(arr));
3292 ASSERT_EQ(0, ret);
3293 napi_value result = nullptr;
3294 napi_create_typedarray(env, napi_int8_array, byteLength, resultBuff, 0, &result);
3295 napi_value testString = textDecoder.DecodeToString(env, result, iflag);
3296 size_t bufferSize = 0;
3297 napi_get_value_string_utf16(env, testString, nullptr, 0, &bufferSize);
3298 size_t length = 0;
3299 char16_t* ch = nullptr;
3300 if (bufferSize > 0) {
3301 ch = new char16_t[bufferSize + 1]();
3302 napi_get_value_string_utf16(env, testString, ch, bufferSize + 1, &length);
3303 }
3304 std::string str =
3305 std::wstring_convert<std::codecvt_utf8_utf16<char16_t>, char16_t> {}.to_bytes(ch);
3306 ASSERT_EQ(str, "早上好");
3307 if (ch != nullptr) {
3308 delete []ch;
3309 ch = nullptr;
3310 }
3311 }
3312
3313 /**
3314 * @tc.name: decodergb18030
3315 * @tc.desc: Testing the decoding result of gb18030 data with BOM.
3316 * @tc.type: FUNC
3317 */
3318 HWTEST_F(NativeEngineTest, decodergb18030, testing::ext::TestSize.Level0)
3319 {
3320 HILOG_INFO("decodergb18030 start");
3321 napi_env env = (napi_env)engine_;
3322 int32_t flags = 0;
3323 std::string encoding = "gb18030";
3324 OHOS::Util::TextDecoder textDecoder(encoding, flags);
3325 bool iflag = true;
3326 size_t byteLength = 4;
3327 void* data = nullptr;
3328 napi_value resultBuff = nullptr;
3329 napi_create_arraybuffer(env, byteLength, &data, &resultBuff);
3330 unsigned char arr[4] = {196, 227, 186, 195};
3331 int ret = memcpy_s(data, sizeof(arr), reinterpret_cast<void*>(arr), sizeof(arr));
3332 ASSERT_EQ(0, ret);
3333 napi_value result = nullptr;
3334 napi_create_typedarray(env, napi_int8_array, byteLength, resultBuff, 0, &result);
3335 napi_value testString = textDecoder.DecodeToString(env, result, iflag);
3336 size_t bufferSize = 0;
3337 napi_get_value_string_utf16(env, testString, nullptr, 0, &bufferSize);
3338 size_t length = 0;
3339 char16_t* ch = nullptr;
3340 if (bufferSize > 0) {
3341 ch = new char16_t[bufferSize + 1]();
3342 napi_get_value_string_utf16(env, testString, ch, bufferSize + 1, &length);
3343 }
3344 std::string str =
3345 std::wstring_convert<std::codecvt_utf8_utf16<char16_t>, char16_t> {}.to_bytes(ch);
3346 ASSERT_EQ(str, "你好");
3347 if (ch != nullptr) {
3348 delete []ch;
3349 ch = nullptr;
3350 }
3351 }
3352
3353 /**
3354 * @tc.name: decodergbk
3355 * @tc.desc: Testing the decoding result of gbk data with BOM.
3356 * @tc.type: FUNC
3357 */
3358 HWTEST_F(NativeEngineTest, decodergbk, testing::ext::TestSize.Level0)
3359 {
3360 HILOG_INFO("decodergbk start");
3361 napi_env env = (napi_env)engine_;
3362 int32_t flags = 0;
3363 std::string encoding = "gbk";
3364 OHOS::Util::TextDecoder textDecoder(encoding, flags);
3365 bool iflag = true;
3366 size_t byteLength = 4;
3367 void* data = nullptr;
3368 napi_value resultBuff = nullptr;
3369 napi_create_arraybuffer(env, byteLength, &data, &resultBuff);
3370 unsigned char arr[4] = {196, 227, 186, 195};
3371 int ret = memcpy_s(data, sizeof(arr), reinterpret_cast<void*>(arr), sizeof(arr));
3372 ASSERT_EQ(0, ret);
3373 napi_value result = nullptr;
3374 napi_create_typedarray(env, napi_int8_array, byteLength, resultBuff, 0, &result);
3375 napi_value testString = textDecoder.DecodeToString(env, result, iflag);
3376 size_t bufferSize = 0;
3377 napi_get_value_string_utf16(env, testString, nullptr, 0, &bufferSize);
3378 size_t length = 0;
3379 char16_t* ch = nullptr;
3380 if (bufferSize > 0) {
3381 ch = new char16_t[bufferSize + 1]();
3382 napi_get_value_string_utf16(env, testString, ch, bufferSize + 1, &length);
3383 }
3384 std::string str =
3385 std::wstring_convert<std::codecvt_utf8_utf16<char16_t>, char16_t> {}.to_bytes(ch);
3386 ASSERT_EQ(str, "你好");
3387 if (ch != nullptr) {
3388 delete []ch;
3389 ch = nullptr;
3390 }
3391 }
3392
3393 /**
3394 * @tc.name: decoderwindows1257
3395 * @tc.desc: Testing the decoding result of windows-1257 data with BOM.
3396 * @tc.type: FUNC
3397 */
3398 HWTEST_F(NativeEngineTest, decoderwindows1257, testing::ext::TestSize.Level0)
3399 {
3400 HILOG_INFO("decoderwindows1257 start");
3401 napi_env env = (napi_env)engine_;
3402 int32_t flags = 0;
3403 std::string str = "windows-1257";
3404 OHOS::Util::TextDecoder textDecoder(str, flags);
3405 bool iflag = false;
3406 size_t byteLength = 4;
3407 void* data = nullptr;
3408 napi_value resultBuff = nullptr;
3409 napi_create_arraybuffer(env, byteLength, &data, &resultBuff);
3410 unsigned char arr[4] = {84, 101, 114, 101};
3411 int ret = memcpy_s(data, sizeof(arr), reinterpret_cast<void*>(arr), sizeof(arr));
3412 ASSERT_EQ(0, ret);
3413 napi_value result2 = nullptr;
3414 napi_create_typedarray(env, napi_int8_array, byteLength, resultBuff, 0, &result2);
3415 napi_value testString = textDecoder.DecodeToString(env, result2, iflag);
3416 size_t bufferSize = 0;
3417 napi_get_value_string_utf8(env, testString, nullptr, 0, &bufferSize);
3418 size_t length = 0;
3419 char* ch = nullptr;
3420 if (bufferSize > 0) {
3421 ch = new char[bufferSize + 1]();
3422 napi_get_value_string_utf8(env, testString, ch, bufferSize + 1, &length);
3423 }
3424 ASSERT_STREQ("Tere", ch);
3425 if (ch != nullptr) {
3426 delete []ch;
3427 ch = nullptr;
3428 }
3429 }
3430
3431 /**
3432 * @tc.name: decoderwindows874
3433 * @tc.desc: Testing the decoding result of windows-874 data with BOM.
3434 * @tc.type: FUNC
3435 */
3436 HWTEST_F(NativeEngineTest, decoderwindows874, testing::ext::TestSize.Level0)
3437 {
3438 HILOG_INFO("decoderwindows874 start");
3439 napi_env env = (napi_env)engine_;
3440 int32_t flags = 0;
3441 std::string str = "windows-874";
3442 OHOS::Util::TextDecoder textDecoder(str, flags);
3443 bool iflag = false;
3444 size_t byteLength = 3;
3445 void* data = nullptr;
3446 napi_value resultBuff = nullptr;
3447 napi_create_arraybuffer(env, byteLength, &data, &resultBuff);
3448 unsigned char arr[3] = {0x61, 0x62, 0x63};
3449 int ret = memcpy_s(data, sizeof(arr), reinterpret_cast<void*>(arr), sizeof(arr));
3450 ASSERT_EQ(0, ret);
3451 napi_value result2 = nullptr;
3452 napi_create_typedarray(env, napi_int8_array, byteLength, resultBuff, 0, &result2);
3453 napi_value testString = textDecoder.DecodeToString(env, result2, iflag);
3454 size_t bufferSize = 0;
3455 napi_get_value_string_utf8(env, testString, nullptr, 0, &bufferSize);
3456 size_t length = 0;
3457 char* ch = nullptr;
3458 if (bufferSize > 0) {
3459 ch = new char[bufferSize + 1]();
3460 napi_get_value_string_utf8(env, testString, ch, bufferSize + 1, &length);
3461 }
3462 ASSERT_STREQ("abc", ch);
3463 if (ch != nullptr) {
3464 delete []ch;
3465 ch = nullptr;
3466 }
3467 }
3468
3469 /**
3470 * @tc.name: decodermacintosh
3471 * @tc.desc: Testing the decoding result of macintosh data with BOM.
3472 * @tc.type: FUNC
3473 */
3474 HWTEST_F(NativeEngineTest, decodermacintosh, testing::ext::TestSize.Level0)
3475 {
3476 HILOG_INFO("decodermacintosh start");
3477 napi_env env = (napi_env)engine_;
3478 int32_t flags = 0;
3479 std::string str = "macintosh";
3480 OHOS::Util::TextDecoder textDecoder(str, flags);
3481 bool iflag = false;
3482 size_t byteLength = 3;
3483 void* data = nullptr;
3484 napi_value resultBuff = nullptr;
3485 napi_create_arraybuffer(env, byteLength, &data, &resultBuff);
3486 unsigned char arr[3] = {0x61, 0x62, 0x63};
3487 int ret = memcpy_s(data, sizeof(arr), reinterpret_cast<void*>(arr), sizeof(arr));
3488 ASSERT_EQ(0, ret);
3489 napi_value result2 = nullptr;
3490 napi_create_typedarray(env, napi_int8_array, byteLength, resultBuff, 0, &result2);
3491 napi_value testString = textDecoder.DecodeToString(env, result2, iflag);
3492 size_t bufferSize = 0;
3493 napi_get_value_string_utf8(env, testString, nullptr, 0, &bufferSize);
3494 size_t length = 0;
3495 char* ch = nullptr;
3496 if (bufferSize > 0) {
3497 ch = new char[bufferSize + 1]();
3498 napi_get_value_string_utf8(env, testString, ch, bufferSize + 1, &length);
3499 }
3500 ASSERT_STREQ("abc", ch);
3501 if (ch != nullptr) {
3502 delete []ch;
3503 ch = nullptr;
3504 }
3505 }
3506
3507 /**
3508 * @tc.name: decoderkoi8u
3509 * @tc.desc: Testing the decoding result of koi8-u data with BOM.
3510 * @tc.type: FUNC
3511 */
3512 HWTEST_F(NativeEngineTest, decoderkoi8u, testing::ext::TestSize.Level0)
3513 {
3514 HILOG_INFO("decoderkoi8u start");
3515 napi_env env = (napi_env)engine_;
3516 int32_t flags = 0;
3517 std::string str = "koi8-u";
3518 OHOS::Util::TextDecoder textDecoder(str, flags);
3519 bool iflag = false;
3520 size_t byteLength = 3;
3521 void* data = nullptr;
3522 napi_value resultBuff = nullptr;
3523 napi_create_arraybuffer(env, byteLength, &data, &resultBuff);
3524 unsigned char arr[3] = {0x61, 0x62, 0x63};
3525 int ret = memcpy_s(data, sizeof(arr), reinterpret_cast<void*>(arr), sizeof(arr));
3526 ASSERT_EQ(0, ret);
3527 napi_value result2 = nullptr;
3528 napi_create_typedarray(env, napi_int8_array, byteLength, resultBuff, 0, &result2);
3529 napi_value testString = textDecoder.DecodeToString(env, result2, iflag);
3530 size_t bufferSize = 0;
3531 napi_get_value_string_utf8(env, testString, nullptr, 0, &bufferSize);
3532 size_t length = 0;
3533 char* ch = nullptr;
3534 if (bufferSize > 0) {
3535 ch = new char[bufferSize + 1]();
3536 napi_get_value_string_utf8(env, testString, ch, bufferSize + 1, &length);
3537 }
3538 ASSERT_STREQ("abc", ch);
3539 if (ch != nullptr) {
3540 delete []ch;
3541 ch = nullptr;
3542 }
3543 }
3544
3545 /**
3546 * @tc.name: decoderiso885915
3547 * @tc.desc: Testing the decoding result of iso-8859-15 data with BOM.
3548 * @tc.type: FUNC
3549 */
3550 HWTEST_F(NativeEngineTest, decoderiso885915, testing::ext::TestSize.Level0)
3551 {
3552 HILOG_INFO("decoderiso885915 start");
3553 napi_env env = (napi_env)engine_;
3554 int32_t flags = 0;
3555 std::string str = "iso-8859-15";
3556 OHOS::Util::TextDecoder textDecoder(str, flags);
3557 bool iflag = false;
3558 size_t byteLength = 4;
3559 void* data = nullptr;
3560 napi_value resultBuff = nullptr;
3561 napi_create_arraybuffer(env, byteLength, &data, &resultBuff);
3562 unsigned char arr[4] = {72, 111, 108, 97};
3563 int ret = memcpy_s(data, sizeof(arr), reinterpret_cast<void*>(arr), sizeof(arr));
3564 ASSERT_EQ(0, ret);
3565 napi_value result2 = nullptr;
3566 napi_create_typedarray(env, napi_int8_array, byteLength, resultBuff, 0, &result2);
3567 napi_value testString = textDecoder.DecodeToString(env, result2, iflag);
3568 size_t bufferSize = 0;
3569 napi_get_value_string_utf8(env, testString, nullptr, 0, &bufferSize);
3570 size_t length = 0;
3571 char* ch = nullptr;
3572 if (bufferSize > 0) {
3573 ch = new char[bufferSize + 1]();
3574 napi_get_value_string_utf8(env, testString, ch, bufferSize + 1, &length);
3575 }
3576 ASSERT_STREQ("Hola", ch);
3577 if (ch != nullptr) {
3578 delete []ch;
3579 ch = nullptr;
3580 }
3581 }
3582
3583 /**
3584 * @tc.name: decoderwindows1256
3585 * @tc.desc: Testing the decoding result of windows-1256 data with BOM.
3586 * @tc.type: FUNC
3587 */
3588 HWTEST_F(NativeEngineTest, decoderwindows1256, testing::ext::TestSize.Level0)
3589 {
3590 HILOG_INFO("decoderwindows1256 start");
3591 napi_env env = (napi_env)engine_;
3592 int32_t flags = 0;
3593 std::string str = "windows-1256";
3594 OHOS::Util::TextDecoder textDecoder(str, flags);
3595 bool iflag = false;
3596 size_t byteLength = 7;
3597 void* data = nullptr;
3598 napi_value resultBuff = nullptr;
3599 napi_create_arraybuffer(env, byteLength, &data, &resultBuff);
3600 unsigned char arr[7] = {77, 97, 114, 104, 97, 98, 97};
3601 int ret = memcpy_s(data, sizeof(arr), reinterpret_cast<void*>(arr), sizeof(arr));
3602 ASSERT_EQ(0, ret);
3603 napi_value result2 = nullptr;
3604 napi_create_typedarray(env, napi_int8_array, byteLength, resultBuff, 0, &result2);
3605 napi_value testString = textDecoder.DecodeToString(env, result2, iflag);
3606 size_t bufferSize = 0;
3607 napi_get_value_string_utf8(env, testString, nullptr, 0, &bufferSize);
3608 size_t length = 0;
3609 char* ch = nullptr;
3610 if (bufferSize > 0) {
3611 ch = new char[bufferSize + 1]();
3612 napi_get_value_string_utf8(env, testString, ch, bufferSize + 1, &length);
3613 }
3614 ASSERT_STREQ("Marhaba", ch);
3615 if (ch != nullptr) {
3616 delete []ch;
3617 ch = nullptr;
3618 }
3619 }
3620
3621 /**
3622 * @tc.name: decoderwindows1255
3623 * @tc.desc: Testing the decoding result of windows-1255 data with BOM.
3624 * @tc.type: FUNC
3625 */
3626 HWTEST_F(NativeEngineTest, decoderwindows1255, testing::ext::TestSize.Level0)
3627 {
3628 HILOG_INFO("decoderwindows1255 start");
3629 napi_env env = (napi_env)engine_;
3630 int32_t flags = 0;
3631 std::string str = "windows-1255";
3632 OHOS::Util::TextDecoder textDecoder(str, flags);
3633 bool iflag = false;
3634 size_t byteLength = 7;
3635 void* data = nullptr;
3636 napi_value resultBuff = nullptr;
3637 napi_create_arraybuffer(env, byteLength, &data, &resultBuff);
3638 unsigned char arr[7] = {77, 101, 114, 104, 97, 98, 97};
3639 int ret = memcpy_s(data, sizeof(arr), reinterpret_cast<void*>(arr), sizeof(arr));
3640 ASSERT_EQ(0, ret);
3641 napi_value result2 = nullptr;
3642 napi_create_typedarray(env, napi_int8_array, byteLength, resultBuff, 0, &result2);
3643 napi_value testString = textDecoder.DecodeToString(env, result2, iflag);
3644 size_t bufferSize = 0;
3645 napi_get_value_string_utf8(env, testString, nullptr, 0, &bufferSize);
3646 size_t length = 0;
3647 char* ch = nullptr;
3648 if (bufferSize > 0) {
3649 ch = new char[bufferSize + 1]();
3650 napi_get_value_string_utf8(env, testString, ch, bufferSize + 1, &length);
3651 }
3652 ASSERT_STREQ("Merhaba", ch);
3653 if (ch != nullptr) {
3654 delete []ch;
3655 ch = nullptr;
3656 }
3657 }
3658
3659 /**
3660 * @tc.name: textEncodeIntoTest008
3661 * @tc.desc: Test returns a dictionary object indicating the progress of the encoding
3662 * @tc.type: FUNC
3663 */
3664 HWTEST_F(NativeEngineTest, textEncodeIntoTest008, testing::ext::TestSize.Level0)
3665 {
3666 HILOG_INFO("textEncodeIntoTest008 start");
3667 napi_env env = (napi_env)engine_;
3668 OHOS::Util::TextEncoder textEncoder("utf-8");
3669 std::string input = "";
3670 napi_value src = nullptr;
3671 napi_create_string_utf8(env, input.c_str(), input.size(), &src);
3672 napi_value arrayBuffer = nullptr;
3673 void* arrayBufferPtr = nullptr;
3674 size_t arrayBufferSize = 20;
3675 napi_create_arraybuffer(env, arrayBufferSize, &arrayBufferPtr, &arrayBuffer);
3676 napi_value dest = nullptr;
3677 napi_create_typedarray(env, napi_int8_array, arrayBufferSize, arrayBuffer, 0, &dest);
3678 napi_value result = textEncoder.EncodeInto(env, src, dest);
3679 napi_value read = nullptr;
3680 napi_get_named_property(env, result, "read", &read);
3681 uint32_t resRead = 0;
3682 napi_get_value_uint32(env, read, &resRead);
3683 napi_value written = nullptr;
3684 napi_get_named_property(env, result, "written", &written);
3685 uint32_t resWritten = 0;
3686 napi_get_value_uint32(env, written, &resWritten);
3687 ASSERT_EQ(resRead, static_cast<uint32_t>(2));
3688 ASSERT_EQ(resWritten, static_cast<uint32_t>(4));
3689 }
3690
3691 /**
3692 * @tc.name: textEncodeIntoTest009
3693 * @tc.desc: Test returns a dictionary object indicating the progress of the encoding
3694 * @tc.type: FUNC
3695 */
3696 HWTEST_F(NativeEngineTest, textEncodeIntoTest009, testing::ext::TestSize.Level0)
3697 {
3698 HILOG_INFO("textEncodeIntoTest009 start");
3699 napi_env env = (napi_env)engine_;
3700 OHOS::Util::TextEncoder textEncoder("utf-8");
3701 std::string input = "a";
3702 napi_value src = nullptr;
3703 napi_create_string_utf8(env, input.c_str(), input.size(), &src);
3704 napi_value arrayBuffer = nullptr;
3705 void* arrayBufferPtr = nullptr;
3706 size_t arrayBufferSize = 20;
3707 napi_create_arraybuffer(env, arrayBufferSize, &arrayBufferPtr, &arrayBuffer);
3708 napi_value dest = nullptr;
3709 napi_create_typedarray(env, napi_int8_array, arrayBufferSize, arrayBuffer, 0, &dest);
3710 napi_value result = textEncoder.EncodeInto(env, src, dest);
3711 napi_value read = nullptr;
3712 napi_get_named_property(env, result, "read", &read);
3713 uint32_t resRead = 0;
3714 napi_get_value_uint32(env, read, &resRead);
3715 napi_value written = nullptr;
3716 napi_get_named_property(env, result, "written", &written);
3717 uint32_t resWritten = 0;
3718 napi_get_value_uint32(env, written, &resWritten);
3719 ASSERT_EQ(resRead, static_cast<uint32_t>(3));
3720 ASSERT_EQ(resWritten, static_cast<uint32_t>(5));
3721 }
3722
3723 /**
3724 * @tc.name: textEncodeIntoTest010
3725 * @tc.desc: Test returns a dictionary object indicating the progress of the encoding
3726 * @tc.type: FUNC
3727 */
3728 HWTEST_F(NativeEngineTest, textEncodeIntoTest010, testing::ext::TestSize.Level0)
3729 {
3730 HILOG_INFO("textEncodeIntoTest010 start");
3731 napi_env env = (napi_env)engine_;
3732 OHOS::Util::TextEncoder textEncoder("utf-8");
3733 std::string input = "a";
3734 napi_value src = nullptr;
3735 napi_create_string_utf8(env, input.c_str(), input.size(), &src);
3736 napi_value arrayBuffer = nullptr;
3737 void* arrayBufferPtr = nullptr;
3738 size_t arrayBufferSize = 20;
3739 napi_create_arraybuffer(env, arrayBufferSize, &arrayBufferPtr, &arrayBuffer);
3740 napi_value dest = nullptr;
3741 napi_create_typedarray(env, napi_int8_array, arrayBufferSize, arrayBuffer, 0, &dest);
3742 napi_value result = textEncoder.EncodeInto(env, src, dest);
3743 napi_value read = nullptr;
3744 napi_get_named_property(env, result, "read", &read);
3745 uint32_t resRead = 0;
3746 napi_get_value_uint32(env, read, &resRead);
3747 napi_value written = nullptr;
3748 napi_get_named_property(env, result, "written", &written);
3749 uint32_t resWritten = 0;
3750 napi_get_value_uint32(env, written, &resWritten);
3751 ASSERT_EQ(resRead, static_cast<uint32_t>(5));
3752 ASSERT_EQ(resWritten, static_cast<uint32_t>(9));
3753 }
3754
3755 /**
3756 * @tc.name: textEncodeIntoTest011
3757 * @tc.desc: Test returns a dictionary object indicating the progress of the encoding
3758 * @tc.type: FUNC
3759 */
3760 HWTEST_F(NativeEngineTest, textEncodeIntoTest011, testing::ext::TestSize.Level0)
3761 {
3762 HILOG_INFO("textEncodeIntoTest011 start");
3763 napi_env env = (napi_env)engine_;
3764 OHOS::Util::TextEncoder textEncoder("utf-8");
3765 std::string input = "中a";
3766 napi_value src = nullptr;
3767 napi_create_string_utf8(env, input.c_str(), input.size(), &src);
3768 napi_value arrayBuffer = nullptr;
3769 void* arrayBufferPtr = nullptr;
3770 size_t arrayBufferSize = 20;
3771 napi_create_arraybuffer(env, arrayBufferSize, &arrayBufferPtr, &arrayBuffer);
3772 napi_value dest = nullptr;
3773 napi_create_typedarray(env, napi_int8_array, arrayBufferSize, arrayBuffer, 0, &dest);
3774 napi_value result = textEncoder.EncodeInto(env, src, dest);
3775 napi_value read = nullptr;
3776 napi_get_named_property(env, result, "read", &read);
3777 uint32_t resRead = 0;
3778 napi_get_value_uint32(env, read, &resRead);
3779 napi_value written = nullptr;
3780 napi_get_named_property(env, result, "written", &written);
3781 uint32_t resWritten = 0;
3782 napi_get_value_uint32(env, written, &resWritten);
3783 ASSERT_EQ(resRead, static_cast<uint32_t>(2));
3784 ASSERT_EQ(resWritten, static_cast<uint32_t>(4));
3785 }
3786
3787 /**
3788 * @tc.name: textEncodeIntoTest012
3789 * @tc.desc: Test returns a dictionary object indicating the progress of the encoding
3790 * @tc.type: FUNC
3791 */
3792 HWTEST_F(NativeEngineTest, textEncodeIntoTest012, testing::ext::TestSize.Level0)
3793 {
3794 HILOG_INFO("textEncodeIntoTest012 start");
3795 napi_env env = (napi_env)engine_;
3796 OHOS::Util::TextEncoder textEncoder("utf-8");
3797 std::string input = "中aÿ";
3798 napi_value src = nullptr;
3799 napi_create_string_utf8(env, input.c_str(), input.size(), &src);
3800 napi_value arrayBuffer = nullptr;
3801 void* arrayBufferPtr = nullptr;
3802 size_t arrayBufferSize = 20;
3803 napi_create_arraybuffer(env, arrayBufferSize, &arrayBufferPtr, &arrayBuffer);
3804 napi_value dest = nullptr;
3805 napi_create_typedarray(env, napi_int8_array, arrayBufferSize, arrayBuffer, 0, &dest);
3806 napi_value result = textEncoder.EncodeInto(env, src, dest);
3807 napi_value read = nullptr;
3808 napi_get_named_property(env, result, "read", &read);
3809 uint32_t resRead = 0;
3810 napi_get_value_uint32(env, read, &resRead);
3811 napi_value written = nullptr;
3812 napi_get_named_property(env, result, "written", &written);
3813 uint32_t resWritten = 0;
3814 napi_get_value_uint32(env, written, &resWritten);
3815 ASSERT_EQ(resRead, static_cast<uint32_t>(3));
3816 ASSERT_EQ(resWritten, static_cast<uint32_t>(6));
3817 }
3818
3819 /**
3820 * @tc.name: textIsTypedArray001
3821 * @tc.desc: Test check whether the entered value is the type of typedarray
3822 * @tc.type: FUNCs
3823 */
3824 HWTEST_F(NativeEngineTest, textIsTypedArray001, testing::ext::TestSize.Level0)
3825 {
3826 HILOG_INFO("textIsTypedArray001 start");
3827 napi_env env = (napi_env)engine_;
3828 OHOS::Util::Types types;
3829 napi_value obj = nullptr;
3830 napi_create_object(env, &obj);
3831 bool res = true;
3832 napi_value result = types.IsTypedArray(env, obj);
3833 napi_get_value_bool(env, result, &res);
3834 ASSERT_FALSE(res);
3835 }
3836
3837 /**
3838 * @tc.name: textIsTypedArray002
3839 * @tc.desc: Test check whether the entered value is the type of typedarray
3840 * @tc.type: FUNCs
3841 */
3842 HWTEST_F(NativeEngineTest, textIsTypedArray002, testing::ext::TestSize.Level0)
3843 {
3844 HILOG_INFO("textIsTypedArray002 start");
3845 napi_env env = (napi_env)engine_;
3846 OHOS::Util::Types types;
3847 napi_value arrayBuffer = nullptr;
3848 napi_value src = nullptr;
3849 void* arrayBufferPtr = nullptr;
3850 size_t arrayBufferSize = 20;
3851 napi_create_arraybuffer(env, arrayBufferSize * sizeof(size_t), &arrayBufferPtr, &arrayBuffer);
3852 napi_create_typedarray(env, napi_int8_array, arrayBufferSize, arrayBuffer, 0, &src);
3853 bool res = false;
3854 napi_value result = types.IsTypedArray(env, src);
3855 napi_get_value_bool(env, result, &res);
3856 ASSERT_TRUE(res);
3857 }
3858
3859 /**
3860 * @tc.name: textIsWeakSet001
3861 * @tc.desc: Test check whether the entered value is the type of weakset
3862 * @tc.type: FUNCs
3863 */
3864 HWTEST_F(NativeEngineTest, textIsWeakSet001, testing::ext::TestSize.Level0)
3865 {
3866 HILOG_INFO("textIsWeakSet001 start");
3867 napi_env env = (napi_env)engine_;
3868 OHOS::Util::Types types;
3869 napi_value src = nullptr;
3870 napi_create_object(env, &src);
3871 bool res = true;
3872 napi_value result = types.IsWeakSet(env, src);
3873 napi_get_value_bool(env, result, &res);
3874 ASSERT_FALSE(res);
3875 }
3876
3877 /**
3878 * @tc.name: textIsWeakMap001
3879 * @tc.desc: Test check whether the entered value is the type of weakmap
3880 * @tc.type: FUNCs
3881 */
3882 HWTEST_F(NativeEngineTest, textIsWeakMap001, testing::ext::TestSize.Level0)
3883 {
3884 HILOG_INFO("textIsWeakMap001 start");
3885 napi_env env = (napi_env)engine_;
3886 OHOS::Util::Types types;
3887 napi_value src = nullptr;
3888 std::string input = "str";
3889 napi_create_string_utf8(env, input.c_str(), input.size(), &src);
3890 bool res = true;
3891 napi_value result = types.IsWeakMap(env, src);
3892 napi_get_value_bool(env, result, &res);
3893 ASSERT_FALSE(res);
3894 }
3895
3896 /**
3897 * @tc.name: textIsUint32Array001
3898 * @tc.desc: Test check whether the entered value is the type of uint32array
3899 * @tc.type: FUNCs
3900 */
3901 HWTEST_F(NativeEngineTest, textIsUint32Array001, testing::ext::TestSize.Level0)
3902 {
3903 HILOG_INFO("textIsUint32Array001 start");
3904 napi_env env = (napi_env)engine_;
3905 OHOS::Util::Types types;
3906 napi_value src = nullptr;
3907 std::string input = "str";
3908 napi_create_string_utf8(env, input.c_str(), input.size(), &src);
3909 bool res = true;
3910 napi_value result = types.IsUint32Array(env, src);
3911 napi_get_value_bool(env, result, &res);
3912 ASSERT_FALSE(res);
3913 }
3914
3915 /**
3916 * @tc.name: textIsUint32Array002
3917 * @tc.desc: Test check whether the entered value is the type of uint32array
3918 * @tc.type: FUNCs
3919 */
3920 HWTEST_F(NativeEngineTest, textIsUint32Array002, testing::ext::TestSize.Level0)
3921 {
3922 HILOG_INFO("textIsUint32Array002 start");
3923 napi_env env = (napi_env)engine_;
3924 OHOS::Util::Types types;
3925 napi_value obj = nullptr;
3926 napi_create_object(env, &obj);
3927 bool res = true;
3928 napi_value result = types.IsUint32Array(env, obj);
3929 napi_get_value_bool(env, result, &res);
3930 ASSERT_FALSE(res);
3931 }
3932
3933 /**
3934 * @tc.name: textIsUint32Array003
3935 * @tc.desc: Test check whether the entered value is the type of uint32array
3936 * @tc.type: FUNCs
3937 */
3938 HWTEST_F(NativeEngineTest, textIsUint32Array003, testing::ext::TestSize.Level0)
3939 {
3940 HILOG_INFO("textIsUint32Array003 start");
3941 napi_env env = (napi_env)engine_;
3942 OHOS::Util::Types types;
3943 napi_value arrayBuffer = nullptr;
3944 size_t arrayBufferSize = 20;
3945 void* data = nullptr;
3946 napi_create_arraybuffer(env, arrayBufferSize * sizeof(size_t), &data, &arrayBuffer);
3947 napi_value src = nullptr;
3948 napi_create_typedarray(env, napi_int8_array, arrayBufferSize, arrayBuffer, 0, &src);
3949 bool res = true;
3950 napi_value result = types.IsUint32Array(env, src);
3951 napi_get_value_bool(env, result, &res);
3952 ASSERT_FALSE(res);
3953 }
3954
3955 /**
3956 * @tc.name: textIsUint32Array004
3957 * @tc.desc: Test check whether the entered value is the type of uint32array
3958 * @tc.type: FUNCs
3959 */
3960 HWTEST_F(NativeEngineTest, textIsUint32Array004, testing::ext::TestSize.Level0)
3961 {
3962 HILOG_INFO("textIsUint32Array004 start");
3963 napi_env env = (napi_env)engine_;
3964 OHOS::Util::Types types;
3965 napi_value arrayBuffer = nullptr;
3966 size_t arrayBufferSize = 20;
3967 void* data = nullptr;
3968 napi_create_arraybuffer(env, arrayBufferSize * sizeof(size_t), &data, &arrayBuffer);
3969 napi_value src = nullptr;
3970 napi_create_typedarray(env, napi_uint32_array, arrayBufferSize, arrayBuffer, 0, &src);
3971 bool res = false;
3972 napi_value result = types.IsUint32Array(env, src);
3973 napi_get_value_bool(env, result, &res);
3974 ASSERT_TRUE(res);
3975 }
3976
3977 /**
3978 * @tc.name: textIsUint16Array001
3979 * @tc.desc: Test check whether the entered value is the type of uint16array
3980 * @tc.type: FUNCs
3981 */
3982 HWTEST_F(NativeEngineTest, textIsUint16Array001, testing::ext::TestSize.Level0)
3983 {
3984 HILOG_INFO("textIsUint16Array001 start");
3985 napi_env env = (napi_env)engine_;
3986 OHOS::Util::Types types;
3987 napi_value obj = nullptr;
3988 napi_create_object(env, &obj);
3989 bool res = true;
3990 napi_value result = types.IsUint16Array(env, obj);
3991 napi_get_value_bool(env, result, &res);
3992 ASSERT_FALSE(res);
3993 }
3994
3995 /**
3996 * @tc.name: textIsUint16Array002
3997 * @tc.desc: Test check whether the entered value is the type of uint16array
3998 * @tc.type: FUNCs
3999 */
4000 HWTEST_F(NativeEngineTest, textIsUint16Array002, testing::ext::TestSize.Level0)
4001 {
4002 HILOG_INFO("textIsUint16Array002 start");
4003 napi_env env = (napi_env)engine_;
4004 OHOS::Util::Types types;
4005 napi_value arrayBuffer = nullptr;
4006 size_t arrayBufferSize = 20;
4007 void* data = nullptr;
4008 napi_create_arraybuffer(env, arrayBufferSize * sizeof(size_t), &data, &arrayBuffer);
4009 napi_value src = nullptr;
4010 napi_create_typedarray(env, napi_uint16_array, arrayBufferSize, arrayBuffer, 0, &src);
4011 bool res = false;
4012 napi_value result = types.IsUint16Array(env, src);
4013 napi_get_value_bool(env, result, &res);
4014 ASSERT_TRUE(res);
4015 }
4016
4017 /**
4018 * @tc.name: textIsUint8ClampedArray001
4019 * @tc.desc: Test check whether the entered value is the type of uint8clampedarray
4020 * @tc.type: FUNCs
4021 */
4022 HWTEST_F(NativeEngineTest, textIsUint8ClampedArray001, testing::ext::TestSize.Level0)
4023 {
4024 HILOG_INFO("textIsUint8ClampedArray001 start");
4025 napi_env env = (napi_env)engine_;
4026 OHOS::Util::Types types;
4027 napi_value obj = nullptr;
4028 napi_create_object(env, &obj);
4029 bool res = true;
4030 napi_value result = types.IsUint8ClampedArray(env, obj);
4031 napi_get_value_bool(env, result, &res);
4032 ASSERT_FALSE(res);
4033 }
4034
4035 /**
4036 * @tc.name: textIsUint8ClampedArray002
4037 * @tc.desc: Test check whether the entered value is the type of uint8clampedarray
4038 * @tc.type: FUNCs
4039 */
4040 HWTEST_F(NativeEngineTest, textIsUint8ClampedArray002, testing::ext::TestSize.Level0)
4041 {
4042 HILOG_INFO("textIsUint8ClampedArray002 start");
4043 napi_env env = (napi_env)engine_;
4044 OHOS::Util::Types types;
4045 napi_value arrayBuffer = nullptr;
4046 size_t arrayBufferSize = 20;
4047 void* data = nullptr;
4048 napi_create_arraybuffer(env, arrayBufferSize * sizeof(size_t), &data, &arrayBuffer);
4049 napi_value src = nullptr;
4050 napi_create_typedarray(env, napi_uint8_clamped_array, arrayBufferSize, arrayBuffer, 0, &src);
4051 bool res = false;
4052 napi_value result = types.IsUint8ClampedArray(env, src);
4053 napi_get_value_bool(env, result, &res);
4054 ASSERT_TRUE(res);
4055 }
4056
4057 /**
4058 * @tc.name: textIsUint8Array001
4059 * @tc.desc: Test check whether the entered value is the type of uint8array
4060 * @tc.type: FUNCs
4061 */
4062 HWTEST_F(NativeEngineTest, textIsUint8Array001, testing::ext::TestSize.Level0)
4063 {
4064 HILOG_INFO("textIsUint8Array001 start");
4065 napi_env env = (napi_env)engine_;
4066 OHOS::Util::Types types;
4067 napi_value obj = nullptr;
4068 napi_create_object(env, &obj);
4069 bool res = true;
4070 napi_value result = types.IsUint8Array(env, obj);
4071 napi_get_value_bool(env, result, &res);
4072 ASSERT_FALSE(res);
4073 }
4074
4075 /**
4076 * @tc.name: textIsUint8Array002
4077 * @tc.desc: Test check whether the entered value is the type of uint8array
4078 * @tc.type: FUNCs
4079 */
4080 HWTEST_F(NativeEngineTest, textIsUint8Array002, testing::ext::TestSize.Level0)
4081 {
4082 HILOG_INFO("textIsUint8Array002 start");
4083 napi_env env = (napi_env)engine_;
4084 OHOS::Util::Types types;
4085 napi_value arrayBuffer = nullptr;
4086 size_t arrayBufferSize = 20;
4087 void* data = nullptr;
4088 napi_create_arraybuffer(env, arrayBufferSize * sizeof(size_t), &data, &arrayBuffer);
4089 napi_value src = nullptr;
4090 napi_create_typedarray(env, napi_uint8_array, arrayBufferSize, arrayBuffer, 0, &src);
4091 bool res = false;
4092 napi_value result = types.IsUint8Array(env, src);
4093 napi_get_value_bool(env, result, &res);
4094 ASSERT_TRUE(res);
4095 }
4096
4097 /**
4098 * @tc.name: testIsSymbolObject001
4099 * @tc.desc: Test check whether the entered value is the symbol type of object
4100 * @tc.type: FUNCs
4101 */
4102 HWTEST_F(NativeEngineTest, testIsSymbolObject001, testing::ext::TestSize.Level0)
4103 {
4104 HILOG_INFO("testIsSymbolObject001 start");
4105 napi_env env = (napi_env)engine_;
4106 OHOS::Util::Types types;
4107 napi_value obj = nullptr;
4108 napi_create_object(env, &obj);
4109 bool res = true;
4110 napi_value result = types.IsSymbolObject(env, obj);
4111 napi_get_value_bool(env, result, &res);
4112 ASSERT_FALSE(res);
4113 }
4114
4115 /**
4116 * @tc.name: testIsSymbolObject002
4117 * @tc.desc: Test check whether the entered value is the symbol type of object
4118 * @tc.type: FUNCs
4119 */
4120 HWTEST_F(NativeEngineTest, testIsSymbolObject002, testing::ext::TestSize.Level0)
4121 {
4122 HILOG_INFO("testIsSymbolObject002 start");
4123 napi_env env = (napi_env)engine_;
4124 OHOS::Util::Types types;
4125
4126 std::string input = "testSymbol";
4127 napi_value testSymbol = nullptr;
4128 napi_create_string_utf8(env, input.c_str(), input.size(), &testSymbol);
4129 napi_value symbolVal = nullptr;
4130 napi_create_symbol(env, testSymbol, &symbolVal);
4131
4132 bool res = true;
4133 napi_value result = types.IsSymbolObject(env, symbolVal);
4134 napi_get_value_bool(env, result, &res);
4135 ASSERT_FALSE(res);
4136 }
4137
4138 /**
4139 * @tc.name: testIsStringObject001
4140 * @tc.desc: Test check whether the entered value is the string type of object
4141 * @tc.type: FUNCs
4142 */
4143 HWTEST_F(NativeEngineTest, testIsStringObject001, testing::ext::TestSize.Level0)
4144 {
4145 HILOG_INFO("testIsStringObject001 start");
4146 napi_env env = (napi_env)engine_;
4147 OHOS::Util::Types types;
4148 napi_value obj = nullptr;
4149 napi_create_object(env, &obj);
4150 bool res = true;
4151 napi_value result = types.IsStringObject(env, obj);
4152 napi_get_value_bool(env, result, &res);
4153 ASSERT_FALSE(res);
4154 }
4155
4156 /**
4157 * @tc.name: testIsSharedArrayBuffer001
4158 * @tc.desc: Test check whether the entered value is the type of sharedarraybuffer
4159 * @tc.type: FUNCs
4160 */
4161 HWTEST_F(NativeEngineTest, testIsSharedArrayBuffer001, testing::ext::TestSize.Level0)
4162 {
4163 HILOG_INFO("testIsSharedArrayBuffer001 start");
4164 napi_env env = (napi_env)engine_;
4165 OHOS::Util::Types types;
4166 napi_value obj = nullptr;
4167 napi_create_object(env, &obj);
4168 bool res = true;
4169 napi_value result = types.IsSharedArrayBuffer(env, obj);
4170 napi_get_value_bool(env, result, &res);
4171 ASSERT_FALSE(res);
4172 }
4173
4174 /**
4175 * @tc.name: testIsSharedArrayBuffer002
4176 * @tc.desc: Test check whether the entered value is the type of sharedarraybuffer
4177 * @tc.type: FUNCs
4178 */
4179 HWTEST_F(NativeEngineTest, testIsSharedArrayBuffer002, testing::ext::TestSize.Level0)
4180 {
4181 HILOG_INFO("testIsSharedArrayBuffer002 start");
4182 napi_env env = (napi_env)engine_;
4183 OHOS::Util::Types types;
4184 napi_value arrayBuffer = nullptr;
4185 void* arrayBufferPtr = nullptr;
4186 size_t arrayBufferSize = 1024;
4187 napi_create_arraybuffer(env, arrayBufferSize * sizeof(size_t), &arrayBufferPtr, &arrayBuffer);
4188 bool res = true;
4189 napi_value result = types.IsSharedArrayBuffer(env, arrayBuffer);
4190 napi_get_value_bool(env, result, &res);
4191 ASSERT_FALSE(res);
4192 }
4193
4194 /**
4195 * @tc.name: testIsSetIterator001
4196 * @tc.desc: Test check whether the entered value is the iterator type of set
4197 * @tc.type: FUNCs
4198 */
4199 HWTEST_F(NativeEngineTest, testIsSetIterator001, testing::ext::TestSize.Level0)
4200 {
4201 HILOG_INFO("testIsSetIterator001 start");
4202 napi_env env = (napi_env)engine_;
4203 OHOS::Util::Types types;
4204 napi_value obj = nullptr;
4205 napi_create_object(env, &obj);
4206 bool res = true;
4207 napi_value result = types.IsSetIterator(env, obj);
4208 napi_get_value_bool(env, result, &res);
4209 ASSERT_FALSE(res);
4210 }
4211
4212 /**
4213 * @tc.name: testIsSet001
4214 * @tc.desc: Test check whether the entered value is the type of set
4215 * @tc.type: FUNCs
4216 */
4217 HWTEST_F(NativeEngineTest, testIsSet001, testing::ext::TestSize.Level0)
4218 {
4219 HILOG_INFO("testIsSet001 start");
4220 napi_env env = (napi_env)engine_;
4221 OHOS::Util::Types types;
4222 napi_value obj = nullptr;
4223 napi_create_object(env, &obj);
4224 bool res = true;
4225 napi_value result = types.IsSet(env, obj);
4226 napi_get_value_bool(env, result, &res);
4227 ASSERT_FALSE(res);
4228 }
4229
4230 /**
4231 * @tc.name: testIsRegExp001
4232 * @tc.desc: Test check whether the entered value is the type of regexp
4233 * @tc.type: FUNCs
4234 */
4235 HWTEST_F(NativeEngineTest, testIsRegExp001, testing::ext::TestSize.Level0)
4236 {
4237 HILOG_INFO("testIsRegExp001 start");
4238 napi_env env = (napi_env)engine_;
4239 OHOS::Util::Types types;
4240 napi_value obj = nullptr;
4241 napi_create_object(env, &obj);
4242 bool res = true;
4243 napi_value result = types.IsRegExp(env, obj);
4244 napi_get_value_bool(env, result, &res);
4245 ASSERT_FALSE(res);
4246 }
4247
4248 /**
4249 * @tc.name: testIsProxy001
4250 * @tc.desc: Test check whether the entered value is the type of proxy
4251 * @tc.type: FUNCs
4252 */
4253 HWTEST_F(NativeEngineTest, testIsProxy001, testing::ext::TestSize.Level0)
4254 {
4255 HILOG_INFO("testIsProxy001 start");
4256 napi_env env = (napi_env)engine_;
4257 OHOS::Util::Types types;
4258 napi_value obj = nullptr;
4259 napi_create_object(env, &obj);
4260 bool res = true;
4261 napi_value result = types.IsProxy(env, obj);
4262 napi_get_value_bool(env, result, &res);
4263 ASSERT_FALSE(res);
4264 }
4265
4266 /**
4267 * @tc.name: testIsPromise001
4268 * @tc.desc: Test check whether the entered value is the type of promise
4269 * @tc.type: FUNCs
4270 */
4271 HWTEST_F(NativeEngineTest, testIsPromise001, testing::ext::TestSize.Level0)
4272 {
4273 HILOG_INFO("testIsPromise001 start");
4274 napi_env env = (napi_env)engine_;
4275 OHOS::Util::Types types;
4276 napi_value obj = nullptr;
4277 napi_create_object(env, &obj);
4278 bool res = true;
4279 napi_value result = types.IsPromise(env, obj);
4280 napi_get_value_bool(env, result, &res);
4281 ASSERT_FALSE(res);
4282 }
4283
4284 /**
4285 * @tc.name: testIsPromise002
4286 * @tc.desc: Test check whether the entered value is the type of promise
4287 * @tc.type: FUNCs
4288 */
4289 HWTEST_F(NativeEngineTest, testIsPromise002, testing::ext::TestSize.Level0)
4290 {
4291 HILOG_INFO("testIsPromise002 start");
4292 napi_env env = (napi_env)engine_;
4293 OHOS::Util::Types types;
4294 napi_deferred deferred = nullptr;
4295 napi_value promise = nullptr;
4296 ASSERT_CHECK_CALL(napi_create_promise(env, &deferred, &promise));
4297 ASSERT_NE(deferred, nullptr);
4298 ASSERT_NE(promise, nullptr);
4299 bool res = false;
4300 napi_value result = types.IsPromise(env, promise);
4301 napi_get_value_bool(env, result, &res);
4302 ASSERT_TRUE(res);
4303 }
4304
4305 /**
4306 * @tc.name: testIsNumberObject001
4307 * @tc.desc: Test check whether the entered value is the number type of object
4308 * @tc.type: FUNCs
4309 */
4310 HWTEST_F(NativeEngineTest, testIsNumberObject001, testing::ext::TestSize.Level0)
4311 {
4312 HILOG_INFO("testIsNumberObject001 start");
4313 napi_env env = (napi_env)engine_;
4314 OHOS::Util::Types types;
4315 napi_value obj = nullptr;
4316 napi_create_object(env, &obj);
4317 bool res = true;
4318 napi_value result = types.IsNumberObject(env, obj);
4319 napi_get_value_bool(env, result, &res);
4320 ASSERT_FALSE(res);
4321 }
4322
4323 /**
4324 * @tc.name: testIsNativeError001
4325 * @tc.desc: Test check whether the entered value is of type error
4326 * @tc.type: FUNCs
4327 */
4328 HWTEST_F(NativeEngineTest, testIsNativeError001, testing::ext::TestSize.Level0)
4329 {
4330 HILOG_INFO("testIsNativeError001 start");
4331 napi_env env = (napi_env)engine_;
4332 OHOS::Util::Types types;
4333 napi_value obj = nullptr;
4334 napi_create_object(env, &obj);
4335 bool res = true;
4336 napi_value result = types.IsNativeError(env, obj);
4337 napi_get_value_bool(env, result, &res);
4338 ASSERT_FALSE(res);
4339 }
4340
4341 /**
4342 * @tc.name: testIsNativeError002
4343 * @tc.desc: Test check whether the entered value is of type error
4344 * @tc.type: FUNCs
4345 */
4346 HWTEST_F(NativeEngineTest, testIsNativeError002, testing::ext::TestSize.Level0)
4347 {
4348 HILOG_INFO("testIsNativeError002 start");
4349 napi_env env = (napi_env)engine_;
4350 OHOS::Util::Types types;
4351
4352 napi_value code = nullptr;
4353 napi_value message = nullptr;
4354 std::string input = "abc123";
4355 ASSERT_CHECK_CALL(napi_create_string_utf8(env, input.c_str(), NAPI_AUTO_LENGTH, &code));
4356 ASSERT_CHECK_CALL(napi_create_string_utf8(env, input.c_str(), NAPI_AUTO_LENGTH, &message));
4357
4358 napi_value error = nullptr;
4359 ASSERT_CHECK_CALL(napi_create_error(env, code, message, &error));
4360 ASSERT_TRUE(error != nullptr);
4361
4362 bool res = false;
4363 napi_value result = types.IsNativeError(env, error);
4364 napi_get_value_bool(env, result, &res);
4365 ASSERT_TRUE(res);
4366 }
4367
4368 /**
4369 * @tc.name: testIsModuleNamespaceObject001
4370 * @tc.desc: Test check whether the entered value is the module name space type of object
4371 * @tc.type: FUNCs
4372 */
4373 HWTEST_F(NativeEngineTest, testIsModuleNamespaceObject001, testing::ext::TestSize.Level0)
4374 {
4375 HILOG_INFO("testIsModuleNamespaceObject001 start");
4376 napi_env env = (napi_env)engine_;
4377 OHOS::Util::Types types;
4378 napi_value obj = nullptr;
4379 napi_create_object(env, &obj);
4380 bool res = true;
4381 napi_value result = types.IsModuleNamespaceObject(env, obj);
4382 napi_get_value_bool(env, result, &res);
4383 ASSERT_FALSE(res);
4384 }
4385
4386 /**
4387 * @tc.name: testIsMapIterator001
4388 * @tc.desc: Test check whether the entered value is the iterator type of map
4389 * @tc.type: FUNCs
4390 */
4391 HWTEST_F(NativeEngineTest, testIsMapIterator001, testing::ext::TestSize.Level0)
4392 {
4393 HILOG_INFO("testIsMapIterator001 start");
4394 napi_env env = (napi_env)engine_;
4395 OHOS::Util::Types types;
4396 napi_value obj = nullptr;
4397 napi_create_object(env, &obj);
4398 bool res = true;
4399 napi_value result = types.IsMapIterator(env, obj);
4400 napi_get_value_bool(env, result, &res);
4401 ASSERT_FALSE(res);
4402 }
4403
4404 /**
4405 * @tc.name: textIsDateTest001
4406 * @tc.desc: Check if the input value is of type Date.
4407 * @tc.type: FUNC
4408 */
4409 HWTEST_F(NativeEngineTest, textIsDateTest001, testing::ext::TestSize.Level0)
4410 {
4411 HILOG_INFO("textIsDateTest001 start");
4412 napi_env env = (napi_env)engine_;
4413 OHOS::Util::Types type;
4414 napi_value createResult = nullptr;
4415 double time = 202110181203150; // Date and Time
4416 napi_status status = napi_create_date(env, time, &createResult);
4417 if (status == napi_ok) {
4418 HILOG_INFO("Types::napi_create_date success");
4419 } else {
4420 HILOG_INFO("Types::napi_create_date fail");
4421 }
4422 napi_value result = type.IsDate(env, createResult);
4423 bool res = false;
4424 napi_get_value_bool(env, result, &res);
4425 ASSERT_TRUE(res);
4426 }
4427
4428 /**
4429 * @tc.name: textIsExternalTest001
4430 * @tc.desc: Check if the input value is of type native External.
4431 * @tc.type: FUNC
4432 */
4433 HWTEST_F(NativeEngineTest, textIsExternalTest001, testing::ext::TestSize.Level0)
4434 {
4435 HILOG_INFO("textIsExternalTest001 start");
4436 napi_env env = (napi_env)engine_;
4437 OHOS::Util::Types type;
4438 napi_value dataVal = nullptr;
4439 int* raw = new int(1);
4440 napi_status status = napi_create_external(env, (void*) raw, NULL, NULL, &dataVal);
4441 if (status == napi_ok) {
4442 HILOG_INFO("Types::napi_create_external success");
4443 } else {
4444 HILOG_INFO("Types::napi_create_external fail");
4445 }
4446 napi_value result = type.IsExternal(env, dataVal);
4447 bool res = false;
4448 napi_get_value_bool(env, result, &res);
4449 ASSERT_TRUE(res);
4450 }
4451
4452 /**
4453 * @tc.name: textIsMapTest001
4454 * @tc.desc: Check if the input value is of Map type.
4455 * @tc.type: FUNC
4456 */
4457 HWTEST_F(NativeEngineTest, textIsMapTest001, testing::ext::TestSize.Level0)
4458 {
4459 HILOG_INFO("textIsMapTest001 start");
4460 napi_env env = (napi_env)engine_;
4461 OHOS::Util::Types type;
4462 napi_value resMap = nullptr;
4463 napi_create_map(env, &resMap);
4464 napi_value result = type.IsMap(env, resMap);
4465 bool res = false;
4466 napi_get_value_bool(env, result, &res);
4467 ASSERT_TRUE(res);
4468 }
4469
4470 /**
4471 * @tc.name: textIsMapTest002
4472 * @tc.desc: Check if the input value is of Map type.
4473 * @tc.type: FUNC
4474 */
4475 HWTEST_F(NativeEngineTest, textIsMapTest002, testing::ext::TestSize.Level0)
4476 {
4477 HILOG_INFO("textIsMapTest002 start");
4478 napi_env env = (napi_env)engine_;
4479 OHOS::Util::Types type;
4480 napi_value dest = nullptr;
4481 napi_create_sendable_map(env, &dest);
4482 napi_value result = type.IsMap(env, dest);
4483 bool res = false;
4484 napi_get_value_bool(env, result, &res);
4485 ASSERT_TRUE(res);
4486 }
4487
4488 /**
4489 * @tc.name: textIsAnyArrayBufferTest001
4490 * @tc.desc: Check if the input value is of type ArrayBuffer or SharedArrayBuffer.
4491 * @tc.type: FUNC
4492 */
4493 HWTEST_F(NativeEngineTest, textIsAnyArrayBufferTest001, testing::ext::TestSize.Level0)
4494 {
4495 HILOG_INFO("textIsAnyArrayBufferTest001 start");
4496 napi_env env = (napi_env)engine_;
4497 OHOS::Util::Types type;
4498 napi_value arrayBuffer = nullptr;
4499 void* arrayBufferPtr = nullptr;
4500 size_t arrayBufferSize = 1;
4501 napi_create_arraybuffer(env, arrayBufferSize * sizeof(size_t), &arrayBufferPtr, &arrayBuffer);
4502 napi_value result = type.IsAnyArrayBuffer(env, arrayBuffer);
4503 bool res = false;
4504 napi_get_value_bool(env, result, &res);
4505 ASSERT_TRUE(res);
4506 }
4507
4508 /**
4509 * @tc.name: textIsAnyArrayBufferTest002
4510 * @tc.desc: Check if the input value is of type ArrayBuffer or SharedArrayBuffer.
4511 * @tc.type: FUNC
4512 */
4513 HWTEST_F(NativeEngineTest, textIsAnyArrayBufferTest002, testing::ext::TestSize.Level0)
4514 {
4515 HILOG_INFO("textIsAnyArrayBufferTest002 start");
4516 napi_env env = (napi_env)engine_;
4517 OHOS::Util::Types type;
4518 napi_value testFlag = nullptr;
4519 bool test = false;
4520 napi_get_value_bool(env, testFlag, &test);
4521 napi_value result = type.IsAnyArrayBuffer(env, testFlag);
4522 bool res = false;
4523 napi_get_value_bool(env, result, &res);
4524 ASSERT_FALSE(res);
4525 }
4526
4527 /**
4528 * @tc.name: textIsArrayBufferViewTest001
4529 * @tc.desc: Check if the input value is of the built-in ArrayBufferView auxiliary type.
4530 * @tc.type: FUNC
4531 */
4532 HWTEST_F(NativeEngineTest, textIsArrayBufferViewTest001, testing::ext::TestSize.Level0)
4533 {
4534 HILOG_INFO("textIsArrayBufferViewTest001 start");
4535 napi_env env = (napi_env)engine_;
4536 OHOS::Util::Types type;
4537 napi_value testFlag = nullptr;
4538 bool test = false;
4539 napi_get_value_bool(env, testFlag, &test);
4540 napi_value result = type.IsArrayBufferView(env, testFlag);
4541 bool res = false;
4542 napi_get_value_bool(env, result, &res);
4543 ASSERT_FALSE(res);
4544 }
4545
4546 /**
4547 * @tc.name: textIsArrayBufferViewTest002
4548 * @tc.desc: Check if the input value is of the built-in ArrayBufferView auxiliary type.
4549 * @tc.type: FUNC
4550 */
4551 HWTEST_F(NativeEngineTest, textIsArrayBufferViewTest002, testing::ext::TestSize.Level0)
4552 {
4553 HILOG_INFO("textIsArrayBufferViewTest002 start");
4554 napi_env env = (napi_env)engine_;
4555 OHOS::Util::Types type;
4556 napi_value arrayBuffer = nullptr;
4557 void* arrayBufferPtr = nullptr;
4558 size_t arrayBufferSize = 1;
4559 napi_create_arraybuffer(env, arrayBufferSize, &arrayBufferPtr, &arrayBuffer);
4560 napi_value dataview = nullptr;
4561 napi_create_dataview(env, arrayBufferSize, arrayBuffer, 0, &dataview);
4562 napi_value result = type.IsArrayBufferView(env, dataview);
4563 bool res = false;
4564 napi_get_value_bool(env, result, &res);
4565 ASSERT_TRUE(res);
4566 }
4567
4568 /**
4569 * @tc.name: textIsDataViewTest001
4570 * @tc.desc: Check if the input value is of type WebView.
4571 * @tc.type: FUNC
4572 */
4573 HWTEST_F(NativeEngineTest, textIsDataViewTest001, testing::ext::TestSize.Level0)
4574 {
4575 HILOG_INFO("textIsDataViewTest001 start");
4576 napi_env env = (napi_env)engine_;
4577 OHOS::Util::Types type;
4578 napi_value arrBuffer = nullptr;
4579 void* arrBufferPtr = nullptr;
4580 size_t arrBufferSize = 1;
4581 napi_create_arraybuffer(env, arrBufferSize * sizeof(size_t), &arrBufferPtr, &arrBuffer);
4582 napi_value dataView = nullptr;
4583 napi_create_dataview(env, arrBufferSize * sizeof(size_t), arrBuffer, 0, &dataView);
4584 napi_value result = type.IsDataView(env, dataView);
4585 bool res = false;
4586 napi_get_value_bool(env, result, &res);
4587 ASSERT_TRUE(res);
4588 }
4589
4590 /**
4591 * @tc.name: textIsArrayBufferViewTest003
4592 * @tc.desc: Check if the input value is of the built-in ArrayBufferView auxiliary type.
4593 * @tc.type: FUNC
4594 */
4595 HWTEST_F(NativeEngineTest, textIsArrayBufferViewTest003, testing::ext::TestSize.Level0)
4596 {
4597 HILOG_INFO("textIsArrayBufferViewTest003 start");
4598 napi_env env = (napi_env)engine_;
4599 OHOS::Util::Types type;
4600 size_t byteLength = 1;
4601 void* data = nullptr;
4602 napi_value resultBuff = nullptr;
4603 napi_create_arraybuffer(env, byteLength, &data, &resultBuff);
4604 unsigned char arr[1] = {0x1};
4605 int ret = memcpy_s(data, sizeof(arr), reinterpret_cast<void*>(arr), sizeof(arr));
4606 ASSERT_EQ(0, ret);
4607 napi_value int8Array = nullptr;
4608 napi_create_typedarray(env, napi_int8_array, byteLength, resultBuff, 0, &int8Array);
4609 napi_value result = type.IsArrayBufferView(env, int8Array);
4610 bool res = false;
4611 napi_get_value_bool(env, result, &res);
4612 ASSERT_TRUE(res);
4613 }
4614
4615 /**
4616 * @tc.name: textIsArrayBufferViewTest004
4617 * @tc.desc: Check if the input value is of the built-in ArrayBufferView auxiliary type.
4618 * @tc.type: FUNC
4619 */
4620 HWTEST_F(NativeEngineTest, textIsArrayBufferViewTest004, testing::ext::TestSize.Level0)
4621 {
4622 HILOG_INFO("textIsArrayBufferViewTest004 start");
4623 napi_env env = (napi_env)engine_;
4624 OHOS::Util::Types type;
4625 size_t byteLength = 1;
4626 void* data = nullptr;
4627 napi_value resultBuff = nullptr;
4628 napi_create_arraybuffer(env, byteLength, &data, &resultBuff);
4629 unsigned char arr[1] = {0x1};
4630 int ret = memcpy_s(data, sizeof(arr), reinterpret_cast<void*>(arr), sizeof(arr));
4631 ASSERT_EQ(0, ret);
4632 napi_value uint8Array = nullptr;
4633 napi_create_typedarray(env, napi_uint8_array, byteLength, resultBuff, 0, &uint8Array);
4634 napi_value result = type.IsArrayBufferView(env, uint8Array);
4635 bool res = false;
4636 napi_get_value_bool(env, result, &res);
4637 ASSERT_TRUE(res);
4638 }
4639
4640 /**
4641 * @tc.name: textIsArrayBufferViewTest005
4642 * @tc.desc: Check if the input value is of the built-in ArrayBufferView auxiliary type.
4643 * @tc.type: FUNC
4644 */
4645 HWTEST_F(NativeEngineTest, textIsArrayBufferViewTest005, testing::ext::TestSize.Level0)
4646 {
4647 HILOG_INFO("textIsArrayBufferViewTest005 start");
4648 napi_env env = (napi_env)engine_;
4649 OHOS::Util::Types type;
4650 size_t byteLength = 1;
4651 void* data = nullptr;
4652 napi_value resultBuff = nullptr;
4653 napi_create_arraybuffer(env, byteLength * sizeof(int16_t), &data, &resultBuff);
4654 int16_t arr[1] = {0x1};
4655 int ret = memcpy_s(data, sizeof(arr), reinterpret_cast<void*>(arr), sizeof(arr));
4656 ASSERT_EQ(0, ret);
4657 napi_value int16Array = nullptr;
4658 napi_create_typedarray(env, napi_int16_array, byteLength, resultBuff, 0, &int16Array);
4659 napi_value result = type.IsArrayBufferView(env, int16Array);
4660 bool res = false;
4661 napi_get_value_bool(env, result, &res);
4662 ASSERT_TRUE(res);
4663 }
4664
4665 /**
4666 * @tc.name: textIsArgumentsObjectTest001
4667 * @tc.desc: Check if the input value is an arguments object.
4668 * @tc.type: FUNC
4669 */
4670 HWTEST_F(NativeEngineTest, textIsArgumentsObjectTest001, testing::ext::TestSize.Level0)
4671 {
4672 HILOG_INFO("textIsArgumentsObjectTest001 start");
4673 napi_env env = (napi_env)engine_;
4674 OHOS::Util::Types type;
4675 bool test = false;
4676 napi_value testVal = nullptr;
4677 napi_get_value_bool(env, testVal, &test);
4678 napi_value result = type.IsArgumentsObject(env, testVal);
4679 bool res = false;
4680 napi_get_value_bool(env, result, &res);
4681 ASSERT_FALSE(res);
4682 }
4683
4684 /**
4685 * @tc.name: textIsArrayBufferTest001
4686 * @tc.desc: Check if the input value is of type ArrayBuffer.
4687 * @tc.type: FUNC
4688 */
4689 HWTEST_F(NativeEngineTest, textIsArrayBufferTest001, testing::ext::TestSize.Level0)
4690 {
4691 HILOG_INFO("textIsArrayBufferTest001 start");
4692 napi_env env = (napi_env)engine_;
4693 OHOS::Util::Types type;
4694 napi_value arrayBuffer = nullptr;
4695 void* arrayBufferPtr = nullptr;
4696 size_t arrayBufferSize = 1;
4697 napi_create_arraybuffer(env, arrayBufferSize, &arrayBufferPtr, &arrayBuffer);
4698 napi_value result = type.IsArrayBuffer(env, arrayBuffer);
4699 bool res = false;
4700 napi_get_value_bool(env, result, &res);
4701 ASSERT_TRUE(res);
4702 }
4703
4704 /**
4705 * @tc.name: textIsAsyncFunctionTest001
4706 * @tc.desc: Check if the input value is an asynchronous function type.
4707 * @tc.type: FUNC
4708 */
4709 HWTEST_F(NativeEngineTest, textIsAsyncFunctionTest001, testing::ext::TestSize.Level0)
4710 {
4711 HILOG_INFO("textIsAsyncFunctionTest001 start");
4712 napi_env env = (napi_env)engine_;
4713 OHOS::Util::Types type;
4714 bool testRes = false;
4715 napi_value testRst = nullptr;
4716 napi_get_value_bool(env, testRst, &testRes);
4717 napi_value result = type.IsAsyncFunction(env, testRst);
4718 bool res = false;
4719 napi_get_value_bool(env, result, &res);
4720 ASSERT_FALSE(res);
4721 }
4722
4723 /**
4724 * @tc.name: textIsInt32ArrayTest001
4725 * @tc.desc: Check if the input value is of type Int32Array array.
4726 * @tc.type: FUNC
4727 */
4728 HWTEST_F(NativeEngineTest, textIsInt32ArrayTest001, testing::ext::TestSize.Level0)
4729 {
4730 HILOG_INFO("textIsInt32ArrayTest001 start");
4731 napi_env env = (napi_env)engine_;
4732 OHOS::Util::Types type;
4733 napi_value arrayBuffer = nullptr;
4734 void* arrayBufferPtr = nullptr;
4735 size_t arrayBufferSize = 1;
4736 napi_create_arraybuffer(env, arrayBufferSize * sizeof(size_t), &arrayBufferPtr, &arrayBuffer);
4737 napi_value dest = nullptr;
4738 napi_create_typedarray(env, napi_int32_array, arrayBufferSize, arrayBuffer, 0, &dest);
4739 napi_value result = type.IsInt32Array(env, dest);
4740 bool res = false;
4741 napi_get_value_bool(env, result, &res);
4742 ASSERT_TRUE(res);
4743 }
4744
4745 /**
4746 * @tc.name: textIsBigInt64ArrayTest001
4747 * @tc.desc: Check if the input value is of type BigInt64Array.
4748 * @tc.type: FUNC
4749 */
4750 HWTEST_F(NativeEngineTest, textIsBigInt64ArrayTest001, testing::ext::TestSize.Level0)
4751 {
4752 HILOG_INFO("textIsBigInt64ArrayTest001 start");
4753 napi_env env = (napi_env)engine_;
4754 OHOS::Util::Types type;
4755 napi_value arrayBuffer = nullptr;
4756 void* arrayBufferPtr = nullptr;
4757 size_t arrayBufferSize = 1;
4758 napi_create_arraybuffer(env, arrayBufferSize * sizeof(int64_t), &arrayBufferPtr, &arrayBuffer);
4759 napi_value dest = nullptr;
4760 napi_create_typedarray(env, napi_bigint64_array, arrayBufferSize, arrayBuffer, 0, &dest);
4761 napi_value result = type.IsBigInt64Array(env, dest);
4762 bool res = false;
4763 napi_get_value_bool(env, result, &res);
4764 ASSERT_TRUE(res);
4765 }
4766
4767 /**
4768 * @tc.name: textIsBigUint64ArrayTest001
4769 * @tc.desc: Check if the input value is of type BigUint64Array.
4770 * @tc.type: FUNC
4771 */
4772 HWTEST_F(NativeEngineTest, textIsBigUint64ArrayTest001, testing::ext::TestSize.Level0)
4773 {
4774 HILOG_INFO("textIsBigUint64ArrayTest001 start");
4775 napi_env env = (napi_env)engine_;
4776 OHOS::Util::Types type;
4777 napi_value arrayBuffer = nullptr;
4778 void* arrayBufferPtr = nullptr;
4779 size_t arrayBufferSize = 20; // number 20
4780 napi_create_arraybuffer(env, arrayBufferSize * sizeof(uint64_t), &arrayBufferPtr, &arrayBuffer);
4781 napi_value dest = nullptr;
4782 napi_create_typedarray(env, napi_biguint64_array, arrayBufferSize, arrayBuffer, 0, &dest);
4783 napi_value result = type.IsBigUint64Array(env, dest);
4784 bool res = false;
4785 napi_get_value_bool(env, result, &res);
4786 ASSERT_TRUE(res);
4787 }
4788
4789 /**
4790 * @tc.name: textIsGeneratorFunctionTest001
4791 * @tc.desc: Check if the input value is of the generator function type.
4792 * @tc.type: FUNC
4793 */
4794 HWTEST_F(NativeEngineTest, textIsGeneratorFunctionTest001, testing::ext::TestSize.Level0)
4795 {
4796 HILOG_INFO("textIsGeneratorFunctionTest001 start");
4797 napi_env env = (napi_env)engine_;
4798 OHOS::Util::Types type;
4799 bool resFlag = false;
4800 napi_value dest = nullptr;
4801 napi_get_value_bool(env, dest, &resFlag);
4802 napi_value result = type.IsGeneratorFunction(env, dest);
4803 bool res = false;
4804 napi_get_value_bool(env, result, &res);
4805 ASSERT_FALSE(res);
4806 }
4807
4808 /**
4809 * @tc.name: textIsGeneratorObjectTest001
4810 * @tc.desc: Check if the input value is of the generator object type.
4811 * @tc.type: FUNC
4812 */
4813 HWTEST_F(NativeEngineTest, textIsGeneratorObjectTest001, testing::ext::TestSize.Level0)
4814 {
4815 HILOG_INFO("textIsGeneratorObjectTest001 start");
4816 napi_env env = (napi_env)engine_;
4817 OHOS::Util::Types type;
4818 napi_value dest = nullptr;
4819 napi_create_object(env, &dest);
4820 napi_value result = type.IsGeneratorObject(env, dest);
4821 bool res = false;
4822 napi_get_value_bool(env, result, &res);
4823 ASSERT_FALSE(res);
4824 }
4825
4826 /**
4827 * @tc.name: textIsFloat32ArrayTest001
4828 * @tc.desc: Check if the input value is of Float32Array array type.
4829 * @tc.type: FUNC
4830 */
4831 HWTEST_F(NativeEngineTest, textIsFloat32ArrayTest001, testing::ext::TestSize.Level0)
4832 {
4833 HILOG_INFO("textIsFloat32ArrayTest001 start");
4834 napi_env env = (napi_env)engine_;
4835 OHOS::Util::Types type;
4836 napi_value arrayBuffer = nullptr;
4837 void* arrayBufferPtr = nullptr;
4838 size_t arrayBufferSize = 1;
4839 napi_create_arraybuffer(env, arrayBufferSize * sizeof(float), &arrayBufferPtr, &arrayBuffer);
4840 napi_value dest = nullptr;
4841 napi_create_typedarray(env, napi_float32_array, arrayBufferSize, arrayBuffer, 0, &dest);
4842 napi_value result = type.IsFloat32Array(env, dest);
4843 bool res = false;
4844 napi_get_value_bool(env, result, &res);
4845 ASSERT_TRUE(res);
4846 }
4847
4848 /**
4849 * @tc.name: textIsBooleanObjectTest001
4850 * @tc.desc: Check if the input value is a Boolean object type.
4851 * @tc.type: FUNC
4852 */
4853 HWTEST_F(NativeEngineTest, textIsBooleanObjectTest001, testing::ext::TestSize.Level0)
4854 {
4855 HILOG_INFO("textIsBooleanObjectTest001 start");
4856 napi_env env = (napi_env)engine_;
4857 OHOS::Util::Types type;
4858 napi_value testRst = nullptr;
4859 napi_get_boolean(env, false, &testRst);
4860 napi_value result = type.IsBooleanObject(env, testRst);
4861 bool res = false;
4862 napi_get_value_bool(env, result, &res);
4863 ASSERT_FALSE(res);
4864 }
4865
4866 /**
4867 * @tc.name: textIsBoxedPrimitiveTest001
4868 * @tc.desc: Check if the input value is of Boolean, Number, String, or Symbol object type.
4869 * @tc.type: FUNC
4870 */
4871 HWTEST_F(NativeEngineTest, textIsBoxedPrimitiveTest001, testing::ext::TestSize.Level0)
4872 {
4873 HILOG_INFO("textIsBoxedPrimitiveTest001 start");
4874 napi_env env = (napi_env)engine_;
4875 OHOS::Util::Types type;
4876 napi_value src = nullptr;
4877 napi_create_string_utf8(env, "abcd", NAPI_AUTO_LENGTH, &src);
4878 napi_value result = type.IsBoxedPrimitive(env, src);
4879 bool res = false;
4880 napi_get_value_bool(env, result, &res);
4881 ASSERT_FALSE(res);
4882 }
4883
4884 /**
4885 * @tc.name: textIsFloat64ArrayTest001
4886 * @tc.desc: Check if the input value is of Float64Array array type.
4887 * @tc.type: FUNC
4888 */
4889 HWTEST_F(NativeEngineTest, textIsFloat64ArrayTest001, testing::ext::TestSize.Level0)
4890 {
4891 HILOG_INFO("textIsFloat64ArrayTest001 start");
4892 napi_env env = (napi_env)engine_;
4893 OHOS::Util::Types type;
4894 napi_value arrayBuffer = nullptr;
4895 void* arrayBufferPtr = nullptr;
4896 size_t arrayBufferSize = 1;
4897 napi_create_arraybuffer(env, arrayBufferSize * sizeof(double), &arrayBufferPtr, &arrayBuffer);
4898 napi_value dest = nullptr;
4899 napi_create_typedarray(env, napi_float64_array, arrayBufferSize, arrayBuffer, 0, &dest);
4900 napi_value result = type.IsFloat64Array(env, dest);
4901 bool res = false;
4902 napi_get_value_bool(env, result, &res);
4903 ASSERT_TRUE(res);
4904 }
4905
4906 /**
4907 * @tc.name: textIsInt8ArrayTest001
4908 * @tc.desc: Check if the input value is of type Int8Array array.
4909 * @tc.type: FUNC
4910 */
4911 HWTEST_F(NativeEngineTest, textIsInt8ArrayTest001, testing::ext::TestSize.Level0)
4912 {
4913 HILOG_INFO("textIsInt8ArrayTest001 start");
4914 napi_env env = (napi_env)engine_;
4915 OHOS::Util::Types type;
4916 napi_value arrayBuffer = nullptr;
4917 void* arrayBufferPtr = nullptr;
4918 size_t arrayBufferSize = 1;
4919 napi_create_arraybuffer(env, arrayBufferSize * sizeof(int8_t), &arrayBufferPtr, &arrayBuffer);
4920 napi_value dest = nullptr;
4921 napi_create_typedarray(env, napi_int8_array, arrayBufferSize, arrayBuffer, 0, &dest);
4922 napi_value result = type.IsInt8Array(env, dest);
4923 bool res = false;
4924 napi_get_value_bool(env, result, &res);
4925 ASSERT_TRUE(res);
4926 }
4927
4928 /**
4929 * @tc.name: textIsInt16ArrayTest001
4930 * @tc.desc: Check if the input value is of type Int16Array array.
4931 * @tc.type: FUNC
4932 */
4933 HWTEST_F(NativeEngineTest, textIsInt16ArrayTest001, testing::ext::TestSize.Level0)
4934 {
4935 HILOG_INFO("textIsInt16ArrayTest001 start");
4936 napi_env env = (napi_env)engine_;
4937 OHOS::Util::Types type;
4938 napi_value arrayBuffer = nullptr;
4939 void* arrayBufferPtr = nullptr;
4940 size_t arrayBufferSize = 1;
4941 napi_create_arraybuffer(env, arrayBufferSize * sizeof(int16_t), &arrayBufferPtr, &arrayBuffer);
4942 napi_value dest = nullptr;
4943 napi_create_typedarray(env, napi_int16_array, arrayBufferSize, arrayBuffer, 0, &dest);
4944 napi_value result = type.IsInt16Array(env, dest);
4945 bool res = false;
4946 napi_get_value_bool(env, result, &res);
4947 ASSERT_TRUE(res);
4948 }
4949
4950 /**
4951 * @tc.name: textIsArrayBufferViewTest006
4952 * @tc.desc: Check if the input value is of the built-in ArrayBufferView auxiliary type.
4953 * @tc.type: FUNC
4954 */
4955 HWTEST_F(NativeEngineTest, textIsArrayBufferViewTest006, testing::ext::TestSize.Level0)
4956 {
4957 napi_env env = (napi_env)engine_;
4958 OHOS::Util::Types type;
4959 napi_value arrayBuffer = nullptr;
4960 void* arrayBufferPtr = nullptr;
4961 size_t arrayBufferSize = 20; // number 20
4962 napi_create_arraybuffer(env, arrayBufferSize * sizeof(uint64_t), &arrayBufferPtr, &arrayBuffer);
4963 napi_value dest = nullptr;
4964 napi_create_typedarray(env, napi_biguint64_array, arrayBufferSize, arrayBuffer, 0, &dest);
4965 napi_value result = type.IsArrayBufferView(env, dest);
4966 bool res = false;
4967 napi_get_value_bool(env, result, &res);
4968 ASSERT_FALSE(res);
4969 }
4970
4971 /**
4972 * @tc.name: textIsArgumentsObjectTest002
4973 * @tc.desc: Check if the input value is an arguments object.
4974 * @tc.type: FUNC
4975 */
4976 HWTEST_F(NativeEngineTest, textIsArgumentsObjectTest002, testing::ext::TestSize.Level0)
4977 {
4978 napi_env env = (napi_env)engine_;
4979 OHOS::Util::Types type;
4980 napi_value dest = nullptr;
4981 napi_create_object(env, &dest);
4982 napi_value result = type.IsArgumentsObject(env, dest);
4983 bool res = true;
4984 napi_get_value_bool(env, result, &res);
4985 ASSERT_FALSE(res);
4986 }
4987
SayHello(napi_env env,napi_callback_info info)4988 static napi_value SayHello(napi_env env, napi_callback_info info)
4989 {
4990 napi_value ret;
4991 NAPI_CALL(env, napi_create_int32(env, 1, &ret));
4992 return ret;
4993 }
4994
4995 /**
4996 * @tc.name: textIsAsyncFunctionTest002
4997 * @tc.desc: Check if the input value is an asynchronous function type.
4998 * @tc.type: FUNC
4999 */
5000 HWTEST_F(NativeEngineTest, textIsAsyncFunctionTest002, testing::ext::TestSize.Level0)
5001 {
5002 napi_env env = (napi_env)engine_;
5003 OHOS::Util::Types type;
5004 napi_value funcValue = nullptr;
5005 napi_create_function(env, nullptr, NAPI_AUTO_LENGTH, SayHello, nullptr, &funcValue);
5006 bool res = true;
5007 napi_value result = type.IsAsyncFunction(env, funcValue);
5008 napi_get_value_bool(env, result, &res);
5009 ASSERT_FALSE(res);
5010 }
5011
5012 /**
5013 * @tc.name: textIsBooleanObjectTest002
5014 * @tc.desc: Check if the input value is a Boolean object type.
5015 * @tc.type: FUNC
5016 */
5017 HWTEST_F(NativeEngineTest, textIsBooleanObjectTest002, testing::ext::TestSize.Level0)
5018 {
5019 napi_env env = (napi_env)engine_;
5020 OHOS::Util::Types type;
5021 napi_value dest = nullptr;
5022 napi_create_object(env, &dest);
5023 bool res = true;
5024 napi_value result = type.IsBooleanObject(env, dest);
5025 napi_get_value_bool(env, result, &res);
5026 ASSERT_FALSE(res);
5027 }
5028
5029 /**
5030 * @tc.name: textIsGeneratorFunctionTest002
5031 * @tc.desc: Check if the input value is of the generator function type.
5032 * @tc.type: FUNC
5033 */
5034 HWTEST_F(NativeEngineTest, textIsGeneratorFunctionTest002, testing::ext::TestSize.Level0)
5035 {
5036 napi_env env = (napi_env)engine_;
5037 OHOS::Util::Types type;
5038 napi_value funcValue = nullptr;
5039 napi_create_function(env, nullptr, NAPI_AUTO_LENGTH, SayHello, nullptr, &funcValue);
5040 napi_value result = type.IsGeneratorFunction(env, funcValue);
5041 bool res = true;
5042 napi_get_value_bool(env, result, &res);
5043 ASSERT_FALSE(res);
5044 }