| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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/extensions/extension_install_checker.h" |
| 5 #include "base/bind.h" | 6 #include "base/bind.h" |
| 6 #include "base/location.h" | 7 #include "base/location.h" |
| 8 #include "base/memory/ptr_util.h" |
| 7 #include "base/run_loop.h" | 9 #include "base/run_loop.h" |
| 8 #include "base/single_thread_task_runner.h" | 10 #include "base/single_thread_task_runner.h" |
| 11 #include "base/strings/utf_string_conversions.h" |
| 9 #include "base/threading/thread_task_runner_handle.h" | 12 #include "base/threading/thread_task_runner_handle.h" |
| 10 #include "chrome/browser/extensions/extension_install_checker.h" | 13 #include "extensions/browser/preload_check_test_util.h" |
| 11 #include "testing/gtest/include/gtest/gtest.h" | 14 #include "testing/gtest/include/gtest/gtest.h" |
| 12 | 15 |
| 13 namespace extensions { | 16 namespace extensions { |
| 14 | 17 |
| 15 namespace { | 18 namespace { |
| 16 | 19 |
| 17 const BlacklistState kBlacklistStateError = BLACKLISTED_MALWARE; | 20 const PreloadCheck::Error kBlacklistError = PreloadCheck::BLACKLISTED_ID; |
| 18 const char kDummyRequirementsError[] = "Requirements error"; | 21 const char kDummyRequirementsError[] = "Requirements error"; |
| 19 const char kDummyPolicyError[] = "Cannot install extension"; | 22 const char kDummyPolicyError[] = "Cannot install extension"; |
| 20 | 23 |
| 21 } // namespace | 24 } // namespace |
| 22 | 25 |
| 23 // Stubs most of the checks since we are interested in validating the logic in | 26 // Stubs most of the checks since we are interested in validating the logic in |
| 24 // the install checker. This class implements a synchronous version of all | 27 // the install checker. This class implements a synchronous version of all |
| 25 // checks. | 28 // checks. |
| 26 class ExtensionInstallCheckerForTest : public ExtensionInstallChecker { | 29 class ExtensionInstallCheckerForTest : public ExtensionInstallChecker { |
| 27 public: | 30 public: |
| 28 ExtensionInstallCheckerForTest(int enabled_checks, bool fail_fast) | 31 ExtensionInstallCheckerForTest(int enabled_checks, bool fail_fast) |
| 29 : ExtensionInstallChecker(nullptr, nullptr, enabled_checks, fail_fast), | 32 : ExtensionInstallChecker(nullptr, nullptr, enabled_checks, fail_fast), |
| 30 requirements_check_called_(false), | 33 requirements_check_called_(false), |
| 31 blacklist_check_called_(false), | 34 blacklist_check_called_(false), |
| 32 policy_check_called_(false), | 35 policy_check_called_(false) {} |
| 33 blacklist_state_(NOT_BLACKLISTED) {} | |
| 34 | 36 |
| 35 ~ExtensionInstallCheckerForTest() override {} | 37 ~ExtensionInstallCheckerForTest() override {} |
| 36 | 38 |
| 37 void set_requirements_error(const std::string& error) { | 39 void set_requirements_error(const std::string& error) { |
| 38 requirements_error_ = error; | 40 requirements_error_ = error; |
| 39 } | 41 } |
| 40 void set_policy_check_error(const std::string& error) { | |
| 41 policy_check_error_ = error; | |
| 42 } | |
| 43 void set_blacklist_state(BlacklistState state) { blacklist_state_ = state; } | |
| 44 | 42 |
| 45 bool requirements_check_called() const { return requirements_check_called_; } | 43 bool requirements_check_called() const { return requirements_check_called_; } |
| 46 bool blacklist_check_called() const { return blacklist_check_called_; } | 44 bool blacklist_check_called() const { return blacklist_check_called_; } |
| 47 bool policy_check_called() const { return policy_check_called_; } | 45 bool policy_check_called() const { return policy_check_called_; } |
| 48 | 46 |
| 47 virtual bool is_async() { return false; } |
| 48 |
| 49 void MockCheckRequirements() { | 49 void MockCheckRequirements() { |
| 50 if (!is_running()) | 50 if (!is_running()) |
| 51 return; | 51 return; |
| 52 std::vector<std::string> errors; | 52 std::vector<std::string> errors; |
| 53 if (!requirements_error_.empty()) | 53 if (!requirements_error_.empty()) |
| 54 errors.push_back(requirements_error_); | 54 errors.push_back(requirements_error_); |
| 55 OnRequirementsCheckDone(errors); | 55 OnRequirementsCheckDone(errors); |
| 56 } | 56 } |
| 57 | 57 |
| 58 void MockCheckBlacklistState() { | |
| 59 if (!is_running()) | |
| 60 return; | |
| 61 OnBlacklistStateCheckDone(blacklist_state_); | |
| 62 } | |
| 63 | |
| 64 protected: | 58 protected: |
| 65 void CheckRequirements() override { | 59 void CheckRequirements() override { |
| 66 requirements_check_called_ = true; | 60 requirements_check_called_ = true; |
| 67 MockCheckRequirements(); | 61 if (is_async()) { |
| 62 base::ThreadTaskRunnerHandle::Get()->PostTask( |
| 63 FROM_HERE, |
| 64 base::Bind(&ExtensionInstallCheckerForTest::MockCheckRequirements, |
| 65 base::Unretained(this))); |
| 66 } else { |
| 67 MockCheckRequirements(); |
| 68 } |
| 68 } | 69 } |
| 69 | 70 |
| 70 void CheckManagementPolicy() override { | 71 void CheckManagementPolicy() override { |
| 71 policy_check_called_ = true; | 72 policy_check_called_ = true; |
| 72 OnManagementPolicyCheckDone(policy_check_error_.empty(), | 73 ExtensionInstallChecker::CheckManagementPolicy(); |
| 73 policy_check_error_); | |
| 74 } | 74 } |
| 75 | 75 |
| 76 void CheckBlacklistState() override { | 76 void CheckBlacklistState() override { |
| 77 blacklist_check_called_ = true; | 77 blacklist_check_called_ = true; |
| 78 MockCheckBlacklistState(); | 78 ExtensionInstallChecker::CheckBlacklistState(); |
| 79 } | 79 } |
| 80 | 80 |
| 81 bool requirements_check_called_; | 81 bool requirements_check_called_; |
| 82 bool blacklist_check_called_; | 82 bool blacklist_check_called_; |
| 83 bool policy_check_called_; | 83 bool policy_check_called_; |
| 84 | 84 |
| 85 // Dummy errors for testing. | 85 // Dummy errors for testing. |
| 86 std::string requirements_error_; | 86 std::string requirements_error_; |
| 87 std::string policy_check_error_; | |
| 88 BlacklistState blacklist_state_; | |
| 89 }; | 87 }; |
| 90 | 88 |
| 91 // This class implements asynchronous mocks of the requirements and blacklist | 89 // This class implements asynchronous mocks of the requirements and blacklist |
| 92 // checks. | 90 // checks. |
| 93 class ExtensionInstallCheckerAsync : public ExtensionInstallCheckerForTest { | 91 class ExtensionInstallCheckerAsync : public ExtensionInstallCheckerForTest { |
| 94 public: | 92 public: |
| 95 ExtensionInstallCheckerAsync(int enabled_checks, bool fail_fast) | 93 ExtensionInstallCheckerAsync(int enabled_checks, bool fail_fast) |
| 96 : ExtensionInstallCheckerForTest(enabled_checks, fail_fast) {} | 94 : ExtensionInstallCheckerForTest(enabled_checks, fail_fast) {} |
| 95 ~ExtensionInstallCheckerAsync() override {} |
| 97 | 96 |
| 98 protected: | 97 protected: |
| 99 void CheckRequirements() override { | 98 bool is_async() override { return true; } |
| 100 requirements_check_called_ = true; | |
| 101 | |
| 102 base::ThreadTaskRunnerHandle::Get()->PostTask( | |
| 103 FROM_HERE, | |
| 104 base::Bind(&ExtensionInstallCheckerForTest::MockCheckRequirements, | |
| 105 base::Unretained(this))); | |
| 106 } | |
| 107 | |
| 108 void CheckBlacklistState() override { | |
| 109 blacklist_check_called_ = true; | |
| 110 | |
| 111 base::ThreadTaskRunnerHandle::Get()->PostTask( | |
| 112 FROM_HERE, | |
| 113 base::Bind(&ExtensionInstallCheckerForTest::MockCheckBlacklistState, | |
| 114 base::Unretained(this))); | |
| 115 } | |
| 116 }; | 99 }; |
| 117 | 100 |
| 118 class CheckObserver { | 101 class CheckObserver { |
| 119 public: | 102 public: |
| 120 CheckObserver() : result_(0), call_count_(0) {} | 103 CheckObserver() : result_(0), call_count_(0) {} |
| 121 | 104 |
| 122 int result() const { return result_; } | 105 int result() const { return result_; } |
| 123 int call_count() const { return call_count_; } | 106 int call_count() const { return call_count_; } |
| 124 | 107 |
| 125 void OnChecksComplete(int checks_failed) { | 108 void OnChecksComplete(int checks_failed) { |
| (...skipping 11 matching lines...) Expand all Loading... |
| 137 private: | 120 private: |
| 138 int result_; | 121 int result_; |
| 139 int call_count_; | 122 int call_count_; |
| 140 }; | 123 }; |
| 141 | 124 |
| 142 class ExtensionInstallCheckerTest : public testing::Test { | 125 class ExtensionInstallCheckerTest : public testing::Test { |
| 143 public: | 126 public: |
| 144 ExtensionInstallCheckerTest() {} | 127 ExtensionInstallCheckerTest() {} |
| 145 ~ExtensionInstallCheckerTest() override {} | 128 ~ExtensionInstallCheckerTest() override {} |
| 146 | 129 |
| 130 void SetBlacklistError(ExtensionInstallCheckerForTest* checker, |
| 131 PreloadCheck::Error error) { |
| 132 std::unique_ptr<PreloadCheckStub> blacklist_check = |
| 133 base::MakeUnique<PreloadCheckStub>(checker->is_async()); |
| 134 if (error != PreloadCheck::NONE) |
| 135 blacklist_check->AddError(error); |
| 136 checker->SetBlacklistCheckForTesting(std::move(blacklist_check)); |
| 137 } |
| 138 |
| 139 void SetPolicyError(ExtensionInstallCheckerForTest* checker, |
| 140 PreloadCheck::Error error, |
| 141 std::string message) { |
| 142 // The policy check always runs synchronously. |
| 143 std::unique_ptr<PreloadCheckStub> policy_check = |
| 144 base::MakeUnique<PreloadCheckStub>(); |
| 145 if (error != PreloadCheck::NONE) { |
| 146 policy_check->AddError(error); |
| 147 policy_check->set_error_message(base::UTF8ToUTF16(message)); |
| 148 } |
| 149 checker->SetPolicyCheckForTesting(std::move(policy_check)); |
| 150 } |
| 151 |
| 147 protected: | 152 protected: |
| 153 void SetAllPass(ExtensionInstallCheckerForTest* checker) { |
| 154 SetBlacklistError(checker, PreloadCheck::NONE); |
| 155 SetPolicyError(checker, PreloadCheck::NONE, ""); |
| 156 checker->set_requirements_error(""); |
| 157 } |
| 158 |
| 148 void SetAllErrors(ExtensionInstallCheckerForTest* checker) { | 159 void SetAllErrors(ExtensionInstallCheckerForTest* checker) { |
| 149 checker->set_blacklist_state(kBlacklistStateError); | 160 SetBlacklistError(checker, kBlacklistError); |
| 150 checker->set_policy_check_error(kDummyPolicyError); | 161 SetPolicyError(checker, PreloadCheck::DISALLOWED_BY_POLICY, |
| 162 kDummyPolicyError); |
| 151 checker->set_requirements_error(kDummyRequirementsError); | 163 checker->set_requirements_error(kDummyRequirementsError); |
| 152 } | 164 } |
| 153 | 165 |
| 154 void ValidateExpectedCalls(int call_mask, | 166 void ValidateExpectedCalls(int call_mask, |
| 155 const ExtensionInstallCheckerForTest& checker) { | 167 const ExtensionInstallCheckerForTest& checker) { |
| 156 bool expect_blacklist_checked = | 168 bool expect_blacklist_checked = |
| 157 (call_mask & ExtensionInstallChecker::CHECK_BLACKLIST) != 0; | 169 (call_mask & ExtensionInstallChecker::CHECK_BLACKLIST) != 0; |
| 158 bool expect_requirements_checked = | 170 bool expect_requirements_checked = |
| 159 (call_mask & ExtensionInstallChecker::CHECK_REQUIREMENTS) != 0; | 171 (call_mask & ExtensionInstallChecker::CHECK_REQUIREMENTS) != 0; |
| 160 bool expect_policy_checked = | 172 bool expect_policy_checked = |
| (...skipping 12 matching lines...) Expand all Loading... |
| 173 EXPECT_FALSE(checker.requirement_errors().empty()); | 185 EXPECT_FALSE(checker.requirement_errors().empty()); |
| 174 EXPECT_EQ(std::string(expected_error), | 186 EXPECT_EQ(std::string(expected_error), |
| 175 checker.requirement_errors().front()); | 187 checker.requirement_errors().front()); |
| 176 } | 188 } |
| 177 | 189 |
| 178 void ExpectRequirementsError(const ExtensionInstallCheckerForTest& checker) { | 190 void ExpectRequirementsError(const ExtensionInstallCheckerForTest& checker) { |
| 179 ExpectRequirementsError(kDummyRequirementsError, checker); | 191 ExpectRequirementsError(kDummyRequirementsError, checker); |
| 180 } | 192 } |
| 181 | 193 |
| 182 void ExpectBlacklistPass(const ExtensionInstallCheckerForTest& checker) { | 194 void ExpectBlacklistPass(const ExtensionInstallCheckerForTest& checker) { |
| 183 EXPECT_EQ(NOT_BLACKLISTED, checker.blacklist_state()); | 195 EXPECT_EQ(PreloadCheck::NONE, checker.blacklist_error()); |
| 184 } | 196 } |
| 185 | 197 |
| 186 void ExpectBlacklistError(const ExtensionInstallCheckerForTest& checker) { | 198 void ExpectBlacklistError(const ExtensionInstallCheckerForTest& checker) { |
| 187 EXPECT_EQ(kBlacklistStateError, checker.blacklist_state()); | 199 EXPECT_EQ(kBlacklistError, checker.blacklist_error()); |
| 188 } | 200 } |
| 189 | 201 |
| 190 void ExpectPolicyPass(const ExtensionInstallCheckerForTest& checker) { | 202 void ExpectPolicyPass(const ExtensionInstallCheckerForTest& checker) { |
| 191 EXPECT_TRUE(checker.policy_allows_load()); | |
| 192 EXPECT_TRUE(checker.policy_error().empty()); | 203 EXPECT_TRUE(checker.policy_error().empty()); |
| 193 } | 204 } |
| 194 | 205 |
| 195 void ExpectPolicyError(const char* expected_error, | 206 void ExpectPolicyError(const char* expected_error, |
| 196 const ExtensionInstallCheckerForTest& checker) { | 207 const ExtensionInstallCheckerForTest& checker) { |
| 197 EXPECT_FALSE(checker.policy_allows_load()); | |
| 198 EXPECT_FALSE(checker.policy_error().empty()); | 208 EXPECT_FALSE(checker.policy_error().empty()); |
| 199 EXPECT_EQ(std::string(expected_error), checker.policy_error()); | 209 EXPECT_EQ(std::string(expected_error), checker.policy_error()); |
| 200 } | 210 } |
| 201 | 211 |
| 202 void ExpectPolicyError(const ExtensionInstallCheckerForTest& checker) { | 212 void ExpectPolicyError(const ExtensionInstallCheckerForTest& checker) { |
| 203 ExpectPolicyError(kDummyPolicyError, checker); | 213 ExpectPolicyError(kDummyPolicyError, checker); |
| 204 } | 214 } |
| 205 | 215 |
| 206 void RunChecker(ExtensionInstallCheckerForTest* checker, | 216 void RunChecker(ExtensionInstallCheckerForTest* checker, |
| 207 int expected_checks_run, | 217 int expected_checks_run, |
| 208 int expected_result) { | 218 int expected_result) { |
| 209 CheckObserver observer; | 219 CheckObserver observer; |
| 210 checker->Start(base::Bind(&CheckObserver::OnChecksComplete, | 220 checker->Start(base::Bind(&CheckObserver::OnChecksComplete, |
| 211 base::Unretained(&observer))); | 221 base::Unretained(&observer))); |
| 212 observer.Wait(); | 222 observer.Wait(); |
| 213 | 223 |
| 214 EXPECT_FALSE(checker->is_running()); | 224 EXPECT_FALSE(checker->is_running()); |
| 215 EXPECT_EQ(expected_result, observer.result()); | 225 EXPECT_EQ(expected_result, observer.result()); |
| 216 EXPECT_EQ(1, observer.call_count()); | 226 EXPECT_EQ(1, observer.call_count()); |
| 217 ValidateExpectedCalls(expected_checks_run, *checker); | 227 ValidateExpectedCalls(expected_checks_run, *checker); |
| 218 } | 228 } |
| 219 | 229 |
| 220 void DoRunAllChecksPass(ExtensionInstallCheckerForTest* checker) { | 230 void DoRunAllChecksPass(ExtensionInstallCheckerForTest* checker) { |
| 231 SetAllPass(checker); |
| 221 RunChecker(checker, | 232 RunChecker(checker, |
| 222 ExtensionInstallChecker::CHECK_ALL, | 233 ExtensionInstallChecker::CHECK_ALL, |
| 223 0); | 234 0); |
| 224 | 235 |
| 225 ExpectRequirementsPass(*checker); | 236 ExpectRequirementsPass(*checker); |
| 226 ExpectPolicyPass(*checker); | 237 ExpectPolicyPass(*checker); |
| 227 ExpectBlacklistPass(*checker); | 238 ExpectBlacklistPass(*checker); |
| 228 } | 239 } |
| 229 | 240 |
| 230 void DoRunAllChecksFail(ExtensionInstallCheckerForTest* checker) { | 241 void DoRunAllChecksFail(ExtensionInstallCheckerForTest* checker) { |
| (...skipping 105 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 336 } | 347 } |
| 337 | 348 |
| 338 // Test fail fast with asynchronous callbacks. | 349 // Test fail fast with asynchronous callbacks. |
| 339 TEST_F(ExtensionInstallCheckerTest, FailFastAsync) { | 350 TEST_F(ExtensionInstallCheckerTest, FailFastAsync) { |
| 340 // This test assumes some internal knowledge of the implementation - that | 351 // This test assumes some internal knowledge of the implementation - that |
| 341 // the requirements check runs before the blacklist check. Both checks should | 352 // the requirements check runs before the blacklist check. Both checks should |
| 342 // be called, but the requirements check callback arrives first and the | 353 // be called, but the requirements check callback arrives first and the |
| 343 // blacklist result will be discarded. | 354 // blacklist result will be discarded. |
| 344 ExtensionInstallCheckerAsync checker(ExtensionInstallChecker::CHECK_ALL, | 355 ExtensionInstallCheckerAsync checker(ExtensionInstallChecker::CHECK_ALL, |
| 345 /*fail_fast=*/true); | 356 /*fail_fast=*/true); |
| 346 | |
| 347 SetAllErrors(&checker); | 357 SetAllErrors(&checker); |
| 348 | 358 |
| 349 // The policy check is synchronous and needs to pass for the other tests to | 359 // The policy check is synchronous and needs to pass for the other tests to |
| 350 // run. | 360 // run. |
| 351 checker.set_policy_check_error(std::string()); | 361 SetPolicyError(&checker, PreloadCheck::NONE, ""); |
| 352 | 362 |
| 353 RunChecker(&checker, | 363 RunChecker(&checker, |
| 354 ExtensionInstallChecker::CHECK_ALL, | 364 ExtensionInstallChecker::CHECK_ALL, |
| 355 ExtensionInstallChecker::CHECK_REQUIREMENTS); | 365 ExtensionInstallChecker::CHECK_REQUIREMENTS); |
| 356 | 366 |
| 357 ExpectRequirementsError(checker); | 367 ExpectRequirementsError(checker); |
| 358 ExpectPolicyPass(checker); | 368 ExpectPolicyPass(checker); |
| 359 ExpectBlacklistPass(checker); | 369 ExpectBlacklistPass(checker); |
| 360 } | 370 } |
| 361 | 371 |
| 362 } // namespace extensions | 372 } // namespace extensions |
| OLD | NEW |