| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 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 | 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 "content/browser/dom_storage/dom_storage_database.h" | 5 #include "content/browser/dom_storage/dom_storage_database.h" |
| 6 | 6 |
| 7 #include "base/files/file_path.h" | 7 #include "base/files/file_path.h" |
| 8 #include "base/files/file_util.h" | 8 #include "base/files/file_util.h" |
| 9 #include "base/files/scoped_temp_dir.h" | 9 #include "base/files/scoped_temp_dir.h" |
| 10 #include "base/path_service.h" | 10 #include "base/path_service.h" |
| 11 #include "base/strings/utf_string_conversions.h" | 11 #include "base/strings/utf_string_conversions.h" |
| 12 #include "content/public/common/content_paths.h" | 12 #include "content/public/common/content_paths.h" |
| 13 #include "sql/statement.h" | 13 #include "sql/statement.h" |
| 14 #include "sql/test/scoped_error_ignorer.h" | 14 #include "sql/test/scoped_error_expecter.h" |
| 15 #include "testing/gtest/include/gtest/gtest.h" | 15 #include "testing/gtest/include/gtest/gtest.h" |
| 16 | 16 |
| 17 using base::ASCIIToUTF16; | 17 using base::ASCIIToUTF16; |
| 18 | 18 |
| 19 namespace content { | 19 namespace content { |
| 20 | 20 |
| 21 void CreateV1Table(sql::Connection* db) { | 21 void CreateV1Table(sql::Connection* db) { |
| 22 ASSERT_TRUE(db->is_open()); | 22 ASSERT_TRUE(db->is_open()); |
| 23 ASSERT_TRUE(db->Execute("DROP TABLE IF EXISTS ItemTable")); | 23 ASSERT_TRUE(db->Execute("DROP TABLE IF EXISTS ItemTable")); |
| 24 ASSERT_TRUE(db->Execute( | 24 ASSERT_TRUE(db->Execute( |
| (...skipping 315 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 340 // Write into the temporary file first. | 340 // Write into the temporary file first. |
| 341 base::ScopedTempDir temp_dir; | 341 base::ScopedTempDir temp_dir; |
| 342 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); | 342 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); |
| 343 base::FilePath file_name = | 343 base::FilePath file_name = |
| 344 temp_dir.path().AppendASCII("TestDOMStorageDatabase.db"); | 344 temp_dir.path().AppendASCII("TestDOMStorageDatabase.db"); |
| 345 | 345 |
| 346 const char kData[] = "I am not a database."; | 346 const char kData[] = "I am not a database."; |
| 347 base::WriteFile(file_name, kData, strlen(kData)); | 347 base::WriteFile(file_name, kData, strlen(kData)); |
| 348 | 348 |
| 349 { | 349 { |
| 350 sql::ScopedErrorIgnorer ignore_errors; | 350 sql::test::ScopedErrorExpecter expecter; |
| 351 | 351 |
| 352 // Earlier versions of Chromium compiled against SQLite 3.6.7.3, which | 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 | 353 // returned SQLITE_IOERR_SHORT_READ in this case. Some platforms may still |
| 354 // compile against an earlier SQLite via USE_SYSTEM_SQLITE. | 354 // compile against an earlier SQLite via USE_SYSTEM_SQLITE. |
| 355 if (ignore_errors.SQLiteLibVersionNumber() < 3008005) { | 355 if (expecter.SQLiteLibVersionNumber() < 3008005) { |
| 356 ignore_errors.IgnoreError(SQLITE_IOERR_SHORT_READ); | 356 expecter.ExpectError(SQLITE_IOERR_SHORT_READ); |
| 357 } else { | 357 } else { |
| 358 ignore_errors.IgnoreError(SQLITE_NOTADB); | 358 expecter.ExpectError(SQLITE_NOTADB); |
| 359 } | 359 } |
| 360 | 360 |
| 361 // Try and open the file. As it's not a database, we should end up deleting | 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 | 362 // it and creating a new, valid file, so everything should actually |
| 363 // succeed. | 363 // succeed. |
| 364 DOMStorageDatabase db(file_name); | 364 DOMStorageDatabase db(file_name); |
| 365 DOMStorageValuesMap values; | 365 DOMStorageValuesMap values; |
| 366 CreateMapWithValues(&values); | 366 CreateMapWithValues(&values); |
| 367 EXPECT_TRUE(db.CommitChanges(true, values)); | 367 EXPECT_TRUE(db.CommitChanges(true, values)); |
| 368 EXPECT_TRUE(db.CommitChanges(false, values)); | 368 EXPECT_TRUE(db.CommitChanges(false, values)); |
| 369 EXPECT_TRUE(db.IsOpen()); | 369 EXPECT_TRUE(db.IsOpen()); |
| 370 | 370 |
| 371 CheckValuesMatch(&db, values); | 371 CheckValuesMatch(&db, values); |
| 372 | 372 |
| 373 ASSERT_TRUE(ignore_errors.CheckIgnoredErrors()); | 373 ASSERT_TRUE(expecter.SawExpectedErrors()); |
| 374 } | 374 } |
| 375 | 375 |
| 376 { | 376 { |
| 377 sql::ScopedErrorIgnorer ignore_errors; | 377 sql::test::ScopedErrorExpecter expecter; |
| 378 ignore_errors.IgnoreError(SQLITE_CANTOPEN); | 378 expecter.ExpectError(SQLITE_CANTOPEN); |
| 379 | 379 |
| 380 // Try to open a directory, we should fail gracefully and not attempt | 380 // Try to open a directory, we should fail gracefully and not attempt |
| 381 // to delete it. | 381 // to delete it. |
| 382 DOMStorageDatabase db(temp_dir.path()); | 382 DOMStorageDatabase db(temp_dir.path()); |
| 383 DOMStorageValuesMap values; | 383 DOMStorageValuesMap values; |
| 384 CreateMapWithValues(&values); | 384 CreateMapWithValues(&values); |
| 385 EXPECT_FALSE(db.CommitChanges(true, values)); | 385 EXPECT_FALSE(db.CommitChanges(true, values)); |
| 386 EXPECT_FALSE(db.CommitChanges(false, values)); | 386 EXPECT_FALSE(db.CommitChanges(false, values)); |
| 387 EXPECT_FALSE(db.IsOpen()); | 387 EXPECT_FALSE(db.IsOpen()); |
| 388 | 388 |
| 389 values.clear(); | 389 values.clear(); |
| 390 | 390 |
| 391 db.ReadAllValues(&values); | 391 db.ReadAllValues(&values); |
| 392 EXPECT_EQ(0u, values.size()); | 392 EXPECT_EQ(0u, values.size()); |
| 393 EXPECT_FALSE(db.IsOpen()); | 393 EXPECT_FALSE(db.IsOpen()); |
| 394 | 394 |
| 395 EXPECT_TRUE(base::PathExists(temp_dir.path())); | 395 EXPECT_TRUE(base::PathExists(temp_dir.path())); |
| 396 | 396 |
| 397 ASSERT_TRUE(ignore_errors.CheckIgnoredErrors()); | 397 ASSERT_TRUE(expecter.SawExpectedErrors()); |
| 398 } | 398 } |
| 399 } | 399 } |
| 400 | 400 |
| 401 } // namespace content | 401 } // namespace content |
| OLD | NEW |