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

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: Document reason for unit test. 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
« no previous file with comments | « third_party/WebKit/Source/bindings/modules/v8/V8BindingForModules.cpp ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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(V8TestingScope* scope, Vector<char>* wireBytes) {
131 v8::Isolate* isolate = scope->isolate();
jbroman 2017/03/31 18:42:59 nit: Just pass the isolate as an argument, rather
pwnall 2017/03/31 22:50:24 Done. Thank you for catching this! I forgot to cle
132
133 v8::Local<v8::Object> object = v8::Object::New(isolate);
134 ASSERT_TRUE(object->IsObject());
135 NonThrowableExceptionState nonThrowableExceptionState;
136
137 SerializedScriptValue::SerializeOptions options;
138 RefPtr<SerializedScriptValue> serializedValue =
139 SerializedScriptValue::serialize(isolate, object, options,
140 nonThrowableExceptionState);
141 serializedValue->toWireBytes(*wireBytes);
142
143 // Sanity check that the serialization header has not changed, as the tests
144 // that use this method rely on the header format.
145 //
146 // The cast from char* to unsigned char* is necessary to avoid VS2015 warning
147 // C4309 (truncation of constant value). This happens because VersionTag is
148 // 0xFF.
149 const unsigned char* wireData =
150 reinterpret_cast<unsigned char*>(wireBytes->data());
151 ASSERT_EQ(
152 static_cast<unsigned char>(SerializedScriptValue::wireFormatVersion),
153 wireData[0]);
154 ASSERT_EQ(static_cast<unsigned char>(VersionTag), wireData[1]);
155
156 // 13 is v8::internal::kLatestVersion in v8/src/value-serializer.cc
157 ASSERT_EQ(static_cast<unsigned char>(13), wireData[2]);
jbroman 2017/03/31 18:42:59 This will make the test fail whenever the v8 versi
pwnall 2017/03/31 22:50:24 Done. Thank you for the suggestion! This is a very
158 ASSERT_EQ(static_cast<unsigned char>(VersionTag), wireData[3]);
159 }
160
161 PassRefPtr<IDBValue> createIDBValue(v8::Isolate* isolate,
162 Vector<char>& wireBytes,
163 double primaryKey,
164 const WebString& keyPath) {
165 DCHECK_GE(wireBytes.size(), 5UL);
166 WebData webData(SharedBuffer::adoptVector(wireBytes));
167 DCHECK_GE(webData.size(), 5UL);
168 Vector<WebBlobInfo> webBlobInfo;
169 WebIDBKey webIdbKey = WebIDBKey::createNumber(42.0);
170 WebIDBKeyPath webIdbKeyPath(WebString("foo"));
171 WebIDBValue webIdbValue(webData, webBlobInfo, webIdbKey, webIdbKeyPath);
172 DCHECK_GE(webIdbValue.data.size(), 5UL);
173 return IDBValue::create(webIdbValue, isolate);
174 }
175
119 TEST(IDBKeyFromValueAndKeyPathTest, TopLevelPropertyStringValue) { 176 TEST(IDBKeyFromValueAndKeyPathTest, TopLevelPropertyStringValue) {
120 V8TestingScope scope; 177 V8TestingScope scope;
121 v8::Isolate* isolate = scope.isolate(); 178 v8::Isolate* isolate = scope.isolate();
122 179
123 // object = { foo: "zoo" } 180 // object = { foo: "zoo" }
124 v8::Local<v8::Object> object = v8::Object::New(isolate); 181 v8::Local<v8::Object> object = v8::Object::New(isolate);
125 ASSERT_TRUE( 182 ASSERT_TRUE(
126 v8CallBoolean(object->Set(scope.context(), v8AtomicString(isolate, "foo"), 183 v8CallBoolean(object->Set(scope.context(), v8AtomicString(isolate, "foo"),
127 v8AtomicString(isolate, "zoo")))); 184 v8AtomicString(isolate, "zoo"))));
128 185
(...skipping 102 matching lines...) Expand 10 before | Expand all | Expand 10 after
231 IDBKey::createArray(IDBKey::KeyArray()), scriptObject, 288 IDBKey::createArray(IDBKey::KeyArray()), scriptObject,
232 "foo.baz"); 289 "foo.baz");
233 checkInjection(scope.getScriptState(), 290 checkInjection(scope.getScriptState(),
234 IDBKey::createArray(IDBKey::KeyArray()), scriptObject, "bar"); 291 IDBKey::createArray(IDBKey::KeyArray()), scriptObject, "bar");
235 292
236 checkInjectionDisallowed(scope.getScriptState(), scriptObject, "foo.bar.baz"); 293 checkInjectionDisallowed(scope.getScriptState(), scriptObject, "foo.bar.baz");
237 checkInjection(scope.getScriptState(), IDBKey::createString("zoo"), 294 checkInjection(scope.getScriptState(), IDBKey::createString("zoo"),
238 scriptObject, "foo.xyz.foo"); 295 scriptObject, "foo.xyz.foo");
239 } 296 }
240 297
298 TEST(DeserializeIDBValueTest, CurrentVersions) {
299 V8TestingScope scope;
300 v8::Isolate* isolate = scope.isolate();
301
302 Vector<char> objectBytes;
303 serializeEmptyObject(&scope, &objectBytes);
304 RefPtr<IDBValue> idbValue = createIDBValue(isolate, objectBytes, 42.0, "foo");
305
306 v8::Local<v8::Value> v8Value =
307 deserializeIDBValue(isolate, scope.context()->Global(), idbValue.get());
308 EXPECT_TRUE(!scope.getExceptionState().hadException());
309
310 ASSERT_TRUE(v8Value->IsObject());
311 v8::Local<v8::Object> v8ValueObject = v8Value.As<v8::Object>();
312 v8::Local<v8::Value> v8NumberValue =
313 v8ValueObject->Get(scope.context(), v8AtomicString(isolate, "foo"))
314 .ToLocalChecked();
315 ASSERT_TRUE(v8NumberValue->IsNumber());
316 v8::Local<v8::Number> v8Number = v8NumberValue.As<v8::Number>();
317 EXPECT_EQ(v8Number->Value(), 42.0);
318 }
319
320 TEST(DeserializeIDBValueTest, FutureV8Version) {
321 V8TestingScope scope;
322 v8::Isolate* isolate = scope.isolate();
323
324 // These steps reproduce the circumstances of http://crbug.com/703704
325 Vector<char> objectBytes;
326 serializeEmptyObject(&scope, &objectBytes);
327 objectBytes[2] += 1;
328 RefPtr<IDBValue> idbValue = createIDBValue(isolate, objectBytes, 42.0, "foo");
329
330 v8::Local<v8::Value> v8Value =
331 deserializeIDBValue(isolate, scope.context()->Global(), idbValue.get());
332 // The exception check below will need to be changed when we throw exceptions
333 // instead of returning null on deserialization errors.
jbroman 2017/03/31 18:42:59 V8 does throw, but Blink throwing is a more compli
pwnall 2017/03/31 22:50:24 I made the comment here be less assertive. Thank
334 EXPECT_TRUE(!scope.getExceptionState().hadException());
335 EXPECT_TRUE(v8Value->IsNull());
336 }
337
241 } // namespace blink 338 } // namespace blink
OLDNEW
« no previous file with comments | « third_party/WebKit/Source/bindings/modules/v8/V8BindingForModules.cpp ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698