Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(178)

Side by Side Diff: components/offline_pages/offline_page_metadata_store_impl_unittest.cc

Issue 2019333008: [Offline Pages] Fix crash due to table inconsistency. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: comments from fgorski@ Created 4 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2015 The Chromium Authors. All rights reserved. 1 // Copyright 2015 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 "components/offline_pages/offline_page_metadata_store.h" 5 #include "components/offline_pages/offline_page_metadata_store.h"
6 6
7 #include <stdint.h> 7 #include <stdint.h>
8 8
9 #include <memory> 9 #include <memory>
10 10
11 #include "base/bind.h" 11 #include "base/bind.h"
12 #include "base/files/file_path.h" 12 #include "base/files/file_path.h"
13 #include "base/files/scoped_temp_dir.h" 13 #include "base/files/scoped_temp_dir.h"
14 #include "base/strings/string_number_conversions.h" 14 #include "base/strings/string_number_conversions.h"
15 #include "base/strings/utf_string_conversions.h" 15 #include "base/strings/utf_string_conversions.h"
16 #include "base/test/test_simple_task_runner.h" 16 #include "base/test/test_simple_task_runner.h"
17 #include "base/threading/thread_task_runner_handle.h" 17 #include "base/threading/thread_task_runner_handle.h"
18 #include "components/offline_pages/offline_page_item.h" 18 #include "components/offline_pages/offline_page_item.h"
19 #include "components/offline_pages/offline_page_metadata_store_sql.h" 19 #include "components/offline_pages/offline_page_metadata_store_sql.h"
20 #include "components/offline_pages/offline_page_model.h" 20 #include "components/offline_pages/offline_page_model.h"
21 #include "sql/connection.h"
22 #include "sql/statement.h"
21 #include "testing/gtest/include/gtest/gtest.h" 23 #include "testing/gtest/include/gtest/gtest.h"
22 24
23 namespace offline_pages { 25 namespace offline_pages {
24 26
25 namespace { 27 namespace {
26 28
29 #define OFFLINE_PAGES_TABLE_V1 "offlinepages_v1"
fgorski 2016/06/03 20:04:17 wow. I didn't realize we are versioning the table
jianli 2016/06/03 21:12:18 We can certainly do this, but using define in our
romax 2016/06/03 23:11:39 yes they'll be in a near future plan.
30
27 const char kTestClientNamespace[] = "CLIENT_NAMESPACE"; 31 const char kTestClientNamespace[] = "CLIENT_NAMESPACE";
28 const char kTestURL[] = "https://example.com"; 32 const char kTestURL[] = "https://example.com";
29 const ClientId kTestClientId1(kTestClientNamespace, "1234"); 33 const ClientId kTestClientId1(kTestClientNamespace, "1234");
30 const ClientId kTestClientId2(kTestClientNamespace, "5678"); 34 const ClientId kTestClientId2(kTestClientNamespace, "5678");
31 const base::FilePath::CharType kFilePath[] = 35 const base::FilePath::CharType kFilePath[] =
32 FILE_PATH_LITERAL("/offline_pages/example_com.mhtml"); 36 FILE_PATH_LITERAL("/offline_pages/example_com.mhtml");
33 int64_t kFileSize = 234567; 37 int64_t kFileSize = 234567;
38 int64_t kOfflineId = 12345;
39
40 // Build a store with outdated schema to simulate the upgrading process.
41 // TODO(romax): move it to sql_unittests.
42 void BuildTestStoreV1(const base::FilePath& file) {
jianli 2016/06/03 21:12:18 BuildTestStoreV1 => BuildTestStoreWithOutdatedSche
romax 2016/06/03 23:11:39 Done.
43 sql::Connection connection;
44 ASSERT_TRUE(
45 connection.Open(file.Append(FILE_PATH_LITERAL("OfflinePages.db"))));
46 ASSERT_TRUE(connection.is_open());
47 ASSERT_TRUE(connection.BeginTransaction());
48 ASSERT_TRUE(connection.Execute("CREATE TABLE " OFFLINE_PAGES_TABLE_V1
49 "(offline_id INTEGER PRIMARY KEY NOT NULL, "
50 "creation_time INTEGER NOT NULL, "
51 "file_size INTEGER NOT NULL, "
52 "version INTEGER NOT NULL, "
53 "last_access_time INTEGER NOT NULL, "
54 "access_count INTEGER NOT NULL, "
55 "status INTEGER NOT NULL DEFAULT 0, "
56 "user_initiated INTEGER, "
57 "client_namespace VARCHAR NOT NULL, "
58 "client_id VARCHAR NOT NULL, "
59 "online_url VARCHAR NOT NULL, "
60 "offline_url VARCHAR NOT NULL DEFAULT '', "
61 "file_path VARCHAR NOT NULL "
62 ")"));
63 ASSERT_TRUE(connection.CommitTransaction());
64 sql::Statement statement(connection.GetUniqueStatement(
65 "INSERT INTO " OFFLINE_PAGES_TABLE_V1
66 "(offline_id, creation_time, file_size, version, "
67 "last_access_time, access_count, client_namespace, "
68 "client_id, online_url, file_path) "
69 "VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)"));
70 statement.BindInt64(0, kOfflineId);
71 statement.BindInt(1, 0);
72 statement.BindInt64(2, kFileSize);
73 statement.BindInt(3, 0);
74 statement.BindInt(4, 0);
75 statement.BindInt(5, 1);
76 statement.BindCString(6, kTestClientNamespace);
77 statement.BindString(7, kTestClientId1.id);
78 statement.BindCString(8, kTestURL);
79 base::FilePath path = base::FilePath(kFilePath);
80 #if defined(OS_POSIX)
81 std::string path_string = path.value();
82 #elif defined(OS_WIN)
83 std::string path_string = base::WideToUTF8(path.value());
84 #else
85 #error Unknown OS
86 #endif
87 statement.BindString(9, path_string);
jianli 2016/06/03 21:12:18 Doing "statement.BindString(9, kFilePath.MaybeAsAS
romax 2016/06/03 23:11:39 Done.
88 ASSERT_TRUE(statement.Run());
89 ASSERT_TRUE(connection.DoesTableExist(OFFLINE_PAGES_TABLE_V1));
90 ASSERT_FALSE(
91 connection.DoesColumnExist(OFFLINE_PAGES_TABLE_V1, "expiration_time"));
92 }
34 93
35 class OfflinePageMetadataStoreFactory { 94 class OfflinePageMetadataStoreFactory {
36 public: 95 public:
37 virtual OfflinePageMetadataStore* BuildStore(const base::FilePath& file) = 0; 96 virtual OfflinePageMetadataStore* BuildStore(const base::FilePath& file) = 0;
97 virtual OfflinePageMetadataStore* BuildStoreV1(
98 const base::FilePath& file) = 0;
38 }; 99 };
39 100
40 class OfflinePageMetadataStoreSQLFactory 101 class OfflinePageMetadataStoreSQLFactory
41 : public OfflinePageMetadataStoreFactory { 102 : public OfflinePageMetadataStoreFactory {
42 public: 103 public:
43 OfflinePageMetadataStore* BuildStore(const base::FilePath& file) override { 104 OfflinePageMetadataStore* BuildStore(const base::FilePath& file) override {
44 OfflinePageMetadataStoreSQL* store = new OfflinePageMetadataStoreSQL( 105 OfflinePageMetadataStoreSQL* store = new OfflinePageMetadataStoreSQL(
45 base::ThreadTaskRunnerHandle::Get(), file); 106 base::ThreadTaskRunnerHandle::Get(), file);
46 return store; 107 return store;
47 } 108 }
109
110 OfflinePageMetadataStore* BuildStoreV1(const base::FilePath& file) override {
111 BuildTestStoreV1(file);
112 OfflinePageMetadataStoreSQL* store = new OfflinePageMetadataStoreSQL(
113 base::ThreadTaskRunnerHandle::Get(), file);
114 return store;
115 }
48 }; 116 };
49 117
50 enum CalledCallback { NONE, LOAD, ADD, REMOVE, DESTROY }; 118 enum CalledCallback { NONE, LOAD, ADD, REMOVE, DESTROY };
51 enum Status { STATUS_NONE, STATUS_TRUE, STATUS_FALSE }; 119 enum Status { STATUS_NONE, STATUS_TRUE, STATUS_FALSE };
52 120
53 class OfflinePageMetadataStoreTestBase : public testing::Test { 121 class OfflinePageMetadataStoreTestBase : public testing::Test {
54 public: 122 public:
55 OfflinePageMetadataStoreTestBase(); 123 OfflinePageMetadataStoreTestBase();
56 ~OfflinePageMetadataStoreTestBase() override; 124 ~OfflinePageMetadataStoreTestBase() override;
57 125
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after
112 void OfflinePageMetadataStoreTestBase::ClearResults() { 180 void OfflinePageMetadataStoreTestBase::ClearResults() {
113 last_called_callback_ = NONE; 181 last_called_callback_ = NONE;
114 last_status_ = STATUS_NONE; 182 last_status_ = STATUS_NONE;
115 offline_pages_.clear(); 183 offline_pages_.clear();
116 } 184 }
117 185
118 template <typename T> 186 template <typename T>
119 class OfflinePageMetadataStoreTest : public OfflinePageMetadataStoreTestBase { 187 class OfflinePageMetadataStoreTest : public OfflinePageMetadataStoreTestBase {
120 public: 188 public:
121 std::unique_ptr<OfflinePageMetadataStore> BuildStore(); 189 std::unique_ptr<OfflinePageMetadataStore> BuildStore();
190 std::unique_ptr<OfflinePageMetadataStore> BuildStoreV1WithUpgrade();
jianli 2016/06/03 21:12:18 BuildStoreV1WithUpgrade => BuildStoreWithUpgradeFr
romax 2016/06/03 23:11:39 Done.
122 191
123 protected: 192 protected:
124 T factory_; 193 T factory_;
125 }; 194 };
126 195
127 template <typename T> 196 template <typename T>
128 std::unique_ptr<OfflinePageMetadataStore> 197 std::unique_ptr<OfflinePageMetadataStore>
129 OfflinePageMetadataStoreTest<T>::BuildStore() { 198 OfflinePageMetadataStoreTest<T>::BuildStore() {
130 std::unique_ptr<OfflinePageMetadataStore> store( 199 std::unique_ptr<OfflinePageMetadataStore> store(
131 factory_.BuildStore(temp_directory_.path())); 200 factory_.BuildStore(temp_directory_.path()));
132 store->Load(base::Bind(&OfflinePageMetadataStoreTestBase::LoadCallback, 201 store->Load(base::Bind(&OfflinePageMetadataStoreTestBase::LoadCallback,
133 base::Unretained(this))); 202 base::Unretained(this)));
134 PumpLoop(); 203 PumpLoop();
135 return store; 204 return store;
136 } 205 }
137 206
207 template <typename T>
208 std::unique_ptr<OfflinePageMetadataStore>
209 OfflinePageMetadataStoreTest<T>::BuildStoreV1WithUpgrade() {
210 std::unique_ptr<OfflinePageMetadataStore> store(
211 factory_.BuildStoreV1(temp_directory_.path()));
212 store->Load(base::Bind(&OfflinePageMetadataStoreTestBase::LoadCallback,
213 base::Unretained(this)));
214 PumpLoop();
215 return store;
216 }
217
138 typedef testing::Types<OfflinePageMetadataStoreSQLFactory> MyTypes; 218 typedef testing::Types<OfflinePageMetadataStoreSQLFactory> MyTypes;
139 TYPED_TEST_CASE(OfflinePageMetadataStoreTest, MyTypes); 219 TYPED_TEST_CASE(OfflinePageMetadataStoreTest, MyTypes);
140 220
141 // Loads empty store and makes sure that there are no offline pages stored in 221 // Loads empty store and makes sure that there are no offline pages stored in
142 // it. 222 // it.
143 TYPED_TEST(OfflinePageMetadataStoreTest, LoadEmptyStore) { 223 TYPED_TEST(OfflinePageMetadataStoreTest, LoadEmptyStore) {
144 std::unique_ptr<OfflinePageMetadataStore> store(this->BuildStore()); 224 std::unique_ptr<OfflinePageMetadataStore> store(this->BuildStore());
145 EXPECT_EQ(LOAD, this->last_called_callback_); 225 EXPECT_EQ(LOAD, this->last_called_callback_);
146 EXPECT_EQ(STATUS_TRUE, this->last_status_); 226 EXPECT_EQ(STATUS_TRUE, this->last_status_);
147 EXPECT_EQ(0U, this->offline_pages_.size()); 227 EXPECT_EQ(0U, this->offline_pages_.size());
148 } 228 }
149 229
230 // Loads a store which has an outdated schema.
231 // This test case would crash if it's not handling correctly when we're loading
232 // old version stores.
233 // TODO(romax): Move this to sql_unittest.
234 TYPED_TEST(OfflinePageMetadataStoreTest, LoadPreviousVersionStore) {
235 std::unique_ptr<OfflinePageMetadataStore> store(
236 this->BuildStoreV1WithUpgrade());
237 EXPECT_EQ(LOAD, this->last_called_callback_);
238 EXPECT_EQ(STATUS_TRUE, this->last_status_);
239 EXPECT_EQ(1U, this->offline_pages_.size());
240 OfflinePageItem offline_page(GURL(kTestURL), 1234LL, kTestClientId1,
241 base::FilePath(kFilePath), kFileSize);
242 store->AddOrUpdateOfflinePage(
jianli 2016/06/03 21:12:18 Please also set expiration_time before calling Add
romax 2016/06/03 23:11:39 Done.
243 offline_page,
244 base::Bind(&OfflinePageMetadataStoreTestBase::UpdateCallback,
245 base::Unretained(this), ADD));
246 this->PumpLoop();
247 EXPECT_EQ(ADD, this->last_called_callback_);
248 EXPECT_EQ(STATUS_TRUE, this->last_status_);
249 this->ClearResults();
250
251 // Close the store first to ensure file lock is removed.
252 store.reset();
253 store = this->BuildStore();
254 this->PumpLoop();
255
256 EXPECT_EQ(LOAD, this->last_called_callback_);
257 EXPECT_EQ(STATUS_TRUE, this->last_status_);
258 EXPECT_EQ(2U, this->offline_pages_.size());
jianli 2016/06/03 21:12:18 Change to use ASSERT_EQ. Please also add some bas
romax 2016/06/03 23:11:39 Done.
259 }
260
150 // Adds metadata of an offline page into a store and then opens the store 261 // Adds metadata of an offline page into a store and then opens the store
151 // again to make sure that stored metadata survives store restarts. 262 // again to make sure that stored metadata survives store restarts.
152 TYPED_TEST(OfflinePageMetadataStoreTest, AddOfflinePage) { 263 TYPED_TEST(OfflinePageMetadataStoreTest, AddOfflinePage) {
153 std::unique_ptr<OfflinePageMetadataStore> store(this->BuildStore()); 264 std::unique_ptr<OfflinePageMetadataStore> store(this->BuildStore());
154 OfflinePageItem offline_page(GURL(kTestURL), 1234LL, kTestClientId1, 265 OfflinePageItem offline_page(GURL(kTestURL), 1234LL, kTestClientId1,
155 base::FilePath(kFilePath), kFileSize); 266 base::FilePath(kFilePath), kFileSize);
156 store->AddOrUpdateOfflinePage( 267 store->AddOrUpdateOfflinePage(
157 offline_page, 268 offline_page,
158 base::Bind(&OfflinePageMetadataStoreTestBase::UpdateCallback, 269 base::Bind(&OfflinePageMetadataStoreTestBase::UpdateCallback,
159 base::Unretained(this), ADD)); 270 base::Unretained(this), ADD));
(...skipping 226 matching lines...) Expand 10 before | Expand all | Expand 10 after
386 EXPECT_EQ(offline_page.last_access_time, 497 EXPECT_EQ(offline_page.last_access_time,
387 this->offline_pages_[0].last_access_time); 498 this->offline_pages_[0].last_access_time);
388 EXPECT_EQ(offline_page.expiration_time, 499 EXPECT_EQ(offline_page.expiration_time,
389 this->offline_pages_[0].expiration_time); 500 this->offline_pages_[0].expiration_time);
390 EXPECT_EQ(offline_page.access_count, this->offline_pages_[0].access_count); 501 EXPECT_EQ(offline_page.access_count, this->offline_pages_[0].access_count);
391 EXPECT_EQ(offline_page.client_id, this->offline_pages_[0].client_id); 502 EXPECT_EQ(offline_page.client_id, this->offline_pages_[0].client_id);
392 } 503 }
393 504
394 } // namespace 505 } // namespace
395 } // namespace offline_pages 506 } // namespace offline_pages
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698