blob: 4b18730435347a52d21d17ebbf2dd056d33d81d6 [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
Paul Stewart4108db92013-03-11 12:13:24 -0700391MATCHER(PSKSecurityArgs, "") {
Gaurav Shahda6218a2011-11-11 12:09:33 -0800392 return ContainsKey(arg, wpa_supplicant::kPropertySecurityProtocol) &&
Paul Stewart4108db92013-03-11 12:13:24 -0700393 arg.find(wpa_supplicant::kPropertySecurityProtocol)->second.
394 reader().get_string() == string("WPA RSN") &&
Gaurav Shahda6218a2011-11-11 12:09:33 -0800395 ContainsKey(arg, wpa_supplicant::kPropertyPreSharedKey);
396}
397
Paul Stewarta5e7d5f2013-01-09 18:06:15 -0800398MATCHER(WPA80211wSecurityArgs, "") {
399 return ContainsKey(arg, wpa_supplicant::kPropertySecurityProtocol) &&
400 ContainsKey(arg, wpa_supplicant::kPropertyPreSharedKey) &&
401 ContainsKey(arg, wpa_supplicant::kNetworkPropertyIeee80211w);
402}
403
Gaurav Shah10109f22011-11-11 20:16:22 -0800404MATCHER(EAPSecurityArgs, "") {
405 return ContainsKey(arg, wpa_supplicant::kNetworkPropertyEapIdentity) &&
406 ContainsKey(arg, wpa_supplicant::kNetworkPropertyCaPath);
407}
408
Paul Stewarte2d7c502012-07-16 16:35:10 -0700409MATCHER_P(FrequencyArg, has_arg, "") {
410 return has_arg ==
411 ContainsKey(arg, wpa_supplicant::kNetworkPropertyFrequency);
412}
413
mukesh agrawalf2fd7452011-10-03 16:38:47 -0700414TEST_F(WiFiServiceTest, ConnectTaskWPA) {
Paul Stewart3c504012013-01-17 17:49:58 -0800415 WiFiServiceRefPtr wifi_service = MakeServiceWithWiFi(flimflam::kSecurityWpa);
Paul Stewart4108db92013-03-11 12:13:24 -0700416 EXPECT_CALL(*wifi(), ConnectTo(wifi_service.get(), PSKSecurityArgs()));
Wade Guthrie005bd342012-05-02 09:37:07 -0700417 Error error;
418 wifi_service->SetPassphrase("0:mumblemumblem", &error);
Eric Shienbrood9a245532012-03-07 14:20:39 -0500419 wifi_service->Connect(NULL);
mukesh agrawal6e277772011-09-29 15:04:23 -0700420}
421
mukesh agrawalf2fd7452011-10-03 16:38:47 -0700422TEST_F(WiFiServiceTest, ConnectTaskRSN) {
Paul Stewart3c504012013-01-17 17:49:58 -0800423 WiFiServiceRefPtr wifi_service = MakeServiceWithWiFi(flimflam::kSecurityRsn);
Paul Stewart4108db92013-03-11 12:13:24 -0700424 EXPECT_CALL(*wifi(), ConnectTo(wifi_service.get(), PSKSecurityArgs()));
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(
Thieu Le5133b712013-02-19 14:47:21 -0800434 new NiceMock<MockProfile>(control_interface(), metrics(), manager()));
Christopher Wiley1ce658d2012-10-10 10:02:03 -0700435 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);
Paul Stewart4108db92013-03-11 12:13:24 -0700460 EXPECT_CALL(*wifi(), ConnectTo(wifi_service.get(), PSKSecurityArgs()));
Wade Guthrie005bd342012-05-02 09:37:07 -0700461 Error error;
462 wifi_service->SetPassphrase("0:mumblemumblem", &error);
Eric Shienbrood9a245532012-03-07 14:20:39 -0500463 wifi_service->Connect(NULL);
Gaurav Shahf8721ee2011-11-07 09:12:46 -0800464}
465
Gaurav Shah10109f22011-11-11 20:16:22 -0800466TEST_F(WiFiServiceTest, ConnectTask8021x) {
Paul Stewart3c504012013-01-17 17:49:58 -0800467 WiFiServiceRefPtr service = MakeServiceWithWiFi(flimflam::kSecurity8021x);
Gaurav Shah10109f22011-11-11 20:16:22 -0800468 Service::EapCredentials eap;
469 eap.identity = "identity";
Wade Guthrie005bd342012-05-02 09:37:07 -0700470 eap.password = "mumble";
Paul Stewart3c504012013-01-17 17:49:58 -0800471 service->set_eap(eap);
472 EXPECT_CALL(*wifi(), ConnectTo(service.get(), EAPSecurityArgs()));
473 service->Connect(NULL);
Gaurav Shah10109f22011-11-11 20:16:22 -0800474}
475
Paul Stewarte2d7c502012-07-16 16:35:10 -0700476TEST_F(WiFiServiceTest, ConnectTaskAdHocFrequency) {
477 vector<uint8_t> ssid(1, 'a');
478 WiFiEndpointRefPtr endpoint_nofreq =
mukesh agrawal43970a22013-02-15 16:00:07 -0800479 MakeOpenEndpoint("a", "00:00:00:00:00:01", 0, 0);
Paul Stewarte2d7c502012-07-16 16:35:10 -0700480 WiFiEndpointRefPtr endpoint_freq =
mukesh agrawal43970a22013-02-15 16:00:07 -0800481 MakeOpenEndpoint("a", "00:00:00:00:00:02", 2412, 0);
Paul Stewarte2d7c502012-07-16 16:35:10 -0700482
Paul Stewart3c504012013-01-17 17:49:58 -0800483 WiFiServiceRefPtr wifi_service = MakeServiceWithWiFi(flimflam::kSecurityNone);
Paul Stewarte2d7c502012-07-16 16:35:10 -0700484 wifi_service->AddEndpoint(endpoint_freq);
485 EXPECT_CALL(*wifi(),
486 ConnectTo(wifi_service.get(), FrequencyArg(false)));
487 wifi_service->Connect(NULL);
488
489 wifi_service = new WiFiService(control_interface(),
490 dispatcher(),
491 metrics(),
492 manager(),
Paul Stewart3c504012013-01-17 17:49:58 -0800493 provider(),
Paul Stewarte2d7c502012-07-16 16:35:10 -0700494 ssid,
495 flimflam::kModeAdhoc,
496 flimflam::kSecurityNone,
497 false);
498 EXPECT_CALL(*wifi(),
499 ConnectTo(wifi_service.get(), FrequencyArg(false)));
Paul Stewart3c504012013-01-17 17:49:58 -0800500 SetWiFiForService(wifi_service, wifi());
Paul Stewarte2d7c502012-07-16 16:35:10 -0700501 wifi_service->Connect(NULL);
502
503 wifi_service = new WiFiService(control_interface(),
504 dispatcher(),
505 metrics(),
506 manager(),
Paul Stewart3c504012013-01-17 17:49:58 -0800507 provider(),
Paul Stewarte2d7c502012-07-16 16:35:10 -0700508 ssid,
509 flimflam::kModeAdhoc,
510 flimflam::kSecurityNone,
511 false);
512 wifi_service->AddEndpoint(endpoint_nofreq);
Paul Stewart3c504012013-01-17 17:49:58 -0800513 SetWiFiForService(wifi_service, wifi());
Paul Stewarte2d7c502012-07-16 16:35:10 -0700514 EXPECT_CALL(*wifi(),
515 ConnectTo(wifi_service.get(), FrequencyArg(false)));
516 wifi_service->Connect(NULL);
517
518 wifi_service = new WiFiService(control_interface(),
519 dispatcher(),
520 metrics(),
521 manager(),
Paul Stewart3c504012013-01-17 17:49:58 -0800522 provider(),
Paul Stewarte2d7c502012-07-16 16:35:10 -0700523 ssid,
524 flimflam::kModeAdhoc,
525 flimflam::kSecurityNone,
526 false);
527 wifi_service->AddEndpoint(endpoint_freq);
Paul Stewart3c504012013-01-17 17:49:58 -0800528 SetWiFiForService(wifi_service, wifi());
Paul Stewarte2d7c502012-07-16 16:35:10 -0700529 EXPECT_CALL(*wifi(),
530 ConnectTo(wifi_service.get(), FrequencyArg(true)));
531 wifi_service->Connect(NULL);
532}
533
Paul Stewarta5e7d5f2013-01-09 18:06:15 -0800534TEST_F(WiFiServiceTest, ConnectTaskWPA80211w) {
Paul Stewart3c504012013-01-17 17:49:58 -0800535 WiFiServiceRefPtr wifi_service = MakeServiceWithWiFi(flimflam::kSecurityPsk);
mukesh agrawal43970a22013-02-15 16:00:07 -0800536 WiFiEndpointRefPtr endpoint =
537 MakeOpenEndpoint("a", "00:00:00:00:00:01", 0, 0);
Paul Stewarta5e7d5f2013-01-09 18:06:15 -0800538 endpoint->ieee80211w_required_ = true;
539 wifi_service->AddEndpoint(endpoint);
540 Error error;
541 wifi_service->SetPassphrase("0:mumblemumblem", &error);
542 EXPECT_CALL(*wifi(),
543 ConnectTo(wifi_service.get(), WPA80211wSecurityArgs()));
544 wifi_service->Connect(NULL);
545}
546
Thieu Lef4cbda92011-11-10 23:41:24 +0000547MATCHER(WEPSecurityArgsKeyIndex0, "") {
548 return ContainsKey(arg, wpa_supplicant::kPropertyAuthAlg) &&
549 ContainsKey(arg, wpa_supplicant::kPropertyWEPKey + std::string("0")) &&
550 ContainsKey(arg, wpa_supplicant::kPropertyWEPTxKeyIndex) &&
551 (arg.find(wpa_supplicant::kPropertyWEPTxKeyIndex)->second.
552 reader().get_uint32() == 0);
553}
554
555MATCHER(WEPSecurityArgsKeyIndex1, "") {
556 return ContainsKey(arg, wpa_supplicant::kPropertyAuthAlg) &&
557 ContainsKey(arg, wpa_supplicant::kPropertyWEPKey + std::string("1")) &&
558 ContainsKey(arg, wpa_supplicant::kPropertyWEPTxKeyIndex) &&
559 (arg.find(wpa_supplicant::kPropertyWEPTxKeyIndex)->second.
560 reader().get_uint32() == 1);
561}
562
563MATCHER(WEPSecurityArgsKeyIndex2, "") {
564 return ContainsKey(arg, wpa_supplicant::kPropertyAuthAlg) &&
565 ContainsKey(arg, wpa_supplicant::kPropertyWEPKey + std::string("2")) &&
566 ContainsKey(arg, wpa_supplicant::kPropertyWEPTxKeyIndex) &&
567 (arg.find(wpa_supplicant::kPropertyWEPTxKeyIndex)->second.
568 reader().get_uint32() == 2);
569}
570
571MATCHER(WEPSecurityArgsKeyIndex3, "") {
572 return ContainsKey(arg, wpa_supplicant::kPropertyAuthAlg) &&
573 ContainsKey(arg, wpa_supplicant::kPropertyWEPKey + std::string("3")) &&
574 ContainsKey(arg, wpa_supplicant::kPropertyWEPTxKeyIndex) &&
575 (arg.find(wpa_supplicant::kPropertyWEPTxKeyIndex)->second.
576 reader().get_uint32() == 3);
577}
578
579TEST_F(WiFiServiceTest, ConnectTaskWEP) {
Paul Stewart3c504012013-01-17 17:49:58 -0800580 WiFiServiceRefPtr wifi_service = MakeServiceWithWiFi(flimflam::kSecurityWep);
Thieu Lef4cbda92011-11-10 23:41:24 +0000581 Error error;
582 wifi_service->SetPassphrase("0:abcdefghijklm", &error);
583 EXPECT_CALL(*wifi(),
584 ConnectTo(wifi_service.get(), WEPSecurityArgsKeyIndex0()));
Eric Shienbrood9a245532012-03-07 14:20:39 -0500585 wifi_service->Connect(NULL);
Thieu Lef4cbda92011-11-10 23:41:24 +0000586
587 wifi_service->SetPassphrase("abcdefghijklm", &error);
588 EXPECT_CALL(*wifi(),
589 ConnectTo(wifi_service.get(), WEPSecurityArgsKeyIndex0()));
Eric Shienbrood9a245532012-03-07 14:20:39 -0500590 wifi_service->Connect(NULL);
Thieu Lef4cbda92011-11-10 23:41:24 +0000591
592 wifi_service->SetPassphrase("1:abcdefghijklm", &error);
593 EXPECT_CALL(*wifi(),
594 ConnectTo(wifi_service.get(), WEPSecurityArgsKeyIndex1()));
Eric Shienbrood9a245532012-03-07 14:20:39 -0500595 wifi_service->Connect(NULL);
Thieu Lef4cbda92011-11-10 23:41:24 +0000596
597 wifi_service->SetPassphrase("2:abcdefghijklm", &error);
598 EXPECT_CALL(*wifi(),
599 ConnectTo(wifi_service.get(), WEPSecurityArgsKeyIndex2()));
Eric Shienbrood9a245532012-03-07 14:20:39 -0500600 wifi_service->Connect(NULL);
Thieu Lef4cbda92011-11-10 23:41:24 +0000601
602 wifi_service->SetPassphrase("3:abcdefghijklm", &error);
603 EXPECT_CALL(*wifi(),
604 ConnectTo(wifi_service.get(), WEPSecurityArgsKeyIndex3()));
Eric Shienbrood9a245532012-03-07 14:20:39 -0500605 wifi_service->Connect(NULL);
Thieu Lef4cbda92011-11-10 23:41:24 +0000606}
607
Gaurav Shah29d68882012-01-30 19:06:42 -0800608
609MATCHER(DynamicWEPArgs, "") {
610 return ContainsKey(arg, wpa_supplicant::kNetworkPropertyEapIdentity) &&
611 ContainsKey(arg, wpa_supplicant::kNetworkPropertyCaPath) &&
612 !ContainsKey(arg, wpa_supplicant::kPropertySecurityProtocol);
613}
614
615// Dynamic WEP + 802.1x.
616TEST_F(WiFiServiceTest, ConnectTaskDynamicWEP) {
Paul Stewart3c504012013-01-17 17:49:58 -0800617 WiFiServiceRefPtr wifi_service = MakeServiceWithWiFi(flimflam::kSecurityWep);
Gaurav Shah29d68882012-01-30 19:06:42 -0800618
619 Service::EapCredentials eap;
620 eap.key_management = "IEEE8021X";
621 eap.identity = "something";
Wade Guthrie005bd342012-05-02 09:37:07 -0700622 eap.password = "mumble";
Gaurav Shah29d68882012-01-30 19:06:42 -0800623 wifi_service->set_eap(eap);
624 EXPECT_CALL(*wifi(),
625 ConnectTo(wifi_service.get(), DynamicWEPArgs()));
Eric Shienbrood9a245532012-03-07 14:20:39 -0500626 wifi_service->Connect(NULL);
Gaurav Shah29d68882012-01-30 19:06:42 -0800627}
628
Paul Stewart835934a2012-12-06 19:27:09 -0800629TEST_F(WiFiServiceTest, SetPassphraseRemovesCachedCredentials) {
630 vector<uint8_t> ssid(5);
Paul Stewart3c504012013-01-17 17:49:58 -0800631 WiFiServiceRefPtr wifi_service = MakeServiceWithWiFi(flimflam::kSecurityRsn);
Paul Stewart835934a2012-12-06 19:27:09 -0800632
633 const string kPassphrase = "abcdefgh";
634
635 {
636 Error error;
637 // A changed passphrase should trigger cache removal.
638 EXPECT_CALL(*wifi(), ClearCachedCredentials(wifi_service.get()));
639 wifi_service->SetPassphrase(kPassphrase, &error);
640 Mock::VerifyAndClearExpectations(wifi());
641 EXPECT_TRUE(error.IsSuccess());
642 }
643
644 {
645 Error error;
646 // An unchanged passphrase should not trigger cache removal.
647 EXPECT_CALL(*wifi(), ClearCachedCredentials(_)).Times(0);
648 wifi_service->SetPassphrase(kPassphrase, &error);
649 Mock::VerifyAndClearExpectations(wifi());
650 EXPECT_TRUE(error.IsSuccess());
651 }
652
653 {
654 Error error;
655 // A modified passphrase should trigger cache removal.
656 EXPECT_CALL(*wifi(), ClearCachedCredentials(wifi_service.get()));
657 wifi_service->SetPassphrase(kPassphrase + "X", &error);
658 Mock::VerifyAndClearExpectations(wifi());
659 EXPECT_TRUE(error.IsSuccess());
660 }
661
662 {
663 Error error;
664 // A cleared passphrase should also trigger cache removal.
665 EXPECT_CALL(*wifi(), ClearCachedCredentials(wifi_service.get()));
666 wifi_service->ClearPassphrase(&error);
667 Mock::VerifyAndClearExpectations(wifi());
668 EXPECT_TRUE(error.IsSuccess());
669 }
670
671 {
672 Error error;
673 // An invalid passphrase should not trigger cache removal.
674 EXPECT_CALL(*wifi(), ClearCachedCredentials(_)).Times(0);
675 wifi_service->SetPassphrase("", &error);
676 Mock::VerifyAndClearExpectations(wifi());
677 EXPECT_FALSE(error.IsSuccess());
678 }
679
680 {
681 // Any change to EAP parameters (including a null one) will trigger cache
682 // removal. This is a lot less granular than the passphrase checks above.
683 EXPECT_CALL(*wifi(), ClearCachedCredentials(wifi_service.get()));
684 wifi_service->set_eap(Service::EapCredentials());
685 Mock::VerifyAndClearExpectations(wifi());
686 }
687}
688
Paul Stewartd08f4432011-11-04 07:48:20 -0700689TEST_F(WiFiServiceTest, LoadHidden) {
Paul Stewart3c504012013-01-17 17:49:58 -0800690 WiFiServiceRefPtr service = MakeSimpleService(flimflam::kSecurityNone);
Paul Stewartd08f4432011-11-04 07:48:20 -0700691 ASSERT_FALSE(service->hidden_ssid_);
692 NiceMock<MockStore> mock_store;
693 const string storage_id = service->GetStorageIdentifier();
Paul Stewart71a4d3b2013-01-18 18:12:56 -0800694 set<string> groups;
695 groups.insert(storage_id);
Paul Stewartd08f4432011-11-04 07:48:20 -0700696 EXPECT_CALL(mock_store, ContainsGroup(StrEq(storage_id)))
697 .WillRepeatedly(Return(true));
Paul Stewart71a4d3b2013-01-18 18:12:56 -0800698 EXPECT_CALL(mock_store, GetGroupsWithProperties(
699 ContainsWiFiProperties(
Paul Stewart3c504012013-01-17 17:49:58 -0800700 simple_ssid(), flimflam::kModeManaged, flimflam::kSecurityNone)))
Paul Stewart71a4d3b2013-01-18 18:12:56 -0800701 .WillRepeatedly(Return(groups));
Paul Stewartd08f4432011-11-04 07:48:20 -0700702 EXPECT_CALL(mock_store, GetBool(_, _, _))
703 .WillRepeatedly(Return(false));
704 EXPECT_CALL(mock_store,
705 GetBool(StrEq(storage_id), WiFiService::kStorageHiddenSSID, _))
706 .WillRepeatedly(DoAll(SetArgumentPointee<2>(true), Return(true)));
707 EXPECT_TRUE(service->Load(&mock_store));
708 EXPECT_TRUE(service->hidden_ssid_);
709}
710
Paul Stewart71a4d3b2013-01-18 18:12:56 -0800711TEST_F(WiFiServiceTest, LoadMultipleMatchingGroups) {
Paul Stewart3c504012013-01-17 17:49:58 -0800712 WiFiServiceRefPtr service = MakeServiceWithWiFi(flimflam::kSecurityNone);
Paul Stewart71a4d3b2013-01-18 18:12:56 -0800713 set<string> groups;
714 groups.insert("id0");
715 groups.insert("id1");
716 // Make sure we retain the first matched group in the same way that
717 // WiFiService::Load() will.
718 string first_group = *groups.begin();
719
720 NiceMock<MockStore> mock_store;
721 EXPECT_CALL(mock_store, GetGroupsWithProperties(
722 ContainsWiFiProperties(
Paul Stewart3c504012013-01-17 17:49:58 -0800723 simple_ssid(), flimflam::kModeManaged, flimflam::kSecurityNone)))
Paul Stewart71a4d3b2013-01-18 18:12:56 -0800724 .WillRepeatedly(Return(groups));
725 EXPECT_CALL(mock_store, ContainsGroup(first_group))
726 .WillRepeatedly(Return(true));
727 EXPECT_CALL(mock_store, ContainsGroup(StrNe(first_group))).Times(0);
728 EXPECT_CALL(mock_store, GetBool(first_group, _, _))
729 .WillRepeatedly(Return(false));
730 EXPECT_CALL(mock_store, GetBool(StrNe(first_group), _, _)).Times(0);
731 ScopedMockLog log;
732 EXPECT_CALL(log, Log(_, _, _)).Times(AnyNumber());
733 EXPECT_CALL(log, Log(logging::LOG_WARNING, _,
734 EndsWith("choosing the first.")));
735 EXPECT_TRUE(service->Load(&mock_store));
736}
737
Paul Stewartd08f4432011-11-04 07:48:20 -0700738TEST_F(WiFiServiceSecurityTest, WPAMapping) {
739 EXPECT_TRUE(TestStorageMapping(flimflam::kSecurityRsn,
740 flimflam::kSecurityPsk));
741 EXPECT_TRUE(TestStorageMapping(flimflam::kSecurityWpa,
742 flimflam::kSecurityPsk));
743 EXPECT_TRUE(TestStorageMapping(flimflam::kSecurityPsk,
744 flimflam::kSecurityPsk));
745 EXPECT_TRUE(TestStorageMapping(flimflam::kSecurityWep,
746 flimflam::kSecurityWep));
747 EXPECT_TRUE(TestStorageMapping(flimflam::kSecurityNone,
748 flimflam::kSecurityNone));
Gaurav Shah10109f22011-11-11 20:16:22 -0800749 EXPECT_TRUE(TestStorageMapping(flimflam::kSecurity8021x,
750 flimflam::kSecurity8021x));
Paul Stewartd08f4432011-11-04 07:48:20 -0700751}
752
753TEST_F(WiFiServiceSecurityTest, LoadMapping) {
754 EXPECT_TRUE(TestLoadMapping(flimflam::kSecurityRsn,
755 flimflam::kSecurityPsk,
756 true));
757 EXPECT_TRUE(TestLoadMapping(flimflam::kSecurityRsn,
758 flimflam::kSecurityRsn,
Paul Stewart71a4d3b2013-01-18 18:12:56 -0800759 false));
Paul Stewartd08f4432011-11-04 07:48:20 -0700760 EXPECT_TRUE(TestLoadMapping(flimflam::kSecurityRsn,
761 flimflam::kSecurityWpa,
762 false));
763 EXPECT_TRUE(TestLoadMapping(flimflam::kSecurityWpa,
764 flimflam::kSecurityPsk,
765 true));
766 EXPECT_TRUE(TestLoadMapping(flimflam::kSecurityWpa,
767 flimflam::kSecurityWpa,
Paul Stewart71a4d3b2013-01-18 18:12:56 -0800768 false));
Paul Stewartd08f4432011-11-04 07:48:20 -0700769 EXPECT_TRUE(TestLoadMapping(flimflam::kSecurityWpa,
770 flimflam::kSecurityRsn,
771 false));
772 EXPECT_TRUE(TestLoadMapping(flimflam::kSecurityWep,
773 flimflam::kSecurityWep,
774 true));
775 EXPECT_TRUE(TestLoadMapping(flimflam::kSecurityWep,
776 flimflam::kSecurityPsk,
777 false));
778}
779
Paul Stewartd8ad3c42012-01-09 12:39:38 -0800780TEST_F(WiFiServiceTest, LoadAndUnloadPassphrase) {
Paul Stewart3c504012013-01-17 17:49:58 -0800781 WiFiServiceRefPtr service = MakeSimpleService(flimflam::kSecurityPsk);
Paul Stewartd8ad3c42012-01-09 12:39:38 -0800782 NiceMock<MockStore> mock_store;
783 const string storage_id = service->GetStorageIdentifier();
784 EXPECT_CALL(mock_store, ContainsGroup(StrEq(storage_id)))
785 .WillRepeatedly(Return(true));
Paul Stewart71a4d3b2013-01-18 18:12:56 -0800786 set<string> groups;
787 groups.insert(storage_id);
788 EXPECT_CALL(mock_store, GetGroupsWithProperties(
789 ContainsWiFiProperties(
Paul Stewart3c504012013-01-17 17:49:58 -0800790 simple_ssid(), flimflam::kModeManaged, flimflam::kSecurityPsk)))
Paul Stewart71a4d3b2013-01-18 18:12:56 -0800791 .WillRepeatedly(Return(groups));
Paul Stewartd8ad3c42012-01-09 12:39:38 -0800792 EXPECT_CALL(mock_store, GetBool(_, _, _))
793 .WillRepeatedly(Return(false));
794 const string passphrase = "passphrase";
795 EXPECT_CALL(mock_store,
796 GetCryptedString(StrEq(storage_id),
797 WiFiService::kStoragePassphrase, _))
798 .WillRepeatedly(DoAll(SetArgumentPointee<2>(passphrase), Return(true)));
799 EXPECT_CALL(mock_store,
800 GetCryptedString(StrEq(storage_id),
801 StrNe(WiFiService::kStoragePassphrase), _))
802 .WillRepeatedly(Return(false));
803 EXPECT_TRUE(service->need_passphrase_);
804 EXPECT_TRUE(service->Load(&mock_store));
805 EXPECT_EQ(passphrase, service->passphrase_);
806 EXPECT_TRUE(service->connectable());
807 EXPECT_FALSE(service->need_passphrase_);
808 service->Unload();
809 EXPECT_EQ(string(""), service->passphrase_);
810 EXPECT_FALSE(service->connectable());
811 EXPECT_TRUE(service->need_passphrase_);
812}
813
Christopher Wiley27b47232012-11-02 13:13:00 -0700814TEST_F(WiFiServiceTest, ConfigureMakesConnectable) {
815 string guid("legit_guid");
816 KeyValueStore args;
817 args.SetString(flimflam::kEapIdentityProperty, "legit_identity");
818 args.SetString(flimflam::kEapPasswordProperty, "legit_password");
819 args.SetString(flimflam::kEAPEAPProperty, "PEAP");
820 args.SetString(flimflam::kGuidProperty, guid);
821 Error error;
Christopher Wiley27b47232012-11-02 13:13:00 -0700822
Paul Stewart3c504012013-01-17 17:49:58 -0800823 WiFiServiceRefPtr service = MakeSimpleService(flimflam::kSecurity8021x);
Christopher Wiley27b47232012-11-02 13:13:00 -0700824 // Hack the GUID in so that we don't have to mess about with WiFi to regsiter
825 // our service. This way, Manager will handle the lookup itself.
826 service->set_guid(guid);
827 manager()->RegisterService(service);
828 EXPECT_FALSE(service->connectable());
829 EXPECT_EQ(service.get(), manager()->GetService(args, &error).get());
830 EXPECT_TRUE(error.IsSuccess());
831 EXPECT_TRUE(service->connectable());
832}
833
Paul Stewart3c504012013-01-17 17:49:58 -0800834TEST_F(WiFiServiceTest, ConfigurePassphrase) {
835 EXPECT_EQ(Error::kNotSupported,
836 TestConfigurePassphrase(flimflam::kSecurityNone, ""));
837 EXPECT_EQ(Error::kNotSupported,
838 TestConfigurePassphrase(flimflam::kSecurityNone, "foo"));
839 EXPECT_EQ(Error::kSuccess,
840 TestConfigurePassphrase(flimflam::kSecurityWep, NULL));
841 EXPECT_EQ(Error::kInvalidPassphrase,
842 TestConfigurePassphrase(flimflam::kSecurityWep, ""));
843 EXPECT_EQ(Error::kInvalidPassphrase,
844 TestConfigurePassphrase(flimflam::kSecurityWep, "abcd"));
845 EXPECT_EQ(Error::kSuccess,
846 TestConfigurePassphrase(flimflam::kSecurityWep, "abcde"));
847 EXPECT_EQ(Error::kSuccess,
848 TestConfigurePassphrase(flimflam::kSecurityWep, "abcdefghijklm"));
849 EXPECT_EQ(Error::kSuccess,
850 TestConfigurePassphrase(flimflam::kSecurityWep, "0:abcdefghijklm"));
851 EXPECT_EQ(Error::kSuccess,
852 TestConfigurePassphrase(flimflam::kSecurityWep, "0102030405"));
853 EXPECT_EQ(Error::kInvalidPassphrase,
854 TestConfigurePassphrase(flimflam::kSecurityWep, "0x0102030405"));
855 EXPECT_EQ(Error::kInvalidPassphrase,
856 TestConfigurePassphrase(flimflam::kSecurityWep, "O102030405"));
857 EXPECT_EQ(Error::kInvalidPassphrase,
858 TestConfigurePassphrase(flimflam::kSecurityWep, "1:O102030405"));
859 EXPECT_EQ(Error::kInvalidPassphrase,
860 TestConfigurePassphrase(flimflam::kSecurityWep, "1:0xO102030405"));
861 EXPECT_EQ(Error::kInvalidPassphrase,
862 TestConfigurePassphrase(flimflam::kSecurityWep, "0xO102030405"));
863 EXPECT_EQ(Error::kSuccess,
864 TestConfigurePassphrase(flimflam::kSecurityWep,
865 "0102030405060708090a0b0c0d"));
866 EXPECT_EQ(Error::kSuccess,
867 TestConfigurePassphrase(flimflam::kSecurityWep,
868 "0102030405060708090A0B0C0D"));
869 EXPECT_EQ(Error::kSuccess,
870 TestConfigurePassphrase(flimflam::kSecurityWep,
871 "0:0102030405060708090a0b0c0d"));
872 EXPECT_EQ(Error::kSuccess,
873 TestConfigurePassphrase(flimflam::kSecurityWep,
874 "0:0x0102030405060708090a0b0c0d"));
875 EXPECT_EQ(Error::kSuccess,
876 TestConfigurePassphrase(flimflam::kSecurityWpa, NULL));
877 EXPECT_EQ(Error::kSuccess,
878 TestConfigurePassphrase(flimflam::kSecurityWpa, "secure password"));
879 EXPECT_EQ(Error::kInvalidPassphrase,
880 TestConfigurePassphrase(flimflam::kSecurityWpa, ""));
881 EXPECT_EQ(Error::kSuccess, TestConfigurePassphrase(
882 flimflam::kSecurityWpa,
883 string(IEEE_80211::kWPAAsciiMinLen, 'Z').c_str()));
884 EXPECT_EQ(Error::kSuccess, TestConfigurePassphrase(
885 flimflam::kSecurityWpa,
886 string(IEEE_80211::kWPAAsciiMaxLen, 'Z').c_str()));
887 // subtle: invalid length for hex key, but valid as ascii passphrase
888 EXPECT_EQ(Error::kSuccess, TestConfigurePassphrase(
889 flimflam::kSecurityWpa,
890 string(IEEE_80211::kWPAHexLen-1, '1').c_str()));
891 EXPECT_EQ(Error::kSuccess, TestConfigurePassphrase(
892 flimflam::kSecurityWpa,
893 string(IEEE_80211::kWPAHexLen, '1').c_str()));
894 EXPECT_EQ(Error::kInvalidPassphrase, TestConfigurePassphrase(
895 flimflam::kSecurityWpa,
896 string(IEEE_80211::kWPAAsciiMinLen-1, 'Z').c_str()));
897 EXPECT_EQ(Error::kInvalidPassphrase, TestConfigurePassphrase(
898 flimflam::kSecurityWpa,
899 string(IEEE_80211::kWPAAsciiMaxLen+1, 'Z').c_str()));
900 EXPECT_EQ(Error::kInvalidPassphrase, TestConfigurePassphrase(
901 flimflam::kSecurityWpa,
902 string(IEEE_80211::kWPAHexLen+1, '1').c_str()));
903}
904
905TEST_F(WiFiServiceTest, ConfigureRedundantProperties) {
906 WiFiServiceRefPtr service = MakeSimpleService(flimflam::kSecurityNone);
907 KeyValueStore args;
908 args.SetString(flimflam::kTypeProperty, flimflam::kTypeWifi);
909 args.SetString(flimflam::kSSIDProperty, simple_ssid_string());
910 args.SetString(flimflam::kSecurityProperty, flimflam::kSecurityNone);
911 const string kGUID = "aguid";
912 args.SetString(flimflam::kGuidProperty, kGUID);
913
914 EXPECT_EQ("", service->guid());
915 Error error;
916 service->Configure(args, &error);
917 EXPECT_TRUE(error.IsSuccess());
918 EXPECT_EQ(kGUID, service->guid());
919}
920
921TEST_F(WiFiServiceTest, DisconnectWithWiFi) {
922 WiFiServiceRefPtr service = MakeServiceWithWiFi(flimflam::kSecurityWep);
923 EXPECT_CALL(*wifi(), DisconnectFrom(service.get())).Times(1);
924 Error error;
925 service->Disconnect(&error);
926}
927
928TEST_F(WiFiServiceTest, DisconnectWithoutWiFi) {
929 WiFiServiceRefPtr service = MakeSimpleService(flimflam::kSecurityWep);
930 EXPECT_CALL(*wifi(), DisconnectFrom(_)).Times(0);
931 Error error;
932 service->Disconnect(&error);
933 EXPECT_EQ(Error::kOperationFailed, error.type());
934}
935
936TEST_F(WiFiServiceTest, DisconnectWithoutWiFiWhileAssociating) {
937 WiFiServiceRefPtr service = MakeSimpleService(flimflam::kSecurityWep);
938 EXPECT_CALL(*wifi(), DisconnectFrom(_)).Times(0);
939 service->SetState(Service::kStateAssociating);
940 ScopedMockLog log;
941 EXPECT_CALL(log, Log(_, _, _)).Times(AnyNumber());
942 EXPECT_CALL(log, Log(logging::LOG_ERROR, _,
943 HasSubstr("WiFi endpoints do not (yet) exist.")));
944 Error error;
945 service->Disconnect(&error);
946 EXPECT_EQ(Error::kOperationFailed, error.type());
947}
948
Paul Stewart835934a2012-12-06 19:27:09 -0800949TEST_F(WiFiServiceTest, UnloadAndClearCacheWEP) {
Paul Stewart3c504012013-01-17 17:49:58 -0800950 WiFiServiceRefPtr service = MakeServiceWithWiFi(flimflam::kSecurityWep);
Paul Stewart835934a2012-12-06 19:27:09 -0800951 EXPECT_CALL(*wifi(), ClearCachedCredentials(service.get())).Times(1);
952 EXPECT_CALL(*wifi(), DisconnectFrom(service.get())).Times(1);
Paul Stewart66c86002012-01-30 18:00:52 -0800953 service->Unload();
954}
955
956TEST_F(WiFiServiceTest, UnloadAndClearCache8021x) {
Paul Stewart3c504012013-01-17 17:49:58 -0800957 WiFiServiceRefPtr service = MakeServiceWithWiFi(flimflam::kSecurity8021x);
Paul Stewart835934a2012-12-06 19:27:09 -0800958 EXPECT_CALL(*wifi(), ClearCachedCredentials(service.get())).Times(1);
959 EXPECT_CALL(*wifi(), DisconnectFrom(service.get())).Times(1);
Paul Stewart66c86002012-01-30 18:00:52 -0800960 service->Unload();
961}
962
Paul Stewart0756db92012-01-27 08:34:47 -0800963TEST_F(WiFiServiceTest, ParseStorageIdentifierNone) {
Paul Stewart3c504012013-01-17 17:49:58 -0800964 WiFiServiceRefPtr service = MakeSimpleService(flimflam::kSecurityNone);
Paul Stewarta41e38d2011-11-11 07:47:29 -0800965 const string storage_id = service->GetStorageIdentifier();
966 string address;
967 string mode;
968 string security;
969 EXPECT_TRUE(service->ParseStorageIdentifier(storage_id, &address, &mode,
970 &security));
Paul Stewart3c504012013-01-17 17:49:58 -0800971 EXPECT_EQ(StringToLowerASCII(GetAnyDeviceAddress()), address);
Paul Stewarta41e38d2011-11-11 07:47:29 -0800972 EXPECT_EQ(flimflam::kModeManaged, mode);
973 EXPECT_EQ(flimflam::kSecurityNone, security);
974}
975
Paul Stewart0756db92012-01-27 08:34:47 -0800976TEST_F(WiFiServiceTest, ParseStorageIdentifier8021x) {
977 // Do a separate test for 802.1x, since kSecurity8021x contains a "_",
978 // which needs to be dealt with specially in the parser.
Paul Stewart3c504012013-01-17 17:49:58 -0800979 WiFiServiceRefPtr service = MakeSimpleService(flimflam::kSecurity8021x);
Paul Stewart0756db92012-01-27 08:34:47 -0800980 const string storage_id = service->GetStorageIdentifier();
981 string address;
982 string mode;
983 string security;
984 EXPECT_TRUE(service->ParseStorageIdentifier(storage_id, &address, &mode,
985 &security));
Paul Stewart3c504012013-01-17 17:49:58 -0800986 EXPECT_EQ(StringToLowerASCII(GetAnyDeviceAddress()), address);
Paul Stewart0756db92012-01-27 08:34:47 -0800987 EXPECT_EQ(flimflam::kModeManaged, mode);
988 EXPECT_EQ(flimflam::kSecurity8021x, security);
989}
990
Paul Stewart85aea152013-01-22 09:31:56 -0800991TEST_F(WiFiServiceFixupStorageTest, FixedEntries) {
992 const string kNonWiFiId = "vpn_foo";
993 const string kUnparsableWiFiId = "wifi_foo";
994
995 AddGroup(kNonWiFiId);
996 AddGroup(kUnparsableWiFiId);
Paul Stewart71a4d3b2013-01-18 18:12:56 -0800997 AddServiceEntry(true, true, true, true);
998 AddServiceEntry(false, false, false, false);
999 AddServiceEntry(true, true, true, true);
1000 AddServiceEntry(false, false, false, false);
Paul Stewart85aea152013-01-22 09:31:56 -08001001 EXPECT_TRUE(FixupServiceEntries());
1002}
1003
1004TEST_F(WiFiServiceFixupStorageTest, NoFixedEntries) {
1005 const string kNonWiFiId = "vpn_foo";
1006 const string kUnparsableWiFiId = "wifi_foo";
1007
1008 AddGroup(kNonWiFiId);
1009 AddGroup(kUnparsableWiFiId);
Paul Stewart71a4d3b2013-01-18 18:12:56 -08001010 AddServiceEntry(true, true, true, true);
Paul Stewart85aea152013-01-22 09:31:56 -08001011 EXPECT_FALSE(FixupServiceEntries());
1012}
1013
1014TEST_F(WiFiServiceFixupStorageTest, MissingTypeProperty) {
Paul Stewart71a4d3b2013-01-18 18:12:56 -08001015 AddServiceEntry(false, true, true, true);
Paul Stewart85aea152013-01-22 09:31:56 -08001016 EXPECT_TRUE(FixupServiceEntries());
1017}
1018
1019TEST_F(WiFiServiceFixupStorageTest, MissingModeProperty) {
Paul Stewart71a4d3b2013-01-18 18:12:56 -08001020 AddServiceEntry(true, false, true, true);
Paul Stewart85aea152013-01-22 09:31:56 -08001021 EXPECT_TRUE(FixupServiceEntries());
1022}
1023
1024TEST_F(WiFiServiceFixupStorageTest, MissingSecurityProperty) {
Paul Stewart71a4d3b2013-01-18 18:12:56 -08001025 AddServiceEntry(true, true, false, true);
Paul Stewart85aea152013-01-22 09:31:56 -08001026 EXPECT_TRUE(FixupServiceEntries());
1027}
1028
Paul Stewart71a4d3b2013-01-18 18:12:56 -08001029TEST_F(WiFiServiceFixupStorageTest, MissingSecurityClassProperty) {
1030 AddServiceEntry(true, true, true, false);
1031 EXPECT_TRUE(FixupServiceEntries());
1032}
Paul Stewart85aea152013-01-22 09:31:56 -08001033
mukesh agrawal29c13a12011-11-24 00:09:19 +00001034TEST_F(WiFiServiceTest, Connectable) {
1035 // Open network should be connectable.
Gaurav Shah10109f22011-11-11 20:16:22 -08001036 EXPECT_TRUE(CheckConnectable(flimflam::kSecurityNone, NULL, NULL));
mukesh agrawal29c13a12011-11-24 00:09:19 +00001037
1038 // Open network should remain connectable if we try to set a password on it.
Gaurav Shah10109f22011-11-11 20:16:22 -08001039 EXPECT_TRUE(CheckConnectable(flimflam::kSecurityNone, "abcde", NULL));
mukesh agrawal29c13a12011-11-24 00:09:19 +00001040
1041 // WEP network with passphrase set should be connectable.
Gaurav Shah10109f22011-11-11 20:16:22 -08001042 EXPECT_TRUE(CheckConnectable(flimflam::kSecurityWep, "abcde", NULL));
mukesh agrawal29c13a12011-11-24 00:09:19 +00001043
1044 // WEP network without passphrase set should NOT be connectable.
Gaurav Shah10109f22011-11-11 20:16:22 -08001045 EXPECT_FALSE(CheckConnectable(flimflam::kSecurityWep, NULL, NULL));
mukesh agrawal29c13a12011-11-24 00:09:19 +00001046
1047 // A bad passphrase should not make a WEP network connectable.
Gaurav Shah10109f22011-11-11 20:16:22 -08001048 EXPECT_FALSE(CheckConnectable(flimflam::kSecurityWep, "a", NULL));
mukesh agrawal29c13a12011-11-24 00:09:19 +00001049
1050 // Similar to WEP, for WPA.
Gaurav Shah10109f22011-11-11 20:16:22 -08001051 EXPECT_TRUE(CheckConnectable(flimflam::kSecurityWpa, "abcdefgh", NULL));
1052 EXPECT_FALSE(CheckConnectable(flimflam::kSecurityWpa, NULL, NULL));
1053 EXPECT_FALSE(CheckConnectable(flimflam::kSecurityWpa, "a", NULL));
mukesh agrawal29c13a12011-11-24 00:09:19 +00001054
1055 // Unconfigured 802.1x should NOT be connectable.
Gaurav Shah10109f22011-11-11 20:16:22 -08001056 EXPECT_FALSE(CheckConnectable(flimflam::kSecurity8021x, NULL, NULL));
1057
1058 Service::EapCredentials eap;
1059 // Empty EAP credentials should not make a 802.1x network connectable.
1060 EXPECT_FALSE(CheckConnectable(flimflam::kSecurity8021x, NULL, &eap));
1061
1062 eap.identity = "something";
1063 // If client certificate is being used, a private key must exist.
1064 eap.client_cert = "some client cert";
1065 EXPECT_FALSE(CheckConnectable(flimflam::kSecurity8021x, NULL, &eap));
1066 eap.private_key = "some private key";
1067 EXPECT_TRUE(CheckConnectable(flimflam::kSecurity8021x, NULL, &eap));
1068
1069 // Identity is always required.
1070 eap.identity.clear();
1071 EXPECT_FALSE(CheckConnectable(flimflam::kSecurity8021x, NULL, &eap));
1072
1073 eap.identity = "something";
1074 // For non EAP-TLS types, a password is required.
1075 eap.eap = "Non-TLS";
1076 EXPECT_FALSE(CheckConnectable(flimflam::kSecurity8021x, NULL, &eap));
1077 eap.password = "some password";
1078 EXPECT_TRUE(CheckConnectable(flimflam::kSecurity8021x, NULL, &eap));
Gaurav Shah29d68882012-01-30 19:06:42 -08001079 // Dynamic WEP + 802.1X should be connectable under the same conditions.
1080 eap.key_management = "IEEE8021X";
1081 EXPECT_TRUE(CheckConnectable(flimflam::kSecurityWep, NULL, &eap));
mukesh agrawal29c13a12011-11-24 00:09:19 +00001082}
1083
mukesh agrawal8a3188d2011-12-01 20:56:44 +00001084TEST_F(WiFiServiceTest, IsAutoConnectable) {
mukesh agrawalbf14e942012-03-02 14:36:34 -08001085 const char *reason;
Paul Stewart3c504012013-01-17 17:49:58 -08001086 WiFiServiceRefPtr service = MakeSimpleService(flimflam::kSecurityNone);
mukesh agrawal8a3188d2011-12-01 20:56:44 +00001087 EXPECT_CALL(*wifi(), IsIdle())
1088 .WillRepeatedly(Return(true));
1089 EXPECT_FALSE(service->HasEndpoints());
mukesh agrawalbf14e942012-03-02 14:36:34 -08001090 EXPECT_FALSE(service->IsAutoConnectable(&reason));
1091 EXPECT_STREQ(WiFiService::kAutoConnNoEndpoint, reason);
mukesh agrawal8a3188d2011-12-01 20:56:44 +00001092
mukesh agrawalbf14e942012-03-02 14:36:34 -08001093 reason = "";
mukesh agrawal43970a22013-02-15 16:00:07 -08001094 WiFiEndpointRefPtr endpoint =
1095 MakeOpenEndpoint("a", "00:00:00:00:00:01", 0, 0);
mukesh agrawal8a3188d2011-12-01 20:56:44 +00001096 service->AddEndpoint(endpoint);
1097 EXPECT_CALL(*wifi(), IsIdle())
1098 .WillRepeatedly(Return(true));
1099 EXPECT_TRUE(service->HasEndpoints());
mukesh agrawalbf14e942012-03-02 14:36:34 -08001100 EXPECT_TRUE(service->IsAutoConnectable(&reason));
1101 EXPECT_STREQ("", reason);
mukesh agrawal8a3188d2011-12-01 20:56:44 +00001102
1103 // WiFi only supports connecting to one Service at a time. So, to
1104 // avoid disrupting connectivity, we only allow auto-connection to
1105 // a WiFiService when the corresponding WiFi is idle.
1106 EXPECT_CALL(*wifi(), IsIdle())
1107 .WillRepeatedly(Return(false));
1108 EXPECT_TRUE(service->HasEndpoints());
mukesh agrawalbf14e942012-03-02 14:36:34 -08001109 EXPECT_FALSE(service->IsAutoConnectable(&reason));
1110 EXPECT_STREQ(WiFiService::kAutoConnBusy, reason);
mukesh agrawal8a3188d2011-12-01 20:56:44 +00001111}
1112
1113TEST_F(WiFiServiceTest, AutoConnect) {
mukesh agrawalbf14e942012-03-02 14:36:34 -08001114 const char *reason;
Paul Stewart3c504012013-01-17 17:49:58 -08001115 WiFiServiceRefPtr service = MakeSimpleService(flimflam::kSecurityNone);
mukesh agrawalbf14e942012-03-02 14:36:34 -08001116 EXPECT_FALSE(service->IsAutoConnectable(&reason));
mukesh agrawal8a3188d2011-12-01 20:56:44 +00001117 EXPECT_CALL(*wifi(), ConnectTo(_, _))
1118 .Times(0);
1119 service->AutoConnect();
Eric Shienbrood3e20a232012-02-16 11:35:56 -05001120 dispatcher()->DispatchPendingEvents();
mukesh agrawal8a3188d2011-12-01 20:56:44 +00001121
mukesh agrawal43970a22013-02-15 16:00:07 -08001122 WiFiEndpointRefPtr endpoint =
1123 MakeOpenEndpoint("a", "00:00:00:00:00:01", 0, 0);
mukesh agrawal8a3188d2011-12-01 20:56:44 +00001124 service->AddEndpoint(endpoint);
1125 EXPECT_CALL(*wifi(), IsIdle())
1126 .WillRepeatedly(Return(true));
mukesh agrawalbf14e942012-03-02 14:36:34 -08001127 EXPECT_TRUE(service->IsAutoConnectable(&reason));
mukesh agrawal8a3188d2011-12-01 20:56:44 +00001128 EXPECT_CALL(*wifi(), ConnectTo(_, _));
1129 service->AutoConnect();
Eric Shienbrood3e20a232012-02-16 11:35:56 -05001130 dispatcher()->DispatchPendingEvents();
mukesh agrawaladb68482012-01-17 16:31:51 -08001131
1132 Error error;
Christopher Wileyabd3b502012-09-26 13:08:52 -07001133 service->UserInitiatedDisconnect(&error);
Eric Shienbrood3e20a232012-02-16 11:35:56 -05001134 dispatcher()->DispatchPendingEvents();
mukesh agrawalbf14e942012-03-02 14:36:34 -08001135 EXPECT_FALSE(service->IsAutoConnectable(&reason));
mukesh agrawal8a3188d2011-12-01 20:56:44 +00001136}
1137
Gaurav Shah10109f22011-11-11 20:16:22 -08001138TEST_F(WiFiServiceTest, Populate8021x) {
Paul Stewart3c504012013-01-17 17:49:58 -08001139 WiFiServiceRefPtr service = MakeSimpleService(flimflam::kSecurityNone);
Gaurav Shah10109f22011-11-11 20:16:22 -08001140 Service::EapCredentials eap;
1141 eap.identity = "testidentity";
Paul Stewart20550982012-04-16 12:16:11 -07001142 eap.pin = "xxxx";
Gaurav Shah10109f22011-11-11 20:16:22 -08001143 service->set_eap(eap);
1144 map<string, ::DBus::Variant> params;
1145 service->Populate8021xProperties(&params);
1146 // Test that only non-empty 802.1x properties are populated.
Paul Stewartecf4cd12012-04-17 11:08:39 -07001147 EXPECT_TRUE(ContainsKey(params, wpa_supplicant::kNetworkPropertyEapIdentity));
1148 EXPECT_FALSE(ContainsKey(params, wpa_supplicant::kNetworkPropertyEapKeyId));
1149 EXPECT_FALSE(ContainsKey(params, wpa_supplicant::kNetworkPropertyEapCaCert));
1150
Paul Stewart20550982012-04-16 12:16:11 -07001151 // Test that CA path is set by default.
Paul Stewartecf4cd12012-04-17 11:08:39 -07001152 EXPECT_TRUE(ContainsKey(params, wpa_supplicant::kNetworkPropertyCaPath));
1153
Paul Stewart20550982012-04-16 12:16:11 -07001154 // Test that hardware-backed security arguments are not set.
Paul Stewartecf4cd12012-04-17 11:08:39 -07001155 EXPECT_FALSE(ContainsKey(params, wpa_supplicant::kNetworkPropertyEapPin));
1156 EXPECT_FALSE(ContainsKey(params, wpa_supplicant::kNetworkPropertyEngine));
1157 EXPECT_FALSE(ContainsKey(params, wpa_supplicant::kNetworkPropertyEngineId));
Paul Stewart20550982012-04-16 12:16:11 -07001158}
1159
1160TEST_F(WiFiServiceTest, Populate8021xNoSystemCAs) {
Paul Stewart3c504012013-01-17 17:49:58 -08001161 WiFiServiceRefPtr service = MakeSimpleService(flimflam::kSecurityNone);
Paul Stewart20550982012-04-16 12:16:11 -07001162 Service::EapCredentials eap;
1163 eap.identity = "testidentity";
1164 eap.use_system_cas = false;
1165 service->set_eap(eap);
1166 map<string, ::DBus::Variant> params;
1167 service->Populate8021xProperties(&params);
1168 // Test that CA path is not set if use_system_cas is explicitly false.
Paul Stewartecf4cd12012-04-17 11:08:39 -07001169 EXPECT_FALSE(ContainsKey(params, wpa_supplicant::kNetworkPropertyCaPath));
Paul Stewart20550982012-04-16 12:16:11 -07001170}
1171
1172TEST_F(WiFiServiceTest, Populate8021xUsingHardwareAuth) {
Paul Stewart3c504012013-01-17 17:49:58 -08001173 WiFiServiceRefPtr service = MakeSimpleService(flimflam::kSecurityNone);
Paul Stewart20550982012-04-16 12:16:11 -07001174 Service::EapCredentials eap;
1175 eap.identity = "testidentity";
1176 eap.key_id = "key_id";
1177 eap.pin = "xxxx";
1178 service->set_eap(eap);
1179 map<string, ::DBus::Variant> params;
1180 service->Populate8021xProperties(&params);
1181 // Test that EAP engine parameters set if key_id is set.
Paul Stewartecf4cd12012-04-17 11:08:39 -07001182 EXPECT_TRUE(ContainsKey(params, wpa_supplicant::kNetworkPropertyEapPin));
1183 EXPECT_TRUE(ContainsKey(params, wpa_supplicant::kNetworkPropertyEapKeyId));
1184 EXPECT_TRUE(ContainsKey(params, wpa_supplicant::kNetworkPropertyEngine));
1185 EXPECT_TRUE(ContainsKey(params, wpa_supplicant::kNetworkPropertyEngineId));
1186}
1187
1188TEST_F(WiFiServiceTest, Populate8021xNSS) {
1189 vector<uint8_t> ssid(1, 'a');
Paul Stewart3c504012013-01-17 17:49:58 -08001190 WiFiServiceRefPtr service = MakeSimpleService(flimflam::kSecurityNone);
Paul Stewartecf4cd12012-04-17 11:08:39 -07001191 Service::EapCredentials eap;
1192 eap.ca_cert_nss = "nss_nickname";
1193 service->set_eap(eap);
1194 MockNSS nss;
1195 service->nss_ = &nss;
1196
1197 const string kNSSCertfile("/tmp/nss-cert");
1198 FilePath nss_cert(kNSSCertfile);
1199 vector<char> ssid_in_chars(ssid.begin(), ssid.end());
1200 EXPECT_CALL(nss, GetDERCertfile(eap.ca_cert_nss, ssid_in_chars))
1201 .WillOnce(Return(nss_cert));
1202
1203 map<string, ::DBus::Variant> params;
1204 service->Populate8021xProperties(&params);
1205 EXPECT_TRUE(ContainsKey(params, wpa_supplicant::kNetworkPropertyEapCaCert));
1206 if (ContainsKey(params, wpa_supplicant::kNetworkPropertyEapCaCert)) {
1207 EXPECT_EQ(kNSSCertfile, params[wpa_supplicant::kNetworkPropertyEapCaCert]
1208 .reader().get_string());
1209 }
Gaurav Shah10109f22011-11-11 20:16:22 -08001210}
1211
mukesh agrawal8abd2f62012-01-30 14:56:14 -08001212TEST_F(WiFiServiceTest, ClearWriteOnlyDerivedProperty) {
Paul Stewart3c504012013-01-17 17:49:58 -08001213 WiFiServiceRefPtr wifi_service = MakeSimpleService(flimflam::kSecurityWep);
mukesh agrawal8abd2f62012-01-30 14:56:14 -08001214
1215 EXPECT_EQ("", wifi_service->passphrase_);
1216
1217 ::DBus::Error error;
mukesh agrawal6bb9e7c2012-01-30 14:57:54 -08001218 EXPECT_TRUE(DBusAdaptor::SetProperty(
mukesh agrawal8abd2f62012-01-30 14:56:14 -08001219 wifi_service->mutable_store(),
1220 flimflam::kPassphraseProperty,
1221 DBusAdaptor::StringToVariant("0:abcde"),
1222 &error));
1223 EXPECT_EQ("0:abcde", wifi_service->passphrase_);
1224
1225 EXPECT_TRUE(DBusAdaptor::ClearProperty(wifi_service->mutable_store(),
1226 flimflam::kPassphraseProperty,
1227 &error));
1228 EXPECT_EQ("", wifi_service->passphrase_);
1229}
1230
mukesh agrawale1d90e92012-02-15 17:36:08 -08001231TEST_F(WiFiServiceTest, SignalToStrength) {
1232 // Verify that our mapping is sane, in the sense that it preserves ordering.
1233 // We break the test into two domains, because we assume that positive
1234 // values aren't actually in dBm.
1235 for (int16 i = std::numeric_limits<int16>::min(); i < 0; ++i) {
1236 int16 current_mapped = WiFiService::SignalToStrength(i);
1237 int16 next_mapped = WiFiService::SignalToStrength(i+1);
1238 EXPECT_LE(current_mapped, next_mapped)
1239 << "(original values " << i << " " << i+1 << ")";
mukesh agrawal8f3f7752012-02-17 19:42:09 -08001240 EXPECT_GE(current_mapped, Service::kStrengthMin);
1241 EXPECT_LE(current_mapped, Service::kStrengthMax);
mukesh agrawale1d90e92012-02-15 17:36:08 -08001242 }
1243 for (int16 i = 1; i < std::numeric_limits<int16>::max(); ++i) {
1244 int16 current_mapped = WiFiService::SignalToStrength(i);
1245 int16 next_mapped = WiFiService::SignalToStrength(i+1);
1246 EXPECT_LE(current_mapped, next_mapped)
1247 << "(original values " << i << " " << i+1 << ")";
mukesh agrawal8f3f7752012-02-17 19:42:09 -08001248 EXPECT_GE(current_mapped, Service::kStrengthMin);
1249 EXPECT_LE(current_mapped, Service::kStrengthMax);
mukesh agrawale1d90e92012-02-15 17:36:08 -08001250 }
1251}
1252
1253TEST_F(WiFiServiceUpdateFromEndpointsTest, Strengths) {
1254 // If the chosen signal values don't map to distinct strength
1255 // values, then we can't expect our other tests to pass. So verify
1256 // their distinctness.
1257 EXPECT_TRUE(kOkEndpointStrength != kBadEndpointStrength);
1258 EXPECT_TRUE(kOkEndpointStrength != kGoodEndpointStrength);
1259 EXPECT_TRUE(kGoodEndpointStrength != kBadEndpointStrength);
1260}
1261
1262TEST_F(WiFiServiceUpdateFromEndpointsTest, Floating) {
1263 // Initial endpoint updates values.
1264 EXPECT_CALL(adaptor, EmitUint16Changed(
1265 flimflam::kWifiFrequency, kOkEndpointFrequency));
mukesh agrawal923f14f2012-06-04 16:46:08 -07001266 EXPECT_CALL(adaptor, EmitStringChanged(
1267 flimflam::kWifiBSsid, kOkEndpointBssId));
1268 EXPECT_CALL(adaptor, EmitUint8Changed(
mukesh agrawale1d90e92012-02-15 17:36:08 -08001269 flimflam::kSignalStrengthProperty, kOkEndpointStrength));
1270 service->AddEndpoint(ok_endpoint);
Darin Petkov4a66cc52012-06-15 10:08:29 +02001271 EXPECT_EQ(1, service->GetEndpointCount());
mukesh agrawale1d90e92012-02-15 17:36:08 -08001272 Mock::VerifyAndClearExpectations(&adaptor);
1273
1274 // Endpoint with stronger signal updates values.
1275 EXPECT_CALL(adaptor, EmitUint16Changed(
1276 flimflam::kWifiFrequency, kGoodEndpointFrequency));
mukesh agrawal923f14f2012-06-04 16:46:08 -07001277 EXPECT_CALL(adaptor, EmitStringChanged(
1278 flimflam::kWifiBSsid, kGoodEndpointBssId));
mukesh agrawale1d90e92012-02-15 17:36:08 -08001279 EXPECT_CALL(adaptor, EmitUint8Changed(
1280 flimflam::kSignalStrengthProperty, kGoodEndpointStrength));
1281 service->AddEndpoint(good_endpoint);
Darin Petkov4a66cc52012-06-15 10:08:29 +02001282 EXPECT_EQ(2, service->GetEndpointCount());
mukesh agrawale1d90e92012-02-15 17:36:08 -08001283 Mock::VerifyAndClearExpectations(&adaptor);
1284
1285 // Endpoint with lower signal does not change values.
1286 EXPECT_CALL(adaptor, EmitUint16Changed(flimflam::kWifiFrequency, _)).Times(0);
mukesh agrawal923f14f2012-06-04 16:46:08 -07001287 EXPECT_CALL(adaptor, EmitStringChanged(flimflam::kWifiBSsid, _)).Times(0);
mukesh agrawale1d90e92012-02-15 17:36:08 -08001288 EXPECT_CALL(adaptor,
1289 EmitUint8Changed(flimflam::kSignalStrengthProperty, _)).Times(0);
1290 service->AddEndpoint(bad_endpoint);
Darin Petkov4a66cc52012-06-15 10:08:29 +02001291 EXPECT_EQ(3, service->GetEndpointCount());
mukesh agrawale1d90e92012-02-15 17:36:08 -08001292 Mock::VerifyAndClearExpectations(&adaptor);
1293
1294 // Removing non-optimal endpoint does not change values.
1295 EXPECT_CALL(adaptor, EmitUint16Changed(flimflam::kWifiFrequency, _)).Times(0);
mukesh agrawal923f14f2012-06-04 16:46:08 -07001296 EXPECT_CALL(adaptor, EmitStringChanged(flimflam::kWifiBSsid, _)).Times(0);
mukesh agrawale1d90e92012-02-15 17:36:08 -08001297 EXPECT_CALL(adaptor,
1298 EmitUint8Changed(flimflam::kSignalStrengthProperty, _)).Times(0);
1299 service->RemoveEndpoint(bad_endpoint);
Darin Petkov4a66cc52012-06-15 10:08:29 +02001300 EXPECT_EQ(2, service->GetEndpointCount());
mukesh agrawale1d90e92012-02-15 17:36:08 -08001301 Mock::VerifyAndClearExpectations(&adaptor);
1302
1303 // Removing optimal endpoint updates values.
1304 EXPECT_CALL(adaptor, EmitUint16Changed(
1305 flimflam::kWifiFrequency, kOkEndpointFrequency));
mukesh agrawal923f14f2012-06-04 16:46:08 -07001306 EXPECT_CALL(adaptor, EmitStringChanged(
1307 flimflam::kWifiBSsid, kOkEndpointBssId));
mukesh agrawale1d90e92012-02-15 17:36:08 -08001308 EXPECT_CALL(adaptor, EmitUint8Changed(
1309 flimflam::kSignalStrengthProperty, kOkEndpointStrength));
1310 service->RemoveEndpoint(good_endpoint);
Darin Petkov4a66cc52012-06-15 10:08:29 +02001311 EXPECT_EQ(1, service->GetEndpointCount());
mukesh agrawale1d90e92012-02-15 17:36:08 -08001312 Mock::VerifyAndClearExpectations(&adaptor);
1313
1314 // Removing last endpoint updates values (and doesn't crash).
1315 EXPECT_CALL(adaptor, EmitUint16Changed(flimflam::kWifiFrequency, _));
mukesh agrawal923f14f2012-06-04 16:46:08 -07001316 EXPECT_CALL(adaptor, EmitStringChanged(flimflam::kWifiBSsid, _));
mukesh agrawale1d90e92012-02-15 17:36:08 -08001317 EXPECT_CALL(adaptor, EmitUint8Changed(flimflam::kSignalStrengthProperty, _));
1318 service->RemoveEndpoint(ok_endpoint);
Darin Petkov4a66cc52012-06-15 10:08:29 +02001319 EXPECT_EQ(0, service->GetEndpointCount());
mukesh agrawale1d90e92012-02-15 17:36:08 -08001320 Mock::VerifyAndClearExpectations(&adaptor);
1321}
1322
1323TEST_F(WiFiServiceUpdateFromEndpointsTest, Connected) {
1324 EXPECT_CALL(adaptor, EmitUint16Changed(_, _)).Times(AnyNumber());
mukesh agrawal923f14f2012-06-04 16:46:08 -07001325 EXPECT_CALL(adaptor, EmitStringChanged(_, _)).Times(AnyNumber());
mukesh agrawale1d90e92012-02-15 17:36:08 -08001326 EXPECT_CALL(adaptor, EmitUint8Changed(_, _)).Times(AnyNumber());
1327 EXPECT_CALL(adaptor, EmitBoolChanged(_, _)).Times(AnyNumber());
1328 service->AddEndpoint(bad_endpoint);
1329 service->AddEndpoint(ok_endpoint);
Darin Petkov4a66cc52012-06-15 10:08:29 +02001330 EXPECT_EQ(2, service->GetEndpointCount());
mukesh agrawale1d90e92012-02-15 17:36:08 -08001331 Mock::VerifyAndClearExpectations(&adaptor);
1332
1333 // Setting current endpoint forces adoption of its values, even if it
1334 // doesn't have the highest signal.
1335 EXPECT_CALL(adaptor, EmitUint16Changed(
1336 flimflam::kWifiFrequency, kBadEndpointFrequency));
mukesh agrawal923f14f2012-06-04 16:46:08 -07001337 EXPECT_CALL(adaptor, EmitStringChanged(
1338 flimflam::kWifiBSsid, kBadEndpointBssId));
mukesh agrawale1d90e92012-02-15 17:36:08 -08001339 EXPECT_CALL(adaptor, EmitUint8Changed(
1340 flimflam::kSignalStrengthProperty, kBadEndpointStrength));
1341 service->NotifyCurrentEndpoint(bad_endpoint);
1342 Mock::VerifyAndClearExpectations(&adaptor);
1343
1344 // Adding a better endpoint doesn't matter, when current endpoint is set.
1345 EXPECT_CALL(adaptor, EmitUint16Changed(flimflam::kWifiFrequency, _)).Times(0);
mukesh agrawal923f14f2012-06-04 16:46:08 -07001346 EXPECT_CALL(adaptor, EmitStringChanged(flimflam::kWifiBSsid, _)).Times(0);
mukesh agrawale1d90e92012-02-15 17:36:08 -08001347 EXPECT_CALL(adaptor,
1348 EmitUint8Changed(flimflam::kSignalStrengthProperty, _)).Times(0);
1349 service->AddEndpoint(good_endpoint);
Darin Petkov4a66cc52012-06-15 10:08:29 +02001350 EXPECT_EQ(3, service->GetEndpointCount());
mukesh agrawale1d90e92012-02-15 17:36:08 -08001351 Mock::VerifyAndClearExpectations(&adaptor);
1352
1353 // Removing a better endpoint doesn't matter, when current endpoint is set.
1354 EXPECT_CALL(adaptor, EmitUint16Changed(flimflam::kWifiFrequency, _)).Times(0);
mukesh agrawal923f14f2012-06-04 16:46:08 -07001355 EXPECT_CALL(adaptor, EmitStringChanged(flimflam::kWifiBSsid, _)).Times(0);
mukesh agrawale1d90e92012-02-15 17:36:08 -08001356 EXPECT_CALL(adaptor,
1357 EmitUint8Changed(flimflam::kSignalStrengthProperty, _)).Times(0);
1358 service->RemoveEndpoint(good_endpoint);
1359 Mock::VerifyAndClearExpectations(&adaptor);
1360
1361 // Removing the current endpoint is safe and sane.
1362 EXPECT_CALL(adaptor, EmitUint16Changed(
1363 flimflam::kWifiFrequency, kOkEndpointFrequency));
mukesh agrawal923f14f2012-06-04 16:46:08 -07001364 EXPECT_CALL(adaptor, EmitStringChanged(
1365 flimflam::kWifiBSsid, kOkEndpointBssId));
mukesh agrawale1d90e92012-02-15 17:36:08 -08001366 EXPECT_CALL(adaptor, EmitUint8Changed(
1367 flimflam::kSignalStrengthProperty, kOkEndpointStrength));
1368 service->RemoveEndpoint(bad_endpoint);
1369 Mock::VerifyAndClearExpectations(&adaptor);
1370
1371 // Clearing the current endpoint (without removing it) is also safe and sane.
1372 service->NotifyCurrentEndpoint(ok_endpoint);
1373 EXPECT_CALL(adaptor, EmitUint16Changed(flimflam::kWifiFrequency, _)).Times(0);
mukesh agrawal923f14f2012-06-04 16:46:08 -07001374 EXPECT_CALL(adaptor, EmitStringChanged(flimflam::kWifiBSsid, _)).Times(0);
mukesh agrawale1d90e92012-02-15 17:36:08 -08001375 EXPECT_CALL(adaptor,
1376 EmitUint8Changed(flimflam::kSignalStrengthProperty, _)).Times(0);
1377 service->NotifyCurrentEndpoint(NULL);
1378 Mock::VerifyAndClearExpectations(&adaptor);
1379}
1380
1381TEST_F(WiFiServiceUpdateFromEndpointsTest, EndpointModified) {
1382 EXPECT_CALL(adaptor, EmitUint16Changed(_, _)).Times(AnyNumber());
mukesh agrawal923f14f2012-06-04 16:46:08 -07001383 EXPECT_CALL(adaptor, EmitStringChanged(_, _)).Times(AnyNumber());
mukesh agrawale1d90e92012-02-15 17:36:08 -08001384 EXPECT_CALL(adaptor, EmitUint8Changed(_, _)).Times(AnyNumber());
1385 EXPECT_CALL(adaptor, EmitBoolChanged(_, _)).Times(AnyNumber());
1386 service->AddEndpoint(ok_endpoint);
1387 service->AddEndpoint(good_endpoint);
Darin Petkov4a66cc52012-06-15 10:08:29 +02001388 EXPECT_EQ(2, service->GetEndpointCount());
mukesh agrawale1d90e92012-02-15 17:36:08 -08001389 Mock::VerifyAndClearExpectations(&adaptor);
1390
1391 // Updating sub-optimal Endpoint doesn't update Service.
1392 EXPECT_CALL(adaptor, EmitUint16Changed(flimflam::kWifiFrequency, _)).Times(0);
mukesh agrawal923f14f2012-06-04 16:46:08 -07001393 EXPECT_CALL(adaptor, EmitStringChanged(flimflam::kWifiBSsid, _)).Times(0);
mukesh agrawale1d90e92012-02-15 17:36:08 -08001394 EXPECT_CALL(adaptor,
1395 EmitUint8Changed(flimflam::kSignalStrengthProperty, _)).Times(0);
1396 ok_endpoint->signal_strength_ = (kOkEndpointSignal + kGoodEndpointSignal) / 2;
Paul Stewart3c504012013-01-17 17:49:58 -08001397 service->NotifyEndpointUpdated(ok_endpoint);
mukesh agrawale1d90e92012-02-15 17:36:08 -08001398 Mock::VerifyAndClearExpectations(&adaptor);
1399
1400 // Updating optimal Endpoint updates appropriate Service property.
1401 EXPECT_CALL(adaptor, EmitUint16Changed(flimflam::kWifiFrequency, _)).Times(0);
mukesh agrawal923f14f2012-06-04 16:46:08 -07001402 EXPECT_CALL(adaptor, EmitStringChanged(flimflam::kWifiBSsid, _)).Times(0);
mukesh agrawale1d90e92012-02-15 17:36:08 -08001403 EXPECT_CALL(adaptor, EmitUint8Changed(flimflam::kSignalStrengthProperty, _));
1404 good_endpoint->signal_strength_ = kGoodEndpointSignal + 1;
Paul Stewart3c504012013-01-17 17:49:58 -08001405 service->NotifyEndpointUpdated(good_endpoint);
mukesh agrawale1d90e92012-02-15 17:36:08 -08001406 Mock::VerifyAndClearExpectations(&adaptor);
1407
1408 // Change in optimal Endpoint updates Service properties.
1409 EXPECT_CALL(adaptor, EmitUint16Changed(
1410 flimflam::kWifiFrequency, kOkEndpointFrequency));
mukesh agrawal923f14f2012-06-04 16:46:08 -07001411 EXPECT_CALL(adaptor, EmitStringChanged(
1412 flimflam::kWifiBSsid, kOkEndpointBssId));
mukesh agrawale1d90e92012-02-15 17:36:08 -08001413 EXPECT_CALL(adaptor, EmitUint8Changed(flimflam::kSignalStrengthProperty, _));
1414 ok_endpoint->signal_strength_ = kGoodEndpointSignal + 2;
Paul Stewart3c504012013-01-17 17:49:58 -08001415 service->NotifyEndpointUpdated(ok_endpoint);
mukesh agrawale1d90e92012-02-15 17:36:08 -08001416 Mock::VerifyAndClearExpectations(&adaptor);
1417}
1418
Paul Stewarta5e7d5f2013-01-09 18:06:15 -08001419TEST_F(WiFiServiceUpdateFromEndpointsTest, Ieee80211w) {
1420 EXPECT_CALL(adaptor, EmitUint16Changed(_, _)).Times(AnyNumber());
1421 EXPECT_CALL(adaptor, EmitStringChanged(_, _)).Times(AnyNumber());
1422 EXPECT_CALL(adaptor, EmitUint8Changed(_, _)).Times(AnyNumber());
1423 EXPECT_CALL(adaptor, EmitBoolChanged(_, _)).Times(AnyNumber());
1424 service->AddEndpoint(ok_endpoint);
1425 EXPECT_FALSE(service->ieee80211w_required());
1426 good_endpoint->ieee80211w_required_ = true;
1427 service->AddEndpoint(good_endpoint);
1428 EXPECT_TRUE(service->ieee80211w_required());
1429 service->RemoveEndpoint(good_endpoint);
1430 EXPECT_TRUE(service->ieee80211w_required());
1431}
1432
Paul Stewart8653f462013-02-06 12:21:05 -08001433TEST_F(WiFiServiceUpdateFromEndpointsTest, WarningOnDisconnect) {
1434 service->AddEndpoint(ok_endpoint);
1435 service->SetState(Service::kStateAssociating);
1436 ScopedMockLog log;
1437 EXPECT_CALL(log, Log(_, _, _)).Times(AnyNumber());
1438 EXPECT_CALL(log, Log(logging::LOG_WARNING, _,
1439 EndsWith("disconnect due to no remaining endpoints.")));
1440 service->RemoveEndpoint(ok_endpoint);
1441}
1442
Paul Stewart6df20bd2013-03-13 19:31:25 -07001443TEST_F(WiFiServiceTest, SecurityFromCurrentEndpoint) {
1444 WiFiServiceRefPtr service(MakeSimpleService(flimflam::kSecurityPsk));
1445 EXPECT_EQ(flimflam::kSecurityPsk, service->GetSecurity(NULL));
1446 WiFiEndpoint *endpoint = MakeOpenEndpoint(
1447 simple_ssid_string(), "00:00:00:00:00:00", 0, 0);
1448 service->AddEndpoint(endpoint);
1449 EXPECT_EQ(flimflam::kSecurityPsk, service->GetSecurity(NULL));
1450 service->NotifyCurrentEndpoint(endpoint);
1451 EXPECT_EQ(flimflam::kSecurityNone, service->GetSecurity(NULL));
1452 service->NotifyCurrentEndpoint(NULL);
1453 EXPECT_EQ(flimflam::kSecurityPsk, service->GetSecurity(NULL));
1454}
1455
mukesh agrawal43970a22013-02-15 16:00:07 -08001456TEST_F(WiFiServiceTest, UpdateSecurity) {
1457 // Cleartext and pre-shared-key crypto.
1458 {
1459 WiFiServiceRefPtr service = MakeSimpleService(flimflam::kSecurityNone);
1460 EXPECT_EQ(Service::kCryptoNone, service->crypto_algorithm());
1461 EXPECT_FALSE(service->key_rotation());
1462 EXPECT_FALSE(service->endpoint_auth());
1463 }
1464 {
1465 WiFiServiceRefPtr service = MakeSimpleService(flimflam::kSecurityWep);
1466 EXPECT_EQ(Service::kCryptoRc4, service->crypto_algorithm());
1467 EXPECT_FALSE(service->key_rotation());
1468 EXPECT_FALSE(service->endpoint_auth());
1469 }
1470 {
1471 WiFiServiceRefPtr service = MakeSimpleService(flimflam::kSecurityPsk);
1472 EXPECT_EQ(Service::kCryptoRc4, service->crypto_algorithm());
1473 EXPECT_TRUE(service->key_rotation());
1474 EXPECT_FALSE(service->endpoint_auth());
1475 }
1476 {
1477 WiFiServiceRefPtr service = MakeSimpleService(flimflam::kSecurityWpa);
1478 EXPECT_EQ(Service::kCryptoRc4, service->crypto_algorithm());
1479 EXPECT_TRUE(service->key_rotation());
1480 EXPECT_FALSE(service->endpoint_auth());
1481 }
1482 {
1483 WiFiServiceRefPtr service = MakeSimpleService(flimflam::kSecurityRsn);
1484 EXPECT_EQ(Service::kCryptoAes, service->crypto_algorithm());
1485 EXPECT_TRUE(service->key_rotation());
1486 EXPECT_FALSE(service->endpoint_auth());
1487 }
1488
1489 // Crypto with 802.1X key management.
1490 {
1491 // WEP
1492 WiFiServiceRefPtr service = MakeSimpleService(flimflam::kSecurityWep);
1493 service->SetEAPKeyManagement("IEEE8021X");
1494 EXPECT_EQ(Service::kCryptoRc4, service->crypto_algorithm());
1495 EXPECT_TRUE(service->key_rotation());
1496 EXPECT_TRUE(service->endpoint_auth());
1497 }
1498 {
1499 // WPA
1500 WiFiServiceRefPtr service = MakeSimpleService(flimflam::kSecurity8021x);
1501 WiFiEndpointRefPtr endpoint =
1502 MakeEndpoint("a", "00:00:00:00:00:01", 0, 0, true, false);
1503 service->AddEndpoint(endpoint);
1504 EXPECT_EQ(Service::kCryptoRc4, service->crypto_algorithm());
1505 EXPECT_TRUE(service->key_rotation());
1506 EXPECT_TRUE(service->endpoint_auth());
1507 }
1508 {
1509 // RSN
1510 WiFiServiceRefPtr service = MakeSimpleService(flimflam::kSecurity8021x);
1511 WiFiEndpointRefPtr endpoint =
1512 MakeEndpoint("a", "00:00:00:00:00:01", 0, 0, false, true);
1513 service->AddEndpoint(endpoint);
1514 EXPECT_EQ(Service::kCryptoAes, service->crypto_algorithm());
1515 EXPECT_TRUE(service->key_rotation());
1516 EXPECT_TRUE(service->endpoint_auth());
1517 }
1518 {
1519 // AP supports both WPA and RSN.
1520 WiFiServiceRefPtr service = MakeSimpleService(flimflam::kSecurity8021x);
1521 WiFiEndpointRefPtr endpoint =
1522 MakeEndpoint("a", "00:00:00:00:00:01", 0, 0, true, true);
1523 service->AddEndpoint(endpoint);
1524 EXPECT_EQ(Service::kCryptoAes, service->crypto_algorithm());
1525 EXPECT_TRUE(service->key_rotation());
1526 EXPECT_TRUE(service->endpoint_auth());
1527 }
1528}
1529
1530TEST_F(WiFiServiceTest, ComputeCipher8021x) {
1531 // No endpoints.
1532 {
1533 const set<WiFiEndpointConstRefPtr> endpoints;
1534 EXPECT_EQ(Service::kCryptoNone,
1535 WiFiService::ComputeCipher8021x(endpoints));
1536 }
1537
1538 // Single endpoint, various configs.
1539 {
1540 set<WiFiEndpointConstRefPtr> endpoints;
1541 endpoints.insert(
1542 MakeEndpoint("a", "00:00:00:00:00:01", 0, 0, false, false));
1543 EXPECT_EQ(Service::kCryptoNone,
1544 WiFiService::ComputeCipher8021x(endpoints));
1545 }
1546 {
1547 set<WiFiEndpointConstRefPtr> endpoints;
1548 endpoints.insert(
1549 MakeEndpoint("a", "00:00:00:00:00:01", 0, 0, true, false));
1550 EXPECT_EQ(Service::kCryptoRc4,
1551 WiFiService::ComputeCipher8021x(endpoints));
1552 }
1553 {
1554 set<WiFiEndpointConstRefPtr> endpoints;
1555 endpoints.insert(
1556 MakeEndpoint("a", "00:00:00:00:00:01", 0, 0, false, true));
1557 EXPECT_EQ(Service::kCryptoAes,
1558 WiFiService::ComputeCipher8021x(endpoints));
1559 }
1560 {
1561 set<WiFiEndpointConstRefPtr> endpoints;
1562 endpoints.insert(
1563 MakeEndpoint("a", "00:00:00:00:00:01", 0, 0, true, true));
1564 EXPECT_EQ(Service::kCryptoAes,
1565 WiFiService::ComputeCipher8021x(endpoints));
1566 }
1567
1568 // Multiple endpoints.
1569 {
1570 set<WiFiEndpointConstRefPtr> endpoints;
1571 endpoints.insert(
1572 MakeEndpoint("a", "00:00:00:00:00:01", 0, 0, false, false));
1573 endpoints.insert(
1574 MakeEndpoint("a", "00:00:00:00:00:02", 0, 0, false, false));
1575 EXPECT_EQ(Service::kCryptoNone,
1576 WiFiService::ComputeCipher8021x(endpoints));
1577 }
1578 {
1579 set<WiFiEndpointConstRefPtr> endpoints;
1580 endpoints.insert(
1581 MakeEndpoint("a", "00:00:00:00:00:01", 0, 0, false, false));
1582 endpoints.insert(
1583 MakeEndpoint("a", "00:00:00:00:00:02", 0, 0, true, false));
1584 EXPECT_EQ(Service::kCryptoNone,
1585 WiFiService::ComputeCipher8021x(endpoints));
1586 }
1587 {
1588 set<WiFiEndpointConstRefPtr> endpoints;
1589 endpoints.insert(
1590 MakeEndpoint("a", "00:00:00:00:00:01", 0, 0, true, false));
1591 endpoints.insert(
1592 MakeEndpoint("a", "00:00:00:00:00:02", 0, 0, true, false));
1593 EXPECT_EQ(Service::kCryptoRc4,
1594 WiFiService::ComputeCipher8021x(endpoints));
1595 }
1596 {
1597 set<WiFiEndpointConstRefPtr> endpoints;
1598 endpoints.insert(
1599 MakeEndpoint("a", "00:00:00:00:00:01", 0, 0, true, false));
1600 endpoints.insert(
1601 MakeEndpoint("a", "00:00:00:00:00:02", 0, 0, false, true));
1602 EXPECT_EQ(Service::kCryptoRc4,
1603 WiFiService::ComputeCipher8021x(endpoints));
1604 }
1605 {
1606 set<WiFiEndpointConstRefPtr> endpoints;
1607 endpoints.insert(
1608 MakeEndpoint("a", "00:00:00:00:00:01", 0, 0, false, true));
1609 endpoints.insert(
1610 MakeEndpoint("a", "00:00:00:00:00:02", 0, 0, false, true));
1611 EXPECT_EQ(Service::kCryptoAes,
1612 WiFiService::ComputeCipher8021x(endpoints));
1613 }
1614 {
1615 set<WiFiEndpointConstRefPtr> endpoints;
1616 endpoints.insert(
1617 MakeEndpoint("a", "00:00:00:00:00:01", 0, 0, true, true));
1618 endpoints.insert(
1619 MakeEndpoint("a", "00:00:00:00:00:02", 0, 0, true, true));
1620 EXPECT_EQ(Service::kCryptoAes,
1621 WiFiService::ComputeCipher8021x(endpoints));
1622 }
1623}
1624
Chris Masone34af2182011-08-22 11:59:36 -07001625} // namespace shill