1 // Copyright 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "content/browser/dom_storage/dom_storage_database.h"
7 #include "base/files/file_path.h"
8 #include "base/files/file_util.h"
9 #include "base/files/scoped_temp_dir.h"
10 #include "base/path_service.h"
11 #include "base/strings/utf_string_conversions.h"
12 #include "content/public/common/content_paths.h"
13 #include "sql/statement.h"
14 #include "sql/test/scoped_error_ignorer.h"
15 #include "testing/gtest/include/gtest/gtest.h"
17 using base::ASCIIToUTF16
;
21 void CreateV1Table(sql::Connection
* db
) {
22 ASSERT_TRUE(db
->is_open());
23 ASSERT_TRUE(db
->Execute("DROP TABLE IF EXISTS ItemTable"));
24 ASSERT_TRUE(db
->Execute(
25 "CREATE TABLE ItemTable ("
26 "key TEXT UNIQUE ON CONFLICT REPLACE, "
27 "value TEXT NOT NULL ON CONFLICT FAIL)"));
30 void CreateV2Table(sql::Connection
* db
) {
31 ASSERT_TRUE(db
->is_open());
32 ASSERT_TRUE(db
->Execute("DROP TABLE IF EXISTS ItemTable"));
33 ASSERT_TRUE(db
->Execute(
34 "CREATE TABLE ItemTable ("
35 "key TEXT UNIQUE ON CONFLICT REPLACE, "
36 "value BLOB NOT NULL ON CONFLICT FAIL)"));
39 void CreateInvalidKeyColumnTable(sql::Connection
* db
) {
40 // Create a table with the key type as FLOAT - this is "invalid"
41 // as far as the DOM Storage db is concerned.
42 ASSERT_TRUE(db
->is_open());
43 ASSERT_TRUE(db
->Execute("DROP TABLE IF EXISTS ItemTable"));
44 ASSERT_TRUE(db
->Execute(
45 "CREATE TABLE IF NOT EXISTS ItemTable ("
46 "key FLOAT UNIQUE ON CONFLICT REPLACE, "
47 "value BLOB NOT NULL ON CONFLICT FAIL)"));
49 void CreateInvalidValueColumnTable(sql::Connection
* db
) {
50 // Create a table with the value type as FLOAT - this is "invalid"
51 // as far as the DOM Storage db is concerned.
52 ASSERT_TRUE(db
->is_open());
53 ASSERT_TRUE(db
->Execute("DROP TABLE IF EXISTS ItemTable"));
54 ASSERT_TRUE(db
->Execute(
55 "CREATE TABLE IF NOT EXISTS ItemTable ("
56 "key TEXT UNIQUE ON CONFLICT REPLACE, "
57 "value FLOAT NOT NULL ON CONFLICT FAIL)"));
60 void InsertDataV1(sql::Connection
* db
,
61 const base::string16
& key
,
62 const base::string16
& value
) {
63 sql::Statement
statement(db
->GetCachedStatement(SQL_FROM_HERE
,
64 "INSERT INTO ItemTable VALUES (?,?)"));
65 statement
.BindString16(0, key
);
66 statement
.BindString16(1, value
);
67 ASSERT_TRUE(statement
.is_valid());
71 void CheckValuesMatch(DOMStorageDatabase
* db
,
72 const DOMStorageValuesMap
& expected
) {
73 DOMStorageValuesMap values_read
;
74 db
->ReadAllValues(&values_read
);
75 EXPECT_EQ(expected
.size(), values_read
.size());
77 DOMStorageValuesMap::const_iterator it
= values_read
.begin();
78 for (; it
!= values_read
.end(); ++it
) {
79 base::string16 key
= it
->first
;
80 base::NullableString16 value
= it
->second
;
81 base::NullableString16 expected_value
= expected
.find(key
)->second
;
82 EXPECT_EQ(expected_value
.string(), value
.string());
83 EXPECT_EQ(expected_value
.is_null(), value
.is_null());
87 void CreateMapWithValues(DOMStorageValuesMap
* values
) {
88 base::string16 kCannedKeys
[] = {
90 ASCIIToUTF16("company"),
94 base::NullableString16 kCannedValues
[] = {
95 base::NullableString16(ASCIIToUTF16("123"), false),
96 base::NullableString16(ASCIIToUTF16("Google"), false),
97 base::NullableString16(ASCIIToUTF16("18-01-2012"), false),
98 base::NullableString16(base::string16(), false)
100 for (unsigned i
= 0; i
< sizeof(kCannedKeys
) / sizeof(kCannedKeys
[0]); i
++)
101 (*values
)[kCannedKeys
[i
]] = kCannedValues
[i
];
104 TEST(DOMStorageDatabaseTest
, SimpleOpenAndClose
) {
105 DOMStorageDatabase db
;
106 EXPECT_FALSE(db
.IsOpen());
107 ASSERT_TRUE(db
.LazyOpen(true));
108 EXPECT_TRUE(db
.IsOpen());
109 EXPECT_EQ(DOMStorageDatabase::V2
, db
.DetectSchemaVersion());
111 EXPECT_FALSE(db
.IsOpen());
114 TEST(DOMStorageDatabaseTest
, CloseEmptyDatabaseDeletesFile
) {
115 base::ScopedTempDir temp_dir
;
116 ASSERT_TRUE(temp_dir
.CreateUniqueTempDir());
117 base::FilePath file_name
=
118 temp_dir
.path().AppendASCII("TestDOMStorageDatabase.db");
119 DOMStorageValuesMap storage
;
120 CreateMapWithValues(&storage
);
122 // First test the case that explicitly clearing the database will
123 // trigger its deletion from disk.
125 DOMStorageDatabase
db(file_name
);
126 EXPECT_EQ(file_name
, db
.file_path());
127 ASSERT_TRUE(db
.CommitChanges(false, storage
));
129 EXPECT_TRUE(base::PathExists(file_name
));
132 // Check that reading an existing db with data in it
133 // keeps the DB on disk on close.
134 DOMStorageDatabase
db(file_name
);
135 DOMStorageValuesMap values
;
136 db
.ReadAllValues(&values
);
137 EXPECT_EQ(storage
.size(), values
.size());
140 EXPECT_TRUE(base::PathExists(file_name
));
144 DOMStorageDatabase
db(file_name
);
145 ASSERT_TRUE(db
.CommitChanges(true, storage
));
147 EXPECT_FALSE(base::PathExists(file_name
));
149 // Now ensure that a series of updates and removals whose net effect
150 // is an empty database also triggers deletion.
151 CreateMapWithValues(&storage
);
153 DOMStorageDatabase
db(file_name
);
154 ASSERT_TRUE(db
.CommitChanges(false, storage
));
157 EXPECT_TRUE(base::PathExists(file_name
));
160 DOMStorageDatabase
db(file_name
);
161 ASSERT_TRUE(db
.CommitChanges(false, storage
));
162 DOMStorageValuesMap::iterator it
= storage
.begin();
163 for (; it
!= storage
.end(); ++it
)
164 it
->second
= base::NullableString16();
165 ASSERT_TRUE(db
.CommitChanges(false, storage
));
167 EXPECT_FALSE(base::PathExists(file_name
));
170 TEST(DOMStorageDatabaseTest
, TestLazyOpenIsLazy
) {
171 // This test needs to operate with a file on disk to ensure that we will
172 // open a file that already exists when only invoking ReadAllValues.
173 base::ScopedTempDir temp_dir
;
174 ASSERT_TRUE(temp_dir
.CreateUniqueTempDir());
175 base::FilePath file_name
=
176 temp_dir
.path().AppendASCII("TestDOMStorageDatabase.db");
178 DOMStorageDatabase
db(file_name
);
179 EXPECT_FALSE(db
.IsOpen());
180 DOMStorageValuesMap values
;
181 db
.ReadAllValues(&values
);
182 // Reading an empty db should not open the database.
183 EXPECT_FALSE(db
.IsOpen());
185 values
[ASCIIToUTF16("key")] =
186 base::NullableString16(ASCIIToUTF16("value"), false);
187 db
.CommitChanges(false, values
);
188 // Writing content should open the database.
189 EXPECT_TRUE(db
.IsOpen());
192 ASSERT_FALSE(db
.IsOpen());
194 // Reading from an existing database should open the database.
195 CheckValuesMatch(&db
, values
);
196 EXPECT_TRUE(db
.IsOpen());
199 TEST(DOMStorageDatabaseTest
, TestDetectSchemaVersion
) {
200 DOMStorageDatabase db
;
201 db
.db_
.reset(new sql::Connection());
202 ASSERT_TRUE(db
.db_
->OpenInMemory());
204 CreateInvalidValueColumnTable(db
.db_
.get());
205 EXPECT_EQ(DOMStorageDatabase::INVALID
, db
.DetectSchemaVersion());
207 CreateInvalidKeyColumnTable(db
.db_
.get());
208 EXPECT_EQ(DOMStorageDatabase::INVALID
, db
.DetectSchemaVersion());
210 CreateV1Table(db
.db_
.get());
211 EXPECT_EQ(DOMStorageDatabase::V1
, db
.DetectSchemaVersion());
213 CreateV2Table(db
.db_
.get());
214 EXPECT_EQ(DOMStorageDatabase::V2
, db
.DetectSchemaVersion());
217 TEST(DOMStorageDatabaseTest
, TestLazyOpenUpgradesDatabase
) {
218 // This test needs to operate with a file on disk so that we
219 // can create a table at version 1 and then close it again
220 // so that LazyOpen sees there is work to do (LazyOpen will return
221 // early if the database is already open).
222 base::ScopedTempDir temp_dir
;
223 ASSERT_TRUE(temp_dir
.CreateUniqueTempDir());
224 base::FilePath file_name
=
225 temp_dir
.path().AppendASCII("TestDOMStorageDatabase.db");
227 DOMStorageDatabase
db(file_name
);
228 db
.db_
.reset(new sql::Connection());
229 ASSERT_TRUE(db
.db_
->Open(file_name
));
230 CreateV1Table(db
.db_
.get());
233 EXPECT_TRUE(db
.LazyOpen(true));
234 EXPECT_EQ(DOMStorageDatabase::V2
, db
.DetectSchemaVersion());
237 TEST(DOMStorageDatabaseTest
, SimpleWriteAndReadBack
) {
238 DOMStorageDatabase db
;
240 DOMStorageValuesMap storage
;
241 CreateMapWithValues(&storage
);
243 EXPECT_TRUE(db
.CommitChanges(false, storage
));
244 CheckValuesMatch(&db
, storage
);
247 TEST(DOMStorageDatabaseTest
, WriteWithClear
) {
248 DOMStorageDatabase db
;
250 DOMStorageValuesMap storage
;
251 CreateMapWithValues(&storage
);
253 ASSERT_TRUE(db
.CommitChanges(false, storage
));
254 CheckValuesMatch(&db
, storage
);
256 // Insert some values, clearing the database first.
258 storage
[ASCIIToUTF16("another_key")] =
259 base::NullableString16(ASCIIToUTF16("test"), false);
260 ASSERT_TRUE(db
.CommitChanges(true, storage
));
261 CheckValuesMatch(&db
, storage
);
263 // Now clear the values without inserting any new ones.
265 ASSERT_TRUE(db
.CommitChanges(true, storage
));
266 CheckValuesMatch(&db
, storage
);
269 TEST(DOMStorageDatabaseTest
, UpgradeFromV1ToV2WithData
) {
270 const base::string16 kCannedKey
= ASCIIToUTF16("foo");
271 const base::NullableString16
kCannedValue(ASCIIToUTF16("bar"), false);
272 DOMStorageValuesMap expected
;
273 expected
[kCannedKey
] = kCannedValue
;
275 DOMStorageDatabase db
;
276 db
.db_
.reset(new sql::Connection());
277 ASSERT_TRUE(db
.db_
->OpenInMemory());
278 CreateV1Table(db
.db_
.get());
279 InsertDataV1(db
.db_
.get(), kCannedKey
, kCannedValue
.string());
281 ASSERT_TRUE(db
.UpgradeVersion1To2());
283 EXPECT_EQ(DOMStorageDatabase::V2
, db
.DetectSchemaVersion());
285 CheckValuesMatch(&db
, expected
);
288 TEST(DOMStorageDatabaseTest
, TestSimpleRemoveOneValue
) {
289 DOMStorageDatabase db
;
291 ASSERT_TRUE(db
.LazyOpen(true));
292 const base::string16 kCannedKey
= ASCIIToUTF16("test");
293 const base::NullableString16
kCannedValue(ASCIIToUTF16("data"), false);
294 DOMStorageValuesMap expected
;
295 expected
[kCannedKey
] = kCannedValue
;
297 // First write some data into the database.
298 ASSERT_TRUE(db
.CommitChanges(false, expected
));
299 CheckValuesMatch(&db
, expected
);
301 DOMStorageValuesMap values
;
302 // A null string in the map should mean that that key gets
304 values
[kCannedKey
] = base::NullableString16();
305 EXPECT_TRUE(db
.CommitChanges(false, values
));
308 CheckValuesMatch(&db
, expected
);
311 TEST(DOMStorageDatabaseTest
, TestCanOpenAndReadWebCoreDatabase
) {
312 base::FilePath dir_test_data
;
313 ASSERT_TRUE(PathService::Get(DIR_TEST_DATA
, &dir_test_data
));
314 base::FilePath webcore_database
= dir_test_data
.AppendASCII("dom_storage");
316 webcore_database
.AppendASCII("webcore_test_database.localstorage");
318 ASSERT_TRUE(base::PathExists(webcore_database
));
320 DOMStorageDatabase
db(webcore_database
);
321 DOMStorageValuesMap values
;
322 db
.ReadAllValues(&values
);
323 EXPECT_TRUE(db
.IsOpen());
324 EXPECT_EQ(2u, values
.size());
326 DOMStorageValuesMap::const_iterator it
=
327 values
.find(ASCIIToUTF16("value"));
328 EXPECT_TRUE(it
!= values
.end());
329 EXPECT_EQ(ASCIIToUTF16("I am in local storage!"), it
->second
.string());
331 it
= values
.find(ASCIIToUTF16("timestamp"));
332 EXPECT_TRUE(it
!= values
.end());
333 EXPECT_EQ(ASCIIToUTF16("1326738338841"), it
->second
.string());
335 it
= values
.find(ASCIIToUTF16("not_there"));
336 EXPECT_TRUE(it
== values
.end());
339 TEST(DOMStorageDatabaseTest
, TestCanOpenFileThatIsNotADatabase
) {
340 // Write into the temporary file first.
341 base::ScopedTempDir temp_dir
;
342 ASSERT_TRUE(temp_dir
.CreateUniqueTempDir());
343 base::FilePath file_name
=
344 temp_dir
.path().AppendASCII("TestDOMStorageDatabase.db");
346 const char kData
[] = "I am not a database.";
347 base::WriteFile(file_name
, kData
, strlen(kData
));
350 sql::ScopedErrorIgnorer ignore_errors
;
352 // Earlier versions of Chromium compiled against SQLite 3.6.7.3, which
353 // returned SQLITE_IOERR_SHORT_READ in this case. Some platforms may still
354 // compile against an earlier SQLite via USE_SYSTEM_SQLITE.
355 if (ignore_errors
.SQLiteLibVersionNumber() < 3008007) {
356 ignore_errors
.IgnoreError(SQLITE_IOERR_SHORT_READ
);
358 ignore_errors
.IgnoreError(SQLITE_NOTADB
);
361 // Try and open the file. As it's not a database, we should end up deleting
362 // it and creating a new, valid file, so everything should actually
364 DOMStorageDatabase
db(file_name
);
365 DOMStorageValuesMap values
;
366 CreateMapWithValues(&values
);
367 EXPECT_TRUE(db
.CommitChanges(true, values
));
368 EXPECT_TRUE(db
.CommitChanges(false, values
));
369 EXPECT_TRUE(db
.IsOpen());
371 CheckValuesMatch(&db
, values
);
373 ASSERT_TRUE(ignore_errors
.CheckIgnoredErrors());
377 sql::ScopedErrorIgnorer ignore_errors
;
378 ignore_errors
.IgnoreError(SQLITE_CANTOPEN
);
380 // Try to open a directory, we should fail gracefully and not attempt
382 DOMStorageDatabase
db(temp_dir
.path());
383 DOMStorageValuesMap values
;
384 CreateMapWithValues(&values
);
385 EXPECT_FALSE(db
.CommitChanges(true, values
));
386 EXPECT_FALSE(db
.CommitChanges(false, values
));
387 EXPECT_FALSE(db
.IsOpen());
391 db
.ReadAllValues(&values
);
392 EXPECT_EQ(0u, values
.size());
393 EXPECT_FALSE(db
.IsOpen());
395 EXPECT_TRUE(base::PathExists(temp_dir
.path()));
397 ASSERT_TRUE(ignore_errors
.CheckIgnoredErrors());
401 } // namespace content