network_device_handler_unittest.cc revision 5f1c94371a64b3196d4be9466099bb892df9b88e
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.
4
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/fake_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"
16
17namespace chromeos {
18
19namespace {
20
21const char kDefaultCellularDevicePath[] = "stub_cellular_device";
22const char kUnknownCellularDevicePath[] = "unknown_cellular_device";
23const char kDefaultWifiDevicePath[] = "stub_wifi_device";
24const char kResultSuccess[] = "success";
25const char kDefaultPin[] = "1111";
26
27}  // namespace
28
29class NetworkDeviceHandlerTest : public testing::Test {
30 public:
31  NetworkDeviceHandlerTest() : fake_device_client_(NULL) {}
32  virtual ~NetworkDeviceHandlerTest() {}
33
34  virtual void SetUp() OVERRIDE {
35    FakeDBusThreadManager* dbus_manager = new FakeDBusThreadManager;
36    dbus_manager->SetFakeShillClients();
37
38    fake_device_client_ = new FakeShillDeviceClient;
39    dbus_manager->SetShillDeviceClient(
40        scoped_ptr<ShillDeviceClient>(fake_device_client_));
41    DBusThreadManager::InitializeForTesting(dbus_manager);
42
43    success_callback_ = base::Bind(&NetworkDeviceHandlerTest::SuccessCallback,
44                                   base::Unretained(this));
45    properties_success_callback_ =
46        base::Bind(&NetworkDeviceHandlerTest::PropertiesSuccessCallback,
47                   base::Unretained(this));
48    string_success_callback_ =
49        base::Bind(&NetworkDeviceHandlerTest::StringSuccessCallback,
50                   base::Unretained(this));
51    error_callback_ = base::Bind(&NetworkDeviceHandlerTest::ErrorCallback,
52                                 base::Unretained(this));
53
54    network_state_handler_.reset(NetworkStateHandler::InitializeForTest());
55    NetworkDeviceHandlerImpl* device_handler = new NetworkDeviceHandlerImpl;
56    device_handler->Init(network_state_handler_.get());
57    network_device_handler_.reset(device_handler);
58
59    // Add devices after handlers have been initialized.
60    ShillDeviceClient::TestInterface* device_test =
61        fake_device_client_->GetTestInterface();
62    device_test->AddDevice(
63        kDefaultCellularDevicePath, shill::kTypeCellular, "cellular1");
64    device_test->AddDevice(kDefaultWifiDevicePath, shill::kTypeWifi, "wifi1");
65
66    base::ListValue test_ip_configs;
67    test_ip_configs.AppendString("ip_config1");
68    device_test->SetDeviceProperty(
69        kDefaultWifiDevicePath, shill::kIPConfigsProperty, test_ip_configs);
70
71    message_loop_.RunUntilIdle();
72  }
73
74  virtual void TearDown() OVERRIDE {
75    network_device_handler_.reset();
76    network_state_handler_.reset();
77    DBusThreadManager::Shutdown();
78  }
79
80  void ErrorCallback(const std::string& error_name,
81                     scoped_ptr<base::DictionaryValue> error_data) {
82    LOG(ERROR) << "ErrorCallback: " << error_name;
83    result_ = error_name;
84  }
85
86  void SuccessCallback() {
87    result_ = kResultSuccess;
88  }
89
90  void PropertiesSuccessCallback(const std::string& device_path,
91                                 const base::DictionaryValue& properties) {
92    result_ = kResultSuccess;
93    properties_.reset(properties.DeepCopy());
94  }
95
96  void StringSuccessCallback(const std::string& result) {
97    LOG(ERROR) << "StringSuccessCallback: " << result;
98    result_ = kResultSuccess;
99  }
100
101 protected:
102  std::string result_;
103
104  FakeShillDeviceClient* fake_device_client_;
105  scoped_ptr<NetworkDeviceHandler> network_device_handler_;
106  scoped_ptr<NetworkStateHandler> network_state_handler_;
107  base::MessageLoopForUI message_loop_;
108  base::Closure success_callback_;
109  network_handler::DictionaryResultCallback properties_success_callback_;
110  network_handler::StringResultCallback string_success_callback_;
111  network_handler::ErrorCallback error_callback_;
112  scoped_ptr<base::DictionaryValue> properties_;
113
114 private:
115  DISALLOW_COPY_AND_ASSIGN(NetworkDeviceHandlerTest);
116};
117
118TEST_F(NetworkDeviceHandlerTest, GetDeviceProperties) {
119  network_device_handler_->GetDeviceProperties(
120      kDefaultWifiDevicePath, properties_success_callback_, error_callback_);
121  message_loop_.RunUntilIdle();
122  EXPECT_EQ(kResultSuccess, result_);
123  std::string type;
124  properties_->GetString(shill::kTypeProperty, &type);
125  EXPECT_EQ(shill::kTypeWifi, type);
126}
127
128TEST_F(NetworkDeviceHandlerTest, SetDeviceProperty) {
129  // Set the shill::kScanIntervalProperty to true. The call
130  // should succeed and the value should be set.
131  network_device_handler_->SetDeviceProperty(kDefaultCellularDevicePath,
132                                             shill::kScanIntervalProperty,
133                                             base::FundamentalValue(1),
134                                             success_callback_,
135                                             error_callback_);
136  message_loop_.RunUntilIdle();
137  EXPECT_EQ(kResultSuccess, result_);
138
139  // GetDeviceProperties should return the value set by SetDeviceProperty.
140  network_device_handler_->GetDeviceProperties(kDefaultCellularDevicePath,
141                                               properties_success_callback_,
142                                               error_callback_);
143  message_loop_.RunUntilIdle();
144  EXPECT_EQ(kResultSuccess, result_);
145  int interval = 0;
146  EXPECT_TRUE(properties_->GetIntegerWithoutPathExpansion(
147      shill::kScanIntervalProperty, &interval));
148  EXPECT_EQ(1, interval);
149
150  // Repeat the same with value false.
151  network_device_handler_->SetDeviceProperty(kDefaultCellularDevicePath,
152                                             shill::kScanIntervalProperty,
153                                             base::FundamentalValue(2),
154                                             success_callback_,
155                                             error_callback_);
156  message_loop_.RunUntilIdle();
157  EXPECT_EQ(kResultSuccess, result_);
158
159  network_device_handler_->GetDeviceProperties(kDefaultCellularDevicePath,
160                                               properties_success_callback_,
161                                               error_callback_);
162  message_loop_.RunUntilIdle();
163  EXPECT_EQ(kResultSuccess, result_);
164  EXPECT_TRUE(properties_->GetIntegerWithoutPathExpansion(
165      shill::kScanIntervalProperty, &interval));
166  EXPECT_EQ(2, interval);
167
168  // Set property on an invalid path.
169  network_device_handler_->SetDeviceProperty(kUnknownCellularDevicePath,
170                                             shill::kScanIntervalProperty,
171                                             base::FundamentalValue(1),
172                                             success_callback_,
173                                             error_callback_);
174  message_loop_.RunUntilIdle();
175  EXPECT_EQ(NetworkDeviceHandler::kErrorDeviceMissing, result_);
176
177  // Setting a owner-protected device property through SetDeviceProperty must
178  // fail.
179  network_device_handler_->SetDeviceProperty(
180      kDefaultCellularDevicePath,
181      shill::kCellularAllowRoamingProperty,
182      base::FundamentalValue(true),
183      success_callback_,
184      error_callback_);
185  message_loop_.RunUntilIdle();
186  EXPECT_NE(kResultSuccess, result_);
187
188}
189
190TEST_F(NetworkDeviceHandlerTest, CellularAllowRoaming) {
191  // Start with disabled data roaming.
192  ShillDeviceClient::TestInterface* device_test =
193      fake_device_client_->GetTestInterface();
194  device_test->SetDeviceProperty(kDefaultCellularDevicePath,
195                                 shill::kCellularAllowRoamingProperty,
196                                 base::FundamentalValue(false));
197
198  network_device_handler_->SetCellularAllowRoaming(true);
199  message_loop_.RunUntilIdle();
200
201  // Roaming should be enabled now.
202  network_device_handler_->GetDeviceProperties(kDefaultCellularDevicePath,
203                                               properties_success_callback_,
204                                               error_callback_);
205  message_loop_.RunUntilIdle();
206  EXPECT_EQ(kResultSuccess, result_);
207  bool allow_roaming;
208  EXPECT_TRUE(properties_->GetBooleanWithoutPathExpansion(
209      shill::kCellularAllowRoamingProperty, &allow_roaming));
210  EXPECT_TRUE(allow_roaming);
211
212  network_device_handler_->SetCellularAllowRoaming(false);
213  message_loop_.RunUntilIdle();
214
215  // Roaming should be disable again.
216  network_device_handler_->GetDeviceProperties(kDefaultCellularDevicePath,
217                                               properties_success_callback_,
218                                               error_callback_);
219  message_loop_.RunUntilIdle();
220  EXPECT_EQ(kResultSuccess, result_);
221  EXPECT_TRUE(properties_->GetBooleanWithoutPathExpansion(
222      shill::kCellularAllowRoamingProperty, &allow_roaming));
223  EXPECT_FALSE(allow_roaming);
224}
225
226TEST_F(NetworkDeviceHandlerTest, SetWifiTDLSEnabled) {
227  // We add a wifi device by default, initial call should succeed.
228  network_device_handler_->SetWifiTDLSEnabled(
229      "fake_ip_address", true, string_success_callback_, error_callback_);
230  message_loop_.RunUntilIdle();
231  EXPECT_EQ(kResultSuccess, result_);
232}
233
234TEST_F(NetworkDeviceHandlerTest, SetWifiTDLSEnabledMissing) {
235  // Remove the wifi device. Call should fail with "device missing" error.
236  fake_device_client_->GetTestInterface()->RemoveDevice(kDefaultWifiDevicePath);
237  message_loop_.RunUntilIdle();
238  network_device_handler_->SetWifiTDLSEnabled(
239      "fake_ip_address", true, string_success_callback_, error_callback_);
240  message_loop_.RunUntilIdle();
241  EXPECT_EQ(NetworkDeviceHandler::kErrorDeviceMissing, result_);
242}
243
244TEST_F(NetworkDeviceHandlerTest, SetWifiTDLSEnabledBusy) {
245  // Set the busy count, call should succeed after repeat attempt.
246  fake_device_client_->set_tdls_busy_count(1);
247  network_device_handler_->SetWifiTDLSEnabled(
248      "fake_ip_address", true, string_success_callback_, error_callback_);
249  message_loop_.RunUntilIdle();
250  EXPECT_EQ(kResultSuccess, result_);
251
252  // Set the busy count to a large number, call should fail after max number
253  // of repeat attempt.
254  fake_device_client_->set_tdls_busy_count(100000);
255  network_device_handler_->SetWifiTDLSEnabled(
256      "fake_ip_address", true, string_success_callback_, error_callback_);
257  message_loop_.RunUntilIdle();
258  EXPECT_EQ(NetworkDeviceHandler::kErrorTimeout, result_);
259}
260
261TEST_F(NetworkDeviceHandlerTest, GetWifiTDLSStatus) {
262  // We add a wifi device by default, initial call should succeed.
263  network_device_handler_->GetWifiTDLSStatus(
264      "fake_ip_address", string_success_callback_, error_callback_);
265  message_loop_.RunUntilIdle();
266  EXPECT_EQ(kResultSuccess, result_);
267
268  // Remove the wifi device. Call should fail with "device missing" error.
269  fake_device_client_->GetTestInterface()->RemoveDevice(kDefaultWifiDevicePath);
270  message_loop_.RunUntilIdle();
271  network_device_handler_->GetWifiTDLSStatus(
272      "fake_ip_address", string_success_callback_, error_callback_);
273  message_loop_.RunUntilIdle();
274  EXPECT_EQ(NetworkDeviceHandler::kErrorDeviceMissing, result_);
275}
276
277TEST_F(NetworkDeviceHandlerTest, RequestRefreshIPConfigs) {
278  network_device_handler_->RequestRefreshIPConfigs(
279      kDefaultWifiDevicePath, success_callback_, error_callback_);
280  message_loop_.RunUntilIdle();
281  EXPECT_EQ(kResultSuccess, result_);
282  // TODO(stevenjb): Add test interface to ShillIPConfigClient and test
283  // refresh calls.
284}
285
286TEST_F(NetworkDeviceHandlerTest, SetCarrier) {
287  const char kCarrier[] = "carrier";
288
289  // Test that the success callback gets called.
290  network_device_handler_->SetCarrier(
291      kDefaultCellularDevicePath, kCarrier, success_callback_, error_callback_);
292  message_loop_.RunUntilIdle();
293  EXPECT_EQ(kResultSuccess, result_);
294
295  // Test that the shill error propagates to the error callback.
296  network_device_handler_->SetCarrier(
297      kUnknownCellularDevicePath, kCarrier, success_callback_, error_callback_);
298  message_loop_.RunUntilIdle();
299  EXPECT_EQ(NetworkDeviceHandler::kErrorDeviceMissing, result_);
300}
301
302TEST_F(NetworkDeviceHandlerTest, RequirePin) {
303  // Test that the success callback gets called.
304  network_device_handler_->RequirePin(kDefaultCellularDevicePath,
305                                      true,
306                                      kDefaultPin,
307                                      success_callback_,
308                                      error_callback_);
309  message_loop_.RunUntilIdle();
310  EXPECT_EQ(kResultSuccess, result_);
311
312  // Test that the shill error propagates to the error callback.
313  network_device_handler_->RequirePin(kUnknownCellularDevicePath,
314                                      true,
315                                      kDefaultPin,
316                                      success_callback_,
317                                      error_callback_);
318  message_loop_.RunUntilIdle();
319  EXPECT_EQ(NetworkDeviceHandler::kErrorDeviceMissing, result_);
320}
321
322TEST_F(NetworkDeviceHandlerTest, EnterPin) {
323  // Test that the success callback gets called.
324  network_device_handler_->EnterPin(kDefaultCellularDevicePath,
325                                    kDefaultPin,
326                                    success_callback_,
327                                    error_callback_);
328  message_loop_.RunUntilIdle();
329  EXPECT_EQ(kResultSuccess, result_);
330
331  // Test that the shill error propagates to the error callback.
332  network_device_handler_->EnterPin(kUnknownCellularDevicePath,
333                                    kDefaultPin,
334                                    success_callback_,
335                                    error_callback_);
336  message_loop_.RunUntilIdle();
337  EXPECT_EQ(NetworkDeviceHandler::kErrorDeviceMissing, result_);
338}
339
340TEST_F(NetworkDeviceHandlerTest, UnblockPin) {
341  const char kPuk[] = "12345678";
342  const char kPin[] = "1234";
343
344  // Test that the success callback gets called.
345  network_device_handler_->UnblockPin(kDefaultCellularDevicePath,
346                                      kPin,
347                                      kPuk,
348                                      success_callback_,
349                                      error_callback_);
350  message_loop_.RunUntilIdle();
351  EXPECT_EQ(kResultSuccess, result_);
352
353  // Test that the shill error propagates to the error callback.
354  network_device_handler_->UnblockPin(kUnknownCellularDevicePath,
355                                      kPin,
356                                      kPuk,
357                                      success_callback_,
358                                      error_callback_);
359  message_loop_.RunUntilIdle();
360  EXPECT_EQ(NetworkDeviceHandler::kErrorDeviceMissing, result_);
361}
362
363TEST_F(NetworkDeviceHandlerTest, ChangePin) {
364  const char kOldPin[] = "4321";
365  const char kNewPin[] = "1234";
366
367  // Test that the success callback gets called.
368  network_device_handler_->ChangePin(kDefaultCellularDevicePath,
369                                     kOldPin,
370                                     kNewPin,
371                                     success_callback_,
372                                     error_callback_);
373  message_loop_.RunUntilIdle();
374  EXPECT_EQ(kResultSuccess, result_);
375
376  // Test that the shill error propagates to the error callback.
377  network_device_handler_->ChangePin(kUnknownCellularDevicePath,
378                                     kOldPin,
379                                     kNewPin,
380                                     success_callback_,
381                                     error_callback_);
382  message_loop_.RunUntilIdle();
383  EXPECT_EQ(NetworkDeviceHandler::kErrorDeviceMissing, result_);
384}
385
386}  // namespace chromeos
387