OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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 <limits> |
| 6 |
5 #include "base/bind.h" | 7 #include "base/bind.h" |
6 #include "base/files/file_util.h" | 8 #include "base/files/file_util.h" |
7 #include "base/files/scoped_temp_dir.h" | 9 #include "base/files/scoped_temp_dir.h" |
8 #include "base/strings/stringprintf.h" | 10 #include "base/strings/stringprintf.h" |
9 #include "content/browser/appcache/appcache_database.h" | 11 #include "content/browser/appcache/appcache_database.h" |
10 #include "content/browser/appcache/appcache_entry.h" | 12 #include "content/browser/appcache/appcache_entry.h" |
11 #include "sql/connection.h" | 13 #include "sql/connection.h" |
12 #include "sql/meta_table.h" | 14 #include "sql/meta_table.h" |
13 #include "sql/statement.h" | 15 #include "sql/statement.h" |
14 #include "sql/test/scoped_error_ignorer.h" | 16 #include "sql/test/scoped_error_ignorer.h" |
(...skipping 13 matching lines...) Expand all Loading... |
28 class AppCacheDatabaseTest {}; | 30 class AppCacheDatabaseTest {}; |
29 | 31 |
30 TEST(AppCacheDatabaseTest, LazyOpen) { | 32 TEST(AppCacheDatabaseTest, LazyOpen) { |
31 // Use an empty file path to use an in-memory sqlite database. | 33 // Use an empty file path to use an in-memory sqlite database. |
32 const base::FilePath kEmptyPath; | 34 const base::FilePath kEmptyPath; |
33 AppCacheDatabase db(kEmptyPath); | 35 AppCacheDatabase db(kEmptyPath); |
34 | 36 |
35 EXPECT_FALSE(db.LazyOpen(false)); | 37 EXPECT_FALSE(db.LazyOpen(false)); |
36 EXPECT_TRUE(db.LazyOpen(true)); | 38 EXPECT_TRUE(db.LazyOpen(true)); |
37 | 39 |
38 int64 group_id, cache_id, response_id, deleteable_response_rowid; | 40 int64_t group_id, cache_id, response_id, deleteable_response_rowid; |
39 group_id = cache_id = response_id = deleteable_response_rowid = 0; | 41 group_id = cache_id = response_id = deleteable_response_rowid = 0; |
40 EXPECT_TRUE(db.FindLastStorageIds(&group_id, &cache_id, &response_id, | 42 EXPECT_TRUE(db.FindLastStorageIds(&group_id, &cache_id, &response_id, |
41 &deleteable_response_rowid)); | 43 &deleteable_response_rowid)); |
42 EXPECT_EQ(0, group_id); | 44 EXPECT_EQ(0, group_id); |
43 EXPECT_EQ(0, cache_id); | 45 EXPECT_EQ(0, cache_id); |
44 EXPECT_EQ(0, response_id); | 46 EXPECT_EQ(0, response_id); |
45 EXPECT_EQ(0, deleteable_response_rowid); | 47 EXPECT_EQ(0, deleteable_response_rowid); |
46 | 48 |
47 std::set<GURL> origins; | 49 std::set<GURL> origins; |
48 EXPECT_TRUE(db.FindOriginsWithGroups(&origins)); | 50 EXPECT_TRUE(db.FindOriginsWithGroups(&origins)); |
(...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
128 EXPECT_TRUE(base::PathExists(kDbFile)); | 130 EXPECT_TRUE(base::PathExists(kDbFile)); |
129 EXPECT_FALSE(db.was_corruption_detected()); | 131 EXPECT_FALSE(db.was_corruption_detected()); |
130 | 132 |
131 // Break it. | 133 // Break it. |
132 ASSERT_TRUE(sql::test::CorruptSizeInHeader(kDbFile)); | 134 ASSERT_TRUE(sql::test::CorruptSizeInHeader(kDbFile)); |
133 | 135 |
134 // See the the corruption is detected and reported. | 136 // See the the corruption is detected and reported. |
135 { | 137 { |
136 sql::ScopedErrorIgnorer ignore_errors; | 138 sql::ScopedErrorIgnorer ignore_errors; |
137 ignore_errors.IgnoreError(SQLITE_CORRUPT); | 139 ignore_errors.IgnoreError(SQLITE_CORRUPT); |
138 std::map<GURL, int64> usage_map; | 140 std::map<GURL, int64_t> usage_map; |
139 EXPECT_FALSE(db.GetAllOriginUsage(&usage_map)); | 141 EXPECT_FALSE(db.GetAllOriginUsage(&usage_map)); |
140 EXPECT_TRUE(db.was_corruption_detected()); | 142 EXPECT_TRUE(db.was_corruption_detected()); |
141 EXPECT_TRUE(base::PathExists(kDbFile)); | 143 EXPECT_TRUE(base::PathExists(kDbFile)); |
142 EXPECT_TRUE(ignore_errors.CheckIgnoredErrors()); | 144 EXPECT_TRUE(ignore_errors.CheckIgnoredErrors()); |
143 } | 145 } |
144 } | 146 } |
145 | 147 |
146 TEST(AppCacheDatabaseTest, ExperimentalFlags) { | 148 TEST(AppCacheDatabaseTest, ExperimentalFlags) { |
147 const char kExperimentFlagsKey[] = "ExperimentFlags"; | 149 const char kExperimentFlagsKey[] = "ExperimentFlags"; |
148 std::string kInjectedFlags("exp1,exp2"); | 150 std::string kInjectedFlags("exp1,exp2"); |
(...skipping 524 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
673 | 675 |
674 TEST(AppCacheDatabaseTest, DeletableResponseIds) { | 676 TEST(AppCacheDatabaseTest, DeletableResponseIds) { |
675 const base::FilePath kEmptyPath; | 677 const base::FilePath kEmptyPath; |
676 AppCacheDatabase db(kEmptyPath); | 678 AppCacheDatabase db(kEmptyPath); |
677 EXPECT_TRUE(db.LazyOpen(true)); | 679 EXPECT_TRUE(db.LazyOpen(true)); |
678 | 680 |
679 sql::ScopedErrorIgnorer ignore_errors; | 681 sql::ScopedErrorIgnorer ignore_errors; |
680 // TODO(shess): See EntryRecords test. | 682 // TODO(shess): See EntryRecords test. |
681 ignore_errors.IgnoreError(SQLITE_CONSTRAINT); | 683 ignore_errors.IgnoreError(SQLITE_CONSTRAINT); |
682 | 684 |
683 std::vector<int64> ids; | 685 std::vector<int64_t> ids; |
684 | 686 |
685 EXPECT_TRUE(db.GetDeletableResponseIds(&ids, kint64max, 100)); | 687 EXPECT_TRUE(db.GetDeletableResponseIds( |
| 688 &ids, std::numeric_limits<int64_t>::max(), 100)); |
686 EXPECT_TRUE(ids.empty()); | 689 EXPECT_TRUE(ids.empty()); |
687 ids.push_back(0); | 690 ids.push_back(0); |
688 EXPECT_TRUE(db.DeleteDeletableResponseIds(ids)); | 691 EXPECT_TRUE(db.DeleteDeletableResponseIds(ids)); |
689 EXPECT_TRUE(db.InsertDeletableResponseIds(ids)); | 692 EXPECT_TRUE(db.InsertDeletableResponseIds(ids)); |
690 | 693 |
691 ids.clear(); | 694 ids.clear(); |
692 EXPECT_TRUE(db.GetDeletableResponseIds(&ids, kint64max, 100)); | 695 EXPECT_TRUE(db.GetDeletableResponseIds( |
| 696 &ids, std::numeric_limits<int64_t>::max(), 100)); |
693 EXPECT_EQ(1U, ids.size()); | 697 EXPECT_EQ(1U, ids.size()); |
694 EXPECT_EQ(0, ids[0]); | 698 EXPECT_EQ(0, ids[0]); |
695 | 699 |
696 int64 unused, deleteable_response_rowid; | 700 int64_t unused, deleteable_response_rowid; |
697 unused = deleteable_response_rowid = 0; | 701 unused = deleteable_response_rowid = 0; |
698 EXPECT_TRUE(db.FindLastStorageIds(&unused, &unused, &unused, | 702 EXPECT_TRUE(db.FindLastStorageIds(&unused, &unused, &unused, |
699 &deleteable_response_rowid)); | 703 &deleteable_response_rowid)); |
700 EXPECT_EQ(1, deleteable_response_rowid); | 704 EXPECT_EQ(1, deleteable_response_rowid); |
701 | 705 |
702 | 706 |
703 // Expected to fail due to the duplicate id, 0 is already in the table. | 707 // Expected to fail due to the duplicate id, 0 is already in the table. |
704 ids.clear(); | 708 ids.clear(); |
705 ids.push_back(0); | 709 ids.push_back(0); |
706 ids.push_back(1); | 710 ids.push_back(1); |
707 EXPECT_FALSE(db.InsertDeletableResponseIds(ids)); | 711 EXPECT_FALSE(db.InsertDeletableResponseIds(ids)); |
708 | 712 |
709 ids.clear(); | 713 ids.clear(); |
710 for (int i = 1; i < 10; ++i) | 714 for (int i = 1; i < 10; ++i) |
711 ids.push_back(i); | 715 ids.push_back(i); |
712 EXPECT_TRUE(db.InsertDeletableResponseIds(ids)); | 716 EXPECT_TRUE(db.InsertDeletableResponseIds(ids)); |
713 EXPECT_TRUE(db.FindLastStorageIds(&unused, &unused, &unused, | 717 EXPECT_TRUE(db.FindLastStorageIds(&unused, &unused, &unused, |
714 &deleteable_response_rowid)); | 718 &deleteable_response_rowid)); |
715 EXPECT_EQ(10, deleteable_response_rowid); | 719 EXPECT_EQ(10, deleteable_response_rowid); |
716 | 720 |
717 ids.clear(); | 721 ids.clear(); |
718 EXPECT_TRUE(db.GetDeletableResponseIds(&ids, kint64max, 100)); | 722 EXPECT_TRUE(db.GetDeletableResponseIds( |
| 723 &ids, std::numeric_limits<int64_t>::max(), 100)); |
719 EXPECT_EQ(10U, ids.size()); | 724 EXPECT_EQ(10U, ids.size()); |
720 for (int i = 0; i < 10; ++i) | 725 for (int i = 0; i < 10; ++i) |
721 EXPECT_EQ(i, ids[i]); | 726 EXPECT_EQ(i, ids[i]); |
722 | 727 |
723 // Ensure the limit is respected. | 728 // Ensure the limit is respected. |
724 ids.clear(); | 729 ids.clear(); |
725 EXPECT_TRUE(db.GetDeletableResponseIds(&ids, kint64max, 5)); | 730 EXPECT_TRUE( |
| 731 db.GetDeletableResponseIds(&ids, std::numeric_limits<int64_t>::max(), 5)); |
726 EXPECT_EQ(5U, ids.size()); | 732 EXPECT_EQ(5U, ids.size()); |
727 for (int i = 0; i < static_cast<int>(ids.size()); ++i) | 733 for (int i = 0; i < static_cast<int>(ids.size()); ++i) |
728 EXPECT_EQ(i, ids[i]); | 734 EXPECT_EQ(i, ids[i]); |
729 | 735 |
730 // Ensure the max_rowid is respected (the first rowid is 1). | 736 // Ensure the max_rowid is respected (the first rowid is 1). |
731 ids.clear(); | 737 ids.clear(); |
732 EXPECT_TRUE(db.GetDeletableResponseIds(&ids, 5, 100)); | 738 EXPECT_TRUE(db.GetDeletableResponseIds(&ids, 5, 100)); |
733 EXPECT_EQ(5U, ids.size()); | 739 EXPECT_EQ(5U, ids.size()); |
734 for (int i = 0; i < static_cast<int>(ids.size()); ++i) | 740 for (int i = 0; i < static_cast<int>(ids.size()); ++i) |
735 EXPECT_EQ(i, ids[i]); | 741 EXPECT_EQ(i, ids[i]); |
736 | 742 |
737 // Ensure that we can delete from the table. | 743 // Ensure that we can delete from the table. |
738 EXPECT_TRUE(db.DeleteDeletableResponseIds(ids)); | 744 EXPECT_TRUE(db.DeleteDeletableResponseIds(ids)); |
739 ids.clear(); | 745 ids.clear(); |
740 EXPECT_TRUE(db.GetDeletableResponseIds(&ids, kint64max, 100)); | 746 EXPECT_TRUE(db.GetDeletableResponseIds( |
| 747 &ids, std::numeric_limits<int64_t>::max(), 100)); |
741 EXPECT_EQ(5U, ids.size()); | 748 EXPECT_EQ(5U, ids.size()); |
742 for (int i = 0; i < static_cast<int>(ids.size()); ++i) | 749 for (int i = 0; i < static_cast<int>(ids.size()); ++i) |
743 EXPECT_EQ(i + 5, ids[i]); | 750 EXPECT_EQ(i + 5, ids[i]); |
744 | 751 |
745 ASSERT_TRUE(ignore_errors.CheckIgnoredErrors()); | 752 ASSERT_TRUE(ignore_errors.CheckIgnoredErrors()); |
746 } | 753 } |
747 | 754 |
748 TEST(AppCacheDatabaseTest, OriginUsage) { | 755 TEST(AppCacheDatabaseTest, OriginUsage) { |
749 const GURL kManifestUrl("http://blah/manifest"); | 756 const GURL kManifestUrl("http://blah/manifest"); |
750 const GURL kManifestUrl2("http://blah/manifest2"); | 757 const GURL kManifestUrl2("http://blah/manifest2"); |
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
801 EXPECT_TRUE(db.InsertCache(&cache_record)); | 808 EXPECT_TRUE(db.InsertCache(&cache_record)); |
802 | 809 |
803 EXPECT_EQ(5000, db.GetOriginUsage(kOtherOrigin)); | 810 EXPECT_EQ(5000, db.GetOriginUsage(kOtherOrigin)); |
804 | 811 |
805 EXPECT_TRUE(db.FindCachesForOrigin(kOrigin, &cache_records)); | 812 EXPECT_TRUE(db.FindCachesForOrigin(kOrigin, &cache_records)); |
806 EXPECT_EQ(2U, cache_records.size()); | 813 EXPECT_EQ(2U, cache_records.size()); |
807 cache_records.clear(); | 814 cache_records.clear(); |
808 EXPECT_TRUE(db.FindCachesForOrigin(kOtherOrigin, &cache_records)); | 815 EXPECT_TRUE(db.FindCachesForOrigin(kOtherOrigin, &cache_records)); |
809 EXPECT_EQ(1U, cache_records.size()); | 816 EXPECT_EQ(1U, cache_records.size()); |
810 | 817 |
811 std::map<GURL, int64> usage_map; | 818 std::map<GURL, int64_t> usage_map; |
812 EXPECT_TRUE(db.GetAllOriginUsage(&usage_map)); | 819 EXPECT_TRUE(db.GetAllOriginUsage(&usage_map)); |
813 EXPECT_EQ(2U, usage_map.size()); | 820 EXPECT_EQ(2U, usage_map.size()); |
814 EXPECT_EQ(1100, usage_map[kOrigin]); | 821 EXPECT_EQ(1100, usage_map[kOrigin]); |
815 EXPECT_EQ(5000, usage_map[kOtherOrigin]); | 822 EXPECT_EQ(5000, usage_map[kOtherOrigin]); |
816 } | 823 } |
817 | 824 |
818 #if defined(APPCACHE_USE_SIMPLE_CACHE) | 825 #if defined(APPCACHE_USE_SIMPLE_CACHE) |
819 // There is no such upgrade path in this case. | 826 // There is no such upgrade path in this case. |
820 #else | 827 #else |
821 TEST(AppCacheDatabaseTest, UpgradeSchema4to7) { | 828 TEST(AppCacheDatabaseTest, UpgradeSchema4to7) { |
(...skipping 464 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1286 AppCacheDatabase::GroupRecord group; | 1293 AppCacheDatabase::GroupRecord group; |
1287 EXPECT_TRUE(db.FindGroup(1, &group)); | 1294 EXPECT_TRUE(db.FindGroup(1, &group)); |
1288 EXPECT_EQ(kMockTime, group.last_full_update_check_time); | 1295 EXPECT_EQ(kMockTime, group.last_full_update_check_time); |
1289 EXPECT_EQ(kZeroTime, group.first_evictable_error_time); | 1296 EXPECT_EQ(kZeroTime, group.first_evictable_error_time); |
1290 EXPECT_TRUE(db.FindGroup(2, &group)); | 1297 EXPECT_TRUE(db.FindGroup(2, &group)); |
1291 EXPECT_EQ(kZeroTime, group.last_full_update_check_time); | 1298 EXPECT_EQ(kZeroTime, group.last_full_update_check_time); |
1292 EXPECT_EQ(kZeroTime, group.first_evictable_error_time); | 1299 EXPECT_EQ(kZeroTime, group.first_evictable_error_time); |
1293 } | 1300 } |
1294 | 1301 |
1295 } // namespace content | 1302 } // namespace content |
OLD | NEW |