blob: 2e26d15560bc9dce5482d80d5a5d0767a0a7b3d9 [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
mukesh agrawal7ec71312011-11-10 02:08:26 +000017#include <base/memory/ref_counted.h>
mukesh agrawaldc42bb32011-07-28 10:40:26 -070018#include <base/memory/scoped_ptr.h>
Paul Stewarta41e38d2011-11-11 07:47:29 -080019#include <base/stringprintf.h>
mukesh agrawal31950242011-07-14 11:53:38 -070020#include <base/string_number_conversions.h>
21#include <base/string_util.h>
Chris Masone853b81b2011-06-24 14:11:41 -070022#include <chromeos/dbus/service_constants.h>
mukesh agrawal1830fa12011-09-26 14:31:40 -070023#include <dbus-c++/dbus.h>
Chris Masone7156c922011-08-23 20:36:21 -070024#include <gmock/gmock.h>
Chris Masone2ae797d2011-08-23 20:41:00 -070025#include <gtest/gtest.h>
Chris Masone853b81b2011-06-24 14:11:41 -070026
27#include "shill/dbus_adaptor.h"
Paul Stewart26b327e2011-10-19 11:38:09 -070028#include "shill/event_dispatcher.h"
mukesh agrawal8ede0522011-10-03 14:57:44 -070029#include "shill/ieee80211.h"
mukesh agrawal7a4e4002011-09-06 11:26:05 -070030#include "shill/key_value_store.h"
Chris Masone853b81b2011-06-24 14:11:41 -070031#include "shill/manager.h"
Chris Masone853b81b2011-06-24 14:11:41 -070032#include "shill/mock_device.h"
mukesh agrawalc01f3982012-01-24 13:48:39 -080033#include "shill/mock_device_info.h"
mukesh agrawalf2f68a52011-09-01 12:15:48 -070034#include "shill/mock_dhcp_config.h"
35#include "shill/mock_dhcp_provider.h"
Chris Masone2ae797d2011-08-23 20:41:00 -070036#include "shill/mock_manager.h"
Thieu Le3426c8f2012-01-11 17:35:11 -080037#include "shill/mock_metrics.h"
Gary Morainac1bdb42012-02-16 17:42:29 -080038#include "shill/mock_power_manager.h"
39#include "shill/mock_power_manager_proxy.h"
Paul Stewart10ccbb32012-04-26 15:59:30 -070040#include "shill/mock_profile.h"
mukesh agrawal5c4dd0b2011-09-14 13:53:14 -070041#include "shill/mock_rtnl_handler.h"
Paul Stewarta41e38d2011-11-11 07:47:29 -080042#include "shill/mock_store.h"
mukesh agrawalb20776f2012-02-10 16:00:36 -080043#include "shill/mock_supplicant_bss_proxy.h"
mukesh agrawal31950242011-07-14 11:53:38 -070044#include "shill/mock_supplicant_interface_proxy.h"
45#include "shill/mock_supplicant_process_proxy.h"
mukesh agrawal5c05b292012-03-07 10:12:52 -080046#include "shill/mock_time.h"
mukesh agrawal7ec71312011-11-10 02:08:26 +000047#include "shill/mock_wifi_service.h"
mukesh agrawaldc42bb32011-07-28 10:40:26 -070048#include "shill/nice_mock_control.h"
Chris Masone853b81b2011-06-24 14:11:41 -070049#include "shill/property_store_unittest.h"
mukesh agrawal31950242011-07-14 11:53:38 -070050#include "shill/proxy_factory.h"
51#include "shill/wifi_endpoint.h"
mukesh agrawal32399322011-09-01 10:53:43 -070052#include "shill/wifi_service.h"
Paul Stewartced6a0b2011-11-08 15:32:04 -080053#include "shill/wpa_supplicant.h"
Chris Masone853b81b2011-06-24 14:11:41 -070054
Gary Morainac1bdb42012-02-16 17:42:29 -080055
Chris Masone853b81b2011-06-24 14:11:41 -070056using std::map;
Paul Stewarta41e38d2011-11-11 07:47:29 -080057using std::set;
Chris Masone853b81b2011-06-24 14:11:41 -070058using std::string;
59using std::vector;
60using ::testing::_;
mukesh agrawal31950242011-07-14 11:53:38 -070061using ::testing::AnyNumber;
62using ::testing::DefaultValue;
Paul Stewarta41e38d2011-11-11 07:47:29 -080063using ::testing::DoAll;
mukesh agrawal31950242011-07-14 11:53:38 -070064using ::testing::InSequence;
mukesh agrawalb20776f2012-02-10 16:00:36 -080065using ::testing::Invoke;
mukesh agrawal7ec71312011-11-10 02:08:26 +000066using ::testing::Mock;
mukesh agrawaldc42bb32011-07-28 10:40:26 -070067using ::testing::NiceMock;
Chris Masone853b81b2011-06-24 14:11:41 -070068using ::testing::Return;
Gary Morainac1bdb42012-02-16 17:42:29 -080069using ::testing::SaveArg;
Paul Stewarta41e38d2011-11-11 07:47:29 -080070using ::testing::SetArgumentPointee;
71using ::testing::StrEq;
72using ::testing::StrictMock;
Chris Masone853b81b2011-06-24 14:11:41 -070073using ::testing::Test;
mukesh agrawal31950242011-07-14 11:53:38 -070074using ::testing::Throw;
mukesh agrawal8ede0522011-10-03 14:57:44 -070075using ::testing::Values;
Chris Masone853b81b2011-06-24 14:11:41 -070076
77namespace shill {
78
mukesh agrawal31950242011-07-14 11:53:38 -070079class WiFiPropertyTest : public PropertyStoreTest {
Chris Masone853b81b2011-06-24 14:11:41 -070080 public:
mukesh agrawal31950242011-07-14 11:53:38 -070081 WiFiPropertyTest()
Thieu Le3426c8f2012-01-11 17:35:11 -080082 : device_(new WiFi(control_interface(),
83 NULL, NULL, NULL, "wifi", "", 0)) {
Chris Masone853b81b2011-06-24 14:11:41 -070084 }
mukesh agrawal31950242011-07-14 11:53:38 -070085 virtual ~WiFiPropertyTest() {}
Chris Masone853b81b2011-06-24 14:11:41 -070086
87 protected:
mukesh agrawal8abd2f62012-01-30 14:56:14 -080088 WiFiRefPtr device_;
Chris Masone853b81b2011-06-24 14:11:41 -070089};
90
mukesh agrawal31950242011-07-14 11:53:38 -070091TEST_F(WiFiPropertyTest, Contains) {
mukesh agrawalde29fa82011-09-16 16:16:36 -070092 EXPECT_TRUE(device_->store().Contains(flimflam::kNameProperty));
93 EXPECT_FALSE(device_->store().Contains(""));
Chris Masone853b81b2011-06-24 14:11:41 -070094}
95
mukesh agrawal6bb9e7c2012-01-30 14:57:54 -080096TEST_F(WiFiPropertyTest, SetProperty) {
Chris Masonea8a2c252011-06-27 22:16:30 -070097 {
98 ::DBus::Error error;
mukesh agrawal6bb9e7c2012-01-30 14:57:54 -080099 EXPECT_TRUE(DBusAdaptor::SetProperty(
mukesh agrawalde29fa82011-09-16 16:16:36 -0700100 device_->mutable_store(),
Chris Masonea8a2c252011-06-27 22:16:30 -0700101 flimflam::kBgscanSignalThresholdProperty,
102 PropertyStoreTest::kInt32V,
103 &error));
104 }
105 {
106 ::DBus::Error error;
mukesh agrawal6bb9e7c2012-01-30 14:57:54 -0800107 EXPECT_TRUE(DBusAdaptor::SetProperty(device_->mutable_store(),
108 flimflam::kScanIntervalProperty,
109 PropertyStoreTest::kUint16V,
110 &error));
Chris Masonea8a2c252011-06-27 22:16:30 -0700111 }
Chris Masone853b81b2011-06-24 14:11:41 -0700112 // Ensure that an attempt to write a R/O property returns InvalidArgs error.
Chris Masonea8a2c252011-06-27 22:16:30 -0700113 {
114 ::DBus::Error error;
mukesh agrawal6bb9e7c2012-01-30 14:57:54 -0800115 EXPECT_FALSE(DBusAdaptor::SetProperty(device_->mutable_store(),
116 flimflam::kScanningProperty,
117 PropertyStoreTest::kBoolV,
118 &error));
Chris Masone9d779932011-08-25 16:33:41 -0700119 EXPECT_EQ(invalid_args(), error.name());
Chris Masonea8a2c252011-06-27 22:16:30 -0700120 }
Chris Masone853b81b2011-06-24 14:11:41 -0700121
mukesh agrawal4d0401c2012-01-06 16:05:31 -0800122 {
123 ::DBus::Error error;
mukesh agrawal6bb9e7c2012-01-30 14:57:54 -0800124 EXPECT_TRUE(DBusAdaptor::SetProperty(
mukesh agrawal4d0401c2012-01-06 16:05:31 -0800125 device_->mutable_store(),
126 flimflam::kBgscanMethodProperty,
127 DBusAdaptor::StringToVariant(
128 wpa_supplicant::kNetworkBgscanMethodSimple),
129 &error));
130 }
131
132 {
133 ::DBus::Error error;
mukesh agrawal6bb9e7c2012-01-30 14:57:54 -0800134 EXPECT_FALSE(DBusAdaptor::SetProperty(
mukesh agrawal4d0401c2012-01-06 16:05:31 -0800135 device_->mutable_store(),
136 flimflam::kBgscanMethodProperty,
137 DBusAdaptor::StringToVariant("not a real scan method"),
138 &error));
139 }
140}
141
mukesh agrawal8abd2f62012-01-30 14:56:14 -0800142TEST_F(WiFiPropertyTest, ClearDerivedProperty) {
143 EXPECT_NE(wpa_supplicant::kNetworkBgscanMethodLearn,
144 WiFi::kDefaultBgscanMethod);
145 EXPECT_EQ(WiFi::kDefaultBgscanMethod, device_->bgscan_method_);
146
147 ::DBus::Error error;
mukesh agrawal6bb9e7c2012-01-30 14:57:54 -0800148 EXPECT_TRUE(DBusAdaptor::SetProperty(
mukesh agrawal8abd2f62012-01-30 14:56:14 -0800149 device_->mutable_store(),
150 flimflam::kBgscanMethodProperty,
151 DBusAdaptor::StringToVariant(
152 wpa_supplicant::kNetworkBgscanMethodLearn),
153 &error));
154 EXPECT_EQ(wpa_supplicant::kNetworkBgscanMethodLearn, device_->bgscan_method_);
155
156 EXPECT_TRUE(DBusAdaptor::ClearProperty(
157 device_->mutable_store(), flimflam::kBgscanMethodProperty, &error));
158 EXPECT_EQ(WiFi::kDefaultBgscanMethod, device_->bgscan_method_);
159}
160
mukesh agrawal8ede0522011-10-03 14:57:44 -0700161class WiFiMainTest : public ::testing::TestWithParam<string> {
mukesh agrawal31950242011-07-14 11:53:38 -0700162 public:
163 WiFiMainTest()
Thieu Le3426c8f2012-01-11 17:35:11 -0800164 : manager_(&control_interface_, NULL, &metrics_, &glib_),
mukesh agrawalc01f3982012-01-24 13:48:39 -0800165 device_info_(&control_interface_, &dispatcher_, &metrics_, &manager_),
Chris Masone626719f2011-08-18 16:58:48 -0700166 wifi_(new WiFi(&control_interface_,
mukesh agrawal32399322011-09-01 10:53:43 -0700167 &dispatcher_,
Thieu Le3426c8f2012-01-11 17:35:11 -0800168 &metrics_,
Chris Masone626719f2011-08-18 16:58:48 -0700169 &manager_,
170 kDeviceName,
171 kDeviceAddress,
172 0)),
mukesh agrawaldc42bb32011-07-28 10:40:26 -0700173 supplicant_process_proxy_(new NiceMock<MockSupplicantProcessProxy>()),
174 supplicant_interface_proxy_(
175 new NiceMock<MockSupplicantInterfaceProxy>(wifi_)),
mukesh agrawalb20776f2012-02-10 16:00:36 -0800176 supplicant_bss_proxy_(
177 new NiceMock<MockSupplicantBSSProxy>()),
mukesh agrawalf2f68a52011-09-01 12:15:48 -0700178 dhcp_config_(new MockDHCPConfig(&control_interface_,
179 &dispatcher_,
180 &dhcp_provider_,
181 kDeviceName,
Paul Stewartd32f4842012-01-11 16:08:13 -0800182 kHostName,
mukesh agrawalf2f68a52011-09-01 12:15:48 -0700183 &glib_)),
Gary Morainac1bdb42012-02-16 17:42:29 -0800184 proxy_factory_(this),
Eric Shienbrood3e20a232012-02-16 11:35:56 -0500185 power_manager_(new MockPowerManager(&proxy_factory_)) {
mukesh agrawal31950242011-07-14 11:53:38 -0700186 ::testing::DefaultValue< ::DBus::Path>::Set("/default/path");
mukesh agrawal4eb4d782011-12-05 17:34:37 +0000187 // Except for WiFiServices created via WiFi::GetService, we expect
188 // that any WiFiService has been registered with the Manager. So
189 // default Manager.HasService to true, to make the common case
190 // easy.
191 ON_CALL(manager_, HasService(_)).
192 WillByDefault(Return(true));
mukesh agrawalc01f3982012-01-24 13:48:39 -0800193
194 ON_CALL(dhcp_provider_, CreateConfig(_, _)).
195 WillByDefault(Return(dhcp_config_));
196 ON_CALL(*dhcp_config_.get(), RequestIP()).
197 WillByDefault(Return(true));
Gary Morainac1bdb42012-02-16 17:42:29 -0800198
199 // |manager_| takes ownership of |power_manager_|.
200 manager_.set_power_manager(power_manager_);
mukesh agrawal5c05b292012-03-07 10:12:52 -0800201
202 wifi_->time_ = &time_;
mukesh agrawal31950242011-07-14 11:53:38 -0700203 }
mukesh agrawal5c4dd0b2011-09-14 13:53:14 -0700204
205 virtual void SetUp() {
Darin Petkovab565bb2011-10-06 02:55:51 -0700206 wifi_->proxy_factory_ = &proxy_factory_;
mukesh agrawal5c4dd0b2011-09-14 13:53:14 -0700207 static_cast<Device *>(wifi_)->rtnl_handler_ = &rtnl_handler_;
208 wifi_->set_dhcp_provider(&dhcp_provider_);
mukesh agrawalc01f3982012-01-24 13:48:39 -0800209 ON_CALL(manager_, device_info()).
210 WillByDefault(Return(&device_info_));
Chris Masoneb9c00592011-10-06 13:10:39 -0700211 EXPECT_CALL(manager_, DeregisterService(_)).Times(AnyNumber());
mukesh agrawalb20776f2012-02-10 16:00:36 -0800212 EXPECT_CALL(*supplicant_bss_proxy_, Die()).Times(AnyNumber());
mukesh agrawal5c4dd0b2011-09-14 13:53:14 -0700213 }
214
215 virtual void TearDown() {
Gary Moraind9f893f2012-02-06 10:03:40 -0800216 EXPECT_CALL(*manager(), UpdateService(_)).Times(AnyNumber());
Gary Morain91001122012-02-29 16:22:26 -0800217 EXPECT_CALL(*power_manager_, RemoveStateChangeCallback(wifi_->UniqueName()))
218 .Times(AnyNumber());
Eric Shienbrood9a245532012-03-07 14:20:39 -0500219 wifi_->SelectService(NULL);
mukesh agrawalb20776f2012-02-10 16:00:36 -0800220 if (supplicant_bss_proxy_.get()) {
221 EXPECT_CALL(*supplicant_bss_proxy_, Die());
222 }
Darin Petkovab565bb2011-10-06 02:55:51 -0700223 wifi_->proxy_factory_ = NULL;
mukesh agrawal31950242011-07-14 11:53:38 -0700224 // must Stop WiFi instance, to clear its list of services.
225 // otherwise, the WiFi instance will not be deleted. (because
226 // services reference a WiFi instance, creating a cycle.)
Eric Shienbrood9a245532012-03-07 14:20:39 -0500227 wifi_->Stop(NULL, ResultCallback());
mukesh agrawal5c4dd0b2011-09-14 13:53:14 -0700228 wifi_->set_dhcp_provider(NULL);
mukesh agrawalf2f68a52011-09-01 12:15:48 -0700229 }
230
mukesh agrawal31950242011-07-14 11:53:38 -0700231 protected:
mukesh agrawal7ec71312011-11-10 02:08:26 +0000232 typedef scoped_refptr<MockWiFiService> MockWiFiServiceRefPtr;
233
mukesh agrawal31950242011-07-14 11:53:38 -0700234 class TestProxyFactory : public ProxyFactory {
235 public:
mukesh agrawalb20776f2012-02-10 16:00:36 -0800236 explicit TestProxyFactory(WiFiMainTest *test);
mukesh agrawal31950242011-07-14 11:53:38 -0700237
238 virtual SupplicantProcessProxyInterface *CreateSupplicantProcessProxy(
mukesh agrawal1830fa12011-09-26 14:31:40 -0700239 const char */*dbus_path*/, const char */*dbus_addr*/) {
mukesh agrawaldc42bb32011-07-28 10:40:26 -0700240 return test_->supplicant_process_proxy_.release();
mukesh agrawal31950242011-07-14 11:53:38 -0700241 }
242
243 virtual SupplicantInterfaceProxyInterface *CreateSupplicantInterfaceProxy(
mukesh agrawal1830fa12011-09-26 14:31:40 -0700244 const WiFiRefPtr &/*wifi*/,
245 const DBus::Path &/*object_path*/,
246 const char */*dbus_addr*/) {
mukesh agrawaldc42bb32011-07-28 10:40:26 -0700247 return test_->supplicant_interface_proxy_.release();
mukesh agrawal31950242011-07-14 11:53:38 -0700248 }
249
mukesh agrawalb20776f2012-02-10 16:00:36 -0800250 MOCK_METHOD3(CreateSupplicantBSSProxy,
251 SupplicantBSSProxyInterface *(
252 WiFiEndpoint *wifi_endpoint,
253 const DBus::Path &object_path,
254 const char *dbus_addr));
255
Gary Morainac1bdb42012-02-16 17:42:29 -0800256 virtual PowerManagerProxyInterface *CreatePowerManagerProxy(
257 PowerManagerProxyDelegate */*delegate*/) {
258 return new MockPowerManagerProxy();
259 }
260
mukesh agrawal31950242011-07-14 11:53:38 -0700261 private:
mukesh agrawalb20776f2012-02-10 16:00:36 -0800262 SupplicantBSSProxyInterface *CreateSupplicantBSSProxyInternal(
263 WiFiEndpoint */*wifi_endpoint*/,
264 const DBus::Path &/*object_path*/,
265 const char */*dbus_addr*/) {
266 return test_->supplicant_bss_proxy_.release();
267 }
268
mukesh agrawaldc42bb32011-07-28 10:40:26 -0700269 WiFiMainTest *test_;
mukesh agrawal31950242011-07-14 11:53:38 -0700270 };
271
mukesh agrawal15908392011-11-16 18:29:25 +0000272 WiFiServiceRefPtr CreateServiceForEndpoint(const WiFiEndpoint &endpoint) {
273 bool hidden_ssid = false;
274 return wifi_->CreateServiceForEndpoint(endpoint, hidden_ssid);
275 }
276 const WiFiServiceRefPtr &GetCurrentService() {
277 return wifi_->current_service_;
278 }
mukesh agrawal31950242011-07-14 11:53:38 -0700279 const WiFi::EndpointMap &GetEndpointMap() {
mukesh agrawal165e6142011-11-22 02:22:56 +0000280 return wifi_->endpoint_by_rpcid_;
mukesh agrawal31950242011-07-14 11:53:38 -0700281 }
mukesh agrawal15908392011-11-16 18:29:25 +0000282 const WiFiServiceRefPtr &GetPendingService() {
283 return wifi_->pending_service_;
284 }
mukesh agrawal165e6142011-11-22 02:22:56 +0000285 const vector<WiFiServiceRefPtr> &GetServices() {
286 return wifi_->services_;
mukesh agrawal31950242011-07-14 11:53:38 -0700287 }
288 // note: the tests need the proxies referenced by WiFi (not the
289 // proxies instantiated by WiFiMainTest), to ensure that WiFi
290 // sets up its proxies correctly.
291 SupplicantProcessProxyInterface *GetSupplicantProcessProxy() {
292 return wifi_->supplicant_process_proxy_.get();
293 }
mukesh agrawal8a3188d2011-12-01 20:56:44 +0000294 MockSupplicantInterfaceProxy *GetSupplicantInterfaceProxy() {
295 return dynamic_cast<MockSupplicantInterfaceProxy *>(
296 wifi_->supplicant_interface_proxy_.get());
mukesh agrawal31950242011-07-14 11:53:38 -0700297 }
mukesh agrawal7ec71312011-11-10 02:08:26 +0000298 const string &GetSupplicantState() {
299 return wifi_->supplicant_state_;
300 }
301 void InitiateConnect(WiFiServiceRefPtr service) {
mukesh agrawal6e277772011-09-29 15:04:23 -0700302 map<string, ::DBus::Variant> params;
303 wifi_->ConnectTo(service, params);
mukesh agrawal31950242011-07-14 11:53:38 -0700304 }
mukesh agrawal0ed0f2e2011-12-05 20:36:17 +0000305 void InitiateDisconnect(WiFiServiceRefPtr service) {
306 wifi_->DisconnectFrom(service);
307 }
mukesh agrawal15908392011-11-16 18:29:25 +0000308 WiFiEndpointRefPtr MakeEndpoint(const string &ssid, const string &bssid) {
mukesh agrawale1d90e92012-02-15 17:36:08 -0800309 return WiFiEndpoint::MakeOpenEndpoint(
310 &proxy_factory_, NULL, ssid, bssid, 0, 0);
mukesh agrawal15908392011-11-16 18:29:25 +0000311 }
mukesh agrawal7ec71312011-11-10 02:08:26 +0000312 MockWiFiServiceRefPtr MakeMockService() {
313 vector<uint8_t> ssid(1, 'a');
314 return new MockWiFiService(
315 &control_interface_,
316 &dispatcher_,
Thieu Le3426c8f2012-01-11 17:35:11 -0800317 &metrics_,
mukesh agrawal7ec71312011-11-10 02:08:26 +0000318 &manager_,
319 wifi_,
320 ssid,
321 flimflam::kModeManaged,
322 flimflam::kSecurityNone,
323 false);
324 }
mukesh agrawal261daca2011-12-02 18:56:56 +0000325 void RemoveBSS(const ::DBus::Path &bss_path);
mukesh agrawal31950242011-07-14 11:53:38 -0700326 void ReportBSS(const ::DBus::Path &bss_path,
327 const string &ssid,
328 const string &bssid,
329 int16_t signal_strength,
Thieu Lee41a72d2012-02-06 20:46:51 +0000330 uint16 frequency,
mukesh agrawal31950242011-07-14 11:53:38 -0700331 const char *mode);
Paul Stewart66c86002012-01-30 18:00:52 -0800332 void ClearCachedCredentials() {
333 wifi_->ClearCachedCredentials();
334 }
mukesh agrawalc01f3982012-01-24 13:48:39 -0800335 void ReportIPConfigComplete() {
Darin Petkov79d74c92012-03-07 17:20:32 +0100336 wifi_->OnIPConfigUpdated(dhcp_config_, true);
mukesh agrawalc01f3982012-01-24 13:48:39 -0800337 }
mukesh agrawalf2f68a52011-09-01 12:15:48 -0700338 void ReportLinkUp() {
339 wifi_->LinkEvent(IFF_LOWER_UP, IFF_LOWER_UP);
340 }
mukesh agrawal31950242011-07-14 11:53:38 -0700341 void ReportScanDone() {
mukesh agrawaldc42bb32011-07-28 10:40:26 -0700342 wifi_->ScanDoneTask();
mukesh agrawal31950242011-07-14 11:53:38 -0700343 }
mukesh agrawal15908392011-11-16 18:29:25 +0000344 void ReportCurrentBSSChanged(const string &new_bss) {
345 wifi_->CurrentBSSChanged(new_bss);
346 }
mukesh agrawal7ec71312011-11-10 02:08:26 +0000347 void ReportStateChanged(const string &new_state) {
348 wifi_->StateChanged(new_state);
349 }
mukesh agrawal31950242011-07-14 11:53:38 -0700350 void StartWiFi() {
Gary Morainac1bdb42012-02-16 17:42:29 -0800351 EXPECT_CALL(*power_manager_, AddStateChangeCallback(wifi_->UniqueName(), _))
352 .WillOnce(SaveArg<1>(&power_state_callback_));
Eric Shienbrood9a245532012-03-07 14:20:39 -0500353 wifi_->Start(NULL, ResultCallback());
mukesh agrawal31950242011-07-14 11:53:38 -0700354 }
355 void StopWiFi() {
Gary Morain91001122012-02-29 16:22:26 -0800356 EXPECT_CALL(*power_manager_,
357 RemoveStateChangeCallback(wifi_->UniqueName()));
Eric Shienbrood9a245532012-03-07 14:20:39 -0500358 wifi_->Stop(NULL, ResultCallback());
mukesh agrawal31950242011-07-14 11:53:38 -0700359 }
Gary Morainac1bdb42012-02-16 17:42:29 -0800360 void GetOpenService(const char *service_type,
mukesh agrawal7a4e4002011-09-06 11:26:05 -0700361 const char *ssid,
362 const char *mode,
Paul Stewartced6a0b2011-11-08 15:32:04 -0800363 Error *result) {
364 GetServiceInner(service_type, ssid, mode, NULL, NULL, false, result);
mukesh agrawal7a4e4002011-09-06 11:26:05 -0700365 }
366 void GetService(const char *service_type,
367 const char *ssid,
368 const char *mode,
369 const char *security,
370 const char *passphrase,
Paul Stewartced6a0b2011-11-08 15:32:04 -0800371 Error *result) {
372 GetServiceInner(service_type, ssid, mode, security, passphrase, false,
373 result);
374 }
375 WiFiServiceRefPtr GetServiceInner(const char *service_type,
376 const char *ssid,
377 const char *mode,
378 const char *security,
379 const char *passphrase,
Paul Stewarta41e38d2011-11-11 07:47:29 -0800380 bool allow_hidden,
Paul Stewartced6a0b2011-11-08 15:32:04 -0800381 Error *result) {
mukesh agrawal7a4e4002011-09-06 11:26:05 -0700382 map<string, ::DBus::Variant> args;
mukesh agrawal7a4e4002011-09-06 11:26:05 -0700383 // in general, we want to avoid D-Bus specific code for any RPCs
384 // that come in via adaptors. we make an exception here, because
385 // calls to GetWifiService are rerouted from the Manager object to
386 // the Wifi class.
387 if (service_type != NULL)
388 args[flimflam::kTypeProperty].writer().append_string(service_type);
389 if (ssid != NULL)
390 args[flimflam::kSSIDProperty].writer().append_string(ssid);
391 if (mode != NULL)
392 args[flimflam::kModeProperty].writer().append_string(mode);
393 if (security != NULL)
394 args[flimflam::kSecurityProperty].writer().append_string(security);
395 if (passphrase != NULL)
396 args[flimflam::kPassphraseProperty].writer().append_string(passphrase);
Paul Stewarta41e38d2011-11-11 07:47:29 -0800397 if (!allow_hidden)
398 args[flimflam::kWifiHiddenSsid].writer().append_bool(false);
mukesh agrawal7a4e4002011-09-06 11:26:05 -0700399
Paul Stewartced6a0b2011-11-08 15:32:04 -0800400 Error e;
401 KeyValueStore args_kv;
mukesh agrawal7a4e4002011-09-06 11:26:05 -0700402 DBusAdaptor::ArgsToKeyValueStore(args, &args_kv, &e);
Paul Stewartced6a0b2011-11-08 15:32:04 -0800403 return wifi_->GetService(args_kv, result);
mukesh agrawal7a4e4002011-09-06 11:26:05 -0700404 }
Paul Stewartcb59fed2012-03-21 21:14:46 -0700405
406 WiFiServiceRefPtr GetServiceWithKeyValues(const KeyValueStore &args,
407 Error *result) {
408 return wifi_->GetService(args, result);
409 }
410
Paul Stewarta41e38d2011-11-11 07:47:29 -0800411 WiFiServiceRefPtr FindService(const vector<uint8_t> &ssid,
412 const string &mode,
413 const string &security) {
414 return wifi_->FindService(ssid, mode, security);
415 }
416 bool LoadHiddenServices(StoreInterface *storage) {
417 return wifi_->LoadHiddenServices(storage);
418 }
419 void SetupHiddenStorage(MockStore *storage, const string &ssid, string *id) {
420 const string hex_ssid = base::HexEncode(ssid.data(), ssid.size());
421 *id = StringToLowerASCII(base::StringPrintf("%s_%s_%s_%s_%s",
422 flimflam::kTypeWifi,
423 kDeviceAddress,
424 hex_ssid.c_str(),
425 flimflam::kModeManaged,
426 flimflam::kSecurityNone));
427 const char *groups[] = { id->c_str() };
428 EXPECT_CALL(*storage, GetGroupsWithKey(flimflam::kWifiHiddenSsid))
429 .WillRepeatedly(Return(set<string>(groups, groups + 1)));
430 EXPECT_CALL(*storage, GetBool(StrEq(*id), flimflam::kWifiHiddenSsid, _))
431 .WillRepeatedly(DoAll(SetArgumentPointee<2>(true), Return(true)));
432 EXPECT_CALL(*storage, GetString(StrEq(*id), flimflam::kSSIDProperty, _))
433 .WillRepeatedly(DoAll(SetArgumentPointee<2>(hex_ssid), Return(true)));
434 }
Paul Stewart10ccbb32012-04-26 15:59:30 -0700435
436 NiceMockControl *control_interface() {
437 return &control_interface_;
438 }
439
mukesh agrawal32399322011-09-01 10:53:43 -0700440 MockManager *manager() {
441 return &manager_;
442 }
Gary Moraind9f893f2012-02-06 10:03:40 -0800443
444 MockDeviceInfo *device_info() {
445 return &device_info_;
446 }
447
448 MockDHCPProvider *dhcp_provider() {
449 return &dhcp_provider_;
450 }
451
Eric Shienbrood3e20a232012-02-16 11:35:56 -0500452 const PowerManager::PowerStateCallback &power_state_callback() const {
Gary Morainac1bdb42012-02-16 17:42:29 -0800453 return power_state_callback_;
454 }
455
mukesh agrawal32399322011-09-01 10:53:43 -0700456 const WiFiConstRefPtr wifi() const {
457 return wifi_;
458 }
459
mukesh agrawalb20776f2012-02-10 16:00:36 -0800460 TestProxyFactory *proxy_factory() {
461 return &proxy_factory_;
462 }
463
mukesh agrawal32399322011-09-01 10:53:43 -0700464 EventDispatcher dispatcher_;
mukesh agrawal5c4dd0b2011-09-14 13:53:14 -0700465 NiceMock<MockRTNLHandler> rtnl_handler_;
mukesh agrawal5c05b292012-03-07 10:12:52 -0800466 MockTime time_;
mukesh agrawal31950242011-07-14 11:53:38 -0700467
468 private:
mukesh agrawaldc42bb32011-07-28 10:40:26 -0700469 NiceMockControl control_interface_;
Thieu Le3426c8f2012-01-11 17:35:11 -0800470 MockMetrics metrics_;
mukesh agrawalf2f68a52011-09-01 12:15:48 -0700471 MockGLib glib_;
Chris Masone2ae797d2011-08-23 20:41:00 -0700472 MockManager manager_;
mukesh agrawalc01f3982012-01-24 13:48:39 -0800473 MockDeviceInfo device_info_;
mukesh agrawal31950242011-07-14 11:53:38 -0700474 WiFiRefPtr wifi_;
mukesh agrawal31950242011-07-14 11:53:38 -0700475
476 // protected fields interspersed between private fields, due to
477 // initialization order
478 protected:
479 static const char kDeviceName[];
Chris Masone626719f2011-08-18 16:58:48 -0700480 static const char kDeviceAddress[];
Paul Stewartd32f4842012-01-11 16:08:13 -0800481 static const char kHostName[];
mukesh agrawal31950242011-07-14 11:53:38 -0700482 static const char kNetworkModeAdHoc[];
483 static const char kNetworkModeInfrastructure[];
484
mukesh agrawaldc42bb32011-07-28 10:40:26 -0700485 scoped_ptr<MockSupplicantProcessProxy> supplicant_process_proxy_;
486 scoped_ptr<MockSupplicantInterfaceProxy> supplicant_interface_proxy_;
mukesh agrawalb20776f2012-02-10 16:00:36 -0800487 scoped_ptr<MockSupplicantBSSProxy> supplicant_bss_proxy_;
mukesh agrawalf2f68a52011-09-01 12:15:48 -0700488 MockDHCPProvider dhcp_provider_;
489 scoped_refptr<MockDHCPConfig> dhcp_config_;
mukesh agrawal31950242011-07-14 11:53:38 -0700490
491 private:
492 TestProxyFactory proxy_factory_;
Gary Morainac1bdb42012-02-16 17:42:29 -0800493 MockPowerManager *power_manager_;
Eric Shienbrood3e20a232012-02-16 11:35:56 -0500494 PowerManager::PowerStateCallback power_state_callback_;
mukesh agrawal31950242011-07-14 11:53:38 -0700495};
496
497const char WiFiMainTest::kDeviceName[] = "wlan0";
Paul Stewarta41e38d2011-11-11 07:47:29 -0800498const char WiFiMainTest::kDeviceAddress[] = "000102030405";
Paul Stewartd32f4842012-01-11 16:08:13 -0800499const char WiFiMainTest::kHostName[] = "hostname";
mukesh agrawal31950242011-07-14 11:53:38 -0700500const char WiFiMainTest::kNetworkModeAdHoc[] = "ad-hoc";
501const char WiFiMainTest::kNetworkModeInfrastructure[] = "infrastructure";
502
mukesh agrawal261daca2011-12-02 18:56:56 +0000503void WiFiMainTest::RemoveBSS(const ::DBus::Path &bss_path) {
mukesh agrawalb4bc57d2011-12-07 01:07:47 +0000504 wifi_->BSSRemovedTask(bss_path);
mukesh agrawal261daca2011-12-02 18:56:56 +0000505}
506
mukesh agrawal31950242011-07-14 11:53:38 -0700507void WiFiMainTest::ReportBSS(const ::DBus::Path &bss_path,
508 const string &ssid,
509 const string &bssid,
510 int16_t signal_strength,
Thieu Lee41a72d2012-02-06 20:46:51 +0000511 uint16 frequency,
mukesh agrawal31950242011-07-14 11:53:38 -0700512 const char *mode) {
513 map<string, ::DBus::Variant> bss_properties;
514
515 {
516 DBus::MessageIter writer(bss_properties["SSID"].writer());
517 writer << vector<uint8_t>(ssid.begin(), ssid.end());
518 }
519 {
520 string bssid_nosep;
521 vector<uint8_t> bssid_bytes;
522 RemoveChars(bssid, ":", &bssid_nosep);
523 base::HexStringToBytes(bssid_nosep, &bssid_bytes);
524
525 DBus::MessageIter writer(bss_properties["BSSID"].writer());
526 writer << bssid_bytes;
527 }
Thieu Lee41a72d2012-02-06 20:46:51 +0000528 bss_properties[wpa_supplicant::kBSSPropertySignal].writer().
529 append_int16(signal_strength);
530 bss_properties[wpa_supplicant::kBSSPropertyFrequency].writer().
531 append_uint16(frequency);
532 bss_properties[wpa_supplicant::kBSSPropertyMode].writer().append_string(mode);
mukesh agrawalb4bc57d2011-12-07 01:07:47 +0000533 wifi_->BSSAddedTask(bss_path, bss_properties);
mukesh agrawal31950242011-07-14 11:53:38 -0700534}
535
mukesh agrawalb20776f2012-02-10 16:00:36 -0800536WiFiMainTest::TestProxyFactory::TestProxyFactory(WiFiMainTest *test)
537 : test_(test) {
538 EXPECT_CALL(*this, CreateSupplicantBSSProxy(_, _, _)).Times(AnyNumber());
539 ON_CALL(*this, CreateSupplicantBSSProxy(_, _, _))
540 .WillByDefault(
541 Invoke(this, (&TestProxyFactory::CreateSupplicantBSSProxyInternal)));
542}
543
mukesh agrawal31950242011-07-14 11:53:38 -0700544TEST_F(WiFiMainTest, ProxiesSetUpDuringStart) {
545 EXPECT_TRUE(GetSupplicantProcessProxy() == NULL);
546 EXPECT_TRUE(GetSupplicantInterfaceProxy() == NULL);
547
548 StartWiFi();
549 EXPECT_FALSE(GetSupplicantProcessProxy() == NULL);
550 EXPECT_FALSE(GetSupplicantInterfaceProxy() == NULL);
551}
552
553TEST_F(WiFiMainTest, CleanStart) {
554 EXPECT_CALL(*supplicant_process_proxy_, CreateInterface(_));
555 EXPECT_CALL(*supplicant_process_proxy_, GetInterface(_))
556 .Times(AnyNumber())
557 .WillRepeatedly(Throw(
558 DBus::Error(
559 "fi.w1.wpa_supplicant1.InterfaceUnknown",
560 "test threw fi.w1.wpa_supplicant1.InterfaceUnknown")));
561 EXPECT_CALL(*supplicant_interface_proxy_, Scan(_));
562 StartWiFi();
mukesh agrawal32399322011-09-01 10:53:43 -0700563 dispatcher_.DispatchPendingEvents();
mukesh agrawal31950242011-07-14 11:53:38 -0700564}
565
566TEST_F(WiFiMainTest, Restart) {
567 EXPECT_CALL(*supplicant_process_proxy_, CreateInterface(_))
568 .Times(AnyNumber())
569 .WillRepeatedly(Throw(
570 DBus::Error(
571 "fi.w1.wpa_supplicant1.InterfaceExists",
572 "test thew fi.w1.wpa_supplicant1.InterfaceExists")));
573 EXPECT_CALL(*supplicant_process_proxy_, GetInterface(_));
574 EXPECT_CALL(*supplicant_interface_proxy_, Scan(_));
575 StartWiFi();
mukesh agrawal32399322011-09-01 10:53:43 -0700576 dispatcher_.DispatchPendingEvents();
mukesh agrawal31950242011-07-14 11:53:38 -0700577}
578
579TEST_F(WiFiMainTest, StartClearsState) {
580 EXPECT_CALL(*supplicant_interface_proxy_, RemoveAllNetworks());
581 EXPECT_CALL(*supplicant_interface_proxy_, FlushBSS(_));
582 StartWiFi();
583}
584
Gary Morainac1bdb42012-02-16 17:42:29 -0800585TEST_F(WiFiMainTest, PowerChangeToResumeStartsScanWhenIdle) {
586 EXPECT_CALL(*supplicant_interface_proxy_, Scan(_));
587 StartWiFi();
588 dispatcher_.DispatchPendingEvents();
589 Mock::VerifyAndClearExpectations(&supplicant_interface_proxy_);
mukesh agrawal5c05b292012-03-07 10:12:52 -0800590 ReportScanDone();
Eric Shienbrood3e20a232012-02-16 11:35:56 -0500591 ASSERT_FALSE(power_state_callback().is_null());
Gary Morainac1bdb42012-02-16 17:42:29 -0800592 ASSERT_TRUE(wifi()->IsIdle());
593 EXPECT_CALL(*GetSupplicantInterfaceProxy(), Scan(_));
Eric Shienbrood3e20a232012-02-16 11:35:56 -0500594 power_state_callback().Run(PowerManagerProxyDelegate::kOn);
Gary Morainac1bdb42012-02-16 17:42:29 -0800595 dispatcher_.DispatchPendingEvents();
596}
597
598TEST_F(WiFiMainTest, PowerChangeToSuspendDoesNotStartScan) {
599 EXPECT_CALL(*supplicant_interface_proxy_, Scan(_));
600 StartWiFi();
601 dispatcher_.DispatchPendingEvents();
602 Mock::VerifyAndClearExpectations(&supplicant_interface_proxy_);
Eric Shienbrood3e20a232012-02-16 11:35:56 -0500603 ASSERT_FALSE(power_state_callback().is_null());
Gary Morainac1bdb42012-02-16 17:42:29 -0800604 ASSERT_TRUE(wifi()->IsIdle());
605 EXPECT_CALL(*GetSupplicantInterfaceProxy(), Scan(_)).Times(0);
Eric Shienbrood3e20a232012-02-16 11:35:56 -0500606 power_state_callback().Run(PowerManagerProxyDelegate::kStandby);
Gary Morainac1bdb42012-02-16 17:42:29 -0800607 dispatcher_.DispatchPendingEvents();
Eric Shienbrood3e20a232012-02-16 11:35:56 -0500608 power_state_callback().Run(PowerManagerProxyDelegate::kMem);
Gary Morainac1bdb42012-02-16 17:42:29 -0800609 dispatcher_.DispatchPendingEvents();
Eric Shienbrood3e20a232012-02-16 11:35:56 -0500610 power_state_callback().Run(PowerManagerProxyDelegate::kDisk);
Gary Morainac1bdb42012-02-16 17:42:29 -0800611 dispatcher_.DispatchPendingEvents();
612}
613
614TEST_F(WiFiMainTest, PowerChangeDoesNotStartScanWhenNotIdle) {
615 EXPECT_CALL(*supplicant_interface_proxy_, Scan(_));
616 StartWiFi();
617
618 WiFiEndpointRefPtr ap = MakeEndpoint("an_ssid", "00:01:02:03:04:05");
619 WiFiServiceRefPtr service = CreateServiceForEndpoint(*ap);
620 Error error;
621 service->AddEndpoint(ap);
622 service->AutoConnect();
623 EXPECT_FALSE(wifi()->IsIdle());
624 dispatcher_.DispatchPendingEvents();
625 Mock::VerifyAndClearExpectations(&supplicant_interface_proxy_);
Eric Shienbrood3e20a232012-02-16 11:35:56 -0500626 ASSERT_FALSE(power_state_callback().is_null());
Gary Morainac1bdb42012-02-16 17:42:29 -0800627 ASSERT_FALSE(wifi()->IsIdle());
628 EXPECT_CALL(*GetSupplicantInterfaceProxy(), Scan(_)).Times(0);
Eric Shienbrood3e20a232012-02-16 11:35:56 -0500629 power_state_callback().Run(PowerManagerProxyDelegate::kOn);
Gary Morainac1bdb42012-02-16 17:42:29 -0800630 dispatcher_.DispatchPendingEvents();
631}
632
mukesh agrawal31950242011-07-14 11:53:38 -0700633TEST_F(WiFiMainTest, ScanResults) {
Gary Moraind9f893f2012-02-06 10:03:40 -0800634 EXPECT_CALL(*manager(), RegisterService(_)).Times(AnyNumber());
mukesh agrawal31950242011-07-14 11:53:38 -0700635 StartWiFi();
Thieu Lee41a72d2012-02-06 20:46:51 +0000636 ReportBSS("bss0", "ssid0", "00:00:00:00:00:00", 0, 0, kNetworkModeAdHoc);
mukesh agrawal31950242011-07-14 11:53:38 -0700637 ReportBSS(
Thieu Lee41a72d2012-02-06 20:46:51 +0000638 "bss1", "ssid1", "00:00:00:00:00:01", 1, 0, kNetworkModeInfrastructure);
mukesh agrawal31950242011-07-14 11:53:38 -0700639 ReportBSS(
Thieu Lee41a72d2012-02-06 20:46:51 +0000640 "bss2", "ssid2", "00:00:00:00:00:02", 2, 0, kNetworkModeInfrastructure);
mukesh agrawal31950242011-07-14 11:53:38 -0700641 ReportBSS(
Thieu Lee41a72d2012-02-06 20:46:51 +0000642 "bss3", "ssid3", "00:00:00:00:00:03", 3, 0, kNetworkModeInfrastructure);
643 const uint16 frequency = 2412;
644 ReportBSS("bss4", "ssid4", "00:00:00:00:00:04", 4, frequency,
645 kNetworkModeAdHoc);
646
647 const WiFi::EndpointMap &endpoints_by_rpcid = GetEndpointMap();
648 EXPECT_EQ(5, endpoints_by_rpcid.size());
649
650 WiFi::EndpointMap::const_iterator i;
651 WiFiEndpointRefPtr endpoint;
652 for (i = endpoints_by_rpcid.begin();
653 i != endpoints_by_rpcid.end();
654 ++i) {
655 if (i->second->bssid_string() == "00:00:00:00:00:04")
656 break;
657 }
658 ASSERT_TRUE(i != endpoints_by_rpcid.end());
659 EXPECT_EQ(4, i->second->signal_strength());
660 EXPECT_EQ(frequency, i->second->frequency());
661 EXPECT_EQ("adhoc", i->second->network_mode());
mukesh agrawal31950242011-07-14 11:53:38 -0700662}
663
664TEST_F(WiFiMainTest, ScanResultsWithUpdates) {
Gary Moraind9f893f2012-02-06 10:03:40 -0800665 EXPECT_CALL(*manager(), RegisterService(_)).Times(AnyNumber());
666 EXPECT_CALL(*manager(), HasService(_)).Times(AnyNumber());
667 EXPECT_CALL(*manager(), UpdateService(_)).Times(AnyNumber());
mukesh agrawal31950242011-07-14 11:53:38 -0700668 StartWiFi();
Thieu Lee41a72d2012-02-06 20:46:51 +0000669 ReportBSS("bss0", "ssid0", "00:00:00:00:00:00", 0, 0, kNetworkModeAdHoc);
mukesh agrawal31950242011-07-14 11:53:38 -0700670 ReportBSS(
Thieu Lee41a72d2012-02-06 20:46:51 +0000671 "bss1", "ssid1", "00:00:00:00:00:01", 1, 0, kNetworkModeInfrastructure);
mukesh agrawal31950242011-07-14 11:53:38 -0700672 ReportBSS(
Thieu Lee41a72d2012-02-06 20:46:51 +0000673 "bss2", "ssid2", "00:00:00:00:00:02", 2, 0, kNetworkModeInfrastructure);
mukesh agrawal31950242011-07-14 11:53:38 -0700674 ReportBSS(
Thieu Lee41a72d2012-02-06 20:46:51 +0000675 "bss1", "ssid1", "00:00:00:00:00:01", 3, 0, kNetworkModeInfrastructure);
676 ReportBSS("bss0", "ssid0", "00:00:00:00:00:00", 4, 0, kNetworkModeAdHoc);
mukesh agrawal165e6142011-11-22 02:22:56 +0000677
678 const WiFi::EndpointMap &endpoints_by_rpcid = GetEndpointMap();
679 EXPECT_EQ(3, endpoints_by_rpcid.size());
680
681 WiFi::EndpointMap::const_iterator i;
682 WiFiEndpointRefPtr endpoint;
683 for (i = endpoints_by_rpcid.begin();
684 i != endpoints_by_rpcid.end();
685 ++i) {
686 if (i->second->bssid_string() == "00:00:00:00:00:00")
687 break;
688 }
689 ASSERT_TRUE(i != endpoints_by_rpcid.end());
690 EXPECT_EQ(4, i->second->signal_strength());
mukesh agrawal31950242011-07-14 11:53:38 -0700691}
692
693TEST_F(WiFiMainTest, ScanCompleted) {
694 StartWiFi();
mukesh agrawal261daca2011-12-02 18:56:56 +0000695 EXPECT_CALL(*manager(), RegisterService(_))
696 .Times(3);
Thieu Lee41a72d2012-02-06 20:46:51 +0000697 ReportBSS("bss0", "ssid0", "00:00:00:00:00:00", 0, 0, kNetworkModeAdHoc);
mukesh agrawal31950242011-07-14 11:53:38 -0700698 ReportBSS(
Thieu Lee41a72d2012-02-06 20:46:51 +0000699 "bss1", "ssid1", "00:00:00:00:00:01", 1, 0, kNetworkModeInfrastructure);
mukesh agrawal31950242011-07-14 11:53:38 -0700700 ReportBSS(
Thieu Lee41a72d2012-02-06 20:46:51 +0000701 "bss2", "ssid2", "00:00:00:00:00:02", 2, 0, kNetworkModeInfrastructure);
mukesh agrawal31950242011-07-14 11:53:38 -0700702 ReportScanDone();
mukesh agrawal165e6142011-11-22 02:22:56 +0000703 EXPECT_EQ(3, GetServices().size());
mukesh agrawalb3857612012-01-18 16:23:29 -0800704
mukesh agrawale9adda12012-02-09 18:33:48 -0800705 // BSSes with SSIDs that start with NULL should be filtered.
Thieu Lee41a72d2012-02-06 20:46:51 +0000706 ReportBSS("bss3", string(1, 0), "00:00:00:00:00:03", 3, 0, kNetworkModeAdHoc);
mukesh agrawale9adda12012-02-09 18:33:48 -0800707 EXPECT_EQ(3, GetEndpointMap().size());
mukesh agrawalb3857612012-01-18 16:23:29 -0800708 EXPECT_EQ(3, GetServices().size());
709
mukesh agrawale9adda12012-02-09 18:33:48 -0800710 // BSSes with empty SSIDs should be filtered.
711 ReportBSS("bss3", string(), "00:00:00:00:00:03", 3, 0, kNetworkModeAdHoc);
712 EXPECT_EQ(3, GetEndpointMap().size());
713 EXPECT_EQ(3, GetServices().size());
mukesh agrawal165e6142011-11-22 02:22:56 +0000714}
715
716TEST_F(WiFiMainTest, EndpointGroupingTogether) {
717 StartWiFi();
mukesh agrawal261daca2011-12-02 18:56:56 +0000718
719 InSequence s;
720 EXPECT_CALL(*manager(), RegisterService(_));
Gary Moraind9f893f2012-02-06 10:03:40 -0800721 EXPECT_CALL(*manager(), HasService(_));
mukesh agrawal261daca2011-12-02 18:56:56 +0000722 EXPECT_CALL(*manager(), UpdateService(_));
Thieu Lee41a72d2012-02-06 20:46:51 +0000723 ReportBSS("bss0", "ssid", "00:00:00:00:00:00", 0, 0, kNetworkModeAdHoc);
724 ReportBSS("bss1", "ssid", "00:00:00:00:00:01", 0, 0, kNetworkModeAdHoc);
mukesh agrawal165e6142011-11-22 02:22:56 +0000725 ReportScanDone();
726 EXPECT_EQ(1, GetServices().size());
727}
728
729TEST_F(WiFiMainTest, EndpointGroupingDifferentSSID) {
730 StartWiFi();
mukesh agrawal261daca2011-12-02 18:56:56 +0000731 EXPECT_CALL(*manager(), RegisterService(_))
732 .Times(2);
Thieu Lee41a72d2012-02-06 20:46:51 +0000733 ReportBSS("bss0", "ssid1", "00:00:00:00:00:00", 0, 0, kNetworkModeAdHoc);
734 ReportBSS("bss1", "ssid2", "00:00:00:00:00:01", 0, 0, kNetworkModeAdHoc);
mukesh agrawal165e6142011-11-22 02:22:56 +0000735 ReportScanDone();
736 EXPECT_EQ(2, GetServices().size());
737}
738
739TEST_F(WiFiMainTest, EndpointGroupingDifferentMode) {
740 StartWiFi();
mukesh agrawal261daca2011-12-02 18:56:56 +0000741 EXPECT_CALL(*manager(), RegisterService(_))
742 .Times(2);
Thieu Lee41a72d2012-02-06 20:46:51 +0000743 ReportBSS("bss0", "ssid", "00:00:00:00:00:00", 0, 0, kNetworkModeAdHoc);
744 ReportBSS("bss1", "ssid", "00:00:00:00:00:01", 0, 0,
745 kNetworkModeInfrastructure);
mukesh agrawal165e6142011-11-22 02:22:56 +0000746 ReportScanDone();
747 EXPECT_EQ(2, GetServices().size());
mukesh agrawal31950242011-07-14 11:53:38 -0700748}
749
mukesh agrawal261daca2011-12-02 18:56:56 +0000750TEST_F(WiFiMainTest, NonExistentBSSRemoved) {
751 // Removal of non-existent BSS should not cause a crash.
752 StartWiFi();
753 RemoveBSS("bss0");
754 EXPECT_EQ(0, GetServices().size());
755}
756
mukesh agrawale9adda12012-02-09 18:33:48 -0800757TEST_F(WiFiMainTest, BSSWithEmptySSIDRemoved) {
758 // Removal of BSS with an empty SSID should not cause a crash.
759 ReportBSS("bss", string(), "00:00:00:00:00:01", 0, 0, kNetworkModeAdHoc);
760 StartWiFi();
761 RemoveBSS("bss");
762 EXPECT_EQ(0, GetServices().size());
763}
764
765TEST_F(WiFiMainTest, BSSWithNullSSIDRemoved) {
766 // Removal of BSS with a NULL SSID should not cause a crash.
767 ReportBSS("bss", string(1, 0), "00:00:00:00:00:01", 0, 0, kNetworkModeAdHoc);
768 StartWiFi();
769 RemoveBSS("bss");
770 EXPECT_EQ(0, GetServices().size());
771}
772
mukesh agrawal261daca2011-12-02 18:56:56 +0000773TEST_F(WiFiMainTest, LoneBSSRemoved) {
Gary Moraind9f893f2012-02-06 10:03:40 -0800774 EXPECT_CALL(*manager(), RegisterService(_)).Times(AnyNumber());
mukesh agrawal261daca2011-12-02 18:56:56 +0000775 StartWiFi();
Thieu Lee41a72d2012-02-06 20:46:51 +0000776 ReportBSS("bss0", "ssid", "00:00:00:00:00:00", 0, 0, kNetworkModeAdHoc);
mukesh agrawal261daca2011-12-02 18:56:56 +0000777 ReportScanDone();
778 EXPECT_EQ(1, GetServices().size());
779 EXPECT_TRUE(GetServices().front()->IsVisible());
780
mukesh agrawal8a3188d2011-12-01 20:56:44 +0000781 EXPECT_CALL(*manager(), DeregisterService(_));
mukesh agrawal261daca2011-12-02 18:56:56 +0000782 RemoveBSS("bss0");
mukesh agrawal8a3188d2011-12-01 20:56:44 +0000783 EXPECT_TRUE(GetServices().empty());
784}
785
786TEST_F(WiFiMainTest, LoneBSSRemovedWhileConnected) {
Gary Moraind9f893f2012-02-06 10:03:40 -0800787 EXPECT_CALL(*manager(), RegisterService(_)).Times(AnyNumber());
mukesh agrawal8a3188d2011-12-01 20:56:44 +0000788 StartWiFi();
Thieu Lee41a72d2012-02-06 20:46:51 +0000789 ReportBSS("bss0", "ssid", "00:00:00:00:00:00", 0, 0, kNetworkModeAdHoc);
mukesh agrawal8a3188d2011-12-01 20:56:44 +0000790 ReportScanDone();
791 ReportCurrentBSSChanged("bss0");
792
793 EXPECT_CALL(*GetSupplicantInterfaceProxy(), Disconnect());
794 EXPECT_CALL(*manager(), DeregisterService(_));
795 RemoveBSS("bss0");
796 EXPECT_TRUE(GetServices().empty());
797}
798
799TEST_F(WiFiMainTest, LoneBSSRemovedWhileConnectedToHidden) {
Gary Moraind9f893f2012-02-06 10:03:40 -0800800 EXPECT_CALL(*manager(), UpdateService(_)).Times(AnyNumber());
801 EXPECT_CALL(*manager(), HasService(_)).Times(AnyNumber());
mukesh agrawal8a3188d2011-12-01 20:56:44 +0000802 StartWiFi();
803
804 Error e;
805 WiFiServiceRefPtr service =
806 GetServiceInner(flimflam::kTypeWifi, "ssid", flimflam::kModeManaged,
807 NULL, NULL, true, &e);
mukesh agrawal261daca2011-12-02 18:56:56 +0000808 EXPECT_EQ(1, GetServices().size());
mukesh agrawal8a3188d2011-12-01 20:56:44 +0000809
Thieu Lee41a72d2012-02-06 20:46:51 +0000810 ReportBSS("bss", "ssid", "00:00:00:00:00:01", 0, 0,
811 kNetworkModeInfrastructure);
mukesh agrawal8a3188d2011-12-01 20:56:44 +0000812 ReportScanDone();
813 ReportCurrentBSSChanged("bss");
814 EXPECT_EQ(1, GetServices().size());
815
816 EXPECT_CALL(*GetSupplicantInterfaceProxy(), Disconnect());
mukesh agrawal8a3188d2011-12-01 20:56:44 +0000817 RemoveBSS("bss");
mukesh agrawal8a3188d2011-12-01 20:56:44 +0000818 EXPECT_EQ(1, GetServices().size());
819 // Verify expectations now, because WiFi may call UpdateService when
820 // WiFi is Stop()-ed (during TearDown()).
821 Mock::VerifyAndClearExpectations(manager());
Gary Moraind9f893f2012-02-06 10:03:40 -0800822 EXPECT_CALL(*manager(), DeregisterService(_)).Times(AnyNumber());
mukesh agrawal261daca2011-12-02 18:56:56 +0000823}
824
825TEST_F(WiFiMainTest, NonSolitaryBSSRemoved) {
Gary Moraind9f893f2012-02-06 10:03:40 -0800826 EXPECT_CALL(*manager(), RegisterService(_));
827 EXPECT_CALL(*manager(), HasService(_));
828 EXPECT_CALL(*manager(), UpdateService(_));
mukesh agrawal261daca2011-12-02 18:56:56 +0000829 StartWiFi();
Thieu Lee41a72d2012-02-06 20:46:51 +0000830 ReportBSS("bss0", "ssid", "00:00:00:00:00:00", 0, 0, kNetworkModeAdHoc);
831 ReportBSS("bss1", "ssid", "00:00:00:00:00:00", 0, 0, kNetworkModeAdHoc);
mukesh agrawal261daca2011-12-02 18:56:56 +0000832 ReportScanDone();
833 EXPECT_EQ(1, GetServices().size());
834 EXPECT_TRUE(GetServices().front()->IsVisible());
835
836 EXPECT_CALL(*manager(), UpdateService(_));
837 RemoveBSS("bss0");
838 EXPECT_TRUE(GetServices().front()->IsVisible());
839 EXPECT_EQ(1, GetServices().size());
840}
841
mukesh agrawal31950242011-07-14 11:53:38 -0700842TEST_F(WiFiMainTest, Connect) {
Gary Moraind9f893f2012-02-06 10:03:40 -0800843 EXPECT_CALL(*manager(), RegisterService(_)).Times(AnyNumber());
mukesh agrawaldc42bb32011-07-28 10:40:26 -0700844 MockSupplicantInterfaceProxy &supplicant_interface_proxy =
845 *supplicant_interface_proxy_;
846
mukesh agrawal31950242011-07-14 11:53:38 -0700847 StartWiFi();
Thieu Lee41a72d2012-02-06 20:46:51 +0000848 ReportBSS("bss0", "ssid0", "00:00:00:00:00:00", 0, 0, kNetworkModeAdHoc);
mukesh agrawal31950242011-07-14 11:53:38 -0700849 ReportScanDone();
850
851 {
852 InSequence s;
853 DBus::Path fake_path("/fake/path");
mukesh agrawal165e6142011-11-22 02:22:56 +0000854 WiFiService *service(GetServices().begin()->get());
mukesh agrawal31950242011-07-14 11:53:38 -0700855
mukesh agrawaldc42bb32011-07-28 10:40:26 -0700856 EXPECT_CALL(supplicant_interface_proxy, AddNetwork(_))
mukesh agrawal31950242011-07-14 11:53:38 -0700857 .WillOnce(Return(fake_path));
mukesh agrawaldc42bb32011-07-28 10:40:26 -0700858 EXPECT_CALL(supplicant_interface_proxy, SelectNetwork(fake_path));
mukesh agrawal32399322011-09-01 10:53:43 -0700859 InitiateConnect(service);
mukesh agrawalf2f68a52011-09-01 12:15:48 -0700860 EXPECT_EQ(static_cast<Service *>(service),
861 wifi()->selected_service_.get());
mukesh agrawal31950242011-07-14 11:53:38 -0700862 }
863}
864
mukesh agrawal0ed0f2e2011-12-05 20:36:17 +0000865TEST_F(WiFiMainTest, DisconnectPendingService) {
Gary Moraind9f893f2012-02-06 10:03:40 -0800866 EXPECT_CALL(*manager(), RegisterService(_)).Times(AnyNumber());
mukesh agrawal0ed0f2e2011-12-05 20:36:17 +0000867 MockSupplicantInterfaceProxy &supplicant_interface_proxy =
868 *supplicant_interface_proxy_;
869
870 StartWiFi();
Thieu Lee41a72d2012-02-06 20:46:51 +0000871 ReportBSS("bss0", "ssid0", "00:00:00:00:00:00", 0, 0, kNetworkModeAdHoc);
mukesh agrawal0ed0f2e2011-12-05 20:36:17 +0000872 WiFiService *service(GetServices().begin()->get());
873 InitiateConnect(service);
874
875 EXPECT_FALSE(GetPendingService() == NULL);
876 EXPECT_CALL(supplicant_interface_proxy, Disconnect());
877 InitiateDisconnect(service);
878
879 EXPECT_TRUE(GetPendingService() == NULL);
880}
881
882TEST_F(WiFiMainTest, DisconnectPendingServiceWithCurrent) {
Gary Moraind9f893f2012-02-06 10:03:40 -0800883 EXPECT_CALL(*manager(), RegisterService(_)).Times(AnyNumber());
884 EXPECT_CALL(*dhcp_provider(), CreateConfig(_, _)).Times(AnyNumber());
885 EXPECT_CALL(*dhcp_config_.get(), RequestIP()).Times(AnyNumber());
886 EXPECT_CALL(*manager(), UpdateService(_)).Times(AnyNumber());
mukesh agrawal0ed0f2e2011-12-05 20:36:17 +0000887 MockSupplicantInterfaceProxy &supplicant_interface_proxy =
888 *supplicant_interface_proxy_;
889
890 StartWiFi();
Thieu Lee41a72d2012-02-06 20:46:51 +0000891 ReportBSS("bss0", "ssid0", "00:00:00:00:00:00", 0, 0, kNetworkModeAdHoc);
892 ReportBSS("bss1", "ssid1", "00:00:00:00:00:01", 0, 0, kNetworkModeAdHoc);
mukesh agrawal0ed0f2e2011-12-05 20:36:17 +0000893 WiFiService *service0(GetServices()[0].get());
894 WiFiService *service1(GetServices()[1].get());
895
896 InitiateConnect(service0);
897 ReportCurrentBSSChanged("bss0");
898 ReportStateChanged(wpa_supplicant::kInterfaceStateCompleted);
899 InitiateConnect(service1);
900
901 EXPECT_EQ(service0, GetCurrentService());
902 EXPECT_EQ(service1, GetPendingService());
903 EXPECT_CALL(supplicant_interface_proxy, Disconnect());
904 InitiateDisconnect(service1);
905
906 // |current_service_| will be unchanged until supplicant signals
907 // that CurrentBSS has changed.
908 EXPECT_EQ(service0, GetCurrentService());
909 // |pending_service_| is updated immediately.
910 EXPECT_TRUE(GetPendingService() == NULL);
911}
912
913TEST_F(WiFiMainTest, DisconnectCurrentService) {
Gary Moraind9f893f2012-02-06 10:03:40 -0800914 EXPECT_CALL(*manager(), RegisterService(_)).Times(AnyNumber());
915 EXPECT_CALL(*dhcp_provider(), CreateConfig(_, _)).Times(AnyNumber());
916 EXPECT_CALL(*dhcp_config_.get(), RequestIP()).Times(AnyNumber());
917 EXPECT_CALL(*manager(), UpdateService(_)).Times(AnyNumber());
mukesh agrawal0ed0f2e2011-12-05 20:36:17 +0000918 MockSupplicantInterfaceProxy &supplicant_interface_proxy =
919 *supplicant_interface_proxy_;
920
921 StartWiFi();
Thieu Lee41a72d2012-02-06 20:46:51 +0000922 ReportBSS("bss0", "ssid0", "00:00:00:00:00:00", 0, 0, kNetworkModeAdHoc);
mukesh agrawal0ed0f2e2011-12-05 20:36:17 +0000923 WiFiService *service(GetServices().begin()->get());
924 InitiateConnect(service);
925 ReportCurrentBSSChanged("bss0");
926 ReportStateChanged(wpa_supplicant::kInterfaceStateCompleted);
927
928 EXPECT_EQ(service, GetCurrentService());
929 EXPECT_CALL(supplicant_interface_proxy, Disconnect());
930 InitiateDisconnect(service);
931
932 // |current_service_| should not change until supplicant reports
933 // a BSS change.
934 EXPECT_EQ(service, GetCurrentService());
935}
936
937TEST_F(WiFiMainTest, DisconnectCurrentServiceWithPending) {
Gary Moraind9f893f2012-02-06 10:03:40 -0800938 EXPECT_CALL(*manager(), RegisterService(_)).Times(AnyNumber());
939 EXPECT_CALL(*dhcp_provider(), CreateConfig(_, _)).Times(AnyNumber());
940 EXPECT_CALL(*dhcp_config_.get(), RequestIP()).Times(AnyNumber());
941 EXPECT_CALL(*manager(), UpdateService(_)).Times(AnyNumber());
mukesh agrawal0ed0f2e2011-12-05 20:36:17 +0000942 MockSupplicantInterfaceProxy &supplicant_interface_proxy =
943 *supplicant_interface_proxy_;
944
945 StartWiFi();
Thieu Lee41a72d2012-02-06 20:46:51 +0000946 ReportBSS("bss0", "ssid0", "00:00:00:00:00:00", 0, 0, kNetworkModeAdHoc);
947 ReportBSS("bss1", "ssid1", "00:00:00:00:00:01", 0, 0, kNetworkModeAdHoc);
mukesh agrawal0ed0f2e2011-12-05 20:36:17 +0000948 WiFiService *service0(GetServices()[0].get());
949 WiFiService *service1(GetServices()[1].get());
950
951 InitiateConnect(service0);
952 ReportCurrentBSSChanged("bss0");
953 ReportStateChanged(wpa_supplicant::kInterfaceStateCompleted);
954 InitiateConnect(service1);
955
956 EXPECT_EQ(service0, GetCurrentService());
957 EXPECT_EQ(service1, GetPendingService());
958 EXPECT_CALL(supplicant_interface_proxy, Disconnect())
959 .Times(0);
960 InitiateDisconnect(service0);
961
962 EXPECT_EQ(service0, GetCurrentService());
963 EXPECT_EQ(service1, GetPendingService());
964}
965
966TEST_F(WiFiMainTest, DisconnectInvalidService) {
Gary Moraind9f893f2012-02-06 10:03:40 -0800967 EXPECT_CALL(*manager(), RegisterService(_)).Times(AnyNumber());
mukesh agrawal0ed0f2e2011-12-05 20:36:17 +0000968 MockSupplicantInterfaceProxy &supplicant_interface_proxy =
969 *supplicant_interface_proxy_;
970
971 StartWiFi();
Thieu Lee41a72d2012-02-06 20:46:51 +0000972 ReportBSS("bss0", "ssid0", "00:00:00:00:00:00", 0, 0, kNetworkModeAdHoc);
mukesh agrawal0ed0f2e2011-12-05 20:36:17 +0000973 WiFiService *service(GetServices().begin()->get());
974 EXPECT_CALL(supplicant_interface_proxy, Disconnect())
975 .Times(0);
976 InitiateDisconnect(service);
977}
978
979TEST_F(WiFiMainTest, DisconnectCurrentServiceFailure) {
Gary Moraind9f893f2012-02-06 10:03:40 -0800980 EXPECT_CALL(*manager(), RegisterService(_)).Times(AnyNumber());
981 EXPECT_CALL(*dhcp_provider(), CreateConfig(_, _)).Times(AnyNumber());
982 EXPECT_CALL(*dhcp_config_.get(), RequestIP()).Times(AnyNumber());
983 EXPECT_CALL(*manager(), UpdateService(_)).Times(AnyNumber());
mukesh agrawal0ed0f2e2011-12-05 20:36:17 +0000984 MockSupplicantInterfaceProxy &supplicant_interface_proxy =
985 *supplicant_interface_proxy_;
986
987 StartWiFi();
Thieu Lee41a72d2012-02-06 20:46:51 +0000988 ReportBSS("bss0", "ssid0", "00:00:00:00:00:00", 0, 0, kNetworkModeAdHoc);
mukesh agrawal0ed0f2e2011-12-05 20:36:17 +0000989
990 WiFiService *service(GetServices().begin()->get());
991 DBus::Path fake_path("/fake/path");
992 EXPECT_CALL(supplicant_interface_proxy, AddNetwork(_))
993 .WillOnce(Return(fake_path));
994 InitiateConnect(service);
995 ReportCurrentBSSChanged("bss0");
996 ReportStateChanged(wpa_supplicant::kInterfaceStateCompleted);
997
998 EXPECT_EQ(service, GetCurrentService());
999 EXPECT_CALL(supplicant_interface_proxy, Disconnect())
1000 .WillRepeatedly(Throw(
1001 DBus::Error(
1002 "fi.w1.wpa_supplicant1.NotConnected",
1003 "test threw fi.w1.wpa_supplicant1.NotConnected")));
1004 EXPECT_CALL(supplicant_interface_proxy, RemoveNetwork(fake_path));
1005 InitiateDisconnect(service);
1006
1007 EXPECT_TRUE(GetCurrentService() == NULL);
1008}
1009
mukesh agrawal5c4dd0b2011-09-14 13:53:14 -07001010TEST_F(WiFiMainTest, Stop) {
Gary Moraind9f893f2012-02-06 10:03:40 -08001011 EXPECT_CALL(*manager(), RegisterService(_)).Times(AnyNumber());
mukesh agrawalc01f3982012-01-24 13:48:39 -08001012 StartWiFi();
Thieu Lee41a72d2012-02-06 20:46:51 +00001013 ReportBSS("bss0", "ssid0", "00:00:00:00:00:00", 0, 0, kNetworkModeAdHoc);
mukesh agrawalc01f3982012-01-24 13:48:39 -08001014 ReportScanDone();
mukesh agrawal5c4dd0b2011-09-14 13:53:14 -07001015
mukesh agrawalc01f3982012-01-24 13:48:39 -08001016 EXPECT_CALL(*manager(), DeregisterService(_));
1017 StopWiFi();
mukesh agrawal5c4dd0b2011-09-14 13:53:14 -07001018}
1019
mukesh agrawal7a4e4002011-09-06 11:26:05 -07001020TEST_F(WiFiMainTest, GetWifiServiceOpen) {
1021 Error e;
Paul Stewarta41e38d2011-11-11 07:47:29 -08001022 GetOpenService(flimflam::kTypeWifi, "an_ssid", flimflam::kModeManaged, &e);
mukesh agrawal7a4e4002011-09-06 11:26:05 -07001023 EXPECT_TRUE(e.IsSuccess());
1024}
1025
mukesh agrawal7a4e4002011-09-06 11:26:05 -07001026TEST_F(WiFiMainTest, GetWifiServiceOpenNoSSID) {
1027 Error e;
Paul Stewarta41e38d2011-11-11 07:47:29 -08001028 GetOpenService(flimflam::kTypeWifi, NULL, flimflam::kModeManaged, &e);
mukesh agrawal7a4e4002011-09-06 11:26:05 -07001029 EXPECT_EQ(Error::kInvalidArguments, e.type());
1030 EXPECT_EQ("must specify SSID", e.message());
1031}
1032
1033TEST_F(WiFiMainTest, GetWifiServiceOpenLongSSID) {
1034 Error e;
1035 GetOpenService(
Paul Stewarta41e38d2011-11-11 07:47:29 -08001036 flimflam::kTypeWifi, "123456789012345678901234567890123",
1037 flimflam::kModeManaged, &e);
mukesh agrawal7a4e4002011-09-06 11:26:05 -07001038 EXPECT_EQ(Error::kInvalidNetworkName, e.type());
1039 EXPECT_EQ("SSID is too long", e.message());
1040}
1041
1042TEST_F(WiFiMainTest, GetWifiServiceOpenShortSSID) {
1043 Error e;
Paul Stewarta41e38d2011-11-11 07:47:29 -08001044 GetOpenService(flimflam::kTypeWifi, "", flimflam::kModeManaged, &e);
mukesh agrawal7a4e4002011-09-06 11:26:05 -07001045 EXPECT_EQ(Error::kInvalidNetworkName, e.type());
1046 EXPECT_EQ("SSID is too short", e.message());
1047}
1048
1049TEST_F(WiFiMainTest, GetWifiServiceOpenBadMode) {
1050 Error e;
Paul Stewarta41e38d2011-11-11 07:47:29 -08001051 GetOpenService(flimflam::kTypeWifi, "an_ssid", "ad-hoc", &e);
mukesh agrawal7a4e4002011-09-06 11:26:05 -07001052 EXPECT_EQ(Error::kNotSupported, e.type());
1053 EXPECT_EQ("service mode is unsupported", e.message());
1054}
1055
1056TEST_F(WiFiMainTest, GetWifiServiceOpenNoMode) {
1057 Error e;
Paul Stewarta41e38d2011-11-11 07:47:29 -08001058 GetOpenService(flimflam::kTypeWifi, "an_ssid", NULL, &e);
mukesh agrawal7a4e4002011-09-06 11:26:05 -07001059 EXPECT_TRUE(e.IsSuccess());
1060}
1061
1062TEST_F(WiFiMainTest, GetWifiServiceRSN) {
1063 Error e;
Paul Stewarta41e38d2011-11-11 07:47:29 -08001064 GetService(flimflam::kTypeWifi, "an_ssid", flimflam::kModeManaged,
1065 flimflam::kSecurityRsn, "secure password", &e);
mukesh agrawal7a4e4002011-09-06 11:26:05 -07001066 EXPECT_TRUE(e.IsSuccess());
1067}
1068
1069TEST_F(WiFiMainTest, GetWifiServiceRSNNoPassword) {
mukesh agrawalb172e4a2012-04-25 18:15:30 -07001070 // When configuring hidden networks, Chrome expects to be able to
1071 // GetService w/o a password, and supply the password with
1072 // SetProperty afterwards.
mukesh agrawal7a4e4002011-09-06 11:26:05 -07001073 Error e;
Paul Stewarta41e38d2011-11-11 07:47:29 -08001074 GetService(flimflam::kTypeWifi, "an_ssid", flimflam::kModeManaged,
1075 flimflam::kSecurityRsn, NULL, &e);
mukesh agrawalb172e4a2012-04-25 18:15:30 -07001076 EXPECT_TRUE(e.IsSuccess());
mukesh agrawal7a4e4002011-09-06 11:26:05 -07001077}
1078
1079TEST_F(WiFiMainTest, GetWifiServiceBadSecurity) {
1080 Error e;
Paul Stewarta41e38d2011-11-11 07:47:29 -08001081 GetService(flimflam::kTypeWifi, "an_ssid", flimflam::kModeManaged, "rot-13",
1082 NULL, &e);
mukesh agrawal7a4e4002011-09-06 11:26:05 -07001083 EXPECT_EQ(Error::kNotSupported, e.type());
1084 EXPECT_EQ("security mode is unsupported", e.message());
1085}
1086
1087TEST_F(WiFiMainTest, GetWifiServiceWEPNoPassword) {
mukesh agrawalb172e4a2012-04-25 18:15:30 -07001088 // When configuring hidden networks, Chrome expects to be able to
1089 // GetService w/o a password, and supply the password with
1090 // SetProperty afterwards.
mukesh agrawal7a4e4002011-09-06 11:26:05 -07001091 Error e;
Paul Stewarta41e38d2011-11-11 07:47:29 -08001092 GetService(flimflam::kTypeWifi, "an_ssid", flimflam::kModeManaged,
1093 flimflam::kSecurityWep, NULL, &e);
mukesh agrawalb172e4a2012-04-25 18:15:30 -07001094 EXPECT_TRUE(e.IsSuccess());
mukesh agrawal7a4e4002011-09-06 11:26:05 -07001095}
1096
1097TEST_F(WiFiMainTest, GetWifiServiceWEPEmptyPassword) {
1098 Error e;
Paul Stewarta41e38d2011-11-11 07:47:29 -08001099 GetService(flimflam::kTypeWifi, "an_ssid", flimflam::kModeManaged,
1100 flimflam::kSecurityWep, "", &e);
mukesh agrawal7a4e4002011-09-06 11:26:05 -07001101 EXPECT_EQ(Error::kInvalidPassphrase, e.type());
1102}
1103
1104TEST_F(WiFiMainTest, GetWifiServiceWEP40ASCII) {
1105 Error e;
Paul Stewarta41e38d2011-11-11 07:47:29 -08001106 GetService(flimflam::kTypeWifi, "an_ssid", flimflam::kModeManaged,
1107 flimflam::kSecurityWep, "abcde", &e);
mukesh agrawal7a4e4002011-09-06 11:26:05 -07001108 EXPECT_TRUE(e.IsSuccess());
1109}
1110
1111TEST_F(WiFiMainTest, GetWifiServiceWEP104ASCII) {
1112 Error e;
Paul Stewarta41e38d2011-11-11 07:47:29 -08001113 GetService(flimflam::kTypeWifi, "an_ssid", flimflam::kModeManaged,
1114 flimflam::kSecurityWep, "abcdefghijklm", &e);
mukesh agrawal7a4e4002011-09-06 11:26:05 -07001115 EXPECT_TRUE(e.IsSuccess());
1116}
1117
1118TEST_F(WiFiMainTest, GetWifiServiceWEP40ASCIIWithKeyIndex) {
1119 Error e;
Paul Stewarta41e38d2011-11-11 07:47:29 -08001120 GetService(flimflam::kTypeWifi, "an_ssid", flimflam::kModeManaged,
1121 flimflam::kSecurityWep, "0:abcdefghijklm", &e);
mukesh agrawal7a4e4002011-09-06 11:26:05 -07001122 EXPECT_TRUE(e.IsSuccess());
1123}
1124
1125TEST_F(WiFiMainTest, GetWifiServiceWEP40Hex) {
1126 Error e;
Paul Stewarta41e38d2011-11-11 07:47:29 -08001127 GetService(flimflam::kTypeWifi, "an_ssid", flimflam::kModeManaged,
1128 flimflam::kSecurityWep, "0102030405", &e);
mukesh agrawal7a4e4002011-09-06 11:26:05 -07001129 EXPECT_TRUE(e.IsSuccess());
1130}
1131
1132TEST_F(WiFiMainTest, GetWifiServiceWEP40HexBadPassphrase) {
1133 Error e;
Paul Stewarta41e38d2011-11-11 07:47:29 -08001134 GetService(flimflam::kTypeWifi, "an_ssid", flimflam::kModeManaged,
1135 flimflam::kSecurityWep, "O102030405", &e);
mukesh agrawal7a4e4002011-09-06 11:26:05 -07001136 EXPECT_EQ(Error::kInvalidPassphrase, e.type());
1137}
1138
1139TEST_F(WiFiMainTest, GetWifiServiceWEP40HexWithKeyIndexBadPassphrase) {
1140 Error e;
Paul Stewarta41e38d2011-11-11 07:47:29 -08001141 GetService(flimflam::kTypeWifi, "an_ssid", flimflam::kModeManaged,
1142 flimflam::kSecurityWep, "1:O102030405", &e);
mukesh agrawal7a4e4002011-09-06 11:26:05 -07001143 EXPECT_EQ(Error::kInvalidPassphrase, e.type());
1144}
1145
1146TEST_F(WiFiMainTest, GetWifiServiceWEP40HexWithKeyIndexAndBaseBadPassphrase) {
1147 Error e;
Paul Stewarta41e38d2011-11-11 07:47:29 -08001148 GetService(flimflam::kTypeWifi, "an_ssid", flimflam::kModeManaged,
1149 flimflam::kSecurityWep, "1:0xO102030405", &e);
mukesh agrawal7a4e4002011-09-06 11:26:05 -07001150 EXPECT_EQ(Error::kInvalidPassphrase, e.type());
1151}
1152
1153TEST_F(WiFiMainTest, GetWifiServiceWEP40HexWithBaseBadPassphrase) {
1154 Error e;
Paul Stewarta41e38d2011-11-11 07:47:29 -08001155 GetService(flimflam::kTypeWifi, "an_ssid", flimflam::kModeManaged,
1156 flimflam::kSecurityWep, "0xO102030405", &e);
mukesh agrawal7a4e4002011-09-06 11:26:05 -07001157 EXPECT_EQ(Error::kInvalidPassphrase, e.type());
1158}
1159
1160TEST_F(WiFiMainTest, GetWifiServiceWEP104Hex) {
1161 Error e;
Paul Stewarta41e38d2011-11-11 07:47:29 -08001162 GetService(flimflam::kTypeWifi, "an_ssid", flimflam::kModeManaged,
1163 flimflam::kSecurityWep, "0102030405060708090a0b0c0d", &e);
mukesh agrawal7a4e4002011-09-06 11:26:05 -07001164 EXPECT_TRUE(e.IsSuccess());
1165}
1166
1167TEST_F(WiFiMainTest, GetWifiServiceWEP104HexUppercase) {
1168 Error e;
Paul Stewarta41e38d2011-11-11 07:47:29 -08001169 GetService(flimflam::kTypeWifi, "an_ssid", flimflam::kModeManaged,
1170 flimflam::kSecurityWep, "0102030405060708090A0B0C0D", &e);
mukesh agrawal7a4e4002011-09-06 11:26:05 -07001171 EXPECT_TRUE(e.IsSuccess());
1172}
1173
1174TEST_F(WiFiMainTest, GetWifiServiceWEP104HexWithKeyIndex) {
1175 Error e;
Paul Stewarta41e38d2011-11-11 07:47:29 -08001176 GetService(flimflam::kTypeWifi, "an_ssid", flimflam::kModeManaged,
1177 flimflam::kSecurityWep, "0:0102030405060708090a0b0c0d", &e);
mukesh agrawal7a4e4002011-09-06 11:26:05 -07001178 EXPECT_TRUE(e.IsSuccess());
1179}
1180
1181TEST_F(WiFiMainTest, GetWifiServiceWEP104HexWithKeyIndexAndBase) {
1182 Error e;
Paul Stewarta41e38d2011-11-11 07:47:29 -08001183 GetService(flimflam::kTypeWifi, "an_ssid", flimflam::kModeManaged,
1184 flimflam::kSecurityWep, "0:0x0102030405060708090a0b0c0d", &e);
mukesh agrawal7a4e4002011-09-06 11:26:05 -07001185 EXPECT_TRUE(e.IsSuccess());
1186}
1187
mukesh agrawal8ede0522011-10-03 14:57:44 -07001188class WiFiGetServiceSuccessTest : public WiFiMainTest {};
1189class WiFiGetServiceFailureTest : public WiFiMainTest {};
1190
1191TEST_P(WiFiGetServiceSuccessTest, Passphrase) {
1192 Error e;
Paul Stewarta41e38d2011-11-11 07:47:29 -08001193 GetService(flimflam::kTypeWifi, "an_ssid", flimflam::kModeManaged,
1194 flimflam::kSecurityWpa, GetParam().c_str(), &e);
mukesh agrawal8ede0522011-10-03 14:57:44 -07001195 EXPECT_TRUE(e.IsSuccess());
1196}
1197
1198TEST_P(WiFiGetServiceFailureTest, Passphrase) {
1199 Error e;
Paul Stewarta41e38d2011-11-11 07:47:29 -08001200 GetService(flimflam::kTypeWifi, "an_ssid", flimflam::kModeManaged,
1201 flimflam::kSecurityWpa, GetParam().c_str(), &e);
mukesh agrawal8ede0522011-10-03 14:57:44 -07001202 EXPECT_EQ(Error::kInvalidPassphrase, e.type());
1203}
1204
1205INSTANTIATE_TEST_CASE_P(
1206 WiFiGetServiceSuccessTestInstance,
1207 WiFiGetServiceSuccessTest,
1208 Values(
1209 string(IEEE_80211::kWPAAsciiMinLen, 'Z'),
1210 string(IEEE_80211::kWPAAsciiMaxLen, 'Z'),
1211 // subtle: invalid length for hex key, but valid as ascii passphrase
1212 string(IEEE_80211::kWPAHexLen-1, '1'),
1213 string(IEEE_80211::kWPAHexLen, '1')));
1214
1215INSTANTIATE_TEST_CASE_P(
1216 WiFiGetServiceFailureTestInstance,
1217 WiFiGetServiceFailureTest,
1218 Values(
1219 string(IEEE_80211::kWPAAsciiMinLen-1, 'Z'),
1220 string(IEEE_80211::kWPAAsciiMaxLen+1, 'Z'),
1221 string(IEEE_80211::kWPAHexLen+1, '1')));
1222
Paul Stewart6ab23a92011-11-09 17:17:47 -08001223TEST_F(WiFiMainTest, FindServiceWEP) {
1224 const string ssid("an_ssid");
1225 {
1226 Error e;
Paul Stewarta41e38d2011-11-11 07:47:29 -08001227 GetService(flimflam::kTypeWifi, ssid.c_str(), flimflam::kModeManaged,
Paul Stewart6ab23a92011-11-09 17:17:47 -08001228 flimflam::kSecurityWep, "abcde", &e);
1229 EXPECT_TRUE(e.IsSuccess());
1230 }
1231 vector<uint8_t> ssid_bytes(ssid.begin(), ssid.end());
1232
Paul Stewarta41e38d2011-11-11 07:47:29 -08001233 EXPECT_TRUE(FindService(ssid_bytes, flimflam::kModeManaged,
1234 flimflam::kSecurityWep).get());
1235 EXPECT_FALSE(FindService(ssid_bytes, flimflam::kModeManaged,
1236 flimflam::kSecurityWpa).get());
Paul Stewart6ab23a92011-11-09 17:17:47 -08001237}
1238
1239TEST_F(WiFiMainTest, FindServiceWPA) {
1240 const string ssid("an_ssid");
1241 {
1242 Error e;
Paul Stewarta41e38d2011-11-11 07:47:29 -08001243 GetService(flimflam::kTypeWifi, ssid.c_str(), flimflam::kModeManaged,
Paul Stewart6ab23a92011-11-09 17:17:47 -08001244 flimflam::kSecurityRsn, "abcdefgh", &e);
1245 EXPECT_TRUE(e.IsSuccess());
1246 }
1247 vector<uint8_t> ssid_bytes(ssid.begin(), ssid.end());
Paul Stewarta41e38d2011-11-11 07:47:29 -08001248 WiFiServiceRefPtr wpa_service(FindService(ssid_bytes, flimflam::kModeManaged,
1249 flimflam::kSecurityWpa));
Paul Stewart6ab23a92011-11-09 17:17:47 -08001250 EXPECT_TRUE(wpa_service.get());
Paul Stewarta41e38d2011-11-11 07:47:29 -08001251 WiFiServiceRefPtr rsn_service(FindService(ssid_bytes, flimflam::kModeManaged,
1252 flimflam::kSecurityRsn));
Paul Stewart6ab23a92011-11-09 17:17:47 -08001253 EXPECT_TRUE(rsn_service.get());
1254 EXPECT_EQ(wpa_service.get(), rsn_service.get());
Paul Stewarta41e38d2011-11-11 07:47:29 -08001255 WiFiServiceRefPtr psk_service(FindService(ssid_bytes, flimflam::kModeManaged,
1256 flimflam::kSecurityPsk));
Paul Stewart6ab23a92011-11-09 17:17:47 -08001257 EXPECT_EQ(wpa_service.get(), psk_service.get());
1258 // Indirectly test FindService by doing a GetService on something that
1259 // already exists.
1260 {
1261 Error e;
1262 WiFiServiceRefPtr wpa_service2(
Paul Stewarta41e38d2011-11-11 07:47:29 -08001263 GetServiceInner(flimflam::kTypeWifi, ssid.c_str(),
1264 flimflam::kModeManaged, flimflam::kSecurityWpa,
1265 "abcdefgh", false, &e));
Paul Stewart6ab23a92011-11-09 17:17:47 -08001266 EXPECT_TRUE(e.IsSuccess());
1267 EXPECT_EQ(wpa_service.get(), wpa_service2.get());
1268 }
1269}
1270
Paul Stewartcb59fed2012-03-21 21:14:46 -07001271TEST_F(WiFiMainTest, GetServiceWithGUID) {
1272 // Perform a GetService that also configures properties in the base Service
1273 // class using Service::Configure().
1274 KeyValueStore args;
1275 args.SetString(flimflam::kTypeProperty, flimflam::kTypeWifi);
1276 args.SetString(flimflam::kSSIDProperty, "ssid");
1277 args.SetString(flimflam::kSecurityProperty, flimflam::kSecurityNone);
1278 const string kGUID = "aguid"; // Stored as a registered Service property.
1279 args.SetString(flimflam::kGuidProperty, kGUID);
1280 Error e;
1281 WiFiServiceRefPtr service = GetServiceWithKeyValues(args, &e);
1282 EXPECT_TRUE(e.IsSuccess());
1283 EXPECT_EQ(kGUID, service->guid());
1284}
1285
Paul Stewartced6a0b2011-11-08 15:32:04 -08001286MATCHER_P(HasHiddenSSID, ssid, "") {
Paul Stewarta41e38d2011-11-11 07:47:29 -08001287 map<string, DBus::Variant>::const_iterator it =
Paul Stewartced6a0b2011-11-08 15:32:04 -08001288 arg.find(wpa_supplicant::kPropertyScanSSIDs);
1289 if (it == arg.end()) {
1290 return false;
1291 }
1292
1293 const DBus::Variant &ssids_variant = it->second;
1294 EXPECT_TRUE(DBusAdaptor::IsByteArrays(ssids_variant.signature()));
1295 const ByteArrays &ssids = it->second.operator ByteArrays();
1296 // A valid Scan containing a single hidden SSID should contain
1297 // two SSID entries: one containing the SSID we are looking for,
1298 // and an empty entry, signifying that we also want to do a
1299 // broadcast probe request for all non-hidden APs as well.
1300 return ssids.size() == 2 &&
1301 string(ssids[0].begin(), ssids[0].end()) == ssid &&
1302 ssids[1].empty();
1303}
1304
1305TEST_F(WiFiMainTest, ScanHidden) {
1306 EXPECT_CALL(*supplicant_process_proxy_, CreateInterface(_));
1307 EXPECT_CALL(*supplicant_process_proxy_, GetInterface(_))
1308 .Times(AnyNumber())
1309 .WillRepeatedly(Throw(
1310 DBus::Error(
1311 "fi.w1.wpa_supplicant1.InterfaceUnknown",
1312 "test threw fi.w1.wpa_supplicant1.InterfaceUnknown")));
Paul Stewart10ccbb32012-04-26 15:59:30 -07001313 scoped_refptr<MockProfile> profile(
1314 new NiceMock<MockProfile>(control_interface(), manager(), ""));
Paul Stewarta41e38d2011-11-11 07:47:29 -08001315 {
Paul Stewart10ccbb32012-04-26 15:59:30 -07001316 // Create a hidden service with an associated profile.
Paul Stewarta41e38d2011-11-11 07:47:29 -08001317 Error e;
1318 WiFiServiceRefPtr service =
1319 GetServiceInner(flimflam::kTypeWifi, "ssid0", flimflam::kModeManaged,
1320 NULL, NULL, true, &e);
1321 EXPECT_TRUE(e.IsSuccess());
1322 EXPECT_TRUE(service->hidden_ssid());
Paul Stewart10ccbb32012-04-26 15:59:30 -07001323 service->set_profile(profile);
Paul Stewarta41e38d2011-11-11 07:47:29 -08001324 }
1325 {
Paul Stewart10ccbb32012-04-26 15:59:30 -07001326 // Create a hidden service without an associated profile.
Paul Stewarta41e38d2011-11-11 07:47:29 -08001327 Error e;
1328 WiFiServiceRefPtr service =
1329 GetServiceInner(flimflam::kTypeWifi, "ssid1", flimflam::kModeManaged,
1330 NULL, NULL, true, &e);
1331 EXPECT_TRUE(e.IsSuccess());
1332 EXPECT_TRUE(service->hidden_ssid());
1333 }
1334 {
Paul Stewart10ccbb32012-04-26 15:59:30 -07001335 // Create a non-hidden service with an associated profile.
Paul Stewarta41e38d2011-11-11 07:47:29 -08001336 Error e;
1337 WiFiServiceRefPtr service =
1338 GetServiceInner(flimflam::kTypeWifi, "ssid2", flimflam::kModeManaged,
1339 NULL, NULL, false, &e);
1340 EXPECT_TRUE(e.IsSuccess());
1341 EXPECT_FALSE(service->hidden_ssid());
Paul Stewart10ccbb32012-04-26 15:59:30 -07001342 service->set_profile(profile);
Paul Stewarta41e38d2011-11-11 07:47:29 -08001343 }
1344 EXPECT_CALL(*supplicant_interface_proxy_, Scan(HasHiddenSSID("ssid0")));
Paul Stewartced6a0b2011-11-08 15:32:04 -08001345 StartWiFi();
1346 dispatcher_.DispatchPendingEvents();
1347}
1348
mukesh agrawal7ec71312011-11-10 02:08:26 +00001349TEST_F(WiFiMainTest, InitialSupplicantState) {
1350 EXPECT_EQ(WiFi::kInterfaceStateUnknown, GetSupplicantState());
1351}
1352
1353TEST_F(WiFiMainTest, StateChangeNoService) {
1354 // State change should succeed even if there is no pending Service.
1355 ReportStateChanged(wpa_supplicant::kInterfaceStateScanning);
1356 EXPECT_EQ(wpa_supplicant::kInterfaceStateScanning, GetSupplicantState());
1357}
1358
1359TEST_F(WiFiMainTest, StateChangeWithService) {
1360 // Forward transition should trigger a Service state change.
1361 StartWiFi();
1362 dispatcher_.DispatchPendingEvents();
1363 MockWiFiServiceRefPtr service = MakeMockService();
1364 InitiateConnect(service);
1365 EXPECT_CALL(*service.get(), SetState(Service::kStateAssociating));
1366 ReportStateChanged(wpa_supplicant::kInterfaceStateAssociated);
1367 // Verify expectations now, because WiFi may report other state changes
1368 // when WiFi is Stop()-ed (during TearDown()).
1369 Mock::VerifyAndClearExpectations(service.get());
Gary Moraind9f893f2012-02-06 10:03:40 -08001370 EXPECT_CALL(*service.get(), SetState(_)).Times(AnyNumber());
mukesh agrawal7ec71312011-11-10 02:08:26 +00001371}
1372
1373TEST_F(WiFiMainTest, StateChangeBackwardsWithService) {
1374 // Some backwards transitions should not trigger a Service state change.
1375 // Supplicant state should still be updated, however.
Gary Moraind9f893f2012-02-06 10:03:40 -08001376 EXPECT_CALL(*dhcp_provider(), CreateConfig(_, _)).Times(AnyNumber());
1377 EXPECT_CALL(*dhcp_config_.get(), RequestIP()).Times(AnyNumber());
mukesh agrawal7ec71312011-11-10 02:08:26 +00001378 StartWiFi();
1379 dispatcher_.DispatchPendingEvents();
1380 MockWiFiServiceRefPtr service = MakeMockService();
Gary Moraind9f893f2012-02-06 10:03:40 -08001381 EXPECT_CALL(*service.get(), SetState(Service::kStateConfiguring));
mukesh agrawal7ec71312011-11-10 02:08:26 +00001382 InitiateConnect(service);
1383 ReportStateChanged(wpa_supplicant::kInterfaceStateCompleted);
mukesh agrawal7ec71312011-11-10 02:08:26 +00001384 ReportStateChanged(wpa_supplicant::kInterfaceStateAuthenticating);
1385 EXPECT_EQ(wpa_supplicant::kInterfaceStateAuthenticating,
1386 GetSupplicantState());
1387 // Verify expectations now, because WiFi may report other state changes
1388 // when WiFi is Stop()-ed (during TearDown()).
1389 Mock::VerifyAndClearExpectations(service.get());
Gary Moraind9f893f2012-02-06 10:03:40 -08001390 EXPECT_CALL(*service.get(), SetState(_)).Times(AnyNumber());
mukesh agrawal7ec71312011-11-10 02:08:26 +00001391}
1392
Paul Stewarta41e38d2011-11-11 07:47:29 -08001393TEST_F(WiFiMainTest, LoadHiddenServicesFailWithNoGroups) {
1394 StrictMock<MockStore> storage;
1395 EXPECT_CALL(storage, GetGroupsWithKey(flimflam::kWifiHiddenSsid))
1396 .WillOnce(Return(set<string>()));
1397 EXPECT_FALSE(LoadHiddenServices(&storage));
1398}
1399
1400TEST_F(WiFiMainTest, LoadHiddenServicesFailWithMissingHidden) {
1401 string id;
1402 StrictMock<MockStore> storage;
1403 SetupHiddenStorage(&storage, "an_ssid", &id);
1404 // Missing "Hidden" property.
1405 EXPECT_CALL(storage, GetBool(StrEq(id), flimflam::kWifiHiddenSsid, _))
1406 .WillOnce(Return(false));
1407 EXPECT_FALSE(LoadHiddenServices(&storage));
1408}
1409
1410TEST_F(WiFiMainTest, LoadHiddenServicesFailWithFalseHidden) {
1411 string id;
1412 StrictMock<MockStore> storage;
1413 SetupHiddenStorage(&storage, "an_ssid", &id);
1414 // "Hidden" property set to "false".
1415 EXPECT_CALL(storage, GetBool(StrEq(id), flimflam::kWifiHiddenSsid, _))
1416 .WillOnce(DoAll(SetArgumentPointee<2>(true), Return(false)));
1417 EXPECT_FALSE(LoadHiddenServices(&storage));
1418}
1419
1420TEST_F(WiFiMainTest, LoadHiddenServicesFailWithMissingSSID) {
1421 string id;
1422 StrictMock<MockStore> storage;
1423 SetupHiddenStorage(&storage, "an_ssid", &id);
1424 // Missing "SSID" property.
1425 EXPECT_CALL(storage, GetString(StrEq(id), flimflam::kSSIDProperty, _))
1426 .WillOnce(Return(false));
1427 EXPECT_FALSE(LoadHiddenServices(&storage));
1428}
1429
1430
1431TEST_F(WiFiMainTest, LoadHiddenServicesFailWithFoundService) {
1432 StrictMock<MockStore> storage;
1433 string id;
1434 SetupHiddenStorage(&storage, "an_ssid", &id);
1435 Error e;
1436 GetOpenService(flimflam::kTypeWifi, "an_ssid", NULL, &e);
1437 ASSERT_TRUE(e.IsSuccess());
1438 EXPECT_FALSE(LoadHiddenServices(&storage));
1439}
1440
1441TEST_F(WiFiMainTest, LoadHiddenServicesSuccess) {
Gary Moraind9f893f2012-02-06 10:03:40 -08001442 EXPECT_CALL(*manager(), RegisterService(_)).Times(AnyNumber());
Paul Stewarta41e38d2011-11-11 07:47:29 -08001443 StrictMock<MockStore> storage;
1444 string ssid("an_ssid");
1445 string id;
1446 SetupHiddenStorage(&storage, ssid, &id);
1447 EXPECT_TRUE(LoadHiddenServices(&storage));
1448 vector<uint8_t> ssid_bytes(ssid.begin(), ssid.end());
1449 EXPECT_TRUE(FindService(ssid_bytes, flimflam::kModeManaged,
1450 flimflam::kSecurityNone).get());
1451}
1452
mukesh agrawal15908392011-11-16 18:29:25 +00001453TEST_F(WiFiMainTest, CurrentBSSChangeConnectedToDisconnected) {
Gary Moraind9f893f2012-02-06 10:03:40 -08001454 EXPECT_CALL(*manager(), HasService(_)).Times(AnyNumber());
1455 EXPECT_CALL(*dhcp_provider(), CreateConfig(_, _)).Times(AnyNumber());
1456 EXPECT_CALL(*dhcp_config_.get(), RequestIP()).Times(AnyNumber());
1457 EXPECT_CALL(*manager(), UpdateService(_)).Times(AnyNumber());
mukesh agrawal15908392011-11-16 18:29:25 +00001458 WiFiEndpointRefPtr ap = MakeEndpoint("an_ssid", "00:01:02:03:04:05");
1459 WiFiServiceRefPtr service = CreateServiceForEndpoint(*ap);
1460
1461 // Note that the BSS handle used in this test ("an_ap") is not
1462 // intended to reflect the format used by supplicant. It's just
1463 // convenient for testing.
1464
1465 StartWiFi();
Thieu Lee41a72d2012-02-06 20:46:51 +00001466 ReportBSS("an_ap", ap->ssid_string(), ap->bssid_string(), 0, 0,
mukesh agrawal15908392011-11-16 18:29:25 +00001467 kNetworkModeInfrastructure);
1468 InitiateConnect(service);
1469 EXPECT_EQ(service, GetPendingService().get());
1470
1471 ReportCurrentBSSChanged("an_ap");
1472 ReportStateChanged(wpa_supplicant::kInterfaceStateCompleted);
1473 EXPECT_EQ(Service::kStateConfiguring, service->state());
1474 EXPECT_EQ(service, GetCurrentService().get());
1475 EXPECT_EQ(NULL, GetPendingService().get());
1476
1477 ReportCurrentBSSChanged(wpa_supplicant::kCurrentBSSNull);
mukesh agrawal568b5c62012-02-28 14:44:47 -08001478 EXPECT_EQ(Service::kStateIdle, service->state());
1479 EXPECT_TRUE(service->IsFailed());
mukesh agrawal15908392011-11-16 18:29:25 +00001480 EXPECT_EQ(NULL, GetCurrentService().get());
1481 EXPECT_EQ(NULL, GetPendingService().get());
1482}
1483
1484TEST_F(WiFiMainTest, CurrentBSSChangeConnectedToConnectedNewService) {
Gary Moraind9f893f2012-02-06 10:03:40 -08001485 EXPECT_CALL(*manager(), HasService(_)).Times(AnyNumber());
1486 EXPECT_CALL(*dhcp_provider(), CreateConfig(_, _)).Times(AnyNumber());
1487 EXPECT_CALL(*dhcp_config_.get(), RequestIP()).Times(AnyNumber());
1488 EXPECT_CALL(*manager(), UpdateService(_)).Times(AnyNumber());
mukesh agrawal15908392011-11-16 18:29:25 +00001489 WiFiEndpointRefPtr ap1 = MakeEndpoint("an_ssid", "00:01:02:03:04:05");
1490 WiFiEndpointRefPtr ap2 = MakeEndpoint("another_ssid", "01:02:03:04:05:06");
1491 WiFiServiceRefPtr service1 = CreateServiceForEndpoint(*ap1);
1492 WiFiServiceRefPtr service2 = CreateServiceForEndpoint(*ap2);
1493
1494 // Note that the BSS handles used in this test ("ap1", "ap2") are
1495 // not intended to reflect the format used by supplicant. They're
1496 // just convenient for testing.
1497
1498 StartWiFi();
Thieu Lee41a72d2012-02-06 20:46:51 +00001499 ReportBSS("ap1", ap1->ssid_string(), ap1->bssid_string(), 0, 0,
mukesh agrawal15908392011-11-16 18:29:25 +00001500 kNetworkModeInfrastructure);
Thieu Lee41a72d2012-02-06 20:46:51 +00001501 ReportBSS("ap2", ap2->ssid_string(), ap2->bssid_string(), 0, 0,
mukesh agrawal15908392011-11-16 18:29:25 +00001502 kNetworkModeInfrastructure);
1503 InitiateConnect(service1);
1504 ReportCurrentBSSChanged("ap1");
1505 ReportStateChanged(wpa_supplicant::kInterfaceStateCompleted);
1506 EXPECT_EQ(service1.get(), GetCurrentService().get());
1507
mukesh agrawalc01f3982012-01-24 13:48:39 -08001508 // Note that we deliberately omit intermediate supplicant states
1509 // (e.g. kInterfaceStateAssociating), on the theory that they are
1510 // unreliable. Specifically, they may be quashed if the association
1511 // completes before supplicant flushes its changed properties.
mukesh agrawal15908392011-11-16 18:29:25 +00001512 ReportCurrentBSSChanged("ap2");
1513 ReportStateChanged(wpa_supplicant::kInterfaceStateCompleted);
1514 EXPECT_EQ(service2.get(), GetCurrentService().get());
1515 EXPECT_EQ(Service::kStateIdle, service1->state());
1516 EXPECT_EQ(Service::kStateConfiguring, service2->state());
1517}
1518
1519TEST_F(WiFiMainTest, CurrentBSSChangeDisconnectedToConnected) {
Gary Moraind9f893f2012-02-06 10:03:40 -08001520 EXPECT_CALL(*manager(), HasService(_)).Times(AnyNumber());
1521 EXPECT_CALL(*manager(), UpdateService(_)).Times(AnyNumber());
1522 EXPECT_CALL(*dhcp_provider(), CreateConfig(_, _)).Times(AnyNumber());
1523 EXPECT_CALL(*dhcp_config_.get(), RequestIP()).Times(AnyNumber());
mukesh agrawal15908392011-11-16 18:29:25 +00001524 WiFiEndpointRefPtr ap = MakeEndpoint("an_ssid", "00:01:02:03:04:05");
1525 WiFiServiceRefPtr service = CreateServiceForEndpoint(*ap);
1526
1527 // Note that the BSS handle used in this test ("an_ap") is not
1528 // intended to reflect the format used by supplicant. It's just
1529 // convenient for testing.
1530
1531 StartWiFi();
Thieu Lee41a72d2012-02-06 20:46:51 +00001532 ReportBSS("an_ap", ap->ssid_string(), ap->bssid_string(), 0, 0,
mukesh agrawal15908392011-11-16 18:29:25 +00001533 kNetworkModeInfrastructure);
1534 InitiateConnect(service);
1535 ReportCurrentBSSChanged("an_ap");
1536 ReportStateChanged(wpa_supplicant::kInterfaceStateCompleted);
1537 EXPECT_EQ(service.get(), GetCurrentService().get());
1538 EXPECT_EQ(Service::kStateConfiguring, service->state());
1539}
1540
Thieu Lee41a72d2012-02-06 20:46:51 +00001541TEST_F(WiFiMainTest, CurrentBSSChangedUpdateServiceEndpoint) {
mukesh agrawalb20776f2012-02-10 16:00:36 -08001542 EXPECT_CALL(*manager(), RegisterService(_)).Times(AnyNumber());
1543 EXPECT_CALL(*manager(), HasService(_)).Times(AnyNumber());
1544 EXPECT_CALL(*manager(), UpdateService(_)).Times(AnyNumber());
1545
Thieu Lee41a72d2012-02-06 20:46:51 +00001546 const uint16 frequency1 = 2412;
1547 const uint16 frequency2 = 2442;
1548 StartWiFi();
1549 ReportBSS("bss1", "ssid1", "00:00:00:00:00:01", 1, frequency1,
1550 kNetworkModeInfrastructure);
1551 ReportBSS("bss2", "ssid1", "00:00:00:00:00:02", 1, frequency2,
1552 kNetworkModeInfrastructure);
1553 EXPECT_EQ(1, GetServices().size());
1554 WiFiService *service(GetServices()[0].get());
1555 InitiateConnect(service);
1556 ReportCurrentBSSChanged("bss1");
1557 EXPECT_EQ(frequency1, service->frequency_);
1558 ReportCurrentBSSChanged("bss2");
1559 EXPECT_EQ(frequency2, service->frequency_);
1560}
1561
mukesh agrawal4eb4d782011-12-05 17:34:37 +00001562TEST_F(WiFiMainTest, ConfiguredServiceRegistration) {
1563 Error e;
1564 EXPECT_CALL(*manager(), RegisterService(_))
1565 .Times(0);
1566 EXPECT_CALL(*manager(), HasService(_))
1567 .WillOnce(Return(false));
1568 GetOpenService(flimflam::kTypeWifi, "an_ssid", flimflam::kModeManaged, &e);
1569 EXPECT_CALL(*manager(), RegisterService(_));
Thieu Lee41a72d2012-02-06 20:46:51 +00001570 ReportBSS("ap0", "an_ssid", "00:00:00:00:00:00", 0, 0,
mukesh agrawal4eb4d782011-12-05 17:34:37 +00001571 kNetworkModeInfrastructure);
1572}
1573
mukesh agrawal8a3188d2011-12-01 20:56:44 +00001574TEST_F(WiFiMainTest, NewConnectPreemptsPending) {
Gary Moraind9f893f2012-02-06 10:03:40 -08001575 EXPECT_CALL(*manager(), HasService(_)).Times(AnyNumber());
1576 EXPECT_CALL(*manager(), UpdateService(_)).Times(AnyNumber());
mukesh agrawal8a3188d2011-12-01 20:56:44 +00001577 WiFiEndpointRefPtr ap1 = MakeEndpoint("an_ssid", "00:01:02:03:04:05");
1578 WiFiEndpointRefPtr ap2 = MakeEndpoint("another_ssid", "01:02:03:04:05:06");
1579 WiFiServiceRefPtr service1 = CreateServiceForEndpoint(*ap1);
1580 WiFiServiceRefPtr service2 = CreateServiceForEndpoint(*ap2);
1581
1582 StartWiFi();
Thieu Lee41a72d2012-02-06 20:46:51 +00001583 ReportBSS("ap1", ap1->ssid_string(), ap1->bssid_string(), 0, 0,
mukesh agrawal8a3188d2011-12-01 20:56:44 +00001584 kNetworkModeInfrastructure);
Thieu Lee41a72d2012-02-06 20:46:51 +00001585 ReportBSS("ap2", ap2->ssid_string(), ap2->bssid_string(), 0, 0,
mukesh agrawal8a3188d2011-12-01 20:56:44 +00001586 kNetworkModeInfrastructure);
1587 InitiateConnect(service1);
1588 EXPECT_EQ(service1.get(), GetPendingService().get());
1589
1590 EXPECT_CALL(*GetSupplicantInterfaceProxy(), Disconnect());
1591 EXPECT_CALL(*GetSupplicantInterfaceProxy(), AddNetwork(_));
1592 InitiateConnect(service2);
1593 EXPECT_EQ(service2.get(), GetPendingService().get());
1594}
1595
1596TEST_F(WiFiMainTest, IsIdle) {
1597 StartWiFi();
1598 EXPECT_TRUE(wifi()->IsIdle());
1599
1600 WiFiEndpointRefPtr ap = MakeEndpoint("an_ssid", "00:01:02:03:04:05");
1601 WiFiServiceRefPtr service = CreateServiceForEndpoint(*ap);
1602 Error error;
1603 service->AddEndpoint(ap);
1604 service->AutoConnect();
1605 EXPECT_FALSE(wifi()->IsIdle());
1606}
1607
mukesh agrawal4d0401c2012-01-06 16:05:31 -08001608MATCHER(WiFiAddedArgs, "") {
1609 return ContainsKey(arg, wpa_supplicant::kNetworkPropertyScanSSID) &&
1610 ContainsKey(arg, wpa_supplicant::kNetworkPropertyBgscan);
1611}
1612
1613TEST_F(WiFiMainTest, AddNetworkArgs) {
Gary Moraind9f893f2012-02-06 10:03:40 -08001614 EXPECT_CALL(*manager(), RegisterService(_)).Times(AnyNumber());
mukesh agrawal4d0401c2012-01-06 16:05:31 -08001615 MockSupplicantInterfaceProxy &supplicant_interface_proxy =
1616 *supplicant_interface_proxy_;
1617
1618 StartWiFi();
Thieu Lee41a72d2012-02-06 20:46:51 +00001619 ReportBSS("bss0", "ssid0", "00:00:00:00:00:00", 0, 0, kNetworkModeAdHoc);
mukesh agrawal4d0401c2012-01-06 16:05:31 -08001620 WiFiService *service(GetServices().begin()->get());
1621 EXPECT_CALL(supplicant_interface_proxy, AddNetwork(WiFiAddedArgs()));
1622 InitiateConnect(service);
1623}
1624
mukesh agrawalc01f3982012-01-24 13:48:39 -08001625TEST_F(WiFiMainTest, StateAndIPIgnoreLinkEvent) {
1626 StartWiFi();
1627 MockWiFiServiceRefPtr service = MakeMockService();
1628 InitiateConnect(service);
1629 EXPECT_CALL(*service.get(), SetState(_)).Times(0);
1630 EXPECT_CALL(*dhcp_config_.get(), RequestIP()).Times(0);
1631 ReportLinkUp();
1632
1633 // Verify expectations now, because WiFi may cause |service| state
1634 // changes during TearDown().
1635 Mock::VerifyAndClearExpectations(service);
Gary Moraind9f893f2012-02-06 10:03:40 -08001636 EXPECT_CALL(*service.get(), SetState(_)).Times(AnyNumber());
mukesh agrawalc01f3982012-01-24 13:48:39 -08001637}
1638
1639TEST_F(WiFiMainTest, SupplicantCompleted) {
Gary Moraind9f893f2012-02-06 10:03:40 -08001640 EXPECT_CALL(*manager(), UpdateService(_)).Times(AnyNumber());
1641 EXPECT_CALL(*dhcp_provider(), CreateConfig(_, _)).Times(AnyNumber());
1642 EXPECT_CALL(*manager(), HasService(_)).Times(AnyNumber());
mukesh agrawalc01f3982012-01-24 13:48:39 -08001643 WiFiEndpointRefPtr ap = MakeEndpoint("an_ssid", "00:01:02:03:04:05");
1644 WiFiServiceRefPtr service = CreateServiceForEndpoint(*ap);
1645
1646 StartWiFi();
Thieu Lee41a72d2012-02-06 20:46:51 +00001647 ReportBSS("ap", ap->ssid_string(), ap->bssid_string(), 0, 0,
mukesh agrawalc01f3982012-01-24 13:48:39 -08001648 kNetworkModeInfrastructure);
1649 InitiateConnect(service);
1650
1651 EXPECT_CALL(*dhcp_config_.get(), RequestIP());
1652 ReportCurrentBSSChanged("ap");
1653 ReportStateChanged(wpa_supplicant::kInterfaceStateCompleted);
1654 EXPECT_EQ(Service::kStateConfiguring, service->state());
1655}
1656
1657TEST_F(WiFiMainTest, SupplicantCompletedAlreadyConnected) {
Gary Moraind9f893f2012-02-06 10:03:40 -08001658 EXPECT_CALL(*dhcp_config_.get(), RequestIP());
1659 EXPECT_CALL(*manager(), UpdateService(_)).Times(AnyNumber());
1660 EXPECT_CALL(*device_info(), FlushAddresses(_)).Times(AnyNumber());
1661 EXPECT_CALL(*manager(), device_info()).Times(AnyNumber());
1662 EXPECT_CALL(*dhcp_provider(), CreateConfig(_, _)).Times(AnyNumber());
1663 EXPECT_CALL(*manager(), HasService(_)).Times(AnyNumber());
Gary Morainac1bdb42012-02-16 17:42:29 -08001664 EXPECT_CALL(*manager(), IsPortalDetectionEnabled(_)).Times(AnyNumber());
mukesh agrawalc01f3982012-01-24 13:48:39 -08001665 WiFiEndpointRefPtr ap = MakeEndpoint("an_ssid", "00:01:02:03:04:05");
1666 WiFiServiceRefPtr service = CreateServiceForEndpoint(*ap);
1667
mukesh agrawalc01f3982012-01-24 13:48:39 -08001668 StartWiFi();
Thieu Lee41a72d2012-02-06 20:46:51 +00001669 ReportBSS("ap", ap->ssid_string(), ap->bssid_string(), 0, 0,
mukesh agrawalc01f3982012-01-24 13:48:39 -08001670 kNetworkModeInfrastructure);
1671 InitiateConnect(service);
1672 ReportCurrentBSSChanged("ap");
1673 ReportStateChanged(wpa_supplicant::kInterfaceStateCompleted);
1674 ReportIPConfigComplete();
1675 Mock::VerifyAndClearExpectations(service);
1676
1677 EXPECT_CALL(*dhcp_config_.get(), RequestIP()).Times(0);
1678 ReportStateChanged(wpa_supplicant::kInterfaceStateCompleted);
1679}
1680
Paul Stewart66c86002012-01-30 18:00:52 -08001681TEST_F(WiFiMainTest, ClearCachedCredentials) {
1682 MockSupplicantInterfaceProxy &supplicant_interface_proxy =
1683 *supplicant_interface_proxy_;
1684
1685 StartWiFi();
1686
1687 // Ensure call to the proxy is deferred.
1688 EXPECT_CALL(supplicant_interface_proxy, ClearCachedCredentials())
1689 .Times(0);
1690 ClearCachedCredentials();
1691
1692 Mock::VerifyAndClearExpectations(&supplicant_interface_proxy);
1693
1694 EXPECT_CALL(supplicant_interface_proxy, ClearCachedCredentials())
1695 .Times(1);
1696 dispatcher_.DispatchPendingEvents();
1697
1698 Mock::VerifyAndClearExpectations(&supplicant_interface_proxy);
1699
1700 EXPECT_CALL(supplicant_interface_proxy, ClearCachedCredentials())
1701 .Times(0);
1702 ClearCachedCredentials();
1703 ClearCachedCredentials();
1704
1705 Mock::VerifyAndClearExpectations(&supplicant_interface_proxy);
1706
1707 // Ensure multiple calls to ClearCachedCredentials() results in only
1708 // one call to the proxy.
1709 EXPECT_CALL(supplicant_interface_proxy, ClearCachedCredentials())
1710 .Times(1);
1711 dispatcher_.DispatchPendingEvents();
1712}
1713
mukesh agrawalb20776f2012-02-10 16:00:36 -08001714TEST_F(WiFiMainTest, BSSAddedCreatesBSSProxy) {
1715 EXPECT_CALL(*manager(), RegisterService(_)).Times(AnyNumber());
1716 // TODO(quiche): Consider using a factory for WiFiEndpoints, so that
1717 // we can test the interaction between WiFi and WiFiEndpoint. (Right
1718 // now, we're testing across multiple layers.)
1719 EXPECT_CALL(*supplicant_bss_proxy_, Die()).Times(AnyNumber());
1720 EXPECT_CALL(*proxy_factory(), CreateSupplicantBSSProxy(_, _, _));
1721 StartWiFi();
1722 ReportBSS("bss0", "ssid0", "00:00:00:00:00:00", 0, 0, kNetworkModeAdHoc);
1723}
1724
1725TEST_F(WiFiMainTest, BSSRemovedDestroysBSSProxy) {
1726 // TODO(quiche): As for BSSAddedCreatesBSSProxy, consider using a
1727 // factory for WiFiEndpoints.
1728 EXPECT_CALL(*manager(), RegisterService(_)).Times(AnyNumber());
1729
1730 // Get the pointer before we transfer ownership.
1731 MockSupplicantBSSProxy *proxy = supplicant_bss_proxy_.get();
1732 EXPECT_CALL(*proxy, Die());
1733 StartWiFi();
1734 ReportBSS("bss0", "ssid0", "00:00:00:00:00:00", 0, 0, kNetworkModeAdHoc);
1735 RemoveBSS("bss0");
1736 // Check this now, to make sure RemoveBSS killed the proxy (rather
1737 // than TearDown).
1738 Mock::VerifyAndClearExpectations(proxy);
1739}
1740
mukesh agrawal5c05b292012-03-07 10:12:52 -08001741TEST_F(WiFiMainTest, FlushBSSOnResume) {
1742 const struct timeval resume_time = {1, 0};
1743 const struct timeval scan_done_time = {6, 0};
1744 MockSupplicantInterfaceProxy &supplicant_interface_proxy =
1745 *supplicant_interface_proxy_;
1746
1747 StartWiFi();
1748
1749 EXPECT_CALL(time_, GetTimeMonotonic(_))
1750 .WillOnce(DoAll(SetArgumentPointee<0>(resume_time), Return(0)))
1751 .WillOnce(DoAll(SetArgumentPointee<0>(scan_done_time), Return(0)));
1752 EXPECT_CALL(supplicant_interface_proxy,
1753 FlushBSS(WiFi::kMaxBSSResumeAgeSeconds + 5));
Eric Shienbrood3e20a232012-02-16 11:35:56 -05001754 power_state_callback().Run(PowerManagerProxyDelegate::kOn);
mukesh agrawal5c05b292012-03-07 10:12:52 -08001755 ReportScanDone();
1756}
1757
Chris Masone853b81b2011-06-24 14:11:41 -07001758} // namespace shill