Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 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 "components/subresource_filter/core/browser/ruleset_service.h" | 5 #include "components/subresource_filter/core/browser/ruleset_service.h" |
| 6 | 6 |
| 7 #include <stddef.h> | 7 #include <stddef.h> |
| 8 #include <stdint.h> | 8 #include <stdint.h> |
| 9 | 9 |
| 10 #include <memory> | 10 #include <memory> |
| (...skipping 127 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 138 | 138 |
| 139 // Test fixtures -------------------------------------------------------------- | 139 // Test fixtures -------------------------------------------------------------- |
| 140 | 140 |
| 141 using testing::TestRulesetPair; | 141 using testing::TestRulesetPair; |
| 142 using testing::TestRulesetCreator; | 142 using testing::TestRulesetCreator; |
| 143 | 143 |
| 144 class SubresourceFilteringRulesetServiceTest : public ::testing::Test { | 144 class SubresourceFilteringRulesetServiceTest : public ::testing::Test { |
| 145 public: | 145 public: |
| 146 SubresourceFilteringRulesetServiceTest() | 146 SubresourceFilteringRulesetServiceTest() |
| 147 : task_runner_(new base::TestSimpleTaskRunner), | 147 : task_runner_(new base::TestSimpleTaskRunner), |
| 148 task_runner_handle_(task_runner_), | 148 task_runner_handle_(task_runner_) {} |
| 149 mock_delegate_(nullptr) {} | |
| 150 | 149 |
| 151 protected: | 150 protected: |
| 152 void SetUp() override { | 151 void SetUp() override { |
| 153 IndexedRulesetVersion::RegisterPrefs(pref_service_.registry()); | 152 IndexedRulesetVersion::RegisterPrefs(pref_service_.registry()); |
| 154 | 153 |
| 155 SetUpTempDir(); | 154 SetUpTempDir(); |
| 156 ResetRulesetService(); | 155 ResetRulesetService(); |
| 157 RunUntilIdle(); | 156 RunUntilIdle(); |
| 158 | 157 |
| 159 ASSERT_NO_FATAL_FAILURE( | 158 ASSERT_NO_FATAL_FAILURE( |
| 160 ruleset_creator_.CreateRulesetToDisallowURLsWithPathSuffix( | 159 ruleset_creator_.CreateRulesetToDisallowURLsWithPathSuffix( |
| 161 kTestDisallowedSuffix1, &test_ruleset_1_)); | 160 kTestDisallowedSuffix1, &test_ruleset_1_)); |
| 162 ASSERT_NO_FATAL_FAILURE( | 161 ASSERT_NO_FATAL_FAILURE( |
| 163 ruleset_creator_.CreateRulesetToDisallowURLsWithPathSuffix( | 162 ruleset_creator_.CreateRulesetToDisallowURLsWithPathSuffix( |
| 164 kTestDisallowedSuffix2, &test_ruleset_2_)); | 163 kTestDisallowedSuffix2, &test_ruleset_2_)); |
| 165 ASSERT_NO_FATAL_FAILURE( | 164 ASSERT_NO_FATAL_FAILURE( |
| 166 ruleset_creator_.CreateRulesetToDisallowURLsWithPathSuffix( | 165 ruleset_creator_.CreateRulesetToDisallowURLsWithPathSuffix( |
| 167 kTestDisallowedSuffix3, &test_ruleset_3_)); | 166 kTestDisallowedSuffix3, &test_ruleset_3_)); |
| 168 } | 167 } |
| 169 | 168 |
| 170 virtual void SetUpTempDir() { | 169 virtual void SetUpTempDir() { |
| 171 ASSERT_TRUE(scoped_temp_dir_.CreateUniqueTempDir()); | 170 ASSERT_TRUE(scoped_temp_dir_.CreateUniqueTempDir()); |
| 172 } | 171 } |
| 173 | 172 |
| 174 void ResetRulesetService() { | 173 void ResetRulesetService() { |
| 174 mock_delegate_ = base::MakeUnique<MockRulesetServiceDelegate>(); | |
| 175 service_ = base::MakeUnique<RulesetService>( | 175 service_ = base::MakeUnique<RulesetService>( |
| 176 &pref_service_, task_runner_, | 176 &pref_service_, task_runner_, mock_delegate_.get(), base_dir()); |
| 177 base::WrapUnique(mock_delegate_ = new MockRulesetServiceDelegate), | |
| 178 base_dir()); | |
| 179 } | 177 } |
| 180 | 178 |
| 181 void ClearRulesetService() { | 179 void ClearRulesetService() { |
| 182 mock_delegate_ = nullptr; | |
| 183 service_.reset(); | 180 service_.reset(); |
| 181 mock_delegate_.reset(); | |
| 184 } | 182 } |
| 185 | 183 |
| 186 // Creates a new file with the given license |contents| at a unique temporary | 184 // Creates a new file with the given license |contents| at a unique temporary |
| 187 // path, which is returned in |path|. | 185 // path, which is returned in |path|. |
| 188 void CreateTestLicenseFile(const std::string& contents, | 186 void CreateTestLicenseFile(const std::string& contents, |
| 189 base::FilePath* path) { | 187 base::FilePath* path) { |
| 190 ASSERT_NO_FATAL_FAILURE( | 188 ASSERT_NO_FATAL_FAILURE( |
| 191 test_ruleset_creator()->GetUniqueTemporaryPath(path)); | 189 test_ruleset_creator()->GetUniqueTemporaryPath(path)); |
| 192 ASSERT_EQ(static_cast<int>(contents.size()), | 190 ASSERT_EQ(static_cast<int>(contents.size()), |
| 193 base::WriteFile(*path, contents.data(), | 191 base::WriteFile(*path, contents.data(), |
| (...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 262 void AssertReadonlyRulesetFile(base::File* file) { | 260 void AssertReadonlyRulesetFile(base::File* file) { |
| 263 const char kTest[] = "t"; | 261 const char kTest[] = "t"; |
| 264 ASSERT_TRUE(file->IsValid()); | 262 ASSERT_TRUE(file->IsValid()); |
| 265 ASSERT_EQ(-1, file->Write(0, kTest, sizeof(kTest))); | 263 ASSERT_EQ(-1, file->Write(0, kTest, sizeof(kTest))); |
| 266 } | 264 } |
| 267 | 265 |
| 268 void AssertNoPendingTasks() { ASSERT_FALSE(task_runner_->HasPendingTask()); } | 266 void AssertNoPendingTasks() { ASSERT_FALSE(task_runner_->HasPendingTask()); } |
| 269 | 267 |
| 270 PrefService* prefs() { return &pref_service_; } | 268 PrefService* prefs() { return &pref_service_; } |
| 271 RulesetService* service() { return service_.get(); } | 269 RulesetService* service() { return service_.get(); } |
| 272 MockRulesetServiceDelegate* mock_delegate() { return mock_delegate_; } | 270 MockRulesetServiceDelegate* mock_delegate() { return mock_delegate_.get(); } |
| 273 | 271 |
| 274 virtual base::FilePath effective_temp_dir() const { | 272 virtual base::FilePath effective_temp_dir() const { |
| 275 return scoped_temp_dir_.GetPath(); | 273 return scoped_temp_dir_.GetPath(); |
| 276 } | 274 } |
| 277 | 275 |
| 278 TestRulesetCreator* test_ruleset_creator() { return &ruleset_creator_; } | 276 TestRulesetCreator* test_ruleset_creator() { return &ruleset_creator_; } |
| 279 const TestRulesetPair& test_ruleset_1() const { return test_ruleset_1_; } | 277 const TestRulesetPair& test_ruleset_1() const { return test_ruleset_1_; } |
| 280 const TestRulesetPair& test_ruleset_2() const { return test_ruleset_2_; } | 278 const TestRulesetPair& test_ruleset_2() const { return test_ruleset_2_; } |
| 281 const TestRulesetPair& test_ruleset_3() const { return test_ruleset_3_; } | 279 const TestRulesetPair& test_ruleset_3() const { return test_ruleset_3_; } |
| 282 base::FilePath base_dir() const { | 280 base::FilePath base_dir() const { |
| 283 return effective_temp_dir().AppendASCII("Rules").AppendASCII("Indexed"); | 281 return effective_temp_dir().AppendASCII("Rules").AppendASCII("Indexed"); |
| 284 } | 282 } |
| 285 | 283 |
| 286 private: | 284 private: |
| 287 base::ScopedTempDir scoped_temp_dir_; | 285 base::ScopedTempDir scoped_temp_dir_; |
| 288 | 286 |
| 289 scoped_refptr<base::TestSimpleTaskRunner> task_runner_; | 287 scoped_refptr<base::TestSimpleTaskRunner> task_runner_; |
| 290 base::ThreadTaskRunnerHandle task_runner_handle_; | 288 base::ThreadTaskRunnerHandle task_runner_handle_; |
| 291 TestingPrefServiceSimple pref_service_; | 289 TestingPrefServiceSimple pref_service_; |
| 292 | 290 |
| 293 TestRulesetCreator ruleset_creator_; | 291 TestRulesetCreator ruleset_creator_; |
| 294 TestRulesetPair test_ruleset_1_; | 292 TestRulesetPair test_ruleset_1_; |
| 295 TestRulesetPair test_ruleset_2_; | 293 TestRulesetPair test_ruleset_2_; |
| 296 TestRulesetPair test_ruleset_3_; | 294 TestRulesetPair test_ruleset_3_; |
| 297 | 295 |
| 298 std::unique_ptr<RulesetService> service_; | 296 std::unique_ptr<RulesetService> service_; |
| 299 MockRulesetServiceDelegate* mock_delegate_; // Weak, owned by |service_|. | 297 std::unique_ptr<MockRulesetServiceDelegate> mock_delegate_; |
|
pkalinnikov
2017/03/09 16:09:01
Please swap the lines to ensure that |mock_delegat
Charlie Harrison
2017/03/09 16:12:44
Done.
| |
| 300 | 298 |
| 301 DISALLOW_COPY_AND_ASSIGN(SubresourceFilteringRulesetServiceTest); | 299 DISALLOW_COPY_AND_ASSIGN(SubresourceFilteringRulesetServiceTest); |
| 302 }; | 300 }; |
| 303 | 301 |
| 304 // Specialized test fixture for death tests. It exposes the temporary directory | 302 // Specialized test fixture for death tests. It exposes the temporary directory |
| 305 // used by the parent process as an environment variable that is consumed by the | 303 // used by the parent process as an environment variable that is consumed by the |
| 306 // child process, so that both processes use the same temporary directory. This | 304 // child process, so that both processes use the same temporary directory. This |
| 307 // would not otherwise be the case on Windows, where children are not `forked` | 305 // would not otherwise be the case on Windows, where children are not `forked` |
| 308 // and therefore would create their own unique temp directory. | 306 // and therefore would create their own unique temp directory. |
| 309 class SubresourceFilteringRulesetServiceDeathTest | 307 class SubresourceFilteringRulesetServiceDeathTest |
| (...skipping 681 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 991 mock_delegate()->SimulateStartupCompleted(); | 989 mock_delegate()->SimulateStartupCompleted(); |
| 992 IndexAndStoreAndPublishUpdatedRuleset(test_ruleset_1(), kTestContentVersion1); | 990 IndexAndStoreAndPublishUpdatedRuleset(test_ruleset_1(), kTestContentVersion1); |
| 993 RunUntilIdle(); | 991 RunUntilIdle(); |
| 994 | 992 |
| 995 ASSERT_EQ(1u, mock_delegate()->published_rulesets().size()); | 993 ASSERT_EQ(1u, mock_delegate()->published_rulesets().size()); |
| 996 ASSERT_NO_FATAL_FAILURE( | 994 ASSERT_NO_FATAL_FAILURE( |
| 997 AssertReadonlyRulesetFile(&mock_delegate()->published_rulesets()[0])); | 995 AssertReadonlyRulesetFile(&mock_delegate()->published_rulesets()[0])); |
| 998 } | 996 } |
| 999 | 997 |
| 1000 } // namespace subresource_filter | 998 } // namespace subresource_filter |
| OLD | NEW |