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

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

Issue 2570433005: Disallow passing a SharedArrayBuffer in the transfer list. (Closed)
Patch Set: fix comments 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
« src/value-serializer.cc ('K') | « 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;
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
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
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),
2055 should_transfer_shared_array_buffer_(false) {
2046 const uint8_t data[kTestByteLength] = {0x00, 0x01, 0x80, 0xff}; 2056 const uint8_t data[kTestByteLength] = {0x00, 0x01, 0x80, 0xff};
2047 memcpy(data_, data, kTestByteLength); 2057 memcpy(data_, data, kTestByteLength);
2048 { 2058 {
2049 Context::Scope scope(serialization_context()); 2059 Context::Scope scope(serialization_context());
2050 input_buffer_ = 2060 input_buffer_ =
2051 SharedArrayBuffer::New(isolate(), &data_, kTestByteLength); 2061 SharedArrayBuffer::New(isolate(), &data_, kTestByteLength);
2052 } 2062 }
2053 { 2063 {
2054 Context::Scope scope(deserialization_context()); 2064 Context::Scope scope(deserialization_context());
2055 output_buffer_ = 2065 output_buffer_ =
2056 SharedArrayBuffer::New(isolate(), &data_, kTestByteLength); 2066 SharedArrayBuffer::New(isolate(), &data_, kTestByteLength);
2057 } 2067 }
2058 } 2068 }
2059 2069
2060 const Local<SharedArrayBuffer>& input_buffer() { return input_buffer_; } 2070 const Local<SharedArrayBuffer>& input_buffer() { return input_buffer_; }
2061 const Local<SharedArrayBuffer>& output_buffer() { return output_buffer_; } 2071 const Local<SharedArrayBuffer>& output_buffer() { return output_buffer_; }
2062 2072
2063 void BeforeEncode(ValueSerializer* serializer) override { 2073 void BeforeEncode(ValueSerializer* serializer) override {
2064 serializer->TransferSharedArrayBuffer(0, input_buffer_); 2074 if (should_transfer_shared_array_buffer_) {
2075 // TODO(binji): In general, this should not be called, because
2076 // SharedArrayBuffers are not allowed in the transfer list. We allow it
2077 // for testing for now, but this function will be removed soon.
2078 serializer->TransferSharedArrayBuffer(0, input_buffer_);
2079 }
2065 } 2080 }
2066 2081
2067 void BeforeDecode(ValueDeserializer* deserializer) override { 2082 void BeforeDecode(ValueDeserializer* deserializer) override {
2068 deserializer->TransferSharedArrayBuffer(0, output_buffer_); 2083 deserializer->TransferSharedArrayBuffer(0, output_buffer_);
2069 } 2084 }
2070 2085
2071 static void SetUpTestCase() { 2086 static void SetUpTestCase() {
2072 flag_was_enabled_ = i::FLAG_harmony_sharedarraybuffer; 2087 flag_was_enabled_ = i::FLAG_harmony_sharedarraybuffer;
2073 i::FLAG_harmony_sharedarraybuffer = true; 2088 i::FLAG_harmony_sharedarraybuffer = true;
2074 ValueSerializerTest::SetUpTestCase(); 2089 ValueSerializerTest::SetUpTestCase();
2075 } 2090 }
2076 2091
2077 static void TearDownTestCase() { 2092 static void TearDownTestCase() {
2078 ValueSerializerTest::TearDownTestCase(); 2093 ValueSerializerTest::TearDownTestCase();
2079 i::FLAG_harmony_sharedarraybuffer = flag_was_enabled_; 2094 i::FLAG_harmony_sharedarraybuffer = flag_was_enabled_;
2080 flag_was_enabled_ = false; 2095 flag_was_enabled_ = false;
2081 } 2096 }
2082 2097
2098 protected:
2099 // GMock doesn't use the "override" keyword.
2100 #if __clang__
2101 #pragma clang diagnostic push
2102 #pragma clang diagnostic ignored "-Winconsistent-missing-override"
2103 #endif
2104
2105 class SerializerDelegate : public ValueSerializer::Delegate {
2106 public:
2107 explicit SerializerDelegate(
2108 ValueSerializerTestWithSharedArrayBufferTransfer* test)
2109 : test_(test) {}
2110 MOCK_METHOD2(TransferSharedArrayBuffer,
2111 Maybe<uint32_t>(Isolate* isolate,
2112 Local<SharedArrayBuffer> shared_array_buffer));
2113 void ThrowDataCloneError(Local<String> message) override {
2114 test_->isolate()->ThrowException(Exception::Error(message));
2115 }
2116
2117 private:
2118 ValueSerializerTestWithSharedArrayBufferTransfer* test_;
2119 };
2120
2121 #if __clang__
2122 #pragma clang diagnostic pop
2123 #endif
2124
2125 ValueSerializer::Delegate* GetSerializerDelegate() override {
2126 return &serializer_delegate_;
2127 }
2128
2129 SerializerDelegate serializer_delegate_;
2130 bool should_transfer_shared_array_buffer_;
jbroman 2016/12/15 16:15:33 This seems to only ever be set to false. Should it
binji 2016/12/15 19:31:20 Done, forgot to remove after removing the test tha
2131
2083 private: 2132 private:
2084 static bool flag_was_enabled_; 2133 static bool flag_was_enabled_;
2085 uint8_t data_[kTestByteLength]; 2134 uint8_t data_[kTestByteLength];
2086 Local<SharedArrayBuffer> input_buffer_; 2135 Local<SharedArrayBuffer> input_buffer_;
2087 Local<SharedArrayBuffer> output_buffer_; 2136 Local<SharedArrayBuffer> output_buffer_;
2088 }; 2137 };
2089 2138
2090 bool ValueSerializerTestWithSharedArrayBufferTransfer::flag_was_enabled_ = 2139 bool ValueSerializerTestWithSharedArrayBufferTransfer::flag_was_enabled_ =
2091 false; 2140 false;
2092 2141
2093 TEST_F(ValueSerializerTestWithSharedArrayBufferTransfer, 2142 TEST_F(ValueSerializerTestWithSharedArrayBufferTransfer,
2094 RoundTripSharedArrayBufferTransfer) { 2143 RoundTripSharedArrayBufferTransfer) {
2144 should_transfer_shared_array_buffer_ = false;
2145 EXPECT_CALL(serializer_delegate_,
2146 TransferSharedArrayBuffer(isolate(), input_buffer()))
2147 .WillRepeatedly(Return(Just(0U)));
2148
2095 RoundTripTest([this]() { return input_buffer(); }, 2149 RoundTripTest([this]() { return input_buffer(); },
2096 [this](Local<Value> value) { 2150 [this](Local<Value> value) {
2097 ASSERT_TRUE(value->IsSharedArrayBuffer()); 2151 ASSERT_TRUE(value->IsSharedArrayBuffer());
2098 EXPECT_EQ(output_buffer(), value); 2152 EXPECT_EQ(output_buffer(), value);
2099 EXPECT_TRUE(EvaluateScriptForResultBool( 2153 EXPECT_TRUE(EvaluateScriptForResultBool(
2100 "new Uint8Array(result).toString() === '0,1,128,255'")); 2154 "new Uint8Array(result).toString() === '0,1,128,255'"));
2101 }); 2155 });
2102 RoundTripTest( 2156 RoundTripTest(
2103 [this]() { 2157 [this]() {
2104 Local<Object> object = Object::New(isolate()); 2158 Local<Object> object = Object::New(isolate());
(...skipping 11 matching lines...) Expand all
2116 }, 2170 },
2117 [this](Local<Value> value) { 2171 [this](Local<Value> value) {
2118 EXPECT_TRUE(EvaluateScriptForResultBool( 2172 EXPECT_TRUE(EvaluateScriptForResultBool(
2119 "result.a instanceof SharedArrayBuffer")); 2173 "result.a instanceof SharedArrayBuffer"));
2120 EXPECT_TRUE(EvaluateScriptForResultBool("result.a === result.b")); 2174 EXPECT_TRUE(EvaluateScriptForResultBool("result.a === result.b"));
2121 EXPECT_TRUE(EvaluateScriptForResultBool( 2175 EXPECT_TRUE(EvaluateScriptForResultBool(
2122 "new Uint8Array(result.a).toString() === '0,1,128,255'")); 2176 "new Uint8Array(result.a).toString() === '0,1,128,255'"));
2123 }); 2177 });
2124 } 2178 }
2125 2179
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) { 2180 TEST_F(ValueSerializerTest, UnsupportedHostObject) {
2133 InvalidEncodeTest("new ExampleHostObject()"); 2181 InvalidEncodeTest("new ExampleHostObject()");
2134 InvalidEncodeTest("({ a: new ExampleHostObject() })"); 2182 InvalidEncodeTest("({ a: new ExampleHostObject() })");
2135 } 2183 }
2136 2184
2137 class ValueSerializerTestWithHostObject : public ValueSerializerTest { 2185 class ValueSerializerTestWithHostObject : public ValueSerializerTest {
2138 protected: 2186 protected:
2139 ValueSerializerTestWithHostObject() : serializer_delegate_(this) {} 2187 ValueSerializerTestWithHostObject() : serializer_delegate_(this) {}
2140 2188
2141 static const uint8_t kExampleHostObjectTag; 2189 static const uint8_t kExampleHostObjectTag;
(...skipping 385 matching lines...) Expand 10 before | Expand all | Expand 10 after
2527 InvalidDecodeTest(raw); 2575 InvalidDecodeTest(raw);
2528 } 2576 }
2529 2577
2530 TEST_F(ValueSerializerTestWithWasm, DecodeWasmModuleWithInvalidDataLength) { 2578 TEST_F(ValueSerializerTestWithWasm, DecodeWasmModuleWithInvalidDataLength) {
2531 InvalidDecodeTest({0xff, 0x09, 0x3f, 0x00, 0x57, 0x79, 0x7f, 0x00}); 2579 InvalidDecodeTest({0xff, 0x09, 0x3f, 0x00, 0x57, 0x79, 0x7f, 0x00});
2532 InvalidDecodeTest({0xff, 0x09, 0x3f, 0x00, 0x57, 0x79, 0x00, 0x7f}); 2580 InvalidDecodeTest({0xff, 0x09, 0x3f, 0x00, 0x57, 0x79, 0x00, 0x7f});
2533 } 2581 }
2534 2582
2535 } // namespace 2583 } // namespace
2536 } // namespace v8 2584 } // namespace v8
OLDNEW
« src/value-serializer.cc ('K') | « src/value-serializer.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698