OLD | NEW |
| (Empty) |
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 | |
3 // found in the LICENSE file. | |
4 | |
5 #include "chrome/browser/supervised_user/supervised_user_resource_throttle.h" | |
6 | |
7 #include <memory> | |
8 | |
9 #include "base/command_line.h" | |
10 #include "base/files/file_path.h" | |
11 #include "base/memory/ptr_util.h" | |
12 #include "base/path_service.h" | |
13 #include "base/values.h" | |
14 #include "chrome/browser/profiles/profile.h" | |
15 #include "chrome/browser/supervised_user/supervised_user_constants.h" | |
16 #include "chrome/browser/supervised_user/supervised_user_service.h" | |
17 #include "chrome/browser/supervised_user/supervised_user_service_factory.h" | |
18 #include "chrome/browser/supervised_user/supervised_user_settings_service.h" | |
19 #include "chrome/browser/supervised_user/supervised_user_settings_service_factor
y.h" | |
20 #include "chrome/browser/ui/browser.h" | |
21 #include "chrome/browser/ui/tabs/tab_strip_model.h" | |
22 #include "chrome/common/chrome_switches.h" | |
23 #include "chrome/test/base/in_process_browser_test.h" | |
24 #include "chrome/test/base/ui_test_utils.h" | |
25 #include "content/public/browser/navigation_entry.h" | |
26 #include "content/public/browser/web_contents.h" | |
27 #include "content/public/common/content_paths.h" | |
28 #include "content/public/common/page_type.h" | |
29 #include "content/public/test/browser_test_utils.h" | |
30 #include "content/public/test/test_navigation_observer.h" | |
31 #include "net/dns/mock_host_resolver.h" | |
32 #include "net/test/embedded_test_server/embedded_test_server.h" | |
33 | |
34 using content::NavigationController; | |
35 using content::WebContents; | |
36 | |
37 namespace { | |
38 | |
39 static const char* kExampleHost = "www.example.com"; | |
40 static const char* kExampleHost2 = "www.example2.com"; | |
41 | |
42 static const char* kIframeHost2 = "www.iframe2.com"; | |
43 | |
44 } // namespace | |
45 | |
46 class SupervisedUserResourceThrottleTest : public InProcessBrowserTest { | |
47 protected: | |
48 SupervisedUserResourceThrottleTest() {} | |
49 ~SupervisedUserResourceThrottleTest() override {} | |
50 | |
51 void BlockHost(const std::string& host) { | |
52 Profile* profile = browser()->profile(); | |
53 SupervisedUserSettingsService* settings_service = | |
54 SupervisedUserSettingsServiceFactory::GetForProfile(profile); | |
55 auto dict = base::MakeUnique<base::DictionaryValue>(); | |
56 dict->SetBooleanWithoutPathExpansion(host, false); | |
57 settings_service->SetLocalSetting( | |
58 supervised_users::kContentPackManualBehaviorHosts, std::move(dict)); | |
59 } | |
60 | |
61 private: | |
62 void SetUpOnMainThread() override; | |
63 void SetUpCommandLine(base::CommandLine* command_line) override; | |
64 }; | |
65 | |
66 void SupervisedUserResourceThrottleTest::SetUpOnMainThread() { | |
67 // Resolve everything to localhost. | |
68 host_resolver()->AddIPLiteralRule("*", "127.0.0.1", "localhost"); | |
69 | |
70 ASSERT_TRUE(embedded_test_server()->Start()); | |
71 } | |
72 | |
73 void SupervisedUserResourceThrottleTest::SetUpCommandLine( | |
74 base::CommandLine* command_line) { | |
75 command_line->AppendSwitchASCII(switches::kSupervisedUserId, "asdf"); | |
76 } | |
77 | |
78 // Tests that showing the blocking interstitial for a WebContents without a | |
79 // SupervisedUserNavigationObserver doesn't crash. | |
80 IN_PROC_BROWSER_TEST_F(SupervisedUserResourceThrottleTest, | |
81 NoNavigationObserverBlock) { | |
82 Profile* profile = browser()->profile(); | |
83 SupervisedUserSettingsService* supervised_user_settings_service = | |
84 SupervisedUserSettingsServiceFactory::GetForProfile(profile); | |
85 supervised_user_settings_service->SetLocalSetting( | |
86 supervised_users::kContentPackDefaultFilteringBehavior, | |
87 std::unique_ptr<base::Value>( | |
88 new base::Value(SupervisedUserURLFilter::BLOCK))); | |
89 | |
90 std::unique_ptr<WebContents> web_contents( | |
91 WebContents::Create(WebContents::CreateParams(profile))); | |
92 NavigationController& controller = web_contents->GetController(); | |
93 content::TestNavigationObserver observer(web_contents.get()); | |
94 controller.LoadURL(GURL("http://www.example.com"), content::Referrer(), | |
95 ui::PAGE_TRANSITION_TYPED, std::string()); | |
96 observer.Wait(); | |
97 content::NavigationEntry* entry = controller.GetActiveEntry(); | |
98 ASSERT_TRUE(entry); | |
99 EXPECT_EQ(content::PAGE_TYPE_INTERSTITIAL, entry->GetPageType()); | |
100 } | |
101 | |
102 IN_PROC_BROWSER_TEST_F(SupervisedUserResourceThrottleTest, | |
103 BlockMainFrameWithInterstitial) { | |
104 BlockHost(kExampleHost2); | |
105 | |
106 WebContents* tab = browser()->tab_strip_model()->GetActiveWebContents(); | |
107 | |
108 GURL allowed_url = embedded_test_server()->GetURL( | |
109 kExampleHost, "/supervised_user/simple.html"); | |
110 ui_test_utils::NavigateToURL(browser(), allowed_url); | |
111 EXPECT_FALSE(tab->ShowingInterstitialPage()); | |
112 | |
113 GURL blocked_url = embedded_test_server()->GetURL( | |
114 kExampleHost2, "/supervised_user/simple.html"); | |
115 ui_test_utils::NavigateToURL(browser(), blocked_url); | |
116 EXPECT_TRUE(tab->ShowingInterstitialPage()); | |
117 } | |
118 | |
119 IN_PROC_BROWSER_TEST_F(SupervisedUserResourceThrottleTest, DontBlockSubFrame) { | |
120 BlockHost(kExampleHost2); | |
121 BlockHost(kIframeHost2); | |
122 | |
123 WebContents* tab = browser()->tab_strip_model()->GetActiveWebContents(); | |
124 | |
125 GURL allowed_url_with_iframes = embedded_test_server()->GetURL( | |
126 kExampleHost, "/supervised_user/with_iframes.html"); | |
127 ui_test_utils::NavigateToURL(browser(), allowed_url_with_iframes); | |
128 EXPECT_FALSE(tab->ShowingInterstitialPage()); | |
129 | |
130 // Both iframes (from allowed host iframe1.com as well as from blocked host | |
131 // iframe2.com) should be loaded normally, since we don't filter iframes | |
132 // (yet) - see crbug.com/651115. | |
133 bool loaded1 = false; | |
134 ASSERT_TRUE(content::ExecuteScriptAndExtractBool(tab, "loaded1()", &loaded1)); | |
135 EXPECT_TRUE(loaded1); | |
136 bool loaded2 = false; | |
137 ASSERT_TRUE(content::ExecuteScriptAndExtractBool(tab, "loaded2()", &loaded2)); | |
138 EXPECT_TRUE(loaded2); | |
139 } | |
OLD | NEW |