OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "content/browser/indexed_db/indexed_db_leveldb_coding.h" |
| 6 |
| 7 #include <limits> |
| 8 #include <string> |
| 9 #include <vector> |
| 10 |
| 11 #include "base/string16.h" |
| 12 #include "base/strings/utf_string_conversions.h" |
| 13 #include "content/browser/indexed_db/leveldb/leveldb_slice.h" |
| 14 #include "content/common/indexed_db/indexed_db_key.h" |
| 15 #include "content/common/indexed_db/indexed_db_key_path.h" |
| 16 #include "testing/gtest/include/gtest/gtest.h" |
| 17 |
| 18 using WebKit::WebIDBKey; |
| 19 using WebKit::WebIDBKeyPath; |
| 20 |
| 21 namespace content { |
| 22 |
| 23 namespace { |
| 24 |
| 25 static IndexedDBKey CreateArrayIDBKey() { |
| 26 return IndexedDBKey(IndexedDBKey::KeyArray()); |
| 27 } |
| 28 |
| 29 static IndexedDBKey CreateArrayIDBKey(const IndexedDBKey& key1) { |
| 30 IndexedDBKey::KeyArray array; |
| 31 array.push_back(key1); |
| 32 return IndexedDBKey(array); |
| 33 } |
| 34 |
| 35 static IndexedDBKey CreateArrayIDBKey(const IndexedDBKey& key1, |
| 36 const IndexedDBKey& key2) { |
| 37 IndexedDBKey::KeyArray array; |
| 38 array.push_back(key1); |
| 39 array.push_back(key2); |
| 40 return IndexedDBKey(array); |
| 41 } |
| 42 |
| 43 TEST(IndexedDBLevelDBCodingTest, EncodeByte) { |
| 44 std::vector<char> expected; |
| 45 expected.push_back(0); |
| 46 unsigned char c; |
| 47 |
| 48 c = 0; |
| 49 expected[0] = c; |
| 50 EXPECT_EQ(expected, EncodeByte(c)); |
| 51 |
| 52 c = 1; |
| 53 expected[0] = c; |
| 54 EXPECT_EQ(expected, EncodeByte(c)); |
| 55 |
| 56 c = 255; |
| 57 expected[0] = c; |
| 58 EXPECT_EQ(expected, EncodeByte(c)); |
| 59 } |
| 60 |
| 61 TEST(IndexedDBLevelDBCodingTest, DecodeByte) { |
| 62 std::vector<unsigned char> test_cases; |
| 63 test_cases.push_back(0); |
| 64 test_cases.push_back(1); |
| 65 test_cases.push_back(255); |
| 66 |
| 67 for (size_t i = 0; i < test_cases.size(); ++i) { |
| 68 unsigned char n = test_cases[i]; |
| 69 std::vector<char> v = EncodeByte(n); |
| 70 |
| 71 unsigned char res; |
| 72 const char* p = DecodeByte(&*v.begin(), &*v.rbegin() + 1, res); |
| 73 EXPECT_EQ(n, res); |
| 74 EXPECT_EQ(&*v.rbegin() + 1, p); |
| 75 } |
| 76 } |
| 77 |
| 78 TEST(IndexedDBLevelDBCodingTest, EncodeBool) { |
| 79 { |
| 80 std::vector<char> expected; |
| 81 expected.push_back(1); |
| 82 EXPECT_EQ(expected, EncodeBool(true)); |
| 83 } |
| 84 { |
| 85 std::vector<char> expected; |
| 86 expected.push_back(0); |
| 87 EXPECT_EQ(expected, EncodeBool(false)); |
| 88 } |
| 89 } |
| 90 |
| 91 static int CompareKeys(const std::vector<char>& a, const std::vector<char>& b) { |
| 92 bool ok; |
| 93 int result = CompareEncodedIDBKeys(a, b, ok); |
| 94 EXPECT_TRUE(ok); |
| 95 return result; |
| 96 } |
| 97 |
| 98 TEST(IndexedDBLevelDBCodingTest, MaxIDBKey) { |
| 99 std::vector<char> max_key = MaxIDBKey(); |
| 100 |
| 101 std::vector<char> min_key = MinIDBKey(); |
| 102 std::vector<char> array_key = |
| 103 EncodeIDBKey(IndexedDBKey(IndexedDBKey::KeyArray())); |
| 104 std::vector<char> string_key = |
| 105 EncodeIDBKey(IndexedDBKey(ASCIIToUTF16("Hello world"))); |
| 106 std::vector<char> number_key = |
| 107 EncodeIDBKey(IndexedDBKey(3.14, WebIDBKey::NumberType)); |
| 108 std::vector<char> date_key = |
| 109 EncodeIDBKey(IndexedDBKey(1000000, WebIDBKey::DateType)); |
| 110 |
| 111 EXPECT_GT(CompareKeys(max_key, min_key), 0); |
| 112 EXPECT_GT(CompareKeys(max_key, array_key), 0); |
| 113 EXPECT_GT(CompareKeys(max_key, string_key), 0); |
| 114 EXPECT_GT(CompareKeys(max_key, number_key), 0); |
| 115 EXPECT_GT(CompareKeys(max_key, date_key), 0); |
| 116 } |
| 117 |
| 118 TEST(IndexedDBLevelDBCodingTest, MinIDBKey) { |
| 119 std::vector<char> min_key = MinIDBKey(); |
| 120 |
| 121 std::vector<char> max_key = MaxIDBKey(); |
| 122 std::vector<char> array_key = |
| 123 EncodeIDBKey(IndexedDBKey(IndexedDBKey::KeyArray())); |
| 124 std::vector<char> string_key = |
| 125 EncodeIDBKey(IndexedDBKey(ASCIIToUTF16("Hello world"))); |
| 126 std::vector<char> number_key = |
| 127 EncodeIDBKey(IndexedDBKey(3.14, WebIDBKey::NumberType)); |
| 128 std::vector<char> date_key = |
| 129 EncodeIDBKey(IndexedDBKey(1000000, WebIDBKey::DateType)); |
| 130 |
| 131 EXPECT_LT(CompareKeys(min_key, max_key), 0); |
| 132 EXPECT_LT(CompareKeys(min_key, array_key), 0); |
| 133 EXPECT_LT(CompareKeys(min_key, string_key), 0); |
| 134 EXPECT_LT(CompareKeys(min_key, number_key), 0); |
| 135 EXPECT_LT(CompareKeys(min_key, date_key), 0); |
| 136 } |
| 137 |
| 138 TEST(IndexedDBLevelDBCodingTest, EncodeInt) { |
| 139 EXPECT_EQ(static_cast<size_t>(1), EncodeInt(0).size()); |
| 140 EXPECT_EQ(static_cast<size_t>(1), EncodeInt(1).size()); |
| 141 EXPECT_EQ(static_cast<size_t>(1), EncodeInt(255).size()); |
| 142 EXPECT_EQ(static_cast<size_t>(2), EncodeInt(256).size()); |
| 143 EXPECT_EQ(static_cast<size_t>(4), EncodeInt(0xffffffff).size()); |
| 144 #ifdef NDEBUG |
| 145 EXPECT_EQ(static_cast<size_t>(8), EncodeInt(-1).size()); |
| 146 #endif |
| 147 } |
| 148 |
| 149 TEST(IndexedDBLevelDBCodingTest, DecodeBool) { |
| 150 { |
| 151 std::vector<char> encoded; |
| 152 encoded.push_back(1); |
| 153 EXPECT_TRUE(DecodeBool(&*encoded.begin(), &*encoded.rbegin() + 1)); |
| 154 } |
| 155 { |
| 156 std::vector<char> encoded; |
| 157 encoded.push_back(0); |
| 158 EXPECT_FALSE(DecodeBool(&*encoded.begin(), &*encoded.rbegin() + 1)); |
| 159 } |
| 160 } |
| 161 |
| 162 TEST(IndexedDBLevelDBCodingTest, DecodeInt) { |
| 163 std::vector<int64> test_cases; |
| 164 test_cases.push_back(0); |
| 165 test_cases.push_back(1); |
| 166 test_cases.push_back(255); |
| 167 test_cases.push_back(256); |
| 168 test_cases.push_back(65535); |
| 169 test_cases.push_back(655536); |
| 170 test_cases.push_back(7711192431755665792ll); |
| 171 test_cases.push_back(0x7fffffffffffffffll); |
| 172 #ifdef NDEBUG |
| 173 test_cases.push_back(-3); |
| 174 #endif |
| 175 |
| 176 for (size_t i = 0; i < test_cases.size(); ++i) { |
| 177 int64 n = test_cases[i]; |
| 178 std::vector<char> v = EncodeInt(n); |
| 179 EXPECT_EQ(n, DecodeInt(&*v.begin(), &*v.rbegin() + 1)); |
| 180 } |
| 181 } |
| 182 |
| 183 TEST(IndexedDBLevelDBCodingTest, EncodeVarInt) { |
| 184 EXPECT_EQ(static_cast<size_t>(1), EncodeVarInt(0).size()); |
| 185 EXPECT_EQ(static_cast<size_t>(1), EncodeVarInt(1).size()); |
| 186 EXPECT_EQ(static_cast<size_t>(2), EncodeVarInt(255).size()); |
| 187 EXPECT_EQ(static_cast<size_t>(2), EncodeVarInt(256).size()); |
| 188 EXPECT_EQ(static_cast<size_t>(5), EncodeVarInt(0xffffffff).size()); |
| 189 EXPECT_EQ(static_cast<size_t>(8), EncodeVarInt(0xfffffffffffffLL).size()); |
| 190 EXPECT_EQ(static_cast<size_t>(9), EncodeVarInt(0x7fffffffffffffffLL).size()); |
| 191 #ifdef NDEBUG |
| 192 EXPECT_EQ(static_cast<size_t>(10), EncodeVarInt(-100).size()); |
| 193 #endif |
| 194 } |
| 195 |
| 196 TEST(IndexedDBLevelDBCodingTest, DecodeVarInt) { |
| 197 std::vector<int64> test_cases; |
| 198 test_cases.push_back(0); |
| 199 test_cases.push_back(1); |
| 200 test_cases.push_back(255); |
| 201 test_cases.push_back(256); |
| 202 test_cases.push_back(65535); |
| 203 test_cases.push_back(655536); |
| 204 test_cases.push_back(7711192431755665792ll); |
| 205 test_cases.push_back(0x7fffffffffffffffll); |
| 206 #ifdef NDEBUG |
| 207 test_cases.push_back(-3); |
| 208 #endif |
| 209 |
| 210 for (size_t i = 0; i < test_cases.size(); ++i) { |
| 211 int64 n = test_cases[i]; |
| 212 std::vector<char> v = EncodeVarInt(n); |
| 213 |
| 214 int64 res; |
| 215 const char* p = DecodeVarInt(&*v.begin(), &*v.rbegin() + 1, res); |
| 216 EXPECT_EQ(n, res); |
| 217 EXPECT_EQ(&*v.rbegin() + 1, p); |
| 218 |
| 219 p = DecodeVarInt(&*v.begin(), &*v.rbegin(), res); |
| 220 EXPECT_EQ(0, p); |
| 221 p = DecodeVarInt(&*v.begin(), &*v.begin(), res); |
| 222 EXPECT_EQ(0, p); |
| 223 } |
| 224 } |
| 225 |
| 226 TEST(IndexedDBLevelDBCodingTest, EncodeString) { |
| 227 const char16 test_string_a[] = {'f', 'o', 'o', '\0'}; |
| 228 const char16 test_string_b[] = {0xdead, 0xbeef, '\0'}; |
| 229 |
| 230 EXPECT_EQ(static_cast<size_t>(0), EncodeString(ASCIIToUTF16("")).size()); |
| 231 EXPECT_EQ(static_cast<size_t>(2), EncodeString(ASCIIToUTF16("a")).size()); |
| 232 EXPECT_EQ(static_cast<size_t>(6), EncodeString(ASCIIToUTF16("foo")).size()); |
| 233 EXPECT_EQ(static_cast<size_t>(6), |
| 234 EncodeString(string16(test_string_a)).size()); |
| 235 EXPECT_EQ(static_cast<size_t>(4), |
| 236 EncodeString(string16(test_string_b)).size()); |
| 237 } |
| 238 |
| 239 TEST(IndexedDBLevelDBCodingTest, DecodeString) { |
| 240 const char16 test_string_a[] = {'f', 'o', 'o', '\0'}; |
| 241 const char16 test_string_b[] = {0xdead, 0xbeef, '\0'}; |
| 242 const char empty[] = {0}; |
| 243 std::vector<char> v; |
| 244 |
| 245 EXPECT_EQ(string16(), DecodeString(empty, empty)); |
| 246 |
| 247 v = EncodeString(ASCIIToUTF16("a")); |
| 248 EXPECT_EQ(ASCIIToUTF16("a"), DecodeString(&*v.begin(), &*v.rbegin() + 1)); |
| 249 |
| 250 v = EncodeString(ASCIIToUTF16("foo")); |
| 251 EXPECT_EQ(ASCIIToUTF16("foo"), DecodeString(&*v.begin(), &*v.rbegin() + 1)); |
| 252 |
| 253 v = EncodeString(string16(test_string_a)); |
| 254 EXPECT_EQ(string16(test_string_a), |
| 255 DecodeString(&*v.begin(), &*v.rbegin() + 1)); |
| 256 |
| 257 v = EncodeString(string16(test_string_b)); |
| 258 EXPECT_EQ(string16(test_string_b), |
| 259 DecodeString(&*v.begin(), &*v.rbegin() + 1)); |
| 260 } |
| 261 |
| 262 TEST(IndexedDBLevelDBCodingTest, EncodeStringWithLength) { |
| 263 const char16 test_string_a[] = {'f', 'o', 'o', '\0'}; |
| 264 const char16 test_string_b[] = {0xdead, 0xbeef, '\0'}; |
| 265 |
| 266 EXPECT_EQ(static_cast<size_t>(1), EncodeStringWithLength(string16()).size()); |
| 267 EXPECT_EQ(static_cast<size_t>(3), |
| 268 EncodeStringWithLength(ASCIIToUTF16("a")).size()); |
| 269 EXPECT_EQ(static_cast<size_t>(7), |
| 270 EncodeStringWithLength(string16(test_string_a)).size()); |
| 271 EXPECT_EQ(static_cast<size_t>(5), |
| 272 EncodeStringWithLength(string16(test_string_b)).size()); |
| 273 } |
| 274 |
| 275 TEST(IndexedDBLevelDBCodingTest, DecodeStringWithLength) { |
| 276 const char16 test_string_a[] = {'f', 'o', 'o', '\0'}; |
| 277 const char16 test_string_b[] = {0xdead, 0xbeef, '\0'}; |
| 278 |
| 279 const int kLongStringLen = 1234; |
| 280 char16 long_string[kLongStringLen + 1]; |
| 281 for (int i = 0; i < kLongStringLen; ++i) |
| 282 long_string[i] = i; |
| 283 long_string[kLongStringLen] = 0; |
| 284 |
| 285 std::vector<string16> test_cases; |
| 286 test_cases.push_back(ASCIIToUTF16("")); |
| 287 test_cases.push_back(ASCIIToUTF16("a")); |
| 288 test_cases.push_back(ASCIIToUTF16("foo")); |
| 289 test_cases.push_back(string16(test_string_a)); |
| 290 test_cases.push_back(string16(test_string_b)); |
| 291 test_cases.push_back(string16(long_string)); |
| 292 |
| 293 for (size_t i = 0; i < test_cases.size(); ++i) { |
| 294 string16 s = test_cases[i]; |
| 295 std::vector<char> v = EncodeStringWithLength(s); |
| 296 string16 res; |
| 297 const char* p = DecodeStringWithLength(&*v.begin(), &*v.rbegin() + 1, res); |
| 298 EXPECT_EQ(s, res); |
| 299 EXPECT_EQ(&*v.rbegin() + 1, p); |
| 300 |
| 301 EXPECT_EQ(0, DecodeStringWithLength(&*v.begin(), &*v.rbegin(), res)); |
| 302 EXPECT_EQ(0, DecodeStringWithLength(&*v.begin(), &*v.begin(), res)); |
| 303 } |
| 304 } |
| 305 |
| 306 static int CompareStrings(const char* p, |
| 307 const char* limit_p, |
| 308 const char* q, |
| 309 const char* limit_q) { |
| 310 bool ok; |
| 311 int result = CompareEncodedStringsWithLength(p, limit_p, q, limit_q, ok); |
| 312 EXPECT_TRUE(ok); |
| 313 EXPECT_EQ(p, limit_p); |
| 314 EXPECT_EQ(q, limit_q); |
| 315 return result; |
| 316 } |
| 317 |
| 318 TEST(IndexedDBLevelDBCodingTest, CompareEncodedStringsWithLength) { |
| 319 const char16 test_string_a[] = {0x1000, 0x1000, '\0'}; |
| 320 const char16 test_string_b[] = {0x1000, 0x1000, 0x1000, '\0'}; |
| 321 const char16 test_string_c[] = {0x1000, 0x1000, 0x1001, '\0'}; |
| 322 const char16 test_string_d[] = {0x1001, 0x1000, 0x1000, '\0'}; |
| 323 const char16 test_string_e[] = {0xd834, 0xdd1e, '\0'}; |
| 324 const char16 test_string_f[] = {0xfffd, '\0'}; |
| 325 |
| 326 std::vector<string16> test_cases; |
| 327 test_cases.push_back(ASCIIToUTF16("")); |
| 328 test_cases.push_back(ASCIIToUTF16("a")); |
| 329 test_cases.push_back(ASCIIToUTF16("b")); |
| 330 test_cases.push_back(ASCIIToUTF16("baaa")); |
| 331 test_cases.push_back(ASCIIToUTF16("baab")); |
| 332 test_cases.push_back(ASCIIToUTF16("c")); |
| 333 test_cases.push_back(string16(test_string_a)); |
| 334 test_cases.push_back(string16(test_string_b)); |
| 335 test_cases.push_back(string16(test_string_c)); |
| 336 test_cases.push_back(string16(test_string_d)); |
| 337 test_cases.push_back(string16(test_string_e)); |
| 338 test_cases.push_back(string16(test_string_f)); |
| 339 |
| 340 for (size_t i = 0; i < test_cases.size() - 1; ++i) { |
| 341 string16 a = test_cases[i]; |
| 342 string16 b = test_cases[i + 1]; |
| 343 |
| 344 EXPECT_LT(a.compare(b), 0); |
| 345 EXPECT_GT(b.compare(a), 0); |
| 346 EXPECT_EQ(a.compare(a), 0); |
| 347 EXPECT_EQ(b.compare(b), 0); |
| 348 |
| 349 std::vector<char> encoded_a = EncodeStringWithLength(a); |
| 350 EXPECT_TRUE(encoded_a.size()); |
| 351 std::vector<char> encoded_b = EncodeStringWithLength(b); |
| 352 EXPECT_TRUE(encoded_a.size()); |
| 353 |
| 354 const char* p = &*encoded_a.begin(); |
| 355 const char* limit_p = &*encoded_a.rbegin() + 1; |
| 356 const char* q = &*encoded_b.begin(); |
| 357 const char* limit_q = &*encoded_b.rbegin() + 1; |
| 358 |
| 359 EXPECT_LT(CompareStrings(p, limit_p, q, limit_q), 0); |
| 360 EXPECT_GT(CompareStrings(q, limit_q, p, limit_p), 0); |
| 361 EXPECT_EQ(CompareStrings(p, limit_p, p, limit_p), 0); |
| 362 EXPECT_EQ(CompareStrings(q, limit_q, q, limit_q), 0); |
| 363 } |
| 364 } |
| 365 |
| 366 TEST(IndexedDBLevelDBCodingTest, EncodeDouble) { |
| 367 EXPECT_EQ(static_cast<size_t>(8), EncodeDouble(0).size()); |
| 368 EXPECT_EQ(static_cast<size_t>(8), EncodeDouble(3.14).size()); |
| 369 } |
| 370 |
| 371 TEST(IndexedDBLevelDBCodingTest, DecodeDouble) { |
| 372 std::vector<char> v; |
| 373 const char* p; |
| 374 double d; |
| 375 |
| 376 v = EncodeDouble(3.14); |
| 377 p = DecodeDouble(&*v.begin(), &*v.rbegin() + 1, &d); |
| 378 EXPECT_EQ(3.14, d); |
| 379 EXPECT_EQ(&*v.rbegin() + 1, p); |
| 380 |
| 381 v = EncodeDouble(-3.14); |
| 382 p = DecodeDouble(&*v.begin(), &*v.rbegin() + 1, &d); |
| 383 EXPECT_EQ(-3.14, d); |
| 384 EXPECT_EQ(&*v.rbegin() + 1, p); |
| 385 |
| 386 v = EncodeDouble(3.14); |
| 387 p = DecodeDouble(&*v.begin(), &*v.rbegin(), &d); |
| 388 EXPECT_EQ(0, p); |
| 389 } |
| 390 |
| 391 TEST(IndexedDBLevelDBCodingTest, EncodeDecodeIDBKey) { |
| 392 IndexedDBKey expected_key; |
| 393 scoped_ptr<IndexedDBKey> decoded_key; |
| 394 std::vector<char> v; |
| 395 const char* p; |
| 396 |
| 397 expected_key = IndexedDBKey(1234, WebIDBKey::NumberType); |
| 398 v = EncodeIDBKey(expected_key); |
| 399 p = DecodeIDBKey(&*v.begin(), &*v.rbegin() + 1, &decoded_key); |
| 400 EXPECT_TRUE(decoded_key->IsEqual(expected_key)); |
| 401 EXPECT_EQ(&*v.rbegin() + 1, p); |
| 402 EXPECT_EQ(0, DecodeIDBKey(&*v.begin(), &*v.rbegin(), &decoded_key)); |
| 403 |
| 404 expected_key = IndexedDBKey(ASCIIToUTF16("Hello World!")); |
| 405 v = EncodeIDBKey(expected_key); |
| 406 p = DecodeIDBKey(&*v.begin(), &*v.rbegin() + 1, &decoded_key); |
| 407 EXPECT_TRUE(decoded_key->IsEqual(expected_key)); |
| 408 EXPECT_EQ(&*v.rbegin() + 1, p); |
| 409 EXPECT_EQ(0, DecodeIDBKey(&*v.begin(), &*v.rbegin(), &decoded_key)); |
| 410 |
| 411 expected_key = IndexedDBKey(IndexedDBKey::KeyArray()); |
| 412 v = EncodeIDBKey(expected_key); |
| 413 p = DecodeIDBKey(&*v.begin(), &*v.rbegin() + 1, &decoded_key); |
| 414 EXPECT_TRUE(decoded_key->IsEqual(expected_key)); |
| 415 EXPECT_EQ(&*v.rbegin() + 1, p); |
| 416 EXPECT_EQ(0, DecodeIDBKey(&*v.begin(), &*v.rbegin(), &decoded_key)); |
| 417 |
| 418 expected_key = IndexedDBKey(7890, WebIDBKey::DateType); |
| 419 v = EncodeIDBKey(expected_key); |
| 420 p = DecodeIDBKey(&*v.begin(), &*v.rbegin() + 1, &decoded_key); |
| 421 EXPECT_TRUE(decoded_key->IsEqual(expected_key)); |
| 422 EXPECT_EQ(&*v.rbegin() + 1, p); |
| 423 EXPECT_EQ(0, DecodeIDBKey(&*v.begin(), &*v.rbegin(), &decoded_key)); |
| 424 |
| 425 IndexedDBKey::KeyArray array; |
| 426 array.push_back(IndexedDBKey(1234, WebIDBKey::NumberType)); |
| 427 array.push_back(IndexedDBKey(ASCIIToUTF16("Hello World!"))); |
| 428 array.push_back(IndexedDBKey(7890, WebIDBKey::DateType)); |
| 429 expected_key = IndexedDBKey(array); |
| 430 v = EncodeIDBKey(expected_key); |
| 431 p = DecodeIDBKey(&*v.begin(), &*v.rbegin() + 1, &decoded_key); |
| 432 EXPECT_TRUE(decoded_key->IsEqual(expected_key)); |
| 433 EXPECT_EQ(&*v.rbegin() + 1, p); |
| 434 EXPECT_EQ(0, DecodeIDBKey(&*v.begin(), &*v.rbegin(), &decoded_key)); |
| 435 } |
| 436 |
| 437 TEST(IndexedDBLevelDBCodingTest, EncodeIDBKeyPath) { |
| 438 const unsigned char kIDBKeyPathTypeCodedByte1 = 0; |
| 439 const unsigned char kIDBKeyPathTypeCodedByte2 = 0; |
| 440 { |
| 441 IndexedDBKeyPath key_path; |
| 442 EXPECT_EQ(key_path.type(), WebIDBKeyPath::NullType); |
| 443 std::vector<char> v = EncodeIDBKeyPath(key_path); |
| 444 EXPECT_EQ(v.size(), 3U); |
| 445 EXPECT_EQ(v[0], kIDBKeyPathTypeCodedByte1); |
| 446 EXPECT_EQ(v[1], kIDBKeyPathTypeCodedByte2); |
| 447 EXPECT_EQ(v[2], WebIDBKeyPath::NullType); |
| 448 } |
| 449 |
| 450 { |
| 451 std::vector<string16> test_cases; |
| 452 test_cases.push_back(string16()); |
| 453 test_cases.push_back(ASCIIToUTF16("foo")); |
| 454 test_cases.push_back(ASCIIToUTF16("foo.bar")); |
| 455 |
| 456 for (size_t i = 0; i < test_cases.size(); ++i) { |
| 457 IndexedDBKeyPath key_path = IndexedDBKeyPath(test_cases[i]); |
| 458 std::vector<char> v = EncodeIDBKeyPath(key_path); |
| 459 EXPECT_EQ(v.size(), EncodeStringWithLength(test_cases[i]).size() + 3); |
| 460 const char* p = &*v.begin(); |
| 461 const char* limit = &*v.rbegin() + 1; |
| 462 EXPECT_EQ(*p++, kIDBKeyPathTypeCodedByte1); |
| 463 EXPECT_EQ(*p++, kIDBKeyPathTypeCodedByte2); |
| 464 EXPECT_EQ(*p++, WebIDBKeyPath::StringType); |
| 465 string16 string; |
| 466 p = DecodeStringWithLength(p, limit, string); |
| 467 EXPECT_EQ(string, test_cases[i]); |
| 468 EXPECT_EQ(p, limit); |
| 469 } |
| 470 } |
| 471 |
| 472 { |
| 473 std::vector<string16> test_case; |
| 474 test_case.push_back(string16()); |
| 475 test_case.push_back(ASCIIToUTF16("foo")); |
| 476 test_case.push_back(ASCIIToUTF16("foo.bar")); |
| 477 |
| 478 IndexedDBKeyPath key_path(test_case); |
| 479 EXPECT_EQ(key_path.type(), WebIDBKeyPath::ArrayType); |
| 480 std::vector<char> v = EncodeIDBKeyPath(key_path); |
| 481 const char* p = &*v.begin(); |
| 482 const char* limit = &*v.rbegin() + 1; |
| 483 EXPECT_EQ(*p++, kIDBKeyPathTypeCodedByte1); |
| 484 EXPECT_EQ(*p++, kIDBKeyPathTypeCodedByte2); |
| 485 EXPECT_EQ(*p++, WebIDBKeyPath::ArrayType); |
| 486 int64 count; |
| 487 p = DecodeVarInt(p, limit, count); |
| 488 EXPECT_EQ(count, static_cast<int64>(test_case.size())); |
| 489 for (size_t i = 0; i < static_cast<size_t>(count); ++i) { |
| 490 string16 string; |
| 491 p = DecodeStringWithLength(p, limit, string); |
| 492 EXPECT_EQ(string, test_case[i]); |
| 493 } |
| 494 EXPECT_EQ(p, limit); |
| 495 } |
| 496 } |
| 497 |
| 498 TEST(IndexedDBLevelDBCodingTest, DecodeIDBKeyPath) { |
| 499 const unsigned char kIDBKeyPathTypeCodedByte1 = 0; |
| 500 const unsigned char kIDBKeyPathTypeCodedByte2 = 0; |
| 501 const char empty[] = {0}; |
| 502 { |
| 503 // Legacy encoding of string key paths. |
| 504 std::vector<string16> test_cases; |
| 505 test_cases.push_back(string16()); |
| 506 test_cases.push_back(ASCIIToUTF16("foo")); |
| 507 test_cases.push_back(ASCIIToUTF16("foo.bar")); |
| 508 |
| 509 for (size_t i = 0; i < test_cases.size(); ++i) { |
| 510 std::vector<char> v = EncodeString(test_cases[i]); |
| 511 const char* begin; |
| 512 const char* end; |
| 513 if (!v.size()) { |
| 514 begin = empty; |
| 515 end = empty; |
| 516 } else { |
| 517 begin = &*v.begin(); |
| 518 end = &*v.rbegin() + 1; |
| 519 } |
| 520 IndexedDBKeyPath key_path = DecodeIDBKeyPath(begin, end); |
| 521 EXPECT_EQ(key_path.type(), WebIDBKeyPath::StringType); |
| 522 EXPECT_EQ(test_cases[i], key_path.string()); |
| 523 } |
| 524 } |
| 525 { |
| 526 std::vector<char> v; |
| 527 v.push_back(kIDBKeyPathTypeCodedByte1); |
| 528 v.push_back(kIDBKeyPathTypeCodedByte2); |
| 529 v.push_back(WebIDBKeyPath::NullType); |
| 530 IndexedDBKeyPath key_path = DecodeIDBKeyPath(&*v.begin(), &*v.rbegin() + 1); |
| 531 EXPECT_EQ(key_path.type(), WebIDBKeyPath::NullType); |
| 532 EXPECT_TRUE(key_path.IsNull()); |
| 533 } |
| 534 { |
| 535 std::vector<string16> test_cases; |
| 536 test_cases.push_back(string16()); |
| 537 test_cases.push_back(ASCIIToUTF16("foo")); |
| 538 test_cases.push_back(ASCIIToUTF16("foo.bar")); |
| 539 |
| 540 for (size_t i = 0; i < test_cases.size(); ++i) { |
| 541 std::vector<char> v; |
| 542 v.push_back(kIDBKeyPathTypeCodedByte1); |
| 543 v.push_back(kIDBKeyPathTypeCodedByte2); |
| 544 v.push_back(WebIDBKeyPath::StringType); |
| 545 std::vector<char> test_case = EncodeStringWithLength(test_cases[i]); |
| 546 v.insert(v.end(), test_case.begin(), test_case.end()); |
| 547 IndexedDBKeyPath key_path = |
| 548 DecodeIDBKeyPath(&*v.begin(), &*v.rbegin() + 1); |
| 549 EXPECT_EQ(key_path.type(), WebIDBKeyPath::StringType); |
| 550 EXPECT_EQ(test_cases[i], key_path.string()); |
| 551 } |
| 552 } |
| 553 { |
| 554 std::vector<string16> test_case; |
| 555 test_case.push_back(string16()); |
| 556 test_case.push_back(ASCIIToUTF16("foo")); |
| 557 test_case.push_back(ASCIIToUTF16("foo.bar")); |
| 558 |
| 559 std::vector<char> v; |
| 560 v.push_back(kIDBKeyPathTypeCodedByte1); |
| 561 v.push_back(kIDBKeyPathTypeCodedByte2); |
| 562 v.push_back(WebIDBKeyPath::ArrayType); |
| 563 std::vector<char> int_value = EncodeVarInt(test_case.size()); |
| 564 v.insert(v.end(), int_value.begin(), int_value.end()); |
| 565 for (size_t i = 0; i < test_case.size(); ++i) { |
| 566 std::vector<char> test_case_value = EncodeStringWithLength(test_case[i]); |
| 567 v.insert(v.end(), test_case_value.begin(), test_case_value.end()); |
| 568 } |
| 569 IndexedDBKeyPath key_path = DecodeIDBKeyPath(&*v.begin(), &*v.rbegin() + 1); |
| 570 EXPECT_EQ(key_path.type(), WebIDBKeyPath::ArrayType); |
| 571 EXPECT_EQ(key_path.array().size(), test_case.size()); |
| 572 for (size_t i = 0; i < test_case.size(); ++i) |
| 573 EXPECT_EQ(key_path.array()[i], test_case[i]); |
| 574 } |
| 575 } |
| 576 |
| 577 TEST(IndexedDBLevelDBCodingTest, ExtractAndCompareIDBKeys) { |
| 578 std::vector<IndexedDBKey> keys; |
| 579 |
| 580 keys.push_back(IndexedDBKey(-10, WebIDBKey::NumberType)); |
| 581 keys.push_back(IndexedDBKey(0, WebIDBKey::NumberType)); |
| 582 keys.push_back(IndexedDBKey(3.14, WebIDBKey::NumberType)); |
| 583 |
| 584 keys.push_back(IndexedDBKey(0, WebIDBKey::DateType)); |
| 585 keys.push_back(IndexedDBKey(100, WebIDBKey::DateType)); |
| 586 keys.push_back(IndexedDBKey(100000, WebIDBKey::DateType)); |
| 587 |
| 588 keys.push_back(IndexedDBKey(ASCIIToUTF16(""))); |
| 589 keys.push_back(IndexedDBKey(ASCIIToUTF16("a"))); |
| 590 keys.push_back(IndexedDBKey(ASCIIToUTF16("b"))); |
| 591 keys.push_back(IndexedDBKey(ASCIIToUTF16("baaa"))); |
| 592 keys.push_back(IndexedDBKey(ASCIIToUTF16("baab"))); |
| 593 keys.push_back(IndexedDBKey(ASCIIToUTF16("c"))); |
| 594 |
| 595 keys.push_back(CreateArrayIDBKey()); |
| 596 keys.push_back(CreateArrayIDBKey(IndexedDBKey(0, WebIDBKey::NumberType))); |
| 597 keys.push_back(CreateArrayIDBKey(IndexedDBKey(0, WebIDBKey::NumberType), |
| 598 IndexedDBKey(3.14, WebIDBKey::NumberType))); |
| 599 keys.push_back(CreateArrayIDBKey(IndexedDBKey(0, WebIDBKey::DateType))); |
| 600 keys.push_back(CreateArrayIDBKey(IndexedDBKey(0, WebIDBKey::DateType), |
| 601 IndexedDBKey(0, WebIDBKey::DateType))); |
| 602 keys.push_back(CreateArrayIDBKey(IndexedDBKey(ASCIIToUTF16("")))); |
| 603 keys.push_back(CreateArrayIDBKey(IndexedDBKey(ASCIIToUTF16("")), |
| 604 IndexedDBKey(ASCIIToUTF16("a")))); |
| 605 keys.push_back(CreateArrayIDBKey(CreateArrayIDBKey())); |
| 606 keys.push_back(CreateArrayIDBKey(CreateArrayIDBKey(), CreateArrayIDBKey())); |
| 607 keys.push_back(CreateArrayIDBKey(CreateArrayIDBKey(CreateArrayIDBKey()))); |
| 608 keys.push_back(CreateArrayIDBKey( |
| 609 CreateArrayIDBKey(CreateArrayIDBKey(CreateArrayIDBKey())))); |
| 610 |
| 611 for (size_t i = 0; i < keys.size() - 1; ++i) { |
| 612 const IndexedDBKey& key_a = keys[i]; |
| 613 const IndexedDBKey& key_b = keys[i + 1]; |
| 614 |
| 615 EXPECT_TRUE(key_a.IsLessThan(key_b)); |
| 616 |
| 617 std::vector<char> encoded_a = EncodeIDBKey(key_a); |
| 618 EXPECT_TRUE(encoded_a.size()); |
| 619 std::vector<char> encoded_b = EncodeIDBKey(key_b); |
| 620 EXPECT_TRUE(encoded_b.size()); |
| 621 |
| 622 std::vector<char> extracted_a; |
| 623 std::vector<char> extracted_b; |
| 624 |
| 625 const char* p = ExtractEncodedIDBKey( |
| 626 &*encoded_a.begin(), &*encoded_a.rbegin() + 1, &extracted_a); |
| 627 EXPECT_EQ(&*encoded_a.rbegin() + 1, p); |
| 628 EXPECT_EQ(encoded_a, extracted_a); |
| 629 |
| 630 const char* q = ExtractEncodedIDBKey( |
| 631 &*encoded_b.begin(), &*encoded_b.rbegin() + 1, &extracted_b); |
| 632 EXPECT_EQ(&*encoded_b.rbegin() + 1, q); |
| 633 EXPECT_EQ(encoded_b, extracted_b); |
| 634 |
| 635 EXPECT_LT(CompareKeys(extracted_a, extracted_b), 0); |
| 636 EXPECT_GT(CompareKeys(extracted_b, extracted_a), 0); |
| 637 EXPECT_EQ(CompareKeys(extracted_a, extracted_a), 0); |
| 638 EXPECT_EQ(CompareKeys(extracted_b, extracted_b), 0); |
| 639 |
| 640 EXPECT_EQ(0, |
| 641 ExtractEncodedIDBKey( |
| 642 &*encoded_a.begin(), &*encoded_a.rbegin(), &extracted_a)); |
| 643 } |
| 644 } |
| 645 |
| 646 TEST(IndexedDBLevelDBCodingTest, ComparisonTest) { |
| 647 std::vector<std::vector<char> > keys; |
| 648 keys.push_back(SchemaVersionKey::Encode()); |
| 649 keys.push_back(MaxDatabaseIdKey::Encode()); |
| 650 keys.push_back(DatabaseFreeListKey::Encode(0)); |
| 651 keys.push_back(DatabaseFreeListKey::EncodeMaxKey()); |
| 652 keys.push_back(DatabaseNameKey::Encode(ASCIIToUTF16(""), ASCIIToUTF16(""))); |
| 653 keys.push_back(DatabaseNameKey::Encode(ASCIIToUTF16(""), ASCIIToUTF16("a"))); |
| 654 keys.push_back(DatabaseNameKey::Encode(ASCIIToUTF16("a"), ASCIIToUTF16("a"))); |
| 655 keys.push_back( |
| 656 DatabaseMetaDataKey::Encode(1, DatabaseMetaDataKey::ORIGIN_NAME)); |
| 657 keys.push_back( |
| 658 DatabaseMetaDataKey::Encode(1, DatabaseMetaDataKey::DATABASE_NAME)); |
| 659 keys.push_back( |
| 660 DatabaseMetaDataKey::Encode(1, DatabaseMetaDataKey::USER_VERSION)); |
| 661 keys.push_back( |
| 662 DatabaseMetaDataKey::Encode(1, DatabaseMetaDataKey::MAX_OBJECT_STORE_ID)); |
| 663 keys.push_back( |
| 664 DatabaseMetaDataKey::Encode(1, DatabaseMetaDataKey::USER_INT_VERSION)); |
| 665 keys.push_back( |
| 666 ObjectStoreMetaDataKey::Encode(1, 1, ObjectStoreMetaDataKey::NAME)); |
| 667 keys.push_back( |
| 668 ObjectStoreMetaDataKey::Encode(1, 1, ObjectStoreMetaDataKey::KEY_PATH)); |
| 669 keys.push_back(ObjectStoreMetaDataKey::Encode( |
| 670 1, 1, ObjectStoreMetaDataKey::AUTO_INCREMENT)); |
| 671 keys.push_back( |
| 672 ObjectStoreMetaDataKey::Encode(1, 1, ObjectStoreMetaDataKey::EVICTABLE)); |
| 673 keys.push_back(ObjectStoreMetaDataKey::Encode( |
| 674 1, 1, ObjectStoreMetaDataKey::LAST_VERSION)); |
| 675 keys.push_back(ObjectStoreMetaDataKey::Encode( |
| 676 1, 1, ObjectStoreMetaDataKey::MAX_INDEX_ID)); |
| 677 keys.push_back(ObjectStoreMetaDataKey::Encode( |
| 678 1, 1, ObjectStoreMetaDataKey::HAS_KEY_PATH)); |
| 679 keys.push_back(ObjectStoreMetaDataKey::Encode( |
| 680 1, 1, ObjectStoreMetaDataKey::KEY_GENERATOR_CURRENT_NUMBER)); |
| 681 keys.push_back(ObjectStoreMetaDataKey::EncodeMaxKey(1, 1)); |
| 682 keys.push_back(ObjectStoreMetaDataKey::EncodeMaxKey(1, 2)); |
| 683 keys.push_back(ObjectStoreMetaDataKey::EncodeMaxKey(1)); |
| 684 keys.push_back(IndexMetaDataKey::Encode(1, 1, 30, IndexMetaDataKey::NAME)); |
| 685 keys.push_back(IndexMetaDataKey::Encode(1, 1, 30, IndexMetaDataKey::UNIQUE)); |
| 686 keys.push_back( |
| 687 IndexMetaDataKey::Encode(1, 1, 30, IndexMetaDataKey::KEY_PATH)); |
| 688 keys.push_back( |
| 689 IndexMetaDataKey::Encode(1, 1, 30, IndexMetaDataKey::MULTI_ENTRY)); |
| 690 keys.push_back(IndexMetaDataKey::Encode(1, 1, 31, 0)); |
| 691 keys.push_back(IndexMetaDataKey::Encode(1, 1, 31, 1)); |
| 692 keys.push_back(IndexMetaDataKey::EncodeMaxKey(1, 1, 31)); |
| 693 keys.push_back(IndexMetaDataKey::EncodeMaxKey(1, 1, 32)); |
| 694 keys.push_back(IndexMetaDataKey::EncodeMaxKey(1, 1)); |
| 695 keys.push_back(IndexMetaDataKey::EncodeMaxKey(1, 2)); |
| 696 keys.push_back(ObjectStoreFreeListKey::Encode(1, 1)); |
| 697 keys.push_back(ObjectStoreFreeListKey::EncodeMaxKey(1)); |
| 698 keys.push_back(IndexFreeListKey::Encode(1, 1, kMinimumIndexId)); |
| 699 keys.push_back(IndexFreeListKey::EncodeMaxKey(1, 1)); |
| 700 keys.push_back(IndexFreeListKey::Encode(1, 2, kMinimumIndexId)); |
| 701 keys.push_back(IndexFreeListKey::EncodeMaxKey(1, 2)); |
| 702 keys.push_back(ObjectStoreNamesKey::Encode(1, ASCIIToUTF16(""))); |
| 703 keys.push_back(ObjectStoreNamesKey::Encode(1, ASCIIToUTF16("a"))); |
| 704 keys.push_back(IndexNamesKey::Encode(1, 1, ASCIIToUTF16(""))); |
| 705 keys.push_back(IndexNamesKey::Encode(1, 1, ASCIIToUTF16("a"))); |
| 706 keys.push_back(IndexNamesKey::Encode(1, 2, ASCIIToUTF16("a"))); |
| 707 keys.push_back(ObjectStoreDataKey::Encode(1, 1, MinIDBKey())); |
| 708 keys.push_back(ObjectStoreDataKey::Encode(1, 1, MaxIDBKey())); |
| 709 keys.push_back(ExistsEntryKey::Encode(1, 1, MinIDBKey())); |
| 710 keys.push_back(ExistsEntryKey::Encode(1, 1, MaxIDBKey())); |
| 711 keys.push_back(IndexDataKey::Encode(1, 1, 30, MinIDBKey(), MinIDBKey(), 0)); |
| 712 keys.push_back(IndexDataKey::Encode(1, 1, 30, MinIDBKey(), MinIDBKey(), 1)); |
| 713 keys.push_back(IndexDataKey::Encode(1, 1, 30, MinIDBKey(), MaxIDBKey(), 0)); |
| 714 keys.push_back(IndexDataKey::Encode(1, 1, 30, MinIDBKey(), MaxIDBKey(), 1)); |
| 715 keys.push_back(IndexDataKey::Encode(1, 1, 30, MaxIDBKey(), MinIDBKey(), 0)); |
| 716 keys.push_back(IndexDataKey::Encode(1, 1, 30, MaxIDBKey(), MinIDBKey(), 1)); |
| 717 keys.push_back(IndexDataKey::Encode(1, 1, 30, MaxIDBKey(), MaxIDBKey(), 0)); |
| 718 keys.push_back(IndexDataKey::Encode(1, 1, 30, MaxIDBKey(), MaxIDBKey(), 1)); |
| 719 keys.push_back(IndexDataKey::Encode(1, 1, 31, MinIDBKey(), MinIDBKey(), 0)); |
| 720 keys.push_back(IndexDataKey::Encode(1, 2, 30, MinIDBKey(), MinIDBKey(), 0)); |
| 721 keys.push_back( |
| 722 IndexDataKey::EncodeMaxKey(1, 2, std::numeric_limits<int32>::max() - 1)); |
| 723 |
| 724 for (size_t i = 0; i < keys.size(); ++i) { |
| 725 const LevelDBSlice key_a(keys[i]); |
| 726 EXPECT_EQ(Compare(key_a, key_a), 0); |
| 727 |
| 728 for (size_t j = i + 1; j < keys.size(); ++j) { |
| 729 const LevelDBSlice key_b(keys[j]); |
| 730 EXPECT_LT(Compare(key_a, key_b), 0); |
| 731 EXPECT_GT(Compare(key_b, key_a), 0); |
| 732 } |
| 733 } |
| 734 } |
| 735 |
| 736 TEST(IndexedDBLevelDBCodingTest, EncodeVarIntVSEncodeByteTest) { |
| 737 std::vector<unsigned char> test_cases; |
| 738 test_cases.push_back(0); |
| 739 test_cases.push_back(1); |
| 740 test_cases.push_back(127); |
| 741 |
| 742 for (size_t i = 0; i < test_cases.size(); ++i) { |
| 743 unsigned char n = test_cases[i]; |
| 744 |
| 745 std::vector<char> vA = EncodeByte(n); |
| 746 std::vector<char> vB = EncodeVarInt(static_cast<int64>(n)); |
| 747 |
| 748 EXPECT_EQ(vA.size(), vB.size()); |
| 749 EXPECT_EQ(*vA.begin(), *vB.begin()); |
| 750 } |
| 751 } |
| 752 |
| 753 } // namespace |
| 754 |
| 755 } // namespace content |
OLD | NEW |