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

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

Issue 510123004: Apply global auto connect ONC policy in GetManagedProperties. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Addressed nit. Created 6 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
1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include <iostream> 5 #include <iostream>
6 #include <sstream> 6 #include <sstream>
7 7
8 #include "base/bind.h" 8 #include "base/bind.h"
9 #include "base/location.h" 9 #include "base/location.h"
10 #include "base/memory/scoped_ptr.h" 10 #include "base/memory/scoped_ptr.h"
11 #include "base/message_loop/message_loop.h" 11 #include "base/message_loop/message_loop.h"
12 #include "base/stl_util.h" 12 #include "base/stl_util.h"
13 #include "base/values.h" 13 #include "base/values.h"
14 #include "chromeos/dbus/dbus_thread_manager.h" 14 #include "chromeos/dbus/dbus_thread_manager.h"
15 #include "chromeos/dbus/mock_shill_manager_client.h" 15 #include "chromeos/dbus/mock_shill_manager_client.h"
16 #include "chromeos/dbus/mock_shill_profile_client.h" 16 #include "chromeos/dbus/mock_shill_profile_client.h"
17 #include "chromeos/dbus/mock_shill_service_client.h"
17 #include "chromeos/dbus/shill_client_helper.h" 18 #include "chromeos/dbus/shill_client_helper.h"
18 #include "chromeos/network/managed_network_configuration_handler_impl.h" 19 #include "chromeos/network/managed_network_configuration_handler_impl.h"
19 #include "chromeos/network/network_configuration_handler.h" 20 #include "chromeos/network/network_configuration_handler.h"
20 #include "chromeos/network/network_profile_handler.h" 21 #include "chromeos/network/network_profile_handler.h"
21 #include "chromeos/network/onc/onc_test_utils.h" 22 #include "chromeos/network/onc/onc_test_utils.h"
22 #include "chromeos/network/onc/onc_utils.h" 23 #include "chromeos/network/onc/onc_utils.h"
23 #include "dbus/object_path.h" 24 #include "dbus/object_path.h"
24 #include "testing/gmock/include/gmock/gmock.h" 25 #include "testing/gmock/include/gmock/gmock.h"
25 #include "testing/gtest/include/gtest/gtest.h" 26 #include "testing/gtest/include/gtest/gtest.h"
26 #include "third_party/cros_system_api/dbus/service_constants.h" 27 #include "third_party/cros_system_api/dbus/service_constants.h"
(...skipping 96 matching lines...) Expand 10 before | Expand all | Expand 10 after
123 entries->GetDictionaryWithoutPathExpansion(entry_path, &entry); 124 entries->GetDictionaryWithoutPathExpansion(entry_path, &entry);
124 ASSERT_TRUE(entry); 125 ASSERT_TRUE(entry);
125 callback.Run(*entry); 126 callback.Run(*entry);
126 } 127 }
127 128
128 protected: 129 protected:
129 base::DictionaryValue profile_entries_; 130 base::DictionaryValue profile_entries_;
130 std::map<std::string, std::string> profile_to_user_; 131 std::map<std::string, std::string> profile_to_user_;
131 }; 132 };
132 133
134 class ShillServiceTestClient {
135 public:
136 typedef ShillClientHelper::DictionaryValueCallback DictionaryValueCallback;
137 void SetFakeProperties(const base::DictionaryValue& service_properties) {
138 service_properties_.Clear();
139 service_properties_.MergeDictionary(&service_properties);
140 }
141
142 void GetProperties(const dbus::ObjectPath& service_path,
143 const DictionaryValueCallback& callback) {
144 base::MessageLoop::current()->PostTask(
145 FROM_HERE,
146 base::Bind(callback,
147 DBUS_METHOD_CALL_SUCCESS,
148 base::ConstRef(service_properties_)));
149 }
150
151 protected:
152 base::DictionaryValue service_properties_;
153 };
154
133 class TestNetworkProfileHandler : public NetworkProfileHandler { 155 class TestNetworkProfileHandler : public NetworkProfileHandler {
134 public: 156 public:
135 TestNetworkProfileHandler() { 157 TestNetworkProfileHandler() {
136 Init(); 158 Init();
137 } 159 }
138 virtual ~TestNetworkProfileHandler() {} 160 virtual ~TestNetworkProfileHandler() {}
139 161
140 void AddProfileForTest(const NetworkProfile& profile) { 162 void AddProfileForTest(const NetworkProfile& profile) {
141 AddProfile(profile); 163 AddProfile(profile);
142 } 164 }
143 165
144 private: 166 private:
145 DISALLOW_COPY_AND_ASSIGN(TestNetworkProfileHandler); 167 DISALLOW_COPY_AND_ASSIGN(TestNetworkProfileHandler);
146 }; 168 };
147 169
148 } // namespace 170 } // namespace
149 171
150 class ManagedNetworkConfigurationHandlerTest : public testing::Test { 172 class ManagedNetworkConfigurationHandlerTest : public testing::Test {
151 public: 173 public:
152 ManagedNetworkConfigurationHandlerTest() 174 ManagedNetworkConfigurationHandlerTest()
153 : mock_manager_client_(NULL), 175 : mock_manager_client_(NULL),
154 mock_profile_client_(NULL) { 176 mock_profile_client_(NULL),
177 mock_service_client_(NULL) {
155 } 178 }
156 179
157 virtual ~ManagedNetworkConfigurationHandlerTest() { 180 virtual ~ManagedNetworkConfigurationHandlerTest() {
158 } 181 }
159 182
160 virtual void SetUp() OVERRIDE { 183 virtual void SetUp() OVERRIDE {
161 scoped_ptr<DBusThreadManagerSetter> dbus_setter = 184 scoped_ptr<DBusThreadManagerSetter> dbus_setter =
162 DBusThreadManager::GetSetterForTesting(); 185 DBusThreadManager::GetSetterForTesting();
163 mock_manager_client_ = new StrictMock<MockShillManagerClient>(); 186 mock_manager_client_ = new StrictMock<MockShillManagerClient>();
164 mock_profile_client_ = new StrictMock<MockShillProfileClient>(); 187 mock_profile_client_ = new StrictMock<MockShillProfileClient>();
188 mock_service_client_ = new StrictMock<MockShillServiceClient>();
165 dbus_setter->SetShillManagerClient( 189 dbus_setter->SetShillManagerClient(
166 scoped_ptr<ShillManagerClient>(mock_manager_client_).Pass()); 190 scoped_ptr<ShillManagerClient>(mock_manager_client_).Pass());
167 dbus_setter->SetShillProfileClient( 191 dbus_setter->SetShillProfileClient(
168 scoped_ptr<ShillProfileClient>(mock_profile_client_).Pass()); 192 scoped_ptr<ShillProfileClient>(mock_profile_client_).Pass());
193 dbus_setter->SetShillServiceClient(
194 scoped_ptr<ShillServiceClient>(mock_service_client_).Pass());
169 195
170 SetNetworkConfigurationHandlerExpectations(); 196 SetNetworkConfigurationHandlerExpectations();
171 197
172 ON_CALL(*mock_profile_client_, GetProperties(_,_,_)) 198 ON_CALL(*mock_profile_client_, GetProperties(_,_,_))
173 .WillByDefault(Invoke(&profiles_stub_, 199 .WillByDefault(Invoke(&profiles_stub_,
174 &ShillProfileTestClient::GetProperties)); 200 &ShillProfileTestClient::GetProperties));
175 201
176 ON_CALL(*mock_profile_client_, GetEntry(_,_,_,_)) 202 ON_CALL(*mock_profile_client_, GetEntry(_,_,_,_))
177 .WillByDefault(Invoke(&profiles_stub_, 203 .WillByDefault(Invoke(&profiles_stub_,
178 &ShillProfileTestClient::GetEntry)); 204 &ShillProfileTestClient::GetEntry));
179 205
206 ON_CALL(*mock_service_client_, GetProperties(_,_))
207 .WillByDefault(Invoke(&services_stub_,
208 &ShillServiceTestClient::GetProperties));
209
180 network_profile_handler_.reset(new TestNetworkProfileHandler()); 210 network_profile_handler_.reset(new TestNetworkProfileHandler());
181 network_configuration_handler_.reset( 211 network_configuration_handler_.reset(
182 NetworkConfigurationHandler::InitializeForTest( 212 NetworkConfigurationHandler::InitializeForTest(
183 NULL /* no NetworkStateHandler */)); 213 NULL /* no NetworkStateHandler */));
184 managed_network_configuration_handler_.reset( 214 managed_network_configuration_handler_.reset(
185 new ManagedNetworkConfigurationHandlerImpl()); 215 new ManagedNetworkConfigurationHandlerImpl());
186 managed_network_configuration_handler_->Init( 216 managed_network_configuration_handler_->Init(
187 NULL /* no NetworkStateHandler */, 217 NULL /* no NetworkStateHandler */,
188 network_profile_handler_.get(), 218 network_profile_handler_.get(),
189 network_configuration_handler_.get(), 219 network_configuration_handler_.get(),
(...skipping 12 matching lines...) Expand all
202 void VerifyAndClearExpectations() { 232 void VerifyAndClearExpectations() {
203 Mock::VerifyAndClearExpectations(mock_manager_client_); 233 Mock::VerifyAndClearExpectations(mock_manager_client_);
204 Mock::VerifyAndClearExpectations(mock_profile_client_); 234 Mock::VerifyAndClearExpectations(mock_profile_client_);
205 SetNetworkConfigurationHandlerExpectations(); 235 SetNetworkConfigurationHandlerExpectations();
206 } 236 }
207 237
208 void InitializeStandardProfiles() { 238 void InitializeStandardProfiles() {
209 profiles_stub_.AddProfile(kUser1ProfilePath, kUser1); 239 profiles_stub_.AddProfile(kUser1ProfilePath, kUser1);
210 network_profile_handler_-> 240 network_profile_handler_->
211 AddProfileForTest(NetworkProfile(kUser1ProfilePath, kUser1)); 241 AddProfileForTest(NetworkProfile(kUser1ProfilePath, kUser1));
212 network_profile_handler_-> 242
213 AddProfileForTest(NetworkProfile( 243 profiles_stub_.AddProfile(NetworkProfileHandler::GetSharedProfilePath(),
214 NetworkProfileHandler::GetSharedProfilePath(), std::string())); 244 std::string() /* no userhash */);
245 network_profile_handler_->AddProfileForTest(
246 NetworkProfile(NetworkProfileHandler::GetSharedProfilePath(),
247 std::string() /* no userhash */));
215 } 248 }
216 249
217 void SetUpEntry(const std::string& path_to_shill_json, 250 void SetUpEntry(const std::string& path_to_shill_json,
218 const std::string& profile_path, 251 const std::string& profile_path,
219 const std::string& entry_path) { 252 const std::string& entry_path) {
220 scoped_ptr<base::DictionaryValue> entry = 253 scoped_ptr<base::DictionaryValue> entry =
221 test_utils::ReadTestDictionary(path_to_shill_json); 254 test_utils::ReadTestDictionary(path_to_shill_json);
222 profiles_stub_.AddEntry(profile_path, entry_path, *entry); 255 profiles_stub_.AddEntry(profile_path, entry_path, *entry);
223 } 256 }
224 257
(...skipping 27 matching lines...) Expand all
252 EXPECT_CALL(*mock_manager_client_, 285 EXPECT_CALL(*mock_manager_client_,
253 AddPropertyChangedObserver(_)).Times(AnyNumber()); 286 AddPropertyChangedObserver(_)).Times(AnyNumber());
254 EXPECT_CALL(*mock_manager_client_, 287 EXPECT_CALL(*mock_manager_client_,
255 RemovePropertyChangedObserver(_)).Times(AnyNumber()); 288 RemovePropertyChangedObserver(_)).Times(AnyNumber());
256 } 289 }
257 290
258 ManagedNetworkConfigurationHandler* managed_handler() { 291 ManagedNetworkConfigurationHandler* managed_handler() {
259 return managed_network_configuration_handler_.get(); 292 return managed_network_configuration_handler_.get();
260 } 293 }
261 294
295 void GetManagedProperties(const std::string& userhash,
296 const std::string& service_path) {
297 managed_handler()->GetManagedProperties(
298 userhash,
299 service_path,
300 base::Bind(
301 &ManagedNetworkConfigurationHandlerTest::GetPropertiesCallback,
302 base::Unretained(this)),
303 base::Bind(&ManagedNetworkConfigurationHandlerTest::UnexpectedError));
304 }
305
306 void GetPropertiesCallback(const std::string& service_path,
307 const base::DictionaryValue& dictionary) {
308 get_properties_service_path_ = service_path;
309 get_properties_result_.Clear();
310 get_properties_result_.MergeDictionary(&dictionary);
311 }
312
313 static void UnexpectedError(const std::string& error_name,
314 scoped_ptr<base::DictionaryValue> error_data) {
315 ASSERT_FALSE(true);
316 }
317
262 protected: 318 protected:
263 MockShillManagerClient* mock_manager_client_; 319 MockShillManagerClient* mock_manager_client_;
264 MockShillProfileClient* mock_profile_client_; 320 MockShillProfileClient* mock_profile_client_;
321 MockShillServiceClient* mock_service_client_;
265 ShillProfileTestClient profiles_stub_; 322 ShillProfileTestClient profiles_stub_;
323 ShillServiceTestClient services_stub_;
266 scoped_ptr<TestNetworkProfileHandler> network_profile_handler_; 324 scoped_ptr<TestNetworkProfileHandler> network_profile_handler_;
267 scoped_ptr<NetworkConfigurationHandler> network_configuration_handler_; 325 scoped_ptr<NetworkConfigurationHandler> network_configuration_handler_;
268 scoped_ptr<ManagedNetworkConfigurationHandlerImpl> 326 scoped_ptr<ManagedNetworkConfigurationHandlerImpl>
269 managed_network_configuration_handler_; 327 managed_network_configuration_handler_;
270 base::MessageLoop message_loop_; 328 base::MessageLoop message_loop_;
271 329
330 std::string get_properties_service_path_;
331 base::DictionaryValue get_properties_result_;
332
272 private: 333 private:
273 DISALLOW_COPY_AND_ASSIGN(ManagedNetworkConfigurationHandlerTest); 334 DISALLOW_COPY_AND_ASSIGN(ManagedNetworkConfigurationHandlerTest);
274 }; 335 };
275 336
276 TEST_F(ManagedNetworkConfigurationHandlerTest, ProfileInitialization) { 337 TEST_F(ManagedNetworkConfigurationHandlerTest, ProfileInitialization) {
277 InitializeStandardProfiles(); 338 InitializeStandardProfiles();
278 message_loop_.RunUntilIdle(); 339 message_loop_.RunUntilIdle();
279 } 340 }
280 341
281 TEST_F(ManagedNetworkConfigurationHandlerTest, RemoveIrrelevantFields) { 342 TEST_F(ManagedNetworkConfigurationHandlerTest, RemoveIrrelevantFields) {
(...skipping 371 matching lines...) Expand 10 before | Expand all | Expand 10 after
653 dbus::ObjectPath(kUser1ProfilePath), 714 dbus::ObjectPath(kUser1ProfilePath),
654 IsEqualTo(expected_shill_properties.get()), 715 IsEqualTo(expected_shill_properties.get()),
655 _, _)); 716 _, _));
656 717
657 SetPolicy(::onc::ONC_SOURCE_USER_POLICY, kUser1, "policy/policy_wifi1.onc"); 718 SetPolicy(::onc::ONC_SOURCE_USER_POLICY, kUser1, "policy/policy_wifi1.onc");
658 message_loop_.RunUntilIdle(); 719 message_loop_.RunUntilIdle();
659 } 720 }
660 721
661 TEST_F(ManagedNetworkConfigurationHandlerTest, AutoConnectDisallowed) { 722 TEST_F(ManagedNetworkConfigurationHandlerTest, AutoConnectDisallowed) {
662 InitializeStandardProfiles(); 723 InitializeStandardProfiles();
724 // Setup an unmanaged network.
663 SetUpEntry("policy/shill_unmanaged_wifi2.json", 725 SetUpEntry("policy/shill_unmanaged_wifi2.json",
664 kUser1ProfilePath, 726 kUser1ProfilePath,
665 "wifi2_entry_path"); 727 "wifi2_entry_path");
666 728
729 // Apply the user policy with global autoconnect config and expect that
730 // autoconnect is disabled in the network's profile entry.
667 EXPECT_CALL(*mock_profile_client_, 731 EXPECT_CALL(*mock_profile_client_,
668 GetProperties(dbus::ObjectPath(kUser1ProfilePath), _, _)); 732 GetProperties(dbus::ObjectPath(kUser1ProfilePath), _, _));
669 733
670 EXPECT_CALL( 734 EXPECT_CALL(
671 *mock_profile_client_, 735 *mock_profile_client_,
672 GetEntry(dbus::ObjectPath(kUser1ProfilePath), "wifi2_entry_path", _, _)); 736 GetEntry(dbus::ObjectPath(kUser1ProfilePath), "wifi2_entry_path", _, _));
673 737
674 scoped_ptr<base::DictionaryValue> expected_shill_properties = 738 scoped_ptr<base::DictionaryValue> expected_shill_properties =
675 test_utils::ReadTestDictionary( 739 test_utils::ReadTestDictionary(
676 "policy/shill_disallow_autoconnect_on_unmanaged_wifi2.json"); 740 "policy/shill_disallow_autoconnect_on_unmanaged_wifi2.json");
677 741
678 EXPECT_CALL(*mock_manager_client_, 742 EXPECT_CALL(*mock_manager_client_,
679 ConfigureServiceForProfile( 743 ConfigureServiceForProfile(
680 dbus::ObjectPath(kUser1ProfilePath), 744 dbus::ObjectPath(kUser1ProfilePath),
681 IsEqualTo(expected_shill_properties.get()), 745 IsEqualTo(expected_shill_properties.get()),
682 _, _)); 746 _, _));
683 747
684 SetPolicy(::onc::ONC_SOURCE_USER_POLICY, 748 SetPolicy(::onc::ONC_SOURCE_USER_POLICY,
685 kUser1, 749 kUser1,
686 "policy/policy_disallow_autoconnect.onc"); 750 "policy/policy_disallow_autoconnect.onc");
687 message_loop_.RunUntilIdle(); 751 message_loop_.RunUntilIdle();
752
753 // Verify that GetManagedProperties correctly augments the properties with the
754 // global config from the user policy.
755
756 // GetManagedProperties requires the device policy to be set or explicitly
757 // unset.
758 EXPECT_CALL(*mock_profile_client_,
759 GetProperties(dbus::ObjectPath(
760 NetworkProfileHandler::GetSharedProfilePath()),
761 _,
762 _));
763 managed_handler()->SetPolicy(
764 ::onc::ONC_SOURCE_DEVICE_POLICY,
765 std::string(), // no userhash
766 base::ListValue(), // no device network policy
767 base::DictionaryValue()); // no device global config
768
769 services_stub_.SetFakeProperties(*expected_shill_properties);
770 EXPECT_CALL(*mock_service_client_,
771 GetProperties(dbus::ObjectPath(
772 "wifi2"),_));
773
774 GetManagedProperties(kUser1, "wifi2");
775 message_loop_.RunUntilIdle();
776
777 EXPECT_EQ("wifi2", get_properties_service_path_);
778
779 scoped_ptr<base::DictionaryValue> expected_managed_onc =
780 test_utils::ReadTestDictionary(
781 "policy/managed_onc_disallow_autoconnect_on_unmanaged_wifi2.onc");
782 EXPECT_TRUE(onc::test_utils::Equals(expected_managed_onc.get(),
783 &get_properties_result_));
688 } 784 }
689 785
690 TEST_F(ManagedNetworkConfigurationHandlerTest, LateProfileLoading) { 786 TEST_F(ManagedNetworkConfigurationHandlerTest, LateProfileLoading) {
691 SetPolicy(::onc::ONC_SOURCE_USER_POLICY, kUser1, "policy/policy_wifi1.onc"); 787 SetPolicy(::onc::ONC_SOURCE_USER_POLICY, kUser1, "policy/policy_wifi1.onc");
692 788
693 message_loop_.RunUntilIdle(); 789 message_loop_.RunUntilIdle();
694 VerifyAndClearExpectations(); 790 VerifyAndClearExpectations();
695 791
696 scoped_ptr<base::DictionaryValue> expected_shill_properties = 792 scoped_ptr<base::DictionaryValue> expected_shill_properties =
697 test_utils::ReadTestDictionary( 793 test_utils::ReadTestDictionary(
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after
742 838
743 EXPECT_CALL(*mock_profile_client_, 839 EXPECT_CALL(*mock_profile_client_,
744 GetProperties(dbus::ObjectPath(kUser1ProfilePath), _, _)); 840 GetProperties(dbus::ObjectPath(kUser1ProfilePath), _, _));
745 841
746 SetPolicy(::onc::ONC_SOURCE_USER_POLICY, kUser1, "policy/policy_wifi1.onc"); 842 SetPolicy(::onc::ONC_SOURCE_USER_POLICY, kUser1, "policy/policy_wifi1.onc");
747 managed_network_configuration_handler_.reset(); 843 managed_network_configuration_handler_.reset();
748 message_loop_.RunUntilIdle(); 844 message_loop_.RunUntilIdle();
749 } 845 }
750 846
751 } // namespace chromeos 847 } // namespace chromeos
OLDNEW
« no previous file with comments | « chromeos/network/managed_network_configuration_handler_impl.cc ('k') | chromeos/network/policy_applicator.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698