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 18#include "napi/native_api.h" 19#include "napi/native_node_api.h" 20 21#include "converter.h" 22#include "js_blob.h" 23#include "js_buffer.h" 24#include "tools/log.h" 25 26#include <limits> 27 28#define ASSERT_CHECK_CALL(call) \ 29 { \ 30 ASSERT_EQ(call, napi_ok); \ 31 } 32 33#define ASSERT_CHECK_VALUE_TYPE(env, value, type) \ 34 { \ 35 napi_valuetype valueType = napi_undefined; \ 36 ASSERT_TRUE(value != nullptr); \ 37 ASSERT_CHECK_CALL(napi_typeof(env, value, &valueType)); \ 38 ASSERT_EQ(valueType, type); \ 39 } 40void FillZero(OHOS::buffer::Buffer *buf, size_t size) 41{ 42 for (size_t i = 0; i < size; i++) { 43 buf->Set(i, 0); 44 } 45} 46 47/** 48 * @tc.name: ConstructorTest001 49 * @tc.desc: Buffer Constructor. 50 * @tc.type: FUNC 51 * @tc.require:issueI5J5Z3 52 */ 53HWTEST_F(NativeEngineTest, ConstructorTest001, testing::ext::TestSize.Level0) 54{ 55 OHOS::buffer::Buffer *buf = new OHOS::buffer::Buffer(); 56 buf->Init(10); 57 ASSERT_EQ(buf->GetLength(), 10); 58} 59 60/** 61 * @tc.name: ConstructorTest002 62 * @tc.desc: Buffer Constructor. 63 * @tc.type: FUNC 64 * @tc.require:issueI5J5Z3 65 */ 66HWTEST_F(NativeEngineTest, ConstructorTest002, testing::ext::TestSize.Level0) 67{ 68 OHOS::buffer::Buffer *buf1 = new OHOS::buffer::Buffer(); 69 buf1->Init(10); 70 OHOS::buffer::Buffer *buf2 = new OHOS::buffer::Buffer(); 71 buf2->Init(buf1); 72 ASSERT_EQ(buf2->GetLength(), 10); 73} 74 75 76/** 77 * @tc.name: ConstructorTest003 78 * @tc.desc: Buffer Constructor. 79 * @tc.type: FUNC 80 * @tc.require:issueI5J5Z3 81 */ 82HWTEST_F(NativeEngineTest, ConstructorTest003, testing::ext::TestSize.Level0) 83{ 84 OHOS::buffer::Buffer *poolBuffer = new OHOS::buffer::Buffer(); 85 poolBuffer->Init(1024 * 8); 86 OHOS::buffer::Buffer *buf2 = new OHOS::buffer::Buffer(); 87 buf2->Init(poolBuffer, 0, 5); 88 ASSERT_EQ(buf2->GetLength(), 5); 89 ASSERT_EQ(buf2->GetByteOffset(), 0); 90 91 OHOS::buffer::Buffer *buf3 = new OHOS::buffer::Buffer(); 92 buf3->Init(poolBuffer, 5, 6); 93 ASSERT_EQ(buf3->GetLength(), 6); 94 ASSERT_EQ(buf3->GetByteOffset(), 5); 95} 96 97/** 98 * @tc.name: ConstructorTest004 99 * @tc.desc: Buffer Constructor. 100 * @tc.type: FUNC 101 * @tc.require:issueI5J5Z3 102 */ 103HWTEST_F(NativeEngineTest, ConstructorTest004, testing::ext::TestSize.Level0) 104{ 105 OHOS::buffer::Buffer *buf2 = new OHOS::buffer::Buffer(); 106 uint8_t data[4] = {1, 2, 3, 4}; 107 buf2->Init(data, 0, 4); 108 ASSERT_EQ(buf2->GetLength(), 4); 109 ASSERT_EQ(buf2->GetByteOffset(), 0); 110} 111 112/** 113 * @tc.name: ConstructorTest005 114 * @tc.desc: Buffer Constructor. 115 * @tc.type: FUNC 116 * @tc.require:issueI5J5Z3 117 */ 118HWTEST_F(NativeEngineTest, ConstructorTest005, testing::ext::TestSize.Level0) 119{ 120 OHOS::buffer::Buffer *buf = new OHOS::buffer::Buffer(); 121 buf->Init(1); 122 ASSERT_EQ(buf->GetLength(), 1); 123 uint32_t res = buf->Copy(nullptr, 0, 0, 0); 124 ASSERT_EQ(res, 0); 125 int result = buf->Compare(nullptr, 0, 0, 0); 126 ASSERT_EQ(result, 0); 127 buf->ReadBytes(nullptr, 0, 0); 128 buf->FillString("abc", 1, 0, "utf16le"); 129 buf->FillString("abc", 1, 0, "binary"); 130 buf->FillString("abc", 1, 0, "sos"); 131 buf->FillString("abc", 1, 0, "hex"); 132 std::vector<uint8_t> array; 133 buf->FillNumber(array, 0, 0); 134 OHOS::buffer::Buffer *buffer = new OHOS::buffer::Buffer(); 135 buf->FillBuffer(buffer, 0, 0); 136 buf->FillBuffer(nullptr, 1, 0); 137 buf->SetArray(array, 0); 138 result = buf->LastIndexOf(nullptr, 0, 0); 139 ASSERT_EQ(result, -1); 140 uint64_t resultIndex = 0; 141 result = buf->IndexOf(nullptr, 0, 0, resultIndex); 142 ASSERT_EQ(result, -1); 143 delete buf; 144 buf = nullptr; 145 delete buffer; 146 buffer = nullptr; 147} 148 149/** 150 * @tc.name: DestructorTest001 151 * @tc.desc: Buffer Destructor. 152 * @tc.type: FUNC 153 * @tc.require:issueI5J5Z3 154 */ 155HWTEST_F(NativeEngineTest, DestructorTest001, testing::ext::TestSize.Level0) 156{ 157 OHOS::buffer::Buffer *buf = new OHOS::buffer::Buffer(); 158 uint8_t data[4] = {1, 2, 3, 4}; 159 buf->Init(data, 0, 4); 160 ASSERT_EQ(buf->GetLength(), 4); 161 delete buf; 162 buf = nullptr; 163} 164 165/** 166 * @tc.name: GetLengthTest001 167 * @tc.desc: Get buffer Length. 168 * @tc.type: FUNC 169 * @tc.require:issueI5J5Z3 170 */ 171HWTEST_F(NativeEngineTest, GetLengthTest001, testing::ext::TestSize.Level0) 172{ 173 OHOS::buffer::Buffer *buf = new OHOS::buffer::Buffer(); 174 ASSERT_EQ(buf->GetLength(), 0); 175} 176 177/** 178 * @tc.name: GetLengthTest002 179 * @tc.desc: Get buffer Length. 180 * @tc.type: FUNC 181 * @tc.require:issueI5J5Z3 182 */ 183HWTEST_F(NativeEngineTest, GetLengthTest002, testing::ext::TestSize.Level0) 184{ 185 OHOS::buffer::Buffer *buf = new OHOS::buffer::Buffer(); 186 buf->Init(6); 187 ASSERT_EQ(buf->GetLength(), 6); 188} 189 190/** 191 * @tc.name: SetLengthTest001 192 * @tc.desc: Set buffer Length. 193 * @tc.type: FUNC 194 * @tc.require:issueI5J5Z3 195 */ 196HWTEST_F(NativeEngineTest, SetLengthTest001, testing::ext::TestSize.Level0) 197{ 198 OHOS::buffer::Buffer *buf = new OHOS::buffer::Buffer(); 199 buf->Init(6); 200 buf->SetLength(7); 201 ASSERT_EQ(buf->GetLength(), 7); 202} 203 204/** 205 * @tc.name: GetByteOffsetTest001 206 * @tc.desc: Get buffer byteOffset. 207 * @tc.type: FUNC 208 * @tc.require:issueI5J5Z3 209 */ 210HWTEST_F(NativeEngineTest, GetByteOffsetTest001, testing::ext::TestSize.Level0) 211{ 212 OHOS::buffer::Buffer *poolBuffer = new OHOS::buffer::Buffer(); 213 poolBuffer->Init(1024 * 8); 214 OHOS::buffer::Buffer *buf = new OHOS::buffer::Buffer(); 215 buf->Init(poolBuffer, 2, 5); 216 ASSERT_EQ(buf->GetByteOffset(), 2); 217} 218 219/** 220 * @tc.name: GetAndSetTest001 221 * @tc.desc: Get And Set method. 222 * @tc.type: FUNC 223 * @tc.require:issueI5J5Z3 224 */ 225HWTEST_F(NativeEngineTest, GetAndSetTest001, testing::ext::TestSize.Level0) 226{ 227 OHOS::buffer::Buffer *buf = new OHOS::buffer::Buffer(); 228 buf->Init(3); 229 buf->Set(0, 1); 230 int32_t value = buf->Get(0); 231 ASSERT_EQ(value, 1); 232} 233 234/** 235 * @tc.name: GetAndSetTest002 236 * @tc.desc: Get And Set method. 237 * @tc.type: FUNC 238 * @tc.require:issueI5J5Z3 239 */ 240HWTEST_F(NativeEngineTest, GetAndSetTest002, testing::ext::TestSize.Level0) 241{ 242 OHOS::buffer::Buffer *buf = new OHOS::buffer::Buffer(); 243 buf->Init(3); 244 buf->Set(0, 1); 245 buf->Set(1, 2); 246 buf->Set(2, 3); 247 int32_t value = buf->Get(2); 248 ASSERT_EQ(value, 3); 249} 250 251/** 252 * @tc.name: WriteInt32BEAndReadInt32BETest001 253 * @tc.desc: Writes value to buf at the specified offset as big-endian. The value must be a valid signed 32-bit integer 254 * Reads a signed, big-endian 32-bit integer from buf at the specified offset. 255 * @tc.type: FUNC 256 * @tc.require:issueI5J5Z3 257 */ 258HWTEST_F(NativeEngineTest, WriteInt32BEAndReadInt32BETest001, testing::ext::TestSize.Level0) 259{ 260 OHOS::buffer::Buffer *buf = new OHOS::buffer::Buffer(); 261 buf->Init(4); 262 FillZero(buf, 4); 263 buf->WriteInt32BE(0x12345678, 0); 264 int32_t res = buf->ReadInt32BE(0); 265 ASSERT_EQ(res, 0x12345678); 266} 267 268/** 269 * @tc.name: WriteInt32BEAndReadInt32BETest002 270 * @tc.desc: Writes value to buf at the specified offset as big-endian. The value must be a valid signed 32-bit integer 271 * Reads a signed, big-endian 32-bit integer from buf at the specified offset. 272 * @tc.type: FUNC 273 * @tc.require:issueI5J5Z3 274 */ 275HWTEST_F(NativeEngineTest, WriteInt32BEAndReadInt32BETest002, testing::ext::TestSize.Level0) 276{ 277 OHOS::buffer::Buffer *buf = new OHOS::buffer::Buffer(); 278 buf->Init(5); 279 FillZero(buf, 5); 280 buf->WriteInt32BE(0x12345678, 1); 281 int32_t res = buf->ReadInt32BE(1); 282 ASSERT_EQ(res, 0x12345678); 283} 284 285/** 286 * @tc.name: WriteInt32BEAndReadInt32BETest003 287 * @tc.desc: Writes value to buf at the specified offset as big-endian. The value must be a valid signed 32-bit integer 288 * Reads a signed, big-endian 32-bit integer from buf at the specified offset. 289 * @tc.type: FUNC 290 * @tc.require:issueI5J5Z3 291 */ 292HWTEST_F(NativeEngineTest, WriteInt32BEAndReadInt32BETest003, testing::ext::TestSize.Level0) 293{ 294 OHOS::buffer::Buffer *buf = new OHOS::buffer::Buffer(); 295 buf->Init(5); 296 FillZero(buf, 5); 297 buf->WriteInt32BE(0x12345678, 1); 298 int32_t res = buf->ReadInt32BE(1); 299 ASSERT_EQ(res, 0x12345678); 300} 301 302/** 303 * @tc.name: WriteInt32LEAndReadInt32LETest001 304 * @tc.desc: Writes value to buf at the specified offset as little-endian. 305 * The value must be a valid signed 32-bit integer. 306 * Reads a signed, little-endian 32-bit integer from buf at the specified offset. 307 * @tc.type: FUNC 308 * @tc.require:issueI5J5Z3 309 */ 310HWTEST_F(NativeEngineTest, WriteInt32LEAndReadInt32LETest001, testing::ext::TestSize.Level0) 311{ 312 OHOS::buffer::Buffer *buf = new OHOS::buffer::Buffer(); 313 buf->Init(4); 314 FillZero(buf, 4); 315 buf->WriteInt32LE(0x12345678, 0); 316 int32_t res = buf->ReadInt32LE(0); 317 ASSERT_EQ(res, 0x12345678); 318 res = buf->ReadInt32BE(0); 319 ASSERT_EQ(res, 0x78563412); 320} 321 322/** 323 * @tc.name: WriteInt32LEAndReadInt32LETest002 324 * @tc.desc: Writes value to buf at the specified offset as little-endian. 325 * The value must be a valid signed 32-bit integer. 326 * Reads a signed, little-endian 32-bit integer from buf at the specified offset. 327 * @tc.type: FUNC 328 * @tc.require:issueI5J5Z3 329 */ 330HWTEST_F(NativeEngineTest, WriteInt32LEAndReadInt32LETest002, testing::ext::TestSize.Level0) 331{ 332 OHOS::buffer::Buffer *buf = new OHOS::buffer::Buffer(); 333 buf->Init(5); 334 FillZero(buf, 5); 335 buf->WriteInt32LE(0x12345678, 1); 336 int32_t res = buf->ReadInt32LE(0); 337 ASSERT_EQ(res, 0x34567800); 338} 339 340/** 341 * @tc.name: WriteInt32LEAndReadInt32LETest003 342 * @tc.desc: Writes value to buf at the specified offset as little-endian. 343 * The value must be a valid signed 32-bit integer. 344 * Reads a signed, little-endian 32-bit integer from buf at the specified offset. 345 * @tc.type: FUNC 346 * @tc.require:issueI5J5Z3 347 */ 348HWTEST_F(NativeEngineTest, WriteInt32LEAndReadInt32LETest003, testing::ext::TestSize.Level0) 349{ 350 OHOS::buffer::Buffer *buf = new OHOS::buffer::Buffer(); 351 buf->Init(5); 352 FillZero(buf, 5); 353 buf->WriteInt32LE(0x12345678, 1); 354 int32_t res = buf->ReadInt32LE(1); 355 ASSERT_EQ(res, 0x12345678); 356} 357 358/** 359 * @tc.name: WriteUInt32BEAndReadUInt32BETest001 360 * @tc.desc: Writes value to buf at the specified offset as big-endian. 361 * The value must be a valid unsigned 32-bit integer. 362 * Reads an unsigned, big-endian 32-bit integer from buf at the specified offset. 363 * @tc.type: FUNC 364 * @tc.require:issueI5J5Z3 365 */ 366HWTEST_F(NativeEngineTest, WriteUInt32BEAndReadUInt32BETest001, testing::ext::TestSize.Level0) 367{ 368 OHOS::buffer::Buffer *buf = new OHOS::buffer::Buffer(); 369 buf->Init(4); 370 FillZero(buf, 4); 371 buf->WriteUInt32BE(0x12345678, 0); 372 int32_t res = buf->ReadUInt32BE(0); 373 ASSERT_EQ(res, 0x12345678); 374} 375 376/** 377 * @tc.name: WriteUInt32BEAndReadUInt32BETest002 378 * @tc.desc: Writes value to buf at the specified offset as big-endian. 379 * The value must be a valid unsigned 32-bit integer. 380 * Reads an unsigned, big-endian 32-bit integer from buf at the specified offset. 381 * @tc.type: FUNC 382 * @tc.require:issueI5J5Z3 383 */ 384HWTEST_F(NativeEngineTest, WriteUInt32BEAndReadUInt32BETest002, testing::ext::TestSize.Level0) 385{ 386 OHOS::buffer::Buffer *buf = new OHOS::buffer::Buffer(); 387 buf->Init(5); 388 FillZero(buf, 5); 389 buf->WriteUInt32BE(0x12345678, 1); 390 int32_t res = buf->ReadUInt32BE(0); 391 ASSERT_EQ(res, 0x123456); 392} 393 394/** 395 * @tc.name: WriteUInt32BEAndReadUInt32BETest003 396 * @tc.desc: Writes value to buf at the specified offset as big-endian. 397 * The value must be a valid unsigned 32-bit integer. 398 * Reads an unsigned, big-endian 32-bit integer from buf at the specified offset. 399 * @tc.type: FUNC 400 * @tc.require:issueI5J5Z3 401 */ 402HWTEST_F(NativeEngineTest, WriteUInt32BEAndReadUInt32BETest003, testing::ext::TestSize.Level0) 403{ 404 OHOS::buffer::Buffer *buf = new OHOS::buffer::Buffer(); 405 buf->Init(5); 406 FillZero(buf, 5); 407 buf->WriteUInt32BE(0x12345678, 1); 408 int32_t res = buf->ReadUInt32BE(1); 409 ASSERT_EQ(res, 0x12345678); 410} 411 412/** 413 * @tc.name: WriteUInt32LEAndReadUInt32LETest001 414 * @tc.desc: Writes value to buf at the specified offset as little-endian. 415 * The value must be a valid unsigned 32-bit integer. 416 * Reads an unsigned, little-endian 32-bit integer from buf at the specified offset. 417 * @tc.type: FUNC 418 * @tc.require:issueI5J5Z3 419 */ 420HWTEST_F(NativeEngineTest, WriteUInt32LEAndReadUInt32LETest001, testing::ext::TestSize.Level0) 421{ 422 OHOS::buffer::Buffer *buf = new OHOS::buffer::Buffer(); 423 buf->Init(4); 424 FillZero(buf, 4); 425 buf->WriteUInt32LE(0x12345678, 0); 426 int32_t res = buf->ReadUInt32LE(0); 427 ASSERT_EQ(res, 0x12345678); 428} 429 430/** 431 * @tc.name: WriteUInt32LEAndReadUInt32LETest002 432 * @tc.desc: Writes value to buf at the specified offset as little-endian. 433 * The value must be a valid unsigned 32-bit integer. 434 * Reads an unsigned, little-endian 32-bit integer from buf at the specified offset. 435 * @tc.type: FUNC 436 * @tc.require:issueI5J5Z3 437 */ 438HWTEST_F(NativeEngineTest, WriteUInt32LEAndReadUInt32LETest002, testing::ext::TestSize.Level0) 439{ 440 OHOS::buffer::Buffer *buf = new OHOS::buffer::Buffer(); 441 buf->Init(5); 442 FillZero(buf, 5); 443 buf->WriteUInt32LE(0x12345678, 1); 444 int32_t res = buf->ReadUInt32LE(0); 445 ASSERT_EQ(res, 0x34567800); 446} 447 448/** 449 * @tc.name: WriteUInt32LEAndReadUInt32LETest003 450 * @tc.desc: Writes value to buf at the specified offset as little-endian. 451 * The value must be a valid unsigned 32-bit integer. 452 * Reads an unsigned, little-endian 32-bit integer from buf at the specified offset. 453 * @tc.type: FUNC 454 * @tc.require:issueI5J5Z3 455 */ 456HWTEST_F(NativeEngineTest, WriteUInt32LEAndReadUInt32LETest003, testing::ext::TestSize.Level0) 457{ 458 OHOS::buffer::Buffer *buf = new OHOS::buffer::Buffer(); 459 buf->Init(5); 460 FillZero(buf, 5); 461 buf->WriteUInt32LE(0x12345678, 1); 462 int32_t res = buf->ReadUInt32LE(1); 463 ASSERT_EQ(res, 0x12345678); 464} 465 466/** 467 * @tc.name: ReadBytesTest001 468 * @tc.desc: Read value from buffer. 469 * @tc.type: FUNC 470 * @tc.require:issueI5J5Z3 471 */ 472HWTEST_F(NativeEngineTest, ReadBytesTest001, testing::ext::TestSize.Level0) 473{ 474 OHOS::buffer::Buffer *buf = new OHOS::buffer::Buffer(); 475 buf->Init(4); 476 FillZero(buf, 4); 477 buf->WriteUInt32BE(0x12345678, 0); 478 uint32_t length = buf->GetLength(); 479 uint8_t data[length]; 480 buf->ReadBytes(data, 0, length); 481 uint8_t res[4] = {0x12, 0x34, 0x56, 0x78}; 482 for (size_t i = 0; i < length; i++) { 483 ASSERT_EQ(data[i], res[i]); 484 } 485} 486 487/** 488 * @tc.name: ReadBytesTest002 489 * @tc.desc: Read value error when length is zero. 490 * @tc.type: FUNC 491 * @tc.require:issueI5J5Z3 492 */ 493HWTEST_F(NativeEngineTest, ReadBytesTest002, testing::ext::TestSize.Level0) 494{ 495 OHOS::buffer::Buffer *buf = new OHOS::buffer::Buffer(); 496 buf->Init(4); 497 FillZero(buf, 4); 498 buf->WriteUInt32BE(0x12345678, 0); 499 uint32_t length = buf->GetLength(); 500 uint8_t data[length]; 501 buf->ReadBytes(data, 0, 0); 502 uint8_t res[4] = {0x12, 0x34, 0x56, 0x78}; 503 ASSERT_NE(data[0], res[0]); 504} 505 506/** 507 * @tc.name: WriteStringTest001 508 * @tc.desc: Write string to buffer. 509 * @tc.type: FUNC 510 * @tc.require:issueI5J5Z3 511 */ 512HWTEST_F(NativeEngineTest, WriteStringTest001, testing::ext::TestSize.Level0) 513{ 514 OHOS::buffer::Buffer *buf = new OHOS::buffer::Buffer(); 515 buf->Init(10); 516 std::string str = "1234567890"; 517 unsigned int size = buf->WriteString(str, 10); 518 ASSERT_EQ(size, 10); 519} 520 521/** 522 * @tc.name: WriteStringTest002 523 * @tc.desc: Write string to buffer. 524 * @tc.type: FUNC 525 * @tc.require:issueI5J5Z3 526 */ 527HWTEST_F(NativeEngineTest, WriteStringTest002, testing::ext::TestSize.Level0) 528{ 529 OHOS::buffer::Buffer *buf = new OHOS::buffer::Buffer(); 530 buf->Init(9); 531 std::string str = "123456789"; 532 unsigned int size = buf->WriteString(str, 9); 533 uint8_t data[size]; 534 buf->ReadBytes(data, 0, size); 535 uint8_t value = 49; 536 for (size_t i = 0; i < size; i++) { 537 ASSERT_EQ(data[i], value); 538 value++; 539 } 540} 541 542/** 543 * @tc.name: WriteStringTest003 544 * @tc.desc: Write string to buffer. 545 * @tc.type: FUNC 546 * @tc.require:issueI5J5Z3 547 */ 548HWTEST_F(NativeEngineTest, WriteStringTest003, testing::ext::TestSize.Level0) 549{ 550 OHOS::buffer::Buffer *buf = new OHOS::buffer::Buffer(); 551 buf->Init(9); 552 std::string str = "123456789"; 553 unsigned int size = buf->WriteString(str, 0, 9); 554 uint8_t data[size]; 555 buf->ReadBytes(data, 0, size); 556 uint8_t value = 49; 557 for (size_t i = 0; i < size; i++) { 558 ASSERT_EQ(data[i], value); 559 value++; 560 } 561} 562 563/** 564 * @tc.name: WriteStringTest004 565 * @tc.desc: Write string to buffer. 566 * @tc.type: FUNC 567 * @tc.require:issueI5J5Z3 568 */ 569HWTEST_F(NativeEngineTest, WriteStringTest004, testing::ext::TestSize.Level0) 570{ 571 OHOS::buffer::Buffer *buf = new OHOS::buffer::Buffer(); 572 buf->Init(9); 573 std::string str = "123456789"; 574 unsigned int size = buf->WriteString(str, 2, 7); 575 uint8_t data[size]; 576 buf->ReadBytes(data, 0, size); 577 uint8_t value = 49; 578 for (size_t i = 2; i < size; i++) { 579 ASSERT_EQ(data[i], value); 580 value++; 581 } 582} 583 584/** 585 * @tc.name: WriteStringTest005 586 * @tc.desc: Write string to buffer. 587 * @tc.type: FUNC 588 * @tc.require:issueI5J5Z3 589 */ 590HWTEST_F(NativeEngineTest, WriteStringTest005, testing::ext::TestSize.Level0) 591{ 592 OHOS::buffer::Buffer *buf = new OHOS::buffer::Buffer(); 593 buf->Init(9); 594 std::string str = "123456789"; 595 unsigned int size = buf->WriteString(str, 0, 9, "utf8"); 596 uint8_t data[size]; 597 buf->ReadBytes(data, 0, size); 598 uint8_t value = 49; 599 for (size_t i = 0; i < size; i++) { 600 ASSERT_EQ(data[i], value); 601 value++; 602 } 603} 604 605/** 606 * @tc.name: WriteStringTest006 607 * @tc.desc: Write string to buffer. 608 * @tc.type: FUNC 609 * @tc.require:issueI5J5Z3 610 */ 611HWTEST_F(NativeEngineTest, WriteStringTest006, testing::ext::TestSize.Level0) 612{ 613 OHOS::buffer::Buffer *buf = new OHOS::buffer::Buffer(); 614 buf->Init(16); 615 FillZero(buf, 16); 616 std::string str = "12345678"; 617 unsigned int size = buf->WriteString(str, 0, 16, "utf16le"); 618 ASSERT_EQ(size, 16); 619 uint8_t data[size]; 620 buf->ReadBytes(data, 0, size); 621 uint8_t value = 49; 622 for (size_t i = 0; i < size; i++) { 623 if (i % 2 == 0) { 624 ASSERT_EQ(data[i], value); 625 value++; 626 } else { 627 ASSERT_EQ(data[i], 0); 628 } 629 } 630} 631 632/** 633 * @tc.name: SubBufferTest001 634 * @tc.desc: Returns a new Buffer that references the same memory as the original, 635 * but offset and cropped by the start and end indices. 636 * @tc.type: FUNC 637 * @tc.require:issueI5J5Z3 638 */ 639HWTEST_F(NativeEngineTest, SubBufferTest001, testing::ext::TestSize.Level0) 640{ 641 OHOS::buffer::Buffer *buf = new OHOS::buffer::Buffer(); 642 buf->SubBuffer(nullptr, 0, 10); 643 644 OHOS::buffer::Buffer *buf1 = new OHOS::buffer::Buffer(); 645 buf1->Init(10); 646 FillZero(buf1, 10); 647 std::string str = "1234567890"; 648 buf1->WriteString(str, 0, 10); 649 OHOS::buffer::Buffer *buf2 = new OHOS::buffer::Buffer(); 650 buf2->SubBuffer(buf1, 0, 10); 651 buf1->ReadBytesForArrayBuffer(NULL, 0); 652 653 ASSERT_EQ(buf2->GetLength(), 10); 654 uint8_t data[11]; 655 buf2->ReadBytes(data, 0, 10); 656 data[10] = 0; 657 ASSERT_STREQ(reinterpret_cast<char*>(data), str.c_str()); 658} 659 660/** 661 * @tc.name: SubBufferTest002 662 * @tc.desc: Returns a new Buffer that references the same memory as the original, 663 * but offset and cropped by the start and end indices. 664 * @tc.type: FUNC 665 * @tc.require:issueI5J5Z3 666 */ 667HWTEST_F(NativeEngineTest, SubBufferTest002, testing::ext::TestSize.Level0) 668{ 669 OHOS::buffer::Buffer *buf1 = new OHOS::buffer::Buffer(); 670 buf1->Init(10); 671 FillZero(buf1, 10); 672 std::string str = "1234567890"; 673 buf1->WriteString(str, 0, 10); 674 OHOS::buffer::Buffer *buf2 = new OHOS::buffer::Buffer(); 675 buf2->SubBuffer(buf1, 2, 10); 676 ASSERT_EQ(buf2->GetLength(), 8); 677 uint8_t data[9]; 678 buf2->ReadBytes(data, 0, 8); 679 data[8] = 0; 680 ASSERT_STREQ(reinterpret_cast<char*>(data), "34567890"); 681} 682 683/** 684 * @tc.name: CopyTest001 685 * @tc.desc: Copies data from a region of buf to a region in target, 686 * even if the target memory region overlaps with buf. 687 * @tc.type: FUNC 688 * @tc.require:issueI5J5Z3 689 */ 690HWTEST_F(NativeEngineTest, CopyTest001, testing::ext::TestSize.Level0) 691{ 692 OHOS::buffer::Buffer *buffer = new OHOS::buffer::Buffer(); 693 buffer->Init(20); 694 695 OHOS::buffer::Buffer *buf = new OHOS::buffer::Buffer(); 696 buf->Init(20); 697 buf->WriteString("this is a string", 16); 698 699 unsigned int tOffset = 1; 700 unsigned int sOffset = 0; 701 unsigned int tEnd = 16; 702 unsigned int sEnd = 16; 703 buf->Copy(buffer, tOffset, sOffset, sEnd); 704 uint8_t data[20] = {0}; 705 buffer->ReadBytes(data, tOffset, tEnd); 706 ASSERT_STREQ(reinterpret_cast<char*>(data), "this is a string"); 707} 708 709/** 710 * @tc.name: CopyTest002 711 * @tc.desc: Copies data with len 0 712 * @tc.type: FUNC 713 * @tc.require:issueI5J5Z3 714 */ 715HWTEST_F(NativeEngineTest, CopyTest002, testing::ext::TestSize.Level0) 716{ 717 OHOS::buffer::Buffer *buf = new OHOS::buffer::Buffer(); 718 buf->Init(20); 719 std::string value = "YWJjZA"; 720 ASSERT_EQ(buf->Copy(buf, 0, 2, 2), 0); 721} 722/** 723 * @tc.name: CopyTest003 724 * @tc.desc: Buffer Copy. 725 * @tc.type: FUNC 726 * @tc.require:issueI5J5Z3 727 */ 728HWTEST_F(NativeEngineTest, CopyTest003, testing::ext::TestSize.Level0) 729{ 730 OHOS::buffer::Buffer *buf = new OHOS::buffer::Buffer(); 731 uint8_t data[10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; 732 buf->Init(data, 0, 10); 733 uint32_t res = buf->Copy(buf, 0, 4, 10); 734 ASSERT_EQ(res, 6); 735 res = buf->Copy(buf, 0, 0, 0); 736 ASSERT_EQ(res, 0); 737 delete buf; 738 OHOS::buffer::Buffer *buf1 = new OHOS::buffer::Buffer(); 739 res = buf1->Copy(buf1, 0, 4, 10); 740 delete buf1; 741 ASSERT_EQ(res, 0); 742} 743 744/** 745 * @tc.name: CompareTest001 746 * @tc.desc: Compares buf with target and returns a number indicating whether buf comes before, after, 747 * or is the same as target in sort order. Comparison is based on the actual sequence of bytes in each Buffer 748 * @tc.type: FUNC 749 * @tc.require:issueI5J5Z3 750 */ 751HWTEST_F(NativeEngineTest, CompareTest001, testing::ext::TestSize.Level0) 752{ 753 OHOS::buffer::Buffer *buffer = new OHOS::buffer::Buffer(); 754 buffer->Init(20); 755 buffer->WriteString("this is a string", 16); 756 757 OHOS::buffer::Buffer *buf = new OHOS::buffer::Buffer(); 758 buf->Init(20); 759 buf->WriteString("this is a string", 1, 16); 760 761 int result = buf->Compare(buffer, 0, 1, 16); 762 ASSERT_EQ(result, 0); 763} 764 765/** 766 * @tc.name: IndexOfTest001 767 * @tc.desc: The index of the first occurrence of value in buf. 768 * @tc.type: FUNC 769 * @tc.require:issueI5J5Z3 770 */ 771HWTEST_F(NativeEngineTest, IndexOfTest001, testing::ext::TestSize.Level0) 772{ 773 OHOS::buffer::Buffer *buf = new OHOS::buffer::Buffer(); 774 buf->Init(20); 775 buf->WriteString("this is a string", 16); 776 uint64_t resultIndex = 0; 777 int index = buf->IndexOf("is", 0, 2, resultIndex); 778 ASSERT_EQ(index, -2); 779 ASSERT_EQ(resultIndex, 2); 780} 781 782/** 783 * @tc.name: IndexOfTest002 784 * @tc.desc: The index of the first occurrence of value in buf. 785 * @tc.type: FUNC 786 * @tc.require:issueI5J5Z3 787 */ 788HWTEST_F(NativeEngineTest, IndexOfTest002, testing::ext::TestSize.Level0) 789{ 790 OHOS::buffer::Buffer *buf = new OHOS::buffer::Buffer(); 791 buf->Init(7); 792 buf->WriteString("3363333", 7); 793 uint64_t resultIndex = 0; 794 int index = buf->IndexOf("36", 0, 2, resultIndex); 795 ASSERT_EQ(index, -2); 796 ASSERT_EQ(resultIndex, 1); 797} 798 799/** 800 * @tc.name: IndexOfTest003 801 * @tc.desc: The index of the first occurrence of value in buf. 802 * @tc.type: FUNC 803 * @tc.require:issueI5J5Z3 804 */ 805HWTEST_F(NativeEngineTest, IndexOfTest003, testing::ext::TestSize.Level0) 806{ 807 OHOS::buffer::Buffer *buf = new OHOS::buffer::Buffer(); 808 buf->Init(12); 809 buf->WriteString("322362326233", 12); 810 uint64_t resultIndex = 0; 811 int index = buf->IndexOf("2623", 0, 4, resultIndex); 812 ASSERT_EQ(index, -2); 813 ASSERT_EQ(resultIndex, 7); 814} 815 816/** 817 * @tc.name: IndexOfTest004 818 * @tc.desc: Data is not in buf. 819 * @tc.type: FUNC 820 * @tc.require:issueI5J5Z3 821 */ 822HWTEST_F(NativeEngineTest, IndexOfTest004, testing::ext::TestSize.Level0) 823{ 824 OHOS::buffer::Buffer *buf = new OHOS::buffer::Buffer(); 825 buf->Init(12); 826 buf->WriteString("322362326233", 12); 827 uint64_t resultIndex = 0; 828 int index = buf->IndexOf("99", 0, 2, resultIndex); 829 ASSERT_EQ(index, -1); 830} 831 832/** 833 * @tc.name: LastIndexOfTest001 834 * @tc.desc: The index of the last occurrence of value in buf. 835 * @tc.type: FUNC 836 * @tc.require:issueI5J5Z3 837 */ 838HWTEST_F(NativeEngineTest, LastIndexOfTest001, testing::ext::TestSize.Level0) 839{ 840 OHOS::buffer::Buffer *buf = new OHOS::buffer::Buffer(); 841 buf->Init(20); 842 buf->WriteString("this is a string", 16); 843 int index = buf->LastIndexOf("is", 0, 2); 844 ASSERT_EQ(index, 5); 845} 846 847/** 848 * @tc.name: LastIndexOfTest002 849 * @tc.desc: The index of the last occurrence of value in buf. 850 * @tc.type: FUNC 851 * @tc.require:issueI5J5Z3 852 */ 853HWTEST_F(NativeEngineTest, LastIndexOfTest002, testing::ext::TestSize.Level0) 854{ 855 OHOS::buffer::Buffer *buf = new OHOS::buffer::Buffer(); 856 buf->Init(7); 857 buf->WriteString("3363333", 7); 858 int index = buf->LastIndexOf("36", 0, 2); 859 ASSERT_EQ(index, 1); 860} 861 862/** 863 * @tc.name: LastIndexOfTest003 864 * @tc.desc: The index of the last occurrence of value in buf. 865 * @tc.type: FUNC 866 * @tc.require:issueI5J5Z3 867 */ 868HWTEST_F(NativeEngineTest, LastIndexOfTest003, testing::ext::TestSize.Level0) 869{ 870 OHOS::buffer::Buffer *buf = new OHOS::buffer::Buffer(); 871 buf->Init(11); 872 buf->WriteString("32236326233", 11); 873 int index = buf->LastIndexOf("236", 0, 3); 874 ASSERT_EQ(index, 2); 875} 876 877/** 878 * @tc.name: LastIndexOfTest004 879 * @tc.desc: The index of the last occurrence of value in buf. 880 * @tc.type: FUNC 881 * @tc.require:issueI5J5Z3 882 */ 883HWTEST_F(NativeEngineTest, LastIndexOfTest004, testing::ext::TestSize.Level0) 884{ 885 OHOS::buffer::Buffer *buf = new OHOS::buffer::Buffer(); 886 buf->Init(12); 887 buf->WriteString("322362326233", 12); 888 int index = buf->LastIndexOf("2236", 0, 4); 889 ASSERT_EQ(index, 1); 890} 891 892/** 893 * @tc.name: LastIndexOfTest005 894 * @tc.desc: The index of the last occurrence of value in buf. 895 * @tc.type: FUNC 896 * @tc.require:issueI5J5Z3 897 */ 898HWTEST_F(NativeEngineTest, LastIndexOfTest005, testing::ext::TestSize.Level0) 899{ 900 OHOS::buffer::Buffer *buf = new OHOS::buffer::Buffer(); 901 buf->Init(12); 902 buf->WriteString("322362326233", 12); 903 int index = buf->LastIndexOf("136", 0, 3); 904 ASSERT_EQ(index, -1); 905} 906 907 908/** 909 * @tc.name: ToBase64Test001 910 * @tc.desc: Convert the contents of the buffer into a string in Base64 format. 911 * @tc.type: FUNC 912 * @tc.require:issueI5J5Z3 913 */ 914HWTEST_F(NativeEngineTest, ToBase64Test001, testing::ext::TestSize.Level0) 915{ 916 OHOS::buffer::Buffer *buf = new OHOS::buffer::Buffer(); 917 buf->Init(20); 918 buf->WriteString("this is a string", 16); 919 std::string base64Str = buf->ToBase64(0, 16); 920 ASSERT_STREQ(base64Str.c_str(), "dGhpcyBpcyBhIHN0cmluZw=="); 921} 922 923/** 924 * @tc.name: ToBase64Test002 925 * @tc.desc: Convert the contents of the buffer into a string in Base64 format. 926 * @tc.type: FUNC 927 * @tc.require:issueI5J5Z3 928 */ 929HWTEST_F(NativeEngineTest, ToBase64Test002, testing::ext::TestSize.Level0) 930{ 931 OHOS::buffer::Buffer *buf = new OHOS::buffer::Buffer(); 932 buf->Init(30); 933 buf->WriteString("this is a big string", 20); 934 std::string base64Str = buf->ToBase64(0, 20); 935 ASSERT_STREQ(base64Str.c_str(), "dGhpcyBpcyBhIGJpZyBzdHJpbmc="); 936} 937 938/** 939 * @tc.name: ToBase64Test003 940 * @tc.desc: Convert to base64 with length 0. 941 * @tc.type: FUNC 942 * @tc.require:issueI5J5Z3 943 */ 944HWTEST_F(NativeEngineTest, ToBase64Test003, testing::ext::TestSize.Level0) 945{ 946 OHOS::buffer::Buffer *buf = new OHOS::buffer::Buffer(); 947 buf->Init(30); 948 buf->WriteString("this is a big string", 20); 949 std::string base64Str = buf->ToBase64(0, 0); 950 ASSERT_STREQ(base64Str.c_str(), ""); 951} 952 953/** 954 * @tc.name: ToBase64UrlTest001 955 * @tc.desc: Convert the contents of the buffer into a string in Base64 format. 956 * @tc.type: FUNC 957 * @tc.require:issueI5J5Z3 958 */ 959HWTEST_F(NativeEngineTest, ToBase64UrlTest001, testing::ext::TestSize.Level0) 960{ 961 OHOS::buffer::Buffer *buf = new OHOS::buffer::Buffer(); 962 buf->Init(20); 963 buf->WriteString("this is a string", 16); 964 std::string base64Str = buf->ToBase64Url(0, 16); 965 std::string stra = OHOS::buffer::Base64Encode(nullptr, 10, OHOS::buffer::BASE64URL); 966 ASSERT_STREQ(base64Str.c_str(), "dGhpcyBpcyBhIHN0cmluZw"); 967} 968 969/** 970 * @tc.name: GetEncodingTypeTest001 971 * @tc.desc: Get encoding type. 972 * @tc.type: FUNC 973 * @tc.require:issueI5J5Z3 974 */ 975HWTEST_F(NativeEngineTest, GetEncodingTypeTest001, testing::ext::TestSize.Level0) 976{ 977 std::map <std::string, int> _typeMap = 978 { 979 {"hex", OHOS::buffer::HEX}, 980 {"base64url", OHOS::buffer::BASE64URL}, 981 {"ascii", OHOS::buffer::ASCII}, 982 {"base64", OHOS::buffer::BASE64}, 983 {"latin1", OHOS::buffer::LATIN1}, 984 {"binary", OHOS::buffer::BINARY}, 985 {"utf16le", OHOS::buffer::UTF16LE}, 986 {"utf8", OHOS::buffer::UTF8}, 987 }; 988 989 for (auto item =_typeMap.begin(); item != _typeMap.end(); item++) 990 { 991 std::string type = item->first; 992 OHOS::buffer::EncodingType et = OHOS::buffer::Buffer::GetEncodingType(type); 993 ASSERT_EQ(et, item->second); 994 } 995} 996 997/** 998 * @tc.name: SetArrayTest001 999 * @tc.desc: Put the contents of the array into the buffer. 1000 * @tc.type: FUNC 1001 * @tc.require:issueI5J5Z3 1002 */ 1003HWTEST_F(NativeEngineTest, SetArrayTest001, testing::ext::TestSize.Level0) 1004{ 1005 OHOS::buffer::Buffer *buffer = new OHOS::buffer::Buffer(); 1006 buffer->Init(20); 1007 std::vector<uint8_t> numbers; 1008 for (int i = 0; i < 10; i++) { 1009 numbers.push_back(i); 1010 } 1011 buffer->SetArray(numbers); 1012 unsigned int offset = 0; 1013 unsigned int end = 10; 1014 uint8_t data[20] = {0}; 1015 buffer->ReadBytes(data, offset, end); 1016 for (int j = 0; j < 10; j++) { 1017 ASSERT_EQ(data[j], j); 1018 } 1019} 1020 1021/** 1022 * @tc.name: FillBufferTest001 1023 * @tc.desc: Fill the buffer with the buffer object 1024 * @tc.type: FUNC 1025 * @tc.require:issueI5J5Z3 1026 */ 1027HWTEST_F(NativeEngineTest, FillBufferTest001, testing::ext::TestSize.Level0) 1028{ 1029 OHOS::buffer::Buffer *buffer = new OHOS::buffer::Buffer(); 1030 buffer->Init(10); 1031 std::vector<uint8_t> numbers; 1032 for (int i = 0; i < 10; i++) { 1033 numbers.push_back(i); 1034 } 1035 buffer->SetArray(numbers); 1036 unsigned int offset = 0; 1037 unsigned int end = 10; 1038 OHOS::buffer::Buffer *buf = new OHOS::buffer::Buffer(); 1039 buf->Init(20); 1040 buf->FillBuffer(buffer, offset, end); 1041 uint8_t data[20] = {0}; 1042 buf->ReadBytes(data, offset, end); 1043 for (int j = 0; j < 10; j++) { 1044 ASSERT_EQ(data[j], j); 1045 } 1046} 1047 1048/** 1049 * @tc.name: FillNumberTest001 1050 * @tc.desc: Fill the buffer with the number 1051 * @tc.type: FUNC 1052 * @tc.require:issueI5J5Z3 1053 */ 1054HWTEST_F(NativeEngineTest, FillNumberTest001, testing::ext::TestSize.Level0) 1055{ 1056 OHOS::buffer::Buffer *buf = new OHOS::buffer::Buffer(); 1057 buf->Init(20); 1058 std::vector<uint8_t> numbers; 1059 for (int i = 0; i < 10; i++) { 1060 numbers.push_back(i); 1061 } 1062 unsigned int offset = 0; 1063 unsigned int end = 10; 1064 buf->FillNumber(numbers, offset, end); 1065 uint8_t data[20] = {0}; 1066 buf->ReadBytes(data, offset, end); 1067 for (int j = 0; j < 10; j++) { 1068 ASSERT_EQ(data[j], j); 1069 } 1070} 1071 1072/** 1073 * @tc.name: FillStringTest001 1074 * @tc.desc: Fill the buffer with the string 1075 * @tc.type: FUNC 1076 * @tc.require:issueI5J5Z3 1077 */ 1078HWTEST_F(NativeEngineTest, FillStringTest001, testing::ext::TestSize.Level0) 1079{ 1080 OHOS::buffer::Buffer *buf = new OHOS::buffer::Buffer(); 1081 buf->Init(20); 1082 std::string value = "abcd"; 1083 std::string encoding = "ascii"; 1084 buf->FillString(value, 1, 1, encoding); 1085 1086 unsigned int offset = 0; 1087 unsigned int end = 10; 1088 buf->FillString(value, offset, end, encoding); 1089 uint8_t data[20] = {0}; 1090 buf->ReadBytes(data, offset, end); 1091 ASSERT_STREQ(reinterpret_cast<char*>(data), "abcdabcdab"); 1092} 1093 1094/** 1095 * @tc.name: FillStringTest002 1096 * @tc.desc: Fill the buffer with the string 1097 * @tc.type: FUNC 1098 * @tc.require:issueI5J5Z3 1099 */ 1100HWTEST_F(NativeEngineTest, FillStringTest002, testing::ext::TestSize.Level0) 1101{ 1102 OHOS::buffer::Buffer *buf = new OHOS::buffer::Buffer(); 1103 buf->Init(20); 1104 std::string value = "扡摣"; 1105 unsigned int offset = 0; 1106 unsigned int end = 10; 1107 std::string encoding = "utf16le"; 1108 buf->FillString(value, offset, end, encoding); 1109 uint8_t data[20] = {0}; 1110 buf->ReadBytes(data, offset, end); 1111 ASSERT_STREQ(reinterpret_cast<char*>(data), "abcdabcdab"); 1112} 1113 1114/** 1115 * @tc.name: FillStringTest003 1116 * @tc.desc: Fill the buffer with the string 1117 * @tc.type: FUNC 1118 * @tc.require:issueI5J5Z3 1119 */ 1120HWTEST_F(NativeEngineTest, FillStringTest003, testing::ext::TestSize.Level0) 1121{ 1122 OHOS::buffer::Buffer *buf = new OHOS::buffer::Buffer(); 1123 buf->Init(20); 1124 std::string value = "YWJjZA"; 1125 unsigned int offset = 0; 1126 unsigned int end = 10; 1127 std::string encoding = "base64"; 1128 buf->FillString(value, offset, end, encoding); 1129 uint8_t data[20] = {0}; 1130 buf->ReadBytes(data, offset, end); 1131 ASSERT_STREQ(reinterpret_cast<char*>(data), "abcdabcdab"); 1132} 1133 1134/** 1135 * @tc.name: BlobConstructorTest001 1136 * @tc.desc: Blob Constructor 1137 * @tc.type: FUNC 1138 * @tc.require:issueI5J5Z3 1139 */ 1140HWTEST_F(NativeEngineTest, BlobConstructorTest001, testing::ext::TestSize.Level0) 1141{ 1142 OHOS::buffer::Blob *blob = new OHOS::buffer::Blob(); 1143 uint8_t data[4] = {1, 2, 3, 4}; 1144 blob->Init(data, 4); 1145 ASSERT_EQ(blob->GetLength(), 4); 1146} 1147 1148/** 1149 * @tc.name: BlobConstructorTest002 1150 * @tc.desc: Blob Constructor 1151 * @tc.type: FUNC 1152 * @tc.require:issueI5J5Z3 1153 */ 1154HWTEST_F(NativeEngineTest, BlobConstructorTest002, testing::ext::TestSize.Level0) 1155{ 1156 OHOS::buffer::Blob *blob = new OHOS::buffer::Blob(); 1157 uint8_t data[4] = {1, 2, 3, 4}; 1158 blob->Init(data, 4); 1159 1160 OHOS::buffer::Blob *blob2 = new OHOS::buffer::Blob(); 1161 blob2->Init(blob, 0); 1162 1163 ASSERT_EQ(blob2->GetLength(), 4); 1164} 1165 1166/** 1167 * @tc.name: BlobConstructorTest003 1168 * @tc.desc: Blob Constructor 1169 * @tc.type: FUNC 1170 * @tc.require:issueI5J5Z3 1171 */ 1172HWTEST_F(NativeEngineTest, BlobConstructorTest003, testing::ext::TestSize.Level0) 1173{ 1174 OHOS::buffer::Blob *blob = new OHOS::buffer::Blob(); 1175 uint8_t data[4] = {1, 2, 3, 4}; 1176 blob->Init(data, 4); 1177 1178 OHOS::buffer::Blob *blob2 = new OHOS::buffer::Blob(); 1179 blob2->Init(blob, 1, 4); 1180 1181 ASSERT_EQ(blob2->GetLength(), 3); 1182} 1183 1184/** 1185 * @tc.name: BlobConstructorTest004 1186 * @tc.desc: Blob Constructor 1187 * @tc.type: FUNC 1188 * @tc.require:issueI5J5Z3 1189 */ 1190HWTEST_F(NativeEngineTest, BlobConstructorTest004, testing::ext::TestSize.Level0) 1191{ 1192 OHOS::buffer::Blob *blob = new OHOS::buffer::Blob(); 1193 uint8_t data[1] = {1}; 1194 OHOS::buffer::Blob *blob1 = new OHOS::buffer::Blob(); 1195 blob->Init(blob1, 1, 0); 1196 blob->Init(blob1, 1, -1); 1197 blob->Init(blob1, -1, 1); 1198 blob->Init(nullptr, 0, 1); 1199 blob->Init(data, 1); 1200 ASSERT_EQ(blob->GetLength(), 1); 1201 delete blob; 1202 blob = nullptr; 1203 delete blob1; 1204 blob1 = nullptr; 1205} 1206 1207/** 1208 * @tc.name: BlobDestructorTest001 1209 * @tc.desc: Blob Destructor. 1210 * @tc.type: FUNC 1211 * @tc.require:issueI5J5Z3 1212 */ 1213HWTEST_F(NativeEngineTest, BlobDestructorTest001, testing::ext::TestSize.Level0) 1214{ 1215 OHOS::buffer::Blob *blob = new OHOS::buffer::Blob(); 1216 uint8_t data[4] = {1, 2, 3, 4}; 1217 blob->Init(data, 4); 1218 unsigned int len = blob->GetLength(); 1219 ASSERT_EQ(len, 4); 1220 delete blob; 1221 blob = nullptr; 1222} 1223 1224/** 1225 * @tc.name: BlobGetByteTest001 1226 * @tc.desc: Get a byte in blob 1227 * @tc.type: FUNC 1228 * @tc.require:issueI5J5Z3 1229 */ 1230HWTEST_F(NativeEngineTest, BlobGetByteTest001, testing::ext::TestSize.Level0) 1231{ 1232 OHOS::buffer::Blob *blob = new OHOS::buffer::Blob(); 1233 uint8_t data[4] = {1, 2, 3, 4}; 1234 blob->Init(data, 4); 1235 1236 uint8_t byte = blob->GetByte(2); 1237 1238 ASSERT_EQ(byte, 3); 1239} 1240 1241/** 1242 * @tc.name: BlobGetRawTest001 1243 * @tc.desc: Get the raw in blob 1244 * @tc.type: FUNC 1245 * @tc.require:issueI5J5Z3 1246 */ 1247HWTEST_F(NativeEngineTest, BlobGetRawTest001, testing::ext::TestSize.Level0) 1248{ 1249 OHOS::buffer::Blob *blob = new OHOS::buffer::Blob(); 1250 uint8_t data[4] = {1, 2, 3, 4}; 1251 blob->Init(data, 4); 1252 1253 uint8_t *raw = blob->GetRaw(); 1254 1255 ASSERT_TRUE(raw != nullptr); 1256} 1257 1258/** 1259 * @tc.name: BlobGetLengthTest001 1260 * @tc.desc: Get the length in blob 1261 * @tc.type: FUNC 1262 * @tc.require:issueI5J5Z3 1263 */ 1264HWTEST_F(NativeEngineTest, BlobGetLengthTest001, testing::ext::TestSize.Level0) 1265{ 1266 OHOS::buffer::Blob *blob = new OHOS::buffer::Blob(); 1267 uint8_t data[4] = {1, 2, 3, 4}; 1268 blob->Init(data, 4); 1269 1270 unsigned int len = blob->GetLength(); 1271 1272 ASSERT_EQ(len, 4); 1273} 1274 1275/** 1276 * @tc.name: BlobGetLengthTest001 1277 * @tc.desc: Read blob object bytes 1278 * @tc.type: FUNC 1279 * @tc.require:issueI5J5Z3 1280 */ 1281HWTEST_F(NativeEngineTest, BlobReadBytesTest001, testing::ext::TestSize.Level0) 1282{ 1283 OHOS::buffer::Blob *blob = new OHOS::buffer::Blob(); 1284 uint8_t data[10] = {0}; 1285 for (int i = 0; i < 10; i++) { 1286 data[i] = i; 1287 } 1288 blob->Init(data, 10); 1289 1290 uint8_t dat[10] = {0}; 1291 blob->ReadBytes(dat, 10); 1292 1293 for (int i = 0; i < 10; i++) { 1294 ASSERT_EQ(dat[i], i); 1295 } 1296} 1297 1298/** 1299 * @tc.name: Utf8ToUtf16BETest001 1300 * @tc.desc: convert utf8 bytes to utf16 bytes 1301 * @tc.type: FUNC 1302 * @tc.require:issueI5J5Z3 1303 */ 1304HWTEST_F(NativeEngineTest, Utf8ToUtf16BETest001, testing::ext::TestSize.Level0) 1305{ 1306 std::string str8 = ""; 1307 // one byte 1308 str8.append(1, 0x41); 1309 // two bytes 1310 str8.append(1, 0xC3); 1311 str8.append(1, 0x84); 1312 // three bytes 1313 str8.append(1, 0xE5); 1314 str8.append(1, 0x88); 1315 str8.append(1, 0x98); 1316 // four bytes 1317 str8.append(1, 0xf0); 1318 str8.append(1, 0x9f); 1319 str8.append(1, 0x90); 1320 str8.append(1, 0x85); 1321 1322 // another four bytes 1323 str8.append(1, 0xf0); 1324 str8.append(1, 0x8f); 1325 str8.append(1, 0x90); 1326 str8.append(1, 0x85); 1327 1328 bool isOk = false; 1329 std::u16string str16 = OHOS::buffer::Utf8ToUtf16BE(str8, &isOk); 1330 1331 char16_t results[] = {0x41, 0xc4, 0x5218, 0xd83d, 0xdc05, 0xf405}; 1332 for (int i = 0; i < 6; i++) { 1333 ASSERT_EQ(results[i], str16[i]); 1334 } 1335} 1336 1337/** 1338 * @tc.name: HexDecodeTest001 1339 * @tc.desc: decode a hex string 1340 * @tc.type: FUNC 1341 * @tc.require:issueI5J5Z3 1342 */ 1343HWTEST_F(NativeEngineTest, HexDecodeTest001, testing::ext::TestSize.Level0) 1344{ 1345 std::string ret = OHOS::buffer::HexDecode("313g"); 1346 ASSERT_EQ(ret, "1"); 1347} 1348 1349/** 1350 * @tc.name: Utf16BEToLETest001 1351 * @tc.desc: Utf16BEToLE 1352 * @tc.type: FUNC 1353 * @tc.require:issueI5J5Z3 1354 */ 1355HWTEST_F(NativeEngineTest, Utf16BEToLETest001, testing::ext::TestSize.Level0) 1356{ 1357 std::u16string wstrBE = u"\x0041\x0042\x0043"; 1358 std::u16string re = OHOS::buffer::Utf16BEToLE(wstrBE); 1359 char16_t results[] = {0x4100, 0x4200, 0x4300}; 1360 for (int i = 0; i < 3; i++) { 1361 ASSERT_EQ(results[i], re[i]); 1362 } 1363} 1364 1365/** 1366 * @tc.name: Base64EncodeTest001 1367 * @tc.desc: Base64 encode with max size_t 1368 * @tc.type: FUNC 1369 * @tc.require:issueI5J5Z3 1370 */ 1371HWTEST_F(NativeEngineTest, Base64EncodeTest001, testing::ext::TestSize.Level0) 1372{ 1373 unsigned char data[] = {0x1A, 0x2B, 0x3C, 0x4D}; 1374 size_t len = std::numeric_limits<size_t>::max(); 1375 std::string stra = OHOS::buffer::Base64Encode(data, len, OHOS::buffer::BASE64URL); 1376 ASSERT_EQ(stra, ""); 1377} 1378 1379/** 1380 * @tc.name: Base64EncodeTest002 1381 * @tc.desc: Base64 encode 1382 * @tc.type: FUNC 1383 * @tc.require:issueI5J5Z3 1384 */ 1385HWTEST_F(NativeEngineTest, Base64EncodeTest002, testing::ext::TestSize.Level0) 1386{ 1387 std::string value = "abc"; 1388 std::string strb = OHOS::buffer::Base64Decode(value, OHOS::buffer::BASE64URL); 1389 1390 unsigned char data[] = {0x1A, 0x2B, 0x3C, 0x4D}; 1391 std::string stra = OHOS::buffer::Base64Encode(data, 4, OHOS::buffer::BASE64URL); 1392 ASSERT_EQ(stra, "Gis8TQ"); 1393} 1394 1395/** 1396 * @tc.name: GetGoodSuffixLengthByLastCharTest001 1397 * @tc.desc: Get good suffix length by last char 1398 * @tc.type: FUNC 1399 * @tc.require:issueI5J5Z3 1400 */ 1401HWTEST_F(NativeEngineTest, GetGoodSuffixLengthByLastCharTest001, testing::ext::TestSize.Level0) 1402{ 1403 uint8_t pat[] = "ababcab"; 1404 int patLen = sizeof(pat) - 1; 1405 int patIndex = 4; 1406 1407 int length = OHOS::buffer::GetGoodSuffixLengthByLastChar(pat, patIndex, patLen); 1408 ASSERT_EQ(length, 3); 1409} 1410 1411/** 1412 * @tc.name: FindLastIndexTest001 1413 * @tc.desc: Find last index with error 1414 * @tc.type: FUNC 1415 * @tc.require:issueI5J5Z3 1416 */ 1417HWTEST_F(NativeEngineTest, FindLastIndexTest001, testing::ext::TestSize.Level0) 1418{ 1419 int rel1 = OHOS::buffer::FindLastIndex(nullptr, nullptr, 2, 1); 1420 ASSERT_EQ(rel1, -1); 1421 1422 uint8_t data[] = "abc"; 1423 int rel2 = OHOS::buffer::FindLastIndex(data, data, 2, 0); 1424 ASSERT_EQ(rel2, -1); 1425} 1426 1427/** 1428 * @tc.name: FindIndex005 1429 * @tc.desc: Find index with error. 1430 * @tc.type: FUNC 1431 * @tc.require:issueI5J5Z3 1432 */ 1433HWTEST_F(NativeEngineTest, FindIndex005, testing::ext::TestSize.Level0) 1434{ 1435 int rel1 = OHOS::buffer::FindIndex(nullptr, nullptr, 2, 1); 1436 ASSERT_EQ(rel1, -1); 1437 1438 uint8_t data[] = "abc"; 1439 int rel2 = OHOS::buffer::FindIndex(data, data, 2, 0); 1440 ASSERT_EQ(rel2, -1); 1441} 1442 1443/** 1444 * @tc.name: FindIndex001 1445 * @tc.desc: Buffer FindIndex. 1446 * @tc.type: FUNC 1447 * @tc.require:issueI5J5Z3 1448 */ 1449HWTEST_F(NativeEngineTest, FindIndex001, testing::ext::TestSize.Level0) 1450{ 1451 OHOS::buffer::Buffer *buf = new OHOS::buffer::Buffer(); 1452 buf->Init(15); 1453 buf->WriteString("This is a buffer", 15); 1454 uint64_t resultIndex = 0; 1455 int index = buf->IndexOf("2623", 0, 4, resultIndex); 1456 ASSERT_EQ(index, -1); 1457} 1458 1459/** 1460 * @tc.name: FindIndex002 1461 * @tc.desc: Buffer FindIndex. 1462 * @tc.type: FUNC 1463 * @tc.require:issueI5J5Z3 1464 */ 1465HWTEST_F(NativeEngineTest, FindIndex002, testing::ext::TestSize.Level0) 1466{ 1467 OHOS::buffer::Buffer *buf = new OHOS::buffer::Buffer(); 1468 buf->Init(15); 1469 buf->WriteString("This is a buffer", 15); 1470 uint64_t resultIndex = 0; 1471 int index = buf->IndexOf("f", 0, 1, resultIndex); 1472 ASSERT_EQ(index, -2); 1473 ASSERT_EQ(resultIndex, 12); 1474} 1475 1476/** 1477 * @tc.name: FindIndex003 1478 * @tc.desc: Buffer FindIndex. 1479 * @tc.type: FUNC 1480 * @tc.require:issueI5J5Z3 1481 */ 1482HWTEST_F(NativeEngineTest, FindIndex003, testing::ext::TestSize.Level0) 1483{ 1484 OHOS::buffer::Buffer *buf = new OHOS::buffer::Buffer(); 1485 buf->Init(15); 1486 buf->WriteString("23456789abcdefg", 15); 1487 uint64_t resultIndex = 0; 1488 int index = buf->IndexOf("3", 0, 1, resultIndex); 1489 ASSERT_EQ(index, -2); 1490 ASSERT_EQ(resultIndex, 1); 1491} 1492 1493/** 1494 * @tc.name: FindIndex004 1495 * @tc.desc: Buffer FindIndex. 1496 * @tc.type: FUNC 1497 * @tc.require:issueI5J5Z3 1498 */ 1499HWTEST_F(NativeEngineTest, FindIndex004, testing::ext::TestSize.Level0) 1500{ 1501 OHOS::buffer::Buffer *buf = new OHOS::buffer::Buffer(); 1502 buf->Init(15); 1503 buf->WriteString("23456789abcdefg", 15); 1504 uint64_t resultIndex = 0; 1505 int index = buf->IndexOf("3", 10, 1, resultIndex); 1506 ASSERT_EQ(index, -1); 1507} 1508