Pin Chrome's shortcut to the Win10 Start menu on install and OS upgrade.
[chromium-blink-merge.git] / chromeos / network / managed_network_configuration_handler_unittest.cc
blobc95d6b1a87288f0e65fbb6d08edf8176c228af24
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/single_thread_task_runner.h"
14 #include "base/stl_util.h"
15 #include "base/thread_task_runner_handle.h"
16 #include "base/values.h"
17 #include "chromeos/dbus/dbus_thread_manager.h"
18 #include "chromeos/dbus/mock_shill_manager_client.h"
19 #include "chromeos/dbus/mock_shill_profile_client.h"
20 #include "chromeos/dbus/mock_shill_service_client.h"
21 #include "chromeos/dbus/shill_client_helper.h"
22 #include "chromeos/network/managed_network_configuration_handler_impl.h"
23 #include "chromeos/network/network_configuration_handler.h"
24 #include "chromeos/network/network_policy_observer.h"
25 #include "chromeos/network/network_profile_handler.h"
26 #include "chromeos/network/network_state_handler.h"
27 #include "chromeos/network/onc/onc_test_utils.h"
28 #include "chromeos/network/onc/onc_utils.h"
29 #include "dbus/object_path.h"
30 #include "testing/gmock/include/gmock/gmock.h"
31 #include "testing/gtest/include/gtest/gtest.h"
32 #include "third_party/cros_system_api/dbus/service_constants.h"
34 using ::testing::AnyNumber;
35 using ::testing::Invoke;
36 using ::testing::Mock;
37 using ::testing::Pointee;
38 using ::testing::Return;
39 using ::testing::SaveArg;
40 using ::testing::StrEq;
41 using ::testing::StrictMock;
42 using ::testing::_;
44 namespace test_utils = ::chromeos::onc::test_utils;
46 namespace chromeos {
48 namespace {
50 std::string ValueToString(const base::Value* value) {
51 std::stringstream str;
52 str << *value;
53 return str.str();
56 void DereferenceAndCall(
57 base::Callback<void(const base::DictionaryValue& result)> callback,
58 const base::DictionaryValue* value) {
59 callback.Run(*value);
62 const char kUser1[] = "user1";
63 const char kUser1ProfilePath[] = "/profile/user1/shill";
65 // Matcher to match base::Value.
66 MATCHER_P(IsEqualTo,
67 value,
68 std::string(negation ? "isn't" : "is") + " equal to " +
69 ValueToString(value)) {
70 return value->Equals(&arg);
73 // Match properties in |value| to |arg|. |arg| may contain extra properties).
74 MATCHER_P(MatchesProperties,
75 value,
76 std::string(negation ? "does't match " : "matches ") +
77 ValueToString(value)) {
78 for (base::DictionaryValue::Iterator iter(*value); !iter.IsAtEnd();
79 iter.Advance()) {
80 const base::Value* property;
81 if (!arg.GetWithoutPathExpansion(iter.key(), &property) ||
82 !iter.value().Equals(property)) {
83 return false;
86 return true;
89 class ShillProfileTestClient {
90 public:
91 typedef ShillClientHelper::DictionaryValueCallbackWithoutStatus
92 DictionaryValueCallbackWithoutStatus;
93 typedef ShillClientHelper::ErrorCallback ErrorCallback;
95 void AddProfile(const std::string& profile_path,
96 const std::string& userhash) {
97 if (profile_entries_.HasKey(profile_path))
98 return;
100 base::DictionaryValue* profile = new base::DictionaryValue;
101 profile_entries_.SetWithoutPathExpansion(profile_path, profile);
102 profile_to_user_[profile_path] = userhash;
105 void AddEntry(const std::string& profile_path,
106 const std::string& entry_path,
107 const base::DictionaryValue& entry) {
108 base::DictionaryValue* entries = NULL;
109 profile_entries_.GetDictionaryWithoutPathExpansion(profile_path, &entries);
110 ASSERT_TRUE(entries);
112 base::DictionaryValue* new_entry = entry.DeepCopy();
113 new_entry->SetStringWithoutPathExpansion(shill::kProfileProperty,
114 profile_path);
115 entries->SetWithoutPathExpansion(entry_path, new_entry);
118 void GetProperties(const dbus::ObjectPath& profile_path,
119 const DictionaryValueCallbackWithoutStatus& callback,
120 const ErrorCallback& error_callback) {
121 base::DictionaryValue* entries = NULL;
122 profile_entries_.GetDictionaryWithoutPathExpansion(profile_path.value(),
123 &entries);
124 ASSERT_TRUE(entries);
126 scoped_ptr<base::DictionaryValue> result(new base::DictionaryValue);
127 base::ListValue* entry_paths = new base::ListValue;
128 result->SetWithoutPathExpansion(shill::kEntriesProperty, entry_paths);
129 for (base::DictionaryValue::Iterator it(*entries); !it.IsAtEnd();
130 it.Advance()) {
131 entry_paths->AppendString(it.key());
134 ASSERT_TRUE(ContainsKey(profile_to_user_, profile_path.value()));
135 const std::string& userhash = profile_to_user_[profile_path.value()];
136 result->SetStringWithoutPathExpansion(shill::kUserHashProperty, userhash);
138 base::ThreadTaskRunnerHandle::Get()->PostTask(
139 FROM_HERE, base::Bind(base::Bind(&DereferenceAndCall, callback),
140 base::Owned(result.release())));
143 void GetEntry(const dbus::ObjectPath& profile_path,
144 const std::string& entry_path,
145 const DictionaryValueCallbackWithoutStatus& callback,
146 const ErrorCallback& error_callback) {
147 base::DictionaryValue* entries = NULL;
148 profile_entries_.GetDictionaryWithoutPathExpansion(profile_path.value(),
149 &entries);
150 ASSERT_TRUE(entries);
152 base::DictionaryValue* entry = NULL;
153 entries->GetDictionaryWithoutPathExpansion(entry_path, &entry);
154 ASSERT_TRUE(entry);
155 base::ThreadTaskRunnerHandle::Get()->PostTask(
156 FROM_HERE, base::Bind(base::Bind(&DereferenceAndCall, callback),
157 base::Owned(entry->DeepCopy())));
160 protected:
161 base::DictionaryValue profile_entries_;
162 std::map<std::string, std::string> profile_to_user_;
165 class ShillServiceTestClient {
166 public:
167 typedef ShillClientHelper::DictionaryValueCallback DictionaryValueCallback;
168 void SetFakeProperties(const base::DictionaryValue& service_properties) {
169 service_properties_.Clear();
170 service_properties_.MergeDictionary(&service_properties);
173 void GetProperties(const dbus::ObjectPath& service_path,
174 const DictionaryValueCallback& callback) {
175 base::ThreadTaskRunnerHandle::Get()->PostTask(
176 FROM_HERE, base::Bind(callback, DBUS_METHOD_CALL_SUCCESS,
177 base::ConstRef(service_properties_)));
180 protected:
181 base::DictionaryValue service_properties_;
184 class TestNetworkProfileHandler : public NetworkProfileHandler {
185 public:
186 TestNetworkProfileHandler() {
187 Init();
189 ~TestNetworkProfileHandler() override {}
191 void AddProfileForTest(const NetworkProfile& profile) {
192 AddProfile(profile);
195 private:
196 DISALLOW_COPY_AND_ASSIGN(TestNetworkProfileHandler);
199 class TestNetworkPolicyObserver : public NetworkPolicyObserver {
200 public:
201 TestNetworkPolicyObserver() : policies_applied_count_(0) {}
203 void PoliciesApplied(const std::string& userhash) override {
204 policies_applied_count_++;
207 int GetPoliciesAppliedCountAndReset() {
208 int count = policies_applied_count_;
209 policies_applied_count_ = 0;
210 return count;
213 private:
214 int policies_applied_count_;
216 DISALLOW_COPY_AND_ASSIGN(TestNetworkPolicyObserver);
219 } // namespace
221 class ManagedNetworkConfigurationHandlerTest : public testing::Test {
222 public:
223 ManagedNetworkConfigurationHandlerTest()
224 : mock_manager_client_(NULL),
225 mock_profile_client_(NULL),
226 mock_service_client_(NULL) {
229 ~ManagedNetworkConfigurationHandlerTest() override {}
231 void SetUp() override {
232 scoped_ptr<DBusThreadManagerSetter> dbus_setter =
233 DBusThreadManager::GetSetterForTesting();
234 mock_manager_client_ = new StrictMock<MockShillManagerClient>();
235 mock_profile_client_ = new StrictMock<MockShillProfileClient>();
236 mock_service_client_ = new StrictMock<MockShillServiceClient>();
237 dbus_setter->SetShillManagerClient(
238 scoped_ptr<ShillManagerClient>(mock_manager_client_).Pass());
239 dbus_setter->SetShillProfileClient(
240 scoped_ptr<ShillProfileClient>(mock_profile_client_).Pass());
241 dbus_setter->SetShillServiceClient(
242 scoped_ptr<ShillServiceClient>(mock_service_client_).Pass());
244 SetNetworkConfigurationHandlerExpectations();
246 ON_CALL(*mock_profile_client_, GetProperties(_,_,_))
247 .WillByDefault(Invoke(&profiles_stub_,
248 &ShillProfileTestClient::GetProperties));
250 ON_CALL(*mock_profile_client_, GetEntry(_,_,_,_))
251 .WillByDefault(Invoke(&profiles_stub_,
252 &ShillProfileTestClient::GetEntry));
254 ON_CALL(*mock_service_client_, GetProperties(_,_))
255 .WillByDefault(Invoke(&services_stub_,
256 &ShillServiceTestClient::GetProperties));
258 network_state_handler_.reset(NetworkStateHandler::InitializeForTest());
259 network_profile_handler_.reset(new TestNetworkProfileHandler());
260 network_configuration_handler_.reset(
261 NetworkConfigurationHandler::InitializeForTest(
262 network_state_handler_.get(),
263 NULL /* no NetworkDeviceHandler */));
264 managed_network_configuration_handler_.reset(
265 new ManagedNetworkConfigurationHandlerImpl());
266 managed_network_configuration_handler_->Init(
267 network_state_handler_.get(),
268 network_profile_handler_.get(),
269 network_configuration_handler_.get(),
270 NULL /* no DeviceHandler */);
271 managed_network_configuration_handler_->AddObserver(&policy_observer_);
273 message_loop_.RunUntilIdle();
276 void TearDown() override {
277 if (managed_network_configuration_handler_)
278 managed_network_configuration_handler_->RemoveObserver(&policy_observer_);
279 network_state_handler_.reset();
280 managed_network_configuration_handler_.reset();
281 network_configuration_handler_.reset();
282 network_profile_handler_.reset();
283 DBusThreadManager::Shutdown();
286 void VerifyAndClearExpectations() {
287 Mock::VerifyAndClearExpectations(mock_manager_client_);
288 Mock::VerifyAndClearExpectations(mock_profile_client_);
289 SetNetworkConfigurationHandlerExpectations();
292 void InitializeStandardProfiles() {
293 profiles_stub_.AddProfile(kUser1ProfilePath, kUser1);
294 network_profile_handler_->
295 AddProfileForTest(NetworkProfile(kUser1ProfilePath, kUser1));
297 profiles_stub_.AddProfile(NetworkProfileHandler::GetSharedProfilePath(),
298 std::string() /* no userhash */);
299 network_profile_handler_->AddProfileForTest(
300 NetworkProfile(NetworkProfileHandler::GetSharedProfilePath(),
301 std::string() /* no userhash */));
304 void SetUpEntry(const std::string& path_to_shill_json,
305 const std::string& profile_path,
306 const std::string& entry_path) {
307 scoped_ptr<base::DictionaryValue> entry =
308 test_utils::ReadTestDictionary(path_to_shill_json);
309 profiles_stub_.AddEntry(profile_path, entry_path, *entry);
312 void SetPolicy(::onc::ONCSource onc_source,
313 const std::string& userhash,
314 const std::string& path_to_onc) {
315 scoped_ptr<base::DictionaryValue> policy;
316 if (path_to_onc.empty())
317 policy = onc::ReadDictionaryFromJson(onc::kEmptyUnencryptedConfiguration);
318 else
319 policy = test_utils::ReadTestDictionary(path_to_onc);
321 base::ListValue empty_network_configs;
322 base::ListValue* network_configs = &empty_network_configs;
323 policy->GetListWithoutPathExpansion(
324 ::onc::toplevel_config::kNetworkConfigurations, &network_configs);
326 base::DictionaryValue empty_global_config;
327 base::DictionaryValue* global_network_config = &empty_global_config;
328 policy->GetDictionaryWithoutPathExpansion(
329 ::onc::toplevel_config::kGlobalNetworkConfiguration,
330 &global_network_config);
332 managed_handler()->SetPolicy(
333 onc_source, userhash, *network_configs, *global_network_config);
336 void SetNetworkConfigurationHandlerExpectations() {
337 // These calls occur in NetworkConfigurationHandler.
338 EXPECT_CALL(*mock_manager_client_, GetProperties(_)).Times(AnyNumber());
339 EXPECT_CALL(*mock_manager_client_,
340 AddPropertyChangedObserver(_)).Times(AnyNumber());
341 EXPECT_CALL(*mock_manager_client_,
342 RemovePropertyChangedObserver(_)).Times(AnyNumber());
345 ManagedNetworkConfigurationHandler* managed_handler() {
346 return managed_network_configuration_handler_.get();
349 void GetManagedProperties(const std::string& userhash,
350 const std::string& service_path) {
351 managed_handler()->GetManagedProperties(
352 userhash,
353 service_path,
354 base::Bind(
355 &ManagedNetworkConfigurationHandlerTest::GetPropertiesCallback,
356 base::Unretained(this)),
357 base::Bind(&ManagedNetworkConfigurationHandlerTest::UnexpectedError));
360 void GetPropertiesCallback(const std::string& service_path,
361 const base::DictionaryValue& dictionary) {
362 get_properties_service_path_ = service_path;
363 get_properties_result_.Clear();
364 get_properties_result_.MergeDictionary(&dictionary);
367 static void UnexpectedError(const std::string& error_name,
368 scoped_ptr<base::DictionaryValue> error_data) {
369 ASSERT_FALSE(true);
372 protected:
373 MockShillManagerClient* mock_manager_client_;
374 MockShillProfileClient* mock_profile_client_;
375 MockShillServiceClient* mock_service_client_;
376 ShillProfileTestClient profiles_stub_;
377 ShillServiceTestClient services_stub_;
378 TestNetworkPolicyObserver policy_observer_;
379 scoped_ptr<NetworkStateHandler> network_state_handler_;
380 scoped_ptr<TestNetworkProfileHandler> network_profile_handler_;
381 scoped_ptr<NetworkConfigurationHandler> network_configuration_handler_;
382 scoped_ptr<ManagedNetworkConfigurationHandlerImpl>
383 managed_network_configuration_handler_;
384 base::MessageLoop message_loop_;
386 std::string get_properties_service_path_;
387 base::DictionaryValue get_properties_result_;
389 private:
390 DISALLOW_COPY_AND_ASSIGN(ManagedNetworkConfigurationHandlerTest);
393 TEST_F(ManagedNetworkConfigurationHandlerTest, ProfileInitialization) {
394 InitializeStandardProfiles();
395 message_loop_.RunUntilIdle();
398 TEST_F(ManagedNetworkConfigurationHandlerTest, RemoveIrrelevantFields) {
399 InitializeStandardProfiles();
400 scoped_ptr<base::DictionaryValue> expected_shill_properties =
401 test_utils::ReadTestDictionary(
402 "policy/shill_policy_on_unconfigured_wifi1.json");
404 EXPECT_CALL(*mock_profile_client_,
405 GetProperties(dbus::ObjectPath(kUser1ProfilePath), _, _));
407 EXPECT_CALL(*mock_manager_client_,
408 ConfigureServiceForProfile(
409 dbus::ObjectPath(kUser1ProfilePath),
410 IsEqualTo(expected_shill_properties.get()),
411 _, _));
413 SetPolicy(::onc::ONC_SOURCE_USER_POLICY,
414 kUser1,
415 "policy/policy_wifi1_with_redundant_fields.onc");
416 message_loop_.RunUntilIdle();
419 TEST_F(ManagedNetworkConfigurationHandlerTest, SetPolicyManageUnconfigured) {
420 InitializeStandardProfiles();
421 scoped_ptr<base::DictionaryValue> expected_shill_properties =
422 test_utils::ReadTestDictionary(
423 "policy/shill_policy_on_unconfigured_wifi1.json");
425 EXPECT_CALL(*mock_profile_client_,
426 GetProperties(dbus::ObjectPath(kUser1ProfilePath), _, _));
428 EXPECT_CALL(*mock_manager_client_,
429 ConfigureServiceForProfile(
430 dbus::ObjectPath(kUser1ProfilePath),
431 IsEqualTo(expected_shill_properties.get()),
432 _, _));
434 SetPolicy(::onc::ONC_SOURCE_USER_POLICY, kUser1, "policy/policy_wifi1.onc");
435 message_loop_.RunUntilIdle();
438 TEST_F(ManagedNetworkConfigurationHandlerTest, EnableManagedCredentialsWiFi) {
439 InitializeStandardProfiles();
440 scoped_ptr<base::DictionaryValue> expected_shill_properties =
441 test_utils::ReadTestDictionary(
442 "policy/shill_policy_autoconnect_on_unconfigured_wifi1.json");
444 EXPECT_CALL(*mock_profile_client_,
445 GetProperties(dbus::ObjectPath(kUser1ProfilePath), _, _));
447 EXPECT_CALL(*mock_manager_client_,
448 ConfigureServiceForProfile(
449 dbus::ObjectPath(kUser1ProfilePath),
450 IsEqualTo(expected_shill_properties.get()),
451 _, _));
453 SetPolicy(::onc::ONC_SOURCE_USER_POLICY, kUser1,
454 "policy/policy_wifi1_autoconnect.onc");
455 message_loop_.RunUntilIdle();
458 TEST_F(ManagedNetworkConfigurationHandlerTest, EnableManagedCredentialsVPN) {
459 InitializeStandardProfiles();
460 scoped_ptr<base::DictionaryValue> expected_shill_properties =
461 test_utils::ReadTestDictionary(
462 "policy/shill_policy_autoconnect_on_unconfigured_vpn.json");
464 EXPECT_CALL(*mock_profile_client_,
465 GetProperties(dbus::ObjectPath(kUser1ProfilePath), _, _));
467 EXPECT_CALL(*mock_manager_client_,
468 ConfigureServiceForProfile(
469 dbus::ObjectPath(kUser1ProfilePath),
470 IsEqualTo(expected_shill_properties.get()),
471 _, _));
473 SetPolicy(::onc::ONC_SOURCE_USER_POLICY, kUser1,
474 "policy/policy_vpn_autoconnect.onc");
475 message_loop_.RunUntilIdle();
478 // Ensure that EAP settings for ethernet are matched with the right profile
479 // entry and written to the dedicated EthernetEAP service.
480 TEST_F(ManagedNetworkConfigurationHandlerTest,
481 SetPolicyManageUnmanagedEthernetEAP) {
482 InitializeStandardProfiles();
483 scoped_ptr<base::DictionaryValue> expected_shill_properties =
484 test_utils::ReadTestDictionary(
485 "policy/"
486 "shill_policy_on_unmanaged_ethernet_eap.json");
488 SetUpEntry("policy/shill_unmanaged_ethernet_eap.json",
489 kUser1ProfilePath,
490 "eth_entry");
492 // Also setup an unrelated WiFi configuration to verify that the right entry
493 // is matched.
494 SetUpEntry("policy/shill_unmanaged_wifi1.json",
495 kUser1ProfilePath,
496 "wifi_entry");
498 EXPECT_CALL(*mock_profile_client_,
499 GetProperties(dbus::ObjectPath(kUser1ProfilePath), _, _));
501 EXPECT_CALL(*mock_profile_client_,
502 GetEntry(dbus::ObjectPath(kUser1ProfilePath), _, _, _)).Times(2);
504 EXPECT_CALL(
505 *mock_profile_client_,
506 DeleteEntry(dbus::ObjectPath(kUser1ProfilePath), "eth_entry", _, _));
508 EXPECT_CALL(
509 *mock_manager_client_,
510 ConfigureServiceForProfile(dbus::ObjectPath(kUser1ProfilePath),
511 IsEqualTo(expected_shill_properties.get()),
512 _, _));
514 SetPolicy(
515 ::onc::ONC_SOURCE_USER_POLICY, kUser1, "policy/policy_ethernet_eap.onc");
516 message_loop_.RunUntilIdle();
519 TEST_F(ManagedNetworkConfigurationHandlerTest, SetPolicyIgnoreUnmodified) {
520 InitializeStandardProfiles();
521 EXPECT_CALL(*mock_profile_client_, GetProperties(_, _, _));
523 EXPECT_CALL(*mock_manager_client_, ConfigureServiceForProfile(_, _, _, _));
525 SetPolicy(::onc::ONC_SOURCE_USER_POLICY, kUser1, "policy/policy_wifi1.onc");
526 message_loop_.RunUntilIdle();
527 EXPECT_EQ(1, policy_observer_.GetPoliciesAppliedCountAndReset());
528 VerifyAndClearExpectations();
530 SetUpEntry("policy/shill_policy_on_unmanaged_wifi1.json",
531 kUser1ProfilePath,
532 "some_entry_path");
534 EXPECT_CALL(*mock_profile_client_, GetProperties(_, _, _));
536 EXPECT_CALL(
537 *mock_profile_client_,
538 GetEntry(dbus::ObjectPath(kUser1ProfilePath), "some_entry_path", _, _));
540 SetPolicy(::onc::ONC_SOURCE_USER_POLICY, kUser1, "policy/policy_wifi1.onc");
542 message_loop_.RunUntilIdle();
543 EXPECT_EQ(1, policy_observer_.GetPoliciesAppliedCountAndReset());
546 TEST_F(ManagedNetworkConfigurationHandlerTest, PolicyApplicationRunning) {
547 InitializeStandardProfiles();
548 EXPECT_CALL(*mock_profile_client_, GetProperties(_, _, _)).Times(AnyNumber());
549 EXPECT_CALL(*mock_manager_client_, ConfigureServiceForProfile(_, _, _, _))
550 .Times(AnyNumber());
551 EXPECT_CALL(*mock_profile_client_, GetEntry(_, _, _, _)).Times(AnyNumber());
553 EXPECT_FALSE(managed_handler()->IsAnyPolicyApplicationRunning());
555 SetPolicy(::onc::ONC_SOURCE_USER_POLICY, kUser1, "policy/policy_wifi1.onc");
556 managed_handler()->SetPolicy(
557 ::onc::ONC_SOURCE_DEVICE_POLICY,
558 std::string(), // no userhash
559 base::ListValue(), // no device network policy
560 base::DictionaryValue()); // no device global config
562 EXPECT_TRUE(managed_handler()->IsAnyPolicyApplicationRunning());
563 message_loop_.RunUntilIdle();
564 EXPECT_FALSE(managed_handler()->IsAnyPolicyApplicationRunning());
566 SetUpEntry("policy/shill_policy_on_unmanaged_wifi1.json",
567 kUser1ProfilePath,
568 "some_entry_path");
570 SetPolicy(
571 ::onc::ONC_SOURCE_USER_POLICY, kUser1, "policy/policy_wifi1_update.onc");
572 EXPECT_TRUE(managed_handler()->IsAnyPolicyApplicationRunning());
573 message_loop_.RunUntilIdle();
574 EXPECT_FALSE(managed_handler()->IsAnyPolicyApplicationRunning());
577 TEST_F(ManagedNetworkConfigurationHandlerTest, UpdatePolicyAfterFinished) {
578 InitializeStandardProfiles();
579 EXPECT_CALL(*mock_profile_client_, GetProperties(_, _, _));
580 EXPECT_CALL(*mock_manager_client_, ConfigureServiceForProfile(_, _, _, _));
582 SetPolicy(::onc::ONC_SOURCE_USER_POLICY, kUser1, "policy/policy_wifi1.onc");
583 message_loop_.RunUntilIdle();
584 EXPECT_EQ(1, policy_observer_.GetPoliciesAppliedCountAndReset());
585 VerifyAndClearExpectations();
587 SetUpEntry("policy/shill_policy_on_unmanaged_wifi1.json",
588 kUser1ProfilePath,
589 "some_entry_path");
591 EXPECT_CALL(*mock_profile_client_, GetProperties(_, _, _));
592 EXPECT_CALL(
593 *mock_profile_client_,
594 GetEntry(dbus::ObjectPath(kUser1ProfilePath), "some_entry_path", _, _));
595 EXPECT_CALL(*mock_manager_client_, ConfigureServiceForProfile(_, _, _, _));
597 SetPolicy(
598 ::onc::ONC_SOURCE_USER_POLICY, kUser1, "policy/policy_wifi1_update.onc");
599 message_loop_.RunUntilIdle();
600 EXPECT_EQ(1, policy_observer_.GetPoliciesAppliedCountAndReset());
603 TEST_F(ManagedNetworkConfigurationHandlerTest, UpdatePolicyBeforeFinished) {
604 InitializeStandardProfiles();
605 EXPECT_CALL(*mock_profile_client_, GetProperties(_, _, _)).Times(2);
606 EXPECT_CALL(*mock_manager_client_, ConfigureServiceForProfile(_, _, _, _))
607 .Times(2);
609 SetPolicy(::onc::ONC_SOURCE_USER_POLICY, kUser1, "policy/policy_wifi1.onc");
610 // Usually the first call will cause a profile entry to be created, which we
611 // don't fake here.
612 SetPolicy(
613 ::onc::ONC_SOURCE_USER_POLICY, kUser1, "policy/policy_wifi1_update.onc");
615 message_loop_.RunUntilIdle();
616 EXPECT_EQ(1, policy_observer_.GetPoliciesAppliedCountAndReset());
619 TEST_F(ManagedNetworkConfigurationHandlerTest, SetPolicyManageUnmanaged) {
620 InitializeStandardProfiles();
621 SetUpEntry("policy/shill_unmanaged_wifi1.json",
622 kUser1ProfilePath,
623 "old_entry_path");
625 scoped_ptr<base::DictionaryValue> expected_shill_properties =
626 test_utils::ReadTestDictionary(
627 "policy/shill_policy_on_unmanaged_wifi1.json");
629 EXPECT_CALL(*mock_profile_client_,
630 GetProperties(dbus::ObjectPath(kUser1ProfilePath), _, _));
632 EXPECT_CALL(
633 *mock_profile_client_,
634 GetEntry(dbus::ObjectPath(kUser1ProfilePath), "old_entry_path", _, _));
636 EXPECT_CALL(
637 *mock_profile_client_,
638 DeleteEntry(dbus::ObjectPath(kUser1ProfilePath), "old_entry_path", _, _));
640 EXPECT_CALL(*mock_manager_client_,
641 ConfigureServiceForProfile(
642 dbus::ObjectPath(kUser1ProfilePath),
643 IsEqualTo(expected_shill_properties.get()),
644 _, _));
646 SetPolicy(::onc::ONC_SOURCE_USER_POLICY, kUser1, "policy/policy_wifi1.onc");
647 message_loop_.RunUntilIdle();
650 // Old ChromeOS versions may not have used the UIData property
651 TEST_F(ManagedNetworkConfigurationHandlerTest,
652 SetPolicyManageUnmanagedWithoutUIData) {
653 InitializeStandardProfiles();
654 SetUpEntry("policy/shill_unmanaged_wifi1.json",
655 kUser1ProfilePath,
656 "old_entry_path");
658 scoped_ptr<base::DictionaryValue> expected_shill_properties =
659 test_utils::ReadTestDictionary(
660 "policy/shill_policy_on_unmanaged_wifi1.json");
662 EXPECT_CALL(*mock_profile_client_,
663 GetProperties(dbus::ObjectPath(kUser1ProfilePath), _, _));
665 EXPECT_CALL(
666 *mock_profile_client_,
667 GetEntry(dbus::ObjectPath(kUser1ProfilePath), "old_entry_path", _, _));
669 EXPECT_CALL(
670 *mock_profile_client_,
671 DeleteEntry(dbus::ObjectPath(kUser1ProfilePath), "old_entry_path", _, _));
673 EXPECT_CALL(*mock_manager_client_,
674 ConfigureServiceForProfile(
675 dbus::ObjectPath(kUser1ProfilePath),
676 IsEqualTo(expected_shill_properties.get()),
677 _, _));
679 SetPolicy(::onc::ONC_SOURCE_USER_POLICY, kUser1, "policy/policy_wifi1.onc");
680 message_loop_.RunUntilIdle();
683 TEST_F(ManagedNetworkConfigurationHandlerTest, SetPolicyUpdateManagedNewGUID) {
684 InitializeStandardProfiles();
685 SetUpEntry("policy/shill_managed_wifi1.json",
686 kUser1ProfilePath,
687 "old_entry_path");
689 scoped_ptr<base::DictionaryValue> expected_shill_properties =
690 test_utils::ReadTestDictionary(
691 "policy/shill_policy_on_unmanaged_wifi1.json");
693 // The passphrase isn't sent again, because it's configured by the user and
694 // Shill doesn't send it on GetProperties calls.
695 expected_shill_properties->RemoveWithoutPathExpansion(
696 shill::kPassphraseProperty, NULL);
698 EXPECT_CALL(*mock_profile_client_,
699 GetProperties(dbus::ObjectPath(kUser1ProfilePath), _, _));
701 EXPECT_CALL(
702 *mock_profile_client_,
703 GetEntry(dbus::ObjectPath(kUser1ProfilePath), "old_entry_path", _, _));
705 EXPECT_CALL(
706 *mock_profile_client_,
707 DeleteEntry(dbus::ObjectPath(kUser1ProfilePath), "old_entry_path", _, _));
709 EXPECT_CALL(*mock_manager_client_,
710 ConfigureServiceForProfile(
711 dbus::ObjectPath(kUser1ProfilePath),
712 IsEqualTo(expected_shill_properties.get()),
713 _, _));
715 SetPolicy(::onc::ONC_SOURCE_USER_POLICY, kUser1, "policy/policy_wifi1.onc");
716 message_loop_.RunUntilIdle();
719 TEST_F(ManagedNetworkConfigurationHandlerTest, SetPolicyUpdateManagedVPN) {
720 InitializeStandardProfiles();
721 SetUpEntry("policy/shill_managed_vpn.json", kUser1ProfilePath, "entry_path");
723 scoped_ptr<base::DictionaryValue> expected_shill_properties =
724 test_utils::ReadTestDictionary(
725 "policy/shill_policy_on_managed_vpn.json");
727 EXPECT_CALL(*mock_profile_client_,
728 GetProperties(dbus::ObjectPath(kUser1ProfilePath), _, _));
730 EXPECT_CALL(
731 *mock_profile_client_,
732 GetEntry(dbus::ObjectPath(kUser1ProfilePath), "entry_path", _, _));
734 EXPECT_CALL(*mock_manager_client_,
735 ConfigureServiceForProfile(
736 dbus::ObjectPath(kUser1ProfilePath),
737 IsEqualTo(expected_shill_properties.get()),
738 _, _));
740 SetPolicy(::onc::ONC_SOURCE_USER_POLICY, kUser1, "policy/policy_vpn.onc");
741 message_loop_.RunUntilIdle();
742 VerifyAndClearExpectations();
745 TEST_F(ManagedNetworkConfigurationHandlerTest, SetPolicyReapplyToManaged) {
746 InitializeStandardProfiles();
747 SetUpEntry("policy/shill_policy_on_unmanaged_wifi1.json",
748 kUser1ProfilePath,
749 "old_entry_path");
751 scoped_ptr<base::DictionaryValue> expected_shill_properties =
752 test_utils::ReadTestDictionary(
753 "policy/shill_policy_on_unmanaged_wifi1.json");
755 // The passphrase isn't sent again, because it's configured by the user and
756 // Shill doesn't send it on GetProperties calls.
757 expected_shill_properties->RemoveWithoutPathExpansion(
758 shill::kPassphraseProperty, NULL);
760 EXPECT_CALL(*mock_profile_client_,
761 GetProperties(dbus::ObjectPath(kUser1ProfilePath), _, _));
763 EXPECT_CALL(
764 *mock_profile_client_,
765 GetEntry(dbus::ObjectPath(kUser1ProfilePath), "old_entry_path", _, _));
767 EXPECT_CALL(*mock_manager_client_,
768 ConfigureServiceForProfile(
769 dbus::ObjectPath(kUser1ProfilePath),
770 IsEqualTo(expected_shill_properties.get()),
771 _, _));
773 SetPolicy(::onc::ONC_SOURCE_USER_POLICY, kUser1, "policy/policy_wifi1.onc");
774 message_loop_.RunUntilIdle();
775 VerifyAndClearExpectations();
777 // If we apply the policy again, without change, then the Shill profile will
778 // not be modified.
779 EXPECT_CALL(*mock_profile_client_,
780 GetProperties(dbus::ObjectPath(kUser1ProfilePath), _, _));
782 EXPECT_CALL(
783 *mock_profile_client_,
784 GetEntry(dbus::ObjectPath(kUser1ProfilePath), "old_entry_path", _, _));
786 SetPolicy(::onc::ONC_SOURCE_USER_POLICY, kUser1, "policy/policy_wifi1.onc");
787 message_loop_.RunUntilIdle();
790 TEST_F(ManagedNetworkConfigurationHandlerTest, SetPolicyUnmanageManaged) {
791 InitializeStandardProfiles();
792 SetUpEntry("policy/shill_policy_on_unmanaged_wifi1.json",
793 kUser1ProfilePath,
794 "old_entry_path");
796 EXPECT_CALL(*mock_profile_client_,
797 GetProperties(dbus::ObjectPath(kUser1ProfilePath), _, _));
799 EXPECT_CALL(*mock_profile_client_,
800 GetEntry(dbus::ObjectPath(kUser1ProfilePath),
801 "old_entry_path",
802 _, _));
804 EXPECT_CALL(*mock_profile_client_,
805 DeleteEntry(dbus::ObjectPath(kUser1ProfilePath),
806 "old_entry_path",
807 _, _));
809 SetPolicy(::onc::ONC_SOURCE_USER_POLICY, kUser1, "");
810 message_loop_.RunUntilIdle();
813 TEST_F(ManagedNetworkConfigurationHandlerTest, SetEmptyPolicyIgnoreUnmanaged) {
814 InitializeStandardProfiles();
815 SetUpEntry("policy/shill_unmanaged_wifi1.json",
816 kUser1ProfilePath,
817 "old_entry_path");
819 EXPECT_CALL(*mock_profile_client_,
820 GetProperties(dbus::ObjectPath(kUser1ProfilePath), _, _));
822 EXPECT_CALL(*mock_profile_client_,
823 GetEntry(dbus::ObjectPath(kUser1ProfilePath),
824 "old_entry_path",
825 _, _));
827 SetPolicy(::onc::ONC_SOURCE_USER_POLICY, kUser1, "");
828 message_loop_.RunUntilIdle();
829 EXPECT_EQ(1, policy_observer_.GetPoliciesAppliedCountAndReset());
832 TEST_F(ManagedNetworkConfigurationHandlerTest, SetPolicyIgnoreUnmanaged) {
833 InitializeStandardProfiles();
834 SetUpEntry("policy/shill_unmanaged_wifi2.json",
835 kUser1ProfilePath,
836 "wifi2_entry_path");
838 EXPECT_CALL(*mock_profile_client_,
839 GetProperties(dbus::ObjectPath(kUser1ProfilePath), _, _));
841 EXPECT_CALL(
842 *mock_profile_client_,
843 GetEntry(dbus::ObjectPath(kUser1ProfilePath), "wifi2_entry_path", _, _));
845 scoped_ptr<base::DictionaryValue> expected_shill_properties =
846 test_utils::ReadTestDictionary(
847 "policy/shill_policy_on_unconfigured_wifi1.json");
849 EXPECT_CALL(*mock_manager_client_,
850 ConfigureServiceForProfile(
851 dbus::ObjectPath(kUser1ProfilePath),
852 IsEqualTo(expected_shill_properties.get()),
853 _, _));
855 SetPolicy(::onc::ONC_SOURCE_USER_POLICY, kUser1, "policy/policy_wifi1.onc");
856 message_loop_.RunUntilIdle();
859 TEST_F(ManagedNetworkConfigurationHandlerTest, AutoConnectDisallowed) {
860 InitializeStandardProfiles();
861 // Setup an unmanaged network.
862 SetUpEntry("policy/shill_unmanaged_wifi2.json",
863 kUser1ProfilePath,
864 "wifi2_entry_path");
866 // Apply the user policy with global autoconnect config and expect that
867 // autoconnect is disabled in the network's profile entry.
868 EXPECT_CALL(*mock_profile_client_,
869 GetProperties(dbus::ObjectPath(kUser1ProfilePath), _, _));
871 EXPECT_CALL(
872 *mock_profile_client_,
873 GetEntry(dbus::ObjectPath(kUser1ProfilePath), "wifi2_entry_path", _, _));
875 scoped_ptr<base::DictionaryValue> expected_shill_properties =
876 test_utils::ReadTestDictionary(
877 "policy/shill_disallow_autoconnect_on_unmanaged_wifi2.json");
879 EXPECT_CALL(*mock_manager_client_,
880 ConfigureServiceForProfile(
881 dbus::ObjectPath(kUser1ProfilePath),
882 MatchesProperties(expected_shill_properties.get()), _, _));
884 SetPolicy(::onc::ONC_SOURCE_USER_POLICY,
885 kUser1,
886 "policy/policy_disallow_autoconnect.onc");
887 message_loop_.RunUntilIdle();
889 // Verify that GetManagedProperties correctly augments the properties with the
890 // global config from the user policy.
892 // GetManagedProperties requires the device policy to be set or explicitly
893 // unset.
894 EXPECT_CALL(*mock_profile_client_,
895 GetProperties(dbus::ObjectPath(
896 NetworkProfileHandler::GetSharedProfilePath()),
898 _));
899 managed_handler()->SetPolicy(
900 ::onc::ONC_SOURCE_DEVICE_POLICY,
901 std::string(), // no userhash
902 base::ListValue(), // no device network policy
903 base::DictionaryValue()); // no device global config
905 services_stub_.SetFakeProperties(*expected_shill_properties);
906 EXPECT_CALL(*mock_service_client_,
907 GetProperties(dbus::ObjectPath(
908 "wifi2"),_));
910 GetManagedProperties(kUser1, "wifi2");
911 message_loop_.RunUntilIdle();
913 EXPECT_EQ("wifi2", get_properties_service_path_);
915 scoped_ptr<base::DictionaryValue> expected_managed_onc =
916 test_utils::ReadTestDictionary(
917 "policy/managed_onc_disallow_autoconnect_on_unmanaged_wifi2.onc");
918 EXPECT_TRUE(onc::test_utils::Equals(expected_managed_onc.get(),
919 &get_properties_result_));
922 TEST_F(ManagedNetworkConfigurationHandlerTest, LateProfileLoading) {
923 SetPolicy(::onc::ONC_SOURCE_USER_POLICY, kUser1, "policy/policy_wifi1.onc");
925 message_loop_.RunUntilIdle();
926 VerifyAndClearExpectations();
928 scoped_ptr<base::DictionaryValue> expected_shill_properties =
929 test_utils::ReadTestDictionary(
930 "policy/shill_policy_on_unconfigured_wifi1.json");
932 EXPECT_CALL(*mock_profile_client_,
933 GetProperties(dbus::ObjectPath(kUser1ProfilePath), _, _));
935 EXPECT_CALL(*mock_manager_client_,
936 ConfigureServiceForProfile(
937 dbus::ObjectPath(kUser1ProfilePath),
938 IsEqualTo(expected_shill_properties.get()),
939 _, _));
941 InitializeStandardProfiles();
942 message_loop_.RunUntilIdle();
945 class ManagedNetworkConfigurationHandlerShutdownTest
946 : public ManagedNetworkConfigurationHandlerTest {
947 public:
948 void SetUp() override {
949 ManagedNetworkConfigurationHandlerTest::SetUp();
950 ON_CALL(*mock_profile_client_, GetProperties(_, _, _)).WillByDefault(
951 Invoke(&ManagedNetworkConfigurationHandlerShutdownTest::GetProperties));
954 static void GetProperties(
955 const dbus::ObjectPath& profile_path,
956 const ShillClientHelper::DictionaryValueCallbackWithoutStatus& callback,
957 const ShillClientHelper::ErrorCallback& error_callback) {
958 base::ThreadTaskRunnerHandle::Get()->PostTask(
959 FROM_HERE, base::Bind(ManagedNetworkConfigurationHandlerShutdownTest::
960 CallbackWithEmptyDictionary,
961 callback));
964 static void CallbackWithEmptyDictionary(
965 const ShillClientHelper::DictionaryValueCallbackWithoutStatus& callback) {
966 callback.Run(base::DictionaryValue());
970 TEST_F(ManagedNetworkConfigurationHandlerShutdownTest,
971 DuringPolicyApplication) {
972 InitializeStandardProfiles();
974 EXPECT_CALL(*mock_profile_client_,
975 GetProperties(dbus::ObjectPath(kUser1ProfilePath), _, _));
977 SetPolicy(::onc::ONC_SOURCE_USER_POLICY, kUser1, "policy/policy_wifi1.onc");
978 managed_network_configuration_handler_->RemoveObserver(&policy_observer_);
979 managed_network_configuration_handler_.reset();
980 message_loop_.RunUntilIdle();
983 } // namespace chromeos