From a89b2ebb8e192c5e8cea21079bda2ee2c0c7dddd Mon Sep 17 00:00:00 2001 From: Simon Hausmann Date: Fri, 25 May 2012 15:09:11 +0200 Subject: Imported WebKit commit eb5c1b8fe4d4b1b90b5137433fc58a91da0e6878 (http://svn.webkit.org/repository/webkit/trunk@118516) --- .../WebKit/chromium/tests/IDBLevelDBCodingTest.cpp | 128 +++++++++++++++++++++ 1 file changed, 128 insertions(+) (limited to 'Source/WebKit/chromium/tests/IDBLevelDBCodingTest.cpp') diff --git a/Source/WebKit/chromium/tests/IDBLevelDBCodingTest.cpp b/Source/WebKit/chromium/tests/IDBLevelDBCodingTest.cpp index eb9962d58..8f5b8ef01 100644 --- a/Source/WebKit/chromium/tests/IDBLevelDBCodingTest.cpp +++ b/Source/WebKit/chromium/tests/IDBLevelDBCodingTest.cpp @@ -30,6 +30,7 @@ #if USE(LEVELDB) #include "IDBKey.h" +#include "IDBKeyPath.h" #include "LevelDBSlice.h" #include #include @@ -418,6 +419,133 @@ TEST(IDBLevelDBCodingTest, EncodeDecodeIDBKey) EXPECT_EQ(0, decodeIDBKey(v.data(), v.data() + v.size() - 1, decodedKey)); } +TEST(IDBLevelDBCodingTest, EncodeIDBKeyPath) +{ + const unsigned char kIDBKeyPathTypeCodedByte1 = 0; + const unsigned char kIDBKeyPathTypeCodedByte2 = 0; + { + IDBKeyPath keyPath; + EXPECT_EQ(keyPath.type(), IDBKeyPath::NullType); + Vector v = encodeIDBKeyPath(keyPath); + EXPECT_EQ(v.size(), 3U); + EXPECT_EQ(v[0], kIDBKeyPathTypeCodedByte1); + EXPECT_EQ(v[1], kIDBKeyPathTypeCodedByte2); + EXPECT_EQ(v[2], IDBKeyPath::NullType); + } + + { + Vector testCases; + testCases.append(""); + testCases.append("foo"); + testCases.append("foo.bar"); + + for (size_t i = 0; i < testCases.size(); ++i) { + IDBKeyPath keyPath = IDBKeyPath(testCases[i]); + Vector v = encodeIDBKeyPath(keyPath); + EXPECT_EQ(v.size(), encodeStringWithLength(testCases[i]).size() + 3); + const char* p = v.data(); + const char* limit = v.data() + v.size(); + EXPECT_EQ(*p++, kIDBKeyPathTypeCodedByte1); + EXPECT_EQ(*p++, kIDBKeyPathTypeCodedByte2); + EXPECT_EQ(*p++, IDBKeyPath::StringType); + String string; + p = decodeStringWithLength(p, limit, string); + EXPECT_EQ(string, testCases[i]); + EXPECT_EQ(p, limit); + } + } + + { + Vector testCase; + testCase.append(""); + testCase.append("foo"); + testCase.append("foo.bar"); + + IDBKeyPath keyPath(testCase); + EXPECT_EQ(keyPath.type(), IDBKeyPath::ArrayType); + Vector v = encodeIDBKeyPath(keyPath); + const char* p = v.data(); + const char* limit = v.data() + v.size(); + EXPECT_EQ(*p++, kIDBKeyPathTypeCodedByte1); + EXPECT_EQ(*p++, kIDBKeyPathTypeCodedByte2); + EXPECT_EQ(*p++, IDBKeyPath::ArrayType); + int64_t count; + p = decodeVarInt(p, limit, count); + EXPECT_EQ(count, static_cast(testCase.size())); + for (size_t i = 0; i < static_cast(count); ++i) { + String string; + p = decodeStringWithLength(p, limit, string); + EXPECT_EQ(string, testCase[i]); + } + EXPECT_EQ(p, limit); + } +} + +TEST(IDBLevelDBCodingTest, DecodeIDBKeyPath) +{ + const unsigned char kIDBKeyPathTypeCodedByte1 = 0; + const unsigned char kIDBKeyPathTypeCodedByte2 = 0; + { + // Legacy encoding of string key paths. + Vector testCases; + testCases.append(""); + testCases.append("foo"); + testCases.append("foo.bar"); + + for (size_t i = 0; i < testCases.size(); ++i) { + Vector v = encodeString(testCases[i]); + IDBKeyPath keyPath = decodeIDBKeyPath(v.data(), v.data() + v.size()); + EXPECT_EQ(keyPath.type(), IDBKeyPath::StringType); + EXPECT_EQ(testCases[i], keyPath.string()); + } + } + { + Vector v; + v.append(kIDBKeyPathTypeCodedByte1); + v.append(kIDBKeyPathTypeCodedByte2); + v.append(IDBKeyPath::NullType); + IDBKeyPath keyPath = decodeIDBKeyPath(v.data(), v.data() + v.size()); + EXPECT_EQ(keyPath.type(), IDBKeyPath::NullType); + EXPECT_TRUE(keyPath.isNull()); + } + { + Vector testCases; + testCases.append(""); + testCases.append("foo"); + testCases.append("foo.bar"); + + for (size_t i = 0; i < testCases.size(); ++i) { + Vector v; + v.append(kIDBKeyPathTypeCodedByte1); + v.append(kIDBKeyPathTypeCodedByte2); + v.append(IDBKeyPath::StringType); + v.append(encodeStringWithLength(testCases[i])); + IDBKeyPath keyPath = decodeIDBKeyPath(v.data(), v.data() + v.size()); + EXPECT_EQ(keyPath.type(), IDBKeyPath::StringType); + EXPECT_EQ(testCases[i], keyPath.string()); + } + } + { + Vector testCase; + testCase.append(""); + testCase.append("foo"); + testCase.append("foo.bar"); + + Vector v; + v.append(kIDBKeyPathTypeCodedByte1); + v.append(kIDBKeyPathTypeCodedByte2); + v.append(IDBKeyPath::ArrayType); + v.append(encodeVarInt(testCase.size())); + for (size_t i = 0; i < testCase.size(); ++i) + v.append(encodeStringWithLength(testCase[i])); + IDBKeyPath keyPath = decodeIDBKeyPath(v.data(), v.data() + v.size()); + EXPECT_EQ(keyPath.type(), IDBKeyPath::ArrayType); + EXPECT_EQ(keyPath.array().size(), testCase.size()); + for (size_t i = 0; i < testCase.size(); ++i) + EXPECT_EQ(keyPath.array()[i], testCase[i]); + } +} + TEST(IDBLevelDBCodingTest, ExtractAndCompareIDBKeys) { Vector > keys; -- cgit v1.2.1