[Telemetry] Reenable unused-import lint check for telemetry.
[chromium-blink-merge.git] / chromeos / network / managed_network_configuration_handler_unittest.cc
blob4d965eb13e5412b24b42d845f287dc8cfe62ebfa
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/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;
40 using ::testing::_;
42 namespace test_utils = ::chromeos::onc::test_utils;
44 namespace chromeos {
46 namespace {
48 std::string ValueToString(const base::Value* value) {
49 std::stringstream str;
50 str << *value;
51 return str.str();
54 void DereferenceAndCall(
55 base::Callback<void(const base::DictionaryValue& result)> callback,
56 const base::DictionaryValue* value) {
57 callback.Run(*value);
60 const char kUser1[] = "user1";
61 const char kUser1ProfilePath[] = "/profile/user1/shill";
63 // Matcher to match base::Value.
64 MATCHER_P(IsEqualTo,
65 value,
66 std::string(negation ? "isn't" : "is") + " equal to " +
67 ValueToString(value)) {
68 return value->Equals(&arg);
71 // Match properties in |value| to |arg|. |arg| may contain extra properties).
72 MATCHER_P(MatchesProperties,
73 value,
74 std::string(negation ? "does't match " : "matches ") +
75 ValueToString(value)) {
76 for (base::DictionaryValue::Iterator iter(*value); !iter.IsAtEnd();
77 iter.Advance()) {
78 const base::Value* property;
79 if (!arg.GetWithoutPathExpansion(iter.key(), &property) ||
80 !iter.value().Equals(property)) {
81 return false;
84 return true;
87 class ShillProfileTestClient {
88 public:
89 typedef ShillClientHelper::DictionaryValueCallbackWithoutStatus
90 DictionaryValueCallbackWithoutStatus;
91 typedef ShillClientHelper::ErrorCallback ErrorCallback;
93 void AddProfile(const std::string& profile_path,
94 const std::string& userhash) {
95 if (profile_entries_.HasKey(profile_path))
96 return;
98 base::DictionaryValue* profile = new base::DictionaryValue;
99 profile_entries_.SetWithoutPathExpansion(profile_path, profile);
100 profile_to_user_[profile_path] = userhash;
103 void AddEntry(const std::string& profile_path,
104 const std::string& entry_path,
105 const base::DictionaryValue& entry) {
106 base::DictionaryValue* entries = NULL;
107 profile_entries_.GetDictionaryWithoutPathExpansion(profile_path, &entries);
108 ASSERT_TRUE(entries);
110 base::DictionaryValue* new_entry = entry.DeepCopy();
111 new_entry->SetStringWithoutPathExpansion(shill::kProfileProperty,
112 profile_path);
113 entries->SetWithoutPathExpansion(entry_path, new_entry);
116 void GetProperties(const dbus::ObjectPath& profile_path,
117 const DictionaryValueCallbackWithoutStatus& callback,
118 const ErrorCallback& error_callback) {
119 base::DictionaryValue* entries = NULL;
120 profile_entries_.GetDictionaryWithoutPathExpansion(profile_path.value(),
121 &entries);
122 ASSERT_TRUE(entries);
124 scoped_ptr<base::DictionaryValue> result(new base::DictionaryValue);
125 base::ListValue* entry_paths = new base::ListValue;
126 result->SetWithoutPathExpansion(shill::kEntriesProperty, entry_paths);
127 for (base::DictionaryValue::Iterator it(*entries); !it.IsAtEnd();
128 it.Advance()) {
129 entry_paths->AppendString(it.key());
132 ASSERT_TRUE(ContainsKey(profile_to_user_, profile_path.value()));
133 const std::string& userhash = profile_to_user_[profile_path.value()];
134 result->SetStringWithoutPathExpansion(shill::kUserHashProperty, userhash);
136 base::MessageLoop::current()->PostTask(
137 FROM_HERE,
138 base::Bind(base::Bind(&DereferenceAndCall, callback),
139 base::Owned(result.release())));
142 void GetEntry(const dbus::ObjectPath& profile_path,
143 const std::string& entry_path,
144 const DictionaryValueCallbackWithoutStatus& callback,
145 const ErrorCallback& error_callback) {
146 base::DictionaryValue* entries = NULL;
147 profile_entries_.GetDictionaryWithoutPathExpansion(profile_path.value(),
148 &entries);
149 ASSERT_TRUE(entries);
151 base::DictionaryValue* entry = NULL;
152 entries->GetDictionaryWithoutPathExpansion(entry_path, &entry);
153 ASSERT_TRUE(entry);
154 base::MessageLoop::current()->PostTask(
155 FROM_HERE,
156 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::MessageLoop::current()->PostTask(
176 FROM_HERE,
177 base::Bind(callback,
178 DBUS_METHOD_CALL_SUCCESS,
179 base::ConstRef(service_properties_)));
182 protected:
183 base::DictionaryValue service_properties_;
186 class TestNetworkProfileHandler : public NetworkProfileHandler {
187 public:
188 TestNetworkProfileHandler() {
189 Init();
191 ~TestNetworkProfileHandler() override {}
193 void AddProfileForTest(const NetworkProfile& profile) {
194 AddProfile(profile);
197 private:
198 DISALLOW_COPY_AND_ASSIGN(TestNetworkProfileHandler);
201 class TestNetworkPolicyObserver : public NetworkPolicyObserver {
202 public:
203 TestNetworkPolicyObserver() : policies_applied_count_(0) {}
205 void PoliciesApplied(const std::string& userhash) override {
206 policies_applied_count_++;
209 int GetPoliciesAppliedCountAndReset() {
210 int count = policies_applied_count_;
211 policies_applied_count_ = 0;
212 return count;
215 private:
216 int policies_applied_count_;
218 DISALLOW_COPY_AND_ASSIGN(TestNetworkPolicyObserver);
221 } // namespace
223 class ManagedNetworkConfigurationHandlerTest : public testing::Test {
224 public:
225 ManagedNetworkConfigurationHandlerTest()
226 : mock_manager_client_(NULL),
227 mock_profile_client_(NULL),
228 mock_service_client_(NULL) {
231 ~ManagedNetworkConfigurationHandlerTest() override {}
233 void SetUp() override {
234 scoped_ptr<DBusThreadManagerSetter> dbus_setter =
235 DBusThreadManager::GetSetterForTesting();
236 mock_manager_client_ = new StrictMock<MockShillManagerClient>();
237 mock_profile_client_ = new StrictMock<MockShillProfileClient>();
238 mock_service_client_ = new StrictMock<MockShillServiceClient>();
239 dbus_setter->SetShillManagerClient(
240 scoped_ptr<ShillManagerClient>(mock_manager_client_).Pass());
241 dbus_setter->SetShillProfileClient(
242 scoped_ptr<ShillProfileClient>(mock_profile_client_).Pass());
243 dbus_setter->SetShillServiceClient(
244 scoped_ptr<ShillServiceClient>(mock_service_client_).Pass());
246 SetNetworkConfigurationHandlerExpectations();
248 ON_CALL(*mock_profile_client_, GetProperties(_,_,_))
249 .WillByDefault(Invoke(&profiles_stub_,
250 &ShillProfileTestClient::GetProperties));
252 ON_CALL(*mock_profile_client_, GetEntry(_,_,_,_))
253 .WillByDefault(Invoke(&profiles_stub_,
254 &ShillProfileTestClient::GetEntry));
256 ON_CALL(*mock_service_client_, GetProperties(_,_))
257 .WillByDefault(Invoke(&services_stub_,
258 &ShillServiceTestClient::GetProperties));
260 network_state_handler_.reset(NetworkStateHandler::InitializeForTest());
261 network_profile_handler_.reset(new TestNetworkProfileHandler());
262 network_configuration_handler_.reset(
263 NetworkConfigurationHandler::InitializeForTest(
264 network_state_handler_.get(),
265 NULL /* no NetworkDeviceHandler */));
266 managed_network_configuration_handler_.reset(
267 new ManagedNetworkConfigurationHandlerImpl());
268 managed_network_configuration_handler_->Init(
269 network_state_handler_.get(),
270 network_profile_handler_.get(),
271 network_configuration_handler_.get(),
272 NULL /* no DeviceHandler */);
273 managed_network_configuration_handler_->AddObserver(&policy_observer_);
275 message_loop_.RunUntilIdle();
278 void TearDown() override {
279 if (managed_network_configuration_handler_)
280 managed_network_configuration_handler_->RemoveObserver(&policy_observer_);
281 network_state_handler_.reset();
282 managed_network_configuration_handler_.reset();
283 network_configuration_handler_.reset();
284 network_profile_handler_.reset();
285 DBusThreadManager::Shutdown();
288 void VerifyAndClearExpectations() {
289 Mock::VerifyAndClearExpectations(mock_manager_client_);
290 Mock::VerifyAndClearExpectations(mock_profile_client_);
291 SetNetworkConfigurationHandlerExpectations();
294 void InitializeStandardProfiles() {
295 profiles_stub_.AddProfile(kUser1ProfilePath, kUser1);
296 network_profile_handler_->
297 AddProfileForTest(NetworkProfile(kUser1ProfilePath, kUser1));
299 profiles_stub_.AddProfile(NetworkProfileHandler::GetSharedProfilePath(),
300 std::string() /* no userhash */);
301 network_profile_handler_->AddProfileForTest(
302 NetworkProfile(NetworkProfileHandler::GetSharedProfilePath(),
303 std::string() /* no userhash */));
306 void SetUpEntry(const std::string& path_to_shill_json,
307 const std::string& profile_path,
308 const std::string& entry_path) {
309 scoped_ptr<base::DictionaryValue> entry =
310 test_utils::ReadTestDictionary(path_to_shill_json);
311 profiles_stub_.AddEntry(profile_path, entry_path, *entry);
314 void SetPolicy(::onc::ONCSource onc_source,
315 const std::string& userhash,
316 const std::string& path_to_onc) {
317 scoped_ptr<base::DictionaryValue> policy;
318 if (path_to_onc.empty())
319 policy = onc::ReadDictionaryFromJson(onc::kEmptyUnencryptedConfiguration);
320 else
321 policy = test_utils::ReadTestDictionary(path_to_onc);
323 base::ListValue empty_network_configs;
324 base::ListValue* network_configs = &empty_network_configs;
325 policy->GetListWithoutPathExpansion(
326 ::onc::toplevel_config::kNetworkConfigurations, &network_configs);
328 base::DictionaryValue empty_global_config;
329 base::DictionaryValue* global_network_config = &empty_global_config;
330 policy->GetDictionaryWithoutPathExpansion(
331 ::onc::toplevel_config::kGlobalNetworkConfiguration,
332 &global_network_config);
334 managed_handler()->SetPolicy(
335 onc_source, userhash, *network_configs, *global_network_config);
338 void SetNetworkConfigurationHandlerExpectations() {
339 // These calls occur in NetworkConfigurationHandler.
340 EXPECT_CALL(*mock_manager_client_, GetProperties(_)).Times(AnyNumber());
341 EXPECT_CALL(*mock_manager_client_,
342 AddPropertyChangedObserver(_)).Times(AnyNumber());
343 EXPECT_CALL(*mock_manager_client_,
344 RemovePropertyChangedObserver(_)).Times(AnyNumber());
347 ManagedNetworkConfigurationHandler* managed_handler() {
348 return managed_network_configuration_handler_.get();
351 void GetManagedProperties(const std::string& userhash,
352 const std::string& service_path) {
353 managed_handler()->GetManagedProperties(
354 userhash,
355 service_path,
356 base::Bind(
357 &ManagedNetworkConfigurationHandlerTest::GetPropertiesCallback,
358 base::Unretained(this)),
359 base::Bind(&ManagedNetworkConfigurationHandlerTest::UnexpectedError));
362 void GetPropertiesCallback(const std::string& service_path,
363 const base::DictionaryValue& dictionary) {
364 get_properties_service_path_ = service_path;
365 get_properties_result_.Clear();
366 get_properties_result_.MergeDictionary(&dictionary);
369 static void UnexpectedError(const std::string& error_name,
370 scoped_ptr<base::DictionaryValue> error_data) {
371 ASSERT_FALSE(true);
374 protected:
375 MockShillManagerClient* mock_manager_client_;
376 MockShillProfileClient* mock_profile_client_;
377 MockShillServiceClient* mock_service_client_;
378 ShillProfileTestClient profiles_stub_;
379 ShillServiceTestClient services_stub_;
380 TestNetworkPolicyObserver policy_observer_;
381 scoped_ptr<NetworkStateHandler> network_state_handler_;
382 scoped_ptr<TestNetworkProfileHandler> network_profile_handler_;
383 scoped_ptr<NetworkConfigurationHandler> network_configuration_handler_;
384 scoped_ptr<ManagedNetworkConfigurationHandlerImpl>
385 managed_network_configuration_handler_;
386 base::MessageLoop message_loop_;
388 std::string get_properties_service_path_;
389 base::DictionaryValue get_properties_result_;
391 private:
392 DISALLOW_COPY_AND_ASSIGN(ManagedNetworkConfigurationHandlerTest);
395 TEST_F(ManagedNetworkConfigurationHandlerTest, ProfileInitialization) {
396 InitializeStandardProfiles();
397 message_loop_.RunUntilIdle();
400 TEST_F(ManagedNetworkConfigurationHandlerTest, RemoveIrrelevantFields) {
401 InitializeStandardProfiles();
402 scoped_ptr<base::DictionaryValue> expected_shill_properties =
403 test_utils::ReadTestDictionary(
404 "policy/shill_policy_on_unconfigured_wifi1.json");
406 EXPECT_CALL(*mock_profile_client_,
407 GetProperties(dbus::ObjectPath(kUser1ProfilePath), _, _));
409 EXPECT_CALL(*mock_manager_client_,
410 ConfigureServiceForProfile(
411 dbus::ObjectPath(kUser1ProfilePath),
412 IsEqualTo(expected_shill_properties.get()),
413 _, _));
415 SetPolicy(::onc::ONC_SOURCE_USER_POLICY,
416 kUser1,
417 "policy/policy_wifi1_with_redundant_fields.onc");
418 message_loop_.RunUntilIdle();
421 TEST_F(ManagedNetworkConfigurationHandlerTest, SetPolicyManageUnconfigured) {
422 InitializeStandardProfiles();
423 scoped_ptr<base::DictionaryValue> expected_shill_properties =
424 test_utils::ReadTestDictionary(
425 "policy/shill_policy_on_unconfigured_wifi1.json");
427 EXPECT_CALL(*mock_profile_client_,
428 GetProperties(dbus::ObjectPath(kUser1ProfilePath), _, _));
430 EXPECT_CALL(*mock_manager_client_,
431 ConfigureServiceForProfile(
432 dbus::ObjectPath(kUser1ProfilePath),
433 IsEqualTo(expected_shill_properties.get()),
434 _, _));
436 SetPolicy(::onc::ONC_SOURCE_USER_POLICY, kUser1, "policy/policy_wifi1.onc");
437 message_loop_.RunUntilIdle();
440 TEST_F(ManagedNetworkConfigurationHandlerTest, EnableManagedCredentialsWiFi) {
441 InitializeStandardProfiles();
442 scoped_ptr<base::DictionaryValue> expected_shill_properties =
443 test_utils::ReadTestDictionary(
444 "policy/shill_policy_autoconnect_on_unconfigured_wifi1.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_wifi1_autoconnect.onc");
457 message_loop_.RunUntilIdle();
460 TEST_F(ManagedNetworkConfigurationHandlerTest, EnableManagedCredentialsVPN) {
461 InitializeStandardProfiles();
462 scoped_ptr<base::DictionaryValue> expected_shill_properties =
463 test_utils::ReadTestDictionary(
464 "policy/shill_policy_autoconnect_on_unconfigured_vpn.json");
466 EXPECT_CALL(*mock_profile_client_,
467 GetProperties(dbus::ObjectPath(kUser1ProfilePath), _, _));
469 EXPECT_CALL(*mock_manager_client_,
470 ConfigureServiceForProfile(
471 dbus::ObjectPath(kUser1ProfilePath),
472 IsEqualTo(expected_shill_properties.get()),
473 _, _));
475 SetPolicy(::onc::ONC_SOURCE_USER_POLICY, kUser1,
476 "policy/policy_vpn_autoconnect.onc");
477 message_loop_.RunUntilIdle();
480 // Ensure that EAP settings for ethernet are matched with the right profile
481 // entry and written to the dedicated EthernetEAP service.
482 TEST_F(ManagedNetworkConfigurationHandlerTest,
483 SetPolicyManageUnmanagedEthernetEAP) {
484 InitializeStandardProfiles();
485 scoped_ptr<base::DictionaryValue> expected_shill_properties =
486 test_utils::ReadTestDictionary(
487 "policy/"
488 "shill_policy_on_unmanaged_ethernet_eap.json");
490 SetUpEntry("policy/shill_unmanaged_ethernet_eap.json",
491 kUser1ProfilePath,
492 "eth_entry");
494 // Also setup an unrelated WiFi configuration to verify that the right entry
495 // is matched.
496 SetUpEntry("policy/shill_unmanaged_wifi1.json",
497 kUser1ProfilePath,
498 "wifi_entry");
500 EXPECT_CALL(*mock_profile_client_,
501 GetProperties(dbus::ObjectPath(kUser1ProfilePath), _, _));
503 EXPECT_CALL(*mock_profile_client_,
504 GetEntry(dbus::ObjectPath(kUser1ProfilePath), _, _, _)).Times(2);
506 EXPECT_CALL(
507 *mock_profile_client_,
508 DeleteEntry(dbus::ObjectPath(kUser1ProfilePath), "eth_entry", _, _));
510 EXPECT_CALL(
511 *mock_manager_client_,
512 ConfigureServiceForProfile(dbus::ObjectPath(kUser1ProfilePath),
513 IsEqualTo(expected_shill_properties.get()),
514 _, _));
516 SetPolicy(
517 ::onc::ONC_SOURCE_USER_POLICY, kUser1, "policy/policy_ethernet_eap.onc");
518 message_loop_.RunUntilIdle();
521 TEST_F(ManagedNetworkConfigurationHandlerTest, SetPolicyIgnoreUnmodified) {
522 InitializeStandardProfiles();
523 EXPECT_CALL(*mock_profile_client_, GetProperties(_, _, _));
525 EXPECT_CALL(*mock_manager_client_, ConfigureServiceForProfile(_, _, _, _));
527 SetPolicy(::onc::ONC_SOURCE_USER_POLICY, kUser1, "policy/policy_wifi1.onc");
528 message_loop_.RunUntilIdle();
529 EXPECT_EQ(1, policy_observer_.GetPoliciesAppliedCountAndReset());
530 VerifyAndClearExpectations();
532 SetUpEntry("policy/shill_policy_on_unmanaged_wifi1.json",
533 kUser1ProfilePath,
534 "some_entry_path");
536 EXPECT_CALL(*mock_profile_client_, GetProperties(_, _, _));
538 EXPECT_CALL(
539 *mock_profile_client_,
540 GetEntry(dbus::ObjectPath(kUser1ProfilePath), "some_entry_path", _, _));
542 SetPolicy(::onc::ONC_SOURCE_USER_POLICY, kUser1, "policy/policy_wifi1.onc");
544 message_loop_.RunUntilIdle();
545 EXPECT_EQ(1, policy_observer_.GetPoliciesAppliedCountAndReset());
548 TEST_F(ManagedNetworkConfigurationHandlerTest, PolicyApplicationRunning) {
549 InitializeStandardProfiles();
550 EXPECT_CALL(*mock_profile_client_, GetProperties(_, _, _)).Times(AnyNumber());
551 EXPECT_CALL(*mock_manager_client_, ConfigureServiceForProfile(_, _, _, _))
552 .Times(AnyNumber());
553 EXPECT_CALL(*mock_profile_client_, GetEntry(_, _, _, _)).Times(AnyNumber());
555 EXPECT_FALSE(managed_handler()->IsAnyPolicyApplicationRunning());
557 SetPolicy(::onc::ONC_SOURCE_USER_POLICY, kUser1, "policy/policy_wifi1.onc");
558 managed_handler()->SetPolicy(
559 ::onc::ONC_SOURCE_DEVICE_POLICY,
560 std::string(), // no userhash
561 base::ListValue(), // no device network policy
562 base::DictionaryValue()); // no device global config
564 EXPECT_TRUE(managed_handler()->IsAnyPolicyApplicationRunning());
565 message_loop_.RunUntilIdle();
566 EXPECT_FALSE(managed_handler()->IsAnyPolicyApplicationRunning());
568 SetUpEntry("policy/shill_policy_on_unmanaged_wifi1.json",
569 kUser1ProfilePath,
570 "some_entry_path");
572 SetPolicy(
573 ::onc::ONC_SOURCE_USER_POLICY, kUser1, "policy/policy_wifi1_update.onc");
574 EXPECT_TRUE(managed_handler()->IsAnyPolicyApplicationRunning());
575 message_loop_.RunUntilIdle();
576 EXPECT_FALSE(managed_handler()->IsAnyPolicyApplicationRunning());
579 TEST_F(ManagedNetworkConfigurationHandlerTest, UpdatePolicyAfterFinished) {
580 InitializeStandardProfiles();
581 EXPECT_CALL(*mock_profile_client_, GetProperties(_, _, _));
582 EXPECT_CALL(*mock_manager_client_, ConfigureServiceForProfile(_, _, _, _));
584 SetPolicy(::onc::ONC_SOURCE_USER_POLICY, kUser1, "policy/policy_wifi1.onc");
585 message_loop_.RunUntilIdle();
586 EXPECT_EQ(1, policy_observer_.GetPoliciesAppliedCountAndReset());
587 VerifyAndClearExpectations();
589 SetUpEntry("policy/shill_policy_on_unmanaged_wifi1.json",
590 kUser1ProfilePath,
591 "some_entry_path");
593 EXPECT_CALL(*mock_profile_client_, GetProperties(_, _, _));
594 EXPECT_CALL(
595 *mock_profile_client_,
596 GetEntry(dbus::ObjectPath(kUser1ProfilePath), "some_entry_path", _, _));
597 EXPECT_CALL(*mock_manager_client_, ConfigureServiceForProfile(_, _, _, _));
599 SetPolicy(
600 ::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, UpdatePolicyBeforeFinished) {
606 InitializeStandardProfiles();
607 EXPECT_CALL(*mock_profile_client_, GetProperties(_, _, _)).Times(2);
608 EXPECT_CALL(*mock_manager_client_, ConfigureServiceForProfile(_, _, _, _))
609 .Times(2);
611 SetPolicy(::onc::ONC_SOURCE_USER_POLICY, kUser1, "policy/policy_wifi1.onc");
612 // Usually the first call will cause a profile entry to be created, which we
613 // don't fake here.
614 SetPolicy(
615 ::onc::ONC_SOURCE_USER_POLICY, kUser1, "policy/policy_wifi1_update.onc");
617 message_loop_.RunUntilIdle();
618 EXPECT_EQ(1, policy_observer_.GetPoliciesAppliedCountAndReset());
621 TEST_F(ManagedNetworkConfigurationHandlerTest, SetPolicyManageUnmanaged) {
622 InitializeStandardProfiles();
623 SetUpEntry("policy/shill_unmanaged_wifi1.json",
624 kUser1ProfilePath,
625 "old_entry_path");
627 scoped_ptr<base::DictionaryValue> expected_shill_properties =
628 test_utils::ReadTestDictionary(
629 "policy/shill_policy_on_unmanaged_wifi1.json");
631 EXPECT_CALL(*mock_profile_client_,
632 GetProperties(dbus::ObjectPath(kUser1ProfilePath), _, _));
634 EXPECT_CALL(
635 *mock_profile_client_,
636 GetEntry(dbus::ObjectPath(kUser1ProfilePath), "old_entry_path", _, _));
638 EXPECT_CALL(
639 *mock_profile_client_,
640 DeleteEntry(dbus::ObjectPath(kUser1ProfilePath), "old_entry_path", _, _));
642 EXPECT_CALL(*mock_manager_client_,
643 ConfigureServiceForProfile(
644 dbus::ObjectPath(kUser1ProfilePath),
645 IsEqualTo(expected_shill_properties.get()),
646 _, _));
648 SetPolicy(::onc::ONC_SOURCE_USER_POLICY, kUser1, "policy/policy_wifi1.onc");
649 message_loop_.RunUntilIdle();
652 // Old ChromeOS versions may not have used the UIData property
653 TEST_F(ManagedNetworkConfigurationHandlerTest,
654 SetPolicyManageUnmanagedWithoutUIData) {
655 InitializeStandardProfiles();
656 SetUpEntry("policy/shill_unmanaged_wifi1.json",
657 kUser1ProfilePath,
658 "old_entry_path");
660 scoped_ptr<base::DictionaryValue> expected_shill_properties =
661 test_utils::ReadTestDictionary(
662 "policy/shill_policy_on_unmanaged_wifi1.json");
664 EXPECT_CALL(*mock_profile_client_,
665 GetProperties(dbus::ObjectPath(kUser1ProfilePath), _, _));
667 EXPECT_CALL(
668 *mock_profile_client_,
669 GetEntry(dbus::ObjectPath(kUser1ProfilePath), "old_entry_path", _, _));
671 EXPECT_CALL(
672 *mock_profile_client_,
673 DeleteEntry(dbus::ObjectPath(kUser1ProfilePath), "old_entry_path", _, _));
675 EXPECT_CALL(*mock_manager_client_,
676 ConfigureServiceForProfile(
677 dbus::ObjectPath(kUser1ProfilePath),
678 IsEqualTo(expected_shill_properties.get()),
679 _, _));
681 SetPolicy(::onc::ONC_SOURCE_USER_POLICY, kUser1, "policy/policy_wifi1.onc");
682 message_loop_.RunUntilIdle();
685 TEST_F(ManagedNetworkConfigurationHandlerTest, SetPolicyUpdateManagedNewGUID) {
686 InitializeStandardProfiles();
687 SetUpEntry("policy/shill_managed_wifi1.json",
688 kUser1ProfilePath,
689 "old_entry_path");
691 scoped_ptr<base::DictionaryValue> expected_shill_properties =
692 test_utils::ReadTestDictionary(
693 "policy/shill_policy_on_unmanaged_wifi1.json");
695 // The passphrase isn't sent again, because it's configured by the user and
696 // Shill doesn't send it on GetProperties calls.
697 expected_shill_properties->RemoveWithoutPathExpansion(
698 shill::kPassphraseProperty, NULL);
700 EXPECT_CALL(*mock_profile_client_,
701 GetProperties(dbus::ObjectPath(kUser1ProfilePath), _, _));
703 EXPECT_CALL(
704 *mock_profile_client_,
705 GetEntry(dbus::ObjectPath(kUser1ProfilePath), "old_entry_path", _, _));
707 EXPECT_CALL(
708 *mock_profile_client_,
709 DeleteEntry(dbus::ObjectPath(kUser1ProfilePath), "old_entry_path", _, _));
711 EXPECT_CALL(*mock_manager_client_,
712 ConfigureServiceForProfile(
713 dbus::ObjectPath(kUser1ProfilePath),
714 IsEqualTo(expected_shill_properties.get()),
715 _, _));
717 SetPolicy(::onc::ONC_SOURCE_USER_POLICY, kUser1, "policy/policy_wifi1.onc");
718 message_loop_.RunUntilIdle();
721 TEST_F(ManagedNetworkConfigurationHandlerTest, SetPolicyUpdateManagedVPN) {
722 InitializeStandardProfiles();
723 SetUpEntry("policy/shill_managed_vpn.json", kUser1ProfilePath, "entry_path");
725 scoped_ptr<base::DictionaryValue> expected_shill_properties =
726 test_utils::ReadTestDictionary(
727 "policy/shill_policy_on_managed_vpn.json");
729 EXPECT_CALL(*mock_profile_client_,
730 GetProperties(dbus::ObjectPath(kUser1ProfilePath), _, _));
732 EXPECT_CALL(
733 *mock_profile_client_,
734 GetEntry(dbus::ObjectPath(kUser1ProfilePath), "entry_path", _, _));
736 EXPECT_CALL(*mock_manager_client_,
737 ConfigureServiceForProfile(
738 dbus::ObjectPath(kUser1ProfilePath),
739 IsEqualTo(expected_shill_properties.get()),
740 _, _));
742 SetPolicy(::onc::ONC_SOURCE_USER_POLICY, kUser1, "policy/policy_vpn.onc");
743 message_loop_.RunUntilIdle();
744 VerifyAndClearExpectations();
747 TEST_F(ManagedNetworkConfigurationHandlerTest, SetPolicyReapplyToManaged) {
748 InitializeStandardProfiles();
749 SetUpEntry("policy/shill_policy_on_unmanaged_wifi1.json",
750 kUser1ProfilePath,
751 "old_entry_path");
753 scoped_ptr<base::DictionaryValue> expected_shill_properties =
754 test_utils::ReadTestDictionary(
755 "policy/shill_policy_on_unmanaged_wifi1.json");
757 // The passphrase isn't sent again, because it's configured by the user and
758 // Shill doesn't send it on GetProperties calls.
759 expected_shill_properties->RemoveWithoutPathExpansion(
760 shill::kPassphraseProperty, NULL);
762 EXPECT_CALL(*mock_profile_client_,
763 GetProperties(dbus::ObjectPath(kUser1ProfilePath), _, _));
765 EXPECT_CALL(
766 *mock_profile_client_,
767 GetEntry(dbus::ObjectPath(kUser1ProfilePath), "old_entry_path", _, _));
769 EXPECT_CALL(*mock_manager_client_,
770 ConfigureServiceForProfile(
771 dbus::ObjectPath(kUser1ProfilePath),
772 IsEqualTo(expected_shill_properties.get()),
773 _, _));
775 SetPolicy(::onc::ONC_SOURCE_USER_POLICY, kUser1, "policy/policy_wifi1.onc");
776 message_loop_.RunUntilIdle();
777 VerifyAndClearExpectations();
779 // If we apply the policy again, without change, then the Shill profile will
780 // not be modified.
781 EXPECT_CALL(*mock_profile_client_,
782 GetProperties(dbus::ObjectPath(kUser1ProfilePath), _, _));
784 EXPECT_CALL(
785 *mock_profile_client_,
786 GetEntry(dbus::ObjectPath(kUser1ProfilePath), "old_entry_path", _, _));
788 SetPolicy(::onc::ONC_SOURCE_USER_POLICY, kUser1, "policy/policy_wifi1.onc");
789 message_loop_.RunUntilIdle();
792 TEST_F(ManagedNetworkConfigurationHandlerTest, SetPolicyUnmanageManaged) {
793 InitializeStandardProfiles();
794 SetUpEntry("policy/shill_policy_on_unmanaged_wifi1.json",
795 kUser1ProfilePath,
796 "old_entry_path");
798 EXPECT_CALL(*mock_profile_client_,
799 GetProperties(dbus::ObjectPath(kUser1ProfilePath), _, _));
801 EXPECT_CALL(*mock_profile_client_,
802 GetEntry(dbus::ObjectPath(kUser1ProfilePath),
803 "old_entry_path",
804 _, _));
806 EXPECT_CALL(*mock_profile_client_,
807 DeleteEntry(dbus::ObjectPath(kUser1ProfilePath),
808 "old_entry_path",
809 _, _));
811 SetPolicy(::onc::ONC_SOURCE_USER_POLICY, kUser1, "");
812 message_loop_.RunUntilIdle();
815 TEST_F(ManagedNetworkConfigurationHandlerTest, SetEmptyPolicyIgnoreUnmanaged) {
816 InitializeStandardProfiles();
817 SetUpEntry("policy/shill_unmanaged_wifi1.json",
818 kUser1ProfilePath,
819 "old_entry_path");
821 EXPECT_CALL(*mock_profile_client_,
822 GetProperties(dbus::ObjectPath(kUser1ProfilePath), _, _));
824 EXPECT_CALL(*mock_profile_client_,
825 GetEntry(dbus::ObjectPath(kUser1ProfilePath),
826 "old_entry_path",
827 _, _));
829 SetPolicy(::onc::ONC_SOURCE_USER_POLICY, kUser1, "");
830 message_loop_.RunUntilIdle();
831 EXPECT_EQ(1, policy_observer_.GetPoliciesAppliedCountAndReset());
834 TEST_F(ManagedNetworkConfigurationHandlerTest, SetPolicyIgnoreUnmanaged) {
835 InitializeStandardProfiles();
836 SetUpEntry("policy/shill_unmanaged_wifi2.json",
837 kUser1ProfilePath,
838 "wifi2_entry_path");
840 EXPECT_CALL(*mock_profile_client_,
841 GetProperties(dbus::ObjectPath(kUser1ProfilePath), _, _));
843 EXPECT_CALL(
844 *mock_profile_client_,
845 GetEntry(dbus::ObjectPath(kUser1ProfilePath), "wifi2_entry_path", _, _));
847 scoped_ptr<base::DictionaryValue> expected_shill_properties =
848 test_utils::ReadTestDictionary(
849 "policy/shill_policy_on_unconfigured_wifi1.json");
851 EXPECT_CALL(*mock_manager_client_,
852 ConfigureServiceForProfile(
853 dbus::ObjectPath(kUser1ProfilePath),
854 IsEqualTo(expected_shill_properties.get()),
855 _, _));
857 SetPolicy(::onc::ONC_SOURCE_USER_POLICY, kUser1, "policy/policy_wifi1.onc");
858 message_loop_.RunUntilIdle();
861 TEST_F(ManagedNetworkConfigurationHandlerTest, AutoConnectDisallowed) {
862 InitializeStandardProfiles();
863 // Setup an unmanaged network.
864 SetUpEntry("policy/shill_unmanaged_wifi2.json",
865 kUser1ProfilePath,
866 "wifi2_entry_path");
868 // Apply the user policy with global autoconnect config and expect that
869 // autoconnect is disabled in the network's profile entry.
870 EXPECT_CALL(*mock_profile_client_,
871 GetProperties(dbus::ObjectPath(kUser1ProfilePath), _, _));
873 EXPECT_CALL(
874 *mock_profile_client_,
875 GetEntry(dbus::ObjectPath(kUser1ProfilePath), "wifi2_entry_path", _, _));
877 scoped_ptr<base::DictionaryValue> expected_shill_properties =
878 test_utils::ReadTestDictionary(
879 "policy/shill_disallow_autoconnect_on_unmanaged_wifi2.json");
881 EXPECT_CALL(*mock_manager_client_,
882 ConfigureServiceForProfile(
883 dbus::ObjectPath(kUser1ProfilePath),
884 MatchesProperties(expected_shill_properties.get()), _, _));
886 SetPolicy(::onc::ONC_SOURCE_USER_POLICY,
887 kUser1,
888 "policy/policy_disallow_autoconnect.onc");
889 message_loop_.RunUntilIdle();
891 // Verify that GetManagedProperties correctly augments the properties with the
892 // global config from the user policy.
894 // GetManagedProperties requires the device policy to be set or explicitly
895 // unset.
896 EXPECT_CALL(*mock_profile_client_,
897 GetProperties(dbus::ObjectPath(
898 NetworkProfileHandler::GetSharedProfilePath()),
900 _));
901 managed_handler()->SetPolicy(
902 ::onc::ONC_SOURCE_DEVICE_POLICY,
903 std::string(), // no userhash
904 base::ListValue(), // no device network policy
905 base::DictionaryValue()); // no device global config
907 services_stub_.SetFakeProperties(*expected_shill_properties);
908 EXPECT_CALL(*mock_service_client_,
909 GetProperties(dbus::ObjectPath(
910 "wifi2"),_));
912 GetManagedProperties(kUser1, "wifi2");
913 message_loop_.RunUntilIdle();
915 EXPECT_EQ("wifi2", get_properties_service_path_);
917 scoped_ptr<base::DictionaryValue> expected_managed_onc =
918 test_utils::ReadTestDictionary(
919 "policy/managed_onc_disallow_autoconnect_on_unmanaged_wifi2.onc");
920 EXPECT_TRUE(onc::test_utils::Equals(expected_managed_onc.get(),
921 &get_properties_result_));
924 TEST_F(ManagedNetworkConfigurationHandlerTest, LateProfileLoading) {
925 SetPolicy(::onc::ONC_SOURCE_USER_POLICY, kUser1, "policy/policy_wifi1.onc");
927 message_loop_.RunUntilIdle();
928 VerifyAndClearExpectations();
930 scoped_ptr<base::DictionaryValue> expected_shill_properties =
931 test_utils::ReadTestDictionary(
932 "policy/shill_policy_on_unconfigured_wifi1.json");
934 EXPECT_CALL(*mock_profile_client_,
935 GetProperties(dbus::ObjectPath(kUser1ProfilePath), _, _));
937 EXPECT_CALL(*mock_manager_client_,
938 ConfigureServiceForProfile(
939 dbus::ObjectPath(kUser1ProfilePath),
940 IsEqualTo(expected_shill_properties.get()),
941 _, _));
943 InitializeStandardProfiles();
944 message_loop_.RunUntilIdle();
947 class ManagedNetworkConfigurationHandlerShutdownTest
948 : public ManagedNetworkConfigurationHandlerTest {
949 public:
950 void SetUp() override {
951 ManagedNetworkConfigurationHandlerTest::SetUp();
952 ON_CALL(*mock_profile_client_, GetProperties(_, _, _)).WillByDefault(
953 Invoke(&ManagedNetworkConfigurationHandlerShutdownTest::GetProperties));
956 static void GetProperties(
957 const dbus::ObjectPath& profile_path,
958 const ShillClientHelper::DictionaryValueCallbackWithoutStatus& callback,
959 const ShillClientHelper::ErrorCallback& error_callback) {
960 base::MessageLoop::current()->PostTask(
961 FROM_HERE,
962 base::Bind(ManagedNetworkConfigurationHandlerShutdownTest::
963 CallbackWithEmptyDictionary,
964 callback));
967 static void CallbackWithEmptyDictionary(
968 const ShillClientHelper::DictionaryValueCallbackWithoutStatus& callback) {
969 callback.Run(base::DictionaryValue());
973 TEST_F(ManagedNetworkConfigurationHandlerShutdownTest,
974 DuringPolicyApplication) {
975 InitializeStandardProfiles();
977 EXPECT_CALL(*mock_profile_client_,
978 GetProperties(dbus::ObjectPath(kUser1ProfilePath), _, _));
980 SetPolicy(::onc::ONC_SOURCE_USER_POLICY, kUser1, "policy/policy_wifi1.onc");
981 managed_network_configuration_handler_->RemoveObserver(&policy_observer_);
982 managed_network_configuration_handler_.reset();
983 message_loop_.RunUntilIdle();
986 } // namespace chromeos