| 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 "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/bind.h" |    7 #include "base/bind.h" | 
|    8 #include "base/files/file_util.h" |    8 #include "base/files/file_util.h" | 
|    9 #include "base/files/scoped_file.h" |    9 #include "base/files/scoped_file.h" | 
|   10 #include "base/files/scoped_temp_dir.h" |   10 #include "base/files/scoped_temp_dir.h" | 
|   11 #include "base/md5.h" |   11 #include "base/md5.h" | 
|   12 #include "base/path_service.h" |   12 #include "base/path_service.h" | 
 |   13 #include "base/test/test_simple_task_runner.h" | 
|   13 #include "chrome/common/chrome_paths.h" |   14 #include "chrome/common/chrome_paths.h" | 
|   14 #include "testing/gtest/include/gtest/gtest.h" |   15 #include "testing/gtest/include/gtest/gtest.h" | 
|   15 #include "testing/platform_test.h" |   16 #include "testing/platform_test.h" | 
|   16  |   17  | 
|   17 namespace { |   18 namespace { | 
|   18  |   19  | 
|   19 const int kAddChunk1 = 1; |   20 const int kAddChunk1 = 1; | 
|   20 const int kAddChunk2 = 3; |   21 const int kAddChunk2 = 3; | 
|   21 const int kAddChunk3 = 5; |   22 const int kAddChunk3 = 5; | 
|   22 const int kAddChunk4 = 7; |   23 const int kAddChunk4 = 7; | 
| (...skipping 10 matching lines...) Expand all  Loading... | 
|   33  |   34  | 
|   34 const SBPrefix kMinSBPrefix = 0u; |   35 const SBPrefix kMinSBPrefix = 0u; | 
|   35 const SBPrefix kMaxSBPrefix = ~kMinSBPrefix; |   36 const SBPrefix kMaxSBPrefix = ~kMinSBPrefix; | 
|   36  |   37  | 
|   37 }  // namespace |   38 }  // namespace | 
|   38  |   39  | 
|   39 namespace safe_browsing { |   40 namespace safe_browsing { | 
|   40  |   41  | 
|   41 class SafeBrowsingStoreFileTest : public PlatformTest { |   42 class SafeBrowsingStoreFileTest : public PlatformTest { | 
|   42  public: |   43  public: | 
 |   44   SafeBrowsingStoreFileTest() | 
 |   45       : task_runner_(new base::TestSimpleTaskRunner), | 
 |   46         corruption_detected_(false) {} | 
 |   47  | 
|   43   void SetUp() override { |   48   void SetUp() override { | 
|   44     PlatformTest::SetUp(); |   49     PlatformTest::SetUp(); | 
|   45  |   50  | 
|   46     ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); |   51     ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); | 
|   47  |   52  | 
|   48     filename_ = temp_dir_.path(); |   53     filename_ = temp_dir_.path(); | 
|   49     filename_ = filename_.AppendASCII("SafeBrowsingTestStore"); |   54     filename_ = filename_.AppendASCII("SafeBrowsingTestStore"); | 
|   50  |   55  | 
|   51     store_.reset(new SafeBrowsingStoreFile()); |   56     store_.reset(new SafeBrowsingStoreFile(task_runner_)); | 
|   52     store_->Init(filename_, |   57     store_->Init(filename_, | 
|   53                  base::Bind(&SafeBrowsingStoreFileTest::OnCorruptionDetected, |   58                  base::Bind(&SafeBrowsingStoreFileTest::OnCorruptionDetected, | 
|   54                             base::Unretained(this))); |   59                             base::Unretained(this))); | 
|   55     corruption_detected_ = false; |  | 
|   56   } |   60   } | 
|   57   void TearDown() override { |   61   void TearDown() override { | 
|   58     if (store_.get()) |   62     if (store_.get()) | 
|   59       store_->Delete(); |   63       store_->Delete(); | 
|   60     store_.reset(); |   64     store_.reset(); | 
|   61  |   65  | 
|   62     PlatformTest::TearDown(); |   66     PlatformTest::TearDown(); | 
|   63   } |   67   } | 
|   64  |   68  | 
|   65   void OnCorruptionDetected() { |   69   void OnCorruptionDetected() { | 
| (...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after  Loading... | 
|  105   // Manually read the shard stride info from the file. |  109   // Manually read the shard stride info from the file. | 
|  106   uint32 ReadStride() { |  110   uint32 ReadStride() { | 
|  107     base::ScopedFILE file(base::OpenFile(filename_, "rb")); |  111     base::ScopedFILE file(base::OpenFile(filename_, "rb")); | 
|  108     const long kOffset = 4 * sizeof(uint32); |  112     const long kOffset = 4 * sizeof(uint32); | 
|  109     EXPECT_EQ(fseek(file.get(), kOffset, SEEK_SET), 0); |  113     EXPECT_EQ(fseek(file.get(), kOffset, SEEK_SET), 0); | 
|  110     uint32 shard_stride = 0; |  114     uint32 shard_stride = 0; | 
|  111     EXPECT_EQ(fread(&shard_stride, sizeof(shard_stride), 1, file.get()), 1U); |  115     EXPECT_EQ(fread(&shard_stride, sizeof(shard_stride), 1, file.get()), 1U); | 
|  112     return shard_stride; |  116     return shard_stride; | 
|  113   } |  117   } | 
|  114  |  118  | 
 |  119   scoped_refptr<base::TestSimpleTaskRunner> task_runner_; | 
|  115   base::ScopedTempDir temp_dir_; |  120   base::ScopedTempDir temp_dir_; | 
|  116   base::FilePath filename_; |  121   base::FilePath filename_; | 
|  117   scoped_ptr<SafeBrowsingStoreFile> store_; |  122   scoped_ptr<SafeBrowsingStoreFile> store_; | 
|  118   bool corruption_detected_; |  123   bool corruption_detected_; | 
|  119 }; |  124 }; | 
|  120  |  125  | 
|  121 // Test that the empty store looks empty. |  126 // Test that the empty store looks empty. | 
|  122 TEST_F(SafeBrowsingStoreFileTest, Empty) { |  127 TEST_F(SafeBrowsingStoreFileTest, Empty) { | 
|  123   ASSERT_TRUE(store_->BeginUpdate()); |  128   ASSERT_TRUE(store_->BeginUpdate()); | 
|  124  |  129  | 
| (...skipping 329 matching lines...) Expand 10 before | Expand all | Expand 10 after  Loading... | 
|  454  |  459  | 
|  455   EXPECT_FALSE(base::PathExists(filename_)); |  460   EXPECT_FALSE(base::PathExists(filename_)); | 
|  456   EXPECT_FALSE(base::PathExists(temp_file)); |  461   EXPECT_FALSE(base::PathExists(temp_file)); | 
|  457  |  462  | 
|  458   // Starting a transaction creates a temporary file. |  463   // Starting a transaction creates a temporary file. | 
|  459   ASSERT_TRUE(store_->BeginUpdate()); |  464   ASSERT_TRUE(store_->BeginUpdate()); | 
|  460   EXPECT_TRUE(base::PathExists(temp_file)); |  465   EXPECT_TRUE(base::PathExists(temp_file)); | 
|  461  |  466  | 
|  462   // Pull the rug out from under the existing store, simulating a |  467   // Pull the rug out from under the existing store, simulating a | 
|  463   // crash. |  468   // crash. | 
|  464   store_.reset(new SafeBrowsingStoreFile()); |  469   store_.reset(new SafeBrowsingStoreFile(task_runner_)); | 
|  465   store_->Init(filename_, base::Closure()); |  470   store_->Init(filename_, base::Closure()); | 
|  466   EXPECT_FALSE(base::PathExists(filename_)); |  471   EXPECT_FALSE(base::PathExists(filename_)); | 
|  467   EXPECT_TRUE(base::PathExists(temp_file)); |  472   EXPECT_TRUE(base::PathExists(temp_file)); | 
|  468  |  473  | 
|  469   // Make sure the temporary file is deleted. |  474   // Make sure the temporary file is deleted. | 
|  470   EXPECT_TRUE(store_->Delete()); |  475   EXPECT_TRUE(store_->Delete()); | 
|  471   EXPECT_FALSE(base::PathExists(filename_)); |  476   EXPECT_FALSE(base::PathExists(filename_)); | 
|  472   EXPECT_FALSE(base::PathExists(temp_file)); |  477   EXPECT_FALSE(base::PathExists(temp_file)); | 
|  473 } |  478 } | 
|  474  |  479  | 
| (...skipping 266 matching lines...) Expand 10 before | Expand all | Expand 10 after  Loading... | 
|  741   // - Add chunk kAddChunk1 containing kHash1.prefix and kHash2. |  746   // - Add chunk kAddChunk1 containing kHash1.prefix and kHash2. | 
|  742   // - Sub chunk kSubChunk1 containing kHash3. |  747   // - Sub chunk kSubChunk1 containing kHash3. | 
|  743   const char kBasename[] = "FileStoreVersion7"; |  748   const char kBasename[] = "FileStoreVersion7"; | 
|  744   base::FilePath golden_path; |  749   base::FilePath golden_path; | 
|  745   ASSERT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &golden_path)); |  750   ASSERT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &golden_path)); | 
|  746   golden_path = golden_path.AppendASCII("SafeBrowsing"); |  751   golden_path = golden_path.AppendASCII("SafeBrowsing"); | 
|  747   golden_path = golden_path.AppendASCII(kBasename); |  752   golden_path = golden_path.AppendASCII(kBasename); | 
|  748   ASSERT_TRUE(base::CopyFile(golden_path, filename_)); |  753   ASSERT_TRUE(base::CopyFile(golden_path, filename_)); | 
|  749  |  754  | 
|  750   // Reset the store to make sure it re-reads the file. |  755   // Reset the store to make sure it re-reads the file. | 
|  751   store_.reset(new SafeBrowsingStoreFile()); |  756   ASSERT_TRUE(!task_runner_->HasPendingTask()); | 
 |  757   store_.reset(new SafeBrowsingStoreFile(task_runner_)); | 
|  752   store_->Init(filename_, |  758   store_->Init(filename_, | 
|  753                base::Bind(&SafeBrowsingStoreFileTest::OnCorruptionDetected, |  759                base::Bind(&SafeBrowsingStoreFileTest::OnCorruptionDetected, | 
|  754                           base::Unretained(this))); |  760                           base::Unretained(this))); | 
|  755   EXPECT_FALSE(corruption_detected_); |  761   EXPECT_FALSE(corruption_detected_); | 
|  756  |  762  | 
|  757   // The unknown version should be encountered on the first read. |  763   // The unknown version should be encountered on the first read. | 
|  758   EXPECT_FALSE(store_->BeginUpdate()); |  764   EXPECT_FALSE(store_->BeginUpdate()); | 
|  759   EXPECT_TRUE(corruption_detected_); |  765   EXPECT_TRUE(corruption_detected_); | 
|  760  |  766  | 
|  761   // No more to test because corrupt file stores are cleaned up by the database |  767   // No more to test because corrupt file stores are cleaned up by the database | 
| (...skipping 12 matching lines...) Expand all  Loading... | 
|  774   // - Add chunk kAddChunk1 containing kHash1.prefix and kHash2. |  780   // - Add chunk kAddChunk1 containing kHash1.prefix and kHash2. | 
|  775   // - Sub chunk kSubChunk1 containing kHash3. |  781   // - Sub chunk kSubChunk1 containing kHash3. | 
|  776   const char kBasename[] = "FileStoreVersion8"; |  782   const char kBasename[] = "FileStoreVersion8"; | 
|  777   base::FilePath golden_path; |  783   base::FilePath golden_path; | 
|  778   ASSERT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &golden_path)); |  784   ASSERT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &golden_path)); | 
|  779   golden_path = golden_path.AppendASCII("SafeBrowsing"); |  785   golden_path = golden_path.AppendASCII("SafeBrowsing"); | 
|  780   golden_path = golden_path.AppendASCII(kBasename); |  786   golden_path = golden_path.AppendASCII(kBasename); | 
|  781   ASSERT_TRUE(base::CopyFile(golden_path, filename_)); |  787   ASSERT_TRUE(base::CopyFile(golden_path, filename_)); | 
|  782  |  788  | 
|  783   // Reset the store to make sure it re-reads the file. |  789   // Reset the store to make sure it re-reads the file. | 
|  784   store_.reset(new SafeBrowsingStoreFile()); |  790   ASSERT_TRUE(!task_runner_->HasPendingTask()); | 
 |  791   store_.reset(new SafeBrowsingStoreFile(task_runner_)); | 
|  785   store_->Init(filename_, |  792   store_->Init(filename_, | 
|  786                base::Bind(&SafeBrowsingStoreFileTest::OnCorruptionDetected, |  793                base::Bind(&SafeBrowsingStoreFileTest::OnCorruptionDetected, | 
|  787                           base::Unretained(this))); |  794                           base::Unretained(this))); | 
|  788  |  795  | 
|  789   // Check that the expected prefixes and hashes are in place. |  796   // Check that the expected prefixes and hashes are in place. | 
|  790   SBAddPrefixes add_prefixes; |  797   SBAddPrefixes add_prefixes; | 
|  791   EXPECT_TRUE(store_->GetAddPrefixes(&add_prefixes)); |  798   EXPECT_TRUE(store_->GetAddPrefixes(&add_prefixes)); | 
|  792   ASSERT_EQ(2U, add_prefixes.size()); |  799   ASSERT_EQ(2U, add_prefixes.size()); | 
|  793   EXPECT_EQ(kAddChunk1, add_prefixes[0].chunk_id); |  800   EXPECT_EQ(kAddChunk1, add_prefixes[0].chunk_id); | 
|  794   EXPECT_EQ(kHash1.prefix, add_prefixes[0].prefix); |  801   EXPECT_EQ(kHash1.prefix, add_prefixes[0].prefix); | 
| (...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after  Loading... | 
|  835     std::vector<SBPrefix> prefixes_result; |  842     std::vector<SBPrefix> prefixes_result; | 
|  836     builder.GetPrefixSetNoHashes()->GetPrefixes(&prefixes_result); |  843     builder.GetPrefixSetNoHashes()->GetPrefixes(&prefixes_result); | 
|  837     ASSERT_EQ(1U, prefixes_result.size()); |  844     ASSERT_EQ(1U, prefixes_result.size()); | 
|  838     EXPECT_EQ(kHash1.prefix, prefixes_result[0]); |  845     EXPECT_EQ(kHash1.prefix, prefixes_result[0]); | 
|  839     EXPECT_TRUE(add_full_hashes_result.empty()); |  846     EXPECT_TRUE(add_full_hashes_result.empty()); | 
|  840   } |  847   } | 
|  841 } |  848 } | 
|  842 #endif |  849 #endif | 
|  843  |  850  | 
|  844 }  // namespace safe_browsing |  851 }  // namespace safe_browsing | 
| OLD | NEW |