Implement proximity_auth::WireMessage::Serialize().
[chromium-blink-merge.git] / chromeos / network / network_device_handler_unittest.cc
blob1da33978011328f8dded3a90afdfac0efde850c2
1 // Copyright 2013 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 "base/bind.h"
6 #include "base/memory/scoped_ptr.h"
7 #include "base/message_loop/message_loop.h"
8 #include "base/values.h"
9 #include "chromeos/dbus/dbus_thread_manager.h"
10 #include "chromeos/dbus/fake_shill_device_client.h"
11 #include "chromeos/dbus/fake_shill_manager_client.h"
12 #include "chromeos/network/network_device_handler_impl.h"
13 #include "chromeos/network/network_state_handler.h"
14 #include "testing/gtest/include/gtest/gtest.h"
15 #include "third_party/cros_system_api/dbus/service_constants.h"
17 namespace chromeos {
19 namespace {
21 const char kDefaultCellularDevicePath[] = "stub_cellular_device";
22 const char kUnknownCellularDevicePath[] = "unknown_cellular_device";
23 const char kDefaultWifiDevicePath[] = "stub_wifi_device";
24 const char kResultSuccess[] = "success";
25 const char kDefaultPin[] = "1111";
27 } // namespace
29 class NetworkDeviceHandlerTest : public testing::Test {
30 public:
31 NetworkDeviceHandlerTest() : fake_device_client_(NULL) {}
32 ~NetworkDeviceHandlerTest() override {}
34 void SetUp() override {
35 fake_device_client_ = new FakeShillDeviceClient;
36 DBusThreadManager::GetSetterForTesting()->SetShillDeviceClient(
37 scoped_ptr<ShillDeviceClient>(fake_device_client_));
39 success_callback_ = base::Bind(&NetworkDeviceHandlerTest::SuccessCallback,
40 base::Unretained(this));
41 properties_success_callback_ =
42 base::Bind(&NetworkDeviceHandlerTest::PropertiesSuccessCallback,
43 base::Unretained(this));
44 string_success_callback_ =
45 base::Bind(&NetworkDeviceHandlerTest::StringSuccessCallback,
46 base::Unretained(this));
47 error_callback_ = base::Bind(&NetworkDeviceHandlerTest::ErrorCallback,
48 base::Unretained(this));
50 network_state_handler_.reset(NetworkStateHandler::InitializeForTest());
51 NetworkDeviceHandlerImpl* device_handler = new NetworkDeviceHandlerImpl;
52 device_handler->Init(network_state_handler_.get());
53 network_device_handler_.reset(device_handler);
55 // Add devices after handlers have been initialized.
56 ShillDeviceClient::TestInterface* device_test =
57 fake_device_client_->GetTestInterface();
58 device_test->AddDevice(
59 kDefaultCellularDevicePath, shill::kTypeCellular, "cellular1");
60 device_test->AddDevice(kDefaultWifiDevicePath, shill::kTypeWifi, "wifi1");
62 base::ListValue test_ip_configs;
63 test_ip_configs.AppendString("ip_config1");
64 device_test->SetDeviceProperty(
65 kDefaultWifiDevicePath, shill::kIPConfigsProperty, test_ip_configs);
67 message_loop_.RunUntilIdle();
70 void TearDown() override {
71 network_device_handler_.reset();
72 network_state_handler_.reset();
73 DBusThreadManager::Shutdown();
76 void ErrorCallback(const std::string& error_name,
77 scoped_ptr<base::DictionaryValue> error_data) {
78 VLOG(1) << "ErrorCallback: " << error_name;
79 result_ = error_name;
82 void SuccessCallback() {
83 result_ = kResultSuccess;
86 void PropertiesSuccessCallback(const std::string& device_path,
87 const base::DictionaryValue& properties) {
88 result_ = kResultSuccess;
89 properties_.reset(properties.DeepCopy());
92 void StringSuccessCallback(const std::string& result) {
93 VLOG(1) << "StringSuccessCallback: " << result;
94 result_ = result;
97 protected:
98 std::string result_;
100 FakeShillDeviceClient* fake_device_client_;
101 scoped_ptr<NetworkDeviceHandler> network_device_handler_;
102 scoped_ptr<NetworkStateHandler> network_state_handler_;
103 base::MessageLoopForUI message_loop_;
104 base::Closure success_callback_;
105 network_handler::DictionaryResultCallback properties_success_callback_;
106 network_handler::StringResultCallback string_success_callback_;
107 network_handler::ErrorCallback error_callback_;
108 scoped_ptr<base::DictionaryValue> properties_;
110 private:
111 DISALLOW_COPY_AND_ASSIGN(NetworkDeviceHandlerTest);
114 TEST_F(NetworkDeviceHandlerTest, GetDeviceProperties) {
115 network_device_handler_->GetDeviceProperties(
116 kDefaultWifiDevicePath, properties_success_callback_, error_callback_);
117 message_loop_.RunUntilIdle();
118 EXPECT_EQ(kResultSuccess, result_);
119 std::string type;
120 properties_->GetString(shill::kTypeProperty, &type);
121 EXPECT_EQ(shill::kTypeWifi, type);
124 TEST_F(NetworkDeviceHandlerTest, SetDeviceProperty) {
125 // Set the shill::kScanIntervalProperty to true. The call
126 // should succeed and the value should be set.
127 network_device_handler_->SetDeviceProperty(kDefaultCellularDevicePath,
128 shill::kScanIntervalProperty,
129 base::FundamentalValue(1),
130 success_callback_,
131 error_callback_);
132 message_loop_.RunUntilIdle();
133 EXPECT_EQ(kResultSuccess, result_);
135 // GetDeviceProperties should return the value set by SetDeviceProperty.
136 network_device_handler_->GetDeviceProperties(kDefaultCellularDevicePath,
137 properties_success_callback_,
138 error_callback_);
139 message_loop_.RunUntilIdle();
140 EXPECT_EQ(kResultSuccess, result_);
141 int interval = 0;
142 EXPECT_TRUE(properties_->GetIntegerWithoutPathExpansion(
143 shill::kScanIntervalProperty, &interval));
144 EXPECT_EQ(1, interval);
146 // Repeat the same with value false.
147 network_device_handler_->SetDeviceProperty(kDefaultCellularDevicePath,
148 shill::kScanIntervalProperty,
149 base::FundamentalValue(2),
150 success_callback_,
151 error_callback_);
152 message_loop_.RunUntilIdle();
153 EXPECT_EQ(kResultSuccess, result_);
155 network_device_handler_->GetDeviceProperties(kDefaultCellularDevicePath,
156 properties_success_callback_,
157 error_callback_);
158 message_loop_.RunUntilIdle();
159 EXPECT_EQ(kResultSuccess, result_);
160 EXPECT_TRUE(properties_->GetIntegerWithoutPathExpansion(
161 shill::kScanIntervalProperty, &interval));
162 EXPECT_EQ(2, interval);
164 // Set property on an invalid path.
165 network_device_handler_->SetDeviceProperty(kUnknownCellularDevicePath,
166 shill::kScanIntervalProperty,
167 base::FundamentalValue(1),
168 success_callback_,
169 error_callback_);
170 message_loop_.RunUntilIdle();
171 EXPECT_EQ(NetworkDeviceHandler::kErrorDeviceMissing, result_);
173 // Setting a owner-protected device property through SetDeviceProperty must
174 // fail.
175 network_device_handler_->SetDeviceProperty(
176 kDefaultCellularDevicePath,
177 shill::kCellularAllowRoamingProperty,
178 base::FundamentalValue(true),
179 success_callback_,
180 error_callback_);
181 message_loop_.RunUntilIdle();
182 EXPECT_NE(kResultSuccess, result_);
186 TEST_F(NetworkDeviceHandlerTest, CellularAllowRoaming) {
187 // Start with disabled data roaming.
188 ShillDeviceClient::TestInterface* device_test =
189 fake_device_client_->GetTestInterface();
190 device_test->SetDeviceProperty(kDefaultCellularDevicePath,
191 shill::kCellularAllowRoamingProperty,
192 base::FundamentalValue(false));
194 network_device_handler_->SetCellularAllowRoaming(true);
195 message_loop_.RunUntilIdle();
197 // Roaming should be enabled now.
198 network_device_handler_->GetDeviceProperties(kDefaultCellularDevicePath,
199 properties_success_callback_,
200 error_callback_);
201 message_loop_.RunUntilIdle();
202 EXPECT_EQ(kResultSuccess, result_);
203 bool allow_roaming;
204 EXPECT_TRUE(properties_->GetBooleanWithoutPathExpansion(
205 shill::kCellularAllowRoamingProperty, &allow_roaming));
206 EXPECT_TRUE(allow_roaming);
208 network_device_handler_->SetCellularAllowRoaming(false);
209 message_loop_.RunUntilIdle();
211 // Roaming should be disable again.
212 network_device_handler_->GetDeviceProperties(kDefaultCellularDevicePath,
213 properties_success_callback_,
214 error_callback_);
215 message_loop_.RunUntilIdle();
216 EXPECT_EQ(kResultSuccess, result_);
217 EXPECT_TRUE(properties_->GetBooleanWithoutPathExpansion(
218 shill::kCellularAllowRoamingProperty, &allow_roaming));
219 EXPECT_FALSE(allow_roaming);
222 TEST_F(NetworkDeviceHandlerTest, SetWifiTDLSEnabled) {
223 // We add a wifi device by default, initial call should succeed.
224 fake_device_client_->GetTestInterface()->SetTDLSState(
225 shill::kTDLSConnectedState);
226 network_device_handler_->SetWifiTDLSEnabled(
227 "fake_ip_address", true, string_success_callback_, error_callback_);
228 message_loop_.RunUntilIdle();
229 EXPECT_EQ(shill::kTDLSConnectedState, result_);
232 TEST_F(NetworkDeviceHandlerTest, SetWifiTDLSEnabledNonexistent) {
233 // Set the TDLS state to 'Nonexistant'. Call should fail with 'Nonexistant'
234 // result.
235 fake_device_client_->GetTestInterface()->SetTDLSState(
236 shill::kTDLSNonexistentState);
237 network_device_handler_->SetWifiTDLSEnabled(
238 "fake_ip_address", true, string_success_callback_, error_callback_);
239 message_loop_.RunUntilIdle();
240 EXPECT_EQ(shill::kTDLSNonexistentState, result_);
243 TEST_F(NetworkDeviceHandlerTest, SetWifiTDLSEnabledMissing) {
244 // Remove the wifi device. Call should fail with "device missing" error.
245 fake_device_client_->GetTestInterface()->RemoveDevice(kDefaultWifiDevicePath);
246 message_loop_.RunUntilIdle();
247 network_device_handler_->SetWifiTDLSEnabled(
248 "fake_ip_address", true, string_success_callback_, error_callback_);
249 message_loop_.RunUntilIdle();
250 EXPECT_EQ(NetworkDeviceHandler::kErrorDeviceMissing, result_);
253 TEST_F(NetworkDeviceHandlerTest, SetWifiTDLSEnabledBusy) {
254 // Set the busy count, call should succeed after repeat attempt.
255 fake_device_client_->GetTestInterface()->SetTDLSState(
256 shill::kTDLSConnectedState);
257 fake_device_client_->GetTestInterface()->SetTDLSBusyCount(1);
258 network_device_handler_->SetWifiTDLSEnabled(
259 "fake_ip_address", true, string_success_callback_, error_callback_);
260 message_loop_.RunUntilIdle();
261 EXPECT_EQ(shill::kTDLSConnectedState, result_);
263 // Set the busy count to a large number, call should fail after max number
264 // of repeat attempt.
265 fake_device_client_->GetTestInterface()->SetTDLSBusyCount(100000);
266 network_device_handler_->SetWifiTDLSEnabled(
267 "fake_ip_address", true, string_success_callback_, error_callback_);
268 message_loop_.RunUntilIdle();
269 EXPECT_EQ(NetworkDeviceHandler::kErrorTimeout, result_);
272 TEST_F(NetworkDeviceHandlerTest, GetWifiTDLSStatus) {
273 // We add a wifi device by default, initial call should succeed.
274 fake_device_client_->GetTestInterface()->SetTDLSState(
275 shill::kTDLSConnectedState);
276 network_device_handler_->GetWifiTDLSStatus(
277 "fake_ip_address", string_success_callback_, error_callback_);
278 message_loop_.RunUntilIdle();
279 EXPECT_EQ(shill::kTDLSConnectedState, result_);
281 // Remove the wifi device. Call should fail with "device missing" error.
282 fake_device_client_->GetTestInterface()->RemoveDevice(kDefaultWifiDevicePath);
283 message_loop_.RunUntilIdle();
284 network_device_handler_->GetWifiTDLSStatus(
285 "fake_ip_address", string_success_callback_, error_callback_);
286 message_loop_.RunUntilIdle();
287 EXPECT_EQ(NetworkDeviceHandler::kErrorDeviceMissing, result_);
290 TEST_F(NetworkDeviceHandlerTest, RequestRefreshIPConfigs) {
291 network_device_handler_->RequestRefreshIPConfigs(
292 kDefaultWifiDevicePath, success_callback_, error_callback_);
293 message_loop_.RunUntilIdle();
294 EXPECT_EQ(kResultSuccess, result_);
295 // TODO(stevenjb): Add test interface to ShillIPConfigClient and test
296 // refresh calls.
299 TEST_F(NetworkDeviceHandlerTest, SetCarrier) {
300 const char kCarrier[] = "carrier";
302 // Test that the success callback gets called.
303 network_device_handler_->SetCarrier(
304 kDefaultCellularDevicePath, kCarrier, success_callback_, error_callback_);
305 message_loop_.RunUntilIdle();
306 EXPECT_EQ(kResultSuccess, result_);
308 // Test that the shill error propagates to the error callback.
309 network_device_handler_->SetCarrier(
310 kUnknownCellularDevicePath, kCarrier, success_callback_, error_callback_);
311 message_loop_.RunUntilIdle();
312 EXPECT_EQ(NetworkDeviceHandler::kErrorDeviceMissing, result_);
315 TEST_F(NetworkDeviceHandlerTest, RequirePin) {
316 // Test that the success callback gets called.
317 network_device_handler_->RequirePin(kDefaultCellularDevicePath,
318 true,
319 kDefaultPin,
320 success_callback_,
321 error_callback_);
322 message_loop_.RunUntilIdle();
323 EXPECT_EQ(kResultSuccess, result_);
325 // Test that the shill error propagates to the error callback.
326 network_device_handler_->RequirePin(kUnknownCellularDevicePath,
327 true,
328 kDefaultPin,
329 success_callback_,
330 error_callback_);
331 message_loop_.RunUntilIdle();
332 EXPECT_EQ(NetworkDeviceHandler::kErrorDeviceMissing, result_);
335 TEST_F(NetworkDeviceHandlerTest, EnterPin) {
336 // Test that the success callback gets called.
337 network_device_handler_->EnterPin(kDefaultCellularDevicePath,
338 kDefaultPin,
339 success_callback_,
340 error_callback_);
341 message_loop_.RunUntilIdle();
342 EXPECT_EQ(kResultSuccess, result_);
344 // Test that the shill error propagates to the error callback.
345 network_device_handler_->EnterPin(kUnknownCellularDevicePath,
346 kDefaultPin,
347 success_callback_,
348 error_callback_);
349 message_loop_.RunUntilIdle();
350 EXPECT_EQ(NetworkDeviceHandler::kErrorDeviceMissing, result_);
353 TEST_F(NetworkDeviceHandlerTest, UnblockPin) {
354 const char kPuk[] = "12345678";
355 const char kPin[] = "1234";
357 // Test that the success callback gets called.
358 network_device_handler_->UnblockPin(kDefaultCellularDevicePath,
359 kPin,
360 kPuk,
361 success_callback_,
362 error_callback_);
363 message_loop_.RunUntilIdle();
364 EXPECT_EQ(kResultSuccess, result_);
366 // Test that the shill error propagates to the error callback.
367 network_device_handler_->UnblockPin(kUnknownCellularDevicePath,
368 kPin,
369 kPuk,
370 success_callback_,
371 error_callback_);
372 message_loop_.RunUntilIdle();
373 EXPECT_EQ(NetworkDeviceHandler::kErrorDeviceMissing, result_);
376 TEST_F(NetworkDeviceHandlerTest, ChangePin) {
377 const char kOldPin[] = "4321";
378 const char kNewPin[] = "1234";
380 // Test that the success callback gets called.
381 network_device_handler_->ChangePin(kDefaultCellularDevicePath,
382 kOldPin,
383 kNewPin,
384 success_callback_,
385 error_callback_);
386 message_loop_.RunUntilIdle();
387 EXPECT_EQ(kResultSuccess, result_);
389 // Test that the shill error propagates to the error callback.
390 network_device_handler_->ChangePin(kUnknownCellularDevicePath,
391 kOldPin,
392 kNewPin,
393 success_callback_,
394 error_callback_);
395 message_loop_.RunUntilIdle();
396 EXPECT_EQ(NetworkDeviceHandler::kErrorDeviceMissing, result_);
399 } // namespace chromeos