Battery Status API: add UMA logging for Linux.
[chromium-blink-merge.git] / content / browser / dom_storage / dom_storage_database_unittest.cc
blob2593878ce92736e5fa8ff73636d758961ba38a25
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/file_util.h"
8 #include "base/files/file_path.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"
16 #include "third_party/sqlite/sqlite3.h"
18 using base::ASCIIToUTF16;
20 namespace content {
22 void CreateV1Table(sql::Connection* db) {
23 ASSERT_TRUE(db->is_open());
24 ASSERT_TRUE(db->Execute("DROP TABLE IF EXISTS ItemTable"));
25 ASSERT_TRUE(db->Execute(
26 "CREATE TABLE ItemTable ("
27 "key TEXT UNIQUE ON CONFLICT REPLACE, "
28 "value TEXT NOT NULL ON CONFLICT FAIL)"));
31 void CreateV2Table(sql::Connection* db) {
32 ASSERT_TRUE(db->is_open());
33 ASSERT_TRUE(db->Execute("DROP TABLE IF EXISTS ItemTable"));
34 ASSERT_TRUE(db->Execute(
35 "CREATE TABLE ItemTable ("
36 "key TEXT UNIQUE ON CONFLICT REPLACE, "
37 "value BLOB NOT NULL ON CONFLICT FAIL)"));
40 void CreateInvalidKeyColumnTable(sql::Connection* db) {
41 // Create a table with the key type as FLOAT - this is "invalid"
42 // as far as the DOM Storage db is concerned.
43 ASSERT_TRUE(db->is_open());
44 ASSERT_TRUE(db->Execute("DROP TABLE IF EXISTS ItemTable"));
45 ASSERT_TRUE(db->Execute(
46 "CREATE TABLE IF NOT EXISTS ItemTable ("
47 "key FLOAT UNIQUE ON CONFLICT REPLACE, "
48 "value BLOB NOT NULL ON CONFLICT FAIL)"));
50 void CreateInvalidValueColumnTable(sql::Connection* db) {
51 // Create a table with the value type as FLOAT - this is "invalid"
52 // as far as the DOM Storage db is concerned.
53 ASSERT_TRUE(db->is_open());
54 ASSERT_TRUE(db->Execute("DROP TABLE IF EXISTS ItemTable"));
55 ASSERT_TRUE(db->Execute(
56 "CREATE TABLE IF NOT EXISTS ItemTable ("
57 "key TEXT UNIQUE ON CONFLICT REPLACE, "
58 "value FLOAT NOT NULL ON CONFLICT FAIL)"));
61 void InsertDataV1(sql::Connection* db,
62 const base::string16& key,
63 const base::string16& value) {
64 sql::Statement statement(db->GetCachedStatement(SQL_FROM_HERE,
65 "INSERT INTO ItemTable VALUES (?,?)"));
66 statement.BindString16(0, key);
67 statement.BindString16(1, value);
68 ASSERT_TRUE(statement.is_valid());
69 statement.Run();
72 void CheckValuesMatch(DOMStorageDatabase* db,
73 const DOMStorageValuesMap& expected) {
74 DOMStorageValuesMap values_read;
75 db->ReadAllValues(&values_read);
76 EXPECT_EQ(expected.size(), values_read.size());
78 DOMStorageValuesMap::const_iterator it = values_read.begin();
79 for (; it != values_read.end(); ++it) {
80 base::string16 key = it->first;
81 base::NullableString16 value = it->second;
82 base::NullableString16 expected_value = expected.find(key)->second;
83 EXPECT_EQ(expected_value.string(), value.string());
84 EXPECT_EQ(expected_value.is_null(), value.is_null());
88 void CreateMapWithValues(DOMStorageValuesMap* values) {
89 base::string16 kCannedKeys[] = {
90 ASCIIToUTF16("test"),
91 ASCIIToUTF16("company"),
92 ASCIIToUTF16("date"),
93 ASCIIToUTF16("empty")
95 base::NullableString16 kCannedValues[] = {
96 base::NullableString16(ASCIIToUTF16("123"), false),
97 base::NullableString16(ASCIIToUTF16("Google"), false),
98 base::NullableString16(ASCIIToUTF16("18-01-2012"), false),
99 base::NullableString16(base::string16(), false)
101 for (unsigned i = 0; i < sizeof(kCannedKeys) / sizeof(kCannedKeys[0]); i++)
102 (*values)[kCannedKeys[i]] = kCannedValues[i];
105 TEST(DOMStorageDatabaseTest, SimpleOpenAndClose) {
106 DOMStorageDatabase db;
107 EXPECT_FALSE(db.IsOpen());
108 ASSERT_TRUE(db.LazyOpen(true));
109 EXPECT_TRUE(db.IsOpen());
110 EXPECT_EQ(DOMStorageDatabase::V2, db.DetectSchemaVersion());
111 db.Close();
112 EXPECT_FALSE(db.IsOpen());
115 TEST(DOMStorageDatabaseTest, CloseEmptyDatabaseDeletesFile) {
116 base::ScopedTempDir temp_dir;
117 ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
118 base::FilePath file_name =
119 temp_dir.path().AppendASCII("TestDOMStorageDatabase.db");
120 DOMStorageValuesMap storage;
121 CreateMapWithValues(&storage);
123 // First test the case that explicitly clearing the database will
124 // trigger its deletion from disk.
126 DOMStorageDatabase db(file_name);
127 EXPECT_EQ(file_name, db.file_path());
128 ASSERT_TRUE(db.CommitChanges(false, storage));
130 EXPECT_TRUE(base::PathExists(file_name));
133 // Check that reading an existing db with data in it
134 // keeps the DB on disk on close.
135 DOMStorageDatabase db(file_name);
136 DOMStorageValuesMap values;
137 db.ReadAllValues(&values);
138 EXPECT_EQ(storage.size(), values.size());
141 EXPECT_TRUE(base::PathExists(file_name));
142 storage.clear();
145 DOMStorageDatabase db(file_name);
146 ASSERT_TRUE(db.CommitChanges(true, storage));
148 EXPECT_FALSE(base::PathExists(file_name));
150 // Now ensure that a series of updates and removals whose net effect
151 // is an empty database also triggers deletion.
152 CreateMapWithValues(&storage);
154 DOMStorageDatabase db(file_name);
155 ASSERT_TRUE(db.CommitChanges(false, storage));
158 EXPECT_TRUE(base::PathExists(file_name));
161 DOMStorageDatabase db(file_name);
162 ASSERT_TRUE(db.CommitChanges(false, storage));
163 DOMStorageValuesMap::iterator it = storage.begin();
164 for (; it != storage.end(); ++it)
165 it->second = base::NullableString16();
166 ASSERT_TRUE(db.CommitChanges(false, storage));
168 EXPECT_FALSE(base::PathExists(file_name));
171 TEST(DOMStorageDatabaseTest, TestLazyOpenIsLazy) {
172 // This test needs to operate with a file on disk to ensure that we will
173 // open a file that already exists when only invoking ReadAllValues.
174 base::ScopedTempDir temp_dir;
175 ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
176 base::FilePath file_name =
177 temp_dir.path().AppendASCII("TestDOMStorageDatabase.db");
179 DOMStorageDatabase db(file_name);
180 EXPECT_FALSE(db.IsOpen());
181 DOMStorageValuesMap values;
182 db.ReadAllValues(&values);
183 // Reading an empty db should not open the database.
184 EXPECT_FALSE(db.IsOpen());
186 values[ASCIIToUTF16("key")] =
187 base::NullableString16(ASCIIToUTF16("value"), false);
188 db.CommitChanges(false, values);
189 // Writing content should open the database.
190 EXPECT_TRUE(db.IsOpen());
192 db.Close();
193 ASSERT_FALSE(db.IsOpen());
195 // Reading from an existing database should open the database.
196 CheckValuesMatch(&db, values);
197 EXPECT_TRUE(db.IsOpen());
200 TEST(DOMStorageDatabaseTest, TestDetectSchemaVersion) {
201 DOMStorageDatabase db;
202 db.db_.reset(new sql::Connection());
203 ASSERT_TRUE(db.db_->OpenInMemory());
205 CreateInvalidValueColumnTable(db.db_.get());
206 EXPECT_EQ(DOMStorageDatabase::INVALID, db.DetectSchemaVersion());
208 CreateInvalidKeyColumnTable(db.db_.get());
209 EXPECT_EQ(DOMStorageDatabase::INVALID, db.DetectSchemaVersion());
211 CreateV1Table(db.db_.get());
212 EXPECT_EQ(DOMStorageDatabase::V1, db.DetectSchemaVersion());
214 CreateV2Table(db.db_.get());
215 EXPECT_EQ(DOMStorageDatabase::V2, db.DetectSchemaVersion());
218 TEST(DOMStorageDatabaseTest, TestLazyOpenUpgradesDatabase) {
219 // This test needs to operate with a file on disk so that we
220 // can create a table at version 1 and then close it again
221 // so that LazyOpen sees there is work to do (LazyOpen will return
222 // early if the database is already open).
223 base::ScopedTempDir temp_dir;
224 ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
225 base::FilePath file_name =
226 temp_dir.path().AppendASCII("TestDOMStorageDatabase.db");
228 DOMStorageDatabase db(file_name);
229 db.db_.reset(new sql::Connection());
230 ASSERT_TRUE(db.db_->Open(file_name));
231 CreateV1Table(db.db_.get());
232 db.Close();
234 EXPECT_TRUE(db.LazyOpen(true));
235 EXPECT_EQ(DOMStorageDatabase::V2, db.DetectSchemaVersion());
238 TEST(DOMStorageDatabaseTest, SimpleWriteAndReadBack) {
239 DOMStorageDatabase db;
241 DOMStorageValuesMap storage;
242 CreateMapWithValues(&storage);
244 EXPECT_TRUE(db.CommitChanges(false, storage));
245 CheckValuesMatch(&db, storage);
248 TEST(DOMStorageDatabaseTest, WriteWithClear) {
249 DOMStorageDatabase db;
251 DOMStorageValuesMap storage;
252 CreateMapWithValues(&storage);
254 ASSERT_TRUE(db.CommitChanges(false, storage));
255 CheckValuesMatch(&db, storage);
257 // Insert some values, clearing the database first.
258 storage.clear();
259 storage[ASCIIToUTF16("another_key")] =
260 base::NullableString16(ASCIIToUTF16("test"), false);
261 ASSERT_TRUE(db.CommitChanges(true, storage));
262 CheckValuesMatch(&db, storage);
264 // Now clear the values without inserting any new ones.
265 storage.clear();
266 ASSERT_TRUE(db.CommitChanges(true, storage));
267 CheckValuesMatch(&db, storage);
270 TEST(DOMStorageDatabaseTest, UpgradeFromV1ToV2WithData) {
271 const base::string16 kCannedKey = ASCIIToUTF16("foo");
272 const base::NullableString16 kCannedValue(ASCIIToUTF16("bar"), false);
273 DOMStorageValuesMap expected;
274 expected[kCannedKey] = kCannedValue;
276 DOMStorageDatabase db;
277 db.db_.reset(new sql::Connection());
278 ASSERT_TRUE(db.db_->OpenInMemory());
279 CreateV1Table(db.db_.get());
280 InsertDataV1(db.db_.get(), kCannedKey, kCannedValue.string());
282 ASSERT_TRUE(db.UpgradeVersion1To2());
284 EXPECT_EQ(DOMStorageDatabase::V2, db.DetectSchemaVersion());
286 CheckValuesMatch(&db, expected);
289 TEST(DOMStorageDatabaseTest, TestSimpleRemoveOneValue) {
290 DOMStorageDatabase db;
292 ASSERT_TRUE(db.LazyOpen(true));
293 const base::string16 kCannedKey = ASCIIToUTF16("test");
294 const base::NullableString16 kCannedValue(ASCIIToUTF16("data"), false);
295 DOMStorageValuesMap expected;
296 expected[kCannedKey] = kCannedValue;
298 // First write some data into the database.
299 ASSERT_TRUE(db.CommitChanges(false, expected));
300 CheckValuesMatch(&db, expected);
302 DOMStorageValuesMap values;
303 // A null string in the map should mean that that key gets
304 // removed.
305 values[kCannedKey] = base::NullableString16();
306 EXPECT_TRUE(db.CommitChanges(false, values));
308 expected.clear();
309 CheckValuesMatch(&db, expected);
312 TEST(DOMStorageDatabaseTest, TestCanOpenAndReadWebCoreDatabase) {
313 base::FilePath dir_test_data;
314 ASSERT_TRUE(PathService::Get(DIR_TEST_DATA, &dir_test_data));
315 base::FilePath webcore_database = dir_test_data.AppendASCII("dom_storage");
316 webcore_database =
317 webcore_database.AppendASCII("webcore_test_database.localstorage");
319 ASSERT_TRUE(base::PathExists(webcore_database));
321 DOMStorageDatabase db(webcore_database);
322 DOMStorageValuesMap values;
323 db.ReadAllValues(&values);
324 EXPECT_TRUE(db.IsOpen());
325 EXPECT_EQ(2u, values.size());
327 DOMStorageValuesMap::const_iterator it =
328 values.find(ASCIIToUTF16("value"));
329 EXPECT_TRUE(it != values.end());
330 EXPECT_EQ(ASCIIToUTF16("I am in local storage!"), it->second.string());
332 it = values.find(ASCIIToUTF16("timestamp"));
333 EXPECT_TRUE(it != values.end());
334 EXPECT_EQ(ASCIIToUTF16("1326738338841"), it->second.string());
336 it = values.find(ASCIIToUTF16("not_there"));
337 EXPECT_TRUE(it == values.end());
340 TEST(DOMStorageDatabaseTest, TestCanOpenFileThatIsNotADatabase) {
341 // Write into the temporary file first.
342 base::ScopedTempDir temp_dir;
343 ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
344 base::FilePath file_name =
345 temp_dir.path().AppendASCII("TestDOMStorageDatabase.db");
347 const char kData[] = "I am not a database.";
348 base::WriteFile(file_name, kData, strlen(kData));
351 sql::ScopedErrorIgnorer ignore_errors;
352 ignore_errors.IgnoreError(SQLITE_IOERR_SHORT_READ);
354 // Try and open the file. As it's not a database, we should end up deleting
355 // it and creating a new, valid file, so everything should actually
356 // succeed.
357 DOMStorageDatabase db(file_name);
358 DOMStorageValuesMap values;
359 CreateMapWithValues(&values);
360 EXPECT_TRUE(db.CommitChanges(true, values));
361 EXPECT_TRUE(db.CommitChanges(false, values));
362 EXPECT_TRUE(db.IsOpen());
364 CheckValuesMatch(&db, values);
366 ASSERT_TRUE(ignore_errors.CheckIgnoredErrors());
370 sql::ScopedErrorIgnorer ignore_errors;
371 ignore_errors.IgnoreError(SQLITE_CANTOPEN);
373 // Try to open a directory, we should fail gracefully and not attempt
374 // to delete it.
375 DOMStorageDatabase db(temp_dir.path());
376 DOMStorageValuesMap values;
377 CreateMapWithValues(&values);
378 EXPECT_FALSE(db.CommitChanges(true, values));
379 EXPECT_FALSE(db.CommitChanges(false, values));
380 EXPECT_FALSE(db.IsOpen());
382 values.clear();
384 db.ReadAllValues(&values);
385 EXPECT_EQ(0u, values.size());
386 EXPECT_FALSE(db.IsOpen());
388 EXPECT_TRUE(base::PathExists(temp_dir.path()));
390 ASSERT_TRUE(ignore_errors.CheckIgnoredErrors());
394 } // namespace content