blob: 03aa47bce4f7c3bfd05ae5e4e270a984cf15f2b7 [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>
Chris Masone34af2182011-08-22 11:59:36 -07008#include <string>
9#include <vector>
10
11#include <base/string_util.h>
12#include <chromeos/dbus/service_constants.h>
13#include <gmock/gmock.h>
14#include <gtest/gtest.h>
15
Paul Stewart26b327e2011-10-19 11:38:09 -070016#include "shill/event_dispatcher.h"
Chris Masone34af2182011-08-22 11:59:36 -070017#include "shill/manager.h"
18#include "shill/mock_adaptors.h"
19#include "shill/mock_control.h"
Paul Stewartecf4cd12012-04-17 11:08:39 -070020#include "shill/mock_nss.h"
Christopher Wiley1ce658d2012-10-10 10:02:03 -070021#include "shill/mock_profile.h"
Chris Masone34af2182011-08-22 11:59:36 -070022#include "shill/mock_service.h"
23#include "shill/mock_store.h"
mukesh agrawal6e277772011-09-29 15:04:23 -070024#include "shill/mock_wifi.h"
Chris Masone34af2182011-08-22 11:59:36 -070025#include "shill/property_store_unittest.h"
mukesh agrawal8a3188d2011-12-01 20:56:44 +000026#include "shill/refptr_types.h"
27#include "shill/wifi_endpoint.h"
mukesh agrawal6e277772011-09-29 15:04:23 -070028#include "shill/wpa_supplicant.h"
Chris Masone34af2182011-08-22 11:59:36 -070029
mukesh agrawald835b202011-10-07 15:26:47 -070030using std::map;
Chris Masone34af2182011-08-22 11:59:36 -070031using std::string;
32using std::vector;
Paul Stewartd08f4432011-11-04 07:48:20 -070033using ::testing::_;
mukesh agrawale1d90e92012-02-15 17:36:08 -080034using ::testing::AnyNumber;
Paul Stewartd08f4432011-11-04 07:48:20 -070035using ::testing::DoAll;
mukesh agrawale1d90e92012-02-15 17:36:08 -080036using ::testing::Mock;
mukesh agrawal6e277772011-09-29 15:04:23 -070037using ::testing::NiceMock;
Paul Stewartd08f4432011-11-04 07:48:20 -070038using ::testing::Return;
39using ::testing::SetArgumentPointee;
40using ::testing::StrEq;
Paul Stewartd8ad3c42012-01-09 12:39:38 -080041using ::testing::StrNe;
Chris Masone34af2182011-08-22 11:59:36 -070042
mukesh agrawalb20776f2012-02-10 16:00:36 -080043namespace shill {
44
Chris Masone34af2182011-08-22 11:59:36 -070045class WiFiServiceTest : public PropertyStoreTest {
46 public:
mukesh agrawal6e277772011-09-29 15:04:23 -070047 WiFiServiceTest() : wifi_(
48 new NiceMock<MockWiFi>(
49 control_interface(),
50 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -080051 metrics(),
mukesh agrawal6e277772011-09-29 15:04:23 -070052 manager(),
53 "wifi",
54 fake_mac,
55 0)) {}
Chris Masone34af2182011-08-22 11:59:36 -070056 virtual ~WiFiServiceTest() {}
mukesh agrawal6e277772011-09-29 15:04:23 -070057
58 protected:
59 static const char fake_mac[];
mukesh agrawale1d90e92012-02-15 17:36:08 -080060
Gaurav Shah10109f22011-11-11 20:16:22 -080061 bool CheckConnectable(const std::string &security, const char *passphrase,
62 Service::EapCredentials *eap) {
mukesh agrawal29c13a12011-11-24 00:09:19 +000063 Error error;
64 vector<uint8_t> ssid(1, 'a');
65 WiFiServiceRefPtr service = new WiFiService(control_interface(),
66 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -080067 metrics(),
mukesh agrawal29c13a12011-11-24 00:09:19 +000068 manager(),
69 wifi(),
70 ssid,
71 flimflam::kModeManaged,
72 security,
73 false);
74 if (passphrase)
75 service->SetPassphrase(passphrase, &error);
Gaurav Shah10109f22011-11-11 20:16:22 -080076 if (eap) {
77 service->set_eap(*eap);
78 }
mukesh agrawal29c13a12011-11-24 00:09:19 +000079 return service->connectable();
80 }
mukesh agrawale1d90e92012-02-15 17:36:08 -080081 WiFiEndpoint *MakeEndpoint(const string &ssid, const string &bssid,
82 uint16 frequency, int16 signal_dbm) {
83 return WiFiEndpoint::MakeOpenEndpoint(
84 NULL, NULL, ssid, bssid, frequency, signal_dbm);
85 }
86 WiFiService *MakeGenericService() {
87 return new WiFiService(control_interface(),
88 dispatcher(),
89 metrics(),
90 manager(),
91 wifi(),
92 vector<uint8_t>(),
93 flimflam::kModeManaged,
94 flimflam::kSecurityWep,
95 false);
96 }
97 ServiceMockAdaptor *GetAdaptor(WiFiService *service) {
98 return dynamic_cast<ServiceMockAdaptor *>(service->adaptor());
mukesh agrawal8a3188d2011-12-01 20:56:44 +000099 }
mukesh agrawal6e277772011-09-29 15:04:23 -0700100 scoped_refptr<MockWiFi> wifi() { return wifi_; }
101
102 private:
103 scoped_refptr<MockWiFi> wifi_;
Chris Masone34af2182011-08-22 11:59:36 -0700104};
105
mukesh agrawal6e277772011-09-29 15:04:23 -0700106// static
107const char WiFiServiceTest::fake_mac[] = "AaBBcCDDeeFF";
108
Paul Stewartd08f4432011-11-04 07:48:20 -0700109class WiFiServiceSecurityTest : public WiFiServiceTest {
110 public:
111 WiFiServiceRefPtr CreateServiceWithSecurity(const string &security) {
Gaurav Shahda6218a2011-11-11 12:09:33 -0800112 vector<uint8_t> ssid(5);
Paul Stewartd08f4432011-11-04 07:48:20 -0700113 ssid.push_back(0xff);
114
115 return new WiFiService(control_interface(),
116 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -0800117 metrics(),
Paul Stewartd08f4432011-11-04 07:48:20 -0700118 manager(),
119 wifi(),
120 ssid,
121 flimflam::kModeManaged,
Paul Stewartced6a0b2011-11-08 15:32:04 -0800122 security,
123 false);
Paul Stewartd08f4432011-11-04 07:48:20 -0700124 }
125
126 bool TestStorageSecurityIs(WiFiServiceRefPtr wifi_service,
127 const string &security) {
128 string id = wifi_service->GetStorageIdentifier();
129 size_t mac_pos = id.find(StringToLowerASCII(string(fake_mac)));
130 EXPECT_NE(mac_pos, string::npos);
131 size_t mode_pos = id.find(string(flimflam::kModeManaged), mac_pos);
132 EXPECT_NE(mode_pos, string::npos);
133 return id.find(string(security), mode_pos) != string::npos;
134 }
135
136 // Test that a service that is created with security |from_security|
137 // gets by default a storage identifier with |to_security| as its
138 // security component.
139 bool TestStorageMapping(const string &from_security,
140 const string &to_security) {
141 WiFiServiceRefPtr wifi_service = CreateServiceWithSecurity(from_security);
142 return TestStorageSecurityIs(wifi_service, to_security);
143 }
144
145 // Test whether a service of type |service_security| can load from a
146 // storage interface containing an entry for |storage_security|.
147 // Make sure the result meets |expectation|. If |expectation| is
148 // true, also make sure the service storage identifier changes to
149 // match |storage_security|.
150 bool TestLoadMapping(const string &service_security,
151 const string &storage_security,
152 bool expectation) {
153 WiFiServiceRefPtr wifi_service =
154 CreateServiceWithSecurity(service_security);
155 NiceMock<MockStore> mock_store;
156 const string storage_id =
157 wifi_service->GetStorageIdentifierForSecurity(storage_security);
158 EXPECT_CALL(mock_store, ContainsGroup(_))
159 .WillRepeatedly(Return(false));
160 EXPECT_CALL(mock_store, ContainsGroup(StrEq(storage_id)))
161 .WillRepeatedly(Return(true));
162 bool is_loadable = wifi_service->IsLoadableFrom(&mock_store);
163 EXPECT_EQ(expectation, is_loadable);
164 bool is_loaded = wifi_service->Load(&mock_store);
165 EXPECT_EQ(expectation, is_loaded);
166
167 if (expectation != is_loadable || expectation != is_loaded) {
168 return false;
169 } else if (!expectation) {
170 return true;
171 } else {
172 return TestStorageSecurityIs(wifi_service, storage_security);
173 }
174 }
175};
176
mukesh agrawale1d90e92012-02-15 17:36:08 -0800177class WiFiServiceUpdateFromEndpointsTest : public WiFiServiceTest {
178 public:
179 WiFiServiceUpdateFromEndpointsTest()
180 : kOkEndpointStrength(WiFiService::SignalToStrength(kOkEndpointSignal)),
181 kBadEndpointStrength(WiFiService::SignalToStrength(kBadEndpointSignal)),
182 kGoodEndpointStrength(
183 WiFiService::SignalToStrength(kGoodEndpointSignal)),
184 service(MakeGenericService()),
185 adaptor(*GetAdaptor(service)) {
186 ok_endpoint = MakeEndpoint(
mukesh agrawal923f14f2012-06-04 16:46:08 -0700187 "", kOkEndpointBssId, kOkEndpointFrequency, kOkEndpointSignal);
mukesh agrawale1d90e92012-02-15 17:36:08 -0800188 good_endpoint = MakeEndpoint(
mukesh agrawal923f14f2012-06-04 16:46:08 -0700189 "", kGoodEndpointBssId, kGoodEndpointFrequency, kGoodEndpointSignal);
mukesh agrawale1d90e92012-02-15 17:36:08 -0800190 bad_endpoint = MakeEndpoint(
mukesh agrawal923f14f2012-06-04 16:46:08 -0700191 "", kBadEndpointBssId, kBadEndpointFrequency, kBadEndpointSignal);
mukesh agrawale1d90e92012-02-15 17:36:08 -0800192 }
193
194 protected:
195 static const uint16 kOkEndpointFrequency = 2422;
196 static const uint16 kBadEndpointFrequency = 2417;
197 static const uint16 kGoodEndpointFrequency = 2412;
198 static const int16 kOkEndpointSignal = -50;
199 static const int16 kBadEndpointSignal = -75;
200 static const int16 kGoodEndpointSignal = -25;
mukesh agrawal923f14f2012-06-04 16:46:08 -0700201 static const char *kOkEndpointBssId;
202 static const char *kGoodEndpointBssId;
203 static const char *kBadEndpointBssId;
mukesh agrawale1d90e92012-02-15 17:36:08 -0800204 // Can't be both static and const (because initialization requires a
205 // function call). So choose to be just const.
206 const uint8 kOkEndpointStrength;
207 const uint8 kBadEndpointStrength;
208 const uint8 kGoodEndpointStrength;
209 WiFiEndpointRefPtr ok_endpoint;
210 WiFiEndpointRefPtr bad_endpoint;
211 WiFiEndpointRefPtr good_endpoint;
212 WiFiServiceRefPtr service;
213 ServiceMockAdaptor &adaptor;
214};
215
mukesh agrawal923f14f2012-06-04 16:46:08 -0700216const char *WiFiServiceUpdateFromEndpointsTest::kOkEndpointBssId =
217 "00:00:00:00:00:01";
218const char *WiFiServiceUpdateFromEndpointsTest::kGoodEndpointBssId =
219 "00:00:00:00:00:02";
220const char *WiFiServiceUpdateFromEndpointsTest::kBadEndpointBssId =
221 "00:00:00:00:00:03";
mukesh agrawale1d90e92012-02-15 17:36:08 -0800222
Chris Masone34af2182011-08-22 11:59:36 -0700223TEST_F(WiFiServiceTest, StorageId) {
Gaurav Shahda6218a2011-11-11 12:09:33 -0800224 vector<uint8_t> ssid(5);
Chris Masone34af2182011-08-22 11:59:36 -0700225 ssid.push_back(0xff);
Chris Masone9d779932011-08-25 16:33:41 -0700226
Chris Masone2176a882011-09-14 22:29:15 -0700227 WiFiServiceRefPtr wifi_service = new WiFiService(control_interface(),
228 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -0800229 metrics(),
Chris Masone9d779932011-08-25 16:33:41 -0700230 manager(),
mukesh agrawal6e277772011-09-29 15:04:23 -0700231 wifi(),
Chris Masone9d779932011-08-25 16:33:41 -0700232 ssid,
233 flimflam::kModeManaged,
Paul Stewartced6a0b2011-11-08 15:32:04 -0800234 flimflam::kSecurityNone,
235 false);
Chris Masone9d779932011-08-25 16:33:41 -0700236 string id = wifi_service->GetStorageIdentifier();
Chris Masone34af2182011-08-22 11:59:36 -0700237 for (uint i = 0; i < id.length(); ++i) {
238 EXPECT_TRUE(id[i] == '_' ||
239 isxdigit(id[i]) ||
240 (isalpha(id[i]) && islower(id[i])));
241 }
Chris Masone34af2182011-08-22 11:59:36 -0700242 size_t mac_pos = id.find(StringToLowerASCII(string(fake_mac)));
243 EXPECT_NE(mac_pos, string::npos);
244 EXPECT_NE(id.find(string(flimflam::kModeManaged), mac_pos), string::npos);
245}
246
Gaurav Shahda6218a2011-11-11 12:09:33 -0800247// Make sure the passphrase is registered as a write only property
248// by reading and comparing all string properties returned on the store.
249TEST_F(WiFiServiceTest, PassphraseWriteOnly) {
250 vector<uint8_t> ssid(5);
251 WiFiServiceRefPtr wifi_service = new WiFiService(control_interface(),
252 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -0800253 metrics(),
Gaurav Shahda6218a2011-11-11 12:09:33 -0800254 manager(),
255 wifi(),
256 ssid,
257 flimflam::kModeManaged,
258 flimflam::kSecurityWpa,
259 false);
260 ReadablePropertyConstIterator<string> it =
261 (wifi_service->store()).GetStringPropertiesIter();
262 for( ; !it.AtEnd(); it.Advance())
263 EXPECT_NE(it.Key(), flimflam::kPassphraseProperty);
264}
265
Thieu Lef7709452011-11-15 01:13:19 +0000266// Make sure setting the passphrase via D-Bus Service.SetProperty validates
267// the passphrase.
268TEST_F(WiFiServiceTest, PassphraseSetPropertyValidation) {
269 // We only spot check two password cases here to make sure the
270 // SetProperty code path does validation. We're not going to exhaustively
271 // test for all types of passwords.
272 vector<uint8_t> ssid(5);
273 WiFiServiceRefPtr wifi_service = new WiFiService(control_interface(),
274 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -0800275 metrics(),
Thieu Lef7709452011-11-15 01:13:19 +0000276 manager(),
277 wifi(),
278 ssid,
279 flimflam::kModeManaged,
280 flimflam::kSecurityWep,
281 false);
282 Error error;
283 EXPECT_TRUE(wifi_service->mutable_store()->SetStringProperty(
284 flimflam::kPassphraseProperty, "0:abcde", &error));
285 EXPECT_FALSE(wifi_service->mutable_store()->SetStringProperty(
286 flimflam::kPassphraseProperty, "invalid", &error));
287 EXPECT_EQ(Error::kInvalidPassphrase, error.type());
288}
289
290TEST_F(WiFiServiceTest, PassphraseSetPropertyOpenNetwork) {
291 vector<uint8_t> ssid(5);
292 WiFiServiceRefPtr wifi_service = new WiFiService(control_interface(),
293 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -0800294 metrics(),
Thieu Lef7709452011-11-15 01:13:19 +0000295 manager(),
296 wifi(),
297 ssid,
298 flimflam::kModeManaged,
299 flimflam::kSecurityNone,
300 false);
301 Error error;
302 EXPECT_FALSE(wifi_service->mutable_store()->SetStringProperty(
303 flimflam::kPassphraseProperty, "invalid", &error));
304 EXPECT_EQ(Error::kNotSupported, error.type());
305}
306
mukesh agrawald835b202011-10-07 15:26:47 -0700307TEST_F(WiFiServiceTest, NonUTF8SSID) {
308 vector<uint8_t> ssid;
309
310 ssid.push_back(0xff); // not a valid UTF-8 byte-sequence
311 WiFiServiceRefPtr wifi_service = new WiFiService(control_interface(),
312 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -0800313 metrics(),
mukesh agrawald835b202011-10-07 15:26:47 -0700314 manager(),
315 wifi(),
316 ssid,
317 flimflam::kModeManaged,
Paul Stewartced6a0b2011-11-08 15:32:04 -0800318 flimflam::kSecurityNone,
319 false);
mukesh agrawald835b202011-10-07 15:26:47 -0700320 map<string, ::DBus::Variant> properties;
321 // if service doesn't propertly sanitize SSID, this will generate SIGABRT.
322 DBusAdaptor::GetProperties(wifi_service->store(), &properties, NULL);
323}
324
Gaurav Shahda6218a2011-11-11 12:09:33 -0800325MATCHER(WPASecurityArgs, "") {
326 return ContainsKey(arg, wpa_supplicant::kPropertySecurityProtocol) &&
327 ContainsKey(arg, wpa_supplicant::kPropertyPreSharedKey);
328}
329
Paul Stewarta5e7d5f2013-01-09 18:06:15 -0800330MATCHER(WPA80211wSecurityArgs, "") {
331 return ContainsKey(arg, wpa_supplicant::kPropertySecurityProtocol) &&
332 ContainsKey(arg, wpa_supplicant::kPropertyPreSharedKey) &&
333 ContainsKey(arg, wpa_supplicant::kNetworkPropertyIeee80211w);
334}
335
Gaurav Shah10109f22011-11-11 20:16:22 -0800336MATCHER(EAPSecurityArgs, "") {
337 return ContainsKey(arg, wpa_supplicant::kNetworkPropertyEapIdentity) &&
338 ContainsKey(arg, wpa_supplicant::kNetworkPropertyCaPath);
339}
340
Paul Stewarte2d7c502012-07-16 16:35:10 -0700341MATCHER_P(FrequencyArg, has_arg, "") {
342 return has_arg ==
343 ContainsKey(arg, wpa_supplicant::kNetworkPropertyFrequency);
344}
345
mukesh agrawalf2fd7452011-10-03 16:38:47 -0700346TEST_F(WiFiServiceTest, ConnectTaskWPA) {
Gaurav Shahda6218a2011-11-11 12:09:33 -0800347 vector<uint8_t> ssid(5);
mukesh agrawal6e277772011-09-29 15:04:23 -0700348 WiFiServiceRefPtr wifi_service = new WiFiService(control_interface(),
349 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -0800350 metrics(),
mukesh agrawal6e277772011-09-29 15:04:23 -0700351 manager(),
352 wifi(),
353 ssid,
354 flimflam::kModeManaged,
Paul Stewartced6a0b2011-11-08 15:32:04 -0800355 flimflam::kSecurityWpa,
356 false);
mukesh agrawal6e277772011-09-29 15:04:23 -0700357 EXPECT_CALL(*wifi(),
358 ConnectTo(wifi_service.get(), WPASecurityArgs()));
Wade Guthrie005bd342012-05-02 09:37:07 -0700359 Error error;
360 wifi_service->SetPassphrase("0:mumblemumblem", &error);
Eric Shienbrood9a245532012-03-07 14:20:39 -0500361 wifi_service->Connect(NULL);
mukesh agrawal6e277772011-09-29 15:04:23 -0700362}
363
mukesh agrawalf2fd7452011-10-03 16:38:47 -0700364TEST_F(WiFiServiceTest, ConnectTaskRSN) {
Gaurav Shahda6218a2011-11-11 12:09:33 -0800365 vector<uint8_t> ssid(5);
mukesh agrawalf2fd7452011-10-03 16:38:47 -0700366 WiFiServiceRefPtr wifi_service = new WiFiService(control_interface(),
367 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -0800368 metrics(),
mukesh agrawalf2fd7452011-10-03 16:38:47 -0700369 manager(),
370 wifi(),
371 ssid,
372 flimflam::kModeManaged,
Paul Stewartced6a0b2011-11-08 15:32:04 -0800373 flimflam::kSecurityRsn,
374 false);
mukesh agrawalf2fd7452011-10-03 16:38:47 -0700375 EXPECT_CALL(*wifi(),
376 ConnectTo(wifi_service.get(), WPASecurityArgs()));
Wade Guthrie005bd342012-05-02 09:37:07 -0700377 Error error;
378 wifi_service->SetPassphrase("0:mumblemumblem", &error);
Eric Shienbrood9a245532012-03-07 14:20:39 -0500379 wifi_service->Connect(NULL);
mukesh agrawalf2fd7452011-10-03 16:38:47 -0700380}
381
Christopher Wiley1ce658d2012-10-10 10:02:03 -0700382TEST_F(WiFiServiceTest, ConnectConditions) {
383 Error error;
384 vector<uint8_t> ssid(5);
385 WiFiServiceRefPtr wifi_service = new WiFiService(control_interface(),
386 dispatcher(),
387 metrics(),
388 manager(),
389 wifi(),
390 ssid,
391 flimflam::kModeManaged,
392 flimflam::kSecurityNone,
393 false);
394 scoped_refptr<MockProfile> mock_profile(
395 new NiceMock<MockProfile>(control_interface(), manager()));
396 wifi_service->set_profile(mock_profile);
397 // With nothing else going on, the service should attempt to connect.
398 EXPECT_CALL(*wifi(), ConnectTo(wifi_service.get(), _));
399 wifi_service->Connect(&error);
400 Mock::VerifyAndClearExpectations(wifi());
401
402 // But if we're already "connecting" or "connected" then we shouldn't attempt
403 // again.
404 EXPECT_CALL(*wifi(),
405 ConnectTo(wifi_service.get(), _)).Times(0);
406 wifi_service->SetState(Service::kStateAssociating);
407 wifi_service->Connect(&error);
408 wifi_service->SetState(Service::kStateConfiguring);
409 wifi_service->Connect(&error);
410 wifi_service->SetState(Service::kStateConnected);
411 wifi_service->Connect(&error);
412 wifi_service->SetState(Service::kStatePortal);
413 wifi_service->Connect(&error);
414 wifi_service->SetState(Service::kStateOnline);
415 wifi_service->Connect(&error);
416 Mock::VerifyAndClearExpectations(wifi());
417}
418
Gaurav Shahf8721ee2011-11-07 09:12:46 -0800419TEST_F(WiFiServiceTest, ConnectTaskPSK) {
Gaurav Shahda6218a2011-11-11 12:09:33 -0800420 vector<uint8_t> ssid(5);
Gaurav Shahf8721ee2011-11-07 09:12:46 -0800421 WiFiServiceRefPtr wifi_service = new WiFiService(control_interface(),
422 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -0800423 metrics(),
Gaurav Shahf8721ee2011-11-07 09:12:46 -0800424 manager(),
425 wifi(),
426 ssid,
427 flimflam::kModeManaged,
Paul Stewartced6a0b2011-11-08 15:32:04 -0800428 flimflam::kSecurityPsk,
429 false);
Gaurav Shahf8721ee2011-11-07 09:12:46 -0800430 EXPECT_CALL(*wifi(),
431 ConnectTo(wifi_service.get(), WPASecurityArgs()));
Wade Guthrie005bd342012-05-02 09:37:07 -0700432 Error error;
433 wifi_service->SetPassphrase("0:mumblemumblem", &error);
Eric Shienbrood9a245532012-03-07 14:20:39 -0500434 wifi_service->Connect(NULL);
Gaurav Shahf8721ee2011-11-07 09:12:46 -0800435}
436
Gaurav Shah10109f22011-11-11 20:16:22 -0800437TEST_F(WiFiServiceTest, ConnectTask8021x) {
438 vector<uint8_t> ssid(5);
439 WiFiServiceRefPtr wifi_service = new WiFiService(control_interface(),
440 dispatcher(),
441 metrics(),
442 manager(),
443 wifi(),
444 ssid,
445 flimflam::kModeManaged,
446 flimflam::kSecurity8021x,
447 false);
448 Service::EapCredentials eap;
449 eap.identity = "identity";
Wade Guthrie005bd342012-05-02 09:37:07 -0700450 eap.password = "mumble";
Gaurav Shah10109f22011-11-11 20:16:22 -0800451 wifi_service->set_eap(eap);
452 EXPECT_CALL(*wifi(),
453 ConnectTo(wifi_service.get(), EAPSecurityArgs()));
Eric Shienbrood9a245532012-03-07 14:20:39 -0500454 wifi_service->Connect(NULL);
Gaurav Shah10109f22011-11-11 20:16:22 -0800455}
456
Paul Stewarte2d7c502012-07-16 16:35:10 -0700457TEST_F(WiFiServiceTest, ConnectTaskAdHocFrequency) {
458 vector<uint8_t> ssid(1, 'a');
459 WiFiEndpointRefPtr endpoint_nofreq =
460 MakeEndpoint("a", "00:00:00:00:00:01", 0, 0);
461 WiFiEndpointRefPtr endpoint_freq =
462 MakeEndpoint("a", "00:00:00:00:00:02", 2412, 0);
463
464 WiFiServiceRefPtr wifi_service = new WiFiService(control_interface(),
465 dispatcher(),
466 metrics(),
467 manager(),
468 wifi(),
469 ssid,
470 flimflam::kModeManaged,
471 flimflam::kSecurityNone,
472 false);
473 wifi_service->AddEndpoint(endpoint_freq);
474 EXPECT_CALL(*wifi(),
475 ConnectTo(wifi_service.get(), FrequencyArg(false)));
476 wifi_service->Connect(NULL);
477
478 wifi_service = new WiFiService(control_interface(),
479 dispatcher(),
480 metrics(),
481 manager(),
482 wifi(),
483 ssid,
484 flimflam::kModeAdhoc,
485 flimflam::kSecurityNone,
486 false);
487 EXPECT_CALL(*wifi(),
488 ConnectTo(wifi_service.get(), FrequencyArg(false)));
489 wifi_service->Connect(NULL);
490
491 wifi_service = new WiFiService(control_interface(),
492 dispatcher(),
493 metrics(),
494 manager(),
495 wifi(),
496 ssid,
497 flimflam::kModeAdhoc,
498 flimflam::kSecurityNone,
499 false);
500 wifi_service->AddEndpoint(endpoint_nofreq);
501 EXPECT_CALL(*wifi(),
502 ConnectTo(wifi_service.get(), FrequencyArg(false)));
503 wifi_service->Connect(NULL);
504
505 wifi_service = new WiFiService(control_interface(),
506 dispatcher(),
507 metrics(),
508 manager(),
509 wifi(),
510 ssid,
511 flimflam::kModeAdhoc,
512 flimflam::kSecurityNone,
513 false);
514 wifi_service->AddEndpoint(endpoint_freq);
515 EXPECT_CALL(*wifi(),
516 ConnectTo(wifi_service.get(), FrequencyArg(true)));
517 wifi_service->Connect(NULL);
518}
519
Paul Stewarta5e7d5f2013-01-09 18:06:15 -0800520TEST_F(WiFiServiceTest, ConnectTaskWPA80211w) {
521 vector<uint8_t> ssid(1, 'a');
522 WiFiServiceRefPtr wifi_service = new WiFiService(control_interface(),
523 dispatcher(),
524 metrics(),
525 manager(),
526 wifi(),
527 ssid,
528 flimflam::kModeManaged,
529 flimflam::kSecurityPsk,
530 false);
531 WiFiEndpointRefPtr endpoint = MakeEndpoint("a", "00:00:00:00:00:01", 0, 0);
532 endpoint->ieee80211w_required_ = true;
533 wifi_service->AddEndpoint(endpoint);
534 Error error;
535 wifi_service->SetPassphrase("0:mumblemumblem", &error);
536 EXPECT_CALL(*wifi(),
537 ConnectTo(wifi_service.get(), WPA80211wSecurityArgs()));
538 wifi_service->Connect(NULL);
539}
540
Thieu Lef4cbda92011-11-10 23:41:24 +0000541MATCHER(WEPSecurityArgsKeyIndex0, "") {
542 return ContainsKey(arg, wpa_supplicant::kPropertyAuthAlg) &&
543 ContainsKey(arg, wpa_supplicant::kPropertyWEPKey + std::string("0")) &&
544 ContainsKey(arg, wpa_supplicant::kPropertyWEPTxKeyIndex) &&
545 (arg.find(wpa_supplicant::kPropertyWEPTxKeyIndex)->second.
546 reader().get_uint32() == 0);
547}
548
549MATCHER(WEPSecurityArgsKeyIndex1, "") {
550 return ContainsKey(arg, wpa_supplicant::kPropertyAuthAlg) &&
551 ContainsKey(arg, wpa_supplicant::kPropertyWEPKey + std::string("1")) &&
552 ContainsKey(arg, wpa_supplicant::kPropertyWEPTxKeyIndex) &&
553 (arg.find(wpa_supplicant::kPropertyWEPTxKeyIndex)->second.
554 reader().get_uint32() == 1);
555}
556
557MATCHER(WEPSecurityArgsKeyIndex2, "") {
558 return ContainsKey(arg, wpa_supplicant::kPropertyAuthAlg) &&
559 ContainsKey(arg, wpa_supplicant::kPropertyWEPKey + std::string("2")) &&
560 ContainsKey(arg, wpa_supplicant::kPropertyWEPTxKeyIndex) &&
561 (arg.find(wpa_supplicant::kPropertyWEPTxKeyIndex)->second.
562 reader().get_uint32() == 2);
563}
564
565MATCHER(WEPSecurityArgsKeyIndex3, "") {
566 return ContainsKey(arg, wpa_supplicant::kPropertyAuthAlg) &&
567 ContainsKey(arg, wpa_supplicant::kPropertyWEPKey + std::string("3")) &&
568 ContainsKey(arg, wpa_supplicant::kPropertyWEPTxKeyIndex) &&
569 (arg.find(wpa_supplicant::kPropertyWEPTxKeyIndex)->second.
570 reader().get_uint32() == 3);
571}
572
573TEST_F(WiFiServiceTest, ConnectTaskWEP) {
574 vector<uint8_t> ssid(5);
575 WiFiServiceRefPtr wifi_service = new WiFiService(control_interface(),
576 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -0800577 metrics(),
Thieu Lef4cbda92011-11-10 23:41:24 +0000578 manager(),
579 wifi(),
580 ssid,
581 flimflam::kModeManaged,
582 flimflam::kSecurityWep,
583 false);
584 Error error;
585 wifi_service->SetPassphrase("0:abcdefghijklm", &error);
586 EXPECT_CALL(*wifi(),
587 ConnectTo(wifi_service.get(), WEPSecurityArgsKeyIndex0()));
Eric Shienbrood9a245532012-03-07 14:20:39 -0500588 wifi_service->Connect(NULL);
Thieu Lef4cbda92011-11-10 23:41:24 +0000589
590 wifi_service->SetPassphrase("abcdefghijklm", &error);
591 EXPECT_CALL(*wifi(),
592 ConnectTo(wifi_service.get(), WEPSecurityArgsKeyIndex0()));
Eric Shienbrood9a245532012-03-07 14:20:39 -0500593 wifi_service->Connect(NULL);
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()));
Eric Shienbrood9a245532012-03-07 14:20:39 -0500598 wifi_service->Connect(NULL);
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()));
Eric Shienbrood9a245532012-03-07 14:20:39 -0500603 wifi_service->Connect(NULL);
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()));
Eric Shienbrood9a245532012-03-07 14:20:39 -0500608 wifi_service->Connect(NULL);
Thieu Lef4cbda92011-11-10 23:41:24 +0000609}
610
Gaurav Shah29d68882012-01-30 19:06:42 -0800611
612MATCHER(DynamicWEPArgs, "") {
613 return ContainsKey(arg, wpa_supplicant::kNetworkPropertyEapIdentity) &&
614 ContainsKey(arg, wpa_supplicant::kNetworkPropertyCaPath) &&
615 !ContainsKey(arg, wpa_supplicant::kPropertySecurityProtocol);
616}
617
618// Dynamic WEP + 802.1x.
619TEST_F(WiFiServiceTest, ConnectTaskDynamicWEP) {
620 vector<uint8_t> ssid(5);
621 WiFiServiceRefPtr wifi_service = new WiFiService(control_interface(),
622 dispatcher(),
623 metrics(),
624 manager(),
625 wifi(),
626 ssid,
627 flimflam::kModeManaged,
628 flimflam::kSecurityWep,
629 false);
630
631 Service::EapCredentials eap;
632 eap.key_management = "IEEE8021X";
633 eap.identity = "something";
Wade Guthrie005bd342012-05-02 09:37:07 -0700634 eap.password = "mumble";
Gaurav Shah29d68882012-01-30 19:06:42 -0800635 wifi_service->set_eap(eap);
636 EXPECT_CALL(*wifi(),
637 ConnectTo(wifi_service.get(), DynamicWEPArgs()));
Eric Shienbrood9a245532012-03-07 14:20:39 -0500638 wifi_service->Connect(NULL);
Gaurav Shah29d68882012-01-30 19:06:42 -0800639}
640
Paul Stewart835934a2012-12-06 19:27:09 -0800641TEST_F(WiFiServiceTest, SetPassphraseRemovesCachedCredentials) {
642 vector<uint8_t> ssid(5);
643 WiFiServiceRefPtr wifi_service = new WiFiService(control_interface(),
644 dispatcher(),
645 metrics(),
646 manager(),
647 wifi(),
648 ssid,
649 flimflam::kModeManaged,
650 flimflam::kSecurityRsn,
651 false);
652
653 const string kPassphrase = "abcdefgh";
654
655 {
656 Error error;
657 // A changed passphrase should trigger cache removal.
658 EXPECT_CALL(*wifi(), ClearCachedCredentials(wifi_service.get()));
659 wifi_service->SetPassphrase(kPassphrase, &error);
660 Mock::VerifyAndClearExpectations(wifi());
661 EXPECT_TRUE(error.IsSuccess());
662 }
663
664 {
665 Error error;
666 // An unchanged passphrase should not trigger cache removal.
667 EXPECT_CALL(*wifi(), ClearCachedCredentials(_)).Times(0);
668 wifi_service->SetPassphrase(kPassphrase, &error);
669 Mock::VerifyAndClearExpectations(wifi());
670 EXPECT_TRUE(error.IsSuccess());
671 }
672
673 {
674 Error error;
675 // A modified passphrase should trigger cache removal.
676 EXPECT_CALL(*wifi(), ClearCachedCredentials(wifi_service.get()));
677 wifi_service->SetPassphrase(kPassphrase + "X", &error);
678 Mock::VerifyAndClearExpectations(wifi());
679 EXPECT_TRUE(error.IsSuccess());
680 }
681
682 {
683 Error error;
684 // A cleared passphrase should also trigger cache removal.
685 EXPECT_CALL(*wifi(), ClearCachedCredentials(wifi_service.get()));
686 wifi_service->ClearPassphrase(&error);
687 Mock::VerifyAndClearExpectations(wifi());
688 EXPECT_TRUE(error.IsSuccess());
689 }
690
691 {
692 Error error;
693 // An invalid passphrase should not trigger cache removal.
694 EXPECT_CALL(*wifi(), ClearCachedCredentials(_)).Times(0);
695 wifi_service->SetPassphrase("", &error);
696 Mock::VerifyAndClearExpectations(wifi());
697 EXPECT_FALSE(error.IsSuccess());
698 }
699
700 {
701 // Any change to EAP parameters (including a null one) will trigger cache
702 // removal. This is a lot less granular than the passphrase checks above.
703 EXPECT_CALL(*wifi(), ClearCachedCredentials(wifi_service.get()));
704 wifi_service->set_eap(Service::EapCredentials());
705 Mock::VerifyAndClearExpectations(wifi());
706 }
707}
708
Paul Stewartd08f4432011-11-04 07:48:20 -0700709TEST_F(WiFiServiceTest, LoadHidden) {
Gaurav Shahda6218a2011-11-11 12:09:33 -0800710 vector<uint8_t> ssid(5);
Paul Stewartd08f4432011-11-04 07:48:20 -0700711 ssid.push_back(0xff);
712
713 WiFiServiceRefPtr service = new WiFiService(control_interface(),
714 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -0800715 metrics(),
Paul Stewartd08f4432011-11-04 07:48:20 -0700716 manager(),
717 wifi(),
718 ssid,
719 flimflam::kModeManaged,
Paul Stewartced6a0b2011-11-08 15:32:04 -0800720 flimflam::kSecurityNone,
721 false);
Paul Stewartd08f4432011-11-04 07:48:20 -0700722 ASSERT_FALSE(service->hidden_ssid_);
723 NiceMock<MockStore> mock_store;
724 const string storage_id = service->GetStorageIdentifier();
725 EXPECT_CALL(mock_store, ContainsGroup(StrEq(storage_id)))
726 .WillRepeatedly(Return(true));
727 EXPECT_CALL(mock_store, GetBool(_, _, _))
728 .WillRepeatedly(Return(false));
729 EXPECT_CALL(mock_store,
730 GetBool(StrEq(storage_id), WiFiService::kStorageHiddenSSID, _))
731 .WillRepeatedly(DoAll(SetArgumentPointee<2>(true), Return(true)));
732 EXPECT_TRUE(service->Load(&mock_store));
733 EXPECT_TRUE(service->hidden_ssid_);
734}
735
736TEST_F(WiFiServiceSecurityTest, WPAMapping) {
737 EXPECT_TRUE(TestStorageMapping(flimflam::kSecurityRsn,
738 flimflam::kSecurityPsk));
739 EXPECT_TRUE(TestStorageMapping(flimflam::kSecurityWpa,
740 flimflam::kSecurityPsk));
741 EXPECT_TRUE(TestStorageMapping(flimflam::kSecurityPsk,
742 flimflam::kSecurityPsk));
743 EXPECT_TRUE(TestStorageMapping(flimflam::kSecurityWep,
744 flimflam::kSecurityWep));
745 EXPECT_TRUE(TestStorageMapping(flimflam::kSecurityNone,
746 flimflam::kSecurityNone));
Gaurav Shah10109f22011-11-11 20:16:22 -0800747 EXPECT_TRUE(TestStorageMapping(flimflam::kSecurity8021x,
748 flimflam::kSecurity8021x));
Paul Stewartd08f4432011-11-04 07:48:20 -0700749}
750
751TEST_F(WiFiServiceSecurityTest, LoadMapping) {
752 EXPECT_TRUE(TestLoadMapping(flimflam::kSecurityRsn,
753 flimflam::kSecurityPsk,
754 true));
755 EXPECT_TRUE(TestLoadMapping(flimflam::kSecurityRsn,
756 flimflam::kSecurityRsn,
757 true));
758 EXPECT_TRUE(TestLoadMapping(flimflam::kSecurityRsn,
759 flimflam::kSecurityWpa,
760 false));
761 EXPECT_TRUE(TestLoadMapping(flimflam::kSecurityWpa,
762 flimflam::kSecurityPsk,
763 true));
764 EXPECT_TRUE(TestLoadMapping(flimflam::kSecurityWpa,
765 flimflam::kSecurityWpa,
766 true));
767 EXPECT_TRUE(TestLoadMapping(flimflam::kSecurityWpa,
768 flimflam::kSecurityRsn,
769 false));
770 EXPECT_TRUE(TestLoadMapping(flimflam::kSecurityWep,
771 flimflam::kSecurityWep,
772 true));
773 EXPECT_TRUE(TestLoadMapping(flimflam::kSecurityWep,
774 flimflam::kSecurityPsk,
775 false));
776}
777
Paul Stewartd8ad3c42012-01-09 12:39:38 -0800778TEST_F(WiFiServiceTest, LoadAndUnloadPassphrase) {
779 vector<uint8_t> ssid(5);
780 ssid.push_back(0xff);
781
782 WiFiServiceRefPtr service = new WiFiService(control_interface(),
783 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -0800784 metrics(),
Paul Stewartd8ad3c42012-01-09 12:39:38 -0800785 manager(),
786 wifi(),
787 ssid,
788 flimflam::kModeManaged,
789 flimflam::kSecurityPsk,
790 false);
791 NiceMock<MockStore> mock_store;
792 const string storage_id = service->GetStorageIdentifier();
793 EXPECT_CALL(mock_store, ContainsGroup(StrEq(storage_id)))
794 .WillRepeatedly(Return(true));
795 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;
825 vector<uint8_t> ssid(5);
826 ssid.push_back(0xff);
827
828 WiFiServiceRefPtr service = new WiFiService(control_interface(),
829 dispatcher(),
830 metrics(),
831 manager(),
832 wifi(),
833 ssid,
834 flimflam::kModeManaged,
835 flimflam::kSecurity8021x,
836 false);
837 // Hack the GUID in so that we don't have to mess about with WiFi to regsiter
838 // our service. This way, Manager will handle the lookup itself.
839 service->set_guid(guid);
840 manager()->RegisterService(service);
841 EXPECT_FALSE(service->connectable());
842 EXPECT_EQ(service.get(), manager()->GetService(args, &error).get());
843 EXPECT_TRUE(error.IsSuccess());
844 EXPECT_TRUE(service->connectable());
845}
846
Paul Stewart835934a2012-12-06 19:27:09 -0800847TEST_F(WiFiServiceTest, UnloadAndClearCacheWEP) {
Paul Stewart66c86002012-01-30 18:00:52 -0800848 vector<uint8_t> ssid(1, 'a');
849 WiFiServiceRefPtr service = new WiFiService(control_interface(),
850 dispatcher(),
851 metrics(),
852 manager(),
853 wifi(),
854 ssid,
855 flimflam::kModeManaged,
856 flimflam::kSecurityWep,
857 false);
Paul Stewart835934a2012-12-06 19:27:09 -0800858 EXPECT_CALL(*wifi(), ClearCachedCredentials(service.get())).Times(1);
859 EXPECT_CALL(*wifi(), DisconnectFrom(service.get())).Times(1);
Paul Stewart66c86002012-01-30 18:00:52 -0800860 service->Unload();
861}
862
863TEST_F(WiFiServiceTest, UnloadAndClearCache8021x) {
864 vector<uint8_t> ssid(1, 'a');
865 WiFiServiceRefPtr service = new WiFiService(control_interface(),
866 dispatcher(),
867 metrics(),
868 manager(),
869 wifi(),
870 ssid,
871 flimflam::kModeManaged,
872 flimflam::kSecurity8021x,
873 false);
Paul Stewart835934a2012-12-06 19:27:09 -0800874 EXPECT_CALL(*wifi(), ClearCachedCredentials(service.get())).Times(1);
875 EXPECT_CALL(*wifi(), DisconnectFrom(service.get())).Times(1);
Paul Stewart66c86002012-01-30 18:00:52 -0800876 service->Unload();
877}
878
Paul Stewart0756db92012-01-27 08:34:47 -0800879TEST_F(WiFiServiceTest, ParseStorageIdentifierNone) {
Paul Stewarta41e38d2011-11-11 07:47:29 -0800880 vector<uint8_t> ssid(5);
881 ssid.push_back(0xff);
882
883 WiFiServiceRefPtr service = new WiFiService(control_interface(),
884 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -0800885 metrics(),
Paul Stewarta41e38d2011-11-11 07:47:29 -0800886 manager(),
887 wifi(),
888 ssid,
889 flimflam::kModeManaged,
890 flimflam::kSecurityNone,
891 false);
892 const string storage_id = service->GetStorageIdentifier();
893 string address;
894 string mode;
895 string security;
896 EXPECT_TRUE(service->ParseStorageIdentifier(storage_id, &address, &mode,
897 &security));
898 EXPECT_EQ(StringToLowerASCII(string(fake_mac)), address);
899 EXPECT_EQ(flimflam::kModeManaged, mode);
900 EXPECT_EQ(flimflam::kSecurityNone, security);
901}
902
Paul Stewart0756db92012-01-27 08:34:47 -0800903TEST_F(WiFiServiceTest, ParseStorageIdentifier8021x) {
904 // Do a separate test for 802.1x, since kSecurity8021x contains a "_",
905 // which needs to be dealt with specially in the parser.
906 vector<uint8_t> ssid(5);
907 ssid.push_back(0xff);
908
909 WiFiServiceRefPtr service = new WiFiService(control_interface(),
910 dispatcher(),
911 metrics(),
912 manager(),
913 wifi(),
914 ssid,
915 flimflam::kModeManaged,
916 flimflam::kSecurity8021x,
917 false);
918 const string storage_id = service->GetStorageIdentifier();
919 string address;
920 string mode;
921 string security;
922 EXPECT_TRUE(service->ParseStorageIdentifier(storage_id, &address, &mode,
923 &security));
924 EXPECT_EQ(StringToLowerASCII(string(fake_mac)), address);
925 EXPECT_EQ(flimflam::kModeManaged, mode);
926 EXPECT_EQ(flimflam::kSecurity8021x, security);
927}
928
mukesh agrawal29c13a12011-11-24 00:09:19 +0000929TEST_F(WiFiServiceTest, Connectable) {
930 // Open network should be connectable.
Gaurav Shah10109f22011-11-11 20:16:22 -0800931 EXPECT_TRUE(CheckConnectable(flimflam::kSecurityNone, NULL, NULL));
mukesh agrawal29c13a12011-11-24 00:09:19 +0000932
933 // Open network should remain connectable if we try to set a password on it.
Gaurav Shah10109f22011-11-11 20:16:22 -0800934 EXPECT_TRUE(CheckConnectable(flimflam::kSecurityNone, "abcde", NULL));
mukesh agrawal29c13a12011-11-24 00:09:19 +0000935
936 // WEP network with passphrase set should be connectable.
Gaurav Shah10109f22011-11-11 20:16:22 -0800937 EXPECT_TRUE(CheckConnectable(flimflam::kSecurityWep, "abcde", NULL));
mukesh agrawal29c13a12011-11-24 00:09:19 +0000938
939 // WEP network without passphrase set should NOT be connectable.
Gaurav Shah10109f22011-11-11 20:16:22 -0800940 EXPECT_FALSE(CheckConnectable(flimflam::kSecurityWep, NULL, NULL));
mukesh agrawal29c13a12011-11-24 00:09:19 +0000941
942 // A bad passphrase should not make a WEP network connectable.
Gaurav Shah10109f22011-11-11 20:16:22 -0800943 EXPECT_FALSE(CheckConnectable(flimflam::kSecurityWep, "a", NULL));
mukesh agrawal29c13a12011-11-24 00:09:19 +0000944
945 // Similar to WEP, for WPA.
Gaurav Shah10109f22011-11-11 20:16:22 -0800946 EXPECT_TRUE(CheckConnectable(flimflam::kSecurityWpa, "abcdefgh", NULL));
947 EXPECT_FALSE(CheckConnectable(flimflam::kSecurityWpa, NULL, NULL));
948 EXPECT_FALSE(CheckConnectable(flimflam::kSecurityWpa, "a", NULL));
mukesh agrawal29c13a12011-11-24 00:09:19 +0000949
950 // Unconfigured 802.1x should NOT be connectable.
Gaurav Shah10109f22011-11-11 20:16:22 -0800951 EXPECT_FALSE(CheckConnectable(flimflam::kSecurity8021x, NULL, NULL));
952
953 Service::EapCredentials eap;
954 // Empty EAP credentials should not make a 802.1x network connectable.
955 EXPECT_FALSE(CheckConnectable(flimflam::kSecurity8021x, NULL, &eap));
956
957 eap.identity = "something";
958 // If client certificate is being used, a private key must exist.
959 eap.client_cert = "some client cert";
960 EXPECT_FALSE(CheckConnectable(flimflam::kSecurity8021x, NULL, &eap));
961 eap.private_key = "some private key";
962 EXPECT_TRUE(CheckConnectable(flimflam::kSecurity8021x, NULL, &eap));
963
964 // Identity is always required.
965 eap.identity.clear();
966 EXPECT_FALSE(CheckConnectable(flimflam::kSecurity8021x, NULL, &eap));
967
968 eap.identity = "something";
969 // For non EAP-TLS types, a password is required.
970 eap.eap = "Non-TLS";
971 EXPECT_FALSE(CheckConnectable(flimflam::kSecurity8021x, NULL, &eap));
972 eap.password = "some password";
973 EXPECT_TRUE(CheckConnectable(flimflam::kSecurity8021x, NULL, &eap));
Gaurav Shah29d68882012-01-30 19:06:42 -0800974 // Dynamic WEP + 802.1X should be connectable under the same conditions.
975 eap.key_management = "IEEE8021X";
976 EXPECT_TRUE(CheckConnectable(flimflam::kSecurityWep, NULL, &eap));
mukesh agrawal29c13a12011-11-24 00:09:19 +0000977}
978
mukesh agrawal8a3188d2011-12-01 20:56:44 +0000979TEST_F(WiFiServiceTest, IsAutoConnectable) {
mukesh agrawalbf14e942012-03-02 14:36:34 -0800980 const char *reason;
mukesh agrawal8a3188d2011-12-01 20:56:44 +0000981 vector<uint8_t> ssid(1, 'a');
982 WiFiServiceRefPtr service = new WiFiService(control_interface(),
983 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -0800984 metrics(),
mukesh agrawal8a3188d2011-12-01 20:56:44 +0000985 manager(),
986 wifi(),
987 ssid,
988 flimflam::kModeManaged,
989 flimflam::kSecurityNone,
990 false);
991 EXPECT_CALL(*wifi(), IsIdle())
992 .WillRepeatedly(Return(true));
993 EXPECT_FALSE(service->HasEndpoints());
mukesh agrawalbf14e942012-03-02 14:36:34 -0800994 EXPECT_FALSE(service->IsAutoConnectable(&reason));
995 EXPECT_STREQ(WiFiService::kAutoConnNoEndpoint, reason);
mukesh agrawal8a3188d2011-12-01 20:56:44 +0000996
mukesh agrawalbf14e942012-03-02 14:36:34 -0800997 reason = "";
mukesh agrawale1d90e92012-02-15 17:36:08 -0800998 WiFiEndpointRefPtr endpoint = MakeEndpoint("a", "00:00:00:00:00:01", 0, 0);
mukesh agrawal8a3188d2011-12-01 20:56:44 +0000999 service->AddEndpoint(endpoint);
1000 EXPECT_CALL(*wifi(), IsIdle())
1001 .WillRepeatedly(Return(true));
1002 EXPECT_TRUE(service->HasEndpoints());
mukesh agrawalbf14e942012-03-02 14:36:34 -08001003 EXPECT_TRUE(service->IsAutoConnectable(&reason));
1004 EXPECT_STREQ("", reason);
mukesh agrawal8a3188d2011-12-01 20:56:44 +00001005
1006 // WiFi only supports connecting to one Service at a time. So, to
1007 // avoid disrupting connectivity, we only allow auto-connection to
1008 // a WiFiService when the corresponding WiFi is idle.
1009 EXPECT_CALL(*wifi(), IsIdle())
1010 .WillRepeatedly(Return(false));
1011 EXPECT_TRUE(service->HasEndpoints());
mukesh agrawalbf14e942012-03-02 14:36:34 -08001012 EXPECT_FALSE(service->IsAutoConnectable(&reason));
1013 EXPECT_STREQ(WiFiService::kAutoConnBusy, reason);
mukesh agrawal8a3188d2011-12-01 20:56:44 +00001014}
1015
1016TEST_F(WiFiServiceTest, AutoConnect) {
mukesh agrawalbf14e942012-03-02 14:36:34 -08001017 const char *reason;
mukesh agrawal8a3188d2011-12-01 20:56:44 +00001018 vector<uint8_t> ssid(1, 'a');
1019 WiFiServiceRefPtr service = new WiFiService(control_interface(),
1020 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -08001021 metrics(),
mukesh agrawal8a3188d2011-12-01 20:56:44 +00001022 manager(),
1023 wifi(),
1024 ssid,
1025 flimflam::kModeManaged,
1026 flimflam::kSecurityNone,
1027 false);
mukesh agrawalbf14e942012-03-02 14:36:34 -08001028 EXPECT_FALSE(service->IsAutoConnectable(&reason));
mukesh agrawal8a3188d2011-12-01 20:56:44 +00001029 EXPECT_CALL(*wifi(), ConnectTo(_, _))
1030 .Times(0);
1031 service->AutoConnect();
Eric Shienbrood3e20a232012-02-16 11:35:56 -05001032 dispatcher()->DispatchPendingEvents();
mukesh agrawal8a3188d2011-12-01 20:56:44 +00001033
mukesh agrawale1d90e92012-02-15 17:36:08 -08001034 WiFiEndpointRefPtr endpoint = MakeEndpoint("a", "00:00:00:00:00:01", 0, 0);
mukesh agrawal8a3188d2011-12-01 20:56:44 +00001035 service->AddEndpoint(endpoint);
1036 EXPECT_CALL(*wifi(), IsIdle())
1037 .WillRepeatedly(Return(true));
mukesh agrawalbf14e942012-03-02 14:36:34 -08001038 EXPECT_TRUE(service->IsAutoConnectable(&reason));
mukesh agrawal8a3188d2011-12-01 20:56:44 +00001039 EXPECT_CALL(*wifi(), ConnectTo(_, _));
1040 service->AutoConnect();
Eric Shienbrood3e20a232012-02-16 11:35:56 -05001041 dispatcher()->DispatchPendingEvents();
mukesh agrawaladb68482012-01-17 16:31:51 -08001042
1043 Error error;
Christopher Wileyabd3b502012-09-26 13:08:52 -07001044 service->UserInitiatedDisconnect(&error);
Eric Shienbrood3e20a232012-02-16 11:35:56 -05001045 dispatcher()->DispatchPendingEvents();
mukesh agrawalbf14e942012-03-02 14:36:34 -08001046 EXPECT_FALSE(service->IsAutoConnectable(&reason));
mukesh agrawal8a3188d2011-12-01 20:56:44 +00001047}
1048
Gaurav Shah10109f22011-11-11 20:16:22 -08001049TEST_F(WiFiServiceTest, Populate8021x) {
1050 vector<uint8_t> ssid(1, 'a');
1051 WiFiServiceRefPtr service = new WiFiService(control_interface(),
1052 dispatcher(),
1053 metrics(),
1054 manager(),
1055 wifi(),
1056 ssid,
1057 flimflam::kModeManaged,
1058 flimflam::kSecurityNone,
1059 false);
1060 Service::EapCredentials eap;
1061 eap.identity = "testidentity";
Paul Stewart20550982012-04-16 12:16:11 -07001062 eap.pin = "xxxx";
Gaurav Shah10109f22011-11-11 20:16:22 -08001063 service->set_eap(eap);
1064 map<string, ::DBus::Variant> params;
1065 service->Populate8021xProperties(&params);
1066 // Test that only non-empty 802.1x properties are populated.
Paul Stewartecf4cd12012-04-17 11:08:39 -07001067 EXPECT_TRUE(ContainsKey(params, wpa_supplicant::kNetworkPropertyEapIdentity));
1068 EXPECT_FALSE(ContainsKey(params, wpa_supplicant::kNetworkPropertyEapKeyId));
1069 EXPECT_FALSE(ContainsKey(params, wpa_supplicant::kNetworkPropertyEapCaCert));
1070
Paul Stewart20550982012-04-16 12:16:11 -07001071 // Test that CA path is set by default.
Paul Stewartecf4cd12012-04-17 11:08:39 -07001072 EXPECT_TRUE(ContainsKey(params, wpa_supplicant::kNetworkPropertyCaPath));
1073
Paul Stewart20550982012-04-16 12:16:11 -07001074 // Test that hardware-backed security arguments are not set.
Paul Stewartecf4cd12012-04-17 11:08:39 -07001075 EXPECT_FALSE(ContainsKey(params, wpa_supplicant::kNetworkPropertyEapPin));
1076 EXPECT_FALSE(ContainsKey(params, wpa_supplicant::kNetworkPropertyEngine));
1077 EXPECT_FALSE(ContainsKey(params, wpa_supplicant::kNetworkPropertyEngineId));
Paul Stewart20550982012-04-16 12:16:11 -07001078}
1079
1080TEST_F(WiFiServiceTest, Populate8021xNoSystemCAs) {
1081 vector<uint8_t> ssid(1, 'a');
1082 WiFiServiceRefPtr service = new WiFiService(control_interface(),
1083 dispatcher(),
1084 metrics(),
1085 manager(),
1086 wifi(),
1087 ssid,
1088 flimflam::kModeManaged,
1089 flimflam::kSecurityNone,
1090 false);
1091 Service::EapCredentials eap;
1092 eap.identity = "testidentity";
1093 eap.use_system_cas = false;
1094 service->set_eap(eap);
1095 map<string, ::DBus::Variant> params;
1096 service->Populate8021xProperties(&params);
1097 // Test that CA path is not set if use_system_cas is explicitly false.
Paul Stewartecf4cd12012-04-17 11:08:39 -07001098 EXPECT_FALSE(ContainsKey(params, wpa_supplicant::kNetworkPropertyCaPath));
Paul Stewart20550982012-04-16 12:16:11 -07001099}
1100
1101TEST_F(WiFiServiceTest, Populate8021xUsingHardwareAuth) {
1102 vector<uint8_t> ssid(1, 'a');
1103 WiFiServiceRefPtr service = new WiFiService(control_interface(),
1104 dispatcher(),
1105 metrics(),
1106 manager(),
1107 wifi(),
1108 ssid,
1109 flimflam::kModeManaged,
1110 flimflam::kSecurityNone,
1111 false);
1112 Service::EapCredentials eap;
1113 eap.identity = "testidentity";
1114 eap.key_id = "key_id";
1115 eap.pin = "xxxx";
1116 service->set_eap(eap);
1117 map<string, ::DBus::Variant> params;
1118 service->Populate8021xProperties(&params);
1119 // Test that EAP engine parameters set if key_id is set.
Paul Stewartecf4cd12012-04-17 11:08:39 -07001120 EXPECT_TRUE(ContainsKey(params, wpa_supplicant::kNetworkPropertyEapPin));
1121 EXPECT_TRUE(ContainsKey(params, wpa_supplicant::kNetworkPropertyEapKeyId));
1122 EXPECT_TRUE(ContainsKey(params, wpa_supplicant::kNetworkPropertyEngine));
1123 EXPECT_TRUE(ContainsKey(params, wpa_supplicant::kNetworkPropertyEngineId));
1124}
1125
1126TEST_F(WiFiServiceTest, Populate8021xNSS) {
1127 vector<uint8_t> ssid(1, 'a');
1128 WiFiServiceRefPtr service = new WiFiService(control_interface(),
1129 dispatcher(),
1130 metrics(),
1131 manager(),
1132 wifi(),
1133 ssid,
1134 flimflam::kModeManaged,
1135 flimflam::kSecurityNone,
1136 false);
1137 Service::EapCredentials eap;
1138 eap.ca_cert_nss = "nss_nickname";
1139 service->set_eap(eap);
1140 MockNSS nss;
1141 service->nss_ = &nss;
1142
1143 const string kNSSCertfile("/tmp/nss-cert");
1144 FilePath nss_cert(kNSSCertfile);
1145 vector<char> ssid_in_chars(ssid.begin(), ssid.end());
1146 EXPECT_CALL(nss, GetDERCertfile(eap.ca_cert_nss, ssid_in_chars))
1147 .WillOnce(Return(nss_cert));
1148
1149 map<string, ::DBus::Variant> params;
1150 service->Populate8021xProperties(&params);
1151 EXPECT_TRUE(ContainsKey(params, wpa_supplicant::kNetworkPropertyEapCaCert));
1152 if (ContainsKey(params, wpa_supplicant::kNetworkPropertyEapCaCert)) {
1153 EXPECT_EQ(kNSSCertfile, params[wpa_supplicant::kNetworkPropertyEapCaCert]
1154 .reader().get_string());
1155 }
Gaurav Shah10109f22011-11-11 20:16:22 -08001156}
1157
mukesh agrawal8abd2f62012-01-30 14:56:14 -08001158TEST_F(WiFiServiceTest, ClearWriteOnlyDerivedProperty) {
1159 vector<uint8_t> ssid(1, 'a');
1160 WiFiServiceRefPtr wifi_service = new WiFiService(control_interface(),
1161 dispatcher(),
1162 metrics(),
1163 manager(),
1164 wifi(),
1165 ssid,
1166 flimflam::kModeManaged,
1167 flimflam::kSecurityWep,
1168 false);
1169
1170 EXPECT_EQ("", wifi_service->passphrase_);
1171
1172 ::DBus::Error error;
mukesh agrawal6bb9e7c2012-01-30 14:57:54 -08001173 EXPECT_TRUE(DBusAdaptor::SetProperty(
mukesh agrawal8abd2f62012-01-30 14:56:14 -08001174 wifi_service->mutable_store(),
1175 flimflam::kPassphraseProperty,
1176 DBusAdaptor::StringToVariant("0:abcde"),
1177 &error));
1178 EXPECT_EQ("0:abcde", wifi_service->passphrase_);
1179
1180 EXPECT_TRUE(DBusAdaptor::ClearProperty(wifi_service->mutable_store(),
1181 flimflam::kPassphraseProperty,
1182 &error));
1183 EXPECT_EQ("", wifi_service->passphrase_);
1184}
1185
mukesh agrawale1d90e92012-02-15 17:36:08 -08001186TEST_F(WiFiServiceTest, SignalToStrength) {
1187 // Verify that our mapping is sane, in the sense that it preserves ordering.
1188 // We break the test into two domains, because we assume that positive
1189 // values aren't actually in dBm.
1190 for (int16 i = std::numeric_limits<int16>::min(); i < 0; ++i) {
1191 int16 current_mapped = WiFiService::SignalToStrength(i);
1192 int16 next_mapped = WiFiService::SignalToStrength(i+1);
1193 EXPECT_LE(current_mapped, next_mapped)
1194 << "(original values " << i << " " << i+1 << ")";
mukesh agrawal8f3f7752012-02-17 19:42:09 -08001195 EXPECT_GE(current_mapped, Service::kStrengthMin);
1196 EXPECT_LE(current_mapped, Service::kStrengthMax);
mukesh agrawale1d90e92012-02-15 17:36:08 -08001197 }
1198 for (int16 i = 1; i < std::numeric_limits<int16>::max(); ++i) {
1199 int16 current_mapped = WiFiService::SignalToStrength(i);
1200 int16 next_mapped = WiFiService::SignalToStrength(i+1);
1201 EXPECT_LE(current_mapped, next_mapped)
1202 << "(original values " << i << " " << i+1 << ")";
mukesh agrawal8f3f7752012-02-17 19:42:09 -08001203 EXPECT_GE(current_mapped, Service::kStrengthMin);
1204 EXPECT_LE(current_mapped, Service::kStrengthMax);
mukesh agrawale1d90e92012-02-15 17:36:08 -08001205 }
1206}
1207
1208TEST_F(WiFiServiceUpdateFromEndpointsTest, Strengths) {
1209 // If the chosen signal values don't map to distinct strength
1210 // values, then we can't expect our other tests to pass. So verify
1211 // their distinctness.
1212 EXPECT_TRUE(kOkEndpointStrength != kBadEndpointStrength);
1213 EXPECT_TRUE(kOkEndpointStrength != kGoodEndpointStrength);
1214 EXPECT_TRUE(kGoodEndpointStrength != kBadEndpointStrength);
1215}
1216
1217TEST_F(WiFiServiceUpdateFromEndpointsTest, Floating) {
1218 // Initial endpoint updates values.
1219 EXPECT_CALL(adaptor, EmitUint16Changed(
1220 flimflam::kWifiFrequency, kOkEndpointFrequency));
mukesh agrawal923f14f2012-06-04 16:46:08 -07001221 EXPECT_CALL(adaptor, EmitStringChanged(
1222 flimflam::kWifiBSsid, kOkEndpointBssId));
1223 EXPECT_CALL(adaptor, EmitUint8Changed(
mukesh agrawale1d90e92012-02-15 17:36:08 -08001224 flimflam::kSignalStrengthProperty, kOkEndpointStrength));
1225 service->AddEndpoint(ok_endpoint);
Darin Petkov4a66cc52012-06-15 10:08:29 +02001226 EXPECT_EQ(1, service->GetEndpointCount());
mukesh agrawale1d90e92012-02-15 17:36:08 -08001227 Mock::VerifyAndClearExpectations(&adaptor);
1228
1229 // Endpoint with stronger signal updates values.
1230 EXPECT_CALL(adaptor, EmitUint16Changed(
1231 flimflam::kWifiFrequency, kGoodEndpointFrequency));
mukesh agrawal923f14f2012-06-04 16:46:08 -07001232 EXPECT_CALL(adaptor, EmitStringChanged(
1233 flimflam::kWifiBSsid, kGoodEndpointBssId));
mukesh agrawale1d90e92012-02-15 17:36:08 -08001234 EXPECT_CALL(adaptor, EmitUint8Changed(
1235 flimflam::kSignalStrengthProperty, kGoodEndpointStrength));
1236 service->AddEndpoint(good_endpoint);
Darin Petkov4a66cc52012-06-15 10:08:29 +02001237 EXPECT_EQ(2, service->GetEndpointCount());
mukesh agrawale1d90e92012-02-15 17:36:08 -08001238 Mock::VerifyAndClearExpectations(&adaptor);
1239
1240 // Endpoint with lower signal does not change values.
1241 EXPECT_CALL(adaptor, EmitUint16Changed(flimflam::kWifiFrequency, _)).Times(0);
mukesh agrawal923f14f2012-06-04 16:46:08 -07001242 EXPECT_CALL(adaptor, EmitStringChanged(flimflam::kWifiBSsid, _)).Times(0);
mukesh agrawale1d90e92012-02-15 17:36:08 -08001243 EXPECT_CALL(adaptor,
1244 EmitUint8Changed(flimflam::kSignalStrengthProperty, _)).Times(0);
1245 service->AddEndpoint(bad_endpoint);
Darin Petkov4a66cc52012-06-15 10:08:29 +02001246 EXPECT_EQ(3, service->GetEndpointCount());
mukesh agrawale1d90e92012-02-15 17:36:08 -08001247 Mock::VerifyAndClearExpectations(&adaptor);
1248
1249 // Removing non-optimal endpoint does not change values.
1250 EXPECT_CALL(adaptor, EmitUint16Changed(flimflam::kWifiFrequency, _)).Times(0);
mukesh agrawal923f14f2012-06-04 16:46:08 -07001251 EXPECT_CALL(adaptor, EmitStringChanged(flimflam::kWifiBSsid, _)).Times(0);
mukesh agrawale1d90e92012-02-15 17:36:08 -08001252 EXPECT_CALL(adaptor,
1253 EmitUint8Changed(flimflam::kSignalStrengthProperty, _)).Times(0);
1254 service->RemoveEndpoint(bad_endpoint);
Darin Petkov4a66cc52012-06-15 10:08:29 +02001255 EXPECT_EQ(2, service->GetEndpointCount());
mukesh agrawale1d90e92012-02-15 17:36:08 -08001256 Mock::VerifyAndClearExpectations(&adaptor);
1257
1258 // Removing optimal endpoint updates values.
1259 EXPECT_CALL(adaptor, EmitUint16Changed(
1260 flimflam::kWifiFrequency, kOkEndpointFrequency));
mukesh agrawal923f14f2012-06-04 16:46:08 -07001261 EXPECT_CALL(adaptor, EmitStringChanged(
1262 flimflam::kWifiBSsid, kOkEndpointBssId));
mukesh agrawale1d90e92012-02-15 17:36:08 -08001263 EXPECT_CALL(adaptor, EmitUint8Changed(
1264 flimflam::kSignalStrengthProperty, kOkEndpointStrength));
1265 service->RemoveEndpoint(good_endpoint);
Darin Petkov4a66cc52012-06-15 10:08:29 +02001266 EXPECT_EQ(1, service->GetEndpointCount());
mukesh agrawale1d90e92012-02-15 17:36:08 -08001267 Mock::VerifyAndClearExpectations(&adaptor);
1268
1269 // Removing last endpoint updates values (and doesn't crash).
1270 EXPECT_CALL(adaptor, EmitUint16Changed(flimflam::kWifiFrequency, _));
mukesh agrawal923f14f2012-06-04 16:46:08 -07001271 EXPECT_CALL(adaptor, EmitStringChanged(flimflam::kWifiBSsid, _));
mukesh agrawale1d90e92012-02-15 17:36:08 -08001272 EXPECT_CALL(adaptor, EmitUint8Changed(flimflam::kSignalStrengthProperty, _));
1273 service->RemoveEndpoint(ok_endpoint);
Darin Petkov4a66cc52012-06-15 10:08:29 +02001274 EXPECT_EQ(0, service->GetEndpointCount());
mukesh agrawale1d90e92012-02-15 17:36:08 -08001275 Mock::VerifyAndClearExpectations(&adaptor);
1276}
1277
1278TEST_F(WiFiServiceUpdateFromEndpointsTest, Connected) {
1279 EXPECT_CALL(adaptor, EmitUint16Changed(_, _)).Times(AnyNumber());
mukesh agrawal923f14f2012-06-04 16:46:08 -07001280 EXPECT_CALL(adaptor, EmitStringChanged(_, _)).Times(AnyNumber());
mukesh agrawale1d90e92012-02-15 17:36:08 -08001281 EXPECT_CALL(adaptor, EmitUint8Changed(_, _)).Times(AnyNumber());
1282 EXPECT_CALL(adaptor, EmitBoolChanged(_, _)).Times(AnyNumber());
1283 service->AddEndpoint(bad_endpoint);
1284 service->AddEndpoint(ok_endpoint);
Darin Petkov4a66cc52012-06-15 10:08:29 +02001285 EXPECT_EQ(2, service->GetEndpointCount());
mukesh agrawale1d90e92012-02-15 17:36:08 -08001286 Mock::VerifyAndClearExpectations(&adaptor);
1287
1288 // Setting current endpoint forces adoption of its values, even if it
1289 // doesn't have the highest signal.
1290 EXPECT_CALL(adaptor, EmitUint16Changed(
1291 flimflam::kWifiFrequency, kBadEndpointFrequency));
mukesh agrawal923f14f2012-06-04 16:46:08 -07001292 EXPECT_CALL(adaptor, EmitStringChanged(
1293 flimflam::kWifiBSsid, kBadEndpointBssId));
mukesh agrawale1d90e92012-02-15 17:36:08 -08001294 EXPECT_CALL(adaptor, EmitUint8Changed(
1295 flimflam::kSignalStrengthProperty, kBadEndpointStrength));
1296 service->NotifyCurrentEndpoint(bad_endpoint);
1297 Mock::VerifyAndClearExpectations(&adaptor);
1298
1299 // Adding a better endpoint doesn't matter, when current endpoint is set.
1300 EXPECT_CALL(adaptor, EmitUint16Changed(flimflam::kWifiFrequency, _)).Times(0);
mukesh agrawal923f14f2012-06-04 16:46:08 -07001301 EXPECT_CALL(adaptor, EmitStringChanged(flimflam::kWifiBSsid, _)).Times(0);
mukesh agrawale1d90e92012-02-15 17:36:08 -08001302 EXPECT_CALL(adaptor,
1303 EmitUint8Changed(flimflam::kSignalStrengthProperty, _)).Times(0);
1304 service->AddEndpoint(good_endpoint);
Darin Petkov4a66cc52012-06-15 10:08:29 +02001305 EXPECT_EQ(3, service->GetEndpointCount());
mukesh agrawale1d90e92012-02-15 17:36:08 -08001306 Mock::VerifyAndClearExpectations(&adaptor);
1307
1308 // Removing a better endpoint doesn't matter, when current endpoint is set.
1309 EXPECT_CALL(adaptor, EmitUint16Changed(flimflam::kWifiFrequency, _)).Times(0);
mukesh agrawal923f14f2012-06-04 16:46:08 -07001310 EXPECT_CALL(adaptor, EmitStringChanged(flimflam::kWifiBSsid, _)).Times(0);
mukesh agrawale1d90e92012-02-15 17:36:08 -08001311 EXPECT_CALL(adaptor,
1312 EmitUint8Changed(flimflam::kSignalStrengthProperty, _)).Times(0);
1313 service->RemoveEndpoint(good_endpoint);
1314 Mock::VerifyAndClearExpectations(&adaptor);
1315
1316 // Removing the current endpoint is safe and sane.
1317 EXPECT_CALL(adaptor, EmitUint16Changed(
1318 flimflam::kWifiFrequency, kOkEndpointFrequency));
mukesh agrawal923f14f2012-06-04 16:46:08 -07001319 EXPECT_CALL(adaptor, EmitStringChanged(
1320 flimflam::kWifiBSsid, kOkEndpointBssId));
mukesh agrawale1d90e92012-02-15 17:36:08 -08001321 EXPECT_CALL(adaptor, EmitUint8Changed(
1322 flimflam::kSignalStrengthProperty, kOkEndpointStrength));
1323 service->RemoveEndpoint(bad_endpoint);
1324 Mock::VerifyAndClearExpectations(&adaptor);
1325
1326 // Clearing the current endpoint (without removing it) is also safe and sane.
1327 service->NotifyCurrentEndpoint(ok_endpoint);
1328 EXPECT_CALL(adaptor, EmitUint16Changed(flimflam::kWifiFrequency, _)).Times(0);
mukesh agrawal923f14f2012-06-04 16:46:08 -07001329 EXPECT_CALL(adaptor, EmitStringChanged(flimflam::kWifiBSsid, _)).Times(0);
mukesh agrawale1d90e92012-02-15 17:36:08 -08001330 EXPECT_CALL(adaptor,
1331 EmitUint8Changed(flimflam::kSignalStrengthProperty, _)).Times(0);
1332 service->NotifyCurrentEndpoint(NULL);
1333 Mock::VerifyAndClearExpectations(&adaptor);
1334}
1335
1336TEST_F(WiFiServiceUpdateFromEndpointsTest, EndpointModified) {
1337 EXPECT_CALL(adaptor, EmitUint16Changed(_, _)).Times(AnyNumber());
mukesh agrawal923f14f2012-06-04 16:46:08 -07001338 EXPECT_CALL(adaptor, EmitStringChanged(_, _)).Times(AnyNumber());
mukesh agrawale1d90e92012-02-15 17:36:08 -08001339 EXPECT_CALL(adaptor, EmitUint8Changed(_, _)).Times(AnyNumber());
1340 EXPECT_CALL(adaptor, EmitBoolChanged(_, _)).Times(AnyNumber());
1341 service->AddEndpoint(ok_endpoint);
1342 service->AddEndpoint(good_endpoint);
Darin Petkov4a66cc52012-06-15 10:08:29 +02001343 EXPECT_EQ(2, service->GetEndpointCount());
mukesh agrawale1d90e92012-02-15 17:36:08 -08001344 Mock::VerifyAndClearExpectations(&adaptor);
1345
1346 // Updating sub-optimal Endpoint doesn't update Service.
1347 EXPECT_CALL(adaptor, EmitUint16Changed(flimflam::kWifiFrequency, _)).Times(0);
mukesh agrawal923f14f2012-06-04 16:46:08 -07001348 EXPECT_CALL(adaptor, EmitStringChanged(flimflam::kWifiBSsid, _)).Times(0);
mukesh agrawale1d90e92012-02-15 17:36:08 -08001349 EXPECT_CALL(adaptor,
1350 EmitUint8Changed(flimflam::kSignalStrengthProperty, _)).Times(0);
1351 ok_endpoint->signal_strength_ = (kOkEndpointSignal + kGoodEndpointSignal) / 2;
1352 service->NotifyEndpointUpdated(*ok_endpoint);
1353 Mock::VerifyAndClearExpectations(&adaptor);
1354
1355 // Updating optimal Endpoint updates appropriate Service property.
1356 EXPECT_CALL(adaptor, EmitUint16Changed(flimflam::kWifiFrequency, _)).Times(0);
mukesh agrawal923f14f2012-06-04 16:46:08 -07001357 EXPECT_CALL(adaptor, EmitStringChanged(flimflam::kWifiBSsid, _)).Times(0);
mukesh agrawale1d90e92012-02-15 17:36:08 -08001358 EXPECT_CALL(adaptor, EmitUint8Changed(flimflam::kSignalStrengthProperty, _));
1359 good_endpoint->signal_strength_ = kGoodEndpointSignal + 1;
1360 service->NotifyEndpointUpdated(*good_endpoint);
1361 Mock::VerifyAndClearExpectations(&adaptor);
1362
1363 // Change in optimal Endpoint updates Service properties.
1364 EXPECT_CALL(adaptor, EmitUint16Changed(
1365 flimflam::kWifiFrequency, kOkEndpointFrequency));
mukesh agrawal923f14f2012-06-04 16:46:08 -07001366 EXPECT_CALL(adaptor, EmitStringChanged(
1367 flimflam::kWifiBSsid, kOkEndpointBssId));
mukesh agrawale1d90e92012-02-15 17:36:08 -08001368 EXPECT_CALL(adaptor, EmitUint8Changed(flimflam::kSignalStrengthProperty, _));
1369 ok_endpoint->signal_strength_ = kGoodEndpointSignal + 2;
1370 service->NotifyEndpointUpdated(*ok_endpoint);
1371 Mock::VerifyAndClearExpectations(&adaptor);
1372}
1373
Paul Stewarta5e7d5f2013-01-09 18:06:15 -08001374TEST_F(WiFiServiceUpdateFromEndpointsTest, Ieee80211w) {
1375 EXPECT_CALL(adaptor, EmitUint16Changed(_, _)).Times(AnyNumber());
1376 EXPECT_CALL(adaptor, EmitStringChanged(_, _)).Times(AnyNumber());
1377 EXPECT_CALL(adaptor, EmitUint8Changed(_, _)).Times(AnyNumber());
1378 EXPECT_CALL(adaptor, EmitBoolChanged(_, _)).Times(AnyNumber());
1379 service->AddEndpoint(ok_endpoint);
1380 EXPECT_FALSE(service->ieee80211w_required());
1381 good_endpoint->ieee80211w_required_ = true;
1382 service->AddEndpoint(good_endpoint);
1383 EXPECT_TRUE(service->ieee80211w_required());
1384 service->RemoveEndpoint(good_endpoint);
1385 EXPECT_TRUE(service->ieee80211w_required());
1386}
1387
Chris Masone34af2182011-08-22 11:59:36 -07001388} // namespace shill