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