OLD | NEW |
---|---|
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 The Chromium 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 "bindings/core/v8/SerializedScriptValue.h" | 5 #include "bindings/core/v8/SerializedScriptValue.h" |
6 | 6 |
7 #include "bindings/core/v8/ExceptionState.h" | 7 #include "bindings/core/v8/ExceptionState.h" |
8 #include "bindings/core/v8/SerializedScriptValueFactory.h" | 8 #include "bindings/core/v8/SerializedScriptValueFactory.h" |
9 #include "bindings/core/v8/V8Binding.h" | 9 #include "bindings/core/v8/V8Binding.h" |
10 #include "bindings/core/v8/V8BindingForTesting.h" | 10 #include "bindings/core/v8/V8BindingForTesting.h" |
11 #include "bindings/core/v8/V8File.h" | 11 #include "bindings/core/v8/V8File.h" |
12 #include "bindings/core/v8/V8ImageData.h" | |
12 #include "core/fileapi/File.h" | 13 #include "core/fileapi/File.h" |
13 #include "platform/testing/UnitTestHelpers.h" | 14 #include "platform/testing/UnitTestHelpers.h" |
14 #include "testing/gtest/include/gtest/gtest.h" | 15 #include "testing/gtest/include/gtest/gtest.h" |
15 | 16 |
16 namespace blink { | 17 namespace blink { |
17 | 18 |
19 TEST(SerializedScriptValueTest, WireFormatRoundTrip) { | |
20 V8TestingScope scope; | |
21 | |
22 v8::Local<v8::Value> v8OriginalTrue = | |
23 v8::Boolean::New(scope.GetIsolate(), true); | |
jbroman
2017/04/12 18:34:04
More concisely: v8::True(scope.GetIsolate());
At
pwnall
2017/04/12 21:37:30
Done.
Thank you!
| |
24 RefPtr<SerializedScriptValue> sourceSerializedScriptValue = | |
25 SerializedScriptValue::Serialize( | |
26 scope.GetIsolate(), v8OriginalTrue, | |
27 SerializedScriptValue::SerializeOptions(), ASSERT_NO_EXCEPTION); | |
28 | |
29 Vector<char> wireData; | |
30 sourceSerializedScriptValue->ToWireBytes(wireData); | |
31 | |
32 RefPtr<SerializedScriptValue> serializedScriptValue = | |
33 SerializedScriptValue::Create(wireData.Data(), wireData.size()); | |
34 v8::Local<v8::Value> deserialized = | |
35 serializedScriptValue->Deserialize(scope.GetIsolate()); | |
36 EXPECT_TRUE(deserialized->IsTrue()); | |
37 } | |
38 | |
39 TEST(SerializedScriptValueTest, WireFormatVersion17NoByteFlipping) { | |
40 V8TestingScope scope; | |
41 | |
42 const uint8_t data[] = {0xFF, 0x11, 0xFF, 0x0D, 0x54, 0x00}; | |
43 RefPtr<SerializedScriptValue> serializedScriptValue = | |
44 SerializedScriptValue::Create(reinterpret_cast<const char*>(data), | |
45 sizeof(data)); | |
46 v8::Local<v8::Value> deserialized = | |
47 serializedScriptValue->Deserialize(scope.GetIsolate()); | |
48 EXPECT_TRUE(deserialized->IsTrue()); | |
49 } | |
50 | |
51 TEST(SerializedScriptValueTest, WireFormatVersion16ByteFlipping) { | |
52 V8TestingScope scope; | |
53 | |
54 // Using UChar instead of uint8_t to get ntohs() byte flipping. | |
55 const UChar data[] = {0xFF10, 0xFF0D, 0x5400}; | |
56 RefPtr<SerializedScriptValue> serializedScriptValue = | |
57 SerializedScriptValue::Create(reinterpret_cast<const char*>(data), | |
58 sizeof(data)); | |
59 v8::Local<v8::Value> deserialized = | |
60 serializedScriptValue->Deserialize(scope.GetIsolate()); | |
61 EXPECT_TRUE(deserialized->IsTrue()); | |
62 } | |
63 | |
64 TEST(SerializedScriptValueTest, WireFormatVersion13ByteFlipping) { | |
65 V8TestingScope scope; | |
66 | |
67 // Using UChar instead of uint8_t to get ntohs() byte flipping. | |
68 const UChar data[] = {0xFF0D, 0x5400}; | |
69 RefPtr<SerializedScriptValue> serializedScriptValue = | |
70 SerializedScriptValue::Create(reinterpret_cast<const char*>(data), | |
71 sizeof(data)); | |
72 v8::Local<v8::Value> deserialized = | |
73 serializedScriptValue->Deserialize(scope.GetIsolate()); | |
74 EXPECT_TRUE(deserialized->IsTrue()); | |
75 } | |
76 | |
77 TEST(SerializedScriptValueTest, WireFormatVersion0ByteFlipping) { | |
78 V8TestingScope scope; | |
79 | |
80 // Using UChar instead of uint8_t to get ntohs() byte flipping. | |
81 const UChar data[] = {0x5400}; | |
82 RefPtr<SerializedScriptValue> serializedScriptValue = | |
83 SerializedScriptValue::Create(reinterpret_cast<const char*>(data), | |
84 sizeof(data)); | |
85 v8::Local<v8::Value> deserialized = | |
86 serializedScriptValue->Deserialize(scope.GetIsolate()); | |
87 EXPECT_TRUE(deserialized->IsTrue()); | |
88 } | |
89 | |
90 TEST(SerializedScriptValueTest, WireFormatVersion0ImageData) { | |
91 V8TestingScope scope; | |
92 v8::Isolate* isolate = scope.GetIsolate(); | |
93 | |
94 // Using UChar instead of uint8_t to get ntohs() byte flipping. | |
95 // | |
96 // This builds the smallest possible ImageData whose first data byte is 0xFF, | |
97 // as follows. | |
98 // | |
99 // width = 127, encoded as 0xFF 0x00 (degenerate varint) | |
100 // height = 1, encoded as 0x01 (varint) | |
101 // pixelLength = 508 (127 * 1 * 4), encoded as 0xFC 0x03 (varint) | |
102 // pixel data = 508 bytes, all zero | |
103 Vector<UChar> data; | |
104 data.push_back(0x23FF); | |
105 data.push_back(0x001); | |
106 data.push_back(0xFC03); | |
107 data.Resize(257); // (508 pixel data + 6 header bytes) / 2 | |
108 | |
109 // Using UChar instead of uint8_t to get ntohs() byte flipping. | |
110 RefPtr<SerializedScriptValue> serializedScriptValue = | |
111 SerializedScriptValue::Create(reinterpret_cast<const char*>(data.Data()), | |
112 data.size() * sizeof(UChar)); | |
113 v8::Local<v8::Value> deserialized = | |
114 serializedScriptValue->Deserialize(isolate); | |
115 ASSERT_TRUE(deserialized->IsObject()); | |
116 v8::Local<v8::Object> deserializedObject = deserialized.As<v8::Object>(); | |
117 ASSERT_TRUE(V8ImageData::hasInstance(deserializedObject, isolate)); | |
118 ImageData* imageData = V8ImageData::toImpl(deserializedObject); | |
119 EXPECT_EQ(imageData->width(), 127); | |
120 EXPECT_EQ(imageData->height(), 1); | |
121 } | |
122 | |
123 TEST(SerializedScriptValueTest, NullValue) { | |
jbroman
2017/04/12 18:34:04
Is this substantially different from V8ScriptValue
pwnall
2017/04/12 21:37:30
No.
If the intent of the test is to make sure tha
| |
124 V8TestingScope scope; | |
125 | |
126 RefPtr<SerializedScriptValue> serializedScriptValue = | |
127 SerializedScriptValue::NullValue(); | |
128 v8::Local<v8::Value> deserialized = | |
129 serializedScriptValue->Deserialize(scope.GetIsolate()); | |
130 EXPECT_TRUE(deserialized->IsNull()); | |
131 } | |
132 | |
18 TEST(SerializedScriptValueTest, UserSelectedFile) { | 133 TEST(SerializedScriptValueTest, UserSelectedFile) { |
19 V8TestingScope scope; | 134 V8TestingScope scope; |
20 String file_path = testing::BlinkRootDir(); | 135 String file_path = testing::BlinkRootDir(); |
21 file_path.Append("/Source/bindings/core/v8/SerializedScriptValueTest.cpp"); | 136 file_path.Append("/Source/bindings/core/v8/SerializedScriptValueTest.cpp"); |
22 File* original_file = File::Create(file_path); | 137 File* original_file = File::Create(file_path); |
23 ASSERT_TRUE(original_file->HasBackingFile()); | 138 ASSERT_TRUE(original_file->HasBackingFile()); |
24 ASSERT_EQ(File::kIsUserVisible, original_file->GetUserVisibility()); | 139 ASSERT_EQ(File::kIsUserVisible, original_file->GetUserVisibility()); |
25 ASSERT_EQ(file_path, original_file->GetPath()); | 140 ASSERT_EQ(file_path, original_file->GetPath()); |
26 | 141 |
27 v8::Local<v8::Value> v8_original_file = | 142 v8::Local<v8::Value> v8_original_file = |
(...skipping 30 matching lines...) Expand all Loading... | |
58 serialized_script_value->Deserialize(scope.GetIsolate()); | 173 serialized_script_value->Deserialize(scope.GetIsolate()); |
59 | 174 |
60 ASSERT_TRUE(V8File::hasInstance(v8_file, scope.GetIsolate())); | 175 ASSERT_TRUE(V8File::hasInstance(v8_file, scope.GetIsolate())); |
61 File* file = V8File::toImpl(v8::Local<v8::Object>::Cast(v8_file)); | 176 File* file = V8File::toImpl(v8::Local<v8::Object>::Cast(v8_file)); |
62 EXPECT_FALSE(file->HasBackingFile()); | 177 EXPECT_FALSE(file->HasBackingFile()); |
63 EXPECT_EQ(File::kIsNotUserVisible, file->GetUserVisibility()); | 178 EXPECT_EQ(File::kIsNotUserVisible, file->GetUserVisibility()); |
64 EXPECT_EQ("hello.txt", file->name()); | 179 EXPECT_EQ("hello.txt", file->name()); |
65 } | 180 } |
66 | 181 |
67 } // namespace blink | 182 } // namespace blink |
OLD | NEW |