managed_network_configuration_handler_unittest.cc revision c2e0dbddbe15c98d52c4786dac06cb8952a8ae6d
146d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)// Copyright (c) 2013 The Chromium Authors. All rights reserved.
246d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be
346d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)// found in the LICENSE file.
446d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)
546d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)#include "chromeos/network/managed_network_configuration_handler.h"
646d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)
746d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)#include <iostream>
846d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)#include <sstream>
946d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)
1046d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)#include "base/message_loop.h"
1146d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)#include "chromeos/dbus/dbus_thread_manager.h"
1246d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)#include "chromeos/dbus/mock_dbus_thread_manager.h"
1346d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)#include "chromeos/dbus/mock_shill_manager_client.h"
1446d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)#include "chromeos/dbus/mock_shill_profile_client.h"
1546d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)#include "chromeos/dbus/mock_shill_service_client.h"
1646d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)#include "chromeos/dbus/shill_profile_client_stub.h"
1746d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)#include "chromeos/network/network_configuration_handler.h"
1846d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)#include "chromeos/network/onc/onc_test_utils.h"
1946d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)#include "chromeos/network/onc/onc_utils.h"
2046d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)#include "dbus/object_path.h"
2146d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)#include "testing/gmock/include/gmock/gmock.h"
2246d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)#include "testing/gtest/include/gtest/gtest.h"
2346d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)#include "third_party/cros_system_api/dbus/service_constants.h"
2446d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)
2546d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)using ::testing::Invoke;
2646d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)using ::testing::Mock;
2746d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)using ::testing::Pointee;
2846d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)using ::testing::Return;
2946d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)using ::testing::SaveArg;
3046d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)using ::testing::StrEq;
3146d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)using ::testing::StrictMock;
3246d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)using ::testing::_;
3346d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)
3446d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)namespace test_utils = ::chromeos::onc::test_utils;
3546d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)
3646d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)namespace {
37
38std::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.
45MATCHER_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
55namespace chromeos {
56
57class 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    base::ListValue* network_configs = NULL;
115    policy->GetListWithoutPathExpansion(
116        onc::toplevel_config::kNetworkConfigurations, &network_configs);
117
118    managed_handler()->SetPolicy(onc::ONC_SOURCE_USER_POLICY, *network_configs);
119  }
120
121  ManagedNetworkConfigurationHandler* managed_handler() {
122    return ManagedNetworkConfigurationHandler::Get();
123  }
124
125 protected:
126  StrictMock<MockShillManagerClient> mock_manager_client_;
127  StrictMock<MockShillServiceClient> mock_service_client_;
128  StrictMock<MockShillProfileClient> mock_profile_client_;
129  ShillProfileClientStub stub_profile_client_;
130  MessageLoop message_loop_;
131
132 private:
133  DISALLOW_COPY_AND_ASSIGN(ManagedNetworkConfigurationHandlerTest);
134};
135
136TEST_F(ManagedNetworkConfigurationHandlerTest, SetPolicyManageUnconfigured) {
137  scoped_ptr<base::DictionaryValue> expected_shill_properties =
138      test_utils::ReadTestDictionary(
139          "policy/shill_policy_on_unconfigured_wifi1.json");
140
141  EXPECT_CALL(mock_profile_client_,
142              GetProperties(dbus::ObjectPath("/profile/chronos/shill"),
143                            _, _)).Times(1);
144
145  EXPECT_CALL(mock_manager_client_,
146              ConfigureServiceForProfile(
147                  dbus::ObjectPath("/profile/chronos/shill"),
148                  IsEqualTo(expected_shill_properties.get()),
149                  _, _)).Times(1);
150
151  SetPolicy(onc::ONC_SOURCE_USER_POLICY, "policy/policy_wifi1.onc");
152  message_loop_.RunUntilIdle();
153}
154
155TEST_F(ManagedNetworkConfigurationHandlerTest, SetPolicyIgnoreUnmodified) {
156  EXPECT_CALL(mock_profile_client_,
157              GetProperties(_, _, _)).Times(1);
158
159  EXPECT_CALL(mock_manager_client_,
160              ConfigureServiceForProfile(_, _, _, _)).Times(1);
161
162  SetPolicy(onc::ONC_SOURCE_USER_POLICY, "policy/policy_wifi1.onc");
163  message_loop_.RunUntilIdle();
164  Mock::VerifyAndClearExpectations(&mock_profile_client_);
165  Mock::VerifyAndClearExpectations(&mock_manager_client_);
166
167  SetUpEntry("policy/shill_policy_on_unmanaged_user_wifi1.json",
168             "/profile/chronos/shill",
169             "some_entry_path");
170
171  EXPECT_CALL(mock_profile_client_,
172              GetProperties(_, _, _)).Times(1);
173
174  EXPECT_CALL(mock_profile_client_,
175              GetEntry(dbus::ObjectPath("/profile/chronos/shill"),
176                       "some_entry_path",
177                       _, _)).Times(1);
178
179  SetPolicy(onc::ONC_SOURCE_USER_POLICY, "policy/policy_wifi1.onc");
180  message_loop_.RunUntilIdle();
181}
182
183TEST_F(ManagedNetworkConfigurationHandlerTest, SetPolicyManageUnmanaged) {
184  SetUpEntry("policy/shill_unmanaged_user_wifi1.json",
185             "/profile/chronos/shill",
186             "old_entry_path");
187
188  scoped_ptr<base::DictionaryValue> expected_shill_properties =
189      test_utils::ReadTestDictionary(
190          "policy/shill_policy_on_unmanaged_user_wifi1.json");
191
192  EXPECT_CALL(mock_profile_client_,
193              GetProperties(dbus::ObjectPath("/profile/chronos/shill"),
194                            _, _)).Times(1);
195
196  EXPECT_CALL(mock_profile_client_,
197              GetEntry(dbus::ObjectPath("/profile/chronos/shill"),
198                       "old_entry_path",
199                       _, _)).Times(1);
200
201  EXPECT_CALL(mock_manager_client_,
202              ConfigureServiceForProfile(
203                  dbus::ObjectPath("/profile/chronos/shill"),
204                  IsEqualTo(expected_shill_properties.get()),
205                  _, _)).Times(1);
206
207  SetPolicy(onc::ONC_SOURCE_USER_POLICY, "policy/policy_wifi1.onc");
208  message_loop_.RunUntilIdle();
209}
210
211TEST_F(ManagedNetworkConfigurationHandlerTest, SetPolicyUpdateManagedNewGUID) {
212  SetUpEntry("policy/shill_managed_wifi1.json",
213             "/profile/chronos/shill",
214             "old_entry_path");
215
216  scoped_ptr<base::DictionaryValue> expected_shill_properties =
217      test_utils::ReadTestDictionary(
218          "policy/shill_policy_on_unmanaged_user_wifi1.json");
219
220  // The passphrase isn't sent again, because it's configured by the user and
221  // Shill doesn't sent it on GetProperties calls.
222  expected_shill_properties->RemoveWithoutPathExpansion(
223      flimflam::kPassphraseProperty, NULL);
224
225  EXPECT_CALL(mock_profile_client_,
226              GetProperties(dbus::ObjectPath("/profile/chronos/shill"),
227                            _, _)).Times(1);
228
229  EXPECT_CALL(mock_profile_client_,
230              GetEntry(dbus::ObjectPath("/profile/chronos/shill"),
231                       "old_entry_path",
232                       _, _)).Times(1);
233
234  EXPECT_CALL(mock_manager_client_,
235              ConfigureServiceForProfile(
236                  dbus::ObjectPath("/profile/chronos/shill"),
237                  IsEqualTo(expected_shill_properties.get()),
238                  _, _)).Times(1);
239
240  SetPolicy(onc::ONC_SOURCE_USER_POLICY, "policy/policy_wifi1.onc");
241  message_loop_.RunUntilIdle();
242}
243
244TEST_F(ManagedNetworkConfigurationHandlerTest, SetPolicyReapplyToManaged) {
245  SetUpEntry("policy/shill_policy_on_unmanaged_user_wifi1.json",
246             "/profile/chronos/shill",
247             "old_entry_path");
248
249  scoped_ptr<base::DictionaryValue> expected_shill_properties =
250      test_utils::ReadTestDictionary(
251          "policy/shill_policy_on_unmanaged_user_wifi1.json");
252
253  // The passphrase isn't sent again, because it's configured by the user and
254  // Shill doesn't sent it on GetProperties calls.
255  expected_shill_properties->RemoveWithoutPathExpansion(
256      flimflam::kPassphraseProperty, NULL);
257
258  EXPECT_CALL(mock_profile_client_,
259              GetProperties(dbus::ObjectPath("/profile/chronos/shill"),
260                            _, _)).Times(1);
261
262  EXPECT_CALL(mock_profile_client_,
263              GetEntry(dbus::ObjectPath("/profile/chronos/shill"),
264                       "old_entry_path",
265                       _, _)).Times(1);
266
267  EXPECT_CALL(mock_manager_client_,
268              ConfigureServiceForProfile(
269                  dbus::ObjectPath("/profile/chronos/shill"),
270                  IsEqualTo(expected_shill_properties.get()),
271                  _, _)).Times(1);
272
273  SetPolicy(onc::ONC_SOURCE_USER_POLICY, "policy/policy_wifi1.onc");
274  message_loop_.RunUntilIdle();
275}
276
277TEST_F(ManagedNetworkConfigurationHandlerTest, SetPolicyUnmanageManaged) {
278  SetUpEntry("policy/shill_policy_on_unmanaged_user_wifi1.json",
279             "/profile/chronos/shill",
280             "old_entry_path");
281
282  EXPECT_CALL(mock_profile_client_,
283              GetProperties(dbus::ObjectPath("/profile/chronos/shill"),
284                            _, _)).Times(1);
285
286  EXPECT_CALL(mock_profile_client_,
287              GetEntry(dbus::ObjectPath("/profile/chronos/shill"),
288                       "old_entry_path",
289                       _, _)).Times(1);
290
291  EXPECT_CALL(mock_profile_client_,
292              DeleteEntry(dbus::ObjectPath("/profile/chronos/shill"),
293                          "old_entry_path",
294                          _, _)).Times(1);
295
296  SetPolicy(onc::ONC_SOURCE_USER_POLICY, "");
297  message_loop_.RunUntilIdle();
298}
299
300TEST_F(ManagedNetworkConfigurationHandlerTest, SetPolicyIgnoreUnmanaged) {
301  SetUpEntry("policy/shill_unmanaged_user_wifi1.json",
302             "/profile/chronos/shill",
303             "old_entry_path");
304
305  EXPECT_CALL(mock_profile_client_,
306              GetProperties(dbus::ObjectPath("/profile/chronos/shill"),
307                            _, _)).Times(1);
308
309  EXPECT_CALL(mock_profile_client_,
310              GetEntry(dbus::ObjectPath("/profile/chronos/shill"),
311                       "old_entry_path",
312                       _, _)).Times(1);
313
314  SetPolicy(onc::ONC_SOURCE_USER_POLICY, "");
315  message_loop_.RunUntilIdle();
316}
317
318}  // namespace chromeos
319