blob: 16499e997d50e2b58c7adc36d648ca6dfdee2d71 [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"
21#include "shill/mock_adaptors.h"
22#include "shill/mock_control.h"
Paul Stewart71a4d3b2013-01-18 18:12:56 -080023#include "shill/mock_log.h"
Paul Stewartecf4cd12012-04-17 11:08:39 -070024#include "shill/mock_nss.h"
Christopher Wiley1ce658d2012-10-10 10:02:03 -070025#include "shill/mock_profile.h"
Chris Masone34af2182011-08-22 11:59:36 -070026#include "shill/mock_service.h"
27#include "shill/mock_store.h"
mukesh agrawal6e277772011-09-29 15:04:23 -070028#include "shill/mock_wifi.h"
Paul Stewart3c504012013-01-17 17:49:58 -080029#include "shill/mock_wifi_provider.h"
Chris Masone34af2182011-08-22 11:59:36 -070030#include "shill/property_store_unittest.h"
mukesh agrawal8a3188d2011-12-01 20:56:44 +000031#include "shill/refptr_types.h"
32#include "shill/wifi_endpoint.h"
mukesh agrawal6e277772011-09-29 15:04:23 -070033#include "shill/wpa_supplicant.h"
Chris Masone34af2182011-08-22 11:59:36 -070034
Albert Chaulk0e1cdea2013-02-27 15:32:55 -080035using base::FilePath;
mukesh agrawald835b202011-10-07 15:26:47 -070036using std::map;
Paul Stewart85aea152013-01-22 09:31:56 -080037using std::set;
Chris Masone34af2182011-08-22 11:59:36 -070038using std::string;
39using std::vector;
Paul Stewartd08f4432011-11-04 07:48:20 -070040using ::testing::_;
mukesh agrawale1d90e92012-02-15 17:36:08 -080041using ::testing::AnyNumber;
Paul Stewartd08f4432011-11-04 07:48:20 -070042using ::testing::DoAll;
Paul Stewart71a4d3b2013-01-18 18:12:56 -080043using ::testing::EndsWith;
Paul Stewart3c504012013-01-17 17:49:58 -080044using ::testing::HasSubstr;
mukesh agrawale1d90e92012-02-15 17:36:08 -080045using ::testing::Mock;
mukesh agrawal6e277772011-09-29 15:04:23 -070046using ::testing::NiceMock;
Paul Stewartd08f4432011-11-04 07:48:20 -070047using ::testing::Return;
48using ::testing::SetArgumentPointee;
49using ::testing::StrEq;
Paul Stewartd8ad3c42012-01-09 12:39:38 -080050using ::testing::StrNe;
Paul Stewart85aea152013-01-22 09:31:56 -080051using ::testing::StrictMock;
Chris Masone34af2182011-08-22 11:59:36 -070052
mukesh agrawalb20776f2012-02-10 16:00:36 -080053namespace shill {
54
Chris Masone34af2182011-08-22 11:59:36 -070055class WiFiServiceTest : public PropertyStoreTest {
56 public:
Paul Stewart3c504012013-01-17 17:49:58 -080057 WiFiServiceTest()
58 : wifi_(new NiceMock<MockWiFi>(
59 control_interface(),
60 dispatcher(),
61 metrics(),
62 manager(),
63 "wifi",
64 fake_mac,
65 0)),
66 simple_ssid_(1, 'a'),
67 simple_ssid_string_("a") {}
Chris Masone34af2182011-08-22 11:59:36 -070068 virtual ~WiFiServiceTest() {}
mukesh agrawal6e277772011-09-29 15:04:23 -070069
70 protected:
71 static const char fake_mac[];
mukesh agrawale1d90e92012-02-15 17:36:08 -080072
Gaurav Shah10109f22011-11-11 20:16:22 -080073 bool CheckConnectable(const std::string &security, const char *passphrase,
74 Service::EapCredentials *eap) {
mukesh agrawal29c13a12011-11-24 00:09:19 +000075 Error error;
Paul Stewart3c504012013-01-17 17:49:58 -080076 WiFiServiceRefPtr service = MakeSimpleService(security);
mukesh agrawal29c13a12011-11-24 00:09:19 +000077 if (passphrase)
78 service->SetPassphrase(passphrase, &error);
Gaurav Shah10109f22011-11-11 20:16:22 -080079 if (eap) {
80 service->set_eap(*eap);
81 }
mukesh agrawal29c13a12011-11-24 00:09:19 +000082 return service->connectable();
83 }
mukesh agrawale1d90e92012-02-15 17:36:08 -080084 WiFiEndpoint *MakeEndpoint(const string &ssid, const string &bssid,
mukesh agrawal43970a22013-02-15 16:00:07 -080085 uint16 frequency, int16 signal_dbm,
86 bool has_wpa_property, bool has_rsn_property) {
87 return WiFiEndpoint::MakeEndpoint(
88 NULL, wifi(), ssid, bssid, wpa_supplicant::kNetworkModeInfrastructure,
89 frequency, signal_dbm, has_wpa_property, has_rsn_property);
90 }
91 WiFiEndpoint *MakeOpenEndpoint(const string &ssid, const string &bssid,
92 uint16 frequency, int16 signal_dbm) {
mukesh agrawale1d90e92012-02-15 17:36:08 -080093 return WiFiEndpoint::MakeOpenEndpoint(
Paul Stewart3c504012013-01-17 17:49:58 -080094 NULL, wifi(), ssid, bssid, wpa_supplicant::kNetworkModeInfrastructure,
95 frequency, signal_dbm);
mukesh agrawale1d90e92012-02-15 17:36:08 -080096 }
Paul Stewart3c504012013-01-17 17:49:58 -080097 WiFiServiceRefPtr MakeSimpleService(const string &security) {
mukesh agrawale1d90e92012-02-15 17:36:08 -080098 return new WiFiService(control_interface(),
99 dispatcher(),
100 metrics(),
101 manager(),
Paul Stewart3c504012013-01-17 17:49:58 -0800102 &provider_,
103 simple_ssid_,
mukesh agrawale1d90e92012-02-15 17:36:08 -0800104 flimflam::kModeManaged,
Paul Stewart3c504012013-01-17 17:49:58 -0800105 security,
mukesh agrawale1d90e92012-02-15 17:36:08 -0800106 false);
107 }
Paul Stewart3c504012013-01-17 17:49:58 -0800108 WiFiServiceRefPtr MakeGenericService() {
109 return MakeSimpleService(flimflam::kSecurityWep);
110 }
111 void SetWiFiForService(WiFiServiceRefPtr service, WiFiRefPtr wifi) {
112 service->wifi_ = wifi;
113 }
114 WiFiServiceRefPtr MakeServiceWithWiFi(const string &security) {
115 WiFiServiceRefPtr service = MakeSimpleService(security);
116 SetWiFiForService(service, wifi_);
117 return service;
118 }
mukesh agrawale1d90e92012-02-15 17:36:08 -0800119 ServiceMockAdaptor *GetAdaptor(WiFiService *service) {
120 return dynamic_cast<ServiceMockAdaptor *>(service->adaptor());
mukesh agrawal8a3188d2011-12-01 20:56:44 +0000121 }
Paul Stewart3c504012013-01-17 17:49:58 -0800122 Error::Type TestConfigurePassphrase(const string &security,
123 const char *passphrase) {
124 WiFiServiceRefPtr service = MakeSimpleService(security);
125 KeyValueStore args;
126 if (passphrase) {
127 args.SetString(flimflam::kPassphraseProperty, passphrase);
128 }
129 Error error;
130 service->Configure(args, &error);
131 return error.type();
132 }
mukesh agrawal6e277772011-09-29 15:04:23 -0700133 scoped_refptr<MockWiFi> wifi() { return wifi_; }
Paul Stewart3c504012013-01-17 17:49:58 -0800134 MockWiFiProvider *provider() { return &provider_; }
135 string GetAnyDeviceAddress() { return WiFiService::kAnyDeviceAddress; }
136 const vector<uint8_t> &simple_ssid() { return simple_ssid_; }
137 const string &simple_ssid_string() { return simple_ssid_string_; }
mukesh agrawal6e277772011-09-29 15:04:23 -0700138
139 private:
140 scoped_refptr<MockWiFi> wifi_;
Paul Stewart3c504012013-01-17 17:49:58 -0800141 MockWiFiProvider provider_;
142 const vector<uint8_t> simple_ssid_;
143 const string simple_ssid_string_;
Chris Masone34af2182011-08-22 11:59:36 -0700144};
145
mukesh agrawal6e277772011-09-29 15:04:23 -0700146// static
147const char WiFiServiceTest::fake_mac[] = "AaBBcCDDeeFF";
148
Paul Stewart71a4d3b2013-01-18 18:12:56 -0800149MATCHER_P3(ContainsWiFiProperties, ssid, mode, security, "") {
150 string hex_ssid = base::HexEncode(ssid.data(), ssid.size());
151 return
152 arg.ContainsString(WiFiService::kStorageType) &&
153 arg.GetString(WiFiService::kStorageType) == flimflam::kTypeWifi &&
154 arg.ContainsString(WiFiService::kStorageSSID) &&
155 arg.GetString(WiFiService::kStorageSSID) == hex_ssid &&
156 arg.ContainsString(WiFiService::kStorageMode) &&
157 arg.GetString(WiFiService::kStorageMode) == mode &&
158 arg.ContainsString(WiFiService::kStorageSecurityClass) &&
159 arg.GetString(WiFiService::kStorageSecurityClass) == security;
160}
161
Paul Stewartd08f4432011-11-04 07:48:20 -0700162class WiFiServiceSecurityTest : public WiFiServiceTest {
163 public:
Paul Stewartd08f4432011-11-04 07:48:20 -0700164 bool TestStorageSecurityIs(WiFiServiceRefPtr wifi_service,
165 const string &security) {
166 string id = wifi_service->GetStorageIdentifier();
Paul Stewart3c504012013-01-17 17:49:58 -0800167 size_t mac_pos = id.find(StringToLowerASCII(GetAnyDeviceAddress()));
Paul Stewartd08f4432011-11-04 07:48:20 -0700168 EXPECT_NE(mac_pos, string::npos);
169 size_t mode_pos = id.find(string(flimflam::kModeManaged), mac_pos);
170 EXPECT_NE(mode_pos, string::npos);
171 return id.find(string(security), mode_pos) != string::npos;
172 }
173
174 // Test that a service that is created with security |from_security|
175 // gets by default a storage identifier with |to_security| as its
Paul Stewart71a4d3b2013-01-18 18:12:56 -0800176 // security component, and that when saved, it sets the Security
177 // property in to |to_security| as well.
Paul Stewartd08f4432011-11-04 07:48:20 -0700178 bool TestStorageMapping(const string &from_security,
179 const string &to_security) {
Paul Stewart3c504012013-01-17 17:49:58 -0800180 WiFiServiceRefPtr wifi_service = MakeSimpleService(from_security);
Paul Stewart71a4d3b2013-01-18 18:12:56 -0800181 NiceMock<MockStore> mock_store;
182 EXPECT_CALL(mock_store, SetString(_, _, _)).WillRepeatedly(Return(true));
183 EXPECT_CALL(mock_store,
184 SetString(_, WiFiService::kStorageSecurity, from_security))
185 .Times(1);
186 EXPECT_CALL(mock_store,
187 SetString(_, WiFiService::kStorageSecurityClass, to_security))
188 .Times(1);
189 wifi_service->Save(&mock_store);
Paul Stewartd08f4432011-11-04 07:48:20 -0700190 return TestStorageSecurityIs(wifi_service, to_security);
191 }
192
193 // Test whether a service of type |service_security| can load from a
194 // storage interface containing an entry for |storage_security|.
195 // Make sure the result meets |expectation|. If |expectation| is
196 // true, also make sure the service storage identifier changes to
197 // match |storage_security|.
198 bool TestLoadMapping(const string &service_security,
199 const string &storage_security,
200 bool expectation) {
Paul Stewart3c504012013-01-17 17:49:58 -0800201 WiFiServiceRefPtr wifi_service = MakeSimpleService(service_security);
Paul Stewartd08f4432011-11-04 07:48:20 -0700202 NiceMock<MockStore> mock_store;
Paul Stewart71a4d3b2013-01-18 18:12:56 -0800203 EXPECT_CALL(mock_store, GetGroupsWithProperties(_))
204 .WillRepeatedly(Return(set<string>()));
205 const string kStorageId = "storage_id";
206 EXPECT_CALL(mock_store, ContainsGroup(kStorageId))
Paul Stewartd08f4432011-11-04 07:48:20 -0700207 .WillRepeatedly(Return(true));
Paul Stewart71a4d3b2013-01-18 18:12:56 -0800208 set<string> groups;
209 groups.insert(kStorageId);
210 EXPECT_CALL(mock_store, GetGroupsWithProperties(
211 ContainsWiFiProperties(wifi_service->ssid(),
212 flimflam::kModeManaged,
213 storage_security)))
214 .WillRepeatedly(Return(groups));
Paul Stewartd08f4432011-11-04 07:48:20 -0700215 bool is_loadable = wifi_service->IsLoadableFrom(&mock_store);
216 EXPECT_EQ(expectation, is_loadable);
217 bool is_loaded = wifi_service->Load(&mock_store);
218 EXPECT_EQ(expectation, is_loaded);
219
220 if (expectation != is_loadable || expectation != is_loaded) {
221 return false;
222 } else if (!expectation) {
223 return true;
224 } else {
Paul Stewart71a4d3b2013-01-18 18:12:56 -0800225 return wifi_service->GetStorageIdentifier() == kStorageId;
Paul Stewartd08f4432011-11-04 07:48:20 -0700226 }
227 }
228};
229
mukesh agrawale1d90e92012-02-15 17:36:08 -0800230class WiFiServiceUpdateFromEndpointsTest : public WiFiServiceTest {
231 public:
232 WiFiServiceUpdateFromEndpointsTest()
233 : kOkEndpointStrength(WiFiService::SignalToStrength(kOkEndpointSignal)),
234 kBadEndpointStrength(WiFiService::SignalToStrength(kBadEndpointSignal)),
235 kGoodEndpointStrength(
236 WiFiService::SignalToStrength(kGoodEndpointSignal)),
237 service(MakeGenericService()),
238 adaptor(*GetAdaptor(service)) {
mukesh agrawal43970a22013-02-15 16:00:07 -0800239 ok_endpoint = MakeOpenEndpoint(
Paul Stewart3c504012013-01-17 17:49:58 -0800240 simple_ssid_string(), kOkEndpointBssId, kOkEndpointFrequency,
241 kOkEndpointSignal);
mukesh agrawal43970a22013-02-15 16:00:07 -0800242 good_endpoint = MakeOpenEndpoint(
Paul Stewart3c504012013-01-17 17:49:58 -0800243 simple_ssid_string(), kGoodEndpointBssId, kGoodEndpointFrequency,
244 kGoodEndpointSignal);
mukesh agrawal43970a22013-02-15 16:00:07 -0800245 bad_endpoint = MakeOpenEndpoint(
Paul Stewart3c504012013-01-17 17:49:58 -0800246 simple_ssid_string(), kBadEndpointBssId, kBadEndpointFrequency,
247 kBadEndpointSignal);
mukesh agrawale1d90e92012-02-15 17:36:08 -0800248 }
249
250 protected:
251 static const uint16 kOkEndpointFrequency = 2422;
252 static const uint16 kBadEndpointFrequency = 2417;
253 static const uint16 kGoodEndpointFrequency = 2412;
254 static const int16 kOkEndpointSignal = -50;
255 static const int16 kBadEndpointSignal = -75;
256 static const int16 kGoodEndpointSignal = -25;
mukesh agrawal923f14f2012-06-04 16:46:08 -0700257 static const char *kOkEndpointBssId;
258 static const char *kGoodEndpointBssId;
259 static const char *kBadEndpointBssId;
mukesh agrawale1d90e92012-02-15 17:36:08 -0800260 // Can't be both static and const (because initialization requires a
261 // function call). So choose to be just const.
262 const uint8 kOkEndpointStrength;
263 const uint8 kBadEndpointStrength;
264 const uint8 kGoodEndpointStrength;
265 WiFiEndpointRefPtr ok_endpoint;
266 WiFiEndpointRefPtr bad_endpoint;
267 WiFiEndpointRefPtr good_endpoint;
268 WiFiServiceRefPtr service;
269 ServiceMockAdaptor &adaptor;
270};
271
mukesh agrawal923f14f2012-06-04 16:46:08 -0700272const char *WiFiServiceUpdateFromEndpointsTest::kOkEndpointBssId =
273 "00:00:00:00:00:01";
274const char *WiFiServiceUpdateFromEndpointsTest::kGoodEndpointBssId =
275 "00:00:00:00:00:02";
276const char *WiFiServiceUpdateFromEndpointsTest::kBadEndpointBssId =
277 "00:00:00:00:00:03";
mukesh agrawale1d90e92012-02-15 17:36:08 -0800278
Paul Stewart85aea152013-01-22 09:31:56 -0800279class WiFiServiceFixupStorageTest : public WiFiServiceTest {
280 protected:
281 void AddGroup(string group_name) {
282 groups_.insert(group_name);
283 }
284
Paul Stewart71a4d3b2013-01-18 18:12:56 -0800285 void AddServiceEntry(bool has_type, bool has_mode, bool has_security,
286 bool has_security_class) {
Paul Stewart85aea152013-01-22 09:31:56 -0800287 int index = groups_.size();
288 string id = base::StringPrintf("%s_%d_%d_%s_%s", flimflam::kTypeWifi,
289 index, index, flimflam::kModeManaged,
Paul Stewart71a4d3b2013-01-18 18:12:56 -0800290 flimflam::kSecurityWpa);
Paul Stewart85aea152013-01-22 09:31:56 -0800291 AddGroup(id);
292 EXPECT_CALL(store_, GetString(id, WiFiService::kStorageType, _))
293 .WillOnce(Return(has_type));
294 if (!has_type) {
295 EXPECT_CALL(store_, SetString(id, WiFiService::kStorageType,
296 flimflam::kTypeWifi));
297 }
298 EXPECT_CALL(store_, GetString(id, WiFiService::kStorageMode, _))
299 .WillOnce(Return(has_mode));
300 if (!has_mode) {
301 EXPECT_CALL(store_, SetString(id, WiFiService::kStorageMode,
302 flimflam::kModeManaged));
303 }
304 EXPECT_CALL(store_, GetString(id, WiFiService::kStorageSecurity, _))
305 .WillOnce(Return(has_security));
306 if (!has_security) {
307 EXPECT_CALL(store_, SetString(id, WiFiService::kStorageSecurity,
Paul Stewart71a4d3b2013-01-18 18:12:56 -0800308 flimflam::kSecurityWpa));
309 }
310 EXPECT_CALL(store_, GetString(id, WiFiService::kStorageSecurityClass, _))
311 .WillOnce(Return(has_security_class));
312 if (!has_security_class) {
313 EXPECT_CALL(store_, SetString(id, WiFiService::kStorageSecurityClass,
314 flimflam::kSecurityPsk));
Paul Stewart85aea152013-01-22 09:31:56 -0800315 }
316 }
317
318 bool FixupServiceEntries() {
319 EXPECT_CALL(store_, GetGroups()).WillOnce(Return(groups_));
320 return WiFiService::FixupServiceEntries(&store_);
321 }
322
323 private:
324 StrictMock<MockStore> store_;
325 set<string> groups_;
326};
327
Chris Masone34af2182011-08-22 11:59:36 -0700328TEST_F(WiFiServiceTest, StorageId) {
Paul Stewart3c504012013-01-17 17:49:58 -0800329 WiFiServiceRefPtr wifi_service = MakeSimpleService(flimflam::kSecurityNone);
Chris Masone9d779932011-08-25 16:33:41 -0700330 string id = wifi_service->GetStorageIdentifier();
Chris Masone34af2182011-08-22 11:59:36 -0700331 for (uint i = 0; i < id.length(); ++i) {
332 EXPECT_TRUE(id[i] == '_' ||
333 isxdigit(id[i]) ||
334 (isalpha(id[i]) && islower(id[i])));
335 }
Paul Stewart3c504012013-01-17 17:49:58 -0800336 size_t mac_pos = id.find(StringToLowerASCII(GetAnyDeviceAddress()));
Chris Masone34af2182011-08-22 11:59:36 -0700337 EXPECT_NE(mac_pos, string::npos);
338 EXPECT_NE(id.find(string(flimflam::kModeManaged), mac_pos), string::npos);
339}
340
Gaurav Shahda6218a2011-11-11 12:09:33 -0800341// Make sure the passphrase is registered as a write only property
342// by reading and comparing all string properties returned on the store.
343TEST_F(WiFiServiceTest, PassphraseWriteOnly) {
Paul Stewart3c504012013-01-17 17:49:58 -0800344 WiFiServiceRefPtr wifi_service = MakeSimpleService(flimflam::kSecurityWpa);
Gaurav Shahda6218a2011-11-11 12:09:33 -0800345 ReadablePropertyConstIterator<string> it =
346 (wifi_service->store()).GetStringPropertiesIter();
347 for( ; !it.AtEnd(); it.Advance())
348 EXPECT_NE(it.Key(), flimflam::kPassphraseProperty);
349}
350
Thieu Lef7709452011-11-15 01:13:19 +0000351// Make sure setting the passphrase via D-Bus Service.SetProperty validates
352// the passphrase.
353TEST_F(WiFiServiceTest, PassphraseSetPropertyValidation) {
354 // We only spot check two password cases here to make sure the
355 // SetProperty code path does validation. We're not going to exhaustively
356 // test for all types of passwords.
Paul Stewart3c504012013-01-17 17:49:58 -0800357 WiFiServiceRefPtr wifi_service = MakeSimpleService(flimflam::kSecurityWep);
Thieu Lef7709452011-11-15 01:13:19 +0000358 Error error;
359 EXPECT_TRUE(wifi_service->mutable_store()->SetStringProperty(
360 flimflam::kPassphraseProperty, "0:abcde", &error));
361 EXPECT_FALSE(wifi_service->mutable_store()->SetStringProperty(
362 flimflam::kPassphraseProperty, "invalid", &error));
363 EXPECT_EQ(Error::kInvalidPassphrase, error.type());
364}
365
366TEST_F(WiFiServiceTest, PassphraseSetPropertyOpenNetwork) {
Paul Stewart3c504012013-01-17 17:49:58 -0800367 WiFiServiceRefPtr wifi_service = MakeSimpleService(flimflam::kSecurityNone);
Thieu Lef7709452011-11-15 01:13:19 +0000368 Error error;
369 EXPECT_FALSE(wifi_service->mutable_store()->SetStringProperty(
370 flimflam::kPassphraseProperty, "invalid", &error));
371 EXPECT_EQ(Error::kNotSupported, error.type());
372}
373
mukesh agrawald835b202011-10-07 15:26:47 -0700374TEST_F(WiFiServiceTest, NonUTF8SSID) {
375 vector<uint8_t> ssid;
376
377 ssid.push_back(0xff); // not a valid UTF-8 byte-sequence
378 WiFiServiceRefPtr wifi_service = new WiFiService(control_interface(),
379 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -0800380 metrics(),
mukesh agrawald835b202011-10-07 15:26:47 -0700381 manager(),
Paul Stewart3c504012013-01-17 17:49:58 -0800382 provider(),
mukesh agrawald835b202011-10-07 15:26:47 -0700383 ssid,
384 flimflam::kModeManaged,
Paul Stewartced6a0b2011-11-08 15:32:04 -0800385 flimflam::kSecurityNone,
386 false);
mukesh agrawald835b202011-10-07 15:26:47 -0700387 map<string, ::DBus::Variant> properties;
388 // if service doesn't propertly sanitize SSID, this will generate SIGABRT.
389 DBusAdaptor::GetProperties(wifi_service->store(), &properties, NULL);
390}
391
Paul Stewart4108db92013-03-11 12:13:24 -0700392MATCHER(PSKSecurityArgs, "") {
Gaurav Shahda6218a2011-11-11 12:09:33 -0800393 return ContainsKey(arg, wpa_supplicant::kPropertySecurityProtocol) &&
Paul Stewart4108db92013-03-11 12:13:24 -0700394 arg.find(wpa_supplicant::kPropertySecurityProtocol)->second.
395 reader().get_string() == string("WPA RSN") &&
Gaurav Shahda6218a2011-11-11 12:09:33 -0800396 ContainsKey(arg, wpa_supplicant::kPropertyPreSharedKey);
397}
398
Paul Stewarta5e7d5f2013-01-09 18:06:15 -0800399MATCHER(WPA80211wSecurityArgs, "") {
400 return ContainsKey(arg, wpa_supplicant::kPropertySecurityProtocol) &&
401 ContainsKey(arg, wpa_supplicant::kPropertyPreSharedKey) &&
402 ContainsKey(arg, wpa_supplicant::kNetworkPropertyIeee80211w);
403}
404
Gaurav Shah10109f22011-11-11 20:16:22 -0800405MATCHER(EAPSecurityArgs, "") {
406 return ContainsKey(arg, wpa_supplicant::kNetworkPropertyEapIdentity) &&
407 ContainsKey(arg, wpa_supplicant::kNetworkPropertyCaPath);
408}
409
Paul Stewarte2d7c502012-07-16 16:35:10 -0700410MATCHER_P(FrequencyArg, has_arg, "") {
411 return has_arg ==
412 ContainsKey(arg, wpa_supplicant::kNetworkPropertyFrequency);
413}
414
mukesh agrawalf2fd7452011-10-03 16:38:47 -0700415TEST_F(WiFiServiceTest, ConnectTaskWPA) {
Paul Stewart3c504012013-01-17 17:49:58 -0800416 WiFiServiceRefPtr wifi_service = MakeServiceWithWiFi(flimflam::kSecurityWpa);
Paul Stewart4108db92013-03-11 12:13:24 -0700417 EXPECT_CALL(*wifi(), ConnectTo(wifi_service.get(), PSKSecurityArgs()));
Wade Guthrie005bd342012-05-02 09:37:07 -0700418 Error error;
419 wifi_service->SetPassphrase("0:mumblemumblem", &error);
Eric Shienbrood9a245532012-03-07 14:20:39 -0500420 wifi_service->Connect(NULL);
mukesh agrawal6e277772011-09-29 15:04:23 -0700421}
422
mukesh agrawalf2fd7452011-10-03 16:38:47 -0700423TEST_F(WiFiServiceTest, ConnectTaskRSN) {
Paul Stewart3c504012013-01-17 17:49:58 -0800424 WiFiServiceRefPtr wifi_service = MakeServiceWithWiFi(flimflam::kSecurityRsn);
Paul Stewart4108db92013-03-11 12:13:24 -0700425 EXPECT_CALL(*wifi(), ConnectTo(wifi_service.get(), PSKSecurityArgs()));
Wade Guthrie005bd342012-05-02 09:37:07 -0700426 Error error;
427 wifi_service->SetPassphrase("0:mumblemumblem", &error);
Eric Shienbrood9a245532012-03-07 14:20:39 -0500428 wifi_service->Connect(NULL);
mukesh agrawalf2fd7452011-10-03 16:38:47 -0700429}
430
Christopher Wiley1ce658d2012-10-10 10:02:03 -0700431TEST_F(WiFiServiceTest, ConnectConditions) {
432 Error error;
Paul Stewart3c504012013-01-17 17:49:58 -0800433 WiFiServiceRefPtr wifi_service = MakeServiceWithWiFi(flimflam::kSecurityNone);
Christopher Wiley1ce658d2012-10-10 10:02:03 -0700434 scoped_refptr<MockProfile> mock_profile(
Thieu Le5133b712013-02-19 14:47:21 -0800435 new NiceMock<MockProfile>(control_interface(), metrics(), manager()));
Christopher Wiley1ce658d2012-10-10 10:02:03 -0700436 wifi_service->set_profile(mock_profile);
437 // With nothing else going on, the service should attempt to connect.
438 EXPECT_CALL(*wifi(), ConnectTo(wifi_service.get(), _));
439 wifi_service->Connect(&error);
440 Mock::VerifyAndClearExpectations(wifi());
441
442 // But if we're already "connecting" or "connected" then we shouldn't attempt
443 // again.
444 EXPECT_CALL(*wifi(),
445 ConnectTo(wifi_service.get(), _)).Times(0);
446 wifi_service->SetState(Service::kStateAssociating);
447 wifi_service->Connect(&error);
448 wifi_service->SetState(Service::kStateConfiguring);
449 wifi_service->Connect(&error);
450 wifi_service->SetState(Service::kStateConnected);
451 wifi_service->Connect(&error);
452 wifi_service->SetState(Service::kStatePortal);
453 wifi_service->Connect(&error);
454 wifi_service->SetState(Service::kStateOnline);
455 wifi_service->Connect(&error);
456 Mock::VerifyAndClearExpectations(wifi());
457}
458
Gaurav Shahf8721ee2011-11-07 09:12:46 -0800459TEST_F(WiFiServiceTest, ConnectTaskPSK) {
Paul Stewart3c504012013-01-17 17:49:58 -0800460 WiFiServiceRefPtr wifi_service = MakeServiceWithWiFi(flimflam::kSecurityPsk);
Paul Stewart4108db92013-03-11 12:13:24 -0700461 EXPECT_CALL(*wifi(), ConnectTo(wifi_service.get(), PSKSecurityArgs()));
Wade Guthrie005bd342012-05-02 09:37:07 -0700462 Error error;
463 wifi_service->SetPassphrase("0:mumblemumblem", &error);
Eric Shienbrood9a245532012-03-07 14:20:39 -0500464 wifi_service->Connect(NULL);
Gaurav Shahf8721ee2011-11-07 09:12:46 -0800465}
466
Gaurav Shah10109f22011-11-11 20:16:22 -0800467TEST_F(WiFiServiceTest, ConnectTask8021x) {
Paul Stewart3c504012013-01-17 17:49:58 -0800468 WiFiServiceRefPtr service = MakeServiceWithWiFi(flimflam::kSecurity8021x);
Gaurav Shah10109f22011-11-11 20:16:22 -0800469 Service::EapCredentials eap;
470 eap.identity = "identity";
Wade Guthrie005bd342012-05-02 09:37:07 -0700471 eap.password = "mumble";
Paul Stewart3c504012013-01-17 17:49:58 -0800472 service->set_eap(eap);
473 EXPECT_CALL(*wifi(), ConnectTo(service.get(), EAPSecurityArgs()));
474 service->Connect(NULL);
Gaurav Shah10109f22011-11-11 20:16:22 -0800475}
476
Paul Stewarte2d7c502012-07-16 16:35:10 -0700477TEST_F(WiFiServiceTest, ConnectTaskAdHocFrequency) {
478 vector<uint8_t> ssid(1, 'a');
479 WiFiEndpointRefPtr endpoint_nofreq =
mukesh agrawal43970a22013-02-15 16:00:07 -0800480 MakeOpenEndpoint("a", "00:00:00:00:00:01", 0, 0);
Paul Stewarte2d7c502012-07-16 16:35:10 -0700481 WiFiEndpointRefPtr endpoint_freq =
mukesh agrawal43970a22013-02-15 16:00:07 -0800482 MakeOpenEndpoint("a", "00:00:00:00:00:02", 2412, 0);
Paul Stewarte2d7c502012-07-16 16:35:10 -0700483
Paul Stewart3c504012013-01-17 17:49:58 -0800484 WiFiServiceRefPtr wifi_service = MakeServiceWithWiFi(flimflam::kSecurityNone);
Paul Stewarte2d7c502012-07-16 16:35:10 -0700485 wifi_service->AddEndpoint(endpoint_freq);
486 EXPECT_CALL(*wifi(),
487 ConnectTo(wifi_service.get(), FrequencyArg(false)));
488 wifi_service->Connect(NULL);
489
490 wifi_service = new WiFiService(control_interface(),
491 dispatcher(),
492 metrics(),
493 manager(),
Paul Stewart3c504012013-01-17 17:49:58 -0800494 provider(),
Paul Stewarte2d7c502012-07-16 16:35:10 -0700495 ssid,
496 flimflam::kModeAdhoc,
497 flimflam::kSecurityNone,
498 false);
499 EXPECT_CALL(*wifi(),
500 ConnectTo(wifi_service.get(), FrequencyArg(false)));
Paul Stewart3c504012013-01-17 17:49:58 -0800501 SetWiFiForService(wifi_service, wifi());
Paul Stewarte2d7c502012-07-16 16:35:10 -0700502 wifi_service->Connect(NULL);
503
504 wifi_service = new WiFiService(control_interface(),
505 dispatcher(),
506 metrics(),
507 manager(),
Paul Stewart3c504012013-01-17 17:49:58 -0800508 provider(),
Paul Stewarte2d7c502012-07-16 16:35:10 -0700509 ssid,
510 flimflam::kModeAdhoc,
511 flimflam::kSecurityNone,
512 false);
513 wifi_service->AddEndpoint(endpoint_nofreq);
Paul Stewart3c504012013-01-17 17:49:58 -0800514 SetWiFiForService(wifi_service, wifi());
Paul Stewarte2d7c502012-07-16 16:35:10 -0700515 EXPECT_CALL(*wifi(),
516 ConnectTo(wifi_service.get(), FrequencyArg(false)));
517 wifi_service->Connect(NULL);
518
519 wifi_service = new WiFiService(control_interface(),
520 dispatcher(),
521 metrics(),
522 manager(),
Paul Stewart3c504012013-01-17 17:49:58 -0800523 provider(),
Paul Stewarte2d7c502012-07-16 16:35:10 -0700524 ssid,
525 flimflam::kModeAdhoc,
526 flimflam::kSecurityNone,
527 false);
528 wifi_service->AddEndpoint(endpoint_freq);
Paul Stewart3c504012013-01-17 17:49:58 -0800529 SetWiFiForService(wifi_service, wifi());
Paul Stewarte2d7c502012-07-16 16:35:10 -0700530 EXPECT_CALL(*wifi(),
531 ConnectTo(wifi_service.get(), FrequencyArg(true)));
532 wifi_service->Connect(NULL);
533}
534
Paul Stewarta5e7d5f2013-01-09 18:06:15 -0800535TEST_F(WiFiServiceTest, ConnectTaskWPA80211w) {
Paul Stewart3c504012013-01-17 17:49:58 -0800536 WiFiServiceRefPtr wifi_service = MakeServiceWithWiFi(flimflam::kSecurityPsk);
mukesh agrawal43970a22013-02-15 16:00:07 -0800537 WiFiEndpointRefPtr endpoint =
538 MakeOpenEndpoint("a", "00:00:00:00:00:01", 0, 0);
Paul Stewarta5e7d5f2013-01-09 18:06:15 -0800539 endpoint->ieee80211w_required_ = true;
540 wifi_service->AddEndpoint(endpoint);
541 Error error;
542 wifi_service->SetPassphrase("0:mumblemumblem", &error);
543 EXPECT_CALL(*wifi(),
544 ConnectTo(wifi_service.get(), WPA80211wSecurityArgs()));
545 wifi_service->Connect(NULL);
546}
547
Thieu Lef4cbda92011-11-10 23:41:24 +0000548MATCHER(WEPSecurityArgsKeyIndex0, "") {
549 return ContainsKey(arg, wpa_supplicant::kPropertyAuthAlg) &&
550 ContainsKey(arg, wpa_supplicant::kPropertyWEPKey + std::string("0")) &&
551 ContainsKey(arg, wpa_supplicant::kPropertyWEPTxKeyIndex) &&
552 (arg.find(wpa_supplicant::kPropertyWEPTxKeyIndex)->second.
553 reader().get_uint32() == 0);
554}
555
556MATCHER(WEPSecurityArgsKeyIndex1, "") {
557 return ContainsKey(arg, wpa_supplicant::kPropertyAuthAlg) &&
558 ContainsKey(arg, wpa_supplicant::kPropertyWEPKey + std::string("1")) &&
559 ContainsKey(arg, wpa_supplicant::kPropertyWEPTxKeyIndex) &&
560 (arg.find(wpa_supplicant::kPropertyWEPTxKeyIndex)->second.
561 reader().get_uint32() == 1);
562}
563
564MATCHER(WEPSecurityArgsKeyIndex2, "") {
565 return ContainsKey(arg, wpa_supplicant::kPropertyAuthAlg) &&
566 ContainsKey(arg, wpa_supplicant::kPropertyWEPKey + std::string("2")) &&
567 ContainsKey(arg, wpa_supplicant::kPropertyWEPTxKeyIndex) &&
568 (arg.find(wpa_supplicant::kPropertyWEPTxKeyIndex)->second.
569 reader().get_uint32() == 2);
570}
571
572MATCHER(WEPSecurityArgsKeyIndex3, "") {
573 return ContainsKey(arg, wpa_supplicant::kPropertyAuthAlg) &&
574 ContainsKey(arg, wpa_supplicant::kPropertyWEPKey + std::string("3")) &&
575 ContainsKey(arg, wpa_supplicant::kPropertyWEPTxKeyIndex) &&
576 (arg.find(wpa_supplicant::kPropertyWEPTxKeyIndex)->second.
577 reader().get_uint32() == 3);
578}
579
580TEST_F(WiFiServiceTest, ConnectTaskWEP) {
Paul Stewart3c504012013-01-17 17:49:58 -0800581 WiFiServiceRefPtr wifi_service = MakeServiceWithWiFi(flimflam::kSecurityWep);
Thieu Lef4cbda92011-11-10 23:41:24 +0000582 Error error;
583 wifi_service->SetPassphrase("0:abcdefghijklm", &error);
584 EXPECT_CALL(*wifi(),
585 ConnectTo(wifi_service.get(), WEPSecurityArgsKeyIndex0()));
Eric Shienbrood9a245532012-03-07 14:20:39 -0500586 wifi_service->Connect(NULL);
Thieu Lef4cbda92011-11-10 23:41:24 +0000587
588 wifi_service->SetPassphrase("abcdefghijklm", &error);
589 EXPECT_CALL(*wifi(),
590 ConnectTo(wifi_service.get(), WEPSecurityArgsKeyIndex0()));
Eric Shienbrood9a245532012-03-07 14:20:39 -0500591 wifi_service->Connect(NULL);
Thieu Lef4cbda92011-11-10 23:41:24 +0000592
593 wifi_service->SetPassphrase("1:abcdefghijklm", &error);
594 EXPECT_CALL(*wifi(),
595 ConnectTo(wifi_service.get(), WEPSecurityArgsKeyIndex1()));
Eric Shienbrood9a245532012-03-07 14:20:39 -0500596 wifi_service->Connect(NULL);
Thieu Lef4cbda92011-11-10 23:41:24 +0000597
598 wifi_service->SetPassphrase("2:abcdefghijklm", &error);
599 EXPECT_CALL(*wifi(),
600 ConnectTo(wifi_service.get(), WEPSecurityArgsKeyIndex2()));
Eric Shienbrood9a245532012-03-07 14:20:39 -0500601 wifi_service->Connect(NULL);
Thieu Lef4cbda92011-11-10 23:41:24 +0000602
603 wifi_service->SetPassphrase("3:abcdefghijklm", &error);
604 EXPECT_CALL(*wifi(),
605 ConnectTo(wifi_service.get(), WEPSecurityArgsKeyIndex3()));
Eric Shienbrood9a245532012-03-07 14:20:39 -0500606 wifi_service->Connect(NULL);
Thieu Lef4cbda92011-11-10 23:41:24 +0000607}
608
Gaurav Shah29d68882012-01-30 19:06:42 -0800609
610MATCHER(DynamicWEPArgs, "") {
611 return ContainsKey(arg, wpa_supplicant::kNetworkPropertyEapIdentity) &&
612 ContainsKey(arg, wpa_supplicant::kNetworkPropertyCaPath) &&
613 !ContainsKey(arg, wpa_supplicant::kPropertySecurityProtocol);
614}
615
616// Dynamic WEP + 802.1x.
617TEST_F(WiFiServiceTest, ConnectTaskDynamicWEP) {
Paul Stewart3c504012013-01-17 17:49:58 -0800618 WiFiServiceRefPtr wifi_service = MakeServiceWithWiFi(flimflam::kSecurityWep);
Gaurav Shah29d68882012-01-30 19:06:42 -0800619
620 Service::EapCredentials eap;
621 eap.key_management = "IEEE8021X";
622 eap.identity = "something";
Wade Guthrie005bd342012-05-02 09:37:07 -0700623 eap.password = "mumble";
Gaurav Shah29d68882012-01-30 19:06:42 -0800624 wifi_service->set_eap(eap);
625 EXPECT_CALL(*wifi(),
626 ConnectTo(wifi_service.get(), DynamicWEPArgs()));
Eric Shienbrood9a245532012-03-07 14:20:39 -0500627 wifi_service->Connect(NULL);
Gaurav Shah29d68882012-01-30 19:06:42 -0800628}
629
Paul Stewart835934a2012-12-06 19:27:09 -0800630TEST_F(WiFiServiceTest, SetPassphraseRemovesCachedCredentials) {
631 vector<uint8_t> ssid(5);
Paul Stewart3c504012013-01-17 17:49:58 -0800632 WiFiServiceRefPtr wifi_service = MakeServiceWithWiFi(flimflam::kSecurityRsn);
Paul Stewart835934a2012-12-06 19:27:09 -0800633
634 const string kPassphrase = "abcdefgh";
635
636 {
637 Error error;
638 // A changed passphrase should trigger cache removal.
639 EXPECT_CALL(*wifi(), ClearCachedCredentials(wifi_service.get()));
640 wifi_service->SetPassphrase(kPassphrase, &error);
641 Mock::VerifyAndClearExpectations(wifi());
642 EXPECT_TRUE(error.IsSuccess());
643 }
644
645 {
646 Error error;
647 // An unchanged passphrase should not trigger cache removal.
648 EXPECT_CALL(*wifi(), ClearCachedCredentials(_)).Times(0);
649 wifi_service->SetPassphrase(kPassphrase, &error);
650 Mock::VerifyAndClearExpectations(wifi());
651 EXPECT_TRUE(error.IsSuccess());
652 }
653
654 {
655 Error error;
656 // A modified passphrase should trigger cache removal.
657 EXPECT_CALL(*wifi(), ClearCachedCredentials(wifi_service.get()));
658 wifi_service->SetPassphrase(kPassphrase + "X", &error);
659 Mock::VerifyAndClearExpectations(wifi());
660 EXPECT_TRUE(error.IsSuccess());
661 }
662
663 {
664 Error error;
665 // A cleared passphrase should also trigger cache removal.
666 EXPECT_CALL(*wifi(), ClearCachedCredentials(wifi_service.get()));
667 wifi_service->ClearPassphrase(&error);
668 Mock::VerifyAndClearExpectations(wifi());
669 EXPECT_TRUE(error.IsSuccess());
670 }
671
672 {
673 Error error;
674 // An invalid passphrase should not trigger cache removal.
675 EXPECT_CALL(*wifi(), ClearCachedCredentials(_)).Times(0);
676 wifi_service->SetPassphrase("", &error);
677 Mock::VerifyAndClearExpectations(wifi());
678 EXPECT_FALSE(error.IsSuccess());
679 }
680
681 {
682 // Any change to EAP parameters (including a null one) will trigger cache
683 // removal. This is a lot less granular than the passphrase checks above.
684 EXPECT_CALL(*wifi(), ClearCachedCredentials(wifi_service.get()));
685 wifi_service->set_eap(Service::EapCredentials());
686 Mock::VerifyAndClearExpectations(wifi());
687 }
688}
689
Paul Stewartd08f4432011-11-04 07:48:20 -0700690TEST_F(WiFiServiceTest, LoadHidden) {
Paul Stewart3c504012013-01-17 17:49:58 -0800691 WiFiServiceRefPtr service = MakeSimpleService(flimflam::kSecurityNone);
Paul Stewartd08f4432011-11-04 07:48:20 -0700692 ASSERT_FALSE(service->hidden_ssid_);
693 NiceMock<MockStore> mock_store;
694 const string storage_id = service->GetStorageIdentifier();
Paul Stewart71a4d3b2013-01-18 18:12:56 -0800695 set<string> groups;
696 groups.insert(storage_id);
Paul Stewartd08f4432011-11-04 07:48:20 -0700697 EXPECT_CALL(mock_store, ContainsGroup(StrEq(storage_id)))
698 .WillRepeatedly(Return(true));
Paul Stewart71a4d3b2013-01-18 18:12:56 -0800699 EXPECT_CALL(mock_store, GetGroupsWithProperties(
700 ContainsWiFiProperties(
Paul Stewart3c504012013-01-17 17:49:58 -0800701 simple_ssid(), flimflam::kModeManaged, flimflam::kSecurityNone)))
Paul Stewart71a4d3b2013-01-18 18:12:56 -0800702 .WillRepeatedly(Return(groups));
Paul Stewartd08f4432011-11-04 07:48:20 -0700703 EXPECT_CALL(mock_store, GetBool(_, _, _))
704 .WillRepeatedly(Return(false));
705 EXPECT_CALL(mock_store,
706 GetBool(StrEq(storage_id), WiFiService::kStorageHiddenSSID, _))
707 .WillRepeatedly(DoAll(SetArgumentPointee<2>(true), Return(true)));
708 EXPECT_TRUE(service->Load(&mock_store));
709 EXPECT_TRUE(service->hidden_ssid_);
710}
711
Paul Stewart71a4d3b2013-01-18 18:12:56 -0800712TEST_F(WiFiServiceTest, LoadMultipleMatchingGroups) {
Paul Stewart3c504012013-01-17 17:49:58 -0800713 WiFiServiceRefPtr service = MakeServiceWithWiFi(flimflam::kSecurityNone);
Paul Stewart71a4d3b2013-01-18 18:12:56 -0800714 set<string> groups;
715 groups.insert("id0");
716 groups.insert("id1");
717 // Make sure we retain the first matched group in the same way that
718 // WiFiService::Load() will.
719 string first_group = *groups.begin();
720
721 NiceMock<MockStore> mock_store;
722 EXPECT_CALL(mock_store, GetGroupsWithProperties(
723 ContainsWiFiProperties(
Paul Stewart3c504012013-01-17 17:49:58 -0800724 simple_ssid(), flimflam::kModeManaged, flimflam::kSecurityNone)))
Paul Stewart71a4d3b2013-01-18 18:12:56 -0800725 .WillRepeatedly(Return(groups));
726 EXPECT_CALL(mock_store, ContainsGroup(first_group))
727 .WillRepeatedly(Return(true));
728 EXPECT_CALL(mock_store, ContainsGroup(StrNe(first_group))).Times(0);
729 EXPECT_CALL(mock_store, GetBool(first_group, _, _))
730 .WillRepeatedly(Return(false));
731 EXPECT_CALL(mock_store, GetBool(StrNe(first_group), _, _)).Times(0);
732 ScopedMockLog log;
733 EXPECT_CALL(log, Log(_, _, _)).Times(AnyNumber());
734 EXPECT_CALL(log, Log(logging::LOG_WARNING, _,
735 EndsWith("choosing the first.")));
736 EXPECT_TRUE(service->Load(&mock_store));
737}
738
Paul Stewartd08f4432011-11-04 07:48:20 -0700739TEST_F(WiFiServiceSecurityTest, WPAMapping) {
740 EXPECT_TRUE(TestStorageMapping(flimflam::kSecurityRsn,
741 flimflam::kSecurityPsk));
742 EXPECT_TRUE(TestStorageMapping(flimflam::kSecurityWpa,
743 flimflam::kSecurityPsk));
744 EXPECT_TRUE(TestStorageMapping(flimflam::kSecurityPsk,
745 flimflam::kSecurityPsk));
746 EXPECT_TRUE(TestStorageMapping(flimflam::kSecurityWep,
747 flimflam::kSecurityWep));
748 EXPECT_TRUE(TestStorageMapping(flimflam::kSecurityNone,
749 flimflam::kSecurityNone));
Gaurav Shah10109f22011-11-11 20:16:22 -0800750 EXPECT_TRUE(TestStorageMapping(flimflam::kSecurity8021x,
751 flimflam::kSecurity8021x));
Paul Stewartd08f4432011-11-04 07:48:20 -0700752}
753
754TEST_F(WiFiServiceSecurityTest, LoadMapping) {
755 EXPECT_TRUE(TestLoadMapping(flimflam::kSecurityRsn,
756 flimflam::kSecurityPsk,
757 true));
758 EXPECT_TRUE(TestLoadMapping(flimflam::kSecurityRsn,
759 flimflam::kSecurityRsn,
Paul Stewart71a4d3b2013-01-18 18:12:56 -0800760 false));
Paul Stewartd08f4432011-11-04 07:48:20 -0700761 EXPECT_TRUE(TestLoadMapping(flimflam::kSecurityRsn,
762 flimflam::kSecurityWpa,
763 false));
764 EXPECT_TRUE(TestLoadMapping(flimflam::kSecurityWpa,
765 flimflam::kSecurityPsk,
766 true));
767 EXPECT_TRUE(TestLoadMapping(flimflam::kSecurityWpa,
768 flimflam::kSecurityWpa,
Paul Stewart71a4d3b2013-01-18 18:12:56 -0800769 false));
Paul Stewartd08f4432011-11-04 07:48:20 -0700770 EXPECT_TRUE(TestLoadMapping(flimflam::kSecurityWpa,
771 flimflam::kSecurityRsn,
772 false));
773 EXPECT_TRUE(TestLoadMapping(flimflam::kSecurityWep,
774 flimflam::kSecurityWep,
775 true));
776 EXPECT_TRUE(TestLoadMapping(flimflam::kSecurityWep,
777 flimflam::kSecurityPsk,
778 false));
779}
780
Paul Stewartd8ad3c42012-01-09 12:39:38 -0800781TEST_F(WiFiServiceTest, LoadAndUnloadPassphrase) {
Paul Stewart3c504012013-01-17 17:49:58 -0800782 WiFiServiceRefPtr service = MakeSimpleService(flimflam::kSecurityPsk);
Paul Stewartd8ad3c42012-01-09 12:39:38 -0800783 NiceMock<MockStore> mock_store;
784 const string storage_id = service->GetStorageIdentifier();
785 EXPECT_CALL(mock_store, ContainsGroup(StrEq(storage_id)))
786 .WillRepeatedly(Return(true));
Paul Stewart71a4d3b2013-01-18 18:12:56 -0800787 set<string> groups;
788 groups.insert(storage_id);
789 EXPECT_CALL(mock_store, GetGroupsWithProperties(
790 ContainsWiFiProperties(
Paul Stewart3c504012013-01-17 17:49:58 -0800791 simple_ssid(), flimflam::kModeManaged, flimflam::kSecurityPsk)))
Paul Stewart71a4d3b2013-01-18 18:12:56 -0800792 .WillRepeatedly(Return(groups));
Paul Stewartd8ad3c42012-01-09 12:39:38 -0800793 EXPECT_CALL(mock_store, GetBool(_, _, _))
794 .WillRepeatedly(Return(false));
795 const string passphrase = "passphrase";
796 EXPECT_CALL(mock_store,
797 GetCryptedString(StrEq(storage_id),
798 WiFiService::kStoragePassphrase, _))
799 .WillRepeatedly(DoAll(SetArgumentPointee<2>(passphrase), Return(true)));
800 EXPECT_CALL(mock_store,
801 GetCryptedString(StrEq(storage_id),
802 StrNe(WiFiService::kStoragePassphrase), _))
803 .WillRepeatedly(Return(false));
804 EXPECT_TRUE(service->need_passphrase_);
805 EXPECT_TRUE(service->Load(&mock_store));
806 EXPECT_EQ(passphrase, service->passphrase_);
807 EXPECT_TRUE(service->connectable());
808 EXPECT_FALSE(service->need_passphrase_);
809 service->Unload();
810 EXPECT_EQ(string(""), service->passphrase_);
811 EXPECT_FALSE(service->connectable());
812 EXPECT_TRUE(service->need_passphrase_);
813}
814
Christopher Wiley27b47232012-11-02 13:13:00 -0700815TEST_F(WiFiServiceTest, ConfigureMakesConnectable) {
816 string guid("legit_guid");
817 KeyValueStore args;
818 args.SetString(flimflam::kEapIdentityProperty, "legit_identity");
819 args.SetString(flimflam::kEapPasswordProperty, "legit_password");
820 args.SetString(flimflam::kEAPEAPProperty, "PEAP");
821 args.SetString(flimflam::kGuidProperty, guid);
822 Error error;
Christopher Wiley27b47232012-11-02 13:13:00 -0700823
Paul Stewart3c504012013-01-17 17:49:58 -0800824 WiFiServiceRefPtr service = MakeSimpleService(flimflam::kSecurity8021x);
Christopher Wiley27b47232012-11-02 13:13:00 -0700825 // Hack the GUID in so that we don't have to mess about with WiFi to regsiter
826 // our service. This way, Manager will handle the lookup itself.
827 service->set_guid(guid);
828 manager()->RegisterService(service);
829 EXPECT_FALSE(service->connectable());
830 EXPECT_EQ(service.get(), manager()->GetService(args, &error).get());
831 EXPECT_TRUE(error.IsSuccess());
832 EXPECT_TRUE(service->connectable());
833}
834
Paul Stewart3c504012013-01-17 17:49:58 -0800835TEST_F(WiFiServiceTest, ConfigurePassphrase) {
836 EXPECT_EQ(Error::kNotSupported,
837 TestConfigurePassphrase(flimflam::kSecurityNone, ""));
838 EXPECT_EQ(Error::kNotSupported,
839 TestConfigurePassphrase(flimflam::kSecurityNone, "foo"));
840 EXPECT_EQ(Error::kSuccess,
841 TestConfigurePassphrase(flimflam::kSecurityWep, NULL));
842 EXPECT_EQ(Error::kInvalidPassphrase,
843 TestConfigurePassphrase(flimflam::kSecurityWep, ""));
844 EXPECT_EQ(Error::kInvalidPassphrase,
845 TestConfigurePassphrase(flimflam::kSecurityWep, "abcd"));
846 EXPECT_EQ(Error::kSuccess,
847 TestConfigurePassphrase(flimflam::kSecurityWep, "abcde"));
848 EXPECT_EQ(Error::kSuccess,
849 TestConfigurePassphrase(flimflam::kSecurityWep, "abcdefghijklm"));
850 EXPECT_EQ(Error::kSuccess,
851 TestConfigurePassphrase(flimflam::kSecurityWep, "0:abcdefghijklm"));
852 EXPECT_EQ(Error::kSuccess,
853 TestConfigurePassphrase(flimflam::kSecurityWep, "0102030405"));
854 EXPECT_EQ(Error::kInvalidPassphrase,
855 TestConfigurePassphrase(flimflam::kSecurityWep, "0x0102030405"));
856 EXPECT_EQ(Error::kInvalidPassphrase,
857 TestConfigurePassphrase(flimflam::kSecurityWep, "O102030405"));
858 EXPECT_EQ(Error::kInvalidPassphrase,
859 TestConfigurePassphrase(flimflam::kSecurityWep, "1:O102030405"));
860 EXPECT_EQ(Error::kInvalidPassphrase,
861 TestConfigurePassphrase(flimflam::kSecurityWep, "1:0xO102030405"));
862 EXPECT_EQ(Error::kInvalidPassphrase,
863 TestConfigurePassphrase(flimflam::kSecurityWep, "0xO102030405"));
864 EXPECT_EQ(Error::kSuccess,
865 TestConfigurePassphrase(flimflam::kSecurityWep,
866 "0102030405060708090a0b0c0d"));
867 EXPECT_EQ(Error::kSuccess,
868 TestConfigurePassphrase(flimflam::kSecurityWep,
869 "0102030405060708090A0B0C0D"));
870 EXPECT_EQ(Error::kSuccess,
871 TestConfigurePassphrase(flimflam::kSecurityWep,
872 "0:0102030405060708090a0b0c0d"));
873 EXPECT_EQ(Error::kSuccess,
874 TestConfigurePassphrase(flimflam::kSecurityWep,
875 "0:0x0102030405060708090a0b0c0d"));
876 EXPECT_EQ(Error::kSuccess,
877 TestConfigurePassphrase(flimflam::kSecurityWpa, NULL));
878 EXPECT_EQ(Error::kSuccess,
879 TestConfigurePassphrase(flimflam::kSecurityWpa, "secure password"));
880 EXPECT_EQ(Error::kInvalidPassphrase,
881 TestConfigurePassphrase(flimflam::kSecurityWpa, ""));
882 EXPECT_EQ(Error::kSuccess, TestConfigurePassphrase(
883 flimflam::kSecurityWpa,
884 string(IEEE_80211::kWPAAsciiMinLen, 'Z').c_str()));
885 EXPECT_EQ(Error::kSuccess, TestConfigurePassphrase(
886 flimflam::kSecurityWpa,
887 string(IEEE_80211::kWPAAsciiMaxLen, 'Z').c_str()));
888 // subtle: invalid length for hex key, but valid as ascii passphrase
889 EXPECT_EQ(Error::kSuccess, TestConfigurePassphrase(
890 flimflam::kSecurityWpa,
891 string(IEEE_80211::kWPAHexLen-1, '1').c_str()));
892 EXPECT_EQ(Error::kSuccess, TestConfigurePassphrase(
893 flimflam::kSecurityWpa,
894 string(IEEE_80211::kWPAHexLen, '1').c_str()));
895 EXPECT_EQ(Error::kInvalidPassphrase, TestConfigurePassphrase(
896 flimflam::kSecurityWpa,
897 string(IEEE_80211::kWPAAsciiMinLen-1, 'Z').c_str()));
898 EXPECT_EQ(Error::kInvalidPassphrase, TestConfigurePassphrase(
899 flimflam::kSecurityWpa,
900 string(IEEE_80211::kWPAAsciiMaxLen+1, 'Z').c_str()));
901 EXPECT_EQ(Error::kInvalidPassphrase, TestConfigurePassphrase(
902 flimflam::kSecurityWpa,
903 string(IEEE_80211::kWPAHexLen+1, '1').c_str()));
904}
905
906TEST_F(WiFiServiceTest, ConfigureRedundantProperties) {
907 WiFiServiceRefPtr service = MakeSimpleService(flimflam::kSecurityNone);
908 KeyValueStore args;
909 args.SetString(flimflam::kTypeProperty, flimflam::kTypeWifi);
910 args.SetString(flimflam::kSSIDProperty, simple_ssid_string());
911 args.SetString(flimflam::kSecurityProperty, flimflam::kSecurityNone);
912 const string kGUID = "aguid";
913 args.SetString(flimflam::kGuidProperty, kGUID);
914
915 EXPECT_EQ("", service->guid());
916 Error error;
917 service->Configure(args, &error);
918 EXPECT_TRUE(error.IsSuccess());
919 EXPECT_EQ(kGUID, service->guid());
920}
921
922TEST_F(WiFiServiceTest, DisconnectWithWiFi) {
923 WiFiServiceRefPtr service = MakeServiceWithWiFi(flimflam::kSecurityWep);
924 EXPECT_CALL(*wifi(), DisconnectFrom(service.get())).Times(1);
925 Error error;
926 service->Disconnect(&error);
927}
928
929TEST_F(WiFiServiceTest, DisconnectWithoutWiFi) {
930 WiFiServiceRefPtr service = MakeSimpleService(flimflam::kSecurityWep);
931 EXPECT_CALL(*wifi(), DisconnectFrom(_)).Times(0);
932 Error error;
933 service->Disconnect(&error);
934 EXPECT_EQ(Error::kOperationFailed, error.type());
935}
936
937TEST_F(WiFiServiceTest, DisconnectWithoutWiFiWhileAssociating) {
938 WiFiServiceRefPtr service = MakeSimpleService(flimflam::kSecurityWep);
939 EXPECT_CALL(*wifi(), DisconnectFrom(_)).Times(0);
940 service->SetState(Service::kStateAssociating);
941 ScopedMockLog log;
942 EXPECT_CALL(log, Log(_, _, _)).Times(AnyNumber());
943 EXPECT_CALL(log, Log(logging::LOG_ERROR, _,
944 HasSubstr("WiFi endpoints do not (yet) exist.")));
945 Error error;
946 service->Disconnect(&error);
947 EXPECT_EQ(Error::kOperationFailed, error.type());
948}
949
Paul Stewart835934a2012-12-06 19:27:09 -0800950TEST_F(WiFiServiceTest, UnloadAndClearCacheWEP) {
Paul Stewart3c504012013-01-17 17:49:58 -0800951 WiFiServiceRefPtr service = MakeServiceWithWiFi(flimflam::kSecurityWep);
Paul Stewart835934a2012-12-06 19:27:09 -0800952 EXPECT_CALL(*wifi(), ClearCachedCredentials(service.get())).Times(1);
953 EXPECT_CALL(*wifi(), DisconnectFrom(service.get())).Times(1);
Paul Stewart66c86002012-01-30 18:00:52 -0800954 service->Unload();
955}
956
957TEST_F(WiFiServiceTest, UnloadAndClearCache8021x) {
Paul Stewart3c504012013-01-17 17:49:58 -0800958 WiFiServiceRefPtr service = MakeServiceWithWiFi(flimflam::kSecurity8021x);
Paul Stewart835934a2012-12-06 19:27:09 -0800959 EXPECT_CALL(*wifi(), ClearCachedCredentials(service.get())).Times(1);
960 EXPECT_CALL(*wifi(), DisconnectFrom(service.get())).Times(1);
Paul Stewart66c86002012-01-30 18:00:52 -0800961 service->Unload();
962}
963
Paul Stewart0756db92012-01-27 08:34:47 -0800964TEST_F(WiFiServiceTest, ParseStorageIdentifierNone) {
Paul Stewart3c504012013-01-17 17:49:58 -0800965 WiFiServiceRefPtr service = MakeSimpleService(flimflam::kSecurityNone);
Paul Stewarta41e38d2011-11-11 07:47:29 -0800966 const string storage_id = service->GetStorageIdentifier();
967 string address;
968 string mode;
969 string security;
970 EXPECT_TRUE(service->ParseStorageIdentifier(storage_id, &address, &mode,
971 &security));
Paul Stewart3c504012013-01-17 17:49:58 -0800972 EXPECT_EQ(StringToLowerASCII(GetAnyDeviceAddress()), address);
Paul Stewarta41e38d2011-11-11 07:47:29 -0800973 EXPECT_EQ(flimflam::kModeManaged, mode);
974 EXPECT_EQ(flimflam::kSecurityNone, security);
975}
976
Paul Stewart0756db92012-01-27 08:34:47 -0800977TEST_F(WiFiServiceTest, ParseStorageIdentifier8021x) {
978 // Do a separate test for 802.1x, since kSecurity8021x contains a "_",
979 // which needs to be dealt with specially in the parser.
Paul Stewart3c504012013-01-17 17:49:58 -0800980 WiFiServiceRefPtr service = MakeSimpleService(flimflam::kSecurity8021x);
Paul Stewart0756db92012-01-27 08:34:47 -0800981 const string storage_id = service->GetStorageIdentifier();
982 string address;
983 string mode;
984 string security;
985 EXPECT_TRUE(service->ParseStorageIdentifier(storage_id, &address, &mode,
986 &security));
Paul Stewart3c504012013-01-17 17:49:58 -0800987 EXPECT_EQ(StringToLowerASCII(GetAnyDeviceAddress()), address);
Paul Stewart0756db92012-01-27 08:34:47 -0800988 EXPECT_EQ(flimflam::kModeManaged, mode);
989 EXPECT_EQ(flimflam::kSecurity8021x, security);
990}
991
Paul Stewart85aea152013-01-22 09:31:56 -0800992TEST_F(WiFiServiceFixupStorageTest, FixedEntries) {
993 const string kNonWiFiId = "vpn_foo";
994 const string kUnparsableWiFiId = "wifi_foo";
995
996 AddGroup(kNonWiFiId);
997 AddGroup(kUnparsableWiFiId);
Paul Stewart71a4d3b2013-01-18 18:12:56 -0800998 AddServiceEntry(true, true, true, true);
999 AddServiceEntry(false, false, false, false);
1000 AddServiceEntry(true, true, true, true);
1001 AddServiceEntry(false, false, false, false);
Paul Stewart85aea152013-01-22 09:31:56 -08001002 EXPECT_TRUE(FixupServiceEntries());
1003}
1004
1005TEST_F(WiFiServiceFixupStorageTest, NoFixedEntries) {
1006 const string kNonWiFiId = "vpn_foo";
1007 const string kUnparsableWiFiId = "wifi_foo";
1008
1009 AddGroup(kNonWiFiId);
1010 AddGroup(kUnparsableWiFiId);
Paul Stewart71a4d3b2013-01-18 18:12:56 -08001011 AddServiceEntry(true, true, true, true);
Paul Stewart85aea152013-01-22 09:31:56 -08001012 EXPECT_FALSE(FixupServiceEntries());
1013}
1014
1015TEST_F(WiFiServiceFixupStorageTest, MissingTypeProperty) {
Paul Stewart71a4d3b2013-01-18 18:12:56 -08001016 AddServiceEntry(false, true, true, true);
Paul Stewart85aea152013-01-22 09:31:56 -08001017 EXPECT_TRUE(FixupServiceEntries());
1018}
1019
1020TEST_F(WiFiServiceFixupStorageTest, MissingModeProperty) {
Paul Stewart71a4d3b2013-01-18 18:12:56 -08001021 AddServiceEntry(true, false, true, true);
Paul Stewart85aea152013-01-22 09:31:56 -08001022 EXPECT_TRUE(FixupServiceEntries());
1023}
1024
1025TEST_F(WiFiServiceFixupStorageTest, MissingSecurityProperty) {
Paul Stewart71a4d3b2013-01-18 18:12:56 -08001026 AddServiceEntry(true, true, false, true);
Paul Stewart85aea152013-01-22 09:31:56 -08001027 EXPECT_TRUE(FixupServiceEntries());
1028}
1029
Paul Stewart71a4d3b2013-01-18 18:12:56 -08001030TEST_F(WiFiServiceFixupStorageTest, MissingSecurityClassProperty) {
1031 AddServiceEntry(true, true, true, false);
1032 EXPECT_TRUE(FixupServiceEntries());
1033}
Paul Stewart85aea152013-01-22 09:31:56 -08001034
mukesh agrawal29c13a12011-11-24 00:09:19 +00001035TEST_F(WiFiServiceTest, Connectable) {
1036 // Open network should be connectable.
Gaurav Shah10109f22011-11-11 20:16:22 -08001037 EXPECT_TRUE(CheckConnectable(flimflam::kSecurityNone, NULL, NULL));
mukesh agrawal29c13a12011-11-24 00:09:19 +00001038
1039 // Open network should remain connectable if we try to set a password on it.
Gaurav Shah10109f22011-11-11 20:16:22 -08001040 EXPECT_TRUE(CheckConnectable(flimflam::kSecurityNone, "abcde", NULL));
mukesh agrawal29c13a12011-11-24 00:09:19 +00001041
1042 // WEP network with passphrase set should be connectable.
Gaurav Shah10109f22011-11-11 20:16:22 -08001043 EXPECT_TRUE(CheckConnectable(flimflam::kSecurityWep, "abcde", NULL));
mukesh agrawal29c13a12011-11-24 00:09:19 +00001044
1045 // WEP network without passphrase set should NOT be connectable.
Gaurav Shah10109f22011-11-11 20:16:22 -08001046 EXPECT_FALSE(CheckConnectable(flimflam::kSecurityWep, NULL, NULL));
mukesh agrawal29c13a12011-11-24 00:09:19 +00001047
1048 // A bad passphrase should not make a WEP network connectable.
Gaurav Shah10109f22011-11-11 20:16:22 -08001049 EXPECT_FALSE(CheckConnectable(flimflam::kSecurityWep, "a", NULL));
mukesh agrawal29c13a12011-11-24 00:09:19 +00001050
1051 // Similar to WEP, for WPA.
Gaurav Shah10109f22011-11-11 20:16:22 -08001052 EXPECT_TRUE(CheckConnectable(flimflam::kSecurityWpa, "abcdefgh", NULL));
1053 EXPECT_FALSE(CheckConnectable(flimflam::kSecurityWpa, NULL, NULL));
1054 EXPECT_FALSE(CheckConnectable(flimflam::kSecurityWpa, "a", NULL));
mukesh agrawal29c13a12011-11-24 00:09:19 +00001055
1056 // Unconfigured 802.1x should NOT be connectable.
Gaurav Shah10109f22011-11-11 20:16:22 -08001057 EXPECT_FALSE(CheckConnectable(flimflam::kSecurity8021x, NULL, NULL));
1058
1059 Service::EapCredentials eap;
1060 // Empty EAP credentials should not make a 802.1x network connectable.
1061 EXPECT_FALSE(CheckConnectable(flimflam::kSecurity8021x, NULL, &eap));
1062
1063 eap.identity = "something";
1064 // If client certificate is being used, a private key must exist.
1065 eap.client_cert = "some client cert";
1066 EXPECT_FALSE(CheckConnectable(flimflam::kSecurity8021x, NULL, &eap));
1067 eap.private_key = "some private key";
1068 EXPECT_TRUE(CheckConnectable(flimflam::kSecurity8021x, NULL, &eap));
1069
1070 // Identity is always required.
1071 eap.identity.clear();
1072 EXPECT_FALSE(CheckConnectable(flimflam::kSecurity8021x, NULL, &eap));
1073
1074 eap.identity = "something";
1075 // For non EAP-TLS types, a password is required.
1076 eap.eap = "Non-TLS";
1077 EXPECT_FALSE(CheckConnectable(flimflam::kSecurity8021x, NULL, &eap));
1078 eap.password = "some password";
1079 EXPECT_TRUE(CheckConnectable(flimflam::kSecurity8021x, NULL, &eap));
Gaurav Shah29d68882012-01-30 19:06:42 -08001080 // Dynamic WEP + 802.1X should be connectable under the same conditions.
1081 eap.key_management = "IEEE8021X";
1082 EXPECT_TRUE(CheckConnectable(flimflam::kSecurityWep, NULL, &eap));
mukesh agrawal29c13a12011-11-24 00:09:19 +00001083}
1084
mukesh agrawal8a3188d2011-12-01 20:56:44 +00001085TEST_F(WiFiServiceTest, IsAutoConnectable) {
mukesh agrawalbf14e942012-03-02 14:36:34 -08001086 const char *reason;
Paul Stewart3c504012013-01-17 17:49:58 -08001087 WiFiServiceRefPtr service = MakeSimpleService(flimflam::kSecurityNone);
mukesh agrawal8a3188d2011-12-01 20:56:44 +00001088 EXPECT_CALL(*wifi(), IsIdle())
1089 .WillRepeatedly(Return(true));
1090 EXPECT_FALSE(service->HasEndpoints());
mukesh agrawalbf14e942012-03-02 14:36:34 -08001091 EXPECT_FALSE(service->IsAutoConnectable(&reason));
1092 EXPECT_STREQ(WiFiService::kAutoConnNoEndpoint, reason);
mukesh agrawal8a3188d2011-12-01 20:56:44 +00001093
mukesh agrawalbf14e942012-03-02 14:36:34 -08001094 reason = "";
mukesh agrawal43970a22013-02-15 16:00:07 -08001095 WiFiEndpointRefPtr endpoint =
1096 MakeOpenEndpoint("a", "00:00:00:00:00:01", 0, 0);
mukesh agrawal8a3188d2011-12-01 20:56:44 +00001097 service->AddEndpoint(endpoint);
1098 EXPECT_CALL(*wifi(), IsIdle())
1099 .WillRepeatedly(Return(true));
1100 EXPECT_TRUE(service->HasEndpoints());
mukesh agrawalbf14e942012-03-02 14:36:34 -08001101 EXPECT_TRUE(service->IsAutoConnectable(&reason));
1102 EXPECT_STREQ("", reason);
mukesh agrawal8a3188d2011-12-01 20:56:44 +00001103
1104 // WiFi only supports connecting to one Service at a time. So, to
1105 // avoid disrupting connectivity, we only allow auto-connection to
1106 // a WiFiService when the corresponding WiFi is idle.
1107 EXPECT_CALL(*wifi(), IsIdle())
1108 .WillRepeatedly(Return(false));
1109 EXPECT_TRUE(service->HasEndpoints());
mukesh agrawalbf14e942012-03-02 14:36:34 -08001110 EXPECT_FALSE(service->IsAutoConnectable(&reason));
1111 EXPECT_STREQ(WiFiService::kAutoConnBusy, reason);
mukesh agrawal8a3188d2011-12-01 20:56:44 +00001112}
1113
1114TEST_F(WiFiServiceTest, AutoConnect) {
mukesh agrawalbf14e942012-03-02 14:36:34 -08001115 const char *reason;
Paul Stewart3c504012013-01-17 17:49:58 -08001116 WiFiServiceRefPtr service = MakeSimpleService(flimflam::kSecurityNone);
mukesh agrawalbf14e942012-03-02 14:36:34 -08001117 EXPECT_FALSE(service->IsAutoConnectable(&reason));
mukesh agrawal8a3188d2011-12-01 20:56:44 +00001118 EXPECT_CALL(*wifi(), ConnectTo(_, _))
1119 .Times(0);
1120 service->AutoConnect();
Eric Shienbrood3e20a232012-02-16 11:35:56 -05001121 dispatcher()->DispatchPendingEvents();
mukesh agrawal8a3188d2011-12-01 20:56:44 +00001122
mukesh agrawal43970a22013-02-15 16:00:07 -08001123 WiFiEndpointRefPtr endpoint =
1124 MakeOpenEndpoint("a", "00:00:00:00:00:01", 0, 0);
mukesh agrawal8a3188d2011-12-01 20:56:44 +00001125 service->AddEndpoint(endpoint);
1126 EXPECT_CALL(*wifi(), IsIdle())
1127 .WillRepeatedly(Return(true));
mukesh agrawalbf14e942012-03-02 14:36:34 -08001128 EXPECT_TRUE(service->IsAutoConnectable(&reason));
mukesh agrawal8a3188d2011-12-01 20:56:44 +00001129 EXPECT_CALL(*wifi(), ConnectTo(_, _));
1130 service->AutoConnect();
Eric Shienbrood3e20a232012-02-16 11:35:56 -05001131 dispatcher()->DispatchPendingEvents();
mukesh agrawaladb68482012-01-17 16:31:51 -08001132
1133 Error error;
Christopher Wileyabd3b502012-09-26 13:08:52 -07001134 service->UserInitiatedDisconnect(&error);
Eric Shienbrood3e20a232012-02-16 11:35:56 -05001135 dispatcher()->DispatchPendingEvents();
mukesh agrawalbf14e942012-03-02 14:36:34 -08001136 EXPECT_FALSE(service->IsAutoConnectable(&reason));
mukesh agrawal8a3188d2011-12-01 20:56:44 +00001137}
1138
Gaurav Shah10109f22011-11-11 20:16:22 -08001139TEST_F(WiFiServiceTest, Populate8021x) {
Paul Stewart3c504012013-01-17 17:49:58 -08001140 WiFiServiceRefPtr service = MakeSimpleService(flimflam::kSecurityNone);
Gaurav Shah10109f22011-11-11 20:16:22 -08001141 Service::EapCredentials eap;
1142 eap.identity = "testidentity";
Paul Stewart20550982012-04-16 12:16:11 -07001143 eap.pin = "xxxx";
Gaurav Shah10109f22011-11-11 20:16:22 -08001144 service->set_eap(eap);
1145 map<string, ::DBus::Variant> params;
1146 service->Populate8021xProperties(&params);
1147 // Test that only non-empty 802.1x properties are populated.
Paul Stewartecf4cd12012-04-17 11:08:39 -07001148 EXPECT_TRUE(ContainsKey(params, wpa_supplicant::kNetworkPropertyEapIdentity));
1149 EXPECT_FALSE(ContainsKey(params, wpa_supplicant::kNetworkPropertyEapKeyId));
1150 EXPECT_FALSE(ContainsKey(params, wpa_supplicant::kNetworkPropertyEapCaCert));
1151
Paul Stewart20550982012-04-16 12:16:11 -07001152 // Test that CA path is set by default.
Paul Stewartecf4cd12012-04-17 11:08:39 -07001153 EXPECT_TRUE(ContainsKey(params, wpa_supplicant::kNetworkPropertyCaPath));
1154
Paul Stewart20550982012-04-16 12:16:11 -07001155 // Test that hardware-backed security arguments are not set.
Paul Stewartecf4cd12012-04-17 11:08:39 -07001156 EXPECT_FALSE(ContainsKey(params, wpa_supplicant::kNetworkPropertyEapPin));
1157 EXPECT_FALSE(ContainsKey(params, wpa_supplicant::kNetworkPropertyEngine));
1158 EXPECT_FALSE(ContainsKey(params, wpa_supplicant::kNetworkPropertyEngineId));
Paul Stewart20550982012-04-16 12:16:11 -07001159}
1160
1161TEST_F(WiFiServiceTest, Populate8021xNoSystemCAs) {
Paul Stewart3c504012013-01-17 17:49:58 -08001162 WiFiServiceRefPtr service = MakeSimpleService(flimflam::kSecurityNone);
Paul Stewart20550982012-04-16 12:16:11 -07001163 Service::EapCredentials eap;
1164 eap.identity = "testidentity";
1165 eap.use_system_cas = false;
1166 service->set_eap(eap);
1167 map<string, ::DBus::Variant> params;
1168 service->Populate8021xProperties(&params);
1169 // Test that CA path is not set if use_system_cas is explicitly false.
Paul Stewartecf4cd12012-04-17 11:08:39 -07001170 EXPECT_FALSE(ContainsKey(params, wpa_supplicant::kNetworkPropertyCaPath));
Paul Stewart20550982012-04-16 12:16:11 -07001171}
1172
1173TEST_F(WiFiServiceTest, Populate8021xUsingHardwareAuth) {
Paul Stewart3c504012013-01-17 17:49:58 -08001174 WiFiServiceRefPtr service = MakeSimpleService(flimflam::kSecurityNone);
Paul Stewart20550982012-04-16 12:16:11 -07001175 Service::EapCredentials eap;
1176 eap.identity = "testidentity";
1177 eap.key_id = "key_id";
1178 eap.pin = "xxxx";
1179 service->set_eap(eap);
1180 map<string, ::DBus::Variant> params;
1181 service->Populate8021xProperties(&params);
1182 // Test that EAP engine parameters set if key_id is set.
Paul Stewartecf4cd12012-04-17 11:08:39 -07001183 EXPECT_TRUE(ContainsKey(params, wpa_supplicant::kNetworkPropertyEapPin));
1184 EXPECT_TRUE(ContainsKey(params, wpa_supplicant::kNetworkPropertyEapKeyId));
1185 EXPECT_TRUE(ContainsKey(params, wpa_supplicant::kNetworkPropertyEngine));
1186 EXPECT_TRUE(ContainsKey(params, wpa_supplicant::kNetworkPropertyEngineId));
1187}
1188
1189TEST_F(WiFiServiceTest, Populate8021xNSS) {
1190 vector<uint8_t> ssid(1, 'a');
Paul Stewart3c504012013-01-17 17:49:58 -08001191 WiFiServiceRefPtr service = MakeSimpleService(flimflam::kSecurityNone);
Paul Stewartecf4cd12012-04-17 11:08:39 -07001192 Service::EapCredentials eap;
1193 eap.ca_cert_nss = "nss_nickname";
1194 service->set_eap(eap);
1195 MockNSS nss;
1196 service->nss_ = &nss;
1197
1198 const string kNSSCertfile("/tmp/nss-cert");
1199 FilePath nss_cert(kNSSCertfile);
1200 vector<char> ssid_in_chars(ssid.begin(), ssid.end());
1201 EXPECT_CALL(nss, GetDERCertfile(eap.ca_cert_nss, ssid_in_chars))
1202 .WillOnce(Return(nss_cert));
1203
1204 map<string, ::DBus::Variant> params;
1205 service->Populate8021xProperties(&params);
1206 EXPECT_TRUE(ContainsKey(params, wpa_supplicant::kNetworkPropertyEapCaCert));
1207 if (ContainsKey(params, wpa_supplicant::kNetworkPropertyEapCaCert)) {
1208 EXPECT_EQ(kNSSCertfile, params[wpa_supplicant::kNetworkPropertyEapCaCert]
1209 .reader().get_string());
1210 }
Gaurav Shah10109f22011-11-11 20:16:22 -08001211}
1212
mukesh agrawal8abd2f62012-01-30 14:56:14 -08001213TEST_F(WiFiServiceTest, ClearWriteOnlyDerivedProperty) {
Paul Stewart3c504012013-01-17 17:49:58 -08001214 WiFiServiceRefPtr wifi_service = MakeSimpleService(flimflam::kSecurityWep);
mukesh agrawal8abd2f62012-01-30 14:56:14 -08001215
1216 EXPECT_EQ("", wifi_service->passphrase_);
1217
1218 ::DBus::Error error;
mukesh agrawal6bb9e7c2012-01-30 14:57:54 -08001219 EXPECT_TRUE(DBusAdaptor::SetProperty(
mukesh agrawal8abd2f62012-01-30 14:56:14 -08001220 wifi_service->mutable_store(),
1221 flimflam::kPassphraseProperty,
1222 DBusAdaptor::StringToVariant("0:abcde"),
1223 &error));
1224 EXPECT_EQ("0:abcde", wifi_service->passphrase_);
1225
1226 EXPECT_TRUE(DBusAdaptor::ClearProperty(wifi_service->mutable_store(),
1227 flimflam::kPassphraseProperty,
1228 &error));
1229 EXPECT_EQ("", wifi_service->passphrase_);
1230}
1231
mukesh agrawale1d90e92012-02-15 17:36:08 -08001232TEST_F(WiFiServiceTest, SignalToStrength) {
1233 // Verify that our mapping is sane, in the sense that it preserves ordering.
1234 // We break the test into two domains, because we assume that positive
1235 // values aren't actually in dBm.
1236 for (int16 i = std::numeric_limits<int16>::min(); i < 0; ++i) {
1237 int16 current_mapped = WiFiService::SignalToStrength(i);
1238 int16 next_mapped = WiFiService::SignalToStrength(i+1);
1239 EXPECT_LE(current_mapped, next_mapped)
1240 << "(original values " << i << " " << i+1 << ")";
mukesh agrawal8f3f7752012-02-17 19:42:09 -08001241 EXPECT_GE(current_mapped, Service::kStrengthMin);
1242 EXPECT_LE(current_mapped, Service::kStrengthMax);
mukesh agrawale1d90e92012-02-15 17:36:08 -08001243 }
1244 for (int16 i = 1; i < std::numeric_limits<int16>::max(); ++i) {
1245 int16 current_mapped = WiFiService::SignalToStrength(i);
1246 int16 next_mapped = WiFiService::SignalToStrength(i+1);
1247 EXPECT_LE(current_mapped, next_mapped)
1248 << "(original values " << i << " " << i+1 << ")";
mukesh agrawal8f3f7752012-02-17 19:42:09 -08001249 EXPECT_GE(current_mapped, Service::kStrengthMin);
1250 EXPECT_LE(current_mapped, Service::kStrengthMax);
mukesh agrawale1d90e92012-02-15 17:36:08 -08001251 }
1252}
1253
1254TEST_F(WiFiServiceUpdateFromEndpointsTest, Strengths) {
1255 // If the chosen signal values don't map to distinct strength
1256 // values, then we can't expect our other tests to pass. So verify
1257 // their distinctness.
1258 EXPECT_TRUE(kOkEndpointStrength != kBadEndpointStrength);
1259 EXPECT_TRUE(kOkEndpointStrength != kGoodEndpointStrength);
1260 EXPECT_TRUE(kGoodEndpointStrength != kBadEndpointStrength);
1261}
1262
1263TEST_F(WiFiServiceUpdateFromEndpointsTest, Floating) {
1264 // Initial endpoint updates values.
1265 EXPECT_CALL(adaptor, EmitUint16Changed(
1266 flimflam::kWifiFrequency, kOkEndpointFrequency));
mukesh agrawal923f14f2012-06-04 16:46:08 -07001267 EXPECT_CALL(adaptor, EmitStringChanged(
1268 flimflam::kWifiBSsid, kOkEndpointBssId));
1269 EXPECT_CALL(adaptor, EmitUint8Changed(
mukesh agrawale1d90e92012-02-15 17:36:08 -08001270 flimflam::kSignalStrengthProperty, kOkEndpointStrength));
1271 service->AddEndpoint(ok_endpoint);
Darin Petkov4a66cc52012-06-15 10:08:29 +02001272 EXPECT_EQ(1, service->GetEndpointCount());
mukesh agrawale1d90e92012-02-15 17:36:08 -08001273 Mock::VerifyAndClearExpectations(&adaptor);
1274
1275 // Endpoint with stronger signal updates values.
1276 EXPECT_CALL(adaptor, EmitUint16Changed(
1277 flimflam::kWifiFrequency, kGoodEndpointFrequency));
mukesh agrawal923f14f2012-06-04 16:46:08 -07001278 EXPECT_CALL(adaptor, EmitStringChanged(
1279 flimflam::kWifiBSsid, kGoodEndpointBssId));
mukesh agrawale1d90e92012-02-15 17:36:08 -08001280 EXPECT_CALL(adaptor, EmitUint8Changed(
1281 flimflam::kSignalStrengthProperty, kGoodEndpointStrength));
1282 service->AddEndpoint(good_endpoint);
Darin Petkov4a66cc52012-06-15 10:08:29 +02001283 EXPECT_EQ(2, service->GetEndpointCount());
mukesh agrawale1d90e92012-02-15 17:36:08 -08001284 Mock::VerifyAndClearExpectations(&adaptor);
1285
1286 // Endpoint with lower signal does not change values.
1287 EXPECT_CALL(adaptor, EmitUint16Changed(flimflam::kWifiFrequency, _)).Times(0);
mukesh agrawal923f14f2012-06-04 16:46:08 -07001288 EXPECT_CALL(adaptor, EmitStringChanged(flimflam::kWifiBSsid, _)).Times(0);
mukesh agrawale1d90e92012-02-15 17:36:08 -08001289 EXPECT_CALL(adaptor,
1290 EmitUint8Changed(flimflam::kSignalStrengthProperty, _)).Times(0);
1291 service->AddEndpoint(bad_endpoint);
Darin Petkov4a66cc52012-06-15 10:08:29 +02001292 EXPECT_EQ(3, service->GetEndpointCount());
mukesh agrawale1d90e92012-02-15 17:36:08 -08001293 Mock::VerifyAndClearExpectations(&adaptor);
1294
1295 // Removing non-optimal endpoint does not change values.
1296 EXPECT_CALL(adaptor, EmitUint16Changed(flimflam::kWifiFrequency, _)).Times(0);
mukesh agrawal923f14f2012-06-04 16:46:08 -07001297 EXPECT_CALL(adaptor, EmitStringChanged(flimflam::kWifiBSsid, _)).Times(0);
mukesh agrawale1d90e92012-02-15 17:36:08 -08001298 EXPECT_CALL(adaptor,
1299 EmitUint8Changed(flimflam::kSignalStrengthProperty, _)).Times(0);
1300 service->RemoveEndpoint(bad_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 // Removing optimal endpoint updates values.
1305 EXPECT_CALL(adaptor, EmitUint16Changed(
1306 flimflam::kWifiFrequency, kOkEndpointFrequency));
mukesh agrawal923f14f2012-06-04 16:46:08 -07001307 EXPECT_CALL(adaptor, EmitStringChanged(
1308 flimflam::kWifiBSsid, kOkEndpointBssId));
mukesh agrawale1d90e92012-02-15 17:36:08 -08001309 EXPECT_CALL(adaptor, EmitUint8Changed(
1310 flimflam::kSignalStrengthProperty, kOkEndpointStrength));
1311 service->RemoveEndpoint(good_endpoint);
Darin Petkov4a66cc52012-06-15 10:08:29 +02001312 EXPECT_EQ(1, service->GetEndpointCount());
mukesh agrawale1d90e92012-02-15 17:36:08 -08001313 Mock::VerifyAndClearExpectations(&adaptor);
1314
1315 // Removing last endpoint updates values (and doesn't crash).
1316 EXPECT_CALL(adaptor, EmitUint16Changed(flimflam::kWifiFrequency, _));
mukesh agrawal923f14f2012-06-04 16:46:08 -07001317 EXPECT_CALL(adaptor, EmitStringChanged(flimflam::kWifiBSsid, _));
mukesh agrawale1d90e92012-02-15 17:36:08 -08001318 EXPECT_CALL(adaptor, EmitUint8Changed(flimflam::kSignalStrengthProperty, _));
1319 service->RemoveEndpoint(ok_endpoint);
Darin Petkov4a66cc52012-06-15 10:08:29 +02001320 EXPECT_EQ(0, service->GetEndpointCount());
mukesh agrawale1d90e92012-02-15 17:36:08 -08001321 Mock::VerifyAndClearExpectations(&adaptor);
1322}
1323
1324TEST_F(WiFiServiceUpdateFromEndpointsTest, Connected) {
1325 EXPECT_CALL(adaptor, EmitUint16Changed(_, _)).Times(AnyNumber());
mukesh agrawal923f14f2012-06-04 16:46:08 -07001326 EXPECT_CALL(adaptor, EmitStringChanged(_, _)).Times(AnyNumber());
mukesh agrawale1d90e92012-02-15 17:36:08 -08001327 EXPECT_CALL(adaptor, EmitUint8Changed(_, _)).Times(AnyNumber());
1328 EXPECT_CALL(adaptor, EmitBoolChanged(_, _)).Times(AnyNumber());
1329 service->AddEndpoint(bad_endpoint);
1330 service->AddEndpoint(ok_endpoint);
Darin Petkov4a66cc52012-06-15 10:08:29 +02001331 EXPECT_EQ(2, service->GetEndpointCount());
mukesh agrawale1d90e92012-02-15 17:36:08 -08001332 Mock::VerifyAndClearExpectations(&adaptor);
1333
1334 // Setting current endpoint forces adoption of its values, even if it
1335 // doesn't have the highest signal.
1336 EXPECT_CALL(adaptor, EmitUint16Changed(
1337 flimflam::kWifiFrequency, kBadEndpointFrequency));
mukesh agrawal923f14f2012-06-04 16:46:08 -07001338 EXPECT_CALL(adaptor, EmitStringChanged(
1339 flimflam::kWifiBSsid, kBadEndpointBssId));
mukesh agrawale1d90e92012-02-15 17:36:08 -08001340 EXPECT_CALL(adaptor, EmitUint8Changed(
1341 flimflam::kSignalStrengthProperty, kBadEndpointStrength));
1342 service->NotifyCurrentEndpoint(bad_endpoint);
1343 Mock::VerifyAndClearExpectations(&adaptor);
1344
1345 // Adding a better endpoint doesn't matter, when current endpoint is set.
1346 EXPECT_CALL(adaptor, EmitUint16Changed(flimflam::kWifiFrequency, _)).Times(0);
mukesh agrawal923f14f2012-06-04 16:46:08 -07001347 EXPECT_CALL(adaptor, EmitStringChanged(flimflam::kWifiBSsid, _)).Times(0);
mukesh agrawale1d90e92012-02-15 17:36:08 -08001348 EXPECT_CALL(adaptor,
1349 EmitUint8Changed(flimflam::kSignalStrengthProperty, _)).Times(0);
1350 service->AddEndpoint(good_endpoint);
Darin Petkov4a66cc52012-06-15 10:08:29 +02001351 EXPECT_EQ(3, service->GetEndpointCount());
mukesh agrawale1d90e92012-02-15 17:36:08 -08001352 Mock::VerifyAndClearExpectations(&adaptor);
1353
1354 // Removing a better endpoint doesn't matter, when current endpoint is set.
1355 EXPECT_CALL(adaptor, EmitUint16Changed(flimflam::kWifiFrequency, _)).Times(0);
mukesh agrawal923f14f2012-06-04 16:46:08 -07001356 EXPECT_CALL(adaptor, EmitStringChanged(flimflam::kWifiBSsid, _)).Times(0);
mukesh agrawale1d90e92012-02-15 17:36:08 -08001357 EXPECT_CALL(adaptor,
1358 EmitUint8Changed(flimflam::kSignalStrengthProperty, _)).Times(0);
1359 service->RemoveEndpoint(good_endpoint);
1360 Mock::VerifyAndClearExpectations(&adaptor);
1361
1362 // Removing the current endpoint is safe and sane.
1363 EXPECT_CALL(adaptor, EmitUint16Changed(
1364 flimflam::kWifiFrequency, kOkEndpointFrequency));
mukesh agrawal923f14f2012-06-04 16:46:08 -07001365 EXPECT_CALL(adaptor, EmitStringChanged(
1366 flimflam::kWifiBSsid, kOkEndpointBssId));
mukesh agrawale1d90e92012-02-15 17:36:08 -08001367 EXPECT_CALL(adaptor, EmitUint8Changed(
1368 flimflam::kSignalStrengthProperty, kOkEndpointStrength));
1369 service->RemoveEndpoint(bad_endpoint);
1370 Mock::VerifyAndClearExpectations(&adaptor);
1371
1372 // Clearing the current endpoint (without removing it) is also safe and sane.
1373 service->NotifyCurrentEndpoint(ok_endpoint);
1374 EXPECT_CALL(adaptor, EmitUint16Changed(flimflam::kWifiFrequency, _)).Times(0);
mukesh agrawal923f14f2012-06-04 16:46:08 -07001375 EXPECT_CALL(adaptor, EmitStringChanged(flimflam::kWifiBSsid, _)).Times(0);
mukesh agrawale1d90e92012-02-15 17:36:08 -08001376 EXPECT_CALL(adaptor,
1377 EmitUint8Changed(flimflam::kSignalStrengthProperty, _)).Times(0);
1378 service->NotifyCurrentEndpoint(NULL);
1379 Mock::VerifyAndClearExpectations(&adaptor);
1380}
1381
1382TEST_F(WiFiServiceUpdateFromEndpointsTest, EndpointModified) {
1383 EXPECT_CALL(adaptor, EmitUint16Changed(_, _)).Times(AnyNumber());
mukesh agrawal923f14f2012-06-04 16:46:08 -07001384 EXPECT_CALL(adaptor, EmitStringChanged(_, _)).Times(AnyNumber());
mukesh agrawale1d90e92012-02-15 17:36:08 -08001385 EXPECT_CALL(adaptor, EmitUint8Changed(_, _)).Times(AnyNumber());
1386 EXPECT_CALL(adaptor, EmitBoolChanged(_, _)).Times(AnyNumber());
1387 service->AddEndpoint(ok_endpoint);
1388 service->AddEndpoint(good_endpoint);
Darin Petkov4a66cc52012-06-15 10:08:29 +02001389 EXPECT_EQ(2, service->GetEndpointCount());
mukesh agrawale1d90e92012-02-15 17:36:08 -08001390 Mock::VerifyAndClearExpectations(&adaptor);
1391
1392 // Updating sub-optimal Endpoint doesn't update Service.
1393 EXPECT_CALL(adaptor, EmitUint16Changed(flimflam::kWifiFrequency, _)).Times(0);
mukesh agrawal923f14f2012-06-04 16:46:08 -07001394 EXPECT_CALL(adaptor, EmitStringChanged(flimflam::kWifiBSsid, _)).Times(0);
mukesh agrawale1d90e92012-02-15 17:36:08 -08001395 EXPECT_CALL(adaptor,
1396 EmitUint8Changed(flimflam::kSignalStrengthProperty, _)).Times(0);
1397 ok_endpoint->signal_strength_ = (kOkEndpointSignal + kGoodEndpointSignal) / 2;
Paul Stewart3c504012013-01-17 17:49:58 -08001398 service->NotifyEndpointUpdated(ok_endpoint);
mukesh agrawale1d90e92012-02-15 17:36:08 -08001399 Mock::VerifyAndClearExpectations(&adaptor);
1400
1401 // Updating optimal Endpoint updates appropriate Service property.
1402 EXPECT_CALL(adaptor, EmitUint16Changed(flimflam::kWifiFrequency, _)).Times(0);
mukesh agrawal923f14f2012-06-04 16:46:08 -07001403 EXPECT_CALL(adaptor, EmitStringChanged(flimflam::kWifiBSsid, _)).Times(0);
mukesh agrawale1d90e92012-02-15 17:36:08 -08001404 EXPECT_CALL(adaptor, EmitUint8Changed(flimflam::kSignalStrengthProperty, _));
1405 good_endpoint->signal_strength_ = kGoodEndpointSignal + 1;
Paul Stewart3c504012013-01-17 17:49:58 -08001406 service->NotifyEndpointUpdated(good_endpoint);
mukesh agrawale1d90e92012-02-15 17:36:08 -08001407 Mock::VerifyAndClearExpectations(&adaptor);
1408
1409 // Change in optimal Endpoint updates Service properties.
1410 EXPECT_CALL(adaptor, EmitUint16Changed(
1411 flimflam::kWifiFrequency, kOkEndpointFrequency));
mukesh agrawal923f14f2012-06-04 16:46:08 -07001412 EXPECT_CALL(adaptor, EmitStringChanged(
1413 flimflam::kWifiBSsid, kOkEndpointBssId));
mukesh agrawale1d90e92012-02-15 17:36:08 -08001414 EXPECT_CALL(adaptor, EmitUint8Changed(flimflam::kSignalStrengthProperty, _));
1415 ok_endpoint->signal_strength_ = kGoodEndpointSignal + 2;
Paul Stewart3c504012013-01-17 17:49:58 -08001416 service->NotifyEndpointUpdated(ok_endpoint);
mukesh agrawale1d90e92012-02-15 17:36:08 -08001417 Mock::VerifyAndClearExpectations(&adaptor);
1418}
1419
Paul Stewarta5e7d5f2013-01-09 18:06:15 -08001420TEST_F(WiFiServiceUpdateFromEndpointsTest, Ieee80211w) {
1421 EXPECT_CALL(adaptor, EmitUint16Changed(_, _)).Times(AnyNumber());
1422 EXPECT_CALL(adaptor, EmitStringChanged(_, _)).Times(AnyNumber());
1423 EXPECT_CALL(adaptor, EmitUint8Changed(_, _)).Times(AnyNumber());
1424 EXPECT_CALL(adaptor, EmitBoolChanged(_, _)).Times(AnyNumber());
1425 service->AddEndpoint(ok_endpoint);
1426 EXPECT_FALSE(service->ieee80211w_required());
1427 good_endpoint->ieee80211w_required_ = true;
1428 service->AddEndpoint(good_endpoint);
1429 EXPECT_TRUE(service->ieee80211w_required());
1430 service->RemoveEndpoint(good_endpoint);
1431 EXPECT_TRUE(service->ieee80211w_required());
1432}
1433
Paul Stewart8653f462013-02-06 12:21:05 -08001434TEST_F(WiFiServiceUpdateFromEndpointsTest, WarningOnDisconnect) {
1435 service->AddEndpoint(ok_endpoint);
1436 service->SetState(Service::kStateAssociating);
1437 ScopedMockLog log;
1438 EXPECT_CALL(log, Log(_, _, _)).Times(AnyNumber());
1439 EXPECT_CALL(log, Log(logging::LOG_WARNING, _,
1440 EndsWith("disconnect due to no remaining endpoints.")));
1441 service->RemoveEndpoint(ok_endpoint);
1442}
1443
Paul Stewart6df20bd2013-03-13 19:31:25 -07001444TEST_F(WiFiServiceTest, SecurityFromCurrentEndpoint) {
1445 WiFiServiceRefPtr service(MakeSimpleService(flimflam::kSecurityPsk));
1446 EXPECT_EQ(flimflam::kSecurityPsk, service->GetSecurity(NULL));
1447 WiFiEndpoint *endpoint = MakeOpenEndpoint(
1448 simple_ssid_string(), "00:00:00:00:00:00", 0, 0);
1449 service->AddEndpoint(endpoint);
1450 EXPECT_EQ(flimflam::kSecurityPsk, service->GetSecurity(NULL));
1451 service->NotifyCurrentEndpoint(endpoint);
1452 EXPECT_EQ(flimflam::kSecurityNone, service->GetSecurity(NULL));
1453 service->NotifyCurrentEndpoint(NULL);
1454 EXPECT_EQ(flimflam::kSecurityPsk, service->GetSecurity(NULL));
1455}
1456
mukesh agrawal43970a22013-02-15 16:00:07 -08001457TEST_F(WiFiServiceTest, UpdateSecurity) {
1458 // Cleartext and pre-shared-key crypto.
1459 {
1460 WiFiServiceRefPtr service = MakeSimpleService(flimflam::kSecurityNone);
1461 EXPECT_EQ(Service::kCryptoNone, service->crypto_algorithm());
1462 EXPECT_FALSE(service->key_rotation());
1463 EXPECT_FALSE(service->endpoint_auth());
1464 }
1465 {
1466 WiFiServiceRefPtr service = MakeSimpleService(flimflam::kSecurityWep);
1467 EXPECT_EQ(Service::kCryptoRc4, service->crypto_algorithm());
1468 EXPECT_FALSE(service->key_rotation());
1469 EXPECT_FALSE(service->endpoint_auth());
1470 }
1471 {
1472 WiFiServiceRefPtr service = MakeSimpleService(flimflam::kSecurityPsk);
1473 EXPECT_EQ(Service::kCryptoRc4, service->crypto_algorithm());
1474 EXPECT_TRUE(service->key_rotation());
1475 EXPECT_FALSE(service->endpoint_auth());
1476 }
1477 {
1478 WiFiServiceRefPtr service = MakeSimpleService(flimflam::kSecurityWpa);
1479 EXPECT_EQ(Service::kCryptoRc4, service->crypto_algorithm());
1480 EXPECT_TRUE(service->key_rotation());
1481 EXPECT_FALSE(service->endpoint_auth());
1482 }
1483 {
1484 WiFiServiceRefPtr service = MakeSimpleService(flimflam::kSecurityRsn);
1485 EXPECT_EQ(Service::kCryptoAes, service->crypto_algorithm());
1486 EXPECT_TRUE(service->key_rotation());
1487 EXPECT_FALSE(service->endpoint_auth());
1488 }
1489
1490 // Crypto with 802.1X key management.
1491 {
1492 // WEP
1493 WiFiServiceRefPtr service = MakeSimpleService(flimflam::kSecurityWep);
1494 service->SetEAPKeyManagement("IEEE8021X");
1495 EXPECT_EQ(Service::kCryptoRc4, service->crypto_algorithm());
1496 EXPECT_TRUE(service->key_rotation());
1497 EXPECT_TRUE(service->endpoint_auth());
1498 }
1499 {
1500 // WPA
1501 WiFiServiceRefPtr service = MakeSimpleService(flimflam::kSecurity8021x);
1502 WiFiEndpointRefPtr endpoint =
1503 MakeEndpoint("a", "00:00:00:00:00:01", 0, 0, true, false);
1504 service->AddEndpoint(endpoint);
1505 EXPECT_EQ(Service::kCryptoRc4, service->crypto_algorithm());
1506 EXPECT_TRUE(service->key_rotation());
1507 EXPECT_TRUE(service->endpoint_auth());
1508 }
1509 {
1510 // RSN
1511 WiFiServiceRefPtr service = MakeSimpleService(flimflam::kSecurity8021x);
1512 WiFiEndpointRefPtr endpoint =
1513 MakeEndpoint("a", "00:00:00:00:00:01", 0, 0, false, true);
1514 service->AddEndpoint(endpoint);
1515 EXPECT_EQ(Service::kCryptoAes, service->crypto_algorithm());
1516 EXPECT_TRUE(service->key_rotation());
1517 EXPECT_TRUE(service->endpoint_auth());
1518 }
1519 {
1520 // AP supports both WPA and RSN.
1521 WiFiServiceRefPtr service = MakeSimpleService(flimflam::kSecurity8021x);
1522 WiFiEndpointRefPtr endpoint =
1523 MakeEndpoint("a", "00:00:00:00:00:01", 0, 0, true, true);
1524 service->AddEndpoint(endpoint);
1525 EXPECT_EQ(Service::kCryptoAes, service->crypto_algorithm());
1526 EXPECT_TRUE(service->key_rotation());
1527 EXPECT_TRUE(service->endpoint_auth());
1528 }
1529}
1530
1531TEST_F(WiFiServiceTest, ComputeCipher8021x) {
1532 // No endpoints.
1533 {
1534 const set<WiFiEndpointConstRefPtr> endpoints;
1535 EXPECT_EQ(Service::kCryptoNone,
1536 WiFiService::ComputeCipher8021x(endpoints));
1537 }
1538
1539 // Single endpoint, various configs.
1540 {
1541 set<WiFiEndpointConstRefPtr> endpoints;
1542 endpoints.insert(
1543 MakeEndpoint("a", "00:00:00:00:00:01", 0, 0, false, false));
1544 EXPECT_EQ(Service::kCryptoNone,
1545 WiFiService::ComputeCipher8021x(endpoints));
1546 }
1547 {
1548 set<WiFiEndpointConstRefPtr> endpoints;
1549 endpoints.insert(
1550 MakeEndpoint("a", "00:00:00:00:00:01", 0, 0, true, false));
1551 EXPECT_EQ(Service::kCryptoRc4,
1552 WiFiService::ComputeCipher8021x(endpoints));
1553 }
1554 {
1555 set<WiFiEndpointConstRefPtr> endpoints;
1556 endpoints.insert(
1557 MakeEndpoint("a", "00:00:00:00:00:01", 0, 0, false, true));
1558 EXPECT_EQ(Service::kCryptoAes,
1559 WiFiService::ComputeCipher8021x(endpoints));
1560 }
1561 {
1562 set<WiFiEndpointConstRefPtr> endpoints;
1563 endpoints.insert(
1564 MakeEndpoint("a", "00:00:00:00:00:01", 0, 0, true, true));
1565 EXPECT_EQ(Service::kCryptoAes,
1566 WiFiService::ComputeCipher8021x(endpoints));
1567 }
1568
1569 // Multiple endpoints.
1570 {
1571 set<WiFiEndpointConstRefPtr> endpoints;
1572 endpoints.insert(
1573 MakeEndpoint("a", "00:00:00:00:00:01", 0, 0, false, false));
1574 endpoints.insert(
1575 MakeEndpoint("a", "00:00:00:00:00:02", 0, 0, false, false));
1576 EXPECT_EQ(Service::kCryptoNone,
1577 WiFiService::ComputeCipher8021x(endpoints));
1578 }
1579 {
1580 set<WiFiEndpointConstRefPtr> endpoints;
1581 endpoints.insert(
1582 MakeEndpoint("a", "00:00:00:00:00:01", 0, 0, false, false));
1583 endpoints.insert(
1584 MakeEndpoint("a", "00:00:00:00:00:02", 0, 0, true, false));
1585 EXPECT_EQ(Service::kCryptoNone,
1586 WiFiService::ComputeCipher8021x(endpoints));
1587 }
1588 {
1589 set<WiFiEndpointConstRefPtr> endpoints;
1590 endpoints.insert(
1591 MakeEndpoint("a", "00:00:00:00:00:01", 0, 0, true, false));
1592 endpoints.insert(
1593 MakeEndpoint("a", "00:00:00:00:00:02", 0, 0, true, false));
1594 EXPECT_EQ(Service::kCryptoRc4,
1595 WiFiService::ComputeCipher8021x(endpoints));
1596 }
1597 {
1598 set<WiFiEndpointConstRefPtr> endpoints;
1599 endpoints.insert(
1600 MakeEndpoint("a", "00:00:00:00:00:01", 0, 0, true, false));
1601 endpoints.insert(
1602 MakeEndpoint("a", "00:00:00:00:00:02", 0, 0, false, true));
1603 EXPECT_EQ(Service::kCryptoRc4,
1604 WiFiService::ComputeCipher8021x(endpoints));
1605 }
1606 {
1607 set<WiFiEndpointConstRefPtr> endpoints;
1608 endpoints.insert(
1609 MakeEndpoint("a", "00:00:00:00:00:01", 0, 0, false, true));
1610 endpoints.insert(
1611 MakeEndpoint("a", "00:00:00:00:00:02", 0, 0, false, true));
1612 EXPECT_EQ(Service::kCryptoAes,
1613 WiFiService::ComputeCipher8021x(endpoints));
1614 }
1615 {
1616 set<WiFiEndpointConstRefPtr> endpoints;
1617 endpoints.insert(
1618 MakeEndpoint("a", "00:00:00:00:00:01", 0, 0, true, true));
1619 endpoints.insert(
1620 MakeEndpoint("a", "00:00:00:00:00:02", 0, 0, true, true));
1621 EXPECT_EQ(Service::kCryptoAes,
1622 WiFiService::ComputeCipher8021x(endpoints));
1623 }
1624}
1625
Albert Chaulk0e1cdea2013-02-27 15:32:55 -08001626TEST_F(WiFiServiceTest, Unload) {
1627 WiFiServiceRefPtr service = MakeServiceWithWiFi(flimflam::kSecurityNone);
1628 EXPECT_CALL(*wifi(), DestroyIPConfigLease(service->GetStorageIdentifier())).
1629 Times(1);
1630 service->Unload();
1631}
1632
1633
Chris Masone34af2182011-08-22 11:59:36 -07001634} // namespace shill