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 <string> | 7 #include <string> |
8 | 8 |
9 #include "base/file_path.h" | 9 #include "base/file_path.h" |
10 #include "base/file_util.h" | 10 #include "base/file_util.h" |
(...skipping 2065 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2076 TEST_F(DirectoryBackingStoreTest, ModelTypeIds) { | 2076 TEST_F(DirectoryBackingStoreTest, ModelTypeIds) { |
2077 for (int i = FIRST_REAL_MODEL_TYPE; i < MODEL_TYPE_COUNT; ++i) { | 2077 for (int i = FIRST_REAL_MODEL_TYPE; i < MODEL_TYPE_COUNT; ++i) { |
2078 std::string model_id = | 2078 std::string model_id = |
2079 TestDirectoryBackingStore::ModelTypeEnumToModelId(ModelTypeFromInt(i)); | 2079 TestDirectoryBackingStore::ModelTypeEnumToModelId(ModelTypeFromInt(i)); |
2080 EXPECT_EQ(i, | 2080 EXPECT_EQ(i, |
2081 TestDirectoryBackingStore::ModelIdToModelTypeEnum(model_id.data(), | 2081 TestDirectoryBackingStore::ModelIdToModelTypeEnum(model_id.data(), |
2082 model_id.size())); | 2082 model_id.size())); |
2083 } | 2083 } |
2084 } | 2084 } |
2085 | 2085 |
2086 // TODO(109668): This had to be disabled because the latest API will | 2086 namespace { |
2087 // intentionally crash when a database is this badly corrupted. | 2087 |
2088 TEST_F(DirectoryBackingStoreTest, DISABLED_Corruption) { | 2088 class OnDiskDirectoryBackingStoreForTest : public OnDiskDirectoryBackingStore { |
| 2089 public: |
| 2090 OnDiskDirectoryBackingStoreForTest(const std::string& dir_name, |
| 2091 const FilePath& backing_filepath); |
| 2092 virtual ~OnDiskDirectoryBackingStoreForTest(); |
| 2093 bool DidFailFirstOpenAttempt(); |
| 2094 |
| 2095 protected: |
| 2096 virtual void ReportFirstTryOpenFailure() OVERRIDE; |
| 2097 |
| 2098 private: |
| 2099 bool first_open_failed_; |
| 2100 }; |
| 2101 |
| 2102 OnDiskDirectoryBackingStoreForTest::OnDiskDirectoryBackingStoreForTest( |
| 2103 const std::string& dir_name, |
| 2104 const FilePath& backing_filepath) : |
| 2105 OnDiskDirectoryBackingStore(dir_name, backing_filepath), |
| 2106 first_open_failed_(false) { } |
| 2107 |
| 2108 OnDiskDirectoryBackingStoreForTest::~OnDiskDirectoryBackingStoreForTest() { } |
| 2109 |
| 2110 void OnDiskDirectoryBackingStoreForTest::ReportFirstTryOpenFailure() { |
| 2111 // Do nothing, just like we would in release-mode. In debug mode, we DCHECK. |
| 2112 first_open_failed_ = true; |
| 2113 } |
| 2114 |
| 2115 bool OnDiskDirectoryBackingStoreForTest::DidFailFirstOpenAttempt() { |
| 2116 return first_open_failed_; |
| 2117 } |
| 2118 |
| 2119 } // namespace |
| 2120 |
| 2121 // This is a whitebox test intended to exercise the code path where the on-disk |
| 2122 // directory load code decides to delete the current directory and start fresh. |
| 2123 // |
| 2124 // This is considered "minor" corruption because the database recreation is |
| 2125 // expected to succeed. The alternative, where recreation does not succeed (ie. |
| 2126 // due to read-only file system), is not tested here. |
| 2127 TEST_F(DirectoryBackingStoreTest, MinorCorruption) { |
2089 { | 2128 { |
2090 scoped_ptr<OnDiskDirectoryBackingStore> dbs( | 2129 scoped_ptr<OnDiskDirectoryBackingStore> dbs( |
2091 new OnDiskDirectoryBackingStore(GetUsername(), GetDatabasePath())); | 2130 new OnDiskDirectoryBackingStore(GetUsername(), GetDatabasePath())); |
2092 EXPECT_TRUE(LoadAndIgnoreReturnedData(dbs.get())); | 2131 EXPECT_TRUE(LoadAndIgnoreReturnedData(dbs.get())); |
2093 } | 2132 } |
2094 std::string bad_data("BAD DATA"); | 2133 |
2095 EXPECT_TRUE(file_util::WriteFile(GetDatabasePath(), bad_data.data(), | 2134 // Corrupt the root node. |
2096 bad_data.size())); | |
2097 { | 2135 { |
2098 scoped_ptr<OnDiskDirectoryBackingStore> dbs( | 2136 sql::Connection connection; |
2099 new OnDiskDirectoryBackingStore(GetUsername(), GetDatabasePath())); | 2137 ASSERT_TRUE(connection.Open(GetDatabasePath())); |
| 2138 ASSERT_TRUE(connection.Execute( |
| 2139 "UPDATE metas SET parent_id='bogus' WHERE id = 'r';")); |
| 2140 } |
2100 | 2141 |
2101 EXPECT_FALSE(LoadAndIgnoreReturnedData(dbs.get())); | 2142 { |
| 2143 scoped_ptr<OnDiskDirectoryBackingStoreForTest> dbs( |
| 2144 new OnDiskDirectoryBackingStoreForTest(GetUsername(), |
| 2145 GetDatabasePath())); |
| 2146 |
| 2147 EXPECT_TRUE(LoadAndIgnoreReturnedData(dbs.get())); |
| 2148 EXPECT_TRUE(dbs->DidFailFirstOpenAttempt()); |
2102 } | 2149 } |
2103 } | 2150 } |
2104 | 2151 |
2105 TEST_F(DirectoryBackingStoreTest, DeleteEntries) { | 2152 TEST_F(DirectoryBackingStoreTest, DeleteEntries) { |
2106 sql::Connection connection; | 2153 sql::Connection connection; |
2107 ASSERT_TRUE(connection.OpenInMemory()); | 2154 ASSERT_TRUE(connection.OpenInMemory()); |
2108 | 2155 |
2109 SetUpCurrentDatabaseAndCheckVersion(&connection); | 2156 SetUpCurrentDatabaseAndCheckVersion(&connection); |
2110 scoped_ptr<TestDirectoryBackingStore> dbs( | 2157 scoped_ptr<TestDirectoryBackingStore> dbs( |
2111 new TestDirectoryBackingStore(GetUsername(), &connection)); | 2158 new TestDirectoryBackingStore(GetUsername(), &connection)); |
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2153 const std::string& guid2 = TestDirectoryBackingStore::GenerateCacheGUID(); | 2200 const std::string& guid2 = TestDirectoryBackingStore::GenerateCacheGUID(); |
2154 EXPECT_EQ(24U, guid1.size()); | 2201 EXPECT_EQ(24U, guid1.size()); |
2155 EXPECT_EQ(24U, guid2.size()); | 2202 EXPECT_EQ(24U, guid2.size()); |
2156 // In theory this test can fail, but it won't before the universe | 2203 // In theory this test can fail, but it won't before the universe |
2157 // dies of heat death. | 2204 // dies of heat death. |
2158 EXPECT_NE(guid1, guid2); | 2205 EXPECT_NE(guid1, guid2); |
2159 } | 2206 } |
2160 | 2207 |
2161 } // namespace syncable | 2208 } // namespace syncable |
2162 } // namespace syncer | 2209 } // namespace syncer |
OLD | NEW |