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

Side by Side Diff: third_party/WebKit/Source/bindings/modules/v8/V8BindingForModulesTest.cpp

Issue 2781273004: Graceful handling of new versions of IndexedDB serialized data. (Closed)
Patch Set: Addressed feedback. 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 /* 1 /*
2 * Copyright (C) 2011 Google Inc. All rights reserved. 2 * Copyright (C) 2011 Google Inc. All rights reserved.
3 * 3 *
4 * Redistribution and use in source and binary forms, with or without 4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions 5 * modification, are permitted provided that the following conditions
6 * are met: 6 * are met:
7 * 7 *
8 * 1. Redistributions of source code must retain the above copyright 8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer. 9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright 10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the 11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution. 12 * documentation and/or other materials provided with the distribution.
13 * 13 *
14 * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY 14 * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY
15 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 15 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
16 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 16 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
17 * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY 17 * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY
18 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 18 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
19 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 19 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
20 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 20 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
21 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 21 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
23 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 23 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24 */ 24 */
25 25
26 #include "bindings/modules/v8/V8BindingForModules.h" 26 #include "bindings/modules/v8/V8BindingForModules.h"
27 27
28 #include "bindings/core/v8/SerializationTag.h"
29 #include "bindings/core/v8/SerializedScriptValue.h"
28 #include "bindings/core/v8/ToV8.h" 30 #include "bindings/core/v8/ToV8.h"
29 #include "bindings/core/v8/V8Binding.h" 31 #include "bindings/core/v8/V8Binding.h"
30 #include "bindings/core/v8/V8BindingForTesting.h" 32 #include "bindings/core/v8/V8BindingForTesting.h"
31 #include "bindings/core/v8/V8PerIsolateData.h" 33 #include "bindings/core/v8/V8PerIsolateData.h"
32 #include "bindings/modules/v8/ToV8ForModules.h" 34 #include "bindings/modules/v8/ToV8ForModules.h"
35 #include "modules/indexeddb/IDBAny.h"
33 #include "modules/indexeddb/IDBKey.h" 36 #include "modules/indexeddb/IDBKey.h"
34 #include "modules/indexeddb/IDBKeyPath.h" 37 #include "modules/indexeddb/IDBKeyPath.h"
38 #include "modules/indexeddb/IDBValue.h"
39 #include "platform/SharedBuffer.h"
40 #include "public/platform/WebBlobInfo.h"
41 #include "public/platform/WebData.h"
42 #include "public/platform/WebString.h"
43 #include "public/platform/modules/indexeddb/WebIDBKey.h"
44 #include "public/platform/modules/indexeddb/WebIDBKeyPath.h"
45 #include "public/platform/modules/indexeddb/WebIDBValue.h"
35 #include "testing/gtest/include/gtest/gtest.h" 46 #include "testing/gtest/include/gtest/gtest.h"
36 47
37 namespace blink { 48 namespace blink {
38 49
39 namespace { 50 namespace {
40 51
41 IDBKey* checkKeyFromValueAndKeyPathInternal(v8::Isolate* isolate, 52 IDBKey* checkKeyFromValueAndKeyPathInternal(v8::Isolate* isolate,
42 const ScriptValue& value, 53 const ScriptValue& value,
43 const String& keyPath) { 54 const String& keyPath) {
44 IDBKeyPath idbKeyPath(keyPath); 55 IDBKeyPath idbKeyPath(keyPath);
(...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after
109 void checkKeyPathNumberValue(v8::Isolate* isolate, 120 void checkKeyPathNumberValue(v8::Isolate* isolate,
110 const ScriptValue& value, 121 const ScriptValue& value,
111 const String& keyPath, 122 const String& keyPath,
112 int expected) { 123 int expected) {
113 IDBKey* idbKey = checkKeyFromValueAndKeyPathInternal(isolate, value, keyPath); 124 IDBKey* idbKey = checkKeyFromValueAndKeyPathInternal(isolate, value, keyPath);
114 ASSERT_TRUE(idbKey); 125 ASSERT_TRUE(idbKey);
115 ASSERT_EQ(IDBKey::NumberType, idbKey->getType()); 126 ASSERT_EQ(IDBKey::NumberType, idbKey->getType());
116 ASSERT_TRUE(expected == idbKey->number()); 127 ASSERT_TRUE(expected == idbKey->number());
117 } 128 }
118 129
130 void serializeEmptyObject(v8::Isolate* isolate, Vector<char>* wireBytes) {
131 v8::Local<v8::Object> object = v8::Object::New(isolate);
132 ASSERT_TRUE(object->IsObject());
133 NonThrowableExceptionState nonThrowableExceptionState;
134
135 SerializedScriptValue::SerializeOptions options;
136 RefPtr<SerializedScriptValue> serializedValue =
137 SerializedScriptValue::serialize(isolate, object, options,
138 nonThrowableExceptionState);
139 serializedValue->toWireBytes(*wireBytes);
140
141 // Sanity check that the serialization header has not changed, as the tests
142 // that use this method rely on the header format.
143 //
144 // The cast from char* to unsigned char* is necessary to avoid VS2015 warning
145 // C4309 (truncation of constant value). This happens because VersionTag is
146 // 0xFF.
147 const unsigned char* wireData =
148 reinterpret_cast<unsigned char*>(wireBytes->data());
149 ASSERT_EQ(
150 static_cast<unsigned char>(SerializedScriptValue::wireFormatVersion),
151 wireData[0]);
jsbell 2017/04/03 19:31:52 Can you make the offsets (0...3) consts somewhere?
pwnall 2017/04/04 00:55:12 Done.
152 ASSERT_EQ(static_cast<unsigned char>(VersionTag), wireData[1]);
153
154 // 13 is v8::internal::kLatestVersion in v8/src/value-serializer.cc at the
jsbell 2017/04/03 19:31:52 Define this as a const somewhere?
pwnall 2017/04/04 00:55:12 Done.
155 // time when this test was written.
156 ASSERT_GE(static_cast<unsigned char>(13), wireData[2]);
157 ASSERT_EQ(static_cast<unsigned char>(VersionTag), wireData[3]);
158 }
159
160 PassRefPtr<IDBValue> createIDBValue(v8::Isolate* isolate,
161 Vector<char>& wireBytes,
162 double primaryKey,
163 const WebString& keyPath) {
164 DCHECK_GE(wireBytes.size(), 5UL);
165 WebData webData(SharedBuffer::adoptVector(wireBytes));
166 DCHECK_GE(webData.size(), 5UL);
167 Vector<WebBlobInfo> webBlobInfo;
168 WebIDBKey webIdbKey = WebIDBKey::createNumber(42.0);
169 WebIDBKeyPath webIdbKeyPath(WebString("foo"));
170 WebIDBValue webIdbValue(webData, webBlobInfo, webIdbKey, webIdbKeyPath);
171 DCHECK_GE(webIdbValue.data.size(), 5UL);
172 return IDBValue::create(webIdbValue, isolate);
173 }
174
119 TEST(IDBKeyFromValueAndKeyPathTest, TopLevelPropertyStringValue) { 175 TEST(IDBKeyFromValueAndKeyPathTest, TopLevelPropertyStringValue) {
120 V8TestingScope scope; 176 V8TestingScope scope;
121 v8::Isolate* isolate = scope.isolate(); 177 v8::Isolate* isolate = scope.isolate();
122 178
123 // object = { foo: "zoo" } 179 // object = { foo: "zoo" }
124 v8::Local<v8::Object> object = v8::Object::New(isolate); 180 v8::Local<v8::Object> object = v8::Object::New(isolate);
125 ASSERT_TRUE( 181 ASSERT_TRUE(
126 v8CallBoolean(object->Set(scope.context(), v8AtomicString(isolate, "foo"), 182 v8CallBoolean(object->Set(scope.context(), v8AtomicString(isolate, "foo"),
127 v8AtomicString(isolate, "zoo")))); 183 v8AtomicString(isolate, "zoo"))));
128 184
(...skipping 102 matching lines...) Expand 10 before | Expand all | Expand 10 after
231 IDBKey::createArray(IDBKey::KeyArray()), scriptObject, 287 IDBKey::createArray(IDBKey::KeyArray()), scriptObject,
232 "foo.baz"); 288 "foo.baz");
233 checkInjection(scope.getScriptState(), 289 checkInjection(scope.getScriptState(),
234 IDBKey::createArray(IDBKey::KeyArray()), scriptObject, "bar"); 290 IDBKey::createArray(IDBKey::KeyArray()), scriptObject, "bar");
235 291
236 checkInjectionDisallowed(scope.getScriptState(), scriptObject, "foo.bar.baz"); 292 checkInjectionDisallowed(scope.getScriptState(), scriptObject, "foo.bar.baz");
237 checkInjection(scope.getScriptState(), IDBKey::createString("zoo"), 293 checkInjection(scope.getScriptState(), IDBKey::createString("zoo"),
238 scriptObject, "foo.xyz.foo"); 294 scriptObject, "foo.xyz.foo");
239 } 295 }
240 296
297 TEST(DeserializeIDBValueTest, CurrentVersions) {
298 V8TestingScope scope;
299 v8::Isolate* isolate = scope.isolate();
300
301 Vector<char> objectBytes;
302 serializeEmptyObject(isolate, &objectBytes);
303 RefPtr<IDBValue> idbValue = createIDBValue(isolate, objectBytes, 42.0, "foo");
304
305 v8::Local<v8::Value> v8Value =
306 deserializeIDBValue(isolate, scope.context()->Global(), idbValue.get());
307 EXPECT_TRUE(!scope.getExceptionState().hadException());
308
309 ASSERT_TRUE(v8Value->IsObject());
310 v8::Local<v8::Object> v8ValueObject = v8Value.As<v8::Object>();
311 v8::Local<v8::Value> v8NumberValue =
312 v8ValueObject->Get(scope.context(), v8AtomicString(isolate, "foo"))
313 .ToLocalChecked();
314 ASSERT_TRUE(v8NumberValue->IsNumber());
315 v8::Local<v8::Number> v8Number = v8NumberValue.As<v8::Number>();
316 EXPECT_EQ(v8Number->Value(), 42.0);
317 }
318
319 TEST(DeserializeIDBValueTest, FutureV8Version) {
320 V8TestingScope scope;
321 v8::Isolate* isolate = scope.isolate();
322
323 // These steps reproduce the circumstances of http://crbug.com/703704
jsbell 2017/04/03 19:31:52 nit: prefer https Also, I'd put the explanation i
pwnall 2017/04/04 00:55:12 Done. (the https part) The explanation was gettin
324 Vector<char> objectBytes;
325 serializeEmptyObject(isolate, &objectBytes);
326 objectBytes[2] += 1;
jsbell 2017/04/03 19:31:52 Explain what this is doing. (Having the offset be
pwnall 2017/04/04 00:55:12 Done.
327 RefPtr<IDBValue> idbValue = createIDBValue(isolate, objectBytes, 42.0, "foo");
328
329 v8::Local<v8::Value> v8Value =
330 deserializeIDBValue(isolate, scope.context()->Global(), idbValue.get());
331 // The exception check below will need to be changed if we throw exceptions
jsbell 2017/04/03 19:31:52 I don't think you need this comment - it's implici
pwnall 2017/04/04 00:55:12 Done.
332 // instead of returning null on deserialization errors.
333 EXPECT_TRUE(!scope.getExceptionState().hadException());
334 EXPECT_TRUE(v8Value->IsNull());
335 }
336
241 } // namespace blink 337 } // namespace blink
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698