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: textEncodeIntoTest013
3821 * @tc.desc: Test returns a dictionary object indicating the progress of the encoding
3822 * @tc.type: FUNC
3823 */
3824 HWTEST_F(NativeEngineTest, textEncodeIntoTest013, testing::ext::TestSize.Level0)
3825 {
3826 HILOG_INFO("textEncodeIntoTest013 start");
3827 napi_env env = (napi_env)engine_;
3828 OHOS::Util::TextEncoder textEncoder("utf-8");
3829 std::string input = "a";
3830 napi_value src = nullptr;
3831 napi_create_string_utf8(env, input.c_str(), input.size(), &src);
3832 napi_value arrayBuffer = nullptr;
3833 void* arrayBufferPtr = nullptr;
3834 size_t arrayBufferSize = 20;
3835 napi_create_arraybuffer(env, arrayBufferSize, &arrayBufferPtr, &arrayBuffer);
3836 napi_value dest = nullptr;
3837 napi_create_typedarray(env, napi_int8_array, arrayBufferSize, arrayBuffer, 0, &dest);
3838 napi_value result = textEncoder.EncodeInto(env, src, dest);
3839 napi_value read = nullptr;
3840 napi_get_named_property(env, result, "read", &read);
3841 uint32_t resRead = 0;
3842 napi_get_value_uint32(env, read, &resRead);
3843 napi_value written = nullptr;
3844 napi_get_named_property(env, result, "written", &written);
3845 uint32_t resWritten = 0;
3846 napi_get_value_uint32(env, written, &resWritten);
3847 ASSERT_EQ(resRead, static_cast<uint32_t>(3)); // 3: resRead expected results
3848 ASSERT_EQ(resWritten, static_cast<uint32_t>(5)); // 5: resWritten expected results
3849 }
3850
3851 /**
3852 * @tc.name: textEncodeIntoTest014
3853 * @tc.desc: Test returns a dictionary object indicating the progress of the encoding
3854 * @tc.type: FUNC
3855 */
3856 HWTEST_F(NativeEngineTest, textEncodeIntoTest014, testing::ext::TestSize.Level0)
3857 {
3858 HILOG_INFO("textEncodeIntoTest014 start");
3859 napi_env env = (napi_env)engine_;
3860 OHOS::Util::TextEncoder textEncoder("gb2312");
3861 std::string input = "中文abc";
3862 napi_value src = nullptr;
3863 napi_create_string_utf8(env, input.c_str(), input.size(), &src);
3864 napi_value arrayBuffer = nullptr;
3865 void* arrayBufferPtr = nullptr;
3866 size_t arrayBufferSize = 20;
3867 napi_create_arraybuffer(env, arrayBufferSize, &arrayBufferPtr, &arrayBuffer);
3868 napi_value dest = nullptr;
3869 napi_create_typedarray(env, napi_int8_array, arrayBufferSize, arrayBuffer, 0, &dest);
3870 napi_value result = textEncoder.EncodeInto(env, src, dest);
3871 napi_value read = nullptr;
3872 napi_get_named_property(env, result, "read", &read);
3873 uint32_t resRead = 0;
3874 napi_get_value_uint32(env, read, &resRead);
3875 napi_value written = nullptr;
3876 napi_get_named_property(env, result, "written", &written);
3877 uint32_t resWritten = 0;
3878 napi_get_value_uint32(env, written, &resWritten);
3879 ASSERT_EQ(resRead, static_cast<uint32_t>(5)); // 5: resRead expected results
3880 ASSERT_EQ(resWritten, static_cast<uint32_t>(7)); // 7: resWritten expected results
3881 }
3882
3883 /**
3884 * @tc.name: textEncodeIntoTest015
3885 * @tc.desc: Test returns a dictionary object indicating the progress of the encoding
3886 * @tc.type: FUNC
3887 */
3888 HWTEST_F(NativeEngineTest, textEncodeIntoTest015, testing::ext::TestSize.Level0)
3889 {
3890 HILOG_INFO("textEncodeIntoTest015 start");
3891 napi_env env = (napi_env)engine_;
3892 OHOS::Util::TextEncoder textEncoder("iso-8859-1");
3893 std::string input = "München";
3894 napi_value src = nullptr;
3895 napi_create_string_utf8(env, input.c_str(), input.size(), &src);
3896 napi_value arrayBuffer = nullptr;
3897 void* arrayBufferPtr = nullptr;
3898 size_t arrayBufferSize = 20;
3899 napi_create_arraybuffer(env, arrayBufferSize, &arrayBufferPtr, &arrayBuffer);
3900 napi_value dest = nullptr;
3901 napi_create_typedarray(env, napi_int8_array, arrayBufferSize, arrayBuffer, 0, &dest);
3902 napi_value result = textEncoder.EncodeInto(env, src, dest);
3903 napi_value read = nullptr;
3904 napi_get_named_property(env, result, "read", &read);
3905 uint32_t resRead = 0;
3906 napi_get_value_uint32(env, read, &resRead);
3907 napi_value written = nullptr;
3908 napi_get_named_property(env, result, "written", &written);
3909 uint32_t resWritten = 0;
3910 napi_get_value_uint32(env, written, &resWritten);
3911 ASSERT_EQ(resRead, static_cast<uint32_t>(7)); // 7: resRead expected results
3912 ASSERT_EQ(resWritten, static_cast<uint32_t>(7)); // 7: resWritten expected results
3913 }
3914
3915 /**
3916 * @tc.name: textEncodeIntoTest016
3917 * @tc.desc: Test returns a dictionary object indicating the progress of the encoding
3918 * @tc.type: FUNC
3919 */
3920 HWTEST_F(NativeEngineTest, textEncodeIntoTest016, testing::ext::TestSize.Level0)
3921 {
3922 HILOG_INFO("textEncodeIntoTest016 start");
3923 napi_env env = (napi_env)engine_;
3924 OHOS::Util::TextEncoder textEncoder("windows-1251");
3925 std::string input = "Привет";
3926 napi_value src = nullptr;
3927 napi_create_string_utf8(env, input.c_str(), input.size(), &src);
3928 napi_value arrayBuffer = nullptr;
3929 void* arrayBufferPtr = nullptr;
3930 size_t arrayBufferSize = 20;
3931 napi_create_arraybuffer(env, arrayBufferSize, &arrayBufferPtr, &arrayBuffer);
3932 napi_value dest = nullptr;
3933 napi_create_typedarray(env, napi_int8_array, arrayBufferSize, arrayBuffer, 0, &dest);
3934 napi_value result = textEncoder.EncodeInto(env, src, dest);
3935 napi_value read = nullptr;
3936 napi_get_named_property(env, result, "read", &read);
3937 uint32_t resRead = 0;
3938 napi_get_value_uint32(env, read, &resRead);
3939 napi_value written = nullptr;
3940 napi_get_named_property(env, result, "written", &written);
3941 uint32_t resWritten = 0;
3942 napi_get_value_uint32(env, written, &resWritten);
3943 ASSERT_EQ(resRead, static_cast<uint32_t>(6)); // 6: resRead expected results
3944 ASSERT_EQ(resWritten, static_cast<uint32_t>(6)); // 6: resWritten expected results
3945 }
3946
3947 /**
3948 * @tc.name: textEncodeIntoTest017
3949 * @tc.desc: Test returns a dictionary object indicating the progress of the encoding
3950 * @tc.type: FUNC
3951 */
3952 HWTEST_F(NativeEngineTest, textEncodeIntoTest017, testing::ext::TestSize.Level0)
3953 {
3954 HILOG_INFO("textEncodeIntoTest017 start");
3955 napi_env env = (napi_env)engine_;
3956 OHOS::Util::TextEncoder textEncoder("shift_jis");
3957 std::string input = "日本語";
3958 napi_value src = nullptr;
3959 napi_create_string_utf8(env, input.c_str(), input.size(), &src);
3960 napi_value arrayBuffer = nullptr;
3961 void* arrayBufferPtr = nullptr;
3962 size_t arrayBufferSize = 20;
3963 napi_create_arraybuffer(env, arrayBufferSize, &arrayBufferPtr, &arrayBuffer);
3964 napi_value dest = nullptr;
3965 napi_create_typedarray(env, napi_int8_array, arrayBufferSize, arrayBuffer, 0, &dest);
3966 napi_value result = textEncoder.EncodeInto(env, src, dest);
3967 napi_value read = nullptr;
3968 napi_get_named_property(env, result, "read", &read);
3969 uint32_t resRead = 0;
3970 napi_get_value_uint32(env, read, &resRead);
3971 napi_value written = nullptr;
3972 napi_get_named_property(env, result, "written", &written);
3973 uint32_t resWritten = 0;
3974 napi_get_value_uint32(env, written, &resWritten);
3975 ASSERT_EQ(resRead, static_cast<uint32_t>(3)); // 3: resRead expected results
3976 ASSERT_EQ(resWritten, static_cast<uint32_t>(6)); // 6: resWritten expected results
3977 }
3978
3979 /**
3980 * @tc.name: textEncodeIntoTest018
3981 * @tc.desc: Test returns a dictionary object indicating the progress of the encoding
3982 * @tc.type: FUNC
3983 */
3984 HWTEST_F(NativeEngineTest, textEncodeIntoTest018, testing::ext::TestSize.Level0)
3985 {
3986 HILOG_INFO("textEncodeIntoTest018 start");
3987 napi_env env = (napi_env)engine_;
3988 OHOS::Util::TextEncoder textEncoder("windows-874");
3989 std::string input = "สวัสดี";
3990 napi_value src = nullptr;
3991 napi_create_string_utf8(env, input.c_str(), input.size(), &src);
3992 napi_value arrayBuffer = nullptr;
3993 void* arrayBufferPtr = nullptr;
3994 size_t arrayBufferSize = 20;
3995 napi_create_arraybuffer(env, arrayBufferSize, &arrayBufferPtr, &arrayBuffer);
3996 napi_value dest = nullptr;
3997 napi_create_typedarray(env, napi_int8_array, arrayBufferSize, arrayBuffer, 0, &dest);
3998 napi_value result = textEncoder.EncodeInto(env, src, dest);
3999 napi_value read = nullptr;
4000 napi_get_named_property(env, result, "read", &read);
4001 uint32_t resRead = 0;
4002 napi_get_value_uint32(env, read, &resRead);
4003 napi_value written = nullptr;
4004 napi_get_named_property(env, result, "written", &written);
4005 uint32_t resWritten = 0;
4006 napi_get_value_uint32(env, written, &resWritten);
4007 ASSERT_EQ(resRead, static_cast<uint32_t>(6)); // 6: resRead expected results
4008 ASSERT_EQ(resWritten, static_cast<uint32_t>(6)); // 6: resWritten expected results
4009 }
4010
4011 /**
4012 * @tc.name: textEncodeIntoTest019
4013 * @tc.desc: Test returns a dictionary object indicating the progress of the encoding
4014 * @tc.type: FUNC
4015 */
4016 HWTEST_F(NativeEngineTest, textEncodeIntoTest019, testing::ext::TestSize.Level0)
4017 {
4018 HILOG_INFO("textEncodeIntoTest019 start");
4019 napi_env env = (napi_env)engine_;
4020 OHOS::Util::TextEncoder textEncoder("big5");
4021 std::string input = "電腦";
4022 napi_value src = nullptr;
4023 napi_create_string_utf8(env, input.c_str(), input.size(), &src);
4024 napi_value arrayBuffer = nullptr;
4025 void* arrayBufferPtr = nullptr;
4026 size_t arrayBufferSize = 20;
4027 napi_create_arraybuffer(env, arrayBufferSize, &arrayBufferPtr, &arrayBuffer);
4028 napi_value dest = nullptr;
4029 napi_create_typedarray(env, napi_int8_array, arrayBufferSize, arrayBuffer, 0, &dest);
4030 napi_value result = textEncoder.EncodeInto(env, src, dest);
4031 napi_value read = nullptr;
4032 napi_get_named_property(env, result, "read", &read);
4033 uint32_t resRead = 0;
4034 napi_get_value_uint32(env, read, &resRead);
4035 napi_value written = nullptr;
4036 napi_get_named_property(env, result, "written", &written);
4037 uint32_t resWritten = 0;
4038 napi_get_value_uint32(env, written, &resWritten);
4039 ASSERT_EQ(resRead, static_cast<uint32_t>(2)); // 2: resRead expected results
4040 ASSERT_EQ(resWritten, static_cast<uint32_t>(4)); // 4: resWritten expected results
4041 }
4042
4043 /**
4044 * @tc.name: textEncodeIntoTest020
4045 * @tc.desc: Test returns a dictionary object indicating the progress of the encoding
4046 * @tc.type: FUNC
4047 */
4048 HWTEST_F(NativeEngineTest, textEncodeIntoTest020, testing::ext::TestSize.Level0)
4049 {
4050 HILOG_INFO("textEncodeIntoTest020 start");
4051 napi_env env = (napi_env)engine_;
4052 OHOS::Util::TextEncoder textEncoder("koi8-u");
4053 std::string input = "Україна";
4054 napi_value src = nullptr;
4055 napi_create_string_utf8(env, input.c_str(), input.size(), &src);
4056 napi_value arrayBuffer = nullptr;
4057 void* arrayBufferPtr = nullptr;
4058 size_t arrayBufferSize = 20;
4059 napi_create_arraybuffer(env, arrayBufferSize, &arrayBufferPtr, &arrayBuffer);
4060 napi_value dest = nullptr;
4061 napi_create_typedarray(env, napi_int8_array, arrayBufferSize, arrayBuffer, 0, &dest);
4062 napi_value result = textEncoder.EncodeInto(env, src, dest);
4063 napi_value read = nullptr;
4064 napi_get_named_property(env, result, "read", &read);
4065 uint32_t resRead = 0;
4066 napi_get_value_uint32(env, read, &resRead);
4067 napi_value written = nullptr;
4068 napi_get_named_property(env, result, "written", &written);
4069 uint32_t resWritten = 0;
4070 napi_get_value_uint32(env, written, &resWritten);
4071 ASSERT_EQ(resRead, static_cast<uint32_t>(7)); // 7: resRead expected results
4072 ASSERT_EQ(resWritten, static_cast<uint32_t>(7)); // 7: resWritten expected results
4073 }
4074
4075 /**
4076 * @tc.name: textEncodeIntoTest021
4077 * @tc.desc: Test returns a dictionary object indicating the progress of the encoding
4078 * @tc.type: FUNC
4079 */
4080 HWTEST_F(NativeEngineTest, textEncodeIntoTest021, testing::ext::TestSize.Level0)
4081 {
4082 HILOG_INFO("textEncodeIntoTest021 start");
4083 napi_env env = (napi_env)engine_;
4084 OHOS::Util::TextEncoder textEncoder("iso-2022-jp");
4085 std::string input = "Hello日本";
4086 napi_value src = nullptr;
4087 napi_create_string_utf8(env, input.c_str(), input.size(), &src);
4088 napi_value arrayBuffer = nullptr;
4089 void* arrayBufferPtr = nullptr;
4090 size_t arrayBufferSize = 20;
4091 napi_create_arraybuffer(env, arrayBufferSize, &arrayBufferPtr, &arrayBuffer);
4092 napi_value dest = nullptr;
4093 napi_create_typedarray(env, napi_int8_array, arrayBufferSize, arrayBuffer, 0, &dest);
4094 napi_value result = textEncoder.EncodeInto(env, src, dest);
4095 napi_value read = nullptr;
4096 napi_get_named_property(env, result, "read", &read);
4097 uint32_t resRead = 0;
4098 napi_get_value_uint32(env, read, &resRead);
4099 napi_value written = nullptr;
4100 napi_get_named_property(env, result, "written", &written);
4101 uint32_t resWritten = 0;
4102 napi_get_value_uint32(env, written, &resWritten);
4103 ASSERT_EQ(resRead, static_cast<uint32_t>(6)); // 6: resRead expected results
4104 ASSERT_EQ(resWritten, static_cast<uint32_t>(13)); // 13: resWritten expected results
4105 }
4106
4107 /**
4108 * @tc.name: textEncodeIntoTest022
4109 * @tc.desc: Test returns a dictionary object indicating the progress of the encoding
4110 * @tc.type: FUNC
4111 */
4112 HWTEST_F(NativeEngineTest, textEncodeIntoTest022, testing::ext::TestSize.Level0)
4113 {
4114 HILOG_INFO("textEncodeIntoTest022 start");
4115 napi_env env = (napi_env)engine_;
4116 OHOS::Util::TextEncoder textEncoder("ibm866");
4117 std::string input = "Компьютер";
4118 napi_value src = nullptr;
4119 napi_create_string_utf8(env, input.c_str(), input.size(), &src);
4120 napi_value arrayBuffer = nullptr;
4121 void* arrayBufferPtr = nullptr;
4122 size_t arrayBufferSize = 20;
4123 napi_create_arraybuffer(env, arrayBufferSize, &arrayBufferPtr, &arrayBuffer);
4124 napi_value dest = nullptr;
4125 napi_create_typedarray(env, napi_int8_array, arrayBufferSize, arrayBuffer, 0, &dest);
4126 napi_value result = textEncoder.EncodeInto(env, src, dest);
4127 napi_value read = nullptr;
4128 napi_get_named_property(env, result, "read", &read);
4129 uint32_t resRead = 0;
4130 napi_get_value_uint32(env, read, &resRead);
4131 napi_value written = nullptr;
4132 napi_get_named_property(env, result, "written", &written);
4133 uint32_t resWritten = 0;
4134 napi_get_value_uint32(env, written, &resWritten);
4135 ASSERT_EQ(resRead, static_cast<uint32_t>(9)); // 9: resRead expected results
4136 ASSERT_EQ(resWritten, static_cast<uint32_t>(9)); // 9: resWritten expected results
4137 }
4138
4139 /**
4140 * @tc.name: textEncodeIntoTest023
4141 * @tc.desc: Test returns a dictionary object indicating the progress of the encoding
4142 * @tc.type: FUNC
4143 */
4144 HWTEST_F(NativeEngineTest, textEncodeIntoTest023, testing::ext::TestSize.Level0)
4145 {
4146 HILOG_INFO("textEncodeIntoTest023 start");
4147 napi_env env = (napi_env)engine_;
4148 OHOS::Util::TextEncoder textEncoder("gbk");
4149 std::string input = "㐀";
4150 napi_value src = nullptr;
4151 napi_create_string_utf8(env, input.c_str(), input.size(), &src);
4152 napi_value arrayBuffer = nullptr;
4153 void* arrayBufferPtr = nullptr;
4154 size_t arrayBufferSize = 20;
4155 napi_create_arraybuffer(env, arrayBufferSize, &arrayBufferPtr, &arrayBuffer);
4156 napi_value dest = nullptr;
4157 napi_create_typedarray(env, napi_int8_array, arrayBufferSize, arrayBuffer, 0, &dest);
4158 napi_value result = textEncoder.EncodeInto(env, src, dest);
4159 napi_value read = nullptr;
4160 napi_get_named_property(env, result, "read", &read);
4161 uint32_t resRead = 0;
4162 napi_get_value_uint32(env, read, &resRead);
4163 napi_value written = nullptr;
4164 napi_get_named_property(env, result, "written", &written);
4165 uint32_t resWritten = 0;
4166 napi_get_value_uint32(env, written, &resWritten);
4167 ASSERT_EQ(resRead, static_cast<uint32_t>(3)); // 3: resRead expected results
4168 ASSERT_EQ(resWritten, static_cast<uint32_t>(3)); // 3: resWritten expected results
4169 }
4170
4171 /**
4172 * @tc.name: textEncodeIntoTest024
4173 * @tc.desc: Test returns a dictionary object indicating the progress of the encoding
4174 * @tc.type: FUNC
4175 */
4176 HWTEST_F(NativeEngineTest, textEncodeIntoTest024, testing::ext::TestSize.Level0)
4177 {
4178 HILOG_INFO("textEncodeIntoTest024 start");
4179 napi_env env = (napi_env)engine_;
4180 OHOS::Util::TextEncoder textEncoder("euc-kr");
4181 std::string input = "한국어";
4182 napi_value src = nullptr;
4183 napi_create_string_utf8(env, input.c_str(), input.size(), &src);
4184 napi_value arrayBuffer = nullptr;
4185 void* arrayBufferPtr = nullptr;
4186 size_t arrayBufferSize = 20;
4187 napi_create_arraybuffer(env, arrayBufferSize, &arrayBufferPtr, &arrayBuffer);
4188 napi_value dest = nullptr;
4189 napi_create_typedarray(env, napi_int8_array, arrayBufferSize, arrayBuffer, 0, &dest);
4190 napi_value result = textEncoder.EncodeInto(env, src, dest);
4191 napi_value read = nullptr;
4192 napi_get_named_property(env, result, "read", &read);
4193 uint32_t resRead = 0;
4194 napi_get_value_uint32(env, read, &resRead);
4195 napi_value written = nullptr;
4196 napi_get_named_property(env, result, "written", &written);
4197 uint32_t resWritten = 0;
4198 napi_get_value_uint32(env, written, &resWritten);
4199 ASSERT_EQ(resRead, static_cast<uint32_t>(3)); // 3: resRead expected results
4200 ASSERT_EQ(resWritten, static_cast<uint32_t>(6)); // 6: resWritten expected results
4201 }
4202
4203 /**
4204 * @tc.name: textEncodeIntoTest025
4205 * @tc.desc: Test returns a dictionary object indicating the progress of the encoding
4206 * @tc.type: FUNC
4207 */
4208 HWTEST_F(NativeEngineTest, textEncodeIntoTest025, testing::ext::TestSize.Level0)
4209 {
4210 HILOG_INFO("textEncodeIntoTest025 start");
4211 napi_env env = (napi_env)engine_;
4212 OHOS::Util::TextEncoder textEncoder("iso-8859-15");
4213 std::string input = "€100";
4214 napi_value src = nullptr;
4215 napi_create_string_utf8(env, input.c_str(), input.size(), &src);
4216 napi_value arrayBuffer = nullptr;
4217 void* arrayBufferPtr = nullptr;
4218 size_t arrayBufferSize = 20;
4219 napi_create_arraybuffer(env, arrayBufferSize, &arrayBufferPtr, &arrayBuffer);
4220 napi_value dest = nullptr;
4221 napi_create_typedarray(env, napi_int8_array, arrayBufferSize, arrayBuffer, 0, &dest);
4222 napi_value result = textEncoder.EncodeInto(env, src, dest);
4223 napi_value read = nullptr;
4224 napi_get_named_property(env, result, "read", &read);
4225 uint32_t resRead = 0;
4226 napi_get_value_uint32(env, read, &resRead);
4227 napi_value written = nullptr;
4228 napi_get_named_property(env, result, "written", &written);
4229 uint32_t resWritten = 0;
4230 napi_get_value_uint32(env, written, &resWritten);
4231 ASSERT_EQ(resRead, static_cast<uint32_t>(4)); // 4: resRead expected results
4232 ASSERT_EQ(resWritten, static_cast<uint32_t>(4)); // 4: resWritten expected results
4233 }
4234
4235 /**
4236 * @tc.name: textEncodeIntoTest026
4237 * @tc.desc: Test returns a dictionary object indicating the progress of the encoding
4238 * @tc.type: FUNC
4239 */
4240 HWTEST_F(NativeEngineTest, textEncodeIntoTest026, testing::ext::TestSize.Level0)
4241 {
4242 HILOG_INFO("textEncodeIntoTest026 start");
4243 napi_env env = (napi_env)engine_;
4244 OHOS::Util::TextEncoder textEncoder("macintosh");
4245 std::string input = "ƒ©±";
4246 napi_value src = nullptr;
4247 napi_create_string_utf8(env, input.c_str(), input.size(), &src);
4248 napi_value arrayBuffer = nullptr;
4249 void* arrayBufferPtr = nullptr;
4250 size_t arrayBufferSize = 20;
4251 napi_create_arraybuffer(env, arrayBufferSize, &arrayBufferPtr, &arrayBuffer);
4252 napi_value dest = nullptr;
4253 napi_create_typedarray(env, napi_int8_array, arrayBufferSize, arrayBuffer, 0, &dest);
4254 napi_value result = textEncoder.EncodeInto(env, src, dest);
4255 napi_value read = nullptr;
4256 napi_get_named_property(env, result, "read", &read);
4257 uint32_t resRead = 0;
4258 napi_get_value_uint32(env, read, &resRead);
4259 napi_value written = nullptr;
4260 napi_get_named_property(env, result, "written", &written);
4261 uint32_t resWritten = 0;
4262 napi_get_value_uint32(env, written, &resWritten);
4263 ASSERT_EQ(resRead, static_cast<uint32_t>(3)); // 3: resRead expected results
4264 ASSERT_EQ(resWritten, static_cast<uint32_t>(3)); // 3: resWritten expected results
4265 }
4266
4267 /**
4268 * @tc.name: textEncodeIntoTest027
4269 * @tc.desc: Test returns a dictionary object indicating the progress of the encoding
4270 * @tc.type: FUNC
4271 */
4272 HWTEST_F(NativeEngineTest, textEncodeIntoTest027, testing::ext::TestSize.Level0)
4273 {
4274 HILOG_INFO("textEncodeIntoTest027 start");
4275 napi_env env = (napi_env)engine_;
4276 OHOS::Util::TextEncoder textEncoder("windows-1250");
4277 std::string input = "Žluťoučký";
4278 napi_value src = nullptr;
4279 napi_create_string_utf8(env, input.c_str(), input.size(), &src);
4280 napi_value arrayBuffer = nullptr;
4281 void* arrayBufferPtr = nullptr;
4282 size_t arrayBufferSize = 20;
4283 napi_create_arraybuffer(env, arrayBufferSize, &arrayBufferPtr, &arrayBuffer);
4284 napi_value dest = nullptr;
4285 napi_create_typedarray(env, napi_int8_array, arrayBufferSize, arrayBuffer, 0, &dest);
4286 napi_value result = textEncoder.EncodeInto(env, src, dest);
4287 napi_value read = nullptr;
4288 napi_get_named_property(env, result, "read", &read);
4289 uint32_t resRead = 0;
4290 napi_get_value_uint32(env, read, &resRead);
4291 napi_value written = nullptr;
4292 napi_get_named_property(env, result, "written", &written);
4293 uint32_t resWritten = 0;
4294 napi_get_value_uint32(env, written, &resWritten);
4295 ASSERT_EQ(resRead, static_cast<uint32_t>(9)); // 9: resRead expected results
4296 ASSERT_EQ(resWritten, static_cast<uint32_t>(9)); // 9: resWritten expected results
4297 }
4298
4299 /**
4300 * @tc.name: textEncodeIntoTest028
4301 * @tc.desc: Test returns a dictionary object indicating the progress of the encoding
4302 * @tc.type: FUNC
4303 */
4304 HWTEST_F(NativeEngineTest, textEncodeIntoTest028, testing::ext::TestSize.Level0)
4305 {
4306 HILOG_INFO("textEncodeIntoTest028 start");
4307 napi_env env = (napi_env)engine_;
4308 OHOS::Util::TextEncoder textEncoder("gb18030");
4309 std::string input = "字";
4310 napi_value src = nullptr;
4311 napi_create_string_utf8(env, input.c_str(), input.size(), &src);
4312 napi_value arrayBuffer = nullptr;
4313 void* arrayBufferPtr = nullptr;
4314 size_t arrayBufferSize = 20;
4315 napi_create_arraybuffer(env, arrayBufferSize, &arrayBufferPtr, &arrayBuffer);
4316 napi_value dest = nullptr;
4317 napi_create_typedarray(env, napi_int8_array, arrayBufferSize, arrayBuffer, 0, &dest);
4318 napi_value result = textEncoder.EncodeInto(env, src, dest);
4319 napi_value read = nullptr;
4320 napi_get_named_property(env, result, "read", &read);
4321 uint32_t resRead = 0;
4322 napi_get_value_uint32(env, read, &resRead);
4323 napi_value written = nullptr;
4324 napi_get_named_property(env, result, "written", &written);
4325 uint32_t resWritten = 0;
4326 napi_get_value_uint32(env, written, &resWritten);
4327 ASSERT_EQ(resRead, static_cast<uint32_t>(3)); // 3: resRead expected results
4328 ASSERT_EQ(resWritten, static_cast<uint32_t>(4)); // 4: resWritten expected results
4329 }
4330
4331 /**
4332 * @tc.name: textIsTypedArray001
4333 * @tc.desc: Test check whether the entered value is the type of typedarray
4334 * @tc.type: FUNCs
4335 */
4336 HWTEST_F(NativeEngineTest, textIsTypedArray001, testing::ext::TestSize.Level0)
4337 {
4338 HILOG_INFO("textIsTypedArray001 start");
4339 napi_env env = (napi_env)engine_;
4340 OHOS::Util::Types types;
4341 napi_value obj = nullptr;
4342 napi_create_object(env, &obj);
4343 bool res = true;
4344 napi_value result = types.IsTypedArray(env, obj);
4345 napi_get_value_bool(env, result, &res);
4346 ASSERT_FALSE(res);
4347 }
4348
4349 /**
4350 * @tc.name: textIsTypedArray002
4351 * @tc.desc: Test check whether the entered value is the type of typedarray
4352 * @tc.type: FUNCs
4353 */
4354 HWTEST_F(NativeEngineTest, textIsTypedArray002, testing::ext::TestSize.Level0)
4355 {
4356 HILOG_INFO("textIsTypedArray002 start");
4357 napi_env env = (napi_env)engine_;
4358 OHOS::Util::Types types;
4359 napi_value arrayBuffer = nullptr;
4360 napi_value src = nullptr;
4361 void* arrayBufferPtr = nullptr;
4362 size_t arrayBufferSize = 20;
4363 napi_create_arraybuffer(env, arrayBufferSize * sizeof(size_t), &arrayBufferPtr, &arrayBuffer);
4364 napi_create_typedarray(env, napi_int8_array, arrayBufferSize, arrayBuffer, 0, &src);
4365 bool res = false;
4366 napi_value result = types.IsTypedArray(env, src);
4367 napi_get_value_bool(env, result, &res);
4368 ASSERT_TRUE(res);
4369 }
4370
4371 /**
4372 * @tc.name: textIsWeakSet001
4373 * @tc.desc: Test check whether the entered value is the type of weakset
4374 * @tc.type: FUNCs
4375 */
4376 HWTEST_F(NativeEngineTest, textIsWeakSet001, testing::ext::TestSize.Level0)
4377 {
4378 HILOG_INFO("textIsWeakSet001 start");
4379 napi_env env = (napi_env)engine_;
4380 OHOS::Util::Types types;
4381 napi_value src = nullptr;
4382 napi_create_object(env, &src);
4383 bool res = true;
4384 napi_value result = types.IsWeakSet(env, src);
4385 napi_get_value_bool(env, result, &res);
4386 ASSERT_FALSE(res);
4387 }
4388
4389 /**
4390 * @tc.name: textIsWeakMap001
4391 * @tc.desc: Test check whether the entered value is the type of weakmap
4392 * @tc.type: FUNCs
4393 */
4394 HWTEST_F(NativeEngineTest, textIsWeakMap001, testing::ext::TestSize.Level0)
4395 {
4396 HILOG_INFO("textIsWeakMap001 start");
4397 napi_env env = (napi_env)engine_;
4398 OHOS::Util::Types types;
4399 napi_value src = nullptr;
4400 std::string input = "str";
4401 napi_create_string_utf8(env, input.c_str(), input.size(), &src);
4402 bool res = true;
4403 napi_value result = types.IsWeakMap(env, src);
4404 napi_get_value_bool(env, result, &res);
4405 ASSERT_FALSE(res);
4406 }
4407
4408 /**
4409 * @tc.name: textIsUint32Array001
4410 * @tc.desc: Test check whether the entered value is the type of uint32array
4411 * @tc.type: FUNCs
4412 */
4413 HWTEST_F(NativeEngineTest, textIsUint32Array001, testing::ext::TestSize.Level0)
4414 {
4415 HILOG_INFO("textIsUint32Array001 start");
4416 napi_env env = (napi_env)engine_;
4417 OHOS::Util::Types types;
4418 napi_value src = nullptr;
4419 std::string input = "str";
4420 napi_create_string_utf8(env, input.c_str(), input.size(), &src);
4421 bool res = true;
4422 napi_value result = types.IsUint32Array(env, src);
4423 napi_get_value_bool(env, result, &res);
4424 ASSERT_FALSE(res);
4425 }
4426
4427 /**
4428 * @tc.name: textIsUint32Array002
4429 * @tc.desc: Test check whether the entered value is the type of uint32array
4430 * @tc.type: FUNCs
4431 */
4432 HWTEST_F(NativeEngineTest, textIsUint32Array002, testing::ext::TestSize.Level0)
4433 {
4434 HILOG_INFO("textIsUint32Array002 start");
4435 napi_env env = (napi_env)engine_;
4436 OHOS::Util::Types types;
4437 napi_value obj = nullptr;
4438 napi_create_object(env, &obj);
4439 bool res = true;
4440 napi_value result = types.IsUint32Array(env, obj);
4441 napi_get_value_bool(env, result, &res);
4442 ASSERT_FALSE(res);
4443 }
4444
4445 /**
4446 * @tc.name: textIsUint32Array003
4447 * @tc.desc: Test check whether the entered value is the type of uint32array
4448 * @tc.type: FUNCs
4449 */
4450 HWTEST_F(NativeEngineTest, textIsUint32Array003, testing::ext::TestSize.Level0)
4451 {
4452 HILOG_INFO("textIsUint32Array003 start");
4453 napi_env env = (napi_env)engine_;
4454 OHOS::Util::Types types;
4455 napi_value arrayBuffer = nullptr;
4456 size_t arrayBufferSize = 20;
4457 void* data = nullptr;
4458 napi_create_arraybuffer(env, arrayBufferSize * sizeof(size_t), &data, &arrayBuffer);
4459 napi_value src = nullptr;
4460 napi_create_typedarray(env, napi_int8_array, arrayBufferSize, arrayBuffer, 0, &src);
4461 bool res = true;
4462 napi_value result = types.IsUint32Array(env, src);
4463 napi_get_value_bool(env, result, &res);
4464 ASSERT_FALSE(res);
4465 }
4466
4467 /**
4468 * @tc.name: textIsUint32Array004
4469 * @tc.desc: Test check whether the entered value is the type of uint32array
4470 * @tc.type: FUNCs
4471 */
4472 HWTEST_F(NativeEngineTest, textIsUint32Array004, testing::ext::TestSize.Level0)
4473 {
4474 HILOG_INFO("textIsUint32Array004 start");
4475 napi_env env = (napi_env)engine_;
4476 OHOS::Util::Types types;
4477 napi_value arrayBuffer = nullptr;
4478 size_t arrayBufferSize = 20;
4479 void* data = nullptr;
4480 napi_create_arraybuffer(env, arrayBufferSize * sizeof(size_t), &data, &arrayBuffer);
4481 napi_value src = nullptr;
4482 napi_create_typedarray(env, napi_uint32_array, arrayBufferSize, arrayBuffer, 0, &src);
4483 bool res = false;
4484 napi_value result = types.IsUint32Array(env, src);
4485 napi_get_value_bool(env, result, &res);
4486 ASSERT_TRUE(res);
4487 }
4488
4489 /**
4490 * @tc.name: textIsUint16Array001
4491 * @tc.desc: Test check whether the entered value is the type of uint16array
4492 * @tc.type: FUNCs
4493 */
4494 HWTEST_F(NativeEngineTest, textIsUint16Array001, testing::ext::TestSize.Level0)
4495 {
4496 HILOG_INFO("textIsUint16Array001 start");
4497 napi_env env = (napi_env)engine_;
4498 OHOS::Util::Types types;
4499 napi_value obj = nullptr;
4500 napi_create_object(env, &obj);
4501 bool res = true;
4502 napi_value result = types.IsUint16Array(env, obj);
4503 napi_get_value_bool(env, result, &res);
4504 ASSERT_FALSE(res);
4505 }
4506
4507 /**
4508 * @tc.name: textIsUint16Array002
4509 * @tc.desc: Test check whether the entered value is the type of uint16array
4510 * @tc.type: FUNCs
4511 */
4512 HWTEST_F(NativeEngineTest, textIsUint16Array002, testing::ext::TestSize.Level0)
4513 {
4514 HILOG_INFO("textIsUint16Array002 start");
4515 napi_env env = (napi_env)engine_;
4516 OHOS::Util::Types types;
4517 napi_value arrayBuffer = nullptr;
4518 size_t arrayBufferSize = 20;
4519 void* data = nullptr;
4520 napi_create_arraybuffer(env, arrayBufferSize * sizeof(size_t), &data, &arrayBuffer);
4521 napi_value src = nullptr;
4522 napi_create_typedarray(env, napi_uint16_array, arrayBufferSize, arrayBuffer, 0, &src);
4523 bool res = false;
4524 napi_value result = types.IsUint16Array(env, src);
4525 napi_get_value_bool(env, result, &res);
4526 ASSERT_TRUE(res);
4527 }
4528
4529 /**
4530 * @tc.name: textIsUint8ClampedArray001
4531 * @tc.desc: Test check whether the entered value is the type of uint8clampedarray
4532 * @tc.type: FUNCs
4533 */
4534 HWTEST_F(NativeEngineTest, textIsUint8ClampedArray001, testing::ext::TestSize.Level0)
4535 {
4536 HILOG_INFO("textIsUint8ClampedArray001 start");
4537 napi_env env = (napi_env)engine_;
4538 OHOS::Util::Types types;
4539 napi_value obj = nullptr;
4540 napi_create_object(env, &obj);
4541 bool res = true;
4542 napi_value result = types.IsUint8ClampedArray(env, obj);
4543 napi_get_value_bool(env, result, &res);
4544 ASSERT_FALSE(res);
4545 }
4546
4547 /**
4548 * @tc.name: textIsUint8ClampedArray002
4549 * @tc.desc: Test check whether the entered value is the type of uint8clampedarray
4550 * @tc.type: FUNCs
4551 */
4552 HWTEST_F(NativeEngineTest, textIsUint8ClampedArray002, testing::ext::TestSize.Level0)
4553 {
4554 HILOG_INFO("textIsUint8ClampedArray002 start");
4555 napi_env env = (napi_env)engine_;
4556 OHOS::Util::Types types;
4557 napi_value arrayBuffer = nullptr;
4558 size_t arrayBufferSize = 20;
4559 void* data = nullptr;
4560 napi_create_arraybuffer(env, arrayBufferSize * sizeof(size_t), &data, &arrayBuffer);
4561 napi_value src = nullptr;
4562 napi_create_typedarray(env, napi_uint8_clamped_array, arrayBufferSize, arrayBuffer, 0, &src);
4563 bool res = false;
4564 napi_value result = types.IsUint8ClampedArray(env, src);
4565 napi_get_value_bool(env, result, &res);
4566 ASSERT_TRUE(res);
4567 }
4568
4569 /**
4570 * @tc.name: textIsUint8Array001
4571 * @tc.desc: Test check whether the entered value is the type of uint8array
4572 * @tc.type: FUNCs
4573 */
4574 HWTEST_F(NativeEngineTest, textIsUint8Array001, testing::ext::TestSize.Level0)
4575 {
4576 HILOG_INFO("textIsUint8Array001 start");
4577 napi_env env = (napi_env)engine_;
4578 OHOS::Util::Types types;
4579 napi_value obj = nullptr;
4580 napi_create_object(env, &obj);
4581 bool res = true;
4582 napi_value result = types.IsUint8Array(env, obj);
4583 napi_get_value_bool(env, result, &res);
4584 ASSERT_FALSE(res);
4585 }
4586
4587 /**
4588 * @tc.name: textIsUint8Array002
4589 * @tc.desc: Test check whether the entered value is the type of uint8array
4590 * @tc.type: FUNCs
4591 */
4592 HWTEST_F(NativeEngineTest, textIsUint8Array002, testing::ext::TestSize.Level0)
4593 {
4594 HILOG_INFO("textIsUint8Array002 start");
4595 napi_env env = (napi_env)engine_;
4596 OHOS::Util::Types types;
4597 napi_value arrayBuffer = nullptr;
4598 size_t arrayBufferSize = 20;
4599 void* data = nullptr;
4600 napi_create_arraybuffer(env, arrayBufferSize * sizeof(size_t), &data, &arrayBuffer);
4601 napi_value src = nullptr;
4602 napi_create_typedarray(env, napi_uint8_array, arrayBufferSize, arrayBuffer, 0, &src);
4603 bool res = false;
4604 napi_value result = types.IsUint8Array(env, src);
4605 napi_get_value_bool(env, result, &res);
4606 ASSERT_TRUE(res);
4607 }
4608
4609 /**
4610 * @tc.name: testIsSymbolObject001
4611 * @tc.desc: Test check whether the entered value is the symbol type of object
4612 * @tc.type: FUNCs
4613 */
4614 HWTEST_F(NativeEngineTest, testIsSymbolObject001, testing::ext::TestSize.Level0)
4615 {
4616 HILOG_INFO("testIsSymbolObject001 start");
4617 napi_env env = (napi_env)engine_;
4618 OHOS::Util::Types types;
4619 napi_value obj = nullptr;
4620 napi_create_object(env, &obj);
4621 bool res = true;
4622 napi_value result = types.IsSymbolObject(env, obj);
4623 napi_get_value_bool(env, result, &res);
4624 ASSERT_FALSE(res);
4625 }
4626
4627 /**
4628 * @tc.name: testIsSymbolObject002
4629 * @tc.desc: Test check whether the entered value is the symbol type of object
4630 * @tc.type: FUNCs
4631 */
4632 HWTEST_F(NativeEngineTest, testIsSymbolObject002, testing::ext::TestSize.Level0)
4633 {
4634 HILOG_INFO("testIsSymbolObject002 start");
4635 napi_env env = (napi_env)engine_;
4636 OHOS::Util::Types types;
4637
4638 std::string input = "testSymbol";
4639 napi_value testSymbol = nullptr;
4640 napi_create_string_utf8(env, input.c_str(), input.size(), &testSymbol);
4641 napi_value symbolVal = nullptr;
4642 napi_create_symbol(env, testSymbol, &symbolVal);
4643
4644 bool res = true;
4645 napi_value result = types.IsSymbolObject(env, symbolVal);
4646 napi_get_value_bool(env, result, &res);
4647 ASSERT_FALSE(res);
4648 }
4649
4650 /**
4651 * @tc.name: testIsStringObject001
4652 * @tc.desc: Test check whether the entered value is the string type of object
4653 * @tc.type: FUNCs
4654 */
4655 HWTEST_F(NativeEngineTest, testIsStringObject001, testing::ext::TestSize.Level0)
4656 {
4657 HILOG_INFO("testIsStringObject001 start");
4658 napi_env env = (napi_env)engine_;
4659 OHOS::Util::Types types;
4660 napi_value obj = nullptr;
4661 napi_create_object(env, &obj);
4662 bool res = true;
4663 napi_value result = types.IsStringObject(env, obj);
4664 napi_get_value_bool(env, result, &res);
4665 ASSERT_FALSE(res);
4666 }
4667
4668 /**
4669 * @tc.name: testIsSharedArrayBuffer001
4670 * @tc.desc: Test check whether the entered value is the type of sharedarraybuffer
4671 * @tc.type: FUNCs
4672 */
4673 HWTEST_F(NativeEngineTest, testIsSharedArrayBuffer001, testing::ext::TestSize.Level0)
4674 {
4675 HILOG_INFO("testIsSharedArrayBuffer001 start");
4676 napi_env env = (napi_env)engine_;
4677 OHOS::Util::Types types;
4678 napi_value obj = nullptr;
4679 napi_create_object(env, &obj);
4680 bool res = true;
4681 napi_value result = types.IsSharedArrayBuffer(env, obj);
4682 napi_get_value_bool(env, result, &res);
4683 ASSERT_FALSE(res);
4684 }
4685
4686 /**
4687 * @tc.name: testIsSharedArrayBuffer002
4688 * @tc.desc: Test check whether the entered value is the type of sharedarraybuffer
4689 * @tc.type: FUNCs
4690 */
4691 HWTEST_F(NativeEngineTest, testIsSharedArrayBuffer002, testing::ext::TestSize.Level0)
4692 {
4693 HILOG_INFO("testIsSharedArrayBuffer002 start");
4694 napi_env env = (napi_env)engine_;
4695 OHOS::Util::Types types;
4696 napi_value arrayBuffer = nullptr;
4697 void* arrayBufferPtr = nullptr;
4698 size_t arrayBufferSize = 1024;
4699 napi_create_arraybuffer(env, arrayBufferSize * sizeof(size_t), &arrayBufferPtr, &arrayBuffer);
4700 bool res = true;
4701 napi_value result = types.IsSharedArrayBuffer(env, arrayBuffer);
4702 napi_get_value_bool(env, result, &res);
4703 ASSERT_FALSE(res);
4704 }
4705
4706 /**
4707 * @tc.name: testIsSetIterator001
4708 * @tc.desc: Test check whether the entered value is the iterator type of set
4709 * @tc.type: FUNCs
4710 */
4711 HWTEST_F(NativeEngineTest, testIsSetIterator001, testing::ext::TestSize.Level0)
4712 {
4713 HILOG_INFO("testIsSetIterator001 start");
4714 napi_env env = (napi_env)engine_;
4715 OHOS::Util::Types types;
4716 napi_value obj = nullptr;
4717 napi_create_object(env, &obj);
4718 bool res = true;
4719 napi_value result = types.IsSetIterator(env, obj);
4720 napi_get_value_bool(env, result, &res);
4721 ASSERT_FALSE(res);
4722 }
4723
4724 /**
4725 * @tc.name: testIsSet001
4726 * @tc.desc: Test check whether the entered value is the type of set
4727 * @tc.type: FUNCs
4728 */
4729 HWTEST_F(NativeEngineTest, testIsSet001, testing::ext::TestSize.Level0)
4730 {
4731 HILOG_INFO("testIsSet001 start");
4732 napi_env env = (napi_env)engine_;
4733 OHOS::Util::Types types;
4734 napi_value obj = nullptr;
4735 napi_create_object(env, &obj);
4736 bool res = true;
4737 napi_value result = types.IsSet(env, obj);
4738 napi_get_value_bool(env, result, &res);
4739 ASSERT_FALSE(res);
4740 }
4741
4742 /**
4743 * @tc.name: testIsRegExp001
4744 * @tc.desc: Test check whether the entered value is the type of regexp
4745 * @tc.type: FUNCs
4746 */
4747 HWTEST_F(NativeEngineTest, testIsRegExp001, testing::ext::TestSize.Level0)
4748 {
4749 HILOG_INFO("testIsRegExp001 start");
4750 napi_env env = (napi_env)engine_;
4751 OHOS::Util::Types types;
4752 napi_value obj = nullptr;
4753 napi_create_object(env, &obj);
4754 bool res = true;
4755 napi_value result = types.IsRegExp(env, obj);
4756 napi_get_value_bool(env, result, &res);
4757 ASSERT_FALSE(res);
4758 }
4759
4760 /**
4761 * @tc.name: testIsProxy001
4762 * @tc.desc: Test check whether the entered value is the type of proxy
4763 * @tc.type: FUNCs
4764 */
4765 HWTEST_F(NativeEngineTest, testIsProxy001, testing::ext::TestSize.Level0)
4766 {
4767 HILOG_INFO("testIsProxy001 start");
4768 napi_env env = (napi_env)engine_;
4769 OHOS::Util::Types types;
4770 napi_value obj = nullptr;
4771 napi_create_object(env, &obj);
4772 bool res = true;
4773 napi_value result = types.IsProxy(env, obj);
4774 napi_get_value_bool(env, result, &res);
4775 ASSERT_FALSE(res);
4776 }
4777
4778 /**
4779 * @tc.name: testIsPromise001
4780 * @tc.desc: Test check whether the entered value is the type of promise
4781 * @tc.type: FUNCs
4782 */
4783 HWTEST_F(NativeEngineTest, testIsPromise001, testing::ext::TestSize.Level0)
4784 {
4785 HILOG_INFO("testIsPromise001 start");
4786 napi_env env = (napi_env)engine_;
4787 OHOS::Util::Types types;
4788 napi_value obj = nullptr;
4789 napi_create_object(env, &obj);
4790 bool res = true;
4791 napi_value result = types.IsPromise(env, obj);
4792 napi_get_value_bool(env, result, &res);
4793 ASSERT_FALSE(res);
4794 }
4795
4796 /**
4797 * @tc.name: testIsPromise002
4798 * @tc.desc: Test check whether the entered value is the type of promise
4799 * @tc.type: FUNCs
4800 */
4801 HWTEST_F(NativeEngineTest, testIsPromise002, testing::ext::TestSize.Level0)
4802 {
4803 HILOG_INFO("testIsPromise002 start");
4804 napi_env env = (napi_env)engine_;
4805 OHOS::Util::Types types;
4806 napi_deferred deferred = nullptr;
4807 napi_value promise = nullptr;
4808 ASSERT_CHECK_CALL(napi_create_promise(env, &deferred, &promise));
4809 ASSERT_NE(deferred, nullptr);
4810 ASSERT_NE(promise, nullptr);
4811 bool res = false;
4812 napi_value result = types.IsPromise(env, promise);
4813 napi_get_value_bool(env, result, &res);
4814 ASSERT_TRUE(res);
4815 }
4816
4817 /**
4818 * @tc.name: testIsNumberObject001
4819 * @tc.desc: Test check whether the entered value is the number type of object
4820 * @tc.type: FUNCs
4821 */
4822 HWTEST_F(NativeEngineTest, testIsNumberObject001, testing::ext::TestSize.Level0)
4823 {
4824 HILOG_INFO("testIsNumberObject001 start");
4825 napi_env env = (napi_env)engine_;
4826 OHOS::Util::Types types;
4827 napi_value obj = nullptr;
4828 napi_create_object(env, &obj);
4829 bool res = true;
4830 napi_value result = types.IsNumberObject(env, obj);
4831 napi_get_value_bool(env, result, &res);
4832 ASSERT_FALSE(res);
4833 }
4834
4835 /**
4836 * @tc.name: testIsNativeError001
4837 * @tc.desc: Test check whether the entered value is of type error
4838 * @tc.type: FUNCs
4839 */
4840 HWTEST_F(NativeEngineTest, testIsNativeError001, testing::ext::TestSize.Level0)
4841 {
4842 HILOG_INFO("testIsNativeError001 start");
4843 napi_env env = (napi_env)engine_;
4844 OHOS::Util::Types types;
4845 napi_value obj = nullptr;
4846 napi_create_object(env, &obj);
4847 bool res = true;
4848 napi_value result = types.IsNativeError(env, obj);
4849 napi_get_value_bool(env, result, &res);
4850 ASSERT_FALSE(res);
4851 }
4852
4853 /**
4854 * @tc.name: testIsNativeError002
4855 * @tc.desc: Test check whether the entered value is of type error
4856 * @tc.type: FUNCs
4857 */
4858 HWTEST_F(NativeEngineTest, testIsNativeError002, testing::ext::TestSize.Level0)
4859 {
4860 HILOG_INFO("testIsNativeError002 start");
4861 napi_env env = (napi_env)engine_;
4862 OHOS::Util::Types types;
4863
4864 napi_value code = nullptr;
4865 napi_value message = nullptr;
4866 std::string input = "abc123";
4867 ASSERT_CHECK_CALL(napi_create_string_utf8(env, input.c_str(), NAPI_AUTO_LENGTH, &code));
4868 ASSERT_CHECK_CALL(napi_create_string_utf8(env, input.c_str(), NAPI_AUTO_LENGTH, &message));
4869
4870 napi_value error = nullptr;
4871 ASSERT_CHECK_CALL(napi_create_error(env, code, message, &error));
4872 ASSERT_TRUE(error != nullptr);
4873
4874 bool res = false;
4875 napi_value result = types.IsNativeError(env, error);
4876 napi_get_value_bool(env, result, &res);
4877 ASSERT_TRUE(res);
4878 }
4879
4880 /**
4881 * @tc.name: testIsModuleNamespaceObject001
4882 * @tc.desc: Test check whether the entered value is the module name space type of object
4883 * @tc.type: FUNCs
4884 */
4885 HWTEST_F(NativeEngineTest, testIsModuleNamespaceObject001, testing::ext::TestSize.Level0)
4886 {
4887 HILOG_INFO("testIsModuleNamespaceObject001 start");
4888 napi_env env = (napi_env)engine_;
4889 OHOS::Util::Types types;
4890 napi_value obj = nullptr;
4891 napi_create_object(env, &obj);
4892 bool res = true;
4893 napi_value result = types.IsModuleNamespaceObject(env, obj);
4894 napi_get_value_bool(env, result, &res);
4895 ASSERT_FALSE(res);
4896 }
4897
4898 /**
4899 * @tc.name: testIsMapIterator001
4900 * @tc.desc: Test check whether the entered value is the iterator type of map
4901 * @tc.type: FUNCs
4902 */
4903 HWTEST_F(NativeEngineTest, testIsMapIterator001, testing::ext::TestSize.Level0)
4904 {
4905 HILOG_INFO("testIsMapIterator001 start");
4906 napi_env env = (napi_env)engine_;
4907 OHOS::Util::Types types;
4908 napi_value obj = nullptr;
4909 napi_create_object(env, &obj);
4910 bool res = true;
4911 napi_value result = types.IsMapIterator(env, obj);
4912 napi_get_value_bool(env, result, &res);
4913 ASSERT_FALSE(res);
4914 }
4915
4916 /**
4917 * @tc.name: textIsDateTest001
4918 * @tc.desc: Check if the input value is of type Date.
4919 * @tc.type: FUNC
4920 */
4921 HWTEST_F(NativeEngineTest, textIsDateTest001, testing::ext::TestSize.Level0)
4922 {
4923 HILOG_INFO("textIsDateTest001 start");
4924 napi_env env = (napi_env)engine_;
4925 OHOS::Util::Types type;
4926 napi_value createResult = nullptr;
4927 double time = 202110181203150; // Date and Time
4928 napi_status status = napi_create_date(env, time, &createResult);
4929 if (status == napi_ok) {
4930 HILOG_INFO("Types::napi_create_date success");
4931 } else {
4932 HILOG_INFO("Types::napi_create_date fail");
4933 }
4934 napi_value result = type.IsDate(env, createResult);
4935 bool res = false;
4936 napi_get_value_bool(env, result, &res);
4937 ASSERT_TRUE(res);
4938 }
4939
4940 /**
4941 * @tc.name: textIsExternalTest001
4942 * @tc.desc: Check if the input value is of type native External.
4943 * @tc.type: FUNC
4944 */
4945 HWTEST_F(NativeEngineTest, textIsExternalTest001, testing::ext::TestSize.Level0)
4946 {
4947 HILOG_INFO("textIsExternalTest001 start");
4948 napi_env env = (napi_env)engine_;
4949 OHOS::Util::Types type;
4950 napi_value dataVal = nullptr;
4951 int* raw = new int(1);
4952 napi_status status = napi_create_external(env, (void*) raw, NULL, NULL, &dataVal);
4953 if (status == napi_ok) {
4954 HILOG_INFO("Types::napi_create_external success");
4955 } else {
4956 HILOG_INFO("Types::napi_create_external fail");
4957 }
4958 napi_value result = type.IsExternal(env, dataVal);
4959 bool res = false;
4960 napi_get_value_bool(env, result, &res);
4961 ASSERT_TRUE(res);
4962 }
4963
4964 /**
4965 * @tc.name: textIsMapTest001
4966 * @tc.desc: Check if the input value is of Map type.
4967 * @tc.type: FUNC
4968 */
4969 HWTEST_F(NativeEngineTest, textIsMapTest001, testing::ext::TestSize.Level0)
4970 {
4971 HILOG_INFO("textIsMapTest001 start");
4972 napi_env env = (napi_env)engine_;
4973 OHOS::Util::Types type;
4974 napi_value resMap = nullptr;
4975 napi_create_map(env, &resMap);
4976 napi_value result = type.IsMap(env, resMap);
4977 bool res = false;
4978 napi_get_value_bool(env, result, &res);
4979 ASSERT_TRUE(res);
4980 }
4981
4982 /**
4983 * @tc.name: textIsMapTest002
4984 * @tc.desc: Check if the input value is of Map type.
4985 * @tc.type: FUNC
4986 */
4987 HWTEST_F(NativeEngineTest, textIsMapTest002, testing::ext::TestSize.Level0)
4988 {
4989 HILOG_INFO("textIsMapTest002 start");
4990 napi_env env = (napi_env)engine_;
4991 OHOS::Util::Types type;
4992 napi_value dest = nullptr;
4993 napi_create_sendable_map(env, &dest);
4994 napi_value result = type.IsMap(env, dest);
4995 bool res = false;
4996 napi_get_value_bool(env, result, &res);
4997 ASSERT_TRUE(res);
4998 }
4999
5000 /**
5001 * @tc.name: textIsAnyArrayBufferTest001
5002 * @tc.desc: Check if the input value is of type ArrayBuffer or SharedArrayBuffer.
5003 * @tc.type: FUNC
5004 */
5005 HWTEST_F(NativeEngineTest, textIsAnyArrayBufferTest001, testing::ext::TestSize.Level0)
5006 {
5007 HILOG_INFO("textIsAnyArrayBufferTest001 start");
5008 napi_env env = (napi_env)engine_;
5009 OHOS::Util::Types type;
5010 napi_value arrayBuffer = nullptr;
5011 void* arrayBufferPtr = nullptr;
5012 size_t arrayBufferSize = 1;
5013 napi_create_arraybuffer(env, arrayBufferSize * sizeof(size_t), &arrayBufferPtr, &arrayBuffer);
5014 napi_value result = type.IsAnyArrayBuffer(env, arrayBuffer);
5015 bool res = false;
5016 napi_get_value_bool(env, result, &res);
5017 ASSERT_TRUE(res);
5018 }
5019
5020 /**
5021 * @tc.name: textIsAnyArrayBufferTest002
5022 * @tc.desc: Check if the input value is of type ArrayBuffer or SharedArrayBuffer.
5023 * @tc.type: FUNC
5024 */
5025 HWTEST_F(NativeEngineTest, textIsAnyArrayBufferTest002, testing::ext::TestSize.Level0)
5026 {
5027 HILOG_INFO("textIsAnyArrayBufferTest002 start");
5028 napi_env env = (napi_env)engine_;
5029 OHOS::Util::Types type;
5030 napi_value testFlag = nullptr;
5031 bool test = false;
5032 napi_get_value_bool(env, testFlag, &test);
5033 napi_value result = type.IsAnyArrayBuffer(env, testFlag);
5034 bool res = false;
5035 napi_get_value_bool(env, result, &res);
5036 ASSERT_FALSE(res);
5037 }
5038
5039 /**
5040 * @tc.name: textIsArrayBufferViewTest001
5041 * @tc.desc: Check if the input value is of the built-in ArrayBufferView auxiliary type.
5042 * @tc.type: FUNC
5043 */
5044 HWTEST_F(NativeEngineTest, textIsArrayBufferViewTest001, testing::ext::TestSize.Level0)
5045 {
5046 HILOG_INFO("textIsArrayBufferViewTest001 start");
5047 napi_env env = (napi_env)engine_;
5048 OHOS::Util::Types type;
5049 napi_value testFlag = nullptr;
5050 bool test = false;
5051 napi_get_value_bool(env, testFlag, &test);
5052 napi_value result = type.IsArrayBufferView(env, testFlag);
5053 bool res = false;
5054 napi_get_value_bool(env, result, &res);
5055 ASSERT_FALSE(res);
5056 }
5057
5058 /**
5059 * @tc.name: textIsArrayBufferViewTest002
5060 * @tc.desc: Check if the input value is of the built-in ArrayBufferView auxiliary type.
5061 * @tc.type: FUNC
5062 */
5063 HWTEST_F(NativeEngineTest, textIsArrayBufferViewTest002, testing::ext::TestSize.Level0)
5064 {
5065 HILOG_INFO("textIsArrayBufferViewTest002 start");
5066 napi_env env = (napi_env)engine_;
5067 OHOS::Util::Types type;
5068 napi_value arrayBuffer = nullptr;
5069 void* arrayBufferPtr = nullptr;
5070 size_t arrayBufferSize = 1;
5071 napi_create_arraybuffer(env, arrayBufferSize, &arrayBufferPtr, &arrayBuffer);
5072 napi_value dataview = nullptr;
5073 napi_create_dataview(env, arrayBufferSize, arrayBuffer, 0, &dataview);
5074 napi_value result = type.IsArrayBufferView(env, dataview);
5075 bool res = false;
5076 napi_get_value_bool(env, result, &res);
5077 ASSERT_TRUE(res);
5078 }
5079
5080 /**
5081 * @tc.name: textIsDataViewTest001
5082 * @tc.desc: Check if the input value is of type WebView.
5083 * @tc.type: FUNC
5084 */
5085 HWTEST_F(NativeEngineTest, textIsDataViewTest001, testing::ext::TestSize.Level0)
5086 {
5087 HILOG_INFO("textIsDataViewTest001 start");
5088 napi_env env = (napi_env)engine_;
5089 OHOS::Util::Types type;
5090 napi_value arrBuffer = nullptr;
5091 void* arrBufferPtr = nullptr;
5092 size_t arrBufferSize = 1;
5093 napi_create_arraybuffer(env, arrBufferSize * sizeof(size_t), &arrBufferPtr, &arrBuffer);
5094 napi_value dataView = nullptr;
5095 napi_create_dataview(env, arrBufferSize * sizeof(size_t), arrBuffer, 0, &dataView);
5096 napi_value result = type.IsDataView(env, dataView);
5097 bool res = false;
5098 napi_get_value_bool(env, result, &res);
5099 ASSERT_TRUE(res);
5100 }
5101
5102 /**
5103 * @tc.name: textIsArrayBufferViewTest003
5104 * @tc.desc: Check if the input value is of the built-in ArrayBufferView auxiliary type.
5105 * @tc.type: FUNC
5106 */
5107 HWTEST_F(NativeEngineTest, textIsArrayBufferViewTest003, testing::ext::TestSize.Level0)
5108 {
5109 HILOG_INFO("textIsArrayBufferViewTest003 start");
5110 napi_env env = (napi_env)engine_;
5111 OHOS::Util::Types type;
5112 size_t byteLength = 1;
5113 void* data = nullptr;
5114 napi_value resultBuff = nullptr;
5115 napi_create_arraybuffer(env, byteLength, &data, &resultBuff);
5116 unsigned char arr[1] = {0x1};
5117 int ret = memcpy_s(data, sizeof(arr), reinterpret_cast<void*>(arr), sizeof(arr));
5118 ASSERT_EQ(0, ret);
5119 napi_value int8Array = nullptr;
5120 napi_create_typedarray(env, napi_int8_array, byteLength, resultBuff, 0, &int8Array);
5121 napi_value result = type.IsArrayBufferView(env, int8Array);
5122 bool res = false;
5123 napi_get_value_bool(env, result, &res);
5124 ASSERT_TRUE(res);
5125 }
5126
5127 /**
5128 * @tc.name: textIsArrayBufferViewTest004
5129 * @tc.desc: Check if the input value is of the built-in ArrayBufferView auxiliary type.
5130 * @tc.type: FUNC
5131 */
5132 HWTEST_F(NativeEngineTest, textIsArrayBufferViewTest004, testing::ext::TestSize.Level0)
5133 {
5134 HILOG_INFO("textIsArrayBufferViewTest004 start");
5135 napi_env env = (napi_env)engine_;
5136 OHOS::Util::Types type;
5137 size_t byteLength = 1;
5138 void* data = nullptr;
5139 napi_value resultBuff = nullptr;
5140 napi_create_arraybuffer(env, byteLength, &data, &resultBuff);
5141 unsigned char arr[1] = {0x1};
5142 int ret = memcpy_s(data, sizeof(arr), reinterpret_cast<void*>(arr), sizeof(arr));
5143 ASSERT_EQ(0, ret);
5144 napi_value uint8Array = nullptr;
5145 napi_create_typedarray(env, napi_uint8_array, byteLength, resultBuff, 0, &uint8Array);
5146 napi_value result = type.IsArrayBufferView(env, uint8Array);
5147 bool res = false;
5148 napi_get_value_bool(env, result, &res);
5149 ASSERT_TRUE(res);
5150 }
5151
5152 /**
5153 * @tc.name: textIsArrayBufferViewTest005
5154 * @tc.desc: Check if the input value is of the built-in ArrayBufferView auxiliary type.
5155 * @tc.type: FUNC
5156 */
5157 HWTEST_F(NativeEngineTest, textIsArrayBufferViewTest005, testing::ext::TestSize.Level0)
5158 {
5159 HILOG_INFO("textIsArrayBufferViewTest005 start");
5160 napi_env env = (napi_env)engine_;
5161 OHOS::Util::Types type;
5162 size_t byteLength = 1;
5163 void* data = nullptr;
5164 napi_value resultBuff = nullptr;
5165 napi_create_arraybuffer(env, byteLength * sizeof(int16_t), &data, &resultBuff);
5166 int16_t arr[1] = {0x1};
5167 int ret = memcpy_s(data, sizeof(arr), reinterpret_cast<void*>(arr), sizeof(arr));
5168 ASSERT_EQ(0, ret);
5169 napi_value int16Array = nullptr;
5170 napi_create_typedarray(env, napi_int16_array, byteLength, resultBuff, 0, &int16Array);
5171 napi_value result = type.IsArrayBufferView(env, int16Array);
5172 bool res = false;
5173 napi_get_value_bool(env, result, &res);
5174 ASSERT_TRUE(res);
5175 }
5176
5177 /**
5178 * @tc.name: textIsArgumentsObjectTest001
5179 * @tc.desc: Check if the input value is an arguments object.
5180 * @tc.type: FUNC
5181 */
5182 HWTEST_F(NativeEngineTest, textIsArgumentsObjectTest001, testing::ext::TestSize.Level0)
5183 {
5184 HILOG_INFO("textIsArgumentsObjectTest001 start");
5185 napi_env env = (napi_env)engine_;
5186 OHOS::Util::Types type;
5187 bool test = false;
5188 napi_value testVal = nullptr;
5189 napi_get_value_bool(env, testVal, &test);
5190 napi_value result = type.IsArgumentsObject(env, testVal);
5191 bool res = false;
5192 napi_get_value_bool(env, result, &res);
5193 ASSERT_FALSE(res);
5194 }
5195
5196 /**
5197 * @tc.name: textIsArrayBufferTest001
5198 * @tc.desc: Check if the input value is of type ArrayBuffer.
5199 * @tc.type: FUNC
5200 */
5201 HWTEST_F(NativeEngineTest, textIsArrayBufferTest001, testing::ext::TestSize.Level0)
5202 {
5203 HILOG_INFO("textIsArrayBufferTest001 start");
5204 napi_env env = (napi_env)engine_;
5205 OHOS::Util::Types type;
5206 napi_value arrayBuffer = nullptr;
5207 void* arrayBufferPtr = nullptr;
5208 size_t arrayBufferSize = 1;
5209 napi_create_arraybuffer(env, arrayBufferSize, &arrayBufferPtr, &arrayBuffer);
5210 napi_value result = type.IsArrayBuffer(env, arrayBuffer);
5211 bool res = false;
5212 napi_get_value_bool(env, result, &res);
5213 ASSERT_TRUE(res);
5214 }
5215
5216 /**
5217 * @tc.name: textIsAsyncFunctionTest001
5218 * @tc.desc: Check if the input value is an asynchronous function type.
5219 * @tc.type: FUNC
5220 */
5221 HWTEST_F(NativeEngineTest, textIsAsyncFunctionTest001, testing::ext::TestSize.Level0)
5222 {
5223 HILOG_INFO("textIsAsyncFunctionTest001 start");
5224 napi_env env = (napi_env)engine_;
5225 OHOS::Util::Types type;
5226 bool testRes = false;
5227 napi_value testRst = nullptr;
5228 napi_get_value_bool(env, testRst, &testRes);
5229 napi_value result = type.IsAsyncFunction(env, testRst);
5230 bool res = false;
5231 napi_get_value_bool(env, result, &res);
5232 ASSERT_FALSE(res);
5233 }
5234
5235 /**
5236 * @tc.name: textIsInt32ArrayTest001
5237 * @tc.desc: Check if the input value is of type Int32Array array.
5238 * @tc.type: FUNC
5239 */
5240 HWTEST_F(NativeEngineTest, textIsInt32ArrayTest001, testing::ext::TestSize.Level0)
5241 {
5242 HILOG_INFO("textIsInt32ArrayTest001 start");
5243 napi_env env = (napi_env)engine_;
5244 OHOS::Util::Types type;
5245 napi_value arrayBuffer = nullptr;
5246 void* arrayBufferPtr = nullptr;
5247 size_t arrayBufferSize = 1;
5248 napi_create_arraybuffer(env, arrayBufferSize * sizeof(size_t), &arrayBufferPtr, &arrayBuffer);
5249 napi_value dest = nullptr;
5250 napi_create_typedarray(env, napi_int32_array, arrayBufferSize, arrayBuffer, 0, &dest);
5251 napi_value result = type.IsInt32Array(env, dest);
5252 bool res = false;
5253 napi_get_value_bool(env, result, &res);
5254 ASSERT_TRUE(res);
5255 }
5256
5257 /**
5258 * @tc.name: textIsBigInt64ArrayTest001
5259 * @tc.desc: Check if the input value is of type BigInt64Array.
5260 * @tc.type: FUNC
5261 */
5262 HWTEST_F(NativeEngineTest, textIsBigInt64ArrayTest001, testing::ext::TestSize.Level0)
5263 {
5264 HILOG_INFO("textIsBigInt64ArrayTest001 start");
5265 napi_env env = (napi_env)engine_;
5266 OHOS::Util::Types type;
5267 napi_value arrayBuffer = nullptr;
5268 void* arrayBufferPtr = nullptr;
5269 size_t arrayBufferSize = 1;
5270 napi_create_arraybuffer(env, arrayBufferSize * sizeof(int64_t), &arrayBufferPtr, &arrayBuffer);
5271 napi_value dest = nullptr;
5272 napi_create_typedarray(env, napi_bigint64_array, arrayBufferSize, arrayBuffer, 0, &dest);
5273 napi_value result = type.IsBigInt64Array(env, dest);
5274 bool res = false;
5275 napi_get_value_bool(env, result, &res);
5276 ASSERT_TRUE(res);
5277 }
5278
5279 /**
5280 * @tc.name: textIsBigUint64ArrayTest001
5281 * @tc.desc: Check if the input value is of type BigUint64Array.
5282 * @tc.type: FUNC
5283 */
5284 HWTEST_F(NativeEngineTest, textIsBigUint64ArrayTest001, testing::ext::TestSize.Level0)
5285 {
5286 HILOG_INFO("textIsBigUint64ArrayTest001 start");
5287 napi_env env = (napi_env)engine_;
5288 OHOS::Util::Types type;
5289 napi_value arrayBuffer = nullptr;
5290 void* arrayBufferPtr = nullptr;
5291 size_t arrayBufferSize = 20; // number 20
5292 napi_create_arraybuffer(env, arrayBufferSize * sizeof(uint64_t), &arrayBufferPtr, &arrayBuffer);
5293 napi_value dest = nullptr;
5294 napi_create_typedarray(env, napi_biguint64_array, arrayBufferSize, arrayBuffer, 0, &dest);
5295 napi_value result = type.IsBigUint64Array(env, dest);
5296 bool res = false;
5297 napi_get_value_bool(env, result, &res);
5298 ASSERT_TRUE(res);
5299 }
5300
5301 /**
5302 * @tc.name: textIsGeneratorFunctionTest001
5303 * @tc.desc: Check if the input value is of the generator function type.
5304 * @tc.type: FUNC
5305 */
5306 HWTEST_F(NativeEngineTest, textIsGeneratorFunctionTest001, testing::ext::TestSize.Level0)
5307 {
5308 HILOG_INFO("textIsGeneratorFunctionTest001 start");
5309 napi_env env = (napi_env)engine_;
5310 OHOS::Util::Types type;
5311 bool resFlag = false;
5312 napi_value dest = nullptr;
5313 napi_get_value_bool(env, dest, &resFlag);
5314 napi_value result = type.IsGeneratorFunction(env, dest);
5315 bool res = false;
5316 napi_get_value_bool(env, result, &res);
5317 ASSERT_FALSE(res);
5318 }
5319
5320 /**
5321 * @tc.name: textIsGeneratorObjectTest001
5322 * @tc.desc: Check if the input value is of the generator object type.
5323 * @tc.type: FUNC
5324 */
5325 HWTEST_F(NativeEngineTest, textIsGeneratorObjectTest001, testing::ext::TestSize.Level0)
5326 {
5327 HILOG_INFO("textIsGeneratorObjectTest001 start");
5328 napi_env env = (napi_env)engine_;
5329 OHOS::Util::Types type;
5330 napi_value dest = nullptr;
5331 napi_create_object(env, &dest);
5332 napi_value result = type.IsGeneratorObject(env, dest);
5333 bool res = false;
5334 napi_get_value_bool(env, result, &res);
5335 ASSERT_FALSE(res);
5336 }
5337
5338 /**
5339 * @tc.name: textIsFloat32ArrayTest001
5340 * @tc.desc: Check if the input value is of Float32Array array type.
5341 * @tc.type: FUNC
5342 */
5343 HWTEST_F(NativeEngineTest, textIsFloat32ArrayTest001, testing::ext::TestSize.Level0)
5344 {
5345 HILOG_INFO("textIsFloat32ArrayTest001 start");
5346 napi_env env = (napi_env)engine_;
5347 OHOS::Util::Types type;
5348 napi_value arrayBuffer = nullptr;
5349 void* arrayBufferPtr = nullptr;
5350 size_t arrayBufferSize = 1;
5351 napi_create_arraybuffer(env, arrayBufferSize * sizeof(float), &arrayBufferPtr, &arrayBuffer);
5352 napi_value dest = nullptr;
5353 napi_create_typedarray(env, napi_float32_array, arrayBufferSize, arrayBuffer, 0, &dest);
5354 napi_value result = type.IsFloat32Array(env, dest);
5355 bool res = false;
5356 napi_get_value_bool(env, result, &res);
5357 ASSERT_TRUE(res);
5358 }
5359
5360 /**
5361 * @tc.name: textIsBooleanObjectTest001
5362 * @tc.desc: Check if the input value is a Boolean object type.
5363 * @tc.type: FUNC
5364 */
5365 HWTEST_F(NativeEngineTest, textIsBooleanObjectTest001, testing::ext::TestSize.Level0)
5366 {
5367 HILOG_INFO("textIsBooleanObjectTest001 start");
5368 napi_env env = (napi_env)engine_;
5369 OHOS::Util::Types type;
5370 napi_value testRst = nullptr;
5371 napi_get_boolean(env, false, &testRst);
5372 napi_value result = type.IsBooleanObject(env, testRst);
5373 bool res = false;
5374 napi_get_value_bool(env, result, &res);
5375 ASSERT_FALSE(res);
5376 }
5377
5378 /**
5379 * @tc.name: textIsBoxedPrimitiveTest001
5380 * @tc.desc: Check if the input value is of Boolean, Number, String, or Symbol object type.
5381 * @tc.type: FUNC
5382 */
5383 HWTEST_F(NativeEngineTest, textIsBoxedPrimitiveTest001, testing::ext::TestSize.Level0)
5384 {
5385 HILOG_INFO("textIsBoxedPrimitiveTest001 start");
5386 napi_env env = (napi_env)engine_;
5387 OHOS::Util::Types type;
5388 napi_value src = nullptr;
5389 napi_create_string_utf8(env, "abcd", NAPI_AUTO_LENGTH, &src);
5390 napi_value result = type.IsBoxedPrimitive(env, src);
5391 bool res = false;
5392 napi_get_value_bool(env, result, &res);
5393 ASSERT_FALSE(res);
5394 }
5395
5396 /**
5397 * @tc.name: textIsFloat64ArrayTest001
5398 * @tc.desc: Check if the input value is of Float64Array array type.
5399 * @tc.type: FUNC
5400 */
5401 HWTEST_F(NativeEngineTest, textIsFloat64ArrayTest001, testing::ext::TestSize.Level0)
5402 {
5403 HILOG_INFO("textIsFloat64ArrayTest001 start");
5404 napi_env env = (napi_env)engine_;
5405 OHOS::Util::Types type;
5406 napi_value arrayBuffer = nullptr;
5407 void* arrayBufferPtr = nullptr;
5408 size_t arrayBufferSize = 1;
5409 napi_create_arraybuffer(env, arrayBufferSize * sizeof(double), &arrayBufferPtr, &arrayBuffer);
5410 napi_value dest = nullptr;
5411 napi_create_typedarray(env, napi_float64_array, arrayBufferSize, arrayBuffer, 0, &dest);
5412 napi_value result = type.IsFloat64Array(env, dest);
5413 bool res = false;
5414 napi_get_value_bool(env, result, &res);
5415 ASSERT_TRUE(res);
5416 }
5417
5418 /**
5419 * @tc.name: textIsInt8ArrayTest001
5420 * @tc.desc: Check if the input value is of type Int8Array array.
5421 * @tc.type: FUNC
5422 */
5423 HWTEST_F(NativeEngineTest, textIsInt8ArrayTest001, testing::ext::TestSize.Level0)
5424 {
5425 HILOG_INFO("textIsInt8ArrayTest001 start");
5426 napi_env env = (napi_env)engine_;
5427 OHOS::Util::Types type;
5428 napi_value arrayBuffer = nullptr;
5429 void* arrayBufferPtr = nullptr;
5430 size_t arrayBufferSize = 1;
5431 napi_create_arraybuffer(env, arrayBufferSize * sizeof(int8_t), &arrayBufferPtr, &arrayBuffer);
5432 napi_value dest = nullptr;
5433 napi_create_typedarray(env, napi_int8_array, arrayBufferSize, arrayBuffer, 0, &dest);
5434 napi_value result = type.IsInt8Array(env, dest);
5435 bool res = false;
5436 napi_get_value_bool(env, result, &res);
5437 ASSERT_TRUE(res);
5438 }
5439
5440 /**
5441 * @tc.name: textIsInt16ArrayTest001
5442 * @tc.desc: Check if the input value is of type Int16Array array.
5443 * @tc.type: FUNC
5444 */
5445 HWTEST_F(NativeEngineTest, textIsInt16ArrayTest001, testing::ext::TestSize.Level0)
5446 {
5447 HILOG_INFO("textIsInt16ArrayTest001 start");
5448 napi_env env = (napi_env)engine_;
5449 OHOS::Util::Types type;
5450 napi_value arrayBuffer = nullptr;
5451 void* arrayBufferPtr = nullptr;
5452 size_t arrayBufferSize = 1;
5453 napi_create_arraybuffer(env, arrayBufferSize * sizeof(int16_t), &arrayBufferPtr, &arrayBuffer);
5454 napi_value dest = nullptr;
5455 napi_create_typedarray(env, napi_int16_array, arrayBufferSize, arrayBuffer, 0, &dest);
5456 napi_value result = type.IsInt16Array(env, dest);
5457 bool res = false;
5458 napi_get_value_bool(env, result, &res);
5459 ASSERT_TRUE(res);
5460 }
5461
5462 /**
5463 * @tc.name: textIsArrayBufferViewTest006
5464 * @tc.desc: Check if the input value is of the built-in ArrayBufferView auxiliary type.
5465 * @tc.type: FUNC
5466 */
5467 HWTEST_F(NativeEngineTest, textIsArrayBufferViewTest006, testing::ext::TestSize.Level0)
5468 {
5469 napi_env env = (napi_env)engine_;
5470 OHOS::Util::Types type;
5471 napi_value arrayBuffer = nullptr;
5472 void* arrayBufferPtr = nullptr;
5473 size_t arrayBufferSize = 20; // number 20
5474 napi_create_arraybuffer(env, arrayBufferSize * sizeof(uint64_t), &arrayBufferPtr, &arrayBuffer);
5475 napi_value dest = nullptr;
5476 napi_create_typedarray(env, napi_biguint64_array, arrayBufferSize, arrayBuffer, 0, &dest);
5477 napi_value result = type.IsArrayBufferView(env, dest);
5478 bool res = false;
5479 napi_get_value_bool(env, result, &res);
5480 ASSERT_FALSE(res);
5481 }
5482
5483 /**
5484 * @tc.name: textIsArgumentsObjectTest002
5485 * @tc.desc: Check if the input value is an arguments object.
5486 * @tc.type: FUNC
5487 */
5488 HWTEST_F(NativeEngineTest, textIsArgumentsObjectTest002, testing::ext::TestSize.Level0)
5489 {
5490 napi_env env = (napi_env)engine_;
5491 OHOS::Util::Types type;
5492 napi_value dest = nullptr;
5493 napi_create_object(env, &dest);
5494 napi_value result = type.IsArgumentsObject(env, dest);
5495 bool res = true;
5496 napi_get_value_bool(env, result, &res);
5497 ASSERT_FALSE(res);
5498 }
5499
SayHello(napi_env env,napi_callback_info info)5500 static napi_value SayHello(napi_env env, napi_callback_info info)
5501 {
5502 napi_value ret;
5503 NAPI_CALL(env, napi_create_int32(env, 1, &ret));
5504 return ret;
5505 }
5506
5507 /**
5508 * @tc.name: textIsAsyncFunctionTest002
5509 * @tc.desc: Check if the input value is an asynchronous function type.
5510 * @tc.type: FUNC
5511 */
5512 HWTEST_F(NativeEngineTest, textIsAsyncFunctionTest002, testing::ext::TestSize.Level0)
5513 {
5514 napi_env env = (napi_env)engine_;
5515 OHOS::Util::Types type;
5516 napi_value funcValue = nullptr;
5517 napi_create_function(env, nullptr, NAPI_AUTO_LENGTH, SayHello, nullptr, &funcValue);
5518 bool res = true;
5519 napi_value result = type.IsAsyncFunction(env, funcValue);
5520 napi_get_value_bool(env, result, &res);
5521 ASSERT_FALSE(res);
5522 }
5523
5524 /**
5525 * @tc.name: textIsBooleanObjectTest002
5526 * @tc.desc: Check if the input value is a Boolean object type.
5527 * @tc.type: FUNC
5528 */
5529 HWTEST_F(NativeEngineTest, textIsBooleanObjectTest002, testing::ext::TestSize.Level0)
5530 {
5531 napi_env env = (napi_env)engine_;
5532 OHOS::Util::Types type;
5533 napi_value dest = nullptr;
5534 napi_create_object(env, &dest);
5535 bool res = true;
5536 napi_value result = type.IsBooleanObject(env, dest);
5537 napi_get_value_bool(env, result, &res);
5538 ASSERT_FALSE(res);
5539 }
5540
5541 /**
5542 * @tc.name: textIsGeneratorFunctionTest002
5543 * @tc.desc: Check if the input value is of the generator function type.
5544 * @tc.type: FUNC
5545 */
5546 HWTEST_F(NativeEngineTest, textIsGeneratorFunctionTest002, testing::ext::TestSize.Level0)
5547 {
5548 napi_env env = (napi_env)engine_;
5549 OHOS::Util::Types type;
5550 napi_value funcValue = nullptr;
5551 napi_create_function(env, nullptr, NAPI_AUTO_LENGTH, SayHello, nullptr, &funcValue);
5552 napi_value result = type.IsGeneratorFunction(env, funcValue);
5553 bool res = true;
5554 napi_get_value_bool(env, result, &res);
5555 ASSERT_FALSE(res);
5556 }