Roll WebRTC 9745:9761, Libjingle 9742:9761
[chromium-blink-merge.git] / chromeos / network / network_change_notifier_chromeos_unittest.cc
blob570bc36627afd4af61c93a4dfb37290672a62b80
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, ",",
132 base::TRIM_WHITESPACE, base::SPLIT_WANT_ALL);
133 notifier_.dns_servers_ = dns_servers;
136 void VerifyNotifierState(const NotifierState& notifier_state) {
137 EXPECT_EQ(notifier_state.type, notifier_.connection_type_);
138 EXPECT_EQ(notifier_state.service_path, notifier_.service_path_);
139 EXPECT_EQ(notifier_state.ip_address, notifier_.ip_address_);
140 EXPECT_EQ(notifier_state.max_bandwidth, notifier_.max_bandwidth_mbps_);
141 std::vector<std::string> dns_servers =
142 base::SplitString(notifier_state.dns_servers, ",",
143 base::TRIM_WHITESPACE, base::SPLIT_WANT_ALL);
144 EXPECT_EQ(dns_servers, notifier_.dns_servers_);
147 // Sets the default network state used for notifier updates.
148 void SetDefaultNetworkState(
149 const DefaultNetworkState& default_network_state) {
150 default_network_.visible_ = true;
151 if (default_network_state.is_connected)
152 default_network_.connection_state_ = shill::kStateOnline;
153 else
154 default_network_.connection_state_ = shill::kStateConfiguration;
155 default_network_.type_ = default_network_state.type;
156 default_network_.network_technology_ =
157 default_network_state.network_technology;
158 default_network_.path_ = default_network_state.service_path;
159 default_network_.ip_address_ = default_network_state.ip_address;
160 std::vector<std::string> dns_servers =
161 base::SplitString(default_network_state.dns_servers, ",",
162 base::TRIM_WHITESPACE, base::SPLIT_WANT_ALL);
163 default_network_.dns_servers_ = dns_servers;
166 // Process an default network update based on the state of |default_network_|.
167 void ProcessDefaultNetworkUpdate(bool* type_changed,
168 bool* ip_changed,
169 bool* dns_changed,
170 bool* max_bandwidth_changed) {
171 notifier_.UpdateState(&default_network_, type_changed, ip_changed,
172 dns_changed, max_bandwidth_changed);
175 private:
176 base::MessageLoop message_loop_;
177 NetworkState default_network_;
178 NetworkChangeNotifierChromeos notifier_;
181 NotifierUpdateTestCase test_cases[] = {
182 {"Online -> Offline",
183 {NetworkChangeNotifier::CONNECTION_ETHERNET,
184 kService1,
185 kIpAddress1,
186 kDnsServers1,
187 kExpectedEthernetMaxBandwidth},
188 {false, shill::kTypeEthernet, "", kService1, "", ""},
189 {NetworkChangeNotifier::CONNECTION_NONE,
193 kExpectedNoneMaxBandwidth},
194 true,
195 true,
196 true,
197 true},
198 {"Offline -> Offline",
199 {NetworkChangeNotifier::CONNECTION_NONE,
203 kExpectedNoneMaxBandwidth},
204 {false, shill::kTypeEthernet, "", kService1, kIpAddress1, kDnsServers1},
205 {NetworkChangeNotifier::CONNECTION_NONE,
209 kExpectedNoneMaxBandwidth},
210 false,
211 false,
212 false,
213 false},
214 {"Offline -> Online",
215 {NetworkChangeNotifier::CONNECTION_NONE,
219 kExpectedNoneMaxBandwidth},
220 {true, shill::kTypeEthernet, "", kService1, kIpAddress1, kDnsServers1},
221 {NetworkChangeNotifier::CONNECTION_ETHERNET,
222 kService1,
223 kIpAddress1,
224 kDnsServers1,
225 kExpectedEthernetMaxBandwidth},
226 true,
227 true,
228 true,
229 true},
230 {"Online -> Online (new default service, different connection type)",
231 {NetworkChangeNotifier::CONNECTION_ETHERNET,
232 kService1,
233 kIpAddress1,
234 kDnsServers1,
235 kExpectedEthernetMaxBandwidth},
236 {true, shill::kTypeWifi, "", kService2, kIpAddress1, kDnsServers1},
237 {NetworkChangeNotifier::CONNECTION_WIFI,
238 kService2,
239 kIpAddress1,
240 kDnsServers1,
241 kExpectedWifiMaxBandwidth},
242 true,
243 true,
244 true,
245 false},
246 {"Online -> Online (new default service, same connection type)",
247 {NetworkChangeNotifier::CONNECTION_WIFI,
248 kService2,
249 kIpAddress1,
250 kDnsServers1,
251 kExpectedWifiMaxBandwidth},
252 {true, shill::kTypeWifi, "", kService3, kIpAddress1, kDnsServers1},
253 {NetworkChangeNotifier::CONNECTION_WIFI,
254 kService3,
255 kIpAddress1,
256 kDnsServers1,
257 kExpectedWifiMaxBandwidth},
258 false,
259 true,
260 true,
261 false},
262 {"Online -> Online (same default service, first IP address update)",
263 {NetworkChangeNotifier::CONNECTION_WIFI,
264 kService3,
266 kDnsServers1,
267 kExpectedWifiMaxBandwidth},
268 {true, shill::kTypeWifi, "", kService3, kIpAddress2, kDnsServers1},
269 {NetworkChangeNotifier::CONNECTION_WIFI,
270 kService3,
271 kIpAddress2,
272 kDnsServers1,
273 kExpectedWifiMaxBandwidth},
274 false,
275 false,
276 false,
277 false},
278 {"Online -> Online (same default service, new IP address, same DNS)",
279 {NetworkChangeNotifier::CONNECTION_WIFI,
280 kService3,
281 kIpAddress1,
282 kDnsServers1,
283 kExpectedWifiMaxBandwidth},
284 {true, shill::kTypeWifi, "", kService3, kIpAddress2, kDnsServers1},
285 {NetworkChangeNotifier::CONNECTION_WIFI,
286 kService3,
287 kIpAddress2,
288 kDnsServers1,
289 kExpectedWifiMaxBandwidth},
290 false,
291 true,
292 false,
293 false},
294 {"Online -> Online (same default service, same IP address, new DNS)",
295 {NetworkChangeNotifier::CONNECTION_WIFI,
296 kService3,
297 kIpAddress2,
298 kDnsServers1,
299 kExpectedWifiMaxBandwidth},
300 {true, shill::kTypeWifi, "", kService3, kIpAddress2, kDnsServers2},
301 {NetworkChangeNotifier::CONNECTION_WIFI,
302 kService3,
303 kIpAddress2,
304 kDnsServers2,
305 kExpectedWifiMaxBandwidth},
306 false,
307 false,
308 true,
309 false},
310 {"Online -> Online (change of technology but not connection type)",
311 {NetworkChangeNotifier::CONNECTION_3G,
312 kService3,
313 kIpAddress2,
314 kDnsServers1,
315 kExpectedEvdoMaxBandwidth},
316 {true,
317 shill::kTypeCellular,
318 shill::kNetworkTechnologyHspa,
319 kService3,
320 kIpAddress2,
321 kDnsServers1},
322 {NetworkChangeNotifier::CONNECTION_3G,
323 kService3,
324 kIpAddress2,
325 kDnsServers1,
326 kExpectedHspaMaxBandwidth},
327 false,
328 false,
329 false,
330 true},
331 {"Online -> Online (change of technology and connection type)",
332 {NetworkChangeNotifier::CONNECTION_3G,
333 kService3,
334 kIpAddress2,
335 kDnsServers1,
336 kExpectedEvdoMaxBandwidth},
337 {true,
338 shill::kTypeCellular,
339 shill::kNetworkTechnologyLte,
340 kService3,
341 kIpAddress2,
342 kDnsServers1},
343 {NetworkChangeNotifier::CONNECTION_4G,
344 kService3,
345 kIpAddress2,
346 kDnsServers1,
347 kExpectedLteMaxBandwidth},
348 true,
349 false,
350 false,
351 true}};
353 TEST_F(NetworkChangeNotifierChromeosUpdateTest, UpdateDefaultNetwork) {
354 for (size_t i = 0; i < arraysize(test_cases); ++i) {
355 SCOPED_TRACE(test_cases[i].test_description);
356 SetNotifierState(test_cases[i].initial_state);
357 SetDefaultNetworkState(test_cases[i].default_network_state);
358 bool type_changed = false, ip_changed = false, dns_changed = false,
359 max_bandwidth_changed = false;
360 ProcessDefaultNetworkUpdate(&type_changed, &ip_changed, &dns_changed,
361 &max_bandwidth_changed);
362 VerifyNotifierState(test_cases[i].expected_state);
363 EXPECT_EQ(test_cases[i].expected_type_changed, type_changed);
364 EXPECT_EQ(test_cases[i].expected_ip_changed, ip_changed);
365 EXPECT_EQ(test_cases[i].expected_dns_changed, dns_changed);
366 EXPECT_EQ(test_cases[i].expected_max_bandwidth_changed,
367 max_bandwidth_changed);
371 } // namespace chromeos