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_change_notifier_chromeos.h" 6 7 #include <string> 8 9 #include "base/basictypes.h" 10 #include "base/strings/string_split.h" 11 #include "chromeos/network/network_change_notifier_factory_chromeos.h" 12 #include "chromeos/network/network_state.h" 13 #include "net/base/network_change_notifier.h" 14 #include "testing/gtest/include/gtest/gtest.h" 15 #include "third_party/cros_system_api/dbus/service_constants.h" 16 17 namespace chromeos { 18 19 namespace { 20 21 const char kDnsServers1[] = "192.168.0.1,192.168.0.2"; 22 const char kDnsServers2[] = "192.168.3.1,192.168.3.2"; 23 const char kIpAddress1[] = "192.168.1.1"; 24 const char kIpAddress2[] = "192.168.1.2"; 25 const char kService1[] = "/service/1"; 26 const char kService2[] = "/service/2"; 27 const char kService3[] = "/service/3"; 28 29 struct NotifierState { 30 net::NetworkChangeNotifier::ConnectionType type; 31 const char* service_path; 32 const char* ip_address; 33 const char* dns_servers; 34 }; 35 36 struct DefaultNetworkState { 37 bool is_connected; 38 const char* type; 39 const char* network_technology; 40 const char* service_path; 41 const char* ip_address; 42 const char* dns_servers; 43 }; 44 45 struct NotifierUpdateTestCase { 46 const char* test_description; 47 NotifierState initial_state; 48 DefaultNetworkState default_network_state; 49 NotifierState expected_state; 50 bool expected_type_changed; 51 bool expected_ip_changed; 52 bool expected_dns_changed; 53 }; 54 55 } // namespace 56 57 using net::NetworkChangeNotifier; 58 59 TEST(NetworkChangeNotifierChromeosTest, ConnectionTypeFromShill) { 60 struct TypeMapping { 61 const char* shill_type; 62 const char* technology; 63 NetworkChangeNotifier::ConnectionType connection_type; 64 }; 65 TypeMapping type_mappings[] = { 66 { flimflam::kTypeEthernet, "", NetworkChangeNotifier::CONNECTION_ETHERNET }, 67 { flimflam::kTypeWifi, "", NetworkChangeNotifier::CONNECTION_WIFI }, 68 { flimflam::kTypeWimax, "", NetworkChangeNotifier::CONNECTION_4G }, 69 { "unknown type", "unknown technology", 70 NetworkChangeNotifier::CONNECTION_UNKNOWN }, 71 { flimflam::kTypeCellular, flimflam::kNetworkTechnology1Xrtt, 72 NetworkChangeNotifier::CONNECTION_2G }, 73 { flimflam::kTypeCellular, flimflam::kNetworkTechnologyGprs, 74 NetworkChangeNotifier::CONNECTION_2G }, 75 { flimflam::kTypeCellular, flimflam::kNetworkTechnologyEdge, 76 NetworkChangeNotifier::CONNECTION_2G }, 77 { flimflam::kTypeCellular, flimflam::kNetworkTechnologyEvdo, 78 NetworkChangeNotifier::CONNECTION_3G }, 79 { flimflam::kTypeCellular, flimflam::kNetworkTechnologyGsm, 80 NetworkChangeNotifier::CONNECTION_3G }, 81 { flimflam::kTypeCellular, flimflam::kNetworkTechnologyUmts, 82 NetworkChangeNotifier::CONNECTION_3G }, 83 { flimflam::kTypeCellular, flimflam::kNetworkTechnologyHspa, 84 NetworkChangeNotifier::CONNECTION_3G }, 85 { flimflam::kTypeCellular, flimflam::kNetworkTechnologyHspaPlus, 86 NetworkChangeNotifier::CONNECTION_4G }, 87 { flimflam::kTypeCellular, flimflam::kNetworkTechnologyLte, 88 NetworkChangeNotifier::CONNECTION_4G }, 89 { flimflam::kTypeCellular, flimflam::kNetworkTechnologyLteAdvanced, 90 NetworkChangeNotifier::CONNECTION_4G }, 91 { flimflam::kTypeCellular, "unknown technology", 92 NetworkChangeNotifier::CONNECTION_2G } 93 }; 94 95 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(type_mappings); ++i) { 96 NetworkChangeNotifier::ConnectionType type = 97 NetworkChangeNotifierChromeos::ConnectionTypeFromShill( 98 type_mappings[i].shill_type, type_mappings[i].technology); 99 EXPECT_EQ(type_mappings[i].connection_type, type); 100 } 101 } 102 103 class NetworkChangeNotifierChromeosUpdateTest : public testing::Test { 104 protected: 105 NetworkChangeNotifierChromeosUpdateTest() : default_network_("") { 106 } 107 virtual ~NetworkChangeNotifierChromeosUpdateTest() {} 108 109 void SetNotifierState(const NotifierState& notifier_state) { 110 notifier_.connection_type_ = notifier_state.type; 111 notifier_.service_path_ = notifier_state.service_path; 112 notifier_.ip_address_ = notifier_state.ip_address; 113 std::vector<std::string> dns_servers; 114 base::SplitString(notifier_state.dns_servers, ',', &dns_servers); 115 notifier_.dns_servers_ = dns_servers; 116 } 117 118 void VerifyNotifierState(const NotifierState& notifier_state) { 119 EXPECT_EQ(notifier_state.type, notifier_.connection_type_); 120 EXPECT_EQ(notifier_state.service_path, notifier_.service_path_); 121 EXPECT_EQ(notifier_state.ip_address, notifier_.ip_address_); 122 std::vector<std::string> dns_servers; 123 base::SplitString(notifier_state.dns_servers, ',', &dns_servers); 124 EXPECT_EQ(dns_servers, notifier_.dns_servers_); 125 } 126 127 // Sets the default network state used for notifier updates. 128 void SetDefaultNetworkState( 129 const DefaultNetworkState& default_network_state) { 130 if (default_network_state.is_connected) 131 default_network_.connection_state_ = flimflam::kStateOnline; 132 else 133 default_network_.connection_state_ = flimflam::kStateConfiguration; 134 default_network_.type_ = default_network_state.type; 135 default_network_.network_technology_ = 136 default_network_state.network_technology; 137 default_network_.path_ = default_network_state.service_path; 138 default_network_.ip_address_ = default_network_state.ip_address; 139 std::vector<std::string> dns_servers; 140 base::SplitString(default_network_state.dns_servers, ',', &dns_servers); 141 default_network_.dns_servers_ = dns_servers; 142 } 143 144 // Process an default network update based on the state of |default_network_|. 145 void ProcessDefaultNetworkUpdate(bool* type_changed, 146 bool* ip_changed, 147 bool* dns_changed) { 148 notifier_.UpdateState(&default_network_, type_changed, ip_changed, 149 dns_changed); 150 } 151 152 private: 153 NetworkState default_network_; 154 NetworkChangeNotifierChromeos notifier_; 155 }; 156 157 NotifierUpdateTestCase test_cases[] = { 158 { "Online -> Offline", 159 { NetworkChangeNotifier::CONNECTION_ETHERNET, kService1, kIpAddress1, 160 kDnsServers1 }, 161 { false, flimflam::kTypeEthernet, "", kService1, "", "" }, 162 { NetworkChangeNotifier::CONNECTION_NONE, "", "", "" }, 163 true, true, true 164 }, 165 { "Offline -> Offline", 166 { NetworkChangeNotifier::CONNECTION_NONE, "", "", "" }, 167 { false, flimflam::kTypeEthernet, "", kService1, kIpAddress1, 168 kDnsServers1 }, 169 { NetworkChangeNotifier::CONNECTION_NONE, "", "", "" }, 170 false, false, false 171 }, 172 { "Offline -> Online", 173 { NetworkChangeNotifier::CONNECTION_NONE, "", "", "" }, 174 { true, flimflam::kTypeEthernet, "", kService1, kIpAddress1, kDnsServers1 }, 175 { NetworkChangeNotifier::CONNECTION_ETHERNET, kService1, kIpAddress1, 176 kDnsServers1 }, 177 true, true, true 178 }, 179 { "Online -> Online (new default service, different connection type)", 180 { NetworkChangeNotifier::CONNECTION_ETHERNET, kService1, kIpAddress1, 181 kDnsServers1 }, 182 { true, flimflam::kTypeWifi, "", kService2, kIpAddress1, kDnsServers1 }, 183 { NetworkChangeNotifier::CONNECTION_WIFI, kService2, kIpAddress1, 184 kDnsServers1 }, 185 true, true, true 186 }, 187 { "Online -> Online (new default service, same connection type)", 188 { NetworkChangeNotifier::CONNECTION_WIFI, kService2, kIpAddress1, 189 kDnsServers1 }, 190 { true, flimflam::kTypeWifi, "", kService3, kIpAddress1, kDnsServers1 }, 191 { NetworkChangeNotifier::CONNECTION_WIFI, kService3, kIpAddress1, 192 kDnsServers1 }, 193 false, true, true 194 }, 195 { "Online -> Online (same default service, first IP address update)", 196 { NetworkChangeNotifier::CONNECTION_WIFI, kService3, "", kDnsServers1 }, 197 { true, flimflam::kTypeWifi, "", kService3, kIpAddress2, kDnsServers1 }, 198 { NetworkChangeNotifier::CONNECTION_WIFI, kService3, kIpAddress2, 199 kDnsServers1 }, 200 false, false, false 201 }, 202 { "Online -> Online (same default service, new IP address, same DNS)", 203 { NetworkChangeNotifier::CONNECTION_WIFI, kService3, kIpAddress1, 204 kDnsServers1 }, 205 { true, flimflam::kTypeWifi, "", kService3, kIpAddress2, kDnsServers1 }, 206 { NetworkChangeNotifier::CONNECTION_WIFI, kService3, kIpAddress2, 207 kDnsServers1 }, 208 false, true, false 209 }, 210 { "Online -> Online (same default service, same IP address, new DNS)", 211 { NetworkChangeNotifier::CONNECTION_WIFI, kService3, kIpAddress2, 212 kDnsServers1 }, 213 { true, flimflam::kTypeWifi, "", kService3, kIpAddress2, kDnsServers2 }, 214 { NetworkChangeNotifier::CONNECTION_WIFI, kService3, kIpAddress2, 215 kDnsServers2 }, 216 false, false, true 217 } 218 }; 219 220 TEST_F(NetworkChangeNotifierChromeosUpdateTest, UpdateDefaultNetwork) { 221 for (size_t i = 0; i < arraysize(test_cases); ++i) { 222 SCOPED_TRACE(test_cases[i].test_description); 223 SetNotifierState(test_cases[i].initial_state); 224 SetDefaultNetworkState(test_cases[i].default_network_state); 225 bool type_changed = false, ip_changed = false, dns_changed = false; 226 ProcessDefaultNetworkUpdate(&type_changed, &ip_changed, &dns_changed); 227 VerifyNotifierState(test_cases[i].expected_state); 228 EXPECT_TRUE(type_changed == test_cases[i].expected_type_changed); 229 EXPECT_TRUE(ip_changed == test_cases[i].expected_ip_changed); 230 EXPECT_TRUE(dns_changed == test_cases[i].expected_dns_changed); 231 } 232 } 233 234 } // namespace chromeos 235