blob: db560a5410fb13d84b3f7c9f31cb7766e8950df5 [file] [log] [blame]
mukesh agrawal8a3188d2011-12-01 20:56:44 +00001// Copyright (c) 2012 The Chromium OS Authors. All rights reserved.
Chris Masone853b81b2011-06-24 14:11:41 -07002// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#include "shill/wifi.h"
6
mukesh agrawalf2f68a52011-09-01 12:15:48 -07007#include <netinet/ether.h>
8#include <linux/if.h>
mukesh agrawal5c4dd0b2011-09-14 13:53:14 -07009#include <sys/socket.h>
10#include <linux/netlink.h> // Needs typedefs from sys/socket.h.
mukesh agrawalf2f68a52011-09-01 12:15:48 -070011
Chris Masone853b81b2011-06-24 14:11:41 -070012#include <map>
Paul Stewarta41e38d2011-11-11 07:47:29 -080013#include <set>
Chris Masone853b81b2011-06-24 14:11:41 -070014#include <string>
15#include <vector>
16
Darin Petkov60ceaf32012-10-18 10:36:01 +020017#include <base/file_util.h>
mukesh agrawal7ec71312011-11-10 02:08:26 +000018#include <base/memory/ref_counted.h>
mukesh agrawaldc42bb32011-07-28 10:40:26 -070019#include <base/memory/scoped_ptr.h>
Paul Stewarta41e38d2011-11-11 07:47:29 -080020#include <base/stringprintf.h>
mukesh agrawal31950242011-07-14 11:53:38 -070021#include <base/string_number_conversions.h>
Darin Petkov4a66cc52012-06-15 10:08:29 +020022#include <base/string_split.h>
mukesh agrawal31950242011-07-14 11:53:38 -070023#include <base/string_util.h>
Chris Masone853b81b2011-06-24 14:11:41 -070024#include <chromeos/dbus/service_constants.h>
mukesh agrawal1830fa12011-09-26 14:31:40 -070025#include <dbus-c++/dbus.h>
Chris Masone7156c922011-08-23 20:36:21 -070026#include <gmock/gmock.h>
Chris Masone2ae797d2011-08-23 20:41:00 -070027#include <gtest/gtest.h>
Chris Masone853b81b2011-06-24 14:11:41 -070028
29#include "shill/dbus_adaptor.h"
Paul Stewart26b327e2011-10-19 11:38:09 -070030#include "shill/event_dispatcher.h"
Gaurav Shah6d2c72d2012-10-16 16:30:44 -070031#include "shill/geolocation_info.h"
mukesh agrawal8ede0522011-10-03 14:57:44 -070032#include "shill/ieee80211.h"
mukesh agrawal7a4e4002011-09-06 11:26:05 -070033#include "shill/key_value_store.h"
Christopher Wileyb691efd2012-08-09 13:51:51 -070034#include "shill/logging.h"
Chris Masone853b81b2011-06-24 14:11:41 -070035#include "shill/manager.h"
Darin Petkov2b8e44e2012-06-25 15:13:26 +020036#include "shill/mock_dbus_manager.h"
Chris Masone853b81b2011-06-24 14:11:41 -070037#include "shill/mock_device.h"
mukesh agrawalc01f3982012-01-24 13:48:39 -080038#include "shill/mock_device_info.h"
mukesh agrawalf2f68a52011-09-01 12:15:48 -070039#include "shill/mock_dhcp_config.h"
40#include "shill/mock_dhcp_provider.h"
Paul Stewarte369ece2012-05-22 09:11:03 -070041#include "shill/mock_event_dispatcher.h"
Paul Stewart3c508e12012-08-09 11:40:06 -070042#include "shill/mock_link_monitor.h"
mukesh agrawalcf24a242012-05-21 16:46:11 -070043#include "shill/mock_log.h"
Chris Masone2ae797d2011-08-23 20:41:00 -070044#include "shill/mock_manager.h"
Thieu Le3426c8f2012-01-11 17:35:11 -080045#include "shill/mock_metrics.h"
Paul Stewart10ccbb32012-04-26 15:59:30 -070046#include "shill/mock_profile.h"
mukesh agrawal5c4dd0b2011-09-14 13:53:14 -070047#include "shill/mock_rtnl_handler.h"
Paul Stewarta41e38d2011-11-11 07:47:29 -080048#include "shill/mock_store.h"
mukesh agrawalb20776f2012-02-10 16:00:36 -080049#include "shill/mock_supplicant_bss_proxy.h"
mukesh agrawal31950242011-07-14 11:53:38 -070050#include "shill/mock_supplicant_interface_proxy.h"
51#include "shill/mock_supplicant_process_proxy.h"
mukesh agrawal5c05b292012-03-07 10:12:52 -080052#include "shill/mock_time.h"
mukesh agrawal7ec71312011-11-10 02:08:26 +000053#include "shill/mock_wifi_service.h"
mukesh agrawaldc42bb32011-07-28 10:40:26 -070054#include "shill/nice_mock_control.h"
Darin Petkov4a66cc52012-06-15 10:08:29 +020055#include "shill/property_store_inspector.h"
Chris Masone853b81b2011-06-24 14:11:41 -070056#include "shill/property_store_unittest.h"
mukesh agrawal31950242011-07-14 11:53:38 -070057#include "shill/proxy_factory.h"
58#include "shill/wifi_endpoint.h"
mukesh agrawal32399322011-09-01 10:53:43 -070059#include "shill/wifi_service.h"
Paul Stewartced6a0b2011-11-08 15:32:04 -080060#include "shill/wpa_supplicant.h"
Chris Masone853b81b2011-06-24 14:11:41 -070061
Gary Morainac1bdb42012-02-16 17:42:29 -080062
Chris Masone853b81b2011-06-24 14:11:41 -070063using std::map;
Paul Stewarta41e38d2011-11-11 07:47:29 -080064using std::set;
Chris Masone853b81b2011-06-24 14:11:41 -070065using std::string;
66using std::vector;
67using ::testing::_;
mukesh agrawal31950242011-07-14 11:53:38 -070068using ::testing::AnyNumber;
69using ::testing::DefaultValue;
Paul Stewarta41e38d2011-11-11 07:47:29 -080070using ::testing::DoAll;
mukesh agrawalcf24a242012-05-21 16:46:11 -070071using ::testing::EndsWith;
mukesh agrawal31950242011-07-14 11:53:38 -070072using ::testing::InSequence;
mukesh agrawalb20776f2012-02-10 16:00:36 -080073using ::testing::Invoke;
Paul Stewart549d44c2012-07-03 12:40:25 -070074using ::testing::InvokeWithoutArgs;
mukesh agrawal7ec71312011-11-10 02:08:26 +000075using ::testing::Mock;
mukesh agrawaldc42bb32011-07-28 10:40:26 -070076using ::testing::NiceMock;
Chris Masone853b81b2011-06-24 14:11:41 -070077using ::testing::Return;
Gary Morainac1bdb42012-02-16 17:42:29 -080078using ::testing::SaveArg;
Paul Stewarta41e38d2011-11-11 07:47:29 -080079using ::testing::SetArgumentPointee;
80using ::testing::StrEq;
81using ::testing::StrictMock;
Chris Masone853b81b2011-06-24 14:11:41 -070082using ::testing::Test;
mukesh agrawal31950242011-07-14 11:53:38 -070083using ::testing::Throw;
mukesh agrawal8ede0522011-10-03 14:57:44 -070084using ::testing::Values;
Chris Masone853b81b2011-06-24 14:11:41 -070085
86namespace shill {
87
mukesh agrawal31950242011-07-14 11:53:38 -070088class WiFiPropertyTest : public PropertyStoreTest {
Chris Masone853b81b2011-06-24 14:11:41 -070089 public:
mukesh agrawal31950242011-07-14 11:53:38 -070090 WiFiPropertyTest()
Thieu Le3426c8f2012-01-11 17:35:11 -080091 : device_(new WiFi(control_interface(),
92 NULL, NULL, NULL, "wifi", "", 0)) {
Chris Masone853b81b2011-06-24 14:11:41 -070093 }
mukesh agrawal31950242011-07-14 11:53:38 -070094 virtual ~WiFiPropertyTest() {}
Chris Masone853b81b2011-06-24 14:11:41 -070095
96 protected:
mukesh agrawal8abd2f62012-01-30 14:56:14 -080097 WiFiRefPtr device_;
Chris Masone853b81b2011-06-24 14:11:41 -070098};
99
mukesh agrawal31950242011-07-14 11:53:38 -0700100TEST_F(WiFiPropertyTest, Contains) {
mukesh agrawalde29fa82011-09-16 16:16:36 -0700101 EXPECT_TRUE(device_->store().Contains(flimflam::kNameProperty));
102 EXPECT_FALSE(device_->store().Contains(""));
Chris Masone853b81b2011-06-24 14:11:41 -0700103}
104
mukesh agrawal6bb9e7c2012-01-30 14:57:54 -0800105TEST_F(WiFiPropertyTest, SetProperty) {
Chris Masonea8a2c252011-06-27 22:16:30 -0700106 {
107 ::DBus::Error error;
mukesh agrawal6bb9e7c2012-01-30 14:57:54 -0800108 EXPECT_TRUE(DBusAdaptor::SetProperty(
mukesh agrawalde29fa82011-09-16 16:16:36 -0700109 device_->mutable_store(),
Chris Masonea8a2c252011-06-27 22:16:30 -0700110 flimflam::kBgscanSignalThresholdProperty,
111 PropertyStoreTest::kInt32V,
112 &error));
113 }
114 {
115 ::DBus::Error error;
mukesh agrawal6bb9e7c2012-01-30 14:57:54 -0800116 EXPECT_TRUE(DBusAdaptor::SetProperty(device_->mutable_store(),
117 flimflam::kScanIntervalProperty,
118 PropertyStoreTest::kUint16V,
119 &error));
Chris Masonea8a2c252011-06-27 22:16:30 -0700120 }
Chris Masone853b81b2011-06-24 14:11:41 -0700121 // Ensure that an attempt to write a R/O property returns InvalidArgs error.
Chris Masonea8a2c252011-06-27 22:16:30 -0700122 {
123 ::DBus::Error error;
mukesh agrawal6bb9e7c2012-01-30 14:57:54 -0800124 EXPECT_FALSE(DBusAdaptor::SetProperty(device_->mutable_store(),
125 flimflam::kScanningProperty,
126 PropertyStoreTest::kBoolV,
127 &error));
Chris Masone9d779932011-08-25 16:33:41 -0700128 EXPECT_EQ(invalid_args(), error.name());
Chris Masonea8a2c252011-06-27 22:16:30 -0700129 }
Chris Masone853b81b2011-06-24 14:11:41 -0700130
mukesh agrawal4d0401c2012-01-06 16:05:31 -0800131 {
132 ::DBus::Error error;
mukesh agrawal6bb9e7c2012-01-30 14:57:54 -0800133 EXPECT_TRUE(DBusAdaptor::SetProperty(
mukesh agrawal4d0401c2012-01-06 16:05:31 -0800134 device_->mutable_store(),
135 flimflam::kBgscanMethodProperty,
136 DBusAdaptor::StringToVariant(
137 wpa_supplicant::kNetworkBgscanMethodSimple),
138 &error));
139 }
140
141 {
142 ::DBus::Error error;
mukesh agrawal6bb9e7c2012-01-30 14:57:54 -0800143 EXPECT_FALSE(DBusAdaptor::SetProperty(
mukesh agrawal4d0401c2012-01-06 16:05:31 -0800144 device_->mutable_store(),
145 flimflam::kBgscanMethodProperty,
146 DBusAdaptor::StringToVariant("not a real scan method"),
147 &error));
148 }
149}
150
Darin Petkov4a66cc52012-06-15 10:08:29 +0200151TEST_F(WiFiPropertyTest, BgscanMethodProperty) {
mukesh agrawal8abd2f62012-01-30 14:56:14 -0800152 EXPECT_NE(wpa_supplicant::kNetworkBgscanMethodLearn,
153 WiFi::kDefaultBgscanMethod);
Darin Petkov4a66cc52012-06-15 10:08:29 +0200154 EXPECT_TRUE(device_->bgscan_method_.empty());
155
156 string method;
157 PropertyStoreInspector inspector(&device_->store());
158 EXPECT_TRUE(inspector.GetStringProperty(flimflam::kBgscanMethodProperty,
159 &method));
160 EXPECT_EQ(WiFi::kDefaultBgscanMethod, method);
161 EXPECT_EQ(wpa_supplicant::kNetworkBgscanMethodSimple, method);
mukesh agrawal8abd2f62012-01-30 14:56:14 -0800162
163 ::DBus::Error error;
mukesh agrawal6bb9e7c2012-01-30 14:57:54 -0800164 EXPECT_TRUE(DBusAdaptor::SetProperty(
mukesh agrawal8abd2f62012-01-30 14:56:14 -0800165 device_->mutable_store(),
166 flimflam::kBgscanMethodProperty,
167 DBusAdaptor::StringToVariant(
168 wpa_supplicant::kNetworkBgscanMethodLearn),
169 &error));
170 EXPECT_EQ(wpa_supplicant::kNetworkBgscanMethodLearn, device_->bgscan_method_);
Darin Petkov4a66cc52012-06-15 10:08:29 +0200171 EXPECT_TRUE(inspector.GetStringProperty(flimflam::kBgscanMethodProperty,
172 &method));
173 EXPECT_EQ(wpa_supplicant::kNetworkBgscanMethodLearn, method);
mukesh agrawal8abd2f62012-01-30 14:56:14 -0800174
175 EXPECT_TRUE(DBusAdaptor::ClearProperty(
176 device_->mutable_store(), flimflam::kBgscanMethodProperty, &error));
Darin Petkov4a66cc52012-06-15 10:08:29 +0200177 EXPECT_TRUE(inspector.GetStringProperty(flimflam::kBgscanMethodProperty,
178 &method));
179 EXPECT_EQ(WiFi::kDefaultBgscanMethod, method);
180 EXPECT_TRUE(device_->bgscan_method_.empty());
mukesh agrawal8abd2f62012-01-30 14:56:14 -0800181}
182
Paul Stewarte369ece2012-05-22 09:11:03 -0700183class WiFiObjectTest : public ::testing::TestWithParam<string> {
mukesh agrawal31950242011-07-14 11:53:38 -0700184 public:
Paul Stewarte369ece2012-05-22 09:11:03 -0700185 WiFiObjectTest(EventDispatcher *dispatcher)
186 : event_dispatcher_(dispatcher),
187 manager_(&control_interface_, NULL, &metrics_, &glib_),
188 device_info_(&control_interface_, dispatcher, &metrics_, &manager_),
Chris Masone626719f2011-08-18 16:58:48 -0700189 wifi_(new WiFi(&control_interface_,
Paul Stewarte369ece2012-05-22 09:11:03 -0700190 dispatcher,
Thieu Le3426c8f2012-01-11 17:35:11 -0800191 &metrics_,
Chris Masone626719f2011-08-18 16:58:48 -0700192 &manager_,
193 kDeviceName,
194 kDeviceAddress,
195 0)),
mukesh agrawaldc42bb32011-07-28 10:40:26 -0700196 supplicant_process_proxy_(new NiceMock<MockSupplicantProcessProxy>()),
197 supplicant_interface_proxy_(
198 new NiceMock<MockSupplicantInterfaceProxy>(wifi_)),
mukesh agrawalb20776f2012-02-10 16:00:36 -0800199 supplicant_bss_proxy_(
200 new NiceMock<MockSupplicantBSSProxy>()),
mukesh agrawalf2f68a52011-09-01 12:15:48 -0700201 dhcp_config_(new MockDHCPConfig(&control_interface_,
Paul Stewartd408fdf2012-05-07 17:15:57 -0700202 kDeviceName)),
Darin Petkov2b8e44e2012-06-25 15:13:26 +0200203 dbus_manager_(new NiceMock<MockDBusManager>()),
mukesh agrawal2f9df4e2012-08-08 12:29:20 -0700204 proxy_factory_(this) {
mukesh agrawal31950242011-07-14 11:53:38 -0700205 ::testing::DefaultValue< ::DBus::Path>::Set("/default/path");
mukesh agrawal4eb4d782011-12-05 17:34:37 +0000206 // Except for WiFiServices created via WiFi::GetService, we expect
207 // that any WiFiService has been registered with the Manager. So
208 // default Manager.HasService to true, to make the common case
209 // easy.
210 ON_CALL(manager_, HasService(_)).
211 WillByDefault(Return(true));
mukesh agrawalc01f3982012-01-24 13:48:39 -0800212
Paul Stewartd408fdf2012-05-07 17:15:57 -0700213 ON_CALL(dhcp_provider_, CreateConfig(_, _, _, _)).
mukesh agrawalc01f3982012-01-24 13:48:39 -0800214 WillByDefault(Return(dhcp_config_));
215 ON_CALL(*dhcp_config_.get(), RequestIP()).
216 WillByDefault(Return(true));
Gary Morainac1bdb42012-02-16 17:42:29 -0800217
Darin Petkov2b8e44e2012-06-25 15:13:26 +0200218 manager_.dbus_manager_.reset(dbus_manager_); // Transfers ownership.
219
mukesh agrawal5c05b292012-03-07 10:12:52 -0800220 wifi_->time_ = &time_;
mukesh agrawal31950242011-07-14 11:53:38 -0700221 }
mukesh agrawal5c4dd0b2011-09-14 13:53:14 -0700222
223 virtual void SetUp() {
mukesh agrawal3ab56ac2012-05-31 14:41:43 -0700224 // EnableScopes... so that we can EXPECT_CALL for scoped log messages.
225 ScopeLogger::GetInstance()->EnableScopesByName("wifi");
Christopher Wileyd34a47c2012-08-01 15:42:45 -0700226 ScopeLogger::GetInstance()->set_verbose_level(3);
Darin Petkovab565bb2011-10-06 02:55:51 -0700227 wifi_->proxy_factory_ = &proxy_factory_;
mukesh agrawal5c4dd0b2011-09-14 13:53:14 -0700228 static_cast<Device *>(wifi_)->rtnl_handler_ = &rtnl_handler_;
229 wifi_->set_dhcp_provider(&dhcp_provider_);
mukesh agrawalc01f3982012-01-24 13:48:39 -0800230 ON_CALL(manager_, device_info()).
231 WillByDefault(Return(&device_info_));
Darin Petkov2b8e44e2012-06-25 15:13:26 +0200232 EXPECT_CALL(manager_, UpdateEnabledTechnologies()).Times(AnyNumber());
Chris Masoneb9c00592011-10-06 13:10:39 -0700233 EXPECT_CALL(manager_, DeregisterService(_)).Times(AnyNumber());
mukesh agrawalb20776f2012-02-10 16:00:36 -0800234 EXPECT_CALL(*supplicant_bss_proxy_, Die()).Times(AnyNumber());
mukesh agrawal5c4dd0b2011-09-14 13:53:14 -0700235 }
236
237 virtual void TearDown() {
Gary Moraind9f893f2012-02-06 10:03:40 -0800238 EXPECT_CALL(*manager(), UpdateService(_)).Times(AnyNumber());
Eric Shienbrood9a245532012-03-07 14:20:39 -0500239 wifi_->SelectService(NULL);
mukesh agrawalb20776f2012-02-10 16:00:36 -0800240 if (supplicant_bss_proxy_.get()) {
241 EXPECT_CALL(*supplicant_bss_proxy_, Die());
242 }
Darin Petkovab565bb2011-10-06 02:55:51 -0700243 wifi_->proxy_factory_ = NULL;
mukesh agrawal31950242011-07-14 11:53:38 -0700244 // must Stop WiFi instance, to clear its list of services.
245 // otherwise, the WiFi instance will not be deleted. (because
246 // services reference a WiFi instance, creating a cycle.)
Eric Shienbrood9a245532012-03-07 14:20:39 -0500247 wifi_->Stop(NULL, ResultCallback());
mukesh agrawal5c4dd0b2011-09-14 13:53:14 -0700248 wifi_->set_dhcp_provider(NULL);
mukesh agrawal3ab56ac2012-05-31 14:41:43 -0700249 // Reset scope logging, to avoid interfering with other tests.
250 ScopeLogger::GetInstance()->EnableScopesByName("-wifi");
Christopher Wileyd34a47c2012-08-01 15:42:45 -0700251 ScopeLogger::GetInstance()->set_verbose_level(0);
mukesh agrawalf2f68a52011-09-01 12:15:48 -0700252 }
253
Paul Stewart549d44c2012-07-03 12:40:25 -0700254 // Needs to be public since it is called via Invoke().
255 void StopWiFi() {
Paul Stewart549d44c2012-07-03 12:40:25 -0700256 wifi_->SetEnabled(false); // Stop(NULL, ResultCallback());
257 }
258
Christopher Wileyc6184482012-10-24 15:31:56 -0700259 // Needs to be public since it is called via Invoke().
260 void ThrowDBusError() {
261 throw DBus::Error("SomeDBusType", "A handy message");
262 }
263
Paul Stewart549d44c2012-07-03 12:40:25 -0700264 void UnloadService(const ServiceRefPtr &service) {
265 service->Unload();
266 }
267
mukesh agrawal31950242011-07-14 11:53:38 -0700268 protected:
mukesh agrawal7ec71312011-11-10 02:08:26 +0000269 typedef scoped_refptr<MockWiFiService> MockWiFiServiceRefPtr;
270
mukesh agrawal31950242011-07-14 11:53:38 -0700271 class TestProxyFactory : public ProxyFactory {
272 public:
Paul Stewarte369ece2012-05-22 09:11:03 -0700273 explicit TestProxyFactory(WiFiObjectTest *test);
mukesh agrawal31950242011-07-14 11:53:38 -0700274
275 virtual SupplicantProcessProxyInterface *CreateSupplicantProcessProxy(
mukesh agrawal1830fa12011-09-26 14:31:40 -0700276 const char */*dbus_path*/, const char */*dbus_addr*/) {
mukesh agrawaldc42bb32011-07-28 10:40:26 -0700277 return test_->supplicant_process_proxy_.release();
mukesh agrawal31950242011-07-14 11:53:38 -0700278 }
279
280 virtual SupplicantInterfaceProxyInterface *CreateSupplicantInterfaceProxy(
mukesh agrawal1830fa12011-09-26 14:31:40 -0700281 const WiFiRefPtr &/*wifi*/,
282 const DBus::Path &/*object_path*/,
283 const char */*dbus_addr*/) {
mukesh agrawaldc42bb32011-07-28 10:40:26 -0700284 return test_->supplicant_interface_proxy_.release();
mukesh agrawal31950242011-07-14 11:53:38 -0700285 }
286
mukesh agrawalb20776f2012-02-10 16:00:36 -0800287 MOCK_METHOD3(CreateSupplicantBSSProxy,
288 SupplicantBSSProxyInterface *(
289 WiFiEndpoint *wifi_endpoint,
290 const DBus::Path &object_path,
291 const char *dbus_addr));
292
mukesh agrawal31950242011-07-14 11:53:38 -0700293 private:
mukesh agrawalb20776f2012-02-10 16:00:36 -0800294 SupplicantBSSProxyInterface *CreateSupplicantBSSProxyInternal(
295 WiFiEndpoint */*wifi_endpoint*/,
296 const DBus::Path &/*object_path*/,
297 const char */*dbus_addr*/) {
298 return test_->supplicant_bss_proxy_.release();
299 }
300
Paul Stewarte369ece2012-05-22 09:11:03 -0700301 WiFiObjectTest *test_;
mukesh agrawal31950242011-07-14 11:53:38 -0700302 };
303
mukesh agrawalb66c6462012-05-07 11:45:25 -0700304 void CancelScanTimer() {
305 wifi_->scan_timer_callback_.Cancel();
306 }
mukesh agrawal15908392011-11-16 18:29:25 +0000307 WiFiServiceRefPtr CreateServiceForEndpoint(const WiFiEndpoint &endpoint) {
308 bool hidden_ssid = false;
309 return wifi_->CreateServiceForEndpoint(endpoint, hidden_ssid);
310 }
mukesh agrawalb66c6462012-05-07 11:45:25 -0700311 void FireScanTimer() {
312 wifi_->ScanTimerHandler();
313 }
mukesh agrawal15908392011-11-16 18:29:25 +0000314 const WiFiServiceRefPtr &GetCurrentService() {
315 return wifi_->current_service_;
316 }
Paul Stewartbc6e7392012-05-24 07:07:48 -0700317 void SetCurrentService(const WiFiServiceRefPtr &service) {
318 wifi_->current_service_ = service;
319 }
mukesh agrawal31950242011-07-14 11:53:38 -0700320 const WiFi::EndpointMap &GetEndpointMap() {
mukesh agrawal165e6142011-11-22 02:22:56 +0000321 return wifi_->endpoint_by_rpcid_;
mukesh agrawal31950242011-07-14 11:53:38 -0700322 }
mukesh agrawal15908392011-11-16 18:29:25 +0000323 const WiFiServiceRefPtr &GetPendingService() {
324 return wifi_->pending_service_;
325 }
Paul Stewart2b05e622012-07-13 20:38:44 -0700326 const base::CancelableClosure &GetPendingTimeout() {
327 return wifi_->pending_timeout_callback_;
328 }
Paul Stewart1aff7302012-08-04 20:04:47 -0700329 const base::CancelableClosure &GetReconnectTimeoutCallback() {
Paul Stewart44663922012-07-30 11:03:03 -0700330 return wifi_->reconnect_timeout_callback_;
331 }
Paul Stewart1aff7302012-08-04 20:04:47 -0700332 int GetReconnectTimeoutSeconds() {
333 return WiFi::kReconnectTimeoutSeconds;
334 }
mukesh agrawalb66c6462012-05-07 11:45:25 -0700335 const base::CancelableClosure &GetScanTimer() {
336 return wifi_->scan_timer_callback_;
337 }
mukesh agrawal165e6142011-11-22 02:22:56 +0000338 const vector<WiFiServiceRefPtr> &GetServices() {
339 return wifi_->services_;
mukesh agrawal31950242011-07-14 11:53:38 -0700340 }
341 // note: the tests need the proxies referenced by WiFi (not the
Paul Stewarte369ece2012-05-22 09:11:03 -0700342 // proxies instantiated by WiFiObjectTest), to ensure that WiFi
mukesh agrawal31950242011-07-14 11:53:38 -0700343 // sets up its proxies correctly.
344 SupplicantProcessProxyInterface *GetSupplicantProcessProxy() {
345 return wifi_->supplicant_process_proxy_.get();
346 }
mukesh agrawal8a3188d2011-12-01 20:56:44 +0000347 MockSupplicantInterfaceProxy *GetSupplicantInterfaceProxy() {
348 return dynamic_cast<MockSupplicantInterfaceProxy *>(
349 wifi_->supplicant_interface_proxy_.get());
mukesh agrawal31950242011-07-14 11:53:38 -0700350 }
mukesh agrawal7ec71312011-11-10 02:08:26 +0000351 const string &GetSupplicantState() {
352 return wifi_->supplicant_state_;
353 }
354 void InitiateConnect(WiFiServiceRefPtr service) {
mukesh agrawal6e277772011-09-29 15:04:23 -0700355 map<string, ::DBus::Variant> params;
356 wifi_->ConnectTo(service, params);
mukesh agrawal31950242011-07-14 11:53:38 -0700357 }
mukesh agrawal0ed0f2e2011-12-05 20:36:17 +0000358 void InitiateDisconnect(WiFiServiceRefPtr service) {
359 wifi_->DisconnectFrom(service);
360 }
mukesh agrawal15908392011-11-16 18:29:25 +0000361 WiFiEndpointRefPtr MakeEndpoint(const string &ssid, const string &bssid) {
mukesh agrawale1d90e92012-02-15 17:36:08 -0800362 return WiFiEndpoint::MakeOpenEndpoint(
363 &proxy_factory_, NULL, ssid, bssid, 0, 0);
mukesh agrawal15908392011-11-16 18:29:25 +0000364 }
mukesh agrawalcf24a242012-05-21 16:46:11 -0700365 MockWiFiServiceRefPtr MakeMockService(const std::string &security) {
mukesh agrawal7ec71312011-11-10 02:08:26 +0000366 vector<uint8_t> ssid(1, 'a');
367 return new MockWiFiService(
368 &control_interface_,
Paul Stewarte369ece2012-05-22 09:11:03 -0700369 event_dispatcher_,
Thieu Le3426c8f2012-01-11 17:35:11 -0800370 &metrics_,
mukesh agrawal7ec71312011-11-10 02:08:26 +0000371 &manager_,
372 wifi_,
373 ssid,
374 flimflam::kModeManaged,
mukesh agrawalcf24a242012-05-21 16:46:11 -0700375 security,
mukesh agrawal7ec71312011-11-10 02:08:26 +0000376 false);
377 }
Ben Chan381fdcc2012-10-14 21:10:36 -0700378 bool RemoveNetwork(const ::DBus::Path &network) {
379 return wifi_->RemoveNetwork(network);
380 }
mukesh agrawal261daca2011-12-02 18:56:56 +0000381 void RemoveBSS(const ::DBus::Path &bss_path);
mukesh agrawal31950242011-07-14 11:53:38 -0700382 void ReportBSS(const ::DBus::Path &bss_path,
383 const string &ssid,
384 const string &bssid,
385 int16_t signal_strength,
Thieu Lee41a72d2012-02-06 20:46:51 +0000386 uint16 frequency,
mukesh agrawal31950242011-07-14 11:53:38 -0700387 const char *mode);
Paul Stewart66c86002012-01-30 18:00:52 -0800388 void ClearCachedCredentials() {
389 wifi_->ClearCachedCredentials();
390 }
mukesh agrawalc01f3982012-01-24 13:48:39 -0800391 void ReportIPConfigComplete() {
Darin Petkov79d74c92012-03-07 17:20:32 +0100392 wifi_->OnIPConfigUpdated(dhcp_config_, true);
mukesh agrawalc01f3982012-01-24 13:48:39 -0800393 }
mukesh agrawalf2f68a52011-09-01 12:15:48 -0700394 void ReportLinkUp() {
395 wifi_->LinkEvent(IFF_LOWER_UP, IFF_LOWER_UP);
396 }
mukesh agrawal31950242011-07-14 11:53:38 -0700397 void ReportScanDone() {
mukesh agrawaldc42bb32011-07-28 10:40:26 -0700398 wifi_->ScanDoneTask();
mukesh agrawal31950242011-07-14 11:53:38 -0700399 }
mukesh agrawal15908392011-11-16 18:29:25 +0000400 void ReportCurrentBSSChanged(const string &new_bss) {
401 wifi_->CurrentBSSChanged(new_bss);
402 }
mukesh agrawal7ec71312011-11-10 02:08:26 +0000403 void ReportStateChanged(const string &new_state) {
404 wifi_->StateChanged(new_state);
405 }
mukesh agrawalcf24a242012-05-21 16:46:11 -0700406 void SetPendingService(const WiFiServiceRefPtr &service) {
407 wifi_->pending_service_ = service;
408 }
mukesh agrawalb66c6462012-05-07 11:45:25 -0700409 void SetScanInterval(uint16_t interval_seconds) {
410 wifi_->SetScanInterval(interval_seconds, NULL);
411 }
Paul Stewarte369ece2012-05-22 09:11:03 -0700412 uint16_t GetScanInterval() {
413 return wifi_->GetScanInterval(NULL);
414 }
Darin Petkov2b8e44e2012-06-25 15:13:26 +0200415 void StartWiFi(bool supplicant_present) {
Darin Petkov2b8e44e2012-06-25 15:13:26 +0200416 wifi_->supplicant_present_ = supplicant_present;
mukesh agrawalb66c6462012-05-07 11:45:25 -0700417 wifi_->SetEnabled(true); // Start(NULL, ResultCallback());
mukesh agrawal31950242011-07-14 11:53:38 -0700418 }
Darin Petkov2b8e44e2012-06-25 15:13:26 +0200419 void StartWiFi() {
420 StartWiFi(true);
421 }
mukesh agrawal2f9df4e2012-08-08 12:29:20 -0700422 void OnAfterResume() {
423 wifi_->OnAfterResume();
424 }
425 void OnBeforeSuspend() {
426 wifi_->OnBeforeSuspend();
427 }
Darin Petkov2b8e44e2012-06-25 15:13:26 +0200428 void OnSupplicantAppear() {
429 wifi_->OnSupplicantAppear(":1.7");
430 EXPECT_TRUE(wifi_->supplicant_present_);
431 }
432 void OnSupplicantVanish() {
433 wifi_->OnSupplicantVanish();
434 EXPECT_FALSE(wifi_->supplicant_present_);
435 }
436 bool GetSupplicantPresent() {
437 return wifi_->supplicant_present_;
438 }
mukesh agrawalcf24a242012-05-21 16:46:11 -0700439 WiFiServiceRefPtr GetOpenService(const char *service_type,
440 const char *ssid,
441 const char *mode,
442 Error *result) {
443 return GetServiceInner(service_type, ssid, mode, NULL, NULL, false, result);
mukesh agrawal7a4e4002011-09-06 11:26:05 -0700444 }
mukesh agrawalcf24a242012-05-21 16:46:11 -0700445 WiFiServiceRefPtr GetService(const char *service_type,
446 const char *ssid,
447 const char *mode,
448 const char *security,
449 const char *passphrase,
450 Error *result) {
451 return GetServiceInner(service_type, ssid, mode, security, passphrase,
452 false, result);
Paul Stewartced6a0b2011-11-08 15:32:04 -0800453 }
454 WiFiServiceRefPtr GetServiceInner(const char *service_type,
455 const char *ssid,
456 const char *mode,
457 const char *security,
458 const char *passphrase,
Paul Stewarta41e38d2011-11-11 07:47:29 -0800459 bool allow_hidden,
Paul Stewartced6a0b2011-11-08 15:32:04 -0800460 Error *result) {
mukesh agrawal7a4e4002011-09-06 11:26:05 -0700461 map<string, ::DBus::Variant> args;
mukesh agrawal7a4e4002011-09-06 11:26:05 -0700462 // in general, we want to avoid D-Bus specific code for any RPCs
463 // that come in via adaptors. we make an exception here, because
464 // calls to GetWifiService are rerouted from the Manager object to
465 // the Wifi class.
466 if (service_type != NULL)
467 args[flimflam::kTypeProperty].writer().append_string(service_type);
468 if (ssid != NULL)
469 args[flimflam::kSSIDProperty].writer().append_string(ssid);
470 if (mode != NULL)
471 args[flimflam::kModeProperty].writer().append_string(mode);
472 if (security != NULL)
473 args[flimflam::kSecurityProperty].writer().append_string(security);
474 if (passphrase != NULL)
475 args[flimflam::kPassphraseProperty].writer().append_string(passphrase);
Paul Stewarta41e38d2011-11-11 07:47:29 -0800476 if (!allow_hidden)
477 args[flimflam::kWifiHiddenSsid].writer().append_bool(false);
mukesh agrawal7a4e4002011-09-06 11:26:05 -0700478
Paul Stewartced6a0b2011-11-08 15:32:04 -0800479 Error e;
480 KeyValueStore args_kv;
mukesh agrawal7a4e4002011-09-06 11:26:05 -0700481 DBusAdaptor::ArgsToKeyValueStore(args, &args_kv, &e);
Paul Stewartced6a0b2011-11-08 15:32:04 -0800482 return wifi_->GetService(args_kv, result);
mukesh agrawal7a4e4002011-09-06 11:26:05 -0700483 }
Paul Stewartcb59fed2012-03-21 21:14:46 -0700484
485 WiFiServiceRefPtr GetServiceWithKeyValues(const KeyValueStore &args,
486 Error *result) {
487 return wifi_->GetService(args, result);
488 }
489
Paul Stewarta41e38d2011-11-11 07:47:29 -0800490 WiFiServiceRefPtr FindService(const vector<uint8_t> &ssid,
491 const string &mode,
492 const string &security) {
493 return wifi_->FindService(ssid, mode, security);
494 }
495 bool LoadHiddenServices(StoreInterface *storage) {
496 return wifi_->LoadHiddenServices(storage);
497 }
498 void SetupHiddenStorage(MockStore *storage, const string &ssid, string *id) {
499 const string hex_ssid = base::HexEncode(ssid.data(), ssid.size());
500 *id = StringToLowerASCII(base::StringPrintf("%s_%s_%s_%s_%s",
501 flimflam::kTypeWifi,
502 kDeviceAddress,
503 hex_ssid.c_str(),
504 flimflam::kModeManaged,
505 flimflam::kSecurityNone));
506 const char *groups[] = { id->c_str() };
507 EXPECT_CALL(*storage, GetGroupsWithKey(flimflam::kWifiHiddenSsid))
508 .WillRepeatedly(Return(set<string>(groups, groups + 1)));
509 EXPECT_CALL(*storage, GetBool(StrEq(*id), flimflam::kWifiHiddenSsid, _))
510 .WillRepeatedly(DoAll(SetArgumentPointee<2>(true), Return(true)));
511 EXPECT_CALL(*storage, GetString(StrEq(*id), flimflam::kSSIDProperty, _))
512 .WillRepeatedly(DoAll(SetArgumentPointee<2>(hex_ssid), Return(true)));
513 }
Darin Petkov4a66cc52012-06-15 10:08:29 +0200514
Paul Stewart549d44c2012-07-03 12:40:25 -0700515 WiFiService *SetupConnectedService(const DBus::Path &network_path) {
516 EXPECT_CALL(*manager(), RegisterService(_)).Times(AnyNumber());
517 EXPECT_CALL(*dhcp_provider(), CreateConfig(_, _, _, _)).Times(AnyNumber());
518 EXPECT_CALL(*dhcp_config_.get(), RequestIP()).Times(AnyNumber());
519 EXPECT_CALL(*manager(), UpdateService(_)).Times(AnyNumber());
520 if (!network_path.empty()) {
521 EXPECT_CALL(*supplicant_interface_proxy_, AddNetwork(_))
522 .WillOnce(Return(network_path));
523 }
524
525 StartWiFi();
Paul Stewart44663922012-07-30 11:03:03 -0700526 ReportBSS(kBSSName, kSSIDName, "00:00:00:00:00:00", 0, 0,
527 kNetworkModeAdHoc);
Paul Stewart549d44c2012-07-03 12:40:25 -0700528 WiFiService *service(GetServices().begin()->get());
Paul Stewart2b05e622012-07-13 20:38:44 -0700529 EXPECT_TRUE(GetPendingTimeout().IsCancelled());
Paul Stewart549d44c2012-07-03 12:40:25 -0700530 InitiateConnect(service);
Paul Stewart2b05e622012-07-13 20:38:44 -0700531 EXPECT_FALSE(GetPendingTimeout().IsCancelled());
Paul Stewart44663922012-07-30 11:03:03 -0700532 ReportCurrentBSSChanged(kBSSName);
Paul Stewart2b05e622012-07-13 20:38:44 -0700533 EXPECT_TRUE(GetPendingTimeout().IsCancelled());
Paul Stewart549d44c2012-07-03 12:40:25 -0700534 ReportStateChanged(wpa_supplicant::kInterfaceStateCompleted);
535
536 EXPECT_EQ(service, GetCurrentService());
537
538 return service;
539 }
540
Darin Petkov4a66cc52012-06-15 10:08:29 +0200541 bool SetBgscanMethod(const string &method) {
542 ::DBus::Error error;
543 return DBusAdaptor::SetProperty(
544 wifi_->mutable_store(),
545 flimflam::kBgscanMethodProperty,
546 DBusAdaptor::StringToVariant(method),
547 &error);
548 }
549
550 void AppendBgscan(WiFiService *service,
551 std::map<std::string, DBus::Variant> *service_params) {
552 wifi_->AppendBgscan(service, service_params);
553 }
554
Paul Stewartbc6e7392012-05-24 07:07:48 -0700555 void ReportCertification(const map<string, ::DBus::Variant> &properties) {
556 wifi_->CertificationTask(properties);
557 }
Paul Stewart10ccbb32012-04-26 15:59:30 -0700558
Paul Stewarte369ece2012-05-22 09:11:03 -0700559 void RestartFastScanAttempts() {
560 wifi_->RestartFastScanAttempts();
561 }
562
Paul Stewart1aff7302012-08-04 20:04:47 -0700563 void StartReconnectTimer() {
564 wifi_->StartReconnectTimer();
565 }
566
567 void StopReconnectTimer() {
568 wifi_->StopReconnectTimer();
569 }
570
Paul Stewart3c508e12012-08-09 11:40:06 -0700571 void SetLinkMonitor(LinkMonitor *link_monitor) {
572 wifi_->set_link_monitor(link_monitor);
573 }
574
575 void OnLinkMonitorFailure() {
576 wifi_->OnLinkMonitorFailure();
577 }
578
Paul Stewart10ccbb32012-04-26 15:59:30 -0700579 NiceMockControl *control_interface() {
580 return &control_interface_;
581 }
582
mukesh agrawal32399322011-09-01 10:53:43 -0700583 MockManager *manager() {
584 return &manager_;
585 }
Gary Moraind9f893f2012-02-06 10:03:40 -0800586
587 MockDeviceInfo *device_info() {
588 return &device_info_;
589 }
590
591 MockDHCPProvider *dhcp_provider() {
592 return &dhcp_provider_;
593 }
594
mukesh agrawal32399322011-09-01 10:53:43 -0700595 const WiFiConstRefPtr wifi() const {
596 return wifi_;
597 }
598
mukesh agrawalb20776f2012-02-10 16:00:36 -0800599 TestProxyFactory *proxy_factory() {
600 return &proxy_factory_;
601 }
602
Paul Stewarte369ece2012-05-22 09:11:03 -0700603 EventDispatcher *event_dispatcher_;
mukesh agrawal5c4dd0b2011-09-14 13:53:14 -0700604 NiceMock<MockRTNLHandler> rtnl_handler_;
mukesh agrawal5c05b292012-03-07 10:12:52 -0800605 MockTime time_;
mukesh agrawal31950242011-07-14 11:53:38 -0700606
607 private:
mukesh agrawaldc42bb32011-07-28 10:40:26 -0700608 NiceMockControl control_interface_;
Thieu Le3426c8f2012-01-11 17:35:11 -0800609 MockMetrics metrics_;
mukesh agrawalf2f68a52011-09-01 12:15:48 -0700610 MockGLib glib_;
Chris Masone2ae797d2011-08-23 20:41:00 -0700611 MockManager manager_;
mukesh agrawalc01f3982012-01-24 13:48:39 -0800612 MockDeviceInfo device_info_;
mukesh agrawal31950242011-07-14 11:53:38 -0700613 WiFiRefPtr wifi_;
mukesh agrawal31950242011-07-14 11:53:38 -0700614
615 // protected fields interspersed between private fields, due to
616 // initialization order
617 protected:
618 static const char kDeviceName[];
Chris Masone626719f2011-08-18 16:58:48 -0700619 static const char kDeviceAddress[];
mukesh agrawal31950242011-07-14 11:53:38 -0700620 static const char kNetworkModeAdHoc[];
621 static const char kNetworkModeInfrastructure[];
Paul Stewart44663922012-07-30 11:03:03 -0700622 static const char kBSSName[];
623 static const char kSSIDName[];
mukesh agrawal31950242011-07-14 11:53:38 -0700624
mukesh agrawaldc42bb32011-07-28 10:40:26 -0700625 scoped_ptr<MockSupplicantProcessProxy> supplicant_process_proxy_;
626 scoped_ptr<MockSupplicantInterfaceProxy> supplicant_interface_proxy_;
mukesh agrawalb20776f2012-02-10 16:00:36 -0800627 scoped_ptr<MockSupplicantBSSProxy> supplicant_bss_proxy_;
mukesh agrawalf2f68a52011-09-01 12:15:48 -0700628 MockDHCPProvider dhcp_provider_;
629 scoped_refptr<MockDHCPConfig> dhcp_config_;
Darin Petkov2b8e44e2012-06-25 15:13:26 +0200630 NiceMock<MockDBusManager> *dbus_manager_;
mukesh agrawal31950242011-07-14 11:53:38 -0700631
632 private:
633 TestProxyFactory proxy_factory_;
mukesh agrawal31950242011-07-14 11:53:38 -0700634};
635
Paul Stewarte369ece2012-05-22 09:11:03 -0700636const char WiFiObjectTest::kDeviceName[] = "wlan0";
637const char WiFiObjectTest::kDeviceAddress[] = "000102030405";
638const char WiFiObjectTest::kNetworkModeAdHoc[] = "ad-hoc";
639const char WiFiObjectTest::kNetworkModeInfrastructure[] = "infrastructure";
Paul Stewart44663922012-07-30 11:03:03 -0700640const char WiFiObjectTest::kBSSName[] = "bss0";
641const char WiFiObjectTest::kSSIDName[] = "ssid0";
mukesh agrawal31950242011-07-14 11:53:38 -0700642
Paul Stewarte369ece2012-05-22 09:11:03 -0700643void WiFiObjectTest::RemoveBSS(const ::DBus::Path &bss_path) {
mukesh agrawalb4bc57d2011-12-07 01:07:47 +0000644 wifi_->BSSRemovedTask(bss_path);
mukesh agrawal261daca2011-12-02 18:56:56 +0000645}
646
Paul Stewarte369ece2012-05-22 09:11:03 -0700647void WiFiObjectTest::ReportBSS(const ::DBus::Path &bss_path,
mukesh agrawal31950242011-07-14 11:53:38 -0700648 const string &ssid,
649 const string &bssid,
650 int16_t signal_strength,
Thieu Lee41a72d2012-02-06 20:46:51 +0000651 uint16 frequency,
mukesh agrawal31950242011-07-14 11:53:38 -0700652 const char *mode) {
653 map<string, ::DBus::Variant> bss_properties;
654
655 {
656 DBus::MessageIter writer(bss_properties["SSID"].writer());
657 writer << vector<uint8_t>(ssid.begin(), ssid.end());
658 }
659 {
660 string bssid_nosep;
661 vector<uint8_t> bssid_bytes;
662 RemoveChars(bssid, ":", &bssid_nosep);
663 base::HexStringToBytes(bssid_nosep, &bssid_bytes);
664
665 DBus::MessageIter writer(bss_properties["BSSID"].writer());
666 writer << bssid_bytes;
667 }
Thieu Lee41a72d2012-02-06 20:46:51 +0000668 bss_properties[wpa_supplicant::kBSSPropertySignal].writer().
669 append_int16(signal_strength);
670 bss_properties[wpa_supplicant::kBSSPropertyFrequency].writer().
671 append_uint16(frequency);
672 bss_properties[wpa_supplicant::kBSSPropertyMode].writer().append_string(mode);
mukesh agrawalb4bc57d2011-12-07 01:07:47 +0000673 wifi_->BSSAddedTask(bss_path, bss_properties);
mukesh agrawal31950242011-07-14 11:53:38 -0700674}
675
Paul Stewarte369ece2012-05-22 09:11:03 -0700676WiFiObjectTest::TestProxyFactory::TestProxyFactory(WiFiObjectTest *test)
mukesh agrawalb20776f2012-02-10 16:00:36 -0800677 : test_(test) {
678 EXPECT_CALL(*this, CreateSupplicantBSSProxy(_, _, _)).Times(AnyNumber());
679 ON_CALL(*this, CreateSupplicantBSSProxy(_, _, _))
680 .WillByDefault(
681 Invoke(this, (&TestProxyFactory::CreateSupplicantBSSProxyInternal)));
682}
683
Paul Stewarte369ece2012-05-22 09:11:03 -0700684// Most of our tests involve using a real EventDispatcher object.
685class WiFiMainTest : public WiFiObjectTest {
686 public:
687 WiFiMainTest() : WiFiObjectTest(&dispatcher_) {}
688
689 protected:
690 EventDispatcher dispatcher_;
691};
692
mukesh agrawal31950242011-07-14 11:53:38 -0700693TEST_F(WiFiMainTest, ProxiesSetUpDuringStart) {
694 EXPECT_TRUE(GetSupplicantProcessProxy() == NULL);
695 EXPECT_TRUE(GetSupplicantInterfaceProxy() == NULL);
696
697 StartWiFi();
698 EXPECT_FALSE(GetSupplicantProcessProxy() == NULL);
699 EXPECT_FALSE(GetSupplicantInterfaceProxy() == NULL);
700}
701
Darin Petkov2b8e44e2012-06-25 15:13:26 +0200702TEST_F(WiFiMainTest, SupplicantPresent) {
703 EXPECT_FALSE(GetSupplicantPresent());
704}
705
706TEST_F(WiFiMainTest, OnSupplicantAppearStarted) {
707 EXPECT_TRUE(GetSupplicantProcessProxy() == NULL);
708
709 EXPECT_CALL(*dbus_manager_, WatchName(wpa_supplicant::kDBusAddr, _, _));
710 StartWiFi(false); // No supplicant present.
711 EXPECT_TRUE(GetSupplicantProcessProxy() == NULL);
712
713 OnSupplicantAppear();
714 EXPECT_FALSE(GetSupplicantProcessProxy() == NULL);
Darin Petkov9cd7ca12012-07-03 11:06:40 +0200715
716 // If supplicant reappears while the device is started, the device should be
717 // restarted.
718 EXPECT_CALL(*manager(), DeregisterDevice(_));
719 EXPECT_CALL(*manager(), RegisterDevice(_));
720 OnSupplicantAppear();
Darin Petkov2b8e44e2012-06-25 15:13:26 +0200721}
722
723TEST_F(WiFiMainTest, OnSupplicantAppearStopped) {
724 EXPECT_TRUE(GetSupplicantProcessProxy() == NULL);
725
726 OnSupplicantAppear();
727 EXPECT_TRUE(GetSupplicantProcessProxy() == NULL);
Darin Petkov9cd7ca12012-07-03 11:06:40 +0200728
729 // If supplicant reappears while the device is stopped, the device should not
730 // be restarted.
731 EXPECT_CALL(*manager(), DeregisterDevice(_)).Times(0);
732 OnSupplicantAppear();
Darin Petkov2b8e44e2012-06-25 15:13:26 +0200733}
734
735TEST_F(WiFiMainTest, OnSupplicantVanishStarted) {
736 EXPECT_TRUE(GetSupplicantProcessProxy() == NULL);
737
738 StartWiFi();
739 EXPECT_FALSE(GetSupplicantProcessProxy() == NULL);
740 EXPECT_TRUE(GetSupplicantPresent());
741
742 EXPECT_CALL(*manager(), DeregisterDevice(_));
743 EXPECT_CALL(*manager(), RegisterDevice(_));
744 OnSupplicantVanish();
Darin Petkov2b8e44e2012-06-25 15:13:26 +0200745}
746
747TEST_F(WiFiMainTest, OnSupplicantVanishStopped) {
748 OnSupplicantAppear();
749 EXPECT_TRUE(GetSupplicantPresent());
750 EXPECT_CALL(*manager(), DeregisterDevice(_)).Times(0);
751 OnSupplicantVanish();
752}
753
Paul Stewart549d44c2012-07-03 12:40:25 -0700754TEST_F(WiFiMainTest, OnSupplicantVanishedWhileConnected) {
755 MockSupplicantInterfaceProxy &supplicant_interface_proxy =
756 *supplicant_interface_proxy_;
757 WiFiService *service(SetupConnectedService(DBus::Path()));
758 ScopedMockLog log;
759 EXPECT_CALL(log, Log(_, _, _)).Times(AnyNumber());
Christopher Wileyc6184482012-10-24 15:31:56 -0700760 EXPECT_CALL(log, Log(logging::LOG_ERROR, _,
Paul Stewart549d44c2012-07-03 12:40:25 -0700761 EndsWith("silently resetting current_service_.")));
762 EXPECT_CALL(*manager(), DeregisterDevice(_))
763 .WillOnce(InvokeWithoutArgs(this, &WiFiObjectTest::StopWiFi));
764 EXPECT_CALL(*manager(), DeregisterService(ServiceRefPtr(service)))
765 .WillOnce(Invoke(this, &WiFiObjectTest::UnloadService));
766 EXPECT_CALL(supplicant_interface_proxy, Disconnect()).Times(0);
767 EXPECT_CALL(*manager(), RegisterDevice(_));
768 OnSupplicantVanish();
769 EXPECT_TRUE(GetCurrentService() == NULL);
770}
771
mukesh agrawal31950242011-07-14 11:53:38 -0700772TEST_F(WiFiMainTest, CleanStart) {
773 EXPECT_CALL(*supplicant_process_proxy_, CreateInterface(_));
774 EXPECT_CALL(*supplicant_process_proxy_, GetInterface(_))
775 .Times(AnyNumber())
776 .WillRepeatedly(Throw(
777 DBus::Error(
778 "fi.w1.wpa_supplicant1.InterfaceUnknown",
779 "test threw fi.w1.wpa_supplicant1.InterfaceUnknown")));
780 EXPECT_CALL(*supplicant_interface_proxy_, Scan(_));
mukesh agrawalb66c6462012-05-07 11:45:25 -0700781 EXPECT_TRUE(GetScanTimer().IsCancelled());
mukesh agrawal31950242011-07-14 11:53:38 -0700782 StartWiFi();
mukesh agrawal32399322011-09-01 10:53:43 -0700783 dispatcher_.DispatchPendingEvents();
mukesh agrawalb66c6462012-05-07 11:45:25 -0700784 EXPECT_FALSE(GetScanTimer().IsCancelled());
mukesh agrawal31950242011-07-14 11:53:38 -0700785}
786
Ben Chan381fdcc2012-10-14 21:10:36 -0700787TEST_F(WiFiMainTest, RemoveNetwork) {
788 DBus::Path network = "/test/path";
789 EXPECT_CALL(*supplicant_interface_proxy_, RemoveNetwork(network));
790 StartWiFi();
791 EXPECT_TRUE(RemoveNetwork(network));
792}
793
794TEST_F(WiFiMainTest, RemoveNetworkWhenSupplicantReturnsNetworkUnknown) {
795 DBus::Path network = "/test/path";
796 EXPECT_CALL(*supplicant_interface_proxy_, RemoveNetwork(network))
797 .WillRepeatedly(Throw(
798 DBus::Error(
799 "fi.w1.wpa_supplicant1.NetworkUnknown",
800 "test threw fi.w1.wpa_supplicant1.NetworkUnknown")));
801 StartWiFi();
802 EXPECT_TRUE(RemoveNetwork(network));
803}
804
805TEST_F(WiFiMainTest, RemoveNetworkWhenSupplicantReturnsInvalidArgs) {
806 DBus::Path network = "/test/path";
807 EXPECT_CALL(*supplicant_interface_proxy_, RemoveNetwork(network))
808 .WillRepeatedly(Throw(
809 DBus::Error(
810 "fi.w1.wpa_supplicant1.InvalidArgs",
811 "test threw fi.w1.wpa_supplicant1.InvalidArgs")));
812 StartWiFi();
813 EXPECT_FALSE(RemoveNetwork(network));
814}
815
816TEST_F(WiFiMainTest, RemoveNetworkWhenSupplicantReturnsUnknownError) {
817 DBus::Path network = "/test/path";
818 EXPECT_CALL(*supplicant_interface_proxy_, RemoveNetwork(network))
819 .WillRepeatedly(Throw(
820 DBus::Error(
821 "fi.w1.wpa_supplicant1.UnknownError",
822 "test threw fi.w1.wpa_supplicant1.UnknownError")));
823 StartWiFi();
824 EXPECT_FALSE(RemoveNetwork(network));
825}
826
mukesh agrawal31950242011-07-14 11:53:38 -0700827TEST_F(WiFiMainTest, Restart) {
828 EXPECT_CALL(*supplicant_process_proxy_, CreateInterface(_))
829 .Times(AnyNumber())
830 .WillRepeatedly(Throw(
831 DBus::Error(
832 "fi.w1.wpa_supplicant1.InterfaceExists",
Darin Petkov2b8e44e2012-06-25 15:13:26 +0200833 "test threw fi.w1.wpa_supplicant1.InterfaceExists")));
mukesh agrawal31950242011-07-14 11:53:38 -0700834 EXPECT_CALL(*supplicant_process_proxy_, GetInterface(_));
835 EXPECT_CALL(*supplicant_interface_proxy_, Scan(_));
836 StartWiFi();
mukesh agrawal32399322011-09-01 10:53:43 -0700837 dispatcher_.DispatchPendingEvents();
mukesh agrawal31950242011-07-14 11:53:38 -0700838}
839
840TEST_F(WiFiMainTest, StartClearsState) {
841 EXPECT_CALL(*supplicant_interface_proxy_, RemoveAllNetworks());
842 EXPECT_CALL(*supplicant_interface_proxy_, FlushBSS(_));
843 StartWiFi();
844}
845
mukesh agrawal2f9df4e2012-08-08 12:29:20 -0700846TEST_F(WiFiMainTest, ResumeStartsScanWhenIdle) {
Gary Morainac1bdb42012-02-16 17:42:29 -0800847 EXPECT_CALL(*supplicant_interface_proxy_, Scan(_));
848 StartWiFi();
849 dispatcher_.DispatchPendingEvents();
850 Mock::VerifyAndClearExpectations(&supplicant_interface_proxy_);
mukesh agrawal5c05b292012-03-07 10:12:52 -0800851 ReportScanDone();
Gary Morainac1bdb42012-02-16 17:42:29 -0800852 ASSERT_TRUE(wifi()->IsIdle());
853 EXPECT_CALL(*GetSupplicantInterfaceProxy(), Scan(_));
mukesh agrawal2f9df4e2012-08-08 12:29:20 -0700854 OnAfterResume();
Gary Morainac1bdb42012-02-16 17:42:29 -0800855 dispatcher_.DispatchPendingEvents();
856}
857
mukesh agrawal2f9df4e2012-08-08 12:29:20 -0700858TEST_F(WiFiMainTest, SuspendDoesNotStartScan) {
Gary Morainac1bdb42012-02-16 17:42:29 -0800859 EXPECT_CALL(*supplicant_interface_proxy_, Scan(_));
860 StartWiFi();
861 dispatcher_.DispatchPendingEvents();
862 Mock::VerifyAndClearExpectations(&supplicant_interface_proxy_);
Gary Morainac1bdb42012-02-16 17:42:29 -0800863 ASSERT_TRUE(wifi()->IsIdle());
864 EXPECT_CALL(*GetSupplicantInterfaceProxy(), Scan(_)).Times(0);
mukesh agrawal2f9df4e2012-08-08 12:29:20 -0700865 OnBeforeSuspend();
Gary Morainac1bdb42012-02-16 17:42:29 -0800866 dispatcher_.DispatchPendingEvents();
867}
868
mukesh agrawal2f9df4e2012-08-08 12:29:20 -0700869TEST_F(WiFiMainTest, ResumeDoesNotStartScanWhenNotIdle) {
Gary Morainac1bdb42012-02-16 17:42:29 -0800870 EXPECT_CALL(*supplicant_interface_proxy_, Scan(_));
871 StartWiFi();
872
873 WiFiEndpointRefPtr ap = MakeEndpoint("an_ssid", "00:01:02:03:04:05");
874 WiFiServiceRefPtr service = CreateServiceForEndpoint(*ap);
875 Error error;
mukesh agrawal3ab56ac2012-05-31 14:41:43 -0700876 ScopedMockLog log;
Gary Morainac1bdb42012-02-16 17:42:29 -0800877 service->AddEndpoint(ap);
878 service->AutoConnect();
879 EXPECT_FALSE(wifi()->IsIdle());
880 dispatcher_.DispatchPendingEvents();
881 Mock::VerifyAndClearExpectations(&supplicant_interface_proxy_);
Gary Morainac1bdb42012-02-16 17:42:29 -0800882 ASSERT_FALSE(wifi()->IsIdle());
mukesh agrawal3ab56ac2012-05-31 14:41:43 -0700883 EXPECT_CALL(log, Log(_, _, _)).Times(AnyNumber());
884 EXPECT_CALL(log, Log(_, _, EndsWith("already scanning or connected.")));
Gary Morainac1bdb42012-02-16 17:42:29 -0800885 EXPECT_CALL(*GetSupplicantInterfaceProxy(), Scan(_)).Times(0);
mukesh agrawal2f9df4e2012-08-08 12:29:20 -0700886 OnAfterResume();
Gary Morainac1bdb42012-02-16 17:42:29 -0800887 dispatcher_.DispatchPendingEvents();
888}
889
mukesh agrawal31950242011-07-14 11:53:38 -0700890TEST_F(WiFiMainTest, ScanResults) {
Gary Moraind9f893f2012-02-06 10:03:40 -0800891 EXPECT_CALL(*manager(), RegisterService(_)).Times(AnyNumber());
mukesh agrawal31950242011-07-14 11:53:38 -0700892 StartWiFi();
Thieu Lee41a72d2012-02-06 20:46:51 +0000893 ReportBSS("bss0", "ssid0", "00:00:00:00:00:00", 0, 0, kNetworkModeAdHoc);
mukesh agrawal31950242011-07-14 11:53:38 -0700894 ReportBSS(
Thieu Lee41a72d2012-02-06 20:46:51 +0000895 "bss1", "ssid1", "00:00:00:00:00:01", 1, 0, kNetworkModeInfrastructure);
mukesh agrawal31950242011-07-14 11:53:38 -0700896 ReportBSS(
Thieu Lee41a72d2012-02-06 20:46:51 +0000897 "bss2", "ssid2", "00:00:00:00:00:02", 2, 0, kNetworkModeInfrastructure);
mukesh agrawal31950242011-07-14 11:53:38 -0700898 ReportBSS(
Thieu Lee41a72d2012-02-06 20:46:51 +0000899 "bss3", "ssid3", "00:00:00:00:00:03", 3, 0, kNetworkModeInfrastructure);
900 const uint16 frequency = 2412;
901 ReportBSS("bss4", "ssid4", "00:00:00:00:00:04", 4, frequency,
902 kNetworkModeAdHoc);
903
904 const WiFi::EndpointMap &endpoints_by_rpcid = GetEndpointMap();
905 EXPECT_EQ(5, endpoints_by_rpcid.size());
906
907 WiFi::EndpointMap::const_iterator i;
908 WiFiEndpointRefPtr endpoint;
909 for (i = endpoints_by_rpcid.begin();
910 i != endpoints_by_rpcid.end();
911 ++i) {
912 if (i->second->bssid_string() == "00:00:00:00:00:04")
913 break;
914 }
915 ASSERT_TRUE(i != endpoints_by_rpcid.end());
916 EXPECT_EQ(4, i->second->signal_strength());
917 EXPECT_EQ(frequency, i->second->frequency());
918 EXPECT_EQ("adhoc", i->second->network_mode());
mukesh agrawal31950242011-07-14 11:53:38 -0700919}
920
921TEST_F(WiFiMainTest, ScanResultsWithUpdates) {
Gary Moraind9f893f2012-02-06 10:03:40 -0800922 EXPECT_CALL(*manager(), RegisterService(_)).Times(AnyNumber());
923 EXPECT_CALL(*manager(), HasService(_)).Times(AnyNumber());
924 EXPECT_CALL(*manager(), UpdateService(_)).Times(AnyNumber());
mukesh agrawal31950242011-07-14 11:53:38 -0700925 StartWiFi();
Thieu Lee41a72d2012-02-06 20:46:51 +0000926 ReportBSS("bss0", "ssid0", "00:00:00:00:00:00", 0, 0, kNetworkModeAdHoc);
mukesh agrawal31950242011-07-14 11:53:38 -0700927 ReportBSS(
Thieu Lee41a72d2012-02-06 20:46:51 +0000928 "bss1", "ssid1", "00:00:00:00:00:01", 1, 0, kNetworkModeInfrastructure);
mukesh agrawal31950242011-07-14 11:53:38 -0700929 ReportBSS(
Thieu Lee41a72d2012-02-06 20:46:51 +0000930 "bss2", "ssid2", "00:00:00:00:00:02", 2, 0, kNetworkModeInfrastructure);
mukesh agrawal31950242011-07-14 11:53:38 -0700931 ReportBSS(
Thieu Lee41a72d2012-02-06 20:46:51 +0000932 "bss1", "ssid1", "00:00:00:00:00:01", 3, 0, kNetworkModeInfrastructure);
933 ReportBSS("bss0", "ssid0", "00:00:00:00:00:00", 4, 0, kNetworkModeAdHoc);
mukesh agrawal165e6142011-11-22 02:22:56 +0000934
935 const WiFi::EndpointMap &endpoints_by_rpcid = GetEndpointMap();
936 EXPECT_EQ(3, endpoints_by_rpcid.size());
937
938 WiFi::EndpointMap::const_iterator i;
939 WiFiEndpointRefPtr endpoint;
940 for (i = endpoints_by_rpcid.begin();
941 i != endpoints_by_rpcid.end();
942 ++i) {
943 if (i->second->bssid_string() == "00:00:00:00:00:00")
944 break;
945 }
946 ASSERT_TRUE(i != endpoints_by_rpcid.end());
947 EXPECT_EQ(4, i->second->signal_strength());
mukesh agrawal31950242011-07-14 11:53:38 -0700948}
949
950TEST_F(WiFiMainTest, ScanCompleted) {
951 StartWiFi();
mukesh agrawal261daca2011-12-02 18:56:56 +0000952 EXPECT_CALL(*manager(), RegisterService(_))
953 .Times(3);
Thieu Lee41a72d2012-02-06 20:46:51 +0000954 ReportBSS("bss0", "ssid0", "00:00:00:00:00:00", 0, 0, kNetworkModeAdHoc);
mukesh agrawal31950242011-07-14 11:53:38 -0700955 ReportBSS(
Thieu Lee41a72d2012-02-06 20:46:51 +0000956 "bss1", "ssid1", "00:00:00:00:00:01", 1, 0, kNetworkModeInfrastructure);
mukesh agrawal31950242011-07-14 11:53:38 -0700957 ReportBSS(
Thieu Lee41a72d2012-02-06 20:46:51 +0000958 "bss2", "ssid2", "00:00:00:00:00:02", 2, 0, kNetworkModeInfrastructure);
mukesh agrawal31950242011-07-14 11:53:38 -0700959 ReportScanDone();
mukesh agrawal165e6142011-11-22 02:22:56 +0000960 EXPECT_EQ(3, GetServices().size());
mukesh agrawalb3857612012-01-18 16:23:29 -0800961
mukesh agrawale9adda12012-02-09 18:33:48 -0800962 // BSSes with SSIDs that start with NULL should be filtered.
Thieu Lee41a72d2012-02-06 20:46:51 +0000963 ReportBSS("bss3", string(1, 0), "00:00:00:00:00:03", 3, 0, kNetworkModeAdHoc);
mukesh agrawale9adda12012-02-09 18:33:48 -0800964 EXPECT_EQ(3, GetEndpointMap().size());
mukesh agrawalb3857612012-01-18 16:23:29 -0800965 EXPECT_EQ(3, GetServices().size());
966
mukesh agrawale9adda12012-02-09 18:33:48 -0800967 // BSSes with empty SSIDs should be filtered.
968 ReportBSS("bss3", string(), "00:00:00:00:00:03", 3, 0, kNetworkModeAdHoc);
969 EXPECT_EQ(3, GetEndpointMap().size());
970 EXPECT_EQ(3, GetServices().size());
mukesh agrawal165e6142011-11-22 02:22:56 +0000971}
972
973TEST_F(WiFiMainTest, EndpointGroupingTogether) {
974 StartWiFi();
mukesh agrawal261daca2011-12-02 18:56:56 +0000975
976 InSequence s;
977 EXPECT_CALL(*manager(), RegisterService(_));
Gary Moraind9f893f2012-02-06 10:03:40 -0800978 EXPECT_CALL(*manager(), HasService(_));
mukesh agrawal261daca2011-12-02 18:56:56 +0000979 EXPECT_CALL(*manager(), UpdateService(_));
Thieu Lee41a72d2012-02-06 20:46:51 +0000980 ReportBSS("bss0", "ssid", "00:00:00:00:00:00", 0, 0, kNetworkModeAdHoc);
981 ReportBSS("bss1", "ssid", "00:00:00:00:00:01", 0, 0, kNetworkModeAdHoc);
mukesh agrawal165e6142011-11-22 02:22:56 +0000982 ReportScanDone();
983 EXPECT_EQ(1, GetServices().size());
984}
985
986TEST_F(WiFiMainTest, EndpointGroupingDifferentSSID) {
987 StartWiFi();
mukesh agrawal261daca2011-12-02 18:56:56 +0000988 EXPECT_CALL(*manager(), RegisterService(_))
989 .Times(2);
Thieu Lee41a72d2012-02-06 20:46:51 +0000990 ReportBSS("bss0", "ssid1", "00:00:00:00:00:00", 0, 0, kNetworkModeAdHoc);
991 ReportBSS("bss1", "ssid2", "00:00:00:00:00:01", 0, 0, kNetworkModeAdHoc);
mukesh agrawal165e6142011-11-22 02:22:56 +0000992 ReportScanDone();
993 EXPECT_EQ(2, GetServices().size());
994}
995
996TEST_F(WiFiMainTest, EndpointGroupingDifferentMode) {
997 StartWiFi();
mukesh agrawal261daca2011-12-02 18:56:56 +0000998 EXPECT_CALL(*manager(), RegisterService(_))
999 .Times(2);
Thieu Lee41a72d2012-02-06 20:46:51 +00001000 ReportBSS("bss0", "ssid", "00:00:00:00:00:00", 0, 0, kNetworkModeAdHoc);
1001 ReportBSS("bss1", "ssid", "00:00:00:00:00:01", 0, 0,
1002 kNetworkModeInfrastructure);
mukesh agrawal165e6142011-11-22 02:22:56 +00001003 ReportScanDone();
1004 EXPECT_EQ(2, GetServices().size());
mukesh agrawal31950242011-07-14 11:53:38 -07001005}
1006
mukesh agrawal261daca2011-12-02 18:56:56 +00001007TEST_F(WiFiMainTest, NonExistentBSSRemoved) {
1008 // Removal of non-existent BSS should not cause a crash.
1009 StartWiFi();
1010 RemoveBSS("bss0");
1011 EXPECT_EQ(0, GetServices().size());
1012}
1013
mukesh agrawale9adda12012-02-09 18:33:48 -08001014TEST_F(WiFiMainTest, BSSWithEmptySSIDRemoved) {
1015 // Removal of BSS with an empty SSID should not cause a crash.
1016 ReportBSS("bss", string(), "00:00:00:00:00:01", 0, 0, kNetworkModeAdHoc);
1017 StartWiFi();
1018 RemoveBSS("bss");
1019 EXPECT_EQ(0, GetServices().size());
1020}
1021
1022TEST_F(WiFiMainTest, BSSWithNullSSIDRemoved) {
1023 // Removal of BSS with a NULL SSID should not cause a crash.
1024 ReportBSS("bss", string(1, 0), "00:00:00:00:00:01", 0, 0, kNetworkModeAdHoc);
1025 StartWiFi();
1026 RemoveBSS("bss");
1027 EXPECT_EQ(0, GetServices().size());
1028}
1029
mukesh agrawal261daca2011-12-02 18:56:56 +00001030TEST_F(WiFiMainTest, LoneBSSRemoved) {
Gary Moraind9f893f2012-02-06 10:03:40 -08001031 EXPECT_CALL(*manager(), RegisterService(_)).Times(AnyNumber());
mukesh agrawal261daca2011-12-02 18:56:56 +00001032 StartWiFi();
Thieu Lee41a72d2012-02-06 20:46:51 +00001033 ReportBSS("bss0", "ssid", "00:00:00:00:00:00", 0, 0, kNetworkModeAdHoc);
mukesh agrawal261daca2011-12-02 18:56:56 +00001034 ReportScanDone();
1035 EXPECT_EQ(1, GetServices().size());
1036 EXPECT_TRUE(GetServices().front()->IsVisible());
1037
mukesh agrawal8a3188d2011-12-01 20:56:44 +00001038 EXPECT_CALL(*manager(), DeregisterService(_));
mukesh agrawal261daca2011-12-02 18:56:56 +00001039 RemoveBSS("bss0");
mukesh agrawal8a3188d2011-12-01 20:56:44 +00001040 EXPECT_TRUE(GetServices().empty());
1041}
1042
1043TEST_F(WiFiMainTest, LoneBSSRemovedWhileConnected) {
Gary Moraind9f893f2012-02-06 10:03:40 -08001044 EXPECT_CALL(*manager(), RegisterService(_)).Times(AnyNumber());
mukesh agrawal8a3188d2011-12-01 20:56:44 +00001045 StartWiFi();
Thieu Lee41a72d2012-02-06 20:46:51 +00001046 ReportBSS("bss0", "ssid", "00:00:00:00:00:00", 0, 0, kNetworkModeAdHoc);
mukesh agrawal8a3188d2011-12-01 20:56:44 +00001047 ReportScanDone();
1048 ReportCurrentBSSChanged("bss0");
1049
1050 EXPECT_CALL(*GetSupplicantInterfaceProxy(), Disconnect());
1051 EXPECT_CALL(*manager(), DeregisterService(_));
1052 RemoveBSS("bss0");
1053 EXPECT_TRUE(GetServices().empty());
1054}
1055
1056TEST_F(WiFiMainTest, LoneBSSRemovedWhileConnectedToHidden) {
Gary Moraind9f893f2012-02-06 10:03:40 -08001057 EXPECT_CALL(*manager(), UpdateService(_)).Times(AnyNumber());
1058 EXPECT_CALL(*manager(), HasService(_)).Times(AnyNumber());
mukesh agrawal8a3188d2011-12-01 20:56:44 +00001059 StartWiFi();
1060
1061 Error e;
1062 WiFiServiceRefPtr service =
1063 GetServiceInner(flimflam::kTypeWifi, "ssid", flimflam::kModeManaged,
1064 NULL, NULL, true, &e);
mukesh agrawal261daca2011-12-02 18:56:56 +00001065 EXPECT_EQ(1, GetServices().size());
mukesh agrawal8a3188d2011-12-01 20:56:44 +00001066
Thieu Lee41a72d2012-02-06 20:46:51 +00001067 ReportBSS("bss", "ssid", "00:00:00:00:00:01", 0, 0,
1068 kNetworkModeInfrastructure);
mukesh agrawal8a3188d2011-12-01 20:56:44 +00001069 ReportScanDone();
1070 ReportCurrentBSSChanged("bss");
1071 EXPECT_EQ(1, GetServices().size());
1072
1073 EXPECT_CALL(*GetSupplicantInterfaceProxy(), Disconnect());
mukesh agrawal8a3188d2011-12-01 20:56:44 +00001074 RemoveBSS("bss");
mukesh agrawal8a3188d2011-12-01 20:56:44 +00001075 EXPECT_EQ(1, GetServices().size());
1076 // Verify expectations now, because WiFi may call UpdateService when
1077 // WiFi is Stop()-ed (during TearDown()).
1078 Mock::VerifyAndClearExpectations(manager());
Gary Moraind9f893f2012-02-06 10:03:40 -08001079 EXPECT_CALL(*manager(), DeregisterService(_)).Times(AnyNumber());
mukesh agrawal261daca2011-12-02 18:56:56 +00001080}
1081
1082TEST_F(WiFiMainTest, NonSolitaryBSSRemoved) {
Gary Moraind9f893f2012-02-06 10:03:40 -08001083 EXPECT_CALL(*manager(), RegisterService(_));
1084 EXPECT_CALL(*manager(), HasService(_));
1085 EXPECT_CALL(*manager(), UpdateService(_));
mukesh agrawal261daca2011-12-02 18:56:56 +00001086 StartWiFi();
Thieu Lee41a72d2012-02-06 20:46:51 +00001087 ReportBSS("bss0", "ssid", "00:00:00:00:00:00", 0, 0, kNetworkModeAdHoc);
1088 ReportBSS("bss1", "ssid", "00:00:00:00:00:00", 0, 0, kNetworkModeAdHoc);
mukesh agrawal261daca2011-12-02 18:56:56 +00001089 ReportScanDone();
1090 EXPECT_EQ(1, GetServices().size());
1091 EXPECT_TRUE(GetServices().front()->IsVisible());
1092
1093 EXPECT_CALL(*manager(), UpdateService(_));
1094 RemoveBSS("bss0");
1095 EXPECT_TRUE(GetServices().front()->IsVisible());
1096 EXPECT_EQ(1, GetServices().size());
1097}
1098
mukesh agrawal31950242011-07-14 11:53:38 -07001099TEST_F(WiFiMainTest, Connect) {
Gary Moraind9f893f2012-02-06 10:03:40 -08001100 EXPECT_CALL(*manager(), RegisterService(_)).Times(AnyNumber());
mukesh agrawaldc42bb32011-07-28 10:40:26 -07001101 MockSupplicantInterfaceProxy &supplicant_interface_proxy =
1102 *supplicant_interface_proxy_;
1103
mukesh agrawal31950242011-07-14 11:53:38 -07001104 StartWiFi();
Thieu Lee41a72d2012-02-06 20:46:51 +00001105 ReportBSS("bss0", "ssid0", "00:00:00:00:00:00", 0, 0, kNetworkModeAdHoc);
mukesh agrawal31950242011-07-14 11:53:38 -07001106 ReportScanDone();
1107
1108 {
1109 InSequence s;
1110 DBus::Path fake_path("/fake/path");
mukesh agrawal165e6142011-11-22 02:22:56 +00001111 WiFiService *service(GetServices().begin()->get());
mukesh agrawal31950242011-07-14 11:53:38 -07001112
mukesh agrawaldc42bb32011-07-28 10:40:26 -07001113 EXPECT_CALL(supplicant_interface_proxy, AddNetwork(_))
mukesh agrawal31950242011-07-14 11:53:38 -07001114 .WillOnce(Return(fake_path));
mukesh agrawaldc42bb32011-07-28 10:40:26 -07001115 EXPECT_CALL(supplicant_interface_proxy, SelectNetwork(fake_path));
mukesh agrawal32399322011-09-01 10:53:43 -07001116 InitiateConnect(service);
mukesh agrawalf2f68a52011-09-01 12:15:48 -07001117 EXPECT_EQ(static_cast<Service *>(service),
1118 wifi()->selected_service_.get());
Paul Stewart2b05e622012-07-13 20:38:44 -07001119 EXPECT_EQ(Service::kStateAssociating, service->state());
1120 EXPECT_FALSE(GetPendingTimeout().IsCancelled());
mukesh agrawal31950242011-07-14 11:53:38 -07001121 }
1122}
1123
mukesh agrawal0ed0f2e2011-12-05 20:36:17 +00001124TEST_F(WiFiMainTest, DisconnectPendingService) {
Gary Moraind9f893f2012-02-06 10:03:40 -08001125 EXPECT_CALL(*manager(), RegisterService(_)).Times(AnyNumber());
mukesh agrawal0ed0f2e2011-12-05 20:36:17 +00001126 MockSupplicantInterfaceProxy &supplicant_interface_proxy =
1127 *supplicant_interface_proxy_;
1128
1129 StartWiFi();
Thieu Lee41a72d2012-02-06 20:46:51 +00001130 ReportBSS("bss0", "ssid0", "00:00:00:00:00:00", 0, 0, kNetworkModeAdHoc);
mukesh agrawal0ed0f2e2011-12-05 20:36:17 +00001131 WiFiService *service(GetServices().begin()->get());
1132 InitiateConnect(service);
1133
1134 EXPECT_FALSE(GetPendingService() == NULL);
Paul Stewartff96a842012-08-13 15:59:10 -07001135 EXPECT_TRUE(service->IsConnecting());
mukesh agrawal0ed0f2e2011-12-05 20:36:17 +00001136 EXPECT_CALL(supplicant_interface_proxy, Disconnect());
1137 InitiateDisconnect(service);
Paul Stewartff96a842012-08-13 15:59:10 -07001138 EXPECT_FALSE(service->IsConnecting());
mukesh agrawal0ed0f2e2011-12-05 20:36:17 +00001139
1140 EXPECT_TRUE(GetPendingService() == NULL);
1141}
1142
1143TEST_F(WiFiMainTest, DisconnectPendingServiceWithCurrent) {
Gary Moraind9f893f2012-02-06 10:03:40 -08001144 EXPECT_CALL(*manager(), RegisterService(_)).Times(AnyNumber());
Paul Stewartd408fdf2012-05-07 17:15:57 -07001145 EXPECT_CALL(*dhcp_provider(), CreateConfig(_, _, _, _)).Times(AnyNumber());
Gary Moraind9f893f2012-02-06 10:03:40 -08001146 EXPECT_CALL(*dhcp_config_.get(), RequestIP()).Times(AnyNumber());
1147 EXPECT_CALL(*manager(), UpdateService(_)).Times(AnyNumber());
mukesh agrawal0ed0f2e2011-12-05 20:36:17 +00001148 MockSupplicantInterfaceProxy &supplicant_interface_proxy =
1149 *supplicant_interface_proxy_;
1150
1151 StartWiFi();
Thieu Lee41a72d2012-02-06 20:46:51 +00001152 ReportBSS("bss0", "ssid0", "00:00:00:00:00:00", 0, 0, kNetworkModeAdHoc);
1153 ReportBSS("bss1", "ssid1", "00:00:00:00:00:01", 0, 0, kNetworkModeAdHoc);
mukesh agrawal0ed0f2e2011-12-05 20:36:17 +00001154 WiFiService *service0(GetServices()[0].get());
1155 WiFiService *service1(GetServices()[1].get());
1156
1157 InitiateConnect(service0);
1158 ReportCurrentBSSChanged("bss0");
1159 ReportStateChanged(wpa_supplicant::kInterfaceStateCompleted);
1160 InitiateConnect(service1);
1161
1162 EXPECT_EQ(service0, GetCurrentService());
1163 EXPECT_EQ(service1, GetPendingService());
1164 EXPECT_CALL(supplicant_interface_proxy, Disconnect());
1165 InitiateDisconnect(service1);
1166
1167 // |current_service_| will be unchanged until supplicant signals
1168 // that CurrentBSS has changed.
1169 EXPECT_EQ(service0, GetCurrentService());
1170 // |pending_service_| is updated immediately.
1171 EXPECT_TRUE(GetPendingService() == NULL);
Paul Stewart2b05e622012-07-13 20:38:44 -07001172 EXPECT_TRUE(GetPendingTimeout().IsCancelled());
mukesh agrawal0ed0f2e2011-12-05 20:36:17 +00001173}
1174
1175TEST_F(WiFiMainTest, DisconnectCurrentService) {
1176 MockSupplicantInterfaceProxy &supplicant_interface_proxy =
1177 *supplicant_interface_proxy_;
Paul Stewart549d44c2012-07-03 12:40:25 -07001178 WiFiService *service(SetupConnectedService(DBus::Path()));
mukesh agrawal0ed0f2e2011-12-05 20:36:17 +00001179 EXPECT_CALL(supplicant_interface_proxy, Disconnect());
1180 InitiateDisconnect(service);
1181
1182 // |current_service_| should not change until supplicant reports
1183 // a BSS change.
1184 EXPECT_EQ(service, GetCurrentService());
1185}
1186
Christopher Wileyc6184482012-10-24 15:31:56 -07001187TEST_F(WiFiMainTest, DisconnectCurrentServiceWithErrors) {
1188 MockSupplicantInterfaceProxy &supplicant_interface_proxy =
1189 *supplicant_interface_proxy_;
1190 WiFiService *service(SetupConnectedService(DBus::Path()));
1191 EXPECT_CALL(supplicant_interface_proxy, Disconnect())
1192 .WillOnce(InvokeWithoutArgs(this, (&WiFiMainTest::ThrowDBusError)));
1193 InitiateDisconnect(service);
1194
1195 // We may sometimes fail to disconnect via supplicant, and we patch up some
1196 // state when this happens.
1197 EXPECT_EQ(NULL, GetCurrentService().get());
1198 EXPECT_EQ(NULL, wifi()->selected_service().get());
1199}
1200
mukesh agrawal0ed0f2e2011-12-05 20:36:17 +00001201TEST_F(WiFiMainTest, DisconnectCurrentServiceWithPending) {
Gary Moraind9f893f2012-02-06 10:03:40 -08001202 EXPECT_CALL(*manager(), RegisterService(_)).Times(AnyNumber());
Paul Stewartd408fdf2012-05-07 17:15:57 -07001203 EXPECT_CALL(*dhcp_provider(), CreateConfig(_, _, _, _)).Times(AnyNumber());
Gary Moraind9f893f2012-02-06 10:03:40 -08001204 EXPECT_CALL(*dhcp_config_.get(), RequestIP()).Times(AnyNumber());
1205 EXPECT_CALL(*manager(), UpdateService(_)).Times(AnyNumber());
mukesh agrawal0ed0f2e2011-12-05 20:36:17 +00001206 MockSupplicantInterfaceProxy &supplicant_interface_proxy =
1207 *supplicant_interface_proxy_;
1208
1209 StartWiFi();
Thieu Lee41a72d2012-02-06 20:46:51 +00001210 ReportBSS("bss0", "ssid0", "00:00:00:00:00:00", 0, 0, kNetworkModeAdHoc);
1211 ReportBSS("bss1", "ssid1", "00:00:00:00:00:01", 0, 0, kNetworkModeAdHoc);
mukesh agrawal0ed0f2e2011-12-05 20:36:17 +00001212 WiFiService *service0(GetServices()[0].get());
1213 WiFiService *service1(GetServices()[1].get());
1214
1215 InitiateConnect(service0);
1216 ReportCurrentBSSChanged("bss0");
1217 ReportStateChanged(wpa_supplicant::kInterfaceStateCompleted);
1218 InitiateConnect(service1);
1219
1220 EXPECT_EQ(service0, GetCurrentService());
1221 EXPECT_EQ(service1, GetPendingService());
1222 EXPECT_CALL(supplicant_interface_proxy, Disconnect())
1223 .Times(0);
1224 InitiateDisconnect(service0);
1225
1226 EXPECT_EQ(service0, GetCurrentService());
1227 EXPECT_EQ(service1, GetPendingService());
Paul Stewart2b05e622012-07-13 20:38:44 -07001228 EXPECT_FALSE(GetPendingTimeout().IsCancelled());
1229}
1230
1231TEST_F(WiFiMainTest, TimeoutPendingService) {
1232 StartWiFi();
1233 dispatcher_.DispatchPendingEvents();
1234 const base::CancelableClosure &pending_timeout = GetPendingTimeout();
1235 EXPECT_TRUE(pending_timeout.IsCancelled());
1236
1237 InSequence seq;
1238 MockWiFiServiceRefPtr service = MakeMockService(flimflam::kSecurityNone);
1239 EXPECT_CALL(*service, SetState(Service::kStateAssociating));
1240 InitiateConnect(service);
1241 EXPECT_FALSE(pending_timeout.IsCancelled());
1242 EXPECT_EQ(service, GetPendingService());
1243
Paul Stewartff96a842012-08-13 15:59:10 -07001244 {
1245 InSequence seq;
1246 EXPECT_CALL(*service, SetState(Service::kStateIdle));
1247 EXPECT_CALL(*service, SetFailure(Service::kFailureOutOfRange));
1248 pending_timeout.callback().Run();
1249 }
Paul Stewart2b05e622012-07-13 20:38:44 -07001250 EXPECT_EQ(NULL, GetPendingService().get());
1251
1252 // Verify expectations now, because WiFi may report other state changes
1253 // when WiFi is Stop()-ed (during TearDown()).
1254 Mock::VerifyAndClearExpectations(service.get());
mukesh agrawal0ed0f2e2011-12-05 20:36:17 +00001255}
1256
1257TEST_F(WiFiMainTest, DisconnectInvalidService) {
Gary Moraind9f893f2012-02-06 10:03:40 -08001258 EXPECT_CALL(*manager(), RegisterService(_)).Times(AnyNumber());
mukesh agrawal0ed0f2e2011-12-05 20:36:17 +00001259 MockSupplicantInterfaceProxy &supplicant_interface_proxy =
1260 *supplicant_interface_proxy_;
1261
1262 StartWiFi();
Thieu Lee41a72d2012-02-06 20:46:51 +00001263 ReportBSS("bss0", "ssid0", "00:00:00:00:00:00", 0, 0, kNetworkModeAdHoc);
mukesh agrawal0ed0f2e2011-12-05 20:36:17 +00001264 WiFiService *service(GetServices().begin()->get());
1265 EXPECT_CALL(supplicant_interface_proxy, Disconnect())
1266 .Times(0);
1267 InitiateDisconnect(service);
1268}
1269
1270TEST_F(WiFiMainTest, DisconnectCurrentServiceFailure) {
1271 MockSupplicantInterfaceProxy &supplicant_interface_proxy =
1272 *supplicant_interface_proxy_;
mukesh agrawal0ed0f2e2011-12-05 20:36:17 +00001273 DBus::Path fake_path("/fake/path");
Paul Stewart549d44c2012-07-03 12:40:25 -07001274 WiFiService *service(SetupConnectedService(fake_path));
mukesh agrawal0ed0f2e2011-12-05 20:36:17 +00001275 EXPECT_CALL(supplicant_interface_proxy, Disconnect())
1276 .WillRepeatedly(Throw(
1277 DBus::Error(
1278 "fi.w1.wpa_supplicant1.NotConnected",
1279 "test threw fi.w1.wpa_supplicant1.NotConnected")));
1280 EXPECT_CALL(supplicant_interface_proxy, RemoveNetwork(fake_path));
1281 InitiateDisconnect(service);
1282
1283 EXPECT_TRUE(GetCurrentService() == NULL);
1284}
1285
mukesh agrawal5c4dd0b2011-09-14 13:53:14 -07001286TEST_F(WiFiMainTest, Stop) {
Gary Moraind9f893f2012-02-06 10:03:40 -08001287 EXPECT_CALL(*manager(), RegisterService(_)).Times(AnyNumber());
mukesh agrawalc01f3982012-01-24 13:48:39 -08001288 StartWiFi();
Thieu Lee41a72d2012-02-06 20:46:51 +00001289 ReportBSS("bss0", "ssid0", "00:00:00:00:00:00", 0, 0, kNetworkModeAdHoc);
mukesh agrawalc01f3982012-01-24 13:48:39 -08001290 ReportScanDone();
mukesh agrawal5c4dd0b2011-09-14 13:53:14 -07001291
mukesh agrawalc01f3982012-01-24 13:48:39 -08001292 EXPECT_CALL(*manager(), DeregisterService(_));
1293 StopWiFi();
mukesh agrawalb66c6462012-05-07 11:45:25 -07001294 EXPECT_TRUE(GetScanTimer().IsCancelled());
mukesh agrawalc4f368f2012-06-04 19:45:52 -07001295 EXPECT_FALSE(wifi()->weak_ptr_factory_.HasWeakPtrs());
mukesh agrawal5c4dd0b2011-09-14 13:53:14 -07001296}
1297
Paul Stewart549d44c2012-07-03 12:40:25 -07001298TEST_F(WiFiMainTest, StopWhileConnected) {
1299 MockSupplicantInterfaceProxy &supplicant_interface_proxy =
1300 *supplicant_interface_proxy_;
1301 WiFiService *service(SetupConnectedService(DBus::Path()));
1302 EXPECT_CALL(*manager(), DeregisterService(ServiceRefPtr(service)))
1303 .WillOnce(Invoke(this, &WiFiObjectTest::UnloadService));
1304 EXPECT_CALL(supplicant_interface_proxy, Disconnect());
1305 StopWiFi();
1306 EXPECT_TRUE(GetCurrentService() == NULL);
1307}
1308
Paul Stewart44663922012-07-30 11:03:03 -07001309TEST_F(WiFiMainTest, ReconnectTimer) {
1310 MockSupplicantInterfaceProxy &supplicant_interface_proxy =
1311 *supplicant_interface_proxy_;
1312 WiFiService *service(SetupConnectedService(DBus::Path()));
1313 service->SetState(Service::kStateConnected);
Paul Stewart1aff7302012-08-04 20:04:47 -07001314 EXPECT_TRUE(GetReconnectTimeoutCallback().IsCancelled());
Paul Stewart44663922012-07-30 11:03:03 -07001315 ReportStateChanged(wpa_supplicant::kInterfaceStateDisconnected);
Paul Stewart1aff7302012-08-04 20:04:47 -07001316 EXPECT_FALSE(GetReconnectTimeoutCallback().IsCancelled());
Paul Stewart44663922012-07-30 11:03:03 -07001317 ReportStateChanged(wpa_supplicant::kInterfaceStateCompleted);
Paul Stewart1aff7302012-08-04 20:04:47 -07001318 EXPECT_TRUE(GetReconnectTimeoutCallback().IsCancelled());
Paul Stewart44663922012-07-30 11:03:03 -07001319 ReportStateChanged(wpa_supplicant::kInterfaceStateDisconnected);
Paul Stewart1aff7302012-08-04 20:04:47 -07001320 EXPECT_FALSE(GetReconnectTimeoutCallback().IsCancelled());
Paul Stewart44663922012-07-30 11:03:03 -07001321 ReportCurrentBSSChanged(kBSSName);
Paul Stewart1aff7302012-08-04 20:04:47 -07001322 EXPECT_TRUE(GetReconnectTimeoutCallback().IsCancelled());
Paul Stewart44663922012-07-30 11:03:03 -07001323 ReportStateChanged(wpa_supplicant::kInterfaceStateDisconnected);
Paul Stewart1aff7302012-08-04 20:04:47 -07001324 EXPECT_FALSE(GetReconnectTimeoutCallback().IsCancelled());
Paul Stewart44663922012-07-30 11:03:03 -07001325 EXPECT_CALL(supplicant_interface_proxy, Disconnect());
Paul Stewart1aff7302012-08-04 20:04:47 -07001326 GetReconnectTimeoutCallback().callback().Run();
Paul Stewart44663922012-07-30 11:03:03 -07001327 Mock::VerifyAndClearExpectations(&supplicant_interface_proxy_);
Paul Stewart1aff7302012-08-04 20:04:47 -07001328 EXPECT_TRUE(GetReconnectTimeoutCallback().IsCancelled());
Paul Stewart44663922012-07-30 11:03:03 -07001329}
1330
mukesh agrawal7a4e4002011-09-06 11:26:05 -07001331TEST_F(WiFiMainTest, GetWifiServiceOpen) {
1332 Error e;
Paul Stewarta41e38d2011-11-11 07:47:29 -08001333 GetOpenService(flimflam::kTypeWifi, "an_ssid", flimflam::kModeManaged, &e);
mukesh agrawal7a4e4002011-09-06 11:26:05 -07001334 EXPECT_TRUE(e.IsSuccess());
1335}
1336
mukesh agrawal7a4e4002011-09-06 11:26:05 -07001337TEST_F(WiFiMainTest, GetWifiServiceOpenNoSSID) {
1338 Error e;
Paul Stewarta41e38d2011-11-11 07:47:29 -08001339 GetOpenService(flimflam::kTypeWifi, NULL, flimflam::kModeManaged, &e);
mukesh agrawal7a4e4002011-09-06 11:26:05 -07001340 EXPECT_EQ(Error::kInvalidArguments, e.type());
1341 EXPECT_EQ("must specify SSID", e.message());
1342}
1343
1344TEST_F(WiFiMainTest, GetWifiServiceOpenLongSSID) {
1345 Error e;
1346 GetOpenService(
Paul Stewarta41e38d2011-11-11 07:47:29 -08001347 flimflam::kTypeWifi, "123456789012345678901234567890123",
1348 flimflam::kModeManaged, &e);
mukesh agrawal7a4e4002011-09-06 11:26:05 -07001349 EXPECT_EQ(Error::kInvalidNetworkName, e.type());
1350 EXPECT_EQ("SSID is too long", e.message());
1351}
1352
1353TEST_F(WiFiMainTest, GetWifiServiceOpenShortSSID) {
1354 Error e;
Paul Stewarta41e38d2011-11-11 07:47:29 -08001355 GetOpenService(flimflam::kTypeWifi, "", flimflam::kModeManaged, &e);
mukesh agrawal7a4e4002011-09-06 11:26:05 -07001356 EXPECT_EQ(Error::kInvalidNetworkName, e.type());
1357 EXPECT_EQ("SSID is too short", e.message());
1358}
1359
1360TEST_F(WiFiMainTest, GetWifiServiceOpenBadMode) {
1361 Error e;
Paul Stewarta41e38d2011-11-11 07:47:29 -08001362 GetOpenService(flimflam::kTypeWifi, "an_ssid", "ad-hoc", &e);
mukesh agrawal7a4e4002011-09-06 11:26:05 -07001363 EXPECT_EQ(Error::kNotSupported, e.type());
1364 EXPECT_EQ("service mode is unsupported", e.message());
1365}
1366
1367TEST_F(WiFiMainTest, GetWifiServiceOpenNoMode) {
1368 Error e;
Paul Stewarta41e38d2011-11-11 07:47:29 -08001369 GetOpenService(flimflam::kTypeWifi, "an_ssid", NULL, &e);
mukesh agrawal7a4e4002011-09-06 11:26:05 -07001370 EXPECT_TRUE(e.IsSuccess());
1371}
1372
1373TEST_F(WiFiMainTest, GetWifiServiceRSN) {
1374 Error e;
Paul Stewarta41e38d2011-11-11 07:47:29 -08001375 GetService(flimflam::kTypeWifi, "an_ssid", flimflam::kModeManaged,
1376 flimflam::kSecurityRsn, "secure password", &e);
mukesh agrawal7a4e4002011-09-06 11:26:05 -07001377 EXPECT_TRUE(e.IsSuccess());
1378}
1379
1380TEST_F(WiFiMainTest, GetWifiServiceRSNNoPassword) {
mukesh agrawalb172e4a2012-04-25 18:15:30 -07001381 // When configuring hidden networks, Chrome expects to be able to
1382 // GetService w/o a password, and supply the password with
1383 // SetProperty afterwards.
mukesh agrawal7a4e4002011-09-06 11:26:05 -07001384 Error e;
Paul Stewarta41e38d2011-11-11 07:47:29 -08001385 GetService(flimflam::kTypeWifi, "an_ssid", flimflam::kModeManaged,
1386 flimflam::kSecurityRsn, NULL, &e);
mukesh agrawalb172e4a2012-04-25 18:15:30 -07001387 EXPECT_TRUE(e.IsSuccess());
mukesh agrawal7a4e4002011-09-06 11:26:05 -07001388}
1389
1390TEST_F(WiFiMainTest, GetWifiServiceBadSecurity) {
1391 Error e;
Paul Stewarta41e38d2011-11-11 07:47:29 -08001392 GetService(flimflam::kTypeWifi, "an_ssid", flimflam::kModeManaged, "rot-13",
1393 NULL, &e);
mukesh agrawal7a4e4002011-09-06 11:26:05 -07001394 EXPECT_EQ(Error::kNotSupported, e.type());
1395 EXPECT_EQ("security mode is unsupported", e.message());
1396}
1397
1398TEST_F(WiFiMainTest, GetWifiServiceWEPNoPassword) {
mukesh agrawalb172e4a2012-04-25 18:15:30 -07001399 // When configuring hidden networks, Chrome expects to be able to
1400 // GetService w/o a password, and supply the password with
1401 // SetProperty afterwards.
mukesh agrawal7a4e4002011-09-06 11:26:05 -07001402 Error e;
Paul Stewarta41e38d2011-11-11 07:47:29 -08001403 GetService(flimflam::kTypeWifi, "an_ssid", flimflam::kModeManaged,
1404 flimflam::kSecurityWep, NULL, &e);
mukesh agrawalb172e4a2012-04-25 18:15:30 -07001405 EXPECT_TRUE(e.IsSuccess());
mukesh agrawal7a4e4002011-09-06 11:26:05 -07001406}
1407
1408TEST_F(WiFiMainTest, GetWifiServiceWEPEmptyPassword) {
1409 Error e;
Paul Stewarta41e38d2011-11-11 07:47:29 -08001410 GetService(flimflam::kTypeWifi, "an_ssid", flimflam::kModeManaged,
1411 flimflam::kSecurityWep, "", &e);
mukesh agrawal7a4e4002011-09-06 11:26:05 -07001412 EXPECT_EQ(Error::kInvalidPassphrase, e.type());
1413}
1414
1415TEST_F(WiFiMainTest, GetWifiServiceWEP40ASCII) {
1416 Error e;
Paul Stewarta41e38d2011-11-11 07:47:29 -08001417 GetService(flimflam::kTypeWifi, "an_ssid", flimflam::kModeManaged,
1418 flimflam::kSecurityWep, "abcde", &e);
mukesh agrawal7a4e4002011-09-06 11:26:05 -07001419 EXPECT_TRUE(e.IsSuccess());
1420}
1421
1422TEST_F(WiFiMainTest, GetWifiServiceWEP104ASCII) {
1423 Error e;
Paul Stewarta41e38d2011-11-11 07:47:29 -08001424 GetService(flimflam::kTypeWifi, "an_ssid", flimflam::kModeManaged,
1425 flimflam::kSecurityWep, "abcdefghijklm", &e);
mukesh agrawal7a4e4002011-09-06 11:26:05 -07001426 EXPECT_TRUE(e.IsSuccess());
1427}
1428
1429TEST_F(WiFiMainTest, GetWifiServiceWEP40ASCIIWithKeyIndex) {
1430 Error e;
Paul Stewarta41e38d2011-11-11 07:47:29 -08001431 GetService(flimflam::kTypeWifi, "an_ssid", flimflam::kModeManaged,
1432 flimflam::kSecurityWep, "0:abcdefghijklm", &e);
mukesh agrawal7a4e4002011-09-06 11:26:05 -07001433 EXPECT_TRUE(e.IsSuccess());
1434}
1435
1436TEST_F(WiFiMainTest, GetWifiServiceWEP40Hex) {
1437 Error e;
Paul Stewarta41e38d2011-11-11 07:47:29 -08001438 GetService(flimflam::kTypeWifi, "an_ssid", flimflam::kModeManaged,
1439 flimflam::kSecurityWep, "0102030405", &e);
mukesh agrawal7a4e4002011-09-06 11:26:05 -07001440 EXPECT_TRUE(e.IsSuccess());
1441}
1442
1443TEST_F(WiFiMainTest, GetWifiServiceWEP40HexBadPassphrase) {
1444 Error e;
Paul Stewarta41e38d2011-11-11 07:47:29 -08001445 GetService(flimflam::kTypeWifi, "an_ssid", flimflam::kModeManaged,
1446 flimflam::kSecurityWep, "O102030405", &e);
mukesh agrawal7a4e4002011-09-06 11:26:05 -07001447 EXPECT_EQ(Error::kInvalidPassphrase, e.type());
1448}
1449
1450TEST_F(WiFiMainTest, GetWifiServiceWEP40HexWithKeyIndexBadPassphrase) {
1451 Error e;
Paul Stewarta41e38d2011-11-11 07:47:29 -08001452 GetService(flimflam::kTypeWifi, "an_ssid", flimflam::kModeManaged,
1453 flimflam::kSecurityWep, "1:O102030405", &e);
mukesh agrawal7a4e4002011-09-06 11:26:05 -07001454 EXPECT_EQ(Error::kInvalidPassphrase, e.type());
1455}
1456
1457TEST_F(WiFiMainTest, GetWifiServiceWEP40HexWithKeyIndexAndBaseBadPassphrase) {
1458 Error e;
Paul Stewarta41e38d2011-11-11 07:47:29 -08001459 GetService(flimflam::kTypeWifi, "an_ssid", flimflam::kModeManaged,
1460 flimflam::kSecurityWep, "1:0xO102030405", &e);
mukesh agrawal7a4e4002011-09-06 11:26:05 -07001461 EXPECT_EQ(Error::kInvalidPassphrase, e.type());
1462}
1463
1464TEST_F(WiFiMainTest, GetWifiServiceWEP40HexWithBaseBadPassphrase) {
1465 Error e;
Paul Stewarta41e38d2011-11-11 07:47:29 -08001466 GetService(flimflam::kTypeWifi, "an_ssid", flimflam::kModeManaged,
1467 flimflam::kSecurityWep, "0xO102030405", &e);
mukesh agrawal7a4e4002011-09-06 11:26:05 -07001468 EXPECT_EQ(Error::kInvalidPassphrase, e.type());
1469}
1470
1471TEST_F(WiFiMainTest, GetWifiServiceWEP104Hex) {
1472 Error e;
Paul Stewarta41e38d2011-11-11 07:47:29 -08001473 GetService(flimflam::kTypeWifi, "an_ssid", flimflam::kModeManaged,
1474 flimflam::kSecurityWep, "0102030405060708090a0b0c0d", &e);
mukesh agrawal7a4e4002011-09-06 11:26:05 -07001475 EXPECT_TRUE(e.IsSuccess());
1476}
1477
1478TEST_F(WiFiMainTest, GetWifiServiceWEP104HexUppercase) {
1479 Error e;
Paul Stewarta41e38d2011-11-11 07:47:29 -08001480 GetService(flimflam::kTypeWifi, "an_ssid", flimflam::kModeManaged,
1481 flimflam::kSecurityWep, "0102030405060708090A0B0C0D", &e);
mukesh agrawal7a4e4002011-09-06 11:26:05 -07001482 EXPECT_TRUE(e.IsSuccess());
1483}
1484
1485TEST_F(WiFiMainTest, GetWifiServiceWEP104HexWithKeyIndex) {
1486 Error e;
Paul Stewarta41e38d2011-11-11 07:47:29 -08001487 GetService(flimflam::kTypeWifi, "an_ssid", flimflam::kModeManaged,
1488 flimflam::kSecurityWep, "0:0102030405060708090a0b0c0d", &e);
mukesh agrawal7a4e4002011-09-06 11:26:05 -07001489 EXPECT_TRUE(e.IsSuccess());
1490}
1491
1492TEST_F(WiFiMainTest, GetWifiServiceWEP104HexWithKeyIndexAndBase) {
1493 Error e;
Paul Stewarta41e38d2011-11-11 07:47:29 -08001494 GetService(flimflam::kTypeWifi, "an_ssid", flimflam::kModeManaged,
1495 flimflam::kSecurityWep, "0:0x0102030405060708090a0b0c0d", &e);
mukesh agrawal7a4e4002011-09-06 11:26:05 -07001496 EXPECT_TRUE(e.IsSuccess());
1497}
1498
mukesh agrawal8ede0522011-10-03 14:57:44 -07001499class WiFiGetServiceSuccessTest : public WiFiMainTest {};
1500class WiFiGetServiceFailureTest : public WiFiMainTest {};
1501
1502TEST_P(WiFiGetServiceSuccessTest, Passphrase) {
1503 Error e;
Paul Stewarta41e38d2011-11-11 07:47:29 -08001504 GetService(flimflam::kTypeWifi, "an_ssid", flimflam::kModeManaged,
1505 flimflam::kSecurityWpa, GetParam().c_str(), &e);
mukesh agrawal8ede0522011-10-03 14:57:44 -07001506 EXPECT_TRUE(e.IsSuccess());
1507}
1508
1509TEST_P(WiFiGetServiceFailureTest, Passphrase) {
1510 Error e;
Paul Stewarta41e38d2011-11-11 07:47:29 -08001511 GetService(flimflam::kTypeWifi, "an_ssid", flimflam::kModeManaged,
1512 flimflam::kSecurityWpa, GetParam().c_str(), &e);
mukesh agrawal8ede0522011-10-03 14:57:44 -07001513 EXPECT_EQ(Error::kInvalidPassphrase, e.type());
1514}
1515
1516INSTANTIATE_TEST_CASE_P(
1517 WiFiGetServiceSuccessTestInstance,
1518 WiFiGetServiceSuccessTest,
1519 Values(
1520 string(IEEE_80211::kWPAAsciiMinLen, 'Z'),
1521 string(IEEE_80211::kWPAAsciiMaxLen, 'Z'),
1522 // subtle: invalid length for hex key, but valid as ascii passphrase
1523 string(IEEE_80211::kWPAHexLen-1, '1'),
1524 string(IEEE_80211::kWPAHexLen, '1')));
1525
1526INSTANTIATE_TEST_CASE_P(
1527 WiFiGetServiceFailureTestInstance,
1528 WiFiGetServiceFailureTest,
1529 Values(
1530 string(IEEE_80211::kWPAAsciiMinLen-1, 'Z'),
1531 string(IEEE_80211::kWPAAsciiMaxLen+1, 'Z'),
1532 string(IEEE_80211::kWPAHexLen+1, '1')));
1533
Paul Stewart6ab23a92011-11-09 17:17:47 -08001534TEST_F(WiFiMainTest, FindServiceWEP) {
1535 const string ssid("an_ssid");
1536 {
1537 Error e;
Paul Stewarta41e38d2011-11-11 07:47:29 -08001538 GetService(flimflam::kTypeWifi, ssid.c_str(), flimflam::kModeManaged,
Paul Stewart6ab23a92011-11-09 17:17:47 -08001539 flimflam::kSecurityWep, "abcde", &e);
1540 EXPECT_TRUE(e.IsSuccess());
1541 }
1542 vector<uint8_t> ssid_bytes(ssid.begin(), ssid.end());
1543
Paul Stewarta41e38d2011-11-11 07:47:29 -08001544 EXPECT_TRUE(FindService(ssid_bytes, flimflam::kModeManaged,
1545 flimflam::kSecurityWep).get());
1546 EXPECT_FALSE(FindService(ssid_bytes, flimflam::kModeManaged,
1547 flimflam::kSecurityWpa).get());
Paul Stewart6ab23a92011-11-09 17:17:47 -08001548}
1549
1550TEST_F(WiFiMainTest, FindServiceWPA) {
1551 const string ssid("an_ssid");
1552 {
1553 Error e;
Paul Stewarta41e38d2011-11-11 07:47:29 -08001554 GetService(flimflam::kTypeWifi, ssid.c_str(), flimflam::kModeManaged,
Paul Stewart6ab23a92011-11-09 17:17:47 -08001555 flimflam::kSecurityRsn, "abcdefgh", &e);
1556 EXPECT_TRUE(e.IsSuccess());
1557 }
1558 vector<uint8_t> ssid_bytes(ssid.begin(), ssid.end());
Paul Stewarta41e38d2011-11-11 07:47:29 -08001559 WiFiServiceRefPtr wpa_service(FindService(ssid_bytes, flimflam::kModeManaged,
1560 flimflam::kSecurityWpa));
Paul Stewart6ab23a92011-11-09 17:17:47 -08001561 EXPECT_TRUE(wpa_service.get());
Paul Stewarta41e38d2011-11-11 07:47:29 -08001562 WiFiServiceRefPtr rsn_service(FindService(ssid_bytes, flimflam::kModeManaged,
1563 flimflam::kSecurityRsn));
Paul Stewart6ab23a92011-11-09 17:17:47 -08001564 EXPECT_TRUE(rsn_service.get());
1565 EXPECT_EQ(wpa_service.get(), rsn_service.get());
Paul Stewarta41e38d2011-11-11 07:47:29 -08001566 WiFiServiceRefPtr psk_service(FindService(ssid_bytes, flimflam::kModeManaged,
1567 flimflam::kSecurityPsk));
Paul Stewart6ab23a92011-11-09 17:17:47 -08001568 EXPECT_EQ(wpa_service.get(), psk_service.get());
1569 // Indirectly test FindService by doing a GetService on something that
1570 // already exists.
1571 {
1572 Error e;
1573 WiFiServiceRefPtr wpa_service2(
Paul Stewarta41e38d2011-11-11 07:47:29 -08001574 GetServiceInner(flimflam::kTypeWifi, ssid.c_str(),
1575 flimflam::kModeManaged, flimflam::kSecurityWpa,
1576 "abcdefgh", false, &e));
Paul Stewart6ab23a92011-11-09 17:17:47 -08001577 EXPECT_TRUE(e.IsSuccess());
1578 EXPECT_EQ(wpa_service.get(), wpa_service2.get());
1579 }
1580}
1581
Paul Stewartcb59fed2012-03-21 21:14:46 -07001582TEST_F(WiFiMainTest, GetServiceWithGUID) {
1583 // Perform a GetService that also configures properties in the base Service
1584 // class using Service::Configure().
1585 KeyValueStore args;
1586 args.SetString(flimflam::kTypeProperty, flimflam::kTypeWifi);
1587 args.SetString(flimflam::kSSIDProperty, "ssid");
1588 args.SetString(flimflam::kSecurityProperty, flimflam::kSecurityNone);
1589 const string kGUID = "aguid"; // Stored as a registered Service property.
1590 args.SetString(flimflam::kGuidProperty, kGUID);
1591 Error e;
1592 WiFiServiceRefPtr service = GetServiceWithKeyValues(args, &e);
1593 EXPECT_TRUE(e.IsSuccess());
1594 EXPECT_EQ(kGUID, service->guid());
1595}
1596
Paul Stewartced6a0b2011-11-08 15:32:04 -08001597MATCHER_P(HasHiddenSSID, ssid, "") {
Paul Stewarta41e38d2011-11-11 07:47:29 -08001598 map<string, DBus::Variant>::const_iterator it =
Paul Stewartced6a0b2011-11-08 15:32:04 -08001599 arg.find(wpa_supplicant::kPropertyScanSSIDs);
1600 if (it == arg.end()) {
1601 return false;
1602 }
1603
1604 const DBus::Variant &ssids_variant = it->second;
1605 EXPECT_TRUE(DBusAdaptor::IsByteArrays(ssids_variant.signature()));
1606 const ByteArrays &ssids = it->second.operator ByteArrays();
1607 // A valid Scan containing a single hidden SSID should contain
1608 // two SSID entries: one containing the SSID we are looking for,
1609 // and an empty entry, signifying that we also want to do a
1610 // broadcast probe request for all non-hidden APs as well.
1611 return ssids.size() == 2 &&
1612 string(ssids[0].begin(), ssids[0].end()) == ssid &&
1613 ssids[1].empty();
1614}
1615
1616TEST_F(WiFiMainTest, ScanHidden) {
1617 EXPECT_CALL(*supplicant_process_proxy_, CreateInterface(_));
1618 EXPECT_CALL(*supplicant_process_proxy_, GetInterface(_))
1619 .Times(AnyNumber())
1620 .WillRepeatedly(Throw(
1621 DBus::Error(
1622 "fi.w1.wpa_supplicant1.InterfaceUnknown",
1623 "test threw fi.w1.wpa_supplicant1.InterfaceUnknown")));
Paul Stewart10ccbb32012-04-26 15:59:30 -07001624 scoped_refptr<MockProfile> profile(
1625 new NiceMock<MockProfile>(control_interface(), manager(), ""));
Paul Stewarta41e38d2011-11-11 07:47:29 -08001626 {
Paul Stewart10ccbb32012-04-26 15:59:30 -07001627 // Create a hidden service with an associated profile.
Paul Stewarta41e38d2011-11-11 07:47:29 -08001628 Error e;
1629 WiFiServiceRefPtr service =
1630 GetServiceInner(flimflam::kTypeWifi, "ssid0", flimflam::kModeManaged,
1631 NULL, NULL, true, &e);
1632 EXPECT_TRUE(e.IsSuccess());
1633 EXPECT_TRUE(service->hidden_ssid());
Paul Stewart10ccbb32012-04-26 15:59:30 -07001634 service->set_profile(profile);
Paul Stewarta41e38d2011-11-11 07:47:29 -08001635 }
1636 {
Paul Stewart10ccbb32012-04-26 15:59:30 -07001637 // Create a hidden service without an associated profile.
Paul Stewarta41e38d2011-11-11 07:47:29 -08001638 Error e;
1639 WiFiServiceRefPtr service =
1640 GetServiceInner(flimflam::kTypeWifi, "ssid1", flimflam::kModeManaged,
1641 NULL, NULL, true, &e);
1642 EXPECT_TRUE(e.IsSuccess());
1643 EXPECT_TRUE(service->hidden_ssid());
1644 }
1645 {
Paul Stewart10ccbb32012-04-26 15:59:30 -07001646 // Create a non-hidden service with an associated profile.
Paul Stewarta41e38d2011-11-11 07:47:29 -08001647 Error e;
1648 WiFiServiceRefPtr service =
1649 GetServiceInner(flimflam::kTypeWifi, "ssid2", flimflam::kModeManaged,
1650 NULL, NULL, false, &e);
1651 EXPECT_TRUE(e.IsSuccess());
1652 EXPECT_FALSE(service->hidden_ssid());
Paul Stewart10ccbb32012-04-26 15:59:30 -07001653 service->set_profile(profile);
Paul Stewarta41e38d2011-11-11 07:47:29 -08001654 }
1655 EXPECT_CALL(*supplicant_interface_proxy_, Scan(HasHiddenSSID("ssid0")));
Paul Stewartced6a0b2011-11-08 15:32:04 -08001656 StartWiFi();
1657 dispatcher_.DispatchPendingEvents();
1658}
1659
Paul Stewartfae4dae2012-09-13 07:43:32 -07001660TEST_F(WiFiMainTest, ScanWiFiDisabledAfterResume) {
1661 ScopedMockLog log;
1662 EXPECT_CALL(log, Log(_, _, _)).Times(AnyNumber());
1663 EXPECT_CALL(log, Log(_, _, EndsWith(
1664 "Ignoring scan request while device is not enabled."))).Times(1);
1665 EXPECT_CALL(*supplicant_interface_proxy_, Scan(_)).Times(0);
1666 StartWiFi();
1667 StopWiFi();
1668 // A scan is queued when WiFi resumes.
1669 OnAfterResume();
1670 dispatcher_.DispatchPendingEvents();
1671}
1672
mukesh agrawal7ec71312011-11-10 02:08:26 +00001673TEST_F(WiFiMainTest, InitialSupplicantState) {
1674 EXPECT_EQ(WiFi::kInterfaceStateUnknown, GetSupplicantState());
1675}
1676
1677TEST_F(WiFiMainTest, StateChangeNoService) {
1678 // State change should succeed even if there is no pending Service.
1679 ReportStateChanged(wpa_supplicant::kInterfaceStateScanning);
1680 EXPECT_EQ(wpa_supplicant::kInterfaceStateScanning, GetSupplicantState());
1681}
1682
1683TEST_F(WiFiMainTest, StateChangeWithService) {
1684 // Forward transition should trigger a Service state change.
1685 StartWiFi();
1686 dispatcher_.DispatchPendingEvents();
mukesh agrawalcf24a242012-05-21 16:46:11 -07001687 MockWiFiServiceRefPtr service = MakeMockService(flimflam::kSecurityNone);
mukesh agrawal7ec71312011-11-10 02:08:26 +00001688 InitiateConnect(service);
1689 EXPECT_CALL(*service.get(), SetState(Service::kStateAssociating));
1690 ReportStateChanged(wpa_supplicant::kInterfaceStateAssociated);
1691 // Verify expectations now, because WiFi may report other state changes
1692 // when WiFi is Stop()-ed (during TearDown()).
1693 Mock::VerifyAndClearExpectations(service.get());
Gary Moraind9f893f2012-02-06 10:03:40 -08001694 EXPECT_CALL(*service.get(), SetState(_)).Times(AnyNumber());
mukesh agrawal7ec71312011-11-10 02:08:26 +00001695}
1696
1697TEST_F(WiFiMainTest, StateChangeBackwardsWithService) {
1698 // Some backwards transitions should not trigger a Service state change.
1699 // Supplicant state should still be updated, however.
Paul Stewartd408fdf2012-05-07 17:15:57 -07001700 EXPECT_CALL(*dhcp_provider(), CreateConfig(_, _, _, _)).Times(AnyNumber());
Gary Moraind9f893f2012-02-06 10:03:40 -08001701 EXPECT_CALL(*dhcp_config_.get(), RequestIP()).Times(AnyNumber());
mukesh agrawal7ec71312011-11-10 02:08:26 +00001702 StartWiFi();
1703 dispatcher_.DispatchPendingEvents();
mukesh agrawalcf24a242012-05-21 16:46:11 -07001704 MockWiFiServiceRefPtr service = MakeMockService(flimflam::kSecurityNone);
Paul Stewart2b05e622012-07-13 20:38:44 -07001705 EXPECT_CALL(*service.get(), SetState(Service::kStateAssociating));
Gary Moraind9f893f2012-02-06 10:03:40 -08001706 EXPECT_CALL(*service.get(), SetState(Service::kStateConfiguring));
mukesh agrawal7ec71312011-11-10 02:08:26 +00001707 InitiateConnect(service);
1708 ReportStateChanged(wpa_supplicant::kInterfaceStateCompleted);
mukesh agrawal7ec71312011-11-10 02:08:26 +00001709 ReportStateChanged(wpa_supplicant::kInterfaceStateAuthenticating);
1710 EXPECT_EQ(wpa_supplicant::kInterfaceStateAuthenticating,
1711 GetSupplicantState());
1712 // Verify expectations now, because WiFi may report other state changes
1713 // when WiFi is Stop()-ed (during TearDown()).
1714 Mock::VerifyAndClearExpectations(service.get());
Gary Moraind9f893f2012-02-06 10:03:40 -08001715 EXPECT_CALL(*service.get(), SetState(_)).Times(AnyNumber());
mukesh agrawal7ec71312011-11-10 02:08:26 +00001716}
1717
Paul Stewarta41e38d2011-11-11 07:47:29 -08001718TEST_F(WiFiMainTest, LoadHiddenServicesFailWithNoGroups) {
1719 StrictMock<MockStore> storage;
1720 EXPECT_CALL(storage, GetGroupsWithKey(flimflam::kWifiHiddenSsid))
1721 .WillOnce(Return(set<string>()));
1722 EXPECT_FALSE(LoadHiddenServices(&storage));
1723}
1724
1725TEST_F(WiFiMainTest, LoadHiddenServicesFailWithMissingHidden) {
1726 string id;
1727 StrictMock<MockStore> storage;
1728 SetupHiddenStorage(&storage, "an_ssid", &id);
1729 // Missing "Hidden" property.
1730 EXPECT_CALL(storage, GetBool(StrEq(id), flimflam::kWifiHiddenSsid, _))
1731 .WillOnce(Return(false));
1732 EXPECT_FALSE(LoadHiddenServices(&storage));
1733}
1734
1735TEST_F(WiFiMainTest, LoadHiddenServicesFailWithFalseHidden) {
1736 string id;
1737 StrictMock<MockStore> storage;
1738 SetupHiddenStorage(&storage, "an_ssid", &id);
1739 // "Hidden" property set to "false".
1740 EXPECT_CALL(storage, GetBool(StrEq(id), flimflam::kWifiHiddenSsid, _))
1741 .WillOnce(DoAll(SetArgumentPointee<2>(true), Return(false)));
1742 EXPECT_FALSE(LoadHiddenServices(&storage));
1743}
1744
1745TEST_F(WiFiMainTest, LoadHiddenServicesFailWithMissingSSID) {
1746 string id;
1747 StrictMock<MockStore> storage;
1748 SetupHiddenStorage(&storage, "an_ssid", &id);
1749 // Missing "SSID" property.
1750 EXPECT_CALL(storage, GetString(StrEq(id), flimflam::kSSIDProperty, _))
1751 .WillOnce(Return(false));
1752 EXPECT_FALSE(LoadHiddenServices(&storage));
1753}
1754
1755
1756TEST_F(WiFiMainTest, LoadHiddenServicesFailWithFoundService) {
1757 StrictMock<MockStore> storage;
1758 string id;
1759 SetupHiddenStorage(&storage, "an_ssid", &id);
1760 Error e;
1761 GetOpenService(flimflam::kTypeWifi, "an_ssid", NULL, &e);
1762 ASSERT_TRUE(e.IsSuccess());
1763 EXPECT_FALSE(LoadHiddenServices(&storage));
1764}
1765
1766TEST_F(WiFiMainTest, LoadHiddenServicesSuccess) {
Gary Moraind9f893f2012-02-06 10:03:40 -08001767 EXPECT_CALL(*manager(), RegisterService(_)).Times(AnyNumber());
Paul Stewarta41e38d2011-11-11 07:47:29 -08001768 StrictMock<MockStore> storage;
1769 string ssid("an_ssid");
1770 string id;
1771 SetupHiddenStorage(&storage, ssid, &id);
1772 EXPECT_TRUE(LoadHiddenServices(&storage));
1773 vector<uint8_t> ssid_bytes(ssid.begin(), ssid.end());
1774 EXPECT_TRUE(FindService(ssid_bytes, flimflam::kModeManaged,
1775 flimflam::kSecurityNone).get());
1776}
1777
mukesh agrawal15908392011-11-16 18:29:25 +00001778TEST_F(WiFiMainTest, CurrentBSSChangeConnectedToDisconnected) {
Gary Moraind9f893f2012-02-06 10:03:40 -08001779 EXPECT_CALL(*manager(), HasService(_)).Times(AnyNumber());
Paul Stewartd408fdf2012-05-07 17:15:57 -07001780 EXPECT_CALL(*dhcp_provider(), CreateConfig(_, _, _, _)).Times(AnyNumber());
Gary Moraind9f893f2012-02-06 10:03:40 -08001781 EXPECT_CALL(*dhcp_config_.get(), RequestIP()).Times(AnyNumber());
1782 EXPECT_CALL(*manager(), UpdateService(_)).Times(AnyNumber());
mukesh agrawal15908392011-11-16 18:29:25 +00001783 WiFiEndpointRefPtr ap = MakeEndpoint("an_ssid", "00:01:02:03:04:05");
1784 WiFiServiceRefPtr service = CreateServiceForEndpoint(*ap);
1785
1786 // Note that the BSS handle used in this test ("an_ap") is not
1787 // intended to reflect the format used by supplicant. It's just
1788 // convenient for testing.
1789
1790 StartWiFi();
Thieu Lee41a72d2012-02-06 20:46:51 +00001791 ReportBSS("an_ap", ap->ssid_string(), ap->bssid_string(), 0, 0,
mukesh agrawal15908392011-11-16 18:29:25 +00001792 kNetworkModeInfrastructure);
1793 InitiateConnect(service);
1794 EXPECT_EQ(service, GetPendingService().get());
1795
1796 ReportCurrentBSSChanged("an_ap");
1797 ReportStateChanged(wpa_supplicant::kInterfaceStateCompleted);
1798 EXPECT_EQ(Service::kStateConfiguring, service->state());
1799 EXPECT_EQ(service, GetCurrentService().get());
1800 EXPECT_EQ(NULL, GetPendingService().get());
1801
1802 ReportCurrentBSSChanged(wpa_supplicant::kCurrentBSSNull);
mukesh agrawal568b5c62012-02-28 14:44:47 -08001803 EXPECT_EQ(Service::kStateIdle, service->state());
1804 EXPECT_TRUE(service->IsFailed());
mukesh agrawal15908392011-11-16 18:29:25 +00001805 EXPECT_EQ(NULL, GetCurrentService().get());
1806 EXPECT_EQ(NULL, GetPendingService().get());
1807}
1808
1809TEST_F(WiFiMainTest, CurrentBSSChangeConnectedToConnectedNewService) {
Gary Moraind9f893f2012-02-06 10:03:40 -08001810 EXPECT_CALL(*manager(), HasService(_)).Times(AnyNumber());
Paul Stewartd408fdf2012-05-07 17:15:57 -07001811 EXPECT_CALL(*dhcp_provider(), CreateConfig(_, _, _, _)).Times(AnyNumber());
Gary Moraind9f893f2012-02-06 10:03:40 -08001812 EXPECT_CALL(*dhcp_config_.get(), RequestIP()).Times(AnyNumber());
1813 EXPECT_CALL(*manager(), UpdateService(_)).Times(AnyNumber());
mukesh agrawal15908392011-11-16 18:29:25 +00001814 WiFiEndpointRefPtr ap1 = MakeEndpoint("an_ssid", "00:01:02:03:04:05");
1815 WiFiEndpointRefPtr ap2 = MakeEndpoint("another_ssid", "01:02:03:04:05:06");
1816 WiFiServiceRefPtr service1 = CreateServiceForEndpoint(*ap1);
1817 WiFiServiceRefPtr service2 = CreateServiceForEndpoint(*ap2);
1818
1819 // Note that the BSS handles used in this test ("ap1", "ap2") are
1820 // not intended to reflect the format used by supplicant. They're
1821 // just convenient for testing.
1822
1823 StartWiFi();
Thieu Lee41a72d2012-02-06 20:46:51 +00001824 ReportBSS("ap1", ap1->ssid_string(), ap1->bssid_string(), 0, 0,
mukesh agrawal15908392011-11-16 18:29:25 +00001825 kNetworkModeInfrastructure);
Thieu Lee41a72d2012-02-06 20:46:51 +00001826 ReportBSS("ap2", ap2->ssid_string(), ap2->bssid_string(), 0, 0,
mukesh agrawal15908392011-11-16 18:29:25 +00001827 kNetworkModeInfrastructure);
1828 InitiateConnect(service1);
1829 ReportCurrentBSSChanged("ap1");
1830 ReportStateChanged(wpa_supplicant::kInterfaceStateCompleted);
1831 EXPECT_EQ(service1.get(), GetCurrentService().get());
1832
mukesh agrawalc01f3982012-01-24 13:48:39 -08001833 // Note that we deliberately omit intermediate supplicant states
1834 // (e.g. kInterfaceStateAssociating), on the theory that they are
1835 // unreliable. Specifically, they may be quashed if the association
1836 // completes before supplicant flushes its changed properties.
mukesh agrawal15908392011-11-16 18:29:25 +00001837 ReportCurrentBSSChanged("ap2");
1838 ReportStateChanged(wpa_supplicant::kInterfaceStateCompleted);
1839 EXPECT_EQ(service2.get(), GetCurrentService().get());
1840 EXPECT_EQ(Service::kStateIdle, service1->state());
1841 EXPECT_EQ(Service::kStateConfiguring, service2->state());
1842}
1843
1844TEST_F(WiFiMainTest, CurrentBSSChangeDisconnectedToConnected) {
Gary Moraind9f893f2012-02-06 10:03:40 -08001845 EXPECT_CALL(*manager(), HasService(_)).Times(AnyNumber());
1846 EXPECT_CALL(*manager(), UpdateService(_)).Times(AnyNumber());
Paul Stewartd408fdf2012-05-07 17:15:57 -07001847 EXPECT_CALL(*dhcp_provider(), CreateConfig(_, _, _, _)).Times(AnyNumber());
Gary Moraind9f893f2012-02-06 10:03:40 -08001848 EXPECT_CALL(*dhcp_config_.get(), RequestIP()).Times(AnyNumber());
mukesh agrawal15908392011-11-16 18:29:25 +00001849 WiFiEndpointRefPtr ap = MakeEndpoint("an_ssid", "00:01:02:03:04:05");
1850 WiFiServiceRefPtr service = CreateServiceForEndpoint(*ap);
1851
1852 // Note that the BSS handle used in this test ("an_ap") is not
1853 // intended to reflect the format used by supplicant. It's just
1854 // convenient for testing.
1855
1856 StartWiFi();
Thieu Lee41a72d2012-02-06 20:46:51 +00001857 ReportBSS("an_ap", ap->ssid_string(), ap->bssid_string(), 0, 0,
mukesh agrawal15908392011-11-16 18:29:25 +00001858 kNetworkModeInfrastructure);
1859 InitiateConnect(service);
1860 ReportCurrentBSSChanged("an_ap");
1861 ReportStateChanged(wpa_supplicant::kInterfaceStateCompleted);
1862 EXPECT_EQ(service.get(), GetCurrentService().get());
1863 EXPECT_EQ(Service::kStateConfiguring, service->state());
1864}
1865
Thieu Lee41a72d2012-02-06 20:46:51 +00001866TEST_F(WiFiMainTest, CurrentBSSChangedUpdateServiceEndpoint) {
mukesh agrawalb20776f2012-02-10 16:00:36 -08001867 EXPECT_CALL(*manager(), RegisterService(_)).Times(AnyNumber());
1868 EXPECT_CALL(*manager(), HasService(_)).Times(AnyNumber());
1869 EXPECT_CALL(*manager(), UpdateService(_)).Times(AnyNumber());
1870
Thieu Lee41a72d2012-02-06 20:46:51 +00001871 const uint16 frequency1 = 2412;
1872 const uint16 frequency2 = 2442;
1873 StartWiFi();
1874 ReportBSS("bss1", "ssid1", "00:00:00:00:00:01", 1, frequency1,
1875 kNetworkModeInfrastructure);
1876 ReportBSS("bss2", "ssid1", "00:00:00:00:00:02", 1, frequency2,
1877 kNetworkModeInfrastructure);
1878 EXPECT_EQ(1, GetServices().size());
1879 WiFiService *service(GetServices()[0].get());
1880 InitiateConnect(service);
1881 ReportCurrentBSSChanged("bss1");
1882 EXPECT_EQ(frequency1, service->frequency_);
mukesh agrawal923f14f2012-06-04 16:46:08 -07001883 EXPECT_EQ("00:00:00:00:00:01", service->bssid_);
Thieu Lee41a72d2012-02-06 20:46:51 +00001884 ReportCurrentBSSChanged("bss2");
1885 EXPECT_EQ(frequency2, service->frequency_);
mukesh agrawal923f14f2012-06-04 16:46:08 -07001886 EXPECT_EQ("00:00:00:00:00:02", service->bssid_);
Thieu Lee41a72d2012-02-06 20:46:51 +00001887}
1888
mukesh agrawal4eb4d782011-12-05 17:34:37 +00001889TEST_F(WiFiMainTest, ConfiguredServiceRegistration) {
1890 Error e;
1891 EXPECT_CALL(*manager(), RegisterService(_))
1892 .Times(0);
1893 EXPECT_CALL(*manager(), HasService(_))
1894 .WillOnce(Return(false));
1895 GetOpenService(flimflam::kTypeWifi, "an_ssid", flimflam::kModeManaged, &e);
1896 EXPECT_CALL(*manager(), RegisterService(_));
Thieu Lee41a72d2012-02-06 20:46:51 +00001897 ReportBSS("ap0", "an_ssid", "00:00:00:00:00:00", 0, 0,
mukesh agrawal4eb4d782011-12-05 17:34:37 +00001898 kNetworkModeInfrastructure);
1899}
1900
mukesh agrawal8a3188d2011-12-01 20:56:44 +00001901TEST_F(WiFiMainTest, NewConnectPreemptsPending) {
Gary Moraind9f893f2012-02-06 10:03:40 -08001902 EXPECT_CALL(*manager(), HasService(_)).Times(AnyNumber());
1903 EXPECT_CALL(*manager(), UpdateService(_)).Times(AnyNumber());
mukesh agrawal8a3188d2011-12-01 20:56:44 +00001904 WiFiEndpointRefPtr ap1 = MakeEndpoint("an_ssid", "00:01:02:03:04:05");
1905 WiFiEndpointRefPtr ap2 = MakeEndpoint("another_ssid", "01:02:03:04:05:06");
1906 WiFiServiceRefPtr service1 = CreateServiceForEndpoint(*ap1);
1907 WiFiServiceRefPtr service2 = CreateServiceForEndpoint(*ap2);
1908
1909 StartWiFi();
Thieu Lee41a72d2012-02-06 20:46:51 +00001910 ReportBSS("ap1", ap1->ssid_string(), ap1->bssid_string(), 0, 0,
mukesh agrawal8a3188d2011-12-01 20:56:44 +00001911 kNetworkModeInfrastructure);
Thieu Lee41a72d2012-02-06 20:46:51 +00001912 ReportBSS("ap2", ap2->ssid_string(), ap2->bssid_string(), 0, 0,
mukesh agrawal8a3188d2011-12-01 20:56:44 +00001913 kNetworkModeInfrastructure);
1914 InitiateConnect(service1);
1915 EXPECT_EQ(service1.get(), GetPendingService().get());
1916
1917 EXPECT_CALL(*GetSupplicantInterfaceProxy(), Disconnect());
1918 EXPECT_CALL(*GetSupplicantInterfaceProxy(), AddNetwork(_));
1919 InitiateConnect(service2);
1920 EXPECT_EQ(service2.get(), GetPendingService().get());
1921}
1922
1923TEST_F(WiFiMainTest, IsIdle) {
1924 StartWiFi();
1925 EXPECT_TRUE(wifi()->IsIdle());
1926
1927 WiFiEndpointRefPtr ap = MakeEndpoint("an_ssid", "00:01:02:03:04:05");
1928 WiFiServiceRefPtr service = CreateServiceForEndpoint(*ap);
1929 Error error;
1930 service->AddEndpoint(ap);
1931 service->AutoConnect();
1932 EXPECT_FALSE(wifi()->IsIdle());
1933}
1934
Darin Petkov4a66cc52012-06-15 10:08:29 +02001935MATCHER_P(WiFiAddedArgs, bgscan, "") {
mukesh agrawal4d0401c2012-01-06 16:05:31 -08001936 return ContainsKey(arg, wpa_supplicant::kNetworkPropertyScanSSID) &&
Darin Petkov4a66cc52012-06-15 10:08:29 +02001937 ContainsKey(arg, wpa_supplicant::kNetworkPropertyBgscan) == bgscan;
mukesh agrawal4d0401c2012-01-06 16:05:31 -08001938}
1939
1940TEST_F(WiFiMainTest, AddNetworkArgs) {
Gary Moraind9f893f2012-02-06 10:03:40 -08001941 EXPECT_CALL(*manager(), RegisterService(_)).Times(AnyNumber());
mukesh agrawal4d0401c2012-01-06 16:05:31 -08001942 MockSupplicantInterfaceProxy &supplicant_interface_proxy =
1943 *supplicant_interface_proxy_;
1944
1945 StartWiFi();
Thieu Lee41a72d2012-02-06 20:46:51 +00001946 ReportBSS("bss0", "ssid0", "00:00:00:00:00:00", 0, 0, kNetworkModeAdHoc);
mukesh agrawal4d0401c2012-01-06 16:05:31 -08001947 WiFiService *service(GetServices().begin()->get());
Darin Petkov4a66cc52012-06-15 10:08:29 +02001948 EXPECT_CALL(supplicant_interface_proxy, AddNetwork(WiFiAddedArgs(true)));
1949 EXPECT_TRUE(SetBgscanMethod(wpa_supplicant::kNetworkBgscanMethodSimple));
mukesh agrawal4d0401c2012-01-06 16:05:31 -08001950 InitiateConnect(service);
1951}
1952
Darin Petkov4a66cc52012-06-15 10:08:29 +02001953TEST_F(WiFiMainTest, AddNetworkArgsNoBgscan) {
1954 EXPECT_CALL(*manager(), RegisterService(_)).Times(AnyNumber());
1955 MockSupplicantInterfaceProxy &supplicant_interface_proxy =
1956 *supplicant_interface_proxy_;
1957
1958 StartWiFi();
1959 ReportBSS("bss0", "ssid0", "00:00:00:00:00:00", 0, 0, kNetworkModeAdHoc);
1960 WiFiService *service(GetServices().begin()->get());
1961 EXPECT_CALL(supplicant_interface_proxy, AddNetwork(WiFiAddedArgs(false)));
1962 InitiateConnect(service);
1963}
1964
1965TEST_F(WiFiMainTest, AppendBgscan) {
1966 StartWiFi();
1967 WiFiEndpointRefPtr ap1 = MakeEndpoint("an_ssid", "00:01:02:03:04:05");
1968 WiFiServiceRefPtr service = CreateServiceForEndpoint(*ap1);
1969 service->AddEndpoint(ap1);
1970 EXPECT_EQ(1, service->GetEndpointCount());
1971 {
1972 // 1 endpoint, default bgscan method -- background scan disabled.
1973 std::map<std::string, DBus::Variant> params;
1974 AppendBgscan(service.get(), &params);
1975 EXPECT_FALSE(ContainsKey(params, wpa_supplicant::kNetworkPropertyBgscan));
1976 }
1977 WiFiEndpointRefPtr ap2 = MakeEndpoint("an_ssid", "01:02:03:04:05:06");
1978 service->AddEndpoint(ap2);
1979 EXPECT_EQ(2, service->GetEndpointCount());
1980 {
1981 // 2 endpoints, default bgscan method -- background scan frequency reduced.
1982 map<string, DBus::Variant> params;
1983 AppendBgscan(service.get(), &params);
1984 string config_string;
1985 EXPECT_TRUE(
1986 DBusProperties::GetString(params,
1987 wpa_supplicant::kNetworkPropertyBgscan,
1988 &config_string));
1989 vector<string> elements;
1990 base::SplitString(config_string, ':', &elements);
1991 ASSERT_EQ(4, elements.size());
1992 EXPECT_EQ(WiFi::kDefaultBgscanMethod, elements[0]);
1993 EXPECT_EQ(StringPrintf("%d", WiFi::kBackgroundScanIntervalSeconds),
1994 elements[3]);
1995 }
1996 {
1997 // Explicit bgscan method -- regular background scan frequency.
1998 EXPECT_TRUE(SetBgscanMethod(wpa_supplicant::kNetworkBgscanMethodSimple));
1999 std::map<std::string, DBus::Variant> params;
2000 AppendBgscan(service.get(), &params);
2001 string config_string;
2002 EXPECT_TRUE(
2003 DBusProperties::GetString(params,
2004 wpa_supplicant::kNetworkPropertyBgscan,
2005 &config_string));
2006 vector<string> elements;
2007 base::SplitString(config_string, ':', &elements);
2008 ASSERT_EQ(4, elements.size());
2009 EXPECT_EQ(StringPrintf("%d", WiFi::kDefaultScanIntervalSeconds),
2010 elements[3]);
2011 }
Christopher Wiley5c929e22012-07-16 14:06:04 -07002012 {
2013 // No scan method, simply returns without appending properties
2014 EXPECT_TRUE(SetBgscanMethod(wpa_supplicant::kNetworkBgscanMethodNone));
2015 std::map<std::string, DBus::Variant> params;
2016 AppendBgscan(service.get(), &params);
2017 string config_string;
2018 EXPECT_FALSE(
2019 DBusProperties::GetString(params,
2020 wpa_supplicant::kNetworkPropertyBgscan,
2021 &config_string));
2022 }
Darin Petkov4a66cc52012-06-15 10:08:29 +02002023}
2024
mukesh agrawalc01f3982012-01-24 13:48:39 -08002025TEST_F(WiFiMainTest, StateAndIPIgnoreLinkEvent) {
2026 StartWiFi();
mukesh agrawalcf24a242012-05-21 16:46:11 -07002027 MockWiFiServiceRefPtr service = MakeMockService(flimflam::kSecurityNone);
mukesh agrawalc01f3982012-01-24 13:48:39 -08002028 InitiateConnect(service);
2029 EXPECT_CALL(*service.get(), SetState(_)).Times(0);
2030 EXPECT_CALL(*dhcp_config_.get(), RequestIP()).Times(0);
2031 ReportLinkUp();
2032
2033 // Verify expectations now, because WiFi may cause |service| state
2034 // changes during TearDown().
2035 Mock::VerifyAndClearExpectations(service);
Gary Moraind9f893f2012-02-06 10:03:40 -08002036 EXPECT_CALL(*service.get(), SetState(_)).Times(AnyNumber());
mukesh agrawalc01f3982012-01-24 13:48:39 -08002037}
2038
2039TEST_F(WiFiMainTest, SupplicantCompleted) {
Gary Moraind9f893f2012-02-06 10:03:40 -08002040 EXPECT_CALL(*manager(), UpdateService(_)).Times(AnyNumber());
Paul Stewartd408fdf2012-05-07 17:15:57 -07002041 EXPECT_CALL(*dhcp_provider(), CreateConfig(_, _, _, _)).Times(AnyNumber());
Gary Moraind9f893f2012-02-06 10:03:40 -08002042 EXPECT_CALL(*manager(), HasService(_)).Times(AnyNumber());
mukesh agrawalc01f3982012-01-24 13:48:39 -08002043 WiFiEndpointRefPtr ap = MakeEndpoint("an_ssid", "00:01:02:03:04:05");
2044 WiFiServiceRefPtr service = CreateServiceForEndpoint(*ap);
2045
2046 StartWiFi();
Thieu Lee41a72d2012-02-06 20:46:51 +00002047 ReportBSS("ap", ap->ssid_string(), ap->bssid_string(), 0, 0,
mukesh agrawalc01f3982012-01-24 13:48:39 -08002048 kNetworkModeInfrastructure);
2049 InitiateConnect(service);
2050
2051 EXPECT_CALL(*dhcp_config_.get(), RequestIP());
2052 ReportCurrentBSSChanged("ap");
2053 ReportStateChanged(wpa_supplicant::kInterfaceStateCompleted);
2054 EXPECT_EQ(Service::kStateConfiguring, service->state());
2055}
2056
2057TEST_F(WiFiMainTest, SupplicantCompletedAlreadyConnected) {
Gary Moraind9f893f2012-02-06 10:03:40 -08002058 EXPECT_CALL(*dhcp_config_.get(), RequestIP());
2059 EXPECT_CALL(*manager(), UpdateService(_)).Times(AnyNumber());
2060 EXPECT_CALL(*device_info(), FlushAddresses(_)).Times(AnyNumber());
2061 EXPECT_CALL(*manager(), device_info()).Times(AnyNumber());
Paul Stewartd408fdf2012-05-07 17:15:57 -07002062 EXPECT_CALL(*dhcp_provider(), CreateConfig(_, _, _, _)).Times(AnyNumber());
Gary Moraind9f893f2012-02-06 10:03:40 -08002063 EXPECT_CALL(*manager(), HasService(_)).Times(AnyNumber());
Gary Morainac1bdb42012-02-16 17:42:29 -08002064 EXPECT_CALL(*manager(), IsPortalDetectionEnabled(_)).Times(AnyNumber());
mukesh agrawalc01f3982012-01-24 13:48:39 -08002065 WiFiEndpointRefPtr ap = MakeEndpoint("an_ssid", "00:01:02:03:04:05");
2066 WiFiServiceRefPtr service = CreateServiceForEndpoint(*ap);
2067
mukesh agrawalc01f3982012-01-24 13:48:39 -08002068 StartWiFi();
Thieu Lee41a72d2012-02-06 20:46:51 +00002069 ReportBSS("ap", ap->ssid_string(), ap->bssid_string(), 0, 0,
mukesh agrawalc01f3982012-01-24 13:48:39 -08002070 kNetworkModeInfrastructure);
2071 InitiateConnect(service);
2072 ReportCurrentBSSChanged("ap");
2073 ReportStateChanged(wpa_supplicant::kInterfaceStateCompleted);
Christopher Wiley8f81e2a2012-10-17 16:51:32 -07002074 Mock::VerifyAndClearExpectations(dhcp_config_.get());
mukesh agrawalc01f3982012-01-24 13:48:39 -08002075 EXPECT_CALL(*dhcp_config_.get(), RequestIP()).Times(0);
Christopher Wiley8f81e2a2012-10-17 16:51:32 -07002076 // Simulate a rekeying event from the AP. These show as transitions from
2077 // completed->completed from wpa_supplicant.
2078 ReportStateChanged(wpa_supplicant::kInterfaceStateCompleted);
2079 ReportIPConfigComplete();
2080 // Similarly, rekeying events after we have an IP don't trigger L3
2081 // configuration.
mukesh agrawalc01f3982012-01-24 13:48:39 -08002082 ReportStateChanged(wpa_supplicant::kInterfaceStateCompleted);
2083}
2084
Paul Stewart66c86002012-01-30 18:00:52 -08002085TEST_F(WiFiMainTest, ClearCachedCredentials) {
Paul Stewartf48eca42012-10-11 12:10:02 -07002086 {
2087 ClearCachedCredentials();
2088 ScopedMockLog log;
2089 EXPECT_CALL(log, Log(_, _, EndsWith("supplicant proxy is NULL.")));
2090 // Also expect no crash due to supplicant interface proxy being NULL.
2091 dispatcher_.DispatchPendingEvents();
2092 }
2093
Paul Stewart66c86002012-01-30 18:00:52 -08002094 MockSupplicantInterfaceProxy &supplicant_interface_proxy =
2095 *supplicant_interface_proxy_;
2096
2097 StartWiFi();
2098
2099 // Ensure call to the proxy is deferred.
2100 EXPECT_CALL(supplicant_interface_proxy, ClearCachedCredentials())
2101 .Times(0);
2102 ClearCachedCredentials();
2103
2104 Mock::VerifyAndClearExpectations(&supplicant_interface_proxy);
2105
2106 EXPECT_CALL(supplicant_interface_proxy, ClearCachedCredentials())
2107 .Times(1);
2108 dispatcher_.DispatchPendingEvents();
2109
2110 Mock::VerifyAndClearExpectations(&supplicant_interface_proxy);
2111
2112 EXPECT_CALL(supplicant_interface_proxy, ClearCachedCredentials())
2113 .Times(0);
2114 ClearCachedCredentials();
2115 ClearCachedCredentials();
2116
2117 Mock::VerifyAndClearExpectations(&supplicant_interface_proxy);
2118
2119 // Ensure multiple calls to ClearCachedCredentials() results in only
2120 // one call to the proxy.
2121 EXPECT_CALL(supplicant_interface_proxy, ClearCachedCredentials())
2122 .Times(1);
2123 dispatcher_.DispatchPendingEvents();
2124}
2125
mukesh agrawalb20776f2012-02-10 16:00:36 -08002126TEST_F(WiFiMainTest, BSSAddedCreatesBSSProxy) {
2127 EXPECT_CALL(*manager(), RegisterService(_)).Times(AnyNumber());
2128 // TODO(quiche): Consider using a factory for WiFiEndpoints, so that
2129 // we can test the interaction between WiFi and WiFiEndpoint. (Right
2130 // now, we're testing across multiple layers.)
2131 EXPECT_CALL(*supplicant_bss_proxy_, Die()).Times(AnyNumber());
2132 EXPECT_CALL(*proxy_factory(), CreateSupplicantBSSProxy(_, _, _));
2133 StartWiFi();
2134 ReportBSS("bss0", "ssid0", "00:00:00:00:00:00", 0, 0, kNetworkModeAdHoc);
2135}
2136
2137TEST_F(WiFiMainTest, BSSRemovedDestroysBSSProxy) {
2138 // TODO(quiche): As for BSSAddedCreatesBSSProxy, consider using a
2139 // factory for WiFiEndpoints.
2140 EXPECT_CALL(*manager(), RegisterService(_)).Times(AnyNumber());
2141
2142 // Get the pointer before we transfer ownership.
2143 MockSupplicantBSSProxy *proxy = supplicant_bss_proxy_.get();
2144 EXPECT_CALL(*proxy, Die());
2145 StartWiFi();
2146 ReportBSS("bss0", "ssid0", "00:00:00:00:00:00", 0, 0, kNetworkModeAdHoc);
2147 RemoveBSS("bss0");
2148 // Check this now, to make sure RemoveBSS killed the proxy (rather
2149 // than TearDown).
2150 Mock::VerifyAndClearExpectations(proxy);
2151}
2152
mukesh agrawal5c05b292012-03-07 10:12:52 -08002153TEST_F(WiFiMainTest, FlushBSSOnResume) {
2154 const struct timeval resume_time = {1, 0};
2155 const struct timeval scan_done_time = {6, 0};
2156 MockSupplicantInterfaceProxy &supplicant_interface_proxy =
2157 *supplicant_interface_proxy_;
2158
2159 StartWiFi();
2160
2161 EXPECT_CALL(time_, GetTimeMonotonic(_))
2162 .WillOnce(DoAll(SetArgumentPointee<0>(resume_time), Return(0)))
2163 .WillOnce(DoAll(SetArgumentPointee<0>(scan_done_time), Return(0)));
2164 EXPECT_CALL(supplicant_interface_proxy,
2165 FlushBSS(WiFi::kMaxBSSResumeAgeSeconds + 5));
mukesh agrawal2f9df4e2012-08-08 12:29:20 -07002166 OnAfterResume();
mukesh agrawal5c05b292012-03-07 10:12:52 -08002167 ReportScanDone();
2168}
2169
mukesh agrawalb66c6462012-05-07 11:45:25 -07002170TEST_F(WiFiMainTest, ScanTimerIdle) {
2171 StartWiFi();
2172 dispatcher_.DispatchPendingEvents();
2173 ReportScanDone();
2174 CancelScanTimer();
2175 EXPECT_TRUE(GetScanTimer().IsCancelled());
2176
2177 EXPECT_CALL(*GetSupplicantInterfaceProxy(), Scan(_));
2178 FireScanTimer();
2179 dispatcher_.DispatchPendingEvents();
2180 EXPECT_FALSE(GetScanTimer().IsCancelled()); // Automatically re-armed.
2181}
2182
2183TEST_F(WiFiMainTest, ScanTimerScanning) {
2184 StartWiFi();
2185 dispatcher_.DispatchPendingEvents();
2186 CancelScanTimer();
2187 EXPECT_TRUE(GetScanTimer().IsCancelled());
2188
2189 // Should not call Scan, since we're already scanning.
2190 // (Scanning is triggered by StartWiFi.)
2191 EXPECT_CALL(*GetSupplicantInterfaceProxy(), Scan(_)).Times(0);
2192 FireScanTimer();
2193 dispatcher_.DispatchPendingEvents();
2194 EXPECT_FALSE(GetScanTimer().IsCancelled()); // Automatically re-armed.
2195}
2196
2197TEST_F(WiFiMainTest, ScanTimerConnecting) {
2198 StartWiFi();
2199 dispatcher_.DispatchPendingEvents();
2200 ReportBSS("bss0", "ssid0", "00:00:00:00:00:00", 0, 0, kNetworkModeAdHoc);
2201 ReportScanDone();
2202 WiFiService *service(GetServices().begin()->get());
2203 InitiateConnect(service);
2204 CancelScanTimer();
2205 EXPECT_TRUE(GetScanTimer().IsCancelled());
2206
2207 EXPECT_CALL(*GetSupplicantInterfaceProxy(), Scan(_)).Times(0);
2208 FireScanTimer();
2209 dispatcher_.DispatchPendingEvents();
2210 EXPECT_FALSE(GetScanTimer().IsCancelled()); // Automatically re-armed.
2211}
2212
2213TEST_F(WiFiMainTest, ScanTimerReconfigured) {
2214 StartWiFi();
2215 CancelScanTimer();
2216 EXPECT_TRUE(GetScanTimer().IsCancelled());
2217
2218 SetScanInterval(1);
2219 EXPECT_FALSE(GetScanTimer().IsCancelled());
2220}
2221
2222TEST_F(WiFiMainTest, ScanTimerResetOnScanDone) {
2223 StartWiFi();
2224 CancelScanTimer();
2225 EXPECT_TRUE(GetScanTimer().IsCancelled());
2226
2227 ReportScanDone();
2228 EXPECT_FALSE(GetScanTimer().IsCancelled());
2229}
2230
2231TEST_F(WiFiMainTest, ScanTimerStopOnZeroInterval) {
2232 StartWiFi();
2233 EXPECT_FALSE(GetScanTimer().IsCancelled());
2234
2235 SetScanInterval(0);
2236 EXPECT_TRUE(GetScanTimer().IsCancelled());
2237}
2238
2239TEST_F(WiFiMainTest, ScanOnDisconnectWithHidden) {
2240 Error e;
2241 scoped_refptr<MockProfile> profile(
2242 new NiceMock<MockProfile>(control_interface(), manager(), ""));
2243 WiFiServiceRefPtr hidden_service =
2244 GetServiceInner(flimflam::kTypeWifi, "hidden_ssid",
2245 flimflam::kModeManaged, NULL, NULL, true, &e);
2246 hidden_service->set_profile(profile);
2247
2248 StartWiFi();
2249 WiFiEndpointRefPtr ap = MakeEndpoint("an_ssid", "00:01:02:03:04:05");
2250 WiFiServiceRefPtr service = CreateServiceForEndpoint(*ap);
2251 ReportBSS("an_ap", ap->ssid_string(), ap->bssid_string(), 0, 0,
2252 kNetworkModeInfrastructure);
2253 InitiateConnect(service);
2254 ReportCurrentBSSChanged("an_ap");
2255 ReportStateChanged(wpa_supplicant::kInterfaceStateCompleted);
2256 dispatcher_.DispatchPendingEvents();
2257
2258 EXPECT_CALL(*GetSupplicantInterfaceProxy(),
2259 Scan(HasHiddenSSID("hidden_ssid")));
2260 ReportCurrentBSSChanged(wpa_supplicant::kCurrentBSSNull);
2261 dispatcher_.DispatchPendingEvents();
2262}
2263
2264TEST_F(WiFiMainTest, NoScanOnDisconnectWithoutHidden) {
2265 StartWiFi();
2266 WiFiEndpointRefPtr ap = MakeEndpoint("an_ssid", "00:01:02:03:04:05");
2267 WiFiServiceRefPtr service = CreateServiceForEndpoint(*ap);
2268 ReportBSS("an_ap", ap->ssid_string(), ap->bssid_string(), 0, 0,
2269 kNetworkModeInfrastructure);
2270 InitiateConnect(service);
2271 ReportCurrentBSSChanged("an_ap");
2272 ReportStateChanged(wpa_supplicant::kInterfaceStateCompleted);
2273 dispatcher_.DispatchPendingEvents();
2274
2275 EXPECT_CALL(*GetSupplicantInterfaceProxy(), Scan(_)).Times(0);
2276 ReportCurrentBSSChanged(wpa_supplicant::kCurrentBSSNull);
2277 dispatcher_.DispatchPendingEvents();
2278}
2279
Paul Stewart3c508e12012-08-09 11:40:06 -07002280TEST_F(WiFiMainTest, LinkMonitorFailure) {
2281 StartWiFi();
2282 ScopedMockLog log;
2283 EXPECT_CALL(log, Log(_, _, _)).Times(AnyNumber());
2284 MockLinkMonitor *link_monitor = new StrictMock<MockLinkMonitor>();
2285 SetLinkMonitor(link_monitor);
2286 EXPECT_CALL(*link_monitor, IsGatewayFound())
2287 .WillOnce(Return(false))
2288 .WillRepeatedly(Return(true));
2289 EXPECT_CALL(log, Log(logging::LOG_INFO, _,
2290 EndsWith("gateway was never found."))).Times(1);
2291 EXPECT_CALL(*GetSupplicantInterfaceProxy(), Reassociate()).Times(0);
2292 OnLinkMonitorFailure();
2293 EXPECT_CALL(log, Log(logging::LOG_INFO, _,
2294 EndsWith("Called Reassociate()."))).Times(1);
2295 EXPECT_CALL(*GetSupplicantInterfaceProxy(), Reassociate()).Times(1);
2296 OnLinkMonitorFailure();
2297 OnSupplicantVanish();
2298 EXPECT_CALL(log, Log(logging::LOG_ERROR, _,
2299 EndsWith("Cannot reassociate."))).Times(1);
2300 OnLinkMonitorFailure();
2301}
2302
mukesh agrawalcf24a242012-05-21 16:46:11 -07002303TEST_F(WiFiMainTest, SuspectCredentialsOpen) {
2304 Error e;
2305 WiFiServiceRefPtr service = GetOpenService(
2306 flimflam::kTypeWifi, "an_ssid", flimflam::kModeManaged, &e);
2307 ReportStateChanged(wpa_supplicant::kInterfaceState4WayHandshake);
2308 EXPECT_FALSE(service->has_ever_connected());
2309 EXPECT_FALSE(wifi()->SuspectCredentials(*service));
2310}
2311
2312TEST_F(WiFiMainTest, SuspectCredentialsWPANeverConnected) {
2313 Error e;
2314 WiFiServiceRefPtr service =
2315 GetService(flimflam::kTypeWifi, "an_ssid", flimflam::kModeManaged,
2316 flimflam::kSecurityWpa, "abcdefgh", &e);
2317 ReportStateChanged(wpa_supplicant::kInterfaceState4WayHandshake);
2318 EXPECT_FALSE(service->has_ever_connected());
2319 EXPECT_TRUE(wifi()->SuspectCredentials(*service));
2320}
2321
2322TEST_F(WiFiMainTest, SuspectCredentialsWPAPreviouslyConnected) {
2323 Error e;
2324 WiFiServiceRefPtr service =
2325 GetService(flimflam::kTypeWifi, "an_ssid", flimflam::kModeManaged,
2326 flimflam::kSecurityWpa, "abcdefgh", &e);
2327 ReportStateChanged(wpa_supplicant::kInterfaceState4WayHandshake);
2328 service->has_ever_connected_ = true;
2329 EXPECT_FALSE(wifi()->SuspectCredentials(*service));
2330}
2331
2332TEST_F(WiFiMainTest, SuspectCredentialsYieldFailure) {
2333 ScopedMockLog log;
2334 Error e;
2335 MockWiFiServiceRefPtr service = MakeMockService(flimflam::kSecurityWpa);
2336 SetPendingService(service);
2337 ReportStateChanged(wpa_supplicant::kInterfaceState4WayHandshake);
2338 EXPECT_FALSE(service->has_ever_connected());
2339
Paul Stewartf2d60912012-07-15 08:37:30 -07002340 EXPECT_CALL(*service, SetFailure(Service::kFailureBadPassphrase));
mukesh agrawal56e32202012-07-26 16:32:11 -07002341 EXPECT_CALL(*service, SetFailureSilent(_)).Times(0);
2342 EXPECT_CALL(*service, SetState(_)).Times(0);
mukesh agrawalcf24a242012-05-21 16:46:11 -07002343 EXPECT_CALL(log, Log(_, _, _)).Times(AnyNumber());
2344 EXPECT_CALL(log, Log(logging::LOG_ERROR, _, EndsWith("Bad passphrase?")));
2345 ReportCurrentBSSChanged(wpa_supplicant::kCurrentBSSNull);
mukesh agrawalcf24a242012-05-21 16:46:11 -07002346}
2347
Paul Stewarte369ece2012-05-22 09:11:03 -07002348// Scanning tests will use a mock of the event dispatcher instead of a real
2349// one.
Paul Stewart1aff7302012-08-04 20:04:47 -07002350class WiFiTimerTest : public WiFiObjectTest {
Paul Stewarte369ece2012-05-22 09:11:03 -07002351 public:
Paul Stewart1aff7302012-08-04 20:04:47 -07002352 WiFiTimerTest() : WiFiObjectTest(&mock_dispatcher_) {}
Paul Stewarte369ece2012-05-22 09:11:03 -07002353
2354 protected:
2355 void ExpectInitialScanSequence();
2356
2357 StrictMock<MockEventDispatcher> mock_dispatcher_;
2358};
2359
Paul Stewart1aff7302012-08-04 20:04:47 -07002360void WiFiTimerTest::ExpectInitialScanSequence() {
Paul Stewarte369ece2012-05-22 09:11:03 -07002361 // Choose a number of iterations some multiple higher than the fast scan
2362 // count.
2363 const int kScanTimes = WiFi::kNumFastScanAttempts * 4;
2364
2365 // Each time we call FireScanTimer() below, WiFi will post a task to actually
2366 // run Scan() on the wpa_supplicant proxy.
2367 EXPECT_CALL(mock_dispatcher_, PostTask(_))
2368 .Times(kScanTimes);
2369 {
2370 InSequence seq;
2371 // The scans immediately after the initial scan should happen at the short
2372 // interval. If we add the initial scan (not invoked in this function) to
2373 // the ones in the expectation below, we get WiFi::kNumFastScanAttempts at
2374 // the fast scan interval.
2375 EXPECT_CALL(mock_dispatcher_, PostDelayedTask(
2376 _, WiFi::kFastScanIntervalSeconds * 1000))
2377 .Times(WiFi::kNumFastScanAttempts - 1)
2378 .WillRepeatedly(Return(true));
2379
2380 // After this, the WiFi device should use the normal scan interval.
2381 EXPECT_CALL(mock_dispatcher_, PostDelayedTask(
2382 _, GetScanInterval() * 1000))
2383 .Times(kScanTimes - WiFi::kNumFastScanAttempts + 1)
2384 .WillRepeatedly(Return(true));
2385
2386 for (int i = 0; i < kScanTimes; i++) {
2387 FireScanTimer();
2388 }
2389 }
2390}
2391
Paul Stewart1aff7302012-08-04 20:04:47 -07002392TEST_F(WiFiTimerTest, FastRescan) {
Paul Stewarte369ece2012-05-22 09:11:03 -07002393 // This PostTask is a result of the call to Scan(NULL), and is meant to
2394 // post a task to call Scan() on the wpa_supplicant proxy immediately.
2395 EXPECT_CALL(mock_dispatcher_, PostTask(_));
2396 EXPECT_CALL(mock_dispatcher_, PostDelayedTask(
2397 _, WiFi::kFastScanIntervalSeconds * 1000))
2398 .WillOnce(Return(true));
2399 StartWiFi();
2400
2401 ExpectInitialScanSequence();
2402
2403 // If we end up disconnecting, the sequence should repeat.
2404 EXPECT_CALL(mock_dispatcher_, PostDelayedTask(
2405 _, WiFi::kFastScanIntervalSeconds * 1000))
2406 .WillOnce(Return(true));
2407 RestartFastScanAttempts();
2408
2409 ExpectInitialScanSequence();
2410}
2411
Paul Stewart1aff7302012-08-04 20:04:47 -07002412TEST_F(WiFiTimerTest, ReconnectTimer) {
2413 EXPECT_CALL(mock_dispatcher_, PostTask(_)).Times(AnyNumber());
2414 EXPECT_CALL(mock_dispatcher_, PostDelayedTask(_, _)).Times(AnyNumber());
2415 SetupConnectedService(DBus::Path());
2416 Mock::VerifyAndClearExpectations(&mock_dispatcher_);
2417
2418 EXPECT_CALL(mock_dispatcher_, PostDelayedTask(
2419 _, GetReconnectTimeoutSeconds() * 1000)).Times(1);
2420 StartReconnectTimer();
2421 Mock::VerifyAndClearExpectations(&mock_dispatcher_);
2422 StopReconnectTimer();
2423
2424 EXPECT_CALL(mock_dispatcher_, PostDelayedTask(
2425 _, GetReconnectTimeoutSeconds() * 1000)).Times(1);
2426 StartReconnectTimer();
2427 Mock::VerifyAndClearExpectations(&mock_dispatcher_);
2428 GetReconnectTimeoutCallback().callback().Run();
2429
2430 EXPECT_CALL(mock_dispatcher_, PostDelayedTask(
2431 _, GetReconnectTimeoutSeconds() * 1000)).Times(1);
2432 StartReconnectTimer();
2433 Mock::VerifyAndClearExpectations(&mock_dispatcher_);
2434
2435 EXPECT_CALL(mock_dispatcher_, PostDelayedTask(
2436 _, GetReconnectTimeoutSeconds() * 1000)).Times(0);
2437 StartReconnectTimer();
2438}
2439
Paul Stewartbc6e7392012-05-24 07:07:48 -07002440TEST_F(WiFiMainTest, EAPCertification) {
2441 MockWiFiServiceRefPtr service = MakeMockService(flimflam::kSecurity8021x);
2442 EXPECT_CALL(*service, AddEAPCertification(_, _)).Times(0);
2443
2444 ScopedMockLog log;
2445 EXPECT_CALL(log, Log(logging::LOG_ERROR, _, EndsWith("no current service.")));
2446 map<string, ::DBus::Variant> args;
2447 ReportCertification(args);
2448 Mock::VerifyAndClearExpectations(&log);
2449
2450 SetCurrentService(service);
2451 EXPECT_CALL(log, Log(logging::LOG_ERROR, _, EndsWith("no depth parameter.")));
2452 ReportCertification(args);
2453 Mock::VerifyAndClearExpectations(&log);
2454
2455 const uint32 kDepth = 123;
2456 args[wpa_supplicant::kInterfacePropertyDepth].writer().append_uint32(kDepth);
2457
2458 EXPECT_CALL(log,
2459 Log(logging::LOG_ERROR, _, EndsWith("no subject parameter.")));
2460 ReportCertification(args);
2461 Mock::VerifyAndClearExpectations(&log);
2462
2463 const string kSubject("subject");
2464 args[wpa_supplicant::kInterfacePropertySubject].writer()
2465 .append_string(kSubject.c_str());
2466 EXPECT_CALL(*service, AddEAPCertification(kSubject, kDepth)).Times(1);
2467 ReportCertification(args);
2468}
2469
mukesh agrawalc4f368f2012-06-04 19:45:52 -07002470TEST_F(WiFiMainTest, PendingScanDoesNotCrashAfterStop) {
2471 // Scan is one task that should be skipped after Stop. Others are
2472 // skipped by the same mechanism (invalidating weak pointers), so we
2473 // don't test them individually.
2474 //
2475 // Note that we can't test behavior by setting expectations on the
2476 // supplicant_interface_proxy_, since that is destroyed when we StopWiFi().
2477 StartWiFi();
2478 StopWiFi();
2479 dispatcher_.DispatchPendingEvents();
2480}
2481
Darin Petkov60ceaf32012-10-18 10:36:01 +02002482TEST_F(WiFiMainTest, VerifyPaths) {
2483 string path = WiFi::kSupplicantConfPath;
2484 TrimString(path, FilePath::kSeparators, &path);
2485 EXPECT_TRUE(file_util::PathExists(FilePath(SYSROOT).Append(path)));
2486}
2487
Gaurav Shah6d2c72d2012-10-16 16:30:44 -07002488struct BSS {
2489 string bsspath;
2490 string ssid;
2491 string bssid;
2492 int16_t signal_strength;
2493 uint16 frequency;
2494 const char* mode;
2495};
2496
2497TEST_F(WiFiMainTest, GetGeolocationObjects) {
2498 BSS bsses[] = {
2499 {"bssid1", "ssid1", "00:00:00:00:00:00", 5, Metrics::kWiFiFrequency2412,
2500 kNetworkModeInfrastructure},
2501 {"bssid2", "ssid2", "01:00:00:00:00:00", 30, Metrics::kWiFiFrequency5170,
2502 kNetworkModeInfrastructure},
2503 // Same SSID but different BSSID is an additional geolocation object.
2504 {"bssid3", "ssid1", "02:00:00:00:00:00", 100, 0,
2505 kNetworkModeInfrastructure}
2506 };
2507 StartWiFi();
2508 vector<GeolocationInfo> objects;
2509 EXPECT_EQ(objects.size(), 0);
2510
2511 for (size_t i = 0; i < arraysize(bsses); ++i) {
2512 ReportBSS(bsses[i].bsspath, bsses[i].ssid, bsses[i].bssid,
2513 bsses[i].signal_strength, bsses[i].frequency, bsses[i].mode);
2514 objects = wifi()->GetGeolocationObjects();
2515 EXPECT_EQ(objects.size(), i + 1);
2516
2517 GeolocationInfo expected_info;
2518 expected_info.AddField(kGeoMacAddressProperty, bsses[i].bssid);
2519 expected_info.AddField(kGeoSignalStrengthProperty,
2520 StringPrintf("%d", bsses[i].signal_strength));
2521 expected_info.AddField(kGeoChannelProperty, StringPrintf(
2522 "%d", Metrics::WiFiFrequencyToChannel(bsses[i].frequency)));
2523 EXPECT_TRUE(objects[i].Equals(expected_info));
2524 };
2525};
2526
Chris Masone853b81b2011-06-24 14:11:41 -07002527} // namespace shill