OLD | NEW |
(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 std::string ValueToString(const base::Value* value) { |
| 38 std::stringstream str; |
| 39 str << *value; |
| 40 return str.str(); |
| 41 } |
| 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 chromeos { |
| 53 |
| 54 class ManagedNetworkConfigurationHandlerTest : public testing::Test { |
| 55 public: |
| 56 ManagedNetworkConfigurationHandlerTest() { |
| 57 } |
| 58 |
| 59 virtual ~ManagedNetworkConfigurationHandlerTest() {} |
| 60 |
| 61 virtual void SetUp() OVERRIDE { |
| 62 MockDBusThreadManager* dbus_thread_manager = new MockDBusThreadManager; |
| 63 EXPECT_CALL(*dbus_thread_manager, GetSystemBus()) |
| 64 .WillRepeatedly(Return(static_cast<dbus::Bus*>(NULL))); |
| 65 DBusThreadManager::InitializeForTesting(dbus_thread_manager); |
| 66 |
| 67 EXPECT_CALL(*dbus_thread_manager, GetShillManagerClient()) |
| 68 .WillRepeatedly(Return(&mock_manager_client_)); |
| 69 EXPECT_CALL(*dbus_thread_manager, GetShillServiceClient()) |
| 70 .WillRepeatedly(Return(&mock_service_client_)); |
| 71 EXPECT_CALL(*dbus_thread_manager, GetShillProfileClient()) |
| 72 .WillRepeatedly(Return(&mock_profile_client_)); |
| 73 |
| 74 ON_CALL(mock_profile_client_, GetProperties(_,_,_)) |
| 75 .WillByDefault(Invoke(&stub_profile_client_, |
| 76 &ShillProfileClientStub::GetProperties)); |
| 77 |
| 78 ON_CALL(mock_profile_client_, GetEntry(_,_,_,_)) |
| 79 .WillByDefault(Invoke(&stub_profile_client_, |
| 80 &ShillProfileClientStub::GetEntry)); |
| 81 |
| 82 NetworkConfigurationHandler::Initialize(); |
| 83 ManagedNetworkConfigurationHandler::Initialize(); |
| 84 message_loop_.RunUntilIdle(); |
| 85 } |
| 86 |
| 87 virtual void TearDown() OVERRIDE { |
| 88 ManagedNetworkConfigurationHandler::Shutdown(); |
| 89 NetworkConfigurationHandler::Shutdown(); |
| 90 DBusThreadManager::Shutdown(); |
| 91 } |
| 92 |
| 93 void SetUpEntry(const std::string& path_to_shill_json, |
| 94 const std::string& profile_path, |
| 95 const std::string& entry_path) { |
| 96 stub_profile_client_.AddEntry( |
| 97 profile_path, |
| 98 entry_path, |
| 99 *test_utils::ReadTestDictionary(path_to_shill_json)); |
| 100 } |
| 101 |
| 102 void SetPolicy(onc::ONCSource onc_source, |
| 103 const std::string& path_to_onc) { |
| 104 scoped_ptr<base::DictionaryValue> policy; |
| 105 if (path_to_onc.empty()) { |
| 106 policy = onc::ReadDictionaryFromJson(onc::kEmptyUnencryptedConfiguration); |
| 107 } else { |
| 108 policy = test_utils::ReadTestDictionary(path_to_onc); |
| 109 } |
| 110 managed_handler()->SetPolicy(onc::ONC_SOURCE_USER_POLICY, *policy); |
| 111 } |
| 112 |
| 113 ManagedNetworkConfigurationHandler* managed_handler() { |
| 114 return ManagedNetworkConfigurationHandler::Get(); |
| 115 } |
| 116 |
| 117 protected: |
| 118 StrictMock<MockShillManagerClient> mock_manager_client_; |
| 119 StrictMock<MockShillServiceClient> mock_service_client_; |
| 120 StrictMock<MockShillProfileClient> mock_profile_client_; |
| 121 ShillProfileClientStub stub_profile_client_; |
| 122 MessageLoop message_loop_; |
| 123 }; |
| 124 |
| 125 TEST_F(ManagedNetworkConfigurationHandlerTest, SetPolicyManageUnconfigured) { |
| 126 scoped_ptr<base::DictionaryValue> expected_shill_properties = |
| 127 test_utils::ReadTestDictionary( |
| 128 "policy/shill_policy_on_unconfigured_wifi1.json"); |
| 129 |
| 130 EXPECT_CALL(mock_profile_client_, |
| 131 GetProperties(dbus::ObjectPath("/profile/chronos/shill"), |
| 132 _, _)).Times(1); |
| 133 |
| 134 EXPECT_CALL(mock_manager_client_, |
| 135 ConfigureServiceForProfile( |
| 136 dbus::ObjectPath("/profile/chronos/shill"), |
| 137 IsEqualTo(expected_shill_properties.get()), |
| 138 _, _)).Times(1); |
| 139 |
| 140 SetPolicy(onc::ONC_SOURCE_USER_POLICY, "policy/policy_wifi1.onc"); |
| 141 message_loop_.RunUntilIdle(); |
| 142 } |
| 143 |
| 144 TEST_F(ManagedNetworkConfigurationHandlerTest, SetPolicyIgnoreUnmodified) { |
| 145 EXPECT_CALL(mock_profile_client_, |
| 146 GetProperties(_, _, _)).Times(1); |
| 147 |
| 148 EXPECT_CALL(mock_manager_client_, |
| 149 ConfigureServiceForProfile(_, _, _, _)).Times(1); |
| 150 |
| 151 SetPolicy(onc::ONC_SOURCE_USER_POLICY, "policy/policy_wifi1.onc"); |
| 152 message_loop_.RunUntilIdle(); |
| 153 Mock::VerifyAndClearExpectations(&mock_profile_client_); |
| 154 Mock::VerifyAndClearExpectations(&mock_manager_client_); |
| 155 |
| 156 SetUpEntry("policy/shill_policy_on_unmanaged_user_wifi1.json", |
| 157 "/profile/chronos/shill", |
| 158 "some_entry_path"); |
| 159 |
| 160 EXPECT_CALL(mock_profile_client_, |
| 161 GetProperties(_, _, _)).Times(1); |
| 162 |
| 163 EXPECT_CALL(mock_profile_client_, |
| 164 GetEntry(dbus::ObjectPath("/profile/chronos/shill"), |
| 165 "some_entry_path", |
| 166 _, _)).Times(1); |
| 167 |
| 168 SetPolicy(onc::ONC_SOURCE_USER_POLICY, "policy/policy_wifi1.onc"); |
| 169 message_loop_.RunUntilIdle(); |
| 170 } |
| 171 |
| 172 TEST_F(ManagedNetworkConfigurationHandlerTest, SetPolicyManageUnmanaged) { |
| 173 SetUpEntry("policy/shill_unmanaged_user_wifi1.json", |
| 174 "/profile/chronos/shill", |
| 175 "old_entry_path"); |
| 176 |
| 177 scoped_ptr<base::DictionaryValue> expected_shill_properties = |
| 178 test_utils::ReadTestDictionary( |
| 179 "policy/shill_policy_on_unmanaged_user_wifi1.json"); |
| 180 |
| 181 EXPECT_CALL(mock_profile_client_, |
| 182 GetProperties(dbus::ObjectPath("/profile/chronos/shill"), |
| 183 _, _)).Times(1); |
| 184 |
| 185 EXPECT_CALL(mock_profile_client_, |
| 186 GetEntry(dbus::ObjectPath("/profile/chronos/shill"), |
| 187 "old_entry_path", |
| 188 _, _)).Times(1); |
| 189 |
| 190 EXPECT_CALL(mock_manager_client_, |
| 191 ConfigureServiceForProfile( |
| 192 dbus::ObjectPath("/profile/chronos/shill"), |
| 193 IsEqualTo(expected_shill_properties.get()), |
| 194 _, _)).Times(1); |
| 195 |
| 196 SetPolicy(onc::ONC_SOURCE_USER_POLICY, "policy/policy_wifi1.onc"); |
| 197 message_loop_.RunUntilIdle(); |
| 198 } |
| 199 |
| 200 TEST_F(ManagedNetworkConfigurationHandlerTest, SetPolicyUpdateManagedNewGUID) { |
| 201 SetUpEntry("policy/shill_managed_wifi1.json", |
| 202 "/profile/chronos/shill", |
| 203 "old_entry_path"); |
| 204 |
| 205 scoped_ptr<base::DictionaryValue> expected_shill_properties = |
| 206 test_utils::ReadTestDictionary( |
| 207 "policy/shill_policy_on_unmanaged_user_wifi1.json"); |
| 208 |
| 209 // The passphrase isn't sent again, because it's configured by the user and |
| 210 // Shill doesn't sent it on GetProperties calls. |
| 211 expected_shill_properties->RemoveWithoutPathExpansion( |
| 212 flimflam::kPassphraseProperty, NULL); |
| 213 |
| 214 EXPECT_CALL(mock_profile_client_, |
| 215 GetProperties(dbus::ObjectPath("/profile/chronos/shill"), |
| 216 _, _)).Times(1); |
| 217 |
| 218 EXPECT_CALL(mock_profile_client_, |
| 219 GetEntry(dbus::ObjectPath("/profile/chronos/shill"), |
| 220 "old_entry_path", |
| 221 _, _)).Times(1); |
| 222 |
| 223 EXPECT_CALL(mock_manager_client_, |
| 224 ConfigureServiceForProfile( |
| 225 dbus::ObjectPath("/profile/chronos/shill"), |
| 226 IsEqualTo(expected_shill_properties.get()), |
| 227 _, _)).Times(1); |
| 228 |
| 229 SetPolicy(onc::ONC_SOURCE_USER_POLICY, "policy/policy_wifi1.onc"); |
| 230 message_loop_.RunUntilIdle(); |
| 231 } |
| 232 |
| 233 TEST_F(ManagedNetworkConfigurationHandlerTest, SetPolicyReapplyToManaged) { |
| 234 SetUpEntry("policy/shill_policy_on_unmanaged_user_wifi1.json", |
| 235 "/profile/chronos/shill", |
| 236 "old_entry_path"); |
| 237 |
| 238 scoped_ptr<base::DictionaryValue> expected_shill_properties = |
| 239 test_utils::ReadTestDictionary( |
| 240 "policy/shill_policy_on_unmanaged_user_wifi1.json"); |
| 241 |
| 242 // The passphrase isn't sent again, because it's configured by the user and |
| 243 // Shill doesn't sent it on GetProperties calls. |
| 244 expected_shill_properties->RemoveWithoutPathExpansion( |
| 245 flimflam::kPassphraseProperty, NULL); |
| 246 |
| 247 EXPECT_CALL(mock_profile_client_, |
| 248 GetProperties(dbus::ObjectPath("/profile/chronos/shill"), |
| 249 _, _)).Times(1); |
| 250 |
| 251 EXPECT_CALL(mock_profile_client_, |
| 252 GetEntry(dbus::ObjectPath("/profile/chronos/shill"), |
| 253 "old_entry_path", |
| 254 _, _)).Times(1); |
| 255 |
| 256 EXPECT_CALL(mock_manager_client_, |
| 257 ConfigureServiceForProfile( |
| 258 dbus::ObjectPath("/profile/chronos/shill"), |
| 259 IsEqualTo(expected_shill_properties.get()), |
| 260 _, _)).Times(1); |
| 261 |
| 262 SetPolicy(onc::ONC_SOURCE_USER_POLICY, "policy/policy_wifi1.onc"); |
| 263 message_loop_.RunUntilIdle(); |
| 264 } |
| 265 |
| 266 TEST_F(ManagedNetworkConfigurationHandlerTest, SetPolicyUnmanageManaged) { |
| 267 SetUpEntry("policy/shill_policy_on_unmanaged_user_wifi1.json", |
| 268 "/profile/chronos/shill", |
| 269 "old_entry_path"); |
| 270 |
| 271 EXPECT_CALL(mock_profile_client_, |
| 272 GetProperties(dbus::ObjectPath("/profile/chronos/shill"), |
| 273 _, _)).Times(1); |
| 274 |
| 275 EXPECT_CALL(mock_profile_client_, |
| 276 GetEntry(dbus::ObjectPath("/profile/chronos/shill"), |
| 277 "old_entry_path", |
| 278 _, _)).Times(1); |
| 279 |
| 280 EXPECT_CALL(mock_profile_client_, |
| 281 DeleteEntry(dbus::ObjectPath("/profile/chronos/shill"), |
| 282 "old_entry_path", |
| 283 _, _)).Times(1); |
| 284 |
| 285 SetPolicy(onc::ONC_SOURCE_USER_POLICY, ""); |
| 286 message_loop_.RunUntilIdle(); |
| 287 } |
| 288 |
| 289 TEST_F(ManagedNetworkConfigurationHandlerTest, SetPolicyIgnoreUnmanaged) { |
| 290 SetUpEntry("policy/shill_unmanaged_user_wifi1.json", |
| 291 "/profile/chronos/shill", |
| 292 "old_entry_path"); |
| 293 |
| 294 EXPECT_CALL(mock_profile_client_, |
| 295 GetProperties(dbus::ObjectPath("/profile/chronos/shill"), |
| 296 _, _)).Times(1); |
| 297 |
| 298 EXPECT_CALL(mock_profile_client_, |
| 299 GetEntry(dbus::ObjectPath("/profile/chronos/shill"), |
| 300 "old_entry_path", |
| 301 _, _)).Times(1); |
| 302 |
| 303 SetPolicy(onc::ONC_SOURCE_USER_POLICY, ""); |
| 304 message_loop_.RunUntilIdle(); |
| 305 } |
| 306 |
| 307 } // namespace chromeos |
OLD | NEW |