| OLD | NEW |
| 1 // Copyright 2016 the V8 project authors. All rights reserved. | 1 // Copyright 2016 the V8 project authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "src/value-serializer.h" | 5 #include "src/value-serializer.h" |
| 6 | 6 |
| 7 #include <algorithm> | 7 #include <algorithm> |
| 8 #include <string> | 8 #include <string> |
| 9 | 9 |
| 10 #include "include/v8.h" | 10 #include "include/v8.h" |
| (...skipping 449 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 460 EXPECT_EQ(6, String::Cast(*value)->Length()); | 460 EXPECT_EQ(6, String::Cast(*value)->Length()); |
| 461 EXPECT_EQ(kQuebecString, Utf8Value(value)); | 461 EXPECT_EQ(kQuebecString, Utf8Value(value)); |
| 462 }); | 462 }); |
| 463 DecodeTest({0xff, 0x09, 0x53, 0x04, 0xf0, 0x9f, 0x91, 0x8a}, | 463 DecodeTest({0xff, 0x09, 0x53, 0x04, 0xf0, 0x9f, 0x91, 0x8a}, |
| 464 [](Local<Value> value) { | 464 [](Local<Value> value) { |
| 465 ASSERT_TRUE(value->IsString()); | 465 ASSERT_TRUE(value->IsString()); |
| 466 EXPECT_EQ(2, String::Cast(*value)->Length()); | 466 EXPECT_EQ(2, String::Cast(*value)->Length()); |
| 467 EXPECT_EQ(kEmojiString, Utf8Value(value)); | 467 EXPECT_EQ(kEmojiString, Utf8Value(value)); |
| 468 }); | 468 }); |
| 469 | 469 |
| 470 // And from Latin-1 (for the ones that fit). |
| 471 DecodeTest({0xff, 0x0a, 0x22, 0x00}, [](Local<Value> value) { |
| 472 ASSERT_TRUE(value->IsString()); |
| 473 EXPECT_EQ(0, String::Cast(*value)->Length()); |
| 474 }); |
| 475 DecodeTest({0xff, 0x0a, 0x22, 0x05, 'H', 'e', 'l', 'l', 'o'}, |
| 476 [](Local<Value> value) { |
| 477 ASSERT_TRUE(value->IsString()); |
| 478 EXPECT_EQ(5, String::Cast(*value)->Length()); |
| 479 EXPECT_EQ(kHelloString, Utf8Value(value)); |
| 480 }); |
| 481 DecodeTest({0xff, 0x0a, 0x22, 0x06, 'Q', 'u', 0xe9, 'b', 'e', 'c'}, |
| 482 [](Local<Value> value) { |
| 483 ASSERT_TRUE(value->IsString()); |
| 484 EXPECT_EQ(6, String::Cast(*value)->Length()); |
| 485 EXPECT_EQ(kQuebecString, Utf8Value(value)); |
| 486 }); |
| 487 |
| 470 // And from two-byte strings (endianness dependent). | 488 // And from two-byte strings (endianness dependent). |
| 471 #if defined(V8_TARGET_LITTLE_ENDIAN) | 489 #if defined(V8_TARGET_LITTLE_ENDIAN) |
| 472 DecodeTest({0xff, 0x09, 0x63, 0x00}, | 490 DecodeTest({0xff, 0x09, 0x63, 0x00}, |
| 473 [](Local<Value> value) { | 491 [](Local<Value> value) { |
| 474 ASSERT_TRUE(value->IsString()); | 492 ASSERT_TRUE(value->IsString()); |
| 475 EXPECT_EQ(0, String::Cast(*value)->Length()); | 493 EXPECT_EQ(0, String::Cast(*value)->Length()); |
| 476 }); | 494 }); |
| 477 DecodeTest({0xff, 0x09, 0x63, 0x0a, 'H', '\0', 'e', '\0', 'l', '\0', 'l', | 495 DecodeTest({0xff, 0x09, 0x63, 0x0a, 'H', '\0', 'e', '\0', 'l', '\0', 'l', |
| 478 '\0', 'o', '\0'}, | 496 '\0', 'o', '\0'}, |
| 479 [](Local<Value> value) { | 497 [](Local<Value> value) { |
| (...skipping 14 matching lines...) Expand all Loading... |
| 494 EXPECT_EQ(2, String::Cast(*value)->Length()); | 512 EXPECT_EQ(2, String::Cast(*value)->Length()); |
| 495 EXPECT_EQ(kEmojiString, Utf8Value(value)); | 513 EXPECT_EQ(kEmojiString, Utf8Value(value)); |
| 496 }); | 514 }); |
| 497 #endif | 515 #endif |
| 498 // TODO(jbroman): The same for big-endian systems. | 516 // TODO(jbroman): The same for big-endian systems. |
| 499 } | 517 } |
| 500 | 518 |
| 501 TEST_F(ValueSerializerTest, DecodeInvalidString) { | 519 TEST_F(ValueSerializerTest, DecodeInvalidString) { |
| 502 // UTF-8 string with too few bytes available. | 520 // UTF-8 string with too few bytes available. |
| 503 InvalidDecodeTest({0xff, 0x09, 0x53, 0x10, 'v', '8'}); | 521 InvalidDecodeTest({0xff, 0x09, 0x53, 0x10, 'v', '8'}); |
| 522 // One-byte string with too few bytes available. |
| 523 InvalidDecodeTest({0xff, 0x0a, 0x22, 0x10, 'v', '8'}); |
| 504 #if defined(V8_TARGET_LITTLE_ENDIAN) | 524 #if defined(V8_TARGET_LITTLE_ENDIAN) |
| 505 // Two-byte string with too few bytes available. | 525 // Two-byte string with too few bytes available. |
| 506 InvalidDecodeTest({0xff, 0x09, 0x63, 0x10, 'v', '\0', '8', '\0'}); | 526 InvalidDecodeTest({0xff, 0x09, 0x63, 0x10, 'v', '\0', '8', '\0'}); |
| 507 // Two-byte string with an odd byte length. | 527 // Two-byte string with an odd byte length. |
| 508 InvalidDecodeTest({0xff, 0x09, 0x63, 0x03, 'v', '\0', '8'}); | 528 InvalidDecodeTest({0xff, 0x09, 0x63, 0x03, 'v', '\0', '8'}); |
| 509 #endif | 529 #endif |
| 510 // TODO(jbroman): The same for big-endian systems. | 530 // TODO(jbroman): The same for big-endian systems. |
| 511 } | 531 } |
| 512 | 532 |
| 513 TEST_F(ValueSerializerTest, EncodeTwoByteStringUsesPadding) { | 533 TEST_F(ValueSerializerTest, EncodeTwoByteStringUsesPadding) { |
| 514 // As long as the output has a version that Blink expects to be able to read, | 534 // As long as the output has a version that Blink expects to be able to read, |
| 515 // we must respect its alignment requirements. It requires that two-byte | 535 // we must respect its alignment requirements. It requires that two-byte |
| 516 // characters be aligned. | 536 // characters be aligned. |
| 517 EncodeTest( | 537 EncodeTest( |
| 518 [this]() { | 538 [this]() { |
| 519 // We need a string whose length will take two bytes to encode, so that | 539 // We need a string whose length will take two bytes to encode, so that |
| 520 // a padding byte is needed to keep the characters aligned. The string | 540 // a padding byte is needed to keep the characters aligned. The string |
| 521 // must also have a two-byte character, so that it gets the two-byte | 541 // must also have a two-byte character, so that it gets the two-byte |
| 522 // encoding. | 542 // encoding. |
| 523 std::string string(200, ' '); | 543 std::string string(200, ' '); |
| 524 string += kEmojiString; | 544 string += kEmojiString; |
| 525 return StringFromUtf8(string.c_str()); | 545 return StringFromUtf8(string.c_str()); |
| 526 }, | 546 }, |
| 527 [](const std::vector<uint8_t>& data) { | 547 [](const std::vector<uint8_t>& data) { |
| 528 // This is a sufficient but not necessary condition to be aligned. | 548 // This is a sufficient but not necessary condition. This test assumes |
| 529 // Note that the third byte (0x00) is padding. | 549 // that the wire format version is one byte long, but is flexible to |
| 530 const uint8_t expected_prefix[] = {0xff, 0x09, 0x00, 0x63, 0x94, 0x03}; | 550 // what that value may be. |
| 531 ASSERT_GT(data.size(), sizeof(expected_prefix) / sizeof(uint8_t)); | 551 const uint8_t expected_prefix[] = {0x00, 0x63, 0x94, 0x03}; |
| 552 ASSERT_GT(data.size(), sizeof(expected_prefix) + 2); |
| 553 EXPECT_EQ(0xff, data[0]); |
| 554 EXPECT_GE(data[1], 0x09); |
| 555 EXPECT_LE(data[1], 0x7f); |
| 532 EXPECT_TRUE(std::equal(std::begin(expected_prefix), | 556 EXPECT_TRUE(std::equal(std::begin(expected_prefix), |
| 533 std::end(expected_prefix), data.begin())); | 557 std::end(expected_prefix), data.begin() + 2)); |
| 534 }); | 558 }); |
| 535 } | 559 } |
| 536 | 560 |
| 537 TEST_F(ValueSerializerTest, RoundTripDictionaryObject) { | 561 TEST_F(ValueSerializerTest, RoundTripDictionaryObject) { |
| 538 // Empty object. | 562 // Empty object. |
| 539 RoundTripTest("({})", [this](Local<Value> value) { | 563 RoundTripTest("({})", [this](Local<Value> value) { |
| 540 ASSERT_TRUE(value->IsObject()); | 564 ASSERT_TRUE(value->IsObject()); |
| 541 EXPECT_TRUE(EvaluateScriptForResultBool( | 565 EXPECT_TRUE(EvaluateScriptForResultBool( |
| 542 "Object.getPrototypeOf(result) === Object.prototype")); | 566 "Object.getPrototypeOf(result) === Object.prototype")); |
| 543 EXPECT_TRUE(EvaluateScriptForResultBool( | 567 EXPECT_TRUE(EvaluateScriptForResultBool( |
| (...skipping 2066 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 2610 InvalidDecodeTest(raw); | 2634 InvalidDecodeTest(raw); |
| 2611 } | 2635 } |
| 2612 | 2636 |
| 2613 TEST_F(ValueSerializerTestWithWasm, DecodeWasmModuleWithInvalidDataLength) { | 2637 TEST_F(ValueSerializerTestWithWasm, DecodeWasmModuleWithInvalidDataLength) { |
| 2614 InvalidDecodeTest({0xff, 0x09, 0x3f, 0x00, 0x57, 0x79, 0x7f, 0x00}); | 2638 InvalidDecodeTest({0xff, 0x09, 0x3f, 0x00, 0x57, 0x79, 0x7f, 0x00}); |
| 2615 InvalidDecodeTest({0xff, 0x09, 0x3f, 0x00, 0x57, 0x79, 0x00, 0x7f}); | 2639 InvalidDecodeTest({0xff, 0x09, 0x3f, 0x00, 0x57, 0x79, 0x00, 0x7f}); |
| 2616 } | 2640 } |
| 2617 | 2641 |
| 2618 } // namespace | 2642 } // namespace |
| 2619 } // namespace v8 | 2643 } // namespace v8 |
| OLD | NEW |