blob: e8cf2b1ad992ff9c9c7d24eb653f36fc404b8b26 [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
mukesh agrawald835b202011-10-07 15:26:47 -070035using std::map;
Paul Stewart85aea152013-01-22 09:31:56 -080036using std::set;
Chris Masone34af2182011-08-22 11:59:36 -070037using std::string;
38using std::vector;
Paul Stewartd08f4432011-11-04 07:48:20 -070039using ::testing::_;
mukesh agrawale1d90e92012-02-15 17:36:08 -080040using ::testing::AnyNumber;
Paul Stewartd08f4432011-11-04 07:48:20 -070041using ::testing::DoAll;
Paul Stewart71a4d3b2013-01-18 18:12:56 -080042using ::testing::EndsWith;
Paul Stewart3c504012013-01-17 17:49:58 -080043using ::testing::HasSubstr;
mukesh agrawale1d90e92012-02-15 17:36:08 -080044using ::testing::Mock;
mukesh agrawal6e277772011-09-29 15:04:23 -070045using ::testing::NiceMock;
Paul Stewartd08f4432011-11-04 07:48:20 -070046using ::testing::Return;
47using ::testing::SetArgumentPointee;
48using ::testing::StrEq;
Paul Stewartd8ad3c42012-01-09 12:39:38 -080049using ::testing::StrNe;
Paul Stewart85aea152013-01-22 09:31:56 -080050using ::testing::StrictMock;
Chris Masone34af2182011-08-22 11:59:36 -070051
mukesh agrawalb20776f2012-02-10 16:00:36 -080052namespace shill {
53
Chris Masone34af2182011-08-22 11:59:36 -070054class WiFiServiceTest : public PropertyStoreTest {
55 public:
Paul Stewart3c504012013-01-17 17:49:58 -080056 WiFiServiceTest()
57 : wifi_(new NiceMock<MockWiFi>(
58 control_interface(),
59 dispatcher(),
60 metrics(),
61 manager(),
62 "wifi",
63 fake_mac,
64 0)),
65 simple_ssid_(1, 'a'),
66 simple_ssid_string_("a") {}
Chris Masone34af2182011-08-22 11:59:36 -070067 virtual ~WiFiServiceTest() {}
mukesh agrawal6e277772011-09-29 15:04:23 -070068
69 protected:
70 static const char fake_mac[];
mukesh agrawale1d90e92012-02-15 17:36:08 -080071
Gaurav Shah10109f22011-11-11 20:16:22 -080072 bool CheckConnectable(const std::string &security, const char *passphrase,
73 Service::EapCredentials *eap) {
mukesh agrawal29c13a12011-11-24 00:09:19 +000074 Error error;
Paul Stewart3c504012013-01-17 17:49:58 -080075 WiFiServiceRefPtr service = MakeSimpleService(security);
mukesh agrawal29c13a12011-11-24 00:09:19 +000076 if (passphrase)
77 service->SetPassphrase(passphrase, &error);
Gaurav Shah10109f22011-11-11 20:16:22 -080078 if (eap) {
79 service->set_eap(*eap);
80 }
mukesh agrawal29c13a12011-11-24 00:09:19 +000081 return service->connectable();
82 }
mukesh agrawale1d90e92012-02-15 17:36:08 -080083 WiFiEndpoint *MakeEndpoint(const string &ssid, const string &bssid,
mukesh agrawal43970a22013-02-15 16:00:07 -080084 uint16 frequency, int16 signal_dbm,
85 bool has_wpa_property, bool has_rsn_property) {
86 return WiFiEndpoint::MakeEndpoint(
87 NULL, wifi(), ssid, bssid, wpa_supplicant::kNetworkModeInfrastructure,
88 frequency, signal_dbm, has_wpa_property, has_rsn_property);
89 }
90 WiFiEndpoint *MakeOpenEndpoint(const string &ssid, const string &bssid,
91 uint16 frequency, int16 signal_dbm) {
mukesh agrawale1d90e92012-02-15 17:36:08 -080092 return WiFiEndpoint::MakeOpenEndpoint(
Paul Stewart3c504012013-01-17 17:49:58 -080093 NULL, wifi(), ssid, bssid, wpa_supplicant::kNetworkModeInfrastructure,
94 frequency, signal_dbm);
mukesh agrawale1d90e92012-02-15 17:36:08 -080095 }
Paul Stewart3c504012013-01-17 17:49:58 -080096 WiFiServiceRefPtr MakeSimpleService(const string &security) {
mukesh agrawale1d90e92012-02-15 17:36:08 -080097 return new WiFiService(control_interface(),
98 dispatcher(),
99 metrics(),
100 manager(),
Paul Stewart3c504012013-01-17 17:49:58 -0800101 &provider_,
102 simple_ssid_,
mukesh agrawale1d90e92012-02-15 17:36:08 -0800103 flimflam::kModeManaged,
Paul Stewart3c504012013-01-17 17:49:58 -0800104 security,
mukesh agrawale1d90e92012-02-15 17:36:08 -0800105 false);
106 }
Paul Stewart3c504012013-01-17 17:49:58 -0800107 WiFiServiceRefPtr MakeGenericService() {
108 return MakeSimpleService(flimflam::kSecurityWep);
109 }
110 void SetWiFiForService(WiFiServiceRefPtr service, WiFiRefPtr wifi) {
111 service->wifi_ = wifi;
112 }
113 WiFiServiceRefPtr MakeServiceWithWiFi(const string &security) {
114 WiFiServiceRefPtr service = MakeSimpleService(security);
115 SetWiFiForService(service, wifi_);
116 return service;
117 }
mukesh agrawale1d90e92012-02-15 17:36:08 -0800118 ServiceMockAdaptor *GetAdaptor(WiFiService *service) {
119 return dynamic_cast<ServiceMockAdaptor *>(service->adaptor());
mukesh agrawal8a3188d2011-12-01 20:56:44 +0000120 }
Paul Stewart3c504012013-01-17 17:49:58 -0800121 Error::Type TestConfigurePassphrase(const string &security,
122 const char *passphrase) {
123 WiFiServiceRefPtr service = MakeSimpleService(security);
124 KeyValueStore args;
125 if (passphrase) {
126 args.SetString(flimflam::kPassphraseProperty, passphrase);
127 }
128 Error error;
129 service->Configure(args, &error);
130 return error.type();
131 }
mukesh agrawal6e277772011-09-29 15:04:23 -0700132 scoped_refptr<MockWiFi> wifi() { return wifi_; }
Paul Stewart3c504012013-01-17 17:49:58 -0800133 MockWiFiProvider *provider() { return &provider_; }
134 string GetAnyDeviceAddress() { return WiFiService::kAnyDeviceAddress; }
135 const vector<uint8_t> &simple_ssid() { return simple_ssid_; }
136 const string &simple_ssid_string() { return simple_ssid_string_; }
mukesh agrawal6e277772011-09-29 15:04:23 -0700137
138 private:
139 scoped_refptr<MockWiFi> wifi_;
Paul Stewart3c504012013-01-17 17:49:58 -0800140 MockWiFiProvider provider_;
141 const vector<uint8_t> simple_ssid_;
142 const string simple_ssid_string_;
Chris Masone34af2182011-08-22 11:59:36 -0700143};
144
mukesh agrawal6e277772011-09-29 15:04:23 -0700145// static
146const char WiFiServiceTest::fake_mac[] = "AaBBcCDDeeFF";
147
Paul Stewart71a4d3b2013-01-18 18:12:56 -0800148MATCHER_P3(ContainsWiFiProperties, ssid, mode, security, "") {
149 string hex_ssid = base::HexEncode(ssid.data(), ssid.size());
150 return
151 arg.ContainsString(WiFiService::kStorageType) &&
152 arg.GetString(WiFiService::kStorageType) == flimflam::kTypeWifi &&
153 arg.ContainsString(WiFiService::kStorageSSID) &&
154 arg.GetString(WiFiService::kStorageSSID) == hex_ssid &&
155 arg.ContainsString(WiFiService::kStorageMode) &&
156 arg.GetString(WiFiService::kStorageMode) == mode &&
157 arg.ContainsString(WiFiService::kStorageSecurityClass) &&
158 arg.GetString(WiFiService::kStorageSecurityClass) == security;
159}
160
Paul Stewartd08f4432011-11-04 07:48:20 -0700161class WiFiServiceSecurityTest : public WiFiServiceTest {
162 public:
Paul Stewartd08f4432011-11-04 07:48:20 -0700163 bool TestStorageSecurityIs(WiFiServiceRefPtr wifi_service,
164 const string &security) {
165 string id = wifi_service->GetStorageIdentifier();
Paul Stewart3c504012013-01-17 17:49:58 -0800166 size_t mac_pos = id.find(StringToLowerASCII(GetAnyDeviceAddress()));
Paul Stewartd08f4432011-11-04 07:48:20 -0700167 EXPECT_NE(mac_pos, string::npos);
168 size_t mode_pos = id.find(string(flimflam::kModeManaged), mac_pos);
169 EXPECT_NE(mode_pos, string::npos);
170 return id.find(string(security), mode_pos) != string::npos;
171 }
172
173 // Test that a service that is created with security |from_security|
174 // gets by default a storage identifier with |to_security| as its
Paul Stewart71a4d3b2013-01-18 18:12:56 -0800175 // security component, and that when saved, it sets the Security
176 // property in to |to_security| as well.
Paul Stewartd08f4432011-11-04 07:48:20 -0700177 bool TestStorageMapping(const string &from_security,
178 const string &to_security) {
Paul Stewart3c504012013-01-17 17:49:58 -0800179 WiFiServiceRefPtr wifi_service = MakeSimpleService(from_security);
Paul Stewart71a4d3b2013-01-18 18:12:56 -0800180 NiceMock<MockStore> mock_store;
181 EXPECT_CALL(mock_store, SetString(_, _, _)).WillRepeatedly(Return(true));
182 EXPECT_CALL(mock_store,
183 SetString(_, WiFiService::kStorageSecurity, from_security))
184 .Times(1);
185 EXPECT_CALL(mock_store,
186 SetString(_, WiFiService::kStorageSecurityClass, to_security))
187 .Times(1);
188 wifi_service->Save(&mock_store);
Paul Stewartd08f4432011-11-04 07:48:20 -0700189 return TestStorageSecurityIs(wifi_service, to_security);
190 }
191
192 // Test whether a service of type |service_security| can load from a
193 // storage interface containing an entry for |storage_security|.
194 // Make sure the result meets |expectation|. If |expectation| is
195 // true, also make sure the service storage identifier changes to
196 // match |storage_security|.
197 bool TestLoadMapping(const string &service_security,
198 const string &storage_security,
199 bool expectation) {
Paul Stewart3c504012013-01-17 17:49:58 -0800200 WiFiServiceRefPtr wifi_service = MakeSimpleService(service_security);
Paul Stewartd08f4432011-11-04 07:48:20 -0700201 NiceMock<MockStore> mock_store;
Paul Stewart71a4d3b2013-01-18 18:12:56 -0800202 EXPECT_CALL(mock_store, GetGroupsWithProperties(_))
203 .WillRepeatedly(Return(set<string>()));
204 const string kStorageId = "storage_id";
205 EXPECT_CALL(mock_store, ContainsGroup(kStorageId))
Paul Stewartd08f4432011-11-04 07:48:20 -0700206 .WillRepeatedly(Return(true));
Paul Stewart71a4d3b2013-01-18 18:12:56 -0800207 set<string> groups;
208 groups.insert(kStorageId);
209 EXPECT_CALL(mock_store, GetGroupsWithProperties(
210 ContainsWiFiProperties(wifi_service->ssid(),
211 flimflam::kModeManaged,
212 storage_security)))
213 .WillRepeatedly(Return(groups));
Paul Stewartd08f4432011-11-04 07:48:20 -0700214 bool is_loadable = wifi_service->IsLoadableFrom(&mock_store);
215 EXPECT_EQ(expectation, is_loadable);
216 bool is_loaded = wifi_service->Load(&mock_store);
217 EXPECT_EQ(expectation, is_loaded);
218
219 if (expectation != is_loadable || expectation != is_loaded) {
220 return false;
221 } else if (!expectation) {
222 return true;
223 } else {
Paul Stewart71a4d3b2013-01-18 18:12:56 -0800224 return wifi_service->GetStorageIdentifier() == kStorageId;
Paul Stewartd08f4432011-11-04 07:48:20 -0700225 }
226 }
227};
228
mukesh agrawale1d90e92012-02-15 17:36:08 -0800229class WiFiServiceUpdateFromEndpointsTest : public WiFiServiceTest {
230 public:
231 WiFiServiceUpdateFromEndpointsTest()
232 : kOkEndpointStrength(WiFiService::SignalToStrength(kOkEndpointSignal)),
233 kBadEndpointStrength(WiFiService::SignalToStrength(kBadEndpointSignal)),
234 kGoodEndpointStrength(
235 WiFiService::SignalToStrength(kGoodEndpointSignal)),
236 service(MakeGenericService()),
237 adaptor(*GetAdaptor(service)) {
mukesh agrawal43970a22013-02-15 16:00:07 -0800238 ok_endpoint = MakeOpenEndpoint(
Paul Stewart3c504012013-01-17 17:49:58 -0800239 simple_ssid_string(), kOkEndpointBssId, kOkEndpointFrequency,
240 kOkEndpointSignal);
mukesh agrawal43970a22013-02-15 16:00:07 -0800241 good_endpoint = MakeOpenEndpoint(
Paul Stewart3c504012013-01-17 17:49:58 -0800242 simple_ssid_string(), kGoodEndpointBssId, kGoodEndpointFrequency,
243 kGoodEndpointSignal);
mukesh agrawal43970a22013-02-15 16:00:07 -0800244 bad_endpoint = MakeOpenEndpoint(
Paul Stewart3c504012013-01-17 17:49:58 -0800245 simple_ssid_string(), kBadEndpointBssId, kBadEndpointFrequency,
246 kBadEndpointSignal);
mukesh agrawale1d90e92012-02-15 17:36:08 -0800247 }
248
249 protected:
250 static const uint16 kOkEndpointFrequency = 2422;
251 static const uint16 kBadEndpointFrequency = 2417;
252 static const uint16 kGoodEndpointFrequency = 2412;
253 static const int16 kOkEndpointSignal = -50;
254 static const int16 kBadEndpointSignal = -75;
255 static const int16 kGoodEndpointSignal = -25;
mukesh agrawal923f14f2012-06-04 16:46:08 -0700256 static const char *kOkEndpointBssId;
257 static const char *kGoodEndpointBssId;
258 static const char *kBadEndpointBssId;
mukesh agrawale1d90e92012-02-15 17:36:08 -0800259 // Can't be both static and const (because initialization requires a
260 // function call). So choose to be just const.
261 const uint8 kOkEndpointStrength;
262 const uint8 kBadEndpointStrength;
263 const uint8 kGoodEndpointStrength;
264 WiFiEndpointRefPtr ok_endpoint;
265 WiFiEndpointRefPtr bad_endpoint;
266 WiFiEndpointRefPtr good_endpoint;
267 WiFiServiceRefPtr service;
268 ServiceMockAdaptor &adaptor;
269};
270
mukesh agrawal923f14f2012-06-04 16:46:08 -0700271const char *WiFiServiceUpdateFromEndpointsTest::kOkEndpointBssId =
272 "00:00:00:00:00:01";
273const char *WiFiServiceUpdateFromEndpointsTest::kGoodEndpointBssId =
274 "00:00:00:00:00:02";
275const char *WiFiServiceUpdateFromEndpointsTest::kBadEndpointBssId =
276 "00:00:00:00:00:03";
mukesh agrawale1d90e92012-02-15 17:36:08 -0800277
Paul Stewart85aea152013-01-22 09:31:56 -0800278class WiFiServiceFixupStorageTest : public WiFiServiceTest {
279 protected:
280 void AddGroup(string group_name) {
281 groups_.insert(group_name);
282 }
283
Paul Stewart71a4d3b2013-01-18 18:12:56 -0800284 void AddServiceEntry(bool has_type, bool has_mode, bool has_security,
285 bool has_security_class) {
Paul Stewart85aea152013-01-22 09:31:56 -0800286 int index = groups_.size();
287 string id = base::StringPrintf("%s_%d_%d_%s_%s", flimflam::kTypeWifi,
288 index, index, flimflam::kModeManaged,
Paul Stewart71a4d3b2013-01-18 18:12:56 -0800289 flimflam::kSecurityWpa);
Paul Stewart85aea152013-01-22 09:31:56 -0800290 AddGroup(id);
291 EXPECT_CALL(store_, GetString(id, WiFiService::kStorageType, _))
292 .WillOnce(Return(has_type));
293 if (!has_type) {
294 EXPECT_CALL(store_, SetString(id, WiFiService::kStorageType,
295 flimflam::kTypeWifi));
296 }
297 EXPECT_CALL(store_, GetString(id, WiFiService::kStorageMode, _))
298 .WillOnce(Return(has_mode));
299 if (!has_mode) {
300 EXPECT_CALL(store_, SetString(id, WiFiService::kStorageMode,
301 flimflam::kModeManaged));
302 }
303 EXPECT_CALL(store_, GetString(id, WiFiService::kStorageSecurity, _))
304 .WillOnce(Return(has_security));
305 if (!has_security) {
306 EXPECT_CALL(store_, SetString(id, WiFiService::kStorageSecurity,
Paul Stewart71a4d3b2013-01-18 18:12:56 -0800307 flimflam::kSecurityWpa));
308 }
309 EXPECT_CALL(store_, GetString(id, WiFiService::kStorageSecurityClass, _))
310 .WillOnce(Return(has_security_class));
311 if (!has_security_class) {
312 EXPECT_CALL(store_, SetString(id, WiFiService::kStorageSecurityClass,
313 flimflam::kSecurityPsk));
Paul Stewart85aea152013-01-22 09:31:56 -0800314 }
315 }
316
317 bool FixupServiceEntries() {
318 EXPECT_CALL(store_, GetGroups()).WillOnce(Return(groups_));
319 return WiFiService::FixupServiceEntries(&store_);
320 }
321
322 private:
323 StrictMock<MockStore> store_;
324 set<string> groups_;
325};
326
Chris Masone34af2182011-08-22 11:59:36 -0700327TEST_F(WiFiServiceTest, StorageId) {
Paul Stewart3c504012013-01-17 17:49:58 -0800328 WiFiServiceRefPtr wifi_service = MakeSimpleService(flimflam::kSecurityNone);
Chris Masone9d779932011-08-25 16:33:41 -0700329 string id = wifi_service->GetStorageIdentifier();
Chris Masone34af2182011-08-22 11:59:36 -0700330 for (uint i = 0; i < id.length(); ++i) {
331 EXPECT_TRUE(id[i] == '_' ||
332 isxdigit(id[i]) ||
333 (isalpha(id[i]) && islower(id[i])));
334 }
Paul Stewart3c504012013-01-17 17:49:58 -0800335 size_t mac_pos = id.find(StringToLowerASCII(GetAnyDeviceAddress()));
Chris Masone34af2182011-08-22 11:59:36 -0700336 EXPECT_NE(mac_pos, string::npos);
337 EXPECT_NE(id.find(string(flimflam::kModeManaged), mac_pos), string::npos);
338}
339
Gaurav Shahda6218a2011-11-11 12:09:33 -0800340// Make sure the passphrase is registered as a write only property
341// by reading and comparing all string properties returned on the store.
342TEST_F(WiFiServiceTest, PassphraseWriteOnly) {
Paul Stewart3c504012013-01-17 17:49:58 -0800343 WiFiServiceRefPtr wifi_service = MakeSimpleService(flimflam::kSecurityWpa);
Gaurav Shahda6218a2011-11-11 12:09:33 -0800344 ReadablePropertyConstIterator<string> it =
345 (wifi_service->store()).GetStringPropertiesIter();
346 for( ; !it.AtEnd(); it.Advance())
347 EXPECT_NE(it.Key(), flimflam::kPassphraseProperty);
348}
349
Thieu Lef7709452011-11-15 01:13:19 +0000350// Make sure setting the passphrase via D-Bus Service.SetProperty validates
351// the passphrase.
352TEST_F(WiFiServiceTest, PassphraseSetPropertyValidation) {
353 // We only spot check two password cases here to make sure the
354 // SetProperty code path does validation. We're not going to exhaustively
355 // test for all types of passwords.
Paul Stewart3c504012013-01-17 17:49:58 -0800356 WiFiServiceRefPtr wifi_service = MakeSimpleService(flimflam::kSecurityWep);
Thieu Lef7709452011-11-15 01:13:19 +0000357 Error error;
358 EXPECT_TRUE(wifi_service->mutable_store()->SetStringProperty(
359 flimflam::kPassphraseProperty, "0:abcde", &error));
360 EXPECT_FALSE(wifi_service->mutable_store()->SetStringProperty(
361 flimflam::kPassphraseProperty, "invalid", &error));
362 EXPECT_EQ(Error::kInvalidPassphrase, error.type());
363}
364
365TEST_F(WiFiServiceTest, PassphraseSetPropertyOpenNetwork) {
Paul Stewart3c504012013-01-17 17:49:58 -0800366 WiFiServiceRefPtr wifi_service = MakeSimpleService(flimflam::kSecurityNone);
Thieu Lef7709452011-11-15 01:13:19 +0000367 Error error;
368 EXPECT_FALSE(wifi_service->mutable_store()->SetStringProperty(
369 flimflam::kPassphraseProperty, "invalid", &error));
370 EXPECT_EQ(Error::kNotSupported, error.type());
371}
372
mukesh agrawald835b202011-10-07 15:26:47 -0700373TEST_F(WiFiServiceTest, NonUTF8SSID) {
374 vector<uint8_t> ssid;
375
376 ssid.push_back(0xff); // not a valid UTF-8 byte-sequence
377 WiFiServiceRefPtr wifi_service = new WiFiService(control_interface(),
378 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -0800379 metrics(),
mukesh agrawald835b202011-10-07 15:26:47 -0700380 manager(),
Paul Stewart3c504012013-01-17 17:49:58 -0800381 provider(),
mukesh agrawald835b202011-10-07 15:26:47 -0700382 ssid,
383 flimflam::kModeManaged,
Paul Stewartced6a0b2011-11-08 15:32:04 -0800384 flimflam::kSecurityNone,
385 false);
mukesh agrawald835b202011-10-07 15:26:47 -0700386 map<string, ::DBus::Variant> properties;
387 // if service doesn't propertly sanitize SSID, this will generate SIGABRT.
388 DBusAdaptor::GetProperties(wifi_service->store(), &properties, NULL);
389}
390
Gaurav Shahda6218a2011-11-11 12:09:33 -0800391MATCHER(WPASecurityArgs, "") {
392 return ContainsKey(arg, wpa_supplicant::kPropertySecurityProtocol) &&
393 ContainsKey(arg, wpa_supplicant::kPropertyPreSharedKey);
394}
395
Paul Stewarta5e7d5f2013-01-09 18:06:15 -0800396MATCHER(WPA80211wSecurityArgs, "") {
397 return ContainsKey(arg, wpa_supplicant::kPropertySecurityProtocol) &&
398 ContainsKey(arg, wpa_supplicant::kPropertyPreSharedKey) &&
399 ContainsKey(arg, wpa_supplicant::kNetworkPropertyIeee80211w);
400}
401
Gaurav Shah10109f22011-11-11 20:16:22 -0800402MATCHER(EAPSecurityArgs, "") {
403 return ContainsKey(arg, wpa_supplicant::kNetworkPropertyEapIdentity) &&
404 ContainsKey(arg, wpa_supplicant::kNetworkPropertyCaPath);
405}
406
Paul Stewarte2d7c502012-07-16 16:35:10 -0700407MATCHER_P(FrequencyArg, has_arg, "") {
408 return has_arg ==
409 ContainsKey(arg, wpa_supplicant::kNetworkPropertyFrequency);
410}
411
mukesh agrawalf2fd7452011-10-03 16:38:47 -0700412TEST_F(WiFiServiceTest, ConnectTaskWPA) {
Paul Stewart3c504012013-01-17 17:49:58 -0800413 WiFiServiceRefPtr wifi_service = MakeServiceWithWiFi(flimflam::kSecurityWpa);
mukesh agrawal6e277772011-09-29 15:04:23 -0700414 EXPECT_CALL(*wifi(),
415 ConnectTo(wifi_service.get(), WPASecurityArgs()));
Wade Guthrie005bd342012-05-02 09:37:07 -0700416 Error error;
417 wifi_service->SetPassphrase("0:mumblemumblem", &error);
Eric Shienbrood9a245532012-03-07 14:20:39 -0500418 wifi_service->Connect(NULL);
mukesh agrawal6e277772011-09-29 15:04:23 -0700419}
420
mukesh agrawalf2fd7452011-10-03 16:38:47 -0700421TEST_F(WiFiServiceTest, ConnectTaskRSN) {
Paul Stewart3c504012013-01-17 17:49:58 -0800422 WiFiServiceRefPtr wifi_service = MakeServiceWithWiFi(flimflam::kSecurityRsn);
mukesh agrawalf2fd7452011-10-03 16:38:47 -0700423 EXPECT_CALL(*wifi(),
424 ConnectTo(wifi_service.get(), WPASecurityArgs()));
Wade Guthrie005bd342012-05-02 09:37:07 -0700425 Error error;
426 wifi_service->SetPassphrase("0:mumblemumblem", &error);
Eric Shienbrood9a245532012-03-07 14:20:39 -0500427 wifi_service->Connect(NULL);
mukesh agrawalf2fd7452011-10-03 16:38:47 -0700428}
429
Christopher Wiley1ce658d2012-10-10 10:02:03 -0700430TEST_F(WiFiServiceTest, ConnectConditions) {
431 Error error;
Paul Stewart3c504012013-01-17 17:49:58 -0800432 WiFiServiceRefPtr wifi_service = MakeServiceWithWiFi(flimflam::kSecurityNone);
Christopher Wiley1ce658d2012-10-10 10:02:03 -0700433 scoped_refptr<MockProfile> mock_profile(
434 new NiceMock<MockProfile>(control_interface(), manager()));
435 wifi_service->set_profile(mock_profile);
436 // With nothing else going on, the service should attempt to connect.
437 EXPECT_CALL(*wifi(), ConnectTo(wifi_service.get(), _));
438 wifi_service->Connect(&error);
439 Mock::VerifyAndClearExpectations(wifi());
440
441 // But if we're already "connecting" or "connected" then we shouldn't attempt
442 // again.
443 EXPECT_CALL(*wifi(),
444 ConnectTo(wifi_service.get(), _)).Times(0);
445 wifi_service->SetState(Service::kStateAssociating);
446 wifi_service->Connect(&error);
447 wifi_service->SetState(Service::kStateConfiguring);
448 wifi_service->Connect(&error);
449 wifi_service->SetState(Service::kStateConnected);
450 wifi_service->Connect(&error);
451 wifi_service->SetState(Service::kStatePortal);
452 wifi_service->Connect(&error);
453 wifi_service->SetState(Service::kStateOnline);
454 wifi_service->Connect(&error);
455 Mock::VerifyAndClearExpectations(wifi());
456}
457
Gaurav Shahf8721ee2011-11-07 09:12:46 -0800458TEST_F(WiFiServiceTest, ConnectTaskPSK) {
Paul Stewart3c504012013-01-17 17:49:58 -0800459 WiFiServiceRefPtr wifi_service = MakeServiceWithWiFi(flimflam::kSecurityPsk);
Gaurav Shahf8721ee2011-11-07 09:12:46 -0800460 EXPECT_CALL(*wifi(),
461 ConnectTo(wifi_service.get(), WPASecurityArgs()));
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
mukesh agrawal43970a22013-02-15 16:00:07 -08001444TEST_F(WiFiServiceTest, UpdateSecurity) {
1445 // Cleartext and pre-shared-key crypto.
1446 {
1447 WiFiServiceRefPtr service = MakeSimpleService(flimflam::kSecurityNone);
1448 EXPECT_EQ(Service::kCryptoNone, service->crypto_algorithm());
1449 EXPECT_FALSE(service->key_rotation());
1450 EXPECT_FALSE(service->endpoint_auth());
1451 }
1452 {
1453 WiFiServiceRefPtr service = MakeSimpleService(flimflam::kSecurityWep);
1454 EXPECT_EQ(Service::kCryptoRc4, service->crypto_algorithm());
1455 EXPECT_FALSE(service->key_rotation());
1456 EXPECT_FALSE(service->endpoint_auth());
1457 }
1458 {
1459 WiFiServiceRefPtr service = MakeSimpleService(flimflam::kSecurityPsk);
1460 EXPECT_EQ(Service::kCryptoRc4, service->crypto_algorithm());
1461 EXPECT_TRUE(service->key_rotation());
1462 EXPECT_FALSE(service->endpoint_auth());
1463 }
1464 {
1465 WiFiServiceRefPtr service = MakeSimpleService(flimflam::kSecurityWpa);
1466 EXPECT_EQ(Service::kCryptoRc4, service->crypto_algorithm());
1467 EXPECT_TRUE(service->key_rotation());
1468 EXPECT_FALSE(service->endpoint_auth());
1469 }
1470 {
1471 WiFiServiceRefPtr service = MakeSimpleService(flimflam::kSecurityRsn);
1472 EXPECT_EQ(Service::kCryptoAes, service->crypto_algorithm());
1473 EXPECT_TRUE(service->key_rotation());
1474 EXPECT_FALSE(service->endpoint_auth());
1475 }
1476
1477 // Crypto with 802.1X key management.
1478 {
1479 // WEP
1480 WiFiServiceRefPtr service = MakeSimpleService(flimflam::kSecurityWep);
1481 service->SetEAPKeyManagement("IEEE8021X");
1482 EXPECT_EQ(Service::kCryptoRc4, service->crypto_algorithm());
1483 EXPECT_TRUE(service->key_rotation());
1484 EXPECT_TRUE(service->endpoint_auth());
1485 }
1486 {
1487 // WPA
1488 WiFiServiceRefPtr service = MakeSimpleService(flimflam::kSecurity8021x);
1489 WiFiEndpointRefPtr endpoint =
1490 MakeEndpoint("a", "00:00:00:00:00:01", 0, 0, true, false);
1491 service->AddEndpoint(endpoint);
1492 EXPECT_EQ(Service::kCryptoRc4, service->crypto_algorithm());
1493 EXPECT_TRUE(service->key_rotation());
1494 EXPECT_TRUE(service->endpoint_auth());
1495 }
1496 {
1497 // RSN
1498 WiFiServiceRefPtr service = MakeSimpleService(flimflam::kSecurity8021x);
1499 WiFiEndpointRefPtr endpoint =
1500 MakeEndpoint("a", "00:00:00:00:00:01", 0, 0, false, true);
1501 service->AddEndpoint(endpoint);
1502 EXPECT_EQ(Service::kCryptoAes, service->crypto_algorithm());
1503 EXPECT_TRUE(service->key_rotation());
1504 EXPECT_TRUE(service->endpoint_auth());
1505 }
1506 {
1507 // AP supports both WPA and RSN.
1508 WiFiServiceRefPtr service = MakeSimpleService(flimflam::kSecurity8021x);
1509 WiFiEndpointRefPtr endpoint =
1510 MakeEndpoint("a", "00:00:00:00:00:01", 0, 0, true, true);
1511 service->AddEndpoint(endpoint);
1512 EXPECT_EQ(Service::kCryptoAes, service->crypto_algorithm());
1513 EXPECT_TRUE(service->key_rotation());
1514 EXPECT_TRUE(service->endpoint_auth());
1515 }
1516}
1517
1518TEST_F(WiFiServiceTest, ComputeCipher8021x) {
1519 // No endpoints.
1520 {
1521 const set<WiFiEndpointConstRefPtr> endpoints;
1522 EXPECT_EQ(Service::kCryptoNone,
1523 WiFiService::ComputeCipher8021x(endpoints));
1524 }
1525
1526 // Single endpoint, various configs.
1527 {
1528 set<WiFiEndpointConstRefPtr> endpoints;
1529 endpoints.insert(
1530 MakeEndpoint("a", "00:00:00:00:00:01", 0, 0, false, false));
1531 EXPECT_EQ(Service::kCryptoNone,
1532 WiFiService::ComputeCipher8021x(endpoints));
1533 }
1534 {
1535 set<WiFiEndpointConstRefPtr> endpoints;
1536 endpoints.insert(
1537 MakeEndpoint("a", "00:00:00:00:00:01", 0, 0, true, false));
1538 EXPECT_EQ(Service::kCryptoRc4,
1539 WiFiService::ComputeCipher8021x(endpoints));
1540 }
1541 {
1542 set<WiFiEndpointConstRefPtr> endpoints;
1543 endpoints.insert(
1544 MakeEndpoint("a", "00:00:00:00:00:01", 0, 0, false, true));
1545 EXPECT_EQ(Service::kCryptoAes,
1546 WiFiService::ComputeCipher8021x(endpoints));
1547 }
1548 {
1549 set<WiFiEndpointConstRefPtr> endpoints;
1550 endpoints.insert(
1551 MakeEndpoint("a", "00:00:00:00:00:01", 0, 0, true, true));
1552 EXPECT_EQ(Service::kCryptoAes,
1553 WiFiService::ComputeCipher8021x(endpoints));
1554 }
1555
1556 // Multiple endpoints.
1557 {
1558 set<WiFiEndpointConstRefPtr> endpoints;
1559 endpoints.insert(
1560 MakeEndpoint("a", "00:00:00:00:00:01", 0, 0, false, false));
1561 endpoints.insert(
1562 MakeEndpoint("a", "00:00:00:00:00:02", 0, 0, false, false));
1563 EXPECT_EQ(Service::kCryptoNone,
1564 WiFiService::ComputeCipher8021x(endpoints));
1565 }
1566 {
1567 set<WiFiEndpointConstRefPtr> endpoints;
1568 endpoints.insert(
1569 MakeEndpoint("a", "00:00:00:00:00:01", 0, 0, false, false));
1570 endpoints.insert(
1571 MakeEndpoint("a", "00:00:00:00:00:02", 0, 0, true, false));
1572 EXPECT_EQ(Service::kCryptoNone,
1573 WiFiService::ComputeCipher8021x(endpoints));
1574 }
1575 {
1576 set<WiFiEndpointConstRefPtr> endpoints;
1577 endpoints.insert(
1578 MakeEndpoint("a", "00:00:00:00:00:01", 0, 0, true, false));
1579 endpoints.insert(
1580 MakeEndpoint("a", "00:00:00:00:00:02", 0, 0, true, false));
1581 EXPECT_EQ(Service::kCryptoRc4,
1582 WiFiService::ComputeCipher8021x(endpoints));
1583 }
1584 {
1585 set<WiFiEndpointConstRefPtr> endpoints;
1586 endpoints.insert(
1587 MakeEndpoint("a", "00:00:00:00:00:01", 0, 0, true, false));
1588 endpoints.insert(
1589 MakeEndpoint("a", "00:00:00:00:00:02", 0, 0, false, true));
1590 EXPECT_EQ(Service::kCryptoRc4,
1591 WiFiService::ComputeCipher8021x(endpoints));
1592 }
1593 {
1594 set<WiFiEndpointConstRefPtr> endpoints;
1595 endpoints.insert(
1596 MakeEndpoint("a", "00:00:00:00:00:01", 0, 0, false, true));
1597 endpoints.insert(
1598 MakeEndpoint("a", "00:00:00:00:00:02", 0, 0, false, true));
1599 EXPECT_EQ(Service::kCryptoAes,
1600 WiFiService::ComputeCipher8021x(endpoints));
1601 }
1602 {
1603 set<WiFiEndpointConstRefPtr> endpoints;
1604 endpoints.insert(
1605 MakeEndpoint("a", "00:00:00:00:00:01", 0, 0, true, true));
1606 endpoints.insert(
1607 MakeEndpoint("a", "00:00:00:00:00:02", 0, 0, true, true));
1608 EXPECT_EQ(Service::kCryptoAes,
1609 WiFiService::ComputeCipher8021x(endpoints));
1610 }
1611}
1612
Chris Masone34af2182011-08-22 11:59:36 -07001613} // namespace shill