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