Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(31)

Side by Side Diff: chromeos/network/network_change_notifier_chromeos_unittest.cc

Issue 12634019: NetworkChangeNotifierChromeos: Handle IPConfig property changes on the default network (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 7 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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 "chromeos/network/network_change_notifier_factory_chromeos.h" 10 #include "chromeos/network/network_change_notifier_factory_chromeos.h"
11 #include "chromeos/network/network_state.h" 11 #include "chromeos/network/network_state.h"
12 #include "net/base/network_change_notifier.h" 12 #include "net/base/network_change_notifier.h"
13 #include "testing/gtest/include/gtest/gtest.h" 13 #include "testing/gtest/include/gtest/gtest.h"
14 #include "third_party/cros_system_api/dbus/service_constants.h" 14 #include "third_party/cros_system_api/dbus/service_constants.h"
15 15
16 namespace chromeos { 16 namespace chromeos {
17 17
18 namespace {
19
20 const char kDnsServers1[] = "192.168.0.1,192.168.0.2";
21 const char kDnsServers2[] = "192.168.3.1,192.168.3.2";
22 const char kIpAddress1[] = "192.168.1.1";
23 const char kIpAddress2[] = "192.168.1.2";
24 const char kService1[] = "/service/1";
25 const char kService2[] = "/service/2";
26 const char kService3[] = "/service/3";
27
28 }
29
18 using net::NetworkChangeNotifier; 30 using net::NetworkChangeNotifier;
19 31
20 TEST(NetworkChangeNotifierChromeosTest, ConnectionTypeFromShill) { 32 TEST(NetworkChangeNotifierChromeosTest, ConnectionTypeFromShill) {
21 struct TypeMapping { 33 struct TypeMapping {
22 const char* shill_type; 34 const char* shill_type;
23 const char* technology; 35 const char* technology;
24 NetworkChangeNotifier::ConnectionType connection_type; 36 NetworkChangeNotifier::ConnectionType connection_type;
25 }; 37 };
26 TypeMapping type_mappings[] = { 38 TypeMapping type_mappings[] = {
27 { flimflam::kTypeEthernet, "", NetworkChangeNotifier::CONNECTION_ETHERNET }, 39 { flimflam::kTypeEthernet, "", NetworkChangeNotifier::CONNECTION_ETHERNET },
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after
62 } 74 }
63 75
64 class NetworkChangeNotifierChromeosUpdateTest : public testing::Test { 76 class NetworkChangeNotifierChromeosUpdateTest : public testing::Test {
65 protected: 77 protected:
66 NetworkChangeNotifierChromeosUpdateTest() : default_network_("") { 78 NetworkChangeNotifierChromeosUpdateTest() : default_network_("") {
67 } 79 }
68 virtual ~NetworkChangeNotifierChromeosUpdateTest() {} 80 virtual ~NetworkChangeNotifierChromeosUpdateTest() {}
69 81
70 void SetNotifierState(NetworkChangeNotifier::ConnectionType type, 82 void SetNotifierState(NetworkChangeNotifier::ConnectionType type,
71 std::string service_path, 83 std::string service_path,
72 std::string ip_address) { 84 std::string ip_address,
85 std::string dns_servers) {
86 notifier_.connection_type_ = type;
87 notifier_.service_path_ = service_path;
73 notifier_.ip_address_ = ip_address; 88 notifier_.ip_address_ = ip_address;
74 notifier_.service_path_ = service_path; 89 notifier_.dns_servers_ = dns_servers;
75 notifier_.connection_type_ = type;
76 } 90 }
77 91
78 void VerifyNotifierState(NetworkChangeNotifier::ConnectionType expected_type, 92 void VerifyNotifierState(NetworkChangeNotifier::ConnectionType expected_type,
79 std::string expected_service_path, 93 std::string expected_service_path,
80 std::string expected_ip_address) { 94 std::string expected_ip_address,
95 std::string expected_dns_servers) {
81 EXPECT_EQ(expected_type, notifier_.connection_type_); 96 EXPECT_EQ(expected_type, notifier_.connection_type_);
97 EXPECT_EQ(expected_service_path, notifier_.service_path_);
82 EXPECT_EQ(expected_ip_address, notifier_.ip_address_); 98 EXPECT_EQ(expected_ip_address, notifier_.ip_address_);
83 EXPECT_EQ(expected_service_path, notifier_.service_path_); 99 EXPECT_EQ(expected_dns_servers, notifier_.dns_servers_);
84 } 100 }
85 101
86 // Sets the default network state used for notifier updates. 102 // Sets the default network state used for notifier updates.
87 void SetDefaultNetworkState(bool is_connected, 103 void SetDefaultNetworkState(bool is_connected,
88 std::string type, 104 std::string type,
89 std::string technology, 105 std::string technology,
90 std::string service_path, 106 std::string service_path,
91 std::string ip_address) { 107 std::string ip_address,
108 std::string dns_servers) {
92 if (is_connected) 109 if (is_connected)
93 default_network_.connection_state_ = flimflam::kStateOnline; 110 default_network_.connection_state_ = flimflam::kStateOnline;
94 else 111 else
95 default_network_.connection_state_ = flimflam::kStateConfiguration; 112 default_network_.connection_state_ = flimflam::kStateConfiguration;
96 default_network_.type_ = type; 113 default_network_.type_ = type;
97 default_network_.technology_ = technology; 114 default_network_.technology_ = technology;
98 default_network_.path_ = service_path; 115 default_network_.path_ = service_path;
99 default_network_.ip_address_ = ip_address; 116 default_network_.ip_address_ = ip_address;
117 default_network_.dns_servers_ = dns_servers;
100 } 118 }
101 119
102 // Process an default network update based on the state of |default_network_|. 120 // Process an default network update based on the state of |default_network_|.
103 void ProcessDefaultNetworkUpdate(bool* type_changed, 121 void ProcessDefaultNetworkUpdate(bool* type_changed,
104 bool* ip_changed, 122 bool* ip_changed,
105 bool* dns_changed) { 123 bool* dns_changed) {
106 notifier_.UpdateState(&default_network_, type_changed, ip_changed, 124 notifier_.UpdateState(&default_network_, type_changed, ip_changed,
107 dns_changed); 125 dns_changed);
108 } 126 }
109 127
110 private: 128 private:
111 NetworkState default_network_; 129 NetworkState default_network_;
112 NetworkChangeNotifierChromeos notifier_; 130 NetworkChangeNotifierChromeos notifier_;
113 }; 131 };
114 132
133 struct NotifierState {
134 NetworkChangeNotifier::ConnectionType type;
135 const char* service_path;
136 const char* ip_address;
137 const char* dns_servers;
138 };
139
140 struct DefaultNetworkState {
141 bool is_connected;
142 const char* type;
143 const char* technology;
144 const char* service_path;
145 const char* ip_address;
146 const char* dns_servers;
147 };
148
149 struct NotifierUpdateTestCase {
150 const char* test_description;
151 NotifierState initial_state;
152 DefaultNetworkState default_network_state;
153 NotifierState expected_state;
154 bool expected_type_changed;
155 bool expected_ip_changed;
156 bool expected_dns_changed;
157 };
158
159 NotifierUpdateTestCase test_cases[] = {
stevenjb 2013/03/28 20:30:58 To make this more readable, please: * Start each s
gauravsh 2013/03/29 00:02:11 Done. (I used the clang chromium style formatter t
stevenjb 2013/03/29 00:21:36 Interesting. Well, this looks much nicer now, so t
160 { "Online -> Offline", { NetworkChangeNotifier::CONNECTION_ETHERNET,
161 kService1, kIpAddress1, kDnsServers1 },
162 { false, flimflam::kTypeEthernet, "", kService1, "", "" },
163 { NetworkChangeNotifier::CONNECTION_NONE, "", "", "" }, true, true, true },
164 { "Offline -> Offline",
165 { NetworkChangeNotifier::CONNECTION_NONE, "", "", "" },
166 { false, flimflam::kTypeEthernet, "", kService1, kIpAddress1,
167 kDnsServers1 },
168 { NetworkChangeNotifier::CONNECTION_NONE, "", "", "" }, false, false,
169 false },
170 { "Offline -> Online", { NetworkChangeNotifier::CONNECTION_NONE, "", "", "" },
171 { true, flimflam::kTypeEthernet, "", kService1, kIpAddress1, kDnsServers1 },
172 { NetworkChangeNotifier::CONNECTION_ETHERNET, kService1, kIpAddress1,
173 kDnsServers1 },
174 true, true, true },
175 { "Online -> Online (new default service, different connection type)",
176 { NetworkChangeNotifier::CONNECTION_ETHERNET, kService1, kIpAddress1,
177 kDnsServers1 },
178 { true, flimflam::kTypeWifi, "", kService2, kIpAddress1, kDnsServers1 },
179 { NetworkChangeNotifier::CONNECTION_WIFI, kService2, kIpAddress1,
180 kDnsServers1 },
181 true, true, true },
182 { "Online -> Online (new default service, same connection type)",
183 { NetworkChangeNotifier::CONNECTION_WIFI, kService2, kIpAddress1,
184 kDnsServers1 },
185 { true, flimflam::kTypeWifi, "", kService3, kIpAddress1, kDnsServers1 },
186 { NetworkChangeNotifier::CONNECTION_WIFI, kService3, kIpAddress1,
187 kDnsServers1 },
188 false, true, true },
189 { "Online -> Online (same default service, new IP address, same DNS)",
190 { NetworkChangeNotifier::CONNECTION_WIFI, kService3, kIpAddress1,
191 kDnsServers1 },
192 { true, flimflam::kTypeWifi, "", kService3, kIpAddress2, kDnsServers1 },
193 { NetworkChangeNotifier::CONNECTION_WIFI, kService3, kIpAddress2,
194 kDnsServers1 },
195 false, true, false },
196 { "Online -> Online (same default service, same IP address, new DNS)",
197 { NetworkChangeNotifier::CONNECTION_WIFI, kService3, kIpAddress2,
198 kDnsServers1 },
199 { true, flimflam::kTypeWifi, "", kService3, kIpAddress2, kDnsServers2 },
200 { NetworkChangeNotifier::CONNECTION_WIFI, kService3, kIpAddress2,
201 kDnsServers2 },
202 false, false, true }
203 };
204
115 TEST_F(NetworkChangeNotifierChromeosUpdateTest, UpdateDefaultNetworkOffline) { 205 TEST_F(NetworkChangeNotifierChromeosUpdateTest, UpdateDefaultNetworkOffline) {
116 // Test that Online to Offline transitions are correctly handled. 206 for (size_t i = 0; i < arraysize(test_cases); ++i) {
117 SetNotifierState(NetworkChangeNotifier::CONNECTION_ETHERNET, "/service/1", 207 SCOPED_TRACE(test_cases[i].test_description);
118 "192.168.1.1"); 208 SetNotifierState(test_cases[i].initial_state.type,
119 SetDefaultNetworkState(false, // offline. 209 test_cases[i].initial_state.service_path,
120 flimflam::kTypeEthernet, "", "/service/1", ""); 210 test_cases[i].initial_state.ip_address,
121 bool type_changed = false, ip_changed = false, dns_changed = false; 211 test_cases[i].initial_state.dns_servers);
stevenjb 2013/03/28 20:30:58 This could be further simplified by having SetNoti
gauravsh 2013/03/29 00:02:11 Good idea. Done.
122 ProcessDefaultNetworkUpdate(&type_changed, &ip_changed, &dns_changed); 212 SetDefaultNetworkState(
123 VerifyNotifierState(NetworkChangeNotifier::CONNECTION_NONE, "", ""); 213 test_cases[i].default_network_state.is_connected,
124 EXPECT_TRUE(type_changed); 214 test_cases[i].default_network_state.type,
125 EXPECT_TRUE(ip_changed); 215 test_cases[i].default_network_state.technology,
126 EXPECT_TRUE(dns_changed); 216 test_cases[i].default_network_state.service_path,
127 } 217 test_cases[i].default_network_state.ip_address,
128 218 test_cases[i].default_network_state.dns_servers);
stevenjb 2013/03/28 20:30:58 Same here for DefaultNotifierState
gauravsh 2013/03/29 00:02:11 Done.
129 TEST_F(NetworkChangeNotifierChromeosUpdateTest, UpdateDefaultNetworkOnline) { 219 bool type_changed = false, ip_changed = false, dns_changed = false;
130 // Test that Offline to Online transitions are correctly handled. 220 ProcessDefaultNetworkUpdate(&type_changed, &ip_changed, &dns_changed);
131 SetNotifierState(NetworkChangeNotifier::CONNECTION_NONE, "", ""); 221 VerifyNotifierState(test_cases[i].expected_state.type,
132 222 test_cases[i].expected_state.service_path,
133 SetDefaultNetworkState(false, // offline. 223 test_cases[i].expected_state.ip_address,
134 flimflam::kTypeEthernet, "", 224 test_cases[i].expected_state.dns_servers);
stevenjb 2013/03/28 20:30:58 And here with NotifierState&
gauravsh 2013/03/29 00:02:11 Done.
135 "192.168.0.1", "/service/1"); 225 EXPECT_TRUE(type_changed == test_cases[i].expected_type_changed);
136 bool type_changed = false, ip_changed = false, dns_changed = false; 226 EXPECT_TRUE(ip_changed == test_cases[i].expected_ip_changed);
137 ProcessDefaultNetworkUpdate(&type_changed, &ip_changed, &dns_changed); 227 EXPECT_TRUE(dns_changed == test_cases[i].expected_dns_changed);
138 // If the new default network is still offline, nothing should have changed. 228 }
139 VerifyNotifierState(NetworkChangeNotifier::CONNECTION_NONE, "", "");
140 EXPECT_FALSE(type_changed);
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 } 229 }
184 230
185 } // namespace chromeos 231 } // namespace chromeos
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698