blob: 8ff8f2611d7cde711e414dca3d17d431cac3322c [file] [log] [blame]
Paul Stewart21f2aae2013-01-17 17:10:08 -08001// Copyright (c) 2013 The Chromium OS Authors. All rights reserved.
2// 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_provider.h"
6
Paul Stewart3c504012013-01-17 17:49:58 -08007#include <base/string_number_conversions.h>
8#include <base/string_util.h>
9#include <base/stringprintf.h>
10#include <chromeos/dbus/service_constants.h>
Paul Stewart21f2aae2013-01-17 17:10:08 -080011#include <gtest/gtest.h>
12
Paul Stewart3c504012013-01-17 17:49:58 -080013#include <set>
14#include <string>
15#include <vector>
16
17#include "shill/ieee80211.h"
18#include "shill/mock_event_dispatcher.h"
Paul Stewart21f2aae2013-01-17 17:10:08 -080019#include "shill/mock_manager.h"
20#include "shill/mock_metrics.h"
Paul Stewart3c504012013-01-17 17:49:58 -080021#include "shill/mock_profile.h"
22#include "shill/mock_store.h"
23#include "shill/mock_wifi_service.h"
Paul Stewart21f2aae2013-01-17 17:10:08 -080024#include "shill/nice_mock_control.h"
Paul Stewart3c504012013-01-17 17:49:58 -080025#include "shill/technology.h"
26#include "shill/wifi_endpoint.h"
27#include "shill/wpa_supplicant.h"
28
29using std::set;
30using std::string;
31using std::vector;
32using ::testing::_;
Wade Guthrie60a37062013-04-02 11:39:09 -070033using ::testing::ContainerEq;
Paul Stewart3c504012013-01-17 17:49:58 -080034using ::testing::Mock;
35using ::testing::NiceMock;
36using ::testing::Return;
37using ::testing::SetArgumentPointee;
38using ::testing::StrictMock;
Paul Stewart21f2aae2013-01-17 17:10:08 -080039
40namespace shill {
41
42class WiFiProviderTest : public testing::Test {
43 public:
44 WiFiProviderTest()
Thieu Le6c1e3bb2013-02-06 15:20:35 -080045 : metrics_(NULL),
Paul Stewart3c504012013-01-17 17:49:58 -080046 manager_(&control_, &dispatcher_, &metrics_,
47 reinterpret_cast<GLib *>(NULL)),
48 provider_(&control_, &dispatcher_, &metrics_, &manager_),
Thieu Le5133b712013-02-19 14:47:21 -080049 profile_(
50 new NiceMock<MockProfile>(&control_, &metrics_, &manager_, "")),
Paul Stewart3c504012013-01-17 17:49:58 -080051 storage_entry_index_(0) {}
Paul Stewart21f2aae2013-01-17 17:10:08 -080052
53 virtual ~WiFiProviderTest() {}
54
Paul Stewart3c504012013-01-17 17:49:58 -080055 virtual void SetUp() {
56 EXPECT_CALL(*profile_, GetConstStorage()).WillRepeatedly(Return(&storage_));
57 }
58
Paul Stewart21f2aae2013-01-17 17:10:08 -080059 protected:
Paul Stewart3c504012013-01-17 17:49:58 -080060 typedef scoped_refptr<MockWiFiService> MockWiFiServiceRefPtr;
61
62 void CreateServicesFromProfile() {
63 provider_.CreateServicesFromProfile(profile_);
64 }
65
Wade Guthrie60a37062013-04-02 11:39:09 -070066 void LoadAndFixupServiceEntries(bool is_default_profile) {
67 provider_.LoadAndFixupServiceEntries(&storage_, is_default_profile);
Paul Stewart3c504012013-01-17 17:49:58 -080068 }
69
70 const vector<WiFiServiceRefPtr> GetServices() {
71 return provider_.services_;
72 }
73
Paul Stewart0427cc12013-03-25 13:50:39 -070074 const WiFiProvider::EndpointServiceMap &GetServiceByEndpoint() {
75 return provider_.service_by_endpoint_;
76 }
77
Paul Stewart6c351ff2013-02-25 15:13:03 -080078 bool GetRunning() {
79 return provider_.running_;
80 }
81
Paul Stewart3c504012013-01-17 17:49:58 -080082 void AddStringParameterToStorage(const string &id,
83 const string &key,
84 const string &value) {
85 EXPECT_CALL(storage_, GetString(id, key, _))
86 .WillRepeatedly(DoAll(SetArgumentPointee<2>(value),
87 Return(true)));
88 }
89
90 string AddServiceToStorage(const char *ssid,
91 const char *mode,
92 const char *security,
93 bool is_hidden,
94 bool provide_hidden) {
95 string id = StringToLowerASCII(base::StringPrintf("entry_%d",
96 storage_entry_index_));
97 EXPECT_CALL(storage_, GetString(id, _, _)).WillRepeatedly(Return(false));
98 if (ssid) {
99 const string ssid_string(ssid);
100 const string hex_ssid(
101 base::HexEncode(ssid_string.data(), ssid_string.size()));
102 AddStringParameterToStorage(id, WiFiService::kStorageSSID, hex_ssid);
103 }
104 if (mode) {
105 AddStringParameterToStorage(id, WiFiService::kStorageMode, mode);
106 }
107 if (security) {
108 AddStringParameterToStorage(id, WiFiService::kStorageSecurity, security);
109 }
110 if (provide_hidden) {
111 EXPECT_CALL(storage_, GetBool(id, flimflam::kWifiHiddenSsid, _))
112 .WillRepeatedly(
113 DoAll(SetArgumentPointee<2>(is_hidden), Return(true)));
114 } else {
115 EXPECT_CALL(storage_, GetBool(id, flimflam::kWifiHiddenSsid, _))
116 .WillRepeatedly(Return(false));
117 }
118 storage_entry_index_++;
119 return id;
120 }
121
Paul Stewartd2e1c362013-03-03 19:06:07 -0800122 void SetServiceParameters(const char *ssid,
123 const char *mode,
124 const char *security,
125 bool is_hidden,
126 bool provide_hidden,
127 KeyValueStore *args) {
128 args->SetString(flimflam::kTypeProperty, flimflam::kTypeWifi);
129 if (ssid) {
130 args->SetString(flimflam::kSSIDProperty, ssid);
131 }
132 if (mode) {
133 args->SetString(flimflam::kModeProperty, mode);
134 }
135 if (security) {
136 args->SetString(flimflam::kSecurityProperty, security);
137 }
138 if (provide_hidden) {
139 args->SetBool(flimflam::kWifiHiddenSsid, is_hidden);
140 }
141 }
142
143 WiFiServiceRefPtr CreateTemporaryService(const char *ssid,
144 const char *mode,
145 const char *security,
146 bool is_hidden,
147 bool provide_hidden,
148 Error *error) {
149 KeyValueStore args;
150 SetServiceParameters(
151 ssid, mode, security, is_hidden, provide_hidden, &args);
152 return provider_.CreateTemporaryService(args, error);
153 }
154
Paul Stewart3c504012013-01-17 17:49:58 -0800155 WiFiServiceRefPtr GetService(const char *ssid,
156 const char *mode,
157 const char *security,
158 bool is_hidden,
159 bool provide_hidden,
160 Error *error) {
161 KeyValueStore args;
Paul Stewartd2e1c362013-03-03 19:06:07 -0800162 SetServiceParameters(
163 ssid, mode, security, is_hidden, provide_hidden, &args);
Paul Stewart3c504012013-01-17 17:49:58 -0800164 return provider_.GetService(args, error);
165 }
166
167 WiFiServiceRefPtr FindService(const vector<uint8_t> &ssid,
168 const string &mode,
169 const string &security) {
170 return provider_.FindService(ssid, mode, security);
171 }
172 WiFiEndpointRefPtr MakeEndpoint(const string &ssid, const string &bssid,
173 uint16 frequency, int16 signal_dbm) {
174 return WiFiEndpoint::MakeOpenEndpoint(
Paul Stewart0654ece2013-03-26 15:21:26 -0700175 NULL, NULL, ssid, bssid, WPASupplicant::kNetworkModeInfrastructure,
Paul Stewart3c504012013-01-17 17:49:58 -0800176 frequency, signal_dbm);
177 }
178 MockWiFiServiceRefPtr AddMockService(const vector<uint8_t> &ssid,
179 const string &mode,
180 const string &security,
181 bool hidden_ssid) {
182 MockWiFiServiceRefPtr service = new MockWiFiService(
183 &control_,
184 &dispatcher_,
185 &metrics_,
186 &manager_,
187 &provider_,
188 ssid,
189 mode,
190 security,
191 hidden_ssid);
192 provider_.services_.push_back(service);
193 return service;
194 }
Paul Stewart0427cc12013-03-25 13:50:39 -0700195 void AddEndpointToService(WiFiServiceRefPtr service,
196 const WiFiEndpointConstRefPtr &endpoint) {
197 provider_.service_by_endpoint_[endpoint] = service;
198 }
Wade Guthrie60a37062013-04-02 11:39:09 -0700199
200 void BuildFreqCountStrings(vector<string> *strings) {
201 // NOTE: These strings match the frequencies in |BuildFreqCountMap|. They
202 // are also provided, here, in sorted order to match the frequency map
203 // (iterators for which will provide them in frequency-sorted order).
204 static const char *kStrings[] = {
205 "5180:14", "5240:16", "5745:7", "5765:4", "5785:14", "5805:5"
206 };
207 if (!strings) {
208 LOG(ERROR) << "NULL |strings|.";
209 return;
210 }
211 for (size_t i = 0; i < arraysize(kStrings); ++i) {
212 (*strings).push_back(kStrings[i]);
213 }
214 }
215
216 void BuildFreqCountMap(WiFiProvider::ConnectFrequencyMap *frequencies) {
217 // NOTE: These structures match the strings in |BuildFreqCountStrings|.
218 static const struct FreqCount {
219 uint16 freq;
220 int64 count;
221 } kConnectFreq[] = {
222 {5180, 14},
223 {5240, 16},
224 {5745, 7},
225 {5765, 4},
226 {5785, 14},
227 {5805, 5}
228 };
229 if (!frequencies) {
230 LOG(ERROR) << "NULL |frequencies|.";
231 return;
232 }
233 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(kConnectFreq); ++i) {
234 (*frequencies)[kConnectFreq[i].freq] = kConnectFreq[i].count;
235 }
236 }
237
Paul Stewart21f2aae2013-01-17 17:10:08 -0800238 NiceMockControl control_;
Paul Stewart3c504012013-01-17 17:49:58 -0800239 MockEventDispatcher dispatcher_;
Paul Stewart21f2aae2013-01-17 17:10:08 -0800240 MockMetrics metrics_;
Paul Stewart3c504012013-01-17 17:49:58 -0800241 StrictMock<MockManager> manager_;
Paul Stewart21f2aae2013-01-17 17:10:08 -0800242 WiFiProvider provider_;
Paul Stewart3c504012013-01-17 17:49:58 -0800243 scoped_refptr<MockProfile> profile_;
244 StrictMock<MockStore> storage_;
245 int storage_entry_index_;
Paul Stewart21f2aae2013-01-17 17:10:08 -0800246};
247
Paul Stewart3c504012013-01-17 17:49:58 -0800248MATCHER(TypeWiFiPropertyMatch, "") {
249 return
250 arg.bool_properties().empty() &&
251 arg.int_properties().empty() &&
252 arg.uint_properties().empty() &&
253 arg.string_properties().size() == 1 &&
254 arg.LookupString(flimflam::kTypeProperty, "") == flimflam::kTypeWifi;
255}
256
257MATCHER_P(RefPtrMatch, ref, "") {
258 return ref.get() == arg.get();
259}
260
261TEST_F(WiFiProviderTest, Start) {
262 // Doesn't do anything really. Just testing for no crash.
263 EXPECT_TRUE(GetServices().empty());
Paul Stewart6c351ff2013-02-25 15:13:03 -0800264 EXPECT_FALSE(GetRunning());
Paul Stewart3c504012013-01-17 17:49:58 -0800265 provider_.Start();
266 EXPECT_TRUE(GetServices().empty());
Paul Stewart6c351ff2013-02-25 15:13:03 -0800267 EXPECT_TRUE(GetRunning());
Paul Stewart0427cc12013-03-25 13:50:39 -0700268 EXPECT_TRUE(GetServiceByEndpoint().empty());
Paul Stewart3c504012013-01-17 17:49:58 -0800269}
270
271TEST_F(WiFiProviderTest, Stop) {
272 MockWiFiServiceRefPtr service0 = AddMockService(vector<uint8_t>(1, '0'),
273 flimflam::kModeManaged,
274 flimflam::kSecurityNone,
275 false);
276 MockWiFiServiceRefPtr service1 = AddMockService(vector<uint8_t>(1, '1'),
277 flimflam::kModeManaged,
278 flimflam::kSecurityNone,
279 false);
Paul Stewart0427cc12013-03-25 13:50:39 -0700280 WiFiEndpointRefPtr endpoint = MakeEndpoint("", "00:00:00:00:00:00", 0, 0);
281 AddEndpointToService(service0, endpoint);
282
Paul Stewart3c504012013-01-17 17:49:58 -0800283 EXPECT_EQ(2, GetServices().size());
Paul Stewart0427cc12013-03-25 13:50:39 -0700284 EXPECT_FALSE(GetServiceByEndpoint().empty());
Paul Stewart3c504012013-01-17 17:49:58 -0800285 EXPECT_CALL(*service0, ResetWiFi()).Times(1);
286 EXPECT_CALL(*service1, ResetWiFi()).Times(1);
287 EXPECT_CALL(manager_, DeregisterService(RefPtrMatch(service0))).Times(1);
288 EXPECT_CALL(manager_, DeregisterService(RefPtrMatch(service1))).Times(1);
289 provider_.Stop();
290 // Verify now, so it's clear that this happened as a result of the call
291 // above, and not anything in the destructor(s).
292 Mock::VerifyAndClearExpectations(service0);
293 Mock::VerifyAndClearExpectations(service1);
294 Mock::VerifyAndClearExpectations(&manager_);
295 EXPECT_TRUE(GetServices().empty());
Paul Stewart0427cc12013-03-25 13:50:39 -0700296 EXPECT_TRUE(GetServiceByEndpoint().empty());
Paul Stewart3c504012013-01-17 17:49:58 -0800297}
298
299TEST_F(WiFiProviderTest, CreateServicesFromProfileWithNoGroups) {
300 EXPECT_CALL(storage_, GetGroupsWithProperties(TypeWiFiPropertyMatch()))
301 .WillOnce(Return(set<string>()));
302 CreateServicesFromProfile();
303 EXPECT_TRUE(GetServices().empty());
304}
305
306TEST_F(WiFiProviderTest, CreateServicesFromProfileMissingSSID) {
307 string id;
308 StrictMock<MockStore> storage;
309 set<string> groups;
310 groups.insert(AddServiceToStorage(
311 NULL, flimflam::kModeManaged, flimflam::kSecurityNone, false, true));
312 EXPECT_CALL(storage_, GetGroupsWithProperties(TypeWiFiPropertyMatch()))
313 .WillRepeatedly(Return(groups));
314 CreateServicesFromProfile();
315 EXPECT_TRUE(GetServices().empty());
316}
317
318TEST_F(WiFiProviderTest, CreateServicesFromProfileEmptySSID) {
319 string id;
320 StrictMock<MockStore> storage;
321 set<string> groups;
322 groups.insert(AddServiceToStorage(
323 "", flimflam::kModeManaged, flimflam::kSecurityNone, false, true));
324 EXPECT_CALL(storage_, GetGroupsWithProperties(TypeWiFiPropertyMatch()))
325 .WillRepeatedly(Return(groups));
326 CreateServicesFromProfile();
327 EXPECT_TRUE(GetServices().empty());
328}
329
330TEST_F(WiFiProviderTest, CreateServicesFromProfileMissingMode) {
331 string id;
332 StrictMock<MockStore> storage;
333 set<string> groups;
334 groups.insert(AddServiceToStorage(
335 "foo", NULL, flimflam::kSecurityNone, false, true));
336 EXPECT_CALL(storage_, GetGroupsWithProperties(TypeWiFiPropertyMatch()))
337 .WillRepeatedly(Return(groups));
338 CreateServicesFromProfile();
339 EXPECT_TRUE(GetServices().empty());
340}
341
342TEST_F(WiFiProviderTest, CreateServicesFromProfileEmptyMode) {
343 string id;
344 StrictMock<MockStore> storage;
345 set<string> groups;
346 groups.insert(AddServiceToStorage(
347 "foo", "", flimflam::kSecurityNone, false, true));
348 EXPECT_CALL(storage_, GetGroupsWithProperties(TypeWiFiPropertyMatch()))
349 .WillRepeatedly(Return(groups));
350 CreateServicesFromProfile();
351 EXPECT_TRUE(GetServices().empty());
352}
353
354TEST_F(WiFiProviderTest, CreateServicesFromProfileMissingSecurity) {
355 string id;
356 StrictMock<MockStore> storage;
357 set<string> groups;
358 groups.insert(AddServiceToStorage(
359 "foo", flimflam::kModeManaged, NULL, false, true));
360 EXPECT_CALL(storage_, GetGroupsWithProperties(TypeWiFiPropertyMatch()))
361 .WillRepeatedly(Return(groups));
362 CreateServicesFromProfile();
363 EXPECT_TRUE(GetServices().empty());
364}
365
366TEST_F(WiFiProviderTest, CreateServicesFromProfileEmptySecurity) {
367 string id;
368 StrictMock<MockStore> storage;
369 set<string> groups;
370 groups.insert(AddServiceToStorage(
371 "foo", flimflam::kModeManaged, "", false, true));
372 EXPECT_CALL(storage_, GetGroupsWithProperties(TypeWiFiPropertyMatch()))
373 .WillRepeatedly(Return(groups));
374 CreateServicesFromProfile();
375 EXPECT_TRUE(GetServices().empty());
376}
377
378TEST_F(WiFiProviderTest, CreateServicesFromProfileMissingHidden) {
379 string id;
380 StrictMock<MockStore> storage;
381 set<string> groups;
382 groups.insert(AddServiceToStorage(
383 "foo", flimflam::kModeManaged, flimflam::kSecurityNone, false, false));
384 EXPECT_CALL(storage_, GetGroupsWithProperties(TypeWiFiPropertyMatch()))
385 .WillRepeatedly(Return(groups));
386 CreateServicesFromProfile();
387 EXPECT_TRUE(GetServices().empty());
388}
389
390TEST_F(WiFiProviderTest, CreateServicesFromProfileSingle) {
391 string id;
392 StrictMock<MockStore> storage;
393 set<string> groups;
394 string kSSID("foo");
395 groups.insert(AddServiceToStorage(
396 kSSID.c_str(), flimflam::kModeManaged, flimflam::kSecurityNone,
397 false, true));
398 EXPECT_CALL(storage_, GetGroupsWithProperties(TypeWiFiPropertyMatch()))
399 .WillRepeatedly(Return(groups));
400 EXPECT_CALL(manager_, RegisterService(_)).Times(1);
401 CreateServicesFromProfile();
402 Mock::VerifyAndClearExpectations(&manager_);
403 EXPECT_EQ(1, GetServices().size());
404
405 const WiFiServiceRefPtr service = GetServices().front();
406 const string service_ssid(service->ssid().begin(), service->ssid().end());
407 EXPECT_EQ(kSSID, service_ssid);
408 EXPECT_EQ(flimflam::kModeManaged, service->mode());
409 EXPECT_TRUE(service->IsSecurityMatch(flimflam::kSecurityNone));
410
411 EXPECT_CALL(manager_, RegisterService(_)).Times(0);
412 CreateServicesFromProfile();
413 EXPECT_EQ(1, GetServices().size());
414}
415
416TEST_F(WiFiProviderTest, CreateServicesFromProfileHiddenButConnected) {
417 string id;
418 StrictMock<MockStore> storage;
419 set<string> groups;
420 string kSSID("foo");
421 groups.insert(AddServiceToStorage(
422 kSSID.c_str(), flimflam::kModeManaged, flimflam::kSecurityNone,
423 true, true));
424 EXPECT_CALL(storage_, GetGroupsWithProperties(TypeWiFiPropertyMatch()))
425 .WillRepeatedly(Return(groups));
426 EXPECT_CALL(manager_, RegisterService(_)).Times(1);
427 EXPECT_CALL(manager_, IsTechnologyConnected(Technology::kWifi))
428 .WillOnce(Return(true));
Wade Guthrie68d41092013-04-02 12:56:02 -0700429 EXPECT_CALL(manager_, RequestScan(_, _, _)).Times(0);
Paul Stewart3c504012013-01-17 17:49:58 -0800430 CreateServicesFromProfile();
431 Mock::VerifyAndClearExpectations(&manager_);
432
433 EXPECT_CALL(manager_, RegisterService(_)).Times(0);
434 EXPECT_CALL(manager_, IsTechnologyConnected(_)).Times(0);
435 CreateServicesFromProfile();
436}
437
438TEST_F(WiFiProviderTest, CreateServicesFromProfileHiddenNotConnected) {
439 string id;
440 StrictMock<MockStore> storage;
441 set<string> groups;
442 string kSSID("foo");
443 groups.insert(AddServiceToStorage(
444 kSSID.c_str(), flimflam::kModeManaged, flimflam::kSecurityNone,
445 true, true));
446 EXPECT_CALL(storage_, GetGroupsWithProperties(TypeWiFiPropertyMatch()))
447 .WillRepeatedly(Return(groups));
448 EXPECT_CALL(manager_, RegisterService(_)).Times(1);
449 EXPECT_CALL(manager_, IsTechnologyConnected(Technology::kWifi))
450 .WillOnce(Return(false));
Wade Guthrie68d41092013-04-02 12:56:02 -0700451 EXPECT_CALL(manager_, RequestScan(Device::kProgressiveScan,
452 flimflam::kTypeWifi, _)).Times(1);
Paul Stewart3c504012013-01-17 17:49:58 -0800453 CreateServicesFromProfile();
454 Mock::VerifyAndClearExpectations(&manager_);
455
456 EXPECT_CALL(manager_, RegisterService(_)).Times(0);
457 EXPECT_CALL(manager_, IsTechnologyConnected(_)).Times(0);
Wade Guthrie68d41092013-04-02 12:56:02 -0700458 EXPECT_CALL(manager_, RequestScan(_, _, _)).Times(0);
Paul Stewart3c504012013-01-17 17:49:58 -0800459 CreateServicesFromProfile();
460}
461
462TEST_F(WiFiProviderTest, CreateTwoServices) {
463 string id;
464 StrictMock<MockStore> storage;
465 set<string> groups;
466 groups.insert(AddServiceToStorage(
467 "foo", flimflam::kModeManaged, flimflam::kSecurityNone, false, true));
468 groups.insert(AddServiceToStorage(
469 "bar", flimflam::kModeManaged, flimflam::kSecurityNone, true, true));
470 EXPECT_CALL(storage_, GetGroupsWithProperties(TypeWiFiPropertyMatch()))
471 .WillRepeatedly(Return(groups));
472 EXPECT_CALL(manager_, RegisterService(_)).Times(2);
473 EXPECT_CALL(manager_, IsTechnologyConnected(Technology::kWifi))
474 .WillOnce(Return(true));
Wade Guthrie68d41092013-04-02 12:56:02 -0700475 EXPECT_CALL(manager_, RequestScan(_, flimflam::kTypeWifi, _)).Times(0);
Paul Stewart3c504012013-01-17 17:49:58 -0800476 CreateServicesFromProfile();
477 Mock::VerifyAndClearExpectations(&manager_);
478
479 EXPECT_EQ(2, GetServices().size());
480}
481
Paul Stewart4fe4ec22013-02-22 07:47:29 -0800482TEST_F(WiFiProviderTest, GetServiceEmptyMode) {
Paul Stewart3c504012013-01-17 17:49:58 -0800483 Error error;
484 EXPECT_FALSE(GetService("foo", "", flimflam::kSecurityNone,
485 false, false, &error).get());
486 EXPECT_EQ(Error::kNotSupported, error.type());
487}
488
Paul Stewart4fe4ec22013-02-22 07:47:29 -0800489TEST_F(WiFiProviderTest, GetServiceNoMode) {
490 Error error;
491 EXPECT_CALL(manager_, RegisterService(_)).Times(1);
492 EXPECT_TRUE(GetService("foo", NULL, flimflam::kSecurityNone,
493 false, false, &error).get());
494 EXPECT_TRUE(error.IsSuccess());
495}
496
Paul Stewart3c504012013-01-17 17:49:58 -0800497TEST_F(WiFiProviderTest, GetServiceBadMode) {
498 Error error;
Paul Stewartd2e1c362013-03-03 19:06:07 -0800499 EXPECT_FALSE(GetService("foo", "BogoMesh",
Paul Stewart3c504012013-01-17 17:49:58 -0800500 flimflam::kSecurityNone,
501 false, false, &error).get());
502 EXPECT_EQ(Error::kNotSupported, error.type());
503 EXPECT_EQ("service mode is unsupported", error.message());
504}
505
506TEST_F(WiFiProviderTest, GetServiceNoSSID) {
507 Error error;
508 EXPECT_FALSE(GetService(NULL, flimflam::kModeManaged,
509 flimflam::kSecurityNone, false, false,
510 &error).get());
511 EXPECT_EQ(Error::kInvalidArguments, error.type());
512 EXPECT_EQ("must specify SSID", error.message());
513}
514
515TEST_F(WiFiProviderTest, GetServiceEmptySSID) {
516 Error error;
517 EXPECT_FALSE(GetService("", flimflam::kModeManaged,
518 flimflam::kSecurityNone, false, false,
519 &error).get());
520 EXPECT_EQ(Error::kInvalidNetworkName, error.type());
521 EXPECT_EQ("SSID is too short", error.message());
522}
523
524TEST_F(WiFiProviderTest, GetServiceLongSSID) {
525 Error error;
526 string ssid(IEEE_80211::kMaxSSIDLen + 1, '0');
527 EXPECT_FALSE(GetService(ssid.c_str(), flimflam::kModeManaged,
528 flimflam::kSecurityNone, false, false,
529 &error).get());
530 EXPECT_EQ(Error::kInvalidNetworkName, error.type());
531 EXPECT_EQ("SSID is too long", error.message());
532}
533
534TEST_F(WiFiProviderTest, GetServiceJustLongEnoughSSID) {
535 Error error;
536 string ssid(IEEE_80211::kMaxSSIDLen, '0');
537 EXPECT_CALL(manager_, RegisterService(_)).Times(1);
538 EXPECT_TRUE(GetService(ssid.c_str(), flimflam::kModeManaged,
539 flimflam::kSecurityNone, false, false,
540 &error));
541 EXPECT_TRUE(error.IsSuccess());
542}
543
544TEST_F(WiFiProviderTest, GetServiceBadSecurty) {
545 Error error;
546 EXPECT_FALSE(GetService("foo", flimflam::kModeManaged,
547 "pig-80211", false, false,
548 &error));
549 EXPECT_EQ(Error::kNotSupported, error.type());
550 EXPECT_EQ("security mode is unsupported", error.message());
551}
552
553TEST_F(WiFiProviderTest, GetServiceMinimal) {
554 Error error;
555 const string kSSID("foo");
556 EXPECT_CALL(manager_, RegisterService(_)).Times(1);
557 WiFiServiceRefPtr service = GetService(kSSID.c_str(), flimflam::kModeManaged,
558 NULL, false, false, &error);
559 EXPECT_TRUE(service.get());
560 EXPECT_TRUE(error.IsSuccess());
561 const string service_ssid(service->ssid().begin(), service->ssid().end());
562 EXPECT_EQ(kSSID, service_ssid);
563 EXPECT_EQ(flimflam::kModeManaged, service->mode());
564
565 // These two should be set to their default values if not specified.
566 EXPECT_TRUE(service->IsSecurityMatch(flimflam::kSecurityNone));
567 EXPECT_TRUE(service->hidden_ssid());
568}
569
570TEST_F(WiFiProviderTest, GetServiceFullySpecified) {
571 EXPECT_CALL(manager_, RegisterService(_)).Times(1);
572 const string kSSID("bar");
573 Error error;
574 WiFiServiceRefPtr service0 =
575 GetService(kSSID.c_str(), flimflam::kModeManaged,
576 flimflam::kSecurityPsk, false, true, &error);
577 Mock::VerifyAndClearExpectations(&manager_);
578 EXPECT_TRUE(error.IsSuccess());
579 const string service_ssid(service0->ssid().begin(), service0->ssid().end());
580 EXPECT_EQ(kSSID, service_ssid);
581 EXPECT_EQ(flimflam::kModeManaged, service0->mode());
582 EXPECT_TRUE(service0->IsSecurityMatch(flimflam::kSecurityPsk));
583 EXPECT_FALSE(service0->hidden_ssid());
584
585 // Getting the same service parameters (even with a different hidden
586 // parameter) should return the same service.
587 EXPECT_CALL(manager_, RegisterService(_)).Times(0);
588 WiFiServiceRefPtr service1 =
589 GetService(kSSID.c_str(), flimflam::kModeManaged,
590 flimflam::kSecurityPsk, true, true, &error);
591 Mock::VerifyAndClearExpectations(&manager_);
592 EXPECT_EQ(service0.get(), service1.get());
593 EXPECT_EQ(1, GetServices().size());
594
595 // Getting the same ssid with different other parameters should return
596 // a different service.
597 EXPECT_CALL(manager_, RegisterService(_)).Times(1);
598 WiFiServiceRefPtr service2 =
599 GetService(kSSID.c_str(), flimflam::kModeManaged,
600 flimflam::kSecurityNone, true, true, &error);
601 Mock::VerifyAndClearExpectations(&manager_);
602 EXPECT_NE(service0.get(), service2.get());
603 EXPECT_EQ(2, GetServices().size());
604}
605
Paul Stewartd2e1c362013-03-03 19:06:07 -0800606TEST_F(WiFiProviderTest, FindSimilarService) {
607 // Since CreateTemporyService uses exactly the same validation as
608 // GetService, don't bother with testing invalid parameters.
609 const string kSSID("foo");
610 KeyValueStore args;
611 SetServiceParameters(
612 kSSID.c_str(), flimflam::kModeManaged, flimflam::kSecurityNone,
613 true, true, &args);
614 EXPECT_CALL(manager_, RegisterService(_)).Times(1);
615 Error get_service_error;
616 WiFiServiceRefPtr service = provider_.GetService(args, &get_service_error);
617 EXPECT_EQ(1, GetServices().size());
618
619 {
620 Error error;
621 WiFiServiceRefPtr find_service = provider_.FindSimilarService(args, &error);
622 EXPECT_EQ(service.get(), find_service.get());
623 EXPECT_TRUE(error.IsSuccess());
624 }
625
626 args.SetBool(flimflam::kWifiHiddenSsid, false);
627
628 {
629 Error error;
630 WiFiServiceRefPtr find_service = provider_.FindSimilarService(args, &error);
631 EXPECT_EQ(service.get(), find_service.get());
632 EXPECT_TRUE(error.IsSuccess());
633 }
634
635 args.SetString(flimflam::kSecurityProperty, flimflam::kSecurityWpa);
636
637 {
638 Error error;
639 WiFiServiceRefPtr find_service = provider_.FindSimilarService(args, &error);
640 EXPECT_EQ(NULL, find_service.get());
641 EXPECT_EQ(Error::kNotFound, error.type());
642 }
643}
644
645TEST_F(WiFiProviderTest, CreateTemporaryService) {
646 // Since CreateTemporyService uses exactly the same validation as
647 // GetService, don't bother with testing invalid parameters.
648 const string kSSID("foo");
649 EXPECT_CALL(manager_, RegisterService(_)).Times(1);
650 Error error;
651 WiFiServiceRefPtr service0 =
652 GetService(kSSID.c_str(), flimflam::kModeManaged,
653 flimflam::kSecurityNone, true, true, &error);
654 EXPECT_EQ(1, GetServices().size());
655 Mock::VerifyAndClearExpectations(&manager_);
656
657 EXPECT_CALL(manager_, RegisterService(_)).Times(0);
658 WiFiServiceRefPtr service1 =
659 CreateTemporaryService(kSSID.c_str(), flimflam::kModeManaged,
660 flimflam::kSecurityNone, true, true, &error);
661
662 // Test that a new service was created, but not registered with the
663 // manager or added to the provider's service list.
664 EXPECT_EQ(1, GetServices().size());
665 EXPECT_TRUE(service0 != service1);
666 EXPECT_TRUE(service1->HasOneRef());
667}
668
Paul Stewart3c504012013-01-17 17:49:58 -0800669TEST_F(WiFiProviderTest, FindServiceWPA) {
670 const string kSSID("an_ssid");
671 Error error;
672 EXPECT_CALL(manager_, RegisterService(_)).Times(1);
673 WiFiServiceRefPtr service = GetService(
674 kSSID.c_str(), flimflam::kModeManaged, flimflam::kSecurityRsn,
675 false, true, &error);
676 ASSERT_TRUE(service);
677 const vector<uint8_t> ssid_bytes(kSSID.begin(), kSSID.end());
678 WiFiServiceRefPtr wpa_service(FindService(ssid_bytes, flimflam::kModeManaged,
679 flimflam::kSecurityWpa));
680 EXPECT_TRUE(wpa_service);
681 EXPECT_EQ(service.get(), wpa_service.get());
682 WiFiServiceRefPtr rsn_service(FindService(ssid_bytes, flimflam::kModeManaged,
683 flimflam::kSecurityRsn));
684 EXPECT_TRUE(rsn_service.get());
685 EXPECT_EQ(service.get(), rsn_service.get());
686 WiFiServiceRefPtr psk_service(FindService(ssid_bytes, flimflam::kModeManaged,
687 flimflam::kSecurityPsk));
688 EXPECT_EQ(service.get(), psk_service.get());
689 WiFiServiceRefPtr wep_service(FindService(ssid_bytes, flimflam::kModeManaged,
690 flimflam::kSecurityWep));
691 EXPECT_TRUE(service.get() != wep_service.get());
692 EXPECT_EQ(NULL, wep_service.get());
693}
694
695TEST_F(WiFiProviderTest, FindServiceForEndpoint) {
696 EXPECT_CALL(manager_, RegisterService(_)).Times(1);
697 Error error;
698 const string kSSID("an_ssid");
699 WiFiServiceRefPtr service = GetService(
700 kSSID.c_str(), flimflam::kModeManaged, flimflam::kSecurityNone,
701 false, true, &error);
702 ASSERT_TRUE(service);
703 WiFiEndpointRefPtr endpoint = MakeEndpoint(kSSID, "00:00:00:00:00:00", 0, 0);
704 WiFiServiceRefPtr endpoint_service =
705 provider_.FindServiceForEndpoint(endpoint);
Paul Stewart0427cc12013-03-25 13:50:39 -0700706 // Just because a matching service exists, we shouldn't necessarily have
707 // it returned. We will test that this function returns the correct
708 // service if the endpoint is added below.
709 EXPECT_EQ(NULL, endpoint_service.get());
Paul Stewart3c504012013-01-17 17:49:58 -0800710}
711
712TEST_F(WiFiProviderTest, OnEndpointAdded) {
Paul Stewart6c351ff2013-02-25 15:13:03 -0800713 provider_.Start();
Paul Stewart3c504012013-01-17 17:49:58 -0800714 const string ssid0("an_ssid");
715 const vector<uint8_t> ssid0_bytes(ssid0.begin(), ssid0.end());
716 EXPECT_FALSE(FindService(ssid0_bytes, flimflam::kModeManaged,
717 flimflam::kSecurityNone));
718 WiFiEndpointRefPtr endpoint0 = MakeEndpoint(ssid0, "00:00:00:00:00:00", 0, 0);
719 EXPECT_CALL(manager_, RegisterService(_)).Times(1);
720 EXPECT_CALL(manager_, UpdateService(_)).Times(1);
721 provider_.OnEndpointAdded(endpoint0);
722 Mock::VerifyAndClearExpectations(&manager_);
723 EXPECT_EQ(1, GetServices().size());
724 WiFiServiceRefPtr service0(FindService(ssid0_bytes, flimflam::kModeManaged,
725 flimflam::kSecurityNone));
726 EXPECT_TRUE(service0);
727 EXPECT_TRUE(service0->HasEndpoints());
Paul Stewart0427cc12013-03-25 13:50:39 -0700728 EXPECT_EQ(1, GetServiceByEndpoint().size());
729 WiFiServiceRefPtr endpoint_service =
730 provider_.FindServiceForEndpoint(endpoint0);
731 EXPECT_EQ(service0.get(), endpoint_service.get());
Paul Stewart3c504012013-01-17 17:49:58 -0800732
733 WiFiEndpointRefPtr endpoint1 = MakeEndpoint(ssid0, "00:00:00:00:00:01", 0, 0);
734 EXPECT_CALL(manager_, RegisterService(_)).Times(0);
735 EXPECT_CALL(manager_, UpdateService(RefPtrMatch(service0))).Times(1);
736 provider_.OnEndpointAdded(endpoint1);
737 Mock::VerifyAndClearExpectations(&manager_);
738 EXPECT_EQ(1, GetServices().size());
739
740 const string ssid1("another_ssid");
741 const vector<uint8_t> ssid1_bytes(ssid1.begin(), ssid1.end());
742 EXPECT_FALSE(FindService(ssid1_bytes, flimflam::kModeManaged,
743 flimflam::kSecurityNone));
744 WiFiEndpointRefPtr endpoint2 = MakeEndpoint(ssid1, "00:00:00:00:00:02",
745 0, 0);
746 EXPECT_CALL(manager_, RegisterService(_)).Times(1);
747 EXPECT_CALL(manager_, UpdateService(_)).Times(1);
748 provider_.OnEndpointAdded(endpoint2);
749 Mock::VerifyAndClearExpectations(&manager_);
750 EXPECT_EQ(2, GetServices().size());
751
752 WiFiServiceRefPtr service1(FindService(ssid1_bytes, flimflam::kModeManaged,
753 flimflam::kSecurityNone));
754 EXPECT_TRUE(service1);
755 EXPECT_TRUE(service1->HasEndpoints());
756 EXPECT_TRUE(service1 != service0);
757}
758
Paul Stewart08a54eb2013-03-11 12:07:36 -0700759TEST_F(WiFiProviderTest, OnEndpointAddedWithSecurity) {
760 provider_.Start();
761 const string ssid0("an_ssid");
762 const vector<uint8_t> ssid0_bytes(ssid0.begin(), ssid0.end());
763 EXPECT_FALSE(FindService(ssid0_bytes, flimflam::kModeManaged,
764 flimflam::kSecurityNone));
765 WiFiEndpointRefPtr endpoint0 = MakeEndpoint(ssid0, "00:00:00:00:00:00", 0, 0);
766 endpoint0->set_security_mode(flimflam::kSecurityRsn);
767 EXPECT_CALL(manager_, RegisterService(_)).Times(1);
768 EXPECT_CALL(manager_, UpdateService(_)).Times(1);
769 provider_.OnEndpointAdded(endpoint0);
770 Mock::VerifyAndClearExpectations(&manager_);
771 EXPECT_EQ(1, GetServices().size());
772 WiFiServiceRefPtr service0(FindService(ssid0_bytes, flimflam::kModeManaged,
773 flimflam::kSecurityWpa));
774 EXPECT_TRUE(service0);
775 EXPECT_TRUE(service0->HasEndpoints());
776 EXPECT_EQ(flimflam::kSecurityPsk, service0->security_);
777
778 WiFiEndpointRefPtr endpoint1 = MakeEndpoint(ssid0, "00:00:00:00:00:01", 0, 0);
779 endpoint1->set_security_mode(flimflam::kSecurityWpa);
780 EXPECT_CALL(manager_, RegisterService(_)).Times(0);
781 EXPECT_CALL(manager_, UpdateService(RefPtrMatch(service0))).Times(1);
782 provider_.OnEndpointAdded(endpoint1);
783 Mock::VerifyAndClearExpectations(&manager_);
784 EXPECT_EQ(1, GetServices().size());
785
786 const string ssid1("another_ssid");
787 const vector<uint8_t> ssid1_bytes(ssid1.begin(), ssid1.end());
788 EXPECT_FALSE(FindService(ssid1_bytes, flimflam::kModeManaged,
789 flimflam::kSecurityNone));
790 WiFiEndpointRefPtr endpoint2 = MakeEndpoint(ssid1, "00:00:00:00:00:02", 0, 0);
791 endpoint2->set_security_mode(flimflam::kSecurityWpa);
792 EXPECT_CALL(manager_, RegisterService(_)).Times(1);
793 EXPECT_CALL(manager_, UpdateService(_)).Times(1);
794 provider_.OnEndpointAdded(endpoint2);
795 Mock::VerifyAndClearExpectations(&manager_);
796 EXPECT_EQ(2, GetServices().size());
797
798 WiFiServiceRefPtr service1(FindService(ssid1_bytes, flimflam::kModeManaged,
799 flimflam::kSecurityRsn));
800 EXPECT_TRUE(service1);
801 EXPECT_TRUE(service1->HasEndpoints());
802 EXPECT_EQ(flimflam::kSecurityPsk, service1->security_);
803 EXPECT_TRUE(service1 != service0);
804}
805
Paul Stewart6c351ff2013-02-25 15:13:03 -0800806TEST_F(WiFiProviderTest, OnEndpointAddedWhileStopped) {
807 // If we don't call provider_.Start(), OnEndpointAdded should have no effect.
808 const string ssid("an_ssid");
809 WiFiEndpointRefPtr endpoint = MakeEndpoint(ssid, "00:00:00:00:00:00", 0, 0);
810 EXPECT_CALL(manager_, RegisterService(_)).Times(0);
811 EXPECT_CALL(manager_, UpdateService(_)).Times(0);
812 provider_.OnEndpointAdded(endpoint);
813 EXPECT_TRUE(GetServices().empty());
814}
815
Paul Stewart3c504012013-01-17 17:49:58 -0800816TEST_F(WiFiProviderTest, OnEndpointAddedToMockService) {
817 // The previous test allowed the provider to create its own "real"
818 // WiFiServices, which hides some of what we can test with mock
819 // services. Re-do an add-endpoint operation by seeding the provider
820 // with a mock service.
Paul Stewart6c351ff2013-02-25 15:13:03 -0800821 provider_.Start();
Paul Stewart3c504012013-01-17 17:49:58 -0800822 const string ssid0("an_ssid");
823 const vector<uint8_t> ssid0_bytes(ssid0.begin(), ssid0.end());
824 MockWiFiServiceRefPtr service0 = AddMockService(ssid0_bytes,
825 flimflam::kModeManaged,
826 flimflam::kSecurityNone,
827 false);
828 const string ssid1("another_ssid");
829 const vector<uint8_t> ssid1_bytes(ssid1.begin(), ssid1.end());
830 MockWiFiServiceRefPtr service1 = AddMockService(ssid1_bytes,
831 flimflam::kModeManaged,
832 flimflam::kSecurityNone,
833 false);
834 EXPECT_EQ(service0.get(), FindService(ssid0_bytes,
835 flimflam::kModeManaged,
836 flimflam::kSecurityNone).get());
837 WiFiEndpointRefPtr endpoint0 = MakeEndpoint(ssid0, "00:00:00:00:00:00", 0, 0);
838 EXPECT_CALL(manager_, RegisterService(_)).Times(0);
839 EXPECT_CALL(manager_, UpdateService(RefPtrMatch(service0))).Times(1);
840 EXPECT_CALL(*service0, AddEndpoint(RefPtrMatch(endpoint0))).Times(1);
841 EXPECT_CALL(*service1, AddEndpoint(_)).Times(0);
842 provider_.OnEndpointAdded(endpoint0);
843 Mock::VerifyAndClearExpectations(&manager_);
844 Mock::VerifyAndClearExpectations(service0);
845 Mock::VerifyAndClearExpectations(service1);
846
847 WiFiEndpointRefPtr endpoint1 = MakeEndpoint(ssid0, "00:00:00:00:00:01", 0, 0);
848 EXPECT_CALL(manager_, RegisterService(_)).Times(0);
849 EXPECT_CALL(manager_, UpdateService(RefPtrMatch(service0))).Times(1);
850 EXPECT_CALL(*service0, AddEndpoint(RefPtrMatch(endpoint1))).Times(1);
851 EXPECT_CALL(*service1, AddEndpoint(_)).Times(0);
852 provider_.OnEndpointAdded(endpoint1);
853 Mock::VerifyAndClearExpectations(&manager_);
854 Mock::VerifyAndClearExpectations(service0);
855 Mock::VerifyAndClearExpectations(service1);
856
857 WiFiEndpointRefPtr endpoint2 = MakeEndpoint(ssid1, "00:00:00:00:00:02", 0, 0);
858 EXPECT_CALL(manager_, RegisterService(_)).Times(0);
859 EXPECT_CALL(manager_, UpdateService(RefPtrMatch(service1))).Times(1);
860 EXPECT_CALL(*service0, AddEndpoint(_)).Times(0);
861 EXPECT_CALL(*service1, AddEndpoint(RefPtrMatch(endpoint2))).Times(1);
862 provider_.OnEndpointAdded(endpoint2);
863}
864
865TEST_F(WiFiProviderTest, OnEndpointRemoved) {
Paul Stewart6c351ff2013-02-25 15:13:03 -0800866 provider_.Start();
Paul Stewart3c504012013-01-17 17:49:58 -0800867 const string ssid0("an_ssid");
868 const vector<uint8_t> ssid0_bytes(ssid0.begin(), ssid0.end());
869 MockWiFiServiceRefPtr service0 = AddMockService(ssid0_bytes,
870 flimflam::kModeManaged,
871 flimflam::kSecurityNone,
872 false);
873 const string ssid1("another_ssid");
874 const vector<uint8_t> ssid1_bytes(ssid1.begin(), ssid1.end());
875 MockWiFiServiceRefPtr service1 = AddMockService(ssid1_bytes,
876 flimflam::kModeManaged,
877 flimflam::kSecurityNone,
878 false);
879 EXPECT_EQ(2, GetServices().size());
880
881 // Remove the last endpoint of a non-remembered service.
882 WiFiEndpointRefPtr endpoint0 = MakeEndpoint(ssid0, "00:00:00:00:00:00", 0, 0);
Paul Stewart0427cc12013-03-25 13:50:39 -0700883 AddEndpointToService(service0, endpoint0);
884 EXPECT_EQ(1, GetServiceByEndpoint().size());
885
Paul Stewart3c504012013-01-17 17:49:58 -0800886 EXPECT_CALL(*service0, RemoveEndpoint(RefPtrMatch(endpoint0))).Times(1);
887 EXPECT_CALL(*service1, RemoveEndpoint(_)).Times(0);
888 EXPECT_CALL(*service0, HasEndpoints()).WillOnce(Return(false));
889 EXPECT_CALL(*service0, IsRemembered()).WillOnce(Return(false));
890 EXPECT_CALL(*service0, ResetWiFi()).Times(1);
891 EXPECT_CALL(manager_, UpdateService(RefPtrMatch(service0))).Times(0);
892 EXPECT_CALL(manager_, DeregisterService(RefPtrMatch(service0))).Times(1);
893 provider_.OnEndpointRemoved(endpoint0);
894 // Verify now, so it's clear that this happened as a result of the call
895 // above, and not anything in the destructor(s).
896 Mock::VerifyAndClearExpectations(&manager_);
897 Mock::VerifyAndClearExpectations(service0);
898 Mock::VerifyAndClearExpectations(service1);
899 EXPECT_EQ(1, GetServices().size());
900 EXPECT_EQ(service1.get(), GetServices().front().get());
Paul Stewart0427cc12013-03-25 13:50:39 -0700901 EXPECT_TRUE(GetServiceByEndpoint().empty());
Paul Stewart3c504012013-01-17 17:49:58 -0800902}
903
904TEST_F(WiFiProviderTest, OnEndpointRemovedButHasEndpoints) {
Paul Stewart6c351ff2013-02-25 15:13:03 -0800905 provider_.Start();
Paul Stewart3c504012013-01-17 17:49:58 -0800906 const string ssid0("an_ssid");
907 const vector<uint8_t> ssid0_bytes(ssid0.begin(), ssid0.end());
908 MockWiFiServiceRefPtr service0 = AddMockService(ssid0_bytes,
909 flimflam::kModeManaged,
910 flimflam::kSecurityNone,
911 false);
912 EXPECT_EQ(1, GetServices().size());
913
914 // Remove an endpoint of a non-remembered service.
915 WiFiEndpointRefPtr endpoint0 = MakeEndpoint(ssid0, "00:00:00:00:00:00", 0, 0);
Paul Stewart0427cc12013-03-25 13:50:39 -0700916 AddEndpointToService(service0, endpoint0);
917 EXPECT_EQ(1, GetServiceByEndpoint().size());
918
Paul Stewart3c504012013-01-17 17:49:58 -0800919 EXPECT_CALL(*service0, RemoveEndpoint(RefPtrMatch(endpoint0))).Times(1);
920 EXPECT_CALL(*service0, HasEndpoints()).WillOnce(Return(true));
921 EXPECT_CALL(*service0, IsRemembered()).WillRepeatedly(Return(false));
922 EXPECT_CALL(manager_, UpdateService(RefPtrMatch(service0))).Times(1);
923 EXPECT_CALL(*service0, ResetWiFi()).Times(0);
924 EXPECT_CALL(manager_, DeregisterService(_)).Times(0);
925 provider_.OnEndpointRemoved(endpoint0);
926 // Verify now, so it's clear that this happened as a result of the call
927 // above, and not anything in the destructor(s).
928 Mock::VerifyAndClearExpectations(&manager_);
929 Mock::VerifyAndClearExpectations(service0);
930 EXPECT_EQ(1, GetServices().size());
Paul Stewart0427cc12013-03-25 13:50:39 -0700931 EXPECT_TRUE(GetServiceByEndpoint().empty());
Paul Stewart3c504012013-01-17 17:49:58 -0800932}
933
934TEST_F(WiFiProviderTest, OnEndpointRemovedButIsRemembered) {
Paul Stewart6c351ff2013-02-25 15:13:03 -0800935 provider_.Start();
Paul Stewart3c504012013-01-17 17:49:58 -0800936 const string ssid0("an_ssid");
937 const vector<uint8_t> ssid0_bytes(ssid0.begin(), ssid0.end());
938 MockWiFiServiceRefPtr service0 = AddMockService(ssid0_bytes,
939 flimflam::kModeManaged,
940 flimflam::kSecurityNone,
941 false);
942 EXPECT_EQ(1, GetServices().size());
943
944 // Remove the last endpoint of a remembered service.
945 WiFiEndpointRefPtr endpoint0 = MakeEndpoint(ssid0, "00:00:00:00:00:00", 0, 0);
Paul Stewart0427cc12013-03-25 13:50:39 -0700946 AddEndpointToService(service0, endpoint0);
947 EXPECT_EQ(1, GetServiceByEndpoint().size());
948
Paul Stewart3c504012013-01-17 17:49:58 -0800949 EXPECT_CALL(*service0, RemoveEndpoint(RefPtrMatch(endpoint0))).Times(1);
950 EXPECT_CALL(*service0, HasEndpoints()).WillRepeatedly(Return(false));
951 EXPECT_CALL(*service0, IsRemembered()).WillOnce(Return(true));
952 EXPECT_CALL(manager_, UpdateService(RefPtrMatch(service0))).Times(1);
953 EXPECT_CALL(*service0, ResetWiFi()).Times(0);
954 EXPECT_CALL(manager_, DeregisterService(_)).Times(0);
955 provider_.OnEndpointRemoved(endpoint0);
956 // Verify now, so it's clear that this happened as a result of the call
957 // above, and not anything in the destructor(s).
958 Mock::VerifyAndClearExpectations(&manager_);
959 Mock::VerifyAndClearExpectations(service0);
960 EXPECT_EQ(1, GetServices().size());
Paul Stewart0427cc12013-03-25 13:50:39 -0700961 EXPECT_TRUE(GetServiceByEndpoint().empty());
Paul Stewart3c504012013-01-17 17:49:58 -0800962}
963
Paul Stewart6c351ff2013-02-25 15:13:03 -0800964TEST_F(WiFiProviderTest, OnEndpointRemovedWhileStopped) {
965 // If we don't call provider_.Start(), OnEndpointRemoved should not
966 // cause a crash even if a service matching the endpoint does not exist.
967 const string ssid("an_ssid");
968 WiFiEndpointRefPtr endpoint = MakeEndpoint(ssid, "00:00:00:00:00:00", 0, 0);
969 provider_.OnEndpointRemoved(endpoint);
970}
971
Paul Stewart3c504012013-01-17 17:49:58 -0800972TEST_F(WiFiProviderTest, OnServiceUnloaded) {
973 // This function should never unregister services itself -- the Manager
974 // will automatically deregister the service if OnServiceUnloaded()
975 // returns true (via WiFiService::Unload()).
976 EXPECT_CALL(manager_, DeregisterService(_)).Times(0);
977
978 MockWiFiServiceRefPtr service = AddMockService(vector<uint8_t>(1, '0'),
Paul Stewart6c351ff2013-02-25 15:13:03 -0800979 flimflam::kModeManaged,
980 flimflam::kSecurityNone,
981 false);
Paul Stewart3c504012013-01-17 17:49:58 -0800982 EXPECT_EQ(1, GetServices().size());
983 EXPECT_CALL(*service, HasEndpoints()).WillOnce(Return(true));
984 EXPECT_CALL(*service, ResetWiFi()).Times(0);
985 EXPECT_FALSE(provider_.OnServiceUnloaded(service));
986 EXPECT_EQ(1, GetServices().size());
987 Mock::VerifyAndClearExpectations(service);
988
989 EXPECT_CALL(*service, HasEndpoints()).WillOnce(Return(false));
990 EXPECT_CALL(*service, ResetWiFi()).Times(1);
991 EXPECT_TRUE(provider_.OnServiceUnloaded(service));
992 // Verify now, so it's clear that this happened as a result of the call
993 // above, and not anything in the destructor(s).
994 Mock::VerifyAndClearExpectations(service);
995 EXPECT_TRUE(GetServices().empty());
996
997 Mock::VerifyAndClearExpectations(&manager_);
998}
999
Wade Guthrie60a37062013-04-02 11:39:09 -07001000TEST_F(WiFiProviderTest, LoadAndFixupServiceEntries) {
1001 // We test LoadAndFixupServiceEntries indirectly since it calls a static
1002 // method in WiFiService.
Paul Stewart3c504012013-01-17 17:49:58 -08001003 EXPECT_CALL(metrics_, SendEnumToUMA(
1004 "Network.Shill.Wifi.ServiceFixupEntries",
1005 Metrics::kMetricServiceFixupDefaultProfile,
1006 Metrics::kMetricServiceFixupMax)).Times(1);
1007 EXPECT_CALL(storage_, Flush()).Times(1);
1008 const string kGroupId =
1009 StringPrintf("%s_0_0_%s_%s",
1010 flimflam::kTypeWifi,
1011 flimflam::kModeManaged,
1012 flimflam::kSecurityNone);
1013 EXPECT_CALL(storage_,
1014 GetString(kGroupId, _, _)).WillRepeatedly(Return(false));
1015 EXPECT_CALL(storage_,
1016 SetString(kGroupId, _, _)).WillRepeatedly(Return(true));
1017 set<string> groups;
1018 groups.insert(kGroupId);
1019 EXPECT_CALL(storage_, GetGroups()).WillRepeatedly(Return(groups));
Wade Guthrie60a37062013-04-02 11:39:09 -07001020 EXPECT_CALL(storage_, GetStringList(WiFiProvider::kStorageId,
1021 WiFiProvider::kStorageFrequencies, _))
1022 .WillOnce(Return(true));
1023 LoadAndFixupServiceEntries(true);
Paul Stewart3c504012013-01-17 17:49:58 -08001024 Mock::VerifyAndClearExpectations(&metrics_);
1025
1026 EXPECT_CALL(metrics_, SendEnumToUMA(
1027 "Network.Shill.Wifi.ServiceFixupEntries",
1028 Metrics::kMetricServiceFixupUserProfile,
1029 Metrics::kMetricServiceFixupMax)).Times(1);
1030 EXPECT_CALL(storage_, Flush()).Times(1);
Wade Guthrie60a37062013-04-02 11:39:09 -07001031 EXPECT_CALL(storage_, GetStringList(_, _, _)).Times(0);
1032 LoadAndFixupServiceEntries(false);
Paul Stewart3c504012013-01-17 17:49:58 -08001033}
1034
Wade Guthrie60a37062013-04-02 11:39:09 -07001035TEST_F(WiFiProviderTest, LoadAndFixupServiceEntriesNothingToDo) {
Paul Stewart3c504012013-01-17 17:49:58 -08001036 EXPECT_CALL(metrics_, SendEnumToUMA(_, _, _)).Times(0);
1037 EXPECT_CALL(storage_, Flush()).Times(0);
1038 const string kGroupId =
1039 StringPrintf("%s_0_0_%s_%s",
1040 flimflam::kTypeWifi,
1041 flimflam::kModeManaged,
1042 flimflam::kSecurityNone);
1043 EXPECT_CALL(storage_,
1044 GetString(kGroupId, _, _)).WillRepeatedly(Return(true));
1045 set<string> groups;
1046 groups.insert(kGroupId);
1047 EXPECT_CALL(storage_, GetGroups()).WillOnce(Return(groups));
Wade Guthrie60a37062013-04-02 11:39:09 -07001048 EXPECT_CALL(storage_, GetStringList(WiFiProvider::kStorageId,
1049 WiFiProvider::kStorageFrequencies, _))
1050 .WillOnce(Return(true));
1051 LoadAndFixupServiceEntries(true);
Paul Stewart3c504012013-01-17 17:49:58 -08001052}
1053
1054TEST_F(WiFiProviderTest, GetHiddenSSIDList) {
1055 EXPECT_TRUE(provider_.GetHiddenSSIDList().empty());
1056 const vector<uint8_t> ssid0(1, '0');
1057 AddMockService(ssid0, flimflam::kModeManaged,
1058 flimflam::kSecurityNone, false);
1059 EXPECT_TRUE(provider_.GetHiddenSSIDList().empty());
1060
1061 const vector<uint8_t> ssid1(1, '1');
1062 MockWiFiServiceRefPtr service1 = AddMockService(ssid1,
1063 flimflam::kModeManaged,
1064 flimflam::kSecurityNone,
1065 true);
1066 EXPECT_CALL(*service1, IsRemembered()).WillRepeatedly(Return(false));
1067 EXPECT_TRUE(provider_.GetHiddenSSIDList().empty());
1068
1069 const vector<uint8_t> ssid2(1, '2');
1070 MockWiFiServiceRefPtr service2 = AddMockService(ssid2,
1071 flimflam::kModeManaged,
1072 flimflam::kSecurityNone,
1073 true);
1074 EXPECT_CALL(*service2, IsRemembered()).WillRepeatedly(Return(true));
1075 ByteArrays ssid_list = provider_.GetHiddenSSIDList();
1076
1077 EXPECT_EQ(1, ssid_list.size());
1078 EXPECT_TRUE(ssid_list[0] == ssid2);
1079
1080 const vector<uint8_t> ssid3(1, '3');
1081 MockWiFiServiceRefPtr service3 = AddMockService(ssid3,
1082 flimflam::kModeManaged,
1083 flimflam::kSecurityNone,
1084 false);
1085 EXPECT_CALL(*service2, IsRemembered()).WillRepeatedly(Return(true));
1086
1087 ssid_list = provider_.GetHiddenSSIDList();
1088 EXPECT_EQ(1, ssid_list.size());
1089 EXPECT_TRUE(ssid_list[0] == ssid2);
1090
1091 const vector<uint8_t> ssid4(1, '4');
1092 MockWiFiServiceRefPtr service4 = AddMockService(ssid4,
1093 flimflam::kModeManaged,
1094 flimflam::kSecurityNone,
1095 true);
1096 EXPECT_CALL(*service4, IsRemembered()).WillRepeatedly(Return(true));
1097
1098 ssid_list = provider_.GetHiddenSSIDList();
1099 EXPECT_EQ(2, ssid_list.size());
1100 EXPECT_TRUE(ssid_list[0] == ssid2);
1101 EXPECT_TRUE(ssid_list[1] == ssid4);
Paul Stewart21f2aae2013-01-17 17:10:08 -08001102}
1103
Wade Guthrie60a37062013-04-02 11:39:09 -07001104TEST_F(WiFiProviderTest, StringListToFrequencyMap) {
1105 vector<string> strings;
1106 BuildFreqCountStrings(&strings);
1107 WiFiProvider::ConnectFrequencyMap frequencies_result;
1108 WiFiProvider::StringListToFrequencyMap(strings, &frequencies_result);
1109
1110 WiFiProvider::ConnectFrequencyMap frequencies_expect;
1111 BuildFreqCountMap(&frequencies_expect);
1112 EXPECT_THAT(frequencies_result, ContainerEq(frequencies_expect));
1113}
1114
1115TEST_F(WiFiProviderTest, FrequencyMapToStringList) {
1116 WiFiProvider::ConnectFrequencyMap frequencies;
1117 BuildFreqCountMap(&frequencies);
1118 vector<string> strings_result;
1119 WiFiProvider::FrequencyMapToStringList(frequencies, &strings_result);
1120
1121 vector<string> strings_expect;
1122 BuildFreqCountStrings(&strings_expect);
1123 EXPECT_THAT(strings_result, ContainerEq(strings_expect));
1124}
1125
Paul Stewart21f2aae2013-01-17 17:10:08 -08001126} // namespace shill