Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(28)

Side by Side Diff: chrome/browser/extensions/extension_navigation_throttle_unittest.cc

Issue 2830893002: Refactor of ExtensionNavigationThrottle (Closed)
Patch Set: \ Created 3 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 "base/memory/ptr_util.h" 5 #include "base/memory/ptr_util.h"
6 #include "base/strings/stringprintf.h" 6 #include "base/strings/stringprintf.h"
7 #include "chrome/test/base/chrome_render_view_host_test_harness.h" 7 #include "chrome/test/base/chrome_render_view_host_test_harness.h"
8 #include "components/crx_file/id_util.h" 8 #include "components/crx_file/id_util.h"
9 #include "content/public/browser/content_browser_client.h" 9 #include "content/public/browser/content_browser_client.h"
10 #include "content/public/browser/navigation_handle.h" 10 #include "content/public/browser/navigation_handle.h"
(...skipping 22 matching lines...) Expand all
33 class MockBrowserClient : public content::ContentBrowserClient { 33 class MockBrowserClient : public content::ContentBrowserClient {
34 public: 34 public:
35 MockBrowserClient() {} 35 MockBrowserClient() {}
36 ~MockBrowserClient() override {} 36 ~MockBrowserClient() override {}
37 37
38 // Only construct an ExtensionNavigationThrottle so that we can test it in 38 // Only construct an ExtensionNavigationThrottle so that we can test it in
39 // isolation. 39 // isolation.
40 std::vector<std::unique_ptr<NavigationThrottle>> CreateThrottlesForNavigation( 40 std::vector<std::unique_ptr<NavigationThrottle>> CreateThrottlesForNavigation(
41 content::NavigationHandle* handle) override { 41 content::NavigationHandle* handle) override {
42 std::vector<std::unique_ptr<NavigationThrottle>> throttles; 42 std::vector<std::unique_ptr<NavigationThrottle>> throttles;
43 if (!handle->IsInMainFrame()) { // Mirrors ChromeContentBrowserClient. 43 throttles.push_back(base::MakeUnique<ExtensionNavigationThrottle>(handle));
44 throttles.push_back(
45 base::MakeUnique<ExtensionNavigationThrottle>(handle));
46 }
47 return throttles; 44 return throttles;
48 } 45 }
49 }; 46 };
50 47
51 } // namespace 48 } // namespace
52 49
53 class ExtensionNavigationThrottleUnitTest 50 class ExtensionNavigationThrottleUnitTest
54 : public ChromeRenderViewHostTestHarness { 51 : public ChromeRenderViewHostTestHarness {
55 public: 52 public:
56 ExtensionNavigationThrottleUnitTest() {} 53 ExtensionNavigationThrottleUnitTest() {}
(...skipping 106 matching lines...) Expand 10 before | Expand all | Expand 10 after
163 CheckTestCase(grand_child, extension()->GetResourceURL(kPrivate), 160 CheckTestCase(grand_child, extension()->GetResourceURL(kPrivate),
164 NavigationThrottle::BLOCK_REQUEST); 161 NavigationThrottle::BLOCK_REQUEST);
165 CheckTestCase(grand_child, extension()->GetResourceURL(kAccessible), 162 CheckTestCase(grand_child, extension()->GetResourceURL(kAccessible),
166 NavigationThrottle::PROCEED); 163 NavigationThrottle::PROCEED);
167 CheckTestCase(grand_child, 164 CheckTestCase(grand_child,
168 extension()->GetResourceURL(kAccessibleDirResource), 165 extension()->GetResourceURL(kAccessibleDirResource),
169 NavigationThrottle::PROCEED); 166 NavigationThrottle::PROCEED);
170 } 167 }
171 168
172 // Tests that requests to disabled or non-existent extensions are blocked. 169 // Tests that requests to disabled or non-existent extensions are blocked.
173 TEST_F(ExtensionNavigationThrottleUnitTest, InvalidExtension) { 170 TEST_F(ExtensionNavigationThrottleUnitTest, DisabledExtensionChildFrame) {
174 web_contents_tester()->NavigateAndCommit(GURL("http://example.com")); 171 web_contents_tester()->NavigateAndCommit(GURL("http://example.com"));
175 content::RenderFrameHost* child = 172 content::RenderFrameHost* child =
176 render_frame_host_tester(main_rfh())->AppendChild("child"); 173 render_frame_host_tester(main_rfh())->AppendChild("child");
177 174
178 ExtensionRegistry* registry = ExtensionRegistry::Get(browser_context()); 175 ExtensionRegistry* registry = ExtensionRegistry::Get(browser_context());
179 registry->RemoveEnabled(extension()->id()); 176 registry->RemoveEnabled(extension()->id());
180 registry->AddDisabled(extension()); 177 registry->AddDisabled(extension());
181 178
182 // Since the extension is disabled, all requests should be blocked. 179 // Since the extension is disabled, all requests should be blocked.
183 CheckTestCase(child, extension()->GetResourceURL(kPrivate), 180 CheckTestCase(child, extension()->GetResourceURL(kPrivate),
184 NavigationThrottle::BLOCK_REQUEST); 181 NavigationThrottle::BLOCK_REQUEST);
185 CheckTestCase(child, extension()->GetResourceURL(kAccessible), 182 CheckTestCase(child, extension()->GetResourceURL(kAccessible),
186 NavigationThrottle::BLOCK_REQUEST); 183 NavigationThrottle::BLOCK_REQUEST);
187 CheckTestCase(child, extension()->GetResourceURL(kAccessibleDirResource), 184 CheckTestCase(child, extension()->GetResourceURL(kAccessibleDirResource),
188 NavigationThrottle::BLOCK_REQUEST); 185 NavigationThrottle::BLOCK_REQUEST);
189 186
190 std::string second_id = crx_file::id_util::GenerateId("bar"); 187 std::string second_id = crx_file::id_util::GenerateId("bar");
191 ASSERT_NE(second_id, extension()->id()); 188 ASSERT_NE(second_id, extension()->id());
192 GURL invalid_url(base::StringPrintf("chrome-extension://%s/accessible.html", 189 GURL unknown_url(base::StringPrintf("chrome-extension://%s/accessible.html",
193 second_id.c_str())); 190 second_id.c_str()));
194 // Requests to non-existent extensions should be blocked. 191 // Requests to non-existent extensions should be blocked.
195 CheckTestCase(child, invalid_url, NavigationThrottle::BLOCK_REQUEST); 192 CheckTestCase(child, unknown_url, NavigationThrottle::BLOCK_REQUEST);
193
194 // Test blob and filesystem URLs with disabled/unknown extensions.
195 GURL disabled_blob(base::StringPrintf("blob:chrome-extension://%s/SOMEGUID",
196 extension()->id().c_str()));
197 GURL unknown_blob(base::StringPrintf("blob:chrome-extension://%s/SOMEGUID",
198 second_id.c_str()));
199 CheckTestCase(child, disabled_blob, NavigationThrottle::BLOCK_REQUEST);
200 CheckTestCase(child, unknown_blob, NavigationThrottle::BLOCK_REQUEST);
201 GURL disabled_filesystem(
202 base::StringPrintf("filesystem:chrome-extension://%s/temporary/foo.html",
203 extension()->id().c_str()));
204 GURL unknown_filesystem(
205 base::StringPrintf("filesystem:chrome-extension://%s/temporary/foo.html",
206 second_id.c_str()));
207 CheckTestCase(child, disabled_filesystem, NavigationThrottle::BLOCK_REQUEST);
208 CheckTestCase(child, unknown_filesystem, NavigationThrottle::BLOCK_REQUEST);
209 }
210
211 // Tests that requests to disabled or non-existent extensions are blocked.
212 TEST_F(ExtensionNavigationThrottleUnitTest, DisabledExtensionMainFrame) {
213 web_contents_tester()->NavigateAndCommit(GURL("http://example.com"));
214
215 ExtensionRegistry* registry = ExtensionRegistry::Get(browser_context());
216 registry->RemoveEnabled(extension()->id());
217 registry->AddDisabled(extension());
218
219 // Since the extension is disabled, all requests should be blocked.
220 CheckTestCase(main_rfh(), extension()->GetResourceURL(kPrivate),
221 NavigationThrottle::BLOCK_REQUEST);
222 CheckTestCase(main_rfh(), extension()->GetResourceURL(kAccessible),
223 NavigationThrottle::BLOCK_REQUEST);
224 CheckTestCase(main_rfh(), extension()->GetResourceURL(kAccessibleDirResource),
225 NavigationThrottle::BLOCK_REQUEST);
226
227 std::string second_id = crx_file::id_util::GenerateId("bar");
228
229 ASSERT_NE(second_id, extension()->id());
230 GURL unknown_url(base::StringPrintf("chrome-extension://%s/accessible.html",
231 second_id.c_str()));
232 // Requests to non-existent extensions should be blocked.
233 CheckTestCase(main_rfh(), unknown_url, NavigationThrottle::BLOCK_REQUEST);
234
235 // Test blob and filesystem URLs with disabled/unknown extensions.
236 GURL disabled_blob(base::StringPrintf("blob:chrome-extension://%s/SOMEGUID",
237 extension()->id().c_str()));
238 GURL unknown_blob(base::StringPrintf("blob:chrome-extension://%s/SOMEGUID",
239 second_id.c_str()));
240 CheckTestCase(main_rfh(), disabled_blob, NavigationThrottle::BLOCK_REQUEST);
241 CheckTestCase(main_rfh(), unknown_blob, NavigationThrottle::BLOCK_REQUEST);
242 GURL disabled_filesystem(
243 base::StringPrintf("filesystem:chrome-extension://%s/temporary/foo.html",
244 extension()->id().c_str()));
245 GURL unknown_filesystem(
246 base::StringPrintf("filesystem:chrome-extension://%s/temporary/foo.html",
247 second_id.c_str()));
248 CheckTestCase(main_rfh(), disabled_filesystem,
249 NavigationThrottle::BLOCK_REQUEST);
250 CheckTestCase(main_rfh(), unknown_filesystem,
251 NavigationThrottle::BLOCK_REQUEST);
196 } 252 }
197 253
198 } // namespace extensions 254 } // namespace extensions
OLDNEW
« no previous file with comments | « chrome/browser/extensions/extension_browsertest.cc ('k') | chrome/browser/extensions/process_manager_browsertest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698