blob: a9aa87e7a43a5a8a10baa9412ec7d0610b52797a [file] [log] [blame]
Chris Masone853b81b2011-06-24 14:11:41 -07001// Copyright (c) 2011 The Chromium OS Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#include "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 agrawalf2f68a52011-09-01 12:15:48 -070033#include "shill/mock_dhcp_config.h"
34#include "shill/mock_dhcp_provider.h"
Chris Masone2ae797d2011-08-23 20:41:00 -070035#include "shill/mock_manager.h"
mukesh agrawal5c4dd0b2011-09-14 13:53:14 -070036#include "shill/mock_rtnl_handler.h"
Paul Stewarta41e38d2011-11-11 07:47:29 -080037#include "shill/mock_store.h"
mukesh agrawal31950242011-07-14 11:53:38 -070038#include "shill/mock_supplicant_interface_proxy.h"
39#include "shill/mock_supplicant_process_proxy.h"
mukesh agrawal7ec71312011-11-10 02:08:26 +000040#include "shill/mock_wifi_service.h"
mukesh agrawaldc42bb32011-07-28 10:40:26 -070041#include "shill/nice_mock_control.h"
Chris Masone853b81b2011-06-24 14:11:41 -070042#include "shill/property_store_unittest.h"
mukesh agrawal31950242011-07-14 11:53:38 -070043#include "shill/proxy_factory.h"
44#include "shill/wifi_endpoint.h"
45#include "shill/wifi.h"
mukesh agrawal32399322011-09-01 10:53:43 -070046#include "shill/wifi_service.h"
Paul Stewartced6a0b2011-11-08 15:32:04 -080047#include "shill/wpa_supplicant.h"
Chris Masone853b81b2011-06-24 14:11:41 -070048
49using std::map;
Paul Stewarta41e38d2011-11-11 07:47:29 -080050using std::set;
Chris Masone853b81b2011-06-24 14:11:41 -070051using std::string;
52using std::vector;
53using ::testing::_;
mukesh agrawal31950242011-07-14 11:53:38 -070054using ::testing::AnyNumber;
55using ::testing::DefaultValue;
Paul Stewarta41e38d2011-11-11 07:47:29 -080056using ::testing::DoAll;
mukesh agrawal31950242011-07-14 11:53:38 -070057using ::testing::InSequence;
mukesh agrawal7ec71312011-11-10 02:08:26 +000058using ::testing::Mock;
mukesh agrawaldc42bb32011-07-28 10:40:26 -070059using ::testing::NiceMock;
Chris Masone853b81b2011-06-24 14:11:41 -070060using ::testing::Return;
Paul Stewarta41e38d2011-11-11 07:47:29 -080061using ::testing::SetArgumentPointee;
62using ::testing::StrEq;
63using ::testing::StrictMock;
Chris Masone853b81b2011-06-24 14:11:41 -070064using ::testing::Test;
mukesh agrawal31950242011-07-14 11:53:38 -070065using ::testing::Throw;
mukesh agrawal8ede0522011-10-03 14:57:44 -070066using ::testing::Values;
Chris Masone853b81b2011-06-24 14:11:41 -070067
68namespace shill {
69
mukesh agrawal31950242011-07-14 11:53:38 -070070class WiFiPropertyTest : public PropertyStoreTest {
Chris Masone853b81b2011-06-24 14:11:41 -070071 public:
mukesh agrawal31950242011-07-14 11:53:38 -070072 WiFiPropertyTest()
Chris Masone2176a882011-09-14 22:29:15 -070073 : device_(new WiFi(control_interface(), NULL, NULL, "wifi", "", 0)) {
Chris Masone853b81b2011-06-24 14:11:41 -070074 }
mukesh agrawal31950242011-07-14 11:53:38 -070075 virtual ~WiFiPropertyTest() {}
Chris Masone853b81b2011-06-24 14:11:41 -070076
77 protected:
78 DeviceRefPtr device_;
79};
80
mukesh agrawal31950242011-07-14 11:53:38 -070081TEST_F(WiFiPropertyTest, Contains) {
mukesh agrawalde29fa82011-09-16 16:16:36 -070082 EXPECT_TRUE(device_->store().Contains(flimflam::kNameProperty));
83 EXPECT_FALSE(device_->store().Contains(""));
Chris Masone853b81b2011-06-24 14:11:41 -070084}
85
mukesh agrawal31950242011-07-14 11:53:38 -070086TEST_F(WiFiPropertyTest, Dispatch) {
Chris Masonea8a2c252011-06-27 22:16:30 -070087 {
88 ::DBus::Error error;
mukesh agrawalde29fa82011-09-16 16:16:36 -070089 EXPECT_TRUE(DBusAdaptor::DispatchOnType(device_->mutable_store(),
Chris Masonea8a2c252011-06-27 22:16:30 -070090 flimflam::kBgscanMethodProperty,
91 PropertyStoreTest::kStringV,
92 &error));
93 }
94 {
95 ::DBus::Error error;
96 EXPECT_TRUE(DBusAdaptor::DispatchOnType(
mukesh agrawalde29fa82011-09-16 16:16:36 -070097 device_->mutable_store(),
Chris Masonea8a2c252011-06-27 22:16:30 -070098 flimflam::kBgscanSignalThresholdProperty,
99 PropertyStoreTest::kInt32V,
100 &error));
101 }
102 {
103 ::DBus::Error error;
mukesh agrawalde29fa82011-09-16 16:16:36 -0700104 EXPECT_TRUE(DBusAdaptor::DispatchOnType(device_->mutable_store(),
Chris Masonea8a2c252011-06-27 22:16:30 -0700105 flimflam::kScanIntervalProperty,
106 PropertyStoreTest::kUint16V,
107 &error));
108 }
Chris Masone853b81b2011-06-24 14:11:41 -0700109 // Ensure that an attempt to write a R/O property returns InvalidArgs error.
Chris Masonea8a2c252011-06-27 22:16:30 -0700110 {
111 ::DBus::Error error;
mukesh agrawalde29fa82011-09-16 16:16:36 -0700112 EXPECT_FALSE(DBusAdaptor::DispatchOnType(device_->mutable_store(),
Chris Masonea8a2c252011-06-27 22:16:30 -0700113 flimflam::kScanningProperty,
114 PropertyStoreTest::kBoolV,
115 &error));
Chris Masone9d779932011-08-25 16:33:41 -0700116 EXPECT_EQ(invalid_args(), error.name());
Chris Masonea8a2c252011-06-27 22:16:30 -0700117 }
Chris Masone853b81b2011-06-24 14:11:41 -0700118}
119
mukesh agrawal8ede0522011-10-03 14:57:44 -0700120class WiFiMainTest : public ::testing::TestWithParam<string> {
mukesh agrawal31950242011-07-14 11:53:38 -0700121 public:
122 WiFiMainTest()
Chris Masoneb9c00592011-10-06 13:10:39 -0700123 : manager_(&control_interface_, NULL, &glib_),
Chris Masone626719f2011-08-18 16:58:48 -0700124 wifi_(new WiFi(&control_interface_,
mukesh agrawal32399322011-09-01 10:53:43 -0700125 &dispatcher_,
Chris Masone626719f2011-08-18 16:58:48 -0700126 &manager_,
127 kDeviceName,
128 kDeviceAddress,
129 0)),
mukesh agrawaldc42bb32011-07-28 10:40:26 -0700130 supplicant_process_proxy_(new NiceMock<MockSupplicantProcessProxy>()),
131 supplicant_interface_proxy_(
132 new NiceMock<MockSupplicantInterfaceProxy>(wifi_)),
mukesh agrawalf2f68a52011-09-01 12:15:48 -0700133 dhcp_config_(new MockDHCPConfig(&control_interface_,
134 &dispatcher_,
135 &dhcp_provider_,
136 kDeviceName,
137 &glib_)),
mukesh agrawaldc42bb32011-07-28 10:40:26 -0700138 proxy_factory_(this) {
mukesh agrawal31950242011-07-14 11:53:38 -0700139 ::testing::DefaultValue< ::DBus::Path>::Set("/default/path");
140 }
mukesh agrawal5c4dd0b2011-09-14 13:53:14 -0700141
142 virtual void SetUp() {
Darin Petkovab565bb2011-10-06 02:55:51 -0700143 wifi_->proxy_factory_ = &proxy_factory_;
mukesh agrawal5c4dd0b2011-09-14 13:53:14 -0700144 static_cast<Device *>(wifi_)->rtnl_handler_ = &rtnl_handler_;
145 wifi_->set_dhcp_provider(&dhcp_provider_);
Chris Masoneb9c00592011-10-06 13:10:39 -0700146 EXPECT_CALL(manager_, DeregisterService(_)).Times(AnyNumber());
mukesh agrawal5c4dd0b2011-09-14 13:53:14 -0700147 }
148
149 virtual void TearDown() {
Darin Petkovab565bb2011-10-06 02:55:51 -0700150 wifi_->proxy_factory_ = NULL;
mukesh agrawal31950242011-07-14 11:53:38 -0700151 // must Stop WiFi instance, to clear its list of services.
152 // otherwise, the WiFi instance will not be deleted. (because
153 // services reference a WiFi instance, creating a cycle.)
154 wifi_->Stop();
mukesh agrawal5c4dd0b2011-09-14 13:53:14 -0700155 wifi_->set_dhcp_provider(NULL);
mukesh agrawalf2f68a52011-09-01 12:15:48 -0700156 }
157
mukesh agrawal31950242011-07-14 11:53:38 -0700158 protected:
mukesh agrawal7ec71312011-11-10 02:08:26 +0000159 typedef scoped_refptr<MockWiFiService> MockWiFiServiceRefPtr;
160
mukesh agrawal31950242011-07-14 11:53:38 -0700161 class TestProxyFactory : public ProxyFactory {
162 public:
Paul Stewart7355ce12011-09-02 10:47:01 -0700163 explicit TestProxyFactory(WiFiMainTest *test) : test_(test) {}
mukesh agrawal31950242011-07-14 11:53:38 -0700164
165 virtual SupplicantProcessProxyInterface *CreateSupplicantProcessProxy(
mukesh agrawal1830fa12011-09-26 14:31:40 -0700166 const char */*dbus_path*/, const char */*dbus_addr*/) {
mukesh agrawaldc42bb32011-07-28 10:40:26 -0700167 return test_->supplicant_process_proxy_.release();
mukesh agrawal31950242011-07-14 11:53:38 -0700168 }
169
170 virtual SupplicantInterfaceProxyInterface *CreateSupplicantInterfaceProxy(
mukesh agrawal1830fa12011-09-26 14:31:40 -0700171 const WiFiRefPtr &/*wifi*/,
172 const DBus::Path &/*object_path*/,
173 const char */*dbus_addr*/) {
mukesh agrawaldc42bb32011-07-28 10:40:26 -0700174 return test_->supplicant_interface_proxy_.release();
mukesh agrawal31950242011-07-14 11:53:38 -0700175 }
176
177 private:
mukesh agrawaldc42bb32011-07-28 10:40:26 -0700178 WiFiMainTest *test_;
mukesh agrawal31950242011-07-14 11:53:38 -0700179 };
180
mukesh agrawal15908392011-11-16 18:29:25 +0000181 WiFiServiceRefPtr CreateServiceForEndpoint(const WiFiEndpoint &endpoint) {
182 bool hidden_ssid = false;
183 return wifi_->CreateServiceForEndpoint(endpoint, hidden_ssid);
184 }
185 const WiFiServiceRefPtr &GetCurrentService() {
186 return wifi_->current_service_;
187 }
mukesh agrawal31950242011-07-14 11:53:38 -0700188 const WiFi::EndpointMap &GetEndpointMap() {
189 return wifi_->endpoint_by_bssid_;
190 }
mukesh agrawal15908392011-11-16 18:29:25 +0000191 const WiFiServiceRefPtr &GetPendingService() {
192 return wifi_->pending_service_;
193 }
mukesh agrawal31950242011-07-14 11:53:38 -0700194 const WiFi::ServiceMap &GetServiceMap() {
195 return wifi_->service_by_private_id_;
196 }
197 // note: the tests need the proxies referenced by WiFi (not the
198 // proxies instantiated by WiFiMainTest), to ensure that WiFi
199 // sets up its proxies correctly.
200 SupplicantProcessProxyInterface *GetSupplicantProcessProxy() {
201 return wifi_->supplicant_process_proxy_.get();
202 }
203 SupplicantInterfaceProxyInterface *GetSupplicantInterfaceProxy() {
204 return wifi_->supplicant_interface_proxy_.get();
205 }
mukesh agrawal7ec71312011-11-10 02:08:26 +0000206 const string &GetSupplicantState() {
207 return wifi_->supplicant_state_;
208 }
209 void InitiateConnect(WiFiServiceRefPtr service) {
mukesh agrawal6e277772011-09-29 15:04:23 -0700210 map<string, ::DBus::Variant> params;
211 wifi_->ConnectTo(service, params);
mukesh agrawal31950242011-07-14 11:53:38 -0700212 }
mukesh agrawalf2f68a52011-09-01 12:15:48 -0700213 bool IsLinkUp() {
214 return wifi_->link_up_;
215 }
mukesh agrawal15908392011-11-16 18:29:25 +0000216 WiFiEndpointRefPtr MakeEndpoint(const string &ssid, const string &bssid) {
217 map <string, ::DBus::Variant> args;
218 ::DBus::MessageIter writer;
219
220 writer = args[wpa_supplicant::kBSSPropertySSID].writer();
221 writer << vector<uint8_t>(ssid.begin(), ssid.end());
222
223 string bssid_nosep;
224 RemoveChars(bssid, ":", &bssid_nosep);
225 vector<uint8_t> bssid_bytes;
226 base::HexStringToBytes(bssid_nosep, &bssid_bytes);
227 writer = args[wpa_supplicant::kBSSPropertyBSSID].writer();
228 writer << bssid_bytes;
229
230 args[wpa_supplicant::kBSSPropertySignal].writer().append_int16(0);
231 args[wpa_supplicant::kBSSPropertyMode].writer().append_string(
232 wpa_supplicant::kNetworkModeInfrastructure);
233 // We indicate this is an open BSS by leaving out all security properties.
234
235 return new WiFiEndpoint(args);
236 }
mukesh agrawal7ec71312011-11-10 02:08:26 +0000237 MockWiFiServiceRefPtr MakeMockService() {
238 vector<uint8_t> ssid(1, 'a');
239 return new MockWiFiService(
240 &control_interface_,
241 &dispatcher_,
242 &manager_,
243 wifi_,
244 ssid,
245 flimflam::kModeManaged,
246 flimflam::kSecurityNone,
247 false);
248 }
mukesh agrawal31950242011-07-14 11:53:38 -0700249 void ReportBSS(const ::DBus::Path &bss_path,
250 const string &ssid,
251 const string &bssid,
252 int16_t signal_strength,
253 const char *mode);
mukesh agrawalf2f68a52011-09-01 12:15:48 -0700254 void ReportLinkUp() {
255 wifi_->LinkEvent(IFF_LOWER_UP, IFF_LOWER_UP);
256 }
mukesh agrawal31950242011-07-14 11:53:38 -0700257 void ReportScanDone() {
mukesh agrawaldc42bb32011-07-28 10:40:26 -0700258 wifi_->ScanDoneTask();
mukesh agrawal31950242011-07-14 11:53:38 -0700259 }
mukesh agrawal15908392011-11-16 18:29:25 +0000260 void ReportCurrentBSSChanged(const string &new_bss) {
261 wifi_->CurrentBSSChanged(new_bss);
262 }
mukesh agrawal7ec71312011-11-10 02:08:26 +0000263 void ReportStateChanged(const string &new_state) {
264 wifi_->StateChanged(new_state);
265 }
mukesh agrawal31950242011-07-14 11:53:38 -0700266 void StartWiFi() {
267 wifi_->Start();
mukesh agrawal31950242011-07-14 11:53:38 -0700268 }
269 void StopWiFi() {
270 wifi_->Stop();
271 }
Paul Stewarta41e38d2011-11-11 07:47:29 -0800272 void GetOpenService(const char *service_type,
mukesh agrawal7a4e4002011-09-06 11:26:05 -0700273 const char *ssid,
274 const char *mode,
Paul Stewartced6a0b2011-11-08 15:32:04 -0800275 Error *result) {
276 GetServiceInner(service_type, ssid, mode, NULL, NULL, false, result);
mukesh agrawal7a4e4002011-09-06 11:26:05 -0700277 }
278 void GetService(const char *service_type,
279 const char *ssid,
280 const char *mode,
281 const char *security,
282 const char *passphrase,
Paul Stewartced6a0b2011-11-08 15:32:04 -0800283 Error *result) {
284 GetServiceInner(service_type, ssid, mode, security, passphrase, false,
285 result);
286 }
287 WiFiServiceRefPtr GetServiceInner(const char *service_type,
288 const char *ssid,
289 const char *mode,
290 const char *security,
291 const char *passphrase,
Paul Stewarta41e38d2011-11-11 07:47:29 -0800292 bool allow_hidden,
Paul Stewartced6a0b2011-11-08 15:32:04 -0800293 Error *result) {
mukesh agrawal7a4e4002011-09-06 11:26:05 -0700294 map<string, ::DBus::Variant> args;
mukesh agrawal7a4e4002011-09-06 11:26:05 -0700295 // in general, we want to avoid D-Bus specific code for any RPCs
296 // that come in via adaptors. we make an exception here, because
297 // calls to GetWifiService are rerouted from the Manager object to
298 // the Wifi class.
299 if (service_type != NULL)
300 args[flimflam::kTypeProperty].writer().append_string(service_type);
301 if (ssid != NULL)
302 args[flimflam::kSSIDProperty].writer().append_string(ssid);
303 if (mode != NULL)
304 args[flimflam::kModeProperty].writer().append_string(mode);
305 if (security != NULL)
306 args[flimflam::kSecurityProperty].writer().append_string(security);
307 if (passphrase != NULL)
308 args[flimflam::kPassphraseProperty].writer().append_string(passphrase);
Paul Stewarta41e38d2011-11-11 07:47:29 -0800309 if (!allow_hidden)
310 args[flimflam::kWifiHiddenSsid].writer().append_bool(false);
mukesh agrawal7a4e4002011-09-06 11:26:05 -0700311
Paul Stewartced6a0b2011-11-08 15:32:04 -0800312 Error e;
313 KeyValueStore args_kv;
mukesh agrawal7a4e4002011-09-06 11:26:05 -0700314 DBusAdaptor::ArgsToKeyValueStore(args, &args_kv, &e);
Paul Stewartced6a0b2011-11-08 15:32:04 -0800315 return wifi_->GetService(args_kv, result);
mukesh agrawal7a4e4002011-09-06 11:26:05 -0700316 }
Paul Stewarta41e38d2011-11-11 07:47:29 -0800317 WiFiServiceRefPtr FindService(const vector<uint8_t> &ssid,
318 const string &mode,
319 const string &security) {
320 return wifi_->FindService(ssid, mode, security);
321 }
322 bool LoadHiddenServices(StoreInterface *storage) {
323 return wifi_->LoadHiddenServices(storage);
324 }
325 void SetupHiddenStorage(MockStore *storage, const string &ssid, string *id) {
326 const string hex_ssid = base::HexEncode(ssid.data(), ssid.size());
327 *id = StringToLowerASCII(base::StringPrintf("%s_%s_%s_%s_%s",
328 flimflam::kTypeWifi,
329 kDeviceAddress,
330 hex_ssid.c_str(),
331 flimflam::kModeManaged,
332 flimflam::kSecurityNone));
333 const char *groups[] = { id->c_str() };
334 EXPECT_CALL(*storage, GetGroupsWithKey(flimflam::kWifiHiddenSsid))
335 .WillRepeatedly(Return(set<string>(groups, groups + 1)));
336 EXPECT_CALL(*storage, GetBool(StrEq(*id), flimflam::kWifiHiddenSsid, _))
337 .WillRepeatedly(DoAll(SetArgumentPointee<2>(true), Return(true)));
338 EXPECT_CALL(*storage, GetString(StrEq(*id), flimflam::kSSIDProperty, _))
339 .WillRepeatedly(DoAll(SetArgumentPointee<2>(hex_ssid), Return(true)));
340 }
mukesh agrawal32399322011-09-01 10:53:43 -0700341 MockManager *manager() {
342 return &manager_;
343 }
344 const WiFiConstRefPtr wifi() const {
345 return wifi_;
346 }
347
348 EventDispatcher dispatcher_;
mukesh agrawal5c4dd0b2011-09-14 13:53:14 -0700349 NiceMock<MockRTNLHandler> rtnl_handler_;
mukesh agrawal31950242011-07-14 11:53:38 -0700350
351 private:
mukesh agrawaldc42bb32011-07-28 10:40:26 -0700352 NiceMockControl control_interface_;
mukesh agrawalf2f68a52011-09-01 12:15:48 -0700353 MockGLib glib_;
Chris Masone2ae797d2011-08-23 20:41:00 -0700354 MockManager manager_;
mukesh agrawal31950242011-07-14 11:53:38 -0700355 WiFiRefPtr wifi_;
mukesh agrawal31950242011-07-14 11:53:38 -0700356
357 // protected fields interspersed between private fields, due to
358 // initialization order
359 protected:
360 static const char kDeviceName[];
Chris Masone626719f2011-08-18 16:58:48 -0700361 static const char kDeviceAddress[];
mukesh agrawal31950242011-07-14 11:53:38 -0700362 static const char kNetworkModeAdHoc[];
363 static const char kNetworkModeInfrastructure[];
364
mukesh agrawaldc42bb32011-07-28 10:40:26 -0700365 scoped_ptr<MockSupplicantProcessProxy> supplicant_process_proxy_;
366 scoped_ptr<MockSupplicantInterfaceProxy> supplicant_interface_proxy_;
mukesh agrawalf2f68a52011-09-01 12:15:48 -0700367 MockDHCPProvider dhcp_provider_;
368 scoped_refptr<MockDHCPConfig> dhcp_config_;
mukesh agrawal31950242011-07-14 11:53:38 -0700369
370 private:
371 TestProxyFactory proxy_factory_;
mukesh agrawal31950242011-07-14 11:53:38 -0700372};
373
374const char WiFiMainTest::kDeviceName[] = "wlan0";
Paul Stewarta41e38d2011-11-11 07:47:29 -0800375const char WiFiMainTest::kDeviceAddress[] = "000102030405";
mukesh agrawal31950242011-07-14 11:53:38 -0700376const char WiFiMainTest::kNetworkModeAdHoc[] = "ad-hoc";
377const char WiFiMainTest::kNetworkModeInfrastructure[] = "infrastructure";
378
379void WiFiMainTest::ReportBSS(const ::DBus::Path &bss_path,
380 const string &ssid,
381 const string &bssid,
382 int16_t signal_strength,
383 const char *mode) {
384 map<string, ::DBus::Variant> bss_properties;
385
386 {
387 DBus::MessageIter writer(bss_properties["SSID"].writer());
388 writer << vector<uint8_t>(ssid.begin(), ssid.end());
389 }
390 {
391 string bssid_nosep;
392 vector<uint8_t> bssid_bytes;
393 RemoveChars(bssid, ":", &bssid_nosep);
394 base::HexStringToBytes(bssid_nosep, &bssid_bytes);
395
396 DBus::MessageIter writer(bss_properties["BSSID"].writer());
397 writer << bssid_bytes;
398 }
399 bss_properties["Signal"].writer().append_int16(signal_strength);
400 bss_properties["Mode"].writer().append_string(mode);
401 wifi_->BSSAdded(bss_path, bss_properties);
402}
403
404TEST_F(WiFiMainTest, ProxiesSetUpDuringStart) {
405 EXPECT_TRUE(GetSupplicantProcessProxy() == NULL);
406 EXPECT_TRUE(GetSupplicantInterfaceProxy() == NULL);
407
408 StartWiFi();
409 EXPECT_FALSE(GetSupplicantProcessProxy() == NULL);
410 EXPECT_FALSE(GetSupplicantInterfaceProxy() == NULL);
411}
412
413TEST_F(WiFiMainTest, CleanStart) {
414 EXPECT_CALL(*supplicant_process_proxy_, CreateInterface(_));
415 EXPECT_CALL(*supplicant_process_proxy_, GetInterface(_))
416 .Times(AnyNumber())
417 .WillRepeatedly(Throw(
418 DBus::Error(
419 "fi.w1.wpa_supplicant1.InterfaceUnknown",
420 "test threw fi.w1.wpa_supplicant1.InterfaceUnknown")));
421 EXPECT_CALL(*supplicant_interface_proxy_, Scan(_));
422 StartWiFi();
mukesh agrawal32399322011-09-01 10:53:43 -0700423 dispatcher_.DispatchPendingEvents();
mukesh agrawal31950242011-07-14 11:53:38 -0700424}
425
426TEST_F(WiFiMainTest, Restart) {
427 EXPECT_CALL(*supplicant_process_proxy_, CreateInterface(_))
428 .Times(AnyNumber())
429 .WillRepeatedly(Throw(
430 DBus::Error(
431 "fi.w1.wpa_supplicant1.InterfaceExists",
432 "test thew fi.w1.wpa_supplicant1.InterfaceExists")));
433 EXPECT_CALL(*supplicant_process_proxy_, GetInterface(_));
434 EXPECT_CALL(*supplicant_interface_proxy_, Scan(_));
435 StartWiFi();
mukesh agrawal32399322011-09-01 10:53:43 -0700436 dispatcher_.DispatchPendingEvents();
mukesh agrawal31950242011-07-14 11:53:38 -0700437}
438
439TEST_F(WiFiMainTest, StartClearsState) {
440 EXPECT_CALL(*supplicant_interface_proxy_, RemoveAllNetworks());
441 EXPECT_CALL(*supplicant_interface_proxy_, FlushBSS(_));
442 StartWiFi();
443}
444
445TEST_F(WiFiMainTest, ScanResults) {
446 StartWiFi();
447 ReportBSS("bss0", "ssid0", "00:00:00:00:00:00", 0, kNetworkModeAdHoc);
448 ReportBSS(
449 "bss1", "ssid1", "00:00:00:00:00:01", 1, kNetworkModeInfrastructure);
450 ReportBSS(
451 "bss2", "ssid2", "00:00:00:00:00:02", 2, kNetworkModeInfrastructure);
452 ReportBSS(
453 "bss3", "ssid3", "00:00:00:00:00:03", 3, kNetworkModeInfrastructure);
454 ReportBSS("bss4", "ssid4", "00:00:00:00:00:04", 4, kNetworkModeAdHoc);
455 EXPECT_EQ(5, GetEndpointMap().size());
456}
457
458TEST_F(WiFiMainTest, ScanResultsWithUpdates) {
459 StartWiFi();
460 ReportBSS("bss0", "ssid0", "00:00:00:00:00:00", 0, kNetworkModeAdHoc);
461 ReportBSS(
462 "bss1", "ssid1", "00:00:00:00:00:01", 1, kNetworkModeInfrastructure);
463 ReportBSS(
464 "bss2", "ssid2", "00:00:00:00:00:02", 2, kNetworkModeInfrastructure);
465 ReportBSS(
466 "bss1", "ssid1", "00:00:00:00:00:01", 3, kNetworkModeInfrastructure);
467 ReportBSS("bss0", "ssid0", "00:00:00:00:00:00", 4, kNetworkModeAdHoc);
468 EXPECT_EQ(3, GetEndpointMap().size());
469 ASSERT_TRUE(ContainsKey(GetEndpointMap(), "000000000000"));
470 EXPECT_EQ(4, GetEndpointMap().find("000000000000")->second->
471 signal_strength());
472}
473
474TEST_F(WiFiMainTest, ScanCompleted) {
475 StartWiFi();
476 ReportBSS("bss0", "ssid0", "00:00:00:00:00:00", 0, kNetworkModeAdHoc);
477 ReportBSS(
478 "bss1", "ssid1", "00:00:00:00:00:01", 1, kNetworkModeInfrastructure);
479 ReportBSS(
480 "bss2", "ssid2", "00:00:00:00:00:02", 2, kNetworkModeInfrastructure);
mukesh agrawal32399322011-09-01 10:53:43 -0700481 EXPECT_CALL(*manager(), RegisterService(_))
482 .Times(3);
mukesh agrawal31950242011-07-14 11:53:38 -0700483 ReportScanDone();
484 EXPECT_EQ(3, GetServiceMap().size());
485}
486
487TEST_F(WiFiMainTest, Connect) {
mukesh agrawaldc42bb32011-07-28 10:40:26 -0700488 MockSupplicantInterfaceProxy &supplicant_interface_proxy =
489 *supplicant_interface_proxy_;
490
mukesh agrawal31950242011-07-14 11:53:38 -0700491 StartWiFi();
492 ReportBSS("bss0", "ssid0", "00:00:00:00:00:00", 0, kNetworkModeAdHoc);
493 ReportScanDone();
494
495 {
496 InSequence s;
497 DBus::Path fake_path("/fake/path");
mukesh agrawal32399322011-09-01 10:53:43 -0700498 WiFiService *service(GetServiceMap().begin()->second);
mukesh agrawal31950242011-07-14 11:53:38 -0700499
mukesh agrawaldc42bb32011-07-28 10:40:26 -0700500 EXPECT_CALL(supplicant_interface_proxy, AddNetwork(_))
mukesh agrawal31950242011-07-14 11:53:38 -0700501 .WillOnce(Return(fake_path));
mukesh agrawaldc42bb32011-07-28 10:40:26 -0700502 EXPECT_CALL(supplicant_interface_proxy, SelectNetwork(fake_path));
mukesh agrawal32399322011-09-01 10:53:43 -0700503 InitiateConnect(service);
mukesh agrawalf2f68a52011-09-01 12:15:48 -0700504 EXPECT_EQ(static_cast<Service *>(service),
505 wifi()->selected_service_.get());
mukesh agrawal31950242011-07-14 11:53:38 -0700506 }
507}
508
mukesh agrawalf2f68a52011-09-01 12:15:48 -0700509TEST_F(WiFiMainTest, LinkEvent) {
510 EXPECT_FALSE(IsLinkUp());
511 EXPECT_CALL(dhcp_provider_, CreateConfig(_)).
512 WillOnce(Return(dhcp_config_));
513 ReportLinkUp();
514}
515
mukesh agrawal5c4dd0b2011-09-14 13:53:14 -0700516TEST_F(WiFiMainTest, Stop) {
517 {
518 InSequence s;
519
520 StartWiFi();
521 ReportBSS("bss0", "ssid0", "00:00:00:00:00:00", 0, kNetworkModeAdHoc);
522 ReportScanDone();
523 EXPECT_CALL(dhcp_provider_, CreateConfig(_)).
524 WillOnce(Return(dhcp_config_));
525 ReportLinkUp();
526 }
527
528 {
529 EXPECT_CALL(*manager(), DeregisterService(_));
530 StopWiFi();
531 }
532}
533
mukesh agrawal7a4e4002011-09-06 11:26:05 -0700534TEST_F(WiFiMainTest, GetWifiServiceOpen) {
535 Error e;
Paul Stewarta41e38d2011-11-11 07:47:29 -0800536 GetOpenService(flimflam::kTypeWifi, "an_ssid", flimflam::kModeManaged, &e);
mukesh agrawal7a4e4002011-09-06 11:26:05 -0700537 EXPECT_TRUE(e.IsSuccess());
538}
539
540TEST_F(WiFiMainTest, GetWifiServiceOpenNoType) {
541 Error e;
Paul Stewart6ab23a92011-11-09 17:17:47 -0800542 GetOpenService(NULL, "an_ssid", flimflam::kModeManaged, &e);
mukesh agrawal7a4e4002011-09-06 11:26:05 -0700543 EXPECT_EQ(Error::kInvalidArguments, e.type());
544 EXPECT_EQ("must specify service type", e.message());
545}
546
547TEST_F(WiFiMainTest, GetWifiServiceOpenNoSSID) {
548 Error e;
Paul Stewarta41e38d2011-11-11 07:47:29 -0800549 GetOpenService(flimflam::kTypeWifi, NULL, flimflam::kModeManaged, &e);
mukesh agrawal7a4e4002011-09-06 11:26:05 -0700550 EXPECT_EQ(Error::kInvalidArguments, e.type());
551 EXPECT_EQ("must specify SSID", e.message());
552}
553
554TEST_F(WiFiMainTest, GetWifiServiceOpenLongSSID) {
555 Error e;
556 GetOpenService(
Paul Stewarta41e38d2011-11-11 07:47:29 -0800557 flimflam::kTypeWifi, "123456789012345678901234567890123",
558 flimflam::kModeManaged, &e);
mukesh agrawal7a4e4002011-09-06 11:26:05 -0700559 EXPECT_EQ(Error::kInvalidNetworkName, e.type());
560 EXPECT_EQ("SSID is too long", e.message());
561}
562
563TEST_F(WiFiMainTest, GetWifiServiceOpenShortSSID) {
564 Error e;
Paul Stewarta41e38d2011-11-11 07:47:29 -0800565 GetOpenService(flimflam::kTypeWifi, "", flimflam::kModeManaged, &e);
mukesh agrawal7a4e4002011-09-06 11:26:05 -0700566 EXPECT_EQ(Error::kInvalidNetworkName, e.type());
567 EXPECT_EQ("SSID is too short", e.message());
568}
569
570TEST_F(WiFiMainTest, GetWifiServiceOpenBadMode) {
571 Error e;
Paul Stewarta41e38d2011-11-11 07:47:29 -0800572 GetOpenService(flimflam::kTypeWifi, "an_ssid", "ad-hoc", &e);
mukesh agrawal7a4e4002011-09-06 11:26:05 -0700573 EXPECT_EQ(Error::kNotSupported, e.type());
574 EXPECT_EQ("service mode is unsupported", e.message());
575}
576
577TEST_F(WiFiMainTest, GetWifiServiceOpenNoMode) {
578 Error e;
Paul Stewarta41e38d2011-11-11 07:47:29 -0800579 GetOpenService(flimflam::kTypeWifi, "an_ssid", NULL, &e);
mukesh agrawal7a4e4002011-09-06 11:26:05 -0700580 EXPECT_TRUE(e.IsSuccess());
581}
582
583TEST_F(WiFiMainTest, GetWifiServiceRSN) {
584 Error e;
Paul Stewarta41e38d2011-11-11 07:47:29 -0800585 GetService(flimflam::kTypeWifi, "an_ssid", flimflam::kModeManaged,
586 flimflam::kSecurityRsn, "secure password", &e);
mukesh agrawal7a4e4002011-09-06 11:26:05 -0700587 EXPECT_TRUE(e.IsSuccess());
588}
589
590TEST_F(WiFiMainTest, GetWifiServiceRSNNoPassword) {
591 Error e;
Paul Stewarta41e38d2011-11-11 07:47:29 -0800592 GetService(flimflam::kTypeWifi, "an_ssid", flimflam::kModeManaged,
593 flimflam::kSecurityRsn, NULL, &e);
mukesh agrawal7a4e4002011-09-06 11:26:05 -0700594 EXPECT_EQ(Error::kInvalidArguments, e.type());
595 EXPECT_EQ("must specify passphrase", e.message());
596}
597
598TEST_F(WiFiMainTest, GetWifiServiceBadSecurity) {
599 Error e;
Paul Stewarta41e38d2011-11-11 07:47:29 -0800600 GetService(flimflam::kTypeWifi, "an_ssid", flimflam::kModeManaged, "rot-13",
601 NULL, &e);
mukesh agrawal7a4e4002011-09-06 11:26:05 -0700602 EXPECT_EQ(Error::kNotSupported, e.type());
603 EXPECT_EQ("security mode is unsupported", e.message());
604}
605
606TEST_F(WiFiMainTest, GetWifiServiceWEPNoPassword) {
607 Error e;
Paul Stewarta41e38d2011-11-11 07:47:29 -0800608 GetService(flimflam::kTypeWifi, "an_ssid", flimflam::kModeManaged,
609 flimflam::kSecurityWep, NULL, &e);
mukesh agrawal7a4e4002011-09-06 11:26:05 -0700610 EXPECT_EQ(Error::kInvalidArguments, e.type());
611 EXPECT_EQ("must specify passphrase", e.message());
612}
613
614TEST_F(WiFiMainTest, GetWifiServiceWEPEmptyPassword) {
615 Error e;
Paul Stewarta41e38d2011-11-11 07:47:29 -0800616 GetService(flimflam::kTypeWifi, "an_ssid", flimflam::kModeManaged,
617 flimflam::kSecurityWep, "", &e);
mukesh agrawal7a4e4002011-09-06 11:26:05 -0700618 EXPECT_EQ(Error::kInvalidPassphrase, e.type());
619}
620
621TEST_F(WiFiMainTest, GetWifiServiceWEP40ASCII) {
622 Error e;
Paul Stewarta41e38d2011-11-11 07:47:29 -0800623 GetService(flimflam::kTypeWifi, "an_ssid", flimflam::kModeManaged,
624 flimflam::kSecurityWep, "abcde", &e);
mukesh agrawal7a4e4002011-09-06 11:26:05 -0700625 EXPECT_TRUE(e.IsSuccess());
626}
627
628TEST_F(WiFiMainTest, GetWifiServiceWEP104ASCII) {
629 Error e;
Paul Stewarta41e38d2011-11-11 07:47:29 -0800630 GetService(flimflam::kTypeWifi, "an_ssid", flimflam::kModeManaged,
631 flimflam::kSecurityWep, "abcdefghijklm", &e);
mukesh agrawal7a4e4002011-09-06 11:26:05 -0700632 EXPECT_TRUE(e.IsSuccess());
633}
634
635TEST_F(WiFiMainTest, GetWifiServiceWEP40ASCIIWithKeyIndex) {
636 Error e;
Paul Stewarta41e38d2011-11-11 07:47:29 -0800637 GetService(flimflam::kTypeWifi, "an_ssid", flimflam::kModeManaged,
638 flimflam::kSecurityWep, "0:abcdefghijklm", &e);
mukesh agrawal7a4e4002011-09-06 11:26:05 -0700639 EXPECT_TRUE(e.IsSuccess());
640}
641
642TEST_F(WiFiMainTest, GetWifiServiceWEP40Hex) {
643 Error e;
Paul Stewarta41e38d2011-11-11 07:47:29 -0800644 GetService(flimflam::kTypeWifi, "an_ssid", flimflam::kModeManaged,
645 flimflam::kSecurityWep, "0102030405", &e);
mukesh agrawal7a4e4002011-09-06 11:26:05 -0700646 EXPECT_TRUE(e.IsSuccess());
647}
648
649TEST_F(WiFiMainTest, GetWifiServiceWEP40HexBadPassphrase) {
650 Error e;
Paul Stewarta41e38d2011-11-11 07:47:29 -0800651 GetService(flimflam::kTypeWifi, "an_ssid", flimflam::kModeManaged,
652 flimflam::kSecurityWep, "O102030405", &e);
mukesh agrawal7a4e4002011-09-06 11:26:05 -0700653 EXPECT_EQ(Error::kInvalidPassphrase, e.type());
654}
655
656TEST_F(WiFiMainTest, GetWifiServiceWEP40HexWithKeyIndexBadPassphrase) {
657 Error e;
Paul Stewarta41e38d2011-11-11 07:47:29 -0800658 GetService(flimflam::kTypeWifi, "an_ssid", flimflam::kModeManaged,
659 flimflam::kSecurityWep, "1:O102030405", &e);
mukesh agrawal7a4e4002011-09-06 11:26:05 -0700660 EXPECT_EQ(Error::kInvalidPassphrase, e.type());
661}
662
663TEST_F(WiFiMainTest, GetWifiServiceWEP40HexWithKeyIndexAndBaseBadPassphrase) {
664 Error e;
Paul Stewarta41e38d2011-11-11 07:47:29 -0800665 GetService(flimflam::kTypeWifi, "an_ssid", flimflam::kModeManaged,
666 flimflam::kSecurityWep, "1:0xO102030405", &e);
mukesh agrawal7a4e4002011-09-06 11:26:05 -0700667 EXPECT_EQ(Error::kInvalidPassphrase, e.type());
668}
669
670TEST_F(WiFiMainTest, GetWifiServiceWEP40HexWithBaseBadPassphrase) {
671 Error e;
Paul Stewarta41e38d2011-11-11 07:47:29 -0800672 GetService(flimflam::kTypeWifi, "an_ssid", flimflam::kModeManaged,
673 flimflam::kSecurityWep, "0xO102030405", &e);
mukesh agrawal7a4e4002011-09-06 11:26:05 -0700674 EXPECT_EQ(Error::kInvalidPassphrase, e.type());
675}
676
677TEST_F(WiFiMainTest, GetWifiServiceWEP104Hex) {
678 Error e;
Paul Stewarta41e38d2011-11-11 07:47:29 -0800679 GetService(flimflam::kTypeWifi, "an_ssid", flimflam::kModeManaged,
680 flimflam::kSecurityWep, "0102030405060708090a0b0c0d", &e);
mukesh agrawal7a4e4002011-09-06 11:26:05 -0700681 EXPECT_TRUE(e.IsSuccess());
682}
683
684TEST_F(WiFiMainTest, GetWifiServiceWEP104HexUppercase) {
685 Error e;
Paul Stewarta41e38d2011-11-11 07:47:29 -0800686 GetService(flimflam::kTypeWifi, "an_ssid", flimflam::kModeManaged,
687 flimflam::kSecurityWep, "0102030405060708090A0B0C0D", &e);
mukesh agrawal7a4e4002011-09-06 11:26:05 -0700688 EXPECT_TRUE(e.IsSuccess());
689}
690
691TEST_F(WiFiMainTest, GetWifiServiceWEP104HexWithKeyIndex) {
692 Error e;
Paul Stewarta41e38d2011-11-11 07:47:29 -0800693 GetService(flimflam::kTypeWifi, "an_ssid", flimflam::kModeManaged,
694 flimflam::kSecurityWep, "0:0102030405060708090a0b0c0d", &e);
mukesh agrawal7a4e4002011-09-06 11:26:05 -0700695 EXPECT_TRUE(e.IsSuccess());
696}
697
698TEST_F(WiFiMainTest, GetWifiServiceWEP104HexWithKeyIndexAndBase) {
699 Error e;
Paul Stewarta41e38d2011-11-11 07:47:29 -0800700 GetService(flimflam::kTypeWifi, "an_ssid", flimflam::kModeManaged,
701 flimflam::kSecurityWep, "0:0x0102030405060708090a0b0c0d", &e);
mukesh agrawal7a4e4002011-09-06 11:26:05 -0700702 EXPECT_TRUE(e.IsSuccess());
703}
704
mukesh agrawal8ede0522011-10-03 14:57:44 -0700705class WiFiGetServiceSuccessTest : public WiFiMainTest {};
706class WiFiGetServiceFailureTest : public WiFiMainTest {};
707
708TEST_P(WiFiGetServiceSuccessTest, Passphrase) {
709 Error e;
Paul Stewarta41e38d2011-11-11 07:47:29 -0800710 GetService(flimflam::kTypeWifi, "an_ssid", flimflam::kModeManaged,
711 flimflam::kSecurityWpa, GetParam().c_str(), &e);
mukesh agrawal8ede0522011-10-03 14:57:44 -0700712 EXPECT_TRUE(e.IsSuccess());
713}
714
715TEST_P(WiFiGetServiceFailureTest, Passphrase) {
716 Error e;
Paul Stewarta41e38d2011-11-11 07:47:29 -0800717 GetService(flimflam::kTypeWifi, "an_ssid", flimflam::kModeManaged,
718 flimflam::kSecurityWpa, GetParam().c_str(), &e);
mukesh agrawal8ede0522011-10-03 14:57:44 -0700719 EXPECT_EQ(Error::kInvalidPassphrase, e.type());
720}
721
722INSTANTIATE_TEST_CASE_P(
723 WiFiGetServiceSuccessTestInstance,
724 WiFiGetServiceSuccessTest,
725 Values(
726 string(IEEE_80211::kWPAAsciiMinLen, 'Z'),
727 string(IEEE_80211::kWPAAsciiMaxLen, 'Z'),
728 // subtle: invalid length for hex key, but valid as ascii passphrase
729 string(IEEE_80211::kWPAHexLen-1, '1'),
730 string(IEEE_80211::kWPAHexLen, '1')));
731
732INSTANTIATE_TEST_CASE_P(
733 WiFiGetServiceFailureTestInstance,
734 WiFiGetServiceFailureTest,
735 Values(
736 string(IEEE_80211::kWPAAsciiMinLen-1, 'Z'),
737 string(IEEE_80211::kWPAAsciiMaxLen+1, 'Z'),
738 string(IEEE_80211::kWPAHexLen+1, '1')));
739
Paul Stewart6ab23a92011-11-09 17:17:47 -0800740TEST_F(WiFiMainTest, FindServiceWEP) {
741 const string ssid("an_ssid");
742 {
743 Error e;
Paul Stewarta41e38d2011-11-11 07:47:29 -0800744 GetService(flimflam::kTypeWifi, ssid.c_str(), flimflam::kModeManaged,
Paul Stewart6ab23a92011-11-09 17:17:47 -0800745 flimflam::kSecurityWep, "abcde", &e);
746 EXPECT_TRUE(e.IsSuccess());
747 }
748 vector<uint8_t> ssid_bytes(ssid.begin(), ssid.end());
749
Paul Stewarta41e38d2011-11-11 07:47:29 -0800750 EXPECT_TRUE(FindService(ssid_bytes, flimflam::kModeManaged,
751 flimflam::kSecurityWep).get());
752 EXPECT_FALSE(FindService(ssid_bytes, flimflam::kModeManaged,
753 flimflam::kSecurityWpa).get());
Paul Stewart6ab23a92011-11-09 17:17:47 -0800754}
755
756TEST_F(WiFiMainTest, FindServiceWPA) {
757 const string ssid("an_ssid");
758 {
759 Error e;
Paul Stewarta41e38d2011-11-11 07:47:29 -0800760 GetService(flimflam::kTypeWifi, ssid.c_str(), flimflam::kModeManaged,
Paul Stewart6ab23a92011-11-09 17:17:47 -0800761 flimflam::kSecurityRsn, "abcdefgh", &e);
762 EXPECT_TRUE(e.IsSuccess());
763 }
764 vector<uint8_t> ssid_bytes(ssid.begin(), ssid.end());
Paul Stewarta41e38d2011-11-11 07:47:29 -0800765 WiFiServiceRefPtr wpa_service(FindService(ssid_bytes, flimflam::kModeManaged,
766 flimflam::kSecurityWpa));
Paul Stewart6ab23a92011-11-09 17:17:47 -0800767 EXPECT_TRUE(wpa_service.get());
Paul Stewarta41e38d2011-11-11 07:47:29 -0800768 WiFiServiceRefPtr rsn_service(FindService(ssid_bytes, flimflam::kModeManaged,
769 flimflam::kSecurityRsn));
Paul Stewart6ab23a92011-11-09 17:17:47 -0800770 EXPECT_TRUE(rsn_service.get());
771 EXPECT_EQ(wpa_service.get(), rsn_service.get());
Paul Stewarta41e38d2011-11-11 07:47:29 -0800772 WiFiServiceRefPtr psk_service(FindService(ssid_bytes, flimflam::kModeManaged,
773 flimflam::kSecurityPsk));
Paul Stewart6ab23a92011-11-09 17:17:47 -0800774 EXPECT_EQ(wpa_service.get(), psk_service.get());
775 // Indirectly test FindService by doing a GetService on something that
776 // already exists.
777 {
778 Error e;
779 WiFiServiceRefPtr wpa_service2(
Paul Stewarta41e38d2011-11-11 07:47:29 -0800780 GetServiceInner(flimflam::kTypeWifi, ssid.c_str(),
781 flimflam::kModeManaged, flimflam::kSecurityWpa,
782 "abcdefgh", false, &e));
Paul Stewart6ab23a92011-11-09 17:17:47 -0800783 EXPECT_TRUE(e.IsSuccess());
784 EXPECT_EQ(wpa_service.get(), wpa_service2.get());
785 }
786}
787
Paul Stewartced6a0b2011-11-08 15:32:04 -0800788MATCHER_P(HasHiddenSSID, ssid, "") {
Paul Stewarta41e38d2011-11-11 07:47:29 -0800789 map<string, DBus::Variant>::const_iterator it =
Paul Stewartced6a0b2011-11-08 15:32:04 -0800790 arg.find(wpa_supplicant::kPropertyScanSSIDs);
791 if (it == arg.end()) {
792 return false;
793 }
794
795 const DBus::Variant &ssids_variant = it->second;
796 EXPECT_TRUE(DBusAdaptor::IsByteArrays(ssids_variant.signature()));
797 const ByteArrays &ssids = it->second.operator ByteArrays();
798 // A valid Scan containing a single hidden SSID should contain
799 // two SSID entries: one containing the SSID we are looking for,
800 // and an empty entry, signifying that we also want to do a
801 // broadcast probe request for all non-hidden APs as well.
802 return ssids.size() == 2 &&
803 string(ssids[0].begin(), ssids[0].end()) == ssid &&
804 ssids[1].empty();
805}
806
807TEST_F(WiFiMainTest, ScanHidden) {
808 EXPECT_CALL(*supplicant_process_proxy_, CreateInterface(_));
809 EXPECT_CALL(*supplicant_process_proxy_, GetInterface(_))
810 .Times(AnyNumber())
811 .WillRepeatedly(Throw(
812 DBus::Error(
813 "fi.w1.wpa_supplicant1.InterfaceUnknown",
814 "test threw fi.w1.wpa_supplicant1.InterfaceUnknown")));
Paul Stewarta41e38d2011-11-11 07:47:29 -0800815 {
816 // Create a hidden, favorite service.
817 Error e;
818 WiFiServiceRefPtr service =
819 GetServiceInner(flimflam::kTypeWifi, "ssid0", flimflam::kModeManaged,
820 NULL, NULL, true, &e);
821 EXPECT_TRUE(e.IsSuccess());
822 EXPECT_TRUE(service->hidden_ssid());
823 service->set_favorite(true);
824 }
825 {
826 // Create a hidden, non-favorite service.
827 Error e;
828 WiFiServiceRefPtr service =
829 GetServiceInner(flimflam::kTypeWifi, "ssid1", flimflam::kModeManaged,
830 NULL, NULL, true, &e);
831 EXPECT_TRUE(e.IsSuccess());
832 EXPECT_TRUE(service->hidden_ssid());
833 }
834 {
835 // Create a non-hidden, favorite service.
836 Error e;
837 WiFiServiceRefPtr service =
838 GetServiceInner(flimflam::kTypeWifi, "ssid2", flimflam::kModeManaged,
839 NULL, NULL, false, &e);
840 EXPECT_TRUE(e.IsSuccess());
841 EXPECT_FALSE(service->hidden_ssid());
842 service->set_favorite(true);
843 }
844 EXPECT_CALL(*supplicant_interface_proxy_, Scan(HasHiddenSSID("ssid0")));
Paul Stewartced6a0b2011-11-08 15:32:04 -0800845 StartWiFi();
846 dispatcher_.DispatchPendingEvents();
847}
848
mukesh agrawal7ec71312011-11-10 02:08:26 +0000849TEST_F(WiFiMainTest, InitialSupplicantState) {
850 EXPECT_EQ(WiFi::kInterfaceStateUnknown, GetSupplicantState());
851}
852
853TEST_F(WiFiMainTest, StateChangeNoService) {
854 // State change should succeed even if there is no pending Service.
855 ReportStateChanged(wpa_supplicant::kInterfaceStateScanning);
856 EXPECT_EQ(wpa_supplicant::kInterfaceStateScanning, GetSupplicantState());
857}
858
859TEST_F(WiFiMainTest, StateChangeWithService) {
860 // Forward transition should trigger a Service state change.
861 StartWiFi();
862 dispatcher_.DispatchPendingEvents();
863 MockWiFiServiceRefPtr service = MakeMockService();
864 InitiateConnect(service);
865 EXPECT_CALL(*service.get(), SetState(Service::kStateAssociating));
866 ReportStateChanged(wpa_supplicant::kInterfaceStateAssociated);
867 // Verify expectations now, because WiFi may report other state changes
868 // when WiFi is Stop()-ed (during TearDown()).
869 Mock::VerifyAndClearExpectations(service.get());
870}
871
872TEST_F(WiFiMainTest, StateChangeBackwardsWithService) {
873 // Some backwards transitions should not trigger a Service state change.
874 // Supplicant state should still be updated, however.
875 StartWiFi();
876 dispatcher_.DispatchPendingEvents();
877 MockWiFiServiceRefPtr service = MakeMockService();
878 InitiateConnect(service);
879 ReportStateChanged(wpa_supplicant::kInterfaceStateCompleted);
880 EXPECT_CALL(*service.get(), SetState(_)).Times(0);
881 ReportStateChanged(wpa_supplicant::kInterfaceStateAuthenticating);
882 EXPECT_EQ(wpa_supplicant::kInterfaceStateAuthenticating,
883 GetSupplicantState());
884 // Verify expectations now, because WiFi may report other state changes
885 // when WiFi is Stop()-ed (during TearDown()).
886 Mock::VerifyAndClearExpectations(service.get());
887}
888
Paul Stewarta41e38d2011-11-11 07:47:29 -0800889TEST_F(WiFiMainTest, LoadHiddenServicesFailWithNoGroups) {
890 StrictMock<MockStore> storage;
891 EXPECT_CALL(storage, GetGroupsWithKey(flimflam::kWifiHiddenSsid))
892 .WillOnce(Return(set<string>()));
893 EXPECT_FALSE(LoadHiddenServices(&storage));
894}
895
896TEST_F(WiFiMainTest, LoadHiddenServicesFailWithMissingHidden) {
897 string id;
898 StrictMock<MockStore> storage;
899 SetupHiddenStorage(&storage, "an_ssid", &id);
900 // Missing "Hidden" property.
901 EXPECT_CALL(storage, GetBool(StrEq(id), flimflam::kWifiHiddenSsid, _))
902 .WillOnce(Return(false));
903 EXPECT_FALSE(LoadHiddenServices(&storage));
904}
905
906TEST_F(WiFiMainTest, LoadHiddenServicesFailWithFalseHidden) {
907 string id;
908 StrictMock<MockStore> storage;
909 SetupHiddenStorage(&storage, "an_ssid", &id);
910 // "Hidden" property set to "false".
911 EXPECT_CALL(storage, GetBool(StrEq(id), flimflam::kWifiHiddenSsid, _))
912 .WillOnce(DoAll(SetArgumentPointee<2>(true), Return(false)));
913 EXPECT_FALSE(LoadHiddenServices(&storage));
914}
915
916TEST_F(WiFiMainTest, LoadHiddenServicesFailWithMissingSSID) {
917 string id;
918 StrictMock<MockStore> storage;
919 SetupHiddenStorage(&storage, "an_ssid", &id);
920 // Missing "SSID" property.
921 EXPECT_CALL(storage, GetString(StrEq(id), flimflam::kSSIDProperty, _))
922 .WillOnce(Return(false));
923 EXPECT_FALSE(LoadHiddenServices(&storage));
924}
925
926
927TEST_F(WiFiMainTest, LoadHiddenServicesFailWithFoundService) {
928 StrictMock<MockStore> storage;
929 string id;
930 SetupHiddenStorage(&storage, "an_ssid", &id);
931 Error e;
932 GetOpenService(flimflam::kTypeWifi, "an_ssid", NULL, &e);
933 ASSERT_TRUE(e.IsSuccess());
934 EXPECT_FALSE(LoadHiddenServices(&storage));
935}
936
937TEST_F(WiFiMainTest, LoadHiddenServicesSuccess) {
938 StrictMock<MockStore> storage;
939 string ssid("an_ssid");
940 string id;
941 SetupHiddenStorage(&storage, ssid, &id);
942 EXPECT_TRUE(LoadHiddenServices(&storage));
943 vector<uint8_t> ssid_bytes(ssid.begin(), ssid.end());
944 EXPECT_TRUE(FindService(ssid_bytes, flimflam::kModeManaged,
945 flimflam::kSecurityNone).get());
946}
947
mukesh agrawal15908392011-11-16 18:29:25 +0000948TEST_F(WiFiMainTest, CurrentBSSChangeConnectedToDisconnected) {
949 WiFiEndpointRefPtr ap = MakeEndpoint("an_ssid", "00:01:02:03:04:05");
950 WiFiServiceRefPtr service = CreateServiceForEndpoint(*ap);
951
952 // Note that the BSS handle used in this test ("an_ap") is not
953 // intended to reflect the format used by supplicant. It's just
954 // convenient for testing.
955
956 StartWiFi();
957 ReportBSS("an_ap", ap->ssid_string(), ap->bssid_string(), 0,
958 kNetworkModeInfrastructure);
959 InitiateConnect(service);
960 EXPECT_EQ(service, GetPendingService().get());
961
962 ReportCurrentBSSChanged("an_ap");
963 ReportStateChanged(wpa_supplicant::kInterfaceStateCompleted);
964 EXPECT_EQ(Service::kStateConfiguring, service->state());
965 EXPECT_EQ(service, GetCurrentService().get());
966 EXPECT_EQ(NULL, GetPendingService().get());
967
968 ReportCurrentBSSChanged(wpa_supplicant::kCurrentBSSNull);
969 EXPECT_EQ(Service::kStateIdle, service->state());
970 EXPECT_EQ(NULL, GetCurrentService().get());
971 EXPECT_EQ(NULL, GetPendingService().get());
972}
973
974TEST_F(WiFiMainTest, CurrentBSSChangeConnectedToConnectedNewService) {
975 WiFiEndpointRefPtr ap1 = MakeEndpoint("an_ssid", "00:01:02:03:04:05");
976 WiFiEndpointRefPtr ap2 = MakeEndpoint("another_ssid", "01:02:03:04:05:06");
977 WiFiServiceRefPtr service1 = CreateServiceForEndpoint(*ap1);
978 WiFiServiceRefPtr service2 = CreateServiceForEndpoint(*ap2);
979
980 // Note that the BSS handles used in this test ("ap1", "ap2") are
981 // not intended to reflect the format used by supplicant. They're
982 // just convenient for testing.
983
984 StartWiFi();
985 ReportBSS("ap1", ap1->ssid_string(), ap1->bssid_string(), 0,
986 kNetworkModeInfrastructure);
987 ReportBSS("ap2", ap2->ssid_string(), ap2->bssid_string(), 0,
988 kNetworkModeInfrastructure);
989 InitiateConnect(service1);
990 ReportCurrentBSSChanged("ap1");
991 ReportStateChanged(wpa_supplicant::kInterfaceStateCompleted);
992 EXPECT_EQ(service1.get(), GetCurrentService().get());
993
994 ReportCurrentBSSChanged("ap2");
995 ReportStateChanged(wpa_supplicant::kInterfaceStateCompleted);
996 EXPECT_EQ(service2.get(), GetCurrentService().get());
997 EXPECT_EQ(Service::kStateIdle, service1->state());
998 EXPECT_EQ(Service::kStateConfiguring, service2->state());
999}
1000
1001TEST_F(WiFiMainTest, CurrentBSSChangeDisconnectedToConnected) {
1002 WiFiEndpointRefPtr ap = MakeEndpoint("an_ssid", "00:01:02:03:04:05");
1003 WiFiServiceRefPtr service = CreateServiceForEndpoint(*ap);
1004
1005 // Note that the BSS handle used in this test ("an_ap") is not
1006 // intended to reflect the format used by supplicant. It's just
1007 // convenient for testing.
1008
1009 StartWiFi();
1010 ReportBSS("an_ap", ap->ssid_string(), ap->bssid_string(), 0,
1011 kNetworkModeInfrastructure);
1012 InitiateConnect(service);
1013 ReportCurrentBSSChanged("an_ap");
1014 ReportStateChanged(wpa_supplicant::kInterfaceStateCompleted);
1015 EXPECT_EQ(service.get(), GetCurrentService().get());
1016 EXPECT_EQ(Service::kStateConfiguring, service->state());
1017}
1018
Chris Masone853b81b2011-06-24 14:11:41 -07001019} // namespace shill