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

Side by Side Diff: third_party/WebKit/Source/bindings/core/v8/SerializedScriptValueTest.cpp

Issue 2803593005: Avoid unnecessary byte-swapping in blink's SerializedScriptValue. (Closed)
Patch Set: Correct handling for SSV version 0. Created 3 years, 8 months 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
OLDNEW
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 = v8::Boolean::New(scope.isolate(), true);
23 RefPtr<SerializedScriptValue> sourceSerializedScriptValue =
24 SerializedScriptValue::serialize(
25 scope.isolate(), 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.isolate());
35 EXPECT_TRUE(deserialized->IsTrue());
36 }
37
38 TEST(SerializedScriptValueTest, WireFormatVersion17NoByteFlipping) {
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), 6);
44 v8::Local<v8::Value> deserialized =
45 serializedScriptValue->deserialize(scope.isolate());
46 EXPECT_TRUE(deserialized->IsTrue());
47 }
48
49 TEST(SerializedScriptValueTest, WireFormatVersion16ByteFlipping) {
50 V8TestingScope scope;
51
52 // Using UChar instead of uint8_t to get ntohs() byte flipping.
53 const UChar data[] = {0xFF10, 0xFF0D, 0x5400};
54 RefPtr<SerializedScriptValue> serializedScriptValue =
55 SerializedScriptValue::create(reinterpret_cast<const char*>(data), 6);
56 v8::Local<v8::Value> deserialized =
57 serializedScriptValue->deserialize(scope.isolate());
58 EXPECT_TRUE(deserialized->IsTrue());
59 }
60
61 TEST(SerializedScriptValueTest, WireFormatVersion13ByteFlipping) {
62 V8TestingScope scope;
63
64 // Using UChar instead of uint8_t to get ntohs() byte flipping.
65 const UChar data[] = {0xFF0D, 0x5400};
66 RefPtr<SerializedScriptValue> serializedScriptValue =
67 SerializedScriptValue::create(reinterpret_cast<const char*>(data), 4);
68 v8::Local<v8::Value> deserialized =
69 serializedScriptValue->deserialize(scope.isolate());
70 EXPECT_TRUE(deserialized->IsTrue());
71 }
72
73 TEST(SerializedScriptValueTest, WireFormatVersion0ByteFlipping) {
74 V8TestingScope scope;
75
76 // Using UChar instead of uint8_t to get ntohs() byte flipping.
77 const UChar data[] = {0x5400};
78 RefPtr<SerializedScriptValue> serializedScriptValue =
79 SerializedScriptValue::create(reinterpret_cast<const char*>(data), 2);
80 v8::Local<v8::Value> deserialized =
81 serializedScriptValue->deserialize(scope.isolate());
82 EXPECT_TRUE(deserialized->IsTrue());
83 }
84
85 TEST(SerializedScriptValueTest, WireFormatVersion0ImageData) {
86 V8TestingScope scope;
87 v8::Isolate* isolate = scope.isolate();
88
89 // Using UChar instead of uint8_t to get ntohs() byte flipping.
90 //
91 // This builds the smallest possible ImageData whose first data byte is 0xFF,
92 // as follows.
93 //
94 // width = 127, encoded as 0xFF 0x00 (degenerate varint)
95 // height = 1, encoded as 0x01 (varint)
96 // pixelLength = 508 (127 * 1 * 4), encoded as 0xFC 0x03 (varint)
97 // pixel data = 508 bytes, all zero
98 Vector<UChar> data;
99 data.push_back(0x23FF);
100 data.push_back(0x001);
101 data.push_back(0xFC03);
102 data.resize(257); // (508 pixel data + 6 header bytes) / 2
103
104 // Using UChar instead of uint8_t to get ntohs() byte flipping.
105 RefPtr<SerializedScriptValue> serializedScriptValue =
106 SerializedScriptValue::create(reinterpret_cast<const char*>(data.data()),
107 514);
108 v8::Local<v8::Value> deserialized =
109 serializedScriptValue->deserialize(isolate);
110 ASSERT_TRUE(deserialized->IsObject());
111 v8::Local<v8::Object> deserializedObject = deserialized.As<v8::Object>();
112 ASSERT_TRUE(V8ImageData::hasInstance(deserializedObject, isolate));
113 ImageData* imageData = V8ImageData::toImpl(deserializedObject);
114 EXPECT_EQ(imageData->width(), 127);
115 EXPECT_EQ(imageData->height(), 1);
116 }
117
118 TEST(SerializedScriptValueTest, NullValue) {
119 V8TestingScope scope;
120
121 RefPtr<SerializedScriptValue> serializedScriptValue =
122 SerializedScriptValue::nullValue();
123 v8::Local<v8::Value> deserialized =
124 serializedScriptValue->deserialize(scope.isolate());
125 EXPECT_TRUE(deserialized->IsNull());
126 }
127
18 TEST(SerializedScriptValueTest, UserSelectedFile) { 128 TEST(SerializedScriptValueTest, UserSelectedFile) {
19 V8TestingScope scope; 129 V8TestingScope scope;
20 String filePath = testing::blinkRootDir(); 130 String filePath = testing::blinkRootDir();
21 filePath.append("/Source/bindings/core/v8/SerializedScriptValueTest.cpp"); 131 filePath.append("/Source/bindings/core/v8/SerializedScriptValueTest.cpp");
22 File* originalFile = File::create(filePath); 132 File* originalFile = File::create(filePath);
23 ASSERT_TRUE(originalFile->hasBackingFile()); 133 ASSERT_TRUE(originalFile->hasBackingFile());
24 ASSERT_EQ(File::IsUserVisible, originalFile->getUserVisibility()); 134 ASSERT_EQ(File::IsUserVisible, originalFile->getUserVisibility());
25 ASSERT_EQ(filePath, originalFile->path()); 135 ASSERT_EQ(filePath, originalFile->path());
26 136
27 v8::Local<v8::Value> v8OriginalFile = 137 v8::Local<v8::Value> v8OriginalFile =
(...skipping 30 matching lines...) Expand all
58 serializedScriptValue->deserialize(scope.isolate()); 168 serializedScriptValue->deserialize(scope.isolate());
59 169
60 ASSERT_TRUE(V8File::hasInstance(v8File, scope.isolate())); 170 ASSERT_TRUE(V8File::hasInstance(v8File, scope.isolate()));
61 File* file = V8File::toImpl(v8::Local<v8::Object>::Cast(v8File)); 171 File* file = V8File::toImpl(v8::Local<v8::Object>::Cast(v8File));
62 EXPECT_FALSE(file->hasBackingFile()); 172 EXPECT_FALSE(file->hasBackingFile());
63 EXPECT_EQ(File::IsNotUserVisible, file->getUserVisibility()); 173 EXPECT_EQ(File::IsNotUserVisible, file->getUserVisibility());
64 EXPECT_EQ("hello.txt", file->name()); 174 EXPECT_EQ("hello.txt", file->name());
65 } 175 }
66 176
67 } // namespace blink 177 } // namespace blink
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698