Update V8 to version 4.5.63.
[chromium-blink-merge.git] / chromeos / network / network_change_notifier_chromeos_unittest.cc
blobffbe7c740e2547488def7becc258e405351143ad
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.
5 #include "chromeos/network/network_change_notifier_chromeos.h"
7 #include <string>
9 #include "base/basictypes.h"
10 #include "base/message_loop/message_loop.h"
11 #include "base/strings/string_split.h"
12 #include "chromeos/network/network_change_notifier_factory_chromeos.h"
13 #include "chromeos/network/network_state.h"
14 #include "net/base/network_change_notifier.h"
15 #include "testing/gtest/include/gtest/gtest.h"
16 #include "third_party/cros_system_api/dbus/service_constants.h"
18 namespace chromeos {
20 namespace {
22 const char kDnsServers1[] = "192.168.0.1,192.168.0.2";
23 const char kDnsServers2[] = "192.168.3.1,192.168.3.2";
24 const char kIpAddress1[] = "192.168.1.1";
25 const char kIpAddress2[] = "192.168.1.2";
26 const char kService1[] = "/service/1";
27 const char kService2[] = "/service/2";
28 const char kService3[] = "/service/3";
30 // These values come from
31 // http://w3c.github.io/netinfo/#underlying-connection-technology. For types
32 // that have unknown subtypes (wifi and ethernet) positive infinity is used as
33 // per the spec.
34 const double kExpectedNoneMaxBandwidth = 0;
35 const double kExpectedWifiMaxBandwidth =
36 std::numeric_limits<double>::infinity();
37 const double kExpectedEthernetMaxBandwidth =
38 std::numeric_limits<double>::infinity();
39 const double kExpectedLteMaxBandwidth = 100;
40 const double kExpectedEvdoMaxBandwidth = 2.46;
41 const double kExpectedHspaMaxBandwidth = 3.6;
43 struct NotifierState {
44 net::NetworkChangeNotifier::ConnectionType type;
45 const char* service_path;
46 const char* ip_address;
47 const char* dns_servers;
48 double max_bandwidth;
51 struct DefaultNetworkState {
52 bool is_connected;
53 const char* type;
54 const char* network_technology;
55 const char* service_path;
56 const char* ip_address;
57 const char* dns_servers;
60 struct NotifierUpdateTestCase {
61 const char* test_description;
62 NotifierState initial_state;
63 DefaultNetworkState default_network_state;
64 NotifierState expected_state;
65 bool expected_type_changed;
66 bool expected_ip_changed;
67 bool expected_dns_changed;
68 bool expected_max_bandwidth_changed;
71 } // namespace
73 using net::NetworkChangeNotifier;
75 TEST(NetworkChangeNotifierChromeosTest, ConnectionTypeFromShill) {
76 struct TypeMapping {
77 const char* shill_type;
78 const char* technology;
79 NetworkChangeNotifier::ConnectionType connection_type;
81 TypeMapping type_mappings[] = {
82 { shill::kTypeEthernet, "", NetworkChangeNotifier::CONNECTION_ETHERNET },
83 { shill::kTypeWifi, "", NetworkChangeNotifier::CONNECTION_WIFI },
84 { shill::kTypeWimax, "", NetworkChangeNotifier::CONNECTION_4G },
85 { "unknown type", "unknown technology",
86 NetworkChangeNotifier::CONNECTION_UNKNOWN },
87 { shill::kTypeCellular, shill::kNetworkTechnology1Xrtt,
88 NetworkChangeNotifier::CONNECTION_2G },
89 { shill::kTypeCellular, shill::kNetworkTechnologyGprs,
90 NetworkChangeNotifier::CONNECTION_2G },
91 { shill::kTypeCellular, shill::kNetworkTechnologyEdge,
92 NetworkChangeNotifier::CONNECTION_2G },
93 { shill::kTypeCellular, shill::kNetworkTechnologyEvdo,
94 NetworkChangeNotifier::CONNECTION_3G },
95 { shill::kTypeCellular, shill::kNetworkTechnologyGsm,
96 NetworkChangeNotifier::CONNECTION_3G },
97 { shill::kTypeCellular, shill::kNetworkTechnologyUmts,
98 NetworkChangeNotifier::CONNECTION_3G },
99 { shill::kTypeCellular, shill::kNetworkTechnologyHspa,
100 NetworkChangeNotifier::CONNECTION_3G },
101 { shill::kTypeCellular, shill::kNetworkTechnologyHspaPlus,
102 NetworkChangeNotifier::CONNECTION_4G },
103 { shill::kTypeCellular, shill::kNetworkTechnologyLte,
104 NetworkChangeNotifier::CONNECTION_4G },
105 { shill::kTypeCellular, shill::kNetworkTechnologyLteAdvanced,
106 NetworkChangeNotifier::CONNECTION_4G },
107 { shill::kTypeCellular, "unknown technology",
108 NetworkChangeNotifier::CONNECTION_2G }
111 for (size_t i = 0; i < arraysize(type_mappings); ++i) {
112 NetworkChangeNotifier::ConnectionType type =
113 NetworkChangeNotifierChromeos::ConnectionTypeFromShill(
114 type_mappings[i].shill_type, type_mappings[i].technology);
115 EXPECT_EQ(type_mappings[i].connection_type, type);
119 class NetworkChangeNotifierChromeosUpdateTest : public testing::Test {
120 protected:
121 NetworkChangeNotifierChromeosUpdateTest() : default_network_("") {
123 ~NetworkChangeNotifierChromeosUpdateTest() override {}
125 void SetNotifierState(const NotifierState& notifier_state) {
126 notifier_.connection_type_ = notifier_state.type;
127 notifier_.service_path_ = notifier_state.service_path;
128 notifier_.ip_address_ = notifier_state.ip_address;
129 notifier_.max_bandwidth_mbps_ = notifier_state.max_bandwidth;
130 std::vector<std::string> dns_servers;
131 base::SplitString(notifier_state.dns_servers, ',', &dns_servers);
132 notifier_.dns_servers_ = dns_servers;
135 void VerifyNotifierState(const NotifierState& notifier_state) {
136 EXPECT_EQ(notifier_state.type, notifier_.connection_type_);
137 EXPECT_EQ(notifier_state.service_path, notifier_.service_path_);
138 EXPECT_EQ(notifier_state.ip_address, notifier_.ip_address_);
139 EXPECT_EQ(notifier_state.max_bandwidth, notifier_.max_bandwidth_mbps_);
140 std::vector<std::string> dns_servers;
141 base::SplitString(notifier_state.dns_servers, ',', &dns_servers);
142 EXPECT_EQ(dns_servers, notifier_.dns_servers_);
145 // Sets the default network state used for notifier updates.
146 void SetDefaultNetworkState(
147 const DefaultNetworkState& default_network_state) {
148 default_network_.visible_ = true;
149 if (default_network_state.is_connected)
150 default_network_.connection_state_ = shill::kStateOnline;
151 else
152 default_network_.connection_state_ = shill::kStateConfiguration;
153 default_network_.type_ = default_network_state.type;
154 default_network_.network_technology_ =
155 default_network_state.network_technology;
156 default_network_.path_ = default_network_state.service_path;
157 default_network_.ip_address_ = default_network_state.ip_address;
158 std::vector<std::string> dns_servers;
159 base::SplitString(default_network_state.dns_servers, ',', &dns_servers);
160 default_network_.dns_servers_ = dns_servers;
163 // Process an default network update based on the state of |default_network_|.
164 void ProcessDefaultNetworkUpdate(bool* type_changed,
165 bool* ip_changed,
166 bool* dns_changed,
167 bool* max_bandwidth_changed) {
168 notifier_.UpdateState(&default_network_, type_changed, ip_changed,
169 dns_changed, max_bandwidth_changed);
172 private:
173 base::MessageLoop message_loop_;
174 NetworkState default_network_;
175 NetworkChangeNotifierChromeos notifier_;
178 NotifierUpdateTestCase test_cases[] = {
179 {"Online -> Offline",
180 {NetworkChangeNotifier::CONNECTION_ETHERNET,
181 kService1,
182 kIpAddress1,
183 kDnsServers1,
184 kExpectedEthernetMaxBandwidth},
185 {false, shill::kTypeEthernet, "", kService1, "", ""},
186 {NetworkChangeNotifier::CONNECTION_NONE,
190 kExpectedNoneMaxBandwidth},
191 true,
192 true,
193 true,
194 true},
195 {"Offline -> Offline",
196 {NetworkChangeNotifier::CONNECTION_NONE,
200 kExpectedNoneMaxBandwidth},
201 {false, shill::kTypeEthernet, "", kService1, kIpAddress1, kDnsServers1},
202 {NetworkChangeNotifier::CONNECTION_NONE,
206 kExpectedNoneMaxBandwidth},
207 false,
208 false,
209 false,
210 false},
211 {"Offline -> Online",
212 {NetworkChangeNotifier::CONNECTION_NONE,
216 kExpectedNoneMaxBandwidth},
217 {true, shill::kTypeEthernet, "", kService1, kIpAddress1, kDnsServers1},
218 {NetworkChangeNotifier::CONNECTION_ETHERNET,
219 kService1,
220 kIpAddress1,
221 kDnsServers1,
222 kExpectedEthernetMaxBandwidth},
223 true,
224 true,
225 true,
226 true},
227 {"Online -> Online (new default service, different connection type)",
228 {NetworkChangeNotifier::CONNECTION_ETHERNET,
229 kService1,
230 kIpAddress1,
231 kDnsServers1,
232 kExpectedEthernetMaxBandwidth},
233 {true, shill::kTypeWifi, "", kService2, kIpAddress1, kDnsServers1},
234 {NetworkChangeNotifier::CONNECTION_WIFI,
235 kService2,
236 kIpAddress1,
237 kDnsServers1,
238 kExpectedWifiMaxBandwidth},
239 true,
240 true,
241 true,
242 false},
243 {"Online -> Online (new default service, same connection type)",
244 {NetworkChangeNotifier::CONNECTION_WIFI,
245 kService2,
246 kIpAddress1,
247 kDnsServers1,
248 kExpectedWifiMaxBandwidth},
249 {true, shill::kTypeWifi, "", kService3, kIpAddress1, kDnsServers1},
250 {NetworkChangeNotifier::CONNECTION_WIFI,
251 kService3,
252 kIpAddress1,
253 kDnsServers1,
254 kExpectedWifiMaxBandwidth},
255 false,
256 true,
257 true,
258 false},
259 {"Online -> Online (same default service, first IP address update)",
260 {NetworkChangeNotifier::CONNECTION_WIFI,
261 kService3,
263 kDnsServers1,
264 kExpectedWifiMaxBandwidth},
265 {true, shill::kTypeWifi, "", kService3, kIpAddress2, kDnsServers1},
266 {NetworkChangeNotifier::CONNECTION_WIFI,
267 kService3,
268 kIpAddress2,
269 kDnsServers1,
270 kExpectedWifiMaxBandwidth},
271 false,
272 false,
273 false,
274 false},
275 {"Online -> Online (same default service, new IP address, same DNS)",
276 {NetworkChangeNotifier::CONNECTION_WIFI,
277 kService3,
278 kIpAddress1,
279 kDnsServers1,
280 kExpectedWifiMaxBandwidth},
281 {true, shill::kTypeWifi, "", kService3, kIpAddress2, kDnsServers1},
282 {NetworkChangeNotifier::CONNECTION_WIFI,
283 kService3,
284 kIpAddress2,
285 kDnsServers1,
286 kExpectedWifiMaxBandwidth},
287 false,
288 true,
289 false,
290 false},
291 {"Online -> Online (same default service, same IP address, new DNS)",
292 {NetworkChangeNotifier::CONNECTION_WIFI,
293 kService3,
294 kIpAddress2,
295 kDnsServers1,
296 kExpectedWifiMaxBandwidth},
297 {true, shill::kTypeWifi, "", kService3, kIpAddress2, kDnsServers2},
298 {NetworkChangeNotifier::CONNECTION_WIFI,
299 kService3,
300 kIpAddress2,
301 kDnsServers2,
302 kExpectedWifiMaxBandwidth},
303 false,
304 false,
305 true,
306 false},
307 {"Online -> Online (change of technology but not connection type)",
308 {NetworkChangeNotifier::CONNECTION_3G,
309 kService3,
310 kIpAddress2,
311 kDnsServers1,
312 kExpectedEvdoMaxBandwidth},
313 {true,
314 shill::kTypeCellular,
315 shill::kNetworkTechnologyHspa,
316 kService3,
317 kIpAddress2,
318 kDnsServers1},
319 {NetworkChangeNotifier::CONNECTION_3G,
320 kService3,
321 kIpAddress2,
322 kDnsServers1,
323 kExpectedHspaMaxBandwidth},
324 false,
325 false,
326 false,
327 true},
328 {"Online -> Online (change of technology and connection type)",
329 {NetworkChangeNotifier::CONNECTION_3G,
330 kService3,
331 kIpAddress2,
332 kDnsServers1,
333 kExpectedEvdoMaxBandwidth},
334 {true,
335 shill::kTypeCellular,
336 shill::kNetworkTechnologyLte,
337 kService3,
338 kIpAddress2,
339 kDnsServers1},
340 {NetworkChangeNotifier::CONNECTION_4G,
341 kService3,
342 kIpAddress2,
343 kDnsServers1,
344 kExpectedLteMaxBandwidth},
345 true,
346 false,
347 false,
348 true}};
350 TEST_F(NetworkChangeNotifierChromeosUpdateTest, UpdateDefaultNetwork) {
351 for (size_t i = 0; i < arraysize(test_cases); ++i) {
352 SCOPED_TRACE(test_cases[i].test_description);
353 SetNotifierState(test_cases[i].initial_state);
354 SetDefaultNetworkState(test_cases[i].default_network_state);
355 bool type_changed = false, ip_changed = false, dns_changed = false,
356 max_bandwidth_changed = false;
357 ProcessDefaultNetworkUpdate(&type_changed, &ip_changed, &dns_changed,
358 &max_bandwidth_changed);
359 VerifyNotifierState(test_cases[i].expected_state);
360 EXPECT_EQ(test_cases[i].expected_type_changed, type_changed);
361 EXPECT_EQ(test_cases[i].expected_ip_changed, ip_changed);
362 EXPECT_EQ(test_cases[i].expected_dns_changed, dns_changed);
363 EXPECT_EQ(test_cases[i].expected_max_bandwidth_changed,
364 max_bandwidth_changed);
368 } // namespace chromeos