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

Side by Side Diff: chrome/browser/chromeos/arc/arc_settings_service_browsertest.cc

Issue 2306673002: arc: update proxy settings from UI and ONC policy. (Closed)
Patch Set: Removed a log line. Created 4 years, 3 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
(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/profiles/profile.h"
17 #include "chrome/browser/ui/browser.h"
18 #include "chrome/test/base/in_process_browser_test.h"
19 #include "chromeos/dbus/dbus_thread_manager.h"
20 #include "chromeos/dbus/shill_profile_client.h"
21 #include "chromeos/dbus/shill_service_client.h"
22 #include "chromeos/network/network_handler.h"
23 #include "chromeos/network/network_state.h"
24 #include "chromeos/network/network_state_handler.h"
25 #include "components/arc/arc_service_manager.h"
26 #include "components/arc/test/fake_arc_bridge_service.h"
27 #include "components/arc/test/fake_intent_helper_instance.h"
28 #include "components/policy/core/browser/browser_policy_connector.h"
29 #include "components/policy/core/common/mock_configuration_policy_provider.h"
30 #include "components/policy/core/common/policy_map.h"
31 #include "components/policy/core/common/policy_types.h"
32 #include "components/policy/policy_constants.h"
33 #include "components/prefs/pref_service.h"
34 #include "components/proxy_config/proxy_config_dictionary.h"
35 #include "components/proxy_config/proxy_config_pref_names.h"
36 #include "components/proxy_config/proxy_prefs.h"
37 #include "testing/gtest/include/gtest/gtest.h"
38 #include "third_party/cros_system_api/dbus/service_constants.h"
39
40 using testing::_;
41 using testing::Return;
42
43 namespace arc {
44
45 namespace {
46 // char kCmdProxyServer[] = "proxy:8888";
Luis Héctor Chávez 2016/09/06 16:16:25 nit: don't check in commented code.
Polina Bondarenko 2016/09/06 19:38:46 Done.
47 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 char kONCPacUrl[] = "http://domain.com/x";
62 const char* kUserProfilePath = "user_profile";
63
64 // Retruns an amount of |broadcasts| matched with |proxy_settings|.
stevenjb 2016/09/06 15:49:06 Returns
Polina Bondarenko 2016/09/06 19:38:46 Done.
65 int CountProxyBroadcasts(
66 const std::vector<FakeIntentHelperInstance::Broadcast>& broadcasts,
67 const base::DictionaryValue* proxy_settings) {
68 size_t count = 0;
69 for (const FakeIntentHelperInstance::Broadcast& broadcast : broadcasts) {
70 if (broadcast.action == "org.chromium.arc.intent_helper.SET_PROXY") {
71 EXPECT_TRUE(
72 base::JSONReader::Read(broadcast.extras)->Equals(proxy_settings));
73 count++;
74 }
75 }
76 return count;
77 }
78
79 } // namespace
80
81 class ArcSettingsServiceTest : public InProcessBrowserTest {
82 public:
83 ArcSettingsServiceTest() {}
Luis Héctor Chávez 2016/09/06 16:16:25 nit: can you use "= default" for both this and the
Polina Bondarenko 2016/09/06 19:38:46 Done.
84
85 // InProcessBrowserTest:
86 ~ArcSettingsServiceTest() override {}
87
88 void SetUpInProcessBrowserTestFixture() override {
89 EXPECT_CALL(provider_, IsInitializationComplete(_))
90 .WillRepeatedly(Return(true));
91 policy::BrowserPolicyConnector::SetPolicyProviderForTesting(&provider_);
92 fake_intent_helper_instance_.reset(new FakeIntentHelperInstance());
93
94 ArcServiceManager::SetArcBridgeServiceForTesting(
95 base::MakeUnique<FakeArcBridgeService>());
96 }
97
98 void SetUpOnMainThread() override {
99 SetupNetworkEnvironment();
100 DCHECK(base::MessageLoop::current());
101 base::RunLoop loop;
102 loop.RunUntilIdle();
103
104 ArcBridgeService::Get()->intent_helper()->SetInstance(
105 fake_intent_helper_instance_.get());
106 }
107
108 void TearDownOnMainThread() override {
109 ArcBridgeService::Get()->intent_helper()->SetInstance(nullptr);
110 }
111
112 void UpdatePolicy(const policy::PolicyMap& policy) {
113 provider_.UpdateChromePolicy(policy);
114 DCHECK(base::MessageLoop::current());
115 base::RunLoop loop;
116 loop.RunUntilIdle();
117 }
118
119 protected:
120 std::unique_ptr<FakeIntentHelperInstance> fake_intent_helper_instance_;
121
122 private:
123 void SetupNetworkEnvironment() {
124 chromeos::ShillProfileClient::TestInterface* profile_test =
125 chromeos::DBusThreadManager::Get()
126 ->GetShillProfileClient()
127 ->GetTestInterface();
128 chromeos::ShillServiceClient::TestInterface* service_test =
129 chromeos::DBusThreadManager::Get()
130 ->GetShillServiceClient()
131 ->GetTestInterface();
132
133 profile_test->AddProfile(kUserProfilePath, "user");
134
135 service_test->ClearServices();
136 service_test->AddService("stub_ethernet", "stub_ethernet_guid", "eth0",
137 shill::kTypeEthernet, shill::kStateOnline,
138 true /* add_to_visible */);
139 service_test->SetServiceProperty("stub_ethernet", shill::kProfileProperty,
140 base::StringValue(kUserProfilePath));
141 }
142
143 policy::MockConfigurationPolicyProvider provider_;
144
145 DISALLOW_COPY_AND_ASSIGN(ArcSettingsServiceTest);
146 };
147
148 IN_PROC_BROWSER_TEST_F(ArcSettingsServiceTest, ProxyModePolicyTest) {
149 fake_intent_helper_instance_->clear_broadcasts();
150
151 policy::PolicyMap policy;
152 policy.Set(
153 policy::key::kProxyMode, policy::POLICY_LEVEL_MANDATORY,
154 policy::POLICY_SCOPE_USER, policy::POLICY_SOURCE_CLOUD,
155 base::MakeUnique<base::StringValue>(ProxyPrefs::kAutoDetectProxyModeName),
156 nullptr);
157 UpdatePolicy(policy);
158
159 std::unique_ptr<base::DictionaryValue> expected_proxy_config(
160 base::MakeUnique<base::DictionaryValue>());
161 expected_proxy_config->SetString("mode",
162 ProxyPrefs::kAutoDetectProxyModeName);
163 expected_proxy_config->SetString("pacUrl", "http://wpad/wpad.dat");
164 EXPECT_EQ(CountProxyBroadcasts(fake_intent_helper_instance_->broadcasts(),
165 expected_proxy_config.get()),
166 1);
167 }
168
169 IN_PROC_BROWSER_TEST_F(ArcSettingsServiceTest, ONCProxyPolicyTest) {
170 fake_intent_helper_instance_->clear_broadcasts();
171
172 policy::PolicyMap policy;
173 policy.Set(policy::key::kOpenNetworkConfiguration,
174 policy::POLICY_LEVEL_MANDATORY, policy::POLICY_SCOPE_USER,
175 policy::POLICY_SOURCE_CLOUD,
176 base::MakeUnique<base::StringValue>(kONCPolicy), nullptr);
177 UpdatePolicy(policy);
178
179 std::unique_ptr<base::DictionaryValue> expected_proxy_config(
180 base::MakeUnique<base::DictionaryValue>());
181 expected_proxy_config->SetString("mode", ProxyPrefs::kPacScriptProxyModeName);
182 expected_proxy_config->SetString("pacUrl", kONCPacUrl);
183
184 EXPECT_EQ(CountProxyBroadcasts(fake_intent_helper_instance_->broadcasts(),
185 expected_proxy_config.get()),
186 1);
187 }
188
189 // Proxy policy has a higher priority than proxy default settings.
190 IN_PROC_BROWSER_TEST_F(ArcSettingsServiceTest, TwoSourcesTest) {
191 fake_intent_helper_instance_->clear_broadcasts();
192
193 policy::PolicyMap policy;
194 // Proxy policy.
195 policy.Set(policy::key::kProxyMode, policy::POLICY_LEVEL_MANDATORY,
196 policy::POLICY_SCOPE_USER, policy::POLICY_SOURCE_CLOUD,
197 base::MakeUnique<base::StringValue>(
198 ProxyPrefs::kFixedServersProxyModeName),
199 nullptr);
200 policy.Set(policy::key::kProxyServer, policy::POLICY_LEVEL_MANDATORY,
201 policy::POLICY_SCOPE_USER, policy::POLICY_SOURCE_CLOUD,
202 base::MakeUnique<base::StringValue>("proxy:8888"), nullptr);
203 UpdatePolicy(policy);
204
205 std::unique_ptr<base::DictionaryValue> proxy_config(
206 base::MakeUnique<base::DictionaryValue>());
207 proxy_config->SetString("mode", ProxyPrefs::kAutoDetectProxyModeName);
208 ProxyConfigDictionary proxy_config_dict(proxy_config.get());
209 const chromeos::NetworkState* network = chromeos::NetworkHandler::Get()
210 ->network_state_handler()
211 ->DefaultNetwork();
212 ASSERT_TRUE(network);
213 chromeos::proxy_config::SetProxyConfigForNetwork(proxy_config_dict, *network);
214 DCHECK(base::MessageLoop::current());
215 base::RunLoop loop;
216 loop.RunUntilIdle();
217
218 std::unique_ptr<base::DictionaryValue> proxy_settings(
219 base::MakeUnique<base::DictionaryValue>());
220 proxy_settings->SetString("mode", ProxyPrefs::kFixedServersProxyModeName);
221 proxy_settings->SetString("host", "proxy");
222 proxy_settings->SetInteger("port", 8888);
223 EXPECT_EQ(CountProxyBroadcasts(fake_intent_helper_instance_->broadcasts(),
224 proxy_settings.get()),
225 1);
226 }
227
228 IN_PROC_BROWSER_TEST_F(ArcSettingsServiceTest, ProxyPrefTest) {
229 fake_intent_helper_instance_->clear_broadcasts();
230
231 std::unique_ptr<base::DictionaryValue> proxy_config(
232 base::MakeUnique<base::DictionaryValue>());
233 proxy_config->SetString("mode", ProxyPrefs::kPacScriptProxyModeName);
234 proxy_config->SetString("pac_url", "http://proxy");
235 browser()->profile()->GetPrefs()->Set(proxy_config::prefs::kProxy,
236 *proxy_config.get());
237 DCHECK(base::MessageLoop::current());
238 base::RunLoop loop;
239 loop.RunUntilIdle();
240
241 std::unique_ptr<base::DictionaryValue> expected_proxy_config(
242 base::MakeUnique<base::DictionaryValue>());
243 expected_proxy_config->SetString("mode", ProxyPrefs::kPacScriptProxyModeName);
244 expected_proxy_config->SetString("pacUrl", "http://proxy");
245 EXPECT_EQ(CountProxyBroadcasts(fake_intent_helper_instance_->broadcasts(),
246 expected_proxy_config.get()),
247 1);
248 }
249
250 IN_PROC_BROWSER_TEST_F(ArcSettingsServiceTest, DefaultProxyConfigTest) {
stevenjb 2016/09/06 15:49:06 DefaultNetworkProxyConfigTest
Polina Bondarenko 2016/09/06 19:38:46 Done.
251 fake_intent_helper_instance_->clear_broadcasts();
252
253 std::unique_ptr<base::DictionaryValue> proxy_config(
254 base::MakeUnique<base::DictionaryValue>());
255 proxy_config->SetString("mode", ProxyPrefs::kFixedServersProxyModeName);
256 proxy_config->SetString("server", "proxy:8080");
257
258 ProxyConfigDictionary proxy_config_dict(proxy_config.get());
259 const chromeos::NetworkState* network = chromeos::NetworkHandler::Get()
260 ->network_state_handler()
261 ->DefaultNetwork();
262 ASSERT_TRUE(network);
263 chromeos::proxy_config::SetProxyConfigForNetwork(proxy_config_dict, *network);
264 DCHECK(base::MessageLoop::current());
265 base::RunLoop loop;
266 loop.RunUntilIdle();
267
268 std::unique_ptr<base::DictionaryValue> expected_proxy_config(
269 base::MakeUnique<base::DictionaryValue>());
270 expected_proxy_config->SetString("mode",
271 ProxyPrefs::kFixedServersProxyModeName);
272 expected_proxy_config->SetString("host", "proxy");
273 expected_proxy_config->SetInteger("port", 8080);
274 EXPECT_EQ(CountProxyBroadcasts(fake_intent_helper_instance_->broadcasts(),
275 expected_proxy_config.get()),
276 1);
stevenjb 2016/09/06 15:49:06 We still need to test that when the default networ
Polina Bondarenko 2016/09/06 19:38:46 Done.
277 }
278
279 } // namespace arc
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698