OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chromeos/network/network_change_notifier_chromeos.h" | 5 #include "chromeos/network/network_change_notifier_chromeos.h" |
6 | 6 |
7 #include <string> | 7 #include <string> |
8 | 8 |
9 #include "base/basictypes.h" | 9 #include "base/basictypes.h" |
| 10 #include "base/strings/string_split.h" |
10 #include "chromeos/network/network_change_notifier_factory_chromeos.h" | 11 #include "chromeos/network/network_change_notifier_factory_chromeos.h" |
11 #include "chromeos/network/network_state.h" | 12 #include "chromeos/network/network_state.h" |
12 #include "net/base/network_change_notifier.h" | 13 #include "net/base/network_change_notifier.h" |
13 #include "testing/gtest/include/gtest/gtest.h" | 14 #include "testing/gtest/include/gtest/gtest.h" |
14 #include "third_party/cros_system_api/dbus/service_constants.h" | 15 #include "third_party/cros_system_api/dbus/service_constants.h" |
15 | 16 |
16 namespace chromeos { | 17 namespace chromeos { |
17 | 18 |
| 19 namespace { |
| 20 |
| 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"; |
| 28 |
| 29 struct NotifierState { |
| 30 net::NetworkChangeNotifier::ConnectionType type; |
| 31 const char* service_path; |
| 32 const char* ip_address; |
| 33 const char* dns_servers; |
| 34 }; |
| 35 |
| 36 struct DefaultNetworkState { |
| 37 bool is_connected; |
| 38 const char* type; |
| 39 const char* technology; |
| 40 const char* service_path; |
| 41 const char* ip_address; |
| 42 const char* dns_servers; |
| 43 }; |
| 44 |
| 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; |
| 53 }; |
| 54 |
| 55 } // namespace |
| 56 |
18 using net::NetworkChangeNotifier; | 57 using net::NetworkChangeNotifier; |
19 | 58 |
20 TEST(NetworkChangeNotifierChromeosTest, ConnectionTypeFromShill) { | 59 TEST(NetworkChangeNotifierChromeosTest, ConnectionTypeFromShill) { |
21 struct TypeMapping { | 60 struct TypeMapping { |
22 const char* shill_type; | 61 const char* shill_type; |
23 const char* technology; | 62 const char* technology; |
24 NetworkChangeNotifier::ConnectionType connection_type; | 63 NetworkChangeNotifier::ConnectionType connection_type; |
25 }; | 64 }; |
26 TypeMapping type_mappings[] = { | 65 TypeMapping type_mappings[] = { |
27 { flimflam::kTypeEthernet, "", NetworkChangeNotifier::CONNECTION_ETHERNET }, | 66 { flimflam::kTypeEthernet, "", NetworkChangeNotifier::CONNECTION_ETHERNET }, |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
60 EXPECT_EQ(type_mappings[i].connection_type, type); | 99 EXPECT_EQ(type_mappings[i].connection_type, type); |
61 } | 100 } |
62 } | 101 } |
63 | 102 |
64 class NetworkChangeNotifierChromeosUpdateTest : public testing::Test { | 103 class NetworkChangeNotifierChromeosUpdateTest : public testing::Test { |
65 protected: | 104 protected: |
66 NetworkChangeNotifierChromeosUpdateTest() : default_network_("") { | 105 NetworkChangeNotifierChromeosUpdateTest() : default_network_("") { |
67 } | 106 } |
68 virtual ~NetworkChangeNotifierChromeosUpdateTest() {} | 107 virtual ~NetworkChangeNotifierChromeosUpdateTest() {} |
69 | 108 |
70 void SetNotifierState(NetworkChangeNotifier::ConnectionType type, | 109 void SetNotifierState(const NotifierState& notifier_state) { |
71 std::string service_path, | 110 notifier_.connection_type_ = notifier_state.type; |
72 std::string ip_address) { | 111 notifier_.service_path_ = notifier_state.service_path; |
73 notifier_.ip_address_ = ip_address; | 112 notifier_.ip_address_ = notifier_state.ip_address; |
74 notifier_.service_path_ = service_path; | 113 std::vector<std::string> dns_servers; |
75 notifier_.connection_type_ = type; | 114 base::SplitString(notifier_state.dns_servers, ',', &dns_servers); |
| 115 notifier_.dns_servers_ = dns_servers; |
76 } | 116 } |
77 | 117 |
78 void VerifyNotifierState(NetworkChangeNotifier::ConnectionType expected_type, | 118 void VerifyNotifierState(const NotifierState& notifier_state) { |
79 std::string expected_service_path, | 119 EXPECT_EQ(notifier_state.type, notifier_.connection_type_); |
80 std::string expected_ip_address) { | 120 EXPECT_EQ(notifier_state.service_path, notifier_.service_path_); |
81 EXPECT_EQ(expected_type, notifier_.connection_type_); | 121 EXPECT_EQ(notifier_state.ip_address, notifier_.ip_address_); |
82 EXPECT_EQ(expected_ip_address, notifier_.ip_address_); | 122 std::vector<std::string> dns_servers; |
83 EXPECT_EQ(expected_service_path, notifier_.service_path_); | 123 base::SplitString(notifier_state.dns_servers, ',', &dns_servers); |
| 124 EXPECT_EQ(dns_servers, notifier_.dns_servers_); |
84 } | 125 } |
85 | 126 |
86 // Sets the default network state used for notifier updates. | 127 // Sets the default network state used for notifier updates. |
87 void SetDefaultNetworkState(bool is_connected, | 128 void SetDefaultNetworkState( |
88 std::string type, | 129 const DefaultNetworkState& default_network_state) { |
89 std::string technology, | 130 if (default_network_state.is_connected) |
90 std::string service_path, | |
91 std::string ip_address) { | |
92 if (is_connected) | |
93 default_network_.connection_state_ = flimflam::kStateOnline; | 131 default_network_.connection_state_ = flimflam::kStateOnline; |
94 else | 132 else |
95 default_network_.connection_state_ = flimflam::kStateConfiguration; | 133 default_network_.connection_state_ = flimflam::kStateConfiguration; |
96 default_network_.type_ = type; | 134 default_network_.type_ = default_network_state.type; |
97 default_network_.technology_ = technology; | 135 default_network_.technology_ = default_network_state.technology; |
98 default_network_.path_ = service_path; | 136 default_network_.path_ = default_network_state.service_path; |
99 default_network_.ip_address_ = ip_address; | 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); |
100 } | 141 } |
101 | 142 |
102 // Process an default network update based on the state of |default_network_|. | 143 // Process an default network update based on the state of |default_network_|. |
103 void ProcessDefaultNetworkUpdate(bool* type_changed, | 144 void ProcessDefaultNetworkUpdate(bool* type_changed, |
104 bool* ip_changed, | 145 bool* ip_changed, |
105 bool* dns_changed) { | 146 bool* dns_changed) { |
106 notifier_.UpdateState(&default_network_, type_changed, ip_changed, | 147 notifier_.UpdateState(&default_network_, type_changed, ip_changed, |
107 dns_changed); | 148 dns_changed); |
108 } | 149 } |
109 | 150 |
110 private: | 151 private: |
111 NetworkState default_network_; | 152 NetworkState default_network_; |
112 NetworkChangeNotifierChromeos notifier_; | 153 NetworkChangeNotifierChromeos notifier_; |
113 }; | 154 }; |
114 | 155 |
115 TEST_F(NetworkChangeNotifierChromeosUpdateTest, UpdateDefaultNetworkOffline) { | 156 NotifierUpdateTestCase test_cases[] = { |
116 // Test that Online to Offline transitions are correctly handled. | 157 { "Online -> Offline", |
117 SetNotifierState(NetworkChangeNotifier::CONNECTION_ETHERNET, "/service/1", | 158 { NetworkChangeNotifier::CONNECTION_ETHERNET, kService1, kIpAddress1, |
118 "192.168.1.1"); | 159 kDnsServers1 }, |
119 SetDefaultNetworkState(false, // offline. | 160 { false, flimflam::kTypeEthernet, "", kService1, "", "" }, |
120 flimflam::kTypeEthernet, "", "/service/1", ""); | 161 { NetworkChangeNotifier::CONNECTION_NONE, "", "", "" }, |
121 bool type_changed = false, ip_changed = false, dns_changed = false; | 162 true, true, true |
122 ProcessDefaultNetworkUpdate(&type_changed, &ip_changed, &dns_changed); | 163 }, |
123 VerifyNotifierState(NetworkChangeNotifier::CONNECTION_NONE, "", ""); | 164 { "Offline -> Offline", |
124 EXPECT_TRUE(type_changed); | 165 { NetworkChangeNotifier::CONNECTION_NONE, "", "", "" }, |
125 EXPECT_TRUE(ip_changed); | 166 { false, flimflam::kTypeEthernet, "", kService1, kIpAddress1, |
126 EXPECT_TRUE(dns_changed); | 167 kDnsServers1 }, |
127 } | 168 { NetworkChangeNotifier::CONNECTION_NONE, "", "", "" }, |
| 169 false, false, false |
| 170 }, |
| 171 { "Offline -> Online", |
| 172 { NetworkChangeNotifier::CONNECTION_NONE, "", "", "" }, |
| 173 { true, flimflam::kTypeEthernet, "", kService1, kIpAddress1, kDnsServers1 }, |
| 174 { NetworkChangeNotifier::CONNECTION_ETHERNET, kService1, kIpAddress1, |
| 175 kDnsServers1 }, |
| 176 true, true, true |
| 177 }, |
| 178 { "Online -> Online (new default service, different connection type)", |
| 179 { NetworkChangeNotifier::CONNECTION_ETHERNET, kService1, kIpAddress1, |
| 180 kDnsServers1 }, |
| 181 { true, flimflam::kTypeWifi, "", kService2, kIpAddress1, kDnsServers1 }, |
| 182 { NetworkChangeNotifier::CONNECTION_WIFI, kService2, kIpAddress1, |
| 183 kDnsServers1 }, |
| 184 true, true, true |
| 185 }, |
| 186 { "Online -> Online (new default service, same connection type)", |
| 187 { NetworkChangeNotifier::CONNECTION_WIFI, kService2, kIpAddress1, |
| 188 kDnsServers1 }, |
| 189 { true, flimflam::kTypeWifi, "", kService3, kIpAddress1, kDnsServers1 }, |
| 190 { NetworkChangeNotifier::CONNECTION_WIFI, kService3, kIpAddress1, |
| 191 kDnsServers1 }, |
| 192 false, true, true |
| 193 }, |
| 194 { "Online -> Online (same default service, new IP address, same DNS)", |
| 195 { NetworkChangeNotifier::CONNECTION_WIFI, kService3, kIpAddress1, |
| 196 kDnsServers1 }, |
| 197 { true, flimflam::kTypeWifi, "", kService3, kIpAddress2, kDnsServers1 }, |
| 198 { NetworkChangeNotifier::CONNECTION_WIFI, kService3, kIpAddress2, |
| 199 kDnsServers1 }, |
| 200 false, true, false |
| 201 }, |
| 202 { "Online -> Online (same default service, same IP address, new DNS)", |
| 203 { NetworkChangeNotifier::CONNECTION_WIFI, kService3, kIpAddress2, |
| 204 kDnsServers1 }, |
| 205 { true, flimflam::kTypeWifi, "", kService3, kIpAddress2, kDnsServers2 }, |
| 206 { NetworkChangeNotifier::CONNECTION_WIFI, kService3, kIpAddress2, |
| 207 kDnsServers2 }, |
| 208 false, false, true |
| 209 } |
| 210 }; |
128 | 211 |
129 TEST_F(NetworkChangeNotifierChromeosUpdateTest, UpdateDefaultNetworkOnline) { | 212 TEST_F(NetworkChangeNotifierChromeosUpdateTest, UpdateDefaultNetwork) { |
130 // Test that Offline to Online transitions are correctly handled. | 213 for (size_t i = 0; i < arraysize(test_cases); ++i) { |
131 SetNotifierState(NetworkChangeNotifier::CONNECTION_NONE, "", ""); | 214 SCOPED_TRACE(test_cases[i].test_description); |
132 | 215 SetNotifierState(test_cases[i].initial_state); |
133 SetDefaultNetworkState(false, // offline. | 216 SetDefaultNetworkState(test_cases[i].default_network_state); |
134 flimflam::kTypeEthernet, "", | 217 bool type_changed = false, ip_changed = false, dns_changed = false; |
135 "192.168.0.1", "/service/1"); | 218 ProcessDefaultNetworkUpdate(&type_changed, &ip_changed, &dns_changed); |
136 bool type_changed = false, ip_changed = false, dns_changed = false; | 219 VerifyNotifierState(test_cases[i].expected_state); |
137 ProcessDefaultNetworkUpdate(&type_changed, &ip_changed, &dns_changed); | 220 EXPECT_TRUE(type_changed == test_cases[i].expected_type_changed); |
138 // If the new default network is still offline, nothing should have changed. | 221 EXPECT_TRUE(ip_changed == test_cases[i].expected_ip_changed); |
139 VerifyNotifierState(NetworkChangeNotifier::CONNECTION_NONE, "", ""); | 222 EXPECT_TRUE(dns_changed == test_cases[i].expected_dns_changed); |
140 EXPECT_FALSE(type_changed); | 223 } |
141 EXPECT_FALSE(ip_changed); | |
142 EXPECT_FALSE(dns_changed); | |
143 | |
144 SetDefaultNetworkState(true, // online. | |
145 flimflam::kTypeEthernet, "", "/service/1", | |
146 "192.168.0.1"); | |
147 ProcessDefaultNetworkUpdate(&type_changed, &ip_changed, &dns_changed); | |
148 // Now the new default network is online, so this should trigger a notifier | |
149 // state change. | |
150 VerifyNotifierState(NetworkChangeNotifier::CONNECTION_ETHERNET, "/service/1", | |
151 "192.168.0.1"); | |
152 EXPECT_TRUE(type_changed); | |
153 EXPECT_TRUE(ip_changed); | |
154 EXPECT_TRUE(dns_changed); | |
155 } | |
156 | |
157 TEST_F(NetworkChangeNotifierChromeosUpdateTest, UpdateDefaultNetworkChanged) { | |
158 // Test that Online to Online transitions (default network changes) are | |
159 // correctly handled. | |
160 SetNotifierState(NetworkChangeNotifier::CONNECTION_ETHERNET, "/service/1", | |
161 "192.168.1.1"); | |
162 | |
163 SetDefaultNetworkState(true, // online. | |
164 flimflam::kTypeWifi, "", "/service/2", "192.168.1.2"); | |
165 bool type_changed = false, ip_changed = false, dns_changed = false; | |
166 ProcessDefaultNetworkUpdate(&type_changed, &ip_changed, &dns_changed); | |
167 VerifyNotifierState(NetworkChangeNotifier::CONNECTION_WIFI, "/service/2", | |
168 "192.168.1.2" ); | |
169 EXPECT_TRUE(type_changed); | |
170 EXPECT_TRUE(ip_changed); | |
171 EXPECT_TRUE(dns_changed); | |
172 | |
173 SetDefaultNetworkState(true, // online. | |
174 flimflam::kTypeWifi, "", "/service/3", "192.168.1.2"); | |
175 ProcessDefaultNetworkUpdate(&type_changed, &ip_changed, &dns_changed); | |
176 VerifyNotifierState(NetworkChangeNotifier::CONNECTION_WIFI, "/service/3", | |
177 "192.168.1.2" ); | |
178 EXPECT_FALSE(type_changed); | |
179 // A service path change (even with a corresponding IP change) should still | |
180 // trigger an IP address update to observers. | |
181 EXPECT_TRUE(ip_changed); | |
182 EXPECT_TRUE(dns_changed); | |
183 } | 224 } |
184 | 225 |
185 } // namespace chromeos | 226 } // namespace chromeos |
OLD | NEW |