Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(319)

Side by Side Diff: chromeos/network/shill_property_handler_unittest.cc

Issue 12634019: NetworkChangeNotifierChromeos: Handle IPConfig property changes on the default network (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: AddServiceWithIPConfig Created 7 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "chromeos/network/shill_property_handler.h" 5 #include "chromeos/network/shill_property_handler.h"
6 6
7 #include <map> 7 #include <map>
8 #include <set> 8 #include <set>
9 #include <string> 9 #include <string>
10 10
11 #include "base/bind.h" 11 #include "base/bind.h"
12 #include "base/memory/scoped_ptr.h" 12 #include "base/memory/scoped_ptr.h"
13 #include "base/message_loop.h" 13 #include "base/message_loop.h"
14 #include "base/values.h" 14 #include "base/values.h"
15 #include "chromeos/dbus/dbus_thread_manager.h" 15 #include "chromeos/dbus/dbus_thread_manager.h"
16 #include "chromeos/dbus/shill_device_client.h" 16 #include "chromeos/dbus/shill_device_client.h"
17 #include "chromeos/dbus/shill_ipconfig_client.h"
17 #include "chromeos/dbus/shill_manager_client.h" 18 #include "chromeos/dbus/shill_manager_client.h"
18 #include "chromeos/dbus/shill_service_client.h" 19 #include "chromeos/dbus/shill_service_client.h"
19 #include "dbus/object_path.h" 20 #include "dbus/object_path.h"
20 #include "testing/gtest/include/gtest/gtest.h" 21 #include "testing/gtest/include/gtest/gtest.h"
21 #include "third_party/cros_system_api/dbus/service_constants.h" 22 #include "third_party/cros_system_api/dbus/service_constants.h"
22 23
23 namespace chromeos { 24 namespace chromeos {
24 25
25 namespace { 26 namespace {
26 27
28 void DoNothingWithCallStatus(DBusMethodCallStatus call_status) {
29 }
30
27 void ErrorCallbackFunction(const std::string& error_name, 31 void ErrorCallbackFunction(const std::string& error_name,
28 const std::string& error_message) { 32 const std::string& error_message) {
29 LOG(ERROR) << "Shill Error: " << error_name << " : " << error_message; 33 LOG(ERROR) << "Shill Error: " << error_name << " : " << error_message;
30 } 34 }
31 35
32 class TestListener : public internal::ShillPropertyHandler::Listener { 36 class TestListener : public internal::ShillPropertyHandler::Listener {
33 public: 37 public:
34 TestListener() : manager_updates_(0), errors_(0) { 38 TestListener() : manager_updates_(0), errors_(0) {
35 } 39 }
36 40
(...skipping 20 matching lines...) Expand all
57 const std::string& device_path, 61 const std::string& device_path,
58 const std::string& key, 62 const std::string& key,
59 const base::Value& value) OVERRIDE { 63 const base::Value& value) OVERRIDE {
60 AddPropertyUpdate(flimflam::kDevicesProperty, device_path); 64 AddPropertyUpdate(flimflam::kDevicesProperty, device_path);
61 } 65 }
62 66
63 virtual void ManagerPropertyChanged() OVERRIDE { 67 virtual void ManagerPropertyChanged() OVERRIDE {
64 ++manager_updates_; 68 ++manager_updates_;
65 } 69 }
66 70
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( 71 virtual void ManagedStateListChanged(
74 ManagedState::ManagedType type) OVERRIDE { 72 ManagedState::ManagedType type) OVERRIDE {
75 AddStateListUpdate(GetTypeString(type)); 73 AddStateListUpdate(GetTypeString(type));
76 } 74 }
77 75
78 std::vector<std::string>& entries(const std::string& type) { 76 std::vector<std::string>& entries(const std::string& type) {
pneubeck (no reviews) 2013/03/28 10:37:54 change to the number of the entries: ... return
79 return entries_[type]; 77 return entries_[type];
80 } 78 }
81 std::map<std::string, int>& property_updates(const std::string& type) { 79 std::map<std::string, int>& property_updates(const std::string& type) {
pneubeck (no reviews) 2013/03/28 10:37:54 add another argument for the nested key (path).
82 return property_updates_[type]; 80 return property_updates_[type];
83 } 81 }
84 int list_updates(const std::string& type) { return list_updates_[type]; } 82 int list_updates(const std::string& type) { return list_updates_[type]; }
85 int manager_updates() { return manager_updates_; } 83 int manager_updates() { return manager_updates_; }
86 int errors() { return errors_; } 84 int errors() { return errors_; }
87 85
88 private: 86 private:
89 std::string GetTypeString(ManagedState::ManagedType type) { 87 std::string GetTypeString(ManagedState::ManagedType type) {
90 if (type == ManagedState::MANAGED_TYPE_NETWORK) { 88 if (type == ManagedState::MANAGED_TYPE_NETWORK) {
91 return flimflam::kServicesProperty; 89 return flimflam::kServicesProperty;
(...skipping 13 matching lines...) Expand all
105 iter != entries.end(); ++iter) { 103 iter != entries.end(); ++iter) {
106 std::string path; 104 std::string path;
107 if ((*iter)->GetAsString(&path)) 105 if ((*iter)->GetAsString(&path))
108 entries_[type].push_back(path); 106 entries_[type].push_back(path);
109 } 107 }
110 } 108 }
111 109
112 void AddPropertyUpdate(const std::string& type, const std::string& path) { 110 void AddPropertyUpdate(const std::string& type, const std::string& path) {
113 if (type.empty()) 111 if (type.empty())
114 return; 112 return;
115 property_updates(type)[path] += 1; 113 property_updates(type)[path] += 1;
pneubeck (no reviews) 2013/03/28 10:37:54 should be property_updates(type, path) += 1
116 } 114 }
117 115
118 void AddStateListUpdate(const std::string& type) { 116 void AddStateListUpdate(const std::string& type) {
119 if (type.empty()) 117 if (type.empty())
120 return; 118 return;
121 list_updates_[type] += 1; 119 list_updates_[type] += 1;
122 } 120 }
123 121
124 // Map of list-type -> paths 122 // Map of list-type -> paths
125 std::map<std::string, std::vector<std::string> > entries_; 123 std::map<std::string, std::vector<std::string> > entries_;
(...skipping 24 matching lines...) Expand all
150 // default stub properties. 148 // default stub properties.
151 manager_test_ = 149 manager_test_ =
152 DBusThreadManager::Get()->GetShillManagerClient()->GetTestInterface(); 150 DBusThreadManager::Get()->GetShillManagerClient()->GetTestInterface();
153 ASSERT_TRUE(manager_test_); 151 ASSERT_TRUE(manager_test_);
154 device_test_ = 152 device_test_ =
155 DBusThreadManager::Get()->GetShillDeviceClient()->GetTestInterface(); 153 DBusThreadManager::Get()->GetShillDeviceClient()->GetTestInterface();
156 ASSERT_TRUE(device_test_); 154 ASSERT_TRUE(device_test_);
157 service_test_ = 155 service_test_ =
158 DBusThreadManager::Get()->GetShillServiceClient()->GetTestInterface(); 156 DBusThreadManager::Get()->GetShillServiceClient()->GetTestInterface();
159 ASSERT_TRUE(service_test_); 157 ASSERT_TRUE(service_test_);
158 SetupShillPropertyHandler();
159 message_loop_.RunUntilIdle();
160 } 160 }
161 161
162 virtual void TearDown() OVERRIDE { 162 virtual void TearDown() OVERRIDE {
163 shill_property_handler_.reset(); 163 shill_property_handler_.reset();
164 listener_.reset(); 164 listener_.reset();
165 DBusThreadManager::Shutdown(); 165 DBusThreadManager::Shutdown();
166 } 166 }
167 167
168 void AddDevice(const std::string& type, const std::string& id) { 168 void AddDevice(const std::string& type, const std::string& id) {
169 ASSERT_TRUE(IsValidType(type)); 169 ASSERT_TRUE(IsValidType(type));
170 device_test_->AddDevice(id, type, std::string("/device/" + id)); 170 device_test_->AddDevice(id, type, std::string("/device/" + id));
171 } 171 }
172 172
173 void RemoveDevice(const std::string& id) { 173 void RemoveDevice(const std::string& id) {
174 device_test_->RemoveDevice(id); 174 device_test_->RemoveDevice(id);
175 } 175 }
176 176
177 void AddService(const std::string& type, 177 void AddService(const std::string& type,
178 const std::string& id, 178 const std::string& id,
179 const std::string& state, 179 const std::string& state,
180 bool add_to_watch_list) { 180 bool add_to_watch_list) {
181 ASSERT_TRUE(IsValidType(type)); 181 ASSERT_TRUE(IsValidType(type));
182 service_test_->AddService(id, id, type, state, add_to_watch_list); 182 service_test_->AddService(id, id, type, state,
183 add_to_watch_list);
184 }
185
186 void AddServiceWithIPConfig(const std::string& type,
187 const std::string& id,
188 const std::string& state,
189 const std::string& ipconfig_path,
190 bool add_to_watch_list) {
191 ASSERT_TRUE(IsValidType(type));
192 service_test_->AddServiceWithIPConfig(id, id, type, state,
193 ipconfig_path, add_to_watch_list);
183 } 194 }
184 195
185 void RemoveService(const std::string& id) { 196 void RemoveService(const std::string& id) {
186 service_test_->RemoveService(id); 197 service_test_->RemoveService(id);
187 } 198 }
188 199
189 // Call this after any initial Shill client setup 200 // Call this after any initial Shill client setup
190 void SetupShillPropertyHandler() { 201 void SetupShillPropertyHandler() {
191 SetupDefaultShillState(); 202 SetupDefaultShillState();
192 listener_.reset(new TestListener); 203 listener_.reset(new TestListener);
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after
227 scoped_ptr<internal::ShillPropertyHandler> shill_property_handler_; 238 scoped_ptr<internal::ShillPropertyHandler> shill_property_handler_;
228 ShillManagerClient::TestInterface* manager_test_; 239 ShillManagerClient::TestInterface* manager_test_;
229 ShillDeviceClient::TestInterface* device_test_; 240 ShillDeviceClient::TestInterface* device_test_;
230 ShillServiceClient::TestInterface* service_test_; 241 ShillServiceClient::TestInterface* service_test_;
231 242
232 private: 243 private:
233 DISALLOW_COPY_AND_ASSIGN(ShillPropertyHandlerTest); 244 DISALLOW_COPY_AND_ASSIGN(ShillPropertyHandlerTest);
234 }; 245 };
235 246
236 TEST_F(ShillPropertyHandlerTest, ShillPropertyHandlerStub) { 247 TEST_F(ShillPropertyHandlerTest, ShillPropertyHandlerStub) {
237 SetupShillPropertyHandler();
238 message_loop_.RunUntilIdle();
239 EXPECT_EQ(1, listener_->manager_updates()); 248 EXPECT_EQ(1, listener_->manager_updates());
240 EXPECT_TRUE(shill_property_handler_->TechnologyAvailable( 249 EXPECT_TRUE(shill_property_handler_->TechnologyAvailable(
241 flimflam::kTypeWifi)); 250 flimflam::kTypeWifi));
242 EXPECT_TRUE(shill_property_handler_->TechnologyEnabled( 251 EXPECT_TRUE(shill_property_handler_->TechnologyEnabled(
243 flimflam::kTypeWifi)); 252 flimflam::kTypeWifi));
244 const size_t kNumShillManagerClientStubImplDevices = 2; 253 const size_t kNumShillManagerClientStubImplDevices = 2;
245 EXPECT_EQ(kNumShillManagerClientStubImplDevices, 254 EXPECT_EQ(kNumShillManagerClientStubImplDevices,
246 listener_->entries(flimflam::kDevicesProperty).size()); 255 listener_->entries(flimflam::kDevicesProperty).size());
247 const size_t kNumShillManagerClientStubImplServices = 4; 256 const size_t kNumShillManagerClientStubImplServices = 4;
248 EXPECT_EQ(kNumShillManagerClientStubImplServices, 257 EXPECT_EQ(kNumShillManagerClientStubImplServices,
249 listener_->entries(flimflam::kServicesProperty).size()); 258 listener_->entries(flimflam::kServicesProperty).size());
250 259
251 EXPECT_EQ(0, listener_->errors()); 260 EXPECT_EQ(0, listener_->errors());
252 } 261 }
253 262
254 TEST_F(ShillPropertyHandlerTest, ShillPropertyHandlerTechnologyChanged) { 263 TEST_F(ShillPropertyHandlerTest, ShillPropertyHandlerTechnologyChanged) {
255 SetupShillPropertyHandler();
256 message_loop_.RunUntilIdle();
257 EXPECT_EQ(1, listener_->manager_updates()); 264 EXPECT_EQ(1, listener_->manager_updates());
258 // Add a disabled technology. 265 // Add a disabled technology.
259 manager_test_->AddTechnology(flimflam::kTypeWimax, false); 266 manager_test_->AddTechnology(flimflam::kTypeWimax, false);
260 message_loop_.RunUntilIdle(); 267 message_loop_.RunUntilIdle();
261 EXPECT_EQ(2, listener_->manager_updates()); 268 EXPECT_EQ(2, listener_->manager_updates());
262 EXPECT_TRUE(shill_property_handler_->TechnologyAvailable( 269 EXPECT_TRUE(shill_property_handler_->TechnologyAvailable(
263 flimflam::kTypeWimax)); 270 flimflam::kTypeWimax));
264 EXPECT_FALSE(shill_property_handler_->TechnologyEnabled( 271 EXPECT_FALSE(shill_property_handler_->TechnologyEnabled(
265 flimflam::kTypeWimax)); 272 flimflam::kTypeWimax));
266 273
267 // Enable the technology. 274 // Enable the technology.
268 DBusThreadManager::Get()->GetShillManagerClient()->EnableTechnology( 275 DBusThreadManager::Get()->GetShillManagerClient()->EnableTechnology(
269 flimflam::kTypeWimax, 276 flimflam::kTypeWimax,
270 base::Bind(&base::DoNothing), base::Bind(&ErrorCallbackFunction)); 277 base::Bind(&base::DoNothing), base::Bind(&ErrorCallbackFunction));
271 message_loop_.RunUntilIdle(); 278 message_loop_.RunUntilIdle();
272 EXPECT_EQ(3, listener_->manager_updates()); 279 EXPECT_EQ(3, listener_->manager_updates());
273 EXPECT_TRUE(shill_property_handler_->TechnologyEnabled( 280 EXPECT_TRUE(shill_property_handler_->TechnologyEnabled(
274 flimflam::kTypeWimax)); 281 flimflam::kTypeWimax));
275 282
276 EXPECT_EQ(0, listener_->errors()); 283 EXPECT_EQ(0, listener_->errors());
277 } 284 }
278 285
279 TEST_F(ShillPropertyHandlerTest, ShillPropertyHandlerDevicePropertyChanged) { 286 TEST_F(ShillPropertyHandlerTest, ShillPropertyHandlerDevicePropertyChanged) {
280 SetupShillPropertyHandler();
281 message_loop_.RunUntilIdle();
282 EXPECT_EQ(1, listener_->manager_updates()); 287 EXPECT_EQ(1, listener_->manager_updates());
283 EXPECT_EQ(1, listener_->list_updates(flimflam::kDevicesProperty)); 288 EXPECT_EQ(1, listener_->list_updates(flimflam::kDevicesProperty));
284 const size_t kNumShillManagerClientStubImplDevices = 2; 289 const size_t kNumShillManagerClientStubImplDevices = 2;
285 EXPECT_EQ(kNumShillManagerClientStubImplDevices, 290 EXPECT_EQ(kNumShillManagerClientStubImplDevices,
286 listener_->entries(flimflam::kDevicesProperty).size()); 291 listener_->entries(flimflam::kDevicesProperty).size());
287 // Add a device. 292 // Add a device.
288 const std::string kTestDevicePath("test_wifi_device1"); 293 const std::string kTestDevicePath("test_wifi_device1");
289 AddDevice(flimflam::kTypeWifi, kTestDevicePath); 294 AddDevice(flimflam::kTypeWifi, kTestDevicePath);
290 message_loop_.RunUntilIdle(); 295 message_loop_.RunUntilIdle();
291 EXPECT_EQ(1, listener_->manager_updates()); // No new manager updates. 296 EXPECT_EQ(1, listener_->manager_updates()); // No new manager updates.
292 EXPECT_EQ(2, listener_->list_updates(flimflam::kDevicesProperty)); 297 EXPECT_EQ(2, listener_->list_updates(flimflam::kDevicesProperty));
293 EXPECT_EQ(kNumShillManagerClientStubImplDevices + 1, 298 EXPECT_EQ(kNumShillManagerClientStubImplDevices + 1,
294 listener_->entries(flimflam::kDevicesProperty).size()); 299 listener_->entries(flimflam::kDevicesProperty).size());
295 // Device changes are not observed. 300 // Device changes are not observed.
296 // Remove a device 301 // Remove a device
297 RemoveDevice(kTestDevicePath); 302 RemoveDevice(kTestDevicePath);
298 message_loop_.RunUntilIdle(); 303 message_loop_.RunUntilIdle();
299 EXPECT_EQ(3, listener_->list_updates(flimflam::kDevicesProperty)); 304 EXPECT_EQ(3, listener_->list_updates(flimflam::kDevicesProperty));
300 EXPECT_EQ(kNumShillManagerClientStubImplDevices, 305 EXPECT_EQ(kNumShillManagerClientStubImplDevices,
301 listener_->entries(flimflam::kDevicesProperty).size()); 306 listener_->entries(flimflam::kDevicesProperty).size());
302 307
303 EXPECT_EQ(0, listener_->errors()); 308 EXPECT_EQ(0, listener_->errors());
304 } 309 }
305 310
306 TEST_F(ShillPropertyHandlerTest, ShillPropertyHandlerServicePropertyChanged) { 311 TEST_F(ShillPropertyHandlerTest, ShillPropertyHandlerServicePropertyChanged) {
307 SetupShillPropertyHandler();
308 message_loop_.RunUntilIdle();
309 EXPECT_EQ(1, listener_->manager_updates()); 312 EXPECT_EQ(1, listener_->manager_updates());
310 EXPECT_EQ(1, listener_->list_updates(flimflam::kServicesProperty)); 313 EXPECT_EQ(1, listener_->list_updates(flimflam::kServicesProperty));
311 const size_t kNumShillManagerClientStubImplServices = 4; 314 const size_t kNumShillManagerClientStubImplServices = 4;
312 EXPECT_EQ(kNumShillManagerClientStubImplServices, 315 EXPECT_EQ(kNumShillManagerClientStubImplServices,
313 listener_->entries(flimflam::kServicesProperty).size()); 316 listener_->entries(flimflam::kServicesProperty).size());
314 317
315 // Add an unwatched service. 318 // Add an unwatched service.
316 const std::string kTestServicePath("test_wifi_service1"); 319 const std::string kTestServicePath("test_wifi_service1");
317 AddService(flimflam::kTypeWifi, kTestServicePath, 320 AddService(flimflam::kTypeWifi, kTestServicePath,
318 flimflam::kStateIdle, false); 321 flimflam::kStateIdle, false);
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after
361 // Remove a service 364 // Remove a service
362 RemoveService(kTestServicePath); 365 RemoveService(kTestServicePath);
363 message_loop_.RunUntilIdle(); 366 message_loop_.RunUntilIdle();
364 EXPECT_EQ(3, listener_->list_updates(flimflam::kServicesProperty)); 367 EXPECT_EQ(3, listener_->list_updates(flimflam::kServicesProperty));
365 EXPECT_EQ(kNumShillManagerClientStubImplServices, 368 EXPECT_EQ(kNumShillManagerClientStubImplServices,
366 listener_->entries(flimflam::kServicesProperty).size()); 369 listener_->entries(flimflam::kServicesProperty).size());
367 370
368 EXPECT_EQ(0, listener_->errors()); 371 EXPECT_EQ(0, listener_->errors());
369 } 372 }
370 373
371 // TODO(stevenjb): Test IP Configs. 374 TEST_F(ShillPropertyHandlerTest, ShillPropertyHandlerIPConfigPropertyChanged) {
375 // Set the properties for an IP Config object.
376 const std::string kTestIPConfigPath("test_ip_config_path");
377 base::StringValue ip_address("192.168.1.1");
378 DBusThreadManager::Get()->GetShillIPConfigClient()->SetProperty(
379 dbus::ObjectPath(kTestIPConfigPath),
380 flimflam::kAddressProperty,
381 ip_address,
382 base::Bind(&DoNothingWithCallStatus));
383 base::ListValue dns_servers;
384 dns_servers.Append(base::Value::CreateStringValue("192.168.1.100"));
385 dns_servers.Append(base::Value::CreateStringValue("192.168.1.101"));
386 DBusThreadManager::Get()->GetShillIPConfigClient()->SetProperty(
387 dbus::ObjectPath(kTestIPConfigPath),
388 flimflam::kNameServersProperty,
389 dns_servers,
390 base::Bind(&DoNothingWithCallStatus));
391 message_loop_.RunUntilIdle();
392
393 // Add a service with an empty ipconfig and then update
394 // its ipconfig property.
395 const std::string kTestServicePath1("test_wifi_service1");
396 AddService(flimflam::kTypeWifi, kTestServicePath1,
397 flimflam::kStateIdle, true);
398 message_loop_.RunUntilIdle();
399 // This is the initial property update.
400 EXPECT_EQ(1, listener_->
401 property_updates(flimflam::kServicesProperty)[kTestServicePath1]);
402 DBusThreadManager::Get()->GetShillServiceClient()->SetProperty(
403 dbus::ObjectPath(kTestServicePath1),
404 shill::kIPConfigProperty,
405 base::StringValue(kTestIPConfigPath),
406 base::Bind(&base::DoNothing), base::Bind(&ErrorCallbackFunction));
407 message_loop_.RunUntilIdle();
408 // IPConfig property change on the service should trigger property updates for
409 // IP Address and DNS.
410 EXPECT_EQ(3, listener_->
411 property_updates(flimflam::kServicesProperty)[kTestServicePath1]);
412
413 // Now, Add a new watched service with the IPConfig already set.
414 const std::string kTestServicePath2("test_wifi_service2");
415 AddServiceWithIPConfig(flimflam::kTypeWifi, kTestServicePath2,
416 flimflam::kStateIdle, kTestIPConfigPath, true);
417 message_loop_.RunUntilIdle();
418 // A watched service with the IPConfig property already set must
419 // trigger property updates for IP Address and DNS when added.
420 EXPECT_EQ(3, listener_->
421 property_updates(flimflam::kServicesProperty)[kTestServicePath2]);
422 }
372 423
373 } // namespace chromeos 424 } // namespace chromeos
OLDNEW
« chromeos/network/shill_property_handler.cc ('K') | « chromeos/network/shill_property_handler.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698