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 "base/scoped_observer.h" | 5 #include "base/scoped_observer.h" |
6 #include "chrome/browser/extensions/extension_browsertest.h" | 6 #include "chrome/browser/extensions/extension_browsertest.h" |
7 #include "chrome/common/chrome_switches.h" | 7 #include "chrome/common/chrome_switches.h" |
8 #include "content/public/test/test_utils.h" | 8 #include "content/public/test/test_utils.h" |
9 #include "extensions/browser/content_verify_job.h" | 9 #include "extensions/browser/content_verify_job.h" |
10 #include "extensions/browser/extension_prefs.h" | 10 #include "extensions/browser/extension_prefs.h" |
11 #include "extensions/browser/extension_registry.h" | 11 #include "extensions/browser/extension_registry.h" |
12 #include "extensions/browser/extension_registry_observer.h" | 12 #include "extensions/browser/extension_registry_observer.h" |
13 | 13 |
14 namespace extensions { | 14 namespace extensions { |
15 | 15 |
16 namespace { | 16 namespace { |
17 | 17 |
18 // Helper for observing extension unloads. | 18 // Helper for observing extension unloads. |
19 class UnloadObserver : public ExtensionRegistryObserver { | 19 class UnloadObserver : public ExtensionRegistryObserver { |
20 public: | 20 public: |
21 explicit UnloadObserver(ExtensionRegistry* registry) : observer_(this) { | 21 explicit UnloadObserver(ExtensionRegistry* registry) : observer_(this) { |
22 observer_.Add(registry); | 22 observer_.Add(registry); |
23 } | 23 } |
24 virtual ~UnloadObserver() {} | 24 ~UnloadObserver() override {} |
25 | 25 |
26 void WaitForUnload(const ExtensionId& id) { | 26 void WaitForUnload(const ExtensionId& id) { |
27 if (ContainsKey(observed_, id)) | 27 if (ContainsKey(observed_, id)) |
28 return; | 28 return; |
29 | 29 |
30 ASSERT_TRUE(loop_runner_.get() == NULL); | 30 ASSERT_TRUE(loop_runner_.get() == NULL); |
31 awaited_id_ = id; | 31 awaited_id_ = id; |
32 loop_runner_ = new content::MessageLoopRunner(); | 32 loop_runner_ = new content::MessageLoopRunner(); |
33 loop_runner_->Run(); | 33 loop_runner_->Run(); |
34 } | 34 } |
35 | 35 |
36 virtual void OnExtensionUnloaded( | 36 void OnExtensionUnloaded(content::BrowserContext* browser_context, |
37 content::BrowserContext* browser_context, | 37 const Extension* extension, |
38 const Extension* extension, | 38 UnloadedExtensionInfo::Reason reason) override { |
39 UnloadedExtensionInfo::Reason reason) override { | |
40 observed_.insert(extension->id()); | 39 observed_.insert(extension->id()); |
41 if (awaited_id_ == extension->id()) | 40 if (awaited_id_ == extension->id()) |
42 loop_runner_->Quit(); | 41 loop_runner_->Quit(); |
43 } | 42 } |
44 | 43 |
45 private: | 44 private: |
46 ExtensionId awaited_id_; | 45 ExtensionId awaited_id_; |
47 std::set<ExtensionId> observed_; | 46 std::set<ExtensionId> observed_; |
48 scoped_refptr<content::MessageLoopRunner> loop_runner_; | 47 scoped_refptr<content::MessageLoopRunner> loop_runner_; |
49 ScopedObserver<ExtensionRegistry, UnloadObserver> observer_; | 48 ScopedObserver<ExtensionRegistry, UnloadObserver> observer_; |
50 }; | 49 }; |
51 | 50 |
52 // Helper for forcing ContentVerifyJob's to return an error. | 51 // Helper for forcing ContentVerifyJob's to return an error. |
53 class JobDelegate : public ContentVerifyJob::TestDelegate { | 52 class JobDelegate : public ContentVerifyJob::TestDelegate { |
54 public: | 53 public: |
55 JobDelegate() : fail_next_read_(false), fail_next_done_(false) {} | 54 JobDelegate() : fail_next_read_(false), fail_next_done_(false) {} |
56 | 55 |
57 virtual ~JobDelegate() {} | 56 virtual ~JobDelegate() {} |
58 | 57 |
59 void set_id(const ExtensionId& id) { id_ = id; } | 58 void set_id(const ExtensionId& id) { id_ = id; } |
60 void fail_next_read() { fail_next_read_ = true; } | 59 void fail_next_read() { fail_next_read_ = true; } |
61 void fail_next_done() { fail_next_done_ = true; } | 60 void fail_next_done() { fail_next_done_ = true; } |
62 | 61 |
63 virtual ContentVerifyJob::FailureReason BytesRead(const ExtensionId& id, | 62 ContentVerifyJob::FailureReason BytesRead(const ExtensionId& id, |
64 int count, | 63 int count, |
65 const char* data) override { | 64 const char* data) override { |
66 if (id == id_ && fail_next_read_) { | 65 if (id == id_ && fail_next_read_) { |
67 fail_next_read_ = false; | 66 fail_next_read_ = false; |
68 return ContentVerifyJob::HASH_MISMATCH; | 67 return ContentVerifyJob::HASH_MISMATCH; |
69 } | 68 } |
70 return ContentVerifyJob::NONE; | 69 return ContentVerifyJob::NONE; |
71 } | 70 } |
72 | 71 |
73 virtual ContentVerifyJob::FailureReason DoneReading( | 72 ContentVerifyJob::FailureReason DoneReading(const ExtensionId& id) override { |
74 const ExtensionId& id) override { | |
75 if (id == id_ && fail_next_done_) { | 73 if (id == id_ && fail_next_done_) { |
76 fail_next_done_ = false; | 74 fail_next_done_ = false; |
77 return ContentVerifyJob::HASH_MISMATCH; | 75 return ContentVerifyJob::HASH_MISMATCH; |
78 } | 76 } |
79 return ContentVerifyJob::NONE; | 77 return ContentVerifyJob::NONE; |
80 } | 78 } |
81 | 79 |
82 private: | 80 private: |
83 DISALLOW_COPY_AND_ASSIGN(JobDelegate); | 81 DISALLOW_COPY_AND_ASSIGN(JobDelegate); |
84 | 82 |
(...skipping 11 matching lines...) Expand all Loading... |
96 void ExpectJobResult(const std::string& extension_id, | 94 void ExpectJobResult(const std::string& extension_id, |
97 const base::FilePath& relative_path, | 95 const base::FilePath& relative_path, |
98 bool expected_to_fail); | 96 bool expected_to_fail); |
99 | 97 |
100 // Wait to see expected jobs. Returns true if we saw all jobs finish as | 98 // Wait to see expected jobs. Returns true if we saw all jobs finish as |
101 // expected, or false if any job completed with non-expected success/failure | 99 // expected, or false if any job completed with non-expected success/failure |
102 // status. | 100 // status. |
103 bool WaitForExpectedJobs(); | 101 bool WaitForExpectedJobs(); |
104 | 102 |
105 // ContentVerifyJob::TestObserver interface | 103 // ContentVerifyJob::TestObserver interface |
106 virtual void JobStarted(const std::string& extension_id, | 104 void JobStarted(const std::string& extension_id, |
107 const base::FilePath& relative_path) override; | 105 const base::FilePath& relative_path) override; |
108 | 106 |
109 virtual void JobFinished(const std::string& extension_id, | 107 void JobFinished(const std::string& extension_id, |
110 const base::FilePath& relative_path, | 108 const base::FilePath& relative_path, |
111 bool failed) override; | 109 bool failed) override; |
112 | 110 |
113 private: | 111 private: |
114 typedef std::pair<std::string, base::FilePath> ExtensionFile; | 112 typedef std::pair<std::string, base::FilePath> ExtensionFile; |
115 typedef std::map<ExtensionFile, bool> ExpectedJobs; | 113 typedef std::map<ExtensionFile, bool> ExpectedJobs; |
116 ExpectedJobs expected_jobs_; | 114 ExpectedJobs expected_jobs_; |
117 scoped_refptr<content::MessageLoopRunner> loop_runner_; | 115 scoped_refptr<content::MessageLoopRunner> loop_runner_; |
118 bool saw_expected_job_results_; | 116 bool saw_expected_job_results_; |
119 }; | 117 }; |
120 | 118 |
121 void JobObserver::ExpectJobResult(const std::string& extension_id, | 119 void JobObserver::ExpectJobResult(const std::string& extension_id, |
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
163 } | 161 } |
164 } | 162 } |
165 | 163 |
166 } // namespace | 164 } // namespace |
167 | 165 |
168 class ContentVerifierTest : public ExtensionBrowserTest { | 166 class ContentVerifierTest : public ExtensionBrowserTest { |
169 public: | 167 public: |
170 ContentVerifierTest() {} | 168 ContentVerifierTest() {} |
171 virtual ~ContentVerifierTest() {} | 169 virtual ~ContentVerifierTest() {} |
172 | 170 |
173 virtual void SetUpCommandLine(base::CommandLine* command_line) override { | 171 void SetUpCommandLine(base::CommandLine* command_line) override { |
174 ExtensionBrowserTest::SetUpCommandLine(command_line); | 172 ExtensionBrowserTest::SetUpCommandLine(command_line); |
175 command_line->AppendSwitchASCII( | 173 command_line->AppendSwitchASCII( |
176 switches::kExtensionContentVerification, | 174 switches::kExtensionContentVerification, |
177 switches::kExtensionContentVerificationEnforce); | 175 switches::kExtensionContentVerificationEnforce); |
178 } | 176 } |
179 | 177 |
180 // Setup our unload observer and JobDelegate, and install a test extension. | 178 // Setup our unload observer and JobDelegate, and install a test extension. |
181 virtual void SetUpOnMainThread() override { | 179 void SetUpOnMainThread() override { |
182 ExtensionBrowserTest::SetUpOnMainThread(); | 180 ExtensionBrowserTest::SetUpOnMainThread(); |
183 } | 181 } |
184 | 182 |
185 virtual void TearDownOnMainThread() override { | 183 void TearDownOnMainThread() override { |
186 ContentVerifyJob::SetDelegateForTests(NULL); | 184 ContentVerifyJob::SetDelegateForTests(NULL); |
187 ContentVerifyJob::SetObserverForTests(NULL); | 185 ContentVerifyJob::SetObserverForTests(NULL); |
188 ExtensionBrowserTest::TearDownOnMainThread(); | 186 ExtensionBrowserTest::TearDownOnMainThread(); |
189 } | 187 } |
190 | 188 |
191 virtual void OpenPageAndWaitForUnload() { | 189 virtual void OpenPageAndWaitForUnload() { |
192 unload_observer_.reset( | 190 unload_observer_.reset( |
193 new UnloadObserver(ExtensionRegistry::Get(profile()))); | 191 new UnloadObserver(ExtensionRegistry::Get(profile()))); |
194 const Extension* extension = InstallExtensionFromWebstore( | 192 const Extension* extension = InstallExtensionFromWebstore( |
195 test_data_dir_.AppendASCII("content_verifier/v1.crx"), 1); | 193 test_data_dir_.AppendASCII("content_verifier/v1.crx"), 1); |
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
249 | 247 |
250 ASSERT_TRUE(extension); | 248 ASSERT_TRUE(extension); |
251 ASSERT_EQ(extension->id(), id); | 249 ASSERT_EQ(extension->id(), id); |
252 | 250 |
253 EXPECT_TRUE(job_observer.WaitForExpectedJobs()); | 251 EXPECT_TRUE(job_observer.WaitForExpectedJobs()); |
254 | 252 |
255 ContentVerifyJob::SetObserverForTests(NULL); | 253 ContentVerifyJob::SetObserverForTests(NULL); |
256 } | 254 } |
257 | 255 |
258 } // namespace extensions | 256 } // namespace extensions |
OLD | NEW |