| OLD | NEW |
| 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 "chrome/browser/safe_browsing/safe_browsing_store_file.h" | 5 #include "chrome/browser/safe_browsing/safe_browsing_store_file.h" |
| 6 | 6 |
| 7 #include "base/callback.h" | 7 #include "base/callback.h" |
| 8 #include "base/scoped_temp_dir.h" |
| 8 #include "chrome/browser/safe_browsing/safe_browsing_store_unittest_helper.h" | 9 #include "chrome/browser/safe_browsing/safe_browsing_store_unittest_helper.h" |
| 9 #include "chrome/test/file_test_utils.h" | |
| 10 #include "testing/gtest/include/gtest/gtest.h" | 10 #include "testing/gtest/include/gtest/gtest.h" |
| 11 #include "testing/platform_test.h" | 11 #include "testing/platform_test.h" |
| 12 | 12 |
| 13 namespace { | 13 namespace { |
| 14 | 14 |
| 15 const FilePath::CharType kFolderPrefix[] = | 15 const FilePath::CharType kFolderPrefix[] = |
| 16 FILE_PATH_LITERAL("SafeBrowsingTestStoreFile"); | 16 FILE_PATH_LITERAL("SafeBrowsingTestStoreFile"); |
| 17 | 17 |
| 18 class SafeBrowsingStoreFileTest : public PlatformTest { | 18 class SafeBrowsingStoreFileTest : public PlatformTest { |
| 19 public: | 19 public: |
| 20 virtual void SetUp() { | 20 virtual void SetUp() { |
| 21 PlatformTest::SetUp(); | 21 PlatformTest::SetUp(); |
| 22 | 22 |
| 23 FilePath temp_dir; | 23 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); |
| 24 ASSERT_TRUE(file_util::CreateNewTempDirectory(kFolderPrefix, &temp_dir)); | |
| 25 | 24 |
| 26 file_deleter_.reset(new FileAutoDeleter(temp_dir)); | 25 filename_ = temp_dir_.path(); |
| 27 | |
| 28 filename_ = temp_dir; | |
| 29 filename_ = filename_.AppendASCII("SafeBrowsingTestStore"); | 26 filename_ = filename_.AppendASCII("SafeBrowsingTestStore"); |
| 30 file_util::Delete(filename_, false); | |
| 31 | |
| 32 // Make sure an old temporary file isn't hanging around. | |
| 33 const FilePath temp_file = | |
| 34 SafeBrowsingStoreFile::TemporaryFileForFilename(filename_); | |
| 35 file_util::Delete(temp_file, false); | |
| 36 | 27 |
| 37 store_.reset(new SafeBrowsingStoreFile()); | 28 store_.reset(new SafeBrowsingStoreFile()); |
| 38 store_->Init(filename_, NULL); | 29 store_->Init(filename_, NULL); |
| 39 } | 30 } |
| 40 virtual void TearDown() { | 31 virtual void TearDown() { |
| 41 if (store_.get()) | 32 if (store_.get()) |
| 42 store_->Delete(); | 33 store_->Delete(); |
| 43 store_.reset(); | 34 store_.reset(); |
| 44 file_deleter_.reset(); | |
| 45 | 35 |
| 46 PlatformTest::TearDown(); | 36 PlatformTest::TearDown(); |
| 47 } | 37 } |
| 48 | 38 |
| 49 void OnCorruptionDetected() { | 39 void OnCorruptionDetected() { |
| 50 corruption_detected_ = true; | 40 corruption_detected_ = true; |
| 51 } | 41 } |
| 52 | 42 |
| 53 scoped_ptr<FileAutoDeleter> file_deleter_; | 43 ScopedTempDir temp_dir_; |
| 54 FilePath filename_; | 44 FilePath filename_; |
| 55 scoped_ptr<SafeBrowsingStoreFile> store_; | 45 scoped_ptr<SafeBrowsingStoreFile> store_; |
| 56 bool corruption_detected_; | 46 bool corruption_detected_; |
| 57 }; | 47 }; |
| 58 | 48 |
| 59 TEST_STORE(SafeBrowsingStoreFileTest, store_.get(), filename_); | 49 TEST_STORE(SafeBrowsingStoreFileTest, store_.get(), filename_); |
| 60 | 50 |
| 61 // Test that Delete() deletes the temporary store, if present. | 51 // Test that Delete() deletes the temporary store, if present. |
| 62 TEST_F(SafeBrowsingStoreFileTest, DeleteTemp) { | 52 TEST_F(SafeBrowsingStoreFileTest, DeleteTemp) { |
| 63 const FilePath temp_file = | 53 const FilePath temp_file = |
| 64 SafeBrowsingStoreFile::TemporaryFileForFilename(filename_); | 54 SafeBrowsingStoreFile::TemporaryFileForFilename(filename_); |
| 65 | 55 |
| 66 EXPECT_FALSE(file_util::PathExists(filename_)); | 56 EXPECT_FALSE(file_util::PathExists(filename_)); |
| 67 EXPECT_FALSE(file_util::PathExists(temp_file)); | 57 EXPECT_FALSE(file_util::PathExists(temp_file)); |
| 68 | 58 |
| 69 // Starting a transaction creates a temporary file. | 59 // Starting a transaction creates a temporary file. |
| 70 EXPECT_TRUE(store_->BeginUpdate()); | 60 EXPECT_TRUE(store_->BeginUpdate()); |
| 71 EXPECT_TRUE(file_util::PathExists(temp_file)); | 61 EXPECT_TRUE(file_util::PathExists(temp_file)); |
| 72 | 62 |
| 73 // Pull the rug out from under the existing store, simulating a | 63 // Pull the rug out from under the existing store, simulating a |
| 74 // crash. | 64 // crash. |
| (...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 139 EXPECT_EQ(fwrite(&kLargeCount, sizeof(kLargeCount), 1, file.get()), 1U); | 129 EXPECT_EQ(fwrite(&kLargeCount, sizeof(kLargeCount), 1, file.get()), 1U); |
| 140 file.reset(); | 130 file.reset(); |
| 141 | 131 |
| 142 // Detects corruption and fails to even begin the update. | 132 // Detects corruption and fails to even begin the update. |
| 143 corruption_detected_ = false; | 133 corruption_detected_ = false; |
| 144 EXPECT_FALSE(test_store.BeginUpdate()); | 134 EXPECT_FALSE(test_store.BeginUpdate()); |
| 145 EXPECT_TRUE(corruption_detected_); | 135 EXPECT_TRUE(corruption_detected_); |
| 146 } | 136 } |
| 147 | 137 |
| 148 } // namespace | 138 } // namespace |
| OLD | NEW |