blob: d660d00e2ddf684a82cac2190b9bd12c01534ef6 [file] [log] [blame]
[email protected]3c8bd112012-11-07 10:14:591// 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.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_manager_client.h"
18#include "chromeos/dbus/shill_service_client.h"
19#include "dbus/object_path.h"
20#include "testing/gtest/include/gtest/gtest.h"
21#include "third_party/cros_system_api/dbus/service_constants.h"
22
23namespace chromeos {
24
25namespace {
26
27void ErrorCallbackFunction(const std::string& error_name,
28 const std::string& error_message) {
29 LOG(ERROR) << "Shill Error: " << error_name << " : " << error_message;
30}
31
32class TestListener : public internal::ShillPropertyHandler::Listener {
33 public:
34 TestListener() : manager_updates_(0), errors_(0) {
35 }
36
37 virtual void UpdateManagedList(ManagedState::ManagedType type,
38 const base::ListValue& entries) OVERRIDE {
39 UpdateEntries(GetTypeString(type), entries);
40 }
41
[email protected]3c8bd112012-11-07 10:14:5942 virtual void UpdateManagedStateProperties(
43 ManagedState::ManagedType type,
44 const std::string& path,
45 const base::DictionaryValue& properties) OVERRIDE {
46 AddPropertyUpdate(GetTypeString(type), path);
47 }
48
49 virtual void UpdateNetworkServiceProperty(
50 const std::string& service_path,
51 const std::string& key,
52 const base::Value& value) OVERRIDE {
53 AddPropertyUpdate(flimflam::kServicesProperty, service_path);
54 }
55
[email protected]b16d79d72013-02-07 08:14:4656 virtual void UpdateDeviceProperty(
57 const std::string& device_path,
58 const std::string& key,
59 const base::Value& value) OVERRIDE {
60 AddPropertyUpdate(flimflam::kDevicesProperty, device_path);
61 }
62
[email protected]3c8bd112012-11-07 10:14:5963 virtual void ManagerPropertyChanged() OVERRIDE {
64 ++manager_updates_;
65 }
66
67 virtual void UpdateNetworkServiceIPAddress(
68 const std::string& service_path,
69 const std::string& ip_address) OVERRIDE {
70 AddPropertyUpdate(flimflam::kServicesProperty, service_path);
71 }
72
73 virtual void ManagedStateListChanged(
74 ManagedState::ManagedType type) OVERRIDE {
75 AddStateListUpdate(GetTypeString(type));
76 }
77
78 std::vector<std::string>& entries(const std::string& type) {
79 return entries_[type];
80 }
81 std::map<std::string, int>& property_updates(const std::string& type) {
82 return property_updates_[type];
83 }
84 int list_updates(const std::string& type) { return list_updates_[type]; }
85 int manager_updates() { return manager_updates_; }
86 int errors() { return errors_; }
87
88 private:
89 std::string GetTypeString(ManagedState::ManagedType type) {
90 if (type == ManagedState::MANAGED_TYPE_NETWORK) {
91 return flimflam::kServicesProperty;
92 } else if (type == ManagedState::MANAGED_TYPE_DEVICE) {
93 return flimflam::kDevicesProperty;
94 }
95 LOG(ERROR) << "UpdateManagedList called with unrecognized type: " << type;
96 ++errors_;
97 return std::string();
98 }
99
100 void UpdateEntries(const std::string& type, const base::ListValue& entries) {
101 if (type.empty())
102 return;
103 entries_[type].clear();
104 for (base::ListValue::const_iterator iter = entries.begin();
105 iter != entries.end(); ++iter) {
106 std::string path;
107 if ((*iter)->GetAsString(&path))
108 entries_[type].push_back(path);
109 }
110 }
111
112 void AddPropertyUpdate(const std::string& type, const std::string& path) {
113 if (type.empty())
114 return;
115 property_updates(type)[path] += 1;
116 }
117
118 void AddStateListUpdate(const std::string& type) {
119 if (type.empty())
120 return;
121 list_updates_[type] += 1;
122 }
123
124 // Map of list-type -> paths
125 std::map<std::string, std::vector<std::string> > entries_;
126 // Map of list-type -> map of paths -> update counts
127 std::map<std::string, std::map<std::string, int> > property_updates_;
128 // Map of list-type -> list update counts
129 std::map<std::string, int > list_updates_;
130 int manager_updates_;
131 int errors_;
132};
133
134} // namespace
135
136class ShillPropertyHandlerTest : public testing::Test {
137 public:
138 ShillPropertyHandlerTest()
139 : manager_test_(NULL),
140 device_test_(NULL),
141 service_test_(NULL) {
142 }
143 virtual ~ShillPropertyHandlerTest() {
144 }
145
146 virtual void SetUp() OVERRIDE {
147 // Initialize DBusThreadManager with a stub implementation.
148 DBusThreadManager::InitializeWithStub();
149 // Get the test interface for manager / device / service and clear the
150 // default stub properties.
151 manager_test_ =
152 DBusThreadManager::Get()->GetShillManagerClient()->GetTestInterface();
153 ASSERT_TRUE(manager_test_);
154 device_test_ =
155 DBusThreadManager::Get()->GetShillDeviceClient()->GetTestInterface();
156 ASSERT_TRUE(device_test_);
157 service_test_ =
158 DBusThreadManager::Get()->GetShillServiceClient()->GetTestInterface();
159 ASSERT_TRUE(service_test_);
160 }
161
162 virtual void TearDown() OVERRIDE {
163 shill_property_handler_.reset();
164 listener_.reset();
165 DBusThreadManager::Shutdown();
166 }
167
168 void AddDevice(const std::string& type, const std::string& id) {
169 ASSERT_TRUE(IsValidType(type));
170 manager_test_->AddDevice(id);
[email protected]b7c377e2013-01-15 10:00:39171 device_test_->AddDevice(id, type, std::string("/device/" + id));
[email protected]3c8bd112012-11-07 10:14:59172 }
173
174 void RemoveDevice(const std::string& id) {
175 manager_test_->RemoveDevice(id);
176 device_test_->RemoveDevice(id);
177 }
178
179 void AddService(const std::string& type,
180 const std::string& id,
181 const std::string& state,
182 bool add_to_watch_list) {
183 ASSERT_TRUE(IsValidType(type));
184 manager_test_->AddService(id, add_to_watch_list);
185 service_test_->AddService(id, id, type, state);
186 }
187
188 void RemoveService(const std::string& id) {
189 manager_test_->RemoveService(id);
190 service_test_->RemoveService(id);
191 }
192
193 // Call this after any initial Shill client setup
194 void SetupShillPropertyHandler() {
195 listener_.reset(new TestListener);
196 shill_property_handler_.reset(
197 new internal::ShillPropertyHandler(listener_.get()));
198 shill_property_handler_->Init();
199 }
200
201 bool IsValidType(const std::string& type) {
202 return (type == flimflam::kTypeEthernet ||
203 type == flimflam::kTypeWifi ||
204 type == flimflam::kTypeWimax ||
205 type == flimflam::kTypeBluetooth ||
206 type == flimflam::kTypeCellular ||
207 type == flimflam::kTypeVPN);
208 }
209
210 protected:
211 MessageLoopForUI message_loop_;
212 scoped_ptr<TestListener> listener_;
213 scoped_ptr<internal::ShillPropertyHandler> shill_property_handler_;
214 ShillManagerClient::TestInterface* manager_test_;
215 ShillDeviceClient::TestInterface* device_test_;
216 ShillServiceClient::TestInterface* service_test_;
217
218 private:
219 DISALLOW_COPY_AND_ASSIGN(ShillPropertyHandlerTest);
220};
221
222TEST_F(ShillPropertyHandlerTest, ShillPropertyHandlerStub) {
223 SetupShillPropertyHandler();
224 message_loop_.RunUntilIdle();
225 EXPECT_EQ(1, listener_->manager_updates());
226 // ShillManagerClient default stub entries are in shill_manager_client.cc.
227 // TODO(stevenjb): Eliminate default stub entries and add them explicitly.
[email protected]b16d79d72013-02-07 08:14:46228 EXPECT_TRUE(shill_property_handler_->TechnologyAvailable(
229 flimflam::kTypeWifi));
230 EXPECT_TRUE(shill_property_handler_->TechnologyEnabled(
231 flimflam::kTypeWifi));
[email protected]3c8bd112012-11-07 10:14:59232 const size_t kNumShillManagerClientStubImplDevices = 2;
233 EXPECT_EQ(kNumShillManagerClientStubImplDevices,
234 listener_->entries(flimflam::kDevicesProperty).size());
235 const size_t kNumShillManagerClientStubImplServices = 4;
236 EXPECT_EQ(kNumShillManagerClientStubImplServices,
237 listener_->entries(flimflam::kServicesProperty).size());
238
239 EXPECT_EQ(0, listener_->errors());
240}
241
242TEST_F(ShillPropertyHandlerTest, ShillPropertyHandlerTechnologyChanged) {
243 // This relies on the stub dbus implementations for ShillManagerClient,
244 SetupShillPropertyHandler();
245 message_loop_.RunUntilIdle();
246 EXPECT_EQ(1, listener_->manager_updates());
247 // Add a disabled technology.
248 manager_test_->AddTechnology(flimflam::kTypeWimax, false);
249 message_loop_.RunUntilIdle();
250 EXPECT_EQ(2, listener_->manager_updates());
[email protected]b16d79d72013-02-07 08:14:46251 EXPECT_TRUE(shill_property_handler_->TechnologyAvailable(
252 flimflam::kTypeWimax));
253 EXPECT_FALSE(shill_property_handler_->TechnologyEnabled(
254 flimflam::kTypeWimax));
255
[email protected]3c8bd112012-11-07 10:14:59256 // Enable the technology.
257 DBusThreadManager::Get()->GetShillManagerClient()->EnableTechnology(
258 flimflam::kTypeWimax,
259 base::Bind(&base::DoNothing), base::Bind(&ErrorCallbackFunction));
260 message_loop_.RunUntilIdle();
261 EXPECT_EQ(3, listener_->manager_updates());
[email protected]b16d79d72013-02-07 08:14:46262 EXPECT_TRUE(shill_property_handler_->TechnologyEnabled(
263 flimflam::kTypeWimax));
[email protected]3c8bd112012-11-07 10:14:59264
265 EXPECT_EQ(0, listener_->errors());
266}
267
268TEST_F(ShillPropertyHandlerTest, ShillPropertyHandlerDevicePropertyChanged) {
269 // This relies on the stub dbus implementations for ShillManagerClient,
270 SetupShillPropertyHandler();
271 message_loop_.RunUntilIdle();
272 EXPECT_EQ(1, listener_->manager_updates());
273 EXPECT_EQ(1, listener_->list_updates(flimflam::kDevicesProperty));
274 const size_t kNumShillManagerClientStubImplDevices = 2;
275 EXPECT_EQ(kNumShillManagerClientStubImplDevices,
276 listener_->entries(flimflam::kDevicesProperty).size());
277 // Add a device.
278 const std::string kTestDevicePath("test_wifi_device1");
279 AddDevice(flimflam::kTypeWifi, kTestDevicePath);
280 message_loop_.RunUntilIdle();
281 EXPECT_EQ(1, listener_->manager_updates()); // No new manager updates.
282 EXPECT_EQ(2, listener_->list_updates(flimflam::kDevicesProperty));
283 EXPECT_EQ(kNumShillManagerClientStubImplDevices + 1,
284 listener_->entries(flimflam::kDevicesProperty).size());
285 // Device changes are not observed.
286 // Remove a device
287 RemoveDevice(kTestDevicePath);
288 message_loop_.RunUntilIdle();
289 EXPECT_EQ(3, listener_->list_updates(flimflam::kDevicesProperty));
290 EXPECT_EQ(kNumShillManagerClientStubImplDevices,
291 listener_->entries(flimflam::kDevicesProperty).size());
292
293 EXPECT_EQ(0, listener_->errors());
294}
295
296TEST_F(ShillPropertyHandlerTest, ShillPropertyHandlerServicePropertyChanged) {
297 // This relies on the stub dbus implementations for ShillManagerClient,
298 SetupShillPropertyHandler();
299 message_loop_.RunUntilIdle();
300 EXPECT_EQ(1, listener_->manager_updates());
301 EXPECT_EQ(1, listener_->list_updates(flimflam::kServicesProperty));
302 const size_t kNumShillManagerClientStubImplServices = 4;
303 EXPECT_EQ(kNumShillManagerClientStubImplServices,
304 listener_->entries(flimflam::kServicesProperty).size());
305
306 // Add an unwatched service.
307 const std::string kTestServicePath("test_wifi_service1");
308 AddService(flimflam::kTypeWifi, kTestServicePath,
309 flimflam::kStateIdle, false);
310 message_loop_.RunUntilIdle();
311 EXPECT_EQ(1, listener_->manager_updates()); // No new manager updates.
[email protected]b16d79d72013-02-07 08:14:46312 // Only watched services trigger a service list update.
313 EXPECT_EQ(1, listener_->list_updates(flimflam::kServicesProperty));
[email protected]3c8bd112012-11-07 10:14:59314 EXPECT_EQ(kNumShillManagerClientStubImplServices + 1,
315 listener_->entries(flimflam::kServicesProperty).size());
316 // Change a property.
317 base::FundamentalValue scan_interval(3);
318 DBusThreadManager::Get()->GetShillServiceClient()->SetProperty(
319 dbus::ObjectPath(kTestServicePath),
320 flimflam::kScanIntervalProperty,
321 scan_interval,
322 base::Bind(&base::DoNothing), base::Bind(&ErrorCallbackFunction));
323 message_loop_.RunUntilIdle();
324 // Property change should NOT trigger an update.
325 EXPECT_EQ(0, listener_->
326 property_updates(flimflam::kServicesProperty)[kTestServicePath]);
327
328 // Add the existing service to the watch list.
329 AddService(flimflam::kTypeWifi, kTestServicePath,
330 flimflam::kStateIdle, true);
331 message_loop_.RunUntilIdle();
[email protected]fa6797e2012-11-28 15:15:44332 // Service list update should be received when watch list changes.
[email protected]b16d79d72013-02-07 08:14:46333 EXPECT_EQ(2, listener_->list_updates(flimflam::kServicesProperty));
[email protected]fa6797e2012-11-28 15:15:44334 // Number of services shouldn't change.
[email protected]3c8bd112012-11-07 10:14:59335 EXPECT_EQ(kNumShillManagerClientStubImplServices + 1,
336 listener_->entries(flimflam::kServicesProperty).size());
[email protected]fa6797e2012-11-28 15:15:44337 // Property update should be received when watched service is added.
338 EXPECT_EQ(1, listener_->
339 property_updates(flimflam::kServicesProperty)[kTestServicePath]);
340
[email protected]3c8bd112012-11-07 10:14:59341 // Change a property.
342 DBusThreadManager::Get()->GetShillServiceClient()->SetProperty(
343 dbus::ObjectPath(kTestServicePath),
344 flimflam::kScanIntervalProperty,
345 scan_interval,
346 base::Bind(&base::DoNothing), base::Bind(&ErrorCallbackFunction));
347 message_loop_.RunUntilIdle();
[email protected]fa6797e2012-11-28 15:15:44348 // Property change should trigger another update.
349 EXPECT_EQ(2, listener_->
[email protected]3c8bd112012-11-07 10:14:59350 property_updates(flimflam::kServicesProperty)[kTestServicePath]);
351
352 // Remove a service
353 RemoveService(kTestServicePath);
354 message_loop_.RunUntilIdle();
[email protected]b16d79d72013-02-07 08:14:46355 EXPECT_EQ(3, listener_->list_updates(flimflam::kServicesProperty));
[email protected]3c8bd112012-11-07 10:14:59356 EXPECT_EQ(kNumShillManagerClientStubImplServices,
357 listener_->entries(flimflam::kServicesProperty).size());
358
359 EXPECT_EQ(0, listener_->errors());
360}
361
362// TODO(stevenjb): Test IP Configs.
363
364} // namespace chromeos