blob: 74844e0ca38ff0beb25fe1182b5f159146726af4 [file] [log] [blame]
mukesh agrawal8a3188d2011-12-01 20:56:44 +00001// Copyright (c) 2012 The Chromium OS Authors. All rights reserved.
Chris Masone34af2182011-08-22 11:59:36 -07002// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#include "shill/wifi_service.h"
6
mukesh agrawald835b202011-10-07 15:26:47 -07007#include <map>
Paul Stewart85aea152013-01-22 09:31:56 -08008#include <set>
Chris Masone34af2182011-08-22 11:59:36 -07009#include <string>
10#include <vector>
11
Paul Stewart85aea152013-01-22 09:31:56 -080012#include <base/stringprintf.h>
Paul Stewart71a4d3b2013-01-18 18:12:56 -080013#include <base/string_number_conversions.h>
Chris Masone34af2182011-08-22 11:59:36 -070014#include <base/string_util.h>
15#include <chromeos/dbus/service_constants.h>
16#include <gmock/gmock.h>
17#include <gtest/gtest.h>
18
Paul Stewart26b327e2011-10-19 11:38:09 -070019#include "shill/event_dispatcher.h"
Chris Masone34af2182011-08-22 11:59:36 -070020#include "shill/manager.h"
mukesh agrawalf6b32092013-04-10 15:49:55 -070021#include "shill/metrics.h"
Chris Masone34af2182011-08-22 11:59:36 -070022#include "shill/mock_adaptors.h"
Paul Stewart5baebb72013-03-14 11:43:29 -070023#include "shill/mock_certificate_file.h"
Chris Masone34af2182011-08-22 11:59:36 -070024#include "shill/mock_control.h"
Paul Stewart71a4d3b2013-01-18 18:12:56 -080025#include "shill/mock_log.h"
Paul Stewartecf4cd12012-04-17 11:08:39 -070026#include "shill/mock_nss.h"
Christopher Wiley1ce658d2012-10-10 10:02:03 -070027#include "shill/mock_profile.h"
Chris Masone34af2182011-08-22 11:59:36 -070028#include "shill/mock_service.h"
29#include "shill/mock_store.h"
mukesh agrawal6e277772011-09-29 15:04:23 -070030#include "shill/mock_wifi.h"
Paul Stewart3c504012013-01-17 17:49:58 -080031#include "shill/mock_wifi_provider.h"
Chris Masone34af2182011-08-22 11:59:36 -070032#include "shill/property_store_unittest.h"
mukesh agrawal8a3188d2011-12-01 20:56:44 +000033#include "shill/refptr_types.h"
34#include "shill/wifi_endpoint.h"
mukesh agrawal6e277772011-09-29 15:04:23 -070035#include "shill/wpa_supplicant.h"
Chris Masone34af2182011-08-22 11:59:36 -070036
Albert Chaulk0e1cdea2013-02-27 15:32:55 -080037using base::FilePath;
mukesh agrawald835b202011-10-07 15:26:47 -070038using std::map;
Paul Stewart85aea152013-01-22 09:31:56 -080039using std::set;
Chris Masone34af2182011-08-22 11:59:36 -070040using std::string;
41using std::vector;
Paul Stewartd08f4432011-11-04 07:48:20 -070042using ::testing::_;
mukesh agrawale1d90e92012-02-15 17:36:08 -080043using ::testing::AnyNumber;
Paul Stewartd08f4432011-11-04 07:48:20 -070044using ::testing::DoAll;
Paul Stewart71a4d3b2013-01-18 18:12:56 -080045using ::testing::EndsWith;
Paul Stewart3c504012013-01-17 17:49:58 -080046using ::testing::HasSubstr;
mukesh agrawale1d90e92012-02-15 17:36:08 -080047using ::testing::Mock;
mukesh agrawal6e277772011-09-29 15:04:23 -070048using ::testing::NiceMock;
Paul Stewartd08f4432011-11-04 07:48:20 -070049using ::testing::Return;
50using ::testing::SetArgumentPointee;
51using ::testing::StrEq;
Paul Stewartd8ad3c42012-01-09 12:39:38 -080052using ::testing::StrNe;
Paul Stewart85aea152013-01-22 09:31:56 -080053using ::testing::StrictMock;
Chris Masone34af2182011-08-22 11:59:36 -070054
mukesh agrawalb20776f2012-02-10 16:00:36 -080055namespace shill {
56
Chris Masone34af2182011-08-22 11:59:36 -070057class WiFiServiceTest : public PropertyStoreTest {
58 public:
Paul Stewart3c504012013-01-17 17:49:58 -080059 WiFiServiceTest()
60 : wifi_(new NiceMock<MockWiFi>(
61 control_interface(),
62 dispatcher(),
63 metrics(),
64 manager(),
65 "wifi",
66 fake_mac,
67 0)),
68 simple_ssid_(1, 'a'),
69 simple_ssid_string_("a") {}
Chris Masone34af2182011-08-22 11:59:36 -070070 virtual ~WiFiServiceTest() {}
mukesh agrawal6e277772011-09-29 15:04:23 -070071
72 protected:
73 static const char fake_mac[];
mukesh agrawale1d90e92012-02-15 17:36:08 -080074
Gaurav Shah10109f22011-11-11 20:16:22 -080075 bool CheckConnectable(const std::string &security, const char *passphrase,
Paul Stewart0654ece2013-03-26 15:21:26 -070076 EapCredentials *eap) {
mukesh agrawal29c13a12011-11-24 00:09:19 +000077 Error error;
Paul Stewart3c504012013-01-17 17:49:58 -080078 WiFiServiceRefPtr service = MakeSimpleService(security);
mukesh agrawal29c13a12011-11-24 00:09:19 +000079 if (passphrase)
80 service->SetPassphrase(passphrase, &error);
Gaurav Shah10109f22011-11-11 20:16:22 -080081 if (eap) {
82 service->set_eap(*eap);
83 }
mukesh agrawal29c13a12011-11-24 00:09:19 +000084 return service->connectable();
85 }
mukesh agrawale1d90e92012-02-15 17:36:08 -080086 WiFiEndpoint *MakeEndpoint(const string &ssid, const string &bssid,
mukesh agrawal43970a22013-02-15 16:00:07 -080087 uint16 frequency, int16 signal_dbm,
88 bool has_wpa_property, bool has_rsn_property) {
89 return WiFiEndpoint::MakeEndpoint(
Paul Stewart0654ece2013-03-26 15:21:26 -070090 NULL, wifi(), ssid, bssid, WPASupplicant::kNetworkModeInfrastructure,
mukesh agrawal43970a22013-02-15 16:00:07 -080091 frequency, signal_dbm, has_wpa_property, has_rsn_property);
92 }
93 WiFiEndpoint *MakeOpenEndpoint(const string &ssid, const string &bssid,
94 uint16 frequency, int16 signal_dbm) {
mukesh agrawale1d90e92012-02-15 17:36:08 -080095 return WiFiEndpoint::MakeOpenEndpoint(
Paul Stewart0654ece2013-03-26 15:21:26 -070096 NULL, wifi(), ssid, bssid, WPASupplicant::kNetworkModeInfrastructure,
Paul Stewart3c504012013-01-17 17:49:58 -080097 frequency, signal_dbm);
mukesh agrawale1d90e92012-02-15 17:36:08 -080098 }
Paul Stewart3c504012013-01-17 17:49:58 -080099 WiFiServiceRefPtr MakeSimpleService(const string &security) {
mukesh agrawale1d90e92012-02-15 17:36:08 -0800100 return new WiFiService(control_interface(),
101 dispatcher(),
102 metrics(),
103 manager(),
Paul Stewart3c504012013-01-17 17:49:58 -0800104 &provider_,
105 simple_ssid_,
mukesh agrawale1d90e92012-02-15 17:36:08 -0800106 flimflam::kModeManaged,
Paul Stewart3c504012013-01-17 17:49:58 -0800107 security,
mukesh agrawale1d90e92012-02-15 17:36:08 -0800108 false);
109 }
Paul Stewart3c504012013-01-17 17:49:58 -0800110 WiFiServiceRefPtr MakeGenericService() {
111 return MakeSimpleService(flimflam::kSecurityWep);
112 }
113 void SetWiFiForService(WiFiServiceRefPtr service, WiFiRefPtr wifi) {
114 service->wifi_ = wifi;
115 }
116 WiFiServiceRefPtr MakeServiceWithWiFi(const string &security) {
117 WiFiServiceRefPtr service = MakeSimpleService(security);
118 SetWiFiForService(service, wifi_);
119 return service;
120 }
mukesh agrawale1d90e92012-02-15 17:36:08 -0800121 ServiceMockAdaptor *GetAdaptor(WiFiService *service) {
122 return dynamic_cast<ServiceMockAdaptor *>(service->adaptor());
mukesh agrawal8a3188d2011-12-01 20:56:44 +0000123 }
Paul Stewart3c504012013-01-17 17:49:58 -0800124 Error::Type TestConfigurePassphrase(const string &security,
125 const char *passphrase) {
126 WiFiServiceRefPtr service = MakeSimpleService(security);
127 KeyValueStore args;
128 if (passphrase) {
129 args.SetString(flimflam::kPassphraseProperty, passphrase);
130 }
131 Error error;
132 service->Configure(args, &error);
133 return error.type();
134 }
mukesh agrawal6e277772011-09-29 15:04:23 -0700135 scoped_refptr<MockWiFi> wifi() { return wifi_; }
Paul Stewart3c504012013-01-17 17:49:58 -0800136 MockWiFiProvider *provider() { return &provider_; }
137 string GetAnyDeviceAddress() { return WiFiService::kAnyDeviceAddress; }
138 const vector<uint8_t> &simple_ssid() { return simple_ssid_; }
139 const string &simple_ssid_string() { return simple_ssid_string_; }
mukesh agrawal6e277772011-09-29 15:04:23 -0700140
141 private:
142 scoped_refptr<MockWiFi> wifi_;
Paul Stewart3c504012013-01-17 17:49:58 -0800143 MockWiFiProvider provider_;
144 const vector<uint8_t> simple_ssid_;
145 const string simple_ssid_string_;
Chris Masone34af2182011-08-22 11:59:36 -0700146};
147
mukesh agrawal6e277772011-09-29 15:04:23 -0700148// static
149const char WiFiServiceTest::fake_mac[] = "AaBBcCDDeeFF";
150
Paul Stewart71a4d3b2013-01-18 18:12:56 -0800151MATCHER_P3(ContainsWiFiProperties, ssid, mode, security, "") {
152 string hex_ssid = base::HexEncode(ssid.data(), ssid.size());
153 return
154 arg.ContainsString(WiFiService::kStorageType) &&
155 arg.GetString(WiFiService::kStorageType) == flimflam::kTypeWifi &&
156 arg.ContainsString(WiFiService::kStorageSSID) &&
157 arg.GetString(WiFiService::kStorageSSID) == hex_ssid &&
158 arg.ContainsString(WiFiService::kStorageMode) &&
159 arg.GetString(WiFiService::kStorageMode) == mode &&
160 arg.ContainsString(WiFiService::kStorageSecurityClass) &&
161 arg.GetString(WiFiService::kStorageSecurityClass) == security;
162}
163
Paul Stewartd08f4432011-11-04 07:48:20 -0700164class WiFiServiceSecurityTest : public WiFiServiceTest {
165 public:
Paul Stewartd08f4432011-11-04 07:48:20 -0700166 bool TestStorageSecurityIs(WiFiServiceRefPtr wifi_service,
167 const string &security) {
168 string id = wifi_service->GetStorageIdentifier();
Paul Stewart3c504012013-01-17 17:49:58 -0800169 size_t mac_pos = id.find(StringToLowerASCII(GetAnyDeviceAddress()));
Paul Stewartd08f4432011-11-04 07:48:20 -0700170 EXPECT_NE(mac_pos, string::npos);
171 size_t mode_pos = id.find(string(flimflam::kModeManaged), mac_pos);
172 EXPECT_NE(mode_pos, string::npos);
173 return id.find(string(security), mode_pos) != string::npos;
174 }
175
176 // Test that a service that is created with security |from_security|
177 // gets by default a storage identifier with |to_security| as its
Paul Stewart71a4d3b2013-01-18 18:12:56 -0800178 // security component, and that when saved, it sets the Security
179 // property in to |to_security| as well.
Paul Stewartd08f4432011-11-04 07:48:20 -0700180 bool TestStorageMapping(const string &from_security,
181 const string &to_security) {
Paul Stewart3c504012013-01-17 17:49:58 -0800182 WiFiServiceRefPtr wifi_service = MakeSimpleService(from_security);
Paul Stewart71a4d3b2013-01-18 18:12:56 -0800183 NiceMock<MockStore> mock_store;
184 EXPECT_CALL(mock_store, SetString(_, _, _)).WillRepeatedly(Return(true));
185 EXPECT_CALL(mock_store,
186 SetString(_, WiFiService::kStorageSecurity, from_security))
187 .Times(1);
188 EXPECT_CALL(mock_store,
189 SetString(_, WiFiService::kStorageSecurityClass, to_security))
190 .Times(1);
191 wifi_service->Save(&mock_store);
Paul Stewartd08f4432011-11-04 07:48:20 -0700192 return TestStorageSecurityIs(wifi_service, to_security);
193 }
194
195 // Test whether a service of type |service_security| can load from a
196 // storage interface containing an entry for |storage_security|.
197 // Make sure the result meets |expectation|. If |expectation| is
198 // true, also make sure the service storage identifier changes to
199 // match |storage_security|.
200 bool TestLoadMapping(const string &service_security,
201 const string &storage_security,
202 bool expectation) {
Paul Stewart3c504012013-01-17 17:49:58 -0800203 WiFiServiceRefPtr wifi_service = MakeSimpleService(service_security);
Paul Stewartd08f4432011-11-04 07:48:20 -0700204 NiceMock<MockStore> mock_store;
Paul Stewart71a4d3b2013-01-18 18:12:56 -0800205 EXPECT_CALL(mock_store, GetGroupsWithProperties(_))
206 .WillRepeatedly(Return(set<string>()));
207 const string kStorageId = "storage_id";
208 EXPECT_CALL(mock_store, ContainsGroup(kStorageId))
Paul Stewartd08f4432011-11-04 07:48:20 -0700209 .WillRepeatedly(Return(true));
Paul Stewart71a4d3b2013-01-18 18:12:56 -0800210 set<string> groups;
211 groups.insert(kStorageId);
212 EXPECT_CALL(mock_store, GetGroupsWithProperties(
213 ContainsWiFiProperties(wifi_service->ssid(),
214 flimflam::kModeManaged,
215 storage_security)))
216 .WillRepeatedly(Return(groups));
Paul Stewartd08f4432011-11-04 07:48:20 -0700217 bool is_loadable = wifi_service->IsLoadableFrom(&mock_store);
218 EXPECT_EQ(expectation, is_loadable);
219 bool is_loaded = wifi_service->Load(&mock_store);
220 EXPECT_EQ(expectation, is_loaded);
221
222 if (expectation != is_loadable || expectation != is_loaded) {
223 return false;
224 } else if (!expectation) {
225 return true;
226 } else {
Paul Stewart71a4d3b2013-01-18 18:12:56 -0800227 return wifi_service->GetStorageIdentifier() == kStorageId;
Paul Stewartd08f4432011-11-04 07:48:20 -0700228 }
229 }
230};
231
mukesh agrawale1d90e92012-02-15 17:36:08 -0800232class WiFiServiceUpdateFromEndpointsTest : public WiFiServiceTest {
233 public:
234 WiFiServiceUpdateFromEndpointsTest()
235 : kOkEndpointStrength(WiFiService::SignalToStrength(kOkEndpointSignal)),
236 kBadEndpointStrength(WiFiService::SignalToStrength(kBadEndpointSignal)),
237 kGoodEndpointStrength(
238 WiFiService::SignalToStrength(kGoodEndpointSignal)),
239 service(MakeGenericService()),
240 adaptor(*GetAdaptor(service)) {
mukesh agrawal43970a22013-02-15 16:00:07 -0800241 ok_endpoint = MakeOpenEndpoint(
Paul Stewart3c504012013-01-17 17:49:58 -0800242 simple_ssid_string(), kOkEndpointBssId, kOkEndpointFrequency,
243 kOkEndpointSignal);
mukesh agrawal43970a22013-02-15 16:00:07 -0800244 good_endpoint = MakeOpenEndpoint(
Paul Stewart3c504012013-01-17 17:49:58 -0800245 simple_ssid_string(), kGoodEndpointBssId, kGoodEndpointFrequency,
246 kGoodEndpointSignal);
mukesh agrawal43970a22013-02-15 16:00:07 -0800247 bad_endpoint = MakeOpenEndpoint(
Paul Stewart3c504012013-01-17 17:49:58 -0800248 simple_ssid_string(), kBadEndpointBssId, kBadEndpointFrequency,
249 kBadEndpointSignal);
mukesh agrawale1d90e92012-02-15 17:36:08 -0800250 }
251
252 protected:
253 static const uint16 kOkEndpointFrequency = 2422;
254 static const uint16 kBadEndpointFrequency = 2417;
255 static const uint16 kGoodEndpointFrequency = 2412;
256 static const int16 kOkEndpointSignal = -50;
257 static const int16 kBadEndpointSignal = -75;
258 static const int16 kGoodEndpointSignal = -25;
mukesh agrawal923f14f2012-06-04 16:46:08 -0700259 static const char *kOkEndpointBssId;
260 static const char *kGoodEndpointBssId;
261 static const char *kBadEndpointBssId;
mukesh agrawale1d90e92012-02-15 17:36:08 -0800262 // Can't be both static and const (because initialization requires a
263 // function call). So choose to be just const.
264 const uint8 kOkEndpointStrength;
265 const uint8 kBadEndpointStrength;
266 const uint8 kGoodEndpointStrength;
267 WiFiEndpointRefPtr ok_endpoint;
268 WiFiEndpointRefPtr bad_endpoint;
269 WiFiEndpointRefPtr good_endpoint;
270 WiFiServiceRefPtr service;
271 ServiceMockAdaptor &adaptor;
272};
273
mukesh agrawal923f14f2012-06-04 16:46:08 -0700274const char *WiFiServiceUpdateFromEndpointsTest::kOkEndpointBssId =
275 "00:00:00:00:00:01";
276const char *WiFiServiceUpdateFromEndpointsTest::kGoodEndpointBssId =
277 "00:00:00:00:00:02";
278const char *WiFiServiceUpdateFromEndpointsTest::kBadEndpointBssId =
279 "00:00:00:00:00:03";
mukesh agrawale1d90e92012-02-15 17:36:08 -0800280
Paul Stewart85aea152013-01-22 09:31:56 -0800281class WiFiServiceFixupStorageTest : public WiFiServiceTest {
282 protected:
283 void AddGroup(string group_name) {
284 groups_.insert(group_name);
285 }
286
Paul Stewart71a4d3b2013-01-18 18:12:56 -0800287 void AddServiceEntry(bool has_type, bool has_mode, bool has_security,
288 bool has_security_class) {
Paul Stewart85aea152013-01-22 09:31:56 -0800289 int index = groups_.size();
290 string id = base::StringPrintf("%s_%d_%d_%s_%s", flimflam::kTypeWifi,
291 index, index, flimflam::kModeManaged,
Paul Stewart71a4d3b2013-01-18 18:12:56 -0800292 flimflam::kSecurityWpa);
Paul Stewart85aea152013-01-22 09:31:56 -0800293 AddGroup(id);
294 EXPECT_CALL(store_, GetString(id, WiFiService::kStorageType, _))
295 .WillOnce(Return(has_type));
296 if (!has_type) {
297 EXPECT_CALL(store_, SetString(id, WiFiService::kStorageType,
298 flimflam::kTypeWifi));
299 }
300 EXPECT_CALL(store_, GetString(id, WiFiService::kStorageMode, _))
301 .WillOnce(Return(has_mode));
302 if (!has_mode) {
303 EXPECT_CALL(store_, SetString(id, WiFiService::kStorageMode,
304 flimflam::kModeManaged));
305 }
306 EXPECT_CALL(store_, GetString(id, WiFiService::kStorageSecurity, _))
307 .WillOnce(Return(has_security));
308 if (!has_security) {
309 EXPECT_CALL(store_, SetString(id, WiFiService::kStorageSecurity,
Paul Stewart71a4d3b2013-01-18 18:12:56 -0800310 flimflam::kSecurityWpa));
311 }
312 EXPECT_CALL(store_, GetString(id, WiFiService::kStorageSecurityClass, _))
313 .WillOnce(Return(has_security_class));
314 if (!has_security_class) {
315 EXPECT_CALL(store_, SetString(id, WiFiService::kStorageSecurityClass,
316 flimflam::kSecurityPsk));
Paul Stewart85aea152013-01-22 09:31:56 -0800317 }
318 }
319
320 bool FixupServiceEntries() {
321 EXPECT_CALL(store_, GetGroups()).WillOnce(Return(groups_));
322 return WiFiService::FixupServiceEntries(&store_);
323 }
324
325 private:
326 StrictMock<MockStore> store_;
327 set<string> groups_;
328};
329
Chris Masone34af2182011-08-22 11:59:36 -0700330TEST_F(WiFiServiceTest, StorageId) {
Paul Stewart3c504012013-01-17 17:49:58 -0800331 WiFiServiceRefPtr wifi_service = MakeSimpleService(flimflam::kSecurityNone);
Chris Masone9d779932011-08-25 16:33:41 -0700332 string id = wifi_service->GetStorageIdentifier();
Chris Masone34af2182011-08-22 11:59:36 -0700333 for (uint i = 0; i < id.length(); ++i) {
334 EXPECT_TRUE(id[i] == '_' ||
335 isxdigit(id[i]) ||
336 (isalpha(id[i]) && islower(id[i])));
337 }
Paul Stewart3c504012013-01-17 17:49:58 -0800338 size_t mac_pos = id.find(StringToLowerASCII(GetAnyDeviceAddress()));
Chris Masone34af2182011-08-22 11:59:36 -0700339 EXPECT_NE(mac_pos, string::npos);
340 EXPECT_NE(id.find(string(flimflam::kModeManaged), mac_pos), string::npos);
341}
342
Gaurav Shahda6218a2011-11-11 12:09:33 -0800343// Make sure the passphrase is registered as a write only property
344// by reading and comparing all string properties returned on the store.
345TEST_F(WiFiServiceTest, PassphraseWriteOnly) {
Paul Stewart3c504012013-01-17 17:49:58 -0800346 WiFiServiceRefPtr wifi_service = MakeSimpleService(flimflam::kSecurityWpa);
Gaurav Shahda6218a2011-11-11 12:09:33 -0800347 ReadablePropertyConstIterator<string> it =
348 (wifi_service->store()).GetStringPropertiesIter();
349 for( ; !it.AtEnd(); it.Advance())
350 EXPECT_NE(it.Key(), flimflam::kPassphraseProperty);
351}
352
Thieu Lef7709452011-11-15 01:13:19 +0000353// Make sure setting the passphrase via D-Bus Service.SetProperty validates
354// the passphrase.
355TEST_F(WiFiServiceTest, PassphraseSetPropertyValidation) {
356 // We only spot check two password cases here to make sure the
357 // SetProperty code path does validation. We're not going to exhaustively
358 // test for all types of passwords.
Paul Stewart3c504012013-01-17 17:49:58 -0800359 WiFiServiceRefPtr wifi_service = MakeSimpleService(flimflam::kSecurityWep);
Thieu Lef7709452011-11-15 01:13:19 +0000360 Error error;
361 EXPECT_TRUE(wifi_service->mutable_store()->SetStringProperty(
362 flimflam::kPassphraseProperty, "0:abcde", &error));
363 EXPECT_FALSE(wifi_service->mutable_store()->SetStringProperty(
364 flimflam::kPassphraseProperty, "invalid", &error));
365 EXPECT_EQ(Error::kInvalidPassphrase, error.type());
366}
367
368TEST_F(WiFiServiceTest, PassphraseSetPropertyOpenNetwork) {
Paul Stewart3c504012013-01-17 17:49:58 -0800369 WiFiServiceRefPtr wifi_service = MakeSimpleService(flimflam::kSecurityNone);
Thieu Lef7709452011-11-15 01:13:19 +0000370 Error error;
371 EXPECT_FALSE(wifi_service->mutable_store()->SetStringProperty(
372 flimflam::kPassphraseProperty, "invalid", &error));
373 EXPECT_EQ(Error::kNotSupported, error.type());
374}
375
mukesh agrawald835b202011-10-07 15:26:47 -0700376TEST_F(WiFiServiceTest, NonUTF8SSID) {
377 vector<uint8_t> ssid;
378
379 ssid.push_back(0xff); // not a valid UTF-8 byte-sequence
380 WiFiServiceRefPtr wifi_service = new WiFiService(control_interface(),
381 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -0800382 metrics(),
mukesh agrawald835b202011-10-07 15:26:47 -0700383 manager(),
Paul Stewart3c504012013-01-17 17:49:58 -0800384 provider(),
mukesh agrawald835b202011-10-07 15:26:47 -0700385 ssid,
386 flimflam::kModeManaged,
Paul Stewartced6a0b2011-11-08 15:32:04 -0800387 flimflam::kSecurityNone,
388 false);
mukesh agrawald835b202011-10-07 15:26:47 -0700389 map<string, ::DBus::Variant> properties;
390 // if service doesn't propertly sanitize SSID, this will generate SIGABRT.
391 DBusAdaptor::GetProperties(wifi_service->store(), &properties, NULL);
392}
393
Paul Stewart4108db92013-03-11 12:13:24 -0700394MATCHER(PSKSecurityArgs, "") {
Paul Stewart0654ece2013-03-26 15:21:26 -0700395 return ContainsKey(arg, WPASupplicant::kPropertySecurityProtocol) &&
396 arg.find(WPASupplicant::kPropertySecurityProtocol)->second.
Paul Stewart4108db92013-03-11 12:13:24 -0700397 reader().get_string() == string("WPA RSN") &&
Paul Stewart0654ece2013-03-26 15:21:26 -0700398 ContainsKey(arg, WPASupplicant::kPropertyPreSharedKey);
Gaurav Shahda6218a2011-11-11 12:09:33 -0800399}
400
Paul Stewarta5e7d5f2013-01-09 18:06:15 -0800401MATCHER(WPA80211wSecurityArgs, "") {
Paul Stewart0654ece2013-03-26 15:21:26 -0700402 return ContainsKey(arg, WPASupplicant::kPropertySecurityProtocol) &&
403 ContainsKey(arg, WPASupplicant::kPropertyPreSharedKey) &&
404 ContainsKey(arg, WPASupplicant::kNetworkPropertyIeee80211w);
Paul Stewarta5e7d5f2013-01-09 18:06:15 -0800405}
406
Gaurav Shah10109f22011-11-11 20:16:22 -0800407MATCHER(EAPSecurityArgs, "") {
Paul Stewart0654ece2013-03-26 15:21:26 -0700408 return ContainsKey(arg, WPASupplicant::kNetworkPropertyEapIdentity) &&
409 ContainsKey(arg, WPASupplicant::kNetworkPropertyCaPath);
Gaurav Shah10109f22011-11-11 20:16:22 -0800410}
411
Paul Stewarte2d7c502012-07-16 16:35:10 -0700412MATCHER_P(FrequencyArg, has_arg, "") {
413 return has_arg ==
Paul Stewart0654ece2013-03-26 15:21:26 -0700414 ContainsKey(arg, WPASupplicant::kNetworkPropertyFrequency);
Paul Stewarte2d7c502012-07-16 16:35:10 -0700415}
416
mukesh agrawalf2fd7452011-10-03 16:38:47 -0700417TEST_F(WiFiServiceTest, ConnectTaskWPA) {
Paul Stewart3c504012013-01-17 17:49:58 -0800418 WiFiServiceRefPtr wifi_service = MakeServiceWithWiFi(flimflam::kSecurityWpa);
Paul Stewart4108db92013-03-11 12:13:24 -0700419 EXPECT_CALL(*wifi(), ConnectTo(wifi_service.get(), PSKSecurityArgs()));
Wade Guthrie005bd342012-05-02 09:37:07 -0700420 Error error;
421 wifi_service->SetPassphrase("0:mumblemumblem", &error);
mukesh agrawaldc7b8442012-09-27 13:48:14 -0700422 wifi_service->Connect(NULL, "in test");
mukesh agrawal6e277772011-09-29 15:04:23 -0700423}
424
mukesh agrawalf2fd7452011-10-03 16:38:47 -0700425TEST_F(WiFiServiceTest, ConnectTaskRSN) {
Paul Stewart3c504012013-01-17 17:49:58 -0800426 WiFiServiceRefPtr wifi_service = MakeServiceWithWiFi(flimflam::kSecurityRsn);
Paul Stewart4108db92013-03-11 12:13:24 -0700427 EXPECT_CALL(*wifi(), ConnectTo(wifi_service.get(), PSKSecurityArgs()));
Wade Guthrie005bd342012-05-02 09:37:07 -0700428 Error error;
429 wifi_service->SetPassphrase("0:mumblemumblem", &error);
mukesh agrawaldc7b8442012-09-27 13:48:14 -0700430 wifi_service->Connect(NULL, "in test");
mukesh agrawalf2fd7452011-10-03 16:38:47 -0700431}
432
Christopher Wiley1ce658d2012-10-10 10:02:03 -0700433TEST_F(WiFiServiceTest, ConnectConditions) {
434 Error error;
Paul Stewart3c504012013-01-17 17:49:58 -0800435 WiFiServiceRefPtr wifi_service = MakeServiceWithWiFi(flimflam::kSecurityNone);
Christopher Wiley1ce658d2012-10-10 10:02:03 -0700436 scoped_refptr<MockProfile> mock_profile(
Thieu Le5133b712013-02-19 14:47:21 -0800437 new NiceMock<MockProfile>(control_interface(), metrics(), manager()));
Christopher Wiley1ce658d2012-10-10 10:02:03 -0700438 wifi_service->set_profile(mock_profile);
439 // With nothing else going on, the service should attempt to connect.
440 EXPECT_CALL(*wifi(), ConnectTo(wifi_service.get(), _));
mukesh agrawaldc7b8442012-09-27 13:48:14 -0700441 wifi_service->Connect(&error, "in test");
Christopher Wiley1ce658d2012-10-10 10:02:03 -0700442 Mock::VerifyAndClearExpectations(wifi());
443
444 // But if we're already "connecting" or "connected" then we shouldn't attempt
445 // again.
446 EXPECT_CALL(*wifi(),
447 ConnectTo(wifi_service.get(), _)).Times(0);
448 wifi_service->SetState(Service::kStateAssociating);
mukesh agrawaldc7b8442012-09-27 13:48:14 -0700449 wifi_service->Connect(&error, "in test");
Christopher Wiley1ce658d2012-10-10 10:02:03 -0700450 wifi_service->SetState(Service::kStateConfiguring);
mukesh agrawaldc7b8442012-09-27 13:48:14 -0700451 wifi_service->Connect(&error, "in test");
Christopher Wiley1ce658d2012-10-10 10:02:03 -0700452 wifi_service->SetState(Service::kStateConnected);
mukesh agrawaldc7b8442012-09-27 13:48:14 -0700453 wifi_service->Connect(&error, "in test");
Christopher Wiley1ce658d2012-10-10 10:02:03 -0700454 wifi_service->SetState(Service::kStatePortal);
mukesh agrawaldc7b8442012-09-27 13:48:14 -0700455 wifi_service->Connect(&error, "in test");
Christopher Wiley1ce658d2012-10-10 10:02:03 -0700456 wifi_service->SetState(Service::kStateOnline);
mukesh agrawaldc7b8442012-09-27 13:48:14 -0700457 wifi_service->Connect(&error, "in test");
Christopher Wiley1ce658d2012-10-10 10:02:03 -0700458 Mock::VerifyAndClearExpectations(wifi());
459}
460
Gaurav Shahf8721ee2011-11-07 09:12:46 -0800461TEST_F(WiFiServiceTest, ConnectTaskPSK) {
Paul Stewart3c504012013-01-17 17:49:58 -0800462 WiFiServiceRefPtr wifi_service = MakeServiceWithWiFi(flimflam::kSecurityPsk);
Paul Stewart4108db92013-03-11 12:13:24 -0700463 EXPECT_CALL(*wifi(), ConnectTo(wifi_service.get(), PSKSecurityArgs()));
Wade Guthrie005bd342012-05-02 09:37:07 -0700464 Error error;
465 wifi_service->SetPassphrase("0:mumblemumblem", &error);
mukesh agrawaldc7b8442012-09-27 13:48:14 -0700466 wifi_service->Connect(NULL, "in test");
Gaurav Shahf8721ee2011-11-07 09:12:46 -0800467}
468
Gaurav Shah10109f22011-11-11 20:16:22 -0800469TEST_F(WiFiServiceTest, ConnectTask8021x) {
Paul Stewart3c504012013-01-17 17:49:58 -0800470 WiFiServiceRefPtr service = MakeServiceWithWiFi(flimflam::kSecurity8021x);
Paul Stewart0654ece2013-03-26 15:21:26 -0700471 EapCredentials eap;
Gaurav Shah10109f22011-11-11 20:16:22 -0800472 eap.identity = "identity";
Wade Guthrie005bd342012-05-02 09:37:07 -0700473 eap.password = "mumble";
Paul Stewart3c504012013-01-17 17:49:58 -0800474 service->set_eap(eap);
475 EXPECT_CALL(*wifi(), ConnectTo(service.get(), EAPSecurityArgs()));
mukesh agrawaldc7b8442012-09-27 13:48:14 -0700476 service->Connect(NULL, "in test");
Gaurav Shah10109f22011-11-11 20:16:22 -0800477}
478
Paul Stewarte2d7c502012-07-16 16:35:10 -0700479TEST_F(WiFiServiceTest, ConnectTaskAdHocFrequency) {
480 vector<uint8_t> ssid(1, 'a');
481 WiFiEndpointRefPtr endpoint_nofreq =
mukesh agrawal43970a22013-02-15 16:00:07 -0800482 MakeOpenEndpoint("a", "00:00:00:00:00:01", 0, 0);
Paul Stewarte2d7c502012-07-16 16:35:10 -0700483 WiFiEndpointRefPtr endpoint_freq =
mukesh agrawal43970a22013-02-15 16:00:07 -0800484 MakeOpenEndpoint("a", "00:00:00:00:00:02", 2412, 0);
Paul Stewarte2d7c502012-07-16 16:35:10 -0700485
Paul Stewart3c504012013-01-17 17:49:58 -0800486 WiFiServiceRefPtr wifi_service = MakeServiceWithWiFi(flimflam::kSecurityNone);
Paul Stewarte2d7c502012-07-16 16:35:10 -0700487 wifi_service->AddEndpoint(endpoint_freq);
488 EXPECT_CALL(*wifi(),
489 ConnectTo(wifi_service.get(), FrequencyArg(false)));
mukesh agrawaldc7b8442012-09-27 13:48:14 -0700490 wifi_service->Connect(NULL, "in test");
Paul Stewarte2d7c502012-07-16 16:35:10 -0700491
492 wifi_service = new WiFiService(control_interface(),
493 dispatcher(),
494 metrics(),
495 manager(),
Paul Stewart3c504012013-01-17 17:49:58 -0800496 provider(),
Paul Stewarte2d7c502012-07-16 16:35:10 -0700497 ssid,
498 flimflam::kModeAdhoc,
499 flimflam::kSecurityNone,
500 false);
501 EXPECT_CALL(*wifi(),
502 ConnectTo(wifi_service.get(), FrequencyArg(false)));
Paul Stewart3c504012013-01-17 17:49:58 -0800503 SetWiFiForService(wifi_service, wifi());
mukesh agrawaldc7b8442012-09-27 13:48:14 -0700504 wifi_service->Connect(NULL, "in test");
Paul Stewarte2d7c502012-07-16 16:35:10 -0700505
506 wifi_service = new WiFiService(control_interface(),
507 dispatcher(),
508 metrics(),
509 manager(),
Paul Stewart3c504012013-01-17 17:49:58 -0800510 provider(),
Paul Stewarte2d7c502012-07-16 16:35:10 -0700511 ssid,
512 flimflam::kModeAdhoc,
513 flimflam::kSecurityNone,
514 false);
515 wifi_service->AddEndpoint(endpoint_nofreq);
Paul Stewart3c504012013-01-17 17:49:58 -0800516 SetWiFiForService(wifi_service, wifi());
Paul Stewarte2d7c502012-07-16 16:35:10 -0700517 EXPECT_CALL(*wifi(),
518 ConnectTo(wifi_service.get(), FrequencyArg(false)));
mukesh agrawaldc7b8442012-09-27 13:48:14 -0700519 wifi_service->Connect(NULL, "in test");
Paul Stewarte2d7c502012-07-16 16:35:10 -0700520
521 wifi_service = new WiFiService(control_interface(),
522 dispatcher(),
523 metrics(),
524 manager(),
Paul Stewart3c504012013-01-17 17:49:58 -0800525 provider(),
Paul Stewarte2d7c502012-07-16 16:35:10 -0700526 ssid,
527 flimflam::kModeAdhoc,
528 flimflam::kSecurityNone,
529 false);
530 wifi_service->AddEndpoint(endpoint_freq);
Paul Stewart3c504012013-01-17 17:49:58 -0800531 SetWiFiForService(wifi_service, wifi());
Paul Stewarte2d7c502012-07-16 16:35:10 -0700532 EXPECT_CALL(*wifi(),
533 ConnectTo(wifi_service.get(), FrequencyArg(true)));
mukesh agrawaldc7b8442012-09-27 13:48:14 -0700534 wifi_service->Connect(NULL, "in test");
Paul Stewarte2d7c502012-07-16 16:35:10 -0700535}
536
Paul Stewarta5e7d5f2013-01-09 18:06:15 -0800537TEST_F(WiFiServiceTest, ConnectTaskWPA80211w) {
Paul Stewart3c504012013-01-17 17:49:58 -0800538 WiFiServiceRefPtr wifi_service = MakeServiceWithWiFi(flimflam::kSecurityPsk);
mukesh agrawal43970a22013-02-15 16:00:07 -0800539 WiFiEndpointRefPtr endpoint =
540 MakeOpenEndpoint("a", "00:00:00:00:00:01", 0, 0);
Paul Stewarta5e7d5f2013-01-09 18:06:15 -0800541 endpoint->ieee80211w_required_ = true;
542 wifi_service->AddEndpoint(endpoint);
543 Error error;
544 wifi_service->SetPassphrase("0:mumblemumblem", &error);
545 EXPECT_CALL(*wifi(),
546 ConnectTo(wifi_service.get(), WPA80211wSecurityArgs()));
mukesh agrawaldc7b8442012-09-27 13:48:14 -0700547 wifi_service->Connect(NULL, "in test");
Paul Stewarta5e7d5f2013-01-09 18:06:15 -0800548}
549
Thieu Lef4cbda92011-11-10 23:41:24 +0000550MATCHER(WEPSecurityArgsKeyIndex0, "") {
Paul Stewart0654ece2013-03-26 15:21:26 -0700551 return ContainsKey(arg, WPASupplicant::kPropertyAuthAlg) &&
552 ContainsKey(arg, WPASupplicant::kPropertyWEPKey + std::string("0")) &&
553 ContainsKey(arg, WPASupplicant::kPropertyWEPTxKeyIndex) &&
554 (arg.find(WPASupplicant::kPropertyWEPTxKeyIndex)->second.
Thieu Lef4cbda92011-11-10 23:41:24 +0000555 reader().get_uint32() == 0);
556}
557
558MATCHER(WEPSecurityArgsKeyIndex1, "") {
Paul Stewart0654ece2013-03-26 15:21:26 -0700559 return ContainsKey(arg, WPASupplicant::kPropertyAuthAlg) &&
560 ContainsKey(arg, WPASupplicant::kPropertyWEPKey + std::string("1")) &&
561 ContainsKey(arg, WPASupplicant::kPropertyWEPTxKeyIndex) &&
562 (arg.find(WPASupplicant::kPropertyWEPTxKeyIndex)->second.
Thieu Lef4cbda92011-11-10 23:41:24 +0000563 reader().get_uint32() == 1);
564}
565
566MATCHER(WEPSecurityArgsKeyIndex2, "") {
Paul Stewart0654ece2013-03-26 15:21:26 -0700567 return ContainsKey(arg, WPASupplicant::kPropertyAuthAlg) &&
568 ContainsKey(arg, WPASupplicant::kPropertyWEPKey + std::string("2")) &&
569 ContainsKey(arg, WPASupplicant::kPropertyWEPTxKeyIndex) &&
570 (arg.find(WPASupplicant::kPropertyWEPTxKeyIndex)->second.
Thieu Lef4cbda92011-11-10 23:41:24 +0000571 reader().get_uint32() == 2);
572}
573
574MATCHER(WEPSecurityArgsKeyIndex3, "") {
Paul Stewart0654ece2013-03-26 15:21:26 -0700575 return ContainsKey(arg, WPASupplicant::kPropertyAuthAlg) &&
576 ContainsKey(arg, WPASupplicant::kPropertyWEPKey + std::string("3")) &&
577 ContainsKey(arg, WPASupplicant::kPropertyWEPTxKeyIndex) &&
578 (arg.find(WPASupplicant::kPropertyWEPTxKeyIndex)->second.
Thieu Lef4cbda92011-11-10 23:41:24 +0000579 reader().get_uint32() == 3);
580}
581
582TEST_F(WiFiServiceTest, ConnectTaskWEP) {
Paul Stewart3c504012013-01-17 17:49:58 -0800583 WiFiServiceRefPtr wifi_service = MakeServiceWithWiFi(flimflam::kSecurityWep);
Thieu Lef4cbda92011-11-10 23:41:24 +0000584 Error error;
585 wifi_service->SetPassphrase("0:abcdefghijklm", &error);
586 EXPECT_CALL(*wifi(),
587 ConnectTo(wifi_service.get(), WEPSecurityArgsKeyIndex0()));
mukesh agrawaldc7b8442012-09-27 13:48:14 -0700588 wifi_service->Connect(NULL, "in test");
Thieu Lef4cbda92011-11-10 23:41:24 +0000589
590 wifi_service->SetPassphrase("abcdefghijklm", &error);
591 EXPECT_CALL(*wifi(),
592 ConnectTo(wifi_service.get(), WEPSecurityArgsKeyIndex0()));
mukesh agrawaldc7b8442012-09-27 13:48:14 -0700593 wifi_service->Connect(NULL, "in test");
Thieu Lef4cbda92011-11-10 23:41:24 +0000594
595 wifi_service->SetPassphrase("1:abcdefghijklm", &error);
596 EXPECT_CALL(*wifi(),
597 ConnectTo(wifi_service.get(), WEPSecurityArgsKeyIndex1()));
mukesh agrawaldc7b8442012-09-27 13:48:14 -0700598 wifi_service->Connect(NULL, "in test");
Thieu Lef4cbda92011-11-10 23:41:24 +0000599
600 wifi_service->SetPassphrase("2:abcdefghijklm", &error);
601 EXPECT_CALL(*wifi(),
602 ConnectTo(wifi_service.get(), WEPSecurityArgsKeyIndex2()));
mukesh agrawaldc7b8442012-09-27 13:48:14 -0700603 wifi_service->Connect(NULL, "in test");
Thieu Lef4cbda92011-11-10 23:41:24 +0000604
605 wifi_service->SetPassphrase("3:abcdefghijklm", &error);
606 EXPECT_CALL(*wifi(),
607 ConnectTo(wifi_service.get(), WEPSecurityArgsKeyIndex3()));
mukesh agrawaldc7b8442012-09-27 13:48:14 -0700608 wifi_service->Connect(NULL, "in test");
Thieu Lef4cbda92011-11-10 23:41:24 +0000609}
610
Gaurav Shah29d68882012-01-30 19:06:42 -0800611
612MATCHER(DynamicWEPArgs, "") {
Paul Stewart0654ece2013-03-26 15:21:26 -0700613 return ContainsKey(arg, WPASupplicant::kNetworkPropertyEapIdentity) &&
614 ContainsKey(arg, WPASupplicant::kNetworkPropertyCaPath) &&
615 !ContainsKey(arg, WPASupplicant::kPropertySecurityProtocol);
Gaurav Shah29d68882012-01-30 19:06:42 -0800616}
617
618// Dynamic WEP + 802.1x.
619TEST_F(WiFiServiceTest, ConnectTaskDynamicWEP) {
Paul Stewart3c504012013-01-17 17:49:58 -0800620 WiFiServiceRefPtr wifi_service = MakeServiceWithWiFi(flimflam::kSecurityWep);
Gaurav Shah29d68882012-01-30 19:06:42 -0800621
Paul Stewart0654ece2013-03-26 15:21:26 -0700622 EapCredentials eap;
Gaurav Shah29d68882012-01-30 19:06:42 -0800623 eap.key_management = "IEEE8021X";
624 eap.identity = "something";
Wade Guthrie005bd342012-05-02 09:37:07 -0700625 eap.password = "mumble";
Gaurav Shah29d68882012-01-30 19:06:42 -0800626 wifi_service->set_eap(eap);
627 EXPECT_CALL(*wifi(),
628 ConnectTo(wifi_service.get(), DynamicWEPArgs()));
mukesh agrawaldc7b8442012-09-27 13:48:14 -0700629 wifi_service->Connect(NULL, "in test");
Gaurav Shah29d68882012-01-30 19:06:42 -0800630}
631
Paul Stewart835934a2012-12-06 19:27:09 -0800632TEST_F(WiFiServiceTest, SetPassphraseRemovesCachedCredentials) {
633 vector<uint8_t> ssid(5);
Paul Stewart3c504012013-01-17 17:49:58 -0800634 WiFiServiceRefPtr wifi_service = MakeServiceWithWiFi(flimflam::kSecurityRsn);
Paul Stewart835934a2012-12-06 19:27:09 -0800635
636 const string kPassphrase = "abcdefgh";
637
638 {
639 Error error;
640 // A changed passphrase should trigger cache removal.
641 EXPECT_CALL(*wifi(), ClearCachedCredentials(wifi_service.get()));
642 wifi_service->SetPassphrase(kPassphrase, &error);
643 Mock::VerifyAndClearExpectations(wifi());
644 EXPECT_TRUE(error.IsSuccess());
645 }
646
647 {
648 Error error;
649 // An unchanged passphrase should not trigger cache removal.
650 EXPECT_CALL(*wifi(), ClearCachedCredentials(_)).Times(0);
651 wifi_service->SetPassphrase(kPassphrase, &error);
652 Mock::VerifyAndClearExpectations(wifi());
653 EXPECT_TRUE(error.IsSuccess());
654 }
655
656 {
657 Error error;
658 // A modified passphrase should trigger cache removal.
659 EXPECT_CALL(*wifi(), ClearCachedCredentials(wifi_service.get()));
660 wifi_service->SetPassphrase(kPassphrase + "X", &error);
661 Mock::VerifyAndClearExpectations(wifi());
662 EXPECT_TRUE(error.IsSuccess());
663 }
664
665 {
666 Error error;
667 // A cleared passphrase should also trigger cache removal.
668 EXPECT_CALL(*wifi(), ClearCachedCredentials(wifi_service.get()));
669 wifi_service->ClearPassphrase(&error);
670 Mock::VerifyAndClearExpectations(wifi());
671 EXPECT_TRUE(error.IsSuccess());
672 }
673
674 {
675 Error error;
676 // An invalid passphrase should not trigger cache removal.
677 EXPECT_CALL(*wifi(), ClearCachedCredentials(_)).Times(0);
678 wifi_service->SetPassphrase("", &error);
679 Mock::VerifyAndClearExpectations(wifi());
680 EXPECT_FALSE(error.IsSuccess());
681 }
682
683 {
684 // Any change to EAP parameters (including a null one) will trigger cache
685 // removal. This is a lot less granular than the passphrase checks above.
686 EXPECT_CALL(*wifi(), ClearCachedCredentials(wifi_service.get()));
Paul Stewart0654ece2013-03-26 15:21:26 -0700687 wifi_service->set_eap(EapCredentials());
Paul Stewart835934a2012-12-06 19:27:09 -0800688 Mock::VerifyAndClearExpectations(wifi());
689 }
690}
691
Paul Stewartd08f4432011-11-04 07:48:20 -0700692TEST_F(WiFiServiceTest, LoadHidden) {
Paul Stewart3c504012013-01-17 17:49:58 -0800693 WiFiServiceRefPtr service = MakeSimpleService(flimflam::kSecurityNone);
Paul Stewartd08f4432011-11-04 07:48:20 -0700694 ASSERT_FALSE(service->hidden_ssid_);
695 NiceMock<MockStore> mock_store;
696 const string storage_id = service->GetStorageIdentifier();
Paul Stewart71a4d3b2013-01-18 18:12:56 -0800697 set<string> groups;
698 groups.insert(storage_id);
Paul Stewartd08f4432011-11-04 07:48:20 -0700699 EXPECT_CALL(mock_store, ContainsGroup(StrEq(storage_id)))
700 .WillRepeatedly(Return(true));
Paul Stewart71a4d3b2013-01-18 18:12:56 -0800701 EXPECT_CALL(mock_store, GetGroupsWithProperties(
702 ContainsWiFiProperties(
Paul Stewart3c504012013-01-17 17:49:58 -0800703 simple_ssid(), flimflam::kModeManaged, flimflam::kSecurityNone)))
Paul Stewart71a4d3b2013-01-18 18:12:56 -0800704 .WillRepeatedly(Return(groups));
Paul Stewartd08f4432011-11-04 07:48:20 -0700705 EXPECT_CALL(mock_store, GetBool(_, _, _))
706 .WillRepeatedly(Return(false));
707 EXPECT_CALL(mock_store,
708 GetBool(StrEq(storage_id), WiFiService::kStorageHiddenSSID, _))
709 .WillRepeatedly(DoAll(SetArgumentPointee<2>(true), Return(true)));
710 EXPECT_TRUE(service->Load(&mock_store));
711 EXPECT_TRUE(service->hidden_ssid_);
712}
713
Paul Stewart71a4d3b2013-01-18 18:12:56 -0800714TEST_F(WiFiServiceTest, LoadMultipleMatchingGroups) {
Paul Stewart3c504012013-01-17 17:49:58 -0800715 WiFiServiceRefPtr service = MakeServiceWithWiFi(flimflam::kSecurityNone);
Paul Stewart71a4d3b2013-01-18 18:12:56 -0800716 set<string> groups;
717 groups.insert("id0");
718 groups.insert("id1");
719 // Make sure we retain the first matched group in the same way that
720 // WiFiService::Load() will.
721 string first_group = *groups.begin();
722
723 NiceMock<MockStore> mock_store;
724 EXPECT_CALL(mock_store, GetGroupsWithProperties(
725 ContainsWiFiProperties(
Paul Stewart3c504012013-01-17 17:49:58 -0800726 simple_ssid(), flimflam::kModeManaged, flimflam::kSecurityNone)))
Paul Stewart71a4d3b2013-01-18 18:12:56 -0800727 .WillRepeatedly(Return(groups));
728 EXPECT_CALL(mock_store, ContainsGroup(first_group))
729 .WillRepeatedly(Return(true));
730 EXPECT_CALL(mock_store, ContainsGroup(StrNe(first_group))).Times(0);
731 EXPECT_CALL(mock_store, GetBool(first_group, _, _))
732 .WillRepeatedly(Return(false));
733 EXPECT_CALL(mock_store, GetBool(StrNe(first_group), _, _)).Times(0);
734 ScopedMockLog log;
735 EXPECT_CALL(log, Log(_, _, _)).Times(AnyNumber());
736 EXPECT_CALL(log, Log(logging::LOG_WARNING, _,
737 EndsWith("choosing the first.")));
738 EXPECT_TRUE(service->Load(&mock_store));
739}
740
Paul Stewartd08f4432011-11-04 07:48:20 -0700741TEST_F(WiFiServiceSecurityTest, WPAMapping) {
742 EXPECT_TRUE(TestStorageMapping(flimflam::kSecurityRsn,
743 flimflam::kSecurityPsk));
744 EXPECT_TRUE(TestStorageMapping(flimflam::kSecurityWpa,
745 flimflam::kSecurityPsk));
746 EXPECT_TRUE(TestStorageMapping(flimflam::kSecurityPsk,
747 flimflam::kSecurityPsk));
748 EXPECT_TRUE(TestStorageMapping(flimflam::kSecurityWep,
749 flimflam::kSecurityWep));
750 EXPECT_TRUE(TestStorageMapping(flimflam::kSecurityNone,
751 flimflam::kSecurityNone));
Gaurav Shah10109f22011-11-11 20:16:22 -0800752 EXPECT_TRUE(TestStorageMapping(flimflam::kSecurity8021x,
753 flimflam::kSecurity8021x));
Paul Stewartd08f4432011-11-04 07:48:20 -0700754}
755
756TEST_F(WiFiServiceSecurityTest, LoadMapping) {
757 EXPECT_TRUE(TestLoadMapping(flimflam::kSecurityRsn,
758 flimflam::kSecurityPsk,
759 true));
760 EXPECT_TRUE(TestLoadMapping(flimflam::kSecurityRsn,
761 flimflam::kSecurityRsn,
Paul Stewart71a4d3b2013-01-18 18:12:56 -0800762 false));
Paul Stewartd08f4432011-11-04 07:48:20 -0700763 EXPECT_TRUE(TestLoadMapping(flimflam::kSecurityRsn,
764 flimflam::kSecurityWpa,
765 false));
766 EXPECT_TRUE(TestLoadMapping(flimflam::kSecurityWpa,
767 flimflam::kSecurityPsk,
768 true));
769 EXPECT_TRUE(TestLoadMapping(flimflam::kSecurityWpa,
770 flimflam::kSecurityWpa,
Paul Stewart71a4d3b2013-01-18 18:12:56 -0800771 false));
Paul Stewartd08f4432011-11-04 07:48:20 -0700772 EXPECT_TRUE(TestLoadMapping(flimflam::kSecurityWpa,
773 flimflam::kSecurityRsn,
774 false));
775 EXPECT_TRUE(TestLoadMapping(flimflam::kSecurityWep,
776 flimflam::kSecurityWep,
777 true));
778 EXPECT_TRUE(TestLoadMapping(flimflam::kSecurityWep,
779 flimflam::kSecurityPsk,
780 false));
781}
782
Paul Stewartd8ad3c42012-01-09 12:39:38 -0800783TEST_F(WiFiServiceTest, LoadAndUnloadPassphrase) {
Paul Stewart3c504012013-01-17 17:49:58 -0800784 WiFiServiceRefPtr service = MakeSimpleService(flimflam::kSecurityPsk);
Paul Stewartd8ad3c42012-01-09 12:39:38 -0800785 NiceMock<MockStore> mock_store;
786 const string storage_id = service->GetStorageIdentifier();
787 EXPECT_CALL(mock_store, ContainsGroup(StrEq(storage_id)))
788 .WillRepeatedly(Return(true));
Paul Stewart71a4d3b2013-01-18 18:12:56 -0800789 set<string> groups;
790 groups.insert(storage_id);
791 EXPECT_CALL(mock_store, GetGroupsWithProperties(
792 ContainsWiFiProperties(
Paul Stewart3c504012013-01-17 17:49:58 -0800793 simple_ssid(), flimflam::kModeManaged, flimflam::kSecurityPsk)))
Paul Stewart71a4d3b2013-01-18 18:12:56 -0800794 .WillRepeatedly(Return(groups));
Paul Stewartd8ad3c42012-01-09 12:39:38 -0800795 EXPECT_CALL(mock_store, GetBool(_, _, _))
796 .WillRepeatedly(Return(false));
797 const string passphrase = "passphrase";
798 EXPECT_CALL(mock_store,
799 GetCryptedString(StrEq(storage_id),
800 WiFiService::kStoragePassphrase, _))
801 .WillRepeatedly(DoAll(SetArgumentPointee<2>(passphrase), Return(true)));
802 EXPECT_CALL(mock_store,
803 GetCryptedString(StrEq(storage_id),
804 StrNe(WiFiService::kStoragePassphrase), _))
805 .WillRepeatedly(Return(false));
806 EXPECT_TRUE(service->need_passphrase_);
807 EXPECT_TRUE(service->Load(&mock_store));
808 EXPECT_EQ(passphrase, service->passphrase_);
809 EXPECT_TRUE(service->connectable());
810 EXPECT_FALSE(service->need_passphrase_);
811 service->Unload();
812 EXPECT_EQ(string(""), service->passphrase_);
813 EXPECT_FALSE(service->connectable());
814 EXPECT_TRUE(service->need_passphrase_);
815}
816
Christopher Wiley27b47232012-11-02 13:13:00 -0700817TEST_F(WiFiServiceTest, ConfigureMakesConnectable) {
818 string guid("legit_guid");
819 KeyValueStore args;
820 args.SetString(flimflam::kEapIdentityProperty, "legit_identity");
821 args.SetString(flimflam::kEapPasswordProperty, "legit_password");
822 args.SetString(flimflam::kEAPEAPProperty, "PEAP");
823 args.SetString(flimflam::kGuidProperty, guid);
824 Error error;
Christopher Wiley27b47232012-11-02 13:13:00 -0700825
Paul Stewart3c504012013-01-17 17:49:58 -0800826 WiFiServiceRefPtr service = MakeSimpleService(flimflam::kSecurity8021x);
Christopher Wiley27b47232012-11-02 13:13:00 -0700827 // Hack the GUID in so that we don't have to mess about with WiFi to regsiter
828 // our service. This way, Manager will handle the lookup itself.
829 service->set_guid(guid);
830 manager()->RegisterService(service);
831 EXPECT_FALSE(service->connectable());
832 EXPECT_EQ(service.get(), manager()->GetService(args, &error).get());
833 EXPECT_TRUE(error.IsSuccess());
834 EXPECT_TRUE(service->connectable());
835}
836
Paul Stewart3c504012013-01-17 17:49:58 -0800837TEST_F(WiFiServiceTest, ConfigurePassphrase) {
838 EXPECT_EQ(Error::kNotSupported,
839 TestConfigurePassphrase(flimflam::kSecurityNone, ""));
840 EXPECT_EQ(Error::kNotSupported,
841 TestConfigurePassphrase(flimflam::kSecurityNone, "foo"));
842 EXPECT_EQ(Error::kSuccess,
843 TestConfigurePassphrase(flimflam::kSecurityWep, NULL));
844 EXPECT_EQ(Error::kInvalidPassphrase,
845 TestConfigurePassphrase(flimflam::kSecurityWep, ""));
846 EXPECT_EQ(Error::kInvalidPassphrase,
847 TestConfigurePassphrase(flimflam::kSecurityWep, "abcd"));
848 EXPECT_EQ(Error::kSuccess,
849 TestConfigurePassphrase(flimflam::kSecurityWep, "abcde"));
850 EXPECT_EQ(Error::kSuccess,
851 TestConfigurePassphrase(flimflam::kSecurityWep, "abcdefghijklm"));
852 EXPECT_EQ(Error::kSuccess,
853 TestConfigurePassphrase(flimflam::kSecurityWep, "0:abcdefghijklm"));
854 EXPECT_EQ(Error::kSuccess,
855 TestConfigurePassphrase(flimflam::kSecurityWep, "0102030405"));
856 EXPECT_EQ(Error::kInvalidPassphrase,
857 TestConfigurePassphrase(flimflam::kSecurityWep, "0x0102030405"));
858 EXPECT_EQ(Error::kInvalidPassphrase,
859 TestConfigurePassphrase(flimflam::kSecurityWep, "O102030405"));
860 EXPECT_EQ(Error::kInvalidPassphrase,
861 TestConfigurePassphrase(flimflam::kSecurityWep, "1:O102030405"));
862 EXPECT_EQ(Error::kInvalidPassphrase,
863 TestConfigurePassphrase(flimflam::kSecurityWep, "1:0xO102030405"));
864 EXPECT_EQ(Error::kInvalidPassphrase,
865 TestConfigurePassphrase(flimflam::kSecurityWep, "0xO102030405"));
866 EXPECT_EQ(Error::kSuccess,
867 TestConfigurePassphrase(flimflam::kSecurityWep,
868 "0102030405060708090a0b0c0d"));
869 EXPECT_EQ(Error::kSuccess,
870 TestConfigurePassphrase(flimflam::kSecurityWep,
871 "0102030405060708090A0B0C0D"));
872 EXPECT_EQ(Error::kSuccess,
873 TestConfigurePassphrase(flimflam::kSecurityWep,
874 "0:0102030405060708090a0b0c0d"));
875 EXPECT_EQ(Error::kSuccess,
876 TestConfigurePassphrase(flimflam::kSecurityWep,
877 "0:0x0102030405060708090a0b0c0d"));
878 EXPECT_EQ(Error::kSuccess,
879 TestConfigurePassphrase(flimflam::kSecurityWpa, NULL));
880 EXPECT_EQ(Error::kSuccess,
881 TestConfigurePassphrase(flimflam::kSecurityWpa, "secure password"));
882 EXPECT_EQ(Error::kInvalidPassphrase,
883 TestConfigurePassphrase(flimflam::kSecurityWpa, ""));
884 EXPECT_EQ(Error::kSuccess, TestConfigurePassphrase(
885 flimflam::kSecurityWpa,
886 string(IEEE_80211::kWPAAsciiMinLen, 'Z').c_str()));
887 EXPECT_EQ(Error::kSuccess, TestConfigurePassphrase(
888 flimflam::kSecurityWpa,
889 string(IEEE_80211::kWPAAsciiMaxLen, 'Z').c_str()));
890 // subtle: invalid length for hex key, but valid as ascii passphrase
891 EXPECT_EQ(Error::kSuccess, TestConfigurePassphrase(
892 flimflam::kSecurityWpa,
893 string(IEEE_80211::kWPAHexLen-1, '1').c_str()));
894 EXPECT_EQ(Error::kSuccess, TestConfigurePassphrase(
895 flimflam::kSecurityWpa,
896 string(IEEE_80211::kWPAHexLen, '1').c_str()));
897 EXPECT_EQ(Error::kInvalidPassphrase, TestConfigurePassphrase(
898 flimflam::kSecurityWpa,
899 string(IEEE_80211::kWPAAsciiMinLen-1, 'Z').c_str()));
900 EXPECT_EQ(Error::kInvalidPassphrase, TestConfigurePassphrase(
901 flimflam::kSecurityWpa,
902 string(IEEE_80211::kWPAAsciiMaxLen+1, 'Z').c_str()));
903 EXPECT_EQ(Error::kInvalidPassphrase, TestConfigurePassphrase(
904 flimflam::kSecurityWpa,
905 string(IEEE_80211::kWPAHexLen+1, '1').c_str()));
906}
907
908TEST_F(WiFiServiceTest, ConfigureRedundantProperties) {
909 WiFiServiceRefPtr service = MakeSimpleService(flimflam::kSecurityNone);
910 KeyValueStore args;
911 args.SetString(flimflam::kTypeProperty, flimflam::kTypeWifi);
912 args.SetString(flimflam::kSSIDProperty, simple_ssid_string());
913 args.SetString(flimflam::kSecurityProperty, flimflam::kSecurityNone);
914 const string kGUID = "aguid";
915 args.SetString(flimflam::kGuidProperty, kGUID);
916
917 EXPECT_EQ("", service->guid());
918 Error error;
919 service->Configure(args, &error);
920 EXPECT_TRUE(error.IsSuccess());
921 EXPECT_EQ(kGUID, service->guid());
922}
923
924TEST_F(WiFiServiceTest, DisconnectWithWiFi) {
925 WiFiServiceRefPtr service = MakeServiceWithWiFi(flimflam::kSecurityWep);
926 EXPECT_CALL(*wifi(), DisconnectFrom(service.get())).Times(1);
927 Error error;
928 service->Disconnect(&error);
929}
930
931TEST_F(WiFiServiceTest, DisconnectWithoutWiFi) {
932 WiFiServiceRefPtr service = MakeSimpleService(flimflam::kSecurityWep);
933 EXPECT_CALL(*wifi(), DisconnectFrom(_)).Times(0);
934 Error error;
935 service->Disconnect(&error);
936 EXPECT_EQ(Error::kOperationFailed, error.type());
937}
938
939TEST_F(WiFiServiceTest, DisconnectWithoutWiFiWhileAssociating) {
940 WiFiServiceRefPtr service = MakeSimpleService(flimflam::kSecurityWep);
941 EXPECT_CALL(*wifi(), DisconnectFrom(_)).Times(0);
942 service->SetState(Service::kStateAssociating);
943 ScopedMockLog log;
944 EXPECT_CALL(log, Log(_, _, _)).Times(AnyNumber());
945 EXPECT_CALL(log, Log(logging::LOG_ERROR, _,
946 HasSubstr("WiFi endpoints do not (yet) exist.")));
947 Error error;
948 service->Disconnect(&error);
949 EXPECT_EQ(Error::kOperationFailed, error.type());
950}
951
Paul Stewart835934a2012-12-06 19:27:09 -0800952TEST_F(WiFiServiceTest, UnloadAndClearCacheWEP) {
Paul Stewart3c504012013-01-17 17:49:58 -0800953 WiFiServiceRefPtr service = MakeServiceWithWiFi(flimflam::kSecurityWep);
Paul Stewart835934a2012-12-06 19:27:09 -0800954 EXPECT_CALL(*wifi(), ClearCachedCredentials(service.get())).Times(1);
955 EXPECT_CALL(*wifi(), DisconnectFrom(service.get())).Times(1);
Paul Stewart66c86002012-01-30 18:00:52 -0800956 service->Unload();
957}
958
959TEST_F(WiFiServiceTest, UnloadAndClearCache8021x) {
Paul Stewart3c504012013-01-17 17:49:58 -0800960 WiFiServiceRefPtr service = MakeServiceWithWiFi(flimflam::kSecurity8021x);
Paul Stewart835934a2012-12-06 19:27:09 -0800961 EXPECT_CALL(*wifi(), ClearCachedCredentials(service.get())).Times(1);
962 EXPECT_CALL(*wifi(), DisconnectFrom(service.get())).Times(1);
Paul Stewart66c86002012-01-30 18:00:52 -0800963 service->Unload();
964}
965
Paul Stewart0756db92012-01-27 08:34:47 -0800966TEST_F(WiFiServiceTest, ParseStorageIdentifierNone) {
Paul Stewart3c504012013-01-17 17:49:58 -0800967 WiFiServiceRefPtr service = MakeSimpleService(flimflam::kSecurityNone);
Paul Stewarta41e38d2011-11-11 07:47:29 -0800968 const string storage_id = service->GetStorageIdentifier();
969 string address;
970 string mode;
971 string security;
972 EXPECT_TRUE(service->ParseStorageIdentifier(storage_id, &address, &mode,
973 &security));
Paul Stewart3c504012013-01-17 17:49:58 -0800974 EXPECT_EQ(StringToLowerASCII(GetAnyDeviceAddress()), address);
Paul Stewarta41e38d2011-11-11 07:47:29 -0800975 EXPECT_EQ(flimflam::kModeManaged, mode);
976 EXPECT_EQ(flimflam::kSecurityNone, security);
977}
978
Paul Stewart0756db92012-01-27 08:34:47 -0800979TEST_F(WiFiServiceTest, ParseStorageIdentifier8021x) {
980 // Do a separate test for 802.1x, since kSecurity8021x contains a "_",
981 // which needs to be dealt with specially in the parser.
Paul Stewart3c504012013-01-17 17:49:58 -0800982 WiFiServiceRefPtr service = MakeSimpleService(flimflam::kSecurity8021x);
Paul Stewart0756db92012-01-27 08:34:47 -0800983 const string storage_id = service->GetStorageIdentifier();
984 string address;
985 string mode;
986 string security;
987 EXPECT_TRUE(service->ParseStorageIdentifier(storage_id, &address, &mode,
988 &security));
Paul Stewart3c504012013-01-17 17:49:58 -0800989 EXPECT_EQ(StringToLowerASCII(GetAnyDeviceAddress()), address);
Paul Stewart0756db92012-01-27 08:34:47 -0800990 EXPECT_EQ(flimflam::kModeManaged, mode);
991 EXPECT_EQ(flimflam::kSecurity8021x, security);
992}
993
Paul Stewart85aea152013-01-22 09:31:56 -0800994TEST_F(WiFiServiceFixupStorageTest, FixedEntries) {
995 const string kNonWiFiId = "vpn_foo";
996 const string kUnparsableWiFiId = "wifi_foo";
997
998 AddGroup(kNonWiFiId);
999 AddGroup(kUnparsableWiFiId);
Paul Stewart71a4d3b2013-01-18 18:12:56 -08001000 AddServiceEntry(true, true, true, true);
1001 AddServiceEntry(false, false, false, false);
1002 AddServiceEntry(true, true, true, true);
1003 AddServiceEntry(false, false, false, false);
Paul Stewart85aea152013-01-22 09:31:56 -08001004 EXPECT_TRUE(FixupServiceEntries());
1005}
1006
1007TEST_F(WiFiServiceFixupStorageTest, NoFixedEntries) {
1008 const string kNonWiFiId = "vpn_foo";
1009 const string kUnparsableWiFiId = "wifi_foo";
1010
1011 AddGroup(kNonWiFiId);
1012 AddGroup(kUnparsableWiFiId);
Paul Stewart71a4d3b2013-01-18 18:12:56 -08001013 AddServiceEntry(true, true, true, true);
Paul Stewart85aea152013-01-22 09:31:56 -08001014 EXPECT_FALSE(FixupServiceEntries());
1015}
1016
1017TEST_F(WiFiServiceFixupStorageTest, MissingTypeProperty) {
Paul Stewart71a4d3b2013-01-18 18:12:56 -08001018 AddServiceEntry(false, true, true, true);
Paul Stewart85aea152013-01-22 09:31:56 -08001019 EXPECT_TRUE(FixupServiceEntries());
1020}
1021
1022TEST_F(WiFiServiceFixupStorageTest, MissingModeProperty) {
Paul Stewart71a4d3b2013-01-18 18:12:56 -08001023 AddServiceEntry(true, false, true, true);
Paul Stewart85aea152013-01-22 09:31:56 -08001024 EXPECT_TRUE(FixupServiceEntries());
1025}
1026
1027TEST_F(WiFiServiceFixupStorageTest, MissingSecurityProperty) {
Paul Stewart71a4d3b2013-01-18 18:12:56 -08001028 AddServiceEntry(true, true, false, true);
Paul Stewart85aea152013-01-22 09:31:56 -08001029 EXPECT_TRUE(FixupServiceEntries());
1030}
1031
Paul Stewart71a4d3b2013-01-18 18:12:56 -08001032TEST_F(WiFiServiceFixupStorageTest, MissingSecurityClassProperty) {
1033 AddServiceEntry(true, true, true, false);
1034 EXPECT_TRUE(FixupServiceEntries());
1035}
Paul Stewart85aea152013-01-22 09:31:56 -08001036
mukesh agrawal29c13a12011-11-24 00:09:19 +00001037TEST_F(WiFiServiceTest, Connectable) {
1038 // Open network should be connectable.
Gaurav Shah10109f22011-11-11 20:16:22 -08001039 EXPECT_TRUE(CheckConnectable(flimflam::kSecurityNone, NULL, NULL));
mukesh agrawal29c13a12011-11-24 00:09:19 +00001040
1041 // Open network should remain connectable if we try to set a password on it.
Gaurav Shah10109f22011-11-11 20:16:22 -08001042 EXPECT_TRUE(CheckConnectable(flimflam::kSecurityNone, "abcde", NULL));
mukesh agrawal29c13a12011-11-24 00:09:19 +00001043
1044 // WEP network with passphrase set should be connectable.
Gaurav Shah10109f22011-11-11 20:16:22 -08001045 EXPECT_TRUE(CheckConnectable(flimflam::kSecurityWep, "abcde", NULL));
mukesh agrawal29c13a12011-11-24 00:09:19 +00001046
1047 // WEP network without passphrase set should NOT be connectable.
Gaurav Shah10109f22011-11-11 20:16:22 -08001048 EXPECT_FALSE(CheckConnectable(flimflam::kSecurityWep, NULL, NULL));
mukesh agrawal29c13a12011-11-24 00:09:19 +00001049
1050 // A bad passphrase should not make a WEP network connectable.
Gaurav Shah10109f22011-11-11 20:16:22 -08001051 EXPECT_FALSE(CheckConnectable(flimflam::kSecurityWep, "a", NULL));
mukesh agrawal29c13a12011-11-24 00:09:19 +00001052
1053 // Similar to WEP, for WPA.
Gaurav Shah10109f22011-11-11 20:16:22 -08001054 EXPECT_TRUE(CheckConnectable(flimflam::kSecurityWpa, "abcdefgh", NULL));
1055 EXPECT_FALSE(CheckConnectable(flimflam::kSecurityWpa, NULL, NULL));
1056 EXPECT_FALSE(CheckConnectable(flimflam::kSecurityWpa, "a", NULL));
mukesh agrawal29c13a12011-11-24 00:09:19 +00001057
1058 // Unconfigured 802.1x should NOT be connectable.
Gaurav Shah10109f22011-11-11 20:16:22 -08001059 EXPECT_FALSE(CheckConnectable(flimflam::kSecurity8021x, NULL, NULL));
1060
Paul Stewart0654ece2013-03-26 15:21:26 -07001061 EapCredentials eap;
Gaurav Shah10109f22011-11-11 20:16:22 -08001062 // Empty EAP credentials should not make a 802.1x network connectable.
1063 EXPECT_FALSE(CheckConnectable(flimflam::kSecurity8021x, NULL, &eap));
1064
1065 eap.identity = "something";
1066 // If client certificate is being used, a private key must exist.
1067 eap.client_cert = "some client cert";
1068 EXPECT_FALSE(CheckConnectable(flimflam::kSecurity8021x, NULL, &eap));
1069 eap.private_key = "some private key";
1070 EXPECT_TRUE(CheckConnectable(flimflam::kSecurity8021x, NULL, &eap));
1071
1072 // Identity is always required.
1073 eap.identity.clear();
1074 EXPECT_FALSE(CheckConnectable(flimflam::kSecurity8021x, NULL, &eap));
1075
1076 eap.identity = "something";
1077 // For non EAP-TLS types, a password is required.
1078 eap.eap = "Non-TLS";
1079 EXPECT_FALSE(CheckConnectable(flimflam::kSecurity8021x, NULL, &eap));
1080 eap.password = "some password";
1081 EXPECT_TRUE(CheckConnectable(flimflam::kSecurity8021x, NULL, &eap));
Gaurav Shah29d68882012-01-30 19:06:42 -08001082 // Dynamic WEP + 802.1X should be connectable under the same conditions.
1083 eap.key_management = "IEEE8021X";
1084 EXPECT_TRUE(CheckConnectable(flimflam::kSecurityWep, NULL, &eap));
mukesh agrawal29c13a12011-11-24 00:09:19 +00001085}
1086
mukesh agrawal8a3188d2011-12-01 20:56:44 +00001087TEST_F(WiFiServiceTest, IsAutoConnectable) {
mukesh agrawalbf14e942012-03-02 14:36:34 -08001088 const char *reason;
Paul Stewart3c504012013-01-17 17:49:58 -08001089 WiFiServiceRefPtr service = MakeSimpleService(flimflam::kSecurityNone);
mukesh agrawal8a3188d2011-12-01 20:56:44 +00001090 EXPECT_CALL(*wifi(), IsIdle())
1091 .WillRepeatedly(Return(true));
1092 EXPECT_FALSE(service->HasEndpoints());
mukesh agrawalbf14e942012-03-02 14:36:34 -08001093 EXPECT_FALSE(service->IsAutoConnectable(&reason));
1094 EXPECT_STREQ(WiFiService::kAutoConnNoEndpoint, reason);
mukesh agrawal8a3188d2011-12-01 20:56:44 +00001095
mukesh agrawalbf14e942012-03-02 14:36:34 -08001096 reason = "";
mukesh agrawal43970a22013-02-15 16:00:07 -08001097 WiFiEndpointRefPtr endpoint =
1098 MakeOpenEndpoint("a", "00:00:00:00:00:01", 0, 0);
mukesh agrawal8a3188d2011-12-01 20:56:44 +00001099 service->AddEndpoint(endpoint);
1100 EXPECT_CALL(*wifi(), IsIdle())
1101 .WillRepeatedly(Return(true));
1102 EXPECT_TRUE(service->HasEndpoints());
mukesh agrawalbf14e942012-03-02 14:36:34 -08001103 EXPECT_TRUE(service->IsAutoConnectable(&reason));
1104 EXPECT_STREQ("", reason);
mukesh agrawal8a3188d2011-12-01 20:56:44 +00001105
1106 // WiFi only supports connecting to one Service at a time. So, to
1107 // avoid disrupting connectivity, we only allow auto-connection to
1108 // a WiFiService when the corresponding WiFi is idle.
1109 EXPECT_CALL(*wifi(), IsIdle())
1110 .WillRepeatedly(Return(false));
1111 EXPECT_TRUE(service->HasEndpoints());
mukesh agrawalbf14e942012-03-02 14:36:34 -08001112 EXPECT_FALSE(service->IsAutoConnectable(&reason));
1113 EXPECT_STREQ(WiFiService::kAutoConnBusy, reason);
mukesh agrawal8a3188d2011-12-01 20:56:44 +00001114}
1115
1116TEST_F(WiFiServiceTest, AutoConnect) {
mukesh agrawalbf14e942012-03-02 14:36:34 -08001117 const char *reason;
Paul Stewart3c504012013-01-17 17:49:58 -08001118 WiFiServiceRefPtr service = MakeSimpleService(flimflam::kSecurityNone);
mukesh agrawalbf14e942012-03-02 14:36:34 -08001119 EXPECT_FALSE(service->IsAutoConnectable(&reason));
mukesh agrawal8a3188d2011-12-01 20:56:44 +00001120 EXPECT_CALL(*wifi(), ConnectTo(_, _))
1121 .Times(0);
1122 service->AutoConnect();
Eric Shienbrood3e20a232012-02-16 11:35:56 -05001123 dispatcher()->DispatchPendingEvents();
mukesh agrawal8a3188d2011-12-01 20:56:44 +00001124
mukesh agrawal43970a22013-02-15 16:00:07 -08001125 WiFiEndpointRefPtr endpoint =
1126 MakeOpenEndpoint("a", "00:00:00:00:00:01", 0, 0);
mukesh agrawal8a3188d2011-12-01 20:56:44 +00001127 service->AddEndpoint(endpoint);
1128 EXPECT_CALL(*wifi(), IsIdle())
1129 .WillRepeatedly(Return(true));
mukesh agrawalbf14e942012-03-02 14:36:34 -08001130 EXPECT_TRUE(service->IsAutoConnectable(&reason));
mukesh agrawal8a3188d2011-12-01 20:56:44 +00001131 EXPECT_CALL(*wifi(), ConnectTo(_, _));
1132 service->AutoConnect();
Eric Shienbrood3e20a232012-02-16 11:35:56 -05001133 dispatcher()->DispatchPendingEvents();
mukesh agrawaladb68482012-01-17 16:31:51 -08001134
1135 Error error;
Christopher Wileyabd3b502012-09-26 13:08:52 -07001136 service->UserInitiatedDisconnect(&error);
Eric Shienbrood3e20a232012-02-16 11:35:56 -05001137 dispatcher()->DispatchPendingEvents();
mukesh agrawalbf14e942012-03-02 14:36:34 -08001138 EXPECT_FALSE(service->IsAutoConnectable(&reason));
mukesh agrawal8a3188d2011-12-01 20:56:44 +00001139}
1140
mukesh agrawal8abd2f62012-01-30 14:56:14 -08001141TEST_F(WiFiServiceTest, ClearWriteOnlyDerivedProperty) {
Paul Stewart3c504012013-01-17 17:49:58 -08001142 WiFiServiceRefPtr wifi_service = MakeSimpleService(flimflam::kSecurityWep);
mukesh agrawal8abd2f62012-01-30 14:56:14 -08001143
1144 EXPECT_EQ("", wifi_service->passphrase_);
1145
1146 ::DBus::Error error;
mukesh agrawal6bb9e7c2012-01-30 14:57:54 -08001147 EXPECT_TRUE(DBusAdaptor::SetProperty(
mukesh agrawal8abd2f62012-01-30 14:56:14 -08001148 wifi_service->mutable_store(),
1149 flimflam::kPassphraseProperty,
1150 DBusAdaptor::StringToVariant("0:abcde"),
1151 &error));
1152 EXPECT_EQ("0:abcde", wifi_service->passphrase_);
1153
1154 EXPECT_TRUE(DBusAdaptor::ClearProperty(wifi_service->mutable_store(),
1155 flimflam::kPassphraseProperty,
1156 &error));
1157 EXPECT_EQ("", wifi_service->passphrase_);
1158}
1159
mukesh agrawale1d90e92012-02-15 17:36:08 -08001160TEST_F(WiFiServiceTest, SignalToStrength) {
1161 // Verify that our mapping is sane, in the sense that it preserves ordering.
1162 // We break the test into two domains, because we assume that positive
1163 // values aren't actually in dBm.
1164 for (int16 i = std::numeric_limits<int16>::min(); i < 0; ++i) {
1165 int16 current_mapped = WiFiService::SignalToStrength(i);
1166 int16 next_mapped = WiFiService::SignalToStrength(i+1);
1167 EXPECT_LE(current_mapped, next_mapped)
1168 << "(original values " << i << " " << i+1 << ")";
mukesh agrawal8f3f7752012-02-17 19:42:09 -08001169 EXPECT_GE(current_mapped, Service::kStrengthMin);
1170 EXPECT_LE(current_mapped, Service::kStrengthMax);
mukesh agrawale1d90e92012-02-15 17:36:08 -08001171 }
1172 for (int16 i = 1; i < std::numeric_limits<int16>::max(); ++i) {
1173 int16 current_mapped = WiFiService::SignalToStrength(i);
1174 int16 next_mapped = WiFiService::SignalToStrength(i+1);
1175 EXPECT_LE(current_mapped, next_mapped)
1176 << "(original values " << i << " " << i+1 << ")";
mukesh agrawal8f3f7752012-02-17 19:42:09 -08001177 EXPECT_GE(current_mapped, Service::kStrengthMin);
1178 EXPECT_LE(current_mapped, Service::kStrengthMax);
mukesh agrawale1d90e92012-02-15 17:36:08 -08001179 }
1180}
1181
1182TEST_F(WiFiServiceUpdateFromEndpointsTest, Strengths) {
1183 // If the chosen signal values don't map to distinct strength
1184 // values, then we can't expect our other tests to pass. So verify
1185 // their distinctness.
1186 EXPECT_TRUE(kOkEndpointStrength != kBadEndpointStrength);
1187 EXPECT_TRUE(kOkEndpointStrength != kGoodEndpointStrength);
1188 EXPECT_TRUE(kGoodEndpointStrength != kBadEndpointStrength);
1189}
1190
1191TEST_F(WiFiServiceUpdateFromEndpointsTest, Floating) {
1192 // Initial endpoint updates values.
1193 EXPECT_CALL(adaptor, EmitUint16Changed(
1194 flimflam::kWifiFrequency, kOkEndpointFrequency));
mukesh agrawal923f14f2012-06-04 16:46:08 -07001195 EXPECT_CALL(adaptor, EmitStringChanged(
1196 flimflam::kWifiBSsid, kOkEndpointBssId));
1197 EXPECT_CALL(adaptor, EmitUint8Changed(
mukesh agrawale1d90e92012-02-15 17:36:08 -08001198 flimflam::kSignalStrengthProperty, kOkEndpointStrength));
mukesh agrawalf6b32092013-04-10 15:49:55 -07001199 EXPECT_CALL(adaptor, EmitUint16Changed(
1200 flimflam::kWifiPhyMode, Metrics::kWiFiNetworkPhyMode11b));
mukesh agrawale1d90e92012-02-15 17:36:08 -08001201 service->AddEndpoint(ok_endpoint);
Darin Petkov4a66cc52012-06-15 10:08:29 +02001202 EXPECT_EQ(1, service->GetEndpointCount());
mukesh agrawale1d90e92012-02-15 17:36:08 -08001203 Mock::VerifyAndClearExpectations(&adaptor);
1204
1205 // Endpoint with stronger signal updates values.
1206 EXPECT_CALL(adaptor, EmitUint16Changed(
1207 flimflam::kWifiFrequency, kGoodEndpointFrequency));
mukesh agrawal923f14f2012-06-04 16:46:08 -07001208 EXPECT_CALL(adaptor, EmitStringChanged(
1209 flimflam::kWifiBSsid, kGoodEndpointBssId));
mukesh agrawale1d90e92012-02-15 17:36:08 -08001210 EXPECT_CALL(adaptor, EmitUint8Changed(
1211 flimflam::kSignalStrengthProperty, kGoodEndpointStrength));
mukesh agrawalf6b32092013-04-10 15:49:55 -07001212 // However, both endpoints are 11b.
1213 EXPECT_CALL(adaptor, EmitUint16Changed(flimflam::kWifiPhyMode, _)).Times(0);
mukesh agrawale1d90e92012-02-15 17:36:08 -08001214 service->AddEndpoint(good_endpoint);
Darin Petkov4a66cc52012-06-15 10:08:29 +02001215 EXPECT_EQ(2, service->GetEndpointCount());
mukesh agrawale1d90e92012-02-15 17:36:08 -08001216 Mock::VerifyAndClearExpectations(&adaptor);
1217
1218 // Endpoint with lower signal does not change values.
1219 EXPECT_CALL(adaptor, EmitUint16Changed(flimflam::kWifiFrequency, _)).Times(0);
mukesh agrawal923f14f2012-06-04 16:46:08 -07001220 EXPECT_CALL(adaptor, EmitStringChanged(flimflam::kWifiBSsid, _)).Times(0);
mukesh agrawale1d90e92012-02-15 17:36:08 -08001221 EXPECT_CALL(adaptor,
1222 EmitUint8Changed(flimflam::kSignalStrengthProperty, _)).Times(0);
mukesh agrawalf6b32092013-04-10 15:49:55 -07001223 EXPECT_CALL(adaptor, EmitUint16Changed(flimflam::kWifiPhyMode, _)).Times(0);
mukesh agrawale1d90e92012-02-15 17:36:08 -08001224 service->AddEndpoint(bad_endpoint);
Darin Petkov4a66cc52012-06-15 10:08:29 +02001225 EXPECT_EQ(3, service->GetEndpointCount());
mukesh agrawale1d90e92012-02-15 17:36:08 -08001226 Mock::VerifyAndClearExpectations(&adaptor);
1227
1228 // Removing non-optimal endpoint does not change values.
1229 EXPECT_CALL(adaptor, EmitUint16Changed(flimflam::kWifiFrequency, _)).Times(0);
mukesh agrawal923f14f2012-06-04 16:46:08 -07001230 EXPECT_CALL(adaptor, EmitStringChanged(flimflam::kWifiBSsid, _)).Times(0);
mukesh agrawale1d90e92012-02-15 17:36:08 -08001231 EXPECT_CALL(adaptor,
1232 EmitUint8Changed(flimflam::kSignalStrengthProperty, _)).Times(0);
mukesh agrawalf6b32092013-04-10 15:49:55 -07001233 EXPECT_CALL(adaptor, EmitUint16Changed(flimflam::kWifiPhyMode, _)).Times(0);
mukesh agrawale1d90e92012-02-15 17:36:08 -08001234 service->RemoveEndpoint(bad_endpoint);
Darin Petkov4a66cc52012-06-15 10:08:29 +02001235 EXPECT_EQ(2, service->GetEndpointCount());
mukesh agrawale1d90e92012-02-15 17:36:08 -08001236 Mock::VerifyAndClearExpectations(&adaptor);
1237
1238 // Removing optimal endpoint updates values.
1239 EXPECT_CALL(adaptor, EmitUint16Changed(
1240 flimflam::kWifiFrequency, kOkEndpointFrequency));
mukesh agrawal923f14f2012-06-04 16:46:08 -07001241 EXPECT_CALL(adaptor, EmitStringChanged(
1242 flimflam::kWifiBSsid, kOkEndpointBssId));
mukesh agrawale1d90e92012-02-15 17:36:08 -08001243 EXPECT_CALL(adaptor, EmitUint8Changed(
1244 flimflam::kSignalStrengthProperty, kOkEndpointStrength));
mukesh agrawalf6b32092013-04-10 15:49:55 -07001245 // However, both endpoints are 11b.
1246 EXPECT_CALL(adaptor, EmitUint16Changed(flimflam::kWifiPhyMode, _)).Times(0);
mukesh agrawale1d90e92012-02-15 17:36:08 -08001247 service->RemoveEndpoint(good_endpoint);
Darin Petkov4a66cc52012-06-15 10:08:29 +02001248 EXPECT_EQ(1, service->GetEndpointCount());
mukesh agrawale1d90e92012-02-15 17:36:08 -08001249 Mock::VerifyAndClearExpectations(&adaptor);
1250
1251 // Removing last endpoint updates values (and doesn't crash).
1252 EXPECT_CALL(adaptor, EmitUint16Changed(flimflam::kWifiFrequency, _));
mukesh agrawal923f14f2012-06-04 16:46:08 -07001253 EXPECT_CALL(adaptor, EmitStringChanged(flimflam::kWifiBSsid, _));
mukesh agrawale1d90e92012-02-15 17:36:08 -08001254 EXPECT_CALL(adaptor, EmitUint8Changed(flimflam::kSignalStrengthProperty, _));
mukesh agrawalf6b32092013-04-10 15:49:55 -07001255 EXPECT_CALL(adaptor, EmitUint16Changed(
1256 flimflam::kWifiPhyMode, Metrics::kWiFiNetworkPhyModeUndef));
mukesh agrawale1d90e92012-02-15 17:36:08 -08001257 service->RemoveEndpoint(ok_endpoint);
Darin Petkov4a66cc52012-06-15 10:08:29 +02001258 EXPECT_EQ(0, service->GetEndpointCount());
mukesh agrawale1d90e92012-02-15 17:36:08 -08001259 Mock::VerifyAndClearExpectations(&adaptor);
1260}
1261
1262TEST_F(WiFiServiceUpdateFromEndpointsTest, Connected) {
1263 EXPECT_CALL(adaptor, EmitUint16Changed(_, _)).Times(AnyNumber());
mukesh agrawal923f14f2012-06-04 16:46:08 -07001264 EXPECT_CALL(adaptor, EmitStringChanged(_, _)).Times(AnyNumber());
mukesh agrawale1d90e92012-02-15 17:36:08 -08001265 EXPECT_CALL(adaptor, EmitUint8Changed(_, _)).Times(AnyNumber());
1266 EXPECT_CALL(adaptor, EmitBoolChanged(_, _)).Times(AnyNumber());
1267 service->AddEndpoint(bad_endpoint);
1268 service->AddEndpoint(ok_endpoint);
Darin Petkov4a66cc52012-06-15 10:08:29 +02001269 EXPECT_EQ(2, service->GetEndpointCount());
mukesh agrawale1d90e92012-02-15 17:36:08 -08001270 Mock::VerifyAndClearExpectations(&adaptor);
1271
1272 // Setting current endpoint forces adoption of its values, even if it
1273 // doesn't have the highest signal.
1274 EXPECT_CALL(adaptor, EmitUint16Changed(
1275 flimflam::kWifiFrequency, kBadEndpointFrequency));
mukesh agrawal923f14f2012-06-04 16:46:08 -07001276 EXPECT_CALL(adaptor, EmitStringChanged(
1277 flimflam::kWifiBSsid, kBadEndpointBssId));
mukesh agrawale1d90e92012-02-15 17:36:08 -08001278 EXPECT_CALL(adaptor, EmitUint8Changed(
1279 flimflam::kSignalStrengthProperty, kBadEndpointStrength));
1280 service->NotifyCurrentEndpoint(bad_endpoint);
1281 Mock::VerifyAndClearExpectations(&adaptor);
1282
1283 // Adding a better endpoint doesn't matter, when current endpoint is set.
1284 EXPECT_CALL(adaptor, EmitUint16Changed(flimflam::kWifiFrequency, _)).Times(0);
mukesh agrawal923f14f2012-06-04 16:46:08 -07001285 EXPECT_CALL(adaptor, EmitStringChanged(flimflam::kWifiBSsid, _)).Times(0);
mukesh agrawale1d90e92012-02-15 17:36:08 -08001286 EXPECT_CALL(adaptor,
1287 EmitUint8Changed(flimflam::kSignalStrengthProperty, _)).Times(0);
1288 service->AddEndpoint(good_endpoint);
Darin Petkov4a66cc52012-06-15 10:08:29 +02001289 EXPECT_EQ(3, service->GetEndpointCount());
mukesh agrawale1d90e92012-02-15 17:36:08 -08001290 Mock::VerifyAndClearExpectations(&adaptor);
1291
1292 // Removing a better endpoint doesn't matter, when current endpoint is set.
1293 EXPECT_CALL(adaptor, EmitUint16Changed(flimflam::kWifiFrequency, _)).Times(0);
mukesh agrawal923f14f2012-06-04 16:46:08 -07001294 EXPECT_CALL(adaptor, EmitStringChanged(flimflam::kWifiBSsid, _)).Times(0);
mukesh agrawale1d90e92012-02-15 17:36:08 -08001295 EXPECT_CALL(adaptor,
1296 EmitUint8Changed(flimflam::kSignalStrengthProperty, _)).Times(0);
1297 service->RemoveEndpoint(good_endpoint);
1298 Mock::VerifyAndClearExpectations(&adaptor);
1299
1300 // Removing the current endpoint is safe and sane.
1301 EXPECT_CALL(adaptor, EmitUint16Changed(
1302 flimflam::kWifiFrequency, kOkEndpointFrequency));
mukesh agrawal923f14f2012-06-04 16:46:08 -07001303 EXPECT_CALL(adaptor, EmitStringChanged(
1304 flimflam::kWifiBSsid, kOkEndpointBssId));
mukesh agrawale1d90e92012-02-15 17:36:08 -08001305 EXPECT_CALL(adaptor, EmitUint8Changed(
1306 flimflam::kSignalStrengthProperty, kOkEndpointStrength));
1307 service->RemoveEndpoint(bad_endpoint);
1308 Mock::VerifyAndClearExpectations(&adaptor);
1309
1310 // Clearing the current endpoint (without removing it) is also safe and sane.
1311 service->NotifyCurrentEndpoint(ok_endpoint);
1312 EXPECT_CALL(adaptor, EmitUint16Changed(flimflam::kWifiFrequency, _)).Times(0);
mukesh agrawal923f14f2012-06-04 16:46:08 -07001313 EXPECT_CALL(adaptor, EmitStringChanged(flimflam::kWifiBSsid, _)).Times(0);
mukesh agrawale1d90e92012-02-15 17:36:08 -08001314 EXPECT_CALL(adaptor,
1315 EmitUint8Changed(flimflam::kSignalStrengthProperty, _)).Times(0);
1316 service->NotifyCurrentEndpoint(NULL);
1317 Mock::VerifyAndClearExpectations(&adaptor);
1318}
1319
1320TEST_F(WiFiServiceUpdateFromEndpointsTest, EndpointModified) {
1321 EXPECT_CALL(adaptor, EmitUint16Changed(_, _)).Times(AnyNumber());
mukesh agrawal923f14f2012-06-04 16:46:08 -07001322 EXPECT_CALL(adaptor, EmitStringChanged(_, _)).Times(AnyNumber());
mukesh agrawale1d90e92012-02-15 17:36:08 -08001323 EXPECT_CALL(adaptor, EmitUint8Changed(_, _)).Times(AnyNumber());
1324 EXPECT_CALL(adaptor, EmitBoolChanged(_, _)).Times(AnyNumber());
1325 service->AddEndpoint(ok_endpoint);
1326 service->AddEndpoint(good_endpoint);
Darin Petkov4a66cc52012-06-15 10:08:29 +02001327 EXPECT_EQ(2, service->GetEndpointCount());
mukesh agrawale1d90e92012-02-15 17:36:08 -08001328 Mock::VerifyAndClearExpectations(&adaptor);
1329
1330 // Updating sub-optimal Endpoint doesn't update Service.
1331 EXPECT_CALL(adaptor, EmitUint16Changed(flimflam::kWifiFrequency, _)).Times(0);
mukesh agrawal923f14f2012-06-04 16:46:08 -07001332 EXPECT_CALL(adaptor, EmitStringChanged(flimflam::kWifiBSsid, _)).Times(0);
mukesh agrawale1d90e92012-02-15 17:36:08 -08001333 EXPECT_CALL(adaptor,
1334 EmitUint8Changed(flimflam::kSignalStrengthProperty, _)).Times(0);
1335 ok_endpoint->signal_strength_ = (kOkEndpointSignal + kGoodEndpointSignal) / 2;
Paul Stewart3c504012013-01-17 17:49:58 -08001336 service->NotifyEndpointUpdated(ok_endpoint);
mukesh agrawale1d90e92012-02-15 17:36:08 -08001337 Mock::VerifyAndClearExpectations(&adaptor);
1338
1339 // Updating optimal Endpoint updates appropriate Service property.
1340 EXPECT_CALL(adaptor, EmitUint16Changed(flimflam::kWifiFrequency, _)).Times(0);
mukesh agrawal923f14f2012-06-04 16:46:08 -07001341 EXPECT_CALL(adaptor, EmitStringChanged(flimflam::kWifiBSsid, _)).Times(0);
mukesh agrawale1d90e92012-02-15 17:36:08 -08001342 EXPECT_CALL(adaptor, EmitUint8Changed(flimflam::kSignalStrengthProperty, _));
1343 good_endpoint->signal_strength_ = kGoodEndpointSignal + 1;
Paul Stewart3c504012013-01-17 17:49:58 -08001344 service->NotifyEndpointUpdated(good_endpoint);
mukesh agrawale1d90e92012-02-15 17:36:08 -08001345 Mock::VerifyAndClearExpectations(&adaptor);
1346
1347 // Change in optimal Endpoint updates Service properties.
1348 EXPECT_CALL(adaptor, EmitUint16Changed(
1349 flimflam::kWifiFrequency, kOkEndpointFrequency));
mukesh agrawal923f14f2012-06-04 16:46:08 -07001350 EXPECT_CALL(adaptor, EmitStringChanged(
1351 flimflam::kWifiBSsid, kOkEndpointBssId));
mukesh agrawale1d90e92012-02-15 17:36:08 -08001352 EXPECT_CALL(adaptor, EmitUint8Changed(flimflam::kSignalStrengthProperty, _));
1353 ok_endpoint->signal_strength_ = kGoodEndpointSignal + 2;
Paul Stewart3c504012013-01-17 17:49:58 -08001354 service->NotifyEndpointUpdated(ok_endpoint);
mukesh agrawale1d90e92012-02-15 17:36:08 -08001355 Mock::VerifyAndClearExpectations(&adaptor);
1356}
1357
Paul Stewarta5e7d5f2013-01-09 18:06:15 -08001358TEST_F(WiFiServiceUpdateFromEndpointsTest, Ieee80211w) {
1359 EXPECT_CALL(adaptor, EmitUint16Changed(_, _)).Times(AnyNumber());
1360 EXPECT_CALL(adaptor, EmitStringChanged(_, _)).Times(AnyNumber());
1361 EXPECT_CALL(adaptor, EmitUint8Changed(_, _)).Times(AnyNumber());
1362 EXPECT_CALL(adaptor, EmitBoolChanged(_, _)).Times(AnyNumber());
1363 service->AddEndpoint(ok_endpoint);
1364 EXPECT_FALSE(service->ieee80211w_required());
1365 good_endpoint->ieee80211w_required_ = true;
1366 service->AddEndpoint(good_endpoint);
1367 EXPECT_TRUE(service->ieee80211w_required());
1368 service->RemoveEndpoint(good_endpoint);
1369 EXPECT_TRUE(service->ieee80211w_required());
1370}
1371
mukesh agrawalf6b32092013-04-10 15:49:55 -07001372TEST_F(WiFiServiceUpdateFromEndpointsTest, PhysicalMode) {
1373 EXPECT_CALL(adaptor, EmitUint16Changed(_, _)).Times(AnyNumber());
1374 EXPECT_CALL(adaptor, EmitStringChanged(_, _)).Times(AnyNumber());
1375 EXPECT_CALL(adaptor, EmitUint8Changed(_, _)).Times(AnyNumber());
1376 EXPECT_CALL(adaptor, EmitBoolChanged(_, _)).Times(AnyNumber());
1377
1378 // No endpoints -> undef.
1379 EXPECT_EQ(Metrics::kWiFiNetworkPhyModeUndef, service->physical_mode());
1380
1381 // Endpoint has unknown physical mode -> undef.
1382 ok_endpoint->physical_mode_ = Metrics::kWiFiNetworkPhyModeUndef;
1383 service->AddEndpoint(ok_endpoint);
1384 EXPECT_EQ(Metrics::kWiFiNetworkPhyModeUndef, service->physical_mode());
1385
1386 // New endpoint with 802.11a -> 802.11a.
1387 good_endpoint->physical_mode_ = Metrics::kWiFiNetworkPhyMode11a;
1388 service->AddEndpoint(good_endpoint);
1389 EXPECT_EQ(Metrics::kWiFiNetworkPhyMode11a, service->physical_mode());
1390
1391 // Remove 802.11a endpoint -> undef.
1392 service->RemoveEndpoint(good_endpoint);
1393 EXPECT_EQ(Metrics::kWiFiNetworkPhyModeUndef, service->physical_mode());
1394
1395 // Change endpoint -> take endpoint's new value.
1396 ok_endpoint->physical_mode_ = Metrics::kWiFiNetworkPhyMode11n;
1397 service->NotifyEndpointUpdated(ok_endpoint);
1398 EXPECT_EQ(Metrics::kWiFiNetworkPhyMode11n, service->physical_mode());
1399
1400 // No endpoints -> undef.
1401 service->RemoveEndpoint(ok_endpoint);
1402 EXPECT_EQ(Metrics::kWiFiNetworkPhyModeUndef, service->physical_mode());
1403}
1404
Paul Stewart8653f462013-02-06 12:21:05 -08001405TEST_F(WiFiServiceUpdateFromEndpointsTest, WarningOnDisconnect) {
1406 service->AddEndpoint(ok_endpoint);
1407 service->SetState(Service::kStateAssociating);
1408 ScopedMockLog log;
1409 EXPECT_CALL(log, Log(_, _, _)).Times(AnyNumber());
1410 EXPECT_CALL(log, Log(logging::LOG_WARNING, _,
1411 EndsWith("disconnect due to no remaining endpoints.")));
1412 service->RemoveEndpoint(ok_endpoint);
1413}
1414
Paul Stewart6df20bd2013-03-13 19:31:25 -07001415TEST_F(WiFiServiceTest, SecurityFromCurrentEndpoint) {
1416 WiFiServiceRefPtr service(MakeSimpleService(flimflam::kSecurityPsk));
1417 EXPECT_EQ(flimflam::kSecurityPsk, service->GetSecurity(NULL));
1418 WiFiEndpoint *endpoint = MakeOpenEndpoint(
1419 simple_ssid_string(), "00:00:00:00:00:00", 0, 0);
1420 service->AddEndpoint(endpoint);
1421 EXPECT_EQ(flimflam::kSecurityPsk, service->GetSecurity(NULL));
1422 service->NotifyCurrentEndpoint(endpoint);
1423 EXPECT_EQ(flimflam::kSecurityNone, service->GetSecurity(NULL));
1424 service->NotifyCurrentEndpoint(NULL);
1425 EXPECT_EQ(flimflam::kSecurityPsk, service->GetSecurity(NULL));
1426}
1427
mukesh agrawal43970a22013-02-15 16:00:07 -08001428TEST_F(WiFiServiceTest, UpdateSecurity) {
1429 // Cleartext and pre-shared-key crypto.
1430 {
1431 WiFiServiceRefPtr service = MakeSimpleService(flimflam::kSecurityNone);
1432 EXPECT_EQ(Service::kCryptoNone, service->crypto_algorithm());
1433 EXPECT_FALSE(service->key_rotation());
1434 EXPECT_FALSE(service->endpoint_auth());
1435 }
1436 {
1437 WiFiServiceRefPtr service = MakeSimpleService(flimflam::kSecurityWep);
1438 EXPECT_EQ(Service::kCryptoRc4, service->crypto_algorithm());
1439 EXPECT_FALSE(service->key_rotation());
1440 EXPECT_FALSE(service->endpoint_auth());
1441 }
1442 {
1443 WiFiServiceRefPtr service = MakeSimpleService(flimflam::kSecurityPsk);
1444 EXPECT_EQ(Service::kCryptoRc4, service->crypto_algorithm());
1445 EXPECT_TRUE(service->key_rotation());
1446 EXPECT_FALSE(service->endpoint_auth());
1447 }
1448 {
1449 WiFiServiceRefPtr service = MakeSimpleService(flimflam::kSecurityWpa);
1450 EXPECT_EQ(Service::kCryptoRc4, service->crypto_algorithm());
1451 EXPECT_TRUE(service->key_rotation());
1452 EXPECT_FALSE(service->endpoint_auth());
1453 }
1454 {
1455 WiFiServiceRefPtr service = MakeSimpleService(flimflam::kSecurityRsn);
1456 EXPECT_EQ(Service::kCryptoAes, service->crypto_algorithm());
1457 EXPECT_TRUE(service->key_rotation());
1458 EXPECT_FALSE(service->endpoint_auth());
1459 }
1460
1461 // Crypto with 802.1X key management.
1462 {
1463 // WEP
1464 WiFiServiceRefPtr service = MakeSimpleService(flimflam::kSecurityWep);
1465 service->SetEAPKeyManagement("IEEE8021X");
1466 EXPECT_EQ(Service::kCryptoRc4, service->crypto_algorithm());
1467 EXPECT_TRUE(service->key_rotation());
1468 EXPECT_TRUE(service->endpoint_auth());
1469 }
1470 {
1471 // WPA
1472 WiFiServiceRefPtr service = MakeSimpleService(flimflam::kSecurity8021x);
1473 WiFiEndpointRefPtr endpoint =
1474 MakeEndpoint("a", "00:00:00:00:00:01", 0, 0, true, false);
1475 service->AddEndpoint(endpoint);
1476 EXPECT_EQ(Service::kCryptoRc4, service->crypto_algorithm());
1477 EXPECT_TRUE(service->key_rotation());
1478 EXPECT_TRUE(service->endpoint_auth());
1479 }
1480 {
1481 // RSN
1482 WiFiServiceRefPtr service = MakeSimpleService(flimflam::kSecurity8021x);
1483 WiFiEndpointRefPtr endpoint =
1484 MakeEndpoint("a", "00:00:00:00:00:01", 0, 0, false, true);
1485 service->AddEndpoint(endpoint);
1486 EXPECT_EQ(Service::kCryptoAes, service->crypto_algorithm());
1487 EXPECT_TRUE(service->key_rotation());
1488 EXPECT_TRUE(service->endpoint_auth());
1489 }
1490 {
1491 // AP supports both WPA and RSN.
1492 WiFiServiceRefPtr service = MakeSimpleService(flimflam::kSecurity8021x);
1493 WiFiEndpointRefPtr endpoint =
1494 MakeEndpoint("a", "00:00:00:00:00:01", 0, 0, true, true);
1495 service->AddEndpoint(endpoint);
1496 EXPECT_EQ(Service::kCryptoAes, service->crypto_algorithm());
1497 EXPECT_TRUE(service->key_rotation());
1498 EXPECT_TRUE(service->endpoint_auth());
1499 }
1500}
1501
1502TEST_F(WiFiServiceTest, ComputeCipher8021x) {
1503 // No endpoints.
1504 {
1505 const set<WiFiEndpointConstRefPtr> endpoints;
1506 EXPECT_EQ(Service::kCryptoNone,
1507 WiFiService::ComputeCipher8021x(endpoints));
1508 }
1509
1510 // Single endpoint, various configs.
1511 {
1512 set<WiFiEndpointConstRefPtr> endpoints;
1513 endpoints.insert(
1514 MakeEndpoint("a", "00:00:00:00:00:01", 0, 0, false, false));
1515 EXPECT_EQ(Service::kCryptoNone,
1516 WiFiService::ComputeCipher8021x(endpoints));
1517 }
1518 {
1519 set<WiFiEndpointConstRefPtr> endpoints;
1520 endpoints.insert(
1521 MakeEndpoint("a", "00:00:00:00:00:01", 0, 0, true, false));
1522 EXPECT_EQ(Service::kCryptoRc4,
1523 WiFiService::ComputeCipher8021x(endpoints));
1524 }
1525 {
1526 set<WiFiEndpointConstRefPtr> endpoints;
1527 endpoints.insert(
1528 MakeEndpoint("a", "00:00:00:00:00:01", 0, 0, false, true));
1529 EXPECT_EQ(Service::kCryptoAes,
1530 WiFiService::ComputeCipher8021x(endpoints));
1531 }
1532 {
1533 set<WiFiEndpointConstRefPtr> endpoints;
1534 endpoints.insert(
1535 MakeEndpoint("a", "00:00:00:00:00:01", 0, 0, true, true));
1536 EXPECT_EQ(Service::kCryptoAes,
1537 WiFiService::ComputeCipher8021x(endpoints));
1538 }
1539
1540 // Multiple endpoints.
1541 {
1542 set<WiFiEndpointConstRefPtr> endpoints;
1543 endpoints.insert(
1544 MakeEndpoint("a", "00:00:00:00:00:01", 0, 0, false, false));
1545 endpoints.insert(
1546 MakeEndpoint("a", "00:00:00:00:00:02", 0, 0, false, false));
1547 EXPECT_EQ(Service::kCryptoNone,
1548 WiFiService::ComputeCipher8021x(endpoints));
1549 }
1550 {
1551 set<WiFiEndpointConstRefPtr> endpoints;
1552 endpoints.insert(
1553 MakeEndpoint("a", "00:00:00:00:00:01", 0, 0, false, false));
1554 endpoints.insert(
1555 MakeEndpoint("a", "00:00:00:00:00:02", 0, 0, true, false));
1556 EXPECT_EQ(Service::kCryptoNone,
1557 WiFiService::ComputeCipher8021x(endpoints));
1558 }
1559 {
1560 set<WiFiEndpointConstRefPtr> endpoints;
1561 endpoints.insert(
1562 MakeEndpoint("a", "00:00:00:00:00:01", 0, 0, true, false));
1563 endpoints.insert(
1564 MakeEndpoint("a", "00:00:00:00:00:02", 0, 0, true, false));
1565 EXPECT_EQ(Service::kCryptoRc4,
1566 WiFiService::ComputeCipher8021x(endpoints));
1567 }
1568 {
1569 set<WiFiEndpointConstRefPtr> endpoints;
1570 endpoints.insert(
1571 MakeEndpoint("a", "00:00:00:00:00:01", 0, 0, true, false));
1572 endpoints.insert(
1573 MakeEndpoint("a", "00:00:00:00:00:02", 0, 0, false, true));
1574 EXPECT_EQ(Service::kCryptoRc4,
1575 WiFiService::ComputeCipher8021x(endpoints));
1576 }
1577 {
1578 set<WiFiEndpointConstRefPtr> endpoints;
1579 endpoints.insert(
1580 MakeEndpoint("a", "00:00:00:00:00:01", 0, 0, false, true));
1581 endpoints.insert(
1582 MakeEndpoint("a", "00:00:00:00:00:02", 0, 0, false, true));
1583 EXPECT_EQ(Service::kCryptoAes,
1584 WiFiService::ComputeCipher8021x(endpoints));
1585 }
1586 {
1587 set<WiFiEndpointConstRefPtr> endpoints;
1588 endpoints.insert(
1589 MakeEndpoint("a", "00:00:00:00:00:01", 0, 0, true, true));
1590 endpoints.insert(
1591 MakeEndpoint("a", "00:00:00:00:00:02", 0, 0, true, true));
1592 EXPECT_EQ(Service::kCryptoAes,
1593 WiFiService::ComputeCipher8021x(endpoints));
1594 }
1595}
1596
Albert Chaulk0e1cdea2013-02-27 15:32:55 -08001597TEST_F(WiFiServiceTest, Unload) {
1598 WiFiServiceRefPtr service = MakeServiceWithWiFi(flimflam::kSecurityNone);
1599 EXPECT_CALL(*wifi(), DestroyIPConfigLease(service->GetStorageIdentifier())).
1600 Times(1);
1601 service->Unload();
1602}
1603
1604
Chris Masone34af2182011-08-22 11:59:36 -07001605} // namespace shill