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/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"
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";
29 struct NotifierState
{
30 net::NetworkChangeNotifier::ConnectionType type
;
31 const char* service_path
;
32 const char* ip_address
;
33 const char* dns_servers
;
36 struct DefaultNetworkState
{
39 const char* network_technology
;
40 const char* service_path
;
41 const char* ip_address
;
42 const char* dns_servers
;
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
;
57 using net::NetworkChangeNotifier
;
59 TEST(NetworkChangeNotifierChromeosTest
, ConnectionTypeFromShill
) {
61 const char* shill_type
;
62 const char* technology
;
63 NetworkChangeNotifier::ConnectionType connection_type
;
65 TypeMapping type_mappings
[] = {
66 { shill::kTypeEthernet
, "", NetworkChangeNotifier::CONNECTION_ETHERNET
},
67 { shill::kTypeWifi
, "", NetworkChangeNotifier::CONNECTION_WIFI
},
68 { shill::kTypeWimax
, "", NetworkChangeNotifier::CONNECTION_4G
},
69 { "unknown type", "unknown technology",
70 NetworkChangeNotifier::CONNECTION_UNKNOWN
},
71 { shill::kTypeCellular
, shill::kNetworkTechnology1Xrtt
,
72 NetworkChangeNotifier::CONNECTION_2G
},
73 { shill::kTypeCellular
, shill::kNetworkTechnologyGprs
,
74 NetworkChangeNotifier::CONNECTION_2G
},
75 { shill::kTypeCellular
, shill::kNetworkTechnologyEdge
,
76 NetworkChangeNotifier::CONNECTION_2G
},
77 { shill::kTypeCellular
, shill::kNetworkTechnologyEvdo
,
78 NetworkChangeNotifier::CONNECTION_3G
},
79 { shill::kTypeCellular
, shill::kNetworkTechnologyGsm
,
80 NetworkChangeNotifier::CONNECTION_3G
},
81 { shill::kTypeCellular
, shill::kNetworkTechnologyUmts
,
82 NetworkChangeNotifier::CONNECTION_3G
},
83 { shill::kTypeCellular
, shill::kNetworkTechnologyHspa
,
84 NetworkChangeNotifier::CONNECTION_3G
},
85 { shill::kTypeCellular
, shill::kNetworkTechnologyHspaPlus
,
86 NetworkChangeNotifier::CONNECTION_4G
},
87 { shill::kTypeCellular
, shill::kNetworkTechnologyLte
,
88 NetworkChangeNotifier::CONNECTION_4G
},
89 { shill::kTypeCellular
, shill::kNetworkTechnologyLteAdvanced
,
90 NetworkChangeNotifier::CONNECTION_4G
},
91 { shill::kTypeCellular
, "unknown technology",
92 NetworkChangeNotifier::CONNECTION_2G
}
95 for (size_t i
= 0; i
< arraysize(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
);
103 class NetworkChangeNotifierChromeosUpdateTest
: public testing::Test
{
105 NetworkChangeNotifierChromeosUpdateTest() : default_network_("") {
107 ~NetworkChangeNotifierChromeosUpdateTest() override
{}
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
;
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_
);
127 // Sets the default network state used for notifier updates.
128 void SetDefaultNetworkState(
129 const DefaultNetworkState
& default_network_state
) {
130 default_network_
.visible_
= true;
131 if (default_network_state
.is_connected
)
132 default_network_
.connection_state_
= shill::kStateOnline
;
134 default_network_
.connection_state_
= shill::kStateConfiguration
;
135 default_network_
.type_
= default_network_state
.type
;
136 default_network_
.network_technology_
=
137 default_network_state
.network_technology
;
138 default_network_
.path_
= default_network_state
.service_path
;
139 default_network_
.ip_address_
= default_network_state
.ip_address
;
140 std::vector
<std::string
> dns_servers
;
141 base::SplitString(default_network_state
.dns_servers
, ',', &dns_servers
);
142 default_network_
.dns_servers_
= dns_servers
;
145 // Process an default network update based on the state of |default_network_|.
146 void ProcessDefaultNetworkUpdate(bool* type_changed
,
149 notifier_
.UpdateState(&default_network_
, type_changed
, ip_changed
,
154 NetworkState default_network_
;
155 NetworkChangeNotifierChromeos notifier_
;
158 NotifierUpdateTestCase test_cases
[] = {
159 { "Online -> Offline",
160 { NetworkChangeNotifier::CONNECTION_ETHERNET
, kService1
, kIpAddress1
,
162 { false, shill::kTypeEthernet
, "", kService1
, "", "" },
163 { NetworkChangeNotifier::CONNECTION_NONE
, "", "", "" },
166 { "Offline -> Offline",
167 { NetworkChangeNotifier::CONNECTION_NONE
, "", "", "" },
168 { false, shill::kTypeEthernet
, "", kService1
, kIpAddress1
,
170 { NetworkChangeNotifier::CONNECTION_NONE
, "", "", "" },
173 { "Offline -> Online",
174 { NetworkChangeNotifier::CONNECTION_NONE
, "", "", "" },
175 { true, shill::kTypeEthernet
, "", kService1
, kIpAddress1
, kDnsServers1
},
176 { NetworkChangeNotifier::CONNECTION_ETHERNET
, kService1
, kIpAddress1
,
180 { "Online -> Online (new default service, different connection type)",
181 { NetworkChangeNotifier::CONNECTION_ETHERNET
, kService1
, kIpAddress1
,
183 { true, shill::kTypeWifi
, "", kService2
, kIpAddress1
, kDnsServers1
},
184 { NetworkChangeNotifier::CONNECTION_WIFI
, kService2
, kIpAddress1
,
188 { "Online -> Online (new default service, same connection type)",
189 { NetworkChangeNotifier::CONNECTION_WIFI
, kService2
, kIpAddress1
,
191 { true, shill::kTypeWifi
, "", kService3
, kIpAddress1
, kDnsServers1
},
192 { NetworkChangeNotifier::CONNECTION_WIFI
, kService3
, kIpAddress1
,
196 { "Online -> Online (same default service, first IP address update)",
197 { NetworkChangeNotifier::CONNECTION_WIFI
, kService3
, "", kDnsServers1
},
198 { true, shill::kTypeWifi
, "", kService3
, kIpAddress2
, kDnsServers1
},
199 { NetworkChangeNotifier::CONNECTION_WIFI
, kService3
, kIpAddress2
,
203 { "Online -> Online (same default service, new IP address, same DNS)",
204 { NetworkChangeNotifier::CONNECTION_WIFI
, kService3
, kIpAddress1
,
206 { true, shill::kTypeWifi
, "", kService3
, kIpAddress2
, kDnsServers1
},
207 { NetworkChangeNotifier::CONNECTION_WIFI
, kService3
, kIpAddress2
,
211 { "Online -> Online (same default service, same IP address, new DNS)",
212 { NetworkChangeNotifier::CONNECTION_WIFI
, kService3
, kIpAddress2
,
214 { true, shill::kTypeWifi
, "", kService3
, kIpAddress2
, kDnsServers2
},
215 { NetworkChangeNotifier::CONNECTION_WIFI
, kService3
, kIpAddress2
,
221 TEST_F(NetworkChangeNotifierChromeosUpdateTest
, UpdateDefaultNetwork
) {
222 for (size_t i
= 0; i
< arraysize(test_cases
); ++i
) {
223 SCOPED_TRACE(test_cases
[i
].test_description
);
224 SetNotifierState(test_cases
[i
].initial_state
);
225 SetDefaultNetworkState(test_cases
[i
].default_network_state
);
226 bool type_changed
= false, ip_changed
= false, dns_changed
= false;
227 ProcessDefaultNetworkUpdate(&type_changed
, &ip_changed
, &dns_changed
);
228 VerifyNotifierState(test_cases
[i
].expected_state
);
229 EXPECT_TRUE(type_changed
== test_cases
[i
].expected_type_changed
);
230 EXPECT_TRUE(ip_changed
== test_cases
[i
].expected_ip_changed
);
231 EXPECT_TRUE(dns_changed
== test_cases
[i
].expected_dns_changed
);
235 } // namespace chromeos