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.
9 #include "base/location.h"
10 #include "base/memory/scoped_ptr.h"
11 #include "base/message_loop/message_loop.h"
12 #include "base/stl_util.h"
13 #include "base/values.h"
14 #include "chromeos/dbus/dbus_thread_manager.h"
15 #include "chromeos/dbus/mock_shill_manager_client.h"
16 #include "chromeos/dbus/mock_shill_profile_client.h"
17 #include "chromeos/dbus/mock_shill_service_client.h"
18 #include "chromeos/dbus/shill_client_helper.h"
19 #include "chromeos/network/managed_network_configuration_handler_impl.h"
20 #include "chromeos/network/network_configuration_handler.h"
21 #include "chromeos/network/network_profile_handler.h"
22 #include "chromeos/network/onc/onc_test_utils.h"
23 #include "chromeos/network/onc/onc_utils.h"
24 #include "dbus/object_path.h"
25 #include "testing/gmock/include/gmock/gmock.h"
26 #include "testing/gtest/include/gtest/gtest.h"
27 #include "third_party/cros_system_api/dbus/service_constants.h"
29 using ::testing::AnyNumber;
30 using ::testing::Invoke;
31 using ::testing::Mock;
32 using ::testing::Pointee;
33 using ::testing::Return;
34 using ::testing::SaveArg;
35 using ::testing::StrEq;
36 using ::testing::StrictMock;
39 namespace test_utils = ::chromeos::onc::test_utils;
45 std::string ValueToString(const base::Value* value) {
46 std::stringstream str;
51 const char kUser1[] = "user1";
52 const char kUser1ProfilePath[] = "/profile/user1/shill";
54 // Matcher to match base::Value.
57 std::string(negation ? "isn't" : "is") + " equal to " +
58 ValueToString(value)) {
59 return value->Equals(&arg);
62 class ShillProfileTestClient {
64 typedef ShillClientHelper::DictionaryValueCallbackWithoutStatus
65 DictionaryValueCallbackWithoutStatus;
66 typedef ShillClientHelper::ErrorCallback ErrorCallback;
68 void AddProfile(const std::string& profile_path,
69 const std::string& userhash) {
70 if (profile_entries_.HasKey(profile_path))
73 base::DictionaryValue* profile = new base::DictionaryValue;
74 profile_entries_.SetWithoutPathExpansion(profile_path, profile);
75 profile_to_user_[profile_path] = userhash;
78 void AddEntry(const std::string& profile_path,
79 const std::string& entry_path,
80 const base::DictionaryValue& entry) {
81 base::DictionaryValue* entries = NULL;
82 profile_entries_.GetDictionaryWithoutPathExpansion(profile_path, &entries);
85 base::DictionaryValue* new_entry = entry.DeepCopy();
86 new_entry->SetStringWithoutPathExpansion(shill::kProfileProperty,
88 entries->SetWithoutPathExpansion(entry_path, new_entry);
91 void GetProperties(const dbus::ObjectPath& profile_path,
92 const DictionaryValueCallbackWithoutStatus& callback,
93 const ErrorCallback& error_callback) {
94 base::DictionaryValue* entries = NULL;
95 profile_entries_.GetDictionaryWithoutPathExpansion(profile_path.value(),
99 scoped_ptr<base::DictionaryValue> result(new base::DictionaryValue);
100 base::ListValue* entry_paths = new base::ListValue;
101 result->SetWithoutPathExpansion(shill::kEntriesProperty, entry_paths);
102 for (base::DictionaryValue::Iterator it(*entries); !it.IsAtEnd();
104 entry_paths->AppendString(it.key());
107 ASSERT_TRUE(ContainsKey(profile_to_user_, profile_path.value()));
108 const std::string& userhash = profile_to_user_[profile_path.value()];
109 result->SetStringWithoutPathExpansion(shill::kUserHashProperty, userhash);
111 callback.Run(*result);
114 void GetEntry(const dbus::ObjectPath& profile_path,
115 const std::string& entry_path,
116 const DictionaryValueCallbackWithoutStatus& callback,
117 const ErrorCallback& error_callback) {
118 base::DictionaryValue* entries = NULL;
119 profile_entries_.GetDictionaryWithoutPathExpansion(profile_path.value(),
121 ASSERT_TRUE(entries);
123 base::DictionaryValue* entry = NULL;
124 entries->GetDictionaryWithoutPathExpansion(entry_path, &entry);
126 callback.Run(*entry);
130 base::DictionaryValue profile_entries_;
131 std::map<std::string, std::string> profile_to_user_;
134 class ShillServiceTestClient {
136 typedef ShillClientHelper::DictionaryValueCallback DictionaryValueCallback;
137 void SetFakeProperties(const base::DictionaryValue& service_properties) {
138 service_properties_.Clear();
139 service_properties_.MergeDictionary(&service_properties);
142 void GetProperties(const dbus::ObjectPath& service_path,
143 const DictionaryValueCallback& callback) {
144 base::MessageLoop::current()->PostTask(
147 DBUS_METHOD_CALL_SUCCESS,
148 base::ConstRef(service_properties_)));
152 base::DictionaryValue service_properties_;
155 class TestNetworkProfileHandler : public NetworkProfileHandler {
157 TestNetworkProfileHandler() {
160 virtual ~TestNetworkProfileHandler() {}
162 void AddProfileForTest(const NetworkProfile& profile) {
167 DISALLOW_COPY_AND_ASSIGN(TestNetworkProfileHandler);
172 class ManagedNetworkConfigurationHandlerTest : public testing::Test {
174 ManagedNetworkConfigurationHandlerTest()
175 : mock_manager_client_(NULL),
176 mock_profile_client_(NULL),
177 mock_service_client_(NULL) {
180 virtual ~ManagedNetworkConfigurationHandlerTest() {
183 virtual void SetUp() OVERRIDE {
184 scoped_ptr<DBusThreadManagerSetter> dbus_setter =
185 DBusThreadManager::GetSetterForTesting();
186 mock_manager_client_ = new StrictMock<MockShillManagerClient>();
187 mock_profile_client_ = new StrictMock<MockShillProfileClient>();
188 mock_service_client_ = new StrictMock<MockShillServiceClient>();
189 dbus_setter->SetShillManagerClient(
190 scoped_ptr<ShillManagerClient>(mock_manager_client_).Pass());
191 dbus_setter->SetShillProfileClient(
192 scoped_ptr<ShillProfileClient>(mock_profile_client_).Pass());
193 dbus_setter->SetShillServiceClient(
194 scoped_ptr<ShillServiceClient>(mock_service_client_).Pass());
196 SetNetworkConfigurationHandlerExpectations();
198 ON_CALL(*mock_profile_client_, GetProperties(_,_,_))
199 .WillByDefault(Invoke(&profiles_stub_,
200 &ShillProfileTestClient::GetProperties));
202 ON_CALL(*mock_profile_client_, GetEntry(_,_,_,_))
203 .WillByDefault(Invoke(&profiles_stub_,
204 &ShillProfileTestClient::GetEntry));
206 ON_CALL(*mock_service_client_, GetProperties(_,_))
207 .WillByDefault(Invoke(&services_stub_,
208 &ShillServiceTestClient::GetProperties));
210 network_profile_handler_.reset(new TestNetworkProfileHandler());
211 network_configuration_handler_.reset(
212 NetworkConfigurationHandler::InitializeForTest(
213 NULL /* no NetworkStateHandler */));
214 managed_network_configuration_handler_.reset(
215 new ManagedNetworkConfigurationHandlerImpl());
216 managed_network_configuration_handler_->Init(
217 NULL /* no NetworkStateHandler */,
218 network_profile_handler_.get(),
219 network_configuration_handler_.get(),
220 NULL /* no DeviceHandler */);
222 message_loop_.RunUntilIdle();
225 virtual void TearDown() OVERRIDE {
226 managed_network_configuration_handler_.reset();
227 network_configuration_handler_.reset();
228 network_profile_handler_.reset();
229 DBusThreadManager::Shutdown();
232 void VerifyAndClearExpectations() {
233 Mock::VerifyAndClearExpectations(mock_manager_client_);
234 Mock::VerifyAndClearExpectations(mock_profile_client_);
235 SetNetworkConfigurationHandlerExpectations();
238 void InitializeStandardProfiles() {
239 profiles_stub_.AddProfile(kUser1ProfilePath, kUser1);
240 network_profile_handler_->
241 AddProfileForTest(NetworkProfile(kUser1ProfilePath, kUser1));
243 profiles_stub_.AddProfile(NetworkProfileHandler::GetSharedProfilePath(),
244 std::string() /* no userhash */);
245 network_profile_handler_->AddProfileForTest(
246 NetworkProfile(NetworkProfileHandler::GetSharedProfilePath(),
247 std::string() /* no userhash */));
250 void SetUpEntry(const std::string& path_to_shill_json,
251 const std::string& profile_path,
252 const std::string& entry_path) {
253 scoped_ptr<base::DictionaryValue> entry =
254 test_utils::ReadTestDictionary(path_to_shill_json);
255 profiles_stub_.AddEntry(profile_path, entry_path, *entry);
258 void SetPolicy(::onc::ONCSource onc_source,
259 const std::string& userhash,
260 const std::string& path_to_onc) {
261 scoped_ptr<base::DictionaryValue> policy;
262 if (path_to_onc.empty())
263 policy = onc::ReadDictionaryFromJson(onc::kEmptyUnencryptedConfiguration);
265 policy = test_utils::ReadTestDictionary(path_to_onc);
267 base::ListValue empty_network_configs;
268 base::ListValue* network_configs = &empty_network_configs;
269 policy->GetListWithoutPathExpansion(
270 ::onc::toplevel_config::kNetworkConfigurations, &network_configs);
272 base::DictionaryValue empty_global_config;
273 base::DictionaryValue* global_network_config = &empty_global_config;
274 policy->GetDictionaryWithoutPathExpansion(
275 ::onc::toplevel_config::kGlobalNetworkConfiguration,
276 &global_network_config);
278 managed_handler()->SetPolicy(
279 onc_source, userhash, *network_configs, *global_network_config);
282 void SetNetworkConfigurationHandlerExpectations() {
283 // These calls occur in NetworkConfigurationHandler.
284 EXPECT_CALL(*mock_manager_client_, GetProperties(_)).Times(AnyNumber());
285 EXPECT_CALL(*mock_manager_client_,
286 AddPropertyChangedObserver(_)).Times(AnyNumber());
287 EXPECT_CALL(*mock_manager_client_,
288 RemovePropertyChangedObserver(_)).Times(AnyNumber());
291 ManagedNetworkConfigurationHandler* managed_handler() {
292 return managed_network_configuration_handler_.get();
295 void GetManagedProperties(const std::string& userhash,
296 const std::string& service_path) {
297 managed_handler()->GetManagedProperties(
301 &ManagedNetworkConfigurationHandlerTest::GetPropertiesCallback,
302 base::Unretained(this)),
303 base::Bind(&ManagedNetworkConfigurationHandlerTest::UnexpectedError));
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);
313 static void UnexpectedError(const std::string& error_name,
314 scoped_ptr<base::DictionaryValue> error_data) {
319 MockShillManagerClient* mock_manager_client_;
320 MockShillProfileClient* mock_profile_client_;
321 MockShillServiceClient* mock_service_client_;
322 ShillProfileTestClient profiles_stub_;
323 ShillServiceTestClient services_stub_;
324 scoped_ptr<TestNetworkProfileHandler> network_profile_handler_;
325 scoped_ptr<NetworkConfigurationHandler> network_configuration_handler_;
326 scoped_ptr<ManagedNetworkConfigurationHandlerImpl>
327 managed_network_configuration_handler_;
328 base::MessageLoop message_loop_;
330 std::string get_properties_service_path_;
331 base::DictionaryValue get_properties_result_;
334 DISALLOW_COPY_AND_ASSIGN(ManagedNetworkConfigurationHandlerTest);
337 TEST_F(ManagedNetworkConfigurationHandlerTest, ProfileInitialization) {
338 InitializeStandardProfiles();
339 message_loop_.RunUntilIdle();
342 TEST_F(ManagedNetworkConfigurationHandlerTest, RemoveIrrelevantFields) {
343 InitializeStandardProfiles();
344 scoped_ptr<base::DictionaryValue> expected_shill_properties =
345 test_utils::ReadTestDictionary(
346 "policy/shill_policy_on_unconfigured_wifi1.json");
348 EXPECT_CALL(*mock_profile_client_,
349 GetProperties(dbus::ObjectPath(kUser1ProfilePath), _, _));
351 EXPECT_CALL(*mock_manager_client_,
352 ConfigureServiceForProfile(
353 dbus::ObjectPath(kUser1ProfilePath),
354 IsEqualTo(expected_shill_properties.get()),
357 SetPolicy(::onc::ONC_SOURCE_USER_POLICY,
359 "policy/policy_wifi1_with_redundant_fields.onc");
360 message_loop_.RunUntilIdle();
363 TEST_F(ManagedNetworkConfigurationHandlerTest, SetPolicyManageUnconfigured) {
364 InitializeStandardProfiles();
365 scoped_ptr<base::DictionaryValue> expected_shill_properties =
366 test_utils::ReadTestDictionary(
367 "policy/shill_policy_on_unconfigured_wifi1.json");
369 EXPECT_CALL(*mock_profile_client_,
370 GetProperties(dbus::ObjectPath(kUser1ProfilePath), _, _));
372 EXPECT_CALL(*mock_manager_client_,
373 ConfigureServiceForProfile(
374 dbus::ObjectPath(kUser1ProfilePath),
375 IsEqualTo(expected_shill_properties.get()),
378 SetPolicy(::onc::ONC_SOURCE_USER_POLICY, kUser1, "policy/policy_wifi1.onc");
379 message_loop_.RunUntilIdle();
382 // Ensure that EAP settings for ethernet are matched with the right profile
383 // entry and written to the dedicated EthernetEAP service.
384 TEST_F(ManagedNetworkConfigurationHandlerTest,
385 SetPolicyManageUnmanagedEthernetEAP) {
386 InitializeStandardProfiles();
387 scoped_ptr<base::DictionaryValue> expected_shill_properties =
388 test_utils::ReadTestDictionary(
390 "shill_policy_on_unmanaged_ethernet_eap.json");
392 SetUpEntry("policy/shill_unmanaged_ethernet_eap.json",
396 // Also setup an unrelated WiFi configuration to verify that the right entry
398 SetUpEntry("policy/shill_unmanaged_wifi1.json",
402 EXPECT_CALL(*mock_profile_client_,
403 GetProperties(dbus::ObjectPath(kUser1ProfilePath), _, _));
405 EXPECT_CALL(*mock_profile_client_,
406 GetEntry(dbus::ObjectPath(kUser1ProfilePath), _, _, _)).Times(2);
409 *mock_profile_client_,
410 DeleteEntry(dbus::ObjectPath(kUser1ProfilePath), "eth_entry", _, _));
413 *mock_manager_client_,
414 ConfigureServiceForProfile(dbus::ObjectPath(kUser1ProfilePath),
415 IsEqualTo(expected_shill_properties.get()),
419 ::onc::ONC_SOURCE_USER_POLICY, kUser1, "policy/policy_ethernet_eap.onc");
420 message_loop_.RunUntilIdle();
423 TEST_F(ManagedNetworkConfigurationHandlerTest, SetPolicyIgnoreUnmodified) {
424 InitializeStandardProfiles();
425 EXPECT_CALL(*mock_profile_client_, GetProperties(_, _, _));
427 EXPECT_CALL(*mock_manager_client_, ConfigureServiceForProfile(_, _, _, _));
429 SetPolicy(::onc::ONC_SOURCE_USER_POLICY, kUser1, "policy/policy_wifi1.onc");
430 message_loop_.RunUntilIdle();
431 VerifyAndClearExpectations();
433 SetUpEntry("policy/shill_policy_on_unmanaged_wifi1.json",
437 EXPECT_CALL(*mock_profile_client_, GetProperties(_, _, _));
440 *mock_profile_client_,
441 GetEntry(dbus::ObjectPath(kUser1ProfilePath), "some_entry_path", _, _));
443 SetPolicy(::onc::ONC_SOURCE_USER_POLICY, kUser1, "policy/policy_wifi1.onc");
444 message_loop_.RunUntilIdle();
447 TEST_F(ManagedNetworkConfigurationHandlerTest, SetPolicyManageUnmanaged) {
448 InitializeStandardProfiles();
449 SetUpEntry("policy/shill_unmanaged_wifi1.json",
453 scoped_ptr<base::DictionaryValue> expected_shill_properties =
454 test_utils::ReadTestDictionary(
455 "policy/shill_policy_on_unmanaged_wifi1.json");
457 EXPECT_CALL(*mock_profile_client_,
458 GetProperties(dbus::ObjectPath(kUser1ProfilePath), _, _));
461 *mock_profile_client_,
462 GetEntry(dbus::ObjectPath(kUser1ProfilePath), "old_entry_path", _, _));
465 *mock_profile_client_,
466 DeleteEntry(dbus::ObjectPath(kUser1ProfilePath), "old_entry_path", _, _));
468 EXPECT_CALL(*mock_manager_client_,
469 ConfigureServiceForProfile(
470 dbus::ObjectPath(kUser1ProfilePath),
471 IsEqualTo(expected_shill_properties.get()),
474 SetPolicy(::onc::ONC_SOURCE_USER_POLICY, kUser1, "policy/policy_wifi1.onc");
475 message_loop_.RunUntilIdle();
478 // Old ChromeOS versions may not have used the UIData property
479 TEST_F(ManagedNetworkConfigurationHandlerTest,
480 SetPolicyManageUnmanagedWithoutUIData) {
481 InitializeStandardProfiles();
482 SetUpEntry("policy/shill_unmanaged_wifi1.json",
486 scoped_ptr<base::DictionaryValue> expected_shill_properties =
487 test_utils::ReadTestDictionary(
488 "policy/shill_policy_on_unmanaged_wifi1.json");
490 EXPECT_CALL(*mock_profile_client_,
491 GetProperties(dbus::ObjectPath(kUser1ProfilePath), _, _));
494 *mock_profile_client_,
495 GetEntry(dbus::ObjectPath(kUser1ProfilePath), "old_entry_path", _, _));
498 *mock_profile_client_,
499 DeleteEntry(dbus::ObjectPath(kUser1ProfilePath), "old_entry_path", _, _));
501 EXPECT_CALL(*mock_manager_client_,
502 ConfigureServiceForProfile(
503 dbus::ObjectPath(kUser1ProfilePath),
504 IsEqualTo(expected_shill_properties.get()),
507 SetPolicy(::onc::ONC_SOURCE_USER_POLICY, kUser1, "policy/policy_wifi1.onc");
508 message_loop_.RunUntilIdle();
511 TEST_F(ManagedNetworkConfigurationHandlerTest, SetPolicyUpdateManagedNewGUID) {
512 InitializeStandardProfiles();
513 SetUpEntry("policy/shill_managed_wifi1.json",
517 scoped_ptr<base::DictionaryValue> expected_shill_properties =
518 test_utils::ReadTestDictionary(
519 "policy/shill_policy_on_unmanaged_wifi1.json");
521 // The passphrase isn't sent again, because it's configured by the user and
522 // Shill doesn't send it on GetProperties calls.
523 expected_shill_properties->RemoveWithoutPathExpansion(
524 shill::kPassphraseProperty, NULL);
526 EXPECT_CALL(*mock_profile_client_,
527 GetProperties(dbus::ObjectPath(kUser1ProfilePath), _, _));
530 *mock_profile_client_,
531 GetEntry(dbus::ObjectPath(kUser1ProfilePath), "old_entry_path", _, _));
534 *mock_profile_client_,
535 DeleteEntry(dbus::ObjectPath(kUser1ProfilePath), "old_entry_path", _, _));
537 EXPECT_CALL(*mock_manager_client_,
538 ConfigureServiceForProfile(
539 dbus::ObjectPath(kUser1ProfilePath),
540 IsEqualTo(expected_shill_properties.get()),
543 SetPolicy(::onc::ONC_SOURCE_USER_POLICY, kUser1, "policy/policy_wifi1.onc");
544 message_loop_.RunUntilIdle();
547 TEST_F(ManagedNetworkConfigurationHandlerTest, SetPolicyUpdateManagedVPN) {
548 InitializeStandardProfiles();
549 SetUpEntry("policy/shill_managed_vpn.json", kUser1ProfilePath, "entry_path");
551 scoped_ptr<base::DictionaryValue> expected_shill_properties =
552 test_utils::ReadTestDictionary(
553 "policy/shill_policy_on_managed_vpn.json");
555 EXPECT_CALL(*mock_profile_client_,
556 GetProperties(dbus::ObjectPath(kUser1ProfilePath), _, _));
559 *mock_profile_client_,
560 GetEntry(dbus::ObjectPath(kUser1ProfilePath), "entry_path", _, _));
562 EXPECT_CALL(*mock_manager_client_,
563 ConfigureServiceForProfile(
564 dbus::ObjectPath(kUser1ProfilePath),
565 IsEqualTo(expected_shill_properties.get()),
568 SetPolicy(::onc::ONC_SOURCE_USER_POLICY, kUser1, "policy/policy_vpn.onc");
569 message_loop_.RunUntilIdle();
570 VerifyAndClearExpectations();
573 TEST_F(ManagedNetworkConfigurationHandlerTest,
574 SetPolicyUpdateManagedEquivalentSecurity) {
575 InitializeStandardProfiles();
576 SetUpEntry("policy/shill_managed_wifi1_rsn.json",
580 scoped_ptr<base::DictionaryValue> expected_shill_properties =
581 test_utils::ReadTestDictionary(
582 "policy/shill_policy_on_unmanaged_wifi1.json");
584 // The passphrase isn't sent again, because it's configured by the user and
585 // Shill doesn't send it on GetProperties calls.
586 expected_shill_properties->RemoveWithoutPathExpansion(
587 shill::kPassphraseProperty, NULL);
589 EXPECT_CALL(*mock_profile_client_,
590 GetProperties(dbus::ObjectPath(kUser1ProfilePath), _, _));
593 *mock_profile_client_,
594 GetEntry(dbus::ObjectPath(kUser1ProfilePath), "old_entry_path", _, _));
596 // The existing entry must not be deleted because the Security type 'rsa' is
597 // equivalent to 'psk' when identifying networks.
600 *mock_manager_client_,
601 ConfigureServiceForProfile(dbus::ObjectPath(kUser1ProfilePath),
602 IsEqualTo(expected_shill_properties.get()),
605 SetPolicy(::onc::ONC_SOURCE_USER_POLICY, kUser1, "policy/policy_wifi1.onc");
606 message_loop_.RunUntilIdle();
609 TEST_F(ManagedNetworkConfigurationHandlerTest, SetPolicyReapplyToManaged) {
610 InitializeStandardProfiles();
611 SetUpEntry("policy/shill_policy_on_unmanaged_wifi1.json",
615 scoped_ptr<base::DictionaryValue> expected_shill_properties =
616 test_utils::ReadTestDictionary(
617 "policy/shill_policy_on_unmanaged_wifi1.json");
619 // The passphrase isn't sent again, because it's configured by the user and
620 // Shill doesn't send it on GetProperties calls.
621 expected_shill_properties->RemoveWithoutPathExpansion(
622 shill::kPassphraseProperty, NULL);
624 EXPECT_CALL(*mock_profile_client_,
625 GetProperties(dbus::ObjectPath(kUser1ProfilePath), _, _));
628 *mock_profile_client_,
629 GetEntry(dbus::ObjectPath(kUser1ProfilePath), "old_entry_path", _, _));
631 EXPECT_CALL(*mock_manager_client_,
632 ConfigureServiceForProfile(
633 dbus::ObjectPath(kUser1ProfilePath),
634 IsEqualTo(expected_shill_properties.get()),
637 SetPolicy(::onc::ONC_SOURCE_USER_POLICY, kUser1, "policy/policy_wifi1.onc");
638 message_loop_.RunUntilIdle();
639 VerifyAndClearExpectations();
641 // If we apply the policy again, without change, then the Shill profile will
643 EXPECT_CALL(*mock_profile_client_,
644 GetProperties(dbus::ObjectPath(kUser1ProfilePath), _, _));
647 *mock_profile_client_,
648 GetEntry(dbus::ObjectPath(kUser1ProfilePath), "old_entry_path", _, _));
650 SetPolicy(::onc::ONC_SOURCE_USER_POLICY, kUser1, "policy/policy_wifi1.onc");
651 message_loop_.RunUntilIdle();
654 TEST_F(ManagedNetworkConfigurationHandlerTest, SetPolicyUnmanageManaged) {
655 InitializeStandardProfiles();
656 SetUpEntry("policy/shill_policy_on_unmanaged_wifi1.json",
660 EXPECT_CALL(*mock_profile_client_,
661 GetProperties(dbus::ObjectPath(kUser1ProfilePath), _, _));
663 EXPECT_CALL(*mock_profile_client_,
664 GetEntry(dbus::ObjectPath(kUser1ProfilePath),
668 EXPECT_CALL(*mock_profile_client_,
669 DeleteEntry(dbus::ObjectPath(kUser1ProfilePath),
673 SetPolicy(::onc::ONC_SOURCE_USER_POLICY, kUser1, "");
674 message_loop_.RunUntilIdle();
677 TEST_F(ManagedNetworkConfigurationHandlerTest, SetEmptyPolicyIgnoreUnmanaged) {
678 InitializeStandardProfiles();
679 SetUpEntry("policy/shill_unmanaged_wifi1.json",
683 EXPECT_CALL(*mock_profile_client_,
684 GetProperties(dbus::ObjectPath(kUser1ProfilePath), _, _));
686 EXPECT_CALL(*mock_profile_client_,
687 GetEntry(dbus::ObjectPath(kUser1ProfilePath),
691 SetPolicy(::onc::ONC_SOURCE_USER_POLICY, kUser1, "");
692 message_loop_.RunUntilIdle();
695 TEST_F(ManagedNetworkConfigurationHandlerTest, SetPolicyIgnoreUnmanaged) {
696 InitializeStandardProfiles();
697 SetUpEntry("policy/shill_unmanaged_wifi2.json",
701 EXPECT_CALL(*mock_profile_client_,
702 GetProperties(dbus::ObjectPath(kUser1ProfilePath), _, _));
705 *mock_profile_client_,
706 GetEntry(dbus::ObjectPath(kUser1ProfilePath), "wifi2_entry_path", _, _));
708 scoped_ptr<base::DictionaryValue> expected_shill_properties =
709 test_utils::ReadTestDictionary(
710 "policy/shill_policy_on_unconfigured_wifi1.json");
712 EXPECT_CALL(*mock_manager_client_,
713 ConfigureServiceForProfile(
714 dbus::ObjectPath(kUser1ProfilePath),
715 IsEqualTo(expected_shill_properties.get()),
718 SetPolicy(::onc::ONC_SOURCE_USER_POLICY, kUser1, "policy/policy_wifi1.onc");
719 message_loop_.RunUntilIdle();
722 TEST_F(ManagedNetworkConfigurationHandlerTest, AutoConnectDisallowed) {
723 InitializeStandardProfiles();
724 // Setup an unmanaged network.
725 SetUpEntry("policy/shill_unmanaged_wifi2.json",
729 // Apply the user policy with global autoconnect config and expect that
730 // autoconnect is disabled in the network's profile entry.
731 EXPECT_CALL(*mock_profile_client_,
732 GetProperties(dbus::ObjectPath(kUser1ProfilePath), _, _));
735 *mock_profile_client_,
736 GetEntry(dbus::ObjectPath(kUser1ProfilePath), "wifi2_entry_path", _, _));
738 scoped_ptr<base::DictionaryValue> expected_shill_properties =
739 test_utils::ReadTestDictionary(
740 "policy/shill_disallow_autoconnect_on_unmanaged_wifi2.json");
742 EXPECT_CALL(*mock_manager_client_,
743 ConfigureServiceForProfile(
744 dbus::ObjectPath(kUser1ProfilePath),
745 IsEqualTo(expected_shill_properties.get()),
748 SetPolicy(::onc::ONC_SOURCE_USER_POLICY,
750 "policy/policy_disallow_autoconnect.onc");
751 message_loop_.RunUntilIdle();
753 // Verify that GetManagedProperties correctly augments the properties with the
754 // global config from the user policy.
756 // GetManagedProperties requires the device policy to be set or explicitly
758 EXPECT_CALL(*mock_profile_client_,
759 GetProperties(dbus::ObjectPath(
760 NetworkProfileHandler::GetSharedProfilePath()),
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
769 services_stub_.SetFakeProperties(*expected_shill_properties);
770 EXPECT_CALL(*mock_service_client_,
771 GetProperties(dbus::ObjectPath(
774 GetManagedProperties(kUser1, "wifi2");
775 message_loop_.RunUntilIdle();
777 EXPECT_EQ("wifi2", get_properties_service_path_);
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_));
786 TEST_F(ManagedNetworkConfigurationHandlerTest, LateProfileLoading) {
787 SetPolicy(::onc::ONC_SOURCE_USER_POLICY, kUser1, "policy/policy_wifi1.onc");
789 message_loop_.RunUntilIdle();
790 VerifyAndClearExpectations();
792 scoped_ptr<base::DictionaryValue> expected_shill_properties =
793 test_utils::ReadTestDictionary(
794 "policy/shill_policy_on_unconfigured_wifi1.json");
796 EXPECT_CALL(*mock_profile_client_,
797 GetProperties(dbus::ObjectPath(kUser1ProfilePath), _, _));
799 EXPECT_CALL(*mock_manager_client_,
800 ConfigureServiceForProfile(
801 dbus::ObjectPath(kUser1ProfilePath),
802 IsEqualTo(expected_shill_properties.get()),
805 InitializeStandardProfiles();
806 message_loop_.RunUntilIdle();
809 class ManagedNetworkConfigurationHandlerShutdownTest
810 : public ManagedNetworkConfigurationHandlerTest {
812 virtual void SetUp() OVERRIDE {
813 ManagedNetworkConfigurationHandlerTest::SetUp();
814 ON_CALL(*mock_profile_client_, GetProperties(_, _, _)).WillByDefault(
815 Invoke(&ManagedNetworkConfigurationHandlerShutdownTest::GetProperties));
818 static void GetProperties(
819 const dbus::ObjectPath& profile_path,
820 const ShillClientHelper::DictionaryValueCallbackWithoutStatus& callback,
821 const ShillClientHelper::ErrorCallback& error_callback) {
822 base::MessageLoop::current()->PostTask(
824 base::Bind(ManagedNetworkConfigurationHandlerShutdownTest::
825 CallbackWithEmptyDictionary,
829 static void CallbackWithEmptyDictionary(
830 const ShillClientHelper::DictionaryValueCallbackWithoutStatus& callback) {
831 callback.Run(base::DictionaryValue());
835 TEST_F(ManagedNetworkConfigurationHandlerShutdownTest,
836 DuringPolicyApplication) {
837 InitializeStandardProfiles();
839 EXPECT_CALL(*mock_profile_client_,
840 GetProperties(dbus::ObjectPath(kUser1ProfilePath), _, _));
842 SetPolicy(::onc::ONC_SOURCE_USER_POLICY, kUser1, "policy/policy_wifi1.onc");
843 managed_network_configuration_handler_.reset();
844 message_loop_.RunUntilIdle();
847 } // namespace chromeos