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/bind_helpers.h"
10 #include "base/location.h"
11 #include "base/memory/scoped_ptr.h"
12 #include "base/message_loop/message_loop.h"
13 #include "base/stl_util.h"
14 #include "base/values.h"
15 #include "chromeos/dbus/dbus_thread_manager.h"
16 #include "chromeos/dbus/mock_shill_manager_client.h"
17 #include "chromeos/dbus/mock_shill_profile_client.h"
18 #include "chromeos/dbus/mock_shill_service_client.h"
19 #include "chromeos/dbus/shill_client_helper.h"
20 #include "chromeos/network/managed_network_configuration_handler_impl.h"
21 #include "chromeos/network/network_configuration_handler.h"
22 #include "chromeos/network/network_policy_observer.h"
23 #include "chromeos/network/network_profile_handler.h"
24 #include "chromeos/network/network_state_handler.h"
25 #include "chromeos/network/onc/onc_test_utils.h"
26 #include "chromeos/network/onc/onc_utils.h"
27 #include "dbus/object_path.h"
28 #include "testing/gmock/include/gmock/gmock.h"
29 #include "testing/gtest/include/gtest/gtest.h"
30 #include "third_party/cros_system_api/dbus/service_constants.h"
32 using ::testing::AnyNumber
;
33 using ::testing::Invoke
;
34 using ::testing::Mock
;
35 using ::testing::Pointee
;
36 using ::testing::Return
;
37 using ::testing::SaveArg
;
38 using ::testing::StrEq
;
39 using ::testing::StrictMock
;
42 namespace test_utils
= ::chromeos::onc::test_utils
;
48 std::string
ValueToString(const base::Value
* value
) {
49 std::stringstream str
;
54 void DereferenceAndCall(
55 base::Callback
<void(const base::DictionaryValue
& result
)> callback
,
56 const base::DictionaryValue
* value
) {
60 const char kUser1
[] = "user1";
61 const char kUser1ProfilePath
[] = "/profile/user1/shill";
63 // Matcher to match base::Value.
66 std::string(negation
? "isn't" : "is") + " equal to " +
67 ValueToString(value
)) {
68 return value
->Equals(&arg
);
71 class ShillProfileTestClient
{
73 typedef ShillClientHelper::DictionaryValueCallbackWithoutStatus
74 DictionaryValueCallbackWithoutStatus
;
75 typedef ShillClientHelper::ErrorCallback ErrorCallback
;
77 void AddProfile(const std::string
& profile_path
,
78 const std::string
& userhash
) {
79 if (profile_entries_
.HasKey(profile_path
))
82 base::DictionaryValue
* profile
= new base::DictionaryValue
;
83 profile_entries_
.SetWithoutPathExpansion(profile_path
, profile
);
84 profile_to_user_
[profile_path
] = userhash
;
87 void AddEntry(const std::string
& profile_path
,
88 const std::string
& entry_path
,
89 const base::DictionaryValue
& entry
) {
90 base::DictionaryValue
* entries
= NULL
;
91 profile_entries_
.GetDictionaryWithoutPathExpansion(profile_path
, &entries
);
94 base::DictionaryValue
* new_entry
= entry
.DeepCopy();
95 new_entry
->SetStringWithoutPathExpansion(shill::kProfileProperty
,
97 entries
->SetWithoutPathExpansion(entry_path
, new_entry
);
100 void GetProperties(const dbus::ObjectPath
& profile_path
,
101 const DictionaryValueCallbackWithoutStatus
& callback
,
102 const ErrorCallback
& error_callback
) {
103 base::DictionaryValue
* entries
= NULL
;
104 profile_entries_
.GetDictionaryWithoutPathExpansion(profile_path
.value(),
106 ASSERT_TRUE(entries
);
108 scoped_ptr
<base::DictionaryValue
> result(new base::DictionaryValue
);
109 base::ListValue
* entry_paths
= new base::ListValue
;
110 result
->SetWithoutPathExpansion(shill::kEntriesProperty
, entry_paths
);
111 for (base::DictionaryValue::Iterator
it(*entries
); !it
.IsAtEnd();
113 entry_paths
->AppendString(it
.key());
116 ASSERT_TRUE(ContainsKey(profile_to_user_
, profile_path
.value()));
117 const std::string
& userhash
= profile_to_user_
[profile_path
.value()];
118 result
->SetStringWithoutPathExpansion(shill::kUserHashProperty
, userhash
);
120 base::MessageLoop::current()->PostTask(
122 base::Bind(base::Bind(&DereferenceAndCall
, callback
),
123 base::Owned(result
.release())));
126 void GetEntry(const dbus::ObjectPath
& profile_path
,
127 const std::string
& entry_path
,
128 const DictionaryValueCallbackWithoutStatus
& callback
,
129 const ErrorCallback
& error_callback
) {
130 base::DictionaryValue
* entries
= NULL
;
131 profile_entries_
.GetDictionaryWithoutPathExpansion(profile_path
.value(),
133 ASSERT_TRUE(entries
);
135 base::DictionaryValue
* entry
= NULL
;
136 entries
->GetDictionaryWithoutPathExpansion(entry_path
, &entry
);
138 base::MessageLoop::current()->PostTask(
140 base::Bind(base::Bind(&DereferenceAndCall
, callback
),
141 base::Owned(entry
->DeepCopy())));
145 base::DictionaryValue profile_entries_
;
146 std::map
<std::string
, std::string
> profile_to_user_
;
149 class ShillServiceTestClient
{
151 typedef ShillClientHelper::DictionaryValueCallback DictionaryValueCallback
;
152 void SetFakeProperties(const base::DictionaryValue
& service_properties
) {
153 service_properties_
.Clear();
154 service_properties_
.MergeDictionary(&service_properties
);
157 void GetProperties(const dbus::ObjectPath
& service_path
,
158 const DictionaryValueCallback
& callback
) {
159 base::MessageLoop::current()->PostTask(
162 DBUS_METHOD_CALL_SUCCESS
,
163 base::ConstRef(service_properties_
)));
167 base::DictionaryValue service_properties_
;
170 class TestNetworkProfileHandler
: public NetworkProfileHandler
{
172 TestNetworkProfileHandler() {
175 ~TestNetworkProfileHandler() override
{}
177 void AddProfileForTest(const NetworkProfile
& profile
) {
182 DISALLOW_COPY_AND_ASSIGN(TestNetworkProfileHandler
);
185 class TestNetworkPolicyObserver
: public NetworkPolicyObserver
{
187 TestNetworkPolicyObserver() : policies_applied_count_(0) {}
189 void PoliciesApplied(const std::string
& userhash
) override
{
190 policies_applied_count_
++;
193 int GetPoliciesAppliedCountAndReset() {
194 int count
= policies_applied_count_
;
195 policies_applied_count_
= 0;
200 int policies_applied_count_
;
202 DISALLOW_COPY_AND_ASSIGN(TestNetworkPolicyObserver
);
207 class ManagedNetworkConfigurationHandlerTest
: public testing::Test
{
209 ManagedNetworkConfigurationHandlerTest()
210 : mock_manager_client_(NULL
),
211 mock_profile_client_(NULL
),
212 mock_service_client_(NULL
) {
215 ~ManagedNetworkConfigurationHandlerTest() override
{}
217 void SetUp() override
{
218 scoped_ptr
<DBusThreadManagerSetter
> dbus_setter
=
219 DBusThreadManager::GetSetterForTesting();
220 mock_manager_client_
= new StrictMock
<MockShillManagerClient
>();
221 mock_profile_client_
= new StrictMock
<MockShillProfileClient
>();
222 mock_service_client_
= new StrictMock
<MockShillServiceClient
>();
223 dbus_setter
->SetShillManagerClient(
224 scoped_ptr
<ShillManagerClient
>(mock_manager_client_
).Pass());
225 dbus_setter
->SetShillProfileClient(
226 scoped_ptr
<ShillProfileClient
>(mock_profile_client_
).Pass());
227 dbus_setter
->SetShillServiceClient(
228 scoped_ptr
<ShillServiceClient
>(mock_service_client_
).Pass());
230 SetNetworkConfigurationHandlerExpectations();
232 ON_CALL(*mock_profile_client_
, GetProperties(_
,_
,_
))
233 .WillByDefault(Invoke(&profiles_stub_
,
234 &ShillProfileTestClient::GetProperties
));
236 ON_CALL(*mock_profile_client_
, GetEntry(_
,_
,_
,_
))
237 .WillByDefault(Invoke(&profiles_stub_
,
238 &ShillProfileTestClient::GetEntry
));
240 ON_CALL(*mock_service_client_
, GetProperties(_
,_
))
241 .WillByDefault(Invoke(&services_stub_
,
242 &ShillServiceTestClient::GetProperties
));
244 network_state_handler_
.reset(NetworkStateHandler::InitializeForTest());
245 network_profile_handler_
.reset(new TestNetworkProfileHandler());
246 network_configuration_handler_
.reset(
247 NetworkConfigurationHandler::InitializeForTest(
248 network_state_handler_
.get(),
249 NULL
/* no NetworkDeviceHandler */));
250 managed_network_configuration_handler_
.reset(
251 new ManagedNetworkConfigurationHandlerImpl());
252 managed_network_configuration_handler_
->Init(
253 network_state_handler_
.get(),
254 network_profile_handler_
.get(),
255 network_configuration_handler_
.get(),
256 NULL
/* no DeviceHandler */);
257 managed_network_configuration_handler_
->AddObserver(&policy_observer_
);
259 message_loop_
.RunUntilIdle();
262 void TearDown() override
{
263 if (managed_network_configuration_handler_
)
264 managed_network_configuration_handler_
->RemoveObserver(&policy_observer_
);
265 network_state_handler_
.reset();
266 managed_network_configuration_handler_
.reset();
267 network_configuration_handler_
.reset();
268 network_profile_handler_
.reset();
269 DBusThreadManager::Shutdown();
272 void VerifyAndClearExpectations() {
273 Mock::VerifyAndClearExpectations(mock_manager_client_
);
274 Mock::VerifyAndClearExpectations(mock_profile_client_
);
275 SetNetworkConfigurationHandlerExpectations();
278 void InitializeStandardProfiles() {
279 profiles_stub_
.AddProfile(kUser1ProfilePath
, kUser1
);
280 network_profile_handler_
->
281 AddProfileForTest(NetworkProfile(kUser1ProfilePath
, kUser1
));
283 profiles_stub_
.AddProfile(NetworkProfileHandler::GetSharedProfilePath(),
284 std::string() /* no userhash */);
285 network_profile_handler_
->AddProfileForTest(
286 NetworkProfile(NetworkProfileHandler::GetSharedProfilePath(),
287 std::string() /* no userhash */));
290 void SetUpEntry(const std::string
& path_to_shill_json
,
291 const std::string
& profile_path
,
292 const std::string
& entry_path
) {
293 scoped_ptr
<base::DictionaryValue
> entry
=
294 test_utils::ReadTestDictionary(path_to_shill_json
);
295 profiles_stub_
.AddEntry(profile_path
, entry_path
, *entry
);
298 void SetPolicy(::onc::ONCSource onc_source
,
299 const std::string
& userhash
,
300 const std::string
& path_to_onc
) {
301 scoped_ptr
<base::DictionaryValue
> policy
;
302 if (path_to_onc
.empty())
303 policy
= onc::ReadDictionaryFromJson(onc::kEmptyUnencryptedConfiguration
);
305 policy
= test_utils::ReadTestDictionary(path_to_onc
);
307 base::ListValue empty_network_configs
;
308 base::ListValue
* network_configs
= &empty_network_configs
;
309 policy
->GetListWithoutPathExpansion(
310 ::onc::toplevel_config::kNetworkConfigurations
, &network_configs
);
312 base::DictionaryValue empty_global_config
;
313 base::DictionaryValue
* global_network_config
= &empty_global_config
;
314 policy
->GetDictionaryWithoutPathExpansion(
315 ::onc::toplevel_config::kGlobalNetworkConfiguration
,
316 &global_network_config
);
318 managed_handler()->SetPolicy(
319 onc_source
, userhash
, *network_configs
, *global_network_config
);
322 void SetNetworkConfigurationHandlerExpectations() {
323 // These calls occur in NetworkConfigurationHandler.
324 EXPECT_CALL(*mock_manager_client_
, GetProperties(_
)).Times(AnyNumber());
325 EXPECT_CALL(*mock_manager_client_
,
326 AddPropertyChangedObserver(_
)).Times(AnyNumber());
327 EXPECT_CALL(*mock_manager_client_
,
328 RemovePropertyChangedObserver(_
)).Times(AnyNumber());
331 ManagedNetworkConfigurationHandler
* managed_handler() {
332 return managed_network_configuration_handler_
.get();
335 void GetManagedProperties(const std::string
& userhash
,
336 const std::string
& service_path
) {
337 managed_handler()->GetManagedProperties(
341 &ManagedNetworkConfigurationHandlerTest::GetPropertiesCallback
,
342 base::Unretained(this)),
343 base::Bind(&ManagedNetworkConfigurationHandlerTest::UnexpectedError
));
346 void GetPropertiesCallback(const std::string
& service_path
,
347 const base::DictionaryValue
& dictionary
) {
348 get_properties_service_path_
= service_path
;
349 get_properties_result_
.Clear();
350 get_properties_result_
.MergeDictionary(&dictionary
);
353 static void UnexpectedError(const std::string
& error_name
,
354 scoped_ptr
<base::DictionaryValue
> error_data
) {
359 MockShillManagerClient
* mock_manager_client_
;
360 MockShillProfileClient
* mock_profile_client_
;
361 MockShillServiceClient
* mock_service_client_
;
362 ShillProfileTestClient profiles_stub_
;
363 ShillServiceTestClient services_stub_
;
364 TestNetworkPolicyObserver policy_observer_
;
365 scoped_ptr
<NetworkStateHandler
> network_state_handler_
;
366 scoped_ptr
<TestNetworkProfileHandler
> network_profile_handler_
;
367 scoped_ptr
<NetworkConfigurationHandler
> network_configuration_handler_
;
368 scoped_ptr
<ManagedNetworkConfigurationHandlerImpl
>
369 managed_network_configuration_handler_
;
370 base::MessageLoop message_loop_
;
372 std::string get_properties_service_path_
;
373 base::DictionaryValue get_properties_result_
;
376 DISALLOW_COPY_AND_ASSIGN(ManagedNetworkConfigurationHandlerTest
);
379 TEST_F(ManagedNetworkConfigurationHandlerTest
, ProfileInitialization
) {
380 InitializeStandardProfiles();
381 message_loop_
.RunUntilIdle();
384 TEST_F(ManagedNetworkConfigurationHandlerTest
, RemoveIrrelevantFields
) {
385 InitializeStandardProfiles();
386 scoped_ptr
<base::DictionaryValue
> expected_shill_properties
=
387 test_utils::ReadTestDictionary(
388 "policy/shill_policy_on_unconfigured_wifi1.json");
390 EXPECT_CALL(*mock_profile_client_
,
391 GetProperties(dbus::ObjectPath(kUser1ProfilePath
), _
, _
));
393 EXPECT_CALL(*mock_manager_client_
,
394 ConfigureServiceForProfile(
395 dbus::ObjectPath(kUser1ProfilePath
),
396 IsEqualTo(expected_shill_properties
.get()),
399 SetPolicy(::onc::ONC_SOURCE_USER_POLICY
,
401 "policy/policy_wifi1_with_redundant_fields.onc");
402 message_loop_
.RunUntilIdle();
405 TEST_F(ManagedNetworkConfigurationHandlerTest
, SetPolicyManageUnconfigured
) {
406 InitializeStandardProfiles();
407 scoped_ptr
<base::DictionaryValue
> expected_shill_properties
=
408 test_utils::ReadTestDictionary(
409 "policy/shill_policy_on_unconfigured_wifi1.json");
411 EXPECT_CALL(*mock_profile_client_
,
412 GetProperties(dbus::ObjectPath(kUser1ProfilePath
), _
, _
));
414 EXPECT_CALL(*mock_manager_client_
,
415 ConfigureServiceForProfile(
416 dbus::ObjectPath(kUser1ProfilePath
),
417 IsEqualTo(expected_shill_properties
.get()),
420 SetPolicy(::onc::ONC_SOURCE_USER_POLICY
, kUser1
, "policy/policy_wifi1.onc");
421 message_loop_
.RunUntilIdle();
424 TEST_F(ManagedNetworkConfigurationHandlerTest
, EnableManagedCredentialsWiFi
) {
425 InitializeStandardProfiles();
426 scoped_ptr
<base::DictionaryValue
> expected_shill_properties
=
427 test_utils::ReadTestDictionary(
428 "policy/shill_policy_autoconnect_on_unconfigured_wifi1.json");
430 EXPECT_CALL(*mock_profile_client_
,
431 GetProperties(dbus::ObjectPath(kUser1ProfilePath
), _
, _
));
433 EXPECT_CALL(*mock_manager_client_
,
434 ConfigureServiceForProfile(
435 dbus::ObjectPath(kUser1ProfilePath
),
436 IsEqualTo(expected_shill_properties
.get()),
439 SetPolicy(::onc::ONC_SOURCE_USER_POLICY
, kUser1
,
440 "policy/policy_wifi1_autoconnect.onc");
441 message_loop_
.RunUntilIdle();
444 TEST_F(ManagedNetworkConfigurationHandlerTest
, EnableManagedCredentialsVPN
) {
445 InitializeStandardProfiles();
446 scoped_ptr
<base::DictionaryValue
> expected_shill_properties
=
447 test_utils::ReadTestDictionary(
448 "policy/shill_policy_autoconnect_on_unconfigured_vpn.json");
450 EXPECT_CALL(*mock_profile_client_
,
451 GetProperties(dbus::ObjectPath(kUser1ProfilePath
), _
, _
));
453 EXPECT_CALL(*mock_manager_client_
,
454 ConfigureServiceForProfile(
455 dbus::ObjectPath(kUser1ProfilePath
),
456 IsEqualTo(expected_shill_properties
.get()),
459 SetPolicy(::onc::ONC_SOURCE_USER_POLICY
, kUser1
,
460 "policy/policy_vpn_autoconnect.onc");
461 message_loop_
.RunUntilIdle();
464 // Ensure that EAP settings for ethernet are matched with the right profile
465 // entry and written to the dedicated EthernetEAP service.
466 TEST_F(ManagedNetworkConfigurationHandlerTest
,
467 SetPolicyManageUnmanagedEthernetEAP
) {
468 InitializeStandardProfiles();
469 scoped_ptr
<base::DictionaryValue
> expected_shill_properties
=
470 test_utils::ReadTestDictionary(
472 "shill_policy_on_unmanaged_ethernet_eap.json");
474 SetUpEntry("policy/shill_unmanaged_ethernet_eap.json",
478 // Also setup an unrelated WiFi configuration to verify that the right entry
480 SetUpEntry("policy/shill_unmanaged_wifi1.json",
484 EXPECT_CALL(*mock_profile_client_
,
485 GetProperties(dbus::ObjectPath(kUser1ProfilePath
), _
, _
));
487 EXPECT_CALL(*mock_profile_client_
,
488 GetEntry(dbus::ObjectPath(kUser1ProfilePath
), _
, _
, _
)).Times(2);
491 *mock_profile_client_
,
492 DeleteEntry(dbus::ObjectPath(kUser1ProfilePath
), "eth_entry", _
, _
));
495 *mock_manager_client_
,
496 ConfigureServiceForProfile(dbus::ObjectPath(kUser1ProfilePath
),
497 IsEqualTo(expected_shill_properties
.get()),
501 ::onc::ONC_SOURCE_USER_POLICY
, kUser1
, "policy/policy_ethernet_eap.onc");
502 message_loop_
.RunUntilIdle();
505 TEST_F(ManagedNetworkConfigurationHandlerTest
, SetPolicyIgnoreUnmodified
) {
506 InitializeStandardProfiles();
507 EXPECT_CALL(*mock_profile_client_
, GetProperties(_
, _
, _
));
509 EXPECT_CALL(*mock_manager_client_
, ConfigureServiceForProfile(_
, _
, _
, _
));
511 SetPolicy(::onc::ONC_SOURCE_USER_POLICY
, kUser1
, "policy/policy_wifi1.onc");
512 message_loop_
.RunUntilIdle();
513 EXPECT_EQ(1, policy_observer_
.GetPoliciesAppliedCountAndReset());
514 VerifyAndClearExpectations();
516 SetUpEntry("policy/shill_policy_on_unmanaged_wifi1.json",
520 EXPECT_CALL(*mock_profile_client_
, GetProperties(_
, _
, _
));
523 *mock_profile_client_
,
524 GetEntry(dbus::ObjectPath(kUser1ProfilePath
), "some_entry_path", _
, _
));
526 SetPolicy(::onc::ONC_SOURCE_USER_POLICY
, kUser1
, "policy/policy_wifi1.onc");
528 message_loop_
.RunUntilIdle();
529 EXPECT_EQ(1, policy_observer_
.GetPoliciesAppliedCountAndReset());
532 TEST_F(ManagedNetworkConfigurationHandlerTest
, PolicyApplicationRunning
) {
533 InitializeStandardProfiles();
534 EXPECT_CALL(*mock_profile_client_
, GetProperties(_
, _
, _
)).Times(AnyNumber());
535 EXPECT_CALL(*mock_manager_client_
, ConfigureServiceForProfile(_
, _
, _
, _
))
537 EXPECT_CALL(*mock_profile_client_
, GetEntry(_
, _
, _
, _
)).Times(AnyNumber());
539 EXPECT_FALSE(managed_handler()->IsAnyPolicyApplicationRunning());
541 SetPolicy(::onc::ONC_SOURCE_USER_POLICY
, kUser1
, "policy/policy_wifi1.onc");
542 managed_handler()->SetPolicy(
543 ::onc::ONC_SOURCE_DEVICE_POLICY
,
544 std::string(), // no userhash
545 base::ListValue(), // no device network policy
546 base::DictionaryValue()); // no device global config
548 EXPECT_TRUE(managed_handler()->IsAnyPolicyApplicationRunning());
549 message_loop_
.RunUntilIdle();
550 EXPECT_FALSE(managed_handler()->IsAnyPolicyApplicationRunning());
552 SetUpEntry("policy/shill_policy_on_unmanaged_wifi1.json",
557 ::onc::ONC_SOURCE_USER_POLICY
, kUser1
, "policy/policy_wifi1_update.onc");
558 EXPECT_TRUE(managed_handler()->IsAnyPolicyApplicationRunning());
559 message_loop_
.RunUntilIdle();
560 EXPECT_FALSE(managed_handler()->IsAnyPolicyApplicationRunning());
563 TEST_F(ManagedNetworkConfigurationHandlerTest
, UpdatePolicyAfterFinished
) {
564 InitializeStandardProfiles();
565 EXPECT_CALL(*mock_profile_client_
, GetProperties(_
, _
, _
));
566 EXPECT_CALL(*mock_manager_client_
, ConfigureServiceForProfile(_
, _
, _
, _
));
568 SetPolicy(::onc::ONC_SOURCE_USER_POLICY
, kUser1
, "policy/policy_wifi1.onc");
569 message_loop_
.RunUntilIdle();
570 EXPECT_EQ(1, policy_observer_
.GetPoliciesAppliedCountAndReset());
571 VerifyAndClearExpectations();
573 SetUpEntry("policy/shill_policy_on_unmanaged_wifi1.json",
577 EXPECT_CALL(*mock_profile_client_
, GetProperties(_
, _
, _
));
579 *mock_profile_client_
,
580 GetEntry(dbus::ObjectPath(kUser1ProfilePath
), "some_entry_path", _
, _
));
581 EXPECT_CALL(*mock_manager_client_
, ConfigureServiceForProfile(_
, _
, _
, _
));
584 ::onc::ONC_SOURCE_USER_POLICY
, kUser1
, "policy/policy_wifi1_update.onc");
585 message_loop_
.RunUntilIdle();
586 EXPECT_EQ(1, policy_observer_
.GetPoliciesAppliedCountAndReset());
589 TEST_F(ManagedNetworkConfigurationHandlerTest
, UpdatePolicyBeforeFinished
) {
590 InitializeStandardProfiles();
591 EXPECT_CALL(*mock_profile_client_
, GetProperties(_
, _
, _
)).Times(2);
592 EXPECT_CALL(*mock_manager_client_
, ConfigureServiceForProfile(_
, _
, _
, _
))
595 SetPolicy(::onc::ONC_SOURCE_USER_POLICY
, kUser1
, "policy/policy_wifi1.onc");
596 // Usually the first call will cause a profile entry to be created, which we
599 ::onc::ONC_SOURCE_USER_POLICY
, kUser1
, "policy/policy_wifi1_update.onc");
601 message_loop_
.RunUntilIdle();
602 EXPECT_EQ(1, policy_observer_
.GetPoliciesAppliedCountAndReset());
605 TEST_F(ManagedNetworkConfigurationHandlerTest
, SetPolicyManageUnmanaged
) {
606 InitializeStandardProfiles();
607 SetUpEntry("policy/shill_unmanaged_wifi1.json",
611 scoped_ptr
<base::DictionaryValue
> expected_shill_properties
=
612 test_utils::ReadTestDictionary(
613 "policy/shill_policy_on_unmanaged_wifi1.json");
615 EXPECT_CALL(*mock_profile_client_
,
616 GetProperties(dbus::ObjectPath(kUser1ProfilePath
), _
, _
));
619 *mock_profile_client_
,
620 GetEntry(dbus::ObjectPath(kUser1ProfilePath
), "old_entry_path", _
, _
));
623 *mock_profile_client_
,
624 DeleteEntry(dbus::ObjectPath(kUser1ProfilePath
), "old_entry_path", _
, _
));
626 EXPECT_CALL(*mock_manager_client_
,
627 ConfigureServiceForProfile(
628 dbus::ObjectPath(kUser1ProfilePath
),
629 IsEqualTo(expected_shill_properties
.get()),
632 SetPolicy(::onc::ONC_SOURCE_USER_POLICY
, kUser1
, "policy/policy_wifi1.onc");
633 message_loop_
.RunUntilIdle();
636 // Old ChromeOS versions may not have used the UIData property
637 TEST_F(ManagedNetworkConfigurationHandlerTest
,
638 SetPolicyManageUnmanagedWithoutUIData
) {
639 InitializeStandardProfiles();
640 SetUpEntry("policy/shill_unmanaged_wifi1.json",
644 scoped_ptr
<base::DictionaryValue
> expected_shill_properties
=
645 test_utils::ReadTestDictionary(
646 "policy/shill_policy_on_unmanaged_wifi1.json");
648 EXPECT_CALL(*mock_profile_client_
,
649 GetProperties(dbus::ObjectPath(kUser1ProfilePath
), _
, _
));
652 *mock_profile_client_
,
653 GetEntry(dbus::ObjectPath(kUser1ProfilePath
), "old_entry_path", _
, _
));
656 *mock_profile_client_
,
657 DeleteEntry(dbus::ObjectPath(kUser1ProfilePath
), "old_entry_path", _
, _
));
659 EXPECT_CALL(*mock_manager_client_
,
660 ConfigureServiceForProfile(
661 dbus::ObjectPath(kUser1ProfilePath
),
662 IsEqualTo(expected_shill_properties
.get()),
665 SetPolicy(::onc::ONC_SOURCE_USER_POLICY
, kUser1
, "policy/policy_wifi1.onc");
666 message_loop_
.RunUntilIdle();
669 TEST_F(ManagedNetworkConfigurationHandlerTest
, SetPolicyUpdateManagedNewGUID
) {
670 InitializeStandardProfiles();
671 SetUpEntry("policy/shill_managed_wifi1.json",
675 scoped_ptr
<base::DictionaryValue
> expected_shill_properties
=
676 test_utils::ReadTestDictionary(
677 "policy/shill_policy_on_unmanaged_wifi1.json");
679 // The passphrase isn't sent again, because it's configured by the user and
680 // Shill doesn't send it on GetProperties calls.
681 expected_shill_properties
->RemoveWithoutPathExpansion(
682 shill::kPassphraseProperty
, NULL
);
684 EXPECT_CALL(*mock_profile_client_
,
685 GetProperties(dbus::ObjectPath(kUser1ProfilePath
), _
, _
));
688 *mock_profile_client_
,
689 GetEntry(dbus::ObjectPath(kUser1ProfilePath
), "old_entry_path", _
, _
));
692 *mock_profile_client_
,
693 DeleteEntry(dbus::ObjectPath(kUser1ProfilePath
), "old_entry_path", _
, _
));
695 EXPECT_CALL(*mock_manager_client_
,
696 ConfigureServiceForProfile(
697 dbus::ObjectPath(kUser1ProfilePath
),
698 IsEqualTo(expected_shill_properties
.get()),
701 SetPolicy(::onc::ONC_SOURCE_USER_POLICY
, kUser1
, "policy/policy_wifi1.onc");
702 message_loop_
.RunUntilIdle();
705 TEST_F(ManagedNetworkConfigurationHandlerTest
, SetPolicyUpdateManagedVPN
) {
706 InitializeStandardProfiles();
707 SetUpEntry("policy/shill_managed_vpn.json", kUser1ProfilePath
, "entry_path");
709 scoped_ptr
<base::DictionaryValue
> expected_shill_properties
=
710 test_utils::ReadTestDictionary(
711 "policy/shill_policy_on_managed_vpn.json");
713 EXPECT_CALL(*mock_profile_client_
,
714 GetProperties(dbus::ObjectPath(kUser1ProfilePath
), _
, _
));
717 *mock_profile_client_
,
718 GetEntry(dbus::ObjectPath(kUser1ProfilePath
), "entry_path", _
, _
));
720 EXPECT_CALL(*mock_manager_client_
,
721 ConfigureServiceForProfile(
722 dbus::ObjectPath(kUser1ProfilePath
),
723 IsEqualTo(expected_shill_properties
.get()),
726 SetPolicy(::onc::ONC_SOURCE_USER_POLICY
, kUser1
, "policy/policy_vpn.onc");
727 message_loop_
.RunUntilIdle();
728 VerifyAndClearExpectations();
731 TEST_F(ManagedNetworkConfigurationHandlerTest
, SetPolicyReapplyToManaged
) {
732 InitializeStandardProfiles();
733 SetUpEntry("policy/shill_policy_on_unmanaged_wifi1.json",
737 scoped_ptr
<base::DictionaryValue
> expected_shill_properties
=
738 test_utils::ReadTestDictionary(
739 "policy/shill_policy_on_unmanaged_wifi1.json");
741 // The passphrase isn't sent again, because it's configured by the user and
742 // Shill doesn't send it on GetProperties calls.
743 expected_shill_properties
->RemoveWithoutPathExpansion(
744 shill::kPassphraseProperty
, NULL
);
746 EXPECT_CALL(*mock_profile_client_
,
747 GetProperties(dbus::ObjectPath(kUser1ProfilePath
), _
, _
));
750 *mock_profile_client_
,
751 GetEntry(dbus::ObjectPath(kUser1ProfilePath
), "old_entry_path", _
, _
));
753 EXPECT_CALL(*mock_manager_client_
,
754 ConfigureServiceForProfile(
755 dbus::ObjectPath(kUser1ProfilePath
),
756 IsEqualTo(expected_shill_properties
.get()),
759 SetPolicy(::onc::ONC_SOURCE_USER_POLICY
, kUser1
, "policy/policy_wifi1.onc");
760 message_loop_
.RunUntilIdle();
761 VerifyAndClearExpectations();
763 // If we apply the policy again, without change, then the Shill profile will
765 EXPECT_CALL(*mock_profile_client_
,
766 GetProperties(dbus::ObjectPath(kUser1ProfilePath
), _
, _
));
769 *mock_profile_client_
,
770 GetEntry(dbus::ObjectPath(kUser1ProfilePath
), "old_entry_path", _
, _
));
772 SetPolicy(::onc::ONC_SOURCE_USER_POLICY
, kUser1
, "policy/policy_wifi1.onc");
773 message_loop_
.RunUntilIdle();
776 TEST_F(ManagedNetworkConfigurationHandlerTest
, SetPolicyUnmanageManaged
) {
777 InitializeStandardProfiles();
778 SetUpEntry("policy/shill_policy_on_unmanaged_wifi1.json",
782 EXPECT_CALL(*mock_profile_client_
,
783 GetProperties(dbus::ObjectPath(kUser1ProfilePath
), _
, _
));
785 EXPECT_CALL(*mock_profile_client_
,
786 GetEntry(dbus::ObjectPath(kUser1ProfilePath
),
790 EXPECT_CALL(*mock_profile_client_
,
791 DeleteEntry(dbus::ObjectPath(kUser1ProfilePath
),
795 SetPolicy(::onc::ONC_SOURCE_USER_POLICY
, kUser1
, "");
796 message_loop_
.RunUntilIdle();
799 TEST_F(ManagedNetworkConfigurationHandlerTest
, SetEmptyPolicyIgnoreUnmanaged
) {
800 InitializeStandardProfiles();
801 SetUpEntry("policy/shill_unmanaged_wifi1.json",
805 EXPECT_CALL(*mock_profile_client_
,
806 GetProperties(dbus::ObjectPath(kUser1ProfilePath
), _
, _
));
808 EXPECT_CALL(*mock_profile_client_
,
809 GetEntry(dbus::ObjectPath(kUser1ProfilePath
),
813 SetPolicy(::onc::ONC_SOURCE_USER_POLICY
, kUser1
, "");
814 message_loop_
.RunUntilIdle();
815 EXPECT_EQ(1, policy_observer_
.GetPoliciesAppliedCountAndReset());
818 TEST_F(ManagedNetworkConfigurationHandlerTest
, SetPolicyIgnoreUnmanaged
) {
819 InitializeStandardProfiles();
820 SetUpEntry("policy/shill_unmanaged_wifi2.json",
824 EXPECT_CALL(*mock_profile_client_
,
825 GetProperties(dbus::ObjectPath(kUser1ProfilePath
), _
, _
));
828 *mock_profile_client_
,
829 GetEntry(dbus::ObjectPath(kUser1ProfilePath
), "wifi2_entry_path", _
, _
));
831 scoped_ptr
<base::DictionaryValue
> expected_shill_properties
=
832 test_utils::ReadTestDictionary(
833 "policy/shill_policy_on_unconfigured_wifi1.json");
835 EXPECT_CALL(*mock_manager_client_
,
836 ConfigureServiceForProfile(
837 dbus::ObjectPath(kUser1ProfilePath
),
838 IsEqualTo(expected_shill_properties
.get()),
841 SetPolicy(::onc::ONC_SOURCE_USER_POLICY
, kUser1
, "policy/policy_wifi1.onc");
842 message_loop_
.RunUntilIdle();
845 TEST_F(ManagedNetworkConfigurationHandlerTest
, AutoConnectDisallowed
) {
846 InitializeStandardProfiles();
847 // Setup an unmanaged network.
848 SetUpEntry("policy/shill_unmanaged_wifi2.json",
852 // Apply the user policy with global autoconnect config and expect that
853 // autoconnect is disabled in the network's profile entry.
854 EXPECT_CALL(*mock_profile_client_
,
855 GetProperties(dbus::ObjectPath(kUser1ProfilePath
), _
, _
));
858 *mock_profile_client_
,
859 GetEntry(dbus::ObjectPath(kUser1ProfilePath
), "wifi2_entry_path", _
, _
));
861 scoped_ptr
<base::DictionaryValue
> expected_shill_properties
=
862 test_utils::ReadTestDictionary(
863 "policy/shill_disallow_autoconnect_on_unmanaged_wifi2.json");
865 EXPECT_CALL(*mock_manager_client_
,
866 ConfigureServiceForProfile(
867 dbus::ObjectPath(kUser1ProfilePath
),
868 IsEqualTo(expected_shill_properties
.get()),
871 SetPolicy(::onc::ONC_SOURCE_USER_POLICY
,
873 "policy/policy_disallow_autoconnect.onc");
874 message_loop_
.RunUntilIdle();
876 // Verify that GetManagedProperties correctly augments the properties with the
877 // global config from the user policy.
879 // GetManagedProperties requires the device policy to be set or explicitly
881 EXPECT_CALL(*mock_profile_client_
,
882 GetProperties(dbus::ObjectPath(
883 NetworkProfileHandler::GetSharedProfilePath()),
886 managed_handler()->SetPolicy(
887 ::onc::ONC_SOURCE_DEVICE_POLICY
,
888 std::string(), // no userhash
889 base::ListValue(), // no device network policy
890 base::DictionaryValue()); // no device global config
892 services_stub_
.SetFakeProperties(*expected_shill_properties
);
893 EXPECT_CALL(*mock_service_client_
,
894 GetProperties(dbus::ObjectPath(
897 GetManagedProperties(kUser1
, "wifi2");
898 message_loop_
.RunUntilIdle();
900 EXPECT_EQ("wifi2", get_properties_service_path_
);
902 scoped_ptr
<base::DictionaryValue
> expected_managed_onc
=
903 test_utils::ReadTestDictionary(
904 "policy/managed_onc_disallow_autoconnect_on_unmanaged_wifi2.onc");
905 EXPECT_TRUE(onc::test_utils::Equals(expected_managed_onc
.get(),
906 &get_properties_result_
));
909 TEST_F(ManagedNetworkConfigurationHandlerTest
, LateProfileLoading
) {
910 SetPolicy(::onc::ONC_SOURCE_USER_POLICY
, kUser1
, "policy/policy_wifi1.onc");
912 message_loop_
.RunUntilIdle();
913 VerifyAndClearExpectations();
915 scoped_ptr
<base::DictionaryValue
> expected_shill_properties
=
916 test_utils::ReadTestDictionary(
917 "policy/shill_policy_on_unconfigured_wifi1.json");
919 EXPECT_CALL(*mock_profile_client_
,
920 GetProperties(dbus::ObjectPath(kUser1ProfilePath
), _
, _
));
922 EXPECT_CALL(*mock_manager_client_
,
923 ConfigureServiceForProfile(
924 dbus::ObjectPath(kUser1ProfilePath
),
925 IsEqualTo(expected_shill_properties
.get()),
928 InitializeStandardProfiles();
929 message_loop_
.RunUntilIdle();
932 class ManagedNetworkConfigurationHandlerShutdownTest
933 : public ManagedNetworkConfigurationHandlerTest
{
935 void SetUp() override
{
936 ManagedNetworkConfigurationHandlerTest::SetUp();
937 ON_CALL(*mock_profile_client_
, GetProperties(_
, _
, _
)).WillByDefault(
938 Invoke(&ManagedNetworkConfigurationHandlerShutdownTest::GetProperties
));
941 static void GetProperties(
942 const dbus::ObjectPath
& profile_path
,
943 const ShillClientHelper::DictionaryValueCallbackWithoutStatus
& callback
,
944 const ShillClientHelper::ErrorCallback
& error_callback
) {
945 base::MessageLoop::current()->PostTask(
947 base::Bind(ManagedNetworkConfigurationHandlerShutdownTest::
948 CallbackWithEmptyDictionary
,
952 static void CallbackWithEmptyDictionary(
953 const ShillClientHelper::DictionaryValueCallbackWithoutStatus
& callback
) {
954 callback
.Run(base::DictionaryValue());
958 TEST_F(ManagedNetworkConfigurationHandlerShutdownTest
,
959 DuringPolicyApplication
) {
960 InitializeStandardProfiles();
962 EXPECT_CALL(*mock_profile_client_
,
963 GetProperties(dbus::ObjectPath(kUser1ProfilePath
), _
, _
));
965 SetPolicy(::onc::ONC_SOURCE_USER_POLICY
, kUser1
, "policy/policy_wifi1.onc");
966 managed_network_configuration_handler_
->RemoveObserver(&policy_observer_
);
967 managed_network_configuration_handler_
.reset();
968 message_loop_
.RunUntilIdle();
971 } // namespace chromeos