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"
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"
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
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
;
51 struct DefaultNetworkState
{
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
;
73 using net::NetworkChangeNotifier
;
75 TEST(NetworkChangeNotifierChromeosTest
, ConnectionTypeFromShill
) {
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
{
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
;
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
,
170 bool* max_bandwidth_changed
) {
171 notifier_
.UpdateState(&default_network_
, type_changed
, ip_changed
,
172 dns_changed
, max_bandwidth_changed
);
176 base::MessageLoop message_loop_
;
177 NetworkState default_network_
;
178 NetworkChangeNotifierChromeos notifier_
;
181 NotifierUpdateTestCase test_cases
[] = {
182 {"Online -> Offline",
183 {NetworkChangeNotifier::CONNECTION_ETHERNET
,
187 kExpectedEthernetMaxBandwidth
},
188 {false, shill::kTypeEthernet
, "", kService1
, "", ""},
189 {NetworkChangeNotifier::CONNECTION_NONE
,
193 kExpectedNoneMaxBandwidth
},
198 {"Offline -> Offline",
199 {NetworkChangeNotifier::CONNECTION_NONE
,
203 kExpectedNoneMaxBandwidth
},
204 {false, shill::kTypeEthernet
, "", kService1
, kIpAddress1
, kDnsServers1
},
205 {NetworkChangeNotifier::CONNECTION_NONE
,
209 kExpectedNoneMaxBandwidth
},
214 {"Offline -> Online",
215 {NetworkChangeNotifier::CONNECTION_NONE
,
219 kExpectedNoneMaxBandwidth
},
220 {true, shill::kTypeEthernet
, "", kService1
, kIpAddress1
, kDnsServers1
},
221 {NetworkChangeNotifier::CONNECTION_ETHERNET
,
225 kExpectedEthernetMaxBandwidth
},
230 {"Online -> Online (new default service, different connection type)",
231 {NetworkChangeNotifier::CONNECTION_ETHERNET
,
235 kExpectedEthernetMaxBandwidth
},
236 {true, shill::kTypeWifi
, "", kService2
, kIpAddress1
, kDnsServers1
},
237 {NetworkChangeNotifier::CONNECTION_WIFI
,
241 kExpectedWifiMaxBandwidth
},
246 {"Online -> Online (new default service, same connection type)",
247 {NetworkChangeNotifier::CONNECTION_WIFI
,
251 kExpectedWifiMaxBandwidth
},
252 {true, shill::kTypeWifi
, "", kService3
, kIpAddress1
, kDnsServers1
},
253 {NetworkChangeNotifier::CONNECTION_WIFI
,
257 kExpectedWifiMaxBandwidth
},
262 {"Online -> Online (same default service, first IP address update)",
263 {NetworkChangeNotifier::CONNECTION_WIFI
,
267 kExpectedWifiMaxBandwidth
},
268 {true, shill::kTypeWifi
, "", kService3
, kIpAddress2
, kDnsServers1
},
269 {NetworkChangeNotifier::CONNECTION_WIFI
,
273 kExpectedWifiMaxBandwidth
},
278 {"Online -> Online (same default service, new IP address, same DNS)",
279 {NetworkChangeNotifier::CONNECTION_WIFI
,
283 kExpectedWifiMaxBandwidth
},
284 {true, shill::kTypeWifi
, "", kService3
, kIpAddress2
, kDnsServers1
},
285 {NetworkChangeNotifier::CONNECTION_WIFI
,
289 kExpectedWifiMaxBandwidth
},
294 {"Online -> Online (same default service, same IP address, new DNS)",
295 {NetworkChangeNotifier::CONNECTION_WIFI
,
299 kExpectedWifiMaxBandwidth
},
300 {true, shill::kTypeWifi
, "", kService3
, kIpAddress2
, kDnsServers2
},
301 {NetworkChangeNotifier::CONNECTION_WIFI
,
305 kExpectedWifiMaxBandwidth
},
310 {"Online -> Online (change of technology but not connection type)",
311 {NetworkChangeNotifier::CONNECTION_3G
,
315 kExpectedEvdoMaxBandwidth
},
317 shill::kTypeCellular
,
318 shill::kNetworkTechnologyHspa
,
322 {NetworkChangeNotifier::CONNECTION_3G
,
326 kExpectedHspaMaxBandwidth
},
331 {"Online -> Online (change of technology and connection type)",
332 {NetworkChangeNotifier::CONNECTION_3G
,
336 kExpectedEvdoMaxBandwidth
},
338 shill::kTypeCellular
,
339 shill::kNetworkTechnologyLte
,
343 {NetworkChangeNotifier::CONNECTION_4G
,
347 kExpectedLteMaxBandwidth
},
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