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 "base/bind.h" | 5 #include "base/bind.h" |
6 #include "base/file_util.h" | 6 #include "base/file_util.h" |
| 7 #include "base/files/scoped_file.h" |
7 #include "base/files/scoped_temp_dir.h" | 8 #include "base/files/scoped_temp_dir.h" |
8 #include "base/logging.h" | 9 #include "base/logging.h" |
9 #include "sql/connection.h" | 10 #include "sql/connection.h" |
10 #include "sql/meta_table.h" | 11 #include "sql/meta_table.h" |
11 #include "sql/statement.h" | 12 #include "sql/statement.h" |
12 #include "sql/test/error_callback_support.h" | 13 #include "sql/test/error_callback_support.h" |
13 #include "sql/test/scoped_error_ignorer.h" | 14 #include "sql/test/scoped_error_ignorer.h" |
14 #include "sql/test/test_helpers.h" | 15 #include "sql/test/test_helpers.h" |
15 #include "testing/gtest/include/gtest/gtest.h" | 16 #include "testing/gtest/include/gtest/gtest.h" |
16 #include "third_party/sqlite/sqlite3.h" | 17 #include "third_party/sqlite/sqlite3.h" |
(...skipping 399 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
416 } | 417 } |
417 | 418 |
418 // Verify that Raze() can handle an empty file. SQLite should treat | 419 // Verify that Raze() can handle an empty file. SQLite should treat |
419 // this as an empty database. | 420 // this as an empty database. |
420 TEST_F(SQLConnectionTest, RazeEmptyDB) { | 421 TEST_F(SQLConnectionTest, RazeEmptyDB) { |
421 const char* kCreateSql = "CREATE TABLE foo (id INTEGER PRIMARY KEY, value)"; | 422 const char* kCreateSql = "CREATE TABLE foo (id INTEGER PRIMARY KEY, value)"; |
422 ASSERT_TRUE(db().Execute(kCreateSql)); | 423 ASSERT_TRUE(db().Execute(kCreateSql)); |
423 db().Close(); | 424 db().Close(); |
424 | 425 |
425 { | 426 { |
426 file_util::ScopedFILE file(base::OpenFile(db_path(), "rb+")); | 427 base::ScopedFILE file(base::OpenFile(db_path(), "rb+")); |
427 ASSERT_TRUE(file.get() != NULL); | 428 ASSERT_TRUE(file.get() != NULL); |
428 ASSERT_EQ(0, fseek(file.get(), 0, SEEK_SET)); | 429 ASSERT_EQ(0, fseek(file.get(), 0, SEEK_SET)); |
429 ASSERT_TRUE(base::TruncateFile(file.get())); | 430 ASSERT_TRUE(base::TruncateFile(file.get())); |
430 } | 431 } |
431 | 432 |
432 ASSERT_TRUE(db().Open(db_path())); | 433 ASSERT_TRUE(db().Open(db_path())); |
433 ASSERT_TRUE(db().Raze()); | 434 ASSERT_TRUE(db().Raze()); |
434 EXPECT_EQ(0, SqliteMasterCount(&db())); | 435 EXPECT_EQ(0, SqliteMasterCount(&db())); |
435 } | 436 } |
436 | 437 |
437 // Verify that Raze() can handle a file of junk. | 438 // Verify that Raze() can handle a file of junk. |
438 TEST_F(SQLConnectionTest, RazeNOTADB) { | 439 TEST_F(SQLConnectionTest, RazeNOTADB) { |
439 db().Close(); | 440 db().Close(); |
440 sql::Connection::Delete(db_path()); | 441 sql::Connection::Delete(db_path()); |
441 ASSERT_FALSE(base::PathExists(db_path())); | 442 ASSERT_FALSE(base::PathExists(db_path())); |
442 | 443 |
443 { | 444 { |
444 file_util::ScopedFILE file(base::OpenFile(db_path(), "wb")); | 445 base::ScopedFILE file(base::OpenFile(db_path(), "wb")); |
445 ASSERT_TRUE(file.get() != NULL); | 446 ASSERT_TRUE(file.get() != NULL); |
446 | 447 |
447 const char* kJunk = "This is the hour of our discontent."; | 448 const char* kJunk = "This is the hour of our discontent."; |
448 fputs(kJunk, file.get()); | 449 fputs(kJunk, file.get()); |
449 } | 450 } |
450 ASSERT_TRUE(base::PathExists(db_path())); | 451 ASSERT_TRUE(base::PathExists(db_path())); |
451 | 452 |
452 // SQLite will successfully open the handle, but will fail with | 453 // SQLite will successfully open the handle, but will fail with |
453 // SQLITE_IOERR_SHORT_READ on pragma statemenets which read the | 454 // SQLITE_IOERR_SHORT_READ on pragma statemenets which read the |
454 // header. | 455 // header. |
(...skipping 12 matching lines...) Expand all Loading... |
467 } | 468 } |
468 | 469 |
469 // Verify that Raze() can handle a database overwritten with garbage. | 470 // Verify that Raze() can handle a database overwritten with garbage. |
470 TEST_F(SQLConnectionTest, RazeNOTADB2) { | 471 TEST_F(SQLConnectionTest, RazeNOTADB2) { |
471 const char* kCreateSql = "CREATE TABLE foo (id INTEGER PRIMARY KEY, value)"; | 472 const char* kCreateSql = "CREATE TABLE foo (id INTEGER PRIMARY KEY, value)"; |
472 ASSERT_TRUE(db().Execute(kCreateSql)); | 473 ASSERT_TRUE(db().Execute(kCreateSql)); |
473 ASSERT_EQ(1, SqliteMasterCount(&db())); | 474 ASSERT_EQ(1, SqliteMasterCount(&db())); |
474 db().Close(); | 475 db().Close(); |
475 | 476 |
476 { | 477 { |
477 file_util::ScopedFILE file(base::OpenFile(db_path(), "rb+")); | 478 base::ScopedFILE file(base::OpenFile(db_path(), "rb+")); |
478 ASSERT_TRUE(file.get() != NULL); | 479 ASSERT_TRUE(file.get() != NULL); |
479 ASSERT_EQ(0, fseek(file.get(), 0, SEEK_SET)); | 480 ASSERT_EQ(0, fseek(file.get(), 0, SEEK_SET)); |
480 | 481 |
481 const char* kJunk = "This is the hour of our discontent."; | 482 const char* kJunk = "This is the hour of our discontent."; |
482 fputs(kJunk, file.get()); | 483 fputs(kJunk, file.get()); |
483 } | 484 } |
484 | 485 |
485 // SQLite will successfully open the handle, but will fail with | 486 // SQLite will successfully open the handle, but will fail with |
486 // SQLITE_NOTADB on pragma statemenets which attempt to read the | 487 // SQLITE_NOTADB on pragma statemenets which attempt to read the |
487 // corrupted header. | 488 // corrupted header. |
(...skipping 372 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
860 EXPECT_LT(1u, messages.size()); | 861 EXPECT_LT(1u, messages.size()); |
861 EXPECT_NE(kOk, messages[0]); | 862 EXPECT_NE(kOk, messages[0]); |
862 ASSERT_TRUE(ignore_errors.CheckIgnoredErrors()); | 863 ASSERT_TRUE(ignore_errors.CheckIgnoredErrors()); |
863 } | 864 } |
864 | 865 |
865 // TODO(shess): CorruptTableOrIndex could be used to produce a | 866 // TODO(shess): CorruptTableOrIndex could be used to produce a |
866 // file that would pass the quick check and fail the full check. | 867 // file that would pass the quick check and fail the full check. |
867 } | 868 } |
868 | 869 |
869 } // namespace | 870 } // namespace |
OLD | NEW |