managed_network_configuration_handler_unittest.cc revision cedac228d2dd51db4b79ea1e72c7f249408ee061
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.
4
5#include <iostream>
6#include <sstream>
7
8#include "base/bind.h"
9#include "base/location.h"
10#include "base/memory/scoped_ptr.h"
11#include "base/message_loop/message_loop.h"
12#include "base/stl_util.h"
13#include "base/values.h"
14#include "chromeos/dbus/dbus_thread_manager.h"
15#include "chromeos/dbus/fake_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/shill_client_helper.h"
19#include "chromeos/network/managed_network_configuration_handler_impl.h"
20#include "chromeos/network/network_configuration_handler.h"
21#include "chromeos/network/network_profile_handler.h"
22#include "chromeos/network/onc/onc_test_utils.h"
23#include "chromeos/network/onc/onc_utils.h"
24#include "dbus/object_path.h"
25#include "testing/gmock/include/gmock/gmock.h"
26#include "testing/gtest/include/gtest/gtest.h"
27#include "third_party/cros_system_api/dbus/service_constants.h"
28
29using ::testing::AnyNumber;
30using ::testing::Invoke;
31using ::testing::Mock;
32using ::testing::Pointee;
33using ::testing::Return;
34using ::testing::SaveArg;
35using ::testing::StrEq;
36using ::testing::StrictMock;
37using ::testing::_;
38
39namespace test_utils = ::chromeos::onc::test_utils;
40
41namespace chromeos {
42
43namespace {
44
45std::string ValueToString(const base::Value* value) {
46  std::stringstream str;
47  str << *value;
48  return str.str();
49}
50
51const char kUser1[] = "user1";
52const char kUser1ProfilePath[] = "/profile/user1/shill";
53
54// Matcher to match base::Value.
55MATCHER_P(IsEqualTo,
56          value,
57          std::string(negation ? "isn't" : "is") + " equal to " +
58          ValueToString(value)) {
59  return value->Equals(&arg);
60}
61
62class ShillProfileTestClient {
63 public:
64  typedef ShillClientHelper::DictionaryValueCallbackWithoutStatus
65      DictionaryValueCallbackWithoutStatus;
66  typedef ShillClientHelper::ErrorCallback ErrorCallback;
67
68  void AddProfile(const std::string& profile_path,
69                  const std::string& userhash) {
70    if (profile_entries_.HasKey(profile_path))
71      return;
72
73    base::DictionaryValue* profile = new base::DictionaryValue;
74    profile_entries_.SetWithoutPathExpansion(profile_path, profile);
75    profile_to_user_[profile_path] = userhash;
76  }
77
78  void AddEntry(const std::string& profile_path,
79                const std::string& entry_path,
80                const base::DictionaryValue& entry) {
81    base::DictionaryValue* entries = NULL;
82    profile_entries_.GetDictionaryWithoutPathExpansion(profile_path, &entries);
83    ASSERT_TRUE(entries);
84
85    base::DictionaryValue* new_entry = entry.DeepCopy();
86    new_entry->SetStringWithoutPathExpansion(shill::kProfileProperty,
87                                             profile_path);
88    entries->SetWithoutPathExpansion(entry_path, new_entry);
89  }
90
91  void GetProperties(const dbus::ObjectPath& profile_path,
92                     const DictionaryValueCallbackWithoutStatus& callback,
93                     const ErrorCallback& error_callback) {
94    base::DictionaryValue* entries = NULL;
95    profile_entries_.GetDictionaryWithoutPathExpansion(profile_path.value(),
96                                                       &entries);
97    ASSERT_TRUE(entries);
98
99    scoped_ptr<base::DictionaryValue> result(new base::DictionaryValue);
100    base::ListValue* entry_paths = new base::ListValue;
101    result->SetWithoutPathExpansion(shill::kEntriesProperty, entry_paths);
102    for (base::DictionaryValue::Iterator it(*entries); !it.IsAtEnd();
103         it.Advance()) {
104      entry_paths->AppendString(it.key());
105    }
106
107    ASSERT_TRUE(ContainsKey(profile_to_user_, profile_path.value()));
108    const std::string& userhash = profile_to_user_[profile_path.value()];
109    result->SetStringWithoutPathExpansion(shill::kUserHashProperty, userhash);
110
111    callback.Run(*result);
112  }
113
114  void GetEntry(const dbus::ObjectPath& profile_path,
115                const std::string& entry_path,
116                const DictionaryValueCallbackWithoutStatus& callback,
117                const ErrorCallback& error_callback) {
118    base::DictionaryValue* entries = NULL;
119    profile_entries_.GetDictionaryWithoutPathExpansion(profile_path.value(),
120                                                       &entries);
121    ASSERT_TRUE(entries);
122
123    base::DictionaryValue* entry = NULL;
124    entries->GetDictionaryWithoutPathExpansion(entry_path, &entry);
125    ASSERT_TRUE(entry);
126    callback.Run(*entry);
127  }
128
129 protected:
130  base::DictionaryValue profile_entries_;
131  std::map<std::string, std::string> profile_to_user_;
132};
133
134class TestNetworkProfileHandler : public NetworkProfileHandler {
135 public:
136  TestNetworkProfileHandler() {
137    Init(NULL /* No NetworkStateHandler */);
138  }
139  virtual ~TestNetworkProfileHandler() {}
140
141  void AddProfileForTest(const NetworkProfile& profile) {
142    AddProfile(profile);
143  }
144
145 private:
146  DISALLOW_COPY_AND_ASSIGN(TestNetworkProfileHandler);
147};
148
149}  // namespace
150
151class ManagedNetworkConfigurationHandlerTest : public testing::Test {
152 public:
153  ManagedNetworkConfigurationHandlerTest()
154      : mock_manager_client_(NULL),
155        mock_profile_client_(NULL) {
156  }
157
158  virtual ~ManagedNetworkConfigurationHandlerTest() {
159  }
160
161  virtual void SetUp() OVERRIDE {
162    FakeDBusThreadManager* dbus_thread_manager = new FakeDBusThreadManager;
163    mock_manager_client_ = new StrictMock<MockShillManagerClient>();
164    mock_profile_client_ = new StrictMock<MockShillProfileClient>();
165    dbus_thread_manager->SetShillManagerClient(
166        scoped_ptr<ShillManagerClient>(mock_manager_client_).Pass());
167    dbus_thread_manager->SetShillProfileClient(
168        scoped_ptr<ShillProfileClient>(mock_profile_client_).Pass());
169
170    DBusThreadManager::InitializeForTesting(dbus_thread_manager);
171
172    SetNetworkConfigurationHandlerExpectations();
173
174    ON_CALL(*mock_profile_client_, GetProperties(_,_,_))
175        .WillByDefault(Invoke(&profiles_stub_,
176                              &ShillProfileTestClient::GetProperties));
177
178    ON_CALL(*mock_profile_client_, GetEntry(_,_,_,_))
179        .WillByDefault(Invoke(&profiles_stub_,
180                              &ShillProfileTestClient::GetEntry));
181
182    network_profile_handler_.reset(new TestNetworkProfileHandler());
183    network_configuration_handler_.reset(
184        NetworkConfigurationHandler::InitializeForTest(
185            NULL /* no NetworkStateHandler */));
186    managed_network_configuration_handler_.reset(
187        new ManagedNetworkConfigurationHandlerImpl());
188    managed_network_configuration_handler_->Init(
189        NULL /* no NetworkStateHandler */,
190        network_profile_handler_.get(),
191        network_configuration_handler_.get(),
192        NULL /* no DeviceHandler */);
193
194    message_loop_.RunUntilIdle();
195  }
196
197  virtual void TearDown() OVERRIDE {
198    managed_network_configuration_handler_.reset();
199    network_configuration_handler_.reset();
200    network_profile_handler_.reset();
201    DBusThreadManager::Shutdown();
202  }
203
204  void VerifyAndClearExpectations() {
205    Mock::VerifyAndClearExpectations(mock_manager_client_);
206    Mock::VerifyAndClearExpectations(mock_profile_client_);
207    SetNetworkConfigurationHandlerExpectations();
208  }
209
210  void InitializeStandardProfiles() {
211    profiles_stub_.AddProfile(kUser1ProfilePath, kUser1);
212    network_profile_handler_->
213        AddProfileForTest(NetworkProfile(kUser1ProfilePath, kUser1));
214    network_profile_handler_->
215        AddProfileForTest(NetworkProfile(
216            NetworkProfileHandler::GetSharedProfilePath(), std::string()));
217  }
218
219  void SetUpEntry(const std::string& path_to_shill_json,
220                  const std::string& profile_path,
221                  const std::string& entry_path) {
222    scoped_ptr<base::DictionaryValue> entry =
223        test_utils::ReadTestDictionary(path_to_shill_json);
224    profiles_stub_.AddEntry(profile_path, entry_path, *entry);
225  }
226
227  void SetPolicy(::onc::ONCSource onc_source,
228                 const std::string& userhash,
229                 const std::string& path_to_onc) {
230    scoped_ptr<base::DictionaryValue> policy;
231    if (path_to_onc.empty())
232      policy = onc::ReadDictionaryFromJson(onc::kEmptyUnencryptedConfiguration);
233    else
234      policy = test_utils::ReadTestDictionary(path_to_onc);
235
236    base::ListValue empty_network_configs;
237    base::ListValue* network_configs = &empty_network_configs;
238    policy->GetListWithoutPathExpansion(
239        ::onc::toplevel_config::kNetworkConfigurations, &network_configs);
240
241    base::DictionaryValue empty_global_config;
242    base::DictionaryValue* global_network_config = &empty_global_config;
243    policy->GetDictionaryWithoutPathExpansion(
244        ::onc::toplevel_config::kGlobalNetworkConfiguration,
245        &global_network_config);
246
247    managed_handler()->SetPolicy(
248        onc_source, userhash, *network_configs, *global_network_config);
249  }
250
251  void SetNetworkConfigurationHandlerExpectations() {
252    // These calls occur in NetworkConfigurationHandler.
253    EXPECT_CALL(*mock_manager_client_, GetProperties(_)).Times(AnyNumber());
254    EXPECT_CALL(*mock_manager_client_,
255                AddPropertyChangedObserver(_)).Times(AnyNumber());
256    EXPECT_CALL(*mock_manager_client_,
257                RemovePropertyChangedObserver(_)).Times(AnyNumber());
258  }
259
260  ManagedNetworkConfigurationHandler* managed_handler() {
261    return managed_network_configuration_handler_.get();
262  }
263
264 protected:
265  MockShillManagerClient* mock_manager_client_;
266  MockShillProfileClient* mock_profile_client_;
267  ShillProfileTestClient profiles_stub_;
268  scoped_ptr<TestNetworkProfileHandler> network_profile_handler_;
269  scoped_ptr<NetworkConfigurationHandler> network_configuration_handler_;
270  scoped_ptr<ManagedNetworkConfigurationHandlerImpl>
271        managed_network_configuration_handler_;
272  base::MessageLoop message_loop_;
273
274 private:
275  DISALLOW_COPY_AND_ASSIGN(ManagedNetworkConfigurationHandlerTest);
276};
277
278TEST_F(ManagedNetworkConfigurationHandlerTest, ProfileInitialization) {
279  InitializeStandardProfiles();
280  message_loop_.RunUntilIdle();
281}
282
283TEST_F(ManagedNetworkConfigurationHandlerTest, RemoveIrrelevantFields) {
284  InitializeStandardProfiles();
285  scoped_ptr<base::DictionaryValue> expected_shill_properties =
286      test_utils::ReadTestDictionary(
287          "policy/shill_policy_on_unconfigured_wifi1.json");
288
289  EXPECT_CALL(*mock_profile_client_,
290              GetProperties(dbus::ObjectPath(kUser1ProfilePath), _, _));
291
292  EXPECT_CALL(*mock_manager_client_,
293              ConfigureServiceForProfile(
294                  dbus::ObjectPath(kUser1ProfilePath),
295                  IsEqualTo(expected_shill_properties.get()),
296                  _, _));
297
298  SetPolicy(::onc::ONC_SOURCE_USER_POLICY,
299            kUser1,
300            "policy/policy_wifi1_with_redundant_fields.onc");
301  message_loop_.RunUntilIdle();
302}
303
304TEST_F(ManagedNetworkConfigurationHandlerTest, SetPolicyManageUnconfigured) {
305  InitializeStandardProfiles();
306  scoped_ptr<base::DictionaryValue> expected_shill_properties =
307      test_utils::ReadTestDictionary(
308          "policy/shill_policy_on_unconfigured_wifi1.json");
309
310  EXPECT_CALL(*mock_profile_client_,
311              GetProperties(dbus::ObjectPath(kUser1ProfilePath), _, _));
312
313  EXPECT_CALL(*mock_manager_client_,
314              ConfigureServiceForProfile(
315                  dbus::ObjectPath(kUser1ProfilePath),
316                  IsEqualTo(expected_shill_properties.get()),
317                  _, _));
318
319  SetPolicy(::onc::ONC_SOURCE_USER_POLICY, kUser1, "policy/policy_wifi1.onc");
320  message_loop_.RunUntilIdle();
321}
322
323// Ensure that EAP settings for ethernet are matched with the right profile
324// entry and written to the dedicated EthernetEAP service.
325TEST_F(ManagedNetworkConfigurationHandlerTest,
326       SetPolicyManageUnmanagedEthernetEAP) {
327  InitializeStandardProfiles();
328  scoped_ptr<base::DictionaryValue> expected_shill_properties =
329      test_utils::ReadTestDictionary(
330          "policy/"
331          "shill_policy_on_unmanaged_ethernet_eap.json");
332
333  SetUpEntry("policy/shill_unmanaged_ethernet_eap.json",
334             kUser1ProfilePath,
335             "eth_entry");
336
337  // Also setup an unrelated WiFi configuration to verify that the right entry
338  // is matched.
339  SetUpEntry("policy/shill_unmanaged_wifi1.json",
340             kUser1ProfilePath,
341             "wifi_entry");
342
343  EXPECT_CALL(*mock_profile_client_,
344              GetProperties(dbus::ObjectPath(kUser1ProfilePath), _, _));
345
346  EXPECT_CALL(*mock_profile_client_,
347              GetEntry(dbus::ObjectPath(kUser1ProfilePath), _, _, _)).Times(2);
348
349  EXPECT_CALL(
350      *mock_profile_client_,
351      DeleteEntry(dbus::ObjectPath(kUser1ProfilePath), "eth_entry", _, _));
352
353  EXPECT_CALL(
354      *mock_manager_client_,
355      ConfigureServiceForProfile(dbus::ObjectPath(kUser1ProfilePath),
356                                 IsEqualTo(expected_shill_properties.get()),
357                                 _, _));
358
359  SetPolicy(
360      ::onc::ONC_SOURCE_USER_POLICY, kUser1, "policy/policy_ethernet_eap.onc");
361  message_loop_.RunUntilIdle();
362}
363
364TEST_F(ManagedNetworkConfigurationHandlerTest, SetPolicyIgnoreUnmodified) {
365  InitializeStandardProfiles();
366  EXPECT_CALL(*mock_profile_client_, GetProperties(_, _, _));
367
368  EXPECT_CALL(*mock_manager_client_, ConfigureServiceForProfile(_, _, _, _));
369
370  SetPolicy(::onc::ONC_SOURCE_USER_POLICY, kUser1, "policy/policy_wifi1.onc");
371  message_loop_.RunUntilIdle();
372  VerifyAndClearExpectations();
373
374  SetUpEntry("policy/shill_policy_on_unmanaged_wifi1.json",
375             kUser1ProfilePath,
376             "some_entry_path");
377
378  EXPECT_CALL(*mock_profile_client_, GetProperties(_, _, _));
379
380  EXPECT_CALL(
381      *mock_profile_client_,
382      GetEntry(dbus::ObjectPath(kUser1ProfilePath), "some_entry_path", _, _));
383
384  SetPolicy(::onc::ONC_SOURCE_USER_POLICY, kUser1, "policy/policy_wifi1.onc");
385  message_loop_.RunUntilIdle();
386}
387
388TEST_F(ManagedNetworkConfigurationHandlerTest, SetPolicyManageUnmanaged) {
389  InitializeStandardProfiles();
390  SetUpEntry("policy/shill_unmanaged_wifi1.json",
391             kUser1ProfilePath,
392             "old_entry_path");
393
394  scoped_ptr<base::DictionaryValue> expected_shill_properties =
395      test_utils::ReadTestDictionary(
396          "policy/shill_policy_on_unmanaged_wifi1.json");
397
398  EXPECT_CALL(*mock_profile_client_,
399              GetProperties(dbus::ObjectPath(kUser1ProfilePath), _, _));
400
401  EXPECT_CALL(
402      *mock_profile_client_,
403      GetEntry(dbus::ObjectPath(kUser1ProfilePath), "old_entry_path", _, _));
404
405  EXPECT_CALL(
406      *mock_profile_client_,
407      DeleteEntry(dbus::ObjectPath(kUser1ProfilePath), "old_entry_path", _, _));
408
409  EXPECT_CALL(*mock_manager_client_,
410              ConfigureServiceForProfile(
411                  dbus::ObjectPath(kUser1ProfilePath),
412                  IsEqualTo(expected_shill_properties.get()),
413                  _, _));
414
415  SetPolicy(::onc::ONC_SOURCE_USER_POLICY, kUser1, "policy/policy_wifi1.onc");
416  message_loop_.RunUntilIdle();
417}
418
419// Old ChromeOS versions may not have used the UIData property
420TEST_F(ManagedNetworkConfigurationHandlerTest,
421       SetPolicyManageUnmanagedWithoutUIData) {
422  InitializeStandardProfiles();
423  SetUpEntry("policy/shill_unmanaged_wifi1.json",
424             kUser1ProfilePath,
425             "old_entry_path");
426
427  scoped_ptr<base::DictionaryValue> expected_shill_properties =
428      test_utils::ReadTestDictionary(
429          "policy/shill_policy_on_unmanaged_wifi1.json");
430
431  EXPECT_CALL(*mock_profile_client_,
432              GetProperties(dbus::ObjectPath(kUser1ProfilePath), _, _));
433
434  EXPECT_CALL(
435      *mock_profile_client_,
436      GetEntry(dbus::ObjectPath(kUser1ProfilePath), "old_entry_path", _, _));
437
438  EXPECT_CALL(
439      *mock_profile_client_,
440      DeleteEntry(dbus::ObjectPath(kUser1ProfilePath), "old_entry_path", _, _));
441
442  EXPECT_CALL(*mock_manager_client_,
443              ConfigureServiceForProfile(
444                  dbus::ObjectPath(kUser1ProfilePath),
445                  IsEqualTo(expected_shill_properties.get()),
446                  _, _));
447
448  SetPolicy(::onc::ONC_SOURCE_USER_POLICY, kUser1, "policy/policy_wifi1.onc");
449  message_loop_.RunUntilIdle();
450}
451
452TEST_F(ManagedNetworkConfigurationHandlerTest, SetPolicyUpdateManagedNewGUID) {
453  InitializeStandardProfiles();
454  SetUpEntry("policy/shill_managed_wifi1.json",
455             kUser1ProfilePath,
456             "old_entry_path");
457
458  scoped_ptr<base::DictionaryValue> expected_shill_properties =
459      test_utils::ReadTestDictionary(
460          "policy/shill_policy_on_unmanaged_wifi1.json");
461
462  // The passphrase isn't sent again, because it's configured by the user and
463  // Shill doesn't send it on GetProperties calls.
464  expected_shill_properties->RemoveWithoutPathExpansion(
465      shill::kPassphraseProperty, NULL);
466
467  EXPECT_CALL(*mock_profile_client_,
468              GetProperties(dbus::ObjectPath(kUser1ProfilePath), _, _));
469
470  EXPECT_CALL(
471      *mock_profile_client_,
472      GetEntry(dbus::ObjectPath(kUser1ProfilePath), "old_entry_path", _, _));
473
474  EXPECT_CALL(
475      *mock_profile_client_,
476      DeleteEntry(dbus::ObjectPath(kUser1ProfilePath), "old_entry_path", _, _));
477
478  EXPECT_CALL(*mock_manager_client_,
479              ConfigureServiceForProfile(
480                  dbus::ObjectPath(kUser1ProfilePath),
481                  IsEqualTo(expected_shill_properties.get()),
482                  _, _));
483
484  SetPolicy(::onc::ONC_SOURCE_USER_POLICY, kUser1, "policy/policy_wifi1.onc");
485  message_loop_.RunUntilIdle();
486}
487
488TEST_F(ManagedNetworkConfigurationHandlerTest,
489       SetPolicyUpdateManagedEquivalentSecurity) {
490  InitializeStandardProfiles();
491  SetUpEntry("policy/shill_managed_wifi1_rsn.json",
492             kUser1ProfilePath,
493             "old_entry_path");
494
495  scoped_ptr<base::DictionaryValue> expected_shill_properties =
496      test_utils::ReadTestDictionary(
497          "policy/shill_policy_on_unmanaged_wifi1.json");
498
499  // The passphrase isn't sent again, because it's configured by the user and
500  // Shill doesn't send it on GetProperties calls.
501  expected_shill_properties->RemoveWithoutPathExpansion(
502      shill::kPassphraseProperty, NULL);
503
504  EXPECT_CALL(*mock_profile_client_,
505              GetProperties(dbus::ObjectPath(kUser1ProfilePath), _, _));
506
507  EXPECT_CALL(
508      *mock_profile_client_,
509      GetEntry(dbus::ObjectPath(kUser1ProfilePath), "old_entry_path", _, _));
510
511  // The existing entry must not be deleted because the Security type 'rsa' is
512  // equivalent to 'psk' when identifying networks.
513
514  EXPECT_CALL(
515      *mock_manager_client_,
516      ConfigureServiceForProfile(dbus::ObjectPath(kUser1ProfilePath),
517                                 IsEqualTo(expected_shill_properties.get()),
518                                 _, _));
519
520  SetPolicy(::onc::ONC_SOURCE_USER_POLICY, kUser1, "policy/policy_wifi1.onc");
521  message_loop_.RunUntilIdle();
522}
523
524TEST_F(ManagedNetworkConfigurationHandlerTest, SetPolicyReapplyToManaged) {
525  InitializeStandardProfiles();
526  SetUpEntry("policy/shill_policy_on_unmanaged_wifi1.json",
527             kUser1ProfilePath,
528             "old_entry_path");
529
530  scoped_ptr<base::DictionaryValue> expected_shill_properties =
531      test_utils::ReadTestDictionary(
532          "policy/shill_policy_on_unmanaged_wifi1.json");
533
534  // The passphrase isn't sent again, because it's configured by the user and
535  // Shill doesn't send it on GetProperties calls.
536  expected_shill_properties->RemoveWithoutPathExpansion(
537      shill::kPassphraseProperty, NULL);
538
539  EXPECT_CALL(*mock_profile_client_,
540              GetProperties(dbus::ObjectPath(kUser1ProfilePath), _, _));
541
542  EXPECT_CALL(
543      *mock_profile_client_,
544      GetEntry(dbus::ObjectPath(kUser1ProfilePath), "old_entry_path", _, _));
545
546  EXPECT_CALL(*mock_manager_client_,
547              ConfigureServiceForProfile(
548                  dbus::ObjectPath(kUser1ProfilePath),
549                  IsEqualTo(expected_shill_properties.get()),
550                  _, _));
551
552  SetPolicy(::onc::ONC_SOURCE_USER_POLICY, kUser1, "policy/policy_wifi1.onc");
553  message_loop_.RunUntilIdle();
554  VerifyAndClearExpectations();
555
556  // If we apply the policy again, without change, then the Shill profile will
557  // not be modified.
558  EXPECT_CALL(*mock_profile_client_,
559              GetProperties(dbus::ObjectPath(kUser1ProfilePath), _, _));
560
561  EXPECT_CALL(
562      *mock_profile_client_,
563      GetEntry(dbus::ObjectPath(kUser1ProfilePath), "old_entry_path", _, _));
564
565  SetPolicy(::onc::ONC_SOURCE_USER_POLICY, kUser1, "policy/policy_wifi1.onc");
566  message_loop_.RunUntilIdle();
567}
568
569TEST_F(ManagedNetworkConfigurationHandlerTest, SetPolicyUnmanageManaged) {
570  InitializeStandardProfiles();
571  SetUpEntry("policy/shill_policy_on_unmanaged_wifi1.json",
572             kUser1ProfilePath,
573             "old_entry_path");
574
575  EXPECT_CALL(*mock_profile_client_,
576              GetProperties(dbus::ObjectPath(kUser1ProfilePath), _, _));
577
578  EXPECT_CALL(*mock_profile_client_,
579              GetEntry(dbus::ObjectPath(kUser1ProfilePath),
580                       "old_entry_path",
581                       _, _));
582
583  EXPECT_CALL(*mock_profile_client_,
584              DeleteEntry(dbus::ObjectPath(kUser1ProfilePath),
585                          "old_entry_path",
586                          _, _));
587
588  SetPolicy(::onc::ONC_SOURCE_USER_POLICY, kUser1, "");
589  message_loop_.RunUntilIdle();
590}
591
592TEST_F(ManagedNetworkConfigurationHandlerTest, SetEmptyPolicyIgnoreUnmanaged) {
593  InitializeStandardProfiles();
594  SetUpEntry("policy/shill_unmanaged_wifi1.json",
595             kUser1ProfilePath,
596             "old_entry_path");
597
598  EXPECT_CALL(*mock_profile_client_,
599              GetProperties(dbus::ObjectPath(kUser1ProfilePath), _, _));
600
601  EXPECT_CALL(*mock_profile_client_,
602              GetEntry(dbus::ObjectPath(kUser1ProfilePath),
603                       "old_entry_path",
604                       _, _));
605
606  SetPolicy(::onc::ONC_SOURCE_USER_POLICY, kUser1, "");
607  message_loop_.RunUntilIdle();
608}
609
610TEST_F(ManagedNetworkConfigurationHandlerTest, SetPolicyIgnoreUnmanaged) {
611  InitializeStandardProfiles();
612  SetUpEntry("policy/shill_unmanaged_wifi2.json",
613             kUser1ProfilePath,
614             "wifi2_entry_path");
615
616  EXPECT_CALL(*mock_profile_client_,
617              GetProperties(dbus::ObjectPath(kUser1ProfilePath), _, _));
618
619  EXPECT_CALL(
620      *mock_profile_client_,
621      GetEntry(dbus::ObjectPath(kUser1ProfilePath), "wifi2_entry_path", _, _));
622
623  scoped_ptr<base::DictionaryValue> expected_shill_properties =
624      test_utils::ReadTestDictionary(
625          "policy/shill_policy_on_unconfigured_wifi1.json");
626
627  EXPECT_CALL(*mock_manager_client_,
628              ConfigureServiceForProfile(
629                  dbus::ObjectPath(kUser1ProfilePath),
630                  IsEqualTo(expected_shill_properties.get()),
631                  _, _));
632
633  SetPolicy(::onc::ONC_SOURCE_USER_POLICY, kUser1, "policy/policy_wifi1.onc");
634  message_loop_.RunUntilIdle();
635}
636
637TEST_F(ManagedNetworkConfigurationHandlerTest, AutoConnectDisallowed) {
638  InitializeStandardProfiles();
639  SetUpEntry("policy/shill_unmanaged_wifi2.json",
640             kUser1ProfilePath,
641             "wifi2_entry_path");
642
643  EXPECT_CALL(*mock_profile_client_,
644              GetProperties(dbus::ObjectPath(kUser1ProfilePath), _, _));
645
646  EXPECT_CALL(
647      *mock_profile_client_,
648      GetEntry(dbus::ObjectPath(kUser1ProfilePath), "wifi2_entry_path", _, _));
649
650  scoped_ptr<base::DictionaryValue> expected_shill_properties =
651      test_utils::ReadTestDictionary(
652          "policy/shill_disallow_autoconnect_on_unmanaged_wifi2.json");
653
654  EXPECT_CALL(*mock_manager_client_,
655              ConfigureServiceForProfile(
656                  dbus::ObjectPath(kUser1ProfilePath),
657                  IsEqualTo(expected_shill_properties.get()),
658                  _, _));
659
660  SetPolicy(::onc::ONC_SOURCE_USER_POLICY,
661            kUser1,
662            "policy/policy_disallow_autoconnect.onc");
663  message_loop_.RunUntilIdle();
664}
665
666TEST_F(ManagedNetworkConfigurationHandlerTest, LateProfileLoading) {
667  SetPolicy(::onc::ONC_SOURCE_USER_POLICY, kUser1, "policy/policy_wifi1.onc");
668
669  message_loop_.RunUntilIdle();
670  VerifyAndClearExpectations();
671
672  scoped_ptr<base::DictionaryValue> expected_shill_properties =
673      test_utils::ReadTestDictionary(
674          "policy/shill_policy_on_unconfigured_wifi1.json");
675
676  EXPECT_CALL(*mock_profile_client_,
677              GetProperties(dbus::ObjectPath(kUser1ProfilePath), _, _));
678
679  EXPECT_CALL(*mock_manager_client_,
680              ConfigureServiceForProfile(
681                  dbus::ObjectPath(kUser1ProfilePath),
682                  IsEqualTo(expected_shill_properties.get()),
683                  _, _));
684
685  InitializeStandardProfiles();
686  message_loop_.RunUntilIdle();
687}
688
689class ManagedNetworkConfigurationHandlerShutdownTest
690    : public ManagedNetworkConfigurationHandlerTest {
691 public:
692  virtual void SetUp() OVERRIDE {
693    ManagedNetworkConfigurationHandlerTest::SetUp();
694    ON_CALL(*mock_profile_client_, GetProperties(_, _, _)).WillByDefault(
695        Invoke(&ManagedNetworkConfigurationHandlerShutdownTest::GetProperties));
696  }
697
698  static void GetProperties(
699      const dbus::ObjectPath& profile_path,
700      const ShillClientHelper::DictionaryValueCallbackWithoutStatus& callback,
701      const ShillClientHelper::ErrorCallback& error_callback) {
702    base::MessageLoop::current()->PostTask(
703        FROM_HERE,
704        base::Bind(ManagedNetworkConfigurationHandlerShutdownTest::
705                       CallbackWithEmptyDictionary,
706                   callback));
707  }
708
709  static void CallbackWithEmptyDictionary(
710      const ShillClientHelper::DictionaryValueCallbackWithoutStatus& callback) {
711    callback.Run(base::DictionaryValue());
712  }
713};
714
715TEST_F(ManagedNetworkConfigurationHandlerShutdownTest,
716       DuringPolicyApplication) {
717  InitializeStandardProfiles();
718
719  EXPECT_CALL(*mock_profile_client_,
720              GetProperties(dbus::ObjectPath(kUser1ProfilePath), _, _));
721
722  SetPolicy(::onc::ONC_SOURCE_USER_POLICY, kUser1, "policy/policy_wifi1.onc");
723  managed_network_configuration_handler_.reset();
724  message_loop_.RunUntilIdle();
725}
726
727}  // namespace chromeos
728