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* 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 { flimflam::kTypeEthernet
, "", NetworkChangeNotifier::CONNECTION_ETHERNET
},
67 { flimflam::kTypeWifi
, "", NetworkChangeNotifier::CONNECTION_WIFI
},
68 { flimflam::kTypeWimax
, "", NetworkChangeNotifier::CONNECTION_4G
},
69 { "unknown type", "unknown technology",
70 NetworkChangeNotifier::CONNECTION_UNKNOWN
},
71 { flimflam::kTypeCellular
, flimflam::kNetworkTechnology1Xrtt
,
72 NetworkChangeNotifier::CONNECTION_2G
},
73 { flimflam::kTypeCellular
, flimflam::kNetworkTechnologyGprs
,
74 NetworkChangeNotifier::CONNECTION_2G
},
75 { flimflam::kTypeCellular
, flimflam::kNetworkTechnologyEdge
,
76 NetworkChangeNotifier::CONNECTION_2G
},
77 { flimflam::kTypeCellular
, flimflam::kNetworkTechnologyEvdo
,
78 NetworkChangeNotifier::CONNECTION_3G
},
79 { flimflam::kTypeCellular
, flimflam::kNetworkTechnologyGsm
,
80 NetworkChangeNotifier::CONNECTION_3G
},
81 { flimflam::kTypeCellular
, flimflam::kNetworkTechnologyUmts
,
82 NetworkChangeNotifier::CONNECTION_3G
},
83 { flimflam::kTypeCellular
, flimflam::kNetworkTechnologyHspa
,
84 NetworkChangeNotifier::CONNECTION_3G
},
85 { flimflam::kTypeCellular
, flimflam::kNetworkTechnologyHspaPlus
,
86 NetworkChangeNotifier::CONNECTION_4G
},
87 { flimflam::kTypeCellular
, flimflam::kNetworkTechnologyLte
,
88 NetworkChangeNotifier::CONNECTION_4G
},
89 { flimflam::kTypeCellular
, flimflam::kNetworkTechnologyLteAdvanced
,
90 NetworkChangeNotifier::CONNECTION_4G
},
91 { flimflam::kTypeCellular
, "unknown technology",
92 NetworkChangeNotifier::CONNECTION_2G
}
95 for (size_t i
= 0; i
< ARRAYSIZE_UNSAFE(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 virtual ~NetworkChangeNotifierChromeosUpdateTest() {}
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 if (default_network_state
.is_connected
)
131 default_network_
.connection_state_
= flimflam::kStateOnline
;
133 default_network_
.connection_state_
= flimflam::kStateConfiguration
;
134 default_network_
.type_
= default_network_state
.type
;
135 default_network_
.technology_
= default_network_state
.technology
;
136 default_network_
.path_
= default_network_state
.service_path
;
137 default_network_
.set_ip_address(default_network_state
.ip_address
);
138 std::vector
<std::string
> dns_servers
;
139 base::SplitString(default_network_state
.dns_servers
, ',', &dns_servers
);
140 default_network_
.set_dns_servers(dns_servers
);
143 // Process an default network update based on the state of |default_network_|.
144 void ProcessDefaultNetworkUpdate(bool* type_changed
,
147 notifier_
.UpdateState(&default_network_
, type_changed
, ip_changed
,
152 NetworkState default_network_
;
153 NetworkChangeNotifierChromeos notifier_
;
156 NotifierUpdateTestCase test_cases
[] = {
157 { "Online -> Offline",
158 { NetworkChangeNotifier::CONNECTION_ETHERNET
, kService1
, kIpAddress1
,
160 { false, flimflam::kTypeEthernet
, "", kService1
, "", "" },
161 { NetworkChangeNotifier::CONNECTION_NONE
, "", "", "" },
164 { "Offline -> Offline",
165 { NetworkChangeNotifier::CONNECTION_NONE
, "", "", "" },
166 { false, flimflam::kTypeEthernet
, "", kService1
, kIpAddress1
,
168 { NetworkChangeNotifier::CONNECTION_NONE
, "", "", "" },
171 { "Offline -> Online",
172 { NetworkChangeNotifier::CONNECTION_NONE
, "", "", "" },
173 { true, flimflam::kTypeEthernet
, "", kService1
, kIpAddress1
, kDnsServers1
},
174 { NetworkChangeNotifier::CONNECTION_ETHERNET
, kService1
, kIpAddress1
,
178 { "Online -> Online (new default service, different connection type)",
179 { NetworkChangeNotifier::CONNECTION_ETHERNET
, kService1
, kIpAddress1
,
181 { true, flimflam::kTypeWifi
, "", kService2
, kIpAddress1
, kDnsServers1
},
182 { NetworkChangeNotifier::CONNECTION_WIFI
, kService2
, kIpAddress1
,
186 { "Online -> Online (new default service, same connection type)",
187 { NetworkChangeNotifier::CONNECTION_WIFI
, kService2
, kIpAddress1
,
189 { true, flimflam::kTypeWifi
, "", kService3
, kIpAddress1
, kDnsServers1
},
190 { NetworkChangeNotifier::CONNECTION_WIFI
, kService3
, kIpAddress1
,
194 { "Online -> Online (same default service, new IP address, same DNS)",
195 { NetworkChangeNotifier::CONNECTION_WIFI
, kService3
, kIpAddress1
,
197 { true, flimflam::kTypeWifi
, "", kService3
, kIpAddress2
, kDnsServers1
},
198 { NetworkChangeNotifier::CONNECTION_WIFI
, kService3
, kIpAddress2
,
202 { "Online -> Online (same default service, same IP address, new DNS)",
203 { NetworkChangeNotifier::CONNECTION_WIFI
, kService3
, kIpAddress2
,
205 { true, flimflam::kTypeWifi
, "", kService3
, kIpAddress2
, kDnsServers2
},
206 { NetworkChangeNotifier::CONNECTION_WIFI
, kService3
, kIpAddress2
,
212 TEST_F(NetworkChangeNotifierChromeosUpdateTest
, UpdateDefaultNetwork
) {
213 for (size_t i
= 0; i
< arraysize(test_cases
); ++i
) {
214 SCOPED_TRACE(test_cases
[i
].test_description
);
215 SetNotifierState(test_cases
[i
].initial_state
);
216 SetDefaultNetworkState(test_cases
[i
].default_network_state
);
217 bool type_changed
= false, ip_changed
= false, dns_changed
= false;
218 ProcessDefaultNetworkUpdate(&type_changed
, &ip_changed
, &dns_changed
);
219 VerifyNotifierState(test_cases
[i
].expected_state
);
220 EXPECT_TRUE(type_changed
== test_cases
[i
].expected_type_changed
);
221 EXPECT_TRUE(ip_changed
== test_cases
[i
].expected_ip_changed
);
222 EXPECT_TRUE(dns_changed
== test_cases
[i
].expected_dns_changed
);
226 } // namespace chromeos