Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(184)

Side by Side Diff: test/unittests/value-serializer-unittest.cc

Issue 2579963002: Revert of Disallow passing a SharedArrayBuffer in the transfer list. (Closed)
Patch Set: Created 4 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « src/value-serializer.cc ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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;
23 22
24 class ValueSerializerTest : public TestWithIsolate { 23 class ValueSerializerTest : public TestWithIsolate {
25 protected: 24 protected:
26 ValueSerializerTest() 25 ValueSerializerTest()
27 : serialization_context_(Context::New(isolate())), 26 : serialization_context_(Context::New(isolate())),
28 deserialization_context_(Context::New(isolate())) { 27 deserialization_context_(Context::New(isolate())) {
29 // Create a host object type that can be tested through 28 // Create a host object type that can be tested through
30 // serialization/deserialization delegates below. 29 // serialization/deserialization delegates below.
31 Local<FunctionTemplate> function_template = v8::FunctionTemplate::New( 30 Local<FunctionTemplate> function_template = v8::FunctionTemplate::New(
32 isolate(), [](const FunctionCallbackInfo<Value>& args) { 31 isolate(), [](const FunctionCallbackInfo<Value>& args) {
(...skipping 90 matching lines...) Expand 10 before | Expand all | Expand 10 after
123 const EncodedDataFunctor& encoded_data_functor) { 122 const EncodedDataFunctor& encoded_data_functor) {
124 Context::Scope scope(serialization_context()); 123 Context::Scope scope(serialization_context());
125 TryCatch try_catch(isolate()); 124 TryCatch try_catch(isolate());
126 Local<Value> input_value = input_functor(); 125 Local<Value> input_value = input_functor();
127 std::vector<uint8_t> buffer; 126 std::vector<uint8_t> buffer;
128 ASSERT_TRUE(DoEncode(input_value).To(&buffer)); 127 ASSERT_TRUE(DoEncode(input_value).To(&buffer));
129 ASSERT_FALSE(try_catch.HasCaught()); 128 ASSERT_FALSE(try_catch.HasCaught());
130 encoded_data_functor(buffer); 129 encoded_data_functor(buffer);
131 } 130 }
132 131
133 template <typename InputFunctor, typename MessageFunctor> 132 template <typename MessageFunctor>
134 void InvalidEncodeTest(const InputFunctor& input_functor, 133 void InvalidEncodeTest(const char* source, const MessageFunctor& functor) {
135 const MessageFunctor& functor) {
136 Context::Scope scope(serialization_context()); 134 Context::Scope scope(serialization_context());
137 TryCatch try_catch(isolate()); 135 TryCatch try_catch(isolate());
138 Local<Value> input_value = input_functor(); 136 Local<Value> input_value = EvaluateScriptForInput(source);
139 ASSERT_TRUE(DoEncode(input_value).IsNothing()); 137 ASSERT_TRUE(DoEncode(input_value).IsNothing());
140 functor(try_catch.Message()); 138 functor(try_catch.Message());
141 } 139 }
142 140
143 template <typename MessageFunctor>
144 void InvalidEncodeTest(const char* source, const MessageFunctor& functor) {
145 InvalidEncodeTest(
146 [this, source]() { return EvaluateScriptForInput(source); }, functor);
147 }
148
149 void InvalidEncodeTest(const char* source) { 141 void InvalidEncodeTest(const char* source) {
150 InvalidEncodeTest(source, [](Local<Message>) {}); 142 InvalidEncodeTest(source, [](Local<Message>) {});
151 } 143 }
152 144
153 template <typename OutputFunctor> 145 template <typename OutputFunctor>
154 void DecodeTest(const std::vector<uint8_t>& data, 146 void DecodeTest(const std::vector<uint8_t>& data,
155 const OutputFunctor& output_functor) { 147 const OutputFunctor& output_functor) {
156 Local<Context> context = deserialization_context(); 148 Local<Context> context = deserialization_context();
157 Context::Scope scope(context); 149 Context::Scope scope(context);
158 TryCatch try_catch(isolate()); 150 TryCatch try_catch(isolate());
(...skipping 1884 matching lines...) Expand 10 before | Expand all | Expand 10 after
2043 // Byte offset in range, offset + length out of range. 2035 // Byte offset in range, offset + length out of range.
2044 InvalidDecodeTest( 2036 InvalidDecodeTest(
2045 {0xff, 0x09, 0x42, 0x02, 0x00, 0x00, 0x56, 0x3f, 0x01, 0x03}); 2037 {0xff, 0x09, 0x42, 0x02, 0x00, 0x00, 0x56, 0x3f, 0x01, 0x03});
2046 } 2038 }
2047 2039
2048 class ValueSerializerTestWithSharedArrayBufferTransfer 2040 class ValueSerializerTestWithSharedArrayBufferTransfer
2049 : public ValueSerializerTest { 2041 : public ValueSerializerTest {
2050 protected: 2042 protected:
2051 static const size_t kTestByteLength = 4; 2043 static const size_t kTestByteLength = 4;
2052 2044
2053 ValueSerializerTestWithSharedArrayBufferTransfer() 2045 ValueSerializerTestWithSharedArrayBufferTransfer() {
2054 : serializer_delegate_(this) {
2055 const uint8_t data[kTestByteLength] = {0x00, 0x01, 0x80, 0xff}; 2046 const uint8_t data[kTestByteLength] = {0x00, 0x01, 0x80, 0xff};
2056 memcpy(data_, data, kTestByteLength); 2047 memcpy(data_, data, kTestByteLength);
2057 { 2048 {
2058 Context::Scope scope(serialization_context()); 2049 Context::Scope scope(serialization_context());
2059 input_buffer_ = 2050 input_buffer_ =
2060 SharedArrayBuffer::New(isolate(), &data_, kTestByteLength); 2051 SharedArrayBuffer::New(isolate(), &data_, kTestByteLength);
2061 } 2052 }
2062 { 2053 {
2063 Context::Scope scope(deserialization_context()); 2054 Context::Scope scope(deserialization_context());
2064 output_buffer_ = 2055 output_buffer_ =
2065 SharedArrayBuffer::New(isolate(), &data_, kTestByteLength); 2056 SharedArrayBuffer::New(isolate(), &data_, kTestByteLength);
2066 } 2057 }
2067 } 2058 }
2068 2059
2069 const Local<SharedArrayBuffer>& input_buffer() { return input_buffer_; } 2060 const Local<SharedArrayBuffer>& input_buffer() { return input_buffer_; }
2070 const Local<SharedArrayBuffer>& output_buffer() { return output_buffer_; } 2061 const Local<SharedArrayBuffer>& output_buffer() { return output_buffer_; }
2071 2062
2063 void BeforeEncode(ValueSerializer* serializer) override {
2064 serializer->TransferSharedArrayBuffer(0, input_buffer_);
2065 }
2066
2072 void BeforeDecode(ValueDeserializer* deserializer) override { 2067 void BeforeDecode(ValueDeserializer* deserializer) override {
2073 deserializer->TransferSharedArrayBuffer(0, output_buffer_); 2068 deserializer->TransferSharedArrayBuffer(0, output_buffer_);
2074 } 2069 }
2075 2070
2076 static void SetUpTestCase() { 2071 static void SetUpTestCase() {
2077 flag_was_enabled_ = i::FLAG_harmony_sharedarraybuffer; 2072 flag_was_enabled_ = i::FLAG_harmony_sharedarraybuffer;
2078 i::FLAG_harmony_sharedarraybuffer = true; 2073 i::FLAG_harmony_sharedarraybuffer = true;
2079 ValueSerializerTest::SetUpTestCase(); 2074 ValueSerializerTest::SetUpTestCase();
2080 } 2075 }
2081 2076
2082 static void TearDownTestCase() { 2077 static void TearDownTestCase() {
2083 ValueSerializerTest::TearDownTestCase(); 2078 ValueSerializerTest::TearDownTestCase();
2084 i::FLAG_harmony_sharedarraybuffer = flag_was_enabled_; 2079 i::FLAG_harmony_sharedarraybuffer = flag_was_enabled_;
2085 flag_was_enabled_ = false; 2080 flag_was_enabled_ = false;
2086 } 2081 }
2087 2082
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
2121 private: 2083 private:
2122 static bool flag_was_enabled_; 2084 static bool flag_was_enabled_;
2123 uint8_t data_[kTestByteLength]; 2085 uint8_t data_[kTestByteLength];
2124 Local<SharedArrayBuffer> input_buffer_; 2086 Local<SharedArrayBuffer> input_buffer_;
2125 Local<SharedArrayBuffer> output_buffer_; 2087 Local<SharedArrayBuffer> output_buffer_;
2126 }; 2088 };
2127 2089
2128 bool ValueSerializerTestWithSharedArrayBufferTransfer::flag_was_enabled_ = 2090 bool ValueSerializerTestWithSharedArrayBufferTransfer::flag_was_enabled_ =
2129 false; 2091 false;
2130 2092
2131 TEST_F(ValueSerializerTestWithSharedArrayBufferTransfer, 2093 TEST_F(ValueSerializerTestWithSharedArrayBufferTransfer,
2132 RoundTripSharedArrayBufferTransfer) { 2094 RoundTripSharedArrayBufferTransfer) {
2133 EXPECT_CALL(serializer_delegate_,
2134 GetSharedArrayBufferId(isolate(), input_buffer()))
2135 .WillRepeatedly(Return(Just(0U)));
2136
2137 RoundTripTest([this]() { return input_buffer(); }, 2095 RoundTripTest([this]() { return input_buffer(); },
2138 [this](Local<Value> value) { 2096 [this](Local<Value> value) {
2139 ASSERT_TRUE(value->IsSharedArrayBuffer()); 2097 ASSERT_TRUE(value->IsSharedArrayBuffer());
2140 EXPECT_EQ(output_buffer(), value); 2098 EXPECT_EQ(output_buffer(), value);
2141 EXPECT_TRUE(EvaluateScriptForResultBool( 2099 EXPECT_TRUE(EvaluateScriptForResultBool(
2142 "new Uint8Array(result).toString() === '0,1,128,255'")); 2100 "new Uint8Array(result).toString() === '0,1,128,255'"));
2143 }); 2101 });
2144 RoundTripTest( 2102 RoundTripTest(
2145 [this]() { 2103 [this]() {
2146 Local<Object> object = Object::New(isolate()); 2104 Local<Object> object = Object::New(isolate());
(...skipping 11 matching lines...) Expand all
2158 }, 2116 },
2159 [this](Local<Value> value) { 2117 [this](Local<Value> value) {
2160 EXPECT_TRUE(EvaluateScriptForResultBool( 2118 EXPECT_TRUE(EvaluateScriptForResultBool(
2161 "result.a instanceof SharedArrayBuffer")); 2119 "result.a instanceof SharedArrayBuffer"));
2162 EXPECT_TRUE(EvaluateScriptForResultBool("result.a === result.b")); 2120 EXPECT_TRUE(EvaluateScriptForResultBool("result.a === result.b"));
2163 EXPECT_TRUE(EvaluateScriptForResultBool( 2121 EXPECT_TRUE(EvaluateScriptForResultBool(
2164 "new Uint8Array(result.a).toString() === '0,1,128,255'")); 2122 "new Uint8Array(result.a).toString() === '0,1,128,255'"));
2165 }); 2123 });
2166 } 2124 }
2167 2125
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
2168 TEST_F(ValueSerializerTest, UnsupportedHostObject) { 2132 TEST_F(ValueSerializerTest, UnsupportedHostObject) {
2169 InvalidEncodeTest("new ExampleHostObject()"); 2133 InvalidEncodeTest("new ExampleHostObject()");
2170 InvalidEncodeTest("({ a: new ExampleHostObject() })"); 2134 InvalidEncodeTest("({ a: new ExampleHostObject() })");
2171 } 2135 }
2172 2136
2173 class ValueSerializerTestWithHostObject : public ValueSerializerTest { 2137 class ValueSerializerTestWithHostObject : public ValueSerializerTest {
2174 protected: 2138 protected:
2175 ValueSerializerTestWithHostObject() : serializer_delegate_(this) {} 2139 ValueSerializerTestWithHostObject() : serializer_delegate_(this) {}
2176 2140
2177 static const uint8_t kExampleHostObjectTag; 2141 static const uint8_t kExampleHostObjectTag;
(...skipping 385 matching lines...) Expand 10 before | Expand all | Expand 10 after
2563 InvalidDecodeTest(raw); 2527 InvalidDecodeTest(raw);
2564 } 2528 }
2565 2529
2566 TEST_F(ValueSerializerTestWithWasm, DecodeWasmModuleWithInvalidDataLength) { 2530 TEST_F(ValueSerializerTestWithWasm, DecodeWasmModuleWithInvalidDataLength) {
2567 InvalidDecodeTest({0xff, 0x09, 0x3f, 0x00, 0x57, 0x79, 0x7f, 0x00}); 2531 InvalidDecodeTest({0xff, 0x09, 0x3f, 0x00, 0x57, 0x79, 0x7f, 0x00});
2568 InvalidDecodeTest({0xff, 0x09, 0x3f, 0x00, 0x57, 0x79, 0x00, 0x7f}); 2532 InvalidDecodeTest({0xff, 0x09, 0x3f, 0x00, 0x57, 0x79, 0x00, 0x7f});
2569 } 2533 }
2570 2534
2571 } // namespace 2535 } // namespace
2572 } // namespace v8 2536 } // namespace v8
OLDNEW
« no previous file with comments | « src/value-serializer.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698