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/memory/weak_ptr.h" | |
14 #include "base/run_loop.h" | |
15 #include "base/values.h" | |
16 #include "chrome/browser/chromeos/arc/arc_settings_service.h" | |
17 #include "chrome/test/base/in_process_browser_test.h" | |
18 #include "chromeos/dbus/dbus_thread_manager.h" | |
19 #include "chromeos/dbus/shill_profile_client.h" | |
20 #include "chromeos/dbus/shill_service_client.h" | |
21 #include "components/arc/arc_service_manager.h" | |
22 #include "components/arc/test/fake_arc_bridge_service.h" | |
23 #include "components/arc/test/fake_intent_helper_instance.h" | |
24 #include "components/policy/core/browser/browser_policy_connector.h" | |
25 #include "components/policy/core/common/mock_configuration_policy_provider.h" | |
26 #include "components/policy/core/common/policy_map.h" | |
27 #include "components/policy/core/common/policy_types.h" | |
28 #include "components/policy/policy_constants.h" | |
29 #include "components/proxy_config/proxy_prefs.h" | |
30 #include "testing/gtest/include/gtest/gtest.h" | |
31 #include "third_party/cros_system_api/dbus/service_constants.h" | |
32 | |
33 using testing::_; | |
34 using testing::Return; | |
35 | |
36 namespace arc { | |
37 | |
38 namespace { | |
39 // char kCmdProxyServer[] = "proxy:8888"; | |
40 char kONCPolicy[] = | |
41 "{ \"NetworkConfigurations\": [" | |
42 " { \"GUID\": \"stub_ethernet_guid\"," | |
43 " \"Type\": \"Ethernet\"," | |
44 " \"Name\": \"My Ethernet\"," | |
45 " \"Ethernet\": {" | |
46 " \"Authentication\": \"None\" }," | |
47 " \"ProxySettings\": {" | |
48 " \"PAC\": \"http://domain.com/x\"," | |
49 " \"Type\": \"PAC\" }" | |
50 " }" | |
51 " ]," | |
52 " \"Type\": \"UnencryptedConfiguration\"" | |
53 "}"; | |
54 char kONCPacUrl[] = "http://domain.com/x"; | |
55 const char* kUserProfilePath = "user_profile"; | |
56 | |
57 } // namespace | |
58 | |
59 // Receives a proxy settings broadcast and matches it with the expected proxy | |
60 // settings. | |
61 class ProxyBroadcastCollector { | |
62 public: | |
63 ProxyBroadcastCollector() : proxy_settings_(new base::DictionaryValue()) { | |
64 proxy_settings_->SetString("mode", ProxyPrefs::kDirectProxyModeName); | |
65 } | |
66 | |
67 ~ProxyBroadcastCollector() {} | |
68 | |
69 int number() { return number_; } | |
70 | |
71 void Start(std::unique_ptr<base::DictionaryValue> proxy_settings) { | |
72 number_ = 0; | |
73 proxy_settings_.reset(); | |
74 proxy_settings_ = std::move(proxy_settings); | |
75 } | |
76 | |
77 void OnSendBroadcast(const std::string& action, const std::string& extras) { | |
78 if (action == "org.chromium.arc.intent_helper.SET_PROXY") { | |
79 EXPECT_TRUE( | |
80 base::JSONReader::Read(extras)->Equals(proxy_settings_.get())); | |
81 number_++; | |
82 } | |
83 } | |
84 | |
85 private: | |
86 int number_; | |
87 std::unique_ptr<base::DictionaryValue> proxy_settings_; | |
88 | |
89 DISALLOW_COPY_AND_ASSIGN(ProxyBroadcastCollector); | |
90 }; | |
91 | |
92 class ArcSettingsServiceTest : public InProcessBrowserTest { | |
93 public: | |
94 ArcSettingsServiceTest() | |
95 : proxy_broadcast_collector_(new ProxyBroadcastCollector()), | |
96 weak_ptr_factory_(this) {} | |
97 | |
98 // InProcessBrowserTest: | |
99 ~ArcSettingsServiceTest() override {} | |
100 | |
101 void SetUpInProcessBrowserTestFixture() override { | |
102 EXPECT_CALL(provider_, IsInitializationComplete(_)) | |
103 .WillRepeatedly(Return(true)); | |
104 policy::BrowserPolicyConnector::SetPolicyProviderForTesting(&provider_); | |
105 | |
106 fake_intent_helper_instance_.reset(new FakeIntentHelperInstance()); | |
107 fake_intent_helper_instance_->SetCallback( | |
108 base::Bind(&ArcSettingsServiceTest::OnSendBroadcast, | |
109 weak_ptr_factory_.GetWeakPtr())); | |
110 | |
111 ArcServiceManager::SetArcBridgeServiceForTesting( | |
112 base::MakeUnique<FakeArcBridgeService>()); | |
113 } | |
114 | |
115 void SetUpOnMainThread() override { | |
116 SetupNetworkEnvironment(); | |
117 ArcBridgeService::Get()->intent_helper()->SetInstance( | |
118 fake_intent_helper_instance_.get()); | |
119 } | |
120 | |
121 void TearDownOnMainThread() override { | |
122 ArcBridgeService::Get()->intent_helper()->SetInstance(nullptr); | |
123 } | |
124 | |
125 void OnSendBroadcast(const std::string& action, const std::string& extras) { | |
126 proxy_broadcast_collector_->OnSendBroadcast(action, extras); | |
127 } | |
128 | |
129 void UpdatePolicy(const policy::PolicyMap& policy) { | |
130 provider_.UpdateChromePolicy(policy); | |
131 DCHECK(base::MessageLoop::current()); | |
132 base::RunLoop loop; | |
133 loop.RunUntilIdle(); | |
134 } | |
135 | |
136 protected: | |
137 std::unique_ptr<ProxyBroadcastCollector> proxy_broadcast_collector_; | |
138 | |
139 private: | |
140 void SetupNetworkEnvironment() { | |
141 chromeos::ShillProfileClient::TestInterface* profile_test = | |
142 chromeos::DBusThreadManager::Get() | |
143 ->GetShillProfileClient() | |
144 ->GetTestInterface(); | |
145 chromeos::ShillServiceClient::TestInterface* service_test = | |
146 chromeos::DBusThreadManager::Get() | |
147 ->GetShillServiceClient() | |
148 ->GetTestInterface(); | |
149 | |
150 profile_test->AddProfile(kUserProfilePath, "user"); | |
151 | |
152 service_test->ClearServices(); | |
153 service_test->AddService("stub_ethernet", "stub_ethernet_guid", "eth0", | |
154 shill::kTypeEthernet, shill::kStateOnline, | |
155 true /* add_to_visible */); | |
156 service_test->SetServiceProperty("stub_ethernet", shill::kProfileProperty, | |
157 base::StringValue(kUserProfilePath)); | |
158 } | |
159 | |
160 policy::MockConfigurationPolicyProvider provider_; | |
161 std::unique_ptr<FakeIntentHelperInstance> fake_intent_helper_instance_; | |
162 | |
163 base::WeakPtrFactory<ArcSettingsServiceTest> weak_ptr_factory_; | |
164 | |
165 DISALLOW_COPY_AND_ASSIGN(ArcSettingsServiceTest); | |
166 }; | |
167 | |
168 IN_PROC_BROWSER_TEST_F(ArcSettingsServiceTest, ProxyModePolicyTest) { | |
169 policy::PolicyMap policy; | |
170 policy.Set( | |
171 policy::key::kProxyMode, policy::POLICY_LEVEL_MANDATORY, | |
172 policy::POLICY_SCOPE_USER, policy::POLICY_SOURCE_CLOUD, | |
173 base::MakeUnique<base::StringValue>(ProxyPrefs::kAutoDetectProxyModeName), | |
174 nullptr); | |
175 std::unique_ptr<base::DictionaryValue> proxy_settings( | |
176 base::MakeUnique<base::DictionaryValue>()); | |
177 proxy_settings->SetString("mode", ProxyPrefs::kAutoDetectProxyModeName); | |
178 proxy_settings->SetString("pacUrl", "http://wpad/wpad.dat"); | |
179 proxy_broadcast_collector_->Start(std::move(proxy_settings)); | |
180 | |
181 UpdatePolicy(policy); | |
182 | |
183 EXPECT_EQ(proxy_broadcast_collector_->number(), 1); | |
184 } | |
185 | |
186 IN_PROC_BROWSER_TEST_F(ArcSettingsServiceTest, ONCProxyPolicyTest) { | |
187 policy::PolicyMap policy; | |
188 policy.Set(policy::key::kOpenNetworkConfiguration, | |
189 policy::POLICY_LEVEL_MANDATORY, policy::POLICY_SCOPE_USER, | |
190 policy::POLICY_SOURCE_CLOUD, | |
191 base::MakeUnique<base::StringValue>(kONCPolicy), nullptr); | |
192 | |
193 std::unique_ptr<base::DictionaryValue> proxy_settings( | |
194 base::MakeUnique<base::DictionaryValue>()); | |
195 proxy_settings->SetString("mode", ProxyPrefs::kPacScriptProxyModeName); | |
196 proxy_settings->SetString("pacUrl", kONCPacUrl); | |
197 proxy_broadcast_collector_->Start(std::move(proxy_settings)); | |
198 | |
199 UpdatePolicy(policy); | |
200 | |
201 EXPECT_EQ(proxy_broadcast_collector_->number(), 1); | |
202 } | |
203 | |
204 // Proxy policy has a higher priority than proxy settings in ONC policy. | |
205 IN_PROC_BROWSER_TEST_F(ArcSettingsServiceTest, SetTwoPoliciesTest) { | |
206 policy::PolicyMap policy; | |
207 // Proxy policy. | |
208 policy.Set(policy::key::kProxyMode, policy::POLICY_LEVEL_MANDATORY, | |
209 policy::POLICY_SCOPE_USER, policy::POLICY_SOURCE_CLOUD, | |
210 base::MakeUnique<base::StringValue>( | |
211 ProxyPrefs::kFixedServersProxyModeName), | |
212 nullptr); | |
213 policy.Set(policy::key::kProxyServer, policy::POLICY_LEVEL_MANDATORY, | |
214 policy::POLICY_SCOPE_USER, policy::POLICY_SOURCE_CLOUD, | |
215 base::MakeUnique<base::StringValue>("proxy:8888"), nullptr); | |
216 // ONC policy. | |
217 policy.Set(policy::key::kOpenNetworkConfiguration, | |
218 policy::POLICY_LEVEL_MANDATORY, policy::POLICY_SCOPE_USER, | |
219 policy::POLICY_SOURCE_CLOUD, | |
220 base::MakeUnique<base::StringValue>(kONCPolicy), nullptr); | |
221 | |
222 std::unique_ptr<base::DictionaryValue> proxy_settings( | |
223 base::MakeUnique<base::DictionaryValue>()); | |
224 proxy_settings->SetString("mode", ProxyPrefs::kFixedServersProxyModeName); | |
225 proxy_settings->SetString("host", "proxy"); | |
226 proxy_settings->SetInteger("port", 8888); | |
227 proxy_broadcast_collector_->Start(std::move(proxy_settings)); | |
228 | |
229 UpdatePolicy(policy); | |
230 | |
231 EXPECT_EQ(proxy_broadcast_collector_->number(), 1); | |
232 } | |
stevenjb
2016/09/02 16:20:55
We should test that changes to the default network
Polina Bondarenko
2016/09/05 20:08:45
Done, added tests.
| |
233 | |
234 } // namespace arc | |
OLD | NEW |