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