| 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 <stdint.h> | 5 #include <stdint.h> |
| 6 | 6 |
| 7 #include <limits> | 7 #include <limits> |
| 8 | 8 |
| 9 #include "base/bind.h" | 9 #include "base/bind.h" |
| 10 #include "base/files/file_util.h" | 10 #include "base/files/file_util.h" |
| (...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 51 | 51 |
| 52 std::set<GURL> origins; | 52 std::set<GURL> origins; |
| 53 EXPECT_TRUE(db.FindOriginsWithGroups(&origins)); | 53 EXPECT_TRUE(db.FindOriginsWithGroups(&origins)); |
| 54 EXPECT_TRUE(origins.empty()); | 54 EXPECT_TRUE(origins.empty()); |
| 55 } | 55 } |
| 56 | 56 |
| 57 TEST(AppCacheDatabaseTest, ReCreate) { | 57 TEST(AppCacheDatabaseTest, ReCreate) { |
| 58 // Real files on disk for this test. | 58 // Real files on disk for this test. |
| 59 base::ScopedTempDir temp_dir; | 59 base::ScopedTempDir temp_dir; |
| 60 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); | 60 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); |
| 61 const base::FilePath kDbFile = temp_dir.path().AppendASCII("appcache.db"); | 61 const base::FilePath kDbFile = temp_dir.GetPath().AppendASCII("appcache.db"); |
| 62 const base::FilePath kNestedDir = temp_dir.path().AppendASCII("nested"); | 62 const base::FilePath kNestedDir = temp_dir.GetPath().AppendASCII("nested"); |
| 63 const base::FilePath kOtherFile = kNestedDir.AppendASCII("other_file"); | 63 const base::FilePath kOtherFile = kNestedDir.AppendASCII("other_file"); |
| 64 EXPECT_TRUE(base::CreateDirectory(kNestedDir)); | 64 EXPECT_TRUE(base::CreateDirectory(kNestedDir)); |
| 65 EXPECT_EQ(3, base::WriteFile(kOtherFile, "foo", 3)); | 65 EXPECT_EQ(3, base::WriteFile(kOtherFile, "foo", 3)); |
| 66 | 66 |
| 67 AppCacheDatabase db(kDbFile); | 67 AppCacheDatabase db(kDbFile); |
| 68 EXPECT_FALSE(db.LazyOpen(false)); | 68 EXPECT_FALSE(db.LazyOpen(false)); |
| 69 EXPECT_TRUE(db.LazyOpen(true)); | 69 EXPECT_TRUE(db.LazyOpen(true)); |
| 70 | 70 |
| 71 EXPECT_TRUE(base::PathExists(kDbFile)); | 71 EXPECT_TRUE(base::PathExists(kDbFile)); |
| 72 EXPECT_TRUE(base::DirectoryExists(kNestedDir)); | 72 EXPECT_TRUE(base::DirectoryExists(kNestedDir)); |
| (...skipping 10 matching lines...) Expand all Loading... |
| 83 // Only run in release builds because sql::Connection and familiy | 83 // Only run in release builds because sql::Connection and familiy |
| 84 // crank up DLOG(FATAL)'ness and this test presents it with | 84 // crank up DLOG(FATAL)'ness and this test presents it with |
| 85 // intentionally bad data which causes debug builds to exit instead | 85 // intentionally bad data which causes debug builds to exit instead |
| 86 // of run to completion. In release builds, errors the are delivered | 86 // of run to completion. In release builds, errors the are delivered |
| 87 // to the consumer so we can test the error handling of the consumer. | 87 // to the consumer so we can test the error handling of the consumer. |
| 88 // TODO: crbug/328576 | 88 // TODO: crbug/328576 |
| 89 TEST(AppCacheDatabaseTest, QuickIntegrityCheck) { | 89 TEST(AppCacheDatabaseTest, QuickIntegrityCheck) { |
| 90 // Real files on disk for this test too, a corrupt database file. | 90 // Real files on disk for this test too, a corrupt database file. |
| 91 base::ScopedTempDir temp_dir; | 91 base::ScopedTempDir temp_dir; |
| 92 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); | 92 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); |
| 93 base::FilePath mock_dir = temp_dir.path().AppendASCII("mock"); | 93 base::FilePath mock_dir = temp_dir.GetPath().AppendASCII("mock"); |
| 94 ASSERT_TRUE(base::CreateDirectory(mock_dir)); | 94 ASSERT_TRUE(base::CreateDirectory(mock_dir)); |
| 95 | 95 |
| 96 const base::FilePath kDbFile = mock_dir.AppendASCII("appcache.db"); | 96 const base::FilePath kDbFile = mock_dir.AppendASCII("appcache.db"); |
| 97 const base::FilePath kOtherFile = mock_dir.AppendASCII("other_file"); | 97 const base::FilePath kOtherFile = mock_dir.AppendASCII("other_file"); |
| 98 EXPECT_EQ(3, base::WriteFile(kOtherFile, "foo", 3)); | 98 EXPECT_EQ(3, base::WriteFile(kOtherFile, "foo", 3)); |
| 99 | 99 |
| 100 // First create a valid db file. | 100 // First create a valid db file. |
| 101 { | 101 { |
| 102 AppCacheDatabase db(kDbFile); | 102 AppCacheDatabase db(kDbFile); |
| 103 EXPECT_TRUE(db.LazyOpen(true)); | 103 EXPECT_TRUE(db.LazyOpen(true)); |
| (...skipping 14 matching lines...) Expand all Loading... |
| 118 EXPECT_TRUE(base::PathExists(kDbFile)); | 118 EXPECT_TRUE(base::PathExists(kDbFile)); |
| 119 EXPECT_TRUE(expecter.SawExpectedErrors()); | 119 EXPECT_TRUE(expecter.SawExpectedErrors()); |
| 120 } | 120 } |
| 121 } | 121 } |
| 122 #endif // NDEBUG | 122 #endif // NDEBUG |
| 123 | 123 |
| 124 TEST(AppCacheDatabaseTest, WasCorrutionDetected) { | 124 TEST(AppCacheDatabaseTest, WasCorrutionDetected) { |
| 125 // Real files on disk for this test too, a corrupt database file. | 125 // Real files on disk for this test too, a corrupt database file. |
| 126 base::ScopedTempDir temp_dir; | 126 base::ScopedTempDir temp_dir; |
| 127 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); | 127 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); |
| 128 const base::FilePath kDbFile = temp_dir.path().AppendASCII("appcache.db"); | 128 const base::FilePath kDbFile = temp_dir.GetPath().AppendASCII("appcache.db"); |
| 129 | 129 |
| 130 // First create a valid db file. | 130 // First create a valid db file. |
| 131 AppCacheDatabase db(kDbFile); | 131 AppCacheDatabase db(kDbFile); |
| 132 EXPECT_TRUE(db.LazyOpen(true)); | 132 EXPECT_TRUE(db.LazyOpen(true)); |
| 133 EXPECT_TRUE(base::PathExists(kDbFile)); | 133 EXPECT_TRUE(base::PathExists(kDbFile)); |
| 134 EXPECT_FALSE(db.was_corruption_detected()); | 134 EXPECT_FALSE(db.was_corruption_detected()); |
| 135 | 135 |
| 136 // Break it. | 136 // Break it. |
| 137 ASSERT_TRUE(sql::test::CorruptSizeInHeader(kDbFile)); | 137 ASSERT_TRUE(sql::test::CorruptSizeInHeader(kDbFile)); |
| 138 | 138 |
| 139 // See the the corruption is detected and reported. | 139 // See the the corruption is detected and reported. |
| 140 { | 140 { |
| 141 sql::test::ScopedErrorExpecter expecter; | 141 sql::test::ScopedErrorExpecter expecter; |
| 142 expecter.ExpectError(SQLITE_CORRUPT); | 142 expecter.ExpectError(SQLITE_CORRUPT); |
| 143 std::map<GURL, int64_t> usage_map; | 143 std::map<GURL, int64_t> usage_map; |
| 144 EXPECT_FALSE(db.GetAllOriginUsage(&usage_map)); | 144 EXPECT_FALSE(db.GetAllOriginUsage(&usage_map)); |
| 145 EXPECT_TRUE(db.was_corruption_detected()); | 145 EXPECT_TRUE(db.was_corruption_detected()); |
| 146 EXPECT_TRUE(base::PathExists(kDbFile)); | 146 EXPECT_TRUE(base::PathExists(kDbFile)); |
| 147 EXPECT_TRUE(expecter.SawExpectedErrors()); | 147 EXPECT_TRUE(expecter.SawExpectedErrors()); |
| 148 } | 148 } |
| 149 } | 149 } |
| 150 | 150 |
| 151 TEST(AppCacheDatabaseTest, ExperimentalFlags) { | 151 TEST(AppCacheDatabaseTest, ExperimentalFlags) { |
| 152 const char kExperimentFlagsKey[] = "ExperimentFlags"; | 152 const char kExperimentFlagsKey[] = "ExperimentFlags"; |
| 153 std::string kInjectedFlags("exp1,exp2"); | 153 std::string kInjectedFlags("exp1,exp2"); |
| 154 | 154 |
| 155 // Real files on disk for this test. | 155 // Real files on disk for this test. |
| 156 base::ScopedTempDir temp_dir; | 156 base::ScopedTempDir temp_dir; |
| 157 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); | 157 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); |
| 158 const base::FilePath kDbFile = temp_dir.path().AppendASCII("appcache.db"); | 158 const base::FilePath kDbFile = temp_dir.GetPath().AppendASCII("appcache.db"); |
| 159 const base::FilePath kOtherFile = temp_dir.path().AppendASCII("other_file"); | 159 const base::FilePath kOtherFile = |
| 160 temp_dir.GetPath().AppendASCII("other_file"); |
| 160 EXPECT_EQ(3, base::WriteFile(kOtherFile, "foo", 3)); | 161 EXPECT_EQ(3, base::WriteFile(kOtherFile, "foo", 3)); |
| 161 EXPECT_TRUE(base::PathExists(kOtherFile)); | 162 EXPECT_TRUE(base::PathExists(kOtherFile)); |
| 162 | 163 |
| 163 // Inject a non empty flags value, and verify it got there. | 164 // Inject a non empty flags value, and verify it got there. |
| 164 { | 165 { |
| 165 AppCacheDatabase db(kDbFile); | 166 AppCacheDatabase db(kDbFile); |
| 166 EXPECT_TRUE(db.LazyOpen(true)); | 167 EXPECT_TRUE(db.LazyOpen(true)); |
| 167 EXPECT_TRUE(db.meta_table_->SetValue(kExperimentFlagsKey, kInjectedFlags)); | 168 EXPECT_TRUE(db.meta_table_->SetValue(kExperimentFlagsKey, kInjectedFlags)); |
| 168 std::string flags; | 169 std::string flags; |
| 169 EXPECT_TRUE(db.meta_table_->GetValue(kExperimentFlagsKey, &flags)); | 170 EXPECT_TRUE(db.meta_table_->GetValue(kExperimentFlagsKey, &flags)); |
| (...skipping 655 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 825 EXPECT_EQ(5000, usage_map[kOtherOrigin]); | 826 EXPECT_EQ(5000, usage_map[kOtherOrigin]); |
| 826 } | 827 } |
| 827 | 828 |
| 828 #if defined(APPCACHE_USE_SIMPLE_CACHE) | 829 #if defined(APPCACHE_USE_SIMPLE_CACHE) |
| 829 // There is no such upgrade path in this case. | 830 // There is no such upgrade path in this case. |
| 830 #else | 831 #else |
| 831 TEST(AppCacheDatabaseTest, UpgradeSchema4to7) { | 832 TEST(AppCacheDatabaseTest, UpgradeSchema4to7) { |
| 832 // Real file on disk for this test. | 833 // Real file on disk for this test. |
| 833 base::ScopedTempDir temp_dir; | 834 base::ScopedTempDir temp_dir; |
| 834 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); | 835 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); |
| 835 const base::FilePath kDbFile = temp_dir.path().AppendASCII("upgrade4.db"); | 836 const base::FilePath kDbFile = temp_dir.GetPath().AppendASCII("upgrade4.db"); |
| 836 | 837 |
| 837 const GURL kMockOrigin("http://mockorigin/"); | 838 const GURL kMockOrigin("http://mockorigin/"); |
| 838 const char kNamespaceUrlFormat[] = "namespace%d"; | 839 const char kNamespaceUrlFormat[] = "namespace%d"; |
| 839 const char kWhitelistUrlFormat[] = "whitelist%d"; | 840 const char kWhitelistUrlFormat[] = "whitelist%d"; |
| 840 const char kTargetUrlFormat[] = "target%d"; | 841 const char kTargetUrlFormat[] = "target%d"; |
| 841 const int kNumNamespaces = 10; | 842 const int kNumNamespaces = 10; |
| 842 const int kWhitelistCacheId = 1; | 843 const int kWhitelistCacheId = 1; |
| 843 | 844 |
| 844 // Create a v4 schema based database containing some fallback records. | 845 // Create a v4 schema based database containing some fallback records. |
| 845 { | 846 { |
| (...skipping 224 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1070 } | 1071 } |
| 1071 } | 1072 } |
| 1072 #endif // !APPCACHE_USE_SIMPLE_CACHE | 1073 #endif // !APPCACHE_USE_SIMPLE_CACHE |
| 1073 | 1074 |
| 1074 // Verify last_full_update_check_time and first_evictable_error_time. | 1075 // Verify last_full_update_check_time and first_evictable_error_time. |
| 1075 TEST(AppCacheDatabaseTest, UpgradeSchema5or6to7) { | 1076 TEST(AppCacheDatabaseTest, UpgradeSchema5or6to7) { |
| 1076 // Real file on disk for this test. | 1077 // Real file on disk for this test. |
| 1077 base::ScopedTempDir temp_dir; | 1078 base::ScopedTempDir temp_dir; |
| 1078 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); | 1079 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); |
| 1079 const base::FilePath kDbFile = | 1080 const base::FilePath kDbFile = |
| 1080 temp_dir.path().AppendASCII("upgrade5or6to7.db"); | 1081 temp_dir.GetPath().AppendASCII("upgrade5or6to7.db"); |
| 1081 | 1082 |
| 1082 const GURL kMockOrigin("http://mockorigin/"); | 1083 const GURL kMockOrigin("http://mockorigin/"); |
| 1083 const base::Time kMockTime = base::Time::Now(); | 1084 const base::Time kMockTime = base::Time::Now(); |
| 1084 | 1085 |
| 1085 // Create a v5or6 schema based database containing two groups, one | 1086 // Create a v5or6 schema based database containing two groups, one |
| 1086 // that has an associated cache as expected, and one which erroneously | 1087 // that has an associated cache as expected, and one which erroneously |
| 1087 // is missing its cache record. | 1088 // is missing its cache record. |
| 1088 { | 1089 { |
| 1089 // The SQL schema is the same in these two cases. | 1090 // The SQL schema is the same in these two cases. |
| 1090 #if defined(APPCACHE_USE_SIMPLE_CACHE) | 1091 #if defined(APPCACHE_USE_SIMPLE_CACHE) |
| (...skipping 205 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1296 AppCacheDatabase::GroupRecord group; | 1297 AppCacheDatabase::GroupRecord group; |
| 1297 EXPECT_TRUE(db.FindGroup(1, &group)); | 1298 EXPECT_TRUE(db.FindGroup(1, &group)); |
| 1298 EXPECT_EQ(kMockTime, group.last_full_update_check_time); | 1299 EXPECT_EQ(kMockTime, group.last_full_update_check_time); |
| 1299 EXPECT_EQ(kZeroTime, group.first_evictable_error_time); | 1300 EXPECT_EQ(kZeroTime, group.first_evictable_error_time); |
| 1300 EXPECT_TRUE(db.FindGroup(2, &group)); | 1301 EXPECT_TRUE(db.FindGroup(2, &group)); |
| 1301 EXPECT_EQ(kZeroTime, group.last_full_update_check_time); | 1302 EXPECT_EQ(kZeroTime, group.last_full_update_check_time); |
| 1302 EXPECT_EQ(kZeroTime, group.first_evictable_error_time); | 1303 EXPECT_EQ(kZeroTime, group.first_evictable_error_time); |
| 1303 } | 1304 } |
| 1304 | 1305 |
| 1305 } // namespace content | 1306 } // namespace content |
| OLD | NEW |