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
, ',', &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
;
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
,
167 bool* max_bandwidth_changed
) {
168 notifier_
.UpdateState(&default_network_
, type_changed
, ip_changed
,
169 dns_changed
, max_bandwidth_changed
);
173 base::MessageLoop message_loop_
;
174 NetworkState default_network_
;
175 NetworkChangeNotifierChromeos notifier_
;
178 NotifierUpdateTestCase test_cases
[] = {
179 {"Online -> Offline",
180 {NetworkChangeNotifier::CONNECTION_ETHERNET
,
184 kExpectedEthernetMaxBandwidth
},
185 {false, shill::kTypeEthernet
, "", kService1
, "", ""},
186 {NetworkChangeNotifier::CONNECTION_NONE
,
190 kExpectedNoneMaxBandwidth
},
195 {"Offline -> Offline",
196 {NetworkChangeNotifier::CONNECTION_NONE
,
200 kExpectedNoneMaxBandwidth
},
201 {false, shill::kTypeEthernet
, "", kService1
, kIpAddress1
, kDnsServers1
},
202 {NetworkChangeNotifier::CONNECTION_NONE
,
206 kExpectedNoneMaxBandwidth
},
211 {"Offline -> Online",
212 {NetworkChangeNotifier::CONNECTION_NONE
,
216 kExpectedNoneMaxBandwidth
},
217 {true, shill::kTypeEthernet
, "", kService1
, kIpAddress1
, kDnsServers1
},
218 {NetworkChangeNotifier::CONNECTION_ETHERNET
,
222 kExpectedEthernetMaxBandwidth
},
227 {"Online -> Online (new default service, different connection type)",
228 {NetworkChangeNotifier::CONNECTION_ETHERNET
,
232 kExpectedEthernetMaxBandwidth
},
233 {true, shill::kTypeWifi
, "", kService2
, kIpAddress1
, kDnsServers1
},
234 {NetworkChangeNotifier::CONNECTION_WIFI
,
238 kExpectedWifiMaxBandwidth
},
243 {"Online -> Online (new default service, same connection type)",
244 {NetworkChangeNotifier::CONNECTION_WIFI
,
248 kExpectedWifiMaxBandwidth
},
249 {true, shill::kTypeWifi
, "", kService3
, kIpAddress1
, kDnsServers1
},
250 {NetworkChangeNotifier::CONNECTION_WIFI
,
254 kExpectedWifiMaxBandwidth
},
259 {"Online -> Online (same default service, first IP address update)",
260 {NetworkChangeNotifier::CONNECTION_WIFI
,
264 kExpectedWifiMaxBandwidth
},
265 {true, shill::kTypeWifi
, "", kService3
, kIpAddress2
, kDnsServers1
},
266 {NetworkChangeNotifier::CONNECTION_WIFI
,
270 kExpectedWifiMaxBandwidth
},
275 {"Online -> Online (same default service, new IP address, same DNS)",
276 {NetworkChangeNotifier::CONNECTION_WIFI
,
280 kExpectedWifiMaxBandwidth
},
281 {true, shill::kTypeWifi
, "", kService3
, kIpAddress2
, kDnsServers1
},
282 {NetworkChangeNotifier::CONNECTION_WIFI
,
286 kExpectedWifiMaxBandwidth
},
291 {"Online -> Online (same default service, same IP address, new DNS)",
292 {NetworkChangeNotifier::CONNECTION_WIFI
,
296 kExpectedWifiMaxBandwidth
},
297 {true, shill::kTypeWifi
, "", kService3
, kIpAddress2
, kDnsServers2
},
298 {NetworkChangeNotifier::CONNECTION_WIFI
,
302 kExpectedWifiMaxBandwidth
},
307 {"Online -> Online (change of technology but not connection type)",
308 {NetworkChangeNotifier::CONNECTION_3G
,
312 kExpectedEvdoMaxBandwidth
},
314 shill::kTypeCellular
,
315 shill::kNetworkTechnologyHspa
,
319 {NetworkChangeNotifier::CONNECTION_3G
,
323 kExpectedHspaMaxBandwidth
},
328 {"Online -> Online (change of technology and connection type)",
329 {NetworkChangeNotifier::CONNECTION_3G
,
333 kExpectedEvdoMaxBandwidth
},
335 shill::kTypeCellular
,
336 shill::kNetworkTechnologyLte
,
340 {NetworkChangeNotifier::CONNECTION_4G
,
344 kExpectedLteMaxBandwidth
},
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