OLD | NEW |
| (Empty) |
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 | |
3 // found in the LICENSE file. | |
4 | |
5 #include <memory> | |
6 #include <string> | |
7 | |
8 #include "base/bind.h" | |
9 #include "base/callback.h" | |
10 #include "base/command_line.h" | |
11 #include "base/json/json_reader.h" | |
12 #include "base/memory/ptr_util.h" | |
13 #include "base/run_loop.h" | |
14 #include "base/values.h" | |
15 #include "chrome/browser/chromeos/arc/arc_settings_service.h" | |
16 #include "chrome/browser/chromeos/net/proxy_config_handler.h" | |
17 #include "chrome/browser/profiles/profile.h" | |
18 #include "chrome/browser/ui/browser.h" | |
19 #include "chrome/test/base/in_process_browser_test.h" | |
20 #include "chromeos/dbus/dbus_thread_manager.h" | |
21 #include "chromeos/dbus/shill_profile_client.h" | |
22 #include "chromeos/dbus/shill_service_client.h" | |
23 #include "chromeos/network/network_handler.h" | |
24 #include "chromeos/network/network_state.h" | |
25 #include "chromeos/network/network_state_handler.h" | |
26 #include "components/arc/arc_service_manager.h" | |
27 #include "components/arc/test/fake_arc_bridge_service.h" | |
28 #include "components/arc/test/fake_intent_helper_instance.h" | |
29 #include "components/policy/core/browser/browser_policy_connector.h" | |
30 #include "components/policy/core/common/mock_configuration_policy_provider.h" | |
31 #include "components/policy/core/common/policy_map.h" | |
32 #include "components/policy/core/common/policy_types.h" | |
33 #include "components/policy/policy_constants.h" | |
34 #include "components/prefs/pref_service.h" | |
35 #include "components/proxy_config/proxy_config_dictionary.h" | |
36 #include "components/proxy_config/proxy_config_pref_names.h" | |
37 #include "components/proxy_config/proxy_prefs.h" | |
38 #include "testing/gtest/include/gtest/gtest.h" | |
39 #include "third_party/cros_system_api/dbus/service_constants.h" | |
40 | |
41 using testing::_; | |
42 using testing::Return; | |
43 | |
44 namespace arc { | |
45 | |
46 namespace { | |
47 constexpr char kONCPolicy[] = | |
48 "{ \"NetworkConfigurations\": [" | |
49 " { \"GUID\": \"stub_ethernet_guid\"," | |
50 " \"Type\": \"Ethernet\"," | |
51 " \"Name\": \"My Ethernet\"," | |
52 " \"Ethernet\": {" | |
53 " \"Authentication\": \"None\" }," | |
54 " \"ProxySettings\": {" | |
55 " \"PAC\": \"http://domain.com/x\"," | |
56 " \"Type\": \"PAC\" }" | |
57 " }" | |
58 " ]," | |
59 " \"Type\": \"UnencryptedConfiguration\"" | |
60 "}"; | |
61 constexpr char kONCPacUrl[] = "http://domain.com/x"; | |
62 constexpr char kDefaultServicePath[] = "stub_ethernet"; | |
63 constexpr char kWifiServicePath[] = "stub_wifi"; | |
64 constexpr char kUserProfilePath[] = "user_profile"; | |
65 | |
66 // Returns an amount of |broadcasts| matched with |proxy_settings|. | |
67 int CountProxyBroadcasts( | |
68 const std::vector<FakeIntentHelperInstance::Broadcast>& broadcasts, | |
69 const base::DictionaryValue* proxy_settings) { | |
70 size_t count = 0; | |
71 for (const FakeIntentHelperInstance::Broadcast& broadcast : broadcasts) { | |
72 if (broadcast.action == "org.chromium.arc.intent_helper.SET_PROXY") { | |
73 EXPECT_TRUE( | |
74 base::JSONReader::Read(broadcast.extras)->Equals(proxy_settings)); | |
75 count++; | |
76 } | |
77 } | |
78 return count; | |
79 } | |
80 | |
81 void RunUntilIdle() { | |
82 DCHECK(base::MessageLoop::current()); | |
83 base::RunLoop loop; | |
84 loop.RunUntilIdle(); | |
85 } | |
86 | |
87 } // namespace | |
88 | |
89 class ArcSettingsServiceTest : public InProcessBrowserTest { | |
90 public: | |
91 ArcSettingsServiceTest() = default; | |
92 | |
93 // InProcessBrowserTest: | |
94 ~ArcSettingsServiceTest() override = default; | |
95 | |
96 void SetUpInProcessBrowserTestFixture() override { | |
97 EXPECT_CALL(provider_, IsInitializationComplete(_)) | |
98 .WillRepeatedly(Return(true)); | |
99 policy::BrowserPolicyConnector::SetPolicyProviderForTesting(&provider_); | |
100 fake_intent_helper_instance_.reset(new FakeIntentHelperInstance()); | |
101 | |
102 ArcServiceManager::SetArcBridgeServiceForTesting( | |
103 base::MakeUnique<FakeArcBridgeService>()); | |
104 } | |
105 | |
106 void SetUpOnMainThread() override { | |
107 SetupNetworkEnvironment(); | |
108 RunUntilIdle(); | |
109 | |
110 ArcBridgeService::Get()->intent_helper()->SetInstance( | |
111 fake_intent_helper_instance_.get()); | |
112 } | |
113 | |
114 void TearDownOnMainThread() override { | |
115 ArcBridgeService::Get()->intent_helper()->SetInstance(nullptr); | |
116 } | |
117 | |
118 void UpdatePolicy(const policy::PolicyMap& policy) { | |
119 provider_.UpdateChromePolicy(policy); | |
120 RunUntilIdle(); | |
121 } | |
122 | |
123 protected: | |
124 void DisconnectNetworkService(const std::string& service_path) { | |
125 chromeos::ShillServiceClient::TestInterface* service_test = | |
126 chromeos::DBusThreadManager::Get() | |
127 ->GetShillServiceClient() | |
128 ->GetTestInterface(); | |
129 base::StringValue value(shill::kStateIdle); | |
130 service_test->SetServiceProperty(service_path, shill::kStateProperty, | |
131 value); | |
132 } | |
133 | |
134 void SetProxyConfigForNetworkService( | |
135 const std::string& service_path, | |
136 const base::DictionaryValue* proxy_config) { | |
137 ProxyConfigDictionary proxy_config_dict(proxy_config); | |
138 const chromeos::NetworkState* network = chromeos::NetworkHandler::Get() | |
139 ->network_state_handler() | |
140 ->GetNetworkState(service_path); | |
141 ASSERT_TRUE(network); | |
142 chromeos::proxy_config::SetProxyConfigForNetwork(proxy_config_dict, | |
143 *network); | |
144 } | |
145 | |
146 std::unique_ptr<FakeIntentHelperInstance> fake_intent_helper_instance_; | |
147 | |
148 private: | |
149 void SetupNetworkEnvironment() { | |
150 chromeos::ShillProfileClient::TestInterface* profile_test = | |
151 chromeos::DBusThreadManager::Get() | |
152 ->GetShillProfileClient() | |
153 ->GetTestInterface(); | |
154 chromeos::ShillServiceClient::TestInterface* service_test = | |
155 chromeos::DBusThreadManager::Get() | |
156 ->GetShillServiceClient() | |
157 ->GetTestInterface(); | |
158 | |
159 profile_test->AddProfile(kUserProfilePath, "user"); | |
160 | |
161 service_test->ClearServices(); | |
162 | |
163 service_test->AddService(kDefaultServicePath, "stub_ethernet_guid", "eth0", | |
164 shill::kTypeEthernet, shill::kStateOnline, | |
165 true /* add_to_visible */); | |
166 service_test->SetServiceProperty(kDefaultServicePath, | |
167 shill::kProfileProperty, | |
168 base::StringValue(kUserProfilePath)); | |
169 | |
170 service_test->AddService(kWifiServicePath, "stub_wifi_guid", "wifi0", | |
171 shill::kTypeWifi, shill::kStateOnline, | |
172 true /* add_to_visible */); | |
173 service_test->SetServiceProperty(kWifiServicePath, shill::kProfileProperty, | |
174 base::StringValue(kUserProfilePath)); | |
175 } | |
176 | |
177 policy::MockConfigurationPolicyProvider provider_; | |
178 | |
179 DISALLOW_COPY_AND_ASSIGN(ArcSettingsServiceTest); | |
180 }; | |
181 | |
182 IN_PROC_BROWSER_TEST_F(ArcSettingsServiceTest, ProxyModePolicyTest) { | |
183 fake_intent_helper_instance_->clear_broadcasts(); | |
184 | |
185 policy::PolicyMap policy; | |
186 policy.Set( | |
187 policy::key::kProxyMode, policy::POLICY_LEVEL_MANDATORY, | |
188 policy::POLICY_SCOPE_USER, policy::POLICY_SOURCE_CLOUD, | |
189 base::MakeUnique<base::StringValue>(ProxyPrefs::kAutoDetectProxyModeName), | |
190 nullptr); | |
191 UpdatePolicy(policy); | |
192 | |
193 std::unique_ptr<base::DictionaryValue> expected_proxy_config( | |
194 base::MakeUnique<base::DictionaryValue>()); | |
195 expected_proxy_config->SetString("mode", | |
196 ProxyPrefs::kAutoDetectProxyModeName); | |
197 expected_proxy_config->SetString("pacUrl", "http://wpad/wpad.dat"); | |
198 EXPECT_EQ(CountProxyBroadcasts(fake_intent_helper_instance_->broadcasts(), | |
199 expected_proxy_config.get()), | |
200 1); | |
201 } | |
202 | |
203 IN_PROC_BROWSER_TEST_F(ArcSettingsServiceTest, ONCProxyPolicyTest) { | |
204 fake_intent_helper_instance_->clear_broadcasts(); | |
205 | |
206 policy::PolicyMap policy; | |
207 policy.Set(policy::key::kOpenNetworkConfiguration, | |
208 policy::POLICY_LEVEL_MANDATORY, policy::POLICY_SCOPE_USER, | |
209 policy::POLICY_SOURCE_CLOUD, | |
210 base::MakeUnique<base::StringValue>(kONCPolicy), nullptr); | |
211 UpdatePolicy(policy); | |
212 | |
213 std::unique_ptr<base::DictionaryValue> expected_proxy_config( | |
214 base::MakeUnique<base::DictionaryValue>()); | |
215 expected_proxy_config->SetString("mode", ProxyPrefs::kPacScriptProxyModeName); | |
216 expected_proxy_config->SetString("pacUrl", kONCPacUrl); | |
217 | |
218 EXPECT_EQ(CountProxyBroadcasts(fake_intent_helper_instance_->broadcasts(), | |
219 expected_proxy_config.get()), | |
220 1); | |
221 } | |
222 | |
223 // Proxy policy has a higher priority than proxy default settings. | |
224 IN_PROC_BROWSER_TEST_F(ArcSettingsServiceTest, TwoSourcesTest) { | |
225 fake_intent_helper_instance_->clear_broadcasts(); | |
226 | |
227 policy::PolicyMap policy; | |
228 // Proxy policy. | |
229 policy.Set(policy::key::kProxyMode, policy::POLICY_LEVEL_MANDATORY, | |
230 policy::POLICY_SCOPE_USER, policy::POLICY_SOURCE_CLOUD, | |
231 base::MakeUnique<base::StringValue>( | |
232 ProxyPrefs::kFixedServersProxyModeName), | |
233 nullptr); | |
234 policy.Set(policy::key::kProxyServer, policy::POLICY_LEVEL_MANDATORY, | |
235 policy::POLICY_SCOPE_USER, policy::POLICY_SOURCE_CLOUD, | |
236 base::MakeUnique<base::StringValue>("proxy:8888"), nullptr); | |
237 UpdatePolicy(policy); | |
238 | |
239 std::unique_ptr<base::DictionaryValue> proxy_config( | |
240 base::MakeUnique<base::DictionaryValue>()); | |
241 proxy_config->SetString("mode", ProxyPrefs::kAutoDetectProxyModeName); | |
242 ProxyConfigDictionary proxy_config_dict(proxy_config.get()); | |
243 const chromeos::NetworkState* network = chromeos::NetworkHandler::Get() | |
244 ->network_state_handler() | |
245 ->DefaultNetwork(); | |
246 ASSERT_TRUE(network); | |
247 chromeos::proxy_config::SetProxyConfigForNetwork(proxy_config_dict, *network); | |
248 RunUntilIdle(); | |
249 | |
250 std::unique_ptr<base::DictionaryValue> expected_proxy_config( | |
251 base::MakeUnique<base::DictionaryValue>()); | |
252 expected_proxy_config->SetString("mode", | |
253 ProxyPrefs::kFixedServersProxyModeName); | |
254 expected_proxy_config->SetString("host", "proxy"); | |
255 expected_proxy_config->SetInteger("port", 8888); | |
256 EXPECT_EQ(CountProxyBroadcasts(fake_intent_helper_instance_->broadcasts(), | |
257 expected_proxy_config.get()), | |
258 1); | |
259 } | |
260 | |
261 IN_PROC_BROWSER_TEST_F(ArcSettingsServiceTest, ProxyPrefTest) { | |
262 fake_intent_helper_instance_->clear_broadcasts(); | |
263 | |
264 std::unique_ptr<base::DictionaryValue> proxy_config( | |
265 base::MakeUnique<base::DictionaryValue>()); | |
266 proxy_config->SetString("mode", ProxyPrefs::kPacScriptProxyModeName); | |
267 proxy_config->SetString("pac_url", "http://proxy"); | |
268 browser()->profile()->GetPrefs()->Set(proxy_config::prefs::kProxy, | |
269 *proxy_config.get()); | |
270 RunUntilIdle(); | |
271 | |
272 std::unique_ptr<base::DictionaryValue> expected_proxy_config( | |
273 base::MakeUnique<base::DictionaryValue>()); | |
274 expected_proxy_config->SetString("mode", ProxyPrefs::kPacScriptProxyModeName); | |
275 expected_proxy_config->SetString("pacUrl", "http://proxy"); | |
276 EXPECT_EQ(CountProxyBroadcasts(fake_intent_helper_instance_->broadcasts(), | |
277 expected_proxy_config.get()), | |
278 1); | |
279 } | |
280 | |
281 IN_PROC_BROWSER_TEST_F(ArcSettingsServiceTest, DefaultNetworkProxyConfigTest) { | |
282 fake_intent_helper_instance_->clear_broadcasts(); | |
283 | |
284 std::unique_ptr<base::DictionaryValue> proxy_config( | |
285 base::MakeUnique<base::DictionaryValue>()); | |
286 proxy_config->SetString("mode", ProxyPrefs::kFixedServersProxyModeName); | |
287 proxy_config->SetString("server", "proxy:8080"); | |
288 SetProxyConfigForNetworkService(kDefaultServicePath, proxy_config.get()); | |
289 RunUntilIdle(); | |
290 | |
291 std::unique_ptr<base::DictionaryValue> expected_proxy_config( | |
292 base::MakeUnique<base::DictionaryValue>()); | |
293 expected_proxy_config->SetString("mode", | |
294 ProxyPrefs::kFixedServersProxyModeName); | |
295 expected_proxy_config->SetString("host", "proxy"); | |
296 expected_proxy_config->SetInteger("port", 8080); | |
297 EXPECT_EQ(CountProxyBroadcasts(fake_intent_helper_instance_->broadcasts(), | |
298 expected_proxy_config.get()), | |
299 1); | |
300 } | |
301 | |
302 IN_PROC_BROWSER_TEST_F(ArcSettingsServiceTest, DefaultNetworkDisconnectedTest) { | |
303 fake_intent_helper_instance_->clear_broadcasts(); | |
304 // Set proxy confog for default network. | |
305 std::unique_ptr<base::DictionaryValue> default_proxy_config( | |
306 base::MakeUnique<base::DictionaryValue>()); | |
307 default_proxy_config->SetString("mode", | |
308 ProxyPrefs::kFixedServersProxyModeName); | |
309 default_proxy_config->SetString("server", "default/proxy:8080"); | |
310 SetProxyConfigForNetworkService(kDefaultServicePath, | |
311 default_proxy_config.get()); | |
312 RunUntilIdle(); | |
313 | |
314 // Set proxy confog for WI-FI network. | |
315 std::unique_ptr<base::DictionaryValue> wifi_proxy_config( | |
316 base::MakeUnique<base::DictionaryValue>()); | |
317 wifi_proxy_config->SetString("mode", ProxyPrefs::kFixedServersProxyModeName); | |
318 wifi_proxy_config->SetString("server", "wifi/proxy:8080"); | |
319 SetProxyConfigForNetworkService(kWifiServicePath, wifi_proxy_config.get()); | |
320 RunUntilIdle(); | |
321 | |
322 // Observe default network proxy config broadcast. | |
323 std::unique_ptr<base::DictionaryValue> expected_default_proxy_config( | |
324 base::MakeUnique<base::DictionaryValue>()); | |
325 expected_default_proxy_config->SetString( | |
326 "mode", ProxyPrefs::kFixedServersProxyModeName); | |
327 expected_default_proxy_config->SetString("host", "default/proxy"); | |
328 expected_default_proxy_config->SetInteger("port", 8080); | |
329 EXPECT_EQ(CountProxyBroadcasts(fake_intent_helper_instance_->broadcasts(), | |
330 expected_default_proxy_config.get()), | |
331 1); | |
332 | |
333 // Disconnect default network. | |
334 fake_intent_helper_instance_->clear_broadcasts(); | |
335 DisconnectNetworkService(kDefaultServicePath); | |
336 RunUntilIdle(); | |
337 | |
338 // Observe WI-FI network proxy config broadcast. | |
339 std::unique_ptr<base::DictionaryValue> expected_wifi_proxy_config( | |
340 base::MakeUnique<base::DictionaryValue>()); | |
341 expected_wifi_proxy_config->SetString("mode", | |
342 ProxyPrefs::kFixedServersProxyModeName); | |
343 expected_wifi_proxy_config->SetString("host", "wifi/proxy"); | |
344 expected_wifi_proxy_config->SetInteger("port", 8080); | |
345 | |
346 EXPECT_EQ(CountProxyBroadcasts(fake_intent_helper_instance_->broadcasts(), | |
347 expected_wifi_proxy_config.get()), | |
348 1); | |
349 } | |
350 | |
351 IN_PROC_BROWSER_TEST_F(ArcSettingsServiceTest, NoNetworkConnectedTest) { | |
352 // Disconnect all networks. | |
353 fake_intent_helper_instance_->clear_broadcasts(); | |
354 DisconnectNetworkService(kDefaultServicePath); | |
355 DisconnectNetworkService(kWifiServicePath); | |
356 RunUntilIdle(); | |
357 | |
358 EXPECT_EQ( | |
359 CountProxyBroadcasts(fake_intent_helper_instance_->broadcasts(), nullptr), | |
360 0); | |
361 } | |
362 | |
363 } // namespace arc | |
OLD | NEW |