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