| 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" |
| 11 #include "src/api.h" | 11 #include "src/api.h" |
| 12 #include "src/base/build_config.h" | 12 #include "src/base/build_config.h" |
| 13 #include "test/unittests/test-utils.h" | 13 #include "test/unittests/test-utils.h" |
| 14 #include "testing/gmock/include/gmock/gmock.h" | 14 #include "testing/gmock/include/gmock/gmock.h" |
| 15 #include "testing/gtest/include/gtest/gtest.h" | 15 #include "testing/gtest/include/gtest/gtest.h" |
| 16 | 16 |
| 17 namespace v8 { | 17 namespace v8 { |
| 18 namespace { | 18 namespace { |
| 19 | 19 |
| 20 using ::testing::_; | 20 using ::testing::_; |
| 21 using ::testing::Invoke; | 21 using ::testing::Invoke; |
| 22 using ::testing::Return; |
| 22 | 23 |
| 23 class ValueSerializerTest : public TestWithIsolate { | 24 class ValueSerializerTest : public TestWithIsolate { |
| 24 protected: | 25 protected: |
| 25 ValueSerializerTest() | 26 ValueSerializerTest() |
| 26 : serialization_context_(Context::New(isolate())), | 27 : serialization_context_(Context::New(isolate())), |
| 27 deserialization_context_(Context::New(isolate())) { | 28 deserialization_context_(Context::New(isolate())) { |
| 28 // Create a host object type that can be tested through | 29 // Create a host object type that can be tested through |
| 29 // serialization/deserialization delegates below. | 30 // serialization/deserialization delegates below. |
| 30 Local<FunctionTemplate> function_template = v8::FunctionTemplate::New( | 31 Local<FunctionTemplate> function_template = v8::FunctionTemplate::New( |
| 31 isolate(), [](const FunctionCallbackInfo<Value>& args) { | 32 isolate(), [](const FunctionCallbackInfo<Value>& args) { |
| (...skipping 90 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 122 const EncodedDataFunctor& encoded_data_functor) { | 123 const EncodedDataFunctor& encoded_data_functor) { |
| 123 Context::Scope scope(serialization_context()); | 124 Context::Scope scope(serialization_context()); |
| 124 TryCatch try_catch(isolate()); | 125 TryCatch try_catch(isolate()); |
| 125 Local<Value> input_value = input_functor(); | 126 Local<Value> input_value = input_functor(); |
| 126 std::vector<uint8_t> buffer; | 127 std::vector<uint8_t> buffer; |
| 127 ASSERT_TRUE(DoEncode(input_value).To(&buffer)); | 128 ASSERT_TRUE(DoEncode(input_value).To(&buffer)); |
| 128 ASSERT_FALSE(try_catch.HasCaught()); | 129 ASSERT_FALSE(try_catch.HasCaught()); |
| 129 encoded_data_functor(buffer); | 130 encoded_data_functor(buffer); |
| 130 } | 131 } |
| 131 | 132 |
| 133 template <typename InputFunctor, typename MessageFunctor> |
| 134 void InvalidEncodeTest(const InputFunctor& input_functor, |
| 135 const MessageFunctor& functor) { |
| 136 Context::Scope scope(serialization_context()); |
| 137 TryCatch try_catch(isolate()); |
| 138 Local<Value> input_value = input_functor(); |
| 139 ASSERT_TRUE(DoEncode(input_value).IsNothing()); |
| 140 functor(try_catch.Message()); |
| 141 } |
| 142 |
| 132 template <typename MessageFunctor> | 143 template <typename MessageFunctor> |
| 133 void InvalidEncodeTest(const char* source, const MessageFunctor& functor) { | 144 void InvalidEncodeTest(const char* source, const MessageFunctor& functor) { |
| 134 Context::Scope scope(serialization_context()); | 145 InvalidEncodeTest( |
| 135 TryCatch try_catch(isolate()); | 146 [this, source]() { return EvaluateScriptForInput(source); }, functor); |
| 136 Local<Value> input_value = EvaluateScriptForInput(source); | |
| 137 ASSERT_TRUE(DoEncode(input_value).IsNothing()); | |
| 138 functor(try_catch.Message()); | |
| 139 } | 147 } |
| 140 | 148 |
| 141 void InvalidEncodeTest(const char* source) { | 149 void InvalidEncodeTest(const char* source) { |
| 142 InvalidEncodeTest(source, [](Local<Message>) {}); | 150 InvalidEncodeTest(source, [](Local<Message>) {}); |
| 143 } | 151 } |
| 144 | 152 |
| 145 template <typename OutputFunctor> | 153 template <typename OutputFunctor> |
| 146 void DecodeTest(const std::vector<uint8_t>& data, | 154 void DecodeTest(const std::vector<uint8_t>& data, |
| 147 const OutputFunctor& output_functor) { | 155 const OutputFunctor& output_functor) { |
| 148 Local<Context> context = deserialization_context(); | 156 Local<Context> context = deserialization_context(); |
| (...skipping 1886 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 2035 // Byte offset in range, offset + length out of range. | 2043 // Byte offset in range, offset + length out of range. |
| 2036 InvalidDecodeTest( | 2044 InvalidDecodeTest( |
| 2037 {0xff, 0x09, 0x42, 0x02, 0x00, 0x00, 0x56, 0x3f, 0x01, 0x03}); | 2045 {0xff, 0x09, 0x42, 0x02, 0x00, 0x00, 0x56, 0x3f, 0x01, 0x03}); |
| 2038 } | 2046 } |
| 2039 | 2047 |
| 2040 class ValueSerializerTestWithSharedArrayBufferTransfer | 2048 class ValueSerializerTestWithSharedArrayBufferTransfer |
| 2041 : public ValueSerializerTest { | 2049 : public ValueSerializerTest { |
| 2042 protected: | 2050 protected: |
| 2043 static const size_t kTestByteLength = 4; | 2051 static const size_t kTestByteLength = 4; |
| 2044 | 2052 |
| 2045 ValueSerializerTestWithSharedArrayBufferTransfer() { | 2053 ValueSerializerTestWithSharedArrayBufferTransfer() |
| 2054 : serializer_delegate_(this) { |
| 2046 const uint8_t data[kTestByteLength] = {0x00, 0x01, 0x80, 0xff}; | 2055 const uint8_t data[kTestByteLength] = {0x00, 0x01, 0x80, 0xff}; |
| 2047 memcpy(data_, data, kTestByteLength); | 2056 memcpy(data_, data, kTestByteLength); |
| 2048 { | 2057 { |
| 2049 Context::Scope scope(serialization_context()); | 2058 Context::Scope scope(serialization_context()); |
| 2050 input_buffer_ = | 2059 input_buffer_ = |
| 2051 SharedArrayBuffer::New(isolate(), &data_, kTestByteLength); | 2060 SharedArrayBuffer::New(isolate(), &data_, kTestByteLength); |
| 2052 } | 2061 } |
| 2053 { | 2062 { |
| 2054 Context::Scope scope(deserialization_context()); | 2063 Context::Scope scope(deserialization_context()); |
| 2055 output_buffer_ = | 2064 output_buffer_ = |
| 2056 SharedArrayBuffer::New(isolate(), &data_, kTestByteLength); | 2065 SharedArrayBuffer::New(isolate(), &data_, kTestByteLength); |
| 2057 } | 2066 } |
| 2058 } | 2067 } |
| 2059 | 2068 |
| 2060 const Local<SharedArrayBuffer>& input_buffer() { return input_buffer_; } | 2069 const Local<SharedArrayBuffer>& input_buffer() { return input_buffer_; } |
| 2061 const Local<SharedArrayBuffer>& output_buffer() { return output_buffer_; } | 2070 const Local<SharedArrayBuffer>& output_buffer() { return output_buffer_; } |
| 2062 | 2071 |
| 2063 void BeforeEncode(ValueSerializer* serializer) override { | |
| 2064 serializer->TransferSharedArrayBuffer(0, input_buffer_); | |
| 2065 } | |
| 2066 | |
| 2067 void BeforeDecode(ValueDeserializer* deserializer) override { | 2072 void BeforeDecode(ValueDeserializer* deserializer) override { |
| 2068 deserializer->TransferSharedArrayBuffer(0, output_buffer_); | 2073 deserializer->TransferSharedArrayBuffer(0, output_buffer_); |
| 2069 } | 2074 } |
| 2070 | 2075 |
| 2071 static void SetUpTestCase() { | 2076 static void SetUpTestCase() { |
| 2072 flag_was_enabled_ = i::FLAG_harmony_sharedarraybuffer; | 2077 flag_was_enabled_ = i::FLAG_harmony_sharedarraybuffer; |
| 2073 i::FLAG_harmony_sharedarraybuffer = true; | 2078 i::FLAG_harmony_sharedarraybuffer = true; |
| 2074 ValueSerializerTest::SetUpTestCase(); | 2079 ValueSerializerTest::SetUpTestCase(); |
| 2075 } | 2080 } |
| 2076 | 2081 |
| 2077 static void TearDownTestCase() { | 2082 static void TearDownTestCase() { |
| 2078 ValueSerializerTest::TearDownTestCase(); | 2083 ValueSerializerTest::TearDownTestCase(); |
| 2079 i::FLAG_harmony_sharedarraybuffer = flag_was_enabled_; | 2084 i::FLAG_harmony_sharedarraybuffer = flag_was_enabled_; |
| 2080 flag_was_enabled_ = false; | 2085 flag_was_enabled_ = false; |
| 2081 } | 2086 } |
| 2082 | 2087 |
| 2088 protected: |
| 2089 // GMock doesn't use the "override" keyword. |
| 2090 #if __clang__ |
| 2091 #pragma clang diagnostic push |
| 2092 #pragma clang diagnostic ignored "-Winconsistent-missing-override" |
| 2093 #endif |
| 2094 |
| 2095 class SerializerDelegate : public ValueSerializer::Delegate { |
| 2096 public: |
| 2097 explicit SerializerDelegate( |
| 2098 ValueSerializerTestWithSharedArrayBufferTransfer* test) |
| 2099 : test_(test) {} |
| 2100 MOCK_METHOD2(GetSharedArrayBufferId, |
| 2101 Maybe<uint32_t>(Isolate* isolate, |
| 2102 Local<SharedArrayBuffer> shared_array_buffer)); |
| 2103 void ThrowDataCloneError(Local<String> message) override { |
| 2104 test_->isolate()->ThrowException(Exception::Error(message)); |
| 2105 } |
| 2106 |
| 2107 private: |
| 2108 ValueSerializerTestWithSharedArrayBufferTransfer* test_; |
| 2109 }; |
| 2110 |
| 2111 #if __clang__ |
| 2112 #pragma clang diagnostic pop |
| 2113 #endif |
| 2114 |
| 2115 ValueSerializer::Delegate* GetSerializerDelegate() override { |
| 2116 return &serializer_delegate_; |
| 2117 } |
| 2118 |
| 2119 SerializerDelegate serializer_delegate_; |
| 2120 |
| 2083 private: | 2121 private: |
| 2084 static bool flag_was_enabled_; | 2122 static bool flag_was_enabled_; |
| 2085 uint8_t data_[kTestByteLength]; | 2123 uint8_t data_[kTestByteLength]; |
| 2086 Local<SharedArrayBuffer> input_buffer_; | 2124 Local<SharedArrayBuffer> input_buffer_; |
| 2087 Local<SharedArrayBuffer> output_buffer_; | 2125 Local<SharedArrayBuffer> output_buffer_; |
| 2088 }; | 2126 }; |
| 2089 | 2127 |
| 2090 bool ValueSerializerTestWithSharedArrayBufferTransfer::flag_was_enabled_ = | 2128 bool ValueSerializerTestWithSharedArrayBufferTransfer::flag_was_enabled_ = |
| 2091 false; | 2129 false; |
| 2092 | 2130 |
| 2093 TEST_F(ValueSerializerTestWithSharedArrayBufferTransfer, | 2131 TEST_F(ValueSerializerTestWithSharedArrayBufferTransfer, |
| 2094 RoundTripSharedArrayBufferTransfer) { | 2132 RoundTripSharedArrayBufferTransfer) { |
| 2133 EXPECT_CALL(serializer_delegate_, |
| 2134 GetSharedArrayBufferId(isolate(), input_buffer())) |
| 2135 .WillRepeatedly(Return(Just(0U))); |
| 2136 |
| 2095 RoundTripTest([this]() { return input_buffer(); }, | 2137 RoundTripTest([this]() { return input_buffer(); }, |
| 2096 [this](Local<Value> value) { | 2138 [this](Local<Value> value) { |
| 2097 ASSERT_TRUE(value->IsSharedArrayBuffer()); | 2139 ASSERT_TRUE(value->IsSharedArrayBuffer()); |
| 2098 EXPECT_EQ(output_buffer(), value); | 2140 EXPECT_EQ(output_buffer(), value); |
| 2099 EXPECT_TRUE(EvaluateScriptForResultBool( | 2141 EXPECT_TRUE(EvaluateScriptForResultBool( |
| 2100 "new Uint8Array(result).toString() === '0,1,128,255'")); | 2142 "new Uint8Array(result).toString() === '0,1,128,255'")); |
| 2101 }); | 2143 }); |
| 2102 RoundTripTest( | 2144 RoundTripTest( |
| 2103 [this]() { | 2145 [this]() { |
| 2104 Local<Object> object = Object::New(isolate()); | 2146 Local<Object> object = Object::New(isolate()); |
| (...skipping 11 matching lines...) Expand all Loading... |
| 2116 }, | 2158 }, |
| 2117 [this](Local<Value> value) { | 2159 [this](Local<Value> value) { |
| 2118 EXPECT_TRUE(EvaluateScriptForResultBool( | 2160 EXPECT_TRUE(EvaluateScriptForResultBool( |
| 2119 "result.a instanceof SharedArrayBuffer")); | 2161 "result.a instanceof SharedArrayBuffer")); |
| 2120 EXPECT_TRUE(EvaluateScriptForResultBool("result.a === result.b")); | 2162 EXPECT_TRUE(EvaluateScriptForResultBool("result.a === result.b")); |
| 2121 EXPECT_TRUE(EvaluateScriptForResultBool( | 2163 EXPECT_TRUE(EvaluateScriptForResultBool( |
| 2122 "new Uint8Array(result.a).toString() === '0,1,128,255'")); | 2164 "new Uint8Array(result.a).toString() === '0,1,128,255'")); |
| 2123 }); | 2165 }); |
| 2124 } | 2166 } |
| 2125 | 2167 |
| 2126 TEST_F(ValueSerializerTestWithSharedArrayBufferTransfer, | |
| 2127 SharedArrayBufferMustBeTransferred) { | |
| 2128 // A SharedArrayBuffer which was not marked for transfer should fail encoding. | |
| 2129 InvalidEncodeTest("new SharedArrayBuffer(32)"); | |
| 2130 } | |
| 2131 | |
| 2132 TEST_F(ValueSerializerTest, UnsupportedHostObject) { | 2168 TEST_F(ValueSerializerTest, UnsupportedHostObject) { |
| 2133 InvalidEncodeTest("new ExampleHostObject()"); | 2169 InvalidEncodeTest("new ExampleHostObject()"); |
| 2134 InvalidEncodeTest("({ a: new ExampleHostObject() })"); | 2170 InvalidEncodeTest("({ a: new ExampleHostObject() })"); |
| 2135 } | 2171 } |
| 2136 | 2172 |
| 2137 class ValueSerializerTestWithHostObject : public ValueSerializerTest { | 2173 class ValueSerializerTestWithHostObject : public ValueSerializerTest { |
| 2138 protected: | 2174 protected: |
| 2139 ValueSerializerTestWithHostObject() : serializer_delegate_(this) {} | 2175 ValueSerializerTestWithHostObject() : serializer_delegate_(this) {} |
| 2140 | 2176 |
| 2141 static const uint8_t kExampleHostObjectTag; | 2177 static const uint8_t kExampleHostObjectTag; |
| (...skipping 385 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 2527 InvalidDecodeTest(raw); | 2563 InvalidDecodeTest(raw); |
| 2528 } | 2564 } |
| 2529 | 2565 |
| 2530 TEST_F(ValueSerializerTestWithWasm, DecodeWasmModuleWithInvalidDataLength) { | 2566 TEST_F(ValueSerializerTestWithWasm, DecodeWasmModuleWithInvalidDataLength) { |
| 2531 InvalidDecodeTest({0xff, 0x09, 0x3f, 0x00, 0x57, 0x79, 0x7f, 0x00}); | 2567 InvalidDecodeTest({0xff, 0x09, 0x3f, 0x00, 0x57, 0x79, 0x7f, 0x00}); |
| 2532 InvalidDecodeTest({0xff, 0x09, 0x3f, 0x00, 0x57, 0x79, 0x00, 0x7f}); | 2568 InvalidDecodeTest({0xff, 0x09, 0x3f, 0x00, 0x57, 0x79, 0x00, 0x7f}); |
| 2533 } | 2569 } |
| 2534 | 2570 |
| 2535 } // namespace | 2571 } // namespace |
| 2536 } // namespace v8 | 2572 } // namespace v8 |
| OLD | NEW |