| 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 <list> | 5 #include <list> |
| 6 #include <set> | 6 #include <set> |
| 7 #include <string> | 7 #include <string> |
| 8 | 8 |
| 9 #include "base/scoped_observer.h" | 9 #include "base/scoped_observer.h" |
| 10 #include "chrome/browser/extensions/extension_browsertest.h" | 10 #include "chrome/browser/extensions/extension_browsertest.h" |
| (...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 49 private: | 49 private: |
| 50 ExtensionId awaited_id_; | 50 ExtensionId awaited_id_; |
| 51 std::set<ExtensionId> observed_; | 51 std::set<ExtensionId> observed_; |
| 52 scoped_refptr<content::MessageLoopRunner> loop_runner_; | 52 scoped_refptr<content::MessageLoopRunner> loop_runner_; |
| 53 ScopedObserver<ExtensionRegistry, UnloadObserver> observer_; | 53 ScopedObserver<ExtensionRegistry, UnloadObserver> observer_; |
| 54 }; | 54 }; |
| 55 | 55 |
| 56 // Helper for forcing ContentVerifyJob's to return an error. | 56 // Helper for forcing ContentVerifyJob's to return an error. |
| 57 class JobDelegate : public ContentVerifyJob::TestDelegate { | 57 class JobDelegate : public ContentVerifyJob::TestDelegate { |
| 58 public: | 58 public: |
| 59 JobDelegate() : fail_next_read_(false), fail_next_done_(false) {} | 59 JobDelegate() |
| 60 : fail_next_read_(false), |
| 61 fail_next_done_(false), |
| 62 bytes_read_failed_(0), |
| 63 done_reading_failed_(0) {} |
| 60 | 64 |
| 61 virtual ~JobDelegate() {} | 65 virtual ~JobDelegate() {} |
| 62 | 66 |
| 63 void set_id(const ExtensionId& id) { id_ = id; } | 67 void set_id(const ExtensionId& id) { id_ = id; } |
| 64 void fail_next_read() { fail_next_read_ = true; } | 68 void fail_next_read() { fail_next_read_ = true; } |
| 65 void fail_next_done() { fail_next_done_ = true; } | 69 void fail_next_done() { fail_next_done_ = true; } |
| 66 | 70 |
| 71 // Return the number of BytesRead/DoneReading calls we actually failed, |
| 72 // respectively. |
| 73 int bytes_read_failed() { return bytes_read_failed_; } |
| 74 int done_reading_failed() { return done_reading_failed_; } |
| 75 |
| 67 ContentVerifyJob::FailureReason BytesRead(const ExtensionId& id, | 76 ContentVerifyJob::FailureReason BytesRead(const ExtensionId& id, |
| 68 int count, | 77 int count, |
| 69 const char* data) override { | 78 const char* data) override { |
| 70 if (id == id_ && fail_next_read_) { | 79 if (id == id_ && fail_next_read_) { |
| 71 fail_next_read_ = false; | 80 fail_next_read_ = false; |
| 81 bytes_read_failed_++; |
| 72 return ContentVerifyJob::HASH_MISMATCH; | 82 return ContentVerifyJob::HASH_MISMATCH; |
| 73 } | 83 } |
| 74 return ContentVerifyJob::NONE; | 84 return ContentVerifyJob::NONE; |
| 75 } | 85 } |
| 76 | 86 |
| 77 ContentVerifyJob::FailureReason DoneReading(const ExtensionId& id) override { | 87 ContentVerifyJob::FailureReason DoneReading(const ExtensionId& id) override { |
| 78 if (id == id_ && fail_next_done_) { | 88 if (id == id_ && fail_next_done_) { |
| 79 fail_next_done_ = false; | 89 fail_next_done_ = false; |
| 90 done_reading_failed_++; |
| 80 return ContentVerifyJob::HASH_MISMATCH; | 91 return ContentVerifyJob::HASH_MISMATCH; |
| 81 } | 92 } |
| 82 return ContentVerifyJob::NONE; | 93 return ContentVerifyJob::NONE; |
| 83 } | 94 } |
| 84 | 95 |
| 85 private: | 96 private: |
| 86 DISALLOW_COPY_AND_ASSIGN(JobDelegate); | |
| 87 | |
| 88 ExtensionId id_; | 97 ExtensionId id_; |
| 89 bool fail_next_read_; | 98 bool fail_next_read_; |
| 90 bool fail_next_done_; | 99 bool fail_next_done_; |
| 100 int bytes_read_failed_; |
| 101 int done_reading_failed_; |
| 102 |
| 103 DISALLOW_COPY_AND_ASSIGN(JobDelegate); |
| 91 }; | 104 }; |
| 92 | 105 |
| 93 class JobObserver : public ContentVerifyJob::TestObserver { | 106 class JobObserver : public ContentVerifyJob::TestObserver { |
| 94 public: | 107 public: |
| 95 JobObserver(); | 108 JobObserver(); |
| 96 virtual ~JobObserver(); | 109 virtual ~JobObserver(); |
| 97 | 110 |
| 98 enum class Result { SUCCESS, FAILURE }; | 111 enum class Result { SUCCESS, FAILURE }; |
| 99 | 112 |
| 100 // Call this to add an expected job result. | 113 // Call this to add an expected job result. |
| (...skipping 154 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 255 } | 268 } |
| 256 | 269 |
| 257 void TearDownOnMainThread() override { | 270 void TearDownOnMainThread() override { |
| 258 ContentVerifier::SetObserverForTests(NULL); | 271 ContentVerifier::SetObserverForTests(NULL); |
| 259 ContentVerifyJob::SetDelegateForTests(NULL); | 272 ContentVerifyJob::SetDelegateForTests(NULL); |
| 260 ContentVerifyJob::SetObserverForTests(NULL); | 273 ContentVerifyJob::SetObserverForTests(NULL); |
| 261 ExtensionBrowserTest::TearDownOnMainThread(); | 274 ExtensionBrowserTest::TearDownOnMainThread(); |
| 262 } | 275 } |
| 263 | 276 |
| 264 virtual void OpenPageAndWaitForUnload() { | 277 virtual void OpenPageAndWaitForUnload() { |
| 278 ContentVerifyJob::SetDelegateForTests(&delegate_); |
| 279 std::string id = "npnbmohejbjohgpjnmjagbafnjhkmgko"; |
| 280 delegate_.set_id(id); |
| 265 unload_observer_.reset( | 281 unload_observer_.reset( |
| 266 new UnloadObserver(ExtensionRegistry::Get(profile()))); | 282 new UnloadObserver(ExtensionRegistry::Get(profile()))); |
| 267 const Extension* extension = InstallExtensionFromWebstore( | 283 const Extension* extension = InstallExtensionFromWebstore( |
| 268 test_data_dir_.AppendASCII("content_verifier/v1.crx"), 1); | 284 test_data_dir_.AppendASCII("content_verifier/v1.crx"), 1); |
| 269 ASSERT_TRUE(extension); | 285 ASSERT_TRUE(extension); |
| 270 id_ = extension->id(); | 286 ASSERT_EQ(id, extension->id()); |
| 271 page_url_ = extension->GetResourceURL("page.html"); | 287 page_url_ = extension->GetResourceURL("page.html"); |
| 272 delegate_.set_id(id_); | |
| 273 ContentVerifyJob::SetDelegateForTests(&delegate_); | |
| 274 | 288 |
| 275 // This call passes false for |check_navigation_success|, because checking | 289 // This call passes false for |check_navigation_success|, because checking |
| 276 // for navigation success needs the WebContents to still exist after the | 290 // for navigation success needs the WebContents to still exist after the |
| 277 // navigation, whereas this navigation triggers an unload which destroys | 291 // navigation, whereas this navigation triggers an unload which destroys |
| 278 // the WebContents. | 292 // the WebContents. |
| 279 AddTabAtIndexToBrowser(browser(), 1, page_url_, ui::PAGE_TRANSITION_LINK, | 293 AddTabAtIndexToBrowser(browser(), 1, page_url_, ui::PAGE_TRANSITION_LINK, |
| 280 false); | 294 false); |
| 281 | 295 |
| 282 unload_observer_->WaitForUnload(id_); | 296 unload_observer_->WaitForUnload(id); |
| 283 ExtensionPrefs* prefs = ExtensionPrefs::Get(profile()); | 297 ExtensionPrefs* prefs = ExtensionPrefs::Get(profile()); |
| 284 int reasons = prefs->GetDisableReasons(id_); | 298 int reasons = prefs->GetDisableReasons(id); |
| 285 EXPECT_TRUE(reasons & Extension::DISABLE_CORRUPTED); | 299 EXPECT_TRUE(reasons & Extension::DISABLE_CORRUPTED); |
| 286 | 300 |
| 287 // This needs to happen before the ExtensionRegistry gets deleted, which | 301 // This needs to happen before the ExtensionRegistry gets deleted, which |
| 288 // happens before TearDownOnMainThread is called. | 302 // happens before TearDownOnMainThread is called. |
| 289 unload_observer_.reset(); | 303 unload_observer_.reset(); |
| 290 } | 304 } |
| 291 | 305 |
| 292 protected: | 306 protected: |
| 293 JobDelegate delegate_; | 307 JobDelegate delegate_; |
| 294 scoped_ptr<UnloadObserver> unload_observer_; | 308 scoped_ptr<UnloadObserver> unload_observer_; |
| 295 ExtensionId id_; | |
| 296 GURL page_url_; | 309 GURL page_url_; |
| 297 }; | 310 }; |
| 298 | 311 |
| 299 IN_PROC_BROWSER_TEST_F(ContentVerifierTest, FailOnRead) { | 312 IN_PROC_BROWSER_TEST_F(ContentVerifierTest, FailOnRead) { |
| 313 EXPECT_EQ(0, delegate_.bytes_read_failed()); |
| 300 delegate_.fail_next_read(); | 314 delegate_.fail_next_read(); |
| 301 OpenPageAndWaitForUnload(); | 315 OpenPageAndWaitForUnload(); |
| 316 EXPECT_EQ(1, delegate_.bytes_read_failed()); |
| 302 } | 317 } |
| 303 | 318 |
| 304 IN_PROC_BROWSER_TEST_F(ContentVerifierTest, FailOnDone) { | 319 IN_PROC_BROWSER_TEST_F(ContentVerifierTest, FailOnDone) { |
| 320 EXPECT_EQ(0, delegate_.done_reading_failed()); |
| 305 delegate_.fail_next_done(); | 321 delegate_.fail_next_done(); |
| 306 OpenPageAndWaitForUnload(); | 322 OpenPageAndWaitForUnload(); |
| 323 EXPECT_EQ(1, delegate_.done_reading_failed()); |
| 307 } | 324 } |
| 308 | 325 |
| 309 IN_PROC_BROWSER_TEST_F(ContentVerifierTest, DotSlashPaths) { | 326 IN_PROC_BROWSER_TEST_F(ContentVerifierTest, DotSlashPaths) { |
| 310 JobObserver job_observer; | 327 JobObserver job_observer; |
| 311 ContentVerifyJob::SetObserverForTests(&job_observer); | 328 ContentVerifyJob::SetObserverForTests(&job_observer); |
| 312 std::string id = "hoipipabpcoomfapcecilckodldhmpgl"; | 329 std::string id = "hoipipabpcoomfapcecilckodldhmpgl"; |
| 313 | 330 |
| 314 job_observer.ExpectJobResult( | 331 job_observer.ExpectJobResult( |
| 315 id, base::FilePath(FILE_PATH_LITERAL("background.js")), | 332 id, base::FilePath(FILE_PATH_LITERAL("background.js")), |
| 316 JobObserver::Result::SUCCESS); | 333 JobObserver::Result::SUCCESS); |
| (...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 399 job_observer.ExpectJobResult(id, | 416 job_observer.ExpectJobResult(id, |
| 400 base::FilePath(FILE_PATH_LITERAL("script.js")), | 417 base::FilePath(FILE_PATH_LITERAL("script.js")), |
| 401 JobObserver::Result::FAILURE); | 418 JobObserver::Result::FAILURE); |
| 402 EnableExtension(id); | 419 EnableExtension(id); |
| 403 EXPECT_TRUE(job_observer.WaitForExpectedJobs()); | 420 EXPECT_TRUE(job_observer.WaitForExpectedJobs()); |
| 404 | 421 |
| 405 ContentVerifyJob::SetObserverForTests(NULL); | 422 ContentVerifyJob::SetObserverForTests(NULL); |
| 406 } | 423 } |
| 407 | 424 |
| 408 } // namespace extensions | 425 } // namespace extensions |
| OLD | NEW |