| OLD | NEW |
| 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 "config.h" | 26 #include "config.h" |
| 27 #include "IDBBindingUtilities.h" | 27 #include "IDBBindingUtilities.h" |
| 28 #include "IDBKey.h" | 28 #include "IDBKey.h" |
| 29 #include "IDBKeyPath.h" | 29 #include "IDBKeyPath.h" |
| 30 #include "SerializedScriptValue.h" |
| 30 #include "V8PerIsolateData.h" | 31 #include "V8PerIsolateData.h" |
| 31 #include "V8Utilities.h" | 32 #include "V8Utilities.h" |
| 32 | 33 |
| 33 #include <gtest/gtest.h> | 34 #include <gtest/gtest.h> |
| 34 #include <wtf/Vector.h> | 35 #include <wtf/Vector.h> |
| 35 | 36 |
| 36 #if ENABLE(INDEXED_DATABASE) | 37 #if ENABLE(INDEXED_DATABASE) |
| 37 | 38 |
| 38 using namespace WebCore; | 39 using namespace WebCore; |
| 39 | 40 |
| 40 namespace { | 41 namespace { |
| 41 | 42 |
| 42 PassRefPtr<IDBKey> checkKeyFromValueAndKeyPathInternal(const ScriptValue& value,
const String& keyPath) | 43 PassRefPtr<IDBKey> checkKeyFromValueAndKeyPathInternal(SerializedScriptValue* va
lue, const String& keyPath) |
| 43 { | 44 { |
| 44 IDBKeyPath idbKeyPath(keyPath); | 45 IDBKeyPath idbKeyPath(keyPath); |
| 45 EXPECT_TRUE(idbKeyPath.isValid()); | 46 EXPECT_TRUE(idbKeyPath.isValid()); |
| 46 | 47 return createIDBKeyFromSerializedValueAndKeyPath(value, idbKeyPath); |
| 47 return createIDBKeyFromScriptValueAndKeyPath(value, idbKeyPath); | |
| 48 } | 48 } |
| 49 | 49 |
| 50 void checkKeyPathNullValue(const ScriptValue& value, const String& keyPath) | 50 void checkKeyPathNullValue(SerializedScriptValue* value, const String& keyPath) |
| 51 { | 51 { |
| 52 RefPtr<IDBKey> idbKey = checkKeyFromValueAndKeyPathInternal(value, keyPath); | 52 RefPtr<IDBKey> idbKey = checkKeyFromValueAndKeyPathInternal(value, keyPath); |
| 53 ASSERT_FALSE(idbKey.get()); | 53 ASSERT_FALSE(idbKey.get()); |
| 54 } | 54 } |
| 55 | 55 |
| 56 bool injectKey(PassRefPtr<IDBKey> key, ScriptValue& value, const String& keyPath
) | 56 PassRefPtr<SerializedScriptValue> injectKey(PassRefPtr<IDBKey> key, PassRefPtr<S
erializedScriptValue> value, const String& keyPath) |
| 57 { | 57 { |
| 58 IDBKeyPath idbKeyPath(keyPath); | 58 IDBKeyPath idbKeyPath(keyPath); |
| 59 EXPECT_TRUE(idbKeyPath.isValid()); | 59 EXPECT_TRUE(idbKeyPath.isValid()); |
| 60 return injectIDBKeyIntoScriptValue(key, value, idbKeyPath); | 60 return injectIDBKeyIntoSerializedValue(key, value, idbKeyPath); |
| 61 } | 61 } |
| 62 | 62 |
| 63 void checkInjection(PassRefPtr<IDBKey> prpKey, ScriptValue& value, const String&
keyPath) | 63 void checkInjection(PassRefPtr<IDBKey> prpKey, PassRefPtr<SerializedScriptValue>
value, const String& keyPath) |
| 64 { | 64 { |
| 65 RefPtr<IDBKey> key = prpKey; | 65 RefPtr<IDBKey> key = prpKey; |
| 66 bool result = injectKey(key, value, keyPath); | 66 RefPtr<SerializedScriptValue> newValue = injectKey(key, value, keyPath); |
| 67 ASSERT_TRUE(result); | 67 ASSERT_TRUE(newValue); |
| 68 RefPtr<IDBKey> extractedKey = checkKeyFromValueAndKeyPathInternal(value, key
Path); | 68 RefPtr<IDBKey> extractedKey = checkKeyFromValueAndKeyPathInternal(newValue.g
et(), keyPath); |
| 69 EXPECT_TRUE(key->isEqual(extractedKey.get())); | 69 EXPECT_TRUE(key->isEqual(extractedKey.get())); |
| 70 } | 70 } |
| 71 | 71 |
| 72 void checkInjectionFails(PassRefPtr<IDBKey> key, ScriptValue& value, const Strin
g& keyPath) | 72 void checkInjectionFails(PassRefPtr<IDBKey> key, PassRefPtr<SerializedScriptValu
e> value, const String& keyPath) |
| 73 { | 73 { |
| 74 EXPECT_FALSE(injectKey(key, value, keyPath)); | 74 EXPECT_FALSE(injectKey(key, value, keyPath)); |
| 75 } | 75 } |
| 76 | 76 |
| 77 void checkKeyPathStringValue(const ScriptValue& value, const String& keyPath, co
nst String& expected) | 77 void checkKeyPathStringValue(SerializedScriptValue* value, const String& keyPath
, const String& expected) |
| 78 { | 78 { |
| 79 RefPtr<IDBKey> idbKey = checkKeyFromValueAndKeyPathInternal(value, keyPath); | 79 RefPtr<IDBKey> idbKey = checkKeyFromValueAndKeyPathInternal(value, keyPath); |
| 80 ASSERT_TRUE(idbKey.get()); | 80 ASSERT_TRUE(idbKey.get()); |
| 81 ASSERT_EQ(IDBKey::StringType, idbKey->type()); | 81 ASSERT_EQ(IDBKey::StringType, idbKey->type()); |
| 82 ASSERT_TRUE(expected == idbKey->string()); | 82 ASSERT_TRUE(expected == idbKey->string()); |
| 83 } | 83 } |
| 84 | 84 |
| 85 void checkKeyPathNumberValue(const ScriptValue& value, const String& keyPath, in
t expected) | 85 void checkKeyPathNumberValue(SerializedScriptValue* value, const String& keyPath
, int expected) |
| 86 { | 86 { |
| 87 RefPtr<IDBKey> idbKey = checkKeyFromValueAndKeyPathInternal(value, keyPath); | 87 RefPtr<IDBKey> idbKey = checkKeyFromValueAndKeyPathInternal(value, keyPath); |
| 88 ASSERT_TRUE(idbKey.get()); | 88 ASSERT_TRUE(idbKey.get()); |
| 89 ASSERT_EQ(IDBKey::NumberType, idbKey->type()); | 89 ASSERT_EQ(IDBKey::NumberType, idbKey->type()); |
| 90 ASSERT_TRUE(expected == idbKey->number()); | 90 ASSERT_TRUE(expected == idbKey->number()); |
| 91 } | 91 } |
| 92 | 92 |
| 93 TEST(IDBKeyFromValueAndKeyPathTest, TopLevelPropertyStringValue) | 93 TEST(IDBKeyFromValueAndKeyPathTest, TopLevelPropertyStringValue) |
| 94 { | 94 { |
| 95 v8::HandleScope handleScope; | 95 v8::HandleScope handleScope; |
| 96 v8::Context::Scope scope(V8PerIsolateData::current()->ensureAuxiliaryContext
()); | 96 v8::Context::Scope scope(V8PerIsolateData::current()->ensureAuxiliaryContext
()); |
| 97 | 97 |
| 98 v8::Local<v8::Object> object = v8::Object::New(); | 98 v8::Local<v8::Object> object = v8::Object::New(); |
| 99 object->Set(v8::String::New("foo"), v8::String::New("zoo")); | 99 object->Set(v8::String::New("foo"), v8::String::New("zoo")); |
| 100 | 100 |
| 101 ScriptValue scriptValue(object); | 101 RefPtr<SerializedScriptValue> serializedScriptValue = SerializedScriptValue:
:create(object); |
| 102 | 102 |
| 103 checkKeyPathStringValue(scriptValue, "foo", "zoo"); | 103 checkKeyPathStringValue(serializedScriptValue.get(), "foo", "zoo"); |
| 104 checkKeyPathNullValue(scriptValue, "bar"); | 104 checkKeyPathNullValue(serializedScriptValue.get(), "bar"); |
| 105 } | 105 } |
| 106 | 106 |
| 107 TEST(IDBKeyFromValueAndKeyPathTest, TopLevelPropertyNumberValue) | 107 TEST(IDBKeyFromValueAndKeyPathTest, TopLevelPropertyNumberValue) |
| 108 { | 108 { |
| 109 v8::HandleScope handleScope; | 109 v8::HandleScope handleScope; |
| 110 v8::Context::Scope scope(V8PerIsolateData::current()->ensureAuxiliaryContext
()); | 110 v8::Context::Scope scope(V8PerIsolateData::current()->ensureAuxiliaryContext
()); |
| 111 | 111 |
| 112 v8::Local<v8::Object> object = v8::Object::New(); | 112 v8::Local<v8::Object> object = v8::Object::New(); |
| 113 object->Set(v8::String::New("foo"), v8::Number::New(456)); | 113 object->Set(v8::String::New("foo"), v8::Number::New(456)); |
| 114 | 114 |
| 115 ScriptValue scriptValue(object); | 115 RefPtr<SerializedScriptValue> serializedScriptValue = SerializedScriptValue:
:create(object); |
| 116 | 116 |
| 117 checkKeyPathNumberValue(scriptValue, "foo", 456); | 117 checkKeyPathNumberValue(serializedScriptValue.get(), "foo", 456); |
| 118 checkKeyPathNullValue(scriptValue, "bar"); | 118 checkKeyPathNullValue(serializedScriptValue.get(), "bar"); |
| 119 } | 119 } |
| 120 | 120 |
| 121 TEST(IDBKeyFromValueAndKeyPathTest, SubProperty) | 121 TEST(IDBKeyFromValueAndKeyPathTest, SubProperty) |
| 122 { | 122 { |
| 123 v8::HandleScope handleScope; | 123 v8::HandleScope handleScope; |
| 124 v8::Context::Scope scope(V8PerIsolateData::current()->ensureAuxiliaryContext
()); | 124 v8::Context::Scope scope(V8PerIsolateData::current()->ensureAuxiliaryContext
()); |
| 125 | 125 |
| 126 v8::Local<v8::Object> object = v8::Object::New(); | 126 v8::Local<v8::Object> object = v8::Object::New(); |
| 127 v8::Local<v8::Object> subProperty = v8::Object::New(); | 127 v8::Local<v8::Object> subProperty = v8::Object::New(); |
| 128 subProperty->Set(v8::String::New("bar"), v8::String::New("zee")); | 128 subProperty->Set(v8::String::New("bar"), v8::String::New("zee")); |
| 129 object->Set(v8::String::New("foo"), subProperty); | 129 object->Set(v8::String::New("foo"), subProperty); |
| 130 | 130 |
| 131 ScriptValue scriptValue(object); | 131 RefPtr<SerializedScriptValue> serializedScriptValue = SerializedScriptValue:
:create(object); |
| 132 | 132 |
| 133 checkKeyPathStringValue(scriptValue, "foo.bar", "zee"); | 133 checkKeyPathStringValue(serializedScriptValue.get(), "foo.bar", "zee"); |
| 134 checkKeyPathNullValue(scriptValue, "bar"); | 134 checkKeyPathNullValue(serializedScriptValue.get(), "bar"); |
| 135 } | 135 } |
| 136 | 136 |
| 137 TEST(InjectIDBKeyTest, TopLevelPropertyStringValue) | 137 TEST(InjectIDBKeyTest, TopLevelPropertyStringValue) |
| 138 { | 138 { |
| 139 v8::HandleScope handleScope; | 139 v8::HandleScope handleScope; |
| 140 v8::Context::Scope scope(V8PerIsolateData::current()->ensureAuxiliaryContext
()); | 140 v8::Context::Scope scope(V8PerIsolateData::current()->ensureAuxiliaryContext
()); |
| 141 | 141 |
| 142 v8::Local<v8::Object> object = v8::Object::New(); | 142 v8::Local<v8::Object> object = v8::Object::New(); |
| 143 object->Set(v8::String::New("foo"), v8::String::New("zoo")); | 143 object->Set(v8::String::New("foo"), v8::String::New("zoo")); |
| 144 | 144 |
| 145 ScriptValue foozoo(object); | 145 checkInjection(IDBKey::createString("myNewKey"), SerializedScriptValue::crea
te(object), "bar"); |
| 146 checkInjection(IDBKey::createString("myNewKey"), foozoo, "bar"); | 146 checkInjection(IDBKey::createNumber(1234), SerializedScriptValue::create(obj
ect), "bar"); |
| 147 checkInjection(IDBKey::createNumber(1234), foozoo, "bar"); | |
| 148 | 147 |
| 149 checkInjectionFails(IDBKey::createString("key"), foozoo, "foo.bar"); | 148 checkInjectionFails(IDBKey::createString("key"), SerializedScriptValue::crea
te(object), "foo.bar"); |
| 150 } | 149 } |
| 151 | 150 |
| 152 TEST(InjectIDBKeyTest, SubProperty) | 151 TEST(InjectIDBKeyTest, SubProperty) |
| 153 { | 152 { |
| 154 v8::HandleScope handleScope; | 153 v8::HandleScope handleScope; |
| 155 v8::Context::Scope scope(V8PerIsolateData::current()->ensureAuxiliaryContext
()); | 154 v8::Context::Scope scope(V8PerIsolateData::current()->ensureAuxiliaryContext
()); |
| 156 | 155 |
| 157 v8::Local<v8::Object> object = v8::Object::New(); | 156 v8::Local<v8::Object> object = v8::Object::New(); |
| 158 v8::Local<v8::Object> subProperty = v8::Object::New(); | 157 v8::Local<v8::Object> subProperty = v8::Object::New(); |
| 159 subProperty->Set(v8::String::New("bar"), v8::String::New("zee")); | 158 subProperty->Set(v8::String::New("bar"), v8::String::New("zee")); |
| 160 object->Set(v8::String::New("foo"), subProperty); | 159 object->Set(v8::String::New("foo"), subProperty); |
| 161 | 160 |
| 162 ScriptValue scriptObject(object); | 161 checkInjection(IDBKey::createString("myNewKey"), SerializedScriptValue::crea
te(object), "foo.baz"); |
| 163 checkInjection(IDBKey::createString("myNewKey"), scriptObject, "foo.baz"); | 162 checkInjection(IDBKey::createNumber(789), SerializedScriptValue::create(obje
ct), "foo.baz"); |
| 164 checkInjection(IDBKey::createNumber(789), scriptObject, "foo.baz"); | 163 checkInjection(IDBKey::createDate(4567), SerializedScriptValue::create(objec
t), "foo.baz"); |
| 165 checkInjection(IDBKey::createDate(4567), scriptObject, "foo.baz"); | 164 checkInjection(IDBKey::createDate(4567), SerializedScriptValue::create(objec
t), "bar"); |
| 166 checkInjection(IDBKey::createDate(4567), scriptObject, "bar"); | 165 checkInjection(IDBKey::createArray(IDBKey::KeyArray()), SerializedScriptValu
e::create(object), "foo.baz"); |
| 167 checkInjection(IDBKey::createArray(IDBKey::KeyArray()), scriptObject, "foo.b
az"); | 166 checkInjection(IDBKey::createArray(IDBKey::KeyArray()), SerializedScriptValu
e::create(object), "bar"); |
| 168 checkInjection(IDBKey::createArray(IDBKey::KeyArray()), scriptObject, "bar")
; | |
| 169 | 167 |
| 170 checkInjectionFails(IDBKey::createString("zoo"), scriptObject, "foo.bar.baz"
); | 168 checkInjectionFails(IDBKey::createString("zoo"), SerializedScriptValue::crea
te(object), "foo.bar.baz"); |
| 171 checkInjection(IDBKey::createString("zoo"), scriptObject, "foo.xyz.foo"); | 169 checkInjection(IDBKey::createString("zoo"), SerializedScriptValue::create(ob
ject), "foo.xyz.foo"); |
| 172 } | 170 } |
| 173 | 171 |
| 174 } // namespace | 172 } // namespace |
| 175 | 173 |
| 176 #endif // ENABLE(INDEXED_DATABASE) | 174 #endif // ENABLE(INDEXED_DATABASE) |
| OLD | NEW |