blob: 1d02151e2d0d56d9718adb9692e55b48078535f3 [file] [log] [blame]
mukesh agrawal8a3188d2011-12-01 20:56:44 +00001// Copyright (c) 2012 The Chromium OS Authors. All rights reserved.
Chris Masone34af2182011-08-22 11:59:36 -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_service.h"
6
mukesh agrawald835b202011-10-07 15:26:47 -07007#include <map>
Paul Stewart85aea152013-01-22 09:31:56 -08008#include <set>
Chris Masone34af2182011-08-22 11:59:36 -07009#include <string>
10#include <vector>
11
Paul Stewart85aea152013-01-22 09:31:56 -080012#include <base/stringprintf.h>
Paul Stewart71a4d3b2013-01-18 18:12:56 -080013#include <base/string_number_conversions.h>
Chris Masone34af2182011-08-22 11:59:36 -070014#include <base/string_util.h>
15#include <chromeos/dbus/service_constants.h>
16#include <gmock/gmock.h>
17#include <gtest/gtest.h>
18
Paul Stewart26b327e2011-10-19 11:38:09 -070019#include "shill/event_dispatcher.h"
Chris Masone34af2182011-08-22 11:59:36 -070020#include "shill/manager.h"
mukesh agrawalf6b32092013-04-10 15:49:55 -070021#include "shill/metrics.h"
Chris Masone34af2182011-08-22 11:59:36 -070022#include "shill/mock_adaptors.h"
Paul Stewart5baebb72013-03-14 11:43:29 -070023#include "shill/mock_certificate_file.h"
Chris Masone34af2182011-08-22 11:59:36 -070024#include "shill/mock_control.h"
Paul Stewart71a4d3b2013-01-18 18:12:56 -080025#include "shill/mock_log.h"
Paul Stewartecf4cd12012-04-17 11:08:39 -070026#include "shill/mock_nss.h"
Christopher Wiley1ce658d2012-10-10 10:02:03 -070027#include "shill/mock_profile.h"
Chris Masone34af2182011-08-22 11:59:36 -070028#include "shill/mock_service.h"
29#include "shill/mock_store.h"
mukesh agrawal6e277772011-09-29 15:04:23 -070030#include "shill/mock_wifi.h"
Paul Stewart3c504012013-01-17 17:49:58 -080031#include "shill/mock_wifi_provider.h"
Chris Masone34af2182011-08-22 11:59:36 -070032#include "shill/property_store_unittest.h"
mukesh agrawal8a3188d2011-12-01 20:56:44 +000033#include "shill/refptr_types.h"
34#include "shill/wifi_endpoint.h"
mukesh agrawal6e277772011-09-29 15:04:23 -070035#include "shill/wpa_supplicant.h"
Chris Masone34af2182011-08-22 11:59:36 -070036
Albert Chaulk0e1cdea2013-02-27 15:32:55 -080037using base::FilePath;
mukesh agrawald835b202011-10-07 15:26:47 -070038using std::map;
Paul Stewart85aea152013-01-22 09:31:56 -080039using std::set;
Chris Masone34af2182011-08-22 11:59:36 -070040using std::string;
41using std::vector;
Paul Stewartd08f4432011-11-04 07:48:20 -070042using ::testing::_;
mukesh agrawale1d90e92012-02-15 17:36:08 -080043using ::testing::AnyNumber;
Paul Stewartd08f4432011-11-04 07:48:20 -070044using ::testing::DoAll;
Paul Stewart71a4d3b2013-01-18 18:12:56 -080045using ::testing::EndsWith;
Paul Stewart3c504012013-01-17 17:49:58 -080046using ::testing::HasSubstr;
mukesh agrawale1d90e92012-02-15 17:36:08 -080047using ::testing::Mock;
mukesh agrawal6e277772011-09-29 15:04:23 -070048using ::testing::NiceMock;
Paul Stewartd08f4432011-11-04 07:48:20 -070049using ::testing::Return;
50using ::testing::SetArgumentPointee;
51using ::testing::StrEq;
Paul Stewartd8ad3c42012-01-09 12:39:38 -080052using ::testing::StrNe;
Paul Stewart85aea152013-01-22 09:31:56 -080053using ::testing::StrictMock;
Chris Masone34af2182011-08-22 11:59:36 -070054
mukesh agrawalb20776f2012-02-10 16:00:36 -080055namespace shill {
56
Chris Masone34af2182011-08-22 11:59:36 -070057class WiFiServiceTest : public PropertyStoreTest {
58 public:
Paul Stewart3c504012013-01-17 17:49:58 -080059 WiFiServiceTest()
60 : wifi_(new NiceMock<MockWiFi>(
61 control_interface(),
62 dispatcher(),
63 metrics(),
64 manager(),
65 "wifi",
66 fake_mac,
67 0)),
68 simple_ssid_(1, 'a'),
69 simple_ssid_string_("a") {}
Chris Masone34af2182011-08-22 11:59:36 -070070 virtual ~WiFiServiceTest() {}
mukesh agrawal6e277772011-09-29 15:04:23 -070071
72 protected:
73 static const char fake_mac[];
mukesh agrawale1d90e92012-02-15 17:36:08 -080074
Gaurav Shah10109f22011-11-11 20:16:22 -080075 bool CheckConnectable(const std::string &security, const char *passphrase,
Paul Stewart0654ece2013-03-26 15:21:26 -070076 EapCredentials *eap) {
mukesh agrawal29c13a12011-11-24 00:09:19 +000077 Error error;
Paul Stewart3c504012013-01-17 17:49:58 -080078 WiFiServiceRefPtr service = MakeSimpleService(security);
mukesh agrawal29c13a12011-11-24 00:09:19 +000079 if (passphrase)
80 service->SetPassphrase(passphrase, &error);
Gaurav Shah10109f22011-11-11 20:16:22 -080081 if (eap) {
82 service->set_eap(*eap);
83 }
mukesh agrawal29c13a12011-11-24 00:09:19 +000084 return service->connectable();
85 }
mukesh agrawale1d90e92012-02-15 17:36:08 -080086 WiFiEndpoint *MakeEndpoint(const string &ssid, const string &bssid,
mukesh agrawal43970a22013-02-15 16:00:07 -080087 uint16 frequency, int16 signal_dbm,
88 bool has_wpa_property, bool has_rsn_property) {
89 return WiFiEndpoint::MakeEndpoint(
Paul Stewart0654ece2013-03-26 15:21:26 -070090 NULL, wifi(), ssid, bssid, WPASupplicant::kNetworkModeInfrastructure,
mukesh agrawal43970a22013-02-15 16:00:07 -080091 frequency, signal_dbm, has_wpa_property, has_rsn_property);
92 }
93 WiFiEndpoint *MakeOpenEndpoint(const string &ssid, const string &bssid,
94 uint16 frequency, int16 signal_dbm) {
mukesh agrawale1d90e92012-02-15 17:36:08 -080095 return WiFiEndpoint::MakeOpenEndpoint(
Paul Stewart0654ece2013-03-26 15:21:26 -070096 NULL, wifi(), ssid, bssid, WPASupplicant::kNetworkModeInfrastructure,
Paul Stewart3c504012013-01-17 17:49:58 -080097 frequency, signal_dbm);
mukesh agrawale1d90e92012-02-15 17:36:08 -080098 }
Paul Stewart3c504012013-01-17 17:49:58 -080099 WiFiServiceRefPtr MakeSimpleService(const string &security) {
mukesh agrawale1d90e92012-02-15 17:36:08 -0800100 return new WiFiService(control_interface(),
101 dispatcher(),
102 metrics(),
103 manager(),
Paul Stewart3c504012013-01-17 17:49:58 -0800104 &provider_,
105 simple_ssid_,
mukesh agrawale1d90e92012-02-15 17:36:08 -0800106 flimflam::kModeManaged,
Paul Stewart3c504012013-01-17 17:49:58 -0800107 security,
mukesh agrawale1d90e92012-02-15 17:36:08 -0800108 false);
109 }
Paul Stewart3c504012013-01-17 17:49:58 -0800110 WiFiServiceRefPtr MakeGenericService() {
111 return MakeSimpleService(flimflam::kSecurityWep);
112 }
113 void SetWiFiForService(WiFiServiceRefPtr service, WiFiRefPtr wifi) {
114 service->wifi_ = wifi;
115 }
116 WiFiServiceRefPtr MakeServiceWithWiFi(const string &security) {
117 WiFiServiceRefPtr service = MakeSimpleService(security);
118 SetWiFiForService(service, wifi_);
119 return service;
120 }
mukesh agrawale1d90e92012-02-15 17:36:08 -0800121 ServiceMockAdaptor *GetAdaptor(WiFiService *service) {
122 return dynamic_cast<ServiceMockAdaptor *>(service->adaptor());
mukesh agrawal8a3188d2011-12-01 20:56:44 +0000123 }
Paul Stewart3c504012013-01-17 17:49:58 -0800124 Error::Type TestConfigurePassphrase(const string &security,
125 const char *passphrase) {
126 WiFiServiceRefPtr service = MakeSimpleService(security);
127 KeyValueStore args;
128 if (passphrase) {
129 args.SetString(flimflam::kPassphraseProperty, passphrase);
130 }
131 Error error;
132 service->Configure(args, &error);
133 return error.type();
134 }
mukesh agrawal6e277772011-09-29 15:04:23 -0700135 scoped_refptr<MockWiFi> wifi() { return wifi_; }
Paul Stewart3c504012013-01-17 17:49:58 -0800136 MockWiFiProvider *provider() { return &provider_; }
137 string GetAnyDeviceAddress() { return WiFiService::kAnyDeviceAddress; }
138 const vector<uint8_t> &simple_ssid() { return simple_ssid_; }
139 const string &simple_ssid_string() { return simple_ssid_string_; }
mukesh agrawal6e277772011-09-29 15:04:23 -0700140
141 private:
142 scoped_refptr<MockWiFi> wifi_;
Paul Stewart3c504012013-01-17 17:49:58 -0800143 MockWiFiProvider provider_;
144 const vector<uint8_t> simple_ssid_;
145 const string simple_ssid_string_;
Chris Masone34af2182011-08-22 11:59:36 -0700146};
147
mukesh agrawal6e277772011-09-29 15:04:23 -0700148// static
149const char WiFiServiceTest::fake_mac[] = "AaBBcCDDeeFF";
150
Paul Stewart71a4d3b2013-01-18 18:12:56 -0800151MATCHER_P3(ContainsWiFiProperties, ssid, mode, security, "") {
152 string hex_ssid = base::HexEncode(ssid.data(), ssid.size());
153 return
154 arg.ContainsString(WiFiService::kStorageType) &&
155 arg.GetString(WiFiService::kStorageType) == flimflam::kTypeWifi &&
156 arg.ContainsString(WiFiService::kStorageSSID) &&
157 arg.GetString(WiFiService::kStorageSSID) == hex_ssid &&
158 arg.ContainsString(WiFiService::kStorageMode) &&
159 arg.GetString(WiFiService::kStorageMode) == mode &&
160 arg.ContainsString(WiFiService::kStorageSecurityClass) &&
161 arg.GetString(WiFiService::kStorageSecurityClass) == security;
162}
163
Paul Stewartd08f4432011-11-04 07:48:20 -0700164class WiFiServiceSecurityTest : public WiFiServiceTest {
165 public:
Paul Stewartd08f4432011-11-04 07:48:20 -0700166 bool TestStorageSecurityIs(WiFiServiceRefPtr wifi_service,
167 const string &security) {
168 string id = wifi_service->GetStorageIdentifier();
Paul Stewart3c504012013-01-17 17:49:58 -0800169 size_t mac_pos = id.find(StringToLowerASCII(GetAnyDeviceAddress()));
Paul Stewartd08f4432011-11-04 07:48:20 -0700170 EXPECT_NE(mac_pos, string::npos);
171 size_t mode_pos = id.find(string(flimflam::kModeManaged), mac_pos);
172 EXPECT_NE(mode_pos, string::npos);
173 return id.find(string(security), mode_pos) != string::npos;
174 }
175
176 // Test that a service that is created with security |from_security|
177 // gets by default a storage identifier with |to_security| as its
Paul Stewart71a4d3b2013-01-18 18:12:56 -0800178 // security component, and that when saved, it sets the Security
179 // property in to |to_security| as well.
Paul Stewartd08f4432011-11-04 07:48:20 -0700180 bool TestStorageMapping(const string &from_security,
181 const string &to_security) {
Paul Stewart3c504012013-01-17 17:49:58 -0800182 WiFiServiceRefPtr wifi_service = MakeSimpleService(from_security);
Paul Stewart71a4d3b2013-01-18 18:12:56 -0800183 NiceMock<MockStore> mock_store;
184 EXPECT_CALL(mock_store, SetString(_, _, _)).WillRepeatedly(Return(true));
185 EXPECT_CALL(mock_store,
186 SetString(_, WiFiService::kStorageSecurity, from_security))
187 .Times(1);
188 EXPECT_CALL(mock_store,
189 SetString(_, WiFiService::kStorageSecurityClass, to_security))
190 .Times(1);
191 wifi_service->Save(&mock_store);
Paul Stewartd08f4432011-11-04 07:48:20 -0700192 return TestStorageSecurityIs(wifi_service, to_security);
193 }
194
195 // Test whether a service of type |service_security| can load from a
196 // storage interface containing an entry for |storage_security|.
197 // Make sure the result meets |expectation|. If |expectation| is
198 // true, also make sure the service storage identifier changes to
199 // match |storage_security|.
200 bool TestLoadMapping(const string &service_security,
201 const string &storage_security,
202 bool expectation) {
Paul Stewart3c504012013-01-17 17:49:58 -0800203 WiFiServiceRefPtr wifi_service = MakeSimpleService(service_security);
Paul Stewartd08f4432011-11-04 07:48:20 -0700204 NiceMock<MockStore> mock_store;
Paul Stewart71a4d3b2013-01-18 18:12:56 -0800205 EXPECT_CALL(mock_store, GetGroupsWithProperties(_))
206 .WillRepeatedly(Return(set<string>()));
207 const string kStorageId = "storage_id";
208 EXPECT_CALL(mock_store, ContainsGroup(kStorageId))
Paul Stewartd08f4432011-11-04 07:48:20 -0700209 .WillRepeatedly(Return(true));
Paul Stewart71a4d3b2013-01-18 18:12:56 -0800210 set<string> groups;
211 groups.insert(kStorageId);
212 EXPECT_CALL(mock_store, GetGroupsWithProperties(
213 ContainsWiFiProperties(wifi_service->ssid(),
214 flimflam::kModeManaged,
215 storage_security)))
216 .WillRepeatedly(Return(groups));
Paul Stewartd08f4432011-11-04 07:48:20 -0700217 bool is_loadable = wifi_service->IsLoadableFrom(&mock_store);
218 EXPECT_EQ(expectation, is_loadable);
219 bool is_loaded = wifi_service->Load(&mock_store);
220 EXPECT_EQ(expectation, is_loaded);
221
222 if (expectation != is_loadable || expectation != is_loaded) {
223 return false;
224 } else if (!expectation) {
225 return true;
226 } else {
Paul Stewart71a4d3b2013-01-18 18:12:56 -0800227 return wifi_service->GetStorageIdentifier() == kStorageId;
Paul Stewartd08f4432011-11-04 07:48:20 -0700228 }
229 }
230};
231
mukesh agrawale1d90e92012-02-15 17:36:08 -0800232class WiFiServiceUpdateFromEndpointsTest : public WiFiServiceTest {
233 public:
234 WiFiServiceUpdateFromEndpointsTest()
235 : kOkEndpointStrength(WiFiService::SignalToStrength(kOkEndpointSignal)),
236 kBadEndpointStrength(WiFiService::SignalToStrength(kBadEndpointSignal)),
237 kGoodEndpointStrength(
238 WiFiService::SignalToStrength(kGoodEndpointSignal)),
239 service(MakeGenericService()),
240 adaptor(*GetAdaptor(service)) {
mukesh agrawal43970a22013-02-15 16:00:07 -0800241 ok_endpoint = MakeOpenEndpoint(
Paul Stewart3c504012013-01-17 17:49:58 -0800242 simple_ssid_string(), kOkEndpointBssId, kOkEndpointFrequency,
243 kOkEndpointSignal);
mukesh agrawal43970a22013-02-15 16:00:07 -0800244 good_endpoint = MakeOpenEndpoint(
Paul Stewart3c504012013-01-17 17:49:58 -0800245 simple_ssid_string(), kGoodEndpointBssId, kGoodEndpointFrequency,
246 kGoodEndpointSignal);
mukesh agrawal43970a22013-02-15 16:00:07 -0800247 bad_endpoint = MakeOpenEndpoint(
Paul Stewart3c504012013-01-17 17:49:58 -0800248 simple_ssid_string(), kBadEndpointBssId, kBadEndpointFrequency,
249 kBadEndpointSignal);
mukesh agrawale1d90e92012-02-15 17:36:08 -0800250 }
251
252 protected:
253 static const uint16 kOkEndpointFrequency = 2422;
254 static const uint16 kBadEndpointFrequency = 2417;
255 static const uint16 kGoodEndpointFrequency = 2412;
256 static const int16 kOkEndpointSignal = -50;
257 static const int16 kBadEndpointSignal = -75;
258 static const int16 kGoodEndpointSignal = -25;
mukesh agrawal923f14f2012-06-04 16:46:08 -0700259 static const char *kOkEndpointBssId;
260 static const char *kGoodEndpointBssId;
261 static const char *kBadEndpointBssId;
mukesh agrawale1d90e92012-02-15 17:36:08 -0800262 // Can't be both static and const (because initialization requires a
263 // function call). So choose to be just const.
264 const uint8 kOkEndpointStrength;
265 const uint8 kBadEndpointStrength;
266 const uint8 kGoodEndpointStrength;
267 WiFiEndpointRefPtr ok_endpoint;
268 WiFiEndpointRefPtr bad_endpoint;
269 WiFiEndpointRefPtr good_endpoint;
270 WiFiServiceRefPtr service;
271 ServiceMockAdaptor &adaptor;
272};
273
mukesh agrawal923f14f2012-06-04 16:46:08 -0700274const char *WiFiServiceUpdateFromEndpointsTest::kOkEndpointBssId =
275 "00:00:00:00:00:01";
276const char *WiFiServiceUpdateFromEndpointsTest::kGoodEndpointBssId =
277 "00:00:00:00:00:02";
278const char *WiFiServiceUpdateFromEndpointsTest::kBadEndpointBssId =
279 "00:00:00:00:00:03";
mukesh agrawale1d90e92012-02-15 17:36:08 -0800280
Paul Stewart85aea152013-01-22 09:31:56 -0800281class WiFiServiceFixupStorageTest : public WiFiServiceTest {
282 protected:
283 void AddGroup(string group_name) {
284 groups_.insert(group_name);
285 }
286
Paul Stewart71a4d3b2013-01-18 18:12:56 -0800287 void AddServiceEntry(bool has_type, bool has_mode, bool has_security,
288 bool has_security_class) {
Paul Stewart85aea152013-01-22 09:31:56 -0800289 int index = groups_.size();
290 string id = base::StringPrintf("%s_%d_%d_%s_%s", flimflam::kTypeWifi,
291 index, index, flimflam::kModeManaged,
Paul Stewart71a4d3b2013-01-18 18:12:56 -0800292 flimflam::kSecurityWpa);
Paul Stewart85aea152013-01-22 09:31:56 -0800293 AddGroup(id);
294 EXPECT_CALL(store_, GetString(id, WiFiService::kStorageType, _))
295 .WillOnce(Return(has_type));
296 if (!has_type) {
297 EXPECT_CALL(store_, SetString(id, WiFiService::kStorageType,
298 flimflam::kTypeWifi));
299 }
300 EXPECT_CALL(store_, GetString(id, WiFiService::kStorageMode, _))
301 .WillOnce(Return(has_mode));
302 if (!has_mode) {
303 EXPECT_CALL(store_, SetString(id, WiFiService::kStorageMode,
304 flimflam::kModeManaged));
305 }
306 EXPECT_CALL(store_, GetString(id, WiFiService::kStorageSecurity, _))
307 .WillOnce(Return(has_security));
308 if (!has_security) {
309 EXPECT_CALL(store_, SetString(id, WiFiService::kStorageSecurity,
Paul Stewart71a4d3b2013-01-18 18:12:56 -0800310 flimflam::kSecurityWpa));
311 }
312 EXPECT_CALL(store_, GetString(id, WiFiService::kStorageSecurityClass, _))
313 .WillOnce(Return(has_security_class));
314 if (!has_security_class) {
315 EXPECT_CALL(store_, SetString(id, WiFiService::kStorageSecurityClass,
316 flimflam::kSecurityPsk));
Paul Stewart85aea152013-01-22 09:31:56 -0800317 }
318 }
319
320 bool FixupServiceEntries() {
321 EXPECT_CALL(store_, GetGroups()).WillOnce(Return(groups_));
322 return WiFiService::FixupServiceEntries(&store_);
323 }
324
325 private:
326 StrictMock<MockStore> store_;
327 set<string> groups_;
328};
329
Chris Masone34af2182011-08-22 11:59:36 -0700330TEST_F(WiFiServiceTest, StorageId) {
Paul Stewart3c504012013-01-17 17:49:58 -0800331 WiFiServiceRefPtr wifi_service = MakeSimpleService(flimflam::kSecurityNone);
Chris Masone9d779932011-08-25 16:33:41 -0700332 string id = wifi_service->GetStorageIdentifier();
Chris Masone34af2182011-08-22 11:59:36 -0700333 for (uint i = 0; i < id.length(); ++i) {
334 EXPECT_TRUE(id[i] == '_' ||
335 isxdigit(id[i]) ||
336 (isalpha(id[i]) && islower(id[i])));
337 }
Paul Stewart3c504012013-01-17 17:49:58 -0800338 size_t mac_pos = id.find(StringToLowerASCII(GetAnyDeviceAddress()));
Chris Masone34af2182011-08-22 11:59:36 -0700339 EXPECT_NE(mac_pos, string::npos);
340 EXPECT_NE(id.find(string(flimflam::kModeManaged), mac_pos), string::npos);
341}
342
Gaurav Shahda6218a2011-11-11 12:09:33 -0800343// Make sure the passphrase is registered as a write only property
344// by reading and comparing all string properties returned on the store.
345TEST_F(WiFiServiceTest, PassphraseWriteOnly) {
Paul Stewart3c504012013-01-17 17:49:58 -0800346 WiFiServiceRefPtr wifi_service = MakeSimpleService(flimflam::kSecurityWpa);
Gaurav Shahda6218a2011-11-11 12:09:33 -0800347 ReadablePropertyConstIterator<string> it =
348 (wifi_service->store()).GetStringPropertiesIter();
349 for( ; !it.AtEnd(); it.Advance())
350 EXPECT_NE(it.Key(), flimflam::kPassphraseProperty);
351}
352
Thieu Lef7709452011-11-15 01:13:19 +0000353// Make sure setting the passphrase via D-Bus Service.SetProperty validates
354// the passphrase.
355TEST_F(WiFiServiceTest, PassphraseSetPropertyValidation) {
356 // We only spot check two password cases here to make sure the
357 // SetProperty code path does validation. We're not going to exhaustively
358 // test for all types of passwords.
Paul Stewart3c504012013-01-17 17:49:58 -0800359 WiFiServiceRefPtr wifi_service = MakeSimpleService(flimflam::kSecurityWep);
Thieu Lef7709452011-11-15 01:13:19 +0000360 Error error;
361 EXPECT_TRUE(wifi_service->mutable_store()->SetStringProperty(
362 flimflam::kPassphraseProperty, "0:abcde", &error));
363 EXPECT_FALSE(wifi_service->mutable_store()->SetStringProperty(
364 flimflam::kPassphraseProperty, "invalid", &error));
365 EXPECT_EQ(Error::kInvalidPassphrase, error.type());
366}
367
368TEST_F(WiFiServiceTest, PassphraseSetPropertyOpenNetwork) {
Paul Stewart3c504012013-01-17 17:49:58 -0800369 WiFiServiceRefPtr wifi_service = MakeSimpleService(flimflam::kSecurityNone);
Thieu Lef7709452011-11-15 01:13:19 +0000370 Error error;
371 EXPECT_FALSE(wifi_service->mutable_store()->SetStringProperty(
372 flimflam::kPassphraseProperty, "invalid", &error));
373 EXPECT_EQ(Error::kNotSupported, error.type());
374}
375
mukesh agrawald835b202011-10-07 15:26:47 -0700376TEST_F(WiFiServiceTest, NonUTF8SSID) {
377 vector<uint8_t> ssid;
378
379 ssid.push_back(0xff); // not a valid UTF-8 byte-sequence
380 WiFiServiceRefPtr wifi_service = new WiFiService(control_interface(),
381 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -0800382 metrics(),
mukesh agrawald835b202011-10-07 15:26:47 -0700383 manager(),
Paul Stewart3c504012013-01-17 17:49:58 -0800384 provider(),
mukesh agrawald835b202011-10-07 15:26:47 -0700385 ssid,
386 flimflam::kModeManaged,
Paul Stewartced6a0b2011-11-08 15:32:04 -0800387 flimflam::kSecurityNone,
388 false);
mukesh agrawald835b202011-10-07 15:26:47 -0700389 map<string, ::DBus::Variant> properties;
390 // if service doesn't propertly sanitize SSID, this will generate SIGABRT.
391 DBusAdaptor::GetProperties(wifi_service->store(), &properties, NULL);
392}
393
Paul Stewart4108db92013-03-11 12:13:24 -0700394MATCHER(PSKSecurityArgs, "") {
Paul Stewart0654ece2013-03-26 15:21:26 -0700395 return ContainsKey(arg, WPASupplicant::kPropertySecurityProtocol) &&
396 arg.find(WPASupplicant::kPropertySecurityProtocol)->second.
Paul Stewart4108db92013-03-11 12:13:24 -0700397 reader().get_string() == string("WPA RSN") &&
Paul Stewart0654ece2013-03-26 15:21:26 -0700398 ContainsKey(arg, WPASupplicant::kPropertyPreSharedKey);
Gaurav Shahda6218a2011-11-11 12:09:33 -0800399}
400
Paul Stewarta5e7d5f2013-01-09 18:06:15 -0800401MATCHER(WPA80211wSecurityArgs, "") {
Paul Stewart0654ece2013-03-26 15:21:26 -0700402 return ContainsKey(arg, WPASupplicant::kPropertySecurityProtocol) &&
403 ContainsKey(arg, WPASupplicant::kPropertyPreSharedKey) &&
404 ContainsKey(arg, WPASupplicant::kNetworkPropertyIeee80211w);
Paul Stewarta5e7d5f2013-01-09 18:06:15 -0800405}
406
Gaurav Shah10109f22011-11-11 20:16:22 -0800407MATCHER(EAPSecurityArgs, "") {
Paul Stewart0654ece2013-03-26 15:21:26 -0700408 return ContainsKey(arg, WPASupplicant::kNetworkPropertyEapIdentity) &&
409 ContainsKey(arg, WPASupplicant::kNetworkPropertyCaPath);
Gaurav Shah10109f22011-11-11 20:16:22 -0800410}
411
Paul Stewarte2d7c502012-07-16 16:35:10 -0700412MATCHER_P(FrequencyArg, has_arg, "") {
413 return has_arg ==
Paul Stewart0654ece2013-03-26 15:21:26 -0700414 ContainsKey(arg, WPASupplicant::kNetworkPropertyFrequency);
Paul Stewarte2d7c502012-07-16 16:35:10 -0700415}
416
mukesh agrawalf2fd7452011-10-03 16:38:47 -0700417TEST_F(WiFiServiceTest, ConnectTaskWPA) {
Paul Stewart3c504012013-01-17 17:49:58 -0800418 WiFiServiceRefPtr wifi_service = MakeServiceWithWiFi(flimflam::kSecurityWpa);
Paul Stewart4108db92013-03-11 12:13:24 -0700419 EXPECT_CALL(*wifi(), ConnectTo(wifi_service.get(), PSKSecurityArgs()));
Wade Guthrie005bd342012-05-02 09:37:07 -0700420 Error error;
421 wifi_service->SetPassphrase("0:mumblemumblem", &error);
mukesh agrawaldc7b8442012-09-27 13:48:14 -0700422 wifi_service->Connect(NULL, "in test");
mukesh agrawal6e277772011-09-29 15:04:23 -0700423}
424
mukesh agrawalf2fd7452011-10-03 16:38:47 -0700425TEST_F(WiFiServiceTest, ConnectTaskRSN) {
Paul Stewart3c504012013-01-17 17:49:58 -0800426 WiFiServiceRefPtr wifi_service = MakeServiceWithWiFi(flimflam::kSecurityRsn);
Paul Stewart4108db92013-03-11 12:13:24 -0700427 EXPECT_CALL(*wifi(), ConnectTo(wifi_service.get(), PSKSecurityArgs()));
Wade Guthrie005bd342012-05-02 09:37:07 -0700428 Error error;
429 wifi_service->SetPassphrase("0:mumblemumblem", &error);
mukesh agrawaldc7b8442012-09-27 13:48:14 -0700430 wifi_service->Connect(NULL, "in test");
mukesh agrawalf2fd7452011-10-03 16:38:47 -0700431}
432
Christopher Wiley1ce658d2012-10-10 10:02:03 -0700433TEST_F(WiFiServiceTest, ConnectConditions) {
434 Error error;
Paul Stewart3c504012013-01-17 17:49:58 -0800435 WiFiServiceRefPtr wifi_service = MakeServiceWithWiFi(flimflam::kSecurityNone);
Christopher Wiley1ce658d2012-10-10 10:02:03 -0700436 scoped_refptr<MockProfile> mock_profile(
Thieu Le5133b712013-02-19 14:47:21 -0800437 new NiceMock<MockProfile>(control_interface(), metrics(), manager()));
Christopher Wiley1ce658d2012-10-10 10:02:03 -0700438 wifi_service->set_profile(mock_profile);
439 // With nothing else going on, the service should attempt to connect.
440 EXPECT_CALL(*wifi(), ConnectTo(wifi_service.get(), _));
mukesh agrawaldc7b8442012-09-27 13:48:14 -0700441 wifi_service->Connect(&error, "in test");
Christopher Wiley1ce658d2012-10-10 10:02:03 -0700442 Mock::VerifyAndClearExpectations(wifi());
443
444 // But if we're already "connecting" or "connected" then we shouldn't attempt
445 // again.
446 EXPECT_CALL(*wifi(),
447 ConnectTo(wifi_service.get(), _)).Times(0);
448 wifi_service->SetState(Service::kStateAssociating);
mukesh agrawaldc7b8442012-09-27 13:48:14 -0700449 wifi_service->Connect(&error, "in test");
Christopher Wiley1ce658d2012-10-10 10:02:03 -0700450 wifi_service->SetState(Service::kStateConfiguring);
mukesh agrawaldc7b8442012-09-27 13:48:14 -0700451 wifi_service->Connect(&error, "in test");
Christopher Wiley1ce658d2012-10-10 10:02:03 -0700452 wifi_service->SetState(Service::kStateConnected);
mukesh agrawaldc7b8442012-09-27 13:48:14 -0700453 wifi_service->Connect(&error, "in test");
Christopher Wiley1ce658d2012-10-10 10:02:03 -0700454 wifi_service->SetState(Service::kStatePortal);
mukesh agrawaldc7b8442012-09-27 13:48:14 -0700455 wifi_service->Connect(&error, "in test");
Christopher Wiley1ce658d2012-10-10 10:02:03 -0700456 wifi_service->SetState(Service::kStateOnline);
mukesh agrawaldc7b8442012-09-27 13:48:14 -0700457 wifi_service->Connect(&error, "in test");
Christopher Wiley1ce658d2012-10-10 10:02:03 -0700458 Mock::VerifyAndClearExpectations(wifi());
459}
460
Gaurav Shahf8721ee2011-11-07 09:12:46 -0800461TEST_F(WiFiServiceTest, ConnectTaskPSK) {
Paul Stewart3c504012013-01-17 17:49:58 -0800462 WiFiServiceRefPtr wifi_service = MakeServiceWithWiFi(flimflam::kSecurityPsk);
Paul Stewart4108db92013-03-11 12:13:24 -0700463 EXPECT_CALL(*wifi(), ConnectTo(wifi_service.get(), PSKSecurityArgs()));
Wade Guthrie005bd342012-05-02 09:37:07 -0700464 Error error;
465 wifi_service->SetPassphrase("0:mumblemumblem", &error);
mukesh agrawaldc7b8442012-09-27 13:48:14 -0700466 wifi_service->Connect(NULL, "in test");
Gaurav Shahf8721ee2011-11-07 09:12:46 -0800467}
468
Gaurav Shah10109f22011-11-11 20:16:22 -0800469TEST_F(WiFiServiceTest, ConnectTask8021x) {
Paul Stewart3c504012013-01-17 17:49:58 -0800470 WiFiServiceRefPtr service = MakeServiceWithWiFi(flimflam::kSecurity8021x);
Paul Stewart0654ece2013-03-26 15:21:26 -0700471 EapCredentials eap;
Gaurav Shah10109f22011-11-11 20:16:22 -0800472 eap.identity = "identity";
Wade Guthrie005bd342012-05-02 09:37:07 -0700473 eap.password = "mumble";
Paul Stewart3c504012013-01-17 17:49:58 -0800474 service->set_eap(eap);
475 EXPECT_CALL(*wifi(), ConnectTo(service.get(), EAPSecurityArgs()));
mukesh agrawaldc7b8442012-09-27 13:48:14 -0700476 service->Connect(NULL, "in test");
Gaurav Shah10109f22011-11-11 20:16:22 -0800477}
478
Paul Stewarte2d7c502012-07-16 16:35:10 -0700479TEST_F(WiFiServiceTest, ConnectTaskAdHocFrequency) {
480 vector<uint8_t> ssid(1, 'a');
481 WiFiEndpointRefPtr endpoint_nofreq =
mukesh agrawal43970a22013-02-15 16:00:07 -0800482 MakeOpenEndpoint("a", "00:00:00:00:00:01", 0, 0);
Paul Stewarte2d7c502012-07-16 16:35:10 -0700483 WiFiEndpointRefPtr endpoint_freq =
mukesh agrawal43970a22013-02-15 16:00:07 -0800484 MakeOpenEndpoint("a", "00:00:00:00:00:02", 2412, 0);
Paul Stewarte2d7c502012-07-16 16:35:10 -0700485
Paul Stewart3c504012013-01-17 17:49:58 -0800486 WiFiServiceRefPtr wifi_service = MakeServiceWithWiFi(flimflam::kSecurityNone);
Paul Stewarte2d7c502012-07-16 16:35:10 -0700487 wifi_service->AddEndpoint(endpoint_freq);
488 EXPECT_CALL(*wifi(),
489 ConnectTo(wifi_service.get(), FrequencyArg(false)));
mukesh agrawaldc7b8442012-09-27 13:48:14 -0700490 wifi_service->Connect(NULL, "in test");
Paul Stewarte2d7c502012-07-16 16:35:10 -0700491
492 wifi_service = new WiFiService(control_interface(),
493 dispatcher(),
494 metrics(),
495 manager(),
Paul Stewart3c504012013-01-17 17:49:58 -0800496 provider(),
Paul Stewarte2d7c502012-07-16 16:35:10 -0700497 ssid,
498 flimflam::kModeAdhoc,
499 flimflam::kSecurityNone,
500 false);
501 EXPECT_CALL(*wifi(),
502 ConnectTo(wifi_service.get(), FrequencyArg(false)));
Paul Stewart3c504012013-01-17 17:49:58 -0800503 SetWiFiForService(wifi_service, wifi());
mukesh agrawaldc7b8442012-09-27 13:48:14 -0700504 wifi_service->Connect(NULL, "in test");
Paul Stewarte2d7c502012-07-16 16:35:10 -0700505
506 wifi_service = new WiFiService(control_interface(),
507 dispatcher(),
508 metrics(),
509 manager(),
Paul Stewart3c504012013-01-17 17:49:58 -0800510 provider(),
Paul Stewarte2d7c502012-07-16 16:35:10 -0700511 ssid,
512 flimflam::kModeAdhoc,
513 flimflam::kSecurityNone,
514 false);
515 wifi_service->AddEndpoint(endpoint_nofreq);
Paul Stewart3c504012013-01-17 17:49:58 -0800516 SetWiFiForService(wifi_service, wifi());
Paul Stewarte2d7c502012-07-16 16:35:10 -0700517 EXPECT_CALL(*wifi(),
518 ConnectTo(wifi_service.get(), FrequencyArg(false)));
mukesh agrawaldc7b8442012-09-27 13:48:14 -0700519 wifi_service->Connect(NULL, "in test");
Paul Stewarte2d7c502012-07-16 16:35:10 -0700520
521 wifi_service = new WiFiService(control_interface(),
522 dispatcher(),
523 metrics(),
524 manager(),
Paul Stewart3c504012013-01-17 17:49:58 -0800525 provider(),
Paul Stewarte2d7c502012-07-16 16:35:10 -0700526 ssid,
527 flimflam::kModeAdhoc,
528 flimflam::kSecurityNone,
529 false);
530 wifi_service->AddEndpoint(endpoint_freq);
Paul Stewart3c504012013-01-17 17:49:58 -0800531 SetWiFiForService(wifi_service, wifi());
Paul Stewarte2d7c502012-07-16 16:35:10 -0700532 EXPECT_CALL(*wifi(),
533 ConnectTo(wifi_service.get(), FrequencyArg(true)));
mukesh agrawaldc7b8442012-09-27 13:48:14 -0700534 wifi_service->Connect(NULL, "in test");
Paul Stewarte2d7c502012-07-16 16:35:10 -0700535}
536
Paul Stewarta5e7d5f2013-01-09 18:06:15 -0800537TEST_F(WiFiServiceTest, ConnectTaskWPA80211w) {
Paul Stewart3c504012013-01-17 17:49:58 -0800538 WiFiServiceRefPtr wifi_service = MakeServiceWithWiFi(flimflam::kSecurityPsk);
mukesh agrawal43970a22013-02-15 16:00:07 -0800539 WiFiEndpointRefPtr endpoint =
540 MakeOpenEndpoint("a", "00:00:00:00:00:01", 0, 0);
Paul Stewarta5e7d5f2013-01-09 18:06:15 -0800541 endpoint->ieee80211w_required_ = true;
542 wifi_service->AddEndpoint(endpoint);
543 Error error;
544 wifi_service->SetPassphrase("0:mumblemumblem", &error);
545 EXPECT_CALL(*wifi(),
546 ConnectTo(wifi_service.get(), WPA80211wSecurityArgs()));
mukesh agrawaldc7b8442012-09-27 13:48:14 -0700547 wifi_service->Connect(NULL, "in test");
Paul Stewarta5e7d5f2013-01-09 18:06:15 -0800548}
549
Thieu Lef4cbda92011-11-10 23:41:24 +0000550MATCHER(WEPSecurityArgsKeyIndex0, "") {
Paul Stewart0654ece2013-03-26 15:21:26 -0700551 return ContainsKey(arg, WPASupplicant::kPropertyAuthAlg) &&
552 ContainsKey(arg, WPASupplicant::kPropertyWEPKey + std::string("0")) &&
553 ContainsKey(arg, WPASupplicant::kPropertyWEPTxKeyIndex) &&
554 (arg.find(WPASupplicant::kPropertyWEPTxKeyIndex)->second.
Thieu Lef4cbda92011-11-10 23:41:24 +0000555 reader().get_uint32() == 0);
556}
557
558MATCHER(WEPSecurityArgsKeyIndex1, "") {
Paul Stewart0654ece2013-03-26 15:21:26 -0700559 return ContainsKey(arg, WPASupplicant::kPropertyAuthAlg) &&
560 ContainsKey(arg, WPASupplicant::kPropertyWEPKey + std::string("1")) &&
561 ContainsKey(arg, WPASupplicant::kPropertyWEPTxKeyIndex) &&
562 (arg.find(WPASupplicant::kPropertyWEPTxKeyIndex)->second.
Thieu Lef4cbda92011-11-10 23:41:24 +0000563 reader().get_uint32() == 1);
564}
565
566MATCHER(WEPSecurityArgsKeyIndex2, "") {
Paul Stewart0654ece2013-03-26 15:21:26 -0700567 return ContainsKey(arg, WPASupplicant::kPropertyAuthAlg) &&
568 ContainsKey(arg, WPASupplicant::kPropertyWEPKey + std::string("2")) &&
569 ContainsKey(arg, WPASupplicant::kPropertyWEPTxKeyIndex) &&
570 (arg.find(WPASupplicant::kPropertyWEPTxKeyIndex)->second.
Thieu Lef4cbda92011-11-10 23:41:24 +0000571 reader().get_uint32() == 2);
572}
573
574MATCHER(WEPSecurityArgsKeyIndex3, "") {
Paul Stewart0654ece2013-03-26 15:21:26 -0700575 return ContainsKey(arg, WPASupplicant::kPropertyAuthAlg) &&
576 ContainsKey(arg, WPASupplicant::kPropertyWEPKey + std::string("3")) &&
577 ContainsKey(arg, WPASupplicant::kPropertyWEPTxKeyIndex) &&
578 (arg.find(WPASupplicant::kPropertyWEPTxKeyIndex)->second.
Thieu Lef4cbda92011-11-10 23:41:24 +0000579 reader().get_uint32() == 3);
580}
581
582TEST_F(WiFiServiceTest, ConnectTaskWEP) {
Paul Stewart3c504012013-01-17 17:49:58 -0800583 WiFiServiceRefPtr wifi_service = MakeServiceWithWiFi(flimflam::kSecurityWep);
Thieu Lef4cbda92011-11-10 23:41:24 +0000584 Error error;
585 wifi_service->SetPassphrase("0:abcdefghijklm", &error);
586 EXPECT_CALL(*wifi(),
587 ConnectTo(wifi_service.get(), WEPSecurityArgsKeyIndex0()));
mukesh agrawaldc7b8442012-09-27 13:48:14 -0700588 wifi_service->Connect(NULL, "in test");
Thieu Lef4cbda92011-11-10 23:41:24 +0000589
590 wifi_service->SetPassphrase("abcdefghijklm", &error);
591 EXPECT_CALL(*wifi(),
592 ConnectTo(wifi_service.get(), WEPSecurityArgsKeyIndex0()));
mukesh agrawaldc7b8442012-09-27 13:48:14 -0700593 wifi_service->Connect(NULL, "in test");
Thieu Lef4cbda92011-11-10 23:41:24 +0000594
595 wifi_service->SetPassphrase("1:abcdefghijklm", &error);
596 EXPECT_CALL(*wifi(),
597 ConnectTo(wifi_service.get(), WEPSecurityArgsKeyIndex1()));
mukesh agrawaldc7b8442012-09-27 13:48:14 -0700598 wifi_service->Connect(NULL, "in test");
Thieu Lef4cbda92011-11-10 23:41:24 +0000599
600 wifi_service->SetPassphrase("2:abcdefghijklm", &error);
601 EXPECT_CALL(*wifi(),
602 ConnectTo(wifi_service.get(), WEPSecurityArgsKeyIndex2()));
mukesh agrawaldc7b8442012-09-27 13:48:14 -0700603 wifi_service->Connect(NULL, "in test");
Thieu Lef4cbda92011-11-10 23:41:24 +0000604
605 wifi_service->SetPassphrase("3:abcdefghijklm", &error);
606 EXPECT_CALL(*wifi(),
607 ConnectTo(wifi_service.get(), WEPSecurityArgsKeyIndex3()));
mukesh agrawaldc7b8442012-09-27 13:48:14 -0700608 wifi_service->Connect(NULL, "in test");
Thieu Lef4cbda92011-11-10 23:41:24 +0000609}
610
Gaurav Shah29d68882012-01-30 19:06:42 -0800611
612MATCHER(DynamicWEPArgs, "") {
Paul Stewart0654ece2013-03-26 15:21:26 -0700613 return ContainsKey(arg, WPASupplicant::kNetworkPropertyEapIdentity) &&
614 ContainsKey(arg, WPASupplicant::kNetworkPropertyCaPath) &&
615 !ContainsKey(arg, WPASupplicant::kPropertySecurityProtocol);
Gaurav Shah29d68882012-01-30 19:06:42 -0800616}
617
618// Dynamic WEP + 802.1x.
619TEST_F(WiFiServiceTest, ConnectTaskDynamicWEP) {
Paul Stewart3c504012013-01-17 17:49:58 -0800620 WiFiServiceRefPtr wifi_service = MakeServiceWithWiFi(flimflam::kSecurityWep);
Gaurav Shah29d68882012-01-30 19:06:42 -0800621
Paul Stewart0654ece2013-03-26 15:21:26 -0700622 EapCredentials eap;
Gaurav Shah29d68882012-01-30 19:06:42 -0800623 eap.key_management = "IEEE8021X";
624 eap.identity = "something";
Wade Guthrie005bd342012-05-02 09:37:07 -0700625 eap.password = "mumble";
Gaurav Shah29d68882012-01-30 19:06:42 -0800626 wifi_service->set_eap(eap);
627 EXPECT_CALL(*wifi(),
628 ConnectTo(wifi_service.get(), DynamicWEPArgs()));
mukesh agrawaldc7b8442012-09-27 13:48:14 -0700629 wifi_service->Connect(NULL, "in test");
Gaurav Shah29d68882012-01-30 19:06:42 -0800630}
631
Paul Stewart835934a2012-12-06 19:27:09 -0800632TEST_F(WiFiServiceTest, SetPassphraseRemovesCachedCredentials) {
633 vector<uint8_t> ssid(5);
Paul Stewart3c504012013-01-17 17:49:58 -0800634 WiFiServiceRefPtr wifi_service = MakeServiceWithWiFi(flimflam::kSecurityRsn);
Paul Stewart835934a2012-12-06 19:27:09 -0800635
636 const string kPassphrase = "abcdefgh";
637
638 {
639 Error error;
640 // A changed passphrase should trigger cache removal.
641 EXPECT_CALL(*wifi(), ClearCachedCredentials(wifi_service.get()));
642 wifi_service->SetPassphrase(kPassphrase, &error);
643 Mock::VerifyAndClearExpectations(wifi());
644 EXPECT_TRUE(error.IsSuccess());
645 }
646
647 {
648 Error error;
649 // An unchanged passphrase should not trigger cache removal.
650 EXPECT_CALL(*wifi(), ClearCachedCredentials(_)).Times(0);
651 wifi_service->SetPassphrase(kPassphrase, &error);
652 Mock::VerifyAndClearExpectations(wifi());
653 EXPECT_TRUE(error.IsSuccess());
654 }
655
656 {
657 Error error;
658 // A modified passphrase should trigger cache removal.
659 EXPECT_CALL(*wifi(), ClearCachedCredentials(wifi_service.get()));
660 wifi_service->SetPassphrase(kPassphrase + "X", &error);
661 Mock::VerifyAndClearExpectations(wifi());
662 EXPECT_TRUE(error.IsSuccess());
663 }
664
665 {
666 Error error;
667 // A cleared passphrase should also trigger cache removal.
668 EXPECT_CALL(*wifi(), ClearCachedCredentials(wifi_service.get()));
669 wifi_service->ClearPassphrase(&error);
670 Mock::VerifyAndClearExpectations(wifi());
671 EXPECT_TRUE(error.IsSuccess());
672 }
673
674 {
675 Error error;
676 // An invalid passphrase should not trigger cache removal.
677 EXPECT_CALL(*wifi(), ClearCachedCredentials(_)).Times(0);
678 wifi_service->SetPassphrase("", &error);
679 Mock::VerifyAndClearExpectations(wifi());
680 EXPECT_FALSE(error.IsSuccess());
681 }
682
683 {
684 // Any change to EAP parameters (including a null one) will trigger cache
685 // removal. This is a lot less granular than the passphrase checks above.
686 EXPECT_CALL(*wifi(), ClearCachedCredentials(wifi_service.get()));
Paul Stewart0654ece2013-03-26 15:21:26 -0700687 wifi_service->set_eap(EapCredentials());
Paul Stewart835934a2012-12-06 19:27:09 -0800688 Mock::VerifyAndClearExpectations(wifi());
689 }
690}
691
Paul Stewartd08f4432011-11-04 07:48:20 -0700692TEST_F(WiFiServiceTest, LoadHidden) {
Paul Stewart3c504012013-01-17 17:49:58 -0800693 WiFiServiceRefPtr service = MakeSimpleService(flimflam::kSecurityNone);
Paul Stewartd08f4432011-11-04 07:48:20 -0700694 ASSERT_FALSE(service->hidden_ssid_);
695 NiceMock<MockStore> mock_store;
696 const string storage_id = service->GetStorageIdentifier();
Paul Stewart71a4d3b2013-01-18 18:12:56 -0800697 set<string> groups;
698 groups.insert(storage_id);
Paul Stewartd08f4432011-11-04 07:48:20 -0700699 EXPECT_CALL(mock_store, ContainsGroup(StrEq(storage_id)))
700 .WillRepeatedly(Return(true));
Paul Stewart71a4d3b2013-01-18 18:12:56 -0800701 EXPECT_CALL(mock_store, GetGroupsWithProperties(
702 ContainsWiFiProperties(
Paul Stewart3c504012013-01-17 17:49:58 -0800703 simple_ssid(), flimflam::kModeManaged, flimflam::kSecurityNone)))
Paul Stewart71a4d3b2013-01-18 18:12:56 -0800704 .WillRepeatedly(Return(groups));
Paul Stewartd08f4432011-11-04 07:48:20 -0700705 EXPECT_CALL(mock_store, GetBool(_, _, _))
706 .WillRepeatedly(Return(false));
707 EXPECT_CALL(mock_store,
708 GetBool(StrEq(storage_id), WiFiService::kStorageHiddenSSID, _))
709 .WillRepeatedly(DoAll(SetArgumentPointee<2>(true), Return(true)));
710 EXPECT_TRUE(service->Load(&mock_store));
711 EXPECT_TRUE(service->hidden_ssid_);
712}
713
Paul Stewartfa013ab2013-04-11 07:12:03 -0700714TEST_F(WiFiServiceTest, LoadPassphraseForNonPassphraseService) {
715 WiFiServiceRefPtr service = MakeSimpleService(flimflam::kSecurityNone);
716 NiceMock<MockStore> mock_store;
717 const string storage_id = service->GetStorageIdentifier();
718 set<string> groups;
719 groups.insert(storage_id);
720 EXPECT_CALL(mock_store, ContainsGroup(StrEq(storage_id)))
721 .WillRepeatedly(Return(true));
722 EXPECT_CALL(mock_store, GetGroupsWithProperties(
723 ContainsWiFiProperties(
724 simple_ssid(), flimflam::kModeManaged, flimflam::kSecurityNone)))
725 .WillRepeatedly(Return(groups));
726 EXPECT_CALL(mock_store,
727 GetCryptedString(StrEq(storage_id),
728 StrNe(WiFiService::kStoragePassphrase), _))
729 .WillRepeatedly(Return(false));
730 EXPECT_CALL(mock_store,
731 GetCryptedString(StrEq(storage_id),
732 WiFiService::kStoragePassphrase, _))
733 .WillOnce(DoAll(SetArgumentPointee<2>(string("password")), Return(true)))
734 .WillOnce(DoAll(SetArgumentPointee<2>(string()), Return(true)));
735 ScopedMockLog log;
736 EXPECT_CALL(log, Log(logging::LOG_ERROR, _,
737 EndsWith("Passphrase could not be set: "
738 "org.chromium.flimflam.Error.NotSupported")))
739 .Times(1);
740 EXPECT_TRUE(service->Load(&mock_store));
741 Mock::VerifyAndClearExpectations(&log);
742 EXPECT_CALL(log, Log(logging::LOG_ERROR, _, _)).Times(0);
743 EXPECT_TRUE(service->Load(&mock_store));
744}
745
Paul Stewart71a4d3b2013-01-18 18:12:56 -0800746TEST_F(WiFiServiceTest, LoadMultipleMatchingGroups) {
Paul Stewart3c504012013-01-17 17:49:58 -0800747 WiFiServiceRefPtr service = MakeServiceWithWiFi(flimflam::kSecurityNone);
Paul Stewart71a4d3b2013-01-18 18:12:56 -0800748 set<string> groups;
749 groups.insert("id0");
750 groups.insert("id1");
751 // Make sure we retain the first matched group in the same way that
752 // WiFiService::Load() will.
753 string first_group = *groups.begin();
754
755 NiceMock<MockStore> mock_store;
756 EXPECT_CALL(mock_store, GetGroupsWithProperties(
757 ContainsWiFiProperties(
Paul Stewart3c504012013-01-17 17:49:58 -0800758 simple_ssid(), flimflam::kModeManaged, flimflam::kSecurityNone)))
Paul Stewart71a4d3b2013-01-18 18:12:56 -0800759 .WillRepeatedly(Return(groups));
760 EXPECT_CALL(mock_store, ContainsGroup(first_group))
761 .WillRepeatedly(Return(true));
762 EXPECT_CALL(mock_store, ContainsGroup(StrNe(first_group))).Times(0);
763 EXPECT_CALL(mock_store, GetBool(first_group, _, _))
764 .WillRepeatedly(Return(false));
765 EXPECT_CALL(mock_store, GetBool(StrNe(first_group), _, _)).Times(0);
766 ScopedMockLog log;
767 EXPECT_CALL(log, Log(_, _, _)).Times(AnyNumber());
768 EXPECT_CALL(log, Log(logging::LOG_WARNING, _,
769 EndsWith("choosing the first.")));
770 EXPECT_TRUE(service->Load(&mock_store));
771}
772
Paul Stewartd08f4432011-11-04 07:48:20 -0700773TEST_F(WiFiServiceSecurityTest, WPAMapping) {
774 EXPECT_TRUE(TestStorageMapping(flimflam::kSecurityRsn,
775 flimflam::kSecurityPsk));
776 EXPECT_TRUE(TestStorageMapping(flimflam::kSecurityWpa,
777 flimflam::kSecurityPsk));
778 EXPECT_TRUE(TestStorageMapping(flimflam::kSecurityPsk,
779 flimflam::kSecurityPsk));
780 EXPECT_TRUE(TestStorageMapping(flimflam::kSecurityWep,
781 flimflam::kSecurityWep));
782 EXPECT_TRUE(TestStorageMapping(flimflam::kSecurityNone,
783 flimflam::kSecurityNone));
Gaurav Shah10109f22011-11-11 20:16:22 -0800784 EXPECT_TRUE(TestStorageMapping(flimflam::kSecurity8021x,
785 flimflam::kSecurity8021x));
Paul Stewartd08f4432011-11-04 07:48:20 -0700786}
787
788TEST_F(WiFiServiceSecurityTest, LoadMapping) {
789 EXPECT_TRUE(TestLoadMapping(flimflam::kSecurityRsn,
790 flimflam::kSecurityPsk,
791 true));
792 EXPECT_TRUE(TestLoadMapping(flimflam::kSecurityRsn,
793 flimflam::kSecurityRsn,
Paul Stewart71a4d3b2013-01-18 18:12:56 -0800794 false));
Paul Stewartd08f4432011-11-04 07:48:20 -0700795 EXPECT_TRUE(TestLoadMapping(flimflam::kSecurityRsn,
796 flimflam::kSecurityWpa,
797 false));
798 EXPECT_TRUE(TestLoadMapping(flimflam::kSecurityWpa,
799 flimflam::kSecurityPsk,
800 true));
801 EXPECT_TRUE(TestLoadMapping(flimflam::kSecurityWpa,
802 flimflam::kSecurityWpa,
Paul Stewart71a4d3b2013-01-18 18:12:56 -0800803 false));
Paul Stewartd08f4432011-11-04 07:48:20 -0700804 EXPECT_TRUE(TestLoadMapping(flimflam::kSecurityWpa,
805 flimflam::kSecurityRsn,
806 false));
807 EXPECT_TRUE(TestLoadMapping(flimflam::kSecurityWep,
808 flimflam::kSecurityWep,
809 true));
810 EXPECT_TRUE(TestLoadMapping(flimflam::kSecurityWep,
811 flimflam::kSecurityPsk,
812 false));
813}
814
Paul Stewartd8ad3c42012-01-09 12:39:38 -0800815TEST_F(WiFiServiceTest, LoadAndUnloadPassphrase) {
Paul Stewart3c504012013-01-17 17:49:58 -0800816 WiFiServiceRefPtr service = MakeSimpleService(flimflam::kSecurityPsk);
Paul Stewartd8ad3c42012-01-09 12:39:38 -0800817 NiceMock<MockStore> mock_store;
818 const string storage_id = service->GetStorageIdentifier();
819 EXPECT_CALL(mock_store, ContainsGroup(StrEq(storage_id)))
820 .WillRepeatedly(Return(true));
Paul Stewart71a4d3b2013-01-18 18:12:56 -0800821 set<string> groups;
822 groups.insert(storage_id);
823 EXPECT_CALL(mock_store, GetGroupsWithProperties(
824 ContainsWiFiProperties(
Paul Stewart3c504012013-01-17 17:49:58 -0800825 simple_ssid(), flimflam::kModeManaged, flimflam::kSecurityPsk)))
Paul Stewart71a4d3b2013-01-18 18:12:56 -0800826 .WillRepeatedly(Return(groups));
Paul Stewartd8ad3c42012-01-09 12:39:38 -0800827 EXPECT_CALL(mock_store, GetBool(_, _, _))
828 .WillRepeatedly(Return(false));
829 const string passphrase = "passphrase";
830 EXPECT_CALL(mock_store,
831 GetCryptedString(StrEq(storage_id),
832 WiFiService::kStoragePassphrase, _))
833 .WillRepeatedly(DoAll(SetArgumentPointee<2>(passphrase), Return(true)));
834 EXPECT_CALL(mock_store,
835 GetCryptedString(StrEq(storage_id),
836 StrNe(WiFiService::kStoragePassphrase), _))
837 .WillRepeatedly(Return(false));
838 EXPECT_TRUE(service->need_passphrase_);
839 EXPECT_TRUE(service->Load(&mock_store));
840 EXPECT_EQ(passphrase, service->passphrase_);
841 EXPECT_TRUE(service->connectable());
842 EXPECT_FALSE(service->need_passphrase_);
843 service->Unload();
844 EXPECT_EQ(string(""), service->passphrase_);
845 EXPECT_FALSE(service->connectable());
846 EXPECT_TRUE(service->need_passphrase_);
847}
848
Christopher Wiley27b47232012-11-02 13:13:00 -0700849TEST_F(WiFiServiceTest, ConfigureMakesConnectable) {
850 string guid("legit_guid");
851 KeyValueStore args;
852 args.SetString(flimflam::kEapIdentityProperty, "legit_identity");
853 args.SetString(flimflam::kEapPasswordProperty, "legit_password");
854 args.SetString(flimflam::kEAPEAPProperty, "PEAP");
855 args.SetString(flimflam::kGuidProperty, guid);
856 Error error;
Christopher Wiley27b47232012-11-02 13:13:00 -0700857
Paul Stewart3c504012013-01-17 17:49:58 -0800858 WiFiServiceRefPtr service = MakeSimpleService(flimflam::kSecurity8021x);
Christopher Wiley27b47232012-11-02 13:13:00 -0700859 // Hack the GUID in so that we don't have to mess about with WiFi to regsiter
860 // our service. This way, Manager will handle the lookup itself.
861 service->set_guid(guid);
862 manager()->RegisterService(service);
863 EXPECT_FALSE(service->connectable());
864 EXPECT_EQ(service.get(), manager()->GetService(args, &error).get());
865 EXPECT_TRUE(error.IsSuccess());
866 EXPECT_TRUE(service->connectable());
867}
868
Paul Stewart3c504012013-01-17 17:49:58 -0800869TEST_F(WiFiServiceTest, ConfigurePassphrase) {
870 EXPECT_EQ(Error::kNotSupported,
871 TestConfigurePassphrase(flimflam::kSecurityNone, ""));
872 EXPECT_EQ(Error::kNotSupported,
873 TestConfigurePassphrase(flimflam::kSecurityNone, "foo"));
874 EXPECT_EQ(Error::kSuccess,
875 TestConfigurePassphrase(flimflam::kSecurityWep, NULL));
876 EXPECT_EQ(Error::kInvalidPassphrase,
877 TestConfigurePassphrase(flimflam::kSecurityWep, ""));
878 EXPECT_EQ(Error::kInvalidPassphrase,
879 TestConfigurePassphrase(flimflam::kSecurityWep, "abcd"));
880 EXPECT_EQ(Error::kSuccess,
881 TestConfigurePassphrase(flimflam::kSecurityWep, "abcde"));
882 EXPECT_EQ(Error::kSuccess,
883 TestConfigurePassphrase(flimflam::kSecurityWep, "abcdefghijklm"));
884 EXPECT_EQ(Error::kSuccess,
885 TestConfigurePassphrase(flimflam::kSecurityWep, "0:abcdefghijklm"));
886 EXPECT_EQ(Error::kSuccess,
887 TestConfigurePassphrase(flimflam::kSecurityWep, "0102030405"));
888 EXPECT_EQ(Error::kInvalidPassphrase,
889 TestConfigurePassphrase(flimflam::kSecurityWep, "0x0102030405"));
890 EXPECT_EQ(Error::kInvalidPassphrase,
891 TestConfigurePassphrase(flimflam::kSecurityWep, "O102030405"));
892 EXPECT_EQ(Error::kInvalidPassphrase,
893 TestConfigurePassphrase(flimflam::kSecurityWep, "1:O102030405"));
894 EXPECT_EQ(Error::kInvalidPassphrase,
895 TestConfigurePassphrase(flimflam::kSecurityWep, "1:0xO102030405"));
896 EXPECT_EQ(Error::kInvalidPassphrase,
897 TestConfigurePassphrase(flimflam::kSecurityWep, "0xO102030405"));
898 EXPECT_EQ(Error::kSuccess,
899 TestConfigurePassphrase(flimflam::kSecurityWep,
900 "0102030405060708090a0b0c0d"));
901 EXPECT_EQ(Error::kSuccess,
902 TestConfigurePassphrase(flimflam::kSecurityWep,
903 "0102030405060708090A0B0C0D"));
904 EXPECT_EQ(Error::kSuccess,
905 TestConfigurePassphrase(flimflam::kSecurityWep,
906 "0:0102030405060708090a0b0c0d"));
907 EXPECT_EQ(Error::kSuccess,
908 TestConfigurePassphrase(flimflam::kSecurityWep,
909 "0:0x0102030405060708090a0b0c0d"));
910 EXPECT_EQ(Error::kSuccess,
911 TestConfigurePassphrase(flimflam::kSecurityWpa, NULL));
912 EXPECT_EQ(Error::kSuccess,
913 TestConfigurePassphrase(flimflam::kSecurityWpa, "secure password"));
914 EXPECT_EQ(Error::kInvalidPassphrase,
915 TestConfigurePassphrase(flimflam::kSecurityWpa, ""));
916 EXPECT_EQ(Error::kSuccess, TestConfigurePassphrase(
917 flimflam::kSecurityWpa,
918 string(IEEE_80211::kWPAAsciiMinLen, 'Z').c_str()));
919 EXPECT_EQ(Error::kSuccess, TestConfigurePassphrase(
920 flimflam::kSecurityWpa,
921 string(IEEE_80211::kWPAAsciiMaxLen, 'Z').c_str()));
922 // subtle: invalid length for hex key, but valid as ascii passphrase
923 EXPECT_EQ(Error::kSuccess, TestConfigurePassphrase(
924 flimflam::kSecurityWpa,
925 string(IEEE_80211::kWPAHexLen-1, '1').c_str()));
926 EXPECT_EQ(Error::kSuccess, TestConfigurePassphrase(
927 flimflam::kSecurityWpa,
928 string(IEEE_80211::kWPAHexLen, '1').c_str()));
929 EXPECT_EQ(Error::kInvalidPassphrase, TestConfigurePassphrase(
930 flimflam::kSecurityWpa,
931 string(IEEE_80211::kWPAAsciiMinLen-1, 'Z').c_str()));
932 EXPECT_EQ(Error::kInvalidPassphrase, TestConfigurePassphrase(
933 flimflam::kSecurityWpa,
934 string(IEEE_80211::kWPAAsciiMaxLen+1, 'Z').c_str()));
935 EXPECT_EQ(Error::kInvalidPassphrase, TestConfigurePassphrase(
936 flimflam::kSecurityWpa,
937 string(IEEE_80211::kWPAHexLen+1, '1').c_str()));
938}
939
940TEST_F(WiFiServiceTest, ConfigureRedundantProperties) {
941 WiFiServiceRefPtr service = MakeSimpleService(flimflam::kSecurityNone);
942 KeyValueStore args;
943 args.SetString(flimflam::kTypeProperty, flimflam::kTypeWifi);
944 args.SetString(flimflam::kSSIDProperty, simple_ssid_string());
945 args.SetString(flimflam::kSecurityProperty, flimflam::kSecurityNone);
946 const string kGUID = "aguid";
947 args.SetString(flimflam::kGuidProperty, kGUID);
948
949 EXPECT_EQ("", service->guid());
950 Error error;
951 service->Configure(args, &error);
952 EXPECT_TRUE(error.IsSuccess());
953 EXPECT_EQ(kGUID, service->guid());
954}
955
956TEST_F(WiFiServiceTest, DisconnectWithWiFi) {
957 WiFiServiceRefPtr service = MakeServiceWithWiFi(flimflam::kSecurityWep);
958 EXPECT_CALL(*wifi(), DisconnectFrom(service.get())).Times(1);
959 Error error;
960 service->Disconnect(&error);
961}
962
963TEST_F(WiFiServiceTest, DisconnectWithoutWiFi) {
964 WiFiServiceRefPtr service = MakeSimpleService(flimflam::kSecurityWep);
965 EXPECT_CALL(*wifi(), DisconnectFrom(_)).Times(0);
966 Error error;
967 service->Disconnect(&error);
968 EXPECT_EQ(Error::kOperationFailed, error.type());
969}
970
971TEST_F(WiFiServiceTest, DisconnectWithoutWiFiWhileAssociating) {
972 WiFiServiceRefPtr service = MakeSimpleService(flimflam::kSecurityWep);
973 EXPECT_CALL(*wifi(), DisconnectFrom(_)).Times(0);
974 service->SetState(Service::kStateAssociating);
975 ScopedMockLog log;
976 EXPECT_CALL(log, Log(_, _, _)).Times(AnyNumber());
977 EXPECT_CALL(log, Log(logging::LOG_ERROR, _,
978 HasSubstr("WiFi endpoints do not (yet) exist.")));
979 Error error;
980 service->Disconnect(&error);
981 EXPECT_EQ(Error::kOperationFailed, error.type());
982}
983
Paul Stewart835934a2012-12-06 19:27:09 -0800984TEST_F(WiFiServiceTest, UnloadAndClearCacheWEP) {
Paul Stewart3c504012013-01-17 17:49:58 -0800985 WiFiServiceRefPtr service = MakeServiceWithWiFi(flimflam::kSecurityWep);
Paul Stewart835934a2012-12-06 19:27:09 -0800986 EXPECT_CALL(*wifi(), ClearCachedCredentials(service.get())).Times(1);
987 EXPECT_CALL(*wifi(), DisconnectFrom(service.get())).Times(1);
Paul Stewart66c86002012-01-30 18:00:52 -0800988 service->Unload();
989}
990
991TEST_F(WiFiServiceTest, UnloadAndClearCache8021x) {
Paul Stewart3c504012013-01-17 17:49:58 -0800992 WiFiServiceRefPtr service = MakeServiceWithWiFi(flimflam::kSecurity8021x);
Paul Stewart835934a2012-12-06 19:27:09 -0800993 EXPECT_CALL(*wifi(), ClearCachedCredentials(service.get())).Times(1);
994 EXPECT_CALL(*wifi(), DisconnectFrom(service.get())).Times(1);
Paul Stewart66c86002012-01-30 18:00:52 -0800995 service->Unload();
996}
997
Paul Stewart0756db92012-01-27 08:34:47 -0800998TEST_F(WiFiServiceTest, ParseStorageIdentifierNone) {
Paul Stewart3c504012013-01-17 17:49:58 -0800999 WiFiServiceRefPtr service = MakeSimpleService(flimflam::kSecurityNone);
Paul Stewarta41e38d2011-11-11 07:47:29 -08001000 const string storage_id = service->GetStorageIdentifier();
1001 string address;
1002 string mode;
1003 string security;
1004 EXPECT_TRUE(service->ParseStorageIdentifier(storage_id, &address, &mode,
1005 &security));
Paul Stewart3c504012013-01-17 17:49:58 -08001006 EXPECT_EQ(StringToLowerASCII(GetAnyDeviceAddress()), address);
Paul Stewarta41e38d2011-11-11 07:47:29 -08001007 EXPECT_EQ(flimflam::kModeManaged, mode);
1008 EXPECT_EQ(flimflam::kSecurityNone, security);
1009}
1010
Paul Stewart0756db92012-01-27 08:34:47 -08001011TEST_F(WiFiServiceTest, ParseStorageIdentifier8021x) {
1012 // Do a separate test for 802.1x, since kSecurity8021x contains a "_",
1013 // which needs to be dealt with specially in the parser.
Paul Stewart3c504012013-01-17 17:49:58 -08001014 WiFiServiceRefPtr service = MakeSimpleService(flimflam::kSecurity8021x);
Paul Stewart0756db92012-01-27 08:34:47 -08001015 const string storage_id = service->GetStorageIdentifier();
1016 string address;
1017 string mode;
1018 string security;
1019 EXPECT_TRUE(service->ParseStorageIdentifier(storage_id, &address, &mode,
1020 &security));
Paul Stewart3c504012013-01-17 17:49:58 -08001021 EXPECT_EQ(StringToLowerASCII(GetAnyDeviceAddress()), address);
Paul Stewart0756db92012-01-27 08:34:47 -08001022 EXPECT_EQ(flimflam::kModeManaged, mode);
1023 EXPECT_EQ(flimflam::kSecurity8021x, security);
1024}
1025
Paul Stewart85aea152013-01-22 09:31:56 -08001026TEST_F(WiFiServiceFixupStorageTest, FixedEntries) {
1027 const string kNonWiFiId = "vpn_foo";
1028 const string kUnparsableWiFiId = "wifi_foo";
1029
1030 AddGroup(kNonWiFiId);
1031 AddGroup(kUnparsableWiFiId);
Paul Stewart71a4d3b2013-01-18 18:12:56 -08001032 AddServiceEntry(true, true, true, true);
1033 AddServiceEntry(false, false, false, false);
1034 AddServiceEntry(true, true, true, true);
1035 AddServiceEntry(false, false, false, false);
Paul Stewart85aea152013-01-22 09:31:56 -08001036 EXPECT_TRUE(FixupServiceEntries());
1037}
1038
1039TEST_F(WiFiServiceFixupStorageTest, NoFixedEntries) {
1040 const string kNonWiFiId = "vpn_foo";
1041 const string kUnparsableWiFiId = "wifi_foo";
1042
1043 AddGroup(kNonWiFiId);
1044 AddGroup(kUnparsableWiFiId);
Paul Stewart71a4d3b2013-01-18 18:12:56 -08001045 AddServiceEntry(true, true, true, true);
Paul Stewart85aea152013-01-22 09:31:56 -08001046 EXPECT_FALSE(FixupServiceEntries());
1047}
1048
1049TEST_F(WiFiServiceFixupStorageTest, MissingTypeProperty) {
Paul Stewart71a4d3b2013-01-18 18:12:56 -08001050 AddServiceEntry(false, true, true, true);
Paul Stewart85aea152013-01-22 09:31:56 -08001051 EXPECT_TRUE(FixupServiceEntries());
1052}
1053
1054TEST_F(WiFiServiceFixupStorageTest, MissingModeProperty) {
Paul Stewart71a4d3b2013-01-18 18:12:56 -08001055 AddServiceEntry(true, false, true, true);
Paul Stewart85aea152013-01-22 09:31:56 -08001056 EXPECT_TRUE(FixupServiceEntries());
1057}
1058
1059TEST_F(WiFiServiceFixupStorageTest, MissingSecurityProperty) {
Paul Stewart71a4d3b2013-01-18 18:12:56 -08001060 AddServiceEntry(true, true, false, true);
Paul Stewart85aea152013-01-22 09:31:56 -08001061 EXPECT_TRUE(FixupServiceEntries());
1062}
1063
Paul Stewart71a4d3b2013-01-18 18:12:56 -08001064TEST_F(WiFiServiceFixupStorageTest, MissingSecurityClassProperty) {
1065 AddServiceEntry(true, true, true, false);
1066 EXPECT_TRUE(FixupServiceEntries());
1067}
Paul Stewart85aea152013-01-22 09:31:56 -08001068
mukesh agrawal29c13a12011-11-24 00:09:19 +00001069TEST_F(WiFiServiceTest, Connectable) {
1070 // Open network should be connectable.
Gaurav Shah10109f22011-11-11 20:16:22 -08001071 EXPECT_TRUE(CheckConnectable(flimflam::kSecurityNone, NULL, NULL));
mukesh agrawal29c13a12011-11-24 00:09:19 +00001072
1073 // Open network should remain connectable if we try to set a password on it.
Gaurav Shah10109f22011-11-11 20:16:22 -08001074 EXPECT_TRUE(CheckConnectable(flimflam::kSecurityNone, "abcde", NULL));
mukesh agrawal29c13a12011-11-24 00:09:19 +00001075
1076 // WEP network with passphrase set should be connectable.
Gaurav Shah10109f22011-11-11 20:16:22 -08001077 EXPECT_TRUE(CheckConnectable(flimflam::kSecurityWep, "abcde", NULL));
mukesh agrawal29c13a12011-11-24 00:09:19 +00001078
1079 // WEP network without passphrase set should NOT be connectable.
Gaurav Shah10109f22011-11-11 20:16:22 -08001080 EXPECT_FALSE(CheckConnectable(flimflam::kSecurityWep, NULL, NULL));
mukesh agrawal29c13a12011-11-24 00:09:19 +00001081
1082 // A bad passphrase should not make a WEP network connectable.
Gaurav Shah10109f22011-11-11 20:16:22 -08001083 EXPECT_FALSE(CheckConnectable(flimflam::kSecurityWep, "a", NULL));
mukesh agrawal29c13a12011-11-24 00:09:19 +00001084
1085 // Similar to WEP, for WPA.
Gaurav Shah10109f22011-11-11 20:16:22 -08001086 EXPECT_TRUE(CheckConnectable(flimflam::kSecurityWpa, "abcdefgh", NULL));
1087 EXPECT_FALSE(CheckConnectable(flimflam::kSecurityWpa, NULL, NULL));
1088 EXPECT_FALSE(CheckConnectable(flimflam::kSecurityWpa, "a", NULL));
mukesh agrawal29c13a12011-11-24 00:09:19 +00001089
1090 // Unconfigured 802.1x should NOT be connectable.
Gaurav Shah10109f22011-11-11 20:16:22 -08001091 EXPECT_FALSE(CheckConnectable(flimflam::kSecurity8021x, NULL, NULL));
1092
Paul Stewart0654ece2013-03-26 15:21:26 -07001093 EapCredentials eap;
Gaurav Shah10109f22011-11-11 20:16:22 -08001094 // Empty EAP credentials should not make a 802.1x network connectable.
1095 EXPECT_FALSE(CheckConnectable(flimflam::kSecurity8021x, NULL, &eap));
1096
1097 eap.identity = "something";
1098 // If client certificate is being used, a private key must exist.
1099 eap.client_cert = "some client cert";
1100 EXPECT_FALSE(CheckConnectable(flimflam::kSecurity8021x, NULL, &eap));
1101 eap.private_key = "some private key";
1102 EXPECT_TRUE(CheckConnectable(flimflam::kSecurity8021x, NULL, &eap));
1103
1104 // Identity is always required.
1105 eap.identity.clear();
1106 EXPECT_FALSE(CheckConnectable(flimflam::kSecurity8021x, NULL, &eap));
1107
1108 eap.identity = "something";
1109 // For non EAP-TLS types, a password is required.
1110 eap.eap = "Non-TLS";
1111 EXPECT_FALSE(CheckConnectable(flimflam::kSecurity8021x, NULL, &eap));
1112 eap.password = "some password";
1113 EXPECT_TRUE(CheckConnectable(flimflam::kSecurity8021x, NULL, &eap));
Gaurav Shah29d68882012-01-30 19:06:42 -08001114 // Dynamic WEP + 802.1X should be connectable under the same conditions.
1115 eap.key_management = "IEEE8021X";
1116 EXPECT_TRUE(CheckConnectable(flimflam::kSecurityWep, NULL, &eap));
mukesh agrawal29c13a12011-11-24 00:09:19 +00001117}
1118
mukesh agrawal8a3188d2011-12-01 20:56:44 +00001119TEST_F(WiFiServiceTest, IsAutoConnectable) {
mukesh agrawalbf14e942012-03-02 14:36:34 -08001120 const char *reason;
Paul Stewart3c504012013-01-17 17:49:58 -08001121 WiFiServiceRefPtr service = MakeSimpleService(flimflam::kSecurityNone);
mukesh agrawal8a3188d2011-12-01 20:56:44 +00001122 EXPECT_CALL(*wifi(), IsIdle())
1123 .WillRepeatedly(Return(true));
1124 EXPECT_FALSE(service->HasEndpoints());
mukesh agrawalbf14e942012-03-02 14:36:34 -08001125 EXPECT_FALSE(service->IsAutoConnectable(&reason));
1126 EXPECT_STREQ(WiFiService::kAutoConnNoEndpoint, reason);
mukesh agrawal8a3188d2011-12-01 20:56:44 +00001127
mukesh agrawalbf14e942012-03-02 14:36:34 -08001128 reason = "";
mukesh agrawal43970a22013-02-15 16:00:07 -08001129 WiFiEndpointRefPtr endpoint =
1130 MakeOpenEndpoint("a", "00:00:00:00:00:01", 0, 0);
mukesh agrawal8a3188d2011-12-01 20:56:44 +00001131 service->AddEndpoint(endpoint);
1132 EXPECT_CALL(*wifi(), IsIdle())
1133 .WillRepeatedly(Return(true));
1134 EXPECT_TRUE(service->HasEndpoints());
mukesh agrawalbf14e942012-03-02 14:36:34 -08001135 EXPECT_TRUE(service->IsAutoConnectable(&reason));
1136 EXPECT_STREQ("", reason);
mukesh agrawal8a3188d2011-12-01 20:56:44 +00001137
1138 // WiFi only supports connecting to one Service at a time. So, to
1139 // avoid disrupting connectivity, we only allow auto-connection to
1140 // a WiFiService when the corresponding WiFi is idle.
1141 EXPECT_CALL(*wifi(), IsIdle())
1142 .WillRepeatedly(Return(false));
1143 EXPECT_TRUE(service->HasEndpoints());
mukesh agrawalbf14e942012-03-02 14:36:34 -08001144 EXPECT_FALSE(service->IsAutoConnectable(&reason));
1145 EXPECT_STREQ(WiFiService::kAutoConnBusy, reason);
mukesh agrawal8a3188d2011-12-01 20:56:44 +00001146}
1147
1148TEST_F(WiFiServiceTest, AutoConnect) {
mukesh agrawalbf14e942012-03-02 14:36:34 -08001149 const char *reason;
Paul Stewart3c504012013-01-17 17:49:58 -08001150 WiFiServiceRefPtr service = MakeSimpleService(flimflam::kSecurityNone);
mukesh agrawalbf14e942012-03-02 14:36:34 -08001151 EXPECT_FALSE(service->IsAutoConnectable(&reason));
mukesh agrawal8a3188d2011-12-01 20:56:44 +00001152 EXPECT_CALL(*wifi(), ConnectTo(_, _))
1153 .Times(0);
1154 service->AutoConnect();
Eric Shienbrood3e20a232012-02-16 11:35:56 -05001155 dispatcher()->DispatchPendingEvents();
mukesh agrawal8a3188d2011-12-01 20:56:44 +00001156
mukesh agrawal43970a22013-02-15 16:00:07 -08001157 WiFiEndpointRefPtr endpoint =
1158 MakeOpenEndpoint("a", "00:00:00:00:00:01", 0, 0);
mukesh agrawal8a3188d2011-12-01 20:56:44 +00001159 service->AddEndpoint(endpoint);
1160 EXPECT_CALL(*wifi(), IsIdle())
1161 .WillRepeatedly(Return(true));
mukesh agrawalbf14e942012-03-02 14:36:34 -08001162 EXPECT_TRUE(service->IsAutoConnectable(&reason));
mukesh agrawal8a3188d2011-12-01 20:56:44 +00001163 EXPECT_CALL(*wifi(), ConnectTo(_, _));
1164 service->AutoConnect();
Eric Shienbrood3e20a232012-02-16 11:35:56 -05001165 dispatcher()->DispatchPendingEvents();
mukesh agrawaladb68482012-01-17 16:31:51 -08001166
1167 Error error;
Christopher Wileyabd3b502012-09-26 13:08:52 -07001168 service->UserInitiatedDisconnect(&error);
Eric Shienbrood3e20a232012-02-16 11:35:56 -05001169 dispatcher()->DispatchPendingEvents();
mukesh agrawalbf14e942012-03-02 14:36:34 -08001170 EXPECT_FALSE(service->IsAutoConnectable(&reason));
mukesh agrawal8a3188d2011-12-01 20:56:44 +00001171}
1172
mukesh agrawal8abd2f62012-01-30 14:56:14 -08001173TEST_F(WiFiServiceTest, ClearWriteOnlyDerivedProperty) {
Paul Stewart3c504012013-01-17 17:49:58 -08001174 WiFiServiceRefPtr wifi_service = MakeSimpleService(flimflam::kSecurityWep);
mukesh agrawal8abd2f62012-01-30 14:56:14 -08001175
1176 EXPECT_EQ("", wifi_service->passphrase_);
1177
1178 ::DBus::Error error;
mukesh agrawal6bb9e7c2012-01-30 14:57:54 -08001179 EXPECT_TRUE(DBusAdaptor::SetProperty(
mukesh agrawal8abd2f62012-01-30 14:56:14 -08001180 wifi_service->mutable_store(),
1181 flimflam::kPassphraseProperty,
1182 DBusAdaptor::StringToVariant("0:abcde"),
1183 &error));
1184 EXPECT_EQ("0:abcde", wifi_service->passphrase_);
1185
1186 EXPECT_TRUE(DBusAdaptor::ClearProperty(wifi_service->mutable_store(),
1187 flimflam::kPassphraseProperty,
1188 &error));
1189 EXPECT_EQ("", wifi_service->passphrase_);
1190}
1191
mukesh agrawale1d90e92012-02-15 17:36:08 -08001192TEST_F(WiFiServiceTest, SignalToStrength) {
1193 // Verify that our mapping is sane, in the sense that it preserves ordering.
1194 // We break the test into two domains, because we assume that positive
1195 // values aren't actually in dBm.
1196 for (int16 i = std::numeric_limits<int16>::min(); i < 0; ++i) {
1197 int16 current_mapped = WiFiService::SignalToStrength(i);
1198 int16 next_mapped = WiFiService::SignalToStrength(i+1);
1199 EXPECT_LE(current_mapped, next_mapped)
1200 << "(original values " << i << " " << i+1 << ")";
mukesh agrawal8f3f7752012-02-17 19:42:09 -08001201 EXPECT_GE(current_mapped, Service::kStrengthMin);
1202 EXPECT_LE(current_mapped, Service::kStrengthMax);
mukesh agrawale1d90e92012-02-15 17:36:08 -08001203 }
1204 for (int16 i = 1; i < std::numeric_limits<int16>::max(); ++i) {
1205 int16 current_mapped = WiFiService::SignalToStrength(i);
1206 int16 next_mapped = WiFiService::SignalToStrength(i+1);
1207 EXPECT_LE(current_mapped, next_mapped)
1208 << "(original values " << i << " " << i+1 << ")";
mukesh agrawal8f3f7752012-02-17 19:42:09 -08001209 EXPECT_GE(current_mapped, Service::kStrengthMin);
1210 EXPECT_LE(current_mapped, Service::kStrengthMax);
mukesh agrawale1d90e92012-02-15 17:36:08 -08001211 }
1212}
1213
1214TEST_F(WiFiServiceUpdateFromEndpointsTest, Strengths) {
1215 // If the chosen signal values don't map to distinct strength
1216 // values, then we can't expect our other tests to pass. So verify
1217 // their distinctness.
1218 EXPECT_TRUE(kOkEndpointStrength != kBadEndpointStrength);
1219 EXPECT_TRUE(kOkEndpointStrength != kGoodEndpointStrength);
1220 EXPECT_TRUE(kGoodEndpointStrength != kBadEndpointStrength);
1221}
1222
1223TEST_F(WiFiServiceUpdateFromEndpointsTest, Floating) {
1224 // Initial endpoint updates values.
1225 EXPECT_CALL(adaptor, EmitUint16Changed(
1226 flimflam::kWifiFrequency, kOkEndpointFrequency));
mukesh agrawal923f14f2012-06-04 16:46:08 -07001227 EXPECT_CALL(adaptor, EmitStringChanged(
1228 flimflam::kWifiBSsid, kOkEndpointBssId));
1229 EXPECT_CALL(adaptor, EmitUint8Changed(
mukesh agrawale1d90e92012-02-15 17:36:08 -08001230 flimflam::kSignalStrengthProperty, kOkEndpointStrength));
mukesh agrawalf6b32092013-04-10 15:49:55 -07001231 EXPECT_CALL(adaptor, EmitUint16Changed(
1232 flimflam::kWifiPhyMode, Metrics::kWiFiNetworkPhyMode11b));
mukesh agrawale1d90e92012-02-15 17:36:08 -08001233 service->AddEndpoint(ok_endpoint);
Darin Petkov4a66cc52012-06-15 10:08:29 +02001234 EXPECT_EQ(1, service->GetEndpointCount());
mukesh agrawale1d90e92012-02-15 17:36:08 -08001235 Mock::VerifyAndClearExpectations(&adaptor);
1236
1237 // Endpoint with stronger signal updates values.
1238 EXPECT_CALL(adaptor, EmitUint16Changed(
1239 flimflam::kWifiFrequency, kGoodEndpointFrequency));
mukesh agrawal923f14f2012-06-04 16:46:08 -07001240 EXPECT_CALL(adaptor, EmitStringChanged(
1241 flimflam::kWifiBSsid, kGoodEndpointBssId));
mukesh agrawale1d90e92012-02-15 17:36:08 -08001242 EXPECT_CALL(adaptor, EmitUint8Changed(
1243 flimflam::kSignalStrengthProperty, kGoodEndpointStrength));
mukesh agrawalf6b32092013-04-10 15:49:55 -07001244 // However, both endpoints are 11b.
1245 EXPECT_CALL(adaptor, EmitUint16Changed(flimflam::kWifiPhyMode, _)).Times(0);
mukesh agrawale1d90e92012-02-15 17:36:08 -08001246 service->AddEndpoint(good_endpoint);
Darin Petkov4a66cc52012-06-15 10:08:29 +02001247 EXPECT_EQ(2, service->GetEndpointCount());
mukesh agrawale1d90e92012-02-15 17:36:08 -08001248 Mock::VerifyAndClearExpectations(&adaptor);
1249
1250 // Endpoint with lower signal does not change values.
1251 EXPECT_CALL(adaptor, EmitUint16Changed(flimflam::kWifiFrequency, _)).Times(0);
mukesh agrawal923f14f2012-06-04 16:46:08 -07001252 EXPECT_CALL(adaptor, EmitStringChanged(flimflam::kWifiBSsid, _)).Times(0);
mukesh agrawale1d90e92012-02-15 17:36:08 -08001253 EXPECT_CALL(adaptor,
1254 EmitUint8Changed(flimflam::kSignalStrengthProperty, _)).Times(0);
mukesh agrawalf6b32092013-04-10 15:49:55 -07001255 EXPECT_CALL(adaptor, EmitUint16Changed(flimflam::kWifiPhyMode, _)).Times(0);
mukesh agrawale1d90e92012-02-15 17:36:08 -08001256 service->AddEndpoint(bad_endpoint);
Darin Petkov4a66cc52012-06-15 10:08:29 +02001257 EXPECT_EQ(3, service->GetEndpointCount());
mukesh agrawale1d90e92012-02-15 17:36:08 -08001258 Mock::VerifyAndClearExpectations(&adaptor);
1259
1260 // Removing non-optimal endpoint does not change values.
1261 EXPECT_CALL(adaptor, EmitUint16Changed(flimflam::kWifiFrequency, _)).Times(0);
mukesh agrawal923f14f2012-06-04 16:46:08 -07001262 EXPECT_CALL(adaptor, EmitStringChanged(flimflam::kWifiBSsid, _)).Times(0);
mukesh agrawale1d90e92012-02-15 17:36:08 -08001263 EXPECT_CALL(adaptor,
1264 EmitUint8Changed(flimflam::kSignalStrengthProperty, _)).Times(0);
mukesh agrawalf6b32092013-04-10 15:49:55 -07001265 EXPECT_CALL(adaptor, EmitUint16Changed(flimflam::kWifiPhyMode, _)).Times(0);
mukesh agrawale1d90e92012-02-15 17:36:08 -08001266 service->RemoveEndpoint(bad_endpoint);
Darin Petkov4a66cc52012-06-15 10:08:29 +02001267 EXPECT_EQ(2, service->GetEndpointCount());
mukesh agrawale1d90e92012-02-15 17:36:08 -08001268 Mock::VerifyAndClearExpectations(&adaptor);
1269
1270 // Removing optimal endpoint updates values.
1271 EXPECT_CALL(adaptor, EmitUint16Changed(
1272 flimflam::kWifiFrequency, kOkEndpointFrequency));
mukesh agrawal923f14f2012-06-04 16:46:08 -07001273 EXPECT_CALL(adaptor, EmitStringChanged(
1274 flimflam::kWifiBSsid, kOkEndpointBssId));
mukesh agrawale1d90e92012-02-15 17:36:08 -08001275 EXPECT_CALL(adaptor, EmitUint8Changed(
1276 flimflam::kSignalStrengthProperty, kOkEndpointStrength));
mukesh agrawalf6b32092013-04-10 15:49:55 -07001277 // However, both endpoints are 11b.
1278 EXPECT_CALL(adaptor, EmitUint16Changed(flimflam::kWifiPhyMode, _)).Times(0);
mukesh agrawale1d90e92012-02-15 17:36:08 -08001279 service->RemoveEndpoint(good_endpoint);
Darin Petkov4a66cc52012-06-15 10:08:29 +02001280 EXPECT_EQ(1, service->GetEndpointCount());
mukesh agrawale1d90e92012-02-15 17:36:08 -08001281 Mock::VerifyAndClearExpectations(&adaptor);
1282
1283 // Removing last endpoint updates values (and doesn't crash).
1284 EXPECT_CALL(adaptor, EmitUint16Changed(flimflam::kWifiFrequency, _));
mukesh agrawal923f14f2012-06-04 16:46:08 -07001285 EXPECT_CALL(adaptor, EmitStringChanged(flimflam::kWifiBSsid, _));
mukesh agrawale1d90e92012-02-15 17:36:08 -08001286 EXPECT_CALL(adaptor, EmitUint8Changed(flimflam::kSignalStrengthProperty, _));
mukesh agrawalf6b32092013-04-10 15:49:55 -07001287 EXPECT_CALL(adaptor, EmitUint16Changed(
1288 flimflam::kWifiPhyMode, Metrics::kWiFiNetworkPhyModeUndef));
mukesh agrawale1d90e92012-02-15 17:36:08 -08001289 service->RemoveEndpoint(ok_endpoint);
Darin Petkov4a66cc52012-06-15 10:08:29 +02001290 EXPECT_EQ(0, service->GetEndpointCount());
mukesh agrawale1d90e92012-02-15 17:36:08 -08001291 Mock::VerifyAndClearExpectations(&adaptor);
1292}
1293
1294TEST_F(WiFiServiceUpdateFromEndpointsTest, Connected) {
1295 EXPECT_CALL(adaptor, EmitUint16Changed(_, _)).Times(AnyNumber());
mukesh agrawal923f14f2012-06-04 16:46:08 -07001296 EXPECT_CALL(adaptor, EmitStringChanged(_, _)).Times(AnyNumber());
mukesh agrawale1d90e92012-02-15 17:36:08 -08001297 EXPECT_CALL(adaptor, EmitUint8Changed(_, _)).Times(AnyNumber());
1298 EXPECT_CALL(adaptor, EmitBoolChanged(_, _)).Times(AnyNumber());
1299 service->AddEndpoint(bad_endpoint);
1300 service->AddEndpoint(ok_endpoint);
Darin Petkov4a66cc52012-06-15 10:08:29 +02001301 EXPECT_EQ(2, service->GetEndpointCount());
mukesh agrawale1d90e92012-02-15 17:36:08 -08001302 Mock::VerifyAndClearExpectations(&adaptor);
1303
1304 // Setting current endpoint forces adoption of its values, even if it
1305 // doesn't have the highest signal.
1306 EXPECT_CALL(adaptor, EmitUint16Changed(
1307 flimflam::kWifiFrequency, kBadEndpointFrequency));
mukesh agrawal923f14f2012-06-04 16:46:08 -07001308 EXPECT_CALL(adaptor, EmitStringChanged(
1309 flimflam::kWifiBSsid, kBadEndpointBssId));
mukesh agrawale1d90e92012-02-15 17:36:08 -08001310 EXPECT_CALL(adaptor, EmitUint8Changed(
1311 flimflam::kSignalStrengthProperty, kBadEndpointStrength));
1312 service->NotifyCurrentEndpoint(bad_endpoint);
1313 Mock::VerifyAndClearExpectations(&adaptor);
1314
1315 // Adding a better endpoint doesn't matter, when current endpoint is set.
1316 EXPECT_CALL(adaptor, EmitUint16Changed(flimflam::kWifiFrequency, _)).Times(0);
mukesh agrawal923f14f2012-06-04 16:46:08 -07001317 EXPECT_CALL(adaptor, EmitStringChanged(flimflam::kWifiBSsid, _)).Times(0);
mukesh agrawale1d90e92012-02-15 17:36:08 -08001318 EXPECT_CALL(adaptor,
1319 EmitUint8Changed(flimflam::kSignalStrengthProperty, _)).Times(0);
1320 service->AddEndpoint(good_endpoint);
Darin Petkov4a66cc52012-06-15 10:08:29 +02001321 EXPECT_EQ(3, service->GetEndpointCount());
mukesh agrawale1d90e92012-02-15 17:36:08 -08001322 Mock::VerifyAndClearExpectations(&adaptor);
1323
1324 // Removing a better endpoint doesn't matter, when current endpoint is set.
1325 EXPECT_CALL(adaptor, EmitUint16Changed(flimflam::kWifiFrequency, _)).Times(0);
mukesh agrawal923f14f2012-06-04 16:46:08 -07001326 EXPECT_CALL(adaptor, EmitStringChanged(flimflam::kWifiBSsid, _)).Times(0);
mukesh agrawale1d90e92012-02-15 17:36:08 -08001327 EXPECT_CALL(adaptor,
1328 EmitUint8Changed(flimflam::kSignalStrengthProperty, _)).Times(0);
1329 service->RemoveEndpoint(good_endpoint);
1330 Mock::VerifyAndClearExpectations(&adaptor);
1331
1332 // Removing the current endpoint is safe and sane.
1333 EXPECT_CALL(adaptor, EmitUint16Changed(
1334 flimflam::kWifiFrequency, kOkEndpointFrequency));
mukesh agrawal923f14f2012-06-04 16:46:08 -07001335 EXPECT_CALL(adaptor, EmitStringChanged(
1336 flimflam::kWifiBSsid, kOkEndpointBssId));
mukesh agrawale1d90e92012-02-15 17:36:08 -08001337 EXPECT_CALL(adaptor, EmitUint8Changed(
1338 flimflam::kSignalStrengthProperty, kOkEndpointStrength));
1339 service->RemoveEndpoint(bad_endpoint);
1340 Mock::VerifyAndClearExpectations(&adaptor);
1341
1342 // Clearing the current endpoint (without removing it) is also safe and sane.
1343 service->NotifyCurrentEndpoint(ok_endpoint);
1344 EXPECT_CALL(adaptor, EmitUint16Changed(flimflam::kWifiFrequency, _)).Times(0);
mukesh agrawal923f14f2012-06-04 16:46:08 -07001345 EXPECT_CALL(adaptor, EmitStringChanged(flimflam::kWifiBSsid, _)).Times(0);
mukesh agrawale1d90e92012-02-15 17:36:08 -08001346 EXPECT_CALL(adaptor,
1347 EmitUint8Changed(flimflam::kSignalStrengthProperty, _)).Times(0);
1348 service->NotifyCurrentEndpoint(NULL);
1349 Mock::VerifyAndClearExpectations(&adaptor);
1350}
1351
1352TEST_F(WiFiServiceUpdateFromEndpointsTest, EndpointModified) {
1353 EXPECT_CALL(adaptor, EmitUint16Changed(_, _)).Times(AnyNumber());
mukesh agrawal923f14f2012-06-04 16:46:08 -07001354 EXPECT_CALL(adaptor, EmitStringChanged(_, _)).Times(AnyNumber());
mukesh agrawale1d90e92012-02-15 17:36:08 -08001355 EXPECT_CALL(adaptor, EmitUint8Changed(_, _)).Times(AnyNumber());
1356 EXPECT_CALL(adaptor, EmitBoolChanged(_, _)).Times(AnyNumber());
1357 service->AddEndpoint(ok_endpoint);
1358 service->AddEndpoint(good_endpoint);
Darin Petkov4a66cc52012-06-15 10:08:29 +02001359 EXPECT_EQ(2, service->GetEndpointCount());
mukesh agrawale1d90e92012-02-15 17:36:08 -08001360 Mock::VerifyAndClearExpectations(&adaptor);
1361
1362 // Updating sub-optimal Endpoint doesn't update Service.
1363 EXPECT_CALL(adaptor, EmitUint16Changed(flimflam::kWifiFrequency, _)).Times(0);
mukesh agrawal923f14f2012-06-04 16:46:08 -07001364 EXPECT_CALL(adaptor, EmitStringChanged(flimflam::kWifiBSsid, _)).Times(0);
mukesh agrawale1d90e92012-02-15 17:36:08 -08001365 EXPECT_CALL(adaptor,
1366 EmitUint8Changed(flimflam::kSignalStrengthProperty, _)).Times(0);
1367 ok_endpoint->signal_strength_ = (kOkEndpointSignal + kGoodEndpointSignal) / 2;
Paul Stewart3c504012013-01-17 17:49:58 -08001368 service->NotifyEndpointUpdated(ok_endpoint);
mukesh agrawale1d90e92012-02-15 17:36:08 -08001369 Mock::VerifyAndClearExpectations(&adaptor);
1370
1371 // Updating optimal Endpoint updates appropriate Service property.
1372 EXPECT_CALL(adaptor, EmitUint16Changed(flimflam::kWifiFrequency, _)).Times(0);
mukesh agrawal923f14f2012-06-04 16:46:08 -07001373 EXPECT_CALL(adaptor, EmitStringChanged(flimflam::kWifiBSsid, _)).Times(0);
mukesh agrawale1d90e92012-02-15 17:36:08 -08001374 EXPECT_CALL(adaptor, EmitUint8Changed(flimflam::kSignalStrengthProperty, _));
1375 good_endpoint->signal_strength_ = kGoodEndpointSignal + 1;
Paul Stewart3c504012013-01-17 17:49:58 -08001376 service->NotifyEndpointUpdated(good_endpoint);
mukesh agrawale1d90e92012-02-15 17:36:08 -08001377 Mock::VerifyAndClearExpectations(&adaptor);
1378
1379 // Change in optimal Endpoint updates Service properties.
1380 EXPECT_CALL(adaptor, EmitUint16Changed(
1381 flimflam::kWifiFrequency, kOkEndpointFrequency));
mukesh agrawal923f14f2012-06-04 16:46:08 -07001382 EXPECT_CALL(adaptor, EmitStringChanged(
1383 flimflam::kWifiBSsid, kOkEndpointBssId));
mukesh agrawale1d90e92012-02-15 17:36:08 -08001384 EXPECT_CALL(adaptor, EmitUint8Changed(flimflam::kSignalStrengthProperty, _));
1385 ok_endpoint->signal_strength_ = kGoodEndpointSignal + 2;
Paul Stewart3c504012013-01-17 17:49:58 -08001386 service->NotifyEndpointUpdated(ok_endpoint);
mukesh agrawale1d90e92012-02-15 17:36:08 -08001387 Mock::VerifyAndClearExpectations(&adaptor);
1388}
1389
Paul Stewarta5e7d5f2013-01-09 18:06:15 -08001390TEST_F(WiFiServiceUpdateFromEndpointsTest, Ieee80211w) {
1391 EXPECT_CALL(adaptor, EmitUint16Changed(_, _)).Times(AnyNumber());
1392 EXPECT_CALL(adaptor, EmitStringChanged(_, _)).Times(AnyNumber());
1393 EXPECT_CALL(adaptor, EmitUint8Changed(_, _)).Times(AnyNumber());
1394 EXPECT_CALL(adaptor, EmitBoolChanged(_, _)).Times(AnyNumber());
1395 service->AddEndpoint(ok_endpoint);
1396 EXPECT_FALSE(service->ieee80211w_required());
1397 good_endpoint->ieee80211w_required_ = true;
1398 service->AddEndpoint(good_endpoint);
1399 EXPECT_TRUE(service->ieee80211w_required());
1400 service->RemoveEndpoint(good_endpoint);
1401 EXPECT_TRUE(service->ieee80211w_required());
1402}
1403
mukesh agrawalf6b32092013-04-10 15:49:55 -07001404TEST_F(WiFiServiceUpdateFromEndpointsTest, PhysicalMode) {
1405 EXPECT_CALL(adaptor, EmitUint16Changed(_, _)).Times(AnyNumber());
1406 EXPECT_CALL(adaptor, EmitStringChanged(_, _)).Times(AnyNumber());
1407 EXPECT_CALL(adaptor, EmitUint8Changed(_, _)).Times(AnyNumber());
1408 EXPECT_CALL(adaptor, EmitBoolChanged(_, _)).Times(AnyNumber());
1409
1410 // No endpoints -> undef.
1411 EXPECT_EQ(Metrics::kWiFiNetworkPhyModeUndef, service->physical_mode());
1412
1413 // Endpoint has unknown physical mode -> undef.
1414 ok_endpoint->physical_mode_ = Metrics::kWiFiNetworkPhyModeUndef;
1415 service->AddEndpoint(ok_endpoint);
1416 EXPECT_EQ(Metrics::kWiFiNetworkPhyModeUndef, service->physical_mode());
1417
1418 // New endpoint with 802.11a -> 802.11a.
1419 good_endpoint->physical_mode_ = Metrics::kWiFiNetworkPhyMode11a;
1420 service->AddEndpoint(good_endpoint);
1421 EXPECT_EQ(Metrics::kWiFiNetworkPhyMode11a, service->physical_mode());
1422
1423 // Remove 802.11a endpoint -> undef.
1424 service->RemoveEndpoint(good_endpoint);
1425 EXPECT_EQ(Metrics::kWiFiNetworkPhyModeUndef, service->physical_mode());
1426
1427 // Change endpoint -> take endpoint's new value.
1428 ok_endpoint->physical_mode_ = Metrics::kWiFiNetworkPhyMode11n;
1429 service->NotifyEndpointUpdated(ok_endpoint);
1430 EXPECT_EQ(Metrics::kWiFiNetworkPhyMode11n, service->physical_mode());
1431
1432 // No endpoints -> undef.
1433 service->RemoveEndpoint(ok_endpoint);
1434 EXPECT_EQ(Metrics::kWiFiNetworkPhyModeUndef, service->physical_mode());
1435}
1436
Paul Stewart8653f462013-02-06 12:21:05 -08001437TEST_F(WiFiServiceUpdateFromEndpointsTest, WarningOnDisconnect) {
1438 service->AddEndpoint(ok_endpoint);
1439 service->SetState(Service::kStateAssociating);
1440 ScopedMockLog log;
1441 EXPECT_CALL(log, Log(_, _, _)).Times(AnyNumber());
1442 EXPECT_CALL(log, Log(logging::LOG_WARNING, _,
1443 EndsWith("disconnect due to no remaining endpoints.")));
1444 service->RemoveEndpoint(ok_endpoint);
1445}
1446
Paul Stewart6df20bd2013-03-13 19:31:25 -07001447TEST_F(WiFiServiceTest, SecurityFromCurrentEndpoint) {
1448 WiFiServiceRefPtr service(MakeSimpleService(flimflam::kSecurityPsk));
1449 EXPECT_EQ(flimflam::kSecurityPsk, service->GetSecurity(NULL));
1450 WiFiEndpoint *endpoint = MakeOpenEndpoint(
1451 simple_ssid_string(), "00:00:00:00:00:00", 0, 0);
1452 service->AddEndpoint(endpoint);
1453 EXPECT_EQ(flimflam::kSecurityPsk, service->GetSecurity(NULL));
1454 service->NotifyCurrentEndpoint(endpoint);
1455 EXPECT_EQ(flimflam::kSecurityNone, service->GetSecurity(NULL));
1456 service->NotifyCurrentEndpoint(NULL);
1457 EXPECT_EQ(flimflam::kSecurityPsk, service->GetSecurity(NULL));
1458}
1459
mukesh agrawal43970a22013-02-15 16:00:07 -08001460TEST_F(WiFiServiceTest, UpdateSecurity) {
1461 // Cleartext and pre-shared-key crypto.
1462 {
1463 WiFiServiceRefPtr service = MakeSimpleService(flimflam::kSecurityNone);
1464 EXPECT_EQ(Service::kCryptoNone, service->crypto_algorithm());
1465 EXPECT_FALSE(service->key_rotation());
1466 EXPECT_FALSE(service->endpoint_auth());
1467 }
1468 {
1469 WiFiServiceRefPtr service = MakeSimpleService(flimflam::kSecurityWep);
1470 EXPECT_EQ(Service::kCryptoRc4, service->crypto_algorithm());
1471 EXPECT_FALSE(service->key_rotation());
1472 EXPECT_FALSE(service->endpoint_auth());
1473 }
1474 {
1475 WiFiServiceRefPtr service = MakeSimpleService(flimflam::kSecurityPsk);
1476 EXPECT_EQ(Service::kCryptoRc4, service->crypto_algorithm());
1477 EXPECT_TRUE(service->key_rotation());
1478 EXPECT_FALSE(service->endpoint_auth());
1479 }
1480 {
1481 WiFiServiceRefPtr service = MakeSimpleService(flimflam::kSecurityWpa);
1482 EXPECT_EQ(Service::kCryptoRc4, service->crypto_algorithm());
1483 EXPECT_TRUE(service->key_rotation());
1484 EXPECT_FALSE(service->endpoint_auth());
1485 }
1486 {
1487 WiFiServiceRefPtr service = MakeSimpleService(flimflam::kSecurityRsn);
1488 EXPECT_EQ(Service::kCryptoAes, service->crypto_algorithm());
1489 EXPECT_TRUE(service->key_rotation());
1490 EXPECT_FALSE(service->endpoint_auth());
1491 }
1492
1493 // Crypto with 802.1X key management.
1494 {
1495 // WEP
1496 WiFiServiceRefPtr service = MakeSimpleService(flimflam::kSecurityWep);
1497 service->SetEAPKeyManagement("IEEE8021X");
1498 EXPECT_EQ(Service::kCryptoRc4, service->crypto_algorithm());
1499 EXPECT_TRUE(service->key_rotation());
1500 EXPECT_TRUE(service->endpoint_auth());
1501 }
1502 {
1503 // WPA
1504 WiFiServiceRefPtr service = MakeSimpleService(flimflam::kSecurity8021x);
1505 WiFiEndpointRefPtr endpoint =
1506 MakeEndpoint("a", "00:00:00:00:00:01", 0, 0, true, false);
1507 service->AddEndpoint(endpoint);
1508 EXPECT_EQ(Service::kCryptoRc4, service->crypto_algorithm());
1509 EXPECT_TRUE(service->key_rotation());
1510 EXPECT_TRUE(service->endpoint_auth());
1511 }
1512 {
1513 // RSN
1514 WiFiServiceRefPtr service = MakeSimpleService(flimflam::kSecurity8021x);
1515 WiFiEndpointRefPtr endpoint =
1516 MakeEndpoint("a", "00:00:00:00:00:01", 0, 0, false, true);
1517 service->AddEndpoint(endpoint);
1518 EXPECT_EQ(Service::kCryptoAes, service->crypto_algorithm());
1519 EXPECT_TRUE(service->key_rotation());
1520 EXPECT_TRUE(service->endpoint_auth());
1521 }
1522 {
1523 // AP supports both WPA and RSN.
1524 WiFiServiceRefPtr service = MakeSimpleService(flimflam::kSecurity8021x);
1525 WiFiEndpointRefPtr endpoint =
1526 MakeEndpoint("a", "00:00:00:00:00:01", 0, 0, true, true);
1527 service->AddEndpoint(endpoint);
1528 EXPECT_EQ(Service::kCryptoAes, service->crypto_algorithm());
1529 EXPECT_TRUE(service->key_rotation());
1530 EXPECT_TRUE(service->endpoint_auth());
1531 }
1532}
1533
1534TEST_F(WiFiServiceTest, ComputeCipher8021x) {
1535 // No endpoints.
1536 {
1537 const set<WiFiEndpointConstRefPtr> endpoints;
1538 EXPECT_EQ(Service::kCryptoNone,
1539 WiFiService::ComputeCipher8021x(endpoints));
1540 }
1541
1542 // Single endpoint, various configs.
1543 {
1544 set<WiFiEndpointConstRefPtr> endpoints;
1545 endpoints.insert(
1546 MakeEndpoint("a", "00:00:00:00:00:01", 0, 0, false, false));
1547 EXPECT_EQ(Service::kCryptoNone,
1548 WiFiService::ComputeCipher8021x(endpoints));
1549 }
1550 {
1551 set<WiFiEndpointConstRefPtr> endpoints;
1552 endpoints.insert(
1553 MakeEndpoint("a", "00:00:00:00:00:01", 0, 0, true, false));
1554 EXPECT_EQ(Service::kCryptoRc4,
1555 WiFiService::ComputeCipher8021x(endpoints));
1556 }
1557 {
1558 set<WiFiEndpointConstRefPtr> endpoints;
1559 endpoints.insert(
1560 MakeEndpoint("a", "00:00:00:00:00:01", 0, 0, false, true));
1561 EXPECT_EQ(Service::kCryptoAes,
1562 WiFiService::ComputeCipher8021x(endpoints));
1563 }
1564 {
1565 set<WiFiEndpointConstRefPtr> endpoints;
1566 endpoints.insert(
1567 MakeEndpoint("a", "00:00:00:00:00:01", 0, 0, true, true));
1568 EXPECT_EQ(Service::kCryptoAes,
1569 WiFiService::ComputeCipher8021x(endpoints));
1570 }
1571
1572 // Multiple endpoints.
1573 {
1574 set<WiFiEndpointConstRefPtr> endpoints;
1575 endpoints.insert(
1576 MakeEndpoint("a", "00:00:00:00:00:01", 0, 0, false, false));
1577 endpoints.insert(
1578 MakeEndpoint("a", "00:00:00:00:00:02", 0, 0, false, false));
1579 EXPECT_EQ(Service::kCryptoNone,
1580 WiFiService::ComputeCipher8021x(endpoints));
1581 }
1582 {
1583 set<WiFiEndpointConstRefPtr> endpoints;
1584 endpoints.insert(
1585 MakeEndpoint("a", "00:00:00:00:00:01", 0, 0, false, false));
1586 endpoints.insert(
1587 MakeEndpoint("a", "00:00:00:00:00:02", 0, 0, true, false));
1588 EXPECT_EQ(Service::kCryptoNone,
1589 WiFiService::ComputeCipher8021x(endpoints));
1590 }
1591 {
1592 set<WiFiEndpointConstRefPtr> endpoints;
1593 endpoints.insert(
1594 MakeEndpoint("a", "00:00:00:00:00:01", 0, 0, true, false));
1595 endpoints.insert(
1596 MakeEndpoint("a", "00:00:00:00:00:02", 0, 0, true, false));
1597 EXPECT_EQ(Service::kCryptoRc4,
1598 WiFiService::ComputeCipher8021x(endpoints));
1599 }
1600 {
1601 set<WiFiEndpointConstRefPtr> endpoints;
1602 endpoints.insert(
1603 MakeEndpoint("a", "00:00:00:00:00:01", 0, 0, true, false));
1604 endpoints.insert(
1605 MakeEndpoint("a", "00:00:00:00:00:02", 0, 0, false, true));
1606 EXPECT_EQ(Service::kCryptoRc4,
1607 WiFiService::ComputeCipher8021x(endpoints));
1608 }
1609 {
1610 set<WiFiEndpointConstRefPtr> endpoints;
1611 endpoints.insert(
1612 MakeEndpoint("a", "00:00:00:00:00:01", 0, 0, false, true));
1613 endpoints.insert(
1614 MakeEndpoint("a", "00:00:00:00:00:02", 0, 0, false, true));
1615 EXPECT_EQ(Service::kCryptoAes,
1616 WiFiService::ComputeCipher8021x(endpoints));
1617 }
1618 {
1619 set<WiFiEndpointConstRefPtr> endpoints;
1620 endpoints.insert(
1621 MakeEndpoint("a", "00:00:00:00:00:01", 0, 0, true, true));
1622 endpoints.insert(
1623 MakeEndpoint("a", "00:00:00:00:00:02", 0, 0, true, true));
1624 EXPECT_EQ(Service::kCryptoAes,
1625 WiFiService::ComputeCipher8021x(endpoints));
1626 }
1627}
1628
Albert Chaulk0e1cdea2013-02-27 15:32:55 -08001629TEST_F(WiFiServiceTest, Unload) {
1630 WiFiServiceRefPtr service = MakeServiceWithWiFi(flimflam::kSecurityNone);
1631 EXPECT_CALL(*wifi(), DestroyIPConfigLease(service->GetStorageIdentifier())).
1632 Times(1);
1633 service->Unload();
1634}
1635
1636
Chris Masone34af2182011-08-22 11:59:36 -07001637} // namespace shill