OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 "testing/gtest/include/gtest/gtest.h" | 5 #include "testing/gtest/include/gtest/gtest.h" |
6 | 6 |
7 #include "base/file_util.h" | 7 #include "base/file_util.h" |
8 #include "base/files/scoped_temp_dir.h" | 8 #include "base/files/scoped_temp_dir.h" |
9 #include "base/stringprintf.h" | 9 #include "base/stringprintf.h" |
10 #include "sql/connection.h" | 10 #include "sql/connection.h" |
(...skipping 23 matching lines...) Expand all Loading... |
34 }; | 34 }; |
35 | 35 |
36 } // namespace | 36 } // namespace |
37 | 37 |
38 namespace appcache { | 38 namespace appcache { |
39 | 39 |
40 class AppCacheDatabaseTest {}; | 40 class AppCacheDatabaseTest {}; |
41 | 41 |
42 TEST(AppCacheDatabaseTest, LazyOpen) { | 42 TEST(AppCacheDatabaseTest, LazyOpen) { |
43 // Use an empty file path to use an in-memory sqlite database. | 43 // Use an empty file path to use an in-memory sqlite database. |
44 const FilePath kEmptyPath; | 44 const base::FilePath kEmptyPath; |
45 AppCacheDatabase db(kEmptyPath); | 45 AppCacheDatabase db(kEmptyPath); |
46 | 46 |
47 EXPECT_FALSE(db.LazyOpen(false)); | 47 EXPECT_FALSE(db.LazyOpen(false)); |
48 EXPECT_TRUE(db.LazyOpen(true)); | 48 EXPECT_TRUE(db.LazyOpen(true)); |
49 | 49 |
50 int64 group_id, cache_id, response_id, deleteable_response_rowid; | 50 int64 group_id, cache_id, response_id, deleteable_response_rowid; |
51 group_id = cache_id = response_id = deleteable_response_rowid = 0; | 51 group_id = cache_id = response_id = deleteable_response_rowid = 0; |
52 EXPECT_TRUE(db.FindLastStorageIds(&group_id, &cache_id, &response_id, | 52 EXPECT_TRUE(db.FindLastStorageIds(&group_id, &cache_id, &response_id, |
53 &deleteable_response_rowid)); | 53 &deleteable_response_rowid)); |
54 EXPECT_EQ(0, group_id); | 54 EXPECT_EQ(0, group_id); |
55 EXPECT_EQ(0, cache_id); | 55 EXPECT_EQ(0, cache_id); |
56 EXPECT_EQ(0, response_id); | 56 EXPECT_EQ(0, response_id); |
57 EXPECT_EQ(0, deleteable_response_rowid); | 57 EXPECT_EQ(0, deleteable_response_rowid); |
58 | 58 |
59 std::set<GURL> origins; | 59 std::set<GURL> origins; |
60 EXPECT_TRUE(db.FindOriginsWithGroups(&origins)); | 60 EXPECT_TRUE(db.FindOriginsWithGroups(&origins)); |
61 EXPECT_TRUE(origins.empty()); | 61 EXPECT_TRUE(origins.empty()); |
62 } | 62 } |
63 | 63 |
64 TEST(AppCacheDatabaseTest, ReCreate) { | 64 TEST(AppCacheDatabaseTest, ReCreate) { |
65 // Real files on disk for this test. | 65 // Real files on disk for this test. |
66 base::ScopedTempDir temp_dir; | 66 base::ScopedTempDir temp_dir; |
67 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); | 67 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); |
68 const FilePath kDbFile = temp_dir.path().AppendASCII("appcache.db"); | 68 const base::FilePath kDbFile = temp_dir.path().AppendASCII("appcache.db"); |
69 const FilePath kNestedDir = temp_dir.path().AppendASCII("nested"); | 69 const base::FilePath kNestedDir = temp_dir.path().AppendASCII("nested"); |
70 const FilePath kOtherFile = kNestedDir.AppendASCII("other_file"); | 70 const base::FilePath kOtherFile = kNestedDir.AppendASCII("other_file"); |
71 EXPECT_TRUE(file_util::CreateDirectory(kNestedDir)); | 71 EXPECT_TRUE(file_util::CreateDirectory(kNestedDir)); |
72 EXPECT_EQ(3, file_util::WriteFile(kOtherFile, "foo", 3)); | 72 EXPECT_EQ(3, file_util::WriteFile(kOtherFile, "foo", 3)); |
73 | 73 |
74 AppCacheDatabase db(kDbFile); | 74 AppCacheDatabase db(kDbFile); |
75 EXPECT_FALSE(db.LazyOpen(false)); | 75 EXPECT_FALSE(db.LazyOpen(false)); |
76 EXPECT_TRUE(db.LazyOpen(true)); | 76 EXPECT_TRUE(db.LazyOpen(true)); |
77 | 77 |
78 EXPECT_TRUE(file_util::PathExists(kDbFile)); | 78 EXPECT_TRUE(file_util::PathExists(kDbFile)); |
79 EXPECT_TRUE(file_util::DirectoryExists(kNestedDir)); | 79 EXPECT_TRUE(file_util::DirectoryExists(kNestedDir)); |
80 EXPECT_TRUE(file_util::PathExists(kOtherFile)); | 80 EXPECT_TRUE(file_util::PathExists(kOtherFile)); |
81 | 81 |
82 EXPECT_TRUE(db.DeleteExistingAndCreateNewDatabase()); | 82 EXPECT_TRUE(db.DeleteExistingAndCreateNewDatabase()); |
83 | 83 |
84 EXPECT_TRUE(file_util::PathExists(kDbFile)); | 84 EXPECT_TRUE(file_util::PathExists(kDbFile)); |
85 EXPECT_FALSE(file_util::DirectoryExists(kNestedDir)); | 85 EXPECT_FALSE(file_util::DirectoryExists(kNestedDir)); |
86 EXPECT_FALSE(file_util::PathExists(kOtherFile)); | 86 EXPECT_FALSE(file_util::PathExists(kOtherFile)); |
87 } | 87 } |
88 | 88 |
89 TEST(AppCacheDatabaseTest, EntryRecords) { | 89 TEST(AppCacheDatabaseTest, EntryRecords) { |
90 const FilePath kEmptyPath; | 90 const base::FilePath kEmptyPath; |
91 AppCacheDatabase db(kEmptyPath); | 91 AppCacheDatabase db(kEmptyPath); |
92 EXPECT_TRUE(db.LazyOpen(true)); | 92 EXPECT_TRUE(db.LazyOpen(true)); |
93 | 93 |
94 // Set an error delegate that will make all operations return false on error. | 94 // Set an error delegate that will make all operations return false on error. |
95 db.db_->set_error_delegate(new TestErrorDelegate()); | 95 db.db_->set_error_delegate(new TestErrorDelegate()); |
96 | 96 |
97 AppCacheDatabase::EntryRecord entry; | 97 AppCacheDatabase::EntryRecord entry; |
98 | 98 |
99 entry.cache_id = 1; | 99 entry.cache_id = 1; |
100 entry.url = GURL("http://blah/1"); | 100 entry.url = GURL("http://blah/1"); |
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
154 EXPECT_TRUE(db.FindEntriesForCache(2, &found)); | 154 EXPECT_TRUE(db.FindEntriesForCache(2, &found)); |
155 EXPECT_TRUE(found.empty()); | 155 EXPECT_TRUE(found.empty()); |
156 found.clear(); | 156 found.clear(); |
157 | 157 |
158 EXPECT_TRUE(db.DeleteEntriesForCache(1)); | 158 EXPECT_TRUE(db.DeleteEntriesForCache(1)); |
159 EXPECT_FALSE(db.AddEntryFlags(GURL("http://blah/1"), 1, | 159 EXPECT_FALSE(db.AddEntryFlags(GURL("http://blah/1"), 1, |
160 AppCacheEntry::FOREIGN)); | 160 AppCacheEntry::FOREIGN)); |
161 } | 161 } |
162 | 162 |
163 TEST(AppCacheDatabaseTest, CacheRecords) { | 163 TEST(AppCacheDatabaseTest, CacheRecords) { |
164 const FilePath kEmptyPath; | 164 const base::FilePath kEmptyPath; |
165 AppCacheDatabase db(kEmptyPath); | 165 AppCacheDatabase db(kEmptyPath); |
166 EXPECT_TRUE(db.LazyOpen(true)); | 166 EXPECT_TRUE(db.LazyOpen(true)); |
167 | 167 |
168 db.db_->set_error_delegate(new TestErrorDelegate()); | 168 db.db_->set_error_delegate(new TestErrorDelegate()); |
169 | 169 |
170 const AppCacheDatabase::CacheRecord kZeroRecord; | 170 const AppCacheDatabase::CacheRecord kZeroRecord; |
171 AppCacheDatabase::CacheRecord record; | 171 AppCacheDatabase::CacheRecord record; |
172 EXPECT_FALSE(db.FindCache(1, &record)); | 172 EXPECT_FALSE(db.FindCache(1, &record)); |
173 | 173 |
174 record.cache_id = 1; | 174 record.cache_id = 1; |
(...skipping 21 matching lines...) Expand all Loading... |
196 EXPECT_EQ(100, record.cache_size); | 196 EXPECT_EQ(100, record.cache_size); |
197 | 197 |
198 EXPECT_TRUE(db.DeleteCache(1)); | 198 EXPECT_TRUE(db.DeleteCache(1)); |
199 EXPECT_FALSE(db.FindCache(1, &record)); | 199 EXPECT_FALSE(db.FindCache(1, &record)); |
200 EXPECT_FALSE(db.FindCacheForGroup(1, &record)); | 200 EXPECT_FALSE(db.FindCacheForGroup(1, &record)); |
201 | 201 |
202 EXPECT_TRUE(db.DeleteCache(1)); | 202 EXPECT_TRUE(db.DeleteCache(1)); |
203 } | 203 } |
204 | 204 |
205 TEST(AppCacheDatabaseTest, GroupRecords) { | 205 TEST(AppCacheDatabaseTest, GroupRecords) { |
206 const FilePath kEmptyPath; | 206 const base::FilePath kEmptyPath; |
207 AppCacheDatabase db(kEmptyPath); | 207 AppCacheDatabase db(kEmptyPath); |
208 EXPECT_TRUE(db.LazyOpen(true)); | 208 EXPECT_TRUE(db.LazyOpen(true)); |
209 | 209 |
210 db.db_->set_error_delegate(new TestErrorDelegate()); | 210 db.db_->set_error_delegate(new TestErrorDelegate()); |
211 | 211 |
212 const GURL kManifestUrl("http://blah/manifest"); | 212 const GURL kManifestUrl("http://blah/manifest"); |
213 const GURL kOrigin(kManifestUrl.GetOrigin()); | 213 const GURL kOrigin(kManifestUrl.GetOrigin()); |
214 const base::Time kLastAccessTime = base::Time::Now(); | 214 const base::Time kLastAccessTime = base::Time::Now(); |
215 const base::Time kCreationTime = | 215 const base::Time kCreationTime = |
216 kLastAccessTime - base::TimeDelta::FromDays(7); | 216 kLastAccessTime - base::TimeDelta::FromDays(7); |
(...skipping 106 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
323 EXPECT_TRUE(db.InsertCache(&cache_record)); | 323 EXPECT_TRUE(db.InsertCache(&cache_record)); |
324 | 324 |
325 record = kZeroRecord; | 325 record = kZeroRecord; |
326 EXPECT_TRUE(db.FindGroupForCache(1, &record)); | 326 EXPECT_TRUE(db.FindGroupForCache(1, &record)); |
327 EXPECT_EQ(1, record.group_id); | 327 EXPECT_EQ(1, record.group_id); |
328 EXPECT_EQ(kManifest2, record.manifest_url); | 328 EXPECT_EQ(kManifest2, record.manifest_url); |
329 EXPECT_EQ(kOrigin2, record.origin); | 329 EXPECT_EQ(kOrigin2, record.origin); |
330 } | 330 } |
331 | 331 |
332 TEST(AppCacheDatabaseTest, NamespaceRecords) { | 332 TEST(AppCacheDatabaseTest, NamespaceRecords) { |
333 const FilePath kEmptyPath; | 333 const base::FilePath kEmptyPath; |
334 AppCacheDatabase db(kEmptyPath); | 334 AppCacheDatabase db(kEmptyPath); |
335 EXPECT_TRUE(db.LazyOpen(true)); | 335 EXPECT_TRUE(db.LazyOpen(true)); |
336 | 336 |
337 db.db_->set_error_delegate(new TestErrorDelegate()); | 337 db.db_->set_error_delegate(new TestErrorDelegate()); |
338 | 338 |
339 const GURL kFooNameSpace1("http://foo/namespace1"); | 339 const GURL kFooNameSpace1("http://foo/namespace1"); |
340 const GURL kFooNameSpace2("http://foo/namespace2"); | 340 const GURL kFooNameSpace2("http://foo/namespace2"); |
341 const GURL kFooFallbackEntry("http://foo/entry"); | 341 const GURL kFooFallbackEntry("http://foo/entry"); |
342 const GURL kFooOrigin(kFooNameSpace1.GetOrigin()); | 342 const GURL kFooOrigin(kFooNameSpace1.GetOrigin()); |
343 const GURL kBarNameSpace1("http://bar/namespace1"); | 343 const GURL kBarNameSpace1("http://bar/namespace1"); |
(...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
423 | 423 |
424 fallbacks.clear(); | 424 fallbacks.clear(); |
425 EXPECT_TRUE(db.FindNamespacesForCache(3, &intercepts, &fallbacks)); | 425 EXPECT_TRUE(db.FindNamespacesForCache(3, &intercepts, &fallbacks)); |
426 EXPECT_EQ(2U, fallbacks.size()); | 426 EXPECT_EQ(2U, fallbacks.size()); |
427 fallbacks.clear(); | 427 fallbacks.clear(); |
428 EXPECT_TRUE(db.FindNamespacesForOrigin(kBarOrigin, &intercepts, &fallbacks)); | 428 EXPECT_TRUE(db.FindNamespacesForOrigin(kBarOrigin, &intercepts, &fallbacks)); |
429 EXPECT_EQ(2U, fallbacks.size()); | 429 EXPECT_EQ(2U, fallbacks.size()); |
430 } | 430 } |
431 | 431 |
432 TEST(AppCacheDatabaseTest, OnlineWhiteListRecords) { | 432 TEST(AppCacheDatabaseTest, OnlineWhiteListRecords) { |
433 const FilePath kEmptyPath; | 433 const base::FilePath kEmptyPath; |
434 AppCacheDatabase db(kEmptyPath); | 434 AppCacheDatabase db(kEmptyPath); |
435 EXPECT_TRUE(db.LazyOpen(true)); | 435 EXPECT_TRUE(db.LazyOpen(true)); |
436 | 436 |
437 db.db_->set_error_delegate(new TestErrorDelegate()); | 437 db.db_->set_error_delegate(new TestErrorDelegate()); |
438 | 438 |
439 const GURL kFooNameSpace1("http://foo/namespace1"); | 439 const GURL kFooNameSpace1("http://foo/namespace1"); |
440 const GURL kFooNameSpace2("http://foo/namespace2"); | 440 const GURL kFooNameSpace2("http://foo/namespace2"); |
441 const GURL kBarNameSpace1("http://bar/namespace1"); | 441 const GURL kBarNameSpace1("http://bar/namespace1"); |
442 | 442 |
443 const AppCacheDatabase::OnlineWhiteListRecord kZeroRecord; | 443 const AppCacheDatabase::OnlineWhiteListRecord kZeroRecord; |
(...skipping 25 matching lines...) Expand all Loading... |
469 EXPECT_TRUE(db.FindOnlineWhiteListForCache(2, &records)); | 469 EXPECT_TRUE(db.FindOnlineWhiteListForCache(2, &records)); |
470 EXPECT_EQ(1U, records.size()); | 470 EXPECT_EQ(1U, records.size()); |
471 | 471 |
472 EXPECT_TRUE(db.DeleteOnlineWhiteListForCache(1)); | 472 EXPECT_TRUE(db.DeleteOnlineWhiteListForCache(1)); |
473 records.clear(); | 473 records.clear(); |
474 EXPECT_TRUE(db.FindOnlineWhiteListForCache(1, &records)); | 474 EXPECT_TRUE(db.FindOnlineWhiteListForCache(1, &records)); |
475 EXPECT_TRUE(records.empty()); | 475 EXPECT_TRUE(records.empty()); |
476 } | 476 } |
477 | 477 |
478 TEST(AppCacheDatabaseTest, DeletableResponseIds) { | 478 TEST(AppCacheDatabaseTest, DeletableResponseIds) { |
479 const FilePath kEmptyPath; | 479 const base::FilePath kEmptyPath; |
480 AppCacheDatabase db(kEmptyPath); | 480 AppCacheDatabase db(kEmptyPath); |
481 EXPECT_TRUE(db.LazyOpen(true)); | 481 EXPECT_TRUE(db.LazyOpen(true)); |
482 | 482 |
483 db.db_->set_error_delegate(new TestErrorDelegate()); | 483 db.db_->set_error_delegate(new TestErrorDelegate()); |
484 | 484 |
485 std::vector<int64> ids; | 485 std::vector<int64> ids; |
486 | 486 |
487 EXPECT_TRUE(db.GetDeletableResponseIds(&ids, kint64max, 100)); | 487 EXPECT_TRUE(db.GetDeletableResponseIds(&ids, kint64max, 100)); |
488 EXPECT_TRUE(ids.empty()); | 488 EXPECT_TRUE(ids.empty()); |
489 ids.push_back(0); | 489 ids.push_back(0); |
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
545 EXPECT_EQ(i + 5, ids[i]); | 545 EXPECT_EQ(i + 5, ids[i]); |
546 } | 546 } |
547 | 547 |
548 TEST(AppCacheDatabaseTest, OriginUsage) { | 548 TEST(AppCacheDatabaseTest, OriginUsage) { |
549 const GURL kManifestUrl("http://blah/manifest"); | 549 const GURL kManifestUrl("http://blah/manifest"); |
550 const GURL kManifestUrl2("http://blah/manifest2"); | 550 const GURL kManifestUrl2("http://blah/manifest2"); |
551 const GURL kOrigin(kManifestUrl.GetOrigin()); | 551 const GURL kOrigin(kManifestUrl.GetOrigin()); |
552 const GURL kOtherOriginManifestUrl("http://other/manifest"); | 552 const GURL kOtherOriginManifestUrl("http://other/manifest"); |
553 const GURL kOtherOrigin(kOtherOriginManifestUrl.GetOrigin()); | 553 const GURL kOtherOrigin(kOtherOriginManifestUrl.GetOrigin()); |
554 | 554 |
555 const FilePath kEmptyPath; | 555 const base::FilePath kEmptyPath; |
556 AppCacheDatabase db(kEmptyPath); | 556 AppCacheDatabase db(kEmptyPath); |
557 EXPECT_TRUE(db.LazyOpen(true)); | 557 EXPECT_TRUE(db.LazyOpen(true)); |
558 | 558 |
559 db.db_->set_error_delegate(new TestErrorDelegate()); | 559 db.db_->set_error_delegate(new TestErrorDelegate()); |
560 | 560 |
561 std::vector<AppCacheDatabase::CacheRecord> cache_records; | 561 std::vector<AppCacheDatabase::CacheRecord> cache_records; |
562 EXPECT_EQ(0, db.GetOriginUsage(kOrigin)); | 562 EXPECT_EQ(0, db.GetOriginUsage(kOrigin)); |
563 EXPECT_TRUE(db.FindCachesForOrigin(kOrigin, &cache_records)); | 563 EXPECT_TRUE(db.FindCachesForOrigin(kOrigin, &cache_records)); |
564 EXPECT_TRUE(cache_records.empty()); | 564 EXPECT_TRUE(cache_records.empty()); |
565 | 565 |
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
614 EXPECT_TRUE(db.GetAllOriginUsage(&usage_map)); | 614 EXPECT_TRUE(db.GetAllOriginUsage(&usage_map)); |
615 EXPECT_EQ(2U, usage_map.size()); | 615 EXPECT_EQ(2U, usage_map.size()); |
616 EXPECT_EQ(1100, usage_map[kOrigin]); | 616 EXPECT_EQ(1100, usage_map[kOrigin]); |
617 EXPECT_EQ(5000, usage_map[kOtherOrigin]); | 617 EXPECT_EQ(5000, usage_map[kOtherOrigin]); |
618 } | 618 } |
619 | 619 |
620 TEST(AppCacheDatabaseTest, UpgradeSchema3to4) { | 620 TEST(AppCacheDatabaseTest, UpgradeSchema3to4) { |
621 // Real file on disk for this test. | 621 // Real file on disk for this test. |
622 base::ScopedTempDir temp_dir; | 622 base::ScopedTempDir temp_dir; |
623 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); | 623 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); |
624 const FilePath kDbFile = temp_dir.path().AppendASCII("upgrade.db"); | 624 const base::FilePath kDbFile = temp_dir.path().AppendASCII("upgrade.db"); |
625 | 625 |
626 const GURL kMockOrigin("http://mockorigin/"); | 626 const GURL kMockOrigin("http://mockorigin/"); |
627 const char kNamespaceUrlFormat[] = "namespace%d"; | 627 const char kNamespaceUrlFormat[] = "namespace%d"; |
628 const char kTargetUrlFormat[] = "target%d"; | 628 const char kTargetUrlFormat[] = "target%d"; |
629 const int kNumNamespaces = 10; | 629 const int kNumNamespaces = 10; |
630 | 630 |
631 // Create a v3 schema based database containing some fallback records. | 631 // Create a v3 schema based database containing some fallback records. |
632 { | 632 { |
633 const int kVersion3 = 3; | 633 const int kVersion3 = 3; |
634 const char kGroupsTable[] = "Groups"; | 634 const char kGroupsTable[] = "Groups"; |
(...skipping 192 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
827 | 827 |
828 EXPECT_EQ(i, fallbacks[i].cache_id); | 828 EXPECT_EQ(i, fallbacks[i].cache_id); |
829 EXPECT_EQ(FALLBACK_NAMESPACE, fallbacks[i].type); | 829 EXPECT_EQ(FALLBACK_NAMESPACE, fallbacks[i].type); |
830 EXPECT_EQ(kMockOrigin, fallbacks[i].origin); | 830 EXPECT_EQ(kMockOrigin, fallbacks[i].origin); |
831 EXPECT_EQ(expected_namespace_url, fallbacks[i].namespace_url); | 831 EXPECT_EQ(expected_namespace_url, fallbacks[i].namespace_url); |
832 EXPECT_EQ(expected_target_url, fallbacks[i].target_url); | 832 EXPECT_EQ(expected_target_url, fallbacks[i].target_url); |
833 } | 833 } |
834 } | 834 } |
835 | 835 |
836 } // namespace appcache | 836 } // namespace appcache |
OLD | NEW |