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

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

Issue 2570433005: 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
« 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"
(...skipping 111 matching lines...) Expand 10 before | Expand all | Expand 10 after
122 const EncodedDataFunctor& encoded_data_functor) { 122 const EncodedDataFunctor& encoded_data_functor) {
123 Context::Scope scope(serialization_context()); 123 Context::Scope scope(serialization_context());
124 TryCatch try_catch(isolate()); 124 TryCatch try_catch(isolate());
125 Local<Value> input_value = input_functor(); 125 Local<Value> input_value = input_functor();
126 std::vector<uint8_t> buffer; 126 std::vector<uint8_t> buffer;
127 ASSERT_TRUE(DoEncode(input_value).To(&buffer)); 127 ASSERT_TRUE(DoEncode(input_value).To(&buffer));
128 ASSERT_FALSE(try_catch.HasCaught()); 128 ASSERT_FALSE(try_catch.HasCaught());
129 encoded_data_functor(buffer); 129 encoded_data_functor(buffer);
130 } 130 }
131 131
132 template <typename InputFunctor, typename MessageFunctor>
133 void InvalidEncodeTest(const InputFunctor& input_functor,
134 const MessageFunctor& functor) {
135 Context::Scope scope(serialization_context());
136 TryCatch try_catch(isolate());
137 Local<Value> input_value = input_functor();
138 ASSERT_TRUE(DoEncode(input_value).IsNothing());
139 functor(try_catch.Message());
140 }
141
132 template <typename MessageFunctor> 142 template <typename MessageFunctor>
133 void InvalidEncodeTest(const char* source, const MessageFunctor& functor) { 143 void InvalidEncodeTest(const char* source, const MessageFunctor& functor) {
134 Context::Scope scope(serialization_context()); 144 InvalidEncodeTest(
135 TryCatch try_catch(isolate()); 145 [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 } 146 }
140 147
141 void InvalidEncodeTest(const char* source) { 148 void InvalidEncodeTest(const char* source) {
142 InvalidEncodeTest(source, [](Local<Message>) {}); 149 InvalidEncodeTest(source, [](Local<Message>) {});
143 } 150 }
144 151
145 template <typename OutputFunctor> 152 template <typename OutputFunctor>
146 void DecodeTest(const std::vector<uint8_t>& data, 153 void DecodeTest(const std::vector<uint8_t>& data,
147 const OutputFunctor& output_functor) { 154 const OutputFunctor& output_functor) {
148 Local<Context> context = deserialization_context(); 155 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. 2042 // Byte offset in range, offset + length out of range.
2036 InvalidDecodeTest( 2043 InvalidDecodeTest(
2037 {0xff, 0x09, 0x42, 0x02, 0x00, 0x00, 0x56, 0x3f, 0x01, 0x03}); 2044 {0xff, 0x09, 0x42, 0x02, 0x00, 0x00, 0x56, 0x3f, 0x01, 0x03});
2038 } 2045 }
2039 2046
2040 class ValueSerializerTestWithSharedArrayBufferTransfer 2047 class ValueSerializerTestWithSharedArrayBufferTransfer
2041 : public ValueSerializerTest { 2048 : public ValueSerializerTest {
2042 protected: 2049 protected:
2043 static const size_t kTestByteLength = 4; 2050 static const size_t kTestByteLength = 4;
2044 2051
2045 ValueSerializerTestWithSharedArrayBufferTransfer() { 2052 ValueSerializerTestWithSharedArrayBufferTransfer()
2053 : serializer_delegate_(this),
2054 should_transfer_shared_array_buffer_(false) {
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 { 2072 void BeforeEncode(ValueSerializer* serializer) override {
2064 serializer->TransferSharedArrayBuffer(0, input_buffer_); 2073 if (should_transfer_shared_array_buffer_) {
2074 // TODO(binji): In general, this should not be called, because
2075 // SharedArrayBuffers are not allowed in the transfer list. We allow it
2076 // for testing for now, but this function will be removed soon.
2077 serializer->TransferSharedArrayBuffer(0, input_buffer_);
2078 }
2065 } 2079 }
2066 2080
2067 void BeforeDecode(ValueDeserializer* deserializer) override { 2081 void BeforeDecode(ValueDeserializer* deserializer) override {
2068 deserializer->TransferSharedArrayBuffer(0, output_buffer_); 2082 deserializer->TransferSharedArrayBuffer(0, output_buffer_);
2069 } 2083 }
2070 2084
2071 static void SetUpTestCase() { 2085 static void SetUpTestCase() {
2072 flag_was_enabled_ = i::FLAG_harmony_sharedarraybuffer; 2086 flag_was_enabled_ = i::FLAG_harmony_sharedarraybuffer;
2073 i::FLAG_harmony_sharedarraybuffer = true; 2087 i::FLAG_harmony_sharedarraybuffer = true;
2074 ValueSerializerTest::SetUpTestCase(); 2088 ValueSerializerTest::SetUpTestCase();
2075 } 2089 }
2076 2090
2077 static void TearDownTestCase() { 2091 static void TearDownTestCase() {
2078 ValueSerializerTest::TearDownTestCase(); 2092 ValueSerializerTest::TearDownTestCase();
2079 i::FLAG_harmony_sharedarraybuffer = flag_was_enabled_; 2093 i::FLAG_harmony_sharedarraybuffer = flag_was_enabled_;
2080 flag_was_enabled_ = false; 2094 flag_was_enabled_ = false;
2081 } 2095 }
2082 2096
2097 protected:
2098 // GMock doesn't use the "override" keyword.
2099 #if __clang__
2100 #pragma clang diagnostic push
2101 #pragma clang diagnostic ignored "-Winconsistent-missing-override"
2102 #endif
2103
2104 class SerializerDelegate : public ValueSerializer::Delegate {
2105 public:
2106 explicit SerializerDelegate(
2107 ValueSerializerTestWithSharedArrayBufferTransfer* test)
2108 : test_(test) {}
2109 MOCK_METHOD2(TransferSharedArrayBuffer,
2110 Maybe<uint32_t>(Isolate* isolate, Local<Object> object));
2111 void ThrowDataCloneError(Local<String> message) override {
2112 test_->isolate()->ThrowException(Exception::Error(message));
2113 }
2114
2115 private:
2116 ValueSerializerTestWithSharedArrayBufferTransfer* test_;
2117 };
2118
2119 #if __clang__
2120 #pragma clang diagnostic pop
2121 #endif
2122
2123 ValueSerializer::Delegate* GetSerializerDelegate() override {
2124 return &serializer_delegate_;
2125 }
2126
2127 SerializerDelegate serializer_delegate_;
2128 bool should_transfer_shared_array_buffer_;
2129
2083 private: 2130 private:
2084 static bool flag_was_enabled_; 2131 static bool flag_was_enabled_;
2085 uint8_t data_[kTestByteLength]; 2132 uint8_t data_[kTestByteLength];
2086 Local<SharedArrayBuffer> input_buffer_; 2133 Local<SharedArrayBuffer> input_buffer_;
2087 Local<SharedArrayBuffer> output_buffer_; 2134 Local<SharedArrayBuffer> output_buffer_;
2088 }; 2135 };
2089 2136
2090 bool ValueSerializerTestWithSharedArrayBufferTransfer::flag_was_enabled_ = 2137 bool ValueSerializerTestWithSharedArrayBufferTransfer::flag_was_enabled_ =
2091 false; 2138 false;
2092 2139
2093 TEST_F(ValueSerializerTestWithSharedArrayBufferTransfer, 2140 TEST_F(ValueSerializerTestWithSharedArrayBufferTransfer,
2094 RoundTripSharedArrayBufferTransfer) { 2141 RoundTripSharedArrayBufferTransfer) {
2142 should_transfer_shared_array_buffer_ = false;
2143 EXPECT_CALL(serializer_delegate_, TransferSharedArrayBuffer(isolate(), _))
2144 .WillRepeatedly(Invoke([this](Isolate*, Local<Object> object) {
jbroman 2016/12/13 20:56:00 nit: Since this just validates an argument and ret
binji 2016/12/14 23:58:30 Done.
2145 EXPECT_EQ(input_buffer(), object);
2146 return Just(0U);
2147 }));
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 12 matching lines...) Expand all
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, 2180 TEST_F(ValueSerializerTestWithSharedArrayBufferTransfer,
2127 SharedArrayBufferMustBeTransferred) { 2181 SharedArrayBufferMustNotBeTransferred) {
2128 // A SharedArrayBuffer which was not marked for transfer should fail encoding. 2182 // A SharedArrayBuffer which was marked for transfer should fail encoding.
2129 InvalidEncodeTest("new SharedArrayBuffer(32)"); 2183 should_transfer_shared_array_buffer_ = true;
2184 InvalidEncodeTest([this]() { return input_buffer(); }, [](Local<Message>) {});
2130 } 2185 }
2131 2186
2132 TEST_F(ValueSerializerTest, UnsupportedHostObject) { 2187 TEST_F(ValueSerializerTest, UnsupportedHostObject) {
2133 InvalidEncodeTest("new ExampleHostObject()"); 2188 InvalidEncodeTest("new ExampleHostObject()");
2134 InvalidEncodeTest("({ a: new ExampleHostObject() })"); 2189 InvalidEncodeTest("({ a: new ExampleHostObject() })");
2135 } 2190 }
2136 2191
2137 class ValueSerializerTestWithHostObject : public ValueSerializerTest { 2192 class ValueSerializerTestWithHostObject : public ValueSerializerTest {
2138 protected: 2193 protected:
2139 ValueSerializerTestWithHostObject() : serializer_delegate_(this) {} 2194 ValueSerializerTestWithHostObject() : serializer_delegate_(this) {}
(...skipping 387 matching lines...) Expand 10 before | Expand all | Expand 10 after
2527 InvalidDecodeTest(raw); 2582 InvalidDecodeTest(raw);
2528 } 2583 }
2529 2584
2530 TEST_F(ValueSerializerTestWithWasm, DecodeWasmModuleWithInvalidDataLength) { 2585 TEST_F(ValueSerializerTestWithWasm, DecodeWasmModuleWithInvalidDataLength) {
2531 InvalidDecodeTest({0xff, 0x09, 0x3f, 0x00, 0x57, 0x79, 0x7f, 0x00}); 2586 InvalidDecodeTest({0xff, 0x09, 0x3f, 0x00, 0x57, 0x79, 0x7f, 0x00});
2532 InvalidDecodeTest({0xff, 0x09, 0x3f, 0x00, 0x57, 0x79, 0x00, 0x7f}); 2587 InvalidDecodeTest({0xff, 0x09, 0x3f, 0x00, 0x57, 0x79, 0x00, 0x7f});
2533 } 2588 }
2534 2589
2535 } // namespace 2590 } // namespace
2536 } // namespace v8 2591 } // 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