network_state_handler_unittest.cc revision 46d4c2bc3267f3f028f39e7e311b0f89aba2e4fd
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/network_state_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_manager_client.h"
18#include "chromeos/dbus/shill_profile_client.h"
19#include "chromeos/dbus/shill_service_client.h"
20#include "chromeos/network/favorite_state.h"
21#include "chromeos/network/network_state.h"
22#include "chromeos/network/network_state_handler_observer.h"
23#include "dbus/object_path.h"
24#include "testing/gtest/include/gtest/gtest.h"
25#include "third_party/cros_system_api/dbus/service_constants.h"
26
27namespace {
28
29void ErrorCallbackFunction(const std::string& error_name,
30                           const std::string& error_message) {
31  LOG(ERROR) << "Shill Error: " << error_name << " : " << error_message;
32}
33
34const std::string kShillManagerClientStubDefaultService = "/service/eth1";
35const std::string kShillManagerClientStubDefaultWifi = "/service/wifi1";
36const std::string kShillManagerClientStubWifi2 = "/service/wifi2";
37const std::string kShillManagerClientStubCellular = "/service/cellular1";
38
39using chromeos::NetworkState;
40using chromeos::NetworkStateHandler;
41
42class TestObserver : public chromeos::NetworkStateHandlerObserver {
43 public:
44  explicit TestObserver(NetworkStateHandler* handler)
45      : handler_(handler),
46        device_list_changed_count_(0),
47        device_count_(0),
48        network_count_(0),
49        default_network_change_count_(0),
50        favorite_count_(0) {
51  }
52
53  virtual ~TestObserver() {
54  }
55
56  virtual void DeviceListChanged() OVERRIDE {
57    NetworkStateHandler::DeviceStateList devices;
58    handler_->GetDeviceList(&devices);
59    device_count_ = devices.size();
60    ++device_list_changed_count_;
61  }
62
63  virtual void NetworkListChanged() OVERRIDE {
64    NetworkStateHandler::NetworkStateList networks;
65    handler_->GetNetworkList(&networks);
66    network_count_ = networks.size();
67    if (network_count_ == 0) {
68      default_network_ = "";
69      default_network_connection_state_ = "";
70    }
71    NetworkStateHandler::FavoriteStateList favorites;
72    handler_->GetFavoriteList(&favorites);
73    favorite_count_ = favorites.size();
74  }
75
76  virtual void DefaultNetworkChanged(const NetworkState* network) OVERRIDE {
77    ++default_network_change_count_;
78    default_network_ = network ? network->path() : "";
79    default_network_connection_state_ =
80        network ? network->connection_state() : "";
81    DVLOG(1) << "DefaultNetworkChanged: " << default_network_
82             << " State: " << default_network_connection_state_;
83  }
84
85  virtual void NetworkConnectionStateChanged(
86      const NetworkState* network) OVERRIDE {
87    network_connection_state_[network->path()] = network->connection_state();
88    connection_state_changes_[network->path()]++;
89  }
90
91  virtual void NetworkPropertiesUpdated(const NetworkState* network) OVERRIDE {
92    DCHECK(network);
93    property_updates_[network->path()]++;
94  }
95
96  size_t device_list_changed_count() { return device_list_changed_count_; }
97  size_t device_count() { return device_count_; }
98  size_t network_count() { return network_count_; }
99  size_t default_network_change_count() {
100    return default_network_change_count_;
101  }
102  void reset_change_counts() {
103    DVLOG(1) << "=== RESET CHANGE COUNTS ===";
104    default_network_change_count_ = 0;
105    device_list_changed_count_ = 0;
106  }
107  std::string default_network() { return default_network_; }
108  std::string default_network_connection_state() {
109    return default_network_connection_state_;
110  }
111  size_t favorite_count() { return favorite_count_; }
112
113  int PropertyUpdatesForService(const std::string& service_path) {
114    return property_updates_[service_path];
115  }
116
117  int ConnectionStateChangesForService(const std::string& service_path) {
118    return connection_state_changes_[service_path];
119  }
120
121  std::string NetworkConnectionStateForService(
122      const std::string& service_path) {
123    return network_connection_state_[service_path];
124  }
125
126 private:
127  NetworkStateHandler* handler_;
128  size_t device_list_changed_count_;
129  size_t device_count_;
130  size_t network_count_;
131  size_t default_network_change_count_;
132  std::string default_network_;
133  std::string default_network_connection_state_;
134  size_t favorite_count_;
135  std::map<std::string, int> property_updates_;
136  std::map<std::string, int> connection_state_changes_;
137  std::map<std::string, std::string> network_connection_state_;
138
139  DISALLOW_COPY_AND_ASSIGN(TestObserver);
140};
141
142}  // namespace
143
144namespace chromeos {
145
146class NetworkStateHandlerTest : public testing::Test {
147 public:
148  NetworkStateHandlerTest()
149      : device_test_(NULL),
150        manager_test_(NULL),
151        profile_test_(NULL),
152        service_test_(NULL) {}
153  virtual ~NetworkStateHandlerTest() {}
154
155  virtual void SetUp() OVERRIDE {
156    // Initialize DBusThreadManager with a stub implementation.
157    DBusThreadManager::InitializeWithStub();
158    SetupDefaultShillState();
159    network_state_handler_.reset(new NetworkStateHandler);
160    test_observer_.reset(new TestObserver(network_state_handler_.get()));
161    network_state_handler_->AddObserver(test_observer_.get(), FROM_HERE);
162    network_state_handler_->InitShillPropertyHandler();
163    message_loop_.RunUntilIdle();
164    test_observer_->reset_change_counts();
165  }
166
167  virtual void TearDown() OVERRIDE {
168    network_state_handler_->RemoveObserver(test_observer_.get(), FROM_HERE);
169    test_observer_.reset();
170    network_state_handler_.reset();
171    DBusThreadManager::Shutdown();
172  }
173
174 protected:
175  void AddService(const std::string& service_path,
176                  const std::string& name,
177                  const std::string& type,
178                  const std::string& state) {
179    service_test_->AddService(service_path, name, type, state,
180                              true /* add_to_visible */);
181  }
182
183  void SetupDefaultShillState() {
184    message_loop_.RunUntilIdle();  // Process any pending updates
185    device_test_ =
186        DBusThreadManager::Get()->GetShillDeviceClient()->GetTestInterface();
187    ASSERT_TRUE(device_test_);
188    device_test_->ClearDevices();
189    device_test_->AddDevice(
190        "/device/stub_wifi_device1", shill::kTypeWifi, "stub_wifi_device1");
191    device_test_->AddDevice("/device/stub_cellular_device1",
192                            shill::kTypeCellular,
193                            "stub_cellular_device1");
194
195    manager_test_ =
196        DBusThreadManager::Get()->GetShillManagerClient()->GetTestInterface();
197    ASSERT_TRUE(manager_test_);
198
199    profile_test_ =
200        DBusThreadManager::Get()->GetShillProfileClient()->GetTestInterface();
201    ASSERT_TRUE(profile_test_);
202    profile_test_->ClearProfiles();
203
204    service_test_ =
205        DBusThreadManager::Get()->GetShillServiceClient()->GetTestInterface();
206    ASSERT_TRUE(service_test_);
207    service_test_->ClearServices();
208    AddService(kShillManagerClientStubDefaultService,
209               "eth1",
210               shill::kTypeEthernet,
211               shill::kStateOnline);
212    AddService(kShillManagerClientStubDefaultWifi,
213               "wifi1",
214               shill::kTypeWifi,
215               shill::kStateOnline);
216    AddService(kShillManagerClientStubWifi2,
217               "wifi2",
218               shill::kTypeWifi,
219               shill::kStateIdle);
220    AddService(kShillManagerClientStubCellular,
221               "cellular1",
222               shill::kTypeCellular,
223               shill::kStateIdle);
224  }
225
226  void UpdateManagerProperties() {
227    message_loop_.RunUntilIdle();
228    network_state_handler_->UpdateManagerProperties();
229    message_loop_.RunUntilIdle();
230  }
231
232  base::MessageLoopForUI message_loop_;
233  scoped_ptr<NetworkStateHandler> network_state_handler_;
234  scoped_ptr<TestObserver> test_observer_;
235  ShillDeviceClient::TestInterface* device_test_;
236  ShillManagerClient::TestInterface* manager_test_;
237  ShillProfileClient::TestInterface* profile_test_;
238  ShillServiceClient::TestInterface* service_test_;
239
240 private:
241  DISALLOW_COPY_AND_ASSIGN(NetworkStateHandlerTest);
242};
243
244TEST_F(NetworkStateHandlerTest, NetworkStateHandlerStub) {
245  // Ensure that the device and network list are the expected size.
246  const size_t kNumShillManagerClientStubImplDevices = 2;
247  EXPECT_EQ(kNumShillManagerClientStubImplDevices,
248            test_observer_->device_count());
249  const size_t kNumShillManagerClientStubImplServices = 4;
250  EXPECT_EQ(kNumShillManagerClientStubImplServices,
251            test_observer_->network_count());
252  // Ensure that the first stub network is the default network.
253  EXPECT_EQ(kShillManagerClientStubDefaultService,
254            test_observer_->default_network());
255  ASSERT_TRUE(network_state_handler_->DefaultNetwork());
256  EXPECT_EQ(kShillManagerClientStubDefaultService,
257            network_state_handler_->DefaultNetwork()->path());
258  EXPECT_EQ(kShillManagerClientStubDefaultService,
259            network_state_handler_->ConnectedNetworkByType(
260                NetworkTypePattern::Ethernet())->path());
261  EXPECT_EQ(kShillManagerClientStubDefaultWifi,
262            network_state_handler_->ConnectedNetworkByType(
263                NetworkTypePattern::WiFi())->path());
264  EXPECT_EQ(kShillManagerClientStubCellular,
265            network_state_handler_->FirstNetworkByType(
266                NetworkTypePattern::Mobile())->path());
267  EXPECT_EQ(
268      kShillManagerClientStubCellular,
269      network_state_handler_->FirstNetworkByType(NetworkTypePattern::Cellular())
270          ->path());
271  EXPECT_EQ(shill::kStateOnline,
272            test_observer_->default_network_connection_state());
273}
274
275TEST_F(NetworkStateHandlerTest, TechnologyChanged) {
276  // Disable a technology. Will immediately set the state to AVAILABLE and
277  // notify observers.
278  network_state_handler_->SetTechnologyEnabled(
279      NetworkTypePattern::WiFi(), false, network_handler::ErrorCallback());
280  EXPECT_EQ(1u, test_observer_->device_list_changed_count());
281  EXPECT_EQ(
282      NetworkStateHandler::TECHNOLOGY_AVAILABLE,
283      network_state_handler_->GetTechnologyState(NetworkTypePattern::WiFi()));
284
285  // Run the message loop. An additional notification will be received when
286  // Shill updates the enabled technologies. The state should remain AVAILABLE.
287  test_observer_->reset_change_counts();
288  message_loop_.RunUntilIdle();
289  EXPECT_EQ(1u, test_observer_->device_list_changed_count());
290  EXPECT_EQ(
291      NetworkStateHandler::TECHNOLOGY_AVAILABLE,
292      network_state_handler_->GetTechnologyState(NetworkTypePattern::WiFi()));
293
294  // Enable a technology. Will immediately set the state to ENABLING and
295  // notify observers.
296  test_observer_->reset_change_counts();
297  network_state_handler_->SetTechnologyEnabled(
298      NetworkTypePattern::WiFi(), true, network_handler::ErrorCallback());
299  EXPECT_EQ(1u, test_observer_->device_list_changed_count());
300  EXPECT_EQ(
301      NetworkStateHandler::TECHNOLOGY_ENABLING,
302      network_state_handler_->GetTechnologyState(NetworkTypePattern::WiFi()));
303
304  // Run the message loop. State should change to ENABLED.
305  test_observer_->reset_change_counts();
306  message_loop_.RunUntilIdle();
307  EXPECT_EQ(1u, test_observer_->device_list_changed_count());
308  EXPECT_EQ(
309      NetworkStateHandler::TECHNOLOGY_ENABLED,
310      network_state_handler_->GetTechnologyState(NetworkTypePattern::WiFi()));
311}
312
313TEST_F(NetworkStateHandlerTest, TechnologyState) {
314  manager_test_->RemoveTechnology(shill::kTypeWimax);
315  message_loop_.RunUntilIdle();
316  EXPECT_EQ(
317      NetworkStateHandler::TECHNOLOGY_UNAVAILABLE,
318      network_state_handler_->GetTechnologyState(NetworkTypePattern::Wimax()));
319
320  manager_test_->AddTechnology(shill::kTypeWimax, false);
321  message_loop_.RunUntilIdle();
322  EXPECT_EQ(
323      NetworkStateHandler::TECHNOLOGY_AVAILABLE,
324      network_state_handler_->GetTechnologyState(NetworkTypePattern::Wimax()));
325
326  manager_test_->SetTechnologyInitializing(shill::kTypeWimax, true);
327  message_loop_.RunUntilIdle();
328  EXPECT_EQ(
329      NetworkStateHandler::TECHNOLOGY_UNINITIALIZED,
330      network_state_handler_->GetTechnologyState(NetworkTypePattern::Wimax()));
331
332  manager_test_->SetTechnologyInitializing(shill::kTypeWimax, false);
333  network_state_handler_->SetTechnologyEnabled(
334      NetworkTypePattern::Wimax(), true, network_handler::ErrorCallback());
335  message_loop_.RunUntilIdle();
336  EXPECT_EQ(
337      NetworkStateHandler::TECHNOLOGY_ENABLED,
338      network_state_handler_->GetTechnologyState(NetworkTypePattern::Wimax()));
339
340  manager_test_->RemoveTechnology(shill::kTypeWimax);
341  message_loop_.RunUntilIdle();
342  EXPECT_EQ(
343      NetworkStateHandler::TECHNOLOGY_UNAVAILABLE,
344      network_state_handler_->GetTechnologyState(NetworkTypePattern::Wimax()));
345}
346
347TEST_F(NetworkStateHandlerTest, ServicePropertyChanged) {
348  // Set a service property.
349  const std::string eth1 = kShillManagerClientStubDefaultService;
350  const NetworkState* ethernet = network_state_handler_->GetNetworkState(eth1);
351  ASSERT_TRUE(ethernet);
352  EXPECT_EQ("", ethernet->security());
353  EXPECT_EQ(1, test_observer_->PropertyUpdatesForService(eth1));
354  base::StringValue security_value("TestSecurity");
355  DBusThreadManager::Get()->GetShillServiceClient()->SetProperty(
356      dbus::ObjectPath(eth1),
357      shill::kSecurityProperty, security_value,
358      base::Bind(&base::DoNothing), base::Bind(&ErrorCallbackFunction));
359  message_loop_.RunUntilIdle();
360  ethernet = network_state_handler_->GetNetworkState(eth1);
361  EXPECT_EQ("TestSecurity", ethernet->security());
362  EXPECT_EQ(2, test_observer_->PropertyUpdatesForService(eth1));
363
364  // Changing a service to the existing value should not trigger an update.
365  DBusThreadManager::Get()->GetShillServiceClient()->SetProperty(
366      dbus::ObjectPath(eth1),
367      shill::kSecurityProperty, security_value,
368      base::Bind(&base::DoNothing), base::Bind(&ErrorCallbackFunction));
369  message_loop_.RunUntilIdle();
370  EXPECT_EQ(2, test_observer_->PropertyUpdatesForService(eth1));
371}
372
373TEST_F(NetworkStateHandlerTest, GetState) {
374  const std::string profile = "/profile/profile1";
375  const std::string wifi_path = kShillManagerClientStubDefaultWifi;
376
377  // Add a wifi service to a Profile.
378  profile_test_->AddProfile(profile, "" /* userhash */);
379  EXPECT_TRUE(profile_test_->AddService(profile, wifi_path));
380  UpdateManagerProperties();
381
382  // Ensure that a NetworkState and corresponding FavoriteState exist.
383  const NetworkState* wifi_network =
384      network_state_handler_->GetNetworkState(wifi_path);
385  ASSERT_TRUE(wifi_network);
386  const FavoriteState* wifi_favorite =
387      network_state_handler_->GetFavoriteStateFromServicePath(
388          wifi_path, true /* configured_only */);
389  ASSERT_TRUE(wifi_favorite);
390  EXPECT_EQ(wifi_network->path(), wifi_favorite->path());
391
392  // Ensure that we are notified that a Favorite was added.
393  EXPECT_EQ(1u, test_observer_->favorite_count());
394
395  // Test looking up by GUID.
396  ASSERT_FALSE(wifi_favorite->guid().empty());
397  const FavoriteState* wifi_favorite_guid =
398      network_state_handler_->GetFavoriteStateFromGuid(wifi_favorite->guid());
399  EXPECT_EQ(wifi_favorite, wifi_favorite_guid);
400
401  // Remove the service, verify that there is no longer a NetworkState for it.
402  service_test_->RemoveService(wifi_path);
403  UpdateManagerProperties();
404  EXPECT_FALSE(network_state_handler_->GetNetworkState(wifi_path));
405}
406
407TEST_F(NetworkStateHandlerTest, NetworkConnectionStateChanged) {
408  // Change a network state.
409  const std::string eth1 = kShillManagerClientStubDefaultService;
410  base::StringValue connection_state_idle_value(shill::kStateIdle);
411  service_test_->SetServiceProperty(eth1, shill::kStateProperty,
412                                   connection_state_idle_value);
413  message_loop_.RunUntilIdle();
414  EXPECT_EQ(shill::kStateIdle,
415            test_observer_->NetworkConnectionStateForService(eth1));
416  EXPECT_EQ(2, test_observer_->ConnectionStateChangesForService(eth1));
417  // Confirm that changing the connection state to the same value does *not*
418  // signal the observer.
419  service_test_->SetServiceProperty(eth1, shill::kStateProperty,
420                                   connection_state_idle_value);
421  message_loop_.RunUntilIdle();
422  EXPECT_EQ(2, test_observer_->ConnectionStateChangesForService(eth1));
423}
424
425TEST_F(NetworkStateHandlerTest, DefaultServiceDisconnected) {
426  const std::string eth1 = kShillManagerClientStubDefaultService;
427  const std::string wifi1 = kShillManagerClientStubDefaultWifi;
428
429  // Disconnect ethernet.
430  base::StringValue connection_state_idle_value(shill::kStateIdle);
431  service_test_->SetServiceProperty(eth1, shill::kStateProperty,
432                                    connection_state_idle_value);
433  message_loop_.RunUntilIdle();
434  // Expect two changes: first when eth1 becomes disconnected, second when
435  // wifi1 becomes the default.
436  EXPECT_EQ(2u, test_observer_->default_network_change_count());
437  EXPECT_EQ(wifi1, test_observer_->default_network());
438
439  // Disconnect wifi.
440  test_observer_->reset_change_counts();
441  service_test_->SetServiceProperty(wifi1, shill::kStateProperty,
442                                    connection_state_idle_value);
443  message_loop_.RunUntilIdle();
444  EXPECT_EQ(1u, test_observer_->default_network_change_count());
445  EXPECT_EQ("", test_observer_->default_network());
446}
447
448TEST_F(NetworkStateHandlerTest, DefaultServiceConnected) {
449  const std::string eth1 = kShillManagerClientStubDefaultService;
450  const std::string wifi1 = kShillManagerClientStubDefaultWifi;
451
452  // Disconnect ethernet and wifi.
453  base::StringValue connection_state_idle_value(shill::kStateIdle);
454  service_test_->SetServiceProperty(eth1, shill::kStateProperty,
455                                    connection_state_idle_value);
456  service_test_->SetServiceProperty(wifi1, shill::kStateProperty,
457                                    connection_state_idle_value);
458  message_loop_.RunUntilIdle();
459  EXPECT_EQ(std::string(), test_observer_->default_network());
460
461  // Connect ethernet, should become the default network.
462  test_observer_->reset_change_counts();
463  base::StringValue connection_state_ready_value(shill::kStateReady);
464  service_test_->SetServiceProperty(eth1, shill::kStateProperty,
465                                    connection_state_ready_value);
466  message_loop_.RunUntilIdle();
467  EXPECT_EQ(eth1, test_observer_->default_network());
468  EXPECT_EQ(shill::kStateReady,
469            test_observer_->default_network_connection_state());
470  EXPECT_EQ(1u, test_observer_->default_network_change_count());
471}
472
473TEST_F(NetworkStateHandlerTest, DefaultServiceChanged) {
474  const std::string eth1 = kShillManagerClientStubDefaultService;
475  // The default service should be eth1.
476  EXPECT_EQ(eth1, test_observer_->default_network());
477
478  // Change the default network by changing Manager.DefaultService.
479  const std::string wifi1 = kShillManagerClientStubDefaultWifi;
480  base::StringValue wifi1_value(wifi1);
481  manager_test_->SetManagerProperty(
482      shill::kDefaultServiceProperty, wifi1_value);
483  message_loop_.RunUntilIdle();
484  EXPECT_EQ(wifi1, test_observer_->default_network());
485  EXPECT_EQ(1u, test_observer_->default_network_change_count());
486
487  // Change the state of the default network.
488  test_observer_->reset_change_counts();
489  base::StringValue connection_state_ready_value(shill::kStateReady);
490  service_test_->SetServiceProperty(wifi1, shill::kStateProperty,
491                                   connection_state_ready_value);
492  message_loop_.RunUntilIdle();
493  EXPECT_EQ(shill::kStateReady,
494            test_observer_->default_network_connection_state());
495  EXPECT_EQ(1u, test_observer_->default_network_change_count());
496
497  // Updating a property on the default network should trigger
498  // a default network change.
499  test_observer_->reset_change_counts();
500  DBusThreadManager::Get()->GetShillServiceClient()->SetProperty(
501      dbus::ObjectPath(wifi1),
502      shill::kSecurityProperty, base::StringValue("TestSecurity"),
503      base::Bind(&base::DoNothing), base::Bind(&ErrorCallbackFunction));
504  message_loop_.RunUntilIdle();
505  EXPECT_EQ(1u, test_observer_->default_network_change_count());
506
507  // No default network updates for signal strength changes.
508  test_observer_->reset_change_counts();
509  DBusThreadManager::Get()->GetShillServiceClient()->SetProperty(
510      dbus::ObjectPath(wifi1),
511      shill::kSignalStrengthProperty, base::FundamentalValue(32),
512      base::Bind(&base::DoNothing), base::Bind(&ErrorCallbackFunction));
513  message_loop_.RunUntilIdle();
514  EXPECT_EQ(0u, test_observer_->default_network_change_count());
515}
516
517TEST_F(NetworkStateHandlerTest, RequestUpdate) {
518  // Request an update for kShillManagerClientStubDefaultWifi.
519  EXPECT_EQ(1, test_observer_->PropertyUpdatesForService(
520      kShillManagerClientStubDefaultWifi));
521  network_state_handler_->RequestUpdateForNetwork(
522      kShillManagerClientStubDefaultWifi);
523  message_loop_.RunUntilIdle();
524  EXPECT_EQ(2, test_observer_->PropertyUpdatesForService(
525      kShillManagerClientStubDefaultWifi));
526}
527
528TEST_F(NetworkStateHandlerTest, NetworkGuidInProfile) {
529  const std::string profile = "/profile/profile1";
530  const std::string wifi_path = "/service/wifi_with_guid";
531  const std::string wifi_guid = "WIFI_GUID";
532  const bool is_service_configured = true;
533
534  // Add a network to the default Profile with a specified GUID.
535  service_test_->AddServiceWithIPConfig(
536      wifi_path,
537      wifi_guid,
538      wifi_path  /* name */,
539      shill::kTypeWifi,
540      shill::kStateOnline,
541      "" /* ipconfig_path */,
542      true /* add_to_visible */);
543  profile_test_->AddProfile(profile, "" /* userhash */);
544  EXPECT_TRUE(profile_test_->AddService(profile, wifi_path));
545  UpdateManagerProperties();
546
547  // Verify that a FavoriteState exists with a matching GUID.
548  const FavoriteState* favorite =
549      network_state_handler_->GetFavoriteStateFromServicePath(
550          wifi_path, is_service_configured);
551  ASSERT_TRUE(favorite);
552  EXPECT_EQ(wifi_guid, favorite->guid());
553
554  // Verify that a NetworkState exists with the same GUID.
555  const NetworkState* network =
556      network_state_handler_->GetNetworkState(wifi_path);
557  ASSERT_TRUE(network);
558  EXPECT_EQ(wifi_guid, network->guid());
559
560  // Remove the service (simulating a network going out of range).
561  service_test_->RemoveService(wifi_path);
562  UpdateManagerProperties();
563  EXPECT_FALSE(network_state_handler_->GetNetworkState(wifi_path));
564
565  // Add the service (simulating a network coming back in range) and verify that
566  // the NetworkState was created with the same GUID.
567  AddService(wifi_path, wifi_path, shill::kTypeWifi, shill::kStateOnline);
568  UpdateManagerProperties();
569  network = network_state_handler_->GetNetworkState(wifi_path);
570  ASSERT_TRUE(network);
571  EXPECT_EQ(wifi_guid, network->guid());
572
573  // Also verify FavoriteState (mostly to test the stub behavior).
574  favorite = network_state_handler_->GetFavoriteStateFromServicePath(
575      wifi_path, is_service_configured);
576  ASSERT_TRUE(favorite);
577  EXPECT_EQ(wifi_guid, favorite->guid());
578}
579
580TEST_F(NetworkStateHandlerTest, NetworkGuidNotInProfile) {
581  const std::string wifi_path = "/service/wifi_with_guid";
582  const bool is_service_configured = false;
583
584  // Add a network without adding it to a profile.
585  AddService(wifi_path, wifi_path, shill::kTypeWifi, shill::kStateOnline);
586  UpdateManagerProperties();
587
588  // Verify that a FavoriteState exists with an assigned GUID.
589  const FavoriteState* favorite =
590      network_state_handler_->GetFavoriteStateFromServicePath(
591          wifi_path, is_service_configured);
592  ASSERT_TRUE(favorite);
593  std::string wifi_guid = favorite->guid();
594  EXPECT_FALSE(wifi_guid.empty());
595
596  // Verify that a NetworkState exists with the same GUID.
597  const NetworkState* network =
598      network_state_handler_->GetNetworkState(wifi_path);
599  ASSERT_TRUE(network);
600  EXPECT_EQ(wifi_guid, network->guid());
601
602  // Remove the service (simulating a network going out of range).
603  service_test_->RemoveService(wifi_path);
604  UpdateManagerProperties();
605  EXPECT_FALSE(network_state_handler_->GetNetworkState(wifi_path));
606
607  // Add the service (simulating a network coming back in range) and verify that
608  // the NetworkState was created with the same GUID.
609  AddService(wifi_path, wifi_path, shill::kTypeWifi, shill::kStateOnline);
610  UpdateManagerProperties();
611  network = network_state_handler_->GetNetworkState(wifi_path);
612  ASSERT_TRUE(network);
613  EXPECT_EQ(wifi_guid, network->guid());
614
615  // Also verify FavoriteState (mostly to test the stub behavior).
616  favorite = network_state_handler_->GetFavoriteStateFromServicePath(
617      wifi_path, is_service_configured);
618  ASSERT_TRUE(favorite);
619  EXPECT_EQ(wifi_guid, favorite->guid());
620}
621
622}  // namespace chromeos
623