shill_property_handler_unittest.cc revision 9ab5563a3196760eb381d102cbb2bc0f7abc6a50
1// Copyright (c) 2012 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 "chromeos/network/shill_property_handler.h"
6
7#include <map>
8#include <set>
9#include <string>
10
11#include "base/bind.h"
12#include "base/memory/scoped_ptr.h"
13#include "base/message_loop/message_loop.h"
14#include "base/values.h"
15#include "chromeos/dbus/dbus_thread_manager.h"
16#include "chromeos/dbus/shill_device_client.h"
17#include "chromeos/dbus/shill_ipconfig_client.h"
18#include "chromeos/dbus/shill_manager_client.h"
19#include "chromeos/dbus/shill_profile_client.h"
20#include "chromeos/dbus/shill_service_client.h"
21#include "dbus/object_path.h"
22#include "testing/gtest/include/gtest/gtest.h"
23#include "third_party/cros_system_api/dbus/service_constants.h"
24
25namespace chromeos {
26
27namespace {
28
29void DoNothingWithCallStatus(DBusMethodCallStatus call_status) {
30}
31
32void ErrorCallbackFunction(const std::string& error_name,
33                           const std::string& error_message) {
34  LOG(ERROR) << "Shill Error: " << error_name << " : " << error_message;
35}
36
37class TestListener : public internal::ShillPropertyHandler::Listener {
38 public:
39  TestListener() : manager_updates_(0), errors_(0) {
40  }
41
42  virtual void UpdateManagedList(ManagedState::ManagedType type,
43                                 const base::ListValue& entries) OVERRIDE {
44    UpdateEntries(GetTypeString(type), entries);
45  }
46
47  virtual void UpdateManagedStateProperties(
48      ManagedState::ManagedType type,
49      const std::string& path,
50      const base::DictionaryValue& properties) OVERRIDE {
51    AddInitialPropertyUpdate(GetTypeString(type), path);
52  }
53
54  virtual void ProfileListChanged() OVERRIDE {
55  }
56
57  virtual void UpdateNetworkServiceProperty(
58      const std::string& service_path,
59      const std::string& key,
60      const base::Value& value) OVERRIDE {
61    AddPropertyUpdate(flimflam::kServicesProperty, service_path);
62  }
63
64  virtual void UpdateDeviceProperty(
65      const std::string& device_path,
66      const std::string& key,
67      const base::Value& value) OVERRIDE {
68    AddPropertyUpdate(flimflam::kDevicesProperty, device_path);
69  }
70
71  virtual void NotifyManagerPropertyChanged() OVERRIDE {
72    ++manager_updates_;
73  }
74
75  virtual void CheckPortalListChanged(
76      const std::string& check_portal_list) OVERRIDE {
77  }
78
79  virtual void ManagedStateListChanged(
80      ManagedState::ManagedType type) OVERRIDE {
81    AddStateListUpdate(GetTypeString(type));
82  }
83
84  std::vector<std::string>& entries(const std::string& type) {
85    return entries_[type];
86  }
87  std::map<std::string, int>& property_updates(const std::string& type) {
88    return property_updates_[type];
89  }
90  std::map<std::string, int>& initial_property_updates(
91      const std::string& type) {
92    return initial_property_updates_[type];
93  }
94  int list_updates(const std::string& type) { return list_updates_[type]; }
95  int manager_updates() { return manager_updates_; }
96  int errors() { return errors_; }
97
98 private:
99  std::string GetTypeString(ManagedState::ManagedType type) {
100    if (type == ManagedState::MANAGED_TYPE_NETWORK) {
101      return flimflam::kServicesProperty;
102    } else if (type == ManagedState::MANAGED_TYPE_FAVORITE) {
103      return shill::kServiceCompleteListProperty;
104    } else if (type == ManagedState::MANAGED_TYPE_DEVICE) {
105      return flimflam::kDevicesProperty;
106    }
107    LOG(ERROR) << "UpdateManagedList called with unrecognized type: " << type;
108    ++errors_;
109    return std::string();
110  }
111
112  void UpdateEntries(const std::string& type, const base::ListValue& entries) {
113    if (type.empty())
114      return;
115    entries_[type].clear();
116    for (base::ListValue::const_iterator iter = entries.begin();
117         iter != entries.end(); ++iter) {
118      std::string path;
119      if ((*iter)->GetAsString(&path))
120        entries_[type].push_back(path);
121    }
122  }
123
124  void AddPropertyUpdate(const std::string& type, const std::string& path) {
125    if (type.empty())
126      return;
127    property_updates(type)[path] += 1;
128  }
129
130  void AddInitialPropertyUpdate(const std::string& type,
131                                const std::string& path) {
132    if (type.empty())
133      return;
134    initial_property_updates(type)[path] += 1;
135  }
136
137  void AddStateListUpdate(const std::string& type) {
138    if (type.empty())
139      return;
140    list_updates_[type] += 1;
141  }
142
143  // Map of list-type -> paths
144  std::map<std::string, std::vector<std::string> > entries_;
145  // Map of list-type -> map of paths -> update counts
146  std::map<std::string, std::map<std::string, int> > property_updates_;
147  std::map<std::string, std::map<std::string, int> > initial_property_updates_;
148  // Map of list-type -> list update counts
149  std::map<std::string, int > list_updates_;
150  int manager_updates_;
151  int errors_;
152};
153
154}  // namespace
155
156class ShillPropertyHandlerTest : public testing::Test {
157 public:
158  ShillPropertyHandlerTest()
159      : manager_test_(NULL),
160        device_test_(NULL),
161        service_test_(NULL),
162        profile_test_(NULL) {
163  }
164  virtual ~ShillPropertyHandlerTest() {
165  }
166
167  virtual void SetUp() OVERRIDE {
168    // Initialize DBusThreadManager with a stub implementation.
169    DBusThreadManager::InitializeWithStub();
170    // Get the test interface for manager / device / service and clear the
171    // default stub properties.
172    manager_test_ =
173        DBusThreadManager::Get()->GetShillManagerClient()->GetTestInterface();
174    ASSERT_TRUE(manager_test_);
175    device_test_ =
176        DBusThreadManager::Get()->GetShillDeviceClient()->GetTestInterface();
177    ASSERT_TRUE(device_test_);
178    service_test_ =
179        DBusThreadManager::Get()->GetShillServiceClient()->GetTestInterface();
180    ASSERT_TRUE(service_test_);
181    profile_test_ =
182        DBusThreadManager::Get()->GetShillProfileClient()->GetTestInterface();
183    ASSERT_TRUE(profile_test_);
184    SetupShillPropertyHandler();
185    message_loop_.RunUntilIdle();
186  }
187
188  virtual void TearDown() OVERRIDE {
189    shill_property_handler_.reset();
190    listener_.reset();
191    DBusThreadManager::Shutdown();
192  }
193
194  void AddDevice(const std::string& type, const std::string& id) {
195    ASSERT_TRUE(IsValidType(type));
196    device_test_->AddDevice(id, type, std::string("/device/" + id));
197  }
198
199  void RemoveDevice(const std::string& id) {
200    device_test_->RemoveDevice(id);
201  }
202
203  void AddService(const std::string& type,
204                  const std::string& id,
205                  const std::string& state,
206                  bool add_to_watch_list) {
207    ASSERT_TRUE(IsValidType(type));
208    service_test_->AddService(id, id, type, state,
209                              true /* visible */, add_to_watch_list);
210  }
211
212  void AddServiceWithIPConfig(const std::string& type,
213                              const std::string& id,
214                              const std::string& state,
215                              const std::string& ipconfig_path,
216                              bool add_to_watch_list) {
217    ASSERT_TRUE(IsValidType(type));
218    service_test_->AddServiceWithIPConfig(id, id, type, state,
219                                          ipconfig_path,
220                                          true /* visible */,
221                                          add_to_watch_list);
222  }
223
224  void AddServiceToProfile(const std::string& type,
225                           const std::string& id,
226                           bool visible) {
227    service_test_->AddService(id, id, type, flimflam::kStateIdle,
228                              visible, false /* watch */);
229    std::vector<std::string> profiles;
230    profile_test_->GetProfilePaths(&profiles);
231    ASSERT_TRUE(profiles.size() > 0);
232    base::DictionaryValue properties;  // Empty entry
233    profile_test_->AddService(profiles[0], id);
234  }
235
236  void RemoveService(const std::string& id) {
237    service_test_->RemoveService(id);
238  }
239
240  // Call this after any initial Shill client setup
241  void SetupShillPropertyHandler() {
242    SetupDefaultShillState();
243    listener_.reset(new TestListener);
244    shill_property_handler_.reset(
245        new internal::ShillPropertyHandler(listener_.get()));
246    shill_property_handler_->Init();
247  }
248
249  bool IsValidType(const std::string& type) {
250    return (type == flimflam::kTypeEthernet ||
251            type == flimflam::kTypeWifi ||
252            type == flimflam::kTypeWimax ||
253            type == flimflam::kTypeBluetooth ||
254            type == flimflam::kTypeCellular ||
255            type == flimflam::kTypeVPN);
256  }
257
258 protected:
259  void SetupDefaultShillState() {
260    message_loop_.RunUntilIdle();  // Process any pending updates
261    device_test_->ClearDevices();
262    AddDevice(flimflam::kTypeWifi, "stub_wifi_device1");
263    AddDevice(flimflam::kTypeCellular, "stub_cellular_device1");
264    service_test_->ClearServices();
265    const bool add_to_watchlist = true;
266    AddService(flimflam::kTypeEthernet, "stub_ethernet",
267               flimflam::kStateOnline, add_to_watchlist);
268    AddService(flimflam::kTypeWifi, "stub_wifi1",
269               flimflam::kStateOnline, add_to_watchlist);
270    AddService(flimflam::kTypeWifi, "stub_wifi2",
271               flimflam::kStateIdle, add_to_watchlist);
272    AddService(flimflam::kTypeCellular, "stub_cellular1",
273               flimflam::kStateIdle, add_to_watchlist);
274  }
275
276  base::MessageLoopForUI message_loop_;
277  scoped_ptr<TestListener> listener_;
278  scoped_ptr<internal::ShillPropertyHandler> shill_property_handler_;
279  ShillManagerClient::TestInterface* manager_test_;
280  ShillDeviceClient::TestInterface* device_test_;
281  ShillServiceClient::TestInterface* service_test_;
282  ShillProfileClient::TestInterface* profile_test_;
283
284 private:
285  DISALLOW_COPY_AND_ASSIGN(ShillPropertyHandlerTest);
286};
287
288TEST_F(ShillPropertyHandlerTest, ShillPropertyHandlerStub) {
289  EXPECT_EQ(1, listener_->manager_updates());
290  EXPECT_TRUE(shill_property_handler_->IsTechnologyAvailable(
291      flimflam::kTypeWifi));
292  EXPECT_TRUE(shill_property_handler_->IsTechnologyEnabled(
293      flimflam::kTypeWifi));
294  const size_t kNumShillManagerClientStubImplDevices = 2;
295  EXPECT_EQ(kNumShillManagerClientStubImplDevices,
296            listener_->entries(flimflam::kDevicesProperty).size());
297  const size_t kNumShillManagerClientStubImplServices = 4;
298  EXPECT_EQ(kNumShillManagerClientStubImplServices,
299            listener_->entries(flimflam::kServicesProperty).size());
300
301  EXPECT_EQ(0, listener_->errors());
302}
303
304TEST_F(ShillPropertyHandlerTest, ShillPropertyHandlerTechnologyChanged) {
305  EXPECT_EQ(1, listener_->manager_updates());
306  // Add a disabled technology.
307  manager_test_->AddTechnology(flimflam::kTypeWimax, false);
308  message_loop_.RunUntilIdle();
309  EXPECT_EQ(2, listener_->manager_updates());
310  EXPECT_TRUE(shill_property_handler_->IsTechnologyAvailable(
311      flimflam::kTypeWimax));
312  EXPECT_FALSE(shill_property_handler_->IsTechnologyEnabled(
313      flimflam::kTypeWimax));
314
315  // Enable the technology.
316  DBusThreadManager::Get()->GetShillManagerClient()->EnableTechnology(
317      flimflam::kTypeWimax,
318      base::Bind(&base::DoNothing), base::Bind(&ErrorCallbackFunction));
319  message_loop_.RunUntilIdle();
320  EXPECT_EQ(3, listener_->manager_updates());
321  EXPECT_TRUE(shill_property_handler_->IsTechnologyEnabled(
322      flimflam::kTypeWimax));
323
324  EXPECT_EQ(0, listener_->errors());
325}
326
327TEST_F(ShillPropertyHandlerTest, ShillPropertyHandlerDevicePropertyChanged) {
328  EXPECT_EQ(1, listener_->manager_updates());
329  EXPECT_EQ(1, listener_->list_updates(flimflam::kDevicesProperty));
330  const size_t kNumShillManagerClientStubImplDevices = 2;
331  EXPECT_EQ(kNumShillManagerClientStubImplDevices,
332            listener_->entries(flimflam::kDevicesProperty).size());
333  // Add a device.
334  const std::string kTestDevicePath("test_wifi_device1");
335  AddDevice(flimflam::kTypeWifi, kTestDevicePath);
336  message_loop_.RunUntilIdle();
337  EXPECT_EQ(1, listener_->manager_updates());  // No new manager updates.
338  EXPECT_EQ(2, listener_->list_updates(flimflam::kDevicesProperty));
339  EXPECT_EQ(kNumShillManagerClientStubImplDevices + 1,
340            listener_->entries(flimflam::kDevicesProperty).size());
341  // Device changes are not observed.
342  // Remove a device
343  RemoveDevice(kTestDevicePath);
344  message_loop_.RunUntilIdle();
345  EXPECT_EQ(3, listener_->list_updates(flimflam::kDevicesProperty));
346  EXPECT_EQ(kNumShillManagerClientStubImplDevices,
347            listener_->entries(flimflam::kDevicesProperty).size());
348
349  EXPECT_EQ(0, listener_->errors());
350}
351
352TEST_F(ShillPropertyHandlerTest, ShillPropertyHandlerServicePropertyChanged) {
353  EXPECT_EQ(1, listener_->manager_updates());
354  EXPECT_EQ(1, listener_->list_updates(flimflam::kServicesProperty));
355  const size_t kNumShillManagerClientStubImplServices = 4;
356  EXPECT_EQ(kNumShillManagerClientStubImplServices,
357            listener_->entries(flimflam::kServicesProperty).size());
358
359  // Add an unwatched service.
360  const std::string kTestServicePath("test_wifi_service1");
361  AddService(flimflam::kTypeWifi, kTestServicePath,
362             flimflam::kStateIdle, false);
363  message_loop_.RunUntilIdle();
364  EXPECT_EQ(1, listener_->manager_updates());  // No new manager updates.
365  // Watched and unwatched services trigger a service list update.
366  EXPECT_EQ(2, listener_->list_updates(flimflam::kServicesProperty));
367  EXPECT_EQ(kNumShillManagerClientStubImplServices + 1,
368            listener_->entries(flimflam::kServicesProperty).size());
369  // Service receives an initial property update.
370  EXPECT_EQ(1, listener_->initial_property_updates(
371      flimflam::kServicesProperty)[kTestServicePath]);
372  // Change a property.
373  base::FundamentalValue scan_interval(3);
374  DBusThreadManager::Get()->GetShillServiceClient()->SetProperty(
375      dbus::ObjectPath(kTestServicePath),
376      flimflam::kScanIntervalProperty,
377      scan_interval,
378      base::Bind(&base::DoNothing), base::Bind(&ErrorCallbackFunction));
379  message_loop_.RunUntilIdle();
380  // Property change triggers an update.
381  EXPECT_EQ(1, listener_->property_updates(
382      flimflam::kServicesProperty)[kTestServicePath]);
383
384  // Add the existing service to the watch list.
385  AddService(flimflam::kTypeWifi, kTestServicePath,
386             flimflam::kStateIdle, true);
387  message_loop_.RunUntilIdle();
388  // Service list update should be received when watch list changes.
389  EXPECT_EQ(2, listener_->list_updates(flimflam::kServicesProperty));
390  // Number of services shouldn't change.
391  EXPECT_EQ(kNumShillManagerClientStubImplServices + 1,
392            listener_->entries(flimflam::kServicesProperty).size());
393
394  // Change a property.
395  DBusThreadManager::Get()->GetShillServiceClient()->SetProperty(
396      dbus::ObjectPath(kTestServicePath),
397      flimflam::kScanIntervalProperty,
398      scan_interval,
399      base::Bind(&base::DoNothing), base::Bind(&ErrorCallbackFunction));
400  message_loop_.RunUntilIdle();
401  // Property change should trigger another update.
402  EXPECT_EQ(2, listener_->property_updates(
403      flimflam::kServicesProperty)[kTestServicePath]);
404
405  // Remove a service
406  RemoveService(kTestServicePath);
407  message_loop_.RunUntilIdle();
408  EXPECT_EQ(3, listener_->list_updates(flimflam::kServicesProperty));
409  EXPECT_EQ(kNumShillManagerClientStubImplServices,
410            listener_->entries(flimflam::kServicesProperty).size());
411
412  EXPECT_EQ(0, listener_->errors());
413}
414
415TEST_F(ShillPropertyHandlerTest, ShillPropertyHandlerIPConfigPropertyChanged) {
416  // Set the properties for an IP Config object.
417  const std::string kTestIPConfigPath("test_ip_config_path");
418
419  base::StringValue ip_address("192.168.1.1");
420  DBusThreadManager::Get()->GetShillIPConfigClient()->SetProperty(
421      dbus::ObjectPath(kTestIPConfigPath),
422      flimflam::kAddressProperty, ip_address,
423      base::Bind(&DoNothingWithCallStatus));
424  base::ListValue dns_servers;
425  dns_servers.Append(base::Value::CreateStringValue("192.168.1.100"));
426  dns_servers.Append(base::Value::CreateStringValue("192.168.1.101"));
427  DBusThreadManager::Get()->GetShillIPConfigClient()->SetProperty(
428      dbus::ObjectPath(kTestIPConfigPath),
429      flimflam::kNameServersProperty, dns_servers,
430      base::Bind(&DoNothingWithCallStatus));
431  base::FundamentalValue prefixlen(8);
432  DBusThreadManager::Get()->GetShillIPConfigClient()->SetProperty(
433      dbus::ObjectPath(kTestIPConfigPath),
434      flimflam::kPrefixlenProperty, prefixlen,
435      base::Bind(&DoNothingWithCallStatus));
436  base::StringValue gateway("192.0.0.1");
437  DBusThreadManager::Get()->GetShillIPConfigClient()->SetProperty(
438      dbus::ObjectPath(kTestIPConfigPath),
439      flimflam::kGatewayProperty, gateway,
440      base::Bind(&DoNothingWithCallStatus));
441  message_loop_.RunUntilIdle();
442
443  // Add a service with an empty ipconfig and then update
444  // its ipconfig property.
445  const std::string kTestServicePath1("test_wifi_service1");
446  AddService(flimflam::kTypeWifi, kTestServicePath1,
447             flimflam::kStateIdle, true);
448  message_loop_.RunUntilIdle();
449  // This is the initial property update.
450  EXPECT_EQ(1, listener_->initial_property_updates(
451      flimflam::kServicesProperty)[kTestServicePath1]);
452  DBusThreadManager::Get()->GetShillServiceClient()->SetProperty(
453      dbus::ObjectPath(kTestServicePath1),
454      shill::kIPConfigProperty,
455      base::StringValue(kTestIPConfigPath),
456      base::Bind(&base::DoNothing), base::Bind(&ErrorCallbackFunction));
457  message_loop_.RunUntilIdle();
458  // IPConfig property change on the service should trigger property updates for
459  // IP Address, DNS, prefixlen, and gateway.
460  EXPECT_EQ(4, listener_->property_updates(
461      flimflam::kServicesProperty)[kTestServicePath1]);
462
463  // Now, Add a new watched service with the IPConfig already set.
464  const std::string kTestServicePath2("test_wifi_service2");
465  AddServiceWithIPConfig(flimflam::kTypeWifi, kTestServicePath2,
466                         flimflam::kStateIdle, kTestIPConfigPath, true);
467  message_loop_.RunUntilIdle();
468  // A watched service with the IPConfig property already set must trigger
469  // property updates for IP Address, DNS, prefixlen, and gateway when added.
470  EXPECT_EQ(4, listener_->property_updates(
471      flimflam::kServicesProperty)[kTestServicePath2]);
472}
473
474TEST_F(ShillPropertyHandlerTest, ShillPropertyHandlerServiceCompleteList) {
475  // Initial list updates.
476  EXPECT_EQ(1, listener_->list_updates(flimflam::kServicesProperty));
477  EXPECT_EQ(1, listener_->list_updates(shill::kServiceCompleteListProperty));
478
479  // Add a new entry to the profile only; should trigger a single list update
480  // for both Services and ServiceCompleteList, and a single property update
481  // for ServiceCompleteList.
482  const std::string kTestServicePath1("stub_wifi_profile_only1");
483  AddServiceToProfile(flimflam::kTypeWifi, kTestServicePath1, false);
484  shill_property_handler_->UpdateManagerProperties();
485  message_loop_.RunUntilIdle();
486  EXPECT_EQ(2, listener_->list_updates(flimflam::kServicesProperty));
487  EXPECT_EQ(2, listener_->list_updates(shill::kServiceCompleteListProperty));
488  EXPECT_EQ(0, listener_->initial_property_updates(
489      flimflam::kServicesProperty)[kTestServicePath1]);
490  EXPECT_EQ(1, listener_->initial_property_updates(
491      shill::kServiceCompleteListProperty)[kTestServicePath1]);
492  EXPECT_EQ(0, listener_->property_updates(
493      flimflam::kServicesProperty)[kTestServicePath1]);
494  EXPECT_EQ(0, listener_->property_updates(
495      shill::kServiceCompleteListProperty)[kTestServicePath1]);
496
497  // Add a new entry to the services and the profile; should also trigger a
498  // single list update for both Services and ServiceCompleteList, and should
499  // trigger tow property updates for Services (one when the Profile propety
500  // changes, and one for the Request) and one ServiceCompleteList change for
501  // the Request.
502  const std::string kTestServicePath2("stub_wifi_profile_only2");
503  AddServiceToProfile(flimflam::kTypeWifi, kTestServicePath2, true);
504  shill_property_handler_->UpdateManagerProperties();
505  message_loop_.RunUntilIdle();
506  EXPECT_EQ(3, listener_->list_updates(flimflam::kServicesProperty));
507  EXPECT_EQ(3, listener_->list_updates(shill::kServiceCompleteListProperty));
508  EXPECT_EQ(1, listener_->initial_property_updates(
509      flimflam::kServicesProperty)[kTestServicePath2]);
510  EXPECT_EQ(1, listener_->initial_property_updates(
511      shill::kServiceCompleteListProperty)[kTestServicePath2]);
512  // Expect one property update for the Profile property of the Network.
513  EXPECT_EQ(1, listener_->property_updates(
514      flimflam::kServicesProperty)[kTestServicePath2]);
515  EXPECT_EQ(0, listener_->property_updates(
516      shill::kServiceCompleteListProperty)[kTestServicePath2]);
517
518  // Change a property of a Network in a Profile.
519  base::FundamentalValue scan_interval(3);
520  DBusThreadManager::Get()->GetShillServiceClient()->SetProperty(
521      dbus::ObjectPath(kTestServicePath2),
522      flimflam::kScanIntervalProperty,
523      scan_interval,
524      base::Bind(&base::DoNothing), base::Bind(&ErrorCallbackFunction));
525  message_loop_.RunUntilIdle();
526  // Property change should trigger an update for the Network only; no
527  // property updates pushed by Shill affect Favorites.
528  EXPECT_EQ(2, listener_->property_updates(
529      flimflam::kServicesProperty)[kTestServicePath2]);
530  EXPECT_EQ(0, listener_->property_updates(
531      shill::kServiceCompleteListProperty)[kTestServicePath2]);
532}
533
534}  // namespace chromeos
535