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

Side by Side Diff: chromeos/network/managed_network_configuration_handler_unittest.cc

Issue 12676017: Adding policy support to the new network configuration stack. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Fixed clang errors. Created 7 years, 8 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 | Annotate | Revision Log
OLDNEW
(Empty)
1 // Copyright (c) 2013 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 "chromeos/network/managed_network_configuration_handler.h"
6
7 #include <iostream>
8 #include <sstream>
9
10 #include "base/message_loop.h"
11 #include "chromeos/dbus/dbus_thread_manager.h"
12 #include "chromeos/dbus/mock_dbus_thread_manager.h"
13 #include "chromeos/dbus/mock_shill_manager_client.h"
14 #include "chromeos/dbus/mock_shill_profile_client.h"
15 #include "chromeos/dbus/mock_shill_service_client.h"
16 #include "chromeos/dbus/shill_profile_client_stub.h"
17 #include "chromeos/network/network_configuration_handler.h"
18 #include "chromeos/network/onc/onc_test_utils.h"
19 #include "chromeos/network/onc/onc_utils.h"
20 #include "dbus/object_path.h"
21 #include "testing/gmock/include/gmock/gmock.h"
22 #include "testing/gtest/include/gtest/gtest.h"
23 #include "third_party/cros_system_api/dbus/service_constants.h"
24
25 using ::testing::Invoke;
26 using ::testing::Mock;
27 using ::testing::Pointee;
28 using ::testing::Return;
29 using ::testing::SaveArg;
30 using ::testing::StrEq;
31 using ::testing::StrictMock;
32 using ::testing::_;
33
34 namespace test_utils = ::chromeos::onc::test_utils;
35
36 namespace {
37
38 std::string ValueToString(const base::Value* value) {
39 std::stringstream str;
40 str << *value;
41 return str.str();
42 }
43
44 // Matcher to match base::Value.
45 MATCHER_P(IsEqualTo,
46 value,
47 std::string(negation ? "isn't" : "is") + " equal to " +
48 ValueToString(value)) {
49 return value->Equals(&arg);
50 }
51
52 } // namespace
53
54
55 namespace chromeos {
56
57 class ManagedNetworkConfigurationHandlerTest : public testing::Test {
58 public:
59 ManagedNetworkConfigurationHandlerTest() {
60 }
61
62 virtual ~ManagedNetworkConfigurationHandlerTest() {
63 }
64
65 virtual void SetUp() OVERRIDE {
66 MockDBusThreadManager* dbus_thread_manager = new MockDBusThreadManager;
67 EXPECT_CALL(*dbus_thread_manager, GetSystemBus())
68 .WillRepeatedly(Return(static_cast<dbus::Bus*>(NULL)));
69 DBusThreadManager::InitializeForTesting(dbus_thread_manager);
70
71 EXPECT_CALL(*dbus_thread_manager, GetShillManagerClient())
72 .WillRepeatedly(Return(&mock_manager_client_));
73 EXPECT_CALL(*dbus_thread_manager, GetShillServiceClient())
74 .WillRepeatedly(Return(&mock_service_client_));
75 EXPECT_CALL(*dbus_thread_manager, GetShillProfileClient())
76 .WillRepeatedly(Return(&mock_profile_client_));
77
78 ON_CALL(mock_profile_client_, GetProperties(_,_,_))
79 .WillByDefault(Invoke(&stub_profile_client_,
80 &ShillProfileClientStub::GetProperties));
81
82 ON_CALL(mock_profile_client_, GetEntry(_,_,_,_))
83 .WillByDefault(Invoke(&stub_profile_client_,
84 &ShillProfileClientStub::GetEntry));
85
86 NetworkConfigurationHandler::Initialize();
87 ManagedNetworkConfigurationHandler::Initialize();
88 message_loop_.RunUntilIdle();
89 }
90
91 virtual void TearDown() OVERRIDE {
92 ManagedNetworkConfigurationHandler::Shutdown();
93 NetworkConfigurationHandler::Shutdown();
94 DBusThreadManager::Shutdown();
95 }
96
97 void SetUpEntry(const std::string& path_to_shill_json,
98 const std::string& profile_path,
99 const std::string& entry_path) {
100 stub_profile_client_.AddEntry(
101 profile_path,
102 entry_path,
103 *test_utils::ReadTestDictionary(path_to_shill_json));
104 }
105
106 void SetPolicy(onc::ONCSource onc_source,
107 const std::string& path_to_onc) {
108 scoped_ptr<base::DictionaryValue> policy;
109 if (path_to_onc.empty())
110 policy = onc::ReadDictionaryFromJson(onc::kEmptyUnencryptedConfiguration);
111 else
112 policy = test_utils::ReadTestDictionary(path_to_onc);
113
114 managed_handler()->SetPolicy(onc::ONC_SOURCE_USER_POLICY, *policy);
115 }
116
117 ManagedNetworkConfigurationHandler* managed_handler() {
118 return ManagedNetworkConfigurationHandler::Get();
119 }
120
121 protected:
122 StrictMock<MockShillManagerClient> mock_manager_client_;
123 StrictMock<MockShillServiceClient> mock_service_client_;
124 StrictMock<MockShillProfileClient> mock_profile_client_;
125 ShillProfileClientStub stub_profile_client_;
126 MessageLoop message_loop_;
127
128 private:
129 DISALLOW_COPY_AND_ASSIGN(ManagedNetworkConfigurationHandlerTest);
130 };
131
132 TEST_F(ManagedNetworkConfigurationHandlerTest, SetPolicyManageUnconfigured) {
133 scoped_ptr<base::DictionaryValue> expected_shill_properties =
134 test_utils::ReadTestDictionary(
135 "policy/shill_policy_on_unconfigured_wifi1.json");
136
137 EXPECT_CALL(mock_profile_client_,
138 GetProperties(dbus::ObjectPath("/profile/chronos/shill"),
139 _, _)).Times(1);
140
141 EXPECT_CALL(mock_manager_client_,
142 ConfigureServiceForProfile(
143 dbus::ObjectPath("/profile/chronos/shill"),
144 IsEqualTo(expected_shill_properties.get()),
145 _, _)).Times(1);
146
147 SetPolicy(onc::ONC_SOURCE_USER_POLICY, "policy/policy_wifi1.onc");
148 message_loop_.RunUntilIdle();
149 }
150
151 TEST_F(ManagedNetworkConfigurationHandlerTest, SetPolicyIgnoreUnmodified) {
152 EXPECT_CALL(mock_profile_client_,
153 GetProperties(_, _, _)).Times(1);
154
155 EXPECT_CALL(mock_manager_client_,
156 ConfigureServiceForProfile(_, _, _, _)).Times(1);
157
158 SetPolicy(onc::ONC_SOURCE_USER_POLICY, "policy/policy_wifi1.onc");
159 message_loop_.RunUntilIdle();
160 Mock::VerifyAndClearExpectations(&mock_profile_client_);
161 Mock::VerifyAndClearExpectations(&mock_manager_client_);
162
163 SetUpEntry("policy/shill_policy_on_unmanaged_user_wifi1.json",
164 "/profile/chronos/shill",
165 "some_entry_path");
166
167 EXPECT_CALL(mock_profile_client_,
168 GetProperties(_, _, _)).Times(1);
169
170 EXPECT_CALL(mock_profile_client_,
171 GetEntry(dbus::ObjectPath("/profile/chronos/shill"),
172 "some_entry_path",
173 _, _)).Times(1);
174
175 SetPolicy(onc::ONC_SOURCE_USER_POLICY, "policy/policy_wifi1.onc");
176 message_loop_.RunUntilIdle();
177 }
178
179 TEST_F(ManagedNetworkConfigurationHandlerTest, SetPolicyManageUnmanaged) {
180 SetUpEntry("policy/shill_unmanaged_user_wifi1.json",
181 "/profile/chronos/shill",
182 "old_entry_path");
183
184 scoped_ptr<base::DictionaryValue> expected_shill_properties =
185 test_utils::ReadTestDictionary(
186 "policy/shill_policy_on_unmanaged_user_wifi1.json");
187
188 EXPECT_CALL(mock_profile_client_,
189 GetProperties(dbus::ObjectPath("/profile/chronos/shill"),
190 _, _)).Times(1);
191
192 EXPECT_CALL(mock_profile_client_,
193 GetEntry(dbus::ObjectPath("/profile/chronos/shill"),
194 "old_entry_path",
195 _, _)).Times(1);
196
197 EXPECT_CALL(mock_manager_client_,
198 ConfigureServiceForProfile(
199 dbus::ObjectPath("/profile/chronos/shill"),
200 IsEqualTo(expected_shill_properties.get()),
201 _, _)).Times(1);
202
203 SetPolicy(onc::ONC_SOURCE_USER_POLICY, "policy/policy_wifi1.onc");
204 message_loop_.RunUntilIdle();
205 }
206
207 TEST_F(ManagedNetworkConfigurationHandlerTest, SetPolicyUpdateManagedNewGUID) {
208 SetUpEntry("policy/shill_managed_wifi1.json",
209 "/profile/chronos/shill",
210 "old_entry_path");
211
212 scoped_ptr<base::DictionaryValue> expected_shill_properties =
213 test_utils::ReadTestDictionary(
214 "policy/shill_policy_on_unmanaged_user_wifi1.json");
215
216 // The passphrase isn't sent again, because it's configured by the user and
217 // Shill doesn't sent it on GetProperties calls.
218 expected_shill_properties->RemoveWithoutPathExpansion(
219 flimflam::kPassphraseProperty, NULL);
220
221 EXPECT_CALL(mock_profile_client_,
222 GetProperties(dbus::ObjectPath("/profile/chronos/shill"),
223 _, _)).Times(1);
224
225 EXPECT_CALL(mock_profile_client_,
226 GetEntry(dbus::ObjectPath("/profile/chronos/shill"),
227 "old_entry_path",
228 _, _)).Times(1);
229
230 EXPECT_CALL(mock_manager_client_,
231 ConfigureServiceForProfile(
232 dbus::ObjectPath("/profile/chronos/shill"),
233 IsEqualTo(expected_shill_properties.get()),
234 _, _)).Times(1);
235
236 SetPolicy(onc::ONC_SOURCE_USER_POLICY, "policy/policy_wifi1.onc");
237 message_loop_.RunUntilIdle();
238 }
239
240 TEST_F(ManagedNetworkConfigurationHandlerTest, SetPolicyReapplyToManaged) {
241 SetUpEntry("policy/shill_policy_on_unmanaged_user_wifi1.json",
242 "/profile/chronos/shill",
243 "old_entry_path");
244
245 scoped_ptr<base::DictionaryValue> expected_shill_properties =
246 test_utils::ReadTestDictionary(
247 "policy/shill_policy_on_unmanaged_user_wifi1.json");
248
249 // The passphrase isn't sent again, because it's configured by the user and
250 // Shill doesn't sent it on GetProperties calls.
251 expected_shill_properties->RemoveWithoutPathExpansion(
252 flimflam::kPassphraseProperty, NULL);
253
254 EXPECT_CALL(mock_profile_client_,
255 GetProperties(dbus::ObjectPath("/profile/chronos/shill"),
256 _, _)).Times(1);
257
258 EXPECT_CALL(mock_profile_client_,
259 GetEntry(dbus::ObjectPath("/profile/chronos/shill"),
260 "old_entry_path",
261 _, _)).Times(1);
262
263 EXPECT_CALL(mock_manager_client_,
264 ConfigureServiceForProfile(
265 dbus::ObjectPath("/profile/chronos/shill"),
266 IsEqualTo(expected_shill_properties.get()),
267 _, _)).Times(1);
268
269 SetPolicy(onc::ONC_SOURCE_USER_POLICY, "policy/policy_wifi1.onc");
270 message_loop_.RunUntilIdle();
271 }
272
273 TEST_F(ManagedNetworkConfigurationHandlerTest, SetPolicyUnmanageManaged) {
274 SetUpEntry("policy/shill_policy_on_unmanaged_user_wifi1.json",
275 "/profile/chronos/shill",
276 "old_entry_path");
277
278 EXPECT_CALL(mock_profile_client_,
279 GetProperties(dbus::ObjectPath("/profile/chronos/shill"),
280 _, _)).Times(1);
281
282 EXPECT_CALL(mock_profile_client_,
283 GetEntry(dbus::ObjectPath("/profile/chronos/shill"),
284 "old_entry_path",
285 _, _)).Times(1);
286
287 EXPECT_CALL(mock_profile_client_,
288 DeleteEntry(dbus::ObjectPath("/profile/chronos/shill"),
289 "old_entry_path",
290 _, _)).Times(1);
291
292 SetPolicy(onc::ONC_SOURCE_USER_POLICY, "");
293 message_loop_.RunUntilIdle();
294 }
295
296 TEST_F(ManagedNetworkConfigurationHandlerTest, SetPolicyIgnoreUnmanaged) {
297 SetUpEntry("policy/shill_unmanaged_user_wifi1.json",
298 "/profile/chronos/shill",
299 "old_entry_path");
300
301 EXPECT_CALL(mock_profile_client_,
302 GetProperties(dbus::ObjectPath("/profile/chronos/shill"),
303 _, _)).Times(1);
304
305 EXPECT_CALL(mock_profile_client_,
306 GetEntry(dbus::ObjectPath("/profile/chronos/shill"),
307 "old_entry_path",
308 _, _)).Times(1);
309
310 SetPolicy(onc::ONC_SOURCE_USER_POLICY, "");
311 message_loop_.RunUntilIdle();
312 }
313
314 } // namespace chromeos
OLDNEW
« no previous file with comments | « chromeos/network/managed_network_configuration_handler.cc ('k') | chromeos/network/network_configuration_handler.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698