• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_uuid.h"
19 #include "commonlibrary/ets_utils/js_util_module/util/js_textencoder.h"
20 #include "commonlibrary/ets_utils/js_util_module/util/js_textdecoder.h"
21 #include "commonlibrary/ets_utils/js_util_module/util/js_base64.h"
22 #include "napi/native_api.h"
23 #include "napi/native_node_api.h"
24 #include "securec.h"
25 #include "utils/log.h"
26 
27 #define ASSERT_CHECK_CALL(call)   \
28     {                             \
29         ASSERT_EQ(call, napi_ok); \
30     }
31 
32 #define ASSERT_CHECK_VALUE_TYPE(env, value, type)               \
33     {                                                           \
34         napi_valuetype valueType = napi_undefined;              \
35         ASSERT_TRUE(value != nullptr);                          \
36         ASSERT_CHECK_CALL(napi_typeof(env, value, &valueType)); \
37         ASSERT_EQ(valueType, type);                             \
38     }
39 
40 /* @tc.name: GetStringUUIDTest001
41  * @tc.desc: Test Generate a random RFC 4122 version 4 UUID.
42  * @tc.type: FUNC
43  */
44 HWTEST_F(NativeEngineTest, GetStringUUIDTest001, testing::ext::TestSize.Level0)
45 {
46     napi_env env = (napi_env)engine_;
47     std::string uuid = OHOS::Util::GetStringUUID(env, true);
48     ASSERT_EQ(uuid.length(), 36);
49 }
50 
51 /* @tc.name: GetStringUUIDTest002
52  * @tc.desc: Test Generate a random RFC 4122 version 4 UUID.
53  * @tc.type: FUNC
54  */
55 HWTEST_F(NativeEngineTest, GetStringUUIDTest002, testing::ext::TestSize.Level0)
56 {
57     napi_env env = (napi_env)engine_;
58     std::string uuid = OHOS::Util::GetStringUUID(env, false);
59     ASSERT_EQ(uuid.length(), 36);
60 }
61 
62 /* @tc.name: GetBinaryUUIDTest001
63  * @tc.desc: Test Generate a random RFC 4122 version 4 UUID.
64  * @tc.type: FUNC
65  */
66 HWTEST_F(NativeEngineTest, GetBinaryUUIDTest001, testing::ext::TestSize.Level0)
67 {
68     napi_env env = (napi_env)engine_;
69     napi_value arr = OHOS::Util::GetBinaryUUID(env, true);
70     napi_typedarray_type type = napi_int8_array;
71     size_t byteOffset = 0;
72     size_t length = 0;
73     void* resultData = nullptr;
74     napi_value resultBuffer = nullptr;
75     napi_get_typedarray_info(env, arr, &type, &length, &resultData, &resultBuffer, &byteOffset);
76     ASSERT_EQ(length, 16);
77 }
78 
79 /* @tc.name: GetBinaryUUIDTest002
80  * @tc.desc: Test Generate a random RFC 4122 version 4 UUID.
81  * @tc.type: FUNC
82  */
83 HWTEST_F(NativeEngineTest, GetBinaryUUIDTest002, testing::ext::TestSize.Level0)
84 {
85     napi_env env = (napi_env)engine_;
86     napi_value arr = OHOS::Util::GetBinaryUUID(env, false);
87     napi_typedarray_type type = napi_int8_array;
88     size_t byteOffset = 0;
89     size_t length = 0;
90     void* resultData = nullptr;
91     napi_value resultBuffer = nullptr;
92     napi_get_typedarray_info(env, arr, &type, &length, &resultData, &resultBuffer, &byteOffset);
93     ASSERT_EQ(length, 16);
94 }
95 
96 /* @tc.name: DoParseUUIDTest001
97  * @tc.desc: Parse a UUID from the string standard representation as described in the RFC 4122 version 4.
98  * @tc.type: FUNC
99  */
100 HWTEST_F(NativeEngineTest, DoParseUUIDTest001, testing::ext::TestSize.Level0)
101 {
102     napi_env env = (napi_env)engine_;
103     napi_value src = nullptr;
104     napi_create_string_utf8(env, "84bdf796-66cc-4655-9b89-d6218d100f9c", NAPI_AUTO_LENGTH, &src);
105     napi_value arr = OHOS::Util::DoParseUUID(env, src);
106     napi_typedarray_type type = napi_int8_array;
107     size_t byteOffset = 0;
108     size_t length = 0;
109     void* resultData = nullptr;
110     napi_value resultBuffer = nullptr;
111     napi_get_typedarray_info(env, arr, &type, &length, &resultData, &resultBuffer, &byteOffset);
112     ASSERT_EQ(length, 16);
113 }
114 
115 /* @tc.name: DoParseUUIDTest002
116  * @tc.desc: Parse a UUID from the string standard representation as described in the RFC 4122 version 4.
117  * @tc.type: FUNC
118  */
119 HWTEST_F(NativeEngineTest, DoParseUUIDTest002, testing::ext::TestSize.Level0)
120 {
121     napi_env env = (napi_env)engine_;
122     napi_value src = nullptr;
123     std::string input = "abc123";
124     napi_create_string_utf8(env, input.c_str(), NAPI_AUTO_LENGTH, &src);
125     napi_value arr = OHOS::Util::DoParseUUID(env, src);
126     napi_typedarray_type type = napi_int8_array;
127     size_t byteOffset = 0;
128     size_t length = 0;
129     void* resultData = nullptr;
130     napi_value resultBuffer = nullptr;
131     napi_get_typedarray_info(env, arr, &type, &length, &resultData, &resultBuffer, &byteOffset);
132     ASSERT_EQ(length, 16);
133 }
134 
135 /* @tc.name: DoParseUUIDTest003
136  * @tc.desc: Parse a UUID from the string standard representation as described in the RFC 4122 version 4.
137  * @tc.type: FUNC
138  */
139 HWTEST_F(NativeEngineTest, DoParseUUIDTest003, testing::ext::TestSize.Level0)
140 {
141     napi_env env = (napi_env)engine_;
142     napi_value src = nullptr;
143     std::string input = "abc123abc";
144     napi_create_string_utf8(env, input.c_str(), NAPI_AUTO_LENGTH, &src);
145     napi_value arr = OHOS::Util::DoParseUUID(env, src);
146     napi_typedarray_type type = napi_int8_array;
147     size_t byteOffset = 0;
148     size_t length = 0;
149     void* resultData = nullptr;
150     napi_value resultBuffer = nullptr;
151     napi_get_typedarray_info(env, arr, &type, &length, &resultData, &resultBuffer, &byteOffset);
152     ASSERT_EQ(length, 16);
153 }
154 
155 /* @tc.name: getEncodingTest001
156  * @tc.desc: Test acquire encoding mode.
157  * @tc.type: FUNC
158  */
159 HWTEST_F(NativeEngineTest, getEncodingTest001, testing::ext::TestSize.Level0)
160 {
161     HILOG_INFO("getEncodingTest001 start");
162     napi_env env = (napi_env)engine_;
163 
164     OHOS::Util::TextEncoder textEncoder("GBK");
165     napi_value result = textEncoder.GetEncoding(env);
166 
167     char *buffer = nullptr;
168     size_t bufferSize = 0;
169     napi_get_value_string_utf8(env, result, buffer, -1, &bufferSize);
170     if (bufferSize > 0) {
171         buffer = new char[bufferSize + 1];
172         napi_get_value_string_utf8(env, result, buffer, bufferSize + 1, &bufferSize);
173     }
174 
175     ASSERT_STREQ(buffer, "GBK");
176     if (buffer != nullptr) {
177         delete []buffer;
178         buffer = nullptr;
179     }
180 }
181 
182 /* @tc.name: getEncodingTest002
183  * @tc.desc: Test acquire encoding mode.
184  * @tc.type: FUNC
185  */
186 HWTEST_F(NativeEngineTest, getEncodingTest002, testing::ext::TestSize.Level0)
187 {
188     HILOG_INFO("getEncodingTest002 start");
189     napi_env env = (napi_env)engine_;
190 
191     OHOS::Util::TextEncoder textEncoder("gb18030");
192     napi_value result = textEncoder.GetEncoding(env);
193 
194     char *buffer = nullptr;
195     size_t bufferSize = 0;
196     napi_get_value_string_utf8(env, result, buffer, -1, &bufferSize);
197     if (bufferSize > 0) {
198         buffer = new char[bufferSize + 1];
199         napi_get_value_string_utf8(env, result, buffer, bufferSize + 1, &bufferSize);
200     }
201 
202     ASSERT_STREQ(buffer, "gb18030");
203     if (buffer != nullptr) {
204         delete []buffer;
205         buffer = nullptr;
206     }
207 }
208 
209 /* @tc.name: getEncodingTest003
210  * @tc.desc: Test acquire encoding mode.
211  * @tc.type: FUNC
212  */
213 HWTEST_F(NativeEngineTest, getEncodingTest003, testing::ext::TestSize.Level0)
214 {
215     HILOG_INFO("getEncodingTest003 start");
216     napi_env env = (napi_env)engine_;
217 
218     OHOS::Util::TextEncoder textEncoder("GB18030");
219     napi_value result = textEncoder.GetEncoding(env);
220 
221     char *buffer = nullptr;
222     size_t bufferSize = 0;
223     napi_get_value_string_utf8(env, result, buffer, -1, &bufferSize);
224     if (bufferSize > 0) {
225         buffer = new char[bufferSize + 1];
226         napi_get_value_string_utf8(env, result, buffer, bufferSize + 1, &bufferSize);
227     }
228 
229     ASSERT_STREQ(buffer, "GB18030");
230     if (buffer != nullptr) {
231         delete []buffer;
232         buffer = nullptr;
233     }
234 }
235 
236 /**
237  * @tc.name: textEncodeTest001
238  * @tc.desc: Test encode src.
239  * @tc.type: FUNC
240  */
241 HWTEST_F(NativeEngineTest, textEncodeTest001, testing::ext::TestSize.Level0)
242 {
243     HILOG_INFO("getEncodingTest001 start");
244     napi_env env = (napi_env)engine_;
245     OHOS::Util::TextEncoder textEncoder("utf-8");
246 
247     std::string input = "abc123";
248     napi_value src = nullptr;
249     napi_create_string_utf8(env, input.c_str(), input.size(), &src);
250     napi_value result = textEncoder.Encode(env, src);
251 
252     char excepted[7] = {0x61, 0x62, 0x63, 0x31, 0x32, 0x33, 0};
253 
254     napi_typedarray_type type;
255     size_t srcLength = 0;
256     void* srcData = nullptr;
257     napi_value srcBuffer = nullptr;
258     size_t byteOffset = 0;
259 
260     napi_get_typedarray_info(
261         env, result, &type, &srcLength, &srcData, &srcBuffer, &byteOffset);
262 
263     ASSERT_EQ(srcLength, 6);
264     char* res = reinterpret_cast<char*>(srcData);
265 
266     res[srcLength] = 0;
267     ASSERT_STREQ(res, excepted);
268 }
269 
270 /**
271  * @tc.name: textEncodeTest002
272  * @tc.desc: Test encode src.
273  * @tc.type: FUNC
274  */
275 HWTEST_F(NativeEngineTest, textEncodeTest002, testing::ext::TestSize.Level0)
276 {
277     HILOG_INFO("getEncodingTest002 start");
278     napi_env env = (napi_env)engine_;
279     OHOS::Util::TextEncoder textEncoder("utf-8");
280 
281     std::string input = "";
282     napi_value src = nullptr;
283     napi_create_string_utf8(env, input.c_str(), input.size(), &src);
284     napi_value result = textEncoder.Encode(env, src);
285 
286     napi_typedarray_type type;
287     size_t srcLength = 0;
288     void* srcData = nullptr;
289     napi_value srcBuffer = nullptr;
290     size_t byteOffset = 0;
291 
292     napi_get_typedarray_info(
293         env, result, &type, &srcLength, &srcData, &srcBuffer, &byteOffset);
294 
295     ASSERT_STREQ((char*)srcData, nullptr);
296 }
297 
298 /**
299  * @tc.name: textEncodeTest003
300  * @tc.desc: Test encode src.
301  * @tc.type: FUNC
302  */
303 HWTEST_F(NativeEngineTest, textEncodeTest003, testing::ext::TestSize.Level0)
304 {
305     HILOG_INFO("getEncodingTest003 start");
306     napi_env env = (napi_env)engine_;
307     OHOS::Util::TextEncoder textEncoder("utf-8");
308 
309     std::string input = "text";
310     napi_value src = nullptr;
311     napi_create_string_utf8(env, input.c_str(), input.size(), &src);
312     napi_value result = textEncoder.Encode(env, src);
313 
314     char excepted[7] = {0x74, 0x65, 0x78, 0x74, 0};
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_EQ(srcLength, 4);
326     char* res = reinterpret_cast<char*>(srcData);
327 
328     res[srcLength] = 0;
329     ASSERT_STREQ(res, excepted);
330 }
331 
332 /**
333  * @tc.name: textEncodeTest004
334  * @tc.desc: Test encode src.
335  * @tc.type: FUNC
336  */
337 HWTEST_F(NativeEngineTest, textEncodeTest004, testing::ext::TestSize.Level0)
338 {
339     napi_env env = (napi_env)engine_;
340     OHOS::Util::TextEncoder textEncoder("gbk");
341 
342     std::string input = "abc123";
343     napi_value src = nullptr;
344     napi_create_string_utf8(env, input.c_str(), input.size(), &src);
345     napi_value result = textEncoder.Encode(env, src);
346 
347     char excepted[7] = {0x61, 0x62, 0x63, 0x31, 0x32, 0x33, 0}; // 7:nums of args
348 
349     napi_typedarray_type type;
350     size_t srcLength = 0;
351     void *srcData = nullptr;
352     napi_value srcBuffer = nullptr;
353     size_t byteOffset = 0;
354 
355     napi_get_typedarray_info(
356         env, result, &type, &srcLength, &srcData, &srcBuffer, &byteOffset);
357 
358     ASSERT_EQ(srcLength, 6); // 6:string length
359     char *res = reinterpret_cast<char*>(srcData);
360 
361     res[srcLength] = 0;
362     ASSERT_STREQ(res, excepted);
363 }
364 
365 /**
366  * @tc.name: textEncodeTest005
367  * @tc.desc: Test encode src.
368  * @tc.type: FUNC
369  */
370 HWTEST_F(NativeEngineTest, textEncodeTest005, testing::ext::TestSize.Level0)
371 {
372     napi_env env = (napi_env)engine_;
373     OHOS::Util::TextEncoder textEncoder("utf-8");
374     napi_value src = nullptr;
375     napi_value result = textEncoder.Encode(env, src);
376     ASSERT_TRUE(result == nullptr);
377 }
378 
379 /**
380  * @tc.name: textEncodeIntoTest001
381  * @tc.desc: Test returns a dictionary object indicating the progress of the encoding
382  * @tc.type: FUNC
383  */
384 HWTEST_F(NativeEngineTest, textEncodeIntoTest001, testing::ext::TestSize.Level0)
385 {
386     HILOG_INFO("textEncodeIntoTest001 start");
387     napi_env env = (napi_env)engine_;
388     OHOS::Util::TextEncoder textEncoder("utf-8");
389 
390     std::string input = "abc123";
391     napi_value src = nullptr;
392     napi_create_string_utf8(env, input.c_str(), input.size(), &src);
393 
394     napi_value arrayBuffer = nullptr;
395     void* arrayBufferPtr = nullptr;
396     size_t arrayBufferSize = 20;
397     napi_create_arraybuffer(env, arrayBufferSize, &arrayBufferPtr, &arrayBuffer);
398 
399     napi_value dest = nullptr;
400         napi_create_typedarray(env, napi_int8_array, arrayBufferSize, arrayBuffer, 0, &dest);
401 
402     napi_value result = textEncoder.EncodeInto(env, src, dest);
403 
404     napi_value read = nullptr;
405     napi_get_named_property(env, result, "read", &read);
406 
407     uint32_t resRead = 0;
408 
409     napi_get_value_uint32(env, read, &resRead);
410 
411     napi_value written = nullptr;
412     napi_get_named_property(env, result, "written", &written);
413 
414     uint32_t resWritten = 0;
415     napi_get_value_uint32(env, read, &resWritten);
416 
417     ASSERT_EQ(resRead, (uint32_t)6);
418     ASSERT_EQ(resWritten, (uint32_t)6);
419 }
420 
421 /**
422  * @tc.name: textEncodeIntoTest002
423  * @tc.desc: Test returns a dictionary object indicating the progress of the encoding
424  * @tc.type: FUNC
425  */
426 HWTEST_F(NativeEngineTest, textEncodeIntoTest002, testing::ext::TestSize.Level0)
427 {
428     HILOG_INFO("textEncodeIntoTest002 start");
429     napi_env env = (napi_env)engine_;
430     OHOS::Util::TextEncoder textEncoder("utf-8");
431 
432     std::string input = "text";
433     napi_value src = nullptr;
434     napi_create_string_utf8(env, input.c_str(), input.size(), &src);
435 
436     napi_value arrayBuffer = nullptr;
437     void* arrayBufferPtr = nullptr;
438     size_t arrayBufferSize = 20;
439     napi_create_arraybuffer(env, arrayBufferSize, &arrayBufferPtr, &arrayBuffer);
440 
441     napi_value dest = nullptr;
442         napi_create_typedarray(env, napi_int8_array, arrayBufferSize, arrayBuffer, 0, &dest);
443 
444     napi_value result = textEncoder.EncodeInto(env, src, dest);
445 
446     napi_value read = nullptr;
447     napi_get_named_property(env, result, "read", &read);
448 
449     uint32_t resRead = 0;
450 
451     napi_get_value_uint32(env, read, &resRead);
452 
453     napi_value written = nullptr;
454     napi_get_named_property(env, result, "written", &written);
455 
456     uint32_t resWritten = 0;
457     napi_get_value_uint32(env, read, &resWritten);
458 
459     ASSERT_EQ(resRead, (uint32_t)4);
460     ASSERT_EQ(resWritten, (uint32_t)4);
461 }
462 
463 /**
464  * @tc.name: textEncodeIntoTest003
465  * @tc.desc: Test returns a dictionary object indicating the progress of the encoding
466  * @tc.type: FUNC
467  */
468 HWTEST_F(NativeEngineTest, textEncodeIntoTest003, testing::ext::TestSize.Level0)
469 {
470     HILOG_INFO("textEncodeIntoTest003 start");
471     napi_env env = (napi_env)engine_;
472     OHOS::Util::TextEncoder textEncoder("utf-8");
473 
474     std::string input = "12345";
475     napi_value src = nullptr;
476     napi_create_string_utf8(env, input.c_str(), input.size(), &src);
477 
478     napi_value arrayBuffer = nullptr;
479     void* arrayBufferPtr = nullptr;
480     size_t arrayBufferSize = 20;
481     napi_create_arraybuffer(env, arrayBufferSize, &arrayBufferPtr, &arrayBuffer);
482 
483     napi_value dest = nullptr;
484         napi_create_typedarray(env, napi_int8_array, arrayBufferSize, arrayBuffer, 0, &dest);
485 
486     napi_value result = textEncoder.EncodeInto(env, src, dest);
487 
488     napi_value read = nullptr;
489     napi_get_named_property(env, result, "read", &read);
490 
491     uint32_t resRead = 0;
492 
493     napi_get_value_uint32(env, read, &resRead);
494 
495     napi_value written = nullptr;
496     napi_get_named_property(env, result, "written", &written);
497 
498     uint32_t resWritten = 0;
499     napi_get_value_uint32(env, read, &resWritten);
500 
501     ASSERT_EQ(resRead, (uint32_t)5);
502     ASSERT_EQ(resWritten, (uint32_t)5);
503 }
504 
505 /**
506  * @tc.name: GetEncoding001
507  * @tc.desc: Test date type.
508  * @tc.type: FUNC
509  */
510 HWTEST_F(NativeEngineTest, GetEncoding001, testing::ext::TestSize.Level0)
511 {
512     HILOG_INFO("TextDecoder::getEncodingTest001 start");
513     napi_env env = (napi_env)engine_;
514     std::vector<int> inputVec;
515     int fatal = -1;
516     int ignoreBOM = -1;
517     inputVec.push_back(fatal);
518     inputVec.push_back(ignoreBOM);
519     std::string str = "utf-8";
520     OHOS::Util::TextDecoder textDecoder(str, inputVec);
521     napi_value testString = textDecoder.GetEncoding(env);
522     size_t bufferSize = 0;
523     napi_get_value_string_utf8(env, testString, nullptr, 0, &bufferSize);
524     std::string tmpTestStr = "utf-8";
525     size_t strLength = 0;
526     char* buffer = nullptr;
527     if (bufferSize > 0) {
528         buffer = new char[bufferSize + 1]();
529         napi_get_value_string_utf8(env, testString, buffer, bufferSize + 1, &strLength);
530     }
531     const char *result = tmpTestStr.c_str();
532     size_t resultLength = tmpTestStr.length();
533     ASSERT_STREQ(result, buffer);
534     ASSERT_EQ(resultLength, strLength);
535     if (buffer != nullptr) {
536         delete []buffer;
537         buffer = nullptr;
538     }
539 }
540 
541 /**
542  * @tc.name: GetEncoding002
543  * @tc.desc: Test date type.
544  * @tc.type: FUNC
545  */
546 HWTEST_F(NativeEngineTest, GetEncoding002, testing::ext::TestSize.Level0)
547 {
548     HILOG_INFO("TextDecoder::getEncodingTest002 start");
549     napi_env env = (napi_env)engine_;
550     std::vector<int> inputVec;
551     int fatal = -1;
552     int ignoreBOM = -1;
553     inputVec.push_back(fatal);
554     inputVec.push_back(ignoreBOM);
555     std::string str = "GB18030";
556     OHOS::Util::TextDecoder textDecoder(str, inputVec);
557     napi_value testString = textDecoder.GetEncoding(env);
558     size_t bufferSize = 0;
559     napi_get_value_string_utf8(env, testString, nullptr, 0, &bufferSize);
560     std::string tmpTestStr = "GB18030";
561     size_t strLength = 0;
562     char* buffer = nullptr;
563     if (bufferSize > 0) {
564         buffer = new char[bufferSize + 1]();
565         napi_get_value_string_utf8(env, testString, buffer, bufferSize + 1, &strLength);
566     }
567     const char *result = tmpTestStr.c_str();
568     size_t resultLength = tmpTestStr.length();
569     ASSERT_STREQ(result, buffer);
570     ASSERT_EQ(resultLength, strLength);
571     if (buffer != nullptr) {
572         delete []buffer;
573         buffer = nullptr;
574     }
575 }
576 
577 /**
578  * @tc.name: GetEncoding003
579  * @tc.desc: Test date type.
580  * @tc.type: FUNC
581  */
582 HWTEST_F(NativeEngineTest, GetEncoding003, testing::ext::TestSize.Level0)
583 {
584     HILOG_INFO("TextDecoder::getEncodingTest003 start");
585     napi_env env = (napi_env)engine_;
586     std::vector<int> inputVec;
587     int fatal = -1;
588     int ignoreBOM = -1;
589     inputVec.push_back(fatal);
590     inputVec.push_back(ignoreBOM);
591     std::string str = "gb18030";
592     OHOS::Util::TextDecoder textDecoder(str, inputVec);
593     napi_value testString = textDecoder.GetEncoding(env);
594     size_t bufferSize = 0;
595     napi_get_value_string_utf8(env, testString, nullptr, 0, &bufferSize);
596     std::string tmpTestStr = "gb18030";
597     size_t strLength = 0;
598     char* buffer = nullptr;
599     if (bufferSize > 0) {
600         buffer = new char[bufferSize + 1]();
601         napi_get_value_string_utf8(env, testString, buffer, bufferSize + 1, &strLength);
602     }
603     const char *result = tmpTestStr.c_str();
604     size_t resultLength = tmpTestStr.length();
605     ASSERT_STREQ(result, buffer);
606     ASSERT_EQ(resultLength, strLength);
607     if (buffer != nullptr) {
608         delete []buffer;
609         buffer = nullptr;
610     }
611 }
612 
613 /**
614  * @tc.name: GetEncoding004
615  * @tc.desc: Test date type.
616  * @tc.type: FUNC
617  */
618 HWTEST_F(NativeEngineTest, GetEncoding004, testing::ext::TestSize.Level0)
619 {
620     napi_env env = (napi_env)engine_;
621     std::vector<int> inputVec;
622     int fatal = 0;
623     int ignoreBOM = -1;
624     inputVec.push_back(fatal);
625     inputVec.push_back(ignoreBOM);
626     std::string str = "utf-8";
627     OHOS::Util::TextDecoder textDecoder(str, inputVec);
628     napi_value testString = textDecoder.GetEncoding(env);
629     size_t bufferSize = 0;
630     napi_get_value_string_utf8(env, testString, nullptr, 0, &bufferSize);
631     std::string tmpTestStr = "utf-8";
632     size_t strLength = 0;
633     char *buffer = nullptr;
634     if (bufferSize > 0) {
635         buffer = new char[bufferSize + 1]();
636         napi_get_value_string_utf8(env, testString, buffer, bufferSize + 1, &strLength);
637     }
638     const char *result = tmpTestStr.c_str();
639     size_t resultLength = tmpTestStr.length();
640     ASSERT_STREQ(result, buffer);
641     ASSERT_EQ(resultLength, strLength);
642     if (buffer != nullptr) {
643         delete []buffer;
644         buffer = nullptr;
645     }
646 }
647 
648 /**
649  * @tc.name: GetFatal001
650  * @tc.desc: Test date type.
651  * @tc.type: FUNC
652  */
653 HWTEST_F(NativeEngineTest, GetFatal001, testing::ext::TestSize.Level0)
654 {
655     HILOG_INFO("TextDecoder::GetFatal001 start");
656     napi_env env = (napi_env)engine_;
657     std::vector<int> inputVec;
658     int fatal = 1;
659     int ignoreBOM = 0;
660     inputVec.push_back(fatal);
661     inputVec.push_back(ignoreBOM);
662     std::string str = "utf-8";
663     OHOS::Util::TextDecoder textDecoder(str, inputVec);
664     napi_value naVal = textDecoder.GetFatal(env);
665     bool result = false;
666     napi_get_value_bool(env, naVal, &result);
667     ASSERT_TRUE(result);
668 }
669 
670 /**
671  * @tc.name: GetFatal002
672  * @tc.desc: Test date type.
673  * @tc.type: FUNC
674  */
675 HWTEST_F(NativeEngineTest, GetFatal002, testing::ext::TestSize.Level0)
676 {
677     HILOG_INFO("TextDecoder::GetFatal002 start");
678     napi_env env = (napi_env)engine_;
679     std::vector<int> inputVec;
680     int fatal = -1;
681     int ignoreBOM = 1;
682     inputVec.push_back(fatal);
683     inputVec.push_back(ignoreBOM);
684     std::string str = "utf-8";
685     OHOS::Util::TextDecoder textDecoder(str, inputVec);
686     napi_value naVal = textDecoder.GetFatal(env);
687     bool result = false;
688     napi_get_value_bool(env, naVal, &result);
689     ASSERT_FALSE(result);
690 }
691 
692 /**
693  * @tc.name: GetFatal003
694  * @tc.desc: Test date type.
695  * @tc.type: FUNC
696  */
697 HWTEST_F(NativeEngineTest, GetFatal003, testing::ext::TestSize.Level0)
698 {
699     HILOG_INFO("TextDecoder::GetFatal003 start");
700     napi_env env = (napi_env)engine_;
701     std::vector<int> inputVec;
702     int fatal = 0;
703     int ignoreBOM = 1;
704     inputVec.push_back(fatal);
705     inputVec.push_back(ignoreBOM);
706     std::string str = "utf-8";
707     OHOS::Util::TextDecoder textDecoder(str, inputVec);
708     napi_value naVal = textDecoder.GetFatal(env);
709     bool result = false;
710     napi_get_value_bool(env, naVal, &result);
711     ASSERT_FALSE(result);
712 }
713 
714 /**
715  * @tc.name: GetIgnoreBOM001
716  * @tc.desc: Test date type.
717  * @tc.type: FUNC
718  */
719 HWTEST_F(NativeEngineTest, GetIgnoreBOM001, testing::ext::TestSize.Level0)
720 {
721     HILOG_INFO("TextDecoder::GetIgnoreBOM001 start");
722     napi_env env = (napi_env)engine_;
723     std::vector<int> inputVec;
724     int fatal = -1;
725     int ignoreBOM = 1;
726     inputVec.push_back(fatal);
727     inputVec.push_back(ignoreBOM);
728     std::string str = "utf-8";
729     OHOS::Util::TextDecoder textDecoder(str, inputVec);
730     napi_value naVal = textDecoder.GetIgnoreBOM(env);
731     bool result = false;
732     napi_get_value_bool(env, naVal, &result);
733     ASSERT_TRUE(result);
734 }
735 
736 /**
737  * @tc.name: GetIgnoreBOM002
738  * @tc.desc: Test date type.
739  * @tc.type: FUNC
740  */
741 HWTEST_F(NativeEngineTest, GetIgnoreBOM002, testing::ext::TestSize.Level0)
742 {
743     HILOG_INFO("TextDecoder::GetIgnoreBOM002 start");
744     napi_env env = (napi_env)engine_;
745     std::vector<int> inputVec;
746     int fatal = 0;
747     int ignoreBOM = 1;
748     inputVec.push_back(fatal);
749     inputVec.push_back(ignoreBOM);
750     std::string str = "utf-8";
751     OHOS::Util::TextDecoder textDecoder(str, inputVec);
752     napi_value naVal = textDecoder.GetIgnoreBOM(env);
753     bool result = false;
754     napi_get_value_bool(env, naVal, &result);
755     ASSERT_TRUE(result);
756 }
757 
758 /**
759  * @tc.name: GetIgnoreBOM003
760  * @tc.desc: Test date type.
761  * @tc.type: FUNC
762  */
763 HWTEST_F(NativeEngineTest, GetIgnoreBOM003, testing::ext::TestSize.Level0)
764 {
765     HILOG_INFO("TextDecoder::GetIgnoreBOM003 start");
766     napi_env env = (napi_env)engine_;
767     std::vector<int> inputVec;
768     int fatal = 1;
769     int ignoreBOM = 1;
770     inputVec.push_back(fatal);
771     inputVec.push_back(ignoreBOM);
772     std::string str = "utf-8";
773     OHOS::Util::TextDecoder textDecoder(str, inputVec);
774     napi_value naVal = textDecoder.GetIgnoreBOM(env);
775     bool result = false;
776     napi_get_value_bool(env, naVal, &result);
777     ASSERT_TRUE(result);
778 }
779 
780 /**
781  * @tc.name: GetIgnoreBOM004
782  * @tc.desc: Test date type.
783  * @tc.type: FUNC
784  */
785 HWTEST_F(NativeEngineTest, GetIgnoreBOM004, testing::ext::TestSize.Level0)
786 {
787     napi_env env = (napi_env)engine_;
788     std::vector<int> inputVec;
789     int fatal = 0;
790     int ignoreBOM = -1;
791     inputVec.push_back(fatal);
792     inputVec.push_back(ignoreBOM);
793     std::string str = "ssn";
794     OHOS::Util::TextDecoder textDecoder(str, inputVec);
795     napi_value naVal = textDecoder.GetIgnoreBOM(env);
796     bool result = false;
797     napi_get_value_bool(env, naVal, &result);
798     ASSERT_FALSE(result);
799 }
800 
801 /**
802  * @tc.name: decoderUtf8001 utf-8
803  * @tc.desc: Test date type.
804  * @tc.type: FUNC
805  */
806 HWTEST_F(NativeEngineTest, decoderUtf8001, testing::ext::TestSize.Level0)
807 {
808     HILOG_INFO("decoderUtf8001 start");
809     napi_env env = (napi_env)engine_;
810     std::vector<int> inputVec;
811     int fatal = -1;
812     int ignoreBOM = -1;
813     inputVec.push_back(fatal);
814     inputVec.push_back(ignoreBOM);
815     std::string str = "utf-8";
816     OHOS::Util::TextDecoder textDecoder(str, inputVec);
817     bool iflag = false;
818     size_t byteLength = 3;
819     void* data = nullptr;
820     napi_value resultBuff = nullptr;
821     napi_create_arraybuffer(env, byteLength, &data, &resultBuff);
822     unsigned char arr[3] = {0x61, 0x62, 0x63};
823     int ret = memcpy_s(data, sizeof(arr), reinterpret_cast<void*>(arr), sizeof(arr));
824     ASSERT_EQ(0, ret);
825     napi_value result2 = nullptr;
826     napi_create_typedarray(env, napi_int8_array, byteLength, resultBuff, 0, &result2);
827     napi_value testString = textDecoder.Decode(env, result2, iflag);
828     size_t bufferSize = 0;
829     napi_get_value_string_utf8(env, testString, nullptr, 0, &bufferSize);
830     size_t length = 0;
831     char* ch = nullptr;
832     if (bufferSize > 0) {
833         ch = new char[bufferSize + 1]();
834         napi_get_value_string_utf8(env, testString, ch, bufferSize + 1, &length);
835     }
836     ASSERT_STREQ("abc", ch);
837     if (ch != nullptr) {
838         delete []ch;
839         ch = nullptr;
840     }
841 }
842 
843 /**
844  * @tc.name: decoderUtf8002 utf-8
845  * @tc.desc: Test date type.
846  * @tc.type: FUNC
847  */
848 HWTEST_F(NativeEngineTest, decoderUtf8002, testing::ext::TestSize.Level0)
849 {
850     HILOG_INFO("decoderUtf8002 start");
851     napi_env env = (napi_env)engine_;
852     std::vector<int> inputVec;
853     int fatal = -1;
854     int ignoreBOM = 0;
855     inputVec.push_back(fatal);
856     inputVec.push_back(ignoreBOM);
857     std::string str = "utf-8";
858     OHOS::Util::TextDecoder textDecoder(str, inputVec);
859     bool iflag = true;
860     size_t byteLength = 3;
861     void* data = nullptr;
862     napi_value resultBuff = nullptr;
863     napi_create_arraybuffer(env, byteLength, &data, &resultBuff);
864     unsigned char arr[3] = {0x61, 0x62, 0x63};
865     int ret = memcpy_s(data, sizeof(arr), reinterpret_cast<void*>(arr), sizeof(arr));
866     ASSERT_EQ(0, ret);
867     napi_value result2 = nullptr;
868     napi_create_typedarray(env, napi_int8_array, byteLength, resultBuff, 0, &result2);
869     napi_value testString = textDecoder.Decode(env, result2, iflag);
870     size_t bufferSize = 0;
871     size_t length = 0;
872     napi_get_value_string_utf8(env, testString, nullptr, 0, &bufferSize);
873     char* ch = nullptr;
874     if (bufferSize > 0) {
875         ch = new char[bufferSize + 1]();
876         napi_get_value_string_utf8(env, testString, ch, bufferSize + 1, &length);
877     }
878     ASSERT_STREQ("abc", ch);
879     if (ch != nullptr) {
880         delete []ch;
881         ch = nullptr;
882     }
883 }
884 
885 /**
886  * @tc.name: decoderUtf8003 utf-8
887  * @tc.desc: Test date type.
888  * @tc.type: FUNC
889  */
890 HWTEST_F(NativeEngineTest, decoderUtf8003, testing::ext::TestSize.Level0)
891 {
892     napi_env env = (napi_env)engine_;
893     std::vector<int> inputVec;
894     int fatal = -1;
895     int ignoreBOM = -1;
896     inputVec.push_back(fatal);
897     inputVec.push_back(ignoreBOM);
898     std::string str = "utf-8";
899     OHOS::Util::TextDecoder textDecoder(str, inputVec);
900     bool iflag = false;
901     size_t byteLength = 0;
902     void *data = nullptr;
903     napi_value resultBuff = nullptr;
904     napi_create_arraybuffer(env, byteLength, &data, &resultBuff);
905     napi_value result2 = nullptr;
906     napi_create_typedarray(env, napi_int8_array, byteLength, resultBuff, 0, &result2);
907     napi_value testString = textDecoder.Decode(env, result2, iflag);
908     ASSERT_TRUE(testString == nullptr);
909 }
910 
911 /**
912  * @tc.name: decoderUtf16le001 utf-16le
913  * @tc.desc: Test date type.
914  * @tc.type: FUNC
915  */
916 HWTEST_F(NativeEngineTest, decoderUtf16le001, testing::ext::TestSize.Level0)
917 {
918     HILOG_INFO("decoderUtf16le001 start");
919     napi_env env = (napi_env)engine_;
920     std::vector<int> inputVec;
921     int fatal = 0;
922     int ignoreBOM = 0;
923     inputVec.push_back(fatal);
924     inputVec.push_back(ignoreBOM);
925     std::string str = "utf-16le";
926     OHOS::Util::TextDecoder textDecoder(str, inputVec);
927     bool iflag = false;
928     size_t byteLength = 6;
929     void* data = nullptr;
930     napi_value resultBuff = nullptr;
931     napi_create_arraybuffer(env, byteLength, &data, &resultBuff);
932     unsigned char arr[6] = {0x61, 0x00, 0x62, 0x00, 0x63, 0x00};
933     int ret = memcpy_s(data, sizeof(arr), reinterpret_cast<void*>(arr), sizeof(arr));
934     ASSERT_EQ(0, ret);
935     napi_value result2 = nullptr;
936     napi_create_typedarray(env, napi_int8_array, byteLength, resultBuff, 0, &result2);
937     napi_value testString = textDecoder.Decode(env, result2, iflag);
938     size_t bufferSize = 0;
939     size_t length = 0;
940     napi_get_value_string_utf8(env, testString, nullptr, 0, &bufferSize);
941     char* ch = nullptr;
942     if (bufferSize > 0) {
943         ch = new char[bufferSize + 1]();
944         napi_get_value_string_utf8(env, testString, ch, bufferSize + 1, &length);
945     }
946     ASSERT_STREQ("abc", ch);
947     if (ch != nullptr) {
948         delete []ch;
949         ch = nullptr;
950     }
951 }
952 
953 /**
954  * @tc.name: decoderUtf16le002 utf-16le
955  * @tc.desc: Test date type.
956  * @tc.type: FUNC
957  */
958 HWTEST_F(NativeEngineTest, decoderUtf16le002, testing::ext::TestSize.Level0)
959 {
960     HILOG_INFO("decoderUtf16le002 start");
961     napi_env env = (napi_env)engine_;
962     std::vector<int>  inputVec;
963     int fatal = 0;
964     int ignoreBOM = 1;
965     inputVec.push_back(fatal);
966     inputVec.push_back(ignoreBOM);
967     std::string str = "utf-16le";
968     OHOS::Util::TextDecoder textDecoder(str, inputVec);
969     bool iflag = true;
970     size_t byteLength = 6;
971     void* data = nullptr;
972     napi_value resultBuff = nullptr;
973     napi_create_arraybuffer(env, byteLength, &data, &resultBuff);
974     unsigned char arr[6] = {0x61, 0x00, 0x62, 0x00, 0x63, 0x00};
975     int ret = memcpy_s(data, sizeof(arr), reinterpret_cast<void*>(arr), sizeof(arr));
976     ASSERT_EQ(0, ret);
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     size_t bufferSize = 0;
981     napi_get_value_string_utf8(env, testString, nullptr, 0, &bufferSize);
982     char* ch = nullptr;
983     size_t length = 0;
984     if (bufferSize > 0) {
985         ch = new char[bufferSize + 1]();
986         napi_get_value_string_utf8(env, testString, ch, bufferSize + 1, &length);
987     }
988     ASSERT_STREQ("abc", ch);
989     if (ch != nullptr) {
990         delete []ch;
991         ch = nullptr;
992     }
993 }
994 
995 /**
996  * @tc.name: decoderUtf16le003 utf-16le
997  * @tc.desc: Test date type.
998  * @tc.type: FUNC
999  */
1000 HWTEST_F(NativeEngineTest, decoderUtf16le003, testing::ext::TestSize.Level0)
1001 {
1002     HILOG_INFO("decoderUtf16le003 start");
1003     napi_env env = (napi_env)engine_;
1004     std::vector<int>  inputVec;
1005     int fatal = 0;
1006     int ignoreBOM = 0;
1007     inputVec.push_back(fatal);
1008     inputVec.push_back(ignoreBOM);
1009     std::string str = "utf-16le";
1010     OHOS::Util::TextDecoder textDecoder(str, inputVec);
1011     bool iflag = true;
1012     size_t byteLength = 8;
1013     void* data = nullptr;
1014     napi_value resultBuff = nullptr;
1015     napi_create_arraybuffer(env, byteLength, &data, &resultBuff);
1016     unsigned char arr[8] = {0xFF, 0xFE, 0x61, 0x00, 0x62, 0x00, 0x63, 0x00};
1017     int ret = memcpy_s(data, sizeof(arr), reinterpret_cast<void*>(arr), sizeof(arr));
1018     ASSERT_EQ(0, ret);
1019     napi_value result2 = nullptr;
1020     napi_create_typedarray(env, napi_int8_array, byteLength, resultBuff, 0, &result2);
1021     napi_value testString = textDecoder.Decode(env, result2, iflag);
1022     size_t bufferSize = 0;
1023     napi_get_value_string_utf8(env, testString, nullptr, 0, &bufferSize);
1024     char* ch = nullptr;
1025     size_t length = 0;
1026     std::string tempStr01 = "";
1027     if (bufferSize > 0) {
1028         ch = new char[bufferSize + 1]();
1029         napi_get_value_string_utf8(env, testString, ch, bufferSize + 1, &length);
1030         tempStr01 = ch;
1031     }
1032     std::u16string tempU16str02 =
1033         std::wstring_convert<std::codecvt_utf8_utf16<char16_t>, char16_t> {}.from_bytes(tempStr01);
1034     ASSERT_EQ(0xFEFF, (int)tempU16str02[0]);
1035     ASSERT_EQ(0x61, (int)tempU16str02[1]);
1036     ASSERT_EQ(0x62, (int)tempU16str02[2]);
1037     ASSERT_EQ(0x63, (int)tempU16str02[3]);
1038     if (ch != nullptr) {
1039         delete []ch;
1040         ch = nullptr;
1041     }
1042 }
1043 
1044 /**
1045  * @tc.name: decoderUtf16le004 utf-16le
1046  * @tc.desc: Test date type.
1047  * @tc.type: FUNC
1048  */
1049 HWTEST_F(NativeEngineTest, decoderUtf16le004, testing::ext::TestSize.Level0)
1050 {
1051     HILOG_INFO("decoderUtf16le004 start");
1052     napi_env env = (napi_env)engine_;
1053     std::vector<int>  inputVec;
1054     int fatal = -1;
1055     int ignoreBOM = -1;
1056     inputVec.push_back(fatal);
1057     inputVec.push_back(ignoreBOM);
1058     std::string str = "utf-16le";
1059     OHOS::Util::TextDecoder textDecoder(str, inputVec);
1060     bool iflag = false;
1061     size_t byteLength = 8;
1062     void* data = nullptr;
1063     napi_value resultBuff = nullptr;
1064     napi_create_arraybuffer(env, byteLength, &data, &resultBuff);
1065     unsigned char arr[8] = {0xFF, 0xFE, 0x61, 0x00, 0x62, 0x00, 0x63, 0x00};
1066     int ret = memcpy_s(data, sizeof(arr), reinterpret_cast<void*>(arr), sizeof(arr));
1067     ASSERT_EQ(0, ret);
1068     napi_value result2 = nullptr;
1069     napi_create_typedarray(env, napi_int8_array, byteLength, resultBuff, 0, &result2);
1070     napi_value testString = textDecoder.Decode(env, result2, iflag);
1071     size_t bufferSize = 0;
1072     napi_get_value_string_utf8(env, testString, nullptr, 0, &bufferSize);
1073     char* ch = nullptr;
1074     size_t length = 0;
1075     std::string tempStr01 = "";
1076     if (bufferSize > 0) {
1077         ch = new char[bufferSize + 1]();
1078         napi_get_value_string_utf8(env, testString, ch, bufferSize + 1, &length);
1079         tempStr01 = ch;
1080     }
1081     std::u16string tempU16str02 =
1082     std::wstring_convert<std::codecvt_utf8_utf16<char16_t>, char16_t> {}.from_bytes(tempStr01);
1083     ASSERT_EQ(0xFEFF, (int)tempU16str02[0]);
1084     ASSERT_EQ(0x61, (int)tempU16str02[1]);
1085     ASSERT_EQ(0x62, (int)tempU16str02[2]);
1086     ASSERT_EQ(0x63, (int)tempU16str02[3]);
1087     if (ch != nullptr) {
1088         delete []ch;
1089         ch = nullptr;
1090     }
1091 }
1092 
1093 /**
1094  * @tc.name: decoderUtf16be001 utf-16be
1095  * @tc.desc: Test date type.
1096  * @tc.type: FUNC
1097  */
1098 HWTEST_F(NativeEngineTest, decoderUtf16be001, testing::ext::TestSize.Level0)
1099 {
1100     HILOG_INFO("decoderUtf16be001 start");
1101     napi_env env = (napi_env)engine_;
1102     std::vector<int>  inputVec;
1103     int fatal = 0;
1104     int ignoreBOM = 0;
1105     inputVec.push_back(fatal);
1106     inputVec.push_back(ignoreBOM);
1107     std::string str = "utf-16be";
1108     OHOS::Util::TextDecoder textDecoder(str, inputVec);
1109     bool iflag = false;
1110     size_t byteLength = 6;
1111     void* data = nullptr;
1112     napi_value resultBuff = nullptr;
1113     napi_create_arraybuffer(env, byteLength, &data, &resultBuff);
1114     unsigned char arr[6] = {0x00, 0x61, 0x00, 0x62, 0x00, 0x63};
1115     int ret = memcpy_s(data, sizeof(arr), reinterpret_cast<void*>(arr), sizeof(arr));
1116     ASSERT_EQ(0, ret);
1117     napi_value result2 = nullptr;
1118     napi_create_typedarray(env, napi_int8_array, byteLength, resultBuff, 0, &result2);
1119     napi_value testString = textDecoder.Decode(env, result2, iflag);
1120     size_t bufferSize = 0;
1121     napi_get_value_string_utf8(env, testString, nullptr, 0, &bufferSize);
1122     size_t length = 0;
1123     char* ch = nullptr;
1124     if (bufferSize > 0) {
1125         ch = new char[bufferSize + 1]();
1126         napi_get_value_string_utf8(env, testString, ch, bufferSize + 1, &length);
1127     }
1128     ASSERT_STREQ("abc", ch);
1129     if (ch != nullptr) {
1130         delete []ch;
1131         ch = nullptr;
1132     }
1133 }
1134 
1135 /**
1136  * @tc.name: decoderUtf16be002 utf-16be
1137  * @tc.desc: Test date type.
1138  * @tc.type: FUNC
1139  */
1140 HWTEST_F(NativeEngineTest, decoderUtf16be002, testing::ext::TestSize.Level0)
1141 {
1142     HILOG_INFO("decoderUtf16be002 start");
1143     napi_env env = (napi_env)engine_;
1144     std::vector<int>  inputVec;
1145     int fatal = 0;
1146     int ignoreBOM = 0;
1147     inputVec.push_back(fatal);
1148     inputVec.push_back(ignoreBOM);
1149     std::string str = "utf-16be";
1150     OHOS::Util::TextDecoder textDecoder(str, inputVec);
1151     bool iflag = false;
1152     size_t byteLength = 8;
1153     void* data = nullptr;
1154     napi_value resultBuff = nullptr;
1155     napi_create_arraybuffer(env, byteLength, &data, &resultBuff);
1156     unsigned char arr[8] = {0xFE, 0xFF, 0x00, 0x61, 0x00, 0x62, 0x00, 0x63};
1157     int ret = memcpy_s(data, sizeof(arr), reinterpret_cast<void*>(arr), sizeof(arr));
1158     ASSERT_EQ(0, ret);
1159     napi_value result2 = nullptr;
1160     napi_create_typedarray(env, napi_int8_array, byteLength, resultBuff, 0, &result2);
1161     napi_value testString = textDecoder.Decode(env, result2, iflag);
1162     size_t bufferSize = 0;
1163     napi_get_value_string_utf8(env, testString, nullptr, 0, &bufferSize);
1164     size_t length = 0;
1165     char* ch = nullptr;
1166     std::string tempStr01 = "";
1167     if (bufferSize > 0) {
1168         ch = new char[bufferSize + 1]();
1169         napi_get_value_string_utf8(env, testString, ch, bufferSize + 1, &length);
1170         tempStr01 = ch;
1171     }
1172     std::u16string tempU16str02 =
1173     std::wstring_convert<std::codecvt_utf8_utf16<char16_t>, char16_t> {}.from_bytes(tempStr01);
1174     ASSERT_EQ(0xFEFF, (int)tempU16str02[0]);
1175     ASSERT_EQ(0x61, (int)tempU16str02[1]);
1176     ASSERT_EQ(0x62, (int)tempU16str02[2]);
1177     ASSERT_EQ(0x63, (int)tempU16str02[3]);
1178     if (ch != nullptr) {
1179         delete []ch;
1180         ch = nullptr;
1181     }
1182 }
1183 
1184 /**
1185  * @tc.name: decoderUtf16be003 utf-16be
1186  * @tc.desc: Test date type.
1187  * @tc.type: FUNC
1188  */
1189 HWTEST_F(NativeEngineTest, decoderUtf16be003, testing::ext::TestSize.Level0)
1190 {
1191     HILOG_INFO("decoderUtf16be003 start");
1192     napi_env env = (napi_env)engine_;
1193     std::vector<int>  inputVec;
1194     int fatal = 0;
1195     int ignoreBOM = 1;
1196     inputVec.push_back(fatal);
1197     inputVec.push_back(ignoreBOM);
1198     std::string str = "utf-16be";
1199     OHOS::Util::TextDecoder textDecoder(str, inputVec);
1200     bool iflag = true;
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, (int)tempU16str02[0]);
1224     ASSERT_EQ(0x61, (int)tempU16str02[1]);
1225     ASSERT_EQ(0x62, (int)tempU16str02[2]);
1226     ASSERT_EQ(0x63, (int)tempU16str02[3]);
1227     if (ch != nullptr) {
1228         delete []ch;
1229         ch = nullptr;
1230     }
1231 }
1232 
1233 /* @tc.name: encodeTest001
1234  * @tc.desc: Encodes all bytes in the specified u8 array into
1235              the newly allocated u8 array using the Base64 encoding scheme.
1236  * @tc.type: FUNC
1237  */
1238 HWTEST_F(NativeEngineTest, encodeTest001, testing::ext::TestSize.Level0)
1239 {
1240     HILOG_INFO("encodeTest001 start");
1241     napi_env env = (napi_env)engine_;
1242     OHOS::Util::Base64 base64;
1243     unsigned char input[3] = {0x73, 0x31, 0x33};
1244     napi_value arrayBuffer = nullptr;
1245     void* data = nullptr;
1246     size_t arrayBufferSize = 3;
1247     napi_create_arraybuffer(env, arrayBufferSize, &data, &arrayBuffer);
1248     int ret = memcpy_s(data, sizeof(input), reinterpret_cast<void*>(input), sizeof(input));
1249     ASSERT_EQ(0, ret);
1250     napi_value src = nullptr;
1251     napi_create_typedarray(env, napi_uint8_array, arrayBufferSize, arrayBuffer, 0, &src);
1252 
1253     napi_value result = base64.EncodeSync(env, src);
1254     char excepted[4] = {0x63, 0x7A, 0x45, 0x7A};
1255     napi_typedarray_type type;
1256     size_t srcLength = 0;
1257     void* srcData = nullptr;
1258     napi_value srcBuffer = nullptr;
1259     size_t byteOffset = 0;
1260     napi_get_typedarray_info(env, result, &type, &srcLength, &srcData, &srcBuffer, &byteOffset);
1261     char* res = (char*)srcData;
1262     ASSERT_EQ(res[0], excepted[0]);
1263     ASSERT_EQ(res[1], excepted[1]);
1264     ASSERT_EQ(res[2], excepted[2]);
1265     ASSERT_EQ(res[3], excepted[3]);
1266 }
1267 
1268 /* @tc.name: encodeTest002
1269  * @tc.desc: Encodes all bytes in the specified u8 array
1270              into the newly allocated u8 array using the Base64 encoding scheme.
1271  * @tc.type: FUNC
1272  */
1273 HWTEST_F(NativeEngineTest, encodeTest002, testing::ext::TestSize.Level0)
1274 {
1275     HILOG_INFO("encodeTest002 start");
1276     napi_env env = (napi_env)engine_;
1277     OHOS::Util::Base64 base64;
1278     unsigned char input[14] = {66, 97, 115, 101, 54, 52, 32, 78, 111, 100, 101, 46, 106, 115};
1279     napi_value arrayBuffer = nullptr;
1280     void* data = nullptr;
1281     size_t arrayBufferSize = 14;
1282     napi_create_arraybuffer(env, arrayBufferSize, &data, &arrayBuffer);
1283     int ret = memcpy_s(data, sizeof(input), reinterpret_cast<void*>(input), sizeof(input));
1284     ASSERT_EQ(0, ret);
1285     napi_value src = nullptr;
1286     napi_create_typedarray(env, napi_uint8_array, arrayBufferSize, arrayBuffer, 0, &src);
1287 
1288     napi_value result = base64.EncodeSync(env, src);
1289     char excepted[20] = {81, 109, 70, 122, 90, 84, 89, 48, 73, 69, 53, 118, 90, 71, 85, 117, 97, 110, 77, 61};
1290     napi_typedarray_type type;
1291     size_t srcLength = 0;
1292     void* srcData = nullptr;
1293     napi_value srcBuffer = nullptr;
1294     size_t byteOffset = 0;
1295     napi_get_typedarray_info(env, result, &type, &srcLength, &srcData, &srcBuffer, &byteOffset);
1296     char* res = (char*)srcData;
1297     for (size_t i = 0; i < 20; i++) {
1298         ASSERT_EQ(res[i], excepted[i]);
1299     }
1300 }
1301 
1302 /* @tc.name: encodeTest003
1303  * @tc.desc: Encodes all bytes in the specified u8 array
1304              into the newly allocated u8 array using the Base64 encoding scheme.
1305  * @tc.type: FUNC
1306  */
1307 HWTEST_F(NativeEngineTest, encodeTest003, testing::ext::TestSize.Level0)
1308 {
1309     HILOG_INFO("encodeTest003 start");
1310     napi_env env = (napi_env)engine_;
1311     OHOS::Util::Base64 base64;
1312     unsigned char input[26] = {66, 97, 115, 101, 54, 52, 32, 69, 110,
1313                               99, 111, 100, 105, 110, 103, 32, 105, 110, 32, 78, 111, 100, 101, 46, 106, 115};
1314     napi_value arrayBuffer = nullptr;
1315     void* data = nullptr;
1316     size_t arrayBufferSize = 26;
1317     napi_create_arraybuffer(env, arrayBufferSize, &data, &arrayBuffer);
1318     int ret = memcpy_s(data, sizeof(input), reinterpret_cast<void*>(input), sizeof(input));
1319     ASSERT_EQ(0, ret);
1320     napi_value src = nullptr;
1321     napi_create_typedarray(env, napi_uint8_array, arrayBufferSize, arrayBuffer, 0, &src);
1322 
1323     napi_value result = base64.EncodeSync(env, src);
1324     char excepted[36] = {81, 109, 70, 122, 90, 84, 89, 48, 73, 69, 86, 117, 89, 50, 57, 107, 97, 87, 53,
1325                         110, 73, 71, 108, 117, 73, 69, 53, 118, 90, 71, 85, 117, 97, 110, 77, 61};
1326     napi_typedarray_type type;
1327     size_t srcLength = 0;
1328     void* srcData = nullptr;
1329     napi_value srcBuffer = nullptr;
1330     size_t byteOffset = 0;
1331     napi_get_typedarray_info(env, result, &type, &srcLength, &srcData, &srcBuffer, &byteOffset);
1332     char* res = (char*)srcData;
1333     for (size_t i = 0; i < 36; i++) {
1334         ASSERT_EQ(res[i], excepted[i]);
1335     }
1336 }
1337 
1338 /* @tc.name: encodeTest004
1339  * @tc.desc: Encodes all bytes in the specified u8 array into the
1340              newly allocated u8 array using the Base64 encoding scheme.
1341  * @tc.type: FUNC
1342  */
1343 HWTEST_F(NativeEngineTest, encodeTest004, testing::ext::TestSize.Level0)
1344 {
1345     HILOG_INFO("encodeTest004 start");
1346     napi_env env = (napi_env)engine_;
1347     OHOS::Util::Base64 base64;
1348     unsigned char input[4] = {168, 174, 155, 255};
1349     napi_value arrayBuffer = nullptr;
1350     void* data = nullptr;
1351     size_t arrayBufferSize = 4;
1352     napi_create_arraybuffer(env, arrayBufferSize, &data, &arrayBuffer);
1353     int ret = memcpy_s(data, sizeof(input), reinterpret_cast<void*>(input), sizeof(input));
1354     ASSERT_EQ(0, ret);
1355     napi_value src = nullptr;
1356     napi_create_typedarray(env, napi_uint8_array, arrayBufferSize, arrayBuffer, 0, &src);
1357 
1358     napi_value result = base64.EncodeSync(env, src);
1359     char excepted[8] = {113, 75, 54, 98, 47, 119, 61, 61};
1360     napi_typedarray_type type;
1361     size_t srcLength = 0;
1362     void* srcData = nullptr;
1363     napi_value srcBuffer = nullptr;
1364     size_t byteOffset = 0;
1365     napi_get_typedarray_info(env, result, &type, &srcLength, &srcData, &srcBuffer, &byteOffset);
1366     char* res = (char*)srcData;
1367     for (size_t i = 0; i < 8; i++) {
1368         ASSERT_EQ(res[i], excepted[i]);
1369     }
1370 }
1371 
1372 /* @tc.name: encodeTest005
1373  * @tc.desc: Encodes all bytes in the specified u8 array
1374              into the newly allocated u8 array using the Base64 encoding scheme.
1375  * @tc.type: FUNC
1376  */
1377 HWTEST_F(NativeEngineTest, encodeTest005, testing::ext::TestSize.Level0)
1378 {
1379     HILOG_INFO("encodeTest005 start");
1380     napi_env env = (napi_env)engine_;
1381     OHOS::Util::Base64 base64;
1382     unsigned char input[6] = {66, 97, 115, 101, 54, 52};
1383     napi_value arrayBuffer = nullptr;
1384     void* data = nullptr;
1385     size_t arrayBufferSize = 6;
1386     napi_create_arraybuffer(env, arrayBufferSize, &data, &arrayBuffer);
1387     int ret = memcpy_s(data, sizeof(input), reinterpret_cast<void*>(input), sizeof(input));
1388     ASSERT_EQ(0, ret);
1389     napi_value src = nullptr;
1390     napi_create_typedarray(env, napi_uint8_array, arrayBufferSize, arrayBuffer, 0, &src);
1391 
1392     napi_value result = base64.EncodeSync(env, src);
1393     char excepted[8] = {81, 109, 70, 122, 90, 84, 89, 48};
1394     napi_typedarray_type type;
1395     size_t srcLength = 0;
1396     void* srcData = nullptr;
1397     napi_value srcBuffer = nullptr;
1398     size_t byteOffset = 0;
1399     napi_get_typedarray_info(env, result, &type, &srcLength, &srcData, &srcBuffer, &byteOffset);
1400     char* res = (char*)srcData;
1401     for (size_t i = 0; i < 8; i++) {
1402         ASSERT_EQ(res[i], excepted[i]);
1403     }
1404 }
1405 
1406 /* @tc.name: encodeToStringTest001
1407  * @tc.desc: Encodes the specified byte array as a String using the Base64 encoding scheme.
1408  * @tc.type: FUNC
1409  */
1410 HWTEST_F(NativeEngineTest, encodeToStringTest001, testing::ext::TestSize.Level0)
1411 {
1412     HILOG_INFO("encodeToStringTest001 start");
1413     napi_env env = (napi_env)engine_;
1414     OHOS::Util::Base64 base64;
1415 
1416     unsigned char input[3] = {115, 49, 51};
1417     napi_value arrayBuffer = nullptr;
1418     size_t arrayBufferSize = 3;
1419     void* data = nullptr;
1420     napi_create_arraybuffer(env, arrayBufferSize, &data, &arrayBuffer);
1421     int ret = memcpy_s(data, sizeof(input), reinterpret_cast<void*>(input), sizeof(input));
1422     ASSERT_EQ(0, ret);
1423     napi_value src = nullptr;
1424     napi_create_typedarray(env, napi_uint8_array, arrayBufferSize, arrayBuffer, 0, &src);
1425     napi_value result = base64.EncodeToStringSync(env, src);
1426     size_t prolen = 0;
1427     char* inputString = nullptr;
1428     napi_get_value_string_utf8(env, result, nullptr, 0, &prolen);
1429     if (prolen > 0) {
1430         inputString = new char[prolen + 1];
1431         if (memset_s(inputString, prolen + 1, '\0', prolen + 1) != 0) {
1432             napi_throw_error(env, "-1", "decode inputString memset_s failed");
1433         }
1434     } else {
1435         napi_throw_error(env, "-2", "prolen is error !");
1436     }
1437     napi_get_value_string_utf8(env, result, inputString, prolen + 1, &prolen);
1438     ASSERT_STREQ("czEz", inputString);
1439     if (inputString != nullptr) {
1440         delete []inputString;
1441         inputString = nullptr;
1442     }
1443 }
1444 
1445 /* @tc.name: encodeToStringTest002
1446  * @tc.desc: Encodes the specified byte array as a String using the Base64 encoding scheme.
1447  * @tc.type: FUNC
1448  */
1449 HWTEST_F(NativeEngineTest, encodeToStringTest002, testing::ext::TestSize.Level0)
1450 {
1451     HILOG_INFO("encodeToStringTest002 start");
1452     napi_env env = (napi_env)engine_;
1453     OHOS::Util::Base64 base64;
1454 
1455     unsigned char input[14] = {66, 97, 115, 101, 54, 52, 32, 78, 111, 100, 101, 46, 106, 115};
1456     napi_value arrayBuffer = nullptr;
1457     size_t arrayBufferSize = 14;
1458     void* data = nullptr;
1459     napi_create_arraybuffer(env, arrayBufferSize, &data, &arrayBuffer);
1460     int ret = memcpy_s(data, sizeof(input), reinterpret_cast<void*>(input), sizeof(input));
1461     ASSERT_EQ(0, ret);
1462     napi_value src = nullptr;
1463     napi_create_typedarray(env, napi_uint8_array, arrayBufferSize, arrayBuffer, 0, &src);
1464     napi_value result = base64.EncodeToStringSync(env, src);
1465     size_t prolen = 0;
1466     char* inputString = nullptr;
1467     napi_get_value_string_utf8(env, result, nullptr, 0, &prolen);
1468     if (prolen > 0) {
1469         inputString = new char[prolen + 1];
1470         if (memset_s(inputString, prolen + 1, '\0', prolen + 1) != 0) {
1471             napi_throw_error(env, "-1", "decode inputString memset_s failed");
1472         }
1473     } else {
1474         napi_throw_error(env, "-2", "prolen is error !");
1475     }
1476     napi_get_value_string_utf8(env, result, inputString, prolen + 1, &prolen);
1477     ASSERT_STREQ("QmFzZTY0IE5vZGUuanM=", inputString);
1478     if (inputString != nullptr) {
1479         delete []inputString;
1480         inputString = nullptr;
1481     }
1482 }
1483 
1484 /* @tc.name: encodeToStringTest003
1485  * @tc.desc: Encodes the specified byte array as a String using the Base64 encoding scheme.
1486  * @tc.type: FUNC
1487  */
1488 HWTEST_F(NativeEngineTest, encodeToStringTest003, testing::ext::TestSize.Level0)
1489 {
1490     HILOG_INFO("encodeToStringTest003 start");
1491     napi_env env = (napi_env)engine_;
1492     OHOS::Util::Base64 base64;
1493 
1494     unsigned char input[26] = {66, 97, 115, 101, 54, 52, 32, 69, 110,
1495                               99, 111, 100, 105, 110, 103, 32, 105, 110, 32, 78, 111, 100, 101, 46, 106, 115};
1496     napi_value arrayBuffer = nullptr;
1497     size_t arrayBufferSize = 26;
1498     void* data = nullptr;
1499     napi_create_arraybuffer(env, arrayBufferSize, &data, &arrayBuffer);
1500     int ret = memcpy_s(data, sizeof(input), reinterpret_cast<void*>(input), sizeof(input));
1501     ASSERT_EQ(0, ret);
1502     napi_value src = nullptr;
1503     napi_create_typedarray(env, napi_uint8_array, arrayBufferSize, arrayBuffer, 0, &src);
1504     napi_value result = base64.EncodeToStringSync(env, src);
1505     size_t prolen = 0;
1506     char* inputString = nullptr;
1507     napi_get_value_string_utf8(env, result, nullptr, 0, &prolen);
1508     if (prolen > 0) {
1509         inputString = new char[prolen + 1];
1510         if (memset_s(inputString, prolen + 1, '\0', prolen + 1) != 0) {
1511             napi_throw_error(env, "-1", "decode inputString memset_s failed");
1512         }
1513     } else {
1514         napi_throw_error(env, "-2", "prolen is error !");
1515     }
1516     napi_get_value_string_utf8(env, result, inputString, prolen + 1, &prolen);
1517     ASSERT_STREQ("QmFzZTY0IEVuY29kaW5nIGluIE5vZGUuanM=", inputString);
1518     if (inputString != nullptr) {
1519         delete []inputString;
1520         inputString = nullptr;
1521     }
1522 }
1523 
1524 /* @tc.name: encodeToStringTest004
1525  * @tc.desc: Encodes the specified byte array as a String using the Base64 encoding scheme.
1526  * @tc.type: FUNC
1527  */
1528 HWTEST_F(NativeEngineTest, encodeToStringTest004, testing::ext::TestSize.Level0)
1529 {
1530     HILOG_INFO("encodeToStringTest004 start");
1531     napi_env env = (napi_env)engine_;
1532     OHOS::Util::Base64 base64;
1533 
1534     unsigned char input[4] = {168, 174, 155, 255};
1535     napi_value arrayBuffer = nullptr;
1536     size_t arrayBufferSize = 4;
1537     void* data = nullptr;
1538     napi_create_arraybuffer(env, arrayBufferSize, &data, &arrayBuffer);
1539     int ret = memcpy_s(data, sizeof(input), reinterpret_cast<void*>(input), sizeof(input));
1540     ASSERT_EQ(0, ret);
1541     napi_value src = nullptr;
1542     napi_create_typedarray(env, napi_uint8_array, arrayBufferSize, arrayBuffer, 0, &src);
1543     napi_value result = base64.EncodeToStringSync(env, src);
1544     size_t prolen = 0;
1545     char* inputString = nullptr;
1546     napi_get_value_string_utf8(env, result, nullptr, 0, &prolen);
1547     if (prolen > 0) {
1548         inputString = new char[prolen + 1];
1549         if (memset_s(inputString, prolen + 1, '\0', prolen + 1) != 0) {
1550             napi_throw_error(env, "-1", "decode inputString memset_s failed");
1551         }
1552     } else {
1553         napi_throw_error(env, "-2", "prolen is error !");
1554     }
1555     napi_get_value_string_utf8(env, result, inputString, prolen + 1, &prolen);
1556     ASSERT_STREQ("qK6b/w==", inputString);
1557     if (inputString != nullptr) {
1558         delete []inputString;
1559         inputString = nullptr;
1560     }
1561 }
1562 
1563 /* @tc.name: encodeToStringTest005
1564  * @tc.desc: Encodes the specified byte array as a String using the Base64 encoding scheme.
1565  * @tc.type: FUNC
1566  */
1567 HWTEST_F(NativeEngineTest, encodeToStringTest005, testing::ext::TestSize.Level0)
1568 {
1569     HILOG_INFO("encodeToStringTest005 start");
1570     napi_env env = (napi_env)engine_;
1571     OHOS::Util::Base64 base64;
1572 
1573     unsigned char input[6] = {66, 97, 115, 101, 54, 52};
1574     napi_value arrayBuffer = nullptr;
1575     size_t arrayBufferSize = 6;
1576     void* data = nullptr;
1577     napi_create_arraybuffer(env, arrayBufferSize, &data, &arrayBuffer);
1578     int ret = memcpy_s(data, sizeof(input), reinterpret_cast<void*>(input), sizeof(input));
1579     ASSERT_EQ(0, ret);
1580     napi_value src = nullptr;
1581     napi_create_typedarray(env, napi_uint8_array, arrayBufferSize, arrayBuffer, 0, &src);
1582     napi_value result = base64.EncodeToStringSync(env, src);
1583     size_t prolen = 0;
1584     char* inputString = nullptr;
1585     napi_get_value_string_utf8(env, result, nullptr, 0, &prolen);
1586     if (prolen > 0) {
1587         inputString = new char[prolen + 1];
1588         if (memset_s(inputString, prolen + 1, '\0', prolen + 1) != 0) {
1589             napi_throw_error(env, "-1", "decode inputString memset_s failed");
1590         }
1591     } else {
1592         napi_throw_error(env, "-2", "prolen is error !");
1593     }
1594     napi_get_value_string_utf8(env, result, inputString, prolen + 1, &prolen);
1595     ASSERT_STREQ("QmFzZTY0", inputString);
1596     if (inputString != nullptr) {
1597         delete []inputString;
1598         inputString = nullptr;
1599     }
1600 }
1601 
1602 /* @tc.name: decodeTest001
1603  * @tc.desc: Decodes the Base64-encoded string or input u8 array
1604              into the newly allocated u8 array using the Base64 encoding scheme.
1605  * @tc.type: FUNC
1606  */
1607 HWTEST_F(NativeEngineTest, decodeTest001, testing::ext::TestSize.Level0)
1608 {
1609     HILOG_INFO("decodeTest001 start");
1610     napi_env env = (napi_env)engine_;
1611     OHOS::Util::Base64 base64;
1612 
1613     unsigned char input[4] = {99, 122, 69, 122};
1614     napi_value arrayBuffer = nullptr;
1615     size_t arrayBufferSize = 4;
1616     void* data = nullptr;
1617     napi_create_arraybuffer(env, arrayBufferSize, &data, &arrayBuffer);
1618     int ret = memcpy_s(data, sizeof(input), reinterpret_cast<void*>(input), sizeof(input));
1619     ASSERT_EQ(0, ret);
1620     napi_value src = nullptr;
1621     napi_create_typedarray(env, napi_uint8_array, arrayBufferSize, arrayBuffer, 0, &src);
1622     napi_value result = base64.DecodeSync(env, src);
1623     char excepted[3] = {115, 49, 51};
1624     napi_typedarray_type type;
1625     size_t srcLength = 0;
1626     void* srcData = nullptr;
1627     napi_value srcBuffer = nullptr;
1628     size_t byteOffset = 0;
1629     napi_get_typedarray_info(env, result, &type, &srcLength, &srcData, &srcBuffer, &byteOffset);
1630     char* res = (char*)srcData;
1631 
1632     ASSERT_EQ(res[0], excepted[0]);
1633     ASSERT_EQ(res[1], excepted[1]);
1634     ASSERT_EQ(res[2], excepted[2]);
1635 }
1636 
1637 /* @tc.name: decodeTest002
1638  * @tc.desc: Decodes the Base64-encoded string or input u8 array
1639              into the newly allocated u8 array using the Base64 encoding scheme.
1640  * @tc.type: FUNC
1641  */
1642 HWTEST_F(NativeEngineTest, decodeTest002, testing::ext::TestSize.Level0)
1643 {
1644     HILOG_INFO("decodeTest002 start");
1645     napi_env env = (napi_env)engine_;
1646     OHOS::Util::Base64 base64;
1647 
1648     unsigned char input[20] = {81, 109, 70, 122, 90, 84, 89, 48, 73, 69, 53, 118, 90, 71, 85, 117, 97, 110, 77, 61};
1649     napi_value arrayBuffer = nullptr;
1650     size_t arrayBufferSize = 20;
1651     void* data = nullptr;
1652     napi_create_arraybuffer(env, arrayBufferSize, &data, &arrayBuffer);
1653     int ret = memcpy_s(data, sizeof(input), reinterpret_cast<void*>(input), sizeof(input));
1654     ASSERT_EQ(0, ret);
1655     napi_value src = nullptr;
1656     napi_create_typedarray(env, napi_uint8_array, arrayBufferSize, arrayBuffer, 0, &src);
1657     napi_value result = base64.DecodeSync(env, src);
1658     char excepted[14] = {66, 97, 115, 101, 54, 52, 32, 78, 111, 100, 101, 46, 106, 115};
1659     napi_typedarray_type type;
1660     size_t srcLength = 0;
1661     void* srcData = nullptr;
1662     napi_value srcBuffer = nullptr;
1663     size_t byteOffset = 0;
1664     napi_get_typedarray_info(env, result, &type, &srcLength, &srcData, &srcBuffer, &byteOffset);
1665     char* res = (char*)srcData;
1666 
1667     for (size_t i = 0; i < 14; i++) {
1668         ASSERT_EQ(res[i], excepted[i]);
1669     }
1670 }
1671 
1672 /* @tc.name: decodeTest003
1673  * @tc.desc: Decodes the Base64-encoded string or input u8 array
1674              into the newly allocated u8 array using the Base64 encoding scheme.
1675  * @tc.type: FUNC
1676  */
1677 HWTEST_F(NativeEngineTest, decodeTest003, testing::ext::TestSize.Level0)
1678 {
1679     HILOG_INFO("decodeTest003 start");
1680     napi_env env = (napi_env)engine_;
1681     OHOS::Util::Base64 base64;
1682 
1683     std::string input = "czEz";
1684     napi_value src = nullptr;
1685     napi_create_string_utf8(env, input.c_str(), input.size(), &src);
1686     napi_value result = base64.DecodeSync(env, src);
1687     char excepted[3] = {115, 49, 51};
1688     napi_typedarray_type type;
1689     size_t srcLength = 0;
1690     void* srcData = nullptr;
1691     napi_value srcBuffer = nullptr;
1692     size_t byteOffset = 0;
1693     napi_get_typedarray_info(env, result, &type, &srcLength, &srcData, &srcBuffer, &byteOffset);
1694     char* res = (char*)srcData;
1695 
1696     ASSERT_EQ(res[0], excepted[0]);
1697     ASSERT_EQ(res[1], excepted[1]);
1698     ASSERT_EQ(res[2], excepted[2]);
1699 }
1700 
1701 /* @tc.name: decodeTest004
1702  * @tc.desc: Decodes the Base64-encoded string or input u8 array
1703              into the newly allocated u8 array using the Base64 encoding scheme.
1704  * @tc.type: FUNC
1705  */
1706 HWTEST_F(NativeEngineTest, decodeTest004, testing::ext::TestSize.Level0)
1707 {
1708     HILOG_INFO("decodeTest004 start");
1709     napi_env env = (napi_env)engine_;
1710     OHOS::Util::Base64 base64;
1711 
1712     std::string input = "qK6b/w==";
1713     napi_value src = nullptr;
1714     napi_create_string_utf8(env, input.c_str(), input.size(), &src);
1715     napi_value result = base64.DecodeSync(env, src);
1716     char excepted[4] = {168, 174, 155, 255};
1717     napi_typedarray_type type;
1718     size_t srcLength = 0;
1719     void* srcData = nullptr;
1720     napi_value srcBuffer = nullptr;
1721     size_t byteOffset = 0;
1722     napi_get_typedarray_info(env, result, &type, &srcLength, &srcData, &srcBuffer, &byteOffset);
1723     char* res = (char*)srcData;
1724     for (size_t i = 0; i < 4; i++) {
1725         ASSERT_EQ(res[i], excepted[i]);
1726     }
1727 }
1728 
1729 /* @tc.name: decodeTest005
1730  * @tc.desc: Decodes the Base64-encoded string or input u8 array
1731              into the newly allocated u8 array using the Base64 encoding scheme.
1732  * @tc.type: FUNC
1733  */
1734 HWTEST_F(NativeEngineTest, decodeTest005, testing::ext::TestSize.Level0)
1735 {
1736     HILOG_INFO("decodeTest005 start");
1737     napi_env env = (napi_env)engine_;
1738     OHOS::Util::Base64 base64;
1739 
1740     std::string input = "QmFzZTY0";
1741     napi_value src = nullptr;
1742     napi_create_string_utf8(env, input.c_str(), input.size(), &src);
1743     napi_value result = base64.DecodeSync(env, src);
1744     char excepted[6] = {66, 97, 115, 101, 54, 52};
1745     napi_typedarray_type type;
1746     size_t srcLength = 0;
1747     void* srcData = nullptr;
1748     napi_value srcBuffer = nullptr;
1749     size_t byteOffset = 0;
1750     napi_get_typedarray_info(env, result, &type, &srcLength, &srcData, &srcBuffer, &byteOffset);
1751     char* res = (char*)srcData;
1752     for (size_t i = 0; i < 6; i++) {
1753         ASSERT_EQ(res[i], excepted[i]);
1754     }
1755 }
1756 
1757 /* @tc.name: encodeAsyncTest001
1758  * @tc.desc: Asynchronously encodes all bytes in the specified u8 array
1759              into the newly allocated u8 array using the Base64 encoding scheme.
1760  * @tc.type: FUNC
1761  */
1762 HWTEST_F(NativeEngineTest, encodeAsyncTest001, testing::ext::TestSize.Level0)
1763 {
1764     HILOG_INFO("encodeAsyncTest001 start");
1765     napi_env env = (napi_env)engine_;
1766     OHOS::Util::Base64 base64;
1767     unsigned char input[3] = {0x73, 0x31, 0x33};
1768     napi_value arrayBuffer = nullptr;
1769     void* data = nullptr;
1770     size_t arrayBufferSize = 3;
1771     napi_create_arraybuffer(env, arrayBufferSize, &data, &arrayBuffer);
1772     int ret = memcpy_s(data, sizeof(input), reinterpret_cast<void*>(input), sizeof(input));
1773     ASSERT_EQ(0, ret);
1774     napi_value src = nullptr;
1775     napi_create_typedarray(env, napi_uint8_array, arrayBufferSize, arrayBuffer, 0, &src);
1776 
1777     napi_value result = base64.Encode(env, src);
1778     bool res = false;
1779     napi_is_promise(env, result, &res);
1780     ASSERT_TRUE(res);
1781 }
1782 
1783 /* @tc.name: encodeAsyncTest002
1784  * @tc.desc: Asynchronously encodes all bytes in the specified u8 array
1785              into the newly allocated u8 array using the Base64 encoding scheme.
1786  * @tc.type: FUNC
1787  */
1788 HWTEST_F(NativeEngineTest, encodeAsyncTest002, testing::ext::TestSize.Level0)
1789 {
1790     HILOG_INFO("encodeAsyncTest002 start");
1791     napi_env env = (napi_env)engine_;
1792     OHOS::Util::Base64 base64;
1793     unsigned char input[14] = {66, 97, 115, 101, 54, 52, 32, 78, 111, 100, 101, 46, 106, 115};
1794     napi_value arrayBuffer = nullptr;
1795     void* data = nullptr;
1796     size_t arrayBufferSize = 14;
1797     napi_create_arraybuffer(env, arrayBufferSize, &data, &arrayBuffer);
1798     int ret = memcpy_s(data, sizeof(input), reinterpret_cast<void*>(input), sizeof(input));
1799     ASSERT_EQ(0, ret);
1800     napi_value src = nullptr;
1801     napi_create_typedarray(env, napi_uint8_array, arrayBufferSize, arrayBuffer, 0, &src);
1802 
1803     napi_value result = base64.Encode(env, src);
1804     bool res = false;
1805     napi_is_promise(env, result, &res);
1806     ASSERT_TRUE(res);
1807 }
1808 
1809 /* @tc.name: encodeAsyncTest003
1810  * @tc.desc: Asynchronously encodes all bytes in the specified u8 array
1811              into the newly allocated u8 array using the Base64 encoding scheme.
1812  * @tc.type: FUNC
1813  */
1814 HWTEST_F(NativeEngineTest, encodeAsyncTest003, testing::ext::TestSize.Level0)
1815 {
1816     HILOG_INFO("encodeAsyncTest003 start");
1817     napi_env env = (napi_env)engine_;
1818     OHOS::Util::Base64 base64;
1819     unsigned char input[26] = {66, 97, 115, 101, 54, 52, 32, 69, 110,
1820                               99, 111, 100, 105, 110, 103, 32, 105, 110, 32, 78, 111, 100, 101, 46, 106, 115};;
1821     napi_value arrayBuffer = nullptr;
1822     void* data = nullptr;
1823     size_t arrayBufferSize = 26;
1824     napi_create_arraybuffer(env, arrayBufferSize, &data, &arrayBuffer);
1825     int ret = memcpy_s(data, sizeof(input), reinterpret_cast<void*>(input), sizeof(input));
1826     ASSERT_EQ(0, ret);
1827     napi_value src = nullptr;
1828     napi_create_typedarray(env, napi_uint8_array, arrayBufferSize, arrayBuffer, 0, &src);
1829 
1830     napi_value result = base64.Encode(env, src);
1831     bool res = false;
1832     napi_is_promise(env, result, &res);
1833     ASSERT_TRUE(res);
1834 }
1835 
1836 /* @tc.name: encodeAsyncTest004
1837  * @tc.desc: Asynchronously encodes all bytes in the specified u8 array
1838              into the newly allocated u8 array using the Base64 encoding scheme.
1839  * @tc.type: FUNC
1840  */
1841 HWTEST_F(NativeEngineTest, encodeAsyncTest004, testing::ext::TestSize.Level0)
1842 {
1843     HILOG_INFO("encodeAsyncTest004 start");
1844     napi_env env = (napi_env)engine_;
1845     OHOS::Util::Base64 base64;
1846     unsigned char input[4] = {168, 174, 155, 255};
1847     napi_value arrayBuffer = nullptr;
1848     void* data = nullptr;
1849     size_t arrayBufferSize = 4;
1850     napi_create_arraybuffer(env, arrayBufferSize, &data, &arrayBuffer);
1851     int ret = memcpy_s(data, sizeof(input), reinterpret_cast<void*>(input), sizeof(input));
1852     ASSERT_EQ(0, ret);
1853     napi_value src = nullptr;
1854     napi_create_typedarray(env, napi_uint8_array, arrayBufferSize, arrayBuffer, 0, &src);
1855 
1856     napi_value result = base64.Encode(env, src);
1857     bool res = false;
1858     napi_is_promise(env, result, &res);
1859     ASSERT_TRUE(res);
1860 }
1861 
1862 /* @tc.name: encodeAsyncTest005
1863  * @tc.desc: Asynchronously encodes all bytes in the specified u8 array
1864              into the newly allocated u8 array using the Base64 encoding scheme.
1865  * @tc.type: FUNC
1866  */
1867 HWTEST_F(NativeEngineTest, encodeAsyncTest005, testing::ext::TestSize.Level0)
1868 {
1869     HILOG_INFO("encodeAsyncTest005 start");
1870     napi_env env = (napi_env)engine_;
1871     OHOS::Util::Base64 base64;
1872     unsigned char input[6] = {66, 97, 115, 101, 54, 52};
1873     napi_value arrayBuffer = nullptr;
1874     void* data = nullptr;
1875     size_t arrayBufferSize = 6;
1876     napi_create_arraybuffer(env, arrayBufferSize, &data, &arrayBuffer);
1877     int ret = memcpy_s(data, sizeof(input), reinterpret_cast<void*>(input), sizeof(input));
1878     ASSERT_EQ(0, ret);
1879     napi_value src = nullptr;
1880     napi_create_typedarray(env, napi_uint8_array, arrayBufferSize, arrayBuffer, 0, &src);
1881 
1882     napi_value result = base64.Encode(env, src);
1883     bool res = false;
1884     napi_is_promise(env, result, &res);
1885     ASSERT_TRUE(res);
1886 }
1887 
1888 /* @tc.name: encodeToStringAsyncTest001
1889  * @tc.desc: Asynchronously encodes the specified byte array into a String using the Base64 encoding scheme.
1890  * @tc.type: FUNC
1891  */
1892 HWTEST_F(NativeEngineTest, encodeToStringAsyncTest001, testing::ext::TestSize.Level0)
1893 {
1894     HILOG_INFO("encodeToStringAsyncTest001 start");
1895     napi_env env = (napi_env)engine_;
1896     OHOS::Util::Base64 base64;
1897 
1898     unsigned char input[3] = {115, 49, 51};
1899     napi_value arrayBuffer = nullptr;
1900     size_t arrayBufferSize = 3;
1901     void* data = nullptr;
1902     napi_create_arraybuffer(env, arrayBufferSize, &data, &arrayBuffer);
1903     int ret = memcpy_s(data, sizeof(input), reinterpret_cast<void*>(input), sizeof(input));
1904     ASSERT_EQ(0, ret);
1905     napi_value src = nullptr;
1906     napi_create_typedarray(env, napi_uint8_array, arrayBufferSize, arrayBuffer, 0, &src);
1907     napi_value result = base64.EncodeToString(env, src);
1908     bool res = false;
1909     napi_is_promise(env, result, &res);
1910     ASSERT_TRUE(res);
1911 }
1912 
1913 /* @tc.name: encodeToStringAsyncTest002
1914  * @tc.desc: Asynchronously encodes the specified byte array into a String using the Base64 encoding scheme.
1915  * @tc.type: FUNC
1916  */
1917 HWTEST_F(NativeEngineTest, encodeToStringAsyncTest002, testing::ext::TestSize.Level0)
1918 {
1919     HILOG_INFO("encodeToStringAsyncTest002 start");
1920     napi_env env = (napi_env)engine_;
1921     OHOS::Util::Base64 base64;
1922     unsigned char input[14] = {66, 97, 115, 101, 54, 52, 32, 78, 111, 100, 101, 46, 106, 115};
1923     napi_value arrayBuffer = nullptr;
1924     void* data = nullptr;
1925     size_t arrayBufferSize = 14;
1926     napi_create_arraybuffer(env, arrayBufferSize, &data, &arrayBuffer);
1927     int ret = memcpy_s(data, sizeof(input), reinterpret_cast<void*>(input), sizeof(input));
1928     ASSERT_EQ(0, ret);
1929     napi_value src = nullptr;
1930     napi_create_typedarray(env, napi_uint8_array, arrayBufferSize, arrayBuffer, 0, &src);
1931 
1932     napi_value result = base64.EncodeToString(env, src);
1933     bool res = false;
1934     napi_is_promise(env, result, &res);
1935     ASSERT_TRUE(res);
1936 }
1937 
1938 /* @tc.name: encodeToStringAsyncTest003
1939  * @tc.desc: Asynchronously encodes the specified byte array into a String using the Base64 encoding scheme.
1940  * @tc.type: FUNC
1941  */
1942 HWTEST_F(NativeEngineTest, encodeToStringAsyncTest003, testing::ext::TestSize.Level0)
1943 {
1944     HILOG_INFO("encodeToStringAsyncTest003 start");
1945     napi_env env = (napi_env)engine_;
1946     OHOS::Util::Base64 base64;
1947     unsigned char input[26] = {66, 97, 115, 101, 54, 52, 32, 69, 110,
1948                                 99, 111, 100, 105, 110, 103, 32, 105, 110, 32, 78, 111, 100, 101, 46, 106, 115};
1949     napi_value arrayBuffer = nullptr;
1950     void* data = nullptr;
1951     size_t arrayBufferSize = 26;
1952     napi_create_arraybuffer(env, arrayBufferSize, &data, &arrayBuffer);
1953     int ret = memcpy_s(data, sizeof(input), reinterpret_cast<void*>(input), sizeof(input));
1954     ASSERT_EQ(0, ret);
1955     napi_value src = nullptr;
1956     napi_create_typedarray(env, napi_uint8_array, arrayBufferSize, arrayBuffer, 0, &src);
1957 
1958     napi_value result = base64.EncodeToString(env, src);
1959     bool res = false;
1960     napi_is_promise(env, result, &res);
1961     ASSERT_TRUE(res);
1962 }
1963 
1964 /* @tc.name: encodeToStringAsyncTest004
1965  * @tc.desc: Asynchronously encodes the specified byte array into a String using the Base64 encoding scheme.
1966  * @tc.type: FUNC
1967  */
1968 HWTEST_F(NativeEngineTest, encodeToStringAsyncTest004, testing::ext::TestSize.Level0)
1969 {
1970     HILOG_INFO("encodeToStringAsyncTest004 start");
1971     napi_env env = (napi_env)engine_;
1972     OHOS::Util::Base64 base64;
1973     unsigned char input[4] = {168, 174, 155, 255};
1974     napi_value arrayBuffer = nullptr;
1975     void* data = nullptr;
1976     size_t arrayBufferSize = 4;
1977     napi_create_arraybuffer(env, arrayBufferSize, &data, &arrayBuffer);
1978     int ret = memcpy_s(data, sizeof(input), reinterpret_cast<void*>(input), sizeof(input));
1979     ASSERT_EQ(0, ret);
1980     napi_value src = nullptr;
1981     napi_create_typedarray(env, napi_uint8_array, arrayBufferSize, arrayBuffer, 0, &src);
1982 
1983     napi_value result = base64.EncodeToString(env, src);
1984     bool res = false;
1985     napi_is_promise(env, result, &res);
1986     ASSERT_TRUE(res);
1987 }
1988 
1989 /* @tc.name: encodeToStringAsyncTest005
1990  * @tc.desc: Asynchronously encodes the specified byte array into a String using the Base64 encoding scheme.
1991  * @tc.type: FUNC
1992  */
1993 HWTEST_F(NativeEngineTest, encodeToStringAsyncTest005, testing::ext::TestSize.Level0)
1994 {
1995     HILOG_INFO("encodeToStringAsyncTest005 start");
1996     napi_env env = (napi_env)engine_;
1997     OHOS::Util::Base64 base64;
1998     unsigned char input[6] = {66, 97, 115, 101, 54, 52};
1999     napi_value arrayBuffer = nullptr;
2000     void* data = nullptr;
2001     size_t arrayBufferSize = 6;
2002     napi_create_arraybuffer(env, arrayBufferSize, &data, &arrayBuffer);
2003     int ret = memcpy_s(data, sizeof(input), reinterpret_cast<void*>(input), sizeof(input));
2004     ASSERT_EQ(0, ret);
2005     napi_value src = nullptr;
2006     napi_create_typedarray(env, napi_uint8_array, arrayBufferSize, arrayBuffer, 0, &src);
2007 
2008     napi_value result = base64.EncodeToString(env, src);
2009     bool res = false;
2010     napi_is_promise(env, result, &res);
2011     ASSERT_TRUE(res);
2012 }
2013 
2014 /* @tc.name: decodeAsyncTest001
2015  * @tc.desc: Use the Base64 encoding scheme to asynchronously decode a
2016              Base64-encoded string or input u8 array into a newly allocated u8 array.
2017  * @tc.type: FUNC
2018  */
2019 HWTEST_F(NativeEngineTest, decodeAsyncTest001, testing::ext::TestSize.Level0)
2020 {
2021     HILOG_INFO("decodeAsyncTest001 start");
2022     napi_env env = (napi_env)engine_;
2023     OHOS::Util::Base64 base64;
2024 
2025     unsigned char input[4] = {99, 122, 69, 122};
2026     napi_value arrayBuffer = nullptr;
2027     size_t arrayBufferSize = 4;
2028     void* data = nullptr;
2029     napi_create_arraybuffer(env, arrayBufferSize, &data, &arrayBuffer);
2030     int ret = memcpy_s(data, sizeof(input), reinterpret_cast<void*>(input), sizeof(input));
2031     ASSERT_EQ(0, ret);
2032     napi_value src = nullptr;
2033     napi_create_typedarray(env, napi_uint8_array, arrayBufferSize, arrayBuffer, 0, &src);
2034     napi_value result = base64.Decode(env, src);
2035     bool res = false;
2036     napi_is_promise(env, result, &res);
2037     ASSERT_TRUE(res);
2038 }
2039 
2040 /* @tc.name: decodeAsyncTest002
2041  * @tc.desc: Use the Base64 encoding scheme to asynchronously decode a
2042              Base64-encoded string or input u8 array into a newly allocated u8 array.
2043  * @tc.type: FUNC
2044  */
2045 HWTEST_F(NativeEngineTest, decodeAsyncTest002, testing::ext::TestSize.Level0)
2046 {
2047     HILOG_INFO("decodeAsyncTest002 start");
2048     napi_env env = (napi_env)engine_;
2049     OHOS::Util::Base64 base64;
2050 
2051     unsigned char input[8] = {113, 75, 54, 98, 47, 119, 61, 61};
2052     napi_value arrayBuffer = nullptr;
2053     size_t arrayBufferSize = 8;
2054     void* data = nullptr;
2055     napi_create_arraybuffer(env, arrayBufferSize, &data, &arrayBuffer);
2056     int ret = memcpy_s(data, sizeof(input), reinterpret_cast<void*>(input), sizeof(input));
2057     ASSERT_EQ(0, ret);
2058     napi_value src = nullptr;
2059     napi_create_typedarray(env, napi_uint8_array, arrayBufferSize, arrayBuffer, 0, &src);
2060     napi_value result = base64.Decode(env, src);
2061     bool res = false;
2062     napi_is_promise(env, result, &res);
2063     ASSERT_TRUE(res);
2064 }
2065 
2066 /* @tc.name: decodeAsyncTest003
2067  * @tc.desc: Use the Base64 encoding scheme to asynchronously decode a
2068              Base64-encoded string or input u8 array into a newly allocated u8 array.
2069  * @tc.type: FUNC
2070  */
2071 HWTEST_F(NativeEngineTest, decodeAsyncTest003, testing::ext::TestSize.Level0)
2072 {
2073     HILOG_INFO("decodeAsyncTest003 start");
2074     napi_env env = (napi_env)engine_;
2075     OHOS::Util::Base64 base64;
2076 
2077     std::string input = "czEz";
2078     napi_value src = nullptr;
2079     napi_create_string_utf8(env, input.c_str(), input.size(), &src);
2080     napi_value result = base64.Decode(env, src);
2081     bool res = false;
2082     napi_is_promise(env, result, &res);
2083     ASSERT_TRUE(res);
2084 }
2085 
2086 /* @tc.name: decodeAsyncTest004
2087  * @tc.desc: Use the Base64 encoding scheme to asynchronously decode a
2088              Base64-encoded string or input u8 array into a newly allocated u8 array.
2089  * @tc.type: FUNC
2090  */
2091 HWTEST_F(NativeEngineTest, decodeAsyncTest004, testing::ext::TestSize.Level0)
2092 {
2093     HILOG_INFO("decodeAsyncTest004 start");
2094     napi_env env = (napi_env)engine_;
2095     OHOS::Util::Base64 base64;
2096 
2097     std::string input = "QmFzZTY0IEVuY29kaW5nIGluIE5vZGUuanM=";
2098     napi_value src = nullptr;
2099     napi_create_string_utf8(env, input.c_str(), input.size(), &src);
2100     napi_value result = base64.Decode(env, src);
2101     bool res = false;
2102     napi_is_promise(env, result, &res);
2103     ASSERT_TRUE(res);
2104 }
2105 
2106 /* @tc.name: decodeAsyncTest005
2107  * @tc.desc: Use the Base64 encoding scheme to asynchronously decode a
2108              Base64-encoded string or input u8 array into a newly allocated u8 array.
2109  * @tc.type: FUNC
2110  */
2111 HWTEST_F(NativeEngineTest, decodeAsyncTest005, testing::ext::TestSize.Level0)
2112 {
2113     HILOG_INFO("decodeAsyncTest005 start");
2114     napi_env env = (napi_env)engine_;
2115     OHOS::Util::Base64 base64;
2116 
2117     std::string input = "qK6b/w==";
2118     napi_value src = nullptr;
2119     napi_create_string_utf8(env, input.c_str(), input.size(), &src);
2120     napi_value result = base64.Decode(env, src);
2121     bool res = false;
2122     napi_is_promise(env, result, &res);
2123     ASSERT_TRUE(res);
2124 }