Fixed incorrect call of updateContextMenuActionItems.
[chromium-blink-merge.git] / chromeos / network / managed_network_configuration_handler_unittest.cc
blobad825f6fba353533a3d581ed7a2af5cb7f69fbfa
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.
5 #include <iostream>
6 #include <sstream>
8 #include "base/bind.h"
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/onc/onc_test_utils.h"
25 #include "chromeos/network/onc/onc_utils.h"
26 #include "dbus/object_path.h"
27 #include "testing/gmock/include/gmock/gmock.h"
28 #include "testing/gtest/include/gtest/gtest.h"
29 #include "third_party/cros_system_api/dbus/service_constants.h"
31 using ::testing::AnyNumber;
32 using ::testing::Invoke;
33 using ::testing::Mock;
34 using ::testing::Pointee;
35 using ::testing::Return;
36 using ::testing::SaveArg;
37 using ::testing::StrEq;
38 using ::testing::StrictMock;
39 using ::testing::_;
41 namespace test_utils = ::chromeos::onc::test_utils;
43 namespace chromeos {
45 namespace {
47 std::string ValueToString(const base::Value* value) {
48 std::stringstream str;
49 str << *value;
50 return str.str();
53 void DereferenceAndCall(
54 base::Callback<void(const base::DictionaryValue& result)> callback,
55 const base::DictionaryValue* value) {
56 callback.Run(*value);
59 const char kUser1[] = "user1";
60 const char kUser1ProfilePath[] = "/profile/user1/shill";
62 // Matcher to match base::Value.
63 MATCHER_P(IsEqualTo,
64 value,
65 std::string(negation ? "isn't" : "is") + " equal to " +
66 ValueToString(value)) {
67 return value->Equals(&arg);
70 class ShillProfileTestClient {
71 public:
72 typedef ShillClientHelper::DictionaryValueCallbackWithoutStatus
73 DictionaryValueCallbackWithoutStatus;
74 typedef ShillClientHelper::ErrorCallback ErrorCallback;
76 void AddProfile(const std::string& profile_path,
77 const std::string& userhash) {
78 if (profile_entries_.HasKey(profile_path))
79 return;
81 base::DictionaryValue* profile = new base::DictionaryValue;
82 profile_entries_.SetWithoutPathExpansion(profile_path, profile);
83 profile_to_user_[profile_path] = userhash;
86 void AddEntry(const std::string& profile_path,
87 const std::string& entry_path,
88 const base::DictionaryValue& entry) {
89 base::DictionaryValue* entries = NULL;
90 profile_entries_.GetDictionaryWithoutPathExpansion(profile_path, &entries);
91 ASSERT_TRUE(entries);
93 base::DictionaryValue* new_entry = entry.DeepCopy();
94 new_entry->SetStringWithoutPathExpansion(shill::kProfileProperty,
95 profile_path);
96 entries->SetWithoutPathExpansion(entry_path, new_entry);
99 void GetProperties(const dbus::ObjectPath& profile_path,
100 const DictionaryValueCallbackWithoutStatus& callback,
101 const ErrorCallback& error_callback) {
102 base::DictionaryValue* entries = NULL;
103 profile_entries_.GetDictionaryWithoutPathExpansion(profile_path.value(),
104 &entries);
105 ASSERT_TRUE(entries);
107 scoped_ptr<base::DictionaryValue> result(new base::DictionaryValue);
108 base::ListValue* entry_paths = new base::ListValue;
109 result->SetWithoutPathExpansion(shill::kEntriesProperty, entry_paths);
110 for (base::DictionaryValue::Iterator it(*entries); !it.IsAtEnd();
111 it.Advance()) {
112 entry_paths->AppendString(it.key());
115 ASSERT_TRUE(ContainsKey(profile_to_user_, profile_path.value()));
116 const std::string& userhash = profile_to_user_[profile_path.value()];
117 result->SetStringWithoutPathExpansion(shill::kUserHashProperty, userhash);
119 base::MessageLoop::current()->PostTask(
120 FROM_HERE,
121 base::Bind(base::Bind(&DereferenceAndCall, callback),
122 base::Owned(result.release())));
125 void GetEntry(const dbus::ObjectPath& profile_path,
126 const std::string& entry_path,
127 const DictionaryValueCallbackWithoutStatus& callback,
128 const ErrorCallback& error_callback) {
129 base::DictionaryValue* entries = NULL;
130 profile_entries_.GetDictionaryWithoutPathExpansion(profile_path.value(),
131 &entries);
132 ASSERT_TRUE(entries);
134 base::DictionaryValue* entry = NULL;
135 entries->GetDictionaryWithoutPathExpansion(entry_path, &entry);
136 ASSERT_TRUE(entry);
137 base::MessageLoop::current()->PostTask(
138 FROM_HERE,
139 base::Bind(base::Bind(&DereferenceAndCall, callback),
140 base::Owned(entry->DeepCopy())));
143 protected:
144 base::DictionaryValue profile_entries_;
145 std::map<std::string, std::string> profile_to_user_;
148 class ShillServiceTestClient {
149 public:
150 typedef ShillClientHelper::DictionaryValueCallback DictionaryValueCallback;
151 void SetFakeProperties(const base::DictionaryValue& service_properties) {
152 service_properties_.Clear();
153 service_properties_.MergeDictionary(&service_properties);
156 void GetProperties(const dbus::ObjectPath& service_path,
157 const DictionaryValueCallback& callback) {
158 base::MessageLoop::current()->PostTask(
159 FROM_HERE,
160 base::Bind(callback,
161 DBUS_METHOD_CALL_SUCCESS,
162 base::ConstRef(service_properties_)));
165 protected:
166 base::DictionaryValue service_properties_;
169 class TestNetworkProfileHandler : public NetworkProfileHandler {
170 public:
171 TestNetworkProfileHandler() {
172 Init();
174 virtual ~TestNetworkProfileHandler() {}
176 void AddProfileForTest(const NetworkProfile& profile) {
177 AddProfile(profile);
180 private:
181 DISALLOW_COPY_AND_ASSIGN(TestNetworkProfileHandler);
184 class TestNetworkPolicyObserver : public NetworkPolicyObserver {
185 public:
186 TestNetworkPolicyObserver() : policies_applied_count_(0) {}
188 void PoliciesApplied(const std::string& userhash) override {
189 policies_applied_count_++;
192 int GetPoliciesAppliedCountAndReset() {
193 int count = policies_applied_count_;
194 policies_applied_count_ = 0;
195 return count;
198 private:
199 int policies_applied_count_;
201 DISALLOW_COPY_AND_ASSIGN(TestNetworkPolicyObserver);
204 } // namespace
206 class ManagedNetworkConfigurationHandlerTest : public testing::Test {
207 public:
208 ManagedNetworkConfigurationHandlerTest()
209 : mock_manager_client_(NULL),
210 mock_profile_client_(NULL),
211 mock_service_client_(NULL) {
214 virtual ~ManagedNetworkConfigurationHandlerTest() {
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_profile_handler_.reset(new TestNetworkProfileHandler());
245 network_configuration_handler_.reset(
246 NetworkConfigurationHandler::InitializeForTest(
247 NULL /* no NetworkStateHandler */));
248 managed_network_configuration_handler_.reset(
249 new ManagedNetworkConfigurationHandlerImpl());
250 managed_network_configuration_handler_->Init(
251 NULL /* no NetworkStateHandler */,
252 network_profile_handler_.get(),
253 network_configuration_handler_.get(),
254 NULL /* no DeviceHandler */);
255 managed_network_configuration_handler_->AddObserver(&policy_observer_);
257 message_loop_.RunUntilIdle();
260 void TearDown() override {
261 if (managed_network_configuration_handler_)
262 managed_network_configuration_handler_->RemoveObserver(&policy_observer_);
263 managed_network_configuration_handler_.reset();
264 network_configuration_handler_.reset();
265 network_profile_handler_.reset();
266 DBusThreadManager::Shutdown();
269 void VerifyAndClearExpectations() {
270 Mock::VerifyAndClearExpectations(mock_manager_client_);
271 Mock::VerifyAndClearExpectations(mock_profile_client_);
272 SetNetworkConfigurationHandlerExpectations();
275 void InitializeStandardProfiles() {
276 profiles_stub_.AddProfile(kUser1ProfilePath, kUser1);
277 network_profile_handler_->
278 AddProfileForTest(NetworkProfile(kUser1ProfilePath, kUser1));
280 profiles_stub_.AddProfile(NetworkProfileHandler::GetSharedProfilePath(),
281 std::string() /* no userhash */);
282 network_profile_handler_->AddProfileForTest(
283 NetworkProfile(NetworkProfileHandler::GetSharedProfilePath(),
284 std::string() /* no userhash */));
287 void SetUpEntry(const std::string& path_to_shill_json,
288 const std::string& profile_path,
289 const std::string& entry_path) {
290 scoped_ptr<base::DictionaryValue> entry =
291 test_utils::ReadTestDictionary(path_to_shill_json);
292 profiles_stub_.AddEntry(profile_path, entry_path, *entry);
295 void SetPolicy(::onc::ONCSource onc_source,
296 const std::string& userhash,
297 const std::string& path_to_onc) {
298 scoped_ptr<base::DictionaryValue> policy;
299 if (path_to_onc.empty())
300 policy = onc::ReadDictionaryFromJson(onc::kEmptyUnencryptedConfiguration);
301 else
302 policy = test_utils::ReadTestDictionary(path_to_onc);
304 base::ListValue empty_network_configs;
305 base::ListValue* network_configs = &empty_network_configs;
306 policy->GetListWithoutPathExpansion(
307 ::onc::toplevel_config::kNetworkConfigurations, &network_configs);
309 base::DictionaryValue empty_global_config;
310 base::DictionaryValue* global_network_config = &empty_global_config;
311 policy->GetDictionaryWithoutPathExpansion(
312 ::onc::toplevel_config::kGlobalNetworkConfiguration,
313 &global_network_config);
315 managed_handler()->SetPolicy(
316 onc_source, userhash, *network_configs, *global_network_config);
319 void SetNetworkConfigurationHandlerExpectations() {
320 // These calls occur in NetworkConfigurationHandler.
321 EXPECT_CALL(*mock_manager_client_, GetProperties(_)).Times(AnyNumber());
322 EXPECT_CALL(*mock_manager_client_,
323 AddPropertyChangedObserver(_)).Times(AnyNumber());
324 EXPECT_CALL(*mock_manager_client_,
325 RemovePropertyChangedObserver(_)).Times(AnyNumber());
328 ManagedNetworkConfigurationHandler* managed_handler() {
329 return managed_network_configuration_handler_.get();
332 void GetManagedProperties(const std::string& userhash,
333 const std::string& service_path) {
334 managed_handler()->GetManagedProperties(
335 userhash,
336 service_path,
337 base::Bind(
338 &ManagedNetworkConfigurationHandlerTest::GetPropertiesCallback,
339 base::Unretained(this)),
340 base::Bind(&ManagedNetworkConfigurationHandlerTest::UnexpectedError));
343 void GetPropertiesCallback(const std::string& service_path,
344 const base::DictionaryValue& dictionary) {
345 get_properties_service_path_ = service_path;
346 get_properties_result_.Clear();
347 get_properties_result_.MergeDictionary(&dictionary);
350 static void UnexpectedError(const std::string& error_name,
351 scoped_ptr<base::DictionaryValue> error_data) {
352 ASSERT_FALSE(true);
355 protected:
356 MockShillManagerClient* mock_manager_client_;
357 MockShillProfileClient* mock_profile_client_;
358 MockShillServiceClient* mock_service_client_;
359 ShillProfileTestClient profiles_stub_;
360 ShillServiceTestClient services_stub_;
361 TestNetworkPolicyObserver policy_observer_;
362 scoped_ptr<TestNetworkProfileHandler> network_profile_handler_;
363 scoped_ptr<NetworkConfigurationHandler> network_configuration_handler_;
364 scoped_ptr<ManagedNetworkConfigurationHandlerImpl>
365 managed_network_configuration_handler_;
366 base::MessageLoop message_loop_;
368 std::string get_properties_service_path_;
369 base::DictionaryValue get_properties_result_;
371 private:
372 DISALLOW_COPY_AND_ASSIGN(ManagedNetworkConfigurationHandlerTest);
375 TEST_F(ManagedNetworkConfigurationHandlerTest, ProfileInitialization) {
376 InitializeStandardProfiles();
377 message_loop_.RunUntilIdle();
380 TEST_F(ManagedNetworkConfigurationHandlerTest, RemoveIrrelevantFields) {
381 InitializeStandardProfiles();
382 scoped_ptr<base::DictionaryValue> expected_shill_properties =
383 test_utils::ReadTestDictionary(
384 "policy/shill_policy_on_unconfigured_wifi1.json");
386 EXPECT_CALL(*mock_profile_client_,
387 GetProperties(dbus::ObjectPath(kUser1ProfilePath), _, _));
389 EXPECT_CALL(*mock_manager_client_,
390 ConfigureServiceForProfile(
391 dbus::ObjectPath(kUser1ProfilePath),
392 IsEqualTo(expected_shill_properties.get()),
393 _, _));
395 SetPolicy(::onc::ONC_SOURCE_USER_POLICY,
396 kUser1,
397 "policy/policy_wifi1_with_redundant_fields.onc");
398 message_loop_.RunUntilIdle();
401 TEST_F(ManagedNetworkConfigurationHandlerTest, SetPolicyManageUnconfigured) {
402 InitializeStandardProfiles();
403 scoped_ptr<base::DictionaryValue> expected_shill_properties =
404 test_utils::ReadTestDictionary(
405 "policy/shill_policy_on_unconfigured_wifi1.json");
407 EXPECT_CALL(*mock_profile_client_,
408 GetProperties(dbus::ObjectPath(kUser1ProfilePath), _, _));
410 EXPECT_CALL(*mock_manager_client_,
411 ConfigureServiceForProfile(
412 dbus::ObjectPath(kUser1ProfilePath),
413 IsEqualTo(expected_shill_properties.get()),
414 _, _));
416 SetPolicy(::onc::ONC_SOURCE_USER_POLICY, kUser1, "policy/policy_wifi1.onc");
417 message_loop_.RunUntilIdle();
420 TEST_F(ManagedNetworkConfigurationHandlerTest, EnableManagedCredentialsWiFi) {
421 InitializeStandardProfiles();
422 scoped_ptr<base::DictionaryValue> expected_shill_properties =
423 test_utils::ReadTestDictionary(
424 "policy/shill_policy_autoconnect_on_unconfigured_wifi1.json");
426 EXPECT_CALL(*mock_profile_client_,
427 GetProperties(dbus::ObjectPath(kUser1ProfilePath), _, _));
429 EXPECT_CALL(*mock_manager_client_,
430 ConfigureServiceForProfile(
431 dbus::ObjectPath(kUser1ProfilePath),
432 IsEqualTo(expected_shill_properties.get()),
433 _, _));
435 SetPolicy(::onc::ONC_SOURCE_USER_POLICY, kUser1,
436 "policy/policy_wifi1_autoconnect.onc");
437 message_loop_.RunUntilIdle();
440 TEST_F(ManagedNetworkConfigurationHandlerTest, EnableManagedCredentialsVPN) {
441 InitializeStandardProfiles();
442 scoped_ptr<base::DictionaryValue> expected_shill_properties =
443 test_utils::ReadTestDictionary(
444 "policy/shill_policy_autoconnect_on_unconfigured_vpn.json");
446 EXPECT_CALL(*mock_profile_client_,
447 GetProperties(dbus::ObjectPath(kUser1ProfilePath), _, _));
449 EXPECT_CALL(*mock_manager_client_,
450 ConfigureServiceForProfile(
451 dbus::ObjectPath(kUser1ProfilePath),
452 IsEqualTo(expected_shill_properties.get()),
453 _, _));
455 SetPolicy(::onc::ONC_SOURCE_USER_POLICY, kUser1,
456 "policy/policy_vpn_autoconnect.onc");
457 message_loop_.RunUntilIdle();
460 // Ensure that EAP settings for ethernet are matched with the right profile
461 // entry and written to the dedicated EthernetEAP service.
462 TEST_F(ManagedNetworkConfigurationHandlerTest,
463 SetPolicyManageUnmanagedEthernetEAP) {
464 InitializeStandardProfiles();
465 scoped_ptr<base::DictionaryValue> expected_shill_properties =
466 test_utils::ReadTestDictionary(
467 "policy/"
468 "shill_policy_on_unmanaged_ethernet_eap.json");
470 SetUpEntry("policy/shill_unmanaged_ethernet_eap.json",
471 kUser1ProfilePath,
472 "eth_entry");
474 // Also setup an unrelated WiFi configuration to verify that the right entry
475 // is matched.
476 SetUpEntry("policy/shill_unmanaged_wifi1.json",
477 kUser1ProfilePath,
478 "wifi_entry");
480 EXPECT_CALL(*mock_profile_client_,
481 GetProperties(dbus::ObjectPath(kUser1ProfilePath), _, _));
483 EXPECT_CALL(*mock_profile_client_,
484 GetEntry(dbus::ObjectPath(kUser1ProfilePath), _, _, _)).Times(2);
486 EXPECT_CALL(
487 *mock_profile_client_,
488 DeleteEntry(dbus::ObjectPath(kUser1ProfilePath), "eth_entry", _, _));
490 EXPECT_CALL(
491 *mock_manager_client_,
492 ConfigureServiceForProfile(dbus::ObjectPath(kUser1ProfilePath),
493 IsEqualTo(expected_shill_properties.get()),
494 _, _));
496 SetPolicy(
497 ::onc::ONC_SOURCE_USER_POLICY, kUser1, "policy/policy_ethernet_eap.onc");
498 message_loop_.RunUntilIdle();
501 TEST_F(ManagedNetworkConfigurationHandlerTest, SetPolicyIgnoreUnmodified) {
502 InitializeStandardProfiles();
503 EXPECT_CALL(*mock_profile_client_, GetProperties(_, _, _));
505 EXPECT_CALL(*mock_manager_client_, ConfigureServiceForProfile(_, _, _, _));
507 SetPolicy(::onc::ONC_SOURCE_USER_POLICY, kUser1, "policy/policy_wifi1.onc");
508 message_loop_.RunUntilIdle();
509 EXPECT_EQ(1, policy_observer_.GetPoliciesAppliedCountAndReset());
510 VerifyAndClearExpectations();
512 SetUpEntry("policy/shill_policy_on_unmanaged_wifi1.json",
513 kUser1ProfilePath,
514 "some_entry_path");
516 EXPECT_CALL(*mock_profile_client_, GetProperties(_, _, _));
518 EXPECT_CALL(
519 *mock_profile_client_,
520 GetEntry(dbus::ObjectPath(kUser1ProfilePath), "some_entry_path", _, _));
522 SetPolicy(::onc::ONC_SOURCE_USER_POLICY, kUser1, "policy/policy_wifi1.onc");
524 message_loop_.RunUntilIdle();
525 EXPECT_EQ(1, policy_observer_.GetPoliciesAppliedCountAndReset());
528 TEST_F(ManagedNetworkConfigurationHandlerTest, PolicyApplicationRunning) {
529 InitializeStandardProfiles();
530 EXPECT_CALL(*mock_profile_client_, GetProperties(_, _, _)).Times(AnyNumber());
531 EXPECT_CALL(*mock_manager_client_, ConfigureServiceForProfile(_, _, _, _))
532 .Times(AnyNumber());
533 EXPECT_CALL(*mock_profile_client_, GetEntry(_, _, _, _)).Times(AnyNumber());
535 EXPECT_FALSE(managed_handler()->IsAnyPolicyApplicationRunning());
537 SetPolicy(::onc::ONC_SOURCE_USER_POLICY, kUser1, "policy/policy_wifi1.onc");
538 managed_handler()->SetPolicy(
539 ::onc::ONC_SOURCE_DEVICE_POLICY,
540 std::string(), // no userhash
541 base::ListValue(), // no device network policy
542 base::DictionaryValue()); // no device global config
544 EXPECT_TRUE(managed_handler()->IsAnyPolicyApplicationRunning());
545 message_loop_.RunUntilIdle();
546 EXPECT_FALSE(managed_handler()->IsAnyPolicyApplicationRunning());
548 SetUpEntry("policy/shill_policy_on_unmanaged_wifi1.json",
549 kUser1ProfilePath,
550 "some_entry_path");
552 SetPolicy(
553 ::onc::ONC_SOURCE_USER_POLICY, kUser1, "policy/policy_wifi1_update.onc");
554 EXPECT_TRUE(managed_handler()->IsAnyPolicyApplicationRunning());
555 message_loop_.RunUntilIdle();
556 EXPECT_FALSE(managed_handler()->IsAnyPolicyApplicationRunning());
559 TEST_F(ManagedNetworkConfigurationHandlerTest, UpdatePolicyAfterFinished) {
560 InitializeStandardProfiles();
561 EXPECT_CALL(*mock_profile_client_, GetProperties(_, _, _));
562 EXPECT_CALL(*mock_manager_client_, ConfigureServiceForProfile(_, _, _, _));
564 SetPolicy(::onc::ONC_SOURCE_USER_POLICY, kUser1, "policy/policy_wifi1.onc");
565 message_loop_.RunUntilIdle();
566 EXPECT_EQ(1, policy_observer_.GetPoliciesAppliedCountAndReset());
567 VerifyAndClearExpectations();
569 SetUpEntry("policy/shill_policy_on_unmanaged_wifi1.json",
570 kUser1ProfilePath,
571 "some_entry_path");
573 EXPECT_CALL(*mock_profile_client_, GetProperties(_, _, _));
574 EXPECT_CALL(
575 *mock_profile_client_,
576 GetEntry(dbus::ObjectPath(kUser1ProfilePath), "some_entry_path", _, _));
577 EXPECT_CALL(*mock_manager_client_, ConfigureServiceForProfile(_, _, _, _));
579 SetPolicy(
580 ::onc::ONC_SOURCE_USER_POLICY, kUser1, "policy/policy_wifi1_update.onc");
581 message_loop_.RunUntilIdle();
582 EXPECT_EQ(1, policy_observer_.GetPoliciesAppliedCountAndReset());
585 TEST_F(ManagedNetworkConfigurationHandlerTest, UpdatePolicyBeforeFinished) {
586 InitializeStandardProfiles();
587 EXPECT_CALL(*mock_profile_client_, GetProperties(_, _, _)).Times(2);
588 EXPECT_CALL(*mock_manager_client_, ConfigureServiceForProfile(_, _, _, _))
589 .Times(2);
591 SetPolicy(::onc::ONC_SOURCE_USER_POLICY, kUser1, "policy/policy_wifi1.onc");
592 // Usually the first call will cause a profile entry to be created, which we
593 // don't fake here.
594 SetPolicy(
595 ::onc::ONC_SOURCE_USER_POLICY, kUser1, "policy/policy_wifi1_update.onc");
597 message_loop_.RunUntilIdle();
598 EXPECT_EQ(1, policy_observer_.GetPoliciesAppliedCountAndReset());
601 TEST_F(ManagedNetworkConfigurationHandlerTest, SetPolicyManageUnmanaged) {
602 InitializeStandardProfiles();
603 SetUpEntry("policy/shill_unmanaged_wifi1.json",
604 kUser1ProfilePath,
605 "old_entry_path");
607 scoped_ptr<base::DictionaryValue> expected_shill_properties =
608 test_utils::ReadTestDictionary(
609 "policy/shill_policy_on_unmanaged_wifi1.json");
611 EXPECT_CALL(*mock_profile_client_,
612 GetProperties(dbus::ObjectPath(kUser1ProfilePath), _, _));
614 EXPECT_CALL(
615 *mock_profile_client_,
616 GetEntry(dbus::ObjectPath(kUser1ProfilePath), "old_entry_path", _, _));
618 EXPECT_CALL(
619 *mock_profile_client_,
620 DeleteEntry(dbus::ObjectPath(kUser1ProfilePath), "old_entry_path", _, _));
622 EXPECT_CALL(*mock_manager_client_,
623 ConfigureServiceForProfile(
624 dbus::ObjectPath(kUser1ProfilePath),
625 IsEqualTo(expected_shill_properties.get()),
626 _, _));
628 SetPolicy(::onc::ONC_SOURCE_USER_POLICY, kUser1, "policy/policy_wifi1.onc");
629 message_loop_.RunUntilIdle();
632 // Old ChromeOS versions may not have used the UIData property
633 TEST_F(ManagedNetworkConfigurationHandlerTest,
634 SetPolicyManageUnmanagedWithoutUIData) {
635 InitializeStandardProfiles();
636 SetUpEntry("policy/shill_unmanaged_wifi1.json",
637 kUser1ProfilePath,
638 "old_entry_path");
640 scoped_ptr<base::DictionaryValue> expected_shill_properties =
641 test_utils::ReadTestDictionary(
642 "policy/shill_policy_on_unmanaged_wifi1.json");
644 EXPECT_CALL(*mock_profile_client_,
645 GetProperties(dbus::ObjectPath(kUser1ProfilePath), _, _));
647 EXPECT_CALL(
648 *mock_profile_client_,
649 GetEntry(dbus::ObjectPath(kUser1ProfilePath), "old_entry_path", _, _));
651 EXPECT_CALL(
652 *mock_profile_client_,
653 DeleteEntry(dbus::ObjectPath(kUser1ProfilePath), "old_entry_path", _, _));
655 EXPECT_CALL(*mock_manager_client_,
656 ConfigureServiceForProfile(
657 dbus::ObjectPath(kUser1ProfilePath),
658 IsEqualTo(expected_shill_properties.get()),
659 _, _));
661 SetPolicy(::onc::ONC_SOURCE_USER_POLICY, kUser1, "policy/policy_wifi1.onc");
662 message_loop_.RunUntilIdle();
665 TEST_F(ManagedNetworkConfigurationHandlerTest, SetPolicyUpdateManagedNewGUID) {
666 InitializeStandardProfiles();
667 SetUpEntry("policy/shill_managed_wifi1.json",
668 kUser1ProfilePath,
669 "old_entry_path");
671 scoped_ptr<base::DictionaryValue> expected_shill_properties =
672 test_utils::ReadTestDictionary(
673 "policy/shill_policy_on_unmanaged_wifi1.json");
675 // The passphrase isn't sent again, because it's configured by the user and
676 // Shill doesn't send it on GetProperties calls.
677 expected_shill_properties->RemoveWithoutPathExpansion(
678 shill::kPassphraseProperty, NULL);
680 EXPECT_CALL(*mock_profile_client_,
681 GetProperties(dbus::ObjectPath(kUser1ProfilePath), _, _));
683 EXPECT_CALL(
684 *mock_profile_client_,
685 GetEntry(dbus::ObjectPath(kUser1ProfilePath), "old_entry_path", _, _));
687 EXPECT_CALL(
688 *mock_profile_client_,
689 DeleteEntry(dbus::ObjectPath(kUser1ProfilePath), "old_entry_path", _, _));
691 EXPECT_CALL(*mock_manager_client_,
692 ConfigureServiceForProfile(
693 dbus::ObjectPath(kUser1ProfilePath),
694 IsEqualTo(expected_shill_properties.get()),
695 _, _));
697 SetPolicy(::onc::ONC_SOURCE_USER_POLICY, kUser1, "policy/policy_wifi1.onc");
698 message_loop_.RunUntilIdle();
701 TEST_F(ManagedNetworkConfigurationHandlerTest, SetPolicyUpdateManagedVPN) {
702 InitializeStandardProfiles();
703 SetUpEntry("policy/shill_managed_vpn.json", kUser1ProfilePath, "entry_path");
705 scoped_ptr<base::DictionaryValue> expected_shill_properties =
706 test_utils::ReadTestDictionary(
707 "policy/shill_policy_on_managed_vpn.json");
709 EXPECT_CALL(*mock_profile_client_,
710 GetProperties(dbus::ObjectPath(kUser1ProfilePath), _, _));
712 EXPECT_CALL(
713 *mock_profile_client_,
714 GetEntry(dbus::ObjectPath(kUser1ProfilePath), "entry_path", _, _));
716 EXPECT_CALL(*mock_manager_client_,
717 ConfigureServiceForProfile(
718 dbus::ObjectPath(kUser1ProfilePath),
719 IsEqualTo(expected_shill_properties.get()),
720 _, _));
722 SetPolicy(::onc::ONC_SOURCE_USER_POLICY, kUser1, "policy/policy_vpn.onc");
723 message_loop_.RunUntilIdle();
724 VerifyAndClearExpectations();
727 TEST_F(ManagedNetworkConfigurationHandlerTest,
728 SetPolicyUpdateManagedEquivalentSecurity) {
729 InitializeStandardProfiles();
730 SetUpEntry("policy/shill_managed_wifi1_rsn.json",
731 kUser1ProfilePath,
732 "old_entry_path");
734 scoped_ptr<base::DictionaryValue> expected_shill_properties =
735 test_utils::ReadTestDictionary(
736 "policy/shill_policy_on_unmanaged_wifi1.json");
738 // The passphrase isn't sent again, because it's configured by the user and
739 // Shill doesn't send it on GetProperties calls.
740 expected_shill_properties->RemoveWithoutPathExpansion(
741 shill::kPassphraseProperty, NULL);
743 EXPECT_CALL(*mock_profile_client_,
744 GetProperties(dbus::ObjectPath(kUser1ProfilePath), _, _));
746 EXPECT_CALL(
747 *mock_profile_client_,
748 GetEntry(dbus::ObjectPath(kUser1ProfilePath), "old_entry_path", _, _));
750 // The existing entry must not be deleted because the Security type 'rsa' is
751 // equivalent to 'psk' when identifying networks.
753 EXPECT_CALL(
754 *mock_manager_client_,
755 ConfigureServiceForProfile(dbus::ObjectPath(kUser1ProfilePath),
756 IsEqualTo(expected_shill_properties.get()),
757 _, _));
759 SetPolicy(::onc::ONC_SOURCE_USER_POLICY, kUser1, "policy/policy_wifi1.onc");
760 message_loop_.RunUntilIdle();
763 TEST_F(ManagedNetworkConfigurationHandlerTest, SetPolicyReapplyToManaged) {
764 InitializeStandardProfiles();
765 SetUpEntry("policy/shill_policy_on_unmanaged_wifi1.json",
766 kUser1ProfilePath,
767 "old_entry_path");
769 scoped_ptr<base::DictionaryValue> expected_shill_properties =
770 test_utils::ReadTestDictionary(
771 "policy/shill_policy_on_unmanaged_wifi1.json");
773 // The passphrase isn't sent again, because it's configured by the user and
774 // Shill doesn't send it on GetProperties calls.
775 expected_shill_properties->RemoveWithoutPathExpansion(
776 shill::kPassphraseProperty, NULL);
778 EXPECT_CALL(*mock_profile_client_,
779 GetProperties(dbus::ObjectPath(kUser1ProfilePath), _, _));
781 EXPECT_CALL(
782 *mock_profile_client_,
783 GetEntry(dbus::ObjectPath(kUser1ProfilePath), "old_entry_path", _, _));
785 EXPECT_CALL(*mock_manager_client_,
786 ConfigureServiceForProfile(
787 dbus::ObjectPath(kUser1ProfilePath),
788 IsEqualTo(expected_shill_properties.get()),
789 _, _));
791 SetPolicy(::onc::ONC_SOURCE_USER_POLICY, kUser1, "policy/policy_wifi1.onc");
792 message_loop_.RunUntilIdle();
793 VerifyAndClearExpectations();
795 // If we apply the policy again, without change, then the Shill profile will
796 // not be modified.
797 EXPECT_CALL(*mock_profile_client_,
798 GetProperties(dbus::ObjectPath(kUser1ProfilePath), _, _));
800 EXPECT_CALL(
801 *mock_profile_client_,
802 GetEntry(dbus::ObjectPath(kUser1ProfilePath), "old_entry_path", _, _));
804 SetPolicy(::onc::ONC_SOURCE_USER_POLICY, kUser1, "policy/policy_wifi1.onc");
805 message_loop_.RunUntilIdle();
808 TEST_F(ManagedNetworkConfigurationHandlerTest, SetPolicyUnmanageManaged) {
809 InitializeStandardProfiles();
810 SetUpEntry("policy/shill_policy_on_unmanaged_wifi1.json",
811 kUser1ProfilePath,
812 "old_entry_path");
814 EXPECT_CALL(*mock_profile_client_,
815 GetProperties(dbus::ObjectPath(kUser1ProfilePath), _, _));
817 EXPECT_CALL(*mock_profile_client_,
818 GetEntry(dbus::ObjectPath(kUser1ProfilePath),
819 "old_entry_path",
820 _, _));
822 EXPECT_CALL(*mock_profile_client_,
823 DeleteEntry(dbus::ObjectPath(kUser1ProfilePath),
824 "old_entry_path",
825 _, _));
827 SetPolicy(::onc::ONC_SOURCE_USER_POLICY, kUser1, "");
828 message_loop_.RunUntilIdle();
831 TEST_F(ManagedNetworkConfigurationHandlerTest, SetEmptyPolicyIgnoreUnmanaged) {
832 InitializeStandardProfiles();
833 SetUpEntry("policy/shill_unmanaged_wifi1.json",
834 kUser1ProfilePath,
835 "old_entry_path");
837 EXPECT_CALL(*mock_profile_client_,
838 GetProperties(dbus::ObjectPath(kUser1ProfilePath), _, _));
840 EXPECT_CALL(*mock_profile_client_,
841 GetEntry(dbus::ObjectPath(kUser1ProfilePath),
842 "old_entry_path",
843 _, _));
845 SetPolicy(::onc::ONC_SOURCE_USER_POLICY, kUser1, "");
846 message_loop_.RunUntilIdle();
847 EXPECT_EQ(1, policy_observer_.GetPoliciesAppliedCountAndReset());
850 TEST_F(ManagedNetworkConfigurationHandlerTest, SetPolicyIgnoreUnmanaged) {
851 InitializeStandardProfiles();
852 SetUpEntry("policy/shill_unmanaged_wifi2.json",
853 kUser1ProfilePath,
854 "wifi2_entry_path");
856 EXPECT_CALL(*mock_profile_client_,
857 GetProperties(dbus::ObjectPath(kUser1ProfilePath), _, _));
859 EXPECT_CALL(
860 *mock_profile_client_,
861 GetEntry(dbus::ObjectPath(kUser1ProfilePath), "wifi2_entry_path", _, _));
863 scoped_ptr<base::DictionaryValue> expected_shill_properties =
864 test_utils::ReadTestDictionary(
865 "policy/shill_policy_on_unconfigured_wifi1.json");
867 EXPECT_CALL(*mock_manager_client_,
868 ConfigureServiceForProfile(
869 dbus::ObjectPath(kUser1ProfilePath),
870 IsEqualTo(expected_shill_properties.get()),
871 _, _));
873 SetPolicy(::onc::ONC_SOURCE_USER_POLICY, kUser1, "policy/policy_wifi1.onc");
874 message_loop_.RunUntilIdle();
877 TEST_F(ManagedNetworkConfigurationHandlerTest, AutoConnectDisallowed) {
878 InitializeStandardProfiles();
879 // Setup an unmanaged network.
880 SetUpEntry("policy/shill_unmanaged_wifi2.json",
881 kUser1ProfilePath,
882 "wifi2_entry_path");
884 // Apply the user policy with global autoconnect config and expect that
885 // autoconnect is disabled in the network's profile entry.
886 EXPECT_CALL(*mock_profile_client_,
887 GetProperties(dbus::ObjectPath(kUser1ProfilePath), _, _));
889 EXPECT_CALL(
890 *mock_profile_client_,
891 GetEntry(dbus::ObjectPath(kUser1ProfilePath), "wifi2_entry_path", _, _));
893 scoped_ptr<base::DictionaryValue> expected_shill_properties =
894 test_utils::ReadTestDictionary(
895 "policy/shill_disallow_autoconnect_on_unmanaged_wifi2.json");
897 EXPECT_CALL(*mock_manager_client_,
898 ConfigureServiceForProfile(
899 dbus::ObjectPath(kUser1ProfilePath),
900 IsEqualTo(expected_shill_properties.get()),
901 _, _));
903 SetPolicy(::onc::ONC_SOURCE_USER_POLICY,
904 kUser1,
905 "policy/policy_disallow_autoconnect.onc");
906 message_loop_.RunUntilIdle();
908 // Verify that GetManagedProperties correctly augments the properties with the
909 // global config from the user policy.
911 // GetManagedProperties requires the device policy to be set or explicitly
912 // unset.
913 EXPECT_CALL(*mock_profile_client_,
914 GetProperties(dbus::ObjectPath(
915 NetworkProfileHandler::GetSharedProfilePath()),
917 _));
918 managed_handler()->SetPolicy(
919 ::onc::ONC_SOURCE_DEVICE_POLICY,
920 std::string(), // no userhash
921 base::ListValue(), // no device network policy
922 base::DictionaryValue()); // no device global config
924 services_stub_.SetFakeProperties(*expected_shill_properties);
925 EXPECT_CALL(*mock_service_client_,
926 GetProperties(dbus::ObjectPath(
927 "wifi2"),_));
929 GetManagedProperties(kUser1, "wifi2");
930 message_loop_.RunUntilIdle();
932 EXPECT_EQ("wifi2", get_properties_service_path_);
934 scoped_ptr<base::DictionaryValue> expected_managed_onc =
935 test_utils::ReadTestDictionary(
936 "policy/managed_onc_disallow_autoconnect_on_unmanaged_wifi2.onc");
937 EXPECT_TRUE(onc::test_utils::Equals(expected_managed_onc.get(),
938 &get_properties_result_));
941 TEST_F(ManagedNetworkConfigurationHandlerTest, LateProfileLoading) {
942 SetPolicy(::onc::ONC_SOURCE_USER_POLICY, kUser1, "policy/policy_wifi1.onc");
944 message_loop_.RunUntilIdle();
945 VerifyAndClearExpectations();
947 scoped_ptr<base::DictionaryValue> expected_shill_properties =
948 test_utils::ReadTestDictionary(
949 "policy/shill_policy_on_unconfigured_wifi1.json");
951 EXPECT_CALL(*mock_profile_client_,
952 GetProperties(dbus::ObjectPath(kUser1ProfilePath), _, _));
954 EXPECT_CALL(*mock_manager_client_,
955 ConfigureServiceForProfile(
956 dbus::ObjectPath(kUser1ProfilePath),
957 IsEqualTo(expected_shill_properties.get()),
958 _, _));
960 InitializeStandardProfiles();
961 message_loop_.RunUntilIdle();
964 class ManagedNetworkConfigurationHandlerShutdownTest
965 : public ManagedNetworkConfigurationHandlerTest {
966 public:
967 void SetUp() override {
968 ManagedNetworkConfigurationHandlerTest::SetUp();
969 ON_CALL(*mock_profile_client_, GetProperties(_, _, _)).WillByDefault(
970 Invoke(&ManagedNetworkConfigurationHandlerShutdownTest::GetProperties));
973 static void GetProperties(
974 const dbus::ObjectPath& profile_path,
975 const ShillClientHelper::DictionaryValueCallbackWithoutStatus& callback,
976 const ShillClientHelper::ErrorCallback& error_callback) {
977 base::MessageLoop::current()->PostTask(
978 FROM_HERE,
979 base::Bind(ManagedNetworkConfigurationHandlerShutdownTest::
980 CallbackWithEmptyDictionary,
981 callback));
984 static void CallbackWithEmptyDictionary(
985 const ShillClientHelper::DictionaryValueCallbackWithoutStatus& callback) {
986 callback.Run(base::DictionaryValue());
990 TEST_F(ManagedNetworkConfigurationHandlerShutdownTest,
991 DuringPolicyApplication) {
992 InitializeStandardProfiles();
994 EXPECT_CALL(*mock_profile_client_,
995 GetProperties(dbus::ObjectPath(kUser1ProfilePath), _, _));
997 SetPolicy(::onc::ONC_SOURCE_USER_POLICY, kUser1, "policy/policy_wifi1.onc");
998 managed_network_configuration_handler_->RemoveObserver(&policy_observer_);
999 managed_network_configuration_handler_.reset();
1000 message_loop_.RunUntilIdle();
1003 } // namespace chromeos