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_stringdecoder.h" 20 #include "commonlibrary/ets_utils/js_util_module/util/js_textencoder.h" 21 #include "commonlibrary/ets_utils/js_util_module/util/js_textdecoder.h" 22 #include "commonlibrary/ets_utils/js_util_module/util/js_base64.h" 23 #include "ohos/init_data.h" 24 #include "tools/log.h" 25 #include "napi/native_api.h" 26 #include "napi/native_node_api.h" 27 #include "securec.h" 28 29 30 #define ASSERT_CHECK_CALL(call) \ 31 { \ 32 ASSERT_EQ(call, napi_ok); \ 33 } 34 35 #define ASSERT_CHECK_VALUE_TYPE(env, value, type) \ 36 { \ 37 napi_valuetype valueType = napi_undefined; \ 38 ASSERT_TRUE(value != nullptr); \ 39 ASSERT_CHECK_CALL(napi_typeof(env, value, &valueType)); \ 40 ASSERT_EQ(valueType, type); \ 41 } 42 43 /* @tc.name: GetStringUUIDTest001 44 * @tc.desc: Test Generate a random RFC 4122 version 4 UUID. 45 * @tc.type: FUNC 46 */ 47 HWTEST_F(NativeEngineTest, GetStringUUIDTest001, testing::ext::TestSize.Level0) 48 { 49 napi_env env = (napi_env)engine_; 50 std::string uuid = OHOS::Util::GetStringUUID(env, true); 51 ASSERT_EQ(uuid.length(), 36); 52 } 53 54 /* @tc.name: GetStringUUIDTest002 55 * @tc.desc: Test Generate a random RFC 4122 version 4 UUID. 56 * @tc.type: FUNC 57 */ 58 HWTEST_F(NativeEngineTest, GetStringUUIDTest002, testing::ext::TestSize.Level0) 59 { 60 napi_env env = (napi_env)engine_; 61 std::string uuid = OHOS::Util::GetStringUUID(env, false); 62 ASSERT_EQ(uuid.length(), 36); 63 } 64 65 /* @tc.name: GetBinaryUUIDTest001 66 * @tc.desc: Test Generate a random RFC 4122 version 4 UUID. 67 * @tc.type: FUNC 68 */ 69 HWTEST_F(NativeEngineTest, GetBinaryUUIDTest001, testing::ext::TestSize.Level0) 70 { 71 napi_env env = (napi_env)engine_; 72 napi_value arr = OHOS::Util::GetBinaryUUID(env, true); 73 napi_typedarray_type type = napi_int8_array; 74 size_t byteOffset = 0; 75 size_t length = 0; 76 void* resultData = nullptr; 77 napi_value resultBuffer = nullptr; 78 napi_get_typedarray_info(env, arr, &type, &length, &resultData, &resultBuffer, &byteOffset); 79 ASSERT_EQ(length, 16); 80 } 81 82 /* @tc.name: GetBinaryUUIDTest002 83 * @tc.desc: Test Generate a random RFC 4122 version 4 UUID. 84 * @tc.type: FUNC 85 */ 86 HWTEST_F(NativeEngineTest, GetBinaryUUIDTest002, testing::ext::TestSize.Level0) 87 { 88 napi_env env = (napi_env)engine_; 89 napi_value arr = OHOS::Util::GetBinaryUUID(env, false); 90 napi_typedarray_type type = napi_int8_array; 91 size_t byteOffset = 0; 92 size_t length = 0; 93 void* resultData = nullptr; 94 napi_value resultBuffer = nullptr; 95 napi_get_typedarray_info(env, arr, &type, &length, &resultData, &resultBuffer, &byteOffset); 96 ASSERT_EQ(length, 16); 97 } 98 99 /* @tc.name: DoParseUUIDTest001 100 * @tc.desc: Parse a UUID from the string standard representation as described in the RFC 4122 version 4. 101 * @tc.type: FUNC 102 */ 103 HWTEST_F(NativeEngineTest, DoParseUUIDTest001, testing::ext::TestSize.Level0) 104 { 105 napi_env env = (napi_env)engine_; 106 napi_value src = nullptr; 107 napi_create_string_utf8(env, "84bdf796-66cc-4655-9b89-d6218d100f9c", NAPI_AUTO_LENGTH, &src); 108 napi_value arr = OHOS::Util::DoParseUUID(env, src); 109 napi_typedarray_type type = napi_int8_array; 110 size_t byteOffset = 0; 111 size_t length = 0; 112 void* resultData = nullptr; 113 napi_value resultBuffer = nullptr; 114 napi_get_typedarray_info(env, arr, &type, &length, &resultData, &resultBuffer, &byteOffset); 115 ASSERT_EQ(length, 16); 116 } 117 118 /* @tc.name: DoParseUUIDTest002 119 * @tc.desc: Parse a UUID from the string standard representation as described in the RFC 4122 version 4. 120 * @tc.type: FUNC 121 */ 122 HWTEST_F(NativeEngineTest, DoParseUUIDTest002, testing::ext::TestSize.Level0) 123 { 124 napi_env env = (napi_env)engine_; 125 napi_value src = nullptr; 126 std::string input = "abc123"; 127 napi_create_string_utf8(env, input.c_str(), NAPI_AUTO_LENGTH, &src); 128 napi_value arr = OHOS::Util::DoParseUUID(env, src); 129 napi_typedarray_type type = napi_int8_array; 130 size_t byteOffset = 0; 131 size_t length = 0; 132 void* resultData = nullptr; 133 napi_value resultBuffer = nullptr; 134 napi_get_typedarray_info(env, arr, &type, &length, &resultData, &resultBuffer, &byteOffset); 135 ASSERT_EQ(length, 16); 136 } 137 138 /* @tc.name: DoParseUUIDTest003 139 * @tc.desc: Parse a UUID from the string standard representation as described in the RFC 4122 version 4. 140 * @tc.type: FUNC 141 */ 142 HWTEST_F(NativeEngineTest, DoParseUUIDTest003, testing::ext::TestSize.Level0) 143 { 144 napi_env env = (napi_env)engine_; 145 napi_value src = nullptr; 146 std::string input = "abc123abc"; 147 napi_create_string_utf8(env, input.c_str(), NAPI_AUTO_LENGTH, &src); 148 napi_value arr = OHOS::Util::DoParseUUID(env, src); 149 napi_typedarray_type type = napi_int8_array; 150 size_t byteOffset = 0; 151 size_t length = 0; 152 void* resultData = nullptr; 153 napi_value resultBuffer = nullptr; 154 napi_get_typedarray_info(env, arr, &type, &length, &resultData, &resultBuffer, &byteOffset); 155 ASSERT_EQ(length, 16); 156 } 157 158 /* @tc.name: DoParseUUIDTest004 159 * @tc.desc: Parse a UUID from the string standard representation as described in the RFC 4122 version 4. 160 * @tc.type: FUNC 161 */ 162 HWTEST_F(NativeEngineTest, DoParseUUIDTest004, testing::ext::TestSize.Level0) 163 { 164 napi_env env = (napi_env)engine_; 165 napi_value src = nullptr; 166 napi_value arr = OHOS::Util::DoParseUUID(env, src); 167 ASSERT_EQ(arr, nullptr); 168 } 169 170 /* @tc.name: HexToCharUUIDTest001 171 * @tc.desc: Hex to char with g convert to x. 172 * @tc.type: FUNC 173 */ 174 HWTEST_F(NativeEngineTest, HexToCharUUIDTest001, testing::ext::TestSize.Level0) 175 { 176 unsigned char input = 'g'; 177 unsigned char res = OHOS::Util::HexToChar(input); 178 ASSERT_EQ(res, 'x'); 179 } 180 181 /* @tc.name: getEncodingTest001 182 * @tc.desc: Test acquire encoding mode. 183 * @tc.type: FUNC 184 */ 185 HWTEST_F(NativeEngineTest, getEncodingTest001, testing::ext::TestSize.Level0) 186 { 187 HILOG_INFO("getEncodingTest001 start"); 188 napi_env env = (napi_env)engine_; 189 190 OHOS::Util::TextEncoder textEncoder("GBK"); 191 textEncoder.SetOrgEncoding("GBK"); 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, "GBK"); 203 if (buffer != nullptr) { 204 delete []buffer; 205 buffer = nullptr; 206 } 207 } 208 209 /* @tc.name: getEncodingTest002 210 * @tc.desc: Test acquire encoding mode. 211 * @tc.type: FUNC 212 */ 213 HWTEST_F(NativeEngineTest, getEncodingTest002, testing::ext::TestSize.Level0) 214 { 215 HILOG_INFO("getEncodingTest002 start"); 216 napi_env env = (napi_env)engine_; 217 218 OHOS::Util::TextEncoder textEncoder("gb18030"); 219 textEncoder.SetOrgEncoding("gb18030"); 220 napi_value result = textEncoder.GetEncoding(env); 221 222 char *buffer = nullptr; 223 size_t bufferSize = 0; 224 napi_get_value_string_utf8(env, result, buffer, -1, &bufferSize); 225 if (bufferSize > 0) { 226 buffer = new char[bufferSize + 1]; 227 napi_get_value_string_utf8(env, result, buffer, bufferSize + 1, &bufferSize); 228 } 229 230 ASSERT_STREQ(buffer, "gb18030"); 231 if (buffer != nullptr) { 232 delete []buffer; 233 buffer = nullptr; 234 } 235 } 236 237 /* @tc.name: getEncodingTest003 238 * @tc.desc: Test acquire encoding mode. 239 * @tc.type: FUNC 240 */ 241 HWTEST_F(NativeEngineTest, getEncodingTest003, testing::ext::TestSize.Level0) 242 { 243 HILOG_INFO("getEncodingTest003 start"); 244 napi_env env = (napi_env)engine_; 245 246 OHOS::Util::TextEncoder textEncoder("GB18030"); 247 textEncoder.SetOrgEncoding("GB18030"); 248 napi_value result = textEncoder.GetEncoding(env); 249 250 char *buffer = nullptr; 251 size_t bufferSize = 0; 252 napi_get_value_string_utf8(env, result, buffer, -1, &bufferSize); 253 if (bufferSize > 0) { 254 buffer = new char[bufferSize + 1]; 255 napi_get_value_string_utf8(env, result, buffer, bufferSize + 1, &bufferSize); 256 } 257 258 ASSERT_STREQ(buffer, "GB18030"); 259 if (buffer != nullptr) { 260 delete []buffer; 261 buffer = nullptr; 262 } 263 } 264 265 /** 266 * @tc.name: textEncodeTest001 267 * @tc.desc: Test encode src. 268 * @tc.type: FUNC 269 */ 270 HWTEST_F(NativeEngineTest, textEncodeTest001, testing::ext::TestSize.Level0) 271 { 272 HILOG_INFO("getEncodingTest001 start"); 273 napi_env env = (napi_env)engine_; 274 OHOS::Util::TextEncoder textEncoder("utf-8"); 275 276 std::string input = "abc123"; 277 napi_value src = nullptr; 278 napi_create_string_utf8(env, input.c_str(), input.size(), &src); 279 napi_value result = textEncoder.Encode(env, src); 280 281 char excepted[7] = {0x61, 0x62, 0x63, 0x31, 0x32, 0x33, 0}; 282 283 napi_typedarray_type type; 284 size_t srcLength = 0; 285 void* srcData = nullptr; 286 napi_value srcBuffer = nullptr; 287 size_t byteOffset = 0; 288 289 napi_get_typedarray_info( 290 env, result, &type, &srcLength, &srcData, &srcBuffer, &byteOffset); 291 292 ASSERT_EQ(srcLength, 6); 293 char* res = reinterpret_cast<char*>(srcData); 294 295 res[srcLength] = 0; 296 ASSERT_STREQ(res, excepted); 297 } 298 299 /** 300 * @tc.name: textEncodeTest002 301 * @tc.desc: Test encode src. 302 * @tc.type: FUNC 303 */ 304 HWTEST_F(NativeEngineTest, textEncodeTest002, testing::ext::TestSize.Level0) 305 { 306 HILOG_INFO("getEncodingTest002 start"); 307 napi_env env = (napi_env)engine_; 308 OHOS::Util::TextEncoder textEncoder("utf-8"); 309 310 std::string input = ""; 311 napi_value src = nullptr; 312 napi_create_string_utf8(env, input.c_str(), input.size(), &src); 313 napi_value result = textEncoder.Encode(env, src); 314 315 napi_typedarray_type type; 316 size_t srcLength = 0; 317 void* srcData = nullptr; 318 napi_value srcBuffer = nullptr; 319 size_t byteOffset = 0; 320 321 napi_get_typedarray_info( 322 env, result, &type, &srcLength, &srcData, &srcBuffer, &byteOffset); 323 324 ASSERT_STREQ((char*)srcData, nullptr); 325 } 326 327 /** 328 * @tc.name: textEncodeTest003 329 * @tc.desc: Test encode src. 330 * @tc.type: FUNC 331 */ 332 HWTEST_F(NativeEngineTest, textEncodeTest003, testing::ext::TestSize.Level0) 333 { 334 HILOG_INFO("getEncodingTest003 start"); 335 napi_env env = (napi_env)engine_; 336 OHOS::Util::TextEncoder textEncoder("utf-8"); 337 338 std::string input = "text"; 339 napi_value src = nullptr; 340 napi_create_string_utf8(env, input.c_str(), input.size(), &src); 341 napi_value result = textEncoder.Encode(env, src); 342 343 char excepted[7] = {0x74, 0x65, 0x78, 0x74, 0}; 344 345 napi_typedarray_type type; 346 size_t srcLength = 0; 347 void* srcData = nullptr; 348 napi_value srcBuffer = nullptr; 349 size_t byteOffset = 0; 350 351 napi_get_typedarray_info( 352 env, result, &type, &srcLength, &srcData, &srcBuffer, &byteOffset); 353 354 ASSERT_EQ(srcLength, 4); 355 char* res = reinterpret_cast<char*>(srcData); 356 357 res[srcLength] = 0; 358 ASSERT_STREQ(res, excepted); 359 } 360 361 /** 362 * @tc.name: textEncodeTest004 363 * @tc.desc: Test encode src. 364 * @tc.type: FUNC 365 */ 366 HWTEST_F(NativeEngineTest, textEncodeTest004, testing::ext::TestSize.Level0) 367 { 368 SetHwIcuDirectory(); 369 napi_env env = (napi_env)engine_; 370 OHOS::Util::TextEncoder textEncoder("gbk"); 371 372 std::string input = "abc123"; 373 napi_value src = nullptr; 374 napi_create_string_utf8(env, input.c_str(), input.size(), &src); 375 napi_value result = textEncoder.Encode(env, src); 376 377 char excepted[7] = {0x61, 0x62, 0x63, 0x31, 0x32, 0x33, 0}; // 7:nums of args 378 379 napi_typedarray_type type; 380 size_t srcLength = 0; 381 void *srcData = nullptr; 382 napi_value srcBuffer = nullptr; 383 size_t byteOffset = 0; 384 385 napi_get_typedarray_info( 386 env, result, &type, &srcLength, &srcData, &srcBuffer, &byteOffset); 387 388 ASSERT_EQ(srcLength, 6); // 6:string length 389 char *res = reinterpret_cast<char*>(srcData); 390 391 res[srcLength] = 0; 392 ASSERT_STREQ(res, excepted); 393 } 394 395 /** 396 * @tc.name: textEncodeTest005 397 * @tc.desc: Test encode src. 398 * @tc.type: FUNC 399 */ 400 HWTEST_F(NativeEngineTest, textEncodeTest005, testing::ext::TestSize.Level0) 401 { 402 napi_env env = (napi_env)engine_; 403 OHOS::Util::TextEncoder textEncoder("utf-8"); 404 napi_value src = nullptr; 405 napi_value result = textEncoder.Encode(env, src); 406 ASSERT_TRUE(result == nullptr); 407 } 408 409 /** 410 * @tc.name: textEncodeTest006 411 * @tc.desc: Test encode src. 412 * @tc.type: FUNC 413 */ 414 HWTEST_F(NativeEngineTest, textEncodeTest006, testing::ext::TestSize.Level0) 415 { 416 HILOG_INFO("textEncodeTest006 start"); 417 SetHwIcuDirectory(); 418 napi_env env = (napi_env)engine_; 419 OHOS::Util::TextEncoder textEncoder("big5"); 420 421 std::string input = "abc哈熠"; 422 napi_value src = nullptr; 423 napi_create_string_utf8(env, input.c_str(), input.size(), &src); 424 napi_value result = textEncoder.Encode(env, src); 425 426 char excepted[8] = {0x61, 0x62, 0x63, 0xAB, 0xA2, 0xE6, 0x66, 0}; // 8:nums of args 427 428 napi_typedarray_type type; 429 size_t srcLength = 0; 430 void *srcData = nullptr; 431 napi_value srcBuffer = nullptr; 432 size_t byteOffset = 0; 433 434 napi_get_typedarray_info( 435 env, result, &type, &srcLength, &srcData, &srcBuffer, &byteOffset); 436 437 ASSERT_EQ(srcLength, 7); // 7:string length 438 char *res = reinterpret_cast<char*>(srcData); 439 440 res[srcLength] = 0; 441 ASSERT_STREQ(res, excepted); 442 } 443 444 /** 445 * @tc.name: textEncodeTest007 446 * @tc.desc: Test encode src. 447 * @tc.type: FUNC 448 */ 449 HWTEST_F(NativeEngineTest, textEncodeTest007, testing::ext::TestSize.Level0) 450 { 451 HILOG_INFO("textEncodeTest007 start"); 452 SetHwIcuDirectory(); 453 napi_env env = (napi_env)engine_; 454 OHOS::Util::TextEncoder textEncoder("shift_jis"); 455 456 std::string input = "abc哈熠"; 457 napi_value src = nullptr; 458 napi_create_string_utf8(env, input.c_str(), input.size(), &src); 459 napi_value result = textEncoder.Encode(env, src); 460 461 char excepted[8] = {0x61, 0x62, 0x63, 0x99, 0xFB, 0xFC, 0xFC, 0}; // 8:nums of args 462 463 napi_typedarray_type type; 464 size_t srcLength = 0; 465 void *srcData = nullptr; 466 napi_value srcBuffer = nullptr; 467 size_t byteOffset = 0; 468 469 napi_get_typedarray_info( 470 env, result, &type, &srcLength, &srcData, &srcBuffer, &byteOffset); 471 472 ASSERT_EQ(srcLength, 7); // 7:string length 473 char *res = reinterpret_cast<char*>(srcData); 474 475 res[srcLength] = 0; 476 ASSERT_STREQ(res, excepted); 477 } 478 479 /** 480 * @tc.name: textEncodeTest008 481 * @tc.desc: Test encode src. 482 * @tc.type: FUNC 483 */ 484 HWTEST_F(NativeEngineTest, textEncodeTest008, testing::ext::TestSize.Level0) 485 { 486 HILOG_INFO("textEncodeTest008 start"); 487 SetHwIcuDirectory(); 488 napi_env env = (napi_env)engine_; 489 OHOS::Util::TextEncoder textEncoder("iso-2022-jp"); 490 491 std::string input = "abc哈熠"; 492 napi_value src = nullptr; 493 napi_create_string_utf8(env, input.c_str(), input.size(), &src); 494 napi_value result = textEncoder.Encode(env, src); 495 496 char excepted[13] = {0x61, 0x62, 0x63, 0x1B, 0x24, 0x42, 0x52, 0x7D, 0x1B, 0x28, 0x42, 0x1A, 0}; // 13:nums of args 497 498 napi_typedarray_type type; 499 size_t srcLength = 0; 500 void *srcData = nullptr; 501 napi_value srcBuffer = nullptr; 502 size_t byteOffset = 0; 503 504 napi_get_typedarray_info( 505 env, result, &type, &srcLength, &srcData, &srcBuffer, &byteOffset); 506 507 ASSERT_EQ(srcLength, 12); // 12:string length 508 char *res = reinterpret_cast<char*>(srcData); 509 510 res[srcLength] = 0; 511 ASSERT_STREQ(res, excepted); 512 } 513 514 /** 515 * @tc.name: textEncodeTest009 516 * @tc.desc: Test encode src. 517 * @tc.type: FUNC 518 */ 519 HWTEST_F(NativeEngineTest, textEncodeTest009, testing::ext::TestSize.Level0) 520 { 521 HILOG_INFO("textEncodeTest009 start"); 522 SetHwIcuDirectory(); 523 napi_env env = (napi_env)engine_; 524 OHOS::Util::TextEncoder textEncoder("ibm866"); 525 526 std::string input = "abc哈熠"; 527 napi_value src = nullptr; 528 napi_create_string_utf8(env, input.c_str(), input.size(), &src); 529 napi_value result = textEncoder.Encode(env, src); 530 531 char excepted[6] = {0x61, 0x62, 0x63, 0x7F, 0x7F, 0}; // 6:nums of args 532 533 napi_typedarray_type type; 534 size_t srcLength = 0; 535 void *srcData = nullptr; 536 napi_value srcBuffer = nullptr; 537 size_t byteOffset = 0; 538 539 napi_get_typedarray_info( 540 env, result, &type, &srcLength, &srcData, &srcBuffer, &byteOffset); 541 542 ASSERT_EQ(srcLength, 5); // 5:string length 543 char *res = reinterpret_cast<char*>(srcData); 544 545 res[srcLength] = 0; 546 ASSERT_STREQ(res, excepted); 547 } 548 549 /** 550 * @tc.name: textEncodeTest010 551 * @tc.desc: Test encode src. 552 * @tc.type: FUNC 553 */ 554 HWTEST_F(NativeEngineTest, textEncodeTest010, testing::ext::TestSize.Level0) 555 { 556 HILOG_INFO("textEncodeTest010 start"); 557 SetHwIcuDirectory(); 558 napi_env env = (napi_env)engine_; 559 OHOS::Util::TextEncoder textEncoder("macintosh"); 560 561 std::string input = "abc哈熠"; 562 napi_value src = nullptr; 563 napi_create_string_utf8(env, input.c_str(), input.size(), &src); 564 napi_value result = textEncoder.Encode(env, src); 565 566 char excepted[6] = {0x61, 0x62, 0x63, 0x3F, 0x3F, 0}; // 6:nums of args 567 568 napi_typedarray_type type; 569 size_t srcLength = 0; 570 void *srcData = nullptr; 571 napi_value srcBuffer = nullptr; 572 size_t byteOffset = 0; 573 574 napi_get_typedarray_info( 575 env, result, &type, &srcLength, &srcData, &srcBuffer, &byteOffset); 576 577 ASSERT_EQ(srcLength, 5); // 5:string length 578 char *res = reinterpret_cast<char*>(srcData); 579 580 res[srcLength] = 0; 581 ASSERT_STREQ(res, excepted); 582 } 583 584 /** 585 * @tc.name: textEncodeIntoTest001 586 * @tc.desc: Test returns a dictionary object indicating the progress of the encoding 587 * @tc.type: FUNC 588 */ 589 HWTEST_F(NativeEngineTest, textEncodeIntoTest001, testing::ext::TestSize.Level0) 590 { 591 HILOG_INFO("textEncodeIntoTest001 start"); 592 napi_env env = (napi_env)engine_; 593 OHOS::Util::TextEncoder textEncoder("utf-8"); 594 595 std::string input = "abc123"; 596 napi_value src = nullptr; 597 napi_create_string_utf8(env, input.c_str(), input.size(), &src); 598 599 napi_value arrayBuffer = nullptr; 600 void* arrayBufferPtr = nullptr; 601 size_t arrayBufferSize = 20; 602 napi_create_arraybuffer(env, arrayBufferSize, &arrayBufferPtr, &arrayBuffer); 603 604 napi_value dest = nullptr; 605 napi_create_typedarray(env, napi_int8_array, arrayBufferSize, arrayBuffer, 0, &dest); 606 607 napi_value result = textEncoder.EncodeInto(env, src, dest); 608 609 napi_value read = nullptr; 610 napi_get_named_property(env, result, "read", &read); 611 612 uint32_t resRead = 0; 613 614 napi_get_value_uint32(env, read, &resRead); 615 616 napi_value written = nullptr; 617 napi_get_named_property(env, result, "written", &written); 618 619 uint32_t resWritten = 0; 620 napi_get_value_uint32(env, read, &resWritten); 621 622 ASSERT_EQ(resRead, static_cast<uint32_t>(6)); 623 ASSERT_EQ(resWritten, static_cast<uint32_t>(6)); 624 } 625 626 /** 627 * @tc.name: textEncodeIntoTest002 628 * @tc.desc: Test returns a dictionary object indicating the progress of the encoding 629 * @tc.type: FUNC 630 */ 631 HWTEST_F(NativeEngineTest, textEncodeIntoTest002, testing::ext::TestSize.Level0) 632 { 633 HILOG_INFO("textEncodeIntoTest002 start"); 634 napi_env env = (napi_env)engine_; 635 OHOS::Util::TextEncoder textEncoder("utf-8"); 636 637 std::string input = "text"; 638 napi_value src = nullptr; 639 napi_create_string_utf8(env, input.c_str(), input.size(), &src); 640 641 napi_value arrayBuffer = nullptr; 642 void* arrayBufferPtr = nullptr; 643 size_t arrayBufferSize = 20; 644 napi_create_arraybuffer(env, arrayBufferSize, &arrayBufferPtr, &arrayBuffer); 645 646 napi_value dest = nullptr; 647 napi_create_typedarray(env, napi_int8_array, arrayBufferSize, arrayBuffer, 0, &dest); 648 649 napi_value result = textEncoder.EncodeInto(env, src, dest); 650 651 napi_value read = nullptr; 652 napi_get_named_property(env, result, "read", &read); 653 654 uint32_t resRead = 0; 655 656 napi_get_value_uint32(env, read, &resRead); 657 658 napi_value written = nullptr; 659 napi_get_named_property(env, result, "written", &written); 660 661 uint32_t resWritten = 0; 662 napi_get_value_uint32(env, read, &resWritten); 663 664 ASSERT_EQ(resRead, static_cast<uint32_t>(4)); 665 ASSERT_EQ(resWritten, static_cast<uint32_t>(4)); 666 } 667 668 /** 669 * @tc.name: textEncodeIntoTest003 670 * @tc.desc: Test returns a dictionary object indicating the progress of the encoding 671 * @tc.type: FUNC 672 */ 673 HWTEST_F(NativeEngineTest, textEncodeIntoTest003, testing::ext::TestSize.Level0) 674 { 675 HILOG_INFO("textEncodeIntoTest003 start"); 676 napi_env env = (napi_env)engine_; 677 OHOS::Util::TextEncoder textEncoder("utf-8"); 678 679 std::string input = "12345"; 680 napi_value src = nullptr; 681 napi_create_string_utf8(env, input.c_str(), input.size(), &src); 682 683 napi_value arrayBuffer = nullptr; 684 void* arrayBufferPtr = nullptr; 685 size_t arrayBufferSize = 20; 686 napi_create_arraybuffer(env, arrayBufferSize, &arrayBufferPtr, &arrayBuffer); 687 688 napi_value dest = nullptr; 689 napi_create_typedarray(env, napi_int8_array, arrayBufferSize, arrayBuffer, 0, &dest); 690 691 napi_value result = textEncoder.EncodeInto(env, src, dest); 692 693 napi_value read = nullptr; 694 napi_get_named_property(env, result, "read", &read); 695 696 uint32_t resRead = 0; 697 698 napi_get_value_uint32(env, read, &resRead); 699 700 napi_value written = nullptr; 701 napi_get_named_property(env, result, "written", &written); 702 703 uint32_t resWritten = 0; 704 napi_get_value_uint32(env, read, &resWritten); 705 706 ASSERT_EQ(resRead, static_cast<uint32_t>(5)); 707 ASSERT_EQ(resWritten, static_cast<uint32_t>(5)); 708 } 709 710 /** 711 * @tc.name: textEncodeIntoTest004 712 * @tc.desc: Test returns a dictionary object indicating the progress of the encoding 713 * @tc.type: FUNC 714 */ 715 HWTEST_F(NativeEngineTest, textEncodeIntoTest004, testing::ext::TestSize.Level0) 716 { 717 HILOG_INFO("textEncodeIntoTest004 start"); 718 SetHwIcuDirectory(); 719 napi_env env = (napi_env)engine_; 720 OHOS::Util::TextEncoder textEncoder("big5"); 721 722 std::string input = "abc123"; 723 napi_value src = nullptr; 724 napi_create_string_utf8(env, input.c_str(), input.size(), &src); 725 726 napi_value arrayBuffer = nullptr; 727 void* arrayBufferPtr = nullptr; 728 size_t arrayBufferSize = 20; 729 napi_create_arraybuffer(env, arrayBufferSize, &arrayBufferPtr, &arrayBuffer); 730 731 napi_value dest = nullptr; 732 napi_create_typedarray(env, napi_uint8_array, arrayBufferSize, arrayBuffer, 0, &dest); 733 734 napi_value result = textEncoder.EncodeInto(env, src, dest); 735 736 napi_value read = nullptr; 737 napi_get_named_property(env, result, "read", &read); 738 739 uint32_t resRead = 0; 740 741 napi_get_value_uint32(env, read, &resRead); 742 743 napi_value written = nullptr; 744 napi_get_named_property(env, result, "written", &written); 745 746 uint32_t resWritten = 0; 747 napi_get_value_uint32(env, read, &resWritten); 748 749 ASSERT_EQ(resRead, static_cast<uint32_t>(6)); 750 ASSERT_EQ(resWritten, static_cast<uint32_t>(6)); 751 } 752 753 /** 754 * @tc.name: textEncodeIntoTest005 755 * @tc.desc: Test returns a dictionary object indicating the progress of the encoding 756 * @tc.type: FUNC 757 */ 758 HWTEST_F(NativeEngineTest, textEncodeIntoTest005, testing::ext::TestSize.Level0) 759 { 760 HILOG_INFO("textEncodeIntoTest005 start"); 761 SetHwIcuDirectory(); 762 napi_env env = (napi_env)engine_; 763 OHOS::Util::TextEncoder textEncoder("shift_jis"); 764 765 std::string input = "abc123哈"; 766 napi_value src = nullptr; 767 napi_create_string_utf8(env, input.c_str(), input.size(), &src); 768 769 napi_value arrayBuffer = nullptr; 770 void* arrayBufferPtr = nullptr; 771 size_t arrayBufferSize = 20; 772 napi_create_arraybuffer(env, arrayBufferSize, &arrayBufferPtr, &arrayBuffer); 773 774 napi_value dest = nullptr; 775 napi_create_typedarray(env, napi_uint8_array, arrayBufferSize, arrayBuffer, 0, &dest); 776 777 napi_value result = textEncoder.EncodeInto(env, src, dest); 778 779 napi_value read = nullptr; 780 napi_get_named_property(env, result, "read", &read); 781 782 uint32_t resRead = 0; 783 784 napi_get_value_uint32(env, read, &resRead); 785 786 napi_value written = nullptr; 787 napi_get_named_property(env, result, "written", &written); 788 789 uint32_t resWritten = 0; 790 napi_get_value_uint32(env, read, &resWritten); 791 792 ASSERT_EQ(resRead, static_cast<uint32_t>(7)); 793 ASSERT_EQ(resWritten, static_cast<uint32_t>(7)); 794 } 795 796 /** 797 * @tc.name: textEncodeIntoTest006 798 * @tc.desc: Test returns a dictionary object indicating the progress of the encoding 799 * @tc.type: FUNC 800 */ 801 HWTEST_F(NativeEngineTest, textEncodeIntoTest006, testing::ext::TestSize.Level0) 802 { 803 HILOG_INFO("textEncodeIntoTest006 start"); 804 SetHwIcuDirectory(); 805 napi_env env = (napi_env)engine_; 806 OHOS::Util::TextEncoder textEncoder("iso-2022-jp"); 807 808 std::string input = "abc123哈"; 809 napi_value src = nullptr; 810 napi_create_string_utf8(env, input.c_str(), input.size(), &src); 811 812 napi_value arrayBuffer = nullptr; 813 void* arrayBufferPtr = nullptr; 814 size_t arrayBufferSize = 20; 815 napi_create_arraybuffer(env, arrayBufferSize, &arrayBufferPtr, &arrayBuffer); 816 817 napi_value dest = nullptr; 818 napi_create_typedarray(env, napi_uint8_array, arrayBufferSize, arrayBuffer, 0, &dest); 819 820 napi_value result = textEncoder.EncodeInto(env, src, dest); 821 822 napi_value read = nullptr; 823 napi_get_named_property(env, result, "read", &read); 824 825 uint32_t resRead = 0; 826 827 napi_get_value_uint32(env, read, &resRead); 828 829 napi_value written = nullptr; 830 napi_get_named_property(env, result, "written", &written); 831 832 uint32_t resWritten = 0; 833 napi_get_value_uint32(env, read, &resWritten); 834 835 ASSERT_EQ(resRead, static_cast<uint32_t>(7)); 836 ASSERT_EQ(resWritten, static_cast<uint32_t>(7)); 837 } 838 839 /** 840 * @tc.name: textEncodeIntoTest007 841 * @tc.desc: Test returns a dictionary object indicating the progress of the encoding 842 * @tc.type: FUNC 843 */ 844 HWTEST_F(NativeEngineTest, textEncodeIntoTest007, testing::ext::TestSize.Level0) 845 { 846 HILOG_INFO("textEncodeIntoTest007 start"); 847 SetHwIcuDirectory(); 848 napi_env env = (napi_env)engine_; 849 OHOS::Util::TextEncoder textEncoder("ibm866"); 850 851 std::string input = "abc123哈"; 852 napi_value src = nullptr; 853 napi_create_string_utf8(env, input.c_str(), input.size(), &src); 854 855 napi_value arrayBuffer = nullptr; 856 void* arrayBufferPtr = nullptr; 857 size_t arrayBufferSize = 20; 858 napi_create_arraybuffer(env, arrayBufferSize, &arrayBufferPtr, &arrayBuffer); 859 860 napi_value dest = nullptr; 861 napi_create_typedarray(env, napi_uint8_array, arrayBufferSize, arrayBuffer, 0, &dest); 862 863 napi_value result = textEncoder.EncodeInto(env, src, dest); 864 865 napi_value read = nullptr; 866 napi_get_named_property(env, result, "read", &read); 867 868 uint32_t resRead = 0; 869 870 napi_get_value_uint32(env, read, &resRead); 871 872 napi_value written = nullptr; 873 napi_get_named_property(env, result, "written", &written); 874 875 uint32_t resWritten = 0; 876 napi_get_value_uint32(env, read, &resWritten); 877 878 ASSERT_EQ(resRead, static_cast<uint32_t>(7)); 879 ASSERT_EQ(resWritten, static_cast<uint32_t>(7)); 880 } 881 882 /** 883 * @tc.name: GetEncoding001 884 * @tc.desc: Test date type. 885 * @tc.type: FUNC 886 */ 887 HWTEST_F(NativeEngineTest, GetEncoding001, testing::ext::TestSize.Level0) 888 { 889 HILOG_INFO("TextDecoder::getEncodingTest001 start"); 890 napi_env env = (napi_env)engine_; 891 std::vector<int> inputVec; 892 int fatal = -1; 893 int ignoreBOM = -1; 894 inputVec.push_back(fatal); 895 inputVec.push_back(ignoreBOM); 896 std::string str = "utf-8"; 897 OHOS::Util::TextDecoder textDecoder(str, inputVec); 898 napi_value testString = textDecoder.GetEncoding(env); 899 size_t bufferSize = 0; 900 napi_get_value_string_utf8(env, testString, nullptr, 0, &bufferSize); 901 std::string tmpTestStr = "utf-8"; 902 size_t strLength = 0; 903 char* buffer = nullptr; 904 if (bufferSize > 0) { 905 buffer = new char[bufferSize + 1](); 906 napi_get_value_string_utf8(env, testString, buffer, bufferSize + 1, &strLength); 907 } 908 const char *result = tmpTestStr.c_str(); 909 size_t resultLength = tmpTestStr.length(); 910 ASSERT_STREQ(result, buffer); 911 ASSERT_EQ(resultLength, strLength); 912 if (buffer != nullptr) { 913 delete []buffer; 914 buffer = nullptr; 915 } 916 } 917 918 /** 919 * @tc.name: GetEncoding002 920 * @tc.desc: Test date type. 921 * @tc.type: FUNC 922 */ 923 HWTEST_F(NativeEngineTest, GetEncoding002, testing::ext::TestSize.Level0) 924 { 925 HILOG_INFO("TextDecoder::getEncodingTest002 start"); 926 napi_env env = (napi_env)engine_; 927 std::vector<int> inputVec; 928 int fatal = -1; 929 int ignoreBOM = -1; 930 inputVec.push_back(fatal); 931 inputVec.push_back(ignoreBOM); 932 std::string str = "GB18030"; 933 OHOS::Util::TextDecoder textDecoder(str, inputVec); 934 napi_value testString = textDecoder.GetEncoding(env); 935 size_t bufferSize = 0; 936 napi_get_value_string_utf8(env, testString, nullptr, 0, &bufferSize); 937 std::string tmpTestStr = "GB18030"; 938 size_t strLength = 0; 939 char* buffer = nullptr; 940 if (bufferSize > 0) { 941 buffer = new char[bufferSize + 1](); 942 napi_get_value_string_utf8(env, testString, buffer, bufferSize + 1, &strLength); 943 } 944 const char *result = tmpTestStr.c_str(); 945 size_t resultLength = tmpTestStr.length(); 946 ASSERT_STREQ(result, buffer); 947 ASSERT_EQ(resultLength, strLength); 948 if (buffer != nullptr) { 949 delete []buffer; 950 buffer = nullptr; 951 } 952 } 953 954 /** 955 * @tc.name: GetEncoding003 956 * @tc.desc: Test date type. 957 * @tc.type: FUNC 958 */ 959 HWTEST_F(NativeEngineTest, GetEncoding003, testing::ext::TestSize.Level0) 960 { 961 HILOG_INFO("TextDecoder::getEncodingTest003 start"); 962 napi_env env = (napi_env)engine_; 963 std::vector<int> inputVec; 964 int fatal = -1; 965 int ignoreBOM = -1; 966 inputVec.push_back(fatal); 967 inputVec.push_back(ignoreBOM); 968 std::string str = "gb18030"; 969 OHOS::Util::TextDecoder textDecoder(str, inputVec); 970 napi_value testString = textDecoder.GetEncoding(env); 971 size_t bufferSize = 0; 972 napi_get_value_string_utf8(env, testString, nullptr, 0, &bufferSize); 973 std::string tmpTestStr = "gb18030"; 974 size_t strLength = 0; 975 char* buffer = nullptr; 976 if (bufferSize > 0) { 977 buffer = new char[bufferSize + 1](); 978 napi_get_value_string_utf8(env, testString, buffer, bufferSize + 1, &strLength); 979 } 980 const char *result = tmpTestStr.c_str(); 981 size_t resultLength = tmpTestStr.length(); 982 ASSERT_STREQ(result, buffer); 983 ASSERT_EQ(resultLength, strLength); 984 if (buffer != nullptr) { 985 delete []buffer; 986 buffer = nullptr; 987 } 988 } 989 990 /** 991 * @tc.name: GetEncoding004 992 * @tc.desc: Test date type. 993 * @tc.type: FUNC 994 */ 995 HWTEST_F(NativeEngineTest, GetEncoding004, testing::ext::TestSize.Level0) 996 { 997 napi_env env = (napi_env)engine_; 998 std::vector<int> inputVec; 999 int fatal = 0; 1000 int ignoreBOM = -1; 1001 inputVec.push_back(fatal); 1002 inputVec.push_back(ignoreBOM); 1003 std::string str = "utf-8"; 1004 OHOS::Util::TextDecoder textDecoder(str, inputVec); 1005 napi_value testString = textDecoder.GetEncoding(env); 1006 size_t bufferSize = 0; 1007 napi_get_value_string_utf8(env, testString, nullptr, 0, &bufferSize); 1008 std::string tmpTestStr = "utf-8"; 1009 size_t strLength = 0; 1010 char *buffer = nullptr; 1011 if (bufferSize > 0) { 1012 buffer = new char[bufferSize + 1](); 1013 napi_get_value_string_utf8(env, testString, buffer, bufferSize + 1, &strLength); 1014 } 1015 const char *result = tmpTestStr.c_str(); 1016 size_t resultLength = tmpTestStr.length(); 1017 ASSERT_STREQ(result, buffer); 1018 ASSERT_EQ(resultLength, strLength); 1019 if (buffer != nullptr) { 1020 delete []buffer; 1021 buffer = nullptr; 1022 } 1023 } 1024 1025 /** 1026 * @tc.name: GetFatal001 1027 * @tc.desc: Test date type. 1028 * @tc.type: FUNC 1029 */ 1030 HWTEST_F(NativeEngineTest, GetFatal001, testing::ext::TestSize.Level0) 1031 { 1032 HILOG_INFO("TextDecoder::GetFatal001 start"); 1033 napi_env env = (napi_env)engine_; 1034 std::vector<int> inputVec; 1035 int fatal = 1; 1036 int ignoreBOM = 0; 1037 inputVec.push_back(fatal); 1038 inputVec.push_back(ignoreBOM); 1039 std::string str = "utf-8"; 1040 OHOS::Util::TextDecoder textDecoder(str, inputVec); 1041 napi_value naVal = textDecoder.GetFatal(env); 1042 bool result = false; 1043 napi_get_value_bool(env, naVal, &result); 1044 ASSERT_TRUE(result); 1045 } 1046 1047 /** 1048 * @tc.name: GetFatal002 1049 * @tc.desc: Test date type. 1050 * @tc.type: FUNC 1051 */ 1052 HWTEST_F(NativeEngineTest, GetFatal002, testing::ext::TestSize.Level0) 1053 { 1054 HILOG_INFO("TextDecoder::GetFatal002 start"); 1055 napi_env env = (napi_env)engine_; 1056 std::vector<int> inputVec; 1057 int fatal = -1; 1058 int ignoreBOM = 1; 1059 inputVec.push_back(fatal); 1060 inputVec.push_back(ignoreBOM); 1061 std::string str = "utf-8"; 1062 OHOS::Util::TextDecoder textDecoder(str, inputVec); 1063 napi_value naVal = textDecoder.GetFatal(env); 1064 bool result = false; 1065 napi_get_value_bool(env, naVal, &result); 1066 ASSERT_TRUE(result); 1067 } 1068 1069 /** 1070 * @tc.name: GetFatal003 1071 * @tc.desc: Test date type. 1072 * @tc.type: FUNC 1073 */ 1074 HWTEST_F(NativeEngineTest, GetFatal003, testing::ext::TestSize.Level0) 1075 { 1076 HILOG_INFO("TextDecoder::GetFatal003 start"); 1077 napi_env env = (napi_env)engine_; 1078 std::vector<int> inputVec; 1079 int fatal = 0; 1080 int ignoreBOM = 1; 1081 inputVec.push_back(fatal); 1082 inputVec.push_back(ignoreBOM); 1083 std::string str = "utf-8"; 1084 OHOS::Util::TextDecoder textDecoder(str, inputVec); 1085 napi_value naVal = textDecoder.GetFatal(env); 1086 bool result = false; 1087 napi_get_value_bool(env, naVal, &result); 1088 ASSERT_FALSE(result); 1089 } 1090 1091 /** 1092 * @tc.name: GetIgnoreBOM001 1093 * @tc.desc: Test date type. 1094 * @tc.type: FUNC 1095 */ 1096 HWTEST_F(NativeEngineTest, GetIgnoreBOM001, testing::ext::TestSize.Level0) 1097 { 1098 HILOG_INFO("TextDecoder::GetIgnoreBOM001 start"); 1099 napi_env env = (napi_env)engine_; 1100 std::vector<int> inputVec; 1101 int fatal = -1; 1102 int ignoreBOM = 1; 1103 inputVec.push_back(fatal); 1104 inputVec.push_back(ignoreBOM); 1105 std::string str = "utf-8"; 1106 OHOS::Util::TextDecoder textDecoder(str, inputVec); 1107 napi_value naVal = textDecoder.GetIgnoreBOM(env); 1108 bool result = false; 1109 napi_get_value_bool(env, naVal, &result); 1110 ASSERT_TRUE(result); 1111 } 1112 1113 /** 1114 * @tc.name: GetIgnoreBOM002 1115 * @tc.desc: Test date type. 1116 * @tc.type: FUNC 1117 */ 1118 HWTEST_F(NativeEngineTest, GetIgnoreBOM002, testing::ext::TestSize.Level0) 1119 { 1120 HILOG_INFO("TextDecoder::GetIgnoreBOM002 start"); 1121 napi_env env = (napi_env)engine_; 1122 std::vector<int> inputVec; 1123 int fatal = 0; 1124 int ignoreBOM = 1; 1125 inputVec.push_back(fatal); 1126 inputVec.push_back(ignoreBOM); 1127 std::string str = "utf-8"; 1128 OHOS::Util::TextDecoder textDecoder(str, inputVec); 1129 napi_value naVal = textDecoder.GetIgnoreBOM(env); 1130 bool result = false; 1131 napi_get_value_bool(env, naVal, &result); 1132 ASSERT_TRUE(result); 1133 } 1134 1135 /** 1136 * @tc.name: GetIgnoreBOM003 1137 * @tc.desc: Test date type. 1138 * @tc.type: FUNC 1139 */ 1140 HWTEST_F(NativeEngineTest, GetIgnoreBOM003, testing::ext::TestSize.Level0) 1141 { 1142 HILOG_INFO("TextDecoder::GetIgnoreBOM003 start"); 1143 napi_env env = (napi_env)engine_; 1144 std::vector<int> inputVec; 1145 int fatal = 1; 1146 int ignoreBOM = 1; 1147 inputVec.push_back(fatal); 1148 inputVec.push_back(ignoreBOM); 1149 std::string str = "utf-8"; 1150 OHOS::Util::TextDecoder textDecoder(str, inputVec); 1151 napi_value naVal = textDecoder.GetIgnoreBOM(env); 1152 bool result = false; 1153 napi_get_value_bool(env, naVal, &result); 1154 ASSERT_TRUE(result); 1155 } 1156 1157 /** 1158 * @tc.name: GetIgnoreBOM004 1159 * @tc.desc: Test date type. 1160 * @tc.type: FUNC 1161 */ 1162 HWTEST_F(NativeEngineTest, GetIgnoreBOM004, testing::ext::TestSize.Level0) 1163 { 1164 napi_env env = (napi_env)engine_; 1165 std::vector<int> inputVec; 1166 int fatal = 0; 1167 int ignoreBOM = -1; 1168 inputVec.push_back(fatal); 1169 inputVec.push_back(ignoreBOM); 1170 std::string str = "ssn"; 1171 OHOS::Util::TextDecoder textDecoder(str, inputVec); 1172 napi_value naVal = textDecoder.GetIgnoreBOM(env); 1173 bool result = false; 1174 napi_get_value_bool(env, naVal, &result); 1175 ASSERT_TRUE(result); 1176 } 1177 1178 /** 1179 * @tc.name: GetIgnoreBOM005 1180 * @tc.desc: Test date type. 1181 * @tc.type: FUNC 1182 */ 1183 HWTEST_F(NativeEngineTest, GetIgnoreBOM005, testing::ext::TestSize.Level0) 1184 { 1185 napi_env env = (napi_env)engine_; 1186 std::vector<int> inputVec; 1187 int fatal = -1; 1188 int ignoreBOM = 0; 1189 inputVec.push_back(fatal); 1190 inputVec.push_back(ignoreBOM); 1191 std::string str = "ssn"; 1192 OHOS::Util::TextDecoder textDecoder(str, inputVec); 1193 napi_value naVal = textDecoder.GetIgnoreBOM(env); 1194 bool result = false; 1195 napi_get_value_bool(env, naVal, &result); 1196 ASSERT_FALSE(result); 1197 } 1198 1199 /** 1200 * @tc.name: decoderUtf8001 utf-8 1201 * @tc.desc: Test date type. 1202 * @tc.type: FUNC 1203 */ 1204 HWTEST_F(NativeEngineTest, decoderUtf8001, testing::ext::TestSize.Level0) 1205 { 1206 HILOG_INFO("decoderUtf8001 start"); 1207 napi_env env = (napi_env)engine_; 1208 std::vector<int> inputVec; 1209 int fatal = -1; 1210 int ignoreBOM = -1; 1211 inputVec.push_back(fatal); 1212 inputVec.push_back(ignoreBOM); 1213 std::string str = "utf-8"; 1214 OHOS::Util::TextDecoder textDecoder(str, inputVec); 1215 bool iflag = false; 1216 size_t byteLength = 3; 1217 void* data = nullptr; 1218 napi_value resultBuff = nullptr; 1219 napi_create_arraybuffer(env, byteLength, &data, &resultBuff); 1220 unsigned char arr[3] = {0x61, 0x62, 0x63}; 1221 int ret = memcpy_s(data, sizeof(arr), reinterpret_cast<void*>(arr), sizeof(arr)); 1222 ASSERT_EQ(0, ret); 1223 napi_value result2 = nullptr; 1224 napi_create_typedarray(env, napi_int8_array, byteLength, resultBuff, 0, &result2); 1225 napi_value testString = textDecoder.Decode(env, result2, iflag); 1226 size_t bufferSize = 0; 1227 napi_get_value_string_utf8(env, testString, nullptr, 0, &bufferSize); 1228 size_t length = 0; 1229 char* ch = nullptr; 1230 if (bufferSize > 0) { 1231 ch = new char[bufferSize + 1](); 1232 napi_get_value_string_utf8(env, testString, ch, bufferSize + 1, &length); 1233 } 1234 ASSERT_STREQ("abc", ch); 1235 if (ch != nullptr) { 1236 delete []ch; 1237 ch = nullptr; 1238 } 1239 } 1240 1241 /** 1242 * @tc.name: decoderUtf8002 utf-8 1243 * @tc.desc: Test date type. 1244 * @tc.type: FUNC 1245 */ 1246 HWTEST_F(NativeEngineTest, decoderUtf8002, testing::ext::TestSize.Level0) 1247 { 1248 HILOG_INFO("decoderUtf8002 start"); 1249 napi_env env = (napi_env)engine_; 1250 std::vector<int> inputVec; 1251 int fatal = -1; 1252 int ignoreBOM = 0; 1253 inputVec.push_back(fatal); 1254 inputVec.push_back(ignoreBOM); 1255 std::string str = "utf-8"; 1256 OHOS::Util::TextDecoder textDecoder(str, inputVec); 1257 bool iflag = true; 1258 size_t byteLength = 5; 1259 void* data = nullptr; 1260 napi_value resultBuff = nullptr; 1261 napi_create_arraybuffer(env, byteLength, &data, &resultBuff); 1262 unsigned char arr[5] = {0x61, '\0', 0x62, 0x63, '\0'}; 1263 int ret = memcpy_s(data, sizeof(arr), reinterpret_cast<void*>(arr), sizeof(arr)); 1264 ASSERT_EQ(0, ret); 1265 napi_value result2 = nullptr; 1266 napi_create_typedarray(env, napi_int8_array, byteLength, resultBuff, 0, &result2); 1267 napi_value testString = textDecoder.Decode(env, result2, iflag); 1268 size_t bufferSize = 0; 1269 size_t length = 0; 1270 napi_get_value_string_utf8(env, testString, nullptr, 0, &bufferSize); 1271 char* ch = nullptr; 1272 if (bufferSize > 0) { 1273 ch = new char[bufferSize + 1](); 1274 napi_get_value_string_utf8(env, testString, ch, bufferSize + 1, &length); 1275 } 1276 ASSERT_STREQ("a bc ", ch); 1277 if (ch != nullptr) { 1278 delete []ch; 1279 ch = nullptr; 1280 } 1281 } 1282 1283 /** 1284 * @tc.name: decoderUtf8003 utf-8 1285 * @tc.desc: Test date type. 1286 * @tc.type: FUNC 1287 */ 1288 HWTEST_F(NativeEngineTest, decoderUtf8003, testing::ext::TestSize.Level0) 1289 { 1290 napi_env env = (napi_env)engine_; 1291 std::vector<int> inputVec; 1292 int fatal = -1; 1293 int ignoreBOM = -1; 1294 inputVec.push_back(fatal); 1295 inputVec.push_back(ignoreBOM); 1296 std::string str = "utf-8"; 1297 OHOS::Util::TextDecoder textDecoder(str, inputVec); 1298 bool iflag = false; 1299 size_t byteLength = 0; 1300 void *data = nullptr; 1301 napi_value resultBuff = nullptr; 1302 napi_create_arraybuffer(env, byteLength, &data, &resultBuff); 1303 napi_value result2 = nullptr; 1304 napi_create_typedarray(env, napi_int8_array, byteLength, resultBuff, 0, &result2); 1305 napi_value testString = textDecoder.Decode(env, result2, iflag); 1306 ASSERT_TRUE(testString == nullptr); 1307 } 1308 1309 /** 1310 * @tc.name: decoderUtf16le001 utf-16le 1311 * @tc.desc: Test date type. 1312 * @tc.type: FUNC 1313 */ 1314 HWTEST_F(NativeEngineTest, decoderUtf16le001, testing::ext::TestSize.Level0) 1315 { 1316 HILOG_INFO("decoderUtf16le001 start"); 1317 napi_env env = (napi_env)engine_; 1318 std::vector<int> inputVec; 1319 int fatal = 0; 1320 int ignoreBOM = 0; 1321 inputVec.push_back(fatal); 1322 inputVec.push_back(ignoreBOM); 1323 std::string str = "utf-16le"; 1324 OHOS::Util::TextDecoder textDecoder(str, inputVec); 1325 bool iflag = false; 1326 size_t byteLength = 6; 1327 void* data = nullptr; 1328 napi_value resultBuff = nullptr; 1329 napi_create_arraybuffer(env, byteLength, &data, &resultBuff); 1330 unsigned char arr[6] = {0x61, 0x00, 0x62, 0x00, 0x63, 0x00}; 1331 int ret = memcpy_s(data, sizeof(arr), reinterpret_cast<void*>(arr), sizeof(arr)); 1332 ASSERT_EQ(0, ret); 1333 napi_value result2 = nullptr; 1334 napi_create_typedarray(env, napi_int8_array, byteLength, resultBuff, 0, &result2); 1335 napi_value testString = textDecoder.Decode(env, result2, iflag); 1336 size_t bufferSize = 0; 1337 size_t length = 0; 1338 napi_get_value_string_utf8(env, testString, nullptr, 0, &bufferSize); 1339 char* ch = nullptr; 1340 if (bufferSize > 0) { 1341 ch = new char[bufferSize + 1](); 1342 napi_get_value_string_utf8(env, testString, ch, bufferSize + 1, &length); 1343 } 1344 ASSERT_STREQ("abc", ch); 1345 if (ch != nullptr) { 1346 delete []ch; 1347 ch = nullptr; 1348 } 1349 } 1350 1351 /** 1352 * @tc.name: decoderUtf16le002 utf-16le 1353 * @tc.desc: Test date type. 1354 * @tc.type: FUNC 1355 */ 1356 HWTEST_F(NativeEngineTest, decoderUtf16le002, testing::ext::TestSize.Level0) 1357 { 1358 HILOG_INFO("decoderUtf16le002 start"); 1359 napi_env env = (napi_env)engine_; 1360 std::vector<int> inputVec; 1361 int fatal = 0; 1362 int ignoreBOM = 1; 1363 inputVec.push_back(fatal); 1364 inputVec.push_back(ignoreBOM); 1365 std::string str = "utf-16le"; 1366 OHOS::Util::TextDecoder textDecoder(str, inputVec); 1367 bool iflag = true; 1368 size_t byteLength = 6; 1369 void* data = nullptr; 1370 napi_value resultBuff = nullptr; 1371 napi_create_arraybuffer(env, byteLength, &data, &resultBuff); 1372 unsigned char arr[6] = {0x61, 0x00, 0x62, 0x00, 0x63, 0x00}; 1373 int ret = memcpy_s(data, sizeof(arr), reinterpret_cast<void*>(arr), sizeof(arr)); 1374 ASSERT_EQ(0, ret); 1375 napi_value result2 = nullptr; 1376 napi_create_typedarray(env, napi_int8_array, byteLength, resultBuff, 0, &result2); 1377 napi_value testString = textDecoder.Decode(env, result2, iflag); 1378 size_t bufferSize = 0; 1379 napi_get_value_string_utf8(env, testString, nullptr, 0, &bufferSize); 1380 char* ch = nullptr; 1381 size_t length = 0; 1382 if (bufferSize > 0) { 1383 ch = new char[bufferSize + 1](); 1384 napi_get_value_string_utf8(env, testString, ch, bufferSize + 1, &length); 1385 } 1386 ASSERT_STREQ("abc", ch); 1387 if (ch != nullptr) { 1388 delete []ch; 1389 ch = nullptr; 1390 } 1391 } 1392 1393 /** 1394 * @tc.name: decoderUtf16le003 utf-16le 1395 * @tc.desc: Test date type. 1396 * @tc.type: FUNC 1397 */ 1398 HWTEST_F(NativeEngineTest, decoderUtf16le003, testing::ext::TestSize.Level0) 1399 { 1400 HILOG_INFO("decoderUtf16le003 start"); 1401 napi_env env = (napi_env)engine_; 1402 std::vector<int> inputVec; 1403 int fatal = 0; 1404 int ignoreBOM = 0; 1405 inputVec.push_back(fatal); 1406 inputVec.push_back(ignoreBOM); 1407 std::string str = "utf-16le"; 1408 OHOS::Util::TextDecoder textDecoder(str, inputVec); 1409 bool iflag = true; 1410 size_t byteLength = 8; 1411 void* data = nullptr; 1412 napi_value resultBuff = nullptr; 1413 napi_create_arraybuffer(env, byteLength, &data, &resultBuff); 1414 unsigned char arr[8] = {0xFF, 0xFE, 0x61, 0x00, 0x62, 0x00, 0x63, 0x00}; 1415 int ret = memcpy_s(data, sizeof(arr), reinterpret_cast<void*>(arr), sizeof(arr)); 1416 ASSERT_EQ(0, ret); 1417 napi_value result2 = nullptr; 1418 napi_create_typedarray(env, napi_int8_array, byteLength, resultBuff, 0, &result2); 1419 napi_value testString = textDecoder.Decode(env, result2, iflag); 1420 size_t bufferSize = 0; 1421 napi_get_value_string_utf8(env, testString, nullptr, 0, &bufferSize); 1422 char* ch = nullptr; 1423 size_t length = 0; 1424 std::string tempStr01 = ""; 1425 if (bufferSize > 0) { 1426 ch = new char[bufferSize + 1](); 1427 napi_get_value_string_utf8(env, testString, ch, bufferSize + 1, &length); 1428 tempStr01 = ch; 1429 } 1430 std::u16string tempU16str02 = 1431 std::wstring_convert<std::codecvt_utf8_utf16<char16_t>, char16_t> {}.from_bytes(tempStr01); 1432 ASSERT_EQ(0xFEFF, static_cast<int>(tempU16str02[0])); 1433 ASSERT_EQ(0x61, static_cast<int>(tempU16str02[1])); 1434 ASSERT_EQ(0x62, static_cast<int>(tempU16str02[2])); 1435 ASSERT_EQ(0x63, static_cast<int>(tempU16str02[3])); 1436 if (ch != nullptr) { 1437 delete []ch; 1438 ch = nullptr; 1439 } 1440 } 1441 1442 /** 1443 * @tc.name: decoderUtf16le004 utf-16le 1444 * @tc.desc: Test date type. 1445 * @tc.type: FUNC 1446 */ 1447 HWTEST_F(NativeEngineTest, decoderUtf16le004, testing::ext::TestSize.Level0) 1448 { 1449 HILOG_INFO("decoderUtf16le004 start"); 1450 napi_env env = (napi_env)engine_; 1451 std::vector<int> inputVec; 1452 int fatal = -1; 1453 int ignoreBOM = -1; 1454 inputVec.push_back(fatal); 1455 inputVec.push_back(ignoreBOM); 1456 std::string str = "utf-16le"; 1457 OHOS::Util::TextDecoder textDecoder(str, inputVec); 1458 bool iflag = false; 1459 size_t byteLength = 8; 1460 void* data = nullptr; 1461 napi_value resultBuff = nullptr; 1462 napi_create_arraybuffer(env, byteLength, &data, &resultBuff); 1463 unsigned char arr[8] = {0xFF, 0xFE, 0x61, 0x00, 0x62, 0x00, 0x63, 0x00}; 1464 int ret = memcpy_s(data, sizeof(arr), reinterpret_cast<void*>(arr), sizeof(arr)); 1465 ASSERT_EQ(0, ret); 1466 napi_value result2 = nullptr; 1467 napi_create_typedarray(env, napi_int8_array, byteLength, resultBuff, 0, &result2); 1468 napi_value testString = textDecoder.Decode(env, result2, iflag); 1469 size_t bufferSize = 0; 1470 napi_get_value_string_utf8(env, testString, nullptr, 0, &bufferSize); 1471 char* ch = nullptr; 1472 size_t length = 0; 1473 std::string tempStr01 = ""; 1474 if (bufferSize > 0) { 1475 ch = new char[bufferSize + 1](); 1476 napi_get_value_string_utf8(env, testString, ch, bufferSize + 1, &length); 1477 tempStr01 = ch; 1478 } 1479 std::u16string tempU16str02 = 1480 std::wstring_convert<std::codecvt_utf8_utf16<char16_t>, char16_t> {}.from_bytes(tempStr01); 1481 ASSERT_EQ(0xFEFF, static_cast<int>(tempU16str02[0])); 1482 ASSERT_EQ(0x61, static_cast<int>(tempU16str02[1])); 1483 ASSERT_EQ(0x62, static_cast<int>(tempU16str02[2])); 1484 ASSERT_EQ(0x63, static_cast<int>(tempU16str02[3])); 1485 if (ch != nullptr) { 1486 delete []ch; 1487 ch = nullptr; 1488 } 1489 } 1490 1491 /** 1492 * @tc.name: decoderUtf16be001 utf-16be 1493 * @tc.desc: Test date type. 1494 * @tc.type: FUNC 1495 */ 1496 HWTEST_F(NativeEngineTest, decoderUtf16be001, testing::ext::TestSize.Level0) 1497 { 1498 HILOG_INFO("decoderUtf16be001 start"); 1499 napi_env env = (napi_env)engine_; 1500 std::vector<int> inputVec; 1501 int fatal = 0; 1502 int ignoreBOM = 0; 1503 inputVec.push_back(fatal); 1504 inputVec.push_back(ignoreBOM); 1505 std::string str = "utf-16be"; 1506 OHOS::Util::TextDecoder textDecoder(str, inputVec); 1507 bool iflag = false; 1508 size_t byteLength = 6; 1509 void* data = nullptr; 1510 napi_value resultBuff = nullptr; 1511 napi_create_arraybuffer(env, byteLength, &data, &resultBuff); 1512 unsigned char arr[6] = {0x00, 0x61, 0x00, 0x62, 0x00, 0x63}; 1513 int ret = memcpy_s(data, sizeof(arr), reinterpret_cast<void*>(arr), sizeof(arr)); 1514 ASSERT_EQ(0, ret); 1515 napi_value result2 = nullptr; 1516 napi_create_typedarray(env, napi_int8_array, byteLength, resultBuff, 0, &result2); 1517 napi_value testString = textDecoder.Decode(env, result2, iflag); 1518 size_t bufferSize = 0; 1519 napi_get_value_string_utf8(env, testString, nullptr, 0, &bufferSize); 1520 size_t length = 0; 1521 char* ch = nullptr; 1522 if (bufferSize > 0) { 1523 ch = new char[bufferSize + 1](); 1524 napi_get_value_string_utf8(env, testString, ch, bufferSize + 1, &length); 1525 } 1526 ASSERT_STREQ("abc", ch); 1527 if (ch != nullptr) { 1528 delete []ch; 1529 ch = nullptr; 1530 } 1531 } 1532 1533 /** 1534 * @tc.name: decoderUtf16be002 utf-16be 1535 * @tc.desc: Test date type. 1536 * @tc.type: FUNC 1537 */ 1538 HWTEST_F(NativeEngineTest, decoderUtf16be002, testing::ext::TestSize.Level0) 1539 { 1540 HILOG_INFO("decoderUtf16be002 start"); 1541 napi_env env = (napi_env)engine_; 1542 std::vector<int> inputVec; 1543 int fatal = 0; 1544 int ignoreBOM = 0; 1545 inputVec.push_back(fatal); 1546 inputVec.push_back(ignoreBOM); 1547 std::string str = "utf-16be"; 1548 OHOS::Util::TextDecoder textDecoder(str, inputVec); 1549 bool iflag = false; 1550 size_t byteLength = 8; 1551 void* data = nullptr; 1552 napi_value resultBuff = nullptr; 1553 napi_create_arraybuffer(env, byteLength, &data, &resultBuff); 1554 unsigned char arr[8] = {0xFE, 0xFF, 0x00, 0x61, 0x00, 0x62, 0x00, 0x63}; 1555 int ret = memcpy_s(data, sizeof(arr), reinterpret_cast<void*>(arr), sizeof(arr)); 1556 ASSERT_EQ(0, ret); 1557 napi_value result2 = nullptr; 1558 napi_create_typedarray(env, napi_int8_array, byteLength, resultBuff, 0, &result2); 1559 napi_value testString = textDecoder.Decode(env, result2, iflag); 1560 size_t bufferSize = 0; 1561 napi_get_value_string_utf8(env, testString, nullptr, 0, &bufferSize); 1562 size_t length = 0; 1563 char* ch = nullptr; 1564 std::string tempStr01 = ""; 1565 if (bufferSize > 0) { 1566 ch = new char[bufferSize + 1](); 1567 napi_get_value_string_utf8(env, testString, ch, bufferSize + 1, &length); 1568 tempStr01 = ch; 1569 } 1570 std::u16string tempU16str02 = 1571 std::wstring_convert<std::codecvt_utf8_utf16<char16_t>, char16_t> {}.from_bytes(tempStr01); 1572 ASSERT_EQ(0xFEFF, static_cast<int>(tempU16str02[0])); 1573 ASSERT_EQ(0x61, static_cast<int>(tempU16str02[1])); 1574 ASSERT_EQ(0x62, static_cast<int>(tempU16str02[2])); 1575 ASSERT_EQ(0x63, static_cast<int>(tempU16str02[3])); 1576 if (ch != nullptr) { 1577 delete []ch; 1578 ch = nullptr; 1579 } 1580 } 1581 1582 /** 1583 * @tc.name: decoderUtf16be003 utf-16be 1584 * @tc.desc: Test date type. 1585 * @tc.type: FUNC 1586 */ 1587 HWTEST_F(NativeEngineTest, decoderUtf16be003, testing::ext::TestSize.Level0) 1588 { 1589 HILOG_INFO("decoderUtf16be003 start"); 1590 napi_env env = (napi_env)engine_; 1591 std::vector<int> inputVec; 1592 int fatal = 0; 1593 int ignoreBOM = 1; 1594 inputVec.push_back(fatal); 1595 inputVec.push_back(ignoreBOM); 1596 std::string str = "utf-16be"; 1597 OHOS::Util::TextDecoder textDecoder(str, inputVec); 1598 bool iflag = true; 1599 size_t byteLength = 8; 1600 void* data = nullptr; 1601 napi_value resultBuff = nullptr; 1602 napi_create_arraybuffer(env, byteLength, &data, &resultBuff); 1603 unsigned char arr[8] = {0xFE, 0xFF, 0x00, 0x61, 0x00, 0x62, 0x00, 0x63}; 1604 int ret = memcpy_s(data, sizeof(arr), reinterpret_cast<void*>(arr), sizeof(arr)); 1605 ASSERT_EQ(0, ret); 1606 napi_value result2 = nullptr; 1607 napi_create_typedarray(env, napi_int8_array, byteLength, resultBuff, 0, &result2); 1608 napi_value testString = textDecoder.Decode(env, result2, iflag); 1609 size_t bufferSize = 0; 1610 napi_get_value_string_utf8(env, testString, nullptr, 0, &bufferSize); 1611 size_t length = 0; 1612 char* ch = nullptr; 1613 std::string tempStr01 = ""; 1614 if (bufferSize > 0) { 1615 ch = new char[bufferSize + 1](); 1616 napi_get_value_string_utf8(env, testString, ch, bufferSize + 1, &length); 1617 tempStr01 = ch; 1618 } 1619 std::u16string tempU16str02 = 1620 std::wstring_convert<std::codecvt_utf8_utf16<char16_t>, char16_t> {}.from_bytes(tempStr01); 1621 ASSERT_EQ(0xFEFF, static_cast<int>(tempU16str02[0])); 1622 ASSERT_EQ(0x61, static_cast<int>(tempU16str02[1])); 1623 ASSERT_EQ(0x62, static_cast<int>(tempU16str02[2])); 1624 ASSERT_EQ(0x63, static_cast<int>(tempU16str02[3])); 1625 if (ch != nullptr) { 1626 delete []ch; 1627 ch = nullptr; 1628 } 1629 } 1630 1631 /** 1632 * @tc.name: decoderUtf8-BOM001 1633 * @tc.desc: Testing the decoding result of UTF-8 data with BOM. 1634 * @tc.type: FUNC 1635 */ 1636 HWTEST_F(NativeEngineTest, decoderUtf8BOM001, testing::ext::TestSize.Level0) 1637 { 1638 HILOG_INFO("decoderUtf8BOM001 start"); 1639 napi_env env = (napi_env)engine_; 1640 std::vector<int> inputVec; 1641 int fatal = 0; 1642 int ignoreBOM = 1; 1643 inputVec.push_back(fatal); 1644 inputVec.push_back(ignoreBOM); 1645 std::string encoding = "utf-8"; 1646 OHOS::Util::TextDecoder textDecoder(encoding, inputVec); 1647 bool iflag = true; 1648 size_t byteLength = 6; 1649 void* data = nullptr; 1650 napi_value resultBuff = nullptr; 1651 napi_create_arraybuffer(env, byteLength, &data, &resultBuff); 1652 unsigned char arr[8] = {0xEF, 0xBB, 0xBF, 0x41, 0x42, 0x43}; 1653 int ret = memcpy_s(data, sizeof(arr), reinterpret_cast<void*>(arr), sizeof(arr)); 1654 ASSERT_EQ(0, ret); 1655 napi_value result = nullptr; 1656 napi_create_typedarray(env, napi_int8_array, byteLength, resultBuff, 0, &result); 1657 napi_value testString = textDecoder.DecodeToString(env, result, iflag); 1658 size_t bufferSize = 0; 1659 napi_get_value_string_utf16(env, testString, nullptr, 0, &bufferSize); 1660 size_t length = 0; 1661 char16_t* ch = nullptr; 1662 if (bufferSize > 0) { 1663 ch = new char16_t[bufferSize + 1](); 1664 napi_get_value_string_utf16(env, testString, ch, bufferSize + 1, &length); 1665 } 1666 std::string str = 1667 std::wstring_convert<std::codecvt_utf8_utf16<char16_t>, char16_t> {}.to_bytes(ch); 1668 ASSERT_EQ(str, "ABC"); 1669 if (ch != nullptr) { 1670 delete []ch; 1671 ch = nullptr; 1672 } 1673 } 1674 1675 /** 1676 * @tc.name: decoderUtf8-BOM002 1677 * @tc.desc: Testing the decoding result of UTF-8 data with BOM. 1678 * @tc.type: FUNC 1679 */ 1680 HWTEST_F(NativeEngineTest, decoderUtf8BOM002, testing::ext::TestSize.Level0) 1681 { 1682 HILOG_INFO("decoderUtf8BOM002 start"); 1683 napi_env env = (napi_env)engine_; 1684 std::vector<int> inputVec; 1685 int fatal = 0; 1686 int ignoreBOM = 0; 1687 inputVec.push_back(fatal); 1688 inputVec.push_back(ignoreBOM); 1689 std::string encoding = "utf-8"; 1690 OHOS::Util::TextDecoder textDecoder(encoding, inputVec); 1691 bool iflag = true; 1692 size_t byteLength = 6; 1693 void* data = nullptr; 1694 napi_value resultBuff = nullptr; 1695 napi_create_arraybuffer(env, byteLength, &data, &resultBuff); 1696 unsigned char arr[8] = {0xEF, 0xBB, 0xBF, 0x41, 0x42, 0x43}; 1697 int ret = memcpy_s(data, sizeof(arr), reinterpret_cast<void*>(arr), sizeof(arr)); 1698 ASSERT_EQ(0, ret); 1699 napi_value result = nullptr; 1700 napi_create_typedarray(env, napi_int8_array, byteLength, resultBuff, 0, &result); 1701 napi_value testString = textDecoder.DecodeToString(env, result, iflag); 1702 size_t bufferSize = 0; 1703 napi_get_value_string_utf16(env, testString, nullptr, 0, &bufferSize); 1704 size_t length = 0; 1705 char16_t* ch = nullptr; 1706 if (bufferSize > 0) { 1707 ch = new char16_t[bufferSize + 1](); 1708 napi_get_value_string_utf16(env, testString, ch, bufferSize + 1, &length); 1709 } 1710 std::string str = 1711 std::wstring_convert<std::codecvt_utf8_utf16<char16_t>, char16_t> {}.to_bytes(ch); 1712 ASSERT_EQ(str, "\uFEFFABC"); 1713 if (ch != nullptr) { 1714 delete []ch; 1715 ch = nullptr; 1716 } 1717 } 1718 1719 /** 1720 * @tc.name: decoderUtf8-BOM003 1721 * @tc.desc: Decoder utf8 BOM with limit err. 1722 * @tc.type: FUNC 1723 */ 1724 HWTEST_F(NativeEngineTest, decoderUtf8BOM003, testing::ext::TestSize.Level0) 1725 { 1726 napi_env env = (napi_env)engine_; 1727 std::vector<int> inputVec; 1728 int fatal = -1; 1729 int ignoreBOM = -1; 1730 inputVec.push_back(fatal); 1731 inputVec.push_back(ignoreBOM); 1732 std::string str = "utf-8"; 1733 OHOS::Util::TextDecoder textDecoder(str, inputVec); 1734 bool iflag = false; 1735 size_t byteLength = 0; 1736 void *data = nullptr; 1737 napi_value resultBuff = nullptr; 1738 napi_create_arraybuffer(env, byteLength, &data, &resultBuff); 1739 napi_value result2 = nullptr; 1740 napi_create_typedarray(env, napi_int8_array, byteLength, resultBuff, 0, &result2); 1741 napi_value testString = textDecoder.DecodeToString(env, result2, iflag); 1742 ASSERT_TRUE(testString == nullptr); 1743 } 1744 1745 /** 1746 * @tc.name: decoderUtf8-BOM004 1747 * @tc.desc: Testing the decoding result of UTF-8 data with BOM. 1748 * @tc.type: FUNC 1749 */ 1750 HWTEST_F(NativeEngineTest, decoderUtf8BOM004, testing::ext::TestSize.Level0) 1751 { 1752 HILOG_INFO("decoderUtf8BOM001 start"); 1753 napi_env env = (napi_env)engine_; 1754 std::vector<int> inputVec; 1755 int fatal = 0; 1756 int ignoreBOM = 1; 1757 inputVec.push_back(fatal); 1758 inputVec.push_back(ignoreBOM); 1759 std::string encoding = "utf-8"; 1760 OHOS::Util::TextDecoder textDecoder(encoding, inputVec); 1761 bool iflag = false; 1762 size_t byteLength = 6; 1763 void* data = nullptr; 1764 napi_value resultBuff = nullptr; 1765 napi_create_arraybuffer(env, byteLength, &data, &resultBuff); 1766 unsigned char arr[8] = {0xEF, 0xBB, 0xBF, 0x41, 0x42, 0x43}; 1767 int ret = memcpy_s(data, sizeof(arr), reinterpret_cast<void*>(arr), sizeof(arr)); 1768 ASSERT_EQ(0, ret); 1769 napi_value result = nullptr; 1770 napi_create_typedarray(env, napi_int8_array, byteLength, resultBuff, 0, &result); 1771 napi_value testString = textDecoder.DecodeToString(env, result, iflag); 1772 size_t bufferSize = 0; 1773 napi_get_value_string_utf16(env, testString, nullptr, 0, &bufferSize); 1774 size_t length = 0; 1775 char16_t* ch = nullptr; 1776 if (bufferSize > 0) { 1777 ch = new char16_t[bufferSize + 1](); 1778 napi_get_value_string_utf16(env, testString, ch, bufferSize + 1, &length); 1779 } 1780 std::string str = 1781 std::wstring_convert<std::codecvt_utf8_utf16<char16_t>, char16_t> {}.to_bytes(ch); 1782 ASSERT_EQ(str, "ABC"); 1783 if (ch != nullptr) { 1784 delete []ch; 1785 ch = nullptr; 1786 } 1787 } 1788 1789 /** 1790 * @tc.name: decoderUtf8-BOM005 1791 * @tc.desc: Testing the decoding result of UTF-8 data with BOM. 1792 * @tc.type: FUNC 1793 */ 1794 HWTEST_F(NativeEngineTest, decoderUtf8BOM005, testing::ext::TestSize.Level0) 1795 { 1796 HILOG_INFO("decoderUtf8BOM001 start"); 1797 napi_env env = (napi_env)engine_; 1798 std::vector<int> inputVec; 1799 int fatal = 0; 1800 int ignoreBOM = 1; 1801 inputVec.push_back(fatal); 1802 inputVec.push_back(ignoreBOM); 1803 std::string encoding = "utf-16"; 1804 OHOS::Util::TextDecoder textDecoder(encoding, inputVec); 1805 bool iflag = true; 1806 size_t byteLength = 6; 1807 void* data = nullptr; 1808 napi_value resultBuff = nullptr; 1809 napi_create_arraybuffer(env, byteLength, &data, &resultBuff); 1810 unsigned char arr[8] = {0xEF, 0xBB, 0xBF, 0x41, 0x42, 0x43}; 1811 int ret = memcpy_s(data, sizeof(arr), reinterpret_cast<void*>(arr), sizeof(arr)); 1812 ASSERT_EQ(0, ret); 1813 napi_value result = nullptr; 1814 napi_create_typedarray(env, napi_int8_array, byteLength, resultBuff, 0, &result); 1815 napi_value testString = textDecoder.DecodeToString(env, result, iflag); 1816 size_t bufferSize = 0; 1817 napi_get_value_string_utf16(env, testString, nullptr, 0, &bufferSize); 1818 size_t length = 0; 1819 char16_t* ch = nullptr; 1820 if (bufferSize > 0) { 1821 ch = new char16_t[bufferSize + 1](); 1822 napi_get_value_string_utf16(env, testString, ch, bufferSize + 1, &length); 1823 } 1824 std::string str = 1825 std::wstring_convert<std::codecvt_utf8_utf16<char16_t>, char16_t> {}.to_bytes(ch); 1826 ASSERT_TRUE(str != ""); 1827 if (ch != nullptr) { 1828 delete []ch; 1829 ch = nullptr; 1830 } 1831 } 1832 1833 /** 1834 * @tc.name: getMinByteSizeTest001 utf-8 1835 * @tc.desc: get minbyte size with tranTool nullptr. 1836 * @tc.type: FUNC 1837 */ 1838 HWTEST_F(NativeEngineTest, getMinByteSizeTest001, testing::ext::TestSize.Level0) 1839 { 1840 std::vector<int> inputVec; 1841 int fatal = -1; 1842 int ignoreBOM = -1; 1843 inputVec.push_back(fatal); 1844 inputVec.push_back(ignoreBOM); 1845 std::string str = "XYZ123"; 1846 OHOS::Util::TextDecoder textDecoder(str, inputVec); 1847 textDecoder.Reset(); 1848 size_t rel1 = textDecoder.GetMinByteSize(); 1849 ASSERT_EQ(rel1, 0); 1850 } 1851 1852 /* @tc.name: encodeTest001 1853 * @tc.desc: Encodes all bytes in the specified u8 array into 1854 the newly allocated u8 array using the Base64 encoding scheme. 1855 * @tc.type: FUNC 1856 */ 1857 HWTEST_F(NativeEngineTest, encodeTest001, testing::ext::TestSize.Level0) 1858 { 1859 HILOG_INFO("encodeTest001 start"); 1860 napi_env env = (napi_env)engine_; 1861 OHOS::Util::Base64 base64; 1862 unsigned char input[3] = {0x73, 0x31, 0x33}; 1863 napi_value arrayBuffer = nullptr; 1864 void* data = nullptr; 1865 size_t arrayBufferSize = 3; 1866 napi_create_arraybuffer(env, arrayBufferSize, &data, &arrayBuffer); 1867 int ret = memcpy_s(data, sizeof(input), reinterpret_cast<void*>(input), sizeof(input)); 1868 ASSERT_EQ(0, ret); 1869 napi_value src = nullptr; 1870 napi_create_typedarray(env, napi_uint8_array, arrayBufferSize, arrayBuffer, 0, &src); 1871 1872 napi_value result = base64.EncodeSync(env, src, OHOS::Util::Type::BASIC); 1873 char excepted[4] = {0x63, 0x7A, 0x45, 0x7A}; 1874 napi_typedarray_type type; 1875 size_t srcLength = 0; 1876 void* srcData = nullptr; 1877 napi_value srcBuffer = nullptr; 1878 size_t byteOffset = 0; 1879 napi_get_typedarray_info(env, result, &type, &srcLength, &srcData, &srcBuffer, &byteOffset); 1880 char* res = (char*)srcData; 1881 ASSERT_EQ(res[0], excepted[0]); 1882 ASSERT_EQ(res[1], excepted[1]); 1883 ASSERT_EQ(res[2], excepted[2]); 1884 ASSERT_EQ(res[3], excepted[3]); 1885 } 1886 1887 /* @tc.name: encodeTest002 1888 * @tc.desc: Encodes all bytes in the specified u8 array 1889 into the newly allocated u8 array using the Base64 encoding scheme. 1890 * @tc.type: FUNC 1891 */ 1892 HWTEST_F(NativeEngineTest, encodeTest002, testing::ext::TestSize.Level0) 1893 { 1894 HILOG_INFO("encodeTest002 start"); 1895 napi_env env = (napi_env)engine_; 1896 OHOS::Util::Base64 base64; 1897 unsigned char input[14] = {66, 97, 115, 101, 54, 52, 32, 78, 111, 100, 101, 46, 106, 115}; 1898 napi_value arrayBuffer = nullptr; 1899 void* data = nullptr; 1900 size_t arrayBufferSize = 14; 1901 napi_create_arraybuffer(env, arrayBufferSize, &data, &arrayBuffer); 1902 int ret = memcpy_s(data, sizeof(input), reinterpret_cast<void*>(input), sizeof(input)); 1903 ASSERT_EQ(0, ret); 1904 napi_value src = nullptr; 1905 napi_create_typedarray(env, napi_uint8_array, arrayBufferSize, arrayBuffer, 0, &src); 1906 1907 napi_value result = base64.EncodeSync(env, src, OHOS::Util::Type::BASIC); 1908 char excepted[20] = {81, 109, 70, 122, 90, 84, 89, 48, 73, 69, 53, 118, 90, 71, 85, 117, 97, 110, 77, 61}; 1909 napi_typedarray_type type; 1910 size_t srcLength = 0; 1911 void* srcData = nullptr; 1912 napi_value srcBuffer = nullptr; 1913 size_t byteOffset = 0; 1914 napi_get_typedarray_info(env, result, &type, &srcLength, &srcData, &srcBuffer, &byteOffset); 1915 char* res = (char*)srcData; 1916 for (size_t i = 0; i < 20; i++) { 1917 ASSERT_EQ(res[i], excepted[i]); 1918 } 1919 } 1920 1921 /* @tc.name: encodeTest003 1922 * @tc.desc: Encodes all bytes in the specified u8 array 1923 into the newly allocated u8 array using the Base64 encoding scheme. 1924 * @tc.type: FUNC 1925 */ 1926 HWTEST_F(NativeEngineTest, encodeTest003, testing::ext::TestSize.Level0) 1927 { 1928 HILOG_INFO("encodeTest003 start"); 1929 napi_env env = (napi_env)engine_; 1930 OHOS::Util::Base64 base64; 1931 unsigned char input[26] = {66, 97, 115, 101, 54, 52, 32, 69, 110, 1932 99, 111, 100, 105, 110, 103, 32, 105, 110, 32, 78, 111, 100, 101, 46, 106, 115}; 1933 napi_value arrayBuffer = nullptr; 1934 void* data = nullptr; 1935 size_t arrayBufferSize = 26; 1936 napi_create_arraybuffer(env, arrayBufferSize, &data, &arrayBuffer); 1937 int ret = memcpy_s(data, sizeof(input), reinterpret_cast<void*>(input), sizeof(input)); 1938 ASSERT_EQ(0, ret); 1939 napi_value src = nullptr; 1940 napi_create_typedarray(env, napi_uint8_array, arrayBufferSize, arrayBuffer, 0, &src); 1941 1942 napi_value result = base64.EncodeSync(env, src, OHOS::Util::Type::BASIC); 1943 char excepted[36] = {81, 109, 70, 122, 90, 84, 89, 48, 73, 69, 86, 117, 89, 50, 57, 107, 97, 87, 53, 1944 110, 73, 71, 108, 117, 73, 69, 53, 118, 90, 71, 85, 117, 97, 110, 77, 61}; 1945 napi_typedarray_type type; 1946 size_t srcLength = 0; 1947 void* srcData = nullptr; 1948 napi_value srcBuffer = nullptr; 1949 size_t byteOffset = 0; 1950 napi_get_typedarray_info(env, result, &type, &srcLength, &srcData, &srcBuffer, &byteOffset); 1951 char* res = (char*)srcData; 1952 for (size_t i = 0; i < 36; i++) { 1953 ASSERT_EQ(res[i], excepted[i]); 1954 } 1955 } 1956 1957 /* @tc.name: encodeTest004 1958 * @tc.desc: Encodes all bytes in the specified u8 array into the 1959 newly allocated u8 array using the Base64 encoding scheme. 1960 * @tc.type: FUNC 1961 */ 1962 HWTEST_F(NativeEngineTest, encodeTest004, testing::ext::TestSize.Level0) 1963 { 1964 HILOG_INFO("encodeTest004 start"); 1965 napi_env env = (napi_env)engine_; 1966 OHOS::Util::Base64 base64; 1967 unsigned char input[4] = {168, 174, 155, 255}; 1968 napi_value arrayBuffer = nullptr; 1969 void* data = nullptr; 1970 size_t arrayBufferSize = 4; 1971 napi_create_arraybuffer(env, arrayBufferSize, &data, &arrayBuffer); 1972 int ret = memcpy_s(data, sizeof(input), reinterpret_cast<void*>(input), sizeof(input)); 1973 ASSERT_EQ(0, ret); 1974 napi_value src = nullptr; 1975 napi_create_typedarray(env, napi_uint8_array, arrayBufferSize, arrayBuffer, 0, &src); 1976 1977 napi_value result = base64.EncodeSync(env, src, OHOS::Util::Type::BASIC); 1978 char excepted[8] = {113, 75, 54, 98, 47, 119, 61, 61}; 1979 napi_typedarray_type type; 1980 size_t srcLength = 0; 1981 void* srcData = nullptr; 1982 napi_value srcBuffer = nullptr; 1983 size_t byteOffset = 0; 1984 napi_get_typedarray_info(env, result, &type, &srcLength, &srcData, &srcBuffer, &byteOffset); 1985 char* res = (char*)srcData; 1986 for (size_t i = 0; i < 8; i++) { 1987 ASSERT_EQ(res[i], excepted[i]); 1988 } 1989 } 1990 1991 /* @tc.name: encodeTest005 1992 * @tc.desc: Encodes all bytes in the specified u8 array 1993 into the newly allocated u8 array using the Base64 encoding scheme. 1994 * @tc.type: FUNC 1995 */ 1996 HWTEST_F(NativeEngineTest, encodeTest005, testing::ext::TestSize.Level0) 1997 { 1998 HILOG_INFO("encodeTest005 start"); 1999 napi_env env = (napi_env)engine_; 2000 OHOS::Util::Base64 base64; 2001 unsigned char input[6] = {66, 97, 115, 101, 54, 52}; 2002 napi_value arrayBuffer = nullptr; 2003 void* data = nullptr; 2004 size_t arrayBufferSize = 6; 2005 napi_create_arraybuffer(env, arrayBufferSize, &data, &arrayBuffer); 2006 int ret = memcpy_s(data, sizeof(input), reinterpret_cast<void*>(input), sizeof(input)); 2007 ASSERT_EQ(0, ret); 2008 napi_value src = nullptr; 2009 napi_create_typedarray(env, napi_uint8_array, arrayBufferSize, arrayBuffer, 0, &src); 2010 2011 napi_value result = base64.EncodeSync(env, src, OHOS::Util::Type::BASIC); 2012 char excepted[8] = {81, 109, 70, 122, 90, 84, 89, 48}; 2013 napi_typedarray_type type; 2014 size_t srcLength = 0; 2015 void* srcData = nullptr; 2016 napi_value srcBuffer = nullptr; 2017 size_t byteOffset = 0; 2018 napi_get_typedarray_info(env, result, &type, &srcLength, &srcData, &srcBuffer, &byteOffset); 2019 char* res = (char*)srcData; 2020 for (size_t i = 0; i < 8; i++) { 2021 ASSERT_EQ(res[i], excepted[i]); 2022 } 2023 } 2024 2025 /* @tc.name: encodeTest006 2026 * @tc.desc: Encode sync with napi_uint16_array. 2027 * @tc.type: FUNC 2028 */ 2029 HWTEST_F(NativeEngineTest, encodeTest006, testing::ext::TestSize.Level0) 2030 { 2031 HILOG_INFO("encodeTest006 start"); 2032 napi_env env = (napi_env)engine_; 2033 OHOS::Util::Base64 base64; 2034 unsigned char input[6] = {66, 97, 115, 101, 54, 51}; 2035 napi_value arrayBuffer = nullptr; 2036 void* data = nullptr; 2037 size_t arrayBufferSize = 6; 2038 napi_create_arraybuffer(env, arrayBufferSize, &data, &arrayBuffer); 2039 int ret = memcpy_s(data, sizeof(input), reinterpret_cast<void*>(input), sizeof(input)); 2040 ASSERT_EQ(0, ret); 2041 napi_value src = nullptr; 2042 napi_create_typedarray(env, napi_uint16_array, arrayBufferSize, arrayBuffer, 0, &src); 2043 2044 napi_value result = base64.EncodeSync(env, src, OHOS::Util::Type::BASIC); 2045 ASSERT_EQ(nullptr, result); 2046 napi_value result1 = base64.EncodeToStringSync(env, src, OHOS::Util::Type::BASIC); 2047 ASSERT_EQ(nullptr, result1); 2048 napi_value exception; 2049 napi_get_and_clear_last_exception(env, &exception); 2050 } 2051 2052 /* @tc.name: encodeTest007 2053 * @tc.desc: Encodes all bytes in the specified u8 array with type BASIC_URL_SAFE. 2054 * @tc.type: FUNC 2055 */ 2056 HWTEST_F(NativeEngineTest, encodeTest007, testing::ext::TestSize.Level0) 2057 { 2058 HILOG_INFO("encodeTest007 start"); 2059 napi_env env = (napi_env)engine_; 2060 OHOS::Util::Base64 base64; 2061 unsigned char input[4] = {168, 174, 155, 255}; 2062 napi_value arrayBuffer = nullptr; 2063 void* data = nullptr; 2064 size_t arrayBufferSize = 4; 2065 napi_create_arraybuffer(env, arrayBufferSize, &data, &arrayBuffer); 2066 int ret = memcpy_s(data, sizeof(input), reinterpret_cast<void*>(input), sizeof(input)); 2067 ASSERT_EQ(0, ret); 2068 napi_value src = nullptr; 2069 napi_create_typedarray(env, napi_uint8_array, arrayBufferSize, arrayBuffer, 0, &src); 2070 2071 napi_value result = base64.EncodeSync(env, src, OHOS::Util::Type::BASIC_URL_SAFE); 2072 char excepted[7] = {113, 75, 54, 98, 95, 119}; 2073 napi_typedarray_type type; 2074 size_t srcLength = 0; 2075 void* srcData = nullptr; 2076 napi_value srcBuffer = nullptr; 2077 size_t byteOffset = 0; 2078 napi_get_typedarray_info(env, result, &type, &srcLength, &srcData, &srcBuffer, &byteOffset); 2079 char* res = (char*)srcData; 2080 for (size_t i = 0; i < 6; i++) { 2081 ASSERT_EQ(res[i], excepted[i]); 2082 } 2083 } 2084 2085 /* @tc.name: encodeToStringTest001 2086 * @tc.desc: Encodes the specified byte array as a String using the Base64 encoding scheme. 2087 * @tc.type: FUNC 2088 */ 2089 HWTEST_F(NativeEngineTest, encodeToStringTest001, testing::ext::TestSize.Level0) 2090 { 2091 HILOG_INFO("encodeToStringTest001 start"); 2092 napi_env env = (napi_env)engine_; 2093 OHOS::Util::Base64 base64; 2094 2095 unsigned char input[3] = {115, 49, 51}; 2096 napi_value arrayBuffer = nullptr; 2097 size_t arrayBufferSize = 3; 2098 void* data = nullptr; 2099 napi_create_arraybuffer(env, arrayBufferSize, &data, &arrayBuffer); 2100 int ret = memcpy_s(data, sizeof(input), reinterpret_cast<void*>(input), sizeof(input)); 2101 ASSERT_EQ(0, ret); 2102 napi_value src = nullptr; 2103 napi_create_typedarray(env, napi_uint8_array, arrayBufferSize, arrayBuffer, 0, &src); 2104 napi_value result = base64.EncodeToStringSync(env, src, OHOS::Util::Type::BASIC); 2105 size_t prolen = 0; 2106 char* inputString = nullptr; 2107 napi_get_value_string_utf8(env, result, nullptr, 0, &prolen); 2108 if (prolen > 0) { 2109 inputString = new char[prolen + 1]; 2110 if (memset_s(inputString, prolen + 1, '\0', prolen + 1) != 0) { 2111 napi_throw_error(env, "-1", "decode inputString memset_s failed"); 2112 } 2113 } else { 2114 napi_throw_error(env, "-2", "prolen is error !"); 2115 } 2116 napi_get_value_string_utf8(env, result, inputString, prolen + 1, &prolen); 2117 ASSERT_STREQ("czEz", inputString); 2118 if (inputString != nullptr) { 2119 delete []inputString; 2120 inputString = nullptr; 2121 } 2122 } 2123 2124 /* @tc.name: encodeToStringTest002 2125 * @tc.desc: Encodes the specified byte array as a String using the Base64 encoding scheme. 2126 * @tc.type: FUNC 2127 */ 2128 HWTEST_F(NativeEngineTest, encodeToStringTest002, testing::ext::TestSize.Level0) 2129 { 2130 HILOG_INFO("encodeToStringTest002 start"); 2131 napi_env env = (napi_env)engine_; 2132 OHOS::Util::Base64 base64; 2133 2134 unsigned char input[14] = {66, 97, 115, 101, 54, 52, 32, 78, 111, 100, 101, 46, 106, 115}; 2135 napi_value arrayBuffer = nullptr; 2136 size_t arrayBufferSize = 14; 2137 void* data = nullptr; 2138 napi_create_arraybuffer(env, arrayBufferSize, &data, &arrayBuffer); 2139 int ret = memcpy_s(data, sizeof(input), reinterpret_cast<void*>(input), sizeof(input)); 2140 ASSERT_EQ(0, ret); 2141 napi_value src = nullptr; 2142 napi_create_typedarray(env, napi_uint8_array, arrayBufferSize, arrayBuffer, 0, &src); 2143 napi_value result = base64.EncodeToStringSync(env, src, OHOS::Util::Type::BASIC); 2144 size_t prolen = 0; 2145 char* inputString = nullptr; 2146 napi_get_value_string_utf8(env, result, nullptr, 0, &prolen); 2147 if (prolen > 0) { 2148 inputString = new char[prolen + 1]; 2149 if (memset_s(inputString, prolen + 1, '\0', prolen + 1) != 0) { 2150 napi_throw_error(env, "-1", "decode inputString memset_s failed"); 2151 } 2152 } else { 2153 napi_throw_error(env, "-2", "prolen is error !"); 2154 } 2155 napi_get_value_string_utf8(env, result, inputString, prolen + 1, &prolen); 2156 ASSERT_STREQ("QmFzZTY0IE5vZGUuanM=", inputString); 2157 if (inputString != nullptr) { 2158 delete []inputString; 2159 inputString = nullptr; 2160 } 2161 } 2162 2163 /* @tc.name: encodeToStringTest003 2164 * @tc.desc: Encodes the specified byte array as a String using the Base64 encoding scheme. 2165 * @tc.type: FUNC 2166 */ 2167 HWTEST_F(NativeEngineTest, encodeToStringTest003, testing::ext::TestSize.Level0) 2168 { 2169 HILOG_INFO("encodeToStringTest003 start"); 2170 napi_env env = (napi_env)engine_; 2171 OHOS::Util::Base64 base64; 2172 2173 unsigned char input[26] = {66, 97, 115, 101, 54, 52, 32, 69, 110, 2174 99, 111, 100, 105, 110, 103, 32, 105, 110, 32, 78, 111, 100, 101, 46, 106, 115}; 2175 napi_value arrayBuffer = nullptr; 2176 size_t arrayBufferSize = 26; 2177 void* data = nullptr; 2178 napi_create_arraybuffer(env, arrayBufferSize, &data, &arrayBuffer); 2179 int ret = memcpy_s(data, sizeof(input), reinterpret_cast<void*>(input), sizeof(input)); 2180 ASSERT_EQ(0, ret); 2181 napi_value src = nullptr; 2182 napi_create_typedarray(env, napi_uint8_array, arrayBufferSize, arrayBuffer, 0, &src); 2183 napi_value result = base64.EncodeToStringSync(env, src, OHOS::Util::Type::BASIC); 2184 size_t prolen = 0; 2185 char* inputString = nullptr; 2186 napi_get_value_string_utf8(env, result, nullptr, 0, &prolen); 2187 if (prolen > 0) { 2188 inputString = new char[prolen + 1]; 2189 if (memset_s(inputString, prolen + 1, '\0', prolen + 1) != 0) { 2190 napi_throw_error(env, "-1", "decode inputString memset_s failed"); 2191 } 2192 } else { 2193 napi_throw_error(env, "-2", "prolen is error !"); 2194 } 2195 napi_get_value_string_utf8(env, result, inputString, prolen + 1, &prolen); 2196 ASSERT_STREQ("QmFzZTY0IEVuY29kaW5nIGluIE5vZGUuanM=", inputString); 2197 if (inputString != nullptr) { 2198 delete []inputString; 2199 inputString = nullptr; 2200 } 2201 } 2202 2203 /* @tc.name: encodeToStringTest004 2204 * @tc.desc: Encodes the specified byte array as a String using the Base64 encoding scheme. 2205 * @tc.type: FUNC 2206 */ 2207 HWTEST_F(NativeEngineTest, encodeToStringTest004, testing::ext::TestSize.Level0) 2208 { 2209 HILOG_INFO("encodeToStringTest004 start"); 2210 napi_env env = (napi_env)engine_; 2211 OHOS::Util::Base64 base64; 2212 2213 unsigned char input[4] = {168, 174, 155, 255}; 2214 napi_value arrayBuffer = nullptr; 2215 size_t arrayBufferSize = 4; 2216 void* data = nullptr; 2217 napi_create_arraybuffer(env, arrayBufferSize, &data, &arrayBuffer); 2218 int ret = memcpy_s(data, sizeof(input), reinterpret_cast<void*>(input), sizeof(input)); 2219 ASSERT_EQ(0, ret); 2220 napi_value src = nullptr; 2221 napi_create_typedarray(env, napi_uint8_array, arrayBufferSize, arrayBuffer, 0, &src); 2222 napi_value result = base64.EncodeToStringSync(env, src, OHOS::Util::Type::BASIC); 2223 size_t prolen = 0; 2224 char* inputString = nullptr; 2225 napi_get_value_string_utf8(env, result, nullptr, 0, &prolen); 2226 if (prolen > 0) { 2227 inputString = new char[prolen + 1]; 2228 if (memset_s(inputString, prolen + 1, '\0', prolen + 1) != 0) { 2229 napi_throw_error(env, "-1", "decode inputString memset_s failed"); 2230 } 2231 } else { 2232 napi_throw_error(env, "-2", "prolen is error !"); 2233 } 2234 napi_get_value_string_utf8(env, result, inputString, prolen + 1, &prolen); 2235 ASSERT_STREQ("qK6b/w==", inputString); 2236 if (inputString != nullptr) { 2237 delete []inputString; 2238 inputString = nullptr; 2239 } 2240 } 2241 2242 /* @tc.name: encodeToStringTest005 2243 * @tc.desc: Encodes the specified byte array as a String using the Base64 encoding scheme. 2244 * @tc.type: FUNC 2245 */ 2246 HWTEST_F(NativeEngineTest, encodeToStringTest005, testing::ext::TestSize.Level0) 2247 { 2248 HILOG_INFO("encodeToStringTest005 start"); 2249 napi_env env = (napi_env)engine_; 2250 OHOS::Util::Base64 base64; 2251 2252 unsigned char input[6] = {66, 97, 115, 101, 54, 52}; 2253 napi_value arrayBuffer = nullptr; 2254 size_t arrayBufferSize = 6; 2255 void* data = nullptr; 2256 napi_create_arraybuffer(env, arrayBufferSize, &data, &arrayBuffer); 2257 int ret = memcpy_s(data, sizeof(input), reinterpret_cast<void*>(input), sizeof(input)); 2258 ASSERT_EQ(0, ret); 2259 napi_value src = nullptr; 2260 napi_create_typedarray(env, napi_uint8_array, arrayBufferSize, arrayBuffer, 0, &src); 2261 napi_value result = base64.EncodeToStringSync(env, src, OHOS::Util::Type::BASIC); 2262 size_t prolen = 0; 2263 char* inputString = nullptr; 2264 napi_get_value_string_utf8(env, result, nullptr, 0, &prolen); 2265 if (prolen > 0) { 2266 inputString = new char[prolen + 1]; 2267 if (memset_s(inputString, prolen + 1, '\0', prolen + 1) != 0) { 2268 napi_throw_error(env, "-1", "decode inputString memset_s failed"); 2269 } 2270 } else { 2271 napi_throw_error(env, "-2", "prolen is error !"); 2272 } 2273 napi_get_value_string_utf8(env, result, inputString, prolen + 1, &prolen); 2274 ASSERT_STREQ("QmFzZTY0", inputString); 2275 if (inputString != nullptr) { 2276 delete []inputString; 2277 inputString = nullptr; 2278 } 2279 } 2280 2281 /* @tc.name: decodeTest001 2282 * @tc.desc: Decodes the Base64-encoded string or input u8 array 2283 into the newly allocated u8 array using the Base64 encoding scheme. 2284 * @tc.type: FUNC 2285 */ 2286 HWTEST_F(NativeEngineTest, decodeTest001, testing::ext::TestSize.Level0) 2287 { 2288 HILOG_INFO("decodeTest001 start"); 2289 napi_env env = (napi_env)engine_; 2290 OHOS::Util::Base64 base64; 2291 2292 unsigned char input[4] = {99, 122, 69, 122}; 2293 napi_value arrayBuffer = nullptr; 2294 size_t arrayBufferSize = 4; 2295 void* data = nullptr; 2296 napi_create_arraybuffer(env, arrayBufferSize, &data, &arrayBuffer); 2297 int ret = memcpy_s(data, sizeof(input), reinterpret_cast<void*>(input), sizeof(input)); 2298 ASSERT_EQ(0, ret); 2299 napi_value src = nullptr; 2300 napi_create_typedarray(env, napi_uint8_array, arrayBufferSize, arrayBuffer, 0, &src); 2301 napi_value result = base64.DecodeSync(env, src, OHOS::Util::Type::BASIC); 2302 char excepted[3] = {115, 49, 51}; 2303 napi_typedarray_type type; 2304 size_t srcLength = 0; 2305 void* srcData = nullptr; 2306 napi_value srcBuffer = nullptr; 2307 size_t byteOffset = 0; 2308 napi_get_typedarray_info(env, result, &type, &srcLength, &srcData, &srcBuffer, &byteOffset); 2309 char* res = (char*)srcData; 2310 2311 ASSERT_EQ(res[0], excepted[0]); 2312 ASSERT_EQ(res[1], excepted[1]); 2313 ASSERT_EQ(res[2], excepted[2]); 2314 } 2315 2316 /* @tc.name: decodeTest002 2317 * @tc.desc: Decodes the Base64-encoded string or input u8 array 2318 into the newly allocated u8 array using the Base64 encoding scheme. 2319 * @tc.type: FUNC 2320 */ 2321 HWTEST_F(NativeEngineTest, decodeTest002, testing::ext::TestSize.Level0) 2322 { 2323 HILOG_INFO("decodeTest002 start"); 2324 napi_env env = (napi_env)engine_; 2325 OHOS::Util::Base64 base64; 2326 2327 unsigned char input[20] = {81, 109, 70, 122, 90, 84, 89, 48, 73, 69, 53, 118, 90, 71, 85, 117, 97, 110, 77, 61}; 2328 napi_value arrayBuffer = nullptr; 2329 size_t arrayBufferSize = 20; 2330 void* data = nullptr; 2331 napi_create_arraybuffer(env, arrayBufferSize, &data, &arrayBuffer); 2332 int ret = memcpy_s(data, sizeof(input), reinterpret_cast<void*>(input), sizeof(input)); 2333 ASSERT_EQ(0, ret); 2334 napi_value src = nullptr; 2335 napi_create_typedarray(env, napi_uint8_array, arrayBufferSize, arrayBuffer, 0, &src); 2336 napi_value result = base64.DecodeSync(env, src, OHOS::Util::Type::BASIC); 2337 char excepted[14] = {66, 97, 115, 101, 54, 52, 32, 78, 111, 100, 101, 46, 106, 115}; 2338 napi_typedarray_type type; 2339 size_t srcLength = 0; 2340 void* srcData = nullptr; 2341 napi_value srcBuffer = nullptr; 2342 size_t byteOffset = 0; 2343 napi_get_typedarray_info(env, result, &type, &srcLength, &srcData, &srcBuffer, &byteOffset); 2344 char* res = (char*)srcData; 2345 2346 for (size_t i = 0; i < 14; i++) { 2347 ASSERT_EQ(res[i], excepted[i]); 2348 } 2349 } 2350 2351 /* @tc.name: decodeTest003 2352 * @tc.desc: Decodes the Base64-encoded string or input u8 array 2353 into the newly allocated u8 array using the Base64 encoding scheme. 2354 * @tc.type: FUNC 2355 */ 2356 HWTEST_F(NativeEngineTest, decodeTest003, testing::ext::TestSize.Level0) 2357 { 2358 HILOG_INFO("decodeTest003 start"); 2359 napi_env env = (napi_env)engine_; 2360 OHOS::Util::Base64 base64; 2361 2362 std::string input = "czEz"; 2363 napi_value src = nullptr; 2364 napi_create_string_utf8(env, input.c_str(), input.size(), &src); 2365 napi_value result = base64.DecodeSync(env, src, OHOS::Util::Type::BASIC); 2366 char excepted[3] = {115, 49, 51}; 2367 napi_typedarray_type type; 2368 size_t srcLength = 0; 2369 void* srcData = nullptr; 2370 napi_value srcBuffer = nullptr; 2371 size_t byteOffset = 0; 2372 napi_get_typedarray_info(env, result, &type, &srcLength, &srcData, &srcBuffer, &byteOffset); 2373 char* res = (char*)srcData; 2374 2375 ASSERT_EQ(res[0], excepted[0]); 2376 ASSERT_EQ(res[1], excepted[1]); 2377 ASSERT_EQ(res[2], excepted[2]); 2378 } 2379 2380 /* @tc.name: decodeTest004 2381 * @tc.desc: Decodes the Base64-encoded string or input u8 array 2382 into the newly allocated u8 array using the Base64 encoding scheme. 2383 * @tc.type: FUNC 2384 */ 2385 HWTEST_F(NativeEngineTest, decodeTest004, testing::ext::TestSize.Level0) 2386 { 2387 HILOG_INFO("decodeTest004 start"); 2388 napi_env env = (napi_env)engine_; 2389 OHOS::Util::Base64 base64; 2390 2391 std::string input = "qK6b/w=="; 2392 napi_value src = nullptr; 2393 napi_create_string_utf8(env, input.c_str(), input.size(), &src); 2394 napi_value result = base64.DecodeSync(env, src, OHOS::Util::Type::BASIC); 2395 char excepted[4] = {168, 174, 155, 255}; 2396 napi_typedarray_type type; 2397 size_t srcLength = 0; 2398 void* srcData = nullptr; 2399 napi_value srcBuffer = nullptr; 2400 size_t byteOffset = 0; 2401 napi_get_typedarray_info(env, result, &type, &srcLength, &srcData, &srcBuffer, &byteOffset); 2402 char* res = (char*)srcData; 2403 for (size_t i = 0; i < 4; i++) { 2404 ASSERT_EQ(res[i], excepted[i]); 2405 } 2406 } 2407 2408 /* @tc.name: decodeTest005 2409 * @tc.desc: Decodes the Base64-encoded string or input u8 array 2410 into the newly allocated u8 array using the Base64 encoding scheme. 2411 * @tc.type: FUNC 2412 */ 2413 HWTEST_F(NativeEngineTest, decodeTest005, testing::ext::TestSize.Level0) 2414 { 2415 HILOG_INFO("decodeTest005 start"); 2416 napi_env env = (napi_env)engine_; 2417 OHOS::Util::Base64 base64; 2418 2419 std::string input = "QmFzZTY0"; 2420 napi_value src = nullptr; 2421 napi_create_string_utf8(env, input.c_str(), input.size(), &src); 2422 napi_value result = base64.DecodeSync(env, src, OHOS::Util::Type::BASIC); 2423 char excepted[6] = {66, 97, 115, 101, 54, 52}; 2424 napi_typedarray_type type; 2425 size_t srcLength = 0; 2426 void* srcData = nullptr; 2427 napi_value srcBuffer = nullptr; 2428 size_t byteOffset = 0; 2429 napi_get_typedarray_info(env, result, &type, &srcLength, &srcData, &srcBuffer, &byteOffset); 2430 char* res = (char*)srcData; 2431 for (size_t i = 0; i < 6; i++) { 2432 ASSERT_EQ(res[i], excepted[i]); 2433 } 2434 } 2435 2436 /* @tc.name: decodeTest006 2437 * @tc.desc: Decodes the Base64-encoded string or input unit32 array with return null. 2438 * @tc.type: FUNC 2439 */ 2440 HWTEST_F(NativeEngineTest, decodeTest006, testing::ext::TestSize.Level0) 2441 { 2442 HILOG_INFO("decodeTest006 start"); 2443 napi_env env = (napi_env)engine_; 2444 OHOS::Util::Base64 base64; 2445 2446 std::string input1 = ""; 2447 napi_value src1 = nullptr; 2448 napi_create_string_utf8(env, input1.c_str(), input1.size(), &src1); 2449 base64.DecodeSync(env, src1, OHOS::Util::Type::BASIC_URL_SAFE); 2450 napi_value result1 = base64.DecodeSync(env, src1, OHOS::Util::Type::BASIC); 2451 ASSERT_EQ(result1, nullptr); 2452 napi_value exception; 2453 napi_get_and_clear_last_exception(env, &exception); 2454 2455 unsigned char input[20] = {81, 109, 70, 122, 90, 84, 89, 48, 73, 69, 53, 118, 90, 71, 85, 117, 97, 110, 77, 61}; 2456 napi_value arrayBuffer = nullptr; 2457 size_t arrayBufferSize = 20; 2458 void* data = nullptr; 2459 napi_create_arraybuffer(env, arrayBufferSize, &data, &arrayBuffer); 2460 int ret = memcpy_s(data, sizeof(input), reinterpret_cast<void*>(input), sizeof(input)); 2461 ASSERT_EQ(0, ret); 2462 napi_value src = nullptr; 2463 napi_create_typedarray(env, napi_uint32_array, arrayBufferSize, arrayBuffer, 0, &src); 2464 napi_value result = base64.DecodeSync(env, src, OHOS::Util::Type::BASIC); 2465 ASSERT_EQ(result, nullptr); 2466 2467 napi_get_and_clear_last_exception(env, &exception); 2468 } 2469 2470 /* @tc.name: decodeTest007 2471 * @tc.desc: Decodes the Base64-encoded string with type BASIC_URL_SAFE. 2472 * @tc.type: FUNC 2473 */ 2474 HWTEST_F(NativeEngineTest, decodeTest007, testing::ext::TestSize.Level0) 2475 { 2476 HILOG_INFO("decodeTest007 start"); 2477 napi_env env = (napi_env)engine_; 2478 OHOS::Util::Base64 base64; 2479 std::string input = "qK6b/w=="; 2480 napi_value src = nullptr; 2481 napi_create_string_utf8(env, input.c_str(), input.size(), &src); 2482 napi_value result = base64.DecodeSync(env, src, OHOS::Util::Type::BASIC_URL_SAFE); 2483 ASSERT_EQ(nullptr, result); 2484 2485 std::string input1 = "qK6b/w"; 2486 napi_value src1 = nullptr; 2487 napi_create_string_utf8(env, input1.c_str(), input1.size(), &src1); 2488 napi_value result1 = base64.DecodeSync(env, src1, OHOS::Util::Type::BASIC_URL_SAFE); 2489 ASSERT_EQ(nullptr, result1); 2490 2491 std::string input2 = "qK6b/w="; 2492 napi_value src2 = nullptr; 2493 napi_create_string_utf8(env, input2.c_str(), input2.size(), &src2); 2494 napi_value result2 = base64.DecodeSync(env, src2, OHOS::Util::Type::BASIC_URL_SAFE); 2495 ASSERT_EQ(nullptr, result2); 2496 2497 napi_value exception; 2498 napi_get_and_clear_last_exception(env, &exception); 2499 } 2500 2501 /* @tc.name: encodeAsyncTest001 2502 * @tc.desc: Asynchronously encodes all bytes in the specified u8 array 2503 into the newly allocated u8 array using the Base64 encoding scheme. 2504 * @tc.type: FUNC 2505 */ 2506 HWTEST_F(NativeEngineTest, encodeAsyncTest001, testing::ext::TestSize.Level0) 2507 { 2508 HILOG_INFO("encodeAsyncTest001 start"); 2509 napi_env env = (napi_env)engine_; 2510 OHOS::Util::Base64 base64; 2511 unsigned char input[3] = {0x73, 0x31, 0x33}; 2512 napi_value arrayBuffer = nullptr; 2513 void* data = nullptr; 2514 size_t arrayBufferSize = 3; 2515 napi_create_arraybuffer(env, arrayBufferSize, &data, &arrayBuffer); 2516 int ret = memcpy_s(data, sizeof(input), reinterpret_cast<void*>(input), sizeof(input)); 2517 ASSERT_EQ(0, ret); 2518 napi_value src = nullptr; 2519 napi_create_typedarray(env, napi_uint8_array, arrayBufferSize, arrayBuffer, 0, &src); 2520 2521 napi_value result = base64.Encode(env, src, OHOS::Util::Type::BASIC); 2522 bool res = false; 2523 napi_is_promise(env, result, &res); 2524 ASSERT_TRUE(res); 2525 2526 napi_value src1 = nullptr; 2527 napi_create_typedarray(env, napi_uint16_array, arrayBufferSize, arrayBuffer, 0, &src1); 2528 napi_value result1 = base64.Encode(env, src1, OHOS::Util::Type::BASIC); 2529 ASSERT_EQ(result1, nullptr); 2530 2531 napi_value exception; 2532 napi_get_and_clear_last_exception(env, &exception); 2533 } 2534 2535 /* @tc.name: encodeAsyncTest002 2536 * @tc.desc: Asynchronously encodes all bytes in the specified u8 array 2537 into the newly allocated u8 array using the Base64 encoding scheme. 2538 * @tc.type: FUNC 2539 */ 2540 HWTEST_F(NativeEngineTest, encodeAsyncTest002, testing::ext::TestSize.Level0) 2541 { 2542 HILOG_INFO("encodeAsyncTest002 start"); 2543 napi_env env = (napi_env)engine_; 2544 OHOS::Util::Base64 base64; 2545 unsigned char input[14] = {66, 97, 115, 101, 54, 52, 32, 78, 111, 100, 101, 46, 106, 115}; 2546 napi_value arrayBuffer = nullptr; 2547 void* data = nullptr; 2548 size_t arrayBufferSize = 14; 2549 napi_create_arraybuffer(env, arrayBufferSize, &data, &arrayBuffer); 2550 int ret = memcpy_s(data, sizeof(input), reinterpret_cast<void*>(input), sizeof(input)); 2551 ASSERT_EQ(0, ret); 2552 napi_value src = nullptr; 2553 napi_create_typedarray(env, napi_uint8_array, arrayBufferSize, arrayBuffer, 0, &src); 2554 2555 napi_value result = base64.Encode(env, src, OHOS::Util::Type::BASIC); 2556 bool res = false; 2557 napi_is_promise(env, result, &res); 2558 ASSERT_TRUE(res); 2559 } 2560 2561 /* @tc.name: encodeAsyncTest003 2562 * @tc.desc: Asynchronously encodes all bytes in the specified u8 array 2563 into the newly allocated u8 array using the Base64 encoding scheme. 2564 * @tc.type: FUNC 2565 */ 2566 HWTEST_F(NativeEngineTest, encodeAsyncTest003, testing::ext::TestSize.Level0) 2567 { 2568 HILOG_INFO("encodeAsyncTest003 start"); 2569 napi_env env = (napi_env)engine_; 2570 OHOS::Util::Base64 base64; 2571 unsigned char input[26] = {66, 97, 115, 101, 54, 52, 32, 69, 110, 2572 99, 111, 100, 105, 110, 103, 32, 105, 110, 32, 78, 111, 100, 101, 46, 106, 115}; 2573 napi_value arrayBuffer = nullptr; 2574 void* data = nullptr; 2575 size_t arrayBufferSize = 26; 2576 napi_create_arraybuffer(env, arrayBufferSize, &data, &arrayBuffer); 2577 int ret = memcpy_s(data, sizeof(input), reinterpret_cast<void*>(input), sizeof(input)); 2578 ASSERT_EQ(0, ret); 2579 napi_value src = nullptr; 2580 napi_create_typedarray(env, napi_uint8_array, arrayBufferSize, arrayBuffer, 0, &src); 2581 2582 napi_value result = base64.Encode(env, src, OHOS::Util::Type::BASIC); 2583 bool res = false; 2584 napi_is_promise(env, result, &res); 2585 ASSERT_TRUE(res); 2586 } 2587 2588 /* @tc.name: encodeAsyncTest004 2589 * @tc.desc: Asynchronously encodes all bytes in the specified u8 array 2590 into the newly allocated u8 array using the Base64 encoding scheme. 2591 * @tc.type: FUNC 2592 */ 2593 HWTEST_F(NativeEngineTest, encodeAsyncTest004, testing::ext::TestSize.Level0) 2594 { 2595 HILOG_INFO("encodeAsyncTest004 start"); 2596 napi_env env = (napi_env)engine_; 2597 OHOS::Util::Base64 base64; 2598 unsigned char input[4] = {168, 174, 155, 255}; 2599 napi_value arrayBuffer = nullptr; 2600 void* data = nullptr; 2601 size_t arrayBufferSize = 4; 2602 napi_create_arraybuffer(env, arrayBufferSize, &data, &arrayBuffer); 2603 int ret = memcpy_s(data, sizeof(input), reinterpret_cast<void*>(input), sizeof(input)); 2604 ASSERT_EQ(0, ret); 2605 napi_value src = nullptr; 2606 napi_create_typedarray(env, napi_uint8_array, arrayBufferSize, arrayBuffer, 0, &src); 2607 2608 napi_value result = base64.Encode(env, src, OHOS::Util::Type::BASIC); 2609 bool res = false; 2610 napi_is_promise(env, result, &res); 2611 ASSERT_TRUE(res); 2612 } 2613 2614 /* @tc.name: encodeAsyncTest005 2615 * @tc.desc: Asynchronously encodes all bytes in the specified u8 array 2616 into the newly allocated u8 array using the Base64 encoding scheme. 2617 * @tc.type: FUNC 2618 */ 2619 HWTEST_F(NativeEngineTest, encodeAsyncTest005, testing::ext::TestSize.Level0) 2620 { 2621 HILOG_INFO("encodeAsyncTest005 start"); 2622 napi_env env = (napi_env)engine_; 2623 OHOS::Util::Base64 base64; 2624 unsigned char input[6] = {66, 97, 115, 101, 54, 52}; 2625 napi_value arrayBuffer = nullptr; 2626 void* data = nullptr; 2627 size_t arrayBufferSize = 6; 2628 napi_create_arraybuffer(env, arrayBufferSize, &data, &arrayBuffer); 2629 int ret = memcpy_s(data, sizeof(input), reinterpret_cast<void*>(input), sizeof(input)); 2630 ASSERT_EQ(0, ret); 2631 napi_value src = nullptr; 2632 napi_create_typedarray(env, napi_uint8_array, arrayBufferSize, arrayBuffer, 0, &src); 2633 2634 napi_value result = base64.Encode(env, src, OHOS::Util::Type::BASIC); 2635 bool res = false; 2636 napi_is_promise(env, result, &res); 2637 ASSERT_TRUE(res); 2638 } 2639 2640 /* @tc.name: encodeToStringAsyncTest001 2641 * @tc.desc: Asynchronously encodes the specified byte array into a String using the Base64 encoding scheme. 2642 * @tc.type: FUNC 2643 */ 2644 HWTEST_F(NativeEngineTest, encodeToStringAsyncTest001, testing::ext::TestSize.Level0) 2645 { 2646 HILOG_INFO("encodeToStringAsyncTest001 start"); 2647 napi_env env = (napi_env)engine_; 2648 OHOS::Util::Base64 base64; 2649 2650 unsigned char input[3] = {115, 49, 51}; 2651 napi_value arrayBuffer = nullptr; 2652 size_t arrayBufferSize = 3; 2653 void* data = nullptr; 2654 napi_create_arraybuffer(env, arrayBufferSize, &data, &arrayBuffer); 2655 int ret = memcpy_s(data, sizeof(input), reinterpret_cast<void*>(input), sizeof(input)); 2656 ASSERT_EQ(0, ret); 2657 napi_value src = nullptr; 2658 napi_create_typedarray(env, napi_uint8_array, arrayBufferSize, arrayBuffer, 0, &src); 2659 napi_value result = base64.EncodeToString(env, src, OHOS::Util::Type::BASIC); 2660 bool res = false; 2661 napi_is_promise(env, result, &res); 2662 ASSERT_TRUE(res); 2663 2664 napi_value src1 = nullptr; 2665 napi_create_typedarray(env, napi_uint16_array, arrayBufferSize, arrayBuffer, 0, &src1); 2666 napi_value result1 = base64.EncodeToString(env, src1, OHOS::Util::Type::BASIC); 2667 ASSERT_EQ(result1, nullptr); 2668 2669 napi_value exception; 2670 napi_get_and_clear_last_exception(env, &exception); 2671 } 2672 2673 /* @tc.name: encodeToStringAsyncTest002 2674 * @tc.desc: Asynchronously encodes the specified byte array into a String using the Base64 encoding scheme. 2675 * @tc.type: FUNC 2676 */ 2677 HWTEST_F(NativeEngineTest, encodeToStringAsyncTest002, testing::ext::TestSize.Level0) 2678 { 2679 HILOG_INFO("encodeToStringAsyncTest002 start"); 2680 napi_env env = (napi_env)engine_; 2681 OHOS::Util::Base64 base64; 2682 unsigned char input[14] = {66, 97, 115, 101, 54, 52, 32, 78, 111, 100, 101, 46, 106, 115}; 2683 napi_value arrayBuffer = nullptr; 2684 void* data = nullptr; 2685 size_t arrayBufferSize = 14; 2686 napi_create_arraybuffer(env, arrayBufferSize, &data, &arrayBuffer); 2687 int ret = memcpy_s(data, sizeof(input), reinterpret_cast<void*>(input), sizeof(input)); 2688 ASSERT_EQ(0, ret); 2689 napi_value src = nullptr; 2690 napi_create_typedarray(env, napi_uint8_array, arrayBufferSize, arrayBuffer, 0, &src); 2691 2692 napi_value result = base64.EncodeToString(env, src, OHOS::Util::Type::BASIC); 2693 bool res = false; 2694 napi_is_promise(env, result, &res); 2695 ASSERT_TRUE(res); 2696 } 2697 2698 /* @tc.name: encodeToStringAsyncTest003 2699 * @tc.desc: Asynchronously encodes the specified byte array into a String using the Base64 encoding scheme. 2700 * @tc.type: FUNC 2701 */ 2702 HWTEST_F(NativeEngineTest, encodeToStringAsyncTest003, testing::ext::TestSize.Level0) 2703 { 2704 HILOG_INFO("encodeToStringAsyncTest003 start"); 2705 napi_env env = (napi_env)engine_; 2706 OHOS::Util::Base64 base64; 2707 unsigned char input[26] = {66, 97, 115, 101, 54, 52, 32, 69, 110, 2708 99, 111, 100, 105, 110, 103, 32, 105, 110, 32, 78, 111, 100, 101, 46, 106, 115}; 2709 napi_value arrayBuffer = nullptr; 2710 void* data = nullptr; 2711 size_t arrayBufferSize = 26; 2712 napi_create_arraybuffer(env, arrayBufferSize, &data, &arrayBuffer); 2713 int ret = memcpy_s(data, sizeof(input), reinterpret_cast<void*>(input), sizeof(input)); 2714 ASSERT_EQ(0, ret); 2715 napi_value src = nullptr; 2716 napi_create_typedarray(env, napi_uint8_array, arrayBufferSize, arrayBuffer, 0, &src); 2717 2718 napi_value result = base64.EncodeToString(env, src, OHOS::Util::Type::BASIC); 2719 bool res = false; 2720 napi_is_promise(env, result, &res); 2721 ASSERT_TRUE(res); 2722 } 2723 2724 /* @tc.name: encodeToStringAsyncTest004 2725 * @tc.desc: Asynchronously encodes the specified byte array into a String using the Base64 encoding scheme. 2726 * @tc.type: FUNC 2727 */ 2728 HWTEST_F(NativeEngineTest, encodeToStringAsyncTest004, testing::ext::TestSize.Level0) 2729 { 2730 HILOG_INFO("encodeToStringAsyncTest004 start"); 2731 napi_env env = (napi_env)engine_; 2732 OHOS::Util::Base64 base64; 2733 unsigned char input[4] = {168, 174, 155, 255}; 2734 napi_value arrayBuffer = nullptr; 2735 void* data = nullptr; 2736 size_t arrayBufferSize = 4; 2737 napi_create_arraybuffer(env, arrayBufferSize, &data, &arrayBuffer); 2738 int ret = memcpy_s(data, sizeof(input), reinterpret_cast<void*>(input), sizeof(input)); 2739 ASSERT_EQ(0, ret); 2740 napi_value src = nullptr; 2741 napi_create_typedarray(env, napi_uint8_array, arrayBufferSize, arrayBuffer, 0, &src); 2742 2743 napi_value result = base64.EncodeToString(env, src, OHOS::Util::Type::BASIC); 2744 bool res = false; 2745 napi_is_promise(env, result, &res); 2746 ASSERT_TRUE(res); 2747 } 2748 2749 /* @tc.name: encodeToStringAsyncTest005 2750 * @tc.desc: Asynchronously encodes the specified byte array into a String using the Base64 encoding scheme. 2751 * @tc.type: FUNC 2752 */ 2753 HWTEST_F(NativeEngineTest, encodeToStringAsyncTest005, testing::ext::TestSize.Level0) 2754 { 2755 HILOG_INFO("encodeToStringAsyncTest005 start"); 2756 napi_env env = (napi_env)engine_; 2757 OHOS::Util::Base64 base64; 2758 unsigned char input[6] = {66, 97, 115, 101, 54, 52}; 2759 napi_value arrayBuffer = nullptr; 2760 void* data = nullptr; 2761 size_t arrayBufferSize = 6; 2762 napi_create_arraybuffer(env, arrayBufferSize, &data, &arrayBuffer); 2763 int ret = memcpy_s(data, sizeof(input), reinterpret_cast<void*>(input), sizeof(input)); 2764 ASSERT_EQ(0, ret); 2765 napi_value src = nullptr; 2766 napi_create_typedarray(env, napi_uint8_array, arrayBufferSize, arrayBuffer, 0, &src); 2767 2768 napi_value result = base64.EncodeToString(env, src, OHOS::Util::Type::BASIC); 2769 bool res = false; 2770 napi_is_promise(env, result, &res); 2771 ASSERT_TRUE(res); 2772 } 2773 2774 /* @tc.name: decodeAsyncTest001 2775 * @tc.desc: Use the Base64 encoding scheme to asynchronously decode a 2776 Base64-encoded string or input u8 array into a newly allocated u8 array. 2777 * @tc.type: FUNC 2778 */ 2779 HWTEST_F(NativeEngineTest, decodeAsyncTest001, testing::ext::TestSize.Level0) 2780 { 2781 HILOG_INFO("decodeAsyncTest001 start"); 2782 napi_env env = (napi_env)engine_; 2783 OHOS::Util::Base64 base64; 2784 2785 unsigned char input[4] = {99, 122, 69, 122}; 2786 napi_value arrayBuffer = nullptr; 2787 size_t arrayBufferSize = 4; 2788 void* data = nullptr; 2789 napi_create_arraybuffer(env, arrayBufferSize, &data, &arrayBuffer); 2790 int ret = memcpy_s(data, sizeof(input), reinterpret_cast<void*>(input), sizeof(input)); 2791 ASSERT_EQ(0, ret); 2792 napi_value src = nullptr; 2793 napi_create_typedarray(env, napi_uint8_array, arrayBufferSize, arrayBuffer, 0, &src); 2794 napi_value result = base64.Decode(env, src, OHOS::Util::Type::BASIC); 2795 bool res = false; 2796 napi_is_promise(env, result, &res); 2797 ASSERT_TRUE(res); 2798 } 2799 2800 /* @tc.name: decodeAsyncTest002 2801 * @tc.desc: Use the Base64 encoding scheme to asynchronously decode a 2802 Base64-encoded string or input u8 array into a newly allocated u8 array. 2803 * @tc.type: FUNC 2804 */ 2805 HWTEST_F(NativeEngineTest, decodeAsyncTest002, testing::ext::TestSize.Level0) 2806 { 2807 HILOG_INFO("decodeAsyncTest002 start"); 2808 napi_env env = (napi_env)engine_; 2809 OHOS::Util::Base64 base64; 2810 2811 unsigned char input[8] = {113, 75, 54, 98, 47, 119, 61, 61}; 2812 napi_value arrayBuffer = nullptr; 2813 size_t arrayBufferSize = 8; 2814 void* data = nullptr; 2815 napi_create_arraybuffer(env, arrayBufferSize, &data, &arrayBuffer); 2816 int ret = memcpy_s(data, sizeof(input), reinterpret_cast<void*>(input), sizeof(input)); 2817 ASSERT_EQ(0, ret); 2818 napi_value src = nullptr; 2819 napi_create_typedarray(env, napi_uint8_array, arrayBufferSize, arrayBuffer, 0, &src); 2820 napi_value result = base64.Decode(env, src, OHOS::Util::Type::BASIC); 2821 bool res = false; 2822 napi_is_promise(env, result, &res); 2823 ASSERT_TRUE(res); 2824 } 2825 2826 /* @tc.name: decodeAsyncTest003 2827 * @tc.desc: Use the Base64 encoding scheme to asynchronously decode a 2828 Base64-encoded string or input u8 array into a newly allocated u8 array. 2829 * @tc.type: FUNC 2830 */ 2831 HWTEST_F(NativeEngineTest, decodeAsyncTest003, testing::ext::TestSize.Level0) 2832 { 2833 HILOG_INFO("decodeAsyncTest003 start"); 2834 napi_env env = (napi_env)engine_; 2835 OHOS::Util::Base64 base64; 2836 2837 std::string input = "czEz"; 2838 napi_value src = nullptr; 2839 napi_create_string_utf8(env, input.c_str(), input.size(), &src); 2840 napi_value result = base64.Decode(env, src, OHOS::Util::Type::BASIC); 2841 bool res = false; 2842 napi_is_promise(env, result, &res); 2843 ASSERT_TRUE(res); 2844 } 2845 2846 /* @tc.name: decodeAsyncTest004 2847 * @tc.desc: Use the Base64 encoding scheme to asynchronously decode a 2848 Base64-encoded string or input u8 array into a newly allocated u8 array. 2849 * @tc.type: FUNC 2850 */ 2851 HWTEST_F(NativeEngineTest, decodeAsyncTest004, testing::ext::TestSize.Level0) 2852 { 2853 HILOG_INFO("decodeAsyncTest004 start"); 2854 napi_env env = (napi_env)engine_; 2855 OHOS::Util::Base64 base64; 2856 2857 std::string input = "QmFzZTY0IEVuY29kaW5nIGluIE5vZGUuanM="; 2858 napi_value src = nullptr; 2859 napi_create_string_utf8(env, input.c_str(), input.size(), &src); 2860 napi_value result = base64.Decode(env, src, OHOS::Util::Type::BASIC); 2861 bool res = false; 2862 napi_is_promise(env, result, &res); 2863 ASSERT_TRUE(res); 2864 } 2865 2866 /* @tc.name: decodeAsyncTest005 2867 * @tc.desc: Use the Base64 encoding scheme to asynchronously decode a 2868 Base64-encoded string or input u8 array into a newly allocated u8 array. 2869 * @tc.type: FUNC 2870 */ 2871 HWTEST_F(NativeEngineTest, decodeAsyncTest005, testing::ext::TestSize.Level0) 2872 { 2873 HILOG_INFO("decodeAsyncTest005 start"); 2874 napi_env env = (napi_env)engine_; 2875 OHOS::Util::Base64 base64; 2876 2877 std::string input = "qK6b/w=="; 2878 napi_value src = nullptr; 2879 napi_create_string_utf8(env, input.c_str(), input.size(), &src); 2880 napi_value result = base64.Decode(env, src, OHOS::Util::Type::BASIC); 2881 bool res = false; 2882 napi_is_promise(env, result, &res); 2883 ASSERT_TRUE(res); 2884 } 2885 2886 /* @tc.name: decodeAsyncTest006 2887 * @tc.desc: Use the Base64 encoding scheme to asynchronously decode a 2888 Base64-encoded string or input u8 array into a newly allocated u8 array. 2889 * @tc.type: FUNC 2890 */ 2891 HWTEST_F(NativeEngineTest, decodeAsyncTest006, testing::ext::TestSize.Level0) 2892 { 2893 HILOG_INFO("decodeAsyncTest006 start"); 2894 napi_env env = (napi_env)engine_; 2895 OHOS::Util::Base64 base64; 2896 2897 napi_value src = nullptr; 2898 napi_value result = base64.Decode(env, src, OHOS::Util::Type::BASIC); 2899 ASSERT_EQ(nullptr, result); 2900 2901 std::string input1 = ""; 2902 napi_value src1 = nullptr; 2903 napi_create_string_utf8(env, input1.c_str(), input1.size(), &src1); 2904 napi_value result1 = base64.Decode(env, src1, OHOS::Util::Type::BASIC); 2905 ASSERT_EQ(nullptr, result1); 2906 napi_value exception; 2907 napi_get_and_clear_last_exception(env, &exception); 2908 2909 napi_value arrayBuffer = nullptr; 2910 size_t arrayBufferSize = 0; 2911 void* data = nullptr; 2912 napi_create_arraybuffer(env, arrayBufferSize, &data, &arrayBuffer); 2913 napi_value src2 = nullptr; 2914 napi_create_typedarray(env, napi_uint8_array, arrayBufferSize, arrayBuffer, 0, &src2); 2915 napi_value result2 = base64.Decode(env, src2, OHOS::Util::Type::BASIC); 2916 ASSERT_EQ(nullptr, result2); 2917 2918 napi_get_and_clear_last_exception(env, &exception); 2919 } 2920 2921 /** 2922 * @tc.name: stringDecoderWrite001 2923 * @tc.desc: Test the write function with complete data. 2924 * @tc.type: FUNC 2925 */ 2926 HWTEST_F(NativeEngineTest, stringDecoderWrite001, testing::ext::TestSize.Level0) 2927 { 2928 OHOS::Util::StringDecoder stringDecoder("utf-8"); 2929 napi_env env = (napi_env)engine_; 2930 const int arrCount = 6; 2931 size_t byteLength = arrCount; 2932 void* data = nullptr; 2933 napi_value resultBuff = nullptr; 2934 unsigned char arr[arrCount] = {0xE4, 0xBD, 0xA0, 0xE5, 0xA5, 0xBD}; 2935 napi_create_arraybuffer(env, byteLength, &data, &resultBuff); 2936 int ret = memcpy_s(data, sizeof(arr), reinterpret_cast<void*>(arr), sizeof(arr)); 2937 ASSERT_EQ(0, ret); 2938 napi_value result = nullptr; 2939 napi_create_typedarray(env, napi_int8_array, byteLength, resultBuff, 0, &result); 2940 napi_value testRes = stringDecoder.Write(env, result); 2941 size_t bufferSize = 0; 2942 if (napi_get_value_string_utf8(env, testRes, nullptr, 0, &bufferSize) != napi_ok) { 2943 HILOG_ERROR("can not get arg size"); 2944 } 2945 std::string buffer = ""; 2946 buffer.reserve(bufferSize); 2947 buffer.resize(bufferSize); 2948 if (napi_get_value_string_utf8(env, testRes, buffer.data(), bufferSize + 1, &bufferSize) != napi_ok) { 2949 HILOG_ERROR("can not get arg value"); 2950 } 2951 ASSERT_STREQ("你好", buffer.c_str()); 2952 2953 napi_value testResEnd = stringDecoder.End(env); 2954 size_t bufferEndSizeEnd = 0; 2955 if (napi_get_value_string_utf8(env, testResEnd, nullptr, 0, &bufferEndSizeEnd) != napi_ok) { 2956 HILOG_ERROR("can not get arg size"); 2957 } 2958 std::string bufferEnd = ""; 2959 bufferEnd.reserve(bufferEndSizeEnd); 2960 bufferEnd.resize(bufferEndSizeEnd); 2961 if (napi_get_value_string_utf8( 2962 env, testResEnd, bufferEnd.data(), bufferEndSizeEnd + 1, &bufferEndSizeEnd) != napi_ok) { 2963 HILOG_ERROR("can not get arg value"); 2964 } 2965 ASSERT_STREQ("", bufferEnd.c_str()); 2966 } 2967 2968 /** 2969 * @tc.name: stringDecoderWrite002 2970 * @tc.desc: Test the write function by splitting the complete data into two parts. 2971 * @tc.type: FUNC 2972 */ 2973 HWTEST_F(NativeEngineTest, stringDecoderWrite002, testing::ext::TestSize.Level0) 2974 { 2975 OHOS::Util::StringDecoder stringDecoder("utf-8"); 2976 napi_env env = (napi_env)engine_; 2977 const int arrCount = 2; 2978 size_t byteLength = arrCount; 2979 void* data = nullptr; 2980 napi_value resultBuff = nullptr; 2981 unsigned char arr[arrCount] = {0xE4, 0xBD}; 2982 napi_create_arraybuffer(env, byteLength, &data, &resultBuff); 2983 int ret = memcpy_s(data, sizeof(arr), reinterpret_cast<void*>(arr), sizeof(arr)); 2984 ASSERT_EQ(0, ret); 2985 napi_value result = nullptr; 2986 napi_create_typedarray(env, napi_int8_array, byteLength, resultBuff, 0, &result); 2987 napi_value testRes = stringDecoder.Write(env, result); 2988 size_t bufferSize = 0; 2989 if (napi_get_value_string_utf8(env, testRes, nullptr, 0, &bufferSize) != napi_ok) { 2990 HILOG_ERROR("can not get arg size"); 2991 } 2992 std::string buffer = ""; 2993 buffer.reserve(bufferSize); 2994 buffer.resize(bufferSize); 2995 if (napi_get_value_string_utf8(env, testRes, buffer.data(), bufferSize + 1, &bufferSize) != napi_ok) { 2996 HILOG_ERROR("can not get arg value"); 2997 } 2998 ASSERT_STREQ("", buffer.c_str()); 2999 3000 const int count = 4; 3001 byteLength = count; 3002 data = nullptr; 3003 resultBuff = nullptr; 3004 unsigned char uint8[count] = {0xA0, 0xE5, 0xA5, 0xBD}; 3005 napi_create_arraybuffer(env, byteLength, &data, &resultBuff); 3006 ret = memcpy_s(data, sizeof(uint8), reinterpret_cast<void*>(uint8), sizeof(uint8)); 3007 ASSERT_EQ(0, ret); 3008 result = nullptr; 3009 napi_create_typedarray(env, napi_int8_array, byteLength, resultBuff, 0, &result); 3010 testRes = stringDecoder.Write(env, result); 3011 bufferSize = 0; 3012 if (napi_get_value_string_utf8(env, testRes, nullptr, 0, &bufferSize) != napi_ok) { 3013 HILOG_ERROR("can not get arg size"); 3014 } 3015 buffer = ""; 3016 buffer.reserve(bufferSize); 3017 buffer.resize(bufferSize); 3018 if (napi_get_value_string_utf8(env, testRes, buffer.data(), bufferSize + 1, &bufferSize) != napi_ok) { 3019 HILOG_ERROR("can not get arg value"); 3020 } 3021 ASSERT_STREQ("你好", buffer.c_str()); 3022 } 3023 3024 /** 3025 * @tc.name: stringDecoderWrite003 3026 * @tc.desc: Test the write function with not typedarray. 3027 * @tc.type: FUNC 3028 */ 3029 HWTEST_F(NativeEngineTest, stringDecoderWrite003, testing::ext::TestSize.Level0) 3030 { 3031 OHOS::Util::StringDecoder stringDecoder("utf-8"); 3032 napi_env env = (napi_env)engine_; 3033 napi_value testRes = stringDecoder.Write(env, nullptr); 3034 ASSERT_EQ(testRes, nullptr); 3035 3036 napi_value exception; 3037 napi_get_and_clear_last_exception(env, &exception); 3038 } 3039 3040 /** 3041 * @tc.name: stringDecoderEnd001 3042 * @tc.desc: Test the end function by splitting the complete data into two parts. 3043 * @tc.type: FUNC 3044 */ 3045 HWTEST_F(NativeEngineTest, stringDecoderEnd001, testing::ext::TestSize.Level0) 3046 { 3047 OHOS::Util::StringDecoder stringDecoder("utf-8"); 3048 napi_env env = (napi_env)engine_; 3049 const int arrCount = 2; 3050 size_t byteLength = arrCount; 3051 void* data = nullptr; 3052 napi_value resultBuff = nullptr; 3053 unsigned char arr[arrCount] = {0xE4, 0xBD}; 3054 napi_create_arraybuffer(env, byteLength, &data, &resultBuff); 3055 int ret = memcpy_s(data, sizeof(arr), reinterpret_cast<void*>(arr), sizeof(arr)); 3056 ASSERT_EQ(0, ret); 3057 napi_value result = nullptr; 3058 napi_create_typedarray(env, napi_int8_array, byteLength, resultBuff, 0, &result); 3059 napi_value testRes = stringDecoder.Write(env, result); 3060 size_t bufferSize = 0; 3061 if (napi_get_value_string_utf8(env, testRes, nullptr, 0, &bufferSize) != napi_ok) { 3062 HILOG_ERROR("can not get arg size"); 3063 } 3064 std::string buffer = ""; 3065 buffer.reserve(bufferSize); 3066 buffer.resize(bufferSize); 3067 if (napi_get_value_string_utf8(env, testRes, buffer.data(), bufferSize + 1, &bufferSize) != napi_ok) { 3068 HILOG_ERROR("can not get arg value"); 3069 } 3070 ASSERT_STREQ("", buffer.c_str()); 3071 3072 const int count = 4; 3073 byteLength = count; 3074 data = nullptr; 3075 resultBuff = nullptr; 3076 unsigned char uint8[count] = {0xA0, 0xE5, 0xA5, 0xBD}; 3077 napi_create_arraybuffer(env, byteLength, &data, &resultBuff); 3078 ret = memcpy_s(data, sizeof(uint8), reinterpret_cast<void*>(uint8), sizeof(uint8)); 3079 ASSERT_EQ(0, ret); 3080 result = nullptr; 3081 napi_create_typedarray(env, napi_int8_array, byteLength, resultBuff, 0, &result); 3082 testRes = stringDecoder.End(env, result); 3083 bufferSize = 0; 3084 if (napi_get_value_string_utf8(env, testRes, nullptr, 0, &bufferSize) != napi_ok) { 3085 HILOG_ERROR("can not get arg size"); 3086 } 3087 buffer = ""; 3088 buffer.reserve(bufferSize); 3089 buffer.resize(bufferSize); 3090 if (napi_get_value_string_utf8(env, testRes, buffer.data(), bufferSize + 1, &bufferSize) != napi_ok) { 3091 HILOG_ERROR("can not get arg value"); 3092 } 3093 ASSERT_STREQ("你好", buffer.c_str()); 3094 } 3095 3096 /** 3097 * @tc.name: stringDecoderEnd002 3098 * @tc.desc: Test the end function by splitting the complete data into two parts. 3099 * @tc.type: FUNC 3100 */ 3101 HWTEST_F(NativeEngineTest, stringDecoderEnd002, testing::ext::TestSize.Level0) 3102 { 3103 OHOS::Util::StringDecoder stringDecoder("utf-8"); 3104 napi_env env = (napi_env)engine_; 3105 const int arrCount = 3; 3106 size_t byteLength = arrCount; 3107 void* data = nullptr; 3108 napi_value resultBuff = nullptr; 3109 unsigned char arr[arrCount] = {0xE4, 0xBD, 0xA0}; 3110 napi_create_arraybuffer(env, byteLength, &data, &resultBuff); 3111 int ret = memcpy_s(data, sizeof(arr), reinterpret_cast<void*>(arr), sizeof(arr)); 3112 ASSERT_EQ(0, ret); 3113 napi_value result = nullptr; 3114 napi_create_typedarray(env, napi_int8_array, byteLength, resultBuff, 0, &result); 3115 napi_value testRes = stringDecoder.Write(env, result); 3116 size_t bufferSize = 0; 3117 if (napi_get_value_string_utf8(env, testRes, nullptr, 0, &bufferSize) != napi_ok) { 3118 HILOG_ERROR("can not get arg size"); 3119 } 3120 std::string buffer = ""; 3121 buffer.reserve(bufferSize); 3122 buffer.resize(bufferSize); 3123 if (napi_get_value_string_utf8(env, testRes, buffer.data(), bufferSize + 1, &bufferSize) != napi_ok) { 3124 HILOG_ERROR("can not get arg value"); 3125 } 3126 ASSERT_STREQ("你", buffer.c_str()); 3127 testRes = stringDecoder.End(env); 3128 bufferSize = 0; 3129 if (napi_get_value_string_utf8(env, testRes, nullptr, 0, &bufferSize) != napi_ok) { 3130 HILOG_ERROR("can not get arg size"); 3131 } 3132 buffer = ""; 3133 buffer.reserve(bufferSize); 3134 buffer.resize(bufferSize); 3135 if (napi_get_value_string_utf8(env, testRes, buffer.data(), bufferSize + 1, &bufferSize) != napi_ok) { 3136 HILOG_ERROR("can not get arg value"); 3137 } 3138 ASSERT_STREQ("", buffer.c_str()); 3139 } 3140 3141 /** 3142 * @tc.name: stringDecoderEnd003 3143 * @tc.desc: string decoder end with pending len 0. 3144 * @tc.type: FUNC 3145 */ 3146 HWTEST_F(NativeEngineTest, stringDecoderEnd003, testing::ext::TestSize.Level0) 3147 { 3148 OHOS::Util::StringDecoder stringDecoder("utf-8"); 3149 napi_env env = (napi_env)engine_; 3150 const int arrCount = 6; 3151 size_t byteLength = arrCount; 3152 void* data = nullptr; 3153 napi_value resultBuff = nullptr; 3154 unsigned char arr[arrCount] = {0xE4, 0xBD, 0xA0, 0xE5, 0xA5, 0xBD}; 3155 napi_create_arraybuffer(env, byteLength, &data, &resultBuff); 3156 memcpy_s(data, sizeof(arr), reinterpret_cast<void*>(arr), sizeof(arr)); 3157 napi_value result = nullptr; 3158 napi_create_typedarray(env, napi_int8_array, byteLength, resultBuff, 0, &result); 3159 stringDecoder.Write(env, result, false); 3160 3161 napi_value testResEnd = stringDecoder.End(env); 3162 size_t bufferEndSizeEnd = 0; 3163 if (napi_get_value_string_utf8(env, testResEnd, nullptr, 0, &bufferEndSizeEnd) != napi_ok) { 3164 HILOG_ERROR("can not get arg size"); 3165 } 3166 std::string bufferEnd = ""; 3167 bufferEnd.reserve(bufferEndSizeEnd); 3168 bufferEnd.resize(bufferEndSizeEnd); 3169 if (napi_get_value_string_utf8( 3170 env, testResEnd, bufferEnd.data(), bufferEndSizeEnd + 1, &bufferEndSizeEnd) != napi_ok) { 3171 HILOG_ERROR("can not get arg value"); 3172 } 3173 ASSERT_STREQ("", bufferEnd.c_str()); 3174 } 3175 3176 /** 3177 * @tc.name: charEncodeAchieves001 3178 * @tc.desc: char encode achieves with throw error. 3179 * @tc.type: FUNC 3180 */ 3181 HWTEST_F(NativeEngineTest, charEncodeAchieves001, testing::ext::TestSize.Level0) 3182 { 3183 napi_env env = (napi_env)engine_; 3184 OHOS::Util::EncodeInfo* stdEncodeInfo = nullptr; 3185 stdEncodeInfo = new OHOS::Util::EncodeInfo(); 3186 stdEncodeInfo->slength = 0; 3187 unsigned char* res = OHOS::Util::EncodeAchieves(env, stdEncodeInfo); 3188 ASSERT_EQ(nullptr, res); 3189 3190 OHOS::Util::EncodeInfo* stdEncodeInfo1 = nullptr; 3191 unsigned char arr[] = {0xE4, 0xBD, 0xA0, 0xE5, 0xA5, 0xBD}; 3192 stdEncodeInfo1 = new OHOS::Util::EncodeInfo(); 3193 stdEncodeInfo1->sinputEncode = arr; 3194 stdEncodeInfo1->slength = 1; 3195 stdEncodeInfo1->valueType = OHOS::Util::Type::BASIC_URL_SAFE; 3196 unsigned char* res1 = OHOS::Util::EncodeAchieves(env, stdEncodeInfo1); 3197 ASSERT_EQ(0x35, static_cast<unsigned char>(*res1)); 3198 3199 napi_value exception; 3200 napi_get_and_clear_last_exception(env, &exception); 3201 } 3202 3203 /* @tc.name: testDecode001 3204 * @tc.desc: Test for abnormal situations in the decode function 3205 * @tc.type: FUNC 3206 */ 3207 HWTEST_F(NativeEngineTest, testDecode001, testing::ext::TestSize.Level0) 3208 { 3209 HILOG_INFO("testDecode001 start"); 3210 napi_env env = (napi_env)engine_; 3211 OHOS::Util::Base64 base64; 3212 3213 unsigned char input[4] = {99, 122, 69, 122}; 3214 napi_value arrayBuffer = nullptr; 3215 size_t arrayBufferSize = 4; 3216 void* data = nullptr; 3217 napi_create_arraybuffer(env, arrayBufferSize, &data, &arrayBuffer); 3218 int ret = memcpy_s(data, sizeof(input), reinterpret_cast<void*>(input), sizeof(input)); 3219 ASSERT_EQ(0, ret); 3220 napi_value src = nullptr; 3221 napi_create_typedarray(env, napi_int8_array, arrayBufferSize, arrayBuffer, 0, &src); 3222 napi_value result = base64.Decode(env, src, OHOS::Util::Type::BASIC); 3223 ASSERT_TRUE(result == nullptr); 3224 } 3225 3226 /* @tc.name: testDecode002 3227 * @tc.desc: Test for abnormal situations in the decode function 3228 * @tc.type: FUNC 3229 */ 3230 HWTEST_F(NativeEngineTest, testDecode002, testing::ext::TestSize.Level0) 3231 { 3232 HILOG_INFO("testDecode002 start"); 3233 napi_env env = (napi_env)engine_; 3234 OHOS::Util::Base64 base64; 3235 napi_value src = nullptr; 3236 napi_create_int32(env, 9, &src); 3237 napi_value result = base64.Decode(env, src, OHOS::Util::Type::BASIC); 3238 ASSERT_TRUE(result == nullptr); 3239 } 3240 3241 /* @tc.name: testDecodeSync001 3242 * @tc.desc: Test for abnormal situations in the DecodeSync function 3243 * @tc.type: FUNC 3244 */ 3245 HWTEST_F(NativeEngineTest, testDecodeSync001, testing::ext::TestSize.Level0) 3246 { 3247 HILOG_INFO("testDecodeSync001 start"); 3248 napi_env env = (napi_env)engine_; 3249 OHOS::Util::Base64 base64; 3250 3251 napi_value src = nullptr; 3252 napi_create_int32(env, 9, &src); 3253 napi_value result = base64.DecodeSync(env, src, OHOS::Util::Type::BASIC); 3254 ASSERT_TRUE(result == nullptr); 3255 } 3256 3257 /** 3258 * @tc.name: charDecodeAchieves001 3259 * @tc.desc: char dencode achieves with throw error. 3260 * @tc.type: FUNC 3261 */ 3262 HWTEST_F(NativeEngineTest, charDencodeAchieves001, testing::ext::TestSize.Level0) 3263 { 3264 napi_env env = (napi_env)engine_; 3265 OHOS::Util::DecodeInfo* stdDecodeInfo2 = nullptr; 3266 char arr2[] = {0xE4, 0xBD, 0xA0, 0xE5, 0xA5, 0xBD}; 3267 stdDecodeInfo2 = new OHOS::Util::DecodeInfo(); 3268 stdDecodeInfo2->sinputDecode = arr2; 3269 stdDecodeInfo2->slength = 2; 3270 stdDecodeInfo2->valueType = OHOS::Util::Type::BASIC_URL_SAFE; 3271 unsigned char* res2 = OHOS::Util::DecodeAchieves(env, stdDecodeInfo2); 3272 ASSERT_EQ(0, static_cast<const char>(*res2)); 3273 3274 OHOS::Util::DecodeInfo* stdDecodeInfo3 = nullptr; 3275 char arr3[] = {0xE4, 0xBD, 0xA0, 0xE5, 0xA5, 0xBD}; 3276 stdDecodeInfo3 = new OHOS::Util::DecodeInfo(); 3277 stdDecodeInfo3->sinputDecode = arr3; 3278 stdDecodeInfo3->slength = 3; 3279 stdDecodeInfo3->valueType = OHOS::Util::Type::BASIC_URL_SAFE; 3280 unsigned char* res3 = OHOS::Util::DecodeAchieves(env, stdDecodeInfo3); 3281 ASSERT_EQ(0, static_cast<unsigned char>(*res3)); 3282 3283 napi_value exception; 3284 napi_get_and_clear_last_exception(env, &exception); 3285 } 3286 3287 /** 3288 * @tc.name: DecodeToStringNoStream001 3289 * @tc.desc: Testing the ignoreBOM of TextDecoder. 3290 * @tc.type: FUNC 3291 */ 3292 HWTEST_F(NativeEngineTest, DecodeToStringNoStream001, testing::ext::TestSize.Level0) 3293 { 3294 HILOG_INFO("DecodeToStringNoStream start"); 3295 napi_env env = (napi_env)engine_; 3296 std::vector<int> inputVec; 3297 int fatal = 1; 3298 int ignoreBOM = 1; 3299 inputVec.push_back(fatal); 3300 inputVec.push_back(ignoreBOM); 3301 std::string encoding = "utf-8"; 3302 OHOS::Util::TextDecoder textDecoder(encoding, inputVec); 3303 bool res = false; 3304 napi_value bomFlag = textDecoder.GetIgnoreBOM(env); 3305 napi_get_value_bool(env, bomFlag, &res); 3306 ASSERT_TRUE(res); 3307 res = false; 3308 napi_value fatalFlag = textDecoder.GetFatal(env); 3309 napi_get_value_bool(env, bomFlag, &res); 3310 ASSERT_TRUE(res); 3311 bool iflag = false; 3312 size_t byteLength = 6; 3313 void* data = nullptr; 3314 napi_value resultBuff = nullptr; 3315 napi_create_arraybuffer(env, byteLength, &data, &resultBuff); 3316 unsigned char arr[8] = {0xEF, 0xBB, 0xBF, 0x41, 0x42, 0x43}; 3317 int ret = memcpy_s(data, sizeof(arr), reinterpret_cast<void*>(arr), sizeof(arr)); 3318 ASSERT_EQ(0, ret); 3319 napi_value result = nullptr; 3320 napi_create_typedarray(env, napi_int8_array, byteLength, resultBuff, 0, &result); 3321 textDecoder.DecodeToString(env, result, iflag); 3322 res = false; 3323 bomFlag = textDecoder.GetIgnoreBOM(env); 3324 napi_get_value_bool(env, bomFlag, &res); 3325 ASSERT_TRUE(res); 3326 res = false; 3327 fatalFlag = textDecoder.GetFatal(env); 3328 napi_get_value_bool(env, bomFlag, &res); 3329 ASSERT_TRUE(res); 3330 } 3331 3332 /** 3333 * @tc.name: DecodeToStringWithStream001 3334 * @tc.desc: Testing the ignoreBOM of TextDecoder. 3335 * @tc.type: FUNC 3336 */ 3337 HWTEST_F(NativeEngineTest, DecodeToStringWithStream001, testing::ext::TestSize.Level0) 3338 { 3339 HILOG_INFO("DecodeToStringWithStream start"); 3340 napi_env env = (napi_env)engine_; 3341 std::vector<int> inputVec; 3342 int fatal = 1; 3343 int ignoreBOM = 1; 3344 inputVec.push_back(fatal); 3345 inputVec.push_back(ignoreBOM); 3346 std::string encoding = "utf-8"; 3347 OHOS::Util::TextDecoder textDecoder(encoding, inputVec); 3348 bool res = false; 3349 napi_value bomFlag = textDecoder.GetIgnoreBOM(env); 3350 napi_get_value_bool(env, bomFlag, &res); 3351 ASSERT_TRUE(res); 3352 res = false; 3353 napi_value fatalFlag = textDecoder.GetFatal(env); 3354 napi_get_value_bool(env, bomFlag, &res); 3355 ASSERT_TRUE(res); 3356 bool iflag = true; 3357 size_t byteLength = 6; 3358 void* data = nullptr; 3359 napi_value resultBuff = nullptr; 3360 napi_create_arraybuffer(env, byteLength, &data, &resultBuff); 3361 unsigned char arr[8] = {0xEF, 0xBB, 0xBF, 0x41, 0x42, 0x43}; 3362 int ret = memcpy_s(data, sizeof(arr), reinterpret_cast<void*>(arr), sizeof(arr)); 3363 ASSERT_EQ(0, ret); 3364 napi_value result = nullptr; 3365 napi_create_typedarray(env, napi_int8_array, byteLength, resultBuff, 0, &result); 3366 textDecoder.DecodeToString(env, result, iflag); 3367 res = false; 3368 bomFlag = textDecoder.GetIgnoreBOM(env); 3369 napi_get_value_bool(env, bomFlag, &res); 3370 ASSERT_TRUE(res); 3371 res = false; 3372 fatalFlag = textDecoder.GetFatal(env); 3373 napi_get_value_bool(env, bomFlag, &res); 3374 ASSERT_TRUE(res); 3375 }