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 1885 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1896 | 1896 |
1897 TEST_F(ValueSerializerTest, DecodeInvalidDataView) { | 1897 TEST_F(ValueSerializerTest, DecodeInvalidDataView) { |
1898 // Byte offset out of range. | 1898 // Byte offset out of range. |
1899 InvalidDecodeTest( | 1899 InvalidDecodeTest( |
1900 {0xff, 0x09, 0x42, 0x02, 0x00, 0x00, 0x56, 0x3f, 0x03, 0x01}); | 1900 {0xff, 0x09, 0x42, 0x02, 0x00, 0x00, 0x56, 0x3f, 0x03, 0x01}); |
1901 // Byte offset in range, offset + length out of range. | 1901 // Byte offset in range, offset + length out of range. |
1902 InvalidDecodeTest( | 1902 InvalidDecodeTest( |
1903 {0xff, 0x09, 0x42, 0x02, 0x00, 0x00, 0x56, 0x3f, 0x01, 0x03}); | 1903 {0xff, 0x09, 0x42, 0x02, 0x00, 0x00, 0x56, 0x3f, 0x01, 0x03}); |
1904 } | 1904 } |
1905 | 1905 |
| 1906 class ValueSerializerTestWithSharedArrayBufferTransfer |
| 1907 : public ValueSerializerTest { |
| 1908 protected: |
| 1909 static const size_t kTestByteLength = 4; |
| 1910 |
| 1911 ValueSerializerTestWithSharedArrayBufferTransfer() { |
| 1912 const uint8_t data[kTestByteLength] = {0x00, 0x01, 0x80, 0xff}; |
| 1913 memcpy(data_, data, kTestByteLength); |
| 1914 { |
| 1915 Context::Scope scope(serialization_context()); |
| 1916 input_buffer_ = |
| 1917 SharedArrayBuffer::New(isolate(), &data_, kTestByteLength); |
| 1918 } |
| 1919 { |
| 1920 Context::Scope scope(deserialization_context()); |
| 1921 output_buffer_ = |
| 1922 SharedArrayBuffer::New(isolate(), &data_, kTestByteLength); |
| 1923 } |
| 1924 } |
| 1925 |
| 1926 const Local<SharedArrayBuffer>& input_buffer() { return input_buffer_; } |
| 1927 const Local<SharedArrayBuffer>& output_buffer() { return output_buffer_; } |
| 1928 |
| 1929 void BeforeEncode(ValueSerializer* serializer) override { |
| 1930 serializer->TransferSharedArrayBuffer(0, input_buffer_); |
| 1931 } |
| 1932 |
| 1933 void BeforeDecode(ValueDeserializer* deserializer) override { |
| 1934 deserializer->TransferSharedArrayBuffer(0, output_buffer_); |
| 1935 } |
| 1936 |
| 1937 static void SetUpTestCase() { |
| 1938 flag_was_enabled_ = i::FLAG_harmony_sharedarraybuffer; |
| 1939 i::FLAG_harmony_sharedarraybuffer = true; |
| 1940 ValueSerializerTest::SetUpTestCase(); |
| 1941 } |
| 1942 |
| 1943 static void TearDownTestCase() { |
| 1944 ValueSerializerTest::TearDownTestCase(); |
| 1945 i::FLAG_harmony_sharedarraybuffer = flag_was_enabled_; |
| 1946 flag_was_enabled_ = false; |
| 1947 } |
| 1948 |
| 1949 private: |
| 1950 static bool flag_was_enabled_; |
| 1951 uint8_t data_[kTestByteLength]; |
| 1952 Local<SharedArrayBuffer> input_buffer_; |
| 1953 Local<SharedArrayBuffer> output_buffer_; |
| 1954 }; |
| 1955 |
| 1956 bool ValueSerializerTestWithSharedArrayBufferTransfer::flag_was_enabled_ = |
| 1957 false; |
| 1958 |
| 1959 TEST_F(ValueSerializerTestWithSharedArrayBufferTransfer, |
| 1960 RoundTripSharedArrayBufferTransfer) { |
| 1961 RoundTripTest([this]() { return input_buffer(); }, |
| 1962 [this](Local<Value> value) { |
| 1963 ASSERT_TRUE(value->IsSharedArrayBuffer()); |
| 1964 EXPECT_EQ(output_buffer(), value); |
| 1965 EXPECT_TRUE(EvaluateScriptForResultBool( |
| 1966 "new Uint8Array(result).toString() === '0,1,128,255'")); |
| 1967 }); |
| 1968 RoundTripTest( |
| 1969 [this]() { |
| 1970 Local<Object> object = Object::New(isolate()); |
| 1971 EXPECT_TRUE(object |
| 1972 ->CreateDataProperty(serialization_context(), |
| 1973 StringFromUtf8("a"), |
| 1974 input_buffer()) |
| 1975 .FromMaybe(false)); |
| 1976 EXPECT_TRUE(object |
| 1977 ->CreateDataProperty(serialization_context(), |
| 1978 StringFromUtf8("b"), |
| 1979 input_buffer()) |
| 1980 .FromMaybe(false)); |
| 1981 return object; |
| 1982 }, |
| 1983 [this](Local<Value> value) { |
| 1984 EXPECT_TRUE(EvaluateScriptForResultBool( |
| 1985 "result.a instanceof SharedArrayBuffer")); |
| 1986 EXPECT_TRUE(EvaluateScriptForResultBool("result.a === result.b")); |
| 1987 EXPECT_TRUE(EvaluateScriptForResultBool( |
| 1988 "new Uint8Array(result.a).toString() === '0,1,128,255'")); |
| 1989 }); |
| 1990 } |
| 1991 |
| 1992 TEST_F(ValueSerializerTestWithSharedArrayBufferTransfer, |
| 1993 SharedArrayBufferMustBeTransferred) { |
| 1994 // A SharedArrayBuffer which was not marked for transfer should fail encoding. |
| 1995 InvalidEncodeTest("new SharedArrayBuffer(32)"); |
| 1996 } |
| 1997 |
1906 } // namespace | 1998 } // namespace |
1907 } // namespace v8 | 1999 } // namespace v8 |
OLD | NEW |