OLD | NEW |
| (Empty) |
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 | |
3 // found in the LICENSE file. | |
4 | |
5 #include "bindings/core/v8/SerializedScriptValue.h" | |
6 | |
7 #include "bindings/core/v8/ExceptionState.h" | |
8 #include "bindings/core/v8/SerializedScriptValueFactory.h" | |
9 #include "bindings/core/v8/V8BindingForCore.h" | |
10 #include "bindings/core/v8/V8BindingForTesting.h" | |
11 #include "bindings/core/v8/V8File.h" | |
12 #include "bindings/core/v8/V8ImageData.h" | |
13 #include "core/fileapi/File.h" | |
14 #include "platform/testing/UnitTestHelpers.h" | |
15 #include "testing/gtest/include/gtest/gtest.h" | |
16 | |
17 namespace blink { | |
18 | |
19 TEST(SerializedScriptValueTest, WireFormatRoundTrip) { | |
20 V8TestingScope scope; | |
21 | |
22 v8::Local<v8::Value> v8OriginalTrue = v8::True(scope.GetIsolate()); | |
23 RefPtr<SerializedScriptValue> sourceSerializedScriptValue = | |
24 SerializedScriptValue::Serialize( | |
25 scope.GetIsolate(), v8OriginalTrue, | |
26 SerializedScriptValue::SerializeOptions(), ASSERT_NO_EXCEPTION); | |
27 | |
28 Vector<char> wireData; | |
29 sourceSerializedScriptValue->ToWireBytes(wireData); | |
30 | |
31 RefPtr<SerializedScriptValue> serializedScriptValue = | |
32 SerializedScriptValue::Create(wireData.data(), wireData.size()); | |
33 v8::Local<v8::Value> deserialized = | |
34 serializedScriptValue->Deserialize(scope.GetIsolate()); | |
35 EXPECT_TRUE(deserialized->IsTrue()); | |
36 } | |
37 | |
38 TEST(SerializedScriptValueTest, WireFormatVersion17NoByteSwapping) { | |
39 V8TestingScope scope; | |
40 | |
41 const uint8_t data[] = {0xFF, 0x11, 0xFF, 0x0D, 0x54, 0x00}; | |
42 RefPtr<SerializedScriptValue> serializedScriptValue = | |
43 SerializedScriptValue::Create(reinterpret_cast<const char*>(data), | |
44 sizeof(data)); | |
45 v8::Local<v8::Value> deserialized = | |
46 serializedScriptValue->Deserialize(scope.GetIsolate()); | |
47 EXPECT_TRUE(deserialized->IsTrue()); | |
48 } | |
49 | |
50 TEST(SerializedScriptValueTest, WireFormatVersion16ByteSwapping) { | |
51 V8TestingScope scope; | |
52 | |
53 // Using UChar instead of uint8_t to get ntohs() byte swapping. | |
54 const UChar data[] = {0xFF10, 0xFF0D, 0x5400}; | |
55 RefPtr<SerializedScriptValue> serializedScriptValue = | |
56 SerializedScriptValue::Create(reinterpret_cast<const char*>(data), | |
57 sizeof(data)); | |
58 v8::Local<v8::Value> deserialized = | |
59 serializedScriptValue->Deserialize(scope.GetIsolate()); | |
60 EXPECT_TRUE(deserialized->IsTrue()); | |
61 } | |
62 | |
63 TEST(SerializedScriptValueTest, WireFormatVersion13ByteSwapping) { | |
64 V8TestingScope scope; | |
65 | |
66 // Using UChar instead of uint8_t to get ntohs() byte swapping. | |
67 const UChar data[] = {0xFF0D, 0x5400}; | |
68 RefPtr<SerializedScriptValue> serializedScriptValue = | |
69 SerializedScriptValue::Create(reinterpret_cast<const char*>(data), | |
70 sizeof(data)); | |
71 v8::Local<v8::Value> deserialized = | |
72 serializedScriptValue->Deserialize(scope.GetIsolate()); | |
73 EXPECT_TRUE(deserialized->IsTrue()); | |
74 } | |
75 | |
76 TEST(SerializedScriptValueTest, WireFormatVersion0ByteSwapping) { | |
77 V8TestingScope scope; | |
78 | |
79 // Using UChar instead of uint8_t to get ntohs() byte swapping. | |
80 const UChar data[] = {0x5400}; | |
81 RefPtr<SerializedScriptValue> serializedScriptValue = | |
82 SerializedScriptValue::Create(reinterpret_cast<const char*>(data), | |
83 sizeof(data)); | |
84 v8::Local<v8::Value> deserialized = | |
85 serializedScriptValue->Deserialize(scope.GetIsolate()); | |
86 EXPECT_TRUE(deserialized->IsTrue()); | |
87 } | |
88 | |
89 TEST(SerializedScriptValueTest, WireFormatVersion0ImageData) { | |
90 V8TestingScope scope; | |
91 v8::Isolate* isolate = scope.GetIsolate(); | |
92 | |
93 // Using UChar instead of uint8_t to get ntohs() byte swapping. | |
94 // | |
95 // This builds the smallest possible ImageData whose first data byte is 0xFF, | |
96 // as follows. | |
97 // | |
98 // width = 127, encoded as 0xFF 0x00 (degenerate varint) | |
99 // height = 1, encoded as 0x01 (varint) | |
100 // pixelLength = 508 (127 * 1 * 4), encoded as 0xFC 0x03 (varint) | |
101 // pixel data = 508 bytes, all zero | |
102 Vector<UChar> data; | |
103 data.push_back(0x23FF); | |
104 data.push_back(0x001); | |
105 data.push_back(0xFC03); | |
106 data.resize(257); // (508 pixel data + 6 header bytes) / 2 | |
107 | |
108 RefPtr<SerializedScriptValue> serializedScriptValue = | |
109 SerializedScriptValue::Create(reinterpret_cast<const char*>(data.data()), | |
110 data.size() * sizeof(UChar)); | |
111 v8::Local<v8::Value> deserialized = | |
112 serializedScriptValue->Deserialize(isolate); | |
113 ASSERT_TRUE(deserialized->IsObject()); | |
114 v8::Local<v8::Object> deserializedObject = deserialized.As<v8::Object>(); | |
115 ASSERT_TRUE(V8ImageData::hasInstance(deserializedObject, isolate)); | |
116 ImageData* imageData = V8ImageData::toImpl(deserializedObject); | |
117 EXPECT_EQ(imageData->width(), 127); | |
118 EXPECT_EQ(imageData->height(), 1); | |
119 } | |
120 | |
121 TEST(SerializedScriptValueTest, UserSelectedFile) { | |
122 V8TestingScope scope; | |
123 String file_path = testing::BlinkRootDir(); | |
124 file_path.append("/Source/bindings/core/v8/SerializedScriptValueTest.cpp"); | |
125 File* original_file = File::Create(file_path); | |
126 ASSERT_TRUE(original_file->HasBackingFile()); | |
127 ASSERT_EQ(File::kIsUserVisible, original_file->GetUserVisibility()); | |
128 ASSERT_EQ(file_path, original_file->GetPath()); | |
129 | |
130 v8::Local<v8::Value> v8_original_file = | |
131 ToV8(original_file, scope.GetContext()->Global(), scope.GetIsolate()); | |
132 RefPtr<SerializedScriptValue> serialized_script_value = | |
133 SerializedScriptValue::Serialize( | |
134 scope.GetIsolate(), v8_original_file, | |
135 SerializedScriptValue::SerializeOptions(), ASSERT_NO_EXCEPTION); | |
136 v8::Local<v8::Value> v8_file = | |
137 serialized_script_value->Deserialize(scope.GetIsolate()); | |
138 | |
139 ASSERT_TRUE(V8File::hasInstance(v8_file, scope.GetIsolate())); | |
140 File* file = V8File::toImpl(v8::Local<v8::Object>::Cast(v8_file)); | |
141 EXPECT_TRUE(file->HasBackingFile()); | |
142 EXPECT_EQ(File::kIsUserVisible, file->GetUserVisibility()); | |
143 EXPECT_EQ(file_path, file->GetPath()); | |
144 } | |
145 | |
146 TEST(SerializedScriptValueTest, FileConstructorFile) { | |
147 V8TestingScope scope; | |
148 RefPtr<BlobDataHandle> blob_data_handle = BlobDataHandle::Create(); | |
149 File* original_file = File::Create("hello.txt", 12345678.0, blob_data_handle); | |
150 ASSERT_FALSE(original_file->HasBackingFile()); | |
151 ASSERT_EQ(File::kIsNotUserVisible, original_file->GetUserVisibility()); | |
152 ASSERT_EQ("hello.txt", original_file->name()); | |
153 | |
154 v8::Local<v8::Value> v8_original_file = | |
155 ToV8(original_file, scope.GetContext()->Global(), scope.GetIsolate()); | |
156 RefPtr<SerializedScriptValue> serialized_script_value = | |
157 SerializedScriptValue::Serialize( | |
158 scope.GetIsolate(), v8_original_file, | |
159 SerializedScriptValue::SerializeOptions(), ASSERT_NO_EXCEPTION); | |
160 v8::Local<v8::Value> v8_file = | |
161 serialized_script_value->Deserialize(scope.GetIsolate()); | |
162 | |
163 ASSERT_TRUE(V8File::hasInstance(v8_file, scope.GetIsolate())); | |
164 File* file = V8File::toImpl(v8::Local<v8::Object>::Cast(v8_file)); | |
165 EXPECT_FALSE(file->HasBackingFile()); | |
166 EXPECT_EQ(File::kIsNotUserVisible, file->GetUserVisibility()); | |
167 EXPECT_EQ("hello.txt", file->name()); | |
168 } | |
169 | |
170 } // namespace blink | |
OLD | NEW |