blob: 14fb74a284df4cc4a124c792f05a6112bb31a8d5 [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));
[email protected]b7c377e2013-01-15 10:00:39170 device_test_->AddDevice(id, type, std::string("/device/" + id));
[email protected]3c8bd112012-11-07 10:14:59171 }
172
173 void RemoveDevice(const std::string& id) {
[email protected]3c8bd112012-11-07 10:14:59174 device_test_->RemoveDevice(id);
175 }
176
177 void AddService(const std::string& type,
178 const std::string& id,
179 const std::string& state,
180 bool add_to_watch_list) {
181 ASSERT_TRUE(IsValidType(type));
[email protected]fde8b322013-02-28 17:44:24182 service_test_->AddService(id, id, type, state, add_to_watch_list);
[email protected]3c8bd112012-11-07 10:14:59183 }
184
185 void RemoveService(const std::string& id) {
[email protected]3c8bd112012-11-07 10:14:59186 service_test_->RemoveService(id);
187 }
188
189 // Call this after any initial Shill client setup
190 void SetupShillPropertyHandler() {
191 listener_.reset(new TestListener);
192 shill_property_handler_.reset(
193 new internal::ShillPropertyHandler(listener_.get()));
194 shill_property_handler_->Init();
195 }
196
197 bool IsValidType(const std::string& type) {
198 return (type == flimflam::kTypeEthernet ||
199 type == flimflam::kTypeWifi ||
200 type == flimflam::kTypeWimax ||
201 type == flimflam::kTypeBluetooth ||
202 type == flimflam::kTypeCellular ||
203 type == flimflam::kTypeVPN);
204 }
205
206 protected:
207 MessageLoopForUI message_loop_;
208 scoped_ptr<TestListener> listener_;
209 scoped_ptr<internal::ShillPropertyHandler> shill_property_handler_;
210 ShillManagerClient::TestInterface* manager_test_;
211 ShillDeviceClient::TestInterface* device_test_;
212 ShillServiceClient::TestInterface* service_test_;
213
214 private:
215 DISALLOW_COPY_AND_ASSIGN(ShillPropertyHandlerTest);
216};
217
218TEST_F(ShillPropertyHandlerTest, ShillPropertyHandlerStub) {
219 SetupShillPropertyHandler();
220 message_loop_.RunUntilIdle();
221 EXPECT_EQ(1, listener_->manager_updates());
222 // ShillManagerClient default stub entries are in shill_manager_client.cc.
223 // TODO(stevenjb): Eliminate default stub entries and add them explicitly.
[email protected]b16d79d72013-02-07 08:14:46224 EXPECT_TRUE(shill_property_handler_->TechnologyAvailable(
225 flimflam::kTypeWifi));
226 EXPECT_TRUE(shill_property_handler_->TechnologyEnabled(
227 flimflam::kTypeWifi));
[email protected]3c8bd112012-11-07 10:14:59228 const size_t kNumShillManagerClientStubImplDevices = 2;
229 EXPECT_EQ(kNumShillManagerClientStubImplDevices,
230 listener_->entries(flimflam::kDevicesProperty).size());
231 const size_t kNumShillManagerClientStubImplServices = 4;
232 EXPECT_EQ(kNumShillManagerClientStubImplServices,
233 listener_->entries(flimflam::kServicesProperty).size());
234
235 EXPECT_EQ(0, listener_->errors());
236}
237
238TEST_F(ShillPropertyHandlerTest, ShillPropertyHandlerTechnologyChanged) {
239 // This relies on the stub dbus implementations for ShillManagerClient,
240 SetupShillPropertyHandler();
241 message_loop_.RunUntilIdle();
242 EXPECT_EQ(1, listener_->manager_updates());
243 // Add a disabled technology.
244 manager_test_->AddTechnology(flimflam::kTypeWimax, false);
245 message_loop_.RunUntilIdle();
246 EXPECT_EQ(2, listener_->manager_updates());
[email protected]b16d79d72013-02-07 08:14:46247 EXPECT_TRUE(shill_property_handler_->TechnologyAvailable(
248 flimflam::kTypeWimax));
249 EXPECT_FALSE(shill_property_handler_->TechnologyEnabled(
250 flimflam::kTypeWimax));
251
[email protected]3c8bd112012-11-07 10:14:59252 // Enable the technology.
253 DBusThreadManager::Get()->GetShillManagerClient()->EnableTechnology(
254 flimflam::kTypeWimax,
255 base::Bind(&base::DoNothing), base::Bind(&ErrorCallbackFunction));
256 message_loop_.RunUntilIdle();
257 EXPECT_EQ(3, listener_->manager_updates());
[email protected]b16d79d72013-02-07 08:14:46258 EXPECT_TRUE(shill_property_handler_->TechnologyEnabled(
259 flimflam::kTypeWimax));
[email protected]3c8bd112012-11-07 10:14:59260
261 EXPECT_EQ(0, listener_->errors());
262}
263
264TEST_F(ShillPropertyHandlerTest, ShillPropertyHandlerDevicePropertyChanged) {
265 // This relies on the stub dbus implementations for ShillManagerClient,
266 SetupShillPropertyHandler();
267 message_loop_.RunUntilIdle();
268 EXPECT_EQ(1, listener_->manager_updates());
269 EXPECT_EQ(1, listener_->list_updates(flimflam::kDevicesProperty));
270 const size_t kNumShillManagerClientStubImplDevices = 2;
271 EXPECT_EQ(kNumShillManagerClientStubImplDevices,
272 listener_->entries(flimflam::kDevicesProperty).size());
273 // Add a device.
274 const std::string kTestDevicePath("test_wifi_device1");
275 AddDevice(flimflam::kTypeWifi, kTestDevicePath);
276 message_loop_.RunUntilIdle();
277 EXPECT_EQ(1, listener_->manager_updates()); // No new manager updates.
278 EXPECT_EQ(2, listener_->list_updates(flimflam::kDevicesProperty));
279 EXPECT_EQ(kNumShillManagerClientStubImplDevices + 1,
280 listener_->entries(flimflam::kDevicesProperty).size());
281 // Device changes are not observed.
282 // Remove a device
283 RemoveDevice(kTestDevicePath);
284 message_loop_.RunUntilIdle();
285 EXPECT_EQ(3, listener_->list_updates(flimflam::kDevicesProperty));
286 EXPECT_EQ(kNumShillManagerClientStubImplDevices,
287 listener_->entries(flimflam::kDevicesProperty).size());
288
289 EXPECT_EQ(0, listener_->errors());
290}
291
292TEST_F(ShillPropertyHandlerTest, ShillPropertyHandlerServicePropertyChanged) {
293 // This relies on the stub dbus implementations for ShillManagerClient,
294 SetupShillPropertyHandler();
295 message_loop_.RunUntilIdle();
296 EXPECT_EQ(1, listener_->manager_updates());
297 EXPECT_EQ(1, listener_->list_updates(flimflam::kServicesProperty));
298 const size_t kNumShillManagerClientStubImplServices = 4;
299 EXPECT_EQ(kNumShillManagerClientStubImplServices,
300 listener_->entries(flimflam::kServicesProperty).size());
301
302 // Add an unwatched service.
303 const std::string kTestServicePath("test_wifi_service1");
304 AddService(flimflam::kTypeWifi, kTestServicePath,
305 flimflam::kStateIdle, false);
306 message_loop_.RunUntilIdle();
307 EXPECT_EQ(1, listener_->manager_updates()); // No new manager updates.
[email protected]b16d79d72013-02-07 08:14:46308 // Only watched services trigger a service list update.
309 EXPECT_EQ(1, listener_->list_updates(flimflam::kServicesProperty));
[email protected]3c8bd112012-11-07 10:14:59310 EXPECT_EQ(kNumShillManagerClientStubImplServices + 1,
311 listener_->entries(flimflam::kServicesProperty).size());
312 // Change a property.
313 base::FundamentalValue scan_interval(3);
314 DBusThreadManager::Get()->GetShillServiceClient()->SetProperty(
315 dbus::ObjectPath(kTestServicePath),
316 flimflam::kScanIntervalProperty,
317 scan_interval,
318 base::Bind(&base::DoNothing), base::Bind(&ErrorCallbackFunction));
319 message_loop_.RunUntilIdle();
320 // Property change should NOT trigger an update.
321 EXPECT_EQ(0, listener_->
322 property_updates(flimflam::kServicesProperty)[kTestServicePath]);
323
324 // Add the existing service to the watch list.
325 AddService(flimflam::kTypeWifi, kTestServicePath,
326 flimflam::kStateIdle, true);
327 message_loop_.RunUntilIdle();
[email protected]fa6797e2012-11-28 15:15:44328 // Service list update should be received when watch list changes.
[email protected]b16d79d72013-02-07 08:14:46329 EXPECT_EQ(2, listener_->list_updates(flimflam::kServicesProperty));
[email protected]fa6797e2012-11-28 15:15:44330 // Number of services shouldn't change.
[email protected]3c8bd112012-11-07 10:14:59331 EXPECT_EQ(kNumShillManagerClientStubImplServices + 1,
332 listener_->entries(flimflam::kServicesProperty).size());
[email protected]fa6797e2012-11-28 15:15:44333 // Property update should be received when watched service is added.
334 EXPECT_EQ(1, listener_->
335 property_updates(flimflam::kServicesProperty)[kTestServicePath]);
336
[email protected]3c8bd112012-11-07 10:14:59337 // Change a property.
338 DBusThreadManager::Get()->GetShillServiceClient()->SetProperty(
339 dbus::ObjectPath(kTestServicePath),
340 flimflam::kScanIntervalProperty,
341 scan_interval,
342 base::Bind(&base::DoNothing), base::Bind(&ErrorCallbackFunction));
343 message_loop_.RunUntilIdle();
[email protected]fa6797e2012-11-28 15:15:44344 // Property change should trigger another update.
345 EXPECT_EQ(2, listener_->
[email protected]3c8bd112012-11-07 10:14:59346 property_updates(flimflam::kServicesProperty)[kTestServicePath]);
347
348 // Remove a service
349 RemoveService(kTestServicePath);
350 message_loop_.RunUntilIdle();
[email protected]b16d79d72013-02-07 08:14:46351 EXPECT_EQ(3, listener_->list_updates(flimflam::kServicesProperty));
[email protected]3c8bd112012-11-07 10:14:59352 EXPECT_EQ(kNumShillManagerClientStubImplServices,
353 listener_->entries(flimflam::kServicesProperty).size());
354
355 EXPECT_EQ(0, listener_->errors());
356}
357
358// TODO(stevenjb): Test IP Configs.
359
360} // namespace chromeos