blob: b4afd837bdf7eb6a355b89b59231296c08c8fc8e [file] [log] [blame]
mukesh agrawal8a3188d2011-12-01 20:56:44 +00001// Copyright (c) 2012 The Chromium OS Authors. All rights reserved.
Chris Masone853b81b2011-06-24 14:11:41 -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.h"
6
mukesh agrawalf2f68a52011-09-01 12:15:48 -07007#include <linux/if.h>
mukesh agrawal5c4dd0b2011-09-14 13:53:14 -07008#include <linux/netlink.h> // Needs typedefs from sys/socket.h.
Wade Guthrie5a4e2ef2013-04-30 12:51:39 -07009#include <netinet/ether.h>
10#include <sys/socket.h>
mukesh agrawalf2f68a52011-09-01 12:15:48 -070011
Chris Masone853b81b2011-06-24 14:11:41 -070012#include <map>
13#include <string>
14#include <vector>
15
Darin Petkov60ceaf32012-10-18 10:36:01 +020016#include <base/file_util.h>
mukesh agrawal7ec71312011-11-10 02:08:26 +000017#include <base/memory/ref_counted.h>
mukesh agrawaldc42bb32011-07-28 10:40:26 -070018#include <base/memory/scoped_ptr.h>
mukesh agrawal31950242011-07-14 11:53:38 -070019#include <base/string_number_conversions.h>
Darin Petkov4a66cc52012-06-15 10:08:29 +020020#include <base/string_split.h>
mukesh agrawal31950242011-07-14 11:53:38 -070021#include <base/string_util.h>
Wade Guthrie5a4e2ef2013-04-30 12:51:39 -070022#include <base/stringprintf.h>
Chris Masone853b81b2011-06-24 14:11:41 -070023#include <chromeos/dbus/service_constants.h>
mukesh agrawal1830fa12011-09-26 14:31:40 -070024#include <dbus-c++/dbus.h>
Chris Masone7156c922011-08-23 20:36:21 -070025#include <gmock/gmock.h>
Chris Masone2ae797d2011-08-23 20:41:00 -070026#include <gtest/gtest.h>
Chris Masone853b81b2011-06-24 14:11:41 -070027
28#include "shill/dbus_adaptor.h"
Paul Stewart26b327e2011-10-19 11:38:09 -070029#include "shill/event_dispatcher.h"
Gaurav Shah6d2c72d2012-10-16 16:30:44 -070030#include "shill/geolocation_info.h"
mukesh agrawal8ede0522011-10-03 14:57:44 -070031#include "shill/ieee80211.h"
mukesh agrawal7a4e4002011-09-06 11:26:05 -070032#include "shill/key_value_store.h"
Christopher Wileyb691efd2012-08-09 13:51:51 -070033#include "shill/logging.h"
Chris Masone853b81b2011-06-24 14:11:41 -070034#include "shill/manager.h"
Darin Petkov2b8e44e2012-06-25 15:13:26 +020035#include "shill/mock_dbus_manager.h"
Chris Masone853b81b2011-06-24 14:11:41 -070036#include "shill/mock_device.h"
mukesh agrawalc01f3982012-01-24 13:48:39 -080037#include "shill/mock_device_info.h"
mukesh agrawalf2f68a52011-09-01 12:15:48 -070038#include "shill/mock_dhcp_config.h"
39#include "shill/mock_dhcp_provider.h"
Paul Stewarte369ece2012-05-22 09:11:03 -070040#include "shill/mock_event_dispatcher.h"
Paul Stewart3c508e12012-08-09 11:40:06 -070041#include "shill/mock_link_monitor.h"
mukesh agrawalcf24a242012-05-21 16:46:11 -070042#include "shill/mock_log.h"
Chris Masone2ae797d2011-08-23 20:41:00 -070043#include "shill/mock_manager.h"
Thieu Le3426c8f2012-01-11 17:35:11 -080044#include "shill/mock_metrics.h"
Wade Guthriebb9fca22013-04-10 17:21:42 -070045#include "shill/mock_netlink_manager.h"
Paul Stewart10ccbb32012-04-26 15:59:30 -070046#include "shill/mock_profile.h"
mukesh agrawal5c4dd0b2011-09-14 13:53:14 -070047#include "shill/mock_rtnl_handler.h"
Wade Guthrie5a4e2ef2013-04-30 12:51:39 -070048#include "shill/mock_scan_session.h"
Paul Stewarta41e38d2011-11-11 07:47:29 -080049#include "shill/mock_store.h"
mukesh agrawalb20776f2012-02-10 16:00:36 -080050#include "shill/mock_supplicant_bss_proxy.h"
Paul Stewart735eab52013-03-29 09:19:23 -070051#include "shill/mock_supplicant_eap_state_handler.h"
mukesh agrawal31950242011-07-14 11:53:38 -070052#include "shill/mock_supplicant_interface_proxy.h"
Paul Stewart835934a2012-12-06 19:27:09 -080053#include "shill/mock_supplicant_network_proxy.h"
mukesh agrawal31950242011-07-14 11:53:38 -070054#include "shill/mock_supplicant_process_proxy.h"
mukesh agrawal5c05b292012-03-07 10:12:52 -080055#include "shill/mock_time.h"
Paul Stewart3c504012013-01-17 17:49:58 -080056#include "shill/mock_wifi_provider.h"
mukesh agrawal7ec71312011-11-10 02:08:26 +000057#include "shill/mock_wifi_service.h"
Wade Guthrie5a4e2ef2013-04-30 12:51:39 -070058#include "shill/netlink_message_matchers.h"
mukesh agrawaldc42bb32011-07-28 10:40:26 -070059#include "shill/nice_mock_control.h"
Wade Guthrie5a4e2ef2013-04-30 12:51:39 -070060#include "shill/nl80211_message.h"
Chris Masone853b81b2011-06-24 14:11:41 -070061#include "shill/property_store_unittest.h"
mukesh agrawal31950242011-07-14 11:53:38 -070062#include "shill/proxy_factory.h"
Wade Guthrie5a4e2ef2013-04-30 12:51:39 -070063#include "shill/scan_session.h"
Paul Stewart85aea152013-01-22 09:31:56 -080064#include "shill/technology.h"
mukesh agrawal31950242011-07-14 11:53:38 -070065#include "shill/wifi_endpoint.h"
mukesh agrawal32399322011-09-01 10:53:43 -070066#include "shill/wifi_service.h"
Paul Stewartced6a0b2011-11-08 15:32:04 -080067#include "shill/wpa_supplicant.h"
Chris Masone853b81b2011-06-24 14:11:41 -070068
Gary Morainac1bdb42012-02-16 17:42:29 -080069
Albert Chaulk0e1cdea2013-02-27 15:32:55 -080070using base::FilePath;
Chris Masone853b81b2011-06-24 14:11:41 -070071using std::map;
72using std::string;
73using std::vector;
74using ::testing::_;
mukesh agrawal31950242011-07-14 11:53:38 -070075using ::testing::AnyNumber;
Paul Stewart3c504012013-01-17 17:49:58 -080076using ::testing::AtLeast;
mukesh agrawal31950242011-07-14 11:53:38 -070077using ::testing::DefaultValue;
Paul Stewarta41e38d2011-11-11 07:47:29 -080078using ::testing::DoAll;
mukesh agrawalcf24a242012-05-21 16:46:11 -070079using ::testing::EndsWith;
mukesh agrawal31950242011-07-14 11:53:38 -070080using ::testing::InSequence;
mukesh agrawalb20776f2012-02-10 16:00:36 -080081using ::testing::Invoke;
Paul Stewart549d44c2012-07-03 12:40:25 -070082using ::testing::InvokeWithoutArgs;
Wade Guthrie5a4e2ef2013-04-30 12:51:39 -070083using ::testing::MakeMatcher;
84using ::testing::Matcher;
85using ::testing::MatcherInterface;
86using ::testing::MatchResultListener;
mukesh agrawal7ec71312011-11-10 02:08:26 +000087using ::testing::Mock;
mukesh agrawaldc42bb32011-07-28 10:40:26 -070088using ::testing::NiceMock;
mukesh agrawald4dc0832013-03-25 14:38:26 -070089using ::testing::NotNull;
Chris Masone853b81b2011-06-24 14:11:41 -070090using ::testing::Return;
Prathmesh Prabhuba99b592013-04-17 15:13:14 -070091using ::testing::ReturnRef;
Gary Morainac1bdb42012-02-16 17:42:29 -080092using ::testing::SaveArg;
Paul Stewarta41e38d2011-11-11 07:47:29 -080093using ::testing::SetArgumentPointee;
94using ::testing::StrEq;
95using ::testing::StrictMock;
Chris Masone853b81b2011-06-24 14:11:41 -070096using ::testing::Test;
mukesh agrawal31950242011-07-14 11:53:38 -070097using ::testing::Throw;
mukesh agrawal8ede0522011-10-03 14:57:44 -070098using ::testing::Values;
Chris Masone853b81b2011-06-24 14:11:41 -070099
100namespace shill {
101
Wade Guthrie5a4e2ef2013-04-30 12:51:39 -0700102namespace {
103
104const uint16_t kNl80211FamilyId = 0x13;
105const uint16_t kRandomScanFrequency1 = 5600;
106const uint16_t kRandomScanFrequency2 = 5560;
107const uint16_t kRandomScanFrequency3 = 2422;
108
109} // namespace
110
mukesh agrawal31950242011-07-14 11:53:38 -0700111class WiFiPropertyTest : public PropertyStoreTest {
Chris Masone853b81b2011-06-24 14:11:41 -0700112 public:
mukesh agrawal31950242011-07-14 11:53:38 -0700113 WiFiPropertyTest()
Thieu Le3426c8f2012-01-11 17:35:11 -0800114 : device_(new WiFi(control_interface(),
Paul Stewart3c504012013-01-17 17:49:58 -0800115 NULL, NULL, manager(), "wifi", "", 0)) {
Chris Masone853b81b2011-06-24 14:11:41 -0700116 }
mukesh agrawal31950242011-07-14 11:53:38 -0700117 virtual ~WiFiPropertyTest() {}
Chris Masone853b81b2011-06-24 14:11:41 -0700118
119 protected:
mukesh agrawal8abd2f62012-01-30 14:56:14 -0800120 WiFiRefPtr device_;
Chris Masone853b81b2011-06-24 14:11:41 -0700121};
122
mukesh agrawal31950242011-07-14 11:53:38 -0700123TEST_F(WiFiPropertyTest, Contains) {
mukesh agrawalde29fa82011-09-16 16:16:36 -0700124 EXPECT_TRUE(device_->store().Contains(flimflam::kNameProperty));
125 EXPECT_FALSE(device_->store().Contains(""));
Chris Masone853b81b2011-06-24 14:11:41 -0700126}
127
mukesh agrawal6bb9e7c2012-01-30 14:57:54 -0800128TEST_F(WiFiPropertyTest, SetProperty) {
Chris Masonea8a2c252011-06-27 22:16:30 -0700129 {
130 ::DBus::Error error;
mukesh agrawal6bb9e7c2012-01-30 14:57:54 -0800131 EXPECT_TRUE(DBusAdaptor::SetProperty(
mukesh agrawalde29fa82011-09-16 16:16:36 -0700132 device_->mutable_store(),
Chris Masonea8a2c252011-06-27 22:16:30 -0700133 flimflam::kBgscanSignalThresholdProperty,
134 PropertyStoreTest::kInt32V,
135 &error));
136 }
137 {
138 ::DBus::Error error;
mukesh agrawal6bb9e7c2012-01-30 14:57:54 -0800139 EXPECT_TRUE(DBusAdaptor::SetProperty(device_->mutable_store(),
140 flimflam::kScanIntervalProperty,
141 PropertyStoreTest::kUint16V,
142 &error));
Chris Masonea8a2c252011-06-27 22:16:30 -0700143 }
Chris Masone853b81b2011-06-24 14:11:41 -0700144 // Ensure that an attempt to write a R/O property returns InvalidArgs error.
Chris Masonea8a2c252011-06-27 22:16:30 -0700145 {
146 ::DBus::Error error;
mukesh agrawal6bb9e7c2012-01-30 14:57:54 -0800147 EXPECT_FALSE(DBusAdaptor::SetProperty(device_->mutable_store(),
148 flimflam::kScanningProperty,
149 PropertyStoreTest::kBoolV,
150 &error));
mukesh agrawalbebf1b82013-04-23 15:06:33 -0700151 ASSERT_TRUE(error.is_set()); // name() may be invalid otherwise
Chris Masone9d779932011-08-25 16:33:41 -0700152 EXPECT_EQ(invalid_args(), error.name());
Chris Masonea8a2c252011-06-27 22:16:30 -0700153 }
Chris Masone853b81b2011-06-24 14:11:41 -0700154
mukesh agrawal4d0401c2012-01-06 16:05:31 -0800155 {
156 ::DBus::Error error;
mukesh agrawal6bb9e7c2012-01-30 14:57:54 -0800157 EXPECT_TRUE(DBusAdaptor::SetProperty(
mukesh agrawal4d0401c2012-01-06 16:05:31 -0800158 device_->mutable_store(),
159 flimflam::kBgscanMethodProperty,
160 DBusAdaptor::StringToVariant(
Paul Stewart0654ece2013-03-26 15:21:26 -0700161 WPASupplicant::kNetworkBgscanMethodSimple),
mukesh agrawal4d0401c2012-01-06 16:05:31 -0800162 &error));
163 }
164
165 {
166 ::DBus::Error error;
mukesh agrawal6bb9e7c2012-01-30 14:57:54 -0800167 EXPECT_FALSE(DBusAdaptor::SetProperty(
mukesh agrawal4d0401c2012-01-06 16:05:31 -0800168 device_->mutable_store(),
169 flimflam::kBgscanMethodProperty,
170 DBusAdaptor::StringToVariant("not a real scan method"),
171 &error));
172 }
173}
174
Darin Petkov4a66cc52012-06-15 10:08:29 +0200175TEST_F(WiFiPropertyTest, BgscanMethodProperty) {
Paul Stewart0654ece2013-03-26 15:21:26 -0700176 EXPECT_NE(WPASupplicant::kNetworkBgscanMethodLearn,
mukesh agrawal8abd2f62012-01-30 14:56:14 -0800177 WiFi::kDefaultBgscanMethod);
Darin Petkov4a66cc52012-06-15 10:08:29 +0200178 EXPECT_TRUE(device_->bgscan_method_.empty());
179
180 string method;
Paul Stewarte6e8e492013-01-17 11:00:50 -0800181 Error unused_error;
182 EXPECT_TRUE(device_->store().GetStringProperty(
183 flimflam::kBgscanMethodProperty, &method, &unused_error));
Darin Petkov4a66cc52012-06-15 10:08:29 +0200184 EXPECT_EQ(WiFi::kDefaultBgscanMethod, method);
Paul Stewart0654ece2013-03-26 15:21:26 -0700185 EXPECT_EQ(WPASupplicant::kNetworkBgscanMethodSimple, method);
mukesh agrawal8abd2f62012-01-30 14:56:14 -0800186
187 ::DBus::Error error;
mukesh agrawal6bb9e7c2012-01-30 14:57:54 -0800188 EXPECT_TRUE(DBusAdaptor::SetProperty(
mukesh agrawal8abd2f62012-01-30 14:56:14 -0800189 device_->mutable_store(),
190 flimflam::kBgscanMethodProperty,
191 DBusAdaptor::StringToVariant(
Paul Stewart0654ece2013-03-26 15:21:26 -0700192 WPASupplicant::kNetworkBgscanMethodLearn),
mukesh agrawal8abd2f62012-01-30 14:56:14 -0800193 &error));
Paul Stewart0654ece2013-03-26 15:21:26 -0700194 EXPECT_EQ(WPASupplicant::kNetworkBgscanMethodLearn, device_->bgscan_method_);
Paul Stewarte6e8e492013-01-17 11:00:50 -0800195 EXPECT_TRUE(device_->store().GetStringProperty(
196 flimflam::kBgscanMethodProperty, &method, &unused_error));
Paul Stewart0654ece2013-03-26 15:21:26 -0700197 EXPECT_EQ(WPASupplicant::kNetworkBgscanMethodLearn, method);
mukesh agrawal8abd2f62012-01-30 14:56:14 -0800198
199 EXPECT_TRUE(DBusAdaptor::ClearProperty(
200 device_->mutable_store(), flimflam::kBgscanMethodProperty, &error));
Paul Stewarte6e8e492013-01-17 11:00:50 -0800201 EXPECT_TRUE(device_->store().GetStringProperty(
202 flimflam::kBgscanMethodProperty, &method, &unused_error));
Darin Petkov4a66cc52012-06-15 10:08:29 +0200203 EXPECT_EQ(WiFi::kDefaultBgscanMethod, method);
204 EXPECT_TRUE(device_->bgscan_method_.empty());
mukesh agrawal8abd2f62012-01-30 14:56:14 -0800205}
206
Paul Stewart3c504012013-01-17 17:49:58 -0800207MATCHER_P(EndpointMatch, endpoint, "") {
208 return
209 arg->ssid() == endpoint->ssid() &&
210 arg->network_mode() == endpoint->network_mode() &&
211 arg->security_mode() == endpoint->security_mode();
212}
213
Wade Guthrie5a4e2ef2013-04-30 12:51:39 -0700214
Paul Stewarte369ece2012-05-22 09:11:03 -0700215class WiFiObjectTest : public ::testing::TestWithParam<string> {
mukesh agrawal31950242011-07-14 11:53:38 -0700216 public:
Wade Guthriebb9fca22013-04-10 17:21:42 -0700217 explicit WiFiObjectTest(EventDispatcher *dispatcher)
Paul Stewarte369ece2012-05-22 09:11:03 -0700218 : event_dispatcher_(dispatcher),
Thieu Le6c1e3bb2013-02-06 15:20:35 -0800219 metrics_(NULL),
Paul Stewarte369ece2012-05-22 09:11:03 -0700220 manager_(&control_interface_, NULL, &metrics_, &glib_),
221 device_info_(&control_interface_, dispatcher, &metrics_, &manager_),
Chris Masone626719f2011-08-18 16:58:48 -0700222 wifi_(new WiFi(&control_interface_,
Paul Stewarte369ece2012-05-22 09:11:03 -0700223 dispatcher,
Thieu Le3426c8f2012-01-11 17:35:11 -0800224 &metrics_,
Chris Masone626719f2011-08-18 16:58:48 -0700225 &manager_,
226 kDeviceName,
227 kDeviceAddress,
228 0)),
Paul Stewart3c504012013-01-17 17:49:58 -0800229 bss_counter_(0),
mukesh agrawaldc42bb32011-07-28 10:40:26 -0700230 supplicant_process_proxy_(new NiceMock<MockSupplicantProcessProxy>()),
mukesh agrawalb20776f2012-02-10 16:00:36 -0800231 supplicant_bss_proxy_(
232 new NiceMock<MockSupplicantBSSProxy>()),
mukesh agrawalf2f68a52011-09-01 12:15:48 -0700233 dhcp_config_(new MockDHCPConfig(&control_interface_,
Paul Stewartd408fdf2012-05-07 17:15:57 -0700234 kDeviceName)),
Darin Petkov2b8e44e2012-06-25 15:13:26 +0200235 dbus_manager_(new NiceMock<MockDBusManager>()),
Paul Stewart735eab52013-03-29 09:19:23 -0700236 eap_state_handler_(new NiceMock<MockSupplicantEAPStateHandler>()),
Paul Stewart3c504012013-01-17 17:49:58 -0800237 supplicant_interface_proxy_(
Paul Stewart9413bcc2013-04-04 16:12:43 -0700238 new NiceMock<MockSupplicantInterfaceProxy>()),
mukesh agrawal2f9df4e2012-08-08 12:29:20 -0700239 proxy_factory_(this) {
Wade Guthrie5a4e2ef2013-04-30 12:51:39 -0700240 InstallMockScanSession();
mukesh agrawal31950242011-07-14 11:53:38 -0700241 ::testing::DefaultValue< ::DBus::Path>::Set("/default/path");
mukesh agrawalc01f3982012-01-24 13:48:39 -0800242
Paul Stewartd408fdf2012-05-07 17:15:57 -0700243 ON_CALL(dhcp_provider_, CreateConfig(_, _, _, _)).
mukesh agrawalc01f3982012-01-24 13:48:39 -0800244 WillByDefault(Return(dhcp_config_));
245 ON_CALL(*dhcp_config_.get(), RequestIP()).
246 WillByDefault(Return(true));
Paul Stewart835934a2012-12-06 19:27:09 -0800247 ON_CALL(proxy_factory_, CreateSupplicantNetworkProxy(_, _)).
248 WillByDefault(InvokeWithoutArgs(
249 this, &WiFiObjectTest::CreateSupplicantNetworkProxy));
Wade Guthrie318445d2013-05-16 14:05:28 -0700250 Nl80211Message::SetMessageType(kNl80211FamilyId);
Gary Morainac1bdb42012-02-16 17:42:29 -0800251
Paul Stewart735eab52013-03-29 09:19:23 -0700252 // Transfers ownership.
253 manager_.dbus_manager_.reset(dbus_manager_);
254 wifi_->eap_state_handler_.reset(eap_state_handler_);
Darin Petkov2b8e44e2012-06-25 15:13:26 +0200255
Paul Stewart3c504012013-01-17 17:49:58 -0800256 wifi_->provider_ = &wifi_provider_;
mukesh agrawal5c05b292012-03-07 10:12:52 -0800257 wifi_->time_ = &time_;
Wade Guthriebb9fca22013-04-10 17:21:42 -0700258 wifi_->netlink_manager_ = &netlink_manager_;
Wade Guthrie5a4e2ef2013-04-30 12:51:39 -0700259 wifi_->progressive_scan_enabled_ = true;
260
261 // The following is only useful when a real |ScanSession| is used; it is
262 // ignored by |MockScanSession|.
263 wifi_->all_scan_frequencies_.insert(kRandomScanFrequency1);
264 wifi_->all_scan_frequencies_.insert(kRandomScanFrequency2);
265 wifi_->all_scan_frequencies_.insert(kRandomScanFrequency3);
mukesh agrawal31950242011-07-14 11:53:38 -0700266 }
mukesh agrawal5c4dd0b2011-09-14 13:53:14 -0700267
268 virtual void SetUp() {
mukesh agrawal3ab56ac2012-05-31 14:41:43 -0700269 // EnableScopes... so that we can EXPECT_CALL for scoped log messages.
270 ScopeLogger::GetInstance()->EnableScopesByName("wifi");
Christopher Wileyd34a47c2012-08-01 15:42:45 -0700271 ScopeLogger::GetInstance()->set_verbose_level(3);
Darin Petkovab565bb2011-10-06 02:55:51 -0700272 wifi_->proxy_factory_ = &proxy_factory_;
mukesh agrawal5c4dd0b2011-09-14 13:53:14 -0700273 static_cast<Device *>(wifi_)->rtnl_handler_ = &rtnl_handler_;
274 wifi_->set_dhcp_provider(&dhcp_provider_);
Paul Stewart3c504012013-01-17 17:49:58 -0800275 ON_CALL(manager_, device_info()).WillByDefault(Return(&device_info_));
Darin Petkov2b8e44e2012-06-25 15:13:26 +0200276 EXPECT_CALL(manager_, UpdateEnabledTechnologies()).Times(AnyNumber());
mukesh agrawalb20776f2012-02-10 16:00:36 -0800277 EXPECT_CALL(*supplicant_bss_proxy_, Die()).Times(AnyNumber());
mukesh agrawal5c4dd0b2011-09-14 13:53:14 -0700278 }
279
280 virtual void TearDown() {
Paul Stewart3c504012013-01-17 17:49:58 -0800281 EXPECT_CALL(*wifi_provider(), OnEndpointRemoved(_))
282 .WillRepeatedly(Return(reinterpret_cast<WiFiService *>(NULL)));
Eric Shienbrood9a245532012-03-07 14:20:39 -0500283 wifi_->SelectService(NULL);
mukesh agrawalb20776f2012-02-10 16:00:36 -0800284 if (supplicant_bss_proxy_.get()) {
285 EXPECT_CALL(*supplicant_bss_proxy_, Die());
286 }
Darin Petkovab565bb2011-10-06 02:55:51 -0700287 wifi_->proxy_factory_ = NULL;
mukesh agrawal31950242011-07-14 11:53:38 -0700288 // must Stop WiFi instance, to clear its list of services.
289 // otherwise, the WiFi instance will not be deleted. (because
290 // services reference a WiFi instance, creating a cycle.)
Eric Shienbrood9a245532012-03-07 14:20:39 -0500291 wifi_->Stop(NULL, ResultCallback());
mukesh agrawal5c4dd0b2011-09-14 13:53:14 -0700292 wifi_->set_dhcp_provider(NULL);
mukesh agrawal3ab56ac2012-05-31 14:41:43 -0700293 // Reset scope logging, to avoid interfering with other tests.
294 ScopeLogger::GetInstance()->EnableScopesByName("-wifi");
Christopher Wileyd34a47c2012-08-01 15:42:45 -0700295 ScopeLogger::GetInstance()->set_verbose_level(0);
mukesh agrawalf2f68a52011-09-01 12:15:48 -0700296 }
297
Paul Stewart549d44c2012-07-03 12:40:25 -0700298 // Needs to be public since it is called via Invoke().
299 void StopWiFi() {
Paul Stewart549d44c2012-07-03 12:40:25 -0700300 wifi_->SetEnabled(false); // Stop(NULL, ResultCallback());
301 }
302
Christopher Wileyc6184482012-10-24 15:31:56 -0700303 // Needs to be public since it is called via Invoke().
304 void ThrowDBusError() {
305 throw DBus::Error("SomeDBusType", "A handy message");
306 }
Paul Stewart17d90652013-04-04 15:09:11 -0700307 void ResetPendingService() {
308 SetPendingService(NULL);
309 }
Christopher Wileyc6184482012-10-24 15:31:56 -0700310
Wade Guthrie5a4e2ef2013-04-30 12:51:39 -0700311 size_t GetScanFrequencyCount() const {
312 return wifi_->all_scan_frequencies_.size();
313 }
314
315 void SetScanSize(int min, int max) {
316 wifi_->min_frequencies_to_scan_ = min;
317 wifi_->max_frequencies_to_scan_ = max;
318 }
319
320 // This clears WiFi::scan_session_, thereby allowing WiFi::Scan to create a
321 // real scan session.
322 void ClearScanSession() {
323 wifi_->scan_session_.reset();
324 }
325
326 bool IsScanSessionNull() {
327 return !wifi_->scan_session_;
328 }
329
330 void InstallMockScanSession() {
331 WiFiProvider::FrequencyCountList previous_frequencies;
332 std::set<uint16_t> available_frequencies;
333 ScanSession::FractionList fractions;
334 ScanSession::OnScanFailed null_callback;
335 scan_session_ = new MockScanSession(&netlink_manager_,
336 event_dispatcher_,
337 previous_frequencies,
338 available_frequencies,
339 0,
340 fractions,
341 0,
342 0,
Wade Guthrief22681f2013-05-31 11:46:31 -0700343 null_callback,
344 NULL);
Wade Guthrie5a4e2ef2013-04-30 12:51:39 -0700345 wifi_->scan_session_.reset(scan_session_);
346 }
347
348 // Or DisableProgressiveScan()...
349 void EnableFullScan() {
350 wifi_->progressive_scan_enabled_ = false;
351 }
352
Wade Guthrie7347bf22013-04-30 11:21:51 -0700353 void OnTriggerScanResponse(const Nl80211Message &message) {
Wade Guthrie5a4e2ef2013-04-30 12:51:39 -0700354 wifi_->scan_session_->OnTriggerScanResponse(message);
355 }
356
mukesh agrawal31950242011-07-14 11:53:38 -0700357 protected:
mukesh agrawal7ec71312011-11-10 02:08:26 +0000358 typedef scoped_refptr<MockWiFiService> MockWiFiServiceRefPtr;
359
mukesh agrawal31950242011-07-14 11:53:38 -0700360 class TestProxyFactory : public ProxyFactory {
361 public:
Paul Stewarte369ece2012-05-22 09:11:03 -0700362 explicit TestProxyFactory(WiFiObjectTest *test);
mukesh agrawal31950242011-07-14 11:53:38 -0700363
364 virtual SupplicantProcessProxyInterface *CreateSupplicantProcessProxy(
mukesh agrawal1830fa12011-09-26 14:31:40 -0700365 const char */*dbus_path*/, const char */*dbus_addr*/) {
mukesh agrawaldc42bb32011-07-28 10:40:26 -0700366 return test_->supplicant_process_proxy_.release();
mukesh agrawal31950242011-07-14 11:53:38 -0700367 }
368
369 virtual SupplicantInterfaceProxyInterface *CreateSupplicantInterfaceProxy(
Paul Stewart196f50f2013-03-27 18:02:11 -0700370 SupplicantEventDelegateInterface */*delegate*/,
mukesh agrawal1830fa12011-09-26 14:31:40 -0700371 const DBus::Path &/*object_path*/,
372 const char */*dbus_addr*/) {
mukesh agrawaldc42bb32011-07-28 10:40:26 -0700373 return test_->supplicant_interface_proxy_.release();
mukesh agrawal31950242011-07-14 11:53:38 -0700374 }
375
mukesh agrawalb20776f2012-02-10 16:00:36 -0800376 MOCK_METHOD3(CreateSupplicantBSSProxy,
377 SupplicantBSSProxyInterface *(
378 WiFiEndpoint *wifi_endpoint,
379 const DBus::Path &object_path,
380 const char *dbus_addr));
381
Paul Stewart835934a2012-12-06 19:27:09 -0800382 MOCK_METHOD2(CreateSupplicantNetworkProxy,
383 SupplicantNetworkProxyInterface *(
384 const DBus::Path &object_path,
385 const char *dbus_addr));
386
mukesh agrawal31950242011-07-14 11:53:38 -0700387 private:
mukesh agrawalb20776f2012-02-10 16:00:36 -0800388 SupplicantBSSProxyInterface *CreateSupplicantBSSProxyInternal(
389 WiFiEndpoint */*wifi_endpoint*/,
390 const DBus::Path &/*object_path*/,
391 const char */*dbus_addr*/) {
392 return test_->supplicant_bss_proxy_.release();
393 }
394
Paul Stewarte369ece2012-05-22 09:11:03 -0700395 WiFiObjectTest *test_;
mukesh agrawal31950242011-07-14 11:53:38 -0700396 };
397
Paul Stewart3c504012013-01-17 17:49:58 -0800398 // Simulate the course of events when the last enpoint of a service is
399 // removed.
400 class EndpointRemovalHandler {
Wade Guthriebb9fca22013-04-10 17:21:42 -0700401 public:
402 EndpointRemovalHandler(WiFiRefPtr wifi, const WiFiServiceRefPtr &service)
403 : wifi_(wifi), service_(service) {}
404 virtual ~EndpointRemovalHandler() {}
Paul Stewart3c504012013-01-17 17:49:58 -0800405
Wade Guthriebb9fca22013-04-10 17:21:42 -0700406 WiFiServiceRefPtr OnEndpointRemoved(
407 const WiFiEndpointConstRefPtr &endpoint) {
408 wifi_->DisassociateFromService(service_);
409 return service_;
410 }
Paul Stewart3c504012013-01-17 17:49:58 -0800411
412 private:
413 WiFiRefPtr wifi_;
414 WiFiServiceRefPtr service_;
415 };
416
417 EndpointRemovalHandler *MakeEndpointRemovalHandler(
Wade Guthriebb9fca22013-04-10 17:21:42 -0700418 const WiFiServiceRefPtr &service) {
Paul Stewart3c504012013-01-17 17:49:58 -0800419 return new EndpointRemovalHandler(wifi_, service);
420 }
mukesh agrawalb66c6462012-05-07 11:45:25 -0700421 void CancelScanTimer() {
422 wifi_->scan_timer_callback_.Cancel();
423 }
Paul Stewart3c504012013-01-17 17:49:58 -0800424 // This function creates a new endpoint with a mode set to |mode|. We
425 // synthesize new |path| and |bssid| values, since we don't really care
426 // what they are for unit tests. If "use_ssid" is true, we used the
427 // passed-in ssid, otherwise we create a synthesized value for it as well.
428 WiFiEndpointRefPtr MakeNewEndpoint(const char *mode,
429 bool use_ssid,
430 string *ssid,
431 string *path,
432 string *bssid) {
433 bss_counter_++;
434 if (!use_ssid) {
435 *ssid = base::StringPrintf("ssid%d", bss_counter_);
436 }
437 *path = base::StringPrintf("/interface/bss%d", bss_counter_);
438 *bssid = base::StringPrintf("00:00:00:00:00:%02x", bss_counter_);
439 WiFiEndpointRefPtr endpoint = MakeEndpointWithMode(*ssid, *bssid, mode);
440 EXPECT_CALL(wifi_provider_,
441 OnEndpointAdded(EndpointMatch(endpoint))).Times(1);
442 return endpoint;
443 }
444 WiFiEndpointRefPtr MakeEndpoint(const string &ssid, const string &bssid) {
445 return MakeEndpointWithMode(ssid, bssid, kNetworkModeInfrastructure);
446 }
447 WiFiEndpointRefPtr MakeEndpointWithMode(
448 const string &ssid, const string &bssid, const string &mode) {
449 return WiFiEndpoint::MakeOpenEndpoint(
450 &proxy_factory_, NULL, ssid, bssid, mode, 0, 0);
451 }
452 MockWiFiServiceRefPtr MakeMockServiceWithSSID(
453 vector<uint8_t> ssid, const std::string &security) {
454 return new NiceMock<MockWiFiService>(
455 &control_interface_,
456 event_dispatcher_,
457 &metrics_,
458 &manager_,
459 &wifi_provider_,
460 ssid,
461 flimflam::kModeManaged,
462 security,
463 false);
464 }
465 MockWiFiServiceRefPtr MakeMockService(const std::string &security) {
466 return MakeMockServiceWithSSID(vector<uint8_t>(1, 'a'), security);
467 }
468 ::DBus::Path MakeNewEndpointAndService(int16_t signal_strength,
469 uint16 frequency,
470 const char *mode,
471 WiFiEndpointRefPtr *endpoint_ptr,
472 MockWiFiServiceRefPtr *service_ptr) {
473 string ssid;
474 ::DBus::Path path;
475 string bssid;
476 WiFiEndpointRefPtr endpoint =
477 MakeNewEndpoint(mode, false, &ssid, &path, &bssid);
478 MockWiFiServiceRefPtr service =
479 MakeMockServiceWithSSID(endpoint->ssid(), endpoint->security_mode());
480 EXPECT_CALL(wifi_provider_, FindServiceForEndpoint(EndpointMatch(endpoint)))
481 .WillRepeatedly(Return(service));
482 ON_CALL(*service, GetEndpointCount()).WillByDefault(Return(1));
483 ReportBSS(path, ssid, bssid, signal_strength, frequency, mode);
484 if (service_ptr) {
485 *service_ptr = service;
486 }
487 if (endpoint_ptr) {
488 *endpoint_ptr = endpoint;
489 }
490 return path;
491 }
492 ::DBus::Path AddEndpointToService(
493 WiFiServiceRefPtr service,
494 int16_t signal_strength,
495 uint16 frequency,
496 const char *mode,
497 WiFiEndpointRefPtr *endpoint_ptr) {
498 string ssid(service->ssid().begin(), service->ssid().end());
499 ::DBus::Path path;
500 string bssid;
501 WiFiEndpointRefPtr endpoint =
502 MakeNewEndpoint(mode, true, &ssid, &path, &bssid);
503 EXPECT_CALL(wifi_provider_, FindServiceForEndpoint(EndpointMatch(endpoint)))
504 .WillRepeatedly(Return(service));
505 ReportBSS(path, ssid, bssid, signal_strength, frequency, mode);
506 if (endpoint_ptr) {
507 *endpoint_ptr = endpoint;
508 }
509 return path;
510 }
511 void InitiateConnect(WiFiServiceRefPtr service) {
512 map<string, ::DBus::Variant> params;
513 wifi_->ConnectTo(service, params);
514 }
515 void InitiateDisconnect(WiFiServiceRefPtr service) {
516 wifi_->DisconnectFrom(service);
517 }
518 MockWiFiServiceRefPtr SetupConnectingService(
519 const DBus::Path &network_path,
520 WiFiEndpointRefPtr *endpoint_ptr,
521 ::DBus::Path *bss_path_ptr) {
522 if (!network_path.empty()) {
523 EXPECT_CALL(*GetSupplicantInterfaceProxy(), AddNetwork(_))
524 .WillOnce(Return(network_path));
525 EXPECT_CALL(*GetSupplicantInterfaceProxy(), SelectNetwork(network_path));
526 }
527
528 MockWiFiServiceRefPtr service;
529 WiFiEndpointRefPtr endpoint;
530 ::DBus::Path bss_path(MakeNewEndpointAndService(
531 0, 0, kNetworkModeAdHoc, &endpoint, &service));
532 EXPECT_CALL(*service, SetState(Service::kStateAssociating));
533 InitiateConnect(service);
534 Mock::VerifyAndClearExpectations(service);
535 EXPECT_FALSE(GetPendingTimeout().IsCancelled());
536 if (endpoint_ptr) {
537 *endpoint_ptr = endpoint;
538 }
539 if (bss_path_ptr) {
540 *bss_path_ptr = bss_path;
541 }
542 return service;
543 }
544
545 MockWiFiServiceRefPtr SetupConnectedService(
546 const DBus::Path &network_path,
547 WiFiEndpointRefPtr *endpoint_ptr,
548 ::DBus::Path *bss_path_ptr) {
549 WiFiEndpointRefPtr endpoint;
550 ::DBus::Path bss_path;
551 MockWiFiServiceRefPtr service =
552 SetupConnectingService(network_path, &endpoint, &bss_path);
553 if (endpoint_ptr) {
554 *endpoint_ptr = endpoint;
555 }
556 if (bss_path_ptr) {
557 *bss_path_ptr = bss_path;
558 }
559 EXPECT_CALL(*service, NotifyCurrentEndpoint(EndpointMatch(endpoint)));
560 ReportCurrentBSSChanged(bss_path);
561 EXPECT_TRUE(GetPendingTimeout().IsCancelled());
562 Mock::VerifyAndClearExpectations(service);
563
564 EXPECT_CALL(*service, SetState(Service::kStateConfiguring));
565 EXPECT_CALL(*dhcp_provider(), CreateConfig(_, _, _, _)).Times(AnyNumber());
566 EXPECT_CALL(*dhcp_config_.get(), RequestIP()).Times(AnyNumber());
Paul Stewart0654ece2013-03-26 15:21:26 -0700567 ReportStateChanged(WPASupplicant::kInterfaceStateCompleted);
Paul Stewart3c504012013-01-17 17:49:58 -0800568 Mock::VerifyAndClearExpectations(service);
569
570 EXPECT_EQ(service, GetCurrentService());
571 return service;
mukesh agrawal15908392011-11-16 18:29:25 +0000572 }
mukesh agrawalb66c6462012-05-07 11:45:25 -0700573 void FireScanTimer() {
574 wifi_->ScanTimerHandler();
575 }
Wade Guthrie5a4e2ef2013-04-30 12:51:39 -0700576 void TriggerFullScan() {
Wade Guthrie68d41092013-04-02 12:56:02 -0700577 wifi_->Scan(Device::kFullScan, NULL);
Christopher Wileyc68c8672012-11-20 16:52:21 -0800578 }
mukesh agrawal15908392011-11-16 18:29:25 +0000579 const WiFiServiceRefPtr &GetCurrentService() {
580 return wifi_->current_service_;
581 }
Paul Stewartbc6e7392012-05-24 07:07:48 -0700582 void SetCurrentService(const WiFiServiceRefPtr &service) {
583 wifi_->current_service_ = service;
584 }
mukesh agrawal31950242011-07-14 11:53:38 -0700585 const WiFi::EndpointMap &GetEndpointMap() {
mukesh agrawal165e6142011-11-22 02:22:56 +0000586 return wifi_->endpoint_by_rpcid_;
mukesh agrawal31950242011-07-14 11:53:38 -0700587 }
mukesh agrawal15908392011-11-16 18:29:25 +0000588 const WiFiServiceRefPtr &GetPendingService() {
589 return wifi_->pending_service_;
590 }
Paul Stewart2b05e622012-07-13 20:38:44 -0700591 const base::CancelableClosure &GetPendingTimeout() {
592 return wifi_->pending_timeout_callback_;
593 }
Paul Stewart1aff7302012-08-04 20:04:47 -0700594 const base::CancelableClosure &GetReconnectTimeoutCallback() {
Paul Stewart44663922012-07-30 11:03:03 -0700595 return wifi_->reconnect_timeout_callback_;
596 }
Paul Stewart1aff7302012-08-04 20:04:47 -0700597 int GetReconnectTimeoutSeconds() {
598 return WiFi::kReconnectTimeoutSeconds;
599 }
mukesh agrawalb66c6462012-05-07 11:45:25 -0700600 const base::CancelableClosure &GetScanTimer() {
601 return wifi_->scan_timer_callback_;
602 }
mukesh agrawal31950242011-07-14 11:53:38 -0700603 // note: the tests need the proxies referenced by WiFi (not the
Paul Stewarte369ece2012-05-22 09:11:03 -0700604 // proxies instantiated by WiFiObjectTest), to ensure that WiFi
mukesh agrawal31950242011-07-14 11:53:38 -0700605 // sets up its proxies correctly.
606 SupplicantProcessProxyInterface *GetSupplicantProcessProxy() {
607 return wifi_->supplicant_process_proxy_.get();
608 }
Paul Stewart3c504012013-01-17 17:49:58 -0800609 MockSupplicantInterfaceProxy *GetSupplicantInterfaceProxyFromWiFi() {
mukesh agrawal8a3188d2011-12-01 20:56:44 +0000610 return dynamic_cast<MockSupplicantInterfaceProxy *>(
611 wifi_->supplicant_interface_proxy_.get());
mukesh agrawal31950242011-07-14 11:53:38 -0700612 }
Paul Stewart3c504012013-01-17 17:49:58 -0800613 // This function returns the supplicant interface proxy whether
614 // or not we have passed the instantiated object to the WiFi instance
615 // from WiFiObjectTest, so tests don't need to worry about when they
616 // set expectations relative to StartWiFi().
617 MockSupplicantInterfaceProxy *GetSupplicantInterfaceProxy() {
618 MockSupplicantInterfaceProxy *proxy = GetSupplicantInterfaceProxyFromWiFi();
619 return proxy ? proxy : supplicant_interface_proxy_.get();
620 }
Paul Stewart835934a2012-12-06 19:27:09 -0800621 MockSupplicantNetworkProxy *CreateSupplicantNetworkProxy() {
622 return new NiceMock<MockSupplicantNetworkProxy>();
623 }
mukesh agrawal7ec71312011-11-10 02:08:26 +0000624 const string &GetSupplicantState() {
625 return wifi_->supplicant_state_;
626 }
Paul Stewart835934a2012-12-06 19:27:09 -0800627 void ClearCachedCredentials(const WiFiService *service) {
628 return wifi_->ClearCachedCredentials(service);
629 }
Paul Stewart0427cc12013-03-25 13:50:39 -0700630 void NotifyEndpointChanged(const WiFiEndpointConstRefPtr &endpoint) {
631 wifi_->NotifyEndpointChanged(endpoint);
632 }
Ben Chan381fdcc2012-10-14 21:10:36 -0700633 bool RemoveNetwork(const ::DBus::Path &network) {
634 return wifi_->RemoveNetwork(network);
635 }
mukesh agrawal261daca2011-12-02 18:56:56 +0000636 void RemoveBSS(const ::DBus::Path &bss_path);
mukesh agrawal31950242011-07-14 11:53:38 -0700637 void ReportBSS(const ::DBus::Path &bss_path,
638 const string &ssid,
639 const string &bssid,
640 int16_t signal_strength,
Thieu Lee41a72d2012-02-06 20:46:51 +0000641 uint16 frequency,
mukesh agrawal31950242011-07-14 11:53:38 -0700642 const char *mode);
mukesh agrawalc01f3982012-01-24 13:48:39 -0800643 void ReportIPConfigComplete() {
Darin Petkov79d74c92012-03-07 17:20:32 +0100644 wifi_->OnIPConfigUpdated(dhcp_config_, true);
mukesh agrawalc01f3982012-01-24 13:48:39 -0800645 }
mukesh agrawalf2f68a52011-09-01 12:15:48 -0700646 void ReportLinkUp() {
647 wifi_->LinkEvent(IFF_LOWER_UP, IFF_LOWER_UP);
648 }
mukesh agrawal31950242011-07-14 11:53:38 -0700649 void ReportScanDone() {
Wade Guthrie5a4e2ef2013-04-30 12:51:39 -0700650 // Eliminate |scan_session| so |ScanDoneTask| doesn't launch another scan.
651 wifi_->scan_session_.reset();
652 wifi_->ScanDoneTask();
653 // Make a new |scan_session| so that future scanning is done with the mock.
654 InstallMockScanSession();
655 }
656 void ReportScanDoneKeepScanSession() {
mukesh agrawaldc42bb32011-07-28 10:40:26 -0700657 wifi_->ScanDoneTask();
mukesh agrawal31950242011-07-14 11:53:38 -0700658 }
mukesh agrawal15908392011-11-16 18:29:25 +0000659 void ReportCurrentBSSChanged(const string &new_bss) {
660 wifi_->CurrentBSSChanged(new_bss);
661 }
mukesh agrawal7ec71312011-11-10 02:08:26 +0000662 void ReportStateChanged(const string &new_state) {
663 wifi_->StateChanged(new_state);
664 }
Paul Stewart5581d072012-12-17 17:30:20 -0800665 void ReportWiFiDebugScopeChanged(bool enabled) {
666 wifi_->OnWiFiDebugScopeChanged(enabled);
667 }
mukesh agrawalcf24a242012-05-21 16:46:11 -0700668 void SetPendingService(const WiFiServiceRefPtr &service) {
669 wifi_->pending_service_ = service;
670 }
mukesh agrawalbebf1b82013-04-23 15:06:33 -0700671 bool SetScanInterval(uint16_t interval_seconds, Error *error) {
672 return wifi_->SetScanInterval(interval_seconds, error);
mukesh agrawalb66c6462012-05-07 11:45:25 -0700673 }
Paul Stewarte369ece2012-05-22 09:11:03 -0700674 uint16_t GetScanInterval() {
675 return wifi_->GetScanInterval(NULL);
676 }
Darin Petkov2b8e44e2012-06-25 15:13:26 +0200677 void StartWiFi(bool supplicant_present) {
Darin Petkov2b8e44e2012-06-25 15:13:26 +0200678 wifi_->supplicant_present_ = supplicant_present;
mukesh agrawalb66c6462012-05-07 11:45:25 -0700679 wifi_->SetEnabled(true); // Start(NULL, ResultCallback());
mukesh agrawal31950242011-07-14 11:53:38 -0700680 }
Darin Petkov2b8e44e2012-06-25 15:13:26 +0200681 void StartWiFi() {
682 StartWiFi(true);
683 }
mukesh agrawal2f9df4e2012-08-08 12:29:20 -0700684 void OnAfterResume() {
685 wifi_->OnAfterResume();
686 }
687 void OnBeforeSuspend() {
688 wifi_->OnBeforeSuspend();
689 }
Darin Petkov2b8e44e2012-06-25 15:13:26 +0200690 void OnSupplicantAppear() {
691 wifi_->OnSupplicantAppear(":1.7");
692 EXPECT_TRUE(wifi_->supplicant_present_);
693 }
694 void OnSupplicantVanish() {
695 wifi_->OnSupplicantVanish();
696 EXPECT_FALSE(wifi_->supplicant_present_);
697 }
698 bool GetSupplicantPresent() {
699 return wifi_->supplicant_present_;
700 }
Darin Petkov4a66cc52012-06-15 10:08:29 +0200701 bool SetBgscanMethod(const string &method) {
702 ::DBus::Error error;
703 return DBusAdaptor::SetProperty(
704 wifi_->mutable_store(),
705 flimflam::kBgscanMethodProperty,
706 DBusAdaptor::StringToVariant(method),
707 &error);
708 }
709
710 void AppendBgscan(WiFiService *service,
711 std::map<std::string, DBus::Variant> *service_params) {
712 wifi_->AppendBgscan(service, service_params);
713 }
714
Paul Stewartbc6e7392012-05-24 07:07:48 -0700715 void ReportCertification(const map<string, ::DBus::Variant> &properties) {
716 wifi_->CertificationTask(properties);
717 }
Paul Stewart10ccbb32012-04-26 15:59:30 -0700718
Paul Stewartdb0f9172012-11-30 16:48:09 -0800719 void ReportEAPEvent(const string &status, const string &parameter) {
720 wifi_->EAPEventTask(status, parameter);
721 }
722
Paul Stewarte369ece2012-05-22 09:11:03 -0700723 void RestartFastScanAttempts() {
724 wifi_->RestartFastScanAttempts();
725 }
726
Paul Stewart1aff7302012-08-04 20:04:47 -0700727 void StartReconnectTimer() {
728 wifi_->StartReconnectTimer();
729 }
730
731 void StopReconnectTimer() {
732 wifi_->StopReconnectTimer();
733 }
734
Paul Stewart3c508e12012-08-09 11:40:06 -0700735 void SetLinkMonitor(LinkMonitor *link_monitor) {
736 wifi_->set_link_monitor(link_monitor);
737 }
738
Paul Stewart1369c2b2013-01-11 05:41:26 -0800739 bool SuspectCredentials(const WiFiService &service,
740 Service::ConnectFailure *failure) {
741 return wifi_->SuspectCredentials(service, failure);
742 }
743
Paul Stewart3c508e12012-08-09 11:40:06 -0700744 void OnLinkMonitorFailure() {
745 wifi_->OnLinkMonitorFailure();
746 }
747
mukesh agrawalbebf1b82013-04-23 15:06:33 -0700748 bool SetBgscanShortInterval(const uint16 &interval, Error *error) {
749 return wifi_->SetBgscanShortInterval(interval, error);
750 }
751
752 bool SetBgscanSignalThreshold(const int32 &threshold, Error *error) {
753 return wifi_->SetBgscanSignalThreshold(threshold, error);
754 }
755
Paul Stewart10ccbb32012-04-26 15:59:30 -0700756 NiceMockControl *control_interface() {
757 return &control_interface_;
758 }
759
Paul Stewart85aea152013-01-22 09:31:56 -0800760 MockMetrics *metrics() {
761 return &metrics_;
762 }
763
mukesh agrawal32399322011-09-01 10:53:43 -0700764 MockManager *manager() {
765 return &manager_;
766 }
Gary Moraind9f893f2012-02-06 10:03:40 -0800767
768 MockDeviceInfo *device_info() {
769 return &device_info_;
770 }
771
772 MockDHCPProvider *dhcp_provider() {
773 return &dhcp_provider_;
774 }
775
mukesh agrawal32399322011-09-01 10:53:43 -0700776 const WiFiConstRefPtr wifi() const {
777 return wifi_;
778 }
779
mukesh agrawalb20776f2012-02-10 16:00:36 -0800780 TestProxyFactory *proxy_factory() {
781 return &proxy_factory_;
782 }
783
Paul Stewart3c504012013-01-17 17:49:58 -0800784 MockWiFiProvider *wifi_provider() {
785 return &wifi_provider_;
786 }
787
Paul Stewarte369ece2012-05-22 09:11:03 -0700788 EventDispatcher *event_dispatcher_;
Wade Guthrie5a4e2ef2013-04-30 12:51:39 -0700789 MockScanSession *scan_session_; // Owned by |wifi_|.
mukesh agrawal5c4dd0b2011-09-14 13:53:14 -0700790 NiceMock<MockRTNLHandler> rtnl_handler_;
mukesh agrawal5c05b292012-03-07 10:12:52 -0800791 MockTime time_;
mukesh agrawal31950242011-07-14 11:53:38 -0700792
793 private:
mukesh agrawaldc42bb32011-07-28 10:40:26 -0700794 NiceMockControl control_interface_;
Thieu Le3426c8f2012-01-11 17:35:11 -0800795 MockMetrics metrics_;
mukesh agrawalf2f68a52011-09-01 12:15:48 -0700796 MockGLib glib_;
Chris Masone2ae797d2011-08-23 20:41:00 -0700797 MockManager manager_;
mukesh agrawalc01f3982012-01-24 13:48:39 -0800798 MockDeviceInfo device_info_;
mukesh agrawal31950242011-07-14 11:53:38 -0700799 WiFiRefPtr wifi_;
Paul Stewart3c504012013-01-17 17:49:58 -0800800 NiceMock<MockWiFiProvider> wifi_provider_;
801 int bss_counter_;
mukesh agrawal31950242011-07-14 11:53:38 -0700802
803 // protected fields interspersed between private fields, due to
804 // initialization order
805 protected:
806 static const char kDeviceName[];
Chris Masone626719f2011-08-18 16:58:48 -0700807 static const char kDeviceAddress[];
mukesh agrawal31950242011-07-14 11:53:38 -0700808 static const char kNetworkModeAdHoc[];
809 static const char kNetworkModeInfrastructure[];
Paul Stewart44663922012-07-30 11:03:03 -0700810 static const char kBSSName[];
811 static const char kSSIDName[];
mukesh agrawal31950242011-07-14 11:53:38 -0700812
mukesh agrawaldc42bb32011-07-28 10:40:26 -0700813 scoped_ptr<MockSupplicantProcessProxy> supplicant_process_proxy_;
mukesh agrawalb20776f2012-02-10 16:00:36 -0800814 scoped_ptr<MockSupplicantBSSProxy> supplicant_bss_proxy_;
mukesh agrawalf2f68a52011-09-01 12:15:48 -0700815 MockDHCPProvider dhcp_provider_;
816 scoped_refptr<MockDHCPConfig> dhcp_config_;
Paul Stewart735eab52013-03-29 09:19:23 -0700817
818 // These pointers track mock objects owned by the WiFi device instance
819 // and manager so we can perform expectations against them.
820 MockDBusManager *dbus_manager_;
821 MockSupplicantEAPStateHandler *eap_state_handler_;
Wade Guthriebb9fca22013-04-10 17:21:42 -0700822 MockNetlinkManager netlink_manager_;
mukesh agrawal31950242011-07-14 11:53:38 -0700823
824 private:
Paul Stewart3c504012013-01-17 17:49:58 -0800825 scoped_ptr<MockSupplicantInterfaceProxy> supplicant_interface_proxy_;
Paul Stewart835934a2012-12-06 19:27:09 -0800826 NiceMock<TestProxyFactory> proxy_factory_;
mukesh agrawal31950242011-07-14 11:53:38 -0700827};
828
Paul Stewarte369ece2012-05-22 09:11:03 -0700829const char WiFiObjectTest::kDeviceName[] = "wlan0";
830const char WiFiObjectTest::kDeviceAddress[] = "000102030405";
831const char WiFiObjectTest::kNetworkModeAdHoc[] = "ad-hoc";
832const char WiFiObjectTest::kNetworkModeInfrastructure[] = "infrastructure";
Paul Stewart44663922012-07-30 11:03:03 -0700833const char WiFiObjectTest::kBSSName[] = "bss0";
834const char WiFiObjectTest::kSSIDName[] = "ssid0";
mukesh agrawal31950242011-07-14 11:53:38 -0700835
Paul Stewarte369ece2012-05-22 09:11:03 -0700836void WiFiObjectTest::RemoveBSS(const ::DBus::Path &bss_path) {
mukesh agrawalb4bc57d2011-12-07 01:07:47 +0000837 wifi_->BSSRemovedTask(bss_path);
mukesh agrawal261daca2011-12-02 18:56:56 +0000838}
839
Paul Stewarte369ece2012-05-22 09:11:03 -0700840void WiFiObjectTest::ReportBSS(const ::DBus::Path &bss_path,
mukesh agrawal31950242011-07-14 11:53:38 -0700841 const string &ssid,
842 const string &bssid,
843 int16_t signal_strength,
Thieu Lee41a72d2012-02-06 20:46:51 +0000844 uint16 frequency,
mukesh agrawal31950242011-07-14 11:53:38 -0700845 const char *mode) {
846 map<string, ::DBus::Variant> bss_properties;
847
848 {
849 DBus::MessageIter writer(bss_properties["SSID"].writer());
850 writer << vector<uint8_t>(ssid.begin(), ssid.end());
851 }
852 {
853 string bssid_nosep;
854 vector<uint8_t> bssid_bytes;
855 RemoveChars(bssid, ":", &bssid_nosep);
856 base::HexStringToBytes(bssid_nosep, &bssid_bytes);
857
858 DBus::MessageIter writer(bss_properties["BSSID"].writer());
859 writer << bssid_bytes;
860 }
Paul Stewart0654ece2013-03-26 15:21:26 -0700861 bss_properties[WPASupplicant::kBSSPropertySignal].writer().
Thieu Lee41a72d2012-02-06 20:46:51 +0000862 append_int16(signal_strength);
Paul Stewart0654ece2013-03-26 15:21:26 -0700863 bss_properties[WPASupplicant::kBSSPropertyFrequency].writer().
Thieu Lee41a72d2012-02-06 20:46:51 +0000864 append_uint16(frequency);
Paul Stewart0654ece2013-03-26 15:21:26 -0700865 bss_properties[WPASupplicant::kBSSPropertyMode].writer().append_string(mode);
mukesh agrawalb4bc57d2011-12-07 01:07:47 +0000866 wifi_->BSSAddedTask(bss_path, bss_properties);
mukesh agrawal31950242011-07-14 11:53:38 -0700867}
868
Paul Stewarte369ece2012-05-22 09:11:03 -0700869WiFiObjectTest::TestProxyFactory::TestProxyFactory(WiFiObjectTest *test)
mukesh agrawalb20776f2012-02-10 16:00:36 -0800870 : test_(test) {
871 EXPECT_CALL(*this, CreateSupplicantBSSProxy(_, _, _)).Times(AnyNumber());
872 ON_CALL(*this, CreateSupplicantBSSProxy(_, _, _))
873 .WillByDefault(
874 Invoke(this, (&TestProxyFactory::CreateSupplicantBSSProxyInternal)));
875}
876
Paul Stewarte369ece2012-05-22 09:11:03 -0700877// Most of our tests involve using a real EventDispatcher object.
878class WiFiMainTest : public WiFiObjectTest {
879 public:
880 WiFiMainTest() : WiFiObjectTest(&dispatcher_) {}
881
882 protected:
883 EventDispatcher dispatcher_;
884};
885
mukesh agrawal31950242011-07-14 11:53:38 -0700886TEST_F(WiFiMainTest, ProxiesSetUpDuringStart) {
887 EXPECT_TRUE(GetSupplicantProcessProxy() == NULL);
Paul Stewart3c504012013-01-17 17:49:58 -0800888 EXPECT_TRUE(GetSupplicantInterfaceProxyFromWiFi() == NULL);
mukesh agrawal31950242011-07-14 11:53:38 -0700889
890 StartWiFi();
891 EXPECT_FALSE(GetSupplicantProcessProxy() == NULL);
Paul Stewart3c504012013-01-17 17:49:58 -0800892 EXPECT_FALSE(GetSupplicantInterfaceProxyFromWiFi() == NULL);
mukesh agrawal31950242011-07-14 11:53:38 -0700893}
894
Darin Petkov2b8e44e2012-06-25 15:13:26 +0200895TEST_F(WiFiMainTest, SupplicantPresent) {
896 EXPECT_FALSE(GetSupplicantPresent());
897}
898
899TEST_F(WiFiMainTest, OnSupplicantAppearStarted) {
900 EXPECT_TRUE(GetSupplicantProcessProxy() == NULL);
901
Paul Stewart0654ece2013-03-26 15:21:26 -0700902 EXPECT_CALL(*dbus_manager_, WatchName(WPASupplicant::kDBusAddr, _, _));
Darin Petkov2b8e44e2012-06-25 15:13:26 +0200903 StartWiFi(false); // No supplicant present.
904 EXPECT_TRUE(GetSupplicantProcessProxy() == NULL);
905
906 OnSupplicantAppear();
907 EXPECT_FALSE(GetSupplicantProcessProxy() == NULL);
Darin Petkov9cd7ca12012-07-03 11:06:40 +0200908
909 // If supplicant reappears while the device is started, the device should be
910 // restarted.
911 EXPECT_CALL(*manager(), DeregisterDevice(_));
912 EXPECT_CALL(*manager(), RegisterDevice(_));
913 OnSupplicantAppear();
Darin Petkov2b8e44e2012-06-25 15:13:26 +0200914}
915
916TEST_F(WiFiMainTest, OnSupplicantAppearStopped) {
917 EXPECT_TRUE(GetSupplicantProcessProxy() == NULL);
918
919 OnSupplicantAppear();
920 EXPECT_TRUE(GetSupplicantProcessProxy() == NULL);
Darin Petkov9cd7ca12012-07-03 11:06:40 +0200921
922 // If supplicant reappears while the device is stopped, the device should not
923 // be restarted.
924 EXPECT_CALL(*manager(), DeregisterDevice(_)).Times(0);
925 OnSupplicantAppear();
Darin Petkov2b8e44e2012-06-25 15:13:26 +0200926}
927
928TEST_F(WiFiMainTest, OnSupplicantVanishStarted) {
929 EXPECT_TRUE(GetSupplicantProcessProxy() == NULL);
930
931 StartWiFi();
932 EXPECT_FALSE(GetSupplicantProcessProxy() == NULL);
933 EXPECT_TRUE(GetSupplicantPresent());
934
935 EXPECT_CALL(*manager(), DeregisterDevice(_));
936 EXPECT_CALL(*manager(), RegisterDevice(_));
937 OnSupplicantVanish();
Darin Petkov2b8e44e2012-06-25 15:13:26 +0200938}
939
940TEST_F(WiFiMainTest, OnSupplicantVanishStopped) {
941 OnSupplicantAppear();
942 EXPECT_TRUE(GetSupplicantPresent());
943 EXPECT_CALL(*manager(), DeregisterDevice(_)).Times(0);
944 OnSupplicantVanish();
945}
946
Paul Stewart549d44c2012-07-03 12:40:25 -0700947TEST_F(WiFiMainTest, OnSupplicantVanishedWhileConnected) {
Paul Stewart3c504012013-01-17 17:49:58 -0800948 StartWiFi();
949 WiFiEndpointRefPtr endpoint;
950 WiFiServiceRefPtr service(
951 SetupConnectedService(DBus::Path(), &endpoint, NULL));
Paul Stewart549d44c2012-07-03 12:40:25 -0700952 ScopedMockLog log;
953 EXPECT_CALL(log, Log(_, _, _)).Times(AnyNumber());
Christopher Wileyc6184482012-10-24 15:31:56 -0700954 EXPECT_CALL(log, Log(logging::LOG_ERROR, _,
Paul Stewart549d44c2012-07-03 12:40:25 -0700955 EndsWith("silently resetting current_service_.")));
956 EXPECT_CALL(*manager(), DeregisterDevice(_))
957 .WillOnce(InvokeWithoutArgs(this, &WiFiObjectTest::StopWiFi));
Paul Stewart3c504012013-01-17 17:49:58 -0800958 scoped_ptr<EndpointRemovalHandler> handler(
959 MakeEndpointRemovalHandler(service));
960 EXPECT_CALL(*wifi_provider(), OnEndpointRemoved(EndpointMatch(endpoint)))
961 .WillOnce(Invoke(handler.get(),
962 &EndpointRemovalHandler::OnEndpointRemoved));
963 EXPECT_CALL(*GetSupplicantInterfaceProxy(), Disconnect()).Times(0);
Paul Stewart549d44c2012-07-03 12:40:25 -0700964 EXPECT_CALL(*manager(), RegisterDevice(_));
965 OnSupplicantVanish();
966 EXPECT_TRUE(GetCurrentService() == NULL);
967}
968
Wade Guthrie5a4e2ef2013-04-30 12:51:39 -0700969TEST_F(WiFiMainTest, CleanStart_FullScan) {
970 EnableFullScan();
mukesh agrawal31950242011-07-14 11:53:38 -0700971 EXPECT_CALL(*supplicant_process_proxy_, CreateInterface(_));
972 EXPECT_CALL(*supplicant_process_proxy_, GetInterface(_))
973 .Times(AnyNumber())
974 .WillRepeatedly(Throw(
975 DBus::Error(
976 "fi.w1.wpa_supplicant1.InterfaceUnknown",
977 "test threw fi.w1.wpa_supplicant1.InterfaceUnknown")));
mukesh agrawalb66c6462012-05-07 11:45:25 -0700978 EXPECT_TRUE(GetScanTimer().IsCancelled());
mukesh agrawal31950242011-07-14 11:53:38 -0700979 StartWiFi();
Paul Stewart3c504012013-01-17 17:49:58 -0800980 EXPECT_CALL(*GetSupplicantInterfaceProxy(), Scan(_));
mukesh agrawal32399322011-09-01 10:53:43 -0700981 dispatcher_.DispatchPendingEvents();
mukesh agrawalb66c6462012-05-07 11:45:25 -0700982 EXPECT_FALSE(GetScanTimer().IsCancelled());
mukesh agrawal31950242011-07-14 11:53:38 -0700983}
984
Wade Guthrie5a4e2ef2013-04-30 12:51:39 -0700985TEST_F(WiFiMainTest, CleanStart) {
986 EXPECT_CALL(*supplicant_process_proxy_, CreateInterface(_));
987 EXPECT_CALL(*supplicant_process_proxy_, GetInterface(_))
988 .Times(AnyNumber())
989 .WillRepeatedly(Throw(
990 DBus::Error(
991 "fi.w1.wpa_supplicant1.InterfaceUnknown",
992 "test threw fi.w1.wpa_supplicant1.InterfaceUnknown")));
993 EXPECT_TRUE(GetScanTimer().IsCancelled());
994 StartWiFi();
995 EXPECT_CALL(*scan_session_, InitiateScan());
996 dispatcher_.DispatchPendingEvents();
997 EXPECT_FALSE(GetScanTimer().IsCancelled());
998}
999
Paul Stewart835934a2012-12-06 19:27:09 -08001000TEST_F(WiFiMainTest, ClearCachedCredentials) {
Paul Stewart3c504012013-01-17 17:49:58 -08001001 StartWiFi();
Paul Stewart835934a2012-12-06 19:27:09 -08001002 DBus::Path network = "/test/path";
Paul Stewart3c504012013-01-17 17:49:58 -08001003 WiFiServiceRefPtr service(SetupConnectedService(network, NULL, NULL));
1004 EXPECT_CALL(*GetSupplicantInterfaceProxy(), RemoveNetwork(network));
Paul Stewart835934a2012-12-06 19:27:09 -08001005 ClearCachedCredentials(service);
1006}
1007
Paul Stewart0427cc12013-03-25 13:50:39 -07001008TEST_F(WiFiMainTest, NotifyEndpointChanged) {
1009 WiFiEndpointRefPtr endpoint =
1010 MakeEndpointWithMode("ssid", "00:00:00:00:00:00", kNetworkModeAdHoc);
1011 EXPECT_CALL(*wifi_provider(), OnEndpointUpdated(EndpointMatch(endpoint)));
1012 NotifyEndpointChanged(endpoint);
1013}
1014
Ben Chan381fdcc2012-10-14 21:10:36 -07001015TEST_F(WiFiMainTest, RemoveNetwork) {
1016 DBus::Path network = "/test/path";
Ben Chan381fdcc2012-10-14 21:10:36 -07001017 StartWiFi();
Paul Stewart3c504012013-01-17 17:49:58 -08001018 EXPECT_CALL(*GetSupplicantInterfaceProxy(), RemoveNetwork(network));
Ben Chan381fdcc2012-10-14 21:10:36 -07001019 EXPECT_TRUE(RemoveNetwork(network));
1020}
1021
1022TEST_F(WiFiMainTest, RemoveNetworkWhenSupplicantReturnsNetworkUnknown) {
1023 DBus::Path network = "/test/path";
Paul Stewart3c504012013-01-17 17:49:58 -08001024 EXPECT_CALL(*GetSupplicantInterfaceProxy(), RemoveNetwork(network))
Ben Chan381fdcc2012-10-14 21:10:36 -07001025 .WillRepeatedly(Throw(
1026 DBus::Error(
1027 "fi.w1.wpa_supplicant1.NetworkUnknown",
1028 "test threw fi.w1.wpa_supplicant1.NetworkUnknown")));
1029 StartWiFi();
1030 EXPECT_TRUE(RemoveNetwork(network));
1031}
1032
Arman Ugurayed8e6102012-11-29 14:47:20 -08001033TEST_F(WiFiMainTest, UseArpGateway) {
1034 EXPECT_CALL(dhcp_provider_, CreateConfig(kDeviceName, _, _, true))
1035 .WillOnce(Return(dhcp_config_));
1036 const_cast<WiFi *>(wifi().get())->AcquireIPConfig();
1037}
1038
Ben Chan381fdcc2012-10-14 21:10:36 -07001039TEST_F(WiFiMainTest, RemoveNetworkWhenSupplicantReturnsInvalidArgs) {
1040 DBus::Path network = "/test/path";
Paul Stewart3c504012013-01-17 17:49:58 -08001041 EXPECT_CALL(*GetSupplicantInterfaceProxy(), RemoveNetwork(network))
Ben Chan381fdcc2012-10-14 21:10:36 -07001042 .WillRepeatedly(Throw(
1043 DBus::Error(
1044 "fi.w1.wpa_supplicant1.InvalidArgs",
1045 "test threw fi.w1.wpa_supplicant1.InvalidArgs")));
1046 StartWiFi();
1047 EXPECT_FALSE(RemoveNetwork(network));
1048}
1049
1050TEST_F(WiFiMainTest, RemoveNetworkWhenSupplicantReturnsUnknownError) {
1051 DBus::Path network = "/test/path";
Paul Stewart3c504012013-01-17 17:49:58 -08001052 EXPECT_CALL(*GetSupplicantInterfaceProxy(), RemoveNetwork(network))
Ben Chan381fdcc2012-10-14 21:10:36 -07001053 .WillRepeatedly(Throw(
1054 DBus::Error(
1055 "fi.w1.wpa_supplicant1.UnknownError",
1056 "test threw fi.w1.wpa_supplicant1.UnknownError")));
1057 StartWiFi();
1058 EXPECT_FALSE(RemoveNetwork(network));
1059}
1060
Wade Guthrie5a4e2ef2013-04-30 12:51:39 -07001061TEST_F(WiFiMainTest, Restart_FullScan) {
1062 EnableFullScan();
mukesh agrawal31950242011-07-14 11:53:38 -07001063 EXPECT_CALL(*supplicant_process_proxy_, CreateInterface(_))
1064 .Times(AnyNumber())
1065 .WillRepeatedly(Throw(
1066 DBus::Error(
1067 "fi.w1.wpa_supplicant1.InterfaceExists",
Darin Petkov2b8e44e2012-06-25 15:13:26 +02001068 "test threw fi.w1.wpa_supplicant1.InterfaceExists")));
mukesh agrawal31950242011-07-14 11:53:38 -07001069 EXPECT_CALL(*supplicant_process_proxy_, GetInterface(_));
Paul Stewart3c504012013-01-17 17:49:58 -08001070 EXPECT_CALL(*GetSupplicantInterfaceProxy(), Scan(_));
mukesh agrawal31950242011-07-14 11:53:38 -07001071 StartWiFi();
mukesh agrawal32399322011-09-01 10:53:43 -07001072 dispatcher_.DispatchPendingEvents();
mukesh agrawal31950242011-07-14 11:53:38 -07001073}
1074
Wade Guthrie5a4e2ef2013-04-30 12:51:39 -07001075TEST_F(WiFiMainTest, Restart) {
1076 EXPECT_CALL(*supplicant_process_proxy_, CreateInterface(_))
1077 .Times(AnyNumber())
1078 .WillRepeatedly(Throw(
1079 DBus::Error(
1080 "fi.w1.wpa_supplicant1.InterfaceExists",
1081 "test threw fi.w1.wpa_supplicant1.InterfaceExists")));
1082 EXPECT_CALL(*scan_session_, InitiateScan());
1083 StartWiFi();
1084 dispatcher_.DispatchPendingEvents();
1085}
1086
mukesh agrawal31950242011-07-14 11:53:38 -07001087TEST_F(WiFiMainTest, StartClearsState) {
Paul Stewart3c504012013-01-17 17:49:58 -08001088 EXPECT_CALL(*GetSupplicantInterfaceProxy(), RemoveAllNetworks());
1089 EXPECT_CALL(*GetSupplicantInterfaceProxy(), FlushBSS(_));
mukesh agrawal31950242011-07-14 11:53:38 -07001090 StartWiFi();
1091}
1092
Wade Guthrie5a4e2ef2013-04-30 12:51:39 -07001093TEST_F(WiFiMainTest, NoScansWhileConnecting_FullScan) {
1094 EnableFullScan();
Christopher Wileyc68c8672012-11-20 16:52:21 -08001095 StartWiFi();
Paul Stewart3c504012013-01-17 17:49:58 -08001096 EXPECT_CALL(*GetSupplicantInterfaceProxy(), Scan(_)).Times(1);
1097 dispatcher_.DispatchPendingEvents();
1098 Mock::VerifyAndClearExpectations(GetSupplicantInterfaceProxy());
1099 MockWiFiServiceRefPtr service = MakeMockService(flimflam::kSecurityNone);
Christopher Wileyc68c8672012-11-20 16:52:21 -08001100 SetPendingService(service);
1101 // If we're connecting, we ignore scan requests to stay on channel.
Paul Stewart3c504012013-01-17 17:49:58 -08001102 EXPECT_CALL(*service, IsConnecting()).WillOnce(Return(true));
Christopher Wileyc68c8672012-11-20 16:52:21 -08001103 EXPECT_CALL(*GetSupplicantInterfaceProxy(), Scan(_)).Times(0);
Wade Guthrie5a4e2ef2013-04-30 12:51:39 -07001104 TriggerFullScan();
Christopher Wileyc68c8672012-11-20 16:52:21 -08001105 dispatcher_.DispatchPendingEvents();
1106 Mock::VerifyAndClearExpectations(GetSupplicantInterfaceProxy());
Paul Stewart3c504012013-01-17 17:49:58 -08001107 Mock::VerifyAndClearExpectations(service);
1108 EXPECT_CALL(*service, IsConnecting()).WillOnce(Return(false));
Christopher Wileyc68c8672012-11-20 16:52:21 -08001109 EXPECT_CALL(*GetSupplicantInterfaceProxy(), Scan(_)).Times(1);
Wade Guthrie5a4e2ef2013-04-30 12:51:39 -07001110 TriggerFullScan();
Christopher Wileyc68c8672012-11-20 16:52:21 -08001111 dispatcher_.DispatchPendingEvents();
1112 Mock::VerifyAndClearExpectations(GetSupplicantInterfaceProxy());
Paul Stewart3c504012013-01-17 17:49:58 -08001113 Mock::VerifyAndClearExpectations(service);
Christopher Wileyc68c8672012-11-20 16:52:21 -08001114 // Similarly, ignore scans when our connected service is reconnecting.
1115 SetPendingService(NULL);
1116 SetCurrentService(service);
Paul Stewart3c504012013-01-17 17:49:58 -08001117 EXPECT_CALL(*service, IsConnecting()).WillOnce(Return(true));
Christopher Wileyc68c8672012-11-20 16:52:21 -08001118 EXPECT_CALL(*GetSupplicantInterfaceProxy(), Scan(_)).Times(0);
Wade Guthrie5a4e2ef2013-04-30 12:51:39 -07001119 TriggerFullScan();
Christopher Wileyc68c8672012-11-20 16:52:21 -08001120 dispatcher_.DispatchPendingEvents();
1121 Mock::VerifyAndClearExpectations(GetSupplicantInterfaceProxy());
Paul Stewart3c504012013-01-17 17:49:58 -08001122 Mock::VerifyAndClearExpectations(service);
Christopher Wileyc68c8672012-11-20 16:52:21 -08001123 // But otherwise we'll honor the request.
Paul Stewart3c504012013-01-17 17:49:58 -08001124 EXPECT_CALL(*service, IsConnecting()).WillOnce(Return(false));
Christopher Wileyc68c8672012-11-20 16:52:21 -08001125 EXPECT_CALL(*GetSupplicantInterfaceProxy(), Scan(_)).Times(1);
Wade Guthrie5a4e2ef2013-04-30 12:51:39 -07001126 TriggerFullScan();
Christopher Wileyc68c8672012-11-20 16:52:21 -08001127 dispatcher_.DispatchPendingEvents();
1128 Mock::VerifyAndClearExpectations(GetSupplicantInterfaceProxy());
Paul Stewart3c504012013-01-17 17:49:58 -08001129 Mock::VerifyAndClearExpectations(service);
Christopher Wileyc68c8672012-11-20 16:52:21 -08001130}
1131
Wade Guthrie5a4e2ef2013-04-30 12:51:39 -07001132TEST_F(WiFiMainTest, NoScansWhileConnecting) {
1133 StartWiFi();
1134 EXPECT_CALL(*scan_session_, InitiateScan());
1135 dispatcher_.DispatchPendingEvents();
1136 Mock::VerifyAndClearExpectations(GetSupplicantInterfaceProxy());
1137 MockWiFiServiceRefPtr service = MakeMockService(flimflam::kSecurityNone);
1138 SetPendingService(service);
1139 // If we're connecting, we ignore scan requests to stay on channel.
1140 EXPECT_CALL(*service, IsConnecting()).WillOnce(Return(true));
1141 EXPECT_CALL(*GetSupplicantInterfaceProxy(), Scan(_)).Times(0);
1142 EXPECT_CALL(*scan_session_, InitiateScan()).Times(0);
1143 TriggerFullScan();
1144 dispatcher_.DispatchPendingEvents();
1145 Mock::VerifyAndClearExpectations(GetSupplicantInterfaceProxy());
1146 Mock::VerifyAndClearExpectations(service);
1147 EXPECT_CALL(*service, IsConnecting()).WillOnce(Return(false));
1148 EXPECT_CALL(*GetSupplicantInterfaceProxy(), Scan(_)).Times(1);
1149 EXPECT_CALL(*scan_session_, InitiateScan()).Times(0);
1150 TriggerFullScan();
1151 dispatcher_.DispatchPendingEvents();
1152 Mock::VerifyAndClearExpectations(GetSupplicantInterfaceProxy());
1153 Mock::VerifyAndClearExpectations(service);
1154 // Similarly, ignore scans when our connected service is reconnecting.
1155 SetPendingService(NULL);
1156 SetCurrentService(service);
1157 EXPECT_CALL(*service, IsConnecting()).WillOnce(Return(true));
1158 EXPECT_CALL(*GetSupplicantInterfaceProxy(), Scan(_)).Times(0);
1159 EXPECT_CALL(*scan_session_, InitiateScan()).Times(0);
1160 TriggerFullScan();
1161 dispatcher_.DispatchPendingEvents();
1162 Mock::VerifyAndClearExpectations(GetSupplicantInterfaceProxy());
1163 Mock::VerifyAndClearExpectations(service);
1164 // But otherwise we'll honor the request.
1165 EXPECT_CALL(*service, IsConnecting()).WillOnce(Return(false));
1166 EXPECT_CALL(*GetSupplicantInterfaceProxy(), Scan(_)).Times(1);
1167 EXPECT_CALL(*scan_session_, InitiateScan()).Times(0);
1168 TriggerFullScan();
1169 dispatcher_.DispatchPendingEvents();
1170 Mock::VerifyAndClearExpectations(GetSupplicantInterfaceProxy());
1171 Mock::VerifyAndClearExpectations(service);
1172}
1173
1174TEST_F(WiFiMainTest, ResumeStartsScanWhenIdle_FullScan) {
1175 EnableFullScan();
Paul Stewart3c504012013-01-17 17:49:58 -08001176 EXPECT_CALL(*GetSupplicantInterfaceProxy(), Scan(_));
Gary Morainac1bdb42012-02-16 17:42:29 -08001177 StartWiFi();
1178 dispatcher_.DispatchPendingEvents();
Paul Stewart3c504012013-01-17 17:49:58 -08001179 Mock::VerifyAndClearExpectations(GetSupplicantInterfaceProxy());
mukesh agrawal5c05b292012-03-07 10:12:52 -08001180 ReportScanDone();
Gary Morainac1bdb42012-02-16 17:42:29 -08001181 ASSERT_TRUE(wifi()->IsIdle());
1182 EXPECT_CALL(*GetSupplicantInterfaceProxy(), Scan(_));
mukesh agrawal2f9df4e2012-08-08 12:29:20 -07001183 OnAfterResume();
Gary Morainac1bdb42012-02-16 17:42:29 -08001184 dispatcher_.DispatchPendingEvents();
1185}
1186
Wade Guthrie5a4e2ef2013-04-30 12:51:39 -07001187TEST_F(WiFiMainTest, ResumeStartsScanWhenIdle) {
1188 EXPECT_CALL(*scan_session_, InitiateScan());
1189 StartWiFi();
1190 dispatcher_.DispatchPendingEvents();
1191 Mock::VerifyAndClearExpectations(GetSupplicantInterfaceProxy());
1192 ReportScanDone();
1193 ASSERT_TRUE(wifi()->IsIdle());
1194 dispatcher_.DispatchPendingEvents();
1195 EXPECT_CALL(*scan_session_, InitiateScan());
1196 OnAfterResume();
1197 dispatcher_.DispatchPendingEvents();
1198}
1199
1200TEST_F(WiFiMainTest, SuspendDoesNotStartScan_FullScan) {
1201 EnableFullScan();
Paul Stewart3c504012013-01-17 17:49:58 -08001202 EXPECT_CALL(*GetSupplicantInterfaceProxy(), Scan(_));
Gary Morainac1bdb42012-02-16 17:42:29 -08001203 StartWiFi();
1204 dispatcher_.DispatchPendingEvents();
Paul Stewart3c504012013-01-17 17:49:58 -08001205 Mock::VerifyAndClearExpectations(GetSupplicantInterfaceProxy());
Gary Morainac1bdb42012-02-16 17:42:29 -08001206 ASSERT_TRUE(wifi()->IsIdle());
1207 EXPECT_CALL(*GetSupplicantInterfaceProxy(), Scan(_)).Times(0);
mukesh agrawal2f9df4e2012-08-08 12:29:20 -07001208 OnBeforeSuspend();
Gary Morainac1bdb42012-02-16 17:42:29 -08001209 dispatcher_.DispatchPendingEvents();
1210}
1211
Wade Guthrie5a4e2ef2013-04-30 12:51:39 -07001212TEST_F(WiFiMainTest, SuspendDoesNotStartScan) {
1213 EXPECT_CALL(*scan_session_, InitiateScan());
1214 StartWiFi();
1215 dispatcher_.DispatchPendingEvents();
1216 Mock::VerifyAndClearExpectations(GetSupplicantInterfaceProxy());
1217 ASSERT_TRUE(wifi()->IsIdle());
1218 EXPECT_CALL(*GetSupplicantInterfaceProxy(), Scan(_)).Times(0);
1219 EXPECT_CALL(*scan_session_, InitiateScan()).Times(0);
1220 OnBeforeSuspend();
1221 dispatcher_.DispatchPendingEvents();
1222}
1223
1224TEST_F(WiFiMainTest, ResumeDoesNotStartScanWhenNotIdle_FullScan) {
1225 EnableFullScan();
Paul Stewart3c504012013-01-17 17:49:58 -08001226 EXPECT_CALL(*GetSupplicantInterfaceProxy(), Scan(_));
Gary Morainac1bdb42012-02-16 17:42:29 -08001227 StartWiFi();
Gary Morainac1bdb42012-02-16 17:42:29 -08001228 dispatcher_.DispatchPendingEvents();
Paul Stewart3c504012013-01-17 17:49:58 -08001229 Mock::VerifyAndClearExpectations(GetSupplicantInterfaceProxy());
1230 WiFiServiceRefPtr service(SetupConnectedService(DBus::Path(), NULL, NULL));
1231 EXPECT_FALSE(wifi()->IsIdle());
1232 ScopedMockLog log;
mukesh agrawal3ab56ac2012-05-31 14:41:43 -07001233 EXPECT_CALL(log, Log(_, _, _)).Times(AnyNumber());
1234 EXPECT_CALL(log, Log(_, _, EndsWith("already scanning or connected.")));
Gary Morainac1bdb42012-02-16 17:42:29 -08001235 EXPECT_CALL(*GetSupplicantInterfaceProxy(), Scan(_)).Times(0);
mukesh agrawal2f9df4e2012-08-08 12:29:20 -07001236 OnAfterResume();
Gary Morainac1bdb42012-02-16 17:42:29 -08001237 dispatcher_.DispatchPendingEvents();
1238}
1239
Wade Guthrie5a4e2ef2013-04-30 12:51:39 -07001240TEST_F(WiFiMainTest, ResumeDoesNotStartScanWhenNotIdle) {
1241 EXPECT_CALL(*scan_session_, InitiateScan());
1242 StartWiFi();
1243 dispatcher_.DispatchPendingEvents();
1244 Mock::VerifyAndClearExpectations(GetSupplicantInterfaceProxy());
1245 WiFiServiceRefPtr service(SetupConnectedService(DBus::Path(), NULL, NULL));
1246 EXPECT_FALSE(wifi()->IsIdle());
1247 ScopedMockLog log;
1248 EXPECT_CALL(log, Log(_, _, _)).Times(AnyNumber());
1249 EXPECT_CALL(log, Log(_, _, EndsWith("already scanning or connected.")));
1250 EXPECT_CALL(*GetSupplicantInterfaceProxy(), Scan(_)).Times(0);
1251 EXPECT_CALL(*scan_session_, InitiateScan()).Times(0);
1252 OnAfterResume();
1253 dispatcher_.DispatchPendingEvents();
1254}
1255
mukesh agrawal31950242011-07-14 11:53:38 -07001256TEST_F(WiFiMainTest, ScanResults) {
Paul Stewart3c504012013-01-17 17:49:58 -08001257 EXPECT_CALL(*wifi_provider(), OnEndpointAdded(_)).Times(5);
mukesh agrawal31950242011-07-14 11:53:38 -07001258 StartWiFi();
Thieu Lee41a72d2012-02-06 20:46:51 +00001259 ReportBSS("bss0", "ssid0", "00:00:00:00:00:00", 0, 0, kNetworkModeAdHoc);
mukesh agrawal31950242011-07-14 11:53:38 -07001260 ReportBSS(
Thieu Lee41a72d2012-02-06 20:46:51 +00001261 "bss1", "ssid1", "00:00:00:00:00:01", 1, 0, kNetworkModeInfrastructure);
mukesh agrawal31950242011-07-14 11:53:38 -07001262 ReportBSS(
Thieu Lee41a72d2012-02-06 20:46:51 +00001263 "bss2", "ssid2", "00:00:00:00:00:02", 2, 0, kNetworkModeInfrastructure);
mukesh agrawal31950242011-07-14 11:53:38 -07001264 ReportBSS(
Thieu Lee41a72d2012-02-06 20:46:51 +00001265 "bss3", "ssid3", "00:00:00:00:00:03", 3, 0, kNetworkModeInfrastructure);
1266 const uint16 frequency = 2412;
1267 ReportBSS("bss4", "ssid4", "00:00:00:00:00:04", 4, frequency,
1268 kNetworkModeAdHoc);
1269
1270 const WiFi::EndpointMap &endpoints_by_rpcid = GetEndpointMap();
1271 EXPECT_EQ(5, endpoints_by_rpcid.size());
1272
1273 WiFi::EndpointMap::const_iterator i;
1274 WiFiEndpointRefPtr endpoint;
1275 for (i = endpoints_by_rpcid.begin();
1276 i != endpoints_by_rpcid.end();
1277 ++i) {
1278 if (i->second->bssid_string() == "00:00:00:00:00:04")
1279 break;
1280 }
1281 ASSERT_TRUE(i != endpoints_by_rpcid.end());
1282 EXPECT_EQ(4, i->second->signal_strength());
1283 EXPECT_EQ(frequency, i->second->frequency());
1284 EXPECT_EQ("adhoc", i->second->network_mode());
mukesh agrawal31950242011-07-14 11:53:38 -07001285}
1286
mukesh agrawal31950242011-07-14 11:53:38 -07001287TEST_F(WiFiMainTest, ScanCompleted) {
1288 StartWiFi();
Paul Stewart3c504012013-01-17 17:49:58 -08001289 WiFiEndpointRefPtr ap0 = MakeEndpointWithMode("ssid0", "00:00:00:00:00:00",
1290 kNetworkModeAdHoc);
1291 WiFiEndpointRefPtr ap1 = MakeEndpoint("ssid1", "00:00:00:00:00:01");
1292 WiFiEndpointRefPtr ap2 = MakeEndpoint("ssid2", "00:00:00:00:00:02");
1293 EXPECT_CALL(*wifi_provider(), OnEndpointAdded(EndpointMatch(ap0))).Times(1);
1294 EXPECT_CALL(*wifi_provider(), OnEndpointAdded(EndpointMatch(ap1))).Times(1);
1295 EXPECT_CALL(*wifi_provider(), OnEndpointAdded(EndpointMatch(ap2))).Times(1);
1296 ReportBSS("bss0", ap0->ssid_string(), ap0->bssid_string(), 0, 0,
1297 kNetworkModeAdHoc);
1298 ReportBSS("bss1", ap1->ssid_string(), ap1->bssid_string(), 0, 0,
1299 kNetworkModeInfrastructure);
1300 ReportBSS("bss2", ap2->ssid_string(), ap2->bssid_string(), 0, 0,
1301 kNetworkModeInfrastructure);
mukesh agrawal31950242011-07-14 11:53:38 -07001302 ReportScanDone();
Paul Stewart3c504012013-01-17 17:49:58 -08001303 Mock::VerifyAndClearExpectations(wifi_provider());
1304
1305 EXPECT_CALL(*wifi_provider(), OnEndpointAdded(_)).Times(0);
mukesh agrawalb3857612012-01-18 16:23:29 -08001306
mukesh agrawale9adda12012-02-09 18:33:48 -08001307 // BSSes with SSIDs that start with NULL should be filtered.
Thieu Lee41a72d2012-02-06 20:46:51 +00001308 ReportBSS("bss3", string(1, 0), "00:00:00:00:00:03", 3, 0, kNetworkModeAdHoc);
mukesh agrawalb3857612012-01-18 16:23:29 -08001309
mukesh agrawale9adda12012-02-09 18:33:48 -08001310 // BSSes with empty SSIDs should be filtered.
1311 ReportBSS("bss3", string(), "00:00:00:00:00:03", 3, 0, kNetworkModeAdHoc);
mukesh agrawal8a3188d2011-12-01 20:56:44 +00001312}
1313
1314TEST_F(WiFiMainTest, LoneBSSRemovedWhileConnected) {
1315 StartWiFi();
Paul Stewart3c504012013-01-17 17:49:58 -08001316 WiFiEndpointRefPtr endpoint;
1317 DBus::Path bss_path;
1318 WiFiServiceRefPtr service(
1319 SetupConnectedService(DBus::Path(), &endpoint, &bss_path));
1320 scoped_ptr<EndpointRemovalHandler> handler(
1321 MakeEndpointRemovalHandler(service));
1322 EXPECT_CALL(*wifi_provider(), OnEndpointRemoved(EndpointMatch(endpoint)))
1323 .WillOnce(Invoke(handler.get(),
1324 &EndpointRemovalHandler::OnEndpointRemoved));
mukesh agrawal8a3188d2011-12-01 20:56:44 +00001325 EXPECT_CALL(*GetSupplicantInterfaceProxy(), Disconnect());
Paul Stewart3c504012013-01-17 17:49:58 -08001326 RemoveBSS(bss_path);
mukesh agrawal261daca2011-12-02 18:56:56 +00001327}
1328
1329TEST_F(WiFiMainTest, NonSolitaryBSSRemoved) {
1330 StartWiFi();
Paul Stewart3c504012013-01-17 17:49:58 -08001331 WiFiEndpointRefPtr endpoint;
1332 DBus::Path bss_path;
1333 WiFiServiceRefPtr service(
1334 SetupConnectedService(DBus::Path(), &endpoint, &bss_path));
1335 EXPECT_CALL(*wifi_provider(), OnEndpointRemoved(EndpointMatch(endpoint)))
1336 .WillOnce(Return(reinterpret_cast<WiFiService *>(NULL)));
1337 EXPECT_CALL(*GetSupplicantInterfaceProxy(), Disconnect()).Times(0);
1338 RemoveBSS(bss_path);
mukesh agrawal31950242011-07-14 11:53:38 -07001339}
1340
Paul Stewart835934a2012-12-06 19:27:09 -08001341TEST_F(WiFiMainTest, ReconnectPreservesDBusPath) {
Paul Stewart835934a2012-12-06 19:27:09 -08001342 StartWiFi();
Paul Stewart3c504012013-01-17 17:49:58 -08001343 DBus::Path kPath = "/test/path";
1344 WiFiServiceRefPtr service(SetupConnectedService(kPath, NULL, NULL));
Paul Stewart835934a2012-12-06 19:27:09 -08001345
1346 // Return the service to a connectable state.
Paul Stewart3c504012013-01-17 17:49:58 -08001347 EXPECT_CALL(*GetSupplicantInterfaceProxy(), Disconnect());
Paul Stewart835934a2012-12-06 19:27:09 -08001348 InitiateDisconnect(service);
Paul Stewart3c504012013-01-17 17:49:58 -08001349 Mock::VerifyAndClearExpectations(GetSupplicantInterfaceProxy());
1350
1351 // Complete the disconnection by reporting a BSS change.
Paul Stewart0654ece2013-03-26 15:21:26 -07001352 ReportCurrentBSSChanged(WPASupplicant::kCurrentBSSNull);
Paul Stewart835934a2012-12-06 19:27:09 -08001353
1354 // A second connection attempt should remember the DBus path associated
1355 // with this service.
Paul Stewart3c504012013-01-17 17:49:58 -08001356 EXPECT_CALL(*GetSupplicantInterfaceProxy(), AddNetwork(_)).Times(0);
1357 EXPECT_CALL(*GetSupplicantInterfaceProxy(), SelectNetwork(kPath));
Paul Stewart835934a2012-12-06 19:27:09 -08001358 InitiateConnect(service);
1359}
1360
mukesh agrawal0ed0f2e2011-12-05 20:36:17 +00001361TEST_F(WiFiMainTest, DisconnectPendingService) {
mukesh agrawal0ed0f2e2011-12-05 20:36:17 +00001362 StartWiFi();
Paul Stewart3c504012013-01-17 17:49:58 -08001363 MockWiFiServiceRefPtr service(
1364 SetupConnectingService(DBus::Path(), NULL, NULL));
1365 EXPECT_TRUE(GetPendingService() == service.get());
1366 EXPECT_CALL(*GetSupplicantInterfaceProxy(), Disconnect());
1367 EXPECT_CALL(*service, SetState(Service::kStateIdle)).Times(AtLeast(1));
mukesh agrawal0ed0f2e2011-12-05 20:36:17 +00001368 InitiateDisconnect(service);
Paul Stewart3c504012013-01-17 17:49:58 -08001369 Mock::VerifyAndClearExpectations(service.get());
mukesh agrawal0ed0f2e2011-12-05 20:36:17 +00001370 EXPECT_TRUE(GetPendingService() == NULL);
1371}
1372
1373TEST_F(WiFiMainTest, DisconnectPendingServiceWithCurrent) {
mukesh agrawal0ed0f2e2011-12-05 20:36:17 +00001374 StartWiFi();
Paul Stewart3c504012013-01-17 17:49:58 -08001375 MockWiFiServiceRefPtr service0(
1376 SetupConnectedService(DBus::Path(), NULL, NULL));
1377 EXPECT_EQ(service0, GetCurrentService());
1378 EXPECT_EQ(NULL, GetPendingService().get());
mukesh agrawal0ed0f2e2011-12-05 20:36:17 +00001379
Paul Stewart3c504012013-01-17 17:49:58 -08001380 // We don't explicitly call Disconnect() while transitioning to a new
1381 // service. Instead, we use the side-effect of SelectNetwork (verified in
1382 // SetupConnectingService).
1383 EXPECT_CALL(*GetSupplicantInterfaceProxy(), Disconnect()).Times(0);
1384 MockWiFiServiceRefPtr service1(
1385 SetupConnectingService("/new/path", NULL, NULL));
1386 Mock::VerifyAndClearExpectations(GetSupplicantInterfaceProxy());
mukesh agrawal0ed0f2e2011-12-05 20:36:17 +00001387
1388 EXPECT_EQ(service0, GetCurrentService());
1389 EXPECT_EQ(service1, GetPendingService());
Paul Stewart3c504012013-01-17 17:49:58 -08001390 EXPECT_CALL(*service1, SetState(Service::kStateIdle)).Times(AtLeast(1));
1391 EXPECT_CALL(*GetSupplicantInterfaceProxy(), Disconnect());
mukesh agrawal0ed0f2e2011-12-05 20:36:17 +00001392 InitiateDisconnect(service1);
Paul Stewart3c504012013-01-17 17:49:58 -08001393 Mock::VerifyAndClearExpectations(service1.get());
mukesh agrawal0ed0f2e2011-12-05 20:36:17 +00001394
1395 // |current_service_| will be unchanged until supplicant signals
1396 // that CurrentBSS has changed.
1397 EXPECT_EQ(service0, GetCurrentService());
1398 // |pending_service_| is updated immediately.
Paul Stewart3c504012013-01-17 17:49:58 -08001399 EXPECT_EQ(NULL, GetPendingService().get());
Paul Stewart2b05e622012-07-13 20:38:44 -07001400 EXPECT_TRUE(GetPendingTimeout().IsCancelled());
mukesh agrawal0ed0f2e2011-12-05 20:36:17 +00001401}
1402
1403TEST_F(WiFiMainTest, DisconnectCurrentService) {
Paul Stewart3c504012013-01-17 17:49:58 -08001404 StartWiFi();
1405 ::DBus::Path kPath("/fake/path");
1406 MockWiFiServiceRefPtr service(SetupConnectedService(kPath, NULL, NULL));
1407 EXPECT_CALL(*GetSupplicantInterfaceProxy(), Disconnect());
mukesh agrawal0ed0f2e2011-12-05 20:36:17 +00001408 InitiateDisconnect(service);
1409
1410 // |current_service_| should not change until supplicant reports
1411 // a BSS change.
1412 EXPECT_EQ(service, GetCurrentService());
Paul Stewart835934a2012-12-06 19:27:09 -08001413
1414 // Expect that the entry associated with this network will be disabled.
1415 MockSupplicantNetworkProxy *network_proxy = CreateSupplicantNetworkProxy();
1416 EXPECT_CALL(*proxy_factory(), CreateSupplicantNetworkProxy(
Paul Stewart0654ece2013-03-26 15:21:26 -07001417 kPath, WPASupplicant::kDBusAddr))
Paul Stewart835934a2012-12-06 19:27:09 -08001418 .WillOnce(Return(network_proxy));
1419 EXPECT_CALL(*network_proxy, SetEnabled(false));
Paul Stewart735eab52013-03-29 09:19:23 -07001420 EXPECT_CALL(*eap_state_handler_, Reset());
Paul Stewart3c504012013-01-17 17:49:58 -08001421 EXPECT_CALL(*GetSupplicantInterfaceProxy(), RemoveNetwork(kPath)).Times(0);
Paul Stewart0654ece2013-03-26 15:21:26 -07001422 ReportCurrentBSSChanged(WPASupplicant::kCurrentBSSNull);
Paul Stewart3c504012013-01-17 17:49:58 -08001423 EXPECT_EQ(NULL, GetCurrentService().get());
1424 Mock::VerifyAndClearExpectations(GetSupplicantInterfaceProxy());
mukesh agrawal0ed0f2e2011-12-05 20:36:17 +00001425}
1426
Christopher Wileyc6184482012-10-24 15:31:56 -07001427TEST_F(WiFiMainTest, DisconnectCurrentServiceWithErrors) {
Paul Stewart3c504012013-01-17 17:49:58 -08001428 StartWiFi();
1429 DBus::Path kPath("/fake/path");
1430 WiFiServiceRefPtr service(SetupConnectedService(kPath, NULL, NULL));
1431 EXPECT_CALL(*GetSupplicantInterfaceProxy(), Disconnect())
Christopher Wileyc6184482012-10-24 15:31:56 -07001432 .WillOnce(InvokeWithoutArgs(this, (&WiFiMainTest::ThrowDBusError)));
Paul Stewart3c504012013-01-17 17:49:58 -08001433 EXPECT_CALL(*GetSupplicantInterfaceProxy(), RemoveNetwork(kPath)).Times(1);
Christopher Wileyc6184482012-10-24 15:31:56 -07001434 InitiateDisconnect(service);
1435
1436 // We may sometimes fail to disconnect via supplicant, and we patch up some
1437 // state when this happens.
1438 EXPECT_EQ(NULL, GetCurrentService().get());
1439 EXPECT_EQ(NULL, wifi()->selected_service().get());
1440}
1441
mukesh agrawal0ed0f2e2011-12-05 20:36:17 +00001442TEST_F(WiFiMainTest, DisconnectCurrentServiceWithPending) {
mukesh agrawal0ed0f2e2011-12-05 20:36:17 +00001443 StartWiFi();
Paul Stewart3c504012013-01-17 17:49:58 -08001444 WiFiServiceRefPtr service0(SetupConnectedService(DBus::Path(), NULL, NULL));
1445 WiFiServiceRefPtr service1(SetupConnectingService(DBus::Path(), NULL, NULL));
mukesh agrawal0ed0f2e2011-12-05 20:36:17 +00001446 EXPECT_EQ(service0, GetCurrentService());
1447 EXPECT_EQ(service1, GetPendingService());
Paul Stewart3c504012013-01-17 17:49:58 -08001448 EXPECT_CALL(*GetSupplicantInterfaceProxy(), Disconnect()).Times(0);
mukesh agrawal0ed0f2e2011-12-05 20:36:17 +00001449 InitiateDisconnect(service0);
1450
1451 EXPECT_EQ(service0, GetCurrentService());
1452 EXPECT_EQ(service1, GetPendingService());
Paul Stewart2b05e622012-07-13 20:38:44 -07001453 EXPECT_FALSE(GetPendingTimeout().IsCancelled());
1454}
1455
Paul Stewart17d90652013-04-04 15:09:11 -07001456TEST_F(WiFiMainTest, TimeoutPendingServiceWithEndpoints) {
Paul Stewart2b05e622012-07-13 20:38:44 -07001457 StartWiFi();
Paul Stewart2b05e622012-07-13 20:38:44 -07001458 const base::CancelableClosure &pending_timeout = GetPendingTimeout();
1459 EXPECT_TRUE(pending_timeout.IsCancelled());
Paul Stewart3c504012013-01-17 17:49:58 -08001460 MockWiFiServiceRefPtr service(
1461 SetupConnectingService(DBus::Path(), NULL, NULL));
Paul Stewart2b05e622012-07-13 20:38:44 -07001462 EXPECT_FALSE(pending_timeout.IsCancelled());
1463 EXPECT_EQ(service, GetPendingService());
Paul Stewart17d90652013-04-04 15:09:11 -07001464 // Simulate a service with a wifi_ reference calling DisconnectFrom().
1465 EXPECT_CALL(*service, DisconnectWithFailure(Service::kFailureOutOfRange, _))
1466 .WillOnce(InvokeWithoutArgs(this, &WiFiObjectTest::ResetPendingService));
1467 EXPECT_CALL(*service, HasEndpoints()).Times(0);
1468 // DisconnectFrom() should not be called directly from WiFi.
1469 EXPECT_CALL(*service, SetState(Service::kStateIdle)).Times(0);
1470 EXPECT_CALL(*GetSupplicantInterfaceProxy(), Disconnect()).Times(0);
Paul Stewart3c504012013-01-17 17:49:58 -08001471 pending_timeout.callback().Run();
Paul Stewart17d90652013-04-04 15:09:11 -07001472 Mock::VerifyAndClearExpectations(service);
1473}
1474
1475TEST_F(WiFiMainTest, TimeoutPendingServiceWithoutEndpoints) {
1476 StartWiFi();
1477 const base::CancelableClosure &pending_timeout = GetPendingTimeout();
1478 EXPECT_TRUE(pending_timeout.IsCancelled());
1479 MockWiFiServiceRefPtr service(
1480 SetupConnectingService(DBus::Path(), NULL, NULL));
1481 EXPECT_FALSE(pending_timeout.IsCancelled());
1482 EXPECT_EQ(service, GetPendingService());
1483 // We expect the service to get a disconnect call, but in this scenario
1484 // the service does nothing.
1485 EXPECT_CALL(*service, DisconnectWithFailure(Service::kFailureOutOfRange, _));
1486 EXPECT_CALL(*service, HasEndpoints()).WillOnce(Return(false));
1487 // DisconnectFrom() should be called directly from WiFi.
1488 EXPECT_CALL(*service, SetState(Service::kStateIdle)).Times(AtLeast(1));
1489 EXPECT_CALL(*GetSupplicantInterfaceProxy(), Disconnect());
1490 pending_timeout.callback().Run();
1491 EXPECT_EQ(NULL, GetPendingService().get());
mukesh agrawal0ed0f2e2011-12-05 20:36:17 +00001492}
1493
1494TEST_F(WiFiMainTest, DisconnectInvalidService) {
mukesh agrawal0ed0f2e2011-12-05 20:36:17 +00001495 StartWiFi();
Paul Stewart3c504012013-01-17 17:49:58 -08001496 MockWiFiServiceRefPtr service;
1497 MakeNewEndpointAndService(0, 0, kNetworkModeAdHoc, NULL, &service);
1498 EXPECT_CALL(*GetSupplicantInterfaceProxy(), Disconnect()).Times(0);
mukesh agrawal0ed0f2e2011-12-05 20:36:17 +00001499 InitiateDisconnect(service);
1500}
1501
1502TEST_F(WiFiMainTest, DisconnectCurrentServiceFailure) {
Paul Stewart3c504012013-01-17 17:49:58 -08001503 StartWiFi();
1504 DBus::Path kPath("/fake/path");
1505 WiFiServiceRefPtr service(SetupConnectedService(kPath, NULL, NULL));
1506 EXPECT_CALL(*GetSupplicantInterfaceProxy(), Disconnect())
mukesh agrawal0ed0f2e2011-12-05 20:36:17 +00001507 .WillRepeatedly(Throw(
1508 DBus::Error(
1509 "fi.w1.wpa_supplicant1.NotConnected",
1510 "test threw fi.w1.wpa_supplicant1.NotConnected")));
Paul Stewart3c504012013-01-17 17:49:58 -08001511 EXPECT_CALL(*GetSupplicantInterfaceProxy(), RemoveNetwork(kPath));
mukesh agrawal0ed0f2e2011-12-05 20:36:17 +00001512 InitiateDisconnect(service);
Paul Stewart3c504012013-01-17 17:49:58 -08001513 EXPECT_EQ(NULL, GetCurrentService().get());
mukesh agrawal0ed0f2e2011-12-05 20:36:17 +00001514}
1515
mukesh agrawal5c4dd0b2011-09-14 13:53:14 -07001516TEST_F(WiFiMainTest, Stop) {
mukesh agrawalc01f3982012-01-24 13:48:39 -08001517 StartWiFi();
Paul Stewart3c504012013-01-17 17:49:58 -08001518 WiFiEndpointRefPtr endpoint0;
1519 ::DBus::Path kPath("/fake/path");
1520 WiFiServiceRefPtr service0(SetupConnectedService(kPath, &endpoint0, NULL));
1521 WiFiEndpointRefPtr endpoint1;
1522 MakeNewEndpointAndService(0, 0, kNetworkModeAdHoc, &endpoint1, NULL);
mukesh agrawal5c4dd0b2011-09-14 13:53:14 -07001523
Paul Stewart3c504012013-01-17 17:49:58 -08001524 EXPECT_CALL(*wifi_provider(), OnEndpointRemoved(EndpointMatch(endpoint0)))
1525 .WillOnce(Return(reinterpret_cast<WiFiService *>(NULL)));
1526 EXPECT_CALL(*wifi_provider(), OnEndpointRemoved(EndpointMatch(endpoint1)))
1527 .WillOnce(Return(reinterpret_cast<WiFiService *>(NULL)));
1528 EXPECT_CALL(*GetSupplicantInterfaceProxy(), RemoveNetwork(kPath)).Times(1);
mukesh agrawalc01f3982012-01-24 13:48:39 -08001529 StopWiFi();
mukesh agrawalb66c6462012-05-07 11:45:25 -07001530 EXPECT_TRUE(GetScanTimer().IsCancelled());
mukesh agrawalc4f368f2012-06-04 19:45:52 -07001531 EXPECT_FALSE(wifi()->weak_ptr_factory_.HasWeakPtrs());
mukesh agrawal5c4dd0b2011-09-14 13:53:14 -07001532}
1533
Paul Stewart549d44c2012-07-03 12:40:25 -07001534TEST_F(WiFiMainTest, StopWhileConnected) {
Paul Stewart3c504012013-01-17 17:49:58 -08001535 StartWiFi();
1536 WiFiEndpointRefPtr endpoint;
1537 WiFiServiceRefPtr service(
1538 SetupConnectedService(DBus::Path(), &endpoint, NULL));
1539 scoped_ptr<EndpointRemovalHandler> handler(
1540 MakeEndpointRemovalHandler(service));
1541 EXPECT_CALL(*wifi_provider(), OnEndpointRemoved(EndpointMatch(endpoint)))
1542 .WillOnce(Invoke(handler.get(),
1543 &EndpointRemovalHandler::OnEndpointRemoved));
1544 EXPECT_CALL(*GetSupplicantInterfaceProxy(), Disconnect());
Paul Stewart549d44c2012-07-03 12:40:25 -07001545 StopWiFi();
1546 EXPECT_TRUE(GetCurrentService() == NULL);
1547}
1548
Paul Stewart44663922012-07-30 11:03:03 -07001549TEST_F(WiFiMainTest, ReconnectTimer) {
Paul Stewart3c504012013-01-17 17:49:58 -08001550 StartWiFi();
1551 MockWiFiServiceRefPtr service(
1552 SetupConnectedService(DBus::Path(), NULL, NULL));
1553 EXPECT_CALL(*service, IsConnected()).WillRepeatedly(Return(true));
Paul Stewart1aff7302012-08-04 20:04:47 -07001554 EXPECT_TRUE(GetReconnectTimeoutCallback().IsCancelled());
Paul Stewart0654ece2013-03-26 15:21:26 -07001555 ReportStateChanged(WPASupplicant::kInterfaceStateDisconnected);
Paul Stewart1aff7302012-08-04 20:04:47 -07001556 EXPECT_FALSE(GetReconnectTimeoutCallback().IsCancelled());
Paul Stewart0654ece2013-03-26 15:21:26 -07001557 ReportStateChanged(WPASupplicant::kInterfaceStateCompleted);
Paul Stewart1aff7302012-08-04 20:04:47 -07001558 EXPECT_TRUE(GetReconnectTimeoutCallback().IsCancelled());
Paul Stewart0654ece2013-03-26 15:21:26 -07001559 ReportStateChanged(WPASupplicant::kInterfaceStateDisconnected);
Paul Stewart1aff7302012-08-04 20:04:47 -07001560 EXPECT_FALSE(GetReconnectTimeoutCallback().IsCancelled());
Paul Stewart44663922012-07-30 11:03:03 -07001561 ReportCurrentBSSChanged(kBSSName);
Paul Stewart1aff7302012-08-04 20:04:47 -07001562 EXPECT_TRUE(GetReconnectTimeoutCallback().IsCancelled());
Paul Stewart0654ece2013-03-26 15:21:26 -07001563 ReportStateChanged(WPASupplicant::kInterfaceStateDisconnected);
Paul Stewart1aff7302012-08-04 20:04:47 -07001564 EXPECT_FALSE(GetReconnectTimeoutCallback().IsCancelled());
Paul Stewart3c504012013-01-17 17:49:58 -08001565 EXPECT_CALL(*GetSupplicantInterfaceProxy(), Disconnect());
Paul Stewart1aff7302012-08-04 20:04:47 -07001566 GetReconnectTimeoutCallback().callback().Run();
Paul Stewart3c504012013-01-17 17:49:58 -08001567 Mock::VerifyAndClearExpectations(GetSupplicantInterfaceProxy());
Paul Stewart1aff7302012-08-04 20:04:47 -07001568 EXPECT_TRUE(GetReconnectTimeoutCallback().IsCancelled());
Paul Stewart44663922012-07-30 11:03:03 -07001569}
1570
Paul Stewartcb59fed2012-03-21 21:14:46 -07001571
Wade Guthrie5a4e2ef2013-04-30 12:51:39 -07001572MATCHER_P(HasHiddenSSID_FullScan, ssid, "") {
Paul Stewarta41e38d2011-11-11 07:47:29 -08001573 map<string, DBus::Variant>::const_iterator it =
Paul Stewart0654ece2013-03-26 15:21:26 -07001574 arg.find(WPASupplicant::kPropertyScanSSIDs);
Paul Stewartced6a0b2011-11-08 15:32:04 -08001575 if (it == arg.end()) {
1576 return false;
1577 }
1578
1579 const DBus::Variant &ssids_variant = it->second;
1580 EXPECT_TRUE(DBusAdaptor::IsByteArrays(ssids_variant.signature()));
1581 const ByteArrays &ssids = it->second.operator ByteArrays();
1582 // A valid Scan containing a single hidden SSID should contain
1583 // two SSID entries: one containing the SSID we are looking for,
1584 // and an empty entry, signifying that we also want to do a
1585 // broadcast probe request for all non-hidden APs as well.
Paul Stewart3c504012013-01-17 17:49:58 -08001586 return ssids.size() == 2 && ssids[0] == ssid && ssids[1].empty();
1587}
1588
Wade Guthrie5a4e2ef2013-04-30 12:51:39 -07001589MATCHER(HasNoHiddenSSID_FullScan, "") {
Paul Stewart3c504012013-01-17 17:49:58 -08001590 map<string, DBus::Variant>::const_iterator it =
Paul Stewart0654ece2013-03-26 15:21:26 -07001591 arg.find(WPASupplicant::kPropertyScanSSIDs);
Paul Stewart3c504012013-01-17 17:49:58 -08001592 return it == arg.end();
Paul Stewartced6a0b2011-11-08 15:32:04 -08001593}
1594
Wade Guthrie5a4e2ef2013-04-30 12:51:39 -07001595TEST_F(WiFiMainTest, ScanHidden_FullScan) {
1596 EnableFullScan();
Paul Stewart3c504012013-01-17 17:49:58 -08001597 vector<uint8_t>kSSID(1, 'a');
1598 ByteArrays ssids;
1599 ssids.push_back(kSSID);
1600
Paul Stewartced6a0b2011-11-08 15:32:04 -08001601 StartWiFi();
Paul Stewart3c504012013-01-17 17:49:58 -08001602 EXPECT_CALL(*wifi_provider(), GetHiddenSSIDList()).WillOnce(Return(ssids));
Wade Guthrie5a4e2ef2013-04-30 12:51:39 -07001603 EXPECT_CALL(*GetSupplicantInterfaceProxy(),
1604 Scan(HasHiddenSSID_FullScan(kSSID)));
Paul Stewart3c504012013-01-17 17:49:58 -08001605 dispatcher_.DispatchPendingEvents();
1606}
1607
Wade Guthrie5a4e2ef2013-04-30 12:51:39 -07001608// This test is slightly different from the test in scan_session_unittest.cc
1609// because this tests the piece of WiFi that builds the SSID list.
1610TEST_F(WiFiMainTest, ScanHidden) {
1611 // Clear the Mock ScanSession because hidden SSIDs are added when wifi
1612 // instantiates a new ScanSession (and it won't instantiate a new ScanSession
1613 // if there's already one there).
1614 ClearScanSession();
1615 vector<uint8_t>kSSID(1, 'a');
1616 ByteArrays ssids;
1617 ssids.push_back(kSSID);
1618
Wade Guthrie7347bf22013-04-30 11:21:51 -07001619 EXPECT_CALL(netlink_manager_, SendNl80211Message(
1620 IsNl80211Command(kNl80211FamilyId, NL80211_CMD_GET_WIPHY), _, _));
Wade Guthrie5a4e2ef2013-04-30 12:51:39 -07001621 EXPECT_CALL(*wifi_provider(), GetHiddenSSIDList()).WillOnce(Return(ssids));
1622 StartWiFi();
1623 EXPECT_CALL(netlink_manager_,
Wade Guthrie7347bf22013-04-30 11:21:51 -07001624 SendNl80211Message(HasHiddenSSID(kNl80211FamilyId), _, _));
Wade Guthrie5a4e2ef2013-04-30 12:51:39 -07001625 dispatcher_.DispatchPendingEvents();
1626}
1627
1628TEST_F(WiFiMainTest, ScanNoHidden_FullScan) {
1629 EnableFullScan();
Paul Stewart3c504012013-01-17 17:49:58 -08001630 StartWiFi();
1631 EXPECT_CALL(*wifi_provider(), GetHiddenSSIDList())
1632 .WillOnce(Return(ByteArrays()));
Wade Guthrie5a4e2ef2013-04-30 12:51:39 -07001633 EXPECT_CALL(*GetSupplicantInterfaceProxy(), Scan(HasNoHiddenSSID_FullScan()));
1634 dispatcher_.DispatchPendingEvents();
1635}
1636
1637// This test is slightly different from the test in scan_session_unittest.cc
1638// because this tests the piece of WiFi that builds the SSID list.
1639TEST_F(WiFiMainTest, ScanNoHidden) {
1640 // Clear the Mock ScanSession because hidden SSIDs are added when wifi
1641 // instantiates a new ScanSession (and it won't instantiate a new ScanSession
1642 // if there's already one there).
1643 ClearScanSession();
Wade Guthrie7347bf22013-04-30 11:21:51 -07001644 EXPECT_CALL(netlink_manager_, SendNl80211Message(
1645 IsNl80211Command(kNl80211FamilyId, NL80211_CMD_GET_WIPHY), _, _));
Wade Guthrie5a4e2ef2013-04-30 12:51:39 -07001646 EXPECT_CALL(*wifi_provider(), GetHiddenSSIDList())
1647 .WillOnce(Return(ByteArrays()));
1648 StartWiFi();
1649 EXPECT_CALL(netlink_manager_,
Wade Guthrie7347bf22013-04-30 11:21:51 -07001650 SendNl80211Message(HasNoHiddenSSID(kNl80211FamilyId), _, _));
Paul Stewartced6a0b2011-11-08 15:32:04 -08001651 dispatcher_.DispatchPendingEvents();
1652}
1653
Paul Stewartfae4dae2012-09-13 07:43:32 -07001654TEST_F(WiFiMainTest, ScanWiFiDisabledAfterResume) {
1655 ScopedMockLog log;
1656 EXPECT_CALL(log, Log(_, _, _)).Times(AnyNumber());
1657 EXPECT_CALL(log, Log(_, _, EndsWith(
1658 "Ignoring scan request while device is not enabled."))).Times(1);
Paul Stewart3c504012013-01-17 17:49:58 -08001659 EXPECT_CALL(*GetSupplicantInterfaceProxy(), Scan(_)).Times(0);
Wade Guthrie5a4e2ef2013-04-30 12:51:39 -07001660 EXPECT_CALL(*scan_session_, InitiateScan()).Times(0);
Paul Stewartfae4dae2012-09-13 07:43:32 -07001661 StartWiFi();
1662 StopWiFi();
1663 // A scan is queued when WiFi resumes.
1664 OnAfterResume();
1665 dispatcher_.DispatchPendingEvents();
1666}
1667
Wade Guthrie5a4e2ef2013-04-30 12:51:39 -07001668TEST_F(WiFiMainTest, ProgressiveScanFound) {
1669 // Set min & max scan frequency count to 1 so each scan will be of a single
1670 // frequency.
1671 SetScanSize(1, 1);
1672
1673 // Do the first scan (finds nothing).
1674 EXPECT_CALL(*scan_session_, InitiateScan());
1675 StartWiFi();
1676 dispatcher_.DispatchPendingEvents();
1677 ReportScanDoneKeepScanSession();
1678
1679 // Do the second scan (connects afterwards).
1680 EXPECT_CALL(*scan_session_, InitiateScan());
1681 dispatcher_.DispatchPendingEvents();
1682 ReportScanDoneKeepScanSession();
1683
1684 // Connect after second scan.
1685 MockWiFiServiceRefPtr service = MakeMockService(flimflam::kSecurityNone);
1686 SetPendingService(service);
1687
1688 // Verify that the third scan aborts and there is no further scan.
1689 ScopedMockLog log;
1690 EXPECT_CALL(log, Log(_, _, _)).Times(AnyNumber());
1691 EXPECT_CALL(log, Log(_, _, EndsWith(
1692 "Ignoring scan request while connecting to an AP."))).Times(1);
1693 EXPECT_CALL(*metrics(), NotifyDeviceScanFinished(_));
1694 EXPECT_CALL(*scan_session_, InitiateScan()).Times(0);
1695 EXPECT_CALL(*GetSupplicantInterfaceProxy(), Scan(_)).Times(0);
1696 dispatcher_.DispatchPendingEvents();
1697}
1698
1699TEST_F(WiFiMainTest, ProgressiveScanNotFound) {
1700 // Set min & max scan frequency count to 1 so each scan will be of a single
1701 // frequency.
1702 SetScanSize(1, 1);
1703
1704 // Do the first scan (finds nothing).
1705 EXPECT_CALL(*scan_session_, InitiateScan());
1706 StartWiFi();
1707 dispatcher_.DispatchPendingEvents();
1708 ReportScanDoneKeepScanSession();
1709
1710 // Do the second scan (finds nothing).
1711 EXPECT_CALL(*scan_session_, InitiateScan());
1712 dispatcher_.DispatchPendingEvents();
1713 ReportScanDoneKeepScanSession();
1714
1715 // Do the third scan. After (simulated) exhausting of search frequencies,
1716 // verify that this scan uses supplicant rather than internal (progressive)
1717 // scan.
1718 EXPECT_CALL(*scan_session_, HasMoreFrequencies()).WillOnce(Return(false));
1719 EXPECT_CALL(*scan_session_, InitiateScan()).Times(0);
1720 EXPECT_CALL(*GetSupplicantInterfaceProxy(), Scan(_));
1721 dispatcher_.DispatchPendingEvents();
1722
1723 // And verify that ScanDone reports a complete scan (i.e., the
1724 // wifi_::scan_session_ has truly been cleared).
1725 EXPECT_CALL(*metrics(), NotifyDeviceScanFinished(_));
1726 ReportScanDoneKeepScanSession();
1727}
1728
1729TEST_F(WiFiMainTest, ProgressiveScanError) {
1730 // Clear the Mock ScanSession so I can get an actual ScanSession.
1731 ClearScanSession();
1732 StartWiFi();
1733
1734 // This should call |Scan| which posts |ScanTask|
1735 NewScanResultsMessage not_supposed_to_get_this_message;
1736 OnTriggerScanResponse(not_supposed_to_get_this_message);
1737
1738 EXPECT_TRUE(IsScanSessionNull());
1739 EXPECT_CALL(*GetSupplicantInterfaceProxy(), Scan(_)).Times(1);
1740 dispatcher_.DispatchPendingEvents();
1741}
1742
mukesh agrawal7ec71312011-11-10 02:08:26 +00001743TEST_F(WiFiMainTest, InitialSupplicantState) {
1744 EXPECT_EQ(WiFi::kInterfaceStateUnknown, GetSupplicantState());
1745}
1746
1747TEST_F(WiFiMainTest, StateChangeNoService) {
1748 // State change should succeed even if there is no pending Service.
Paul Stewart0654ece2013-03-26 15:21:26 -07001749 ReportStateChanged(WPASupplicant::kInterfaceStateScanning);
1750 EXPECT_EQ(WPASupplicant::kInterfaceStateScanning, GetSupplicantState());
mukesh agrawal7ec71312011-11-10 02:08:26 +00001751}
1752
1753TEST_F(WiFiMainTest, StateChangeWithService) {
1754 // Forward transition should trigger a Service state change.
1755 StartWiFi();
1756 dispatcher_.DispatchPendingEvents();
mukesh agrawalcf24a242012-05-21 16:46:11 -07001757 MockWiFiServiceRefPtr service = MakeMockService(flimflam::kSecurityNone);
mukesh agrawal7ec71312011-11-10 02:08:26 +00001758 InitiateConnect(service);
1759 EXPECT_CALL(*service.get(), SetState(Service::kStateAssociating));
Paul Stewart0654ece2013-03-26 15:21:26 -07001760 ReportStateChanged(WPASupplicant::kInterfaceStateAssociated);
mukesh agrawal7ec71312011-11-10 02:08:26 +00001761 // Verify expectations now, because WiFi may report other state changes
1762 // when WiFi is Stop()-ed (during TearDown()).
1763 Mock::VerifyAndClearExpectations(service.get());
Gary Moraind9f893f2012-02-06 10:03:40 -08001764 EXPECT_CALL(*service.get(), SetState(_)).Times(AnyNumber());
mukesh agrawal7ec71312011-11-10 02:08:26 +00001765}
1766
1767TEST_F(WiFiMainTest, StateChangeBackwardsWithService) {
1768 // Some backwards transitions should not trigger a Service state change.
1769 // Supplicant state should still be updated, however.
Paul Stewartd408fdf2012-05-07 17:15:57 -07001770 EXPECT_CALL(*dhcp_provider(), CreateConfig(_, _, _, _)).Times(AnyNumber());
Gary Moraind9f893f2012-02-06 10:03:40 -08001771 EXPECT_CALL(*dhcp_config_.get(), RequestIP()).Times(AnyNumber());
mukesh agrawal7ec71312011-11-10 02:08:26 +00001772 StartWiFi();
1773 dispatcher_.DispatchPendingEvents();
mukesh agrawalcf24a242012-05-21 16:46:11 -07001774 MockWiFiServiceRefPtr service = MakeMockService(flimflam::kSecurityNone);
Paul Stewart2b05e622012-07-13 20:38:44 -07001775 EXPECT_CALL(*service.get(), SetState(Service::kStateAssociating));
Gary Moraind9f893f2012-02-06 10:03:40 -08001776 EXPECT_CALL(*service.get(), SetState(Service::kStateConfiguring));
mukesh agrawal7ec71312011-11-10 02:08:26 +00001777 InitiateConnect(service);
Paul Stewart0654ece2013-03-26 15:21:26 -07001778 ReportStateChanged(WPASupplicant::kInterfaceStateCompleted);
1779 ReportStateChanged(WPASupplicant::kInterfaceStateAuthenticating);
1780 EXPECT_EQ(WPASupplicant::kInterfaceStateAuthenticating,
mukesh agrawal7ec71312011-11-10 02:08:26 +00001781 GetSupplicantState());
1782 // Verify expectations now, because WiFi may report other state changes
1783 // when WiFi is Stop()-ed (during TearDown()).
Paul Stewart3c504012013-01-17 17:49:58 -08001784 Mock::VerifyAndClearExpectations(service);
1785 EXPECT_CALL(*service, SetState(_)).Times(AnyNumber());
mukesh agrawal7ec71312011-11-10 02:08:26 +00001786}
1787
Paul Stewarta47c3c62012-12-18 12:14:29 -08001788TEST_F(WiFiMainTest, ConnectToServiceWithoutRecentIssues) {
1789 MockSupplicantProcessProxy *process_proxy = supplicant_process_proxy_.get();
1790 StartWiFi();
1791 dispatcher_.DispatchPendingEvents();
1792 MockWiFiServiceRefPtr service = MakeMockService(flimflam::kSecurityNone);
1793 EXPECT_CALL(*process_proxy, GetDebugLevel()).Times(0);
1794 EXPECT_CALL(*process_proxy, SetDebugLevel(_)).Times(0);
1795 EXPECT_CALL(*service.get(), HasRecentConnectionIssues())
1796 .WillOnce(Return(false));
1797 InitiateConnect(service);
1798}
1799
1800TEST_F(WiFiMainTest, ConnectToServiceWithRecentIssues) {
1801 // Turn of WiFi debugging, so the only reason we will turn on supplicant
1802 // debugging will be to debug a problematic connection.
1803 ScopeLogger::GetInstance()->EnableScopesByName("-wifi");
1804
1805 MockSupplicantProcessProxy *process_proxy = supplicant_process_proxy_.get();
1806 StartWiFi();
1807 dispatcher_.DispatchPendingEvents();
1808 MockWiFiServiceRefPtr service = MakeMockService(flimflam::kSecurityNone);
1809 EXPECT_CALL(*process_proxy, GetDebugLevel())
Paul Stewart0654ece2013-03-26 15:21:26 -07001810 .WillOnce(Return(WPASupplicant::kDebugLevelInfo));
1811 EXPECT_CALL(*process_proxy, SetDebugLevel(WPASupplicant::kDebugLevelDebug))
Paul Stewarta47c3c62012-12-18 12:14:29 -08001812 .Times(1);
1813 EXPECT_CALL(*service.get(), HasRecentConnectionIssues())
1814 .WillOnce(Return(true));
1815 InitiateConnect(service);
1816 Mock::VerifyAndClearExpectations(process_proxy);
1817
1818 SetPendingService(NULL);
1819 SetCurrentService(service);
1820
1821 // When we disconnect from the troubled service, we should reduce the
1822 // level of supplciant debugging.
1823 EXPECT_CALL(*process_proxy, GetDebugLevel())
Paul Stewart0654ece2013-03-26 15:21:26 -07001824 .WillOnce(Return(WPASupplicant::kDebugLevelDebug));
1825 EXPECT_CALL(*process_proxy, SetDebugLevel(WPASupplicant::kDebugLevelInfo))
Paul Stewarta47c3c62012-12-18 12:14:29 -08001826 .Times(1);
Paul Stewart0654ece2013-03-26 15:21:26 -07001827 ReportCurrentBSSChanged(WPASupplicant::kCurrentBSSNull);
Paul Stewarta47c3c62012-12-18 12:14:29 -08001828}
1829
mukesh agrawal15908392011-11-16 18:29:25 +00001830TEST_F(WiFiMainTest, CurrentBSSChangeConnectedToDisconnected) {
mukesh agrawal15908392011-11-16 18:29:25 +00001831 StartWiFi();
Paul Stewart3c504012013-01-17 17:49:58 -08001832 WiFiEndpointRefPtr endpoint;
1833 MockWiFiServiceRefPtr service =
1834 SetupConnectedService(DBus::Path(), &endpoint, NULL);
mukesh agrawal15908392011-11-16 18:29:25 +00001835
Paul Stewart3c504012013-01-17 17:49:58 -08001836 EXPECT_CALL(*service, SetState(Service::kStateIdle));
1837 EXPECT_CALL(*service, SetFailureSilent(Service::kFailureUnknown));
Paul Stewart0654ece2013-03-26 15:21:26 -07001838 ReportCurrentBSSChanged(WPASupplicant::kCurrentBSSNull);
mukesh agrawal15908392011-11-16 18:29:25 +00001839 EXPECT_EQ(NULL, GetCurrentService().get());
1840 EXPECT_EQ(NULL, GetPendingService().get());
1841}
1842
1843TEST_F(WiFiMainTest, CurrentBSSChangeConnectedToConnectedNewService) {
mukesh agrawal15908392011-11-16 18:29:25 +00001844 StartWiFi();
Paul Stewart3c504012013-01-17 17:49:58 -08001845 MockWiFiServiceRefPtr service0 =
1846 SetupConnectedService(DBus::Path(), NULL, NULL);
1847 MockWiFiServiceRefPtr service1;
1848 ::DBus::Path bss_path1(MakeNewEndpointAndService(
1849 0, 0, kNetworkModeAdHoc, NULL, &service1));
1850 EXPECT_EQ(service0.get(), GetCurrentService().get());
mukesh agrawal15908392011-11-16 18:29:25 +00001851
mukesh agrawalc01f3982012-01-24 13:48:39 -08001852 // Note that we deliberately omit intermediate supplicant states
1853 // (e.g. kInterfaceStateAssociating), on the theory that they are
1854 // unreliable. Specifically, they may be quashed if the association
1855 // completes before supplicant flushes its changed properties.
Paul Stewart3c504012013-01-17 17:49:58 -08001856 EXPECT_CALL(*service0, SetState(Service::kStateIdle)).Times(AtLeast(1));
1857 ReportCurrentBSSChanged(bss_path1);
1858 EXPECT_CALL(*service1, SetState(Service::kStateConfiguring));
Paul Stewart0654ece2013-03-26 15:21:26 -07001859 ReportStateChanged(WPASupplicant::kInterfaceStateCompleted);
Paul Stewart3c504012013-01-17 17:49:58 -08001860 EXPECT_EQ(service1.get(), GetCurrentService().get());
1861 Mock::VerifyAndClearExpectations(service0);
1862 Mock::VerifyAndClearExpectations(service1);
mukesh agrawal15908392011-11-16 18:29:25 +00001863}
1864
Thieu Lee41a72d2012-02-06 20:46:51 +00001865TEST_F(WiFiMainTest, CurrentBSSChangedUpdateServiceEndpoint) {
Thieu Lee41a72d2012-02-06 20:46:51 +00001866 StartWiFi();
Paul Stewart3c504012013-01-17 17:49:58 -08001867 MockWiFiServiceRefPtr service =
1868 SetupConnectedService(DBus::Path(), NULL, NULL);
1869 WiFiEndpointRefPtr endpoint;
1870 ::DBus::Path bss_path =
1871 AddEndpointToService(service, 0, 0, kNetworkModeAdHoc, &endpoint);
1872 EXPECT_CALL(*service, NotifyCurrentEndpoint(EndpointMatch(endpoint)));
1873 ReportCurrentBSSChanged(bss_path);
mukesh agrawal4eb4d782011-12-05 17:34:37 +00001874}
1875
mukesh agrawal8a3188d2011-12-01 20:56:44 +00001876TEST_F(WiFiMainTest, NewConnectPreemptsPending) {
mukesh agrawal8a3188d2011-12-01 20:56:44 +00001877 StartWiFi();
Paul Stewart3c504012013-01-17 17:49:58 -08001878 MockWiFiServiceRefPtr service0(
1879 SetupConnectingService(DBus::Path(), NULL, NULL));
1880 EXPECT_EQ(service0.get(), GetPendingService().get());
mukesh agrawal8a3188d2011-12-01 20:56:44 +00001881 EXPECT_CALL(*GetSupplicantInterfaceProxy(), Disconnect());
Paul Stewart3c504012013-01-17 17:49:58 -08001882 MockWiFiServiceRefPtr service1(
1883 SetupConnectingService(DBus::Path(), NULL, NULL));
1884 EXPECT_EQ(service1.get(), GetPendingService().get());
1885 EXPECT_EQ(NULL, GetCurrentService().get());
mukesh agrawal8a3188d2011-12-01 20:56:44 +00001886}
1887
1888TEST_F(WiFiMainTest, IsIdle) {
1889 StartWiFi();
1890 EXPECT_TRUE(wifi()->IsIdle());
Paul Stewart3c504012013-01-17 17:49:58 -08001891 MockWiFiServiceRefPtr service(
1892 SetupConnectingService(DBus::Path(), NULL, NULL));
mukesh agrawal8a3188d2011-12-01 20:56:44 +00001893 EXPECT_FALSE(wifi()->IsIdle());
1894}
1895
Darin Petkov4a66cc52012-06-15 10:08:29 +02001896MATCHER_P(WiFiAddedArgs, bgscan, "") {
Paul Stewart0654ece2013-03-26 15:21:26 -07001897 return ContainsKey(arg, WPASupplicant::kNetworkPropertyScanSSID) &&
1898 ContainsKey(arg, WPASupplicant::kNetworkPropertyBgscan) == bgscan;
mukesh agrawal4d0401c2012-01-06 16:05:31 -08001899}
1900
1901TEST_F(WiFiMainTest, AddNetworkArgs) {
mukesh agrawal4d0401c2012-01-06 16:05:31 -08001902 StartWiFi();
Paul Stewart3c504012013-01-17 17:49:58 -08001903 MockWiFiServiceRefPtr service;
1904 MakeNewEndpointAndService(0, 0, kNetworkModeAdHoc, NULL, &service);
1905 EXPECT_CALL(*GetSupplicantInterfaceProxy(), AddNetwork(WiFiAddedArgs(true)));
Paul Stewart0654ece2013-03-26 15:21:26 -07001906 EXPECT_TRUE(SetBgscanMethod(WPASupplicant::kNetworkBgscanMethodSimple));
mukesh agrawal4d0401c2012-01-06 16:05:31 -08001907 InitiateConnect(service);
1908}
1909
Darin Petkov4a66cc52012-06-15 10:08:29 +02001910TEST_F(WiFiMainTest, AddNetworkArgsNoBgscan) {
Darin Petkov4a66cc52012-06-15 10:08:29 +02001911 StartWiFi();
Paul Stewart3c504012013-01-17 17:49:58 -08001912 MockWiFiServiceRefPtr service;
1913 MakeNewEndpointAndService(0, 0, kNetworkModeAdHoc, NULL, &service);
1914 EXPECT_CALL(*GetSupplicantInterfaceProxy(), AddNetwork(WiFiAddedArgs(false)));
Darin Petkov4a66cc52012-06-15 10:08:29 +02001915 InitiateConnect(service);
1916}
1917
1918TEST_F(WiFiMainTest, AppendBgscan) {
1919 StartWiFi();
Paul Stewart3c504012013-01-17 17:49:58 -08001920 MockWiFiServiceRefPtr service = MakeMockService(flimflam::kSecurityNone);
Darin Petkov4a66cc52012-06-15 10:08:29 +02001921 {
1922 // 1 endpoint, default bgscan method -- background scan disabled.
1923 std::map<std::string, DBus::Variant> params;
Paul Stewart3c504012013-01-17 17:49:58 -08001924 EXPECT_CALL(*service, GetEndpointCount()).WillOnce(Return(1));
1925 AppendBgscan(service, &params);
1926 Mock::VerifyAndClearExpectations(service);
Paul Stewart0654ece2013-03-26 15:21:26 -07001927 EXPECT_FALSE(ContainsKey(params, WPASupplicant::kNetworkPropertyBgscan));
Darin Petkov4a66cc52012-06-15 10:08:29 +02001928 }
Darin Petkov4a66cc52012-06-15 10:08:29 +02001929 {
1930 // 2 endpoints, default bgscan method -- background scan frequency reduced.
1931 map<string, DBus::Variant> params;
Paul Stewart3c504012013-01-17 17:49:58 -08001932 EXPECT_CALL(*service, GetEndpointCount()).WillOnce(Return(2));
1933 AppendBgscan(service, &params);
1934 Mock::VerifyAndClearExpectations(service);
Darin Petkov4a66cc52012-06-15 10:08:29 +02001935 string config_string;
1936 EXPECT_TRUE(
1937 DBusProperties::GetString(params,
Paul Stewart0654ece2013-03-26 15:21:26 -07001938 WPASupplicant::kNetworkPropertyBgscan,
Darin Petkov4a66cc52012-06-15 10:08:29 +02001939 &config_string));
1940 vector<string> elements;
1941 base::SplitString(config_string, ':', &elements);
1942 ASSERT_EQ(4, elements.size());
1943 EXPECT_EQ(WiFi::kDefaultBgscanMethod, elements[0]);
1944 EXPECT_EQ(StringPrintf("%d", WiFi::kBackgroundScanIntervalSeconds),
1945 elements[3]);
1946 }
1947 {
1948 // Explicit bgscan method -- regular background scan frequency.
Paul Stewart0654ece2013-03-26 15:21:26 -07001949 EXPECT_TRUE(SetBgscanMethod(WPASupplicant::kNetworkBgscanMethodSimple));
Darin Petkov4a66cc52012-06-15 10:08:29 +02001950 std::map<std::string, DBus::Variant> params;
Paul Stewart3c504012013-01-17 17:49:58 -08001951 EXPECT_CALL(*service, GetEndpointCount()).Times(0);
1952 AppendBgscan(service, &params);
1953 Mock::VerifyAndClearExpectations(service);
Darin Petkov4a66cc52012-06-15 10:08:29 +02001954 string config_string;
1955 EXPECT_TRUE(
1956 DBusProperties::GetString(params,
Paul Stewart0654ece2013-03-26 15:21:26 -07001957 WPASupplicant::kNetworkPropertyBgscan,
Darin Petkov4a66cc52012-06-15 10:08:29 +02001958 &config_string));
1959 vector<string> elements;
1960 base::SplitString(config_string, ':', &elements);
1961 ASSERT_EQ(4, elements.size());
1962 EXPECT_EQ(StringPrintf("%d", WiFi::kDefaultScanIntervalSeconds),
1963 elements[3]);
1964 }
Christopher Wiley5c929e22012-07-16 14:06:04 -07001965 {
1966 // No scan method, simply returns without appending properties
Paul Stewart0654ece2013-03-26 15:21:26 -07001967 EXPECT_TRUE(SetBgscanMethod(WPASupplicant::kNetworkBgscanMethodNone));
Christopher Wiley5c929e22012-07-16 14:06:04 -07001968 std::map<std::string, DBus::Variant> params;
Paul Stewart3c504012013-01-17 17:49:58 -08001969 EXPECT_CALL(*service, GetEndpointCount()).Times(0);
Christopher Wiley5c929e22012-07-16 14:06:04 -07001970 AppendBgscan(service.get(), &params);
Paul Stewart3c504012013-01-17 17:49:58 -08001971 Mock::VerifyAndClearExpectations(service);
Christopher Wiley5c929e22012-07-16 14:06:04 -07001972 string config_string;
1973 EXPECT_FALSE(
1974 DBusProperties::GetString(params,
Paul Stewart0654ece2013-03-26 15:21:26 -07001975 WPASupplicant::kNetworkPropertyBgscan,
Christopher Wiley5c929e22012-07-16 14:06:04 -07001976 &config_string));
1977 }
Darin Petkov4a66cc52012-06-15 10:08:29 +02001978}
1979
mukesh agrawalc01f3982012-01-24 13:48:39 -08001980TEST_F(WiFiMainTest, StateAndIPIgnoreLinkEvent) {
1981 StartWiFi();
Paul Stewart3c504012013-01-17 17:49:58 -08001982 MockWiFiServiceRefPtr service(
1983 SetupConnectingService(DBus::Path(), NULL, NULL));
mukesh agrawalc01f3982012-01-24 13:48:39 -08001984 EXPECT_CALL(*service.get(), SetState(_)).Times(0);
1985 EXPECT_CALL(*dhcp_config_.get(), RequestIP()).Times(0);
1986 ReportLinkUp();
1987
1988 // Verify expectations now, because WiFi may cause |service| state
1989 // changes during TearDown().
1990 Mock::VerifyAndClearExpectations(service);
mukesh agrawalc01f3982012-01-24 13:48:39 -08001991}
1992
1993TEST_F(WiFiMainTest, SupplicantCompletedAlreadyConnected) {
mukesh agrawalc01f3982012-01-24 13:48:39 -08001994 StartWiFi();
Paul Stewart3c504012013-01-17 17:49:58 -08001995 MockWiFiServiceRefPtr service(
1996 SetupConnectedService(DBus::Path(), NULL, NULL));
Christopher Wiley8f81e2a2012-10-17 16:51:32 -07001997 Mock::VerifyAndClearExpectations(dhcp_config_.get());
mukesh agrawalc01f3982012-01-24 13:48:39 -08001998 EXPECT_CALL(*dhcp_config_.get(), RequestIP()).Times(0);
Christopher Wiley8f81e2a2012-10-17 16:51:32 -07001999 // Simulate a rekeying event from the AP. These show as transitions from
2000 // completed->completed from wpa_supplicant.
Paul Stewart0654ece2013-03-26 15:21:26 -07002001 ReportStateChanged(WPASupplicant::kInterfaceStateCompleted);
Christopher Wiley5519e9e2013-01-08 16:55:56 -08002002 // When we get an IP, WiFi should enable high bitrates on the interface again.
Paul Stewart3c504012013-01-17 17:49:58 -08002003 Mock::VerifyAndClearExpectations(GetSupplicantInterfaceProxy());
2004 EXPECT_CALL(*GetSupplicantInterfaceProxy(), EnableHighBitrates()).Times(1);
2005 EXPECT_CALL(*manager(), device_info()).WillOnce(Return(device_info()));
Prathmesh Prabhuba99b592013-04-17 15:13:14 -07002006 string portal_url;
2007 EXPECT_CALL(*manager(), GetPortalCheckURL()).WillOnce(ReturnRef(portal_url));
Christopher Wiley8f81e2a2012-10-17 16:51:32 -07002008 ReportIPConfigComplete();
2009 // Similarly, rekeying events after we have an IP don't trigger L3
Christopher Wiley5519e9e2013-01-08 16:55:56 -08002010 // configuration. However, we treat all transitions to completed as potential
2011 // reassociations, so we will reenable high rates again here.
Paul Stewart3c504012013-01-17 17:49:58 -08002012 Mock::VerifyAndClearExpectations(GetSupplicantInterfaceProxy());
2013 EXPECT_CALL(*service, IsConnected()).WillOnce(Return(true));
2014 EXPECT_CALL(*GetSupplicantInterfaceProxy(), EnableHighBitrates()).Times(1);
Paul Stewart0654ece2013-03-26 15:21:26 -07002015 ReportStateChanged(WPASupplicant::kInterfaceStateCompleted);
mukesh agrawalc01f3982012-01-24 13:48:39 -08002016}
2017
mukesh agrawalb20776f2012-02-10 16:00:36 -08002018TEST_F(WiFiMainTest, BSSAddedCreatesBSSProxy) {
mukesh agrawalb20776f2012-02-10 16:00:36 -08002019 // TODO(quiche): Consider using a factory for WiFiEndpoints, so that
2020 // we can test the interaction between WiFi and WiFiEndpoint. (Right
2021 // now, we're testing across multiple layers.)
2022 EXPECT_CALL(*supplicant_bss_proxy_, Die()).Times(AnyNumber());
2023 EXPECT_CALL(*proxy_factory(), CreateSupplicantBSSProxy(_, _, _));
2024 StartWiFi();
2025 ReportBSS("bss0", "ssid0", "00:00:00:00:00:00", 0, 0, kNetworkModeAdHoc);
2026}
2027
2028TEST_F(WiFiMainTest, BSSRemovedDestroysBSSProxy) {
2029 // TODO(quiche): As for BSSAddedCreatesBSSProxy, consider using a
2030 // factory for WiFiEndpoints.
mukesh agrawalb20776f2012-02-10 16:00:36 -08002031 // Get the pointer before we transfer ownership.
2032 MockSupplicantBSSProxy *proxy = supplicant_bss_proxy_.get();
2033 EXPECT_CALL(*proxy, Die());
2034 StartWiFi();
Paul Stewart3c504012013-01-17 17:49:58 -08002035 DBus::Path bss_path(
2036 MakeNewEndpointAndService(0, 0, kNetworkModeAdHoc, NULL, NULL));
2037 EXPECT_CALL(*wifi_provider(), OnEndpointRemoved(_))
2038 .WillOnce(Return(reinterpret_cast<WiFiService *>(NULL)));
2039 RemoveBSS(bss_path);
mukesh agrawalb20776f2012-02-10 16:00:36 -08002040 // Check this now, to make sure RemoveBSS killed the proxy (rather
2041 // than TearDown).
2042 Mock::VerifyAndClearExpectations(proxy);
2043}
2044
mukesh agrawal5c05b292012-03-07 10:12:52 -08002045TEST_F(WiFiMainTest, FlushBSSOnResume) {
2046 const struct timeval resume_time = {1, 0};
2047 const struct timeval scan_done_time = {6, 0};
mukesh agrawal5c05b292012-03-07 10:12:52 -08002048
2049 StartWiFi();
2050
2051 EXPECT_CALL(time_, GetTimeMonotonic(_))
2052 .WillOnce(DoAll(SetArgumentPointee<0>(resume_time), Return(0)))
2053 .WillOnce(DoAll(SetArgumentPointee<0>(scan_done_time), Return(0)));
Paul Stewart3c504012013-01-17 17:49:58 -08002054 EXPECT_CALL(*GetSupplicantInterfaceProxy(),
mukesh agrawal5c05b292012-03-07 10:12:52 -08002055 FlushBSS(WiFi::kMaxBSSResumeAgeSeconds + 5));
mukesh agrawal2f9df4e2012-08-08 12:29:20 -07002056 OnAfterResume();
mukesh agrawal5c05b292012-03-07 10:12:52 -08002057 ReportScanDone();
2058}
2059
Wade Guthrie5a4e2ef2013-04-30 12:51:39 -07002060TEST_F(WiFiMainTest, ScanTimerIdle_FullScan) {
2061 EnableFullScan();
mukesh agrawalb66c6462012-05-07 11:45:25 -07002062 StartWiFi();
2063 dispatcher_.DispatchPendingEvents();
2064 ReportScanDone();
2065 CancelScanTimer();
2066 EXPECT_TRUE(GetScanTimer().IsCancelled());
2067
2068 EXPECT_CALL(*GetSupplicantInterfaceProxy(), Scan(_));
2069 FireScanTimer();
2070 dispatcher_.DispatchPendingEvents();
2071 EXPECT_FALSE(GetScanTimer().IsCancelled()); // Automatically re-armed.
2072}
2073
Wade Guthrie5a4e2ef2013-04-30 12:51:39 -07002074TEST_F(WiFiMainTest, ScanTimerIdle) {
2075 StartWiFi();
2076 dispatcher_.DispatchPendingEvents();
2077 ReportScanDone();
2078 CancelScanTimer();
2079 EXPECT_TRUE(GetScanTimer().IsCancelled());
2080 EXPECT_CALL(*scan_session_, InitiateScan());
2081 FireScanTimer();
2082 dispatcher_.DispatchPendingEvents();
2083 EXPECT_FALSE(GetScanTimer().IsCancelled()); // Automatically re-armed.
2084}
2085
mukesh agrawalb66c6462012-05-07 11:45:25 -07002086TEST_F(WiFiMainTest, ScanTimerScanning) {
2087 StartWiFi();
2088 dispatcher_.DispatchPendingEvents();
2089 CancelScanTimer();
2090 EXPECT_TRUE(GetScanTimer().IsCancelled());
2091
2092 // Should not call Scan, since we're already scanning.
2093 // (Scanning is triggered by StartWiFi.)
2094 EXPECT_CALL(*GetSupplicantInterfaceProxy(), Scan(_)).Times(0);
Wade Guthrie5a4e2ef2013-04-30 12:51:39 -07002095 EXPECT_CALL(*scan_session_, InitiateScan()).Times(0);
mukesh agrawalb66c6462012-05-07 11:45:25 -07002096 FireScanTimer();
2097 dispatcher_.DispatchPendingEvents();
2098 EXPECT_FALSE(GetScanTimer().IsCancelled()); // Automatically re-armed.
2099}
2100
2101TEST_F(WiFiMainTest, ScanTimerConnecting) {
2102 StartWiFi();
2103 dispatcher_.DispatchPendingEvents();
Paul Stewart3c504012013-01-17 17:49:58 -08002104 MockWiFiServiceRefPtr service =
2105 SetupConnectingService(DBus::Path(), NULL, NULL);
mukesh agrawalb66c6462012-05-07 11:45:25 -07002106 CancelScanTimer();
2107 EXPECT_TRUE(GetScanTimer().IsCancelled());
2108
2109 EXPECT_CALL(*GetSupplicantInterfaceProxy(), Scan(_)).Times(0);
Wade Guthrie5a4e2ef2013-04-30 12:51:39 -07002110 EXPECT_CALL(*scan_session_, InitiateScan()).Times(0);
mukesh agrawalb66c6462012-05-07 11:45:25 -07002111 FireScanTimer();
2112 dispatcher_.DispatchPendingEvents();
2113 EXPECT_FALSE(GetScanTimer().IsCancelled()); // Automatically re-armed.
2114}
2115
2116TEST_F(WiFiMainTest, ScanTimerReconfigured) {
2117 StartWiFi();
2118 CancelScanTimer();
2119 EXPECT_TRUE(GetScanTimer().IsCancelled());
2120
mukesh agrawalbebf1b82013-04-23 15:06:33 -07002121 SetScanInterval(1, NULL);
mukesh agrawalb66c6462012-05-07 11:45:25 -07002122 EXPECT_FALSE(GetScanTimer().IsCancelled());
2123}
2124
2125TEST_F(WiFiMainTest, ScanTimerResetOnScanDone) {
2126 StartWiFi();
2127 CancelScanTimer();
2128 EXPECT_TRUE(GetScanTimer().IsCancelled());
2129
2130 ReportScanDone();
2131 EXPECT_FALSE(GetScanTimer().IsCancelled());
2132}
2133
2134TEST_F(WiFiMainTest, ScanTimerStopOnZeroInterval) {
2135 StartWiFi();
2136 EXPECT_FALSE(GetScanTimer().IsCancelled());
2137
mukesh agrawalbebf1b82013-04-23 15:06:33 -07002138 SetScanInterval(0, NULL);
mukesh agrawalb66c6462012-05-07 11:45:25 -07002139 EXPECT_TRUE(GetScanTimer().IsCancelled());
2140}
2141
Wade Guthrie5a4e2ef2013-04-30 12:51:39 -07002142TEST_F(WiFiMainTest, ScanOnDisconnectWithHidden_FullScan) {
2143 EnableFullScan();
mukesh agrawalb66c6462012-05-07 11:45:25 -07002144 StartWiFi();
mukesh agrawalb66c6462012-05-07 11:45:25 -07002145 dispatcher_.DispatchPendingEvents();
Paul Stewart3c504012013-01-17 17:49:58 -08002146 SetupConnectedService(DBus::Path(), NULL, NULL);
2147 vector<uint8_t>kSSID(1, 'a');
2148 ByteArrays ssids;
2149 ssids.push_back(kSSID);
2150 EXPECT_CALL(*wifi_provider(), GetHiddenSSIDList())
2151 .WillRepeatedly(Return(ssids));
Wade Guthrie5a4e2ef2013-04-30 12:51:39 -07002152 EXPECT_CALL(*GetSupplicantInterfaceProxy(),
2153 Scan(HasHiddenSSID_FullScan(kSSID)));
2154 ReportCurrentBSSChanged(WPASupplicant::kCurrentBSSNull);
2155 dispatcher_.DispatchPendingEvents();
2156}
2157
2158TEST_F(WiFiMainTest, ScanOnDisconnectWithHidden) {
2159 StartWiFi();
2160 dispatcher_.DispatchPendingEvents();
2161 ReportScanDone();
2162 SetupConnectedService(DBus::Path(), NULL, NULL);
2163 vector<uint8_t>kSSID(1, 'a');
2164 ByteArrays ssids;
2165 ssids.push_back(kSSID);
2166 EXPECT_CALL(*wifi_provider(), GetHiddenSSIDList())
2167 .WillRepeatedly(Return(ssids));
2168 EXPECT_CALL(*scan_session_, InitiateScan());
Paul Stewart0654ece2013-03-26 15:21:26 -07002169 ReportCurrentBSSChanged(WPASupplicant::kCurrentBSSNull);
mukesh agrawalb66c6462012-05-07 11:45:25 -07002170 dispatcher_.DispatchPendingEvents();
2171}
2172
2173TEST_F(WiFiMainTest, NoScanOnDisconnectWithoutHidden) {
2174 StartWiFi();
mukesh agrawalb66c6462012-05-07 11:45:25 -07002175 dispatcher_.DispatchPendingEvents();
Paul Stewart3c504012013-01-17 17:49:58 -08002176 SetupConnectedService(DBus::Path(), NULL, NULL);
mukesh agrawalb66c6462012-05-07 11:45:25 -07002177 EXPECT_CALL(*GetSupplicantInterfaceProxy(), Scan(_)).Times(0);
Wade Guthrie5a4e2ef2013-04-30 12:51:39 -07002178 EXPECT_CALL(*scan_session_, InitiateScan()).Times(0);
Paul Stewart3c504012013-01-17 17:49:58 -08002179 EXPECT_CALL(*wifi_provider(), GetHiddenSSIDList())
2180 .WillRepeatedly(Return(ByteArrays()));
Paul Stewart0654ece2013-03-26 15:21:26 -07002181 ReportCurrentBSSChanged(WPASupplicant::kCurrentBSSNull);
mukesh agrawalb66c6462012-05-07 11:45:25 -07002182 dispatcher_.DispatchPendingEvents();
2183}
2184
Paul Stewart3c508e12012-08-09 11:40:06 -07002185TEST_F(WiFiMainTest, LinkMonitorFailure) {
2186 StartWiFi();
2187 ScopedMockLog log;
2188 EXPECT_CALL(log, Log(_, _, _)).Times(AnyNumber());
2189 MockLinkMonitor *link_monitor = new StrictMock<MockLinkMonitor>();
2190 SetLinkMonitor(link_monitor);
2191 EXPECT_CALL(*link_monitor, IsGatewayFound())
2192 .WillOnce(Return(false))
2193 .WillRepeatedly(Return(true));
2194 EXPECT_CALL(log, Log(logging::LOG_INFO, _,
2195 EndsWith("gateway was never found."))).Times(1);
2196 EXPECT_CALL(*GetSupplicantInterfaceProxy(), Reassociate()).Times(0);
2197 OnLinkMonitorFailure();
2198 EXPECT_CALL(log, Log(logging::LOG_INFO, _,
2199 EndsWith("Called Reassociate()."))).Times(1);
2200 EXPECT_CALL(*GetSupplicantInterfaceProxy(), Reassociate()).Times(1);
2201 OnLinkMonitorFailure();
2202 OnSupplicantVanish();
Paul Stewart3c504012013-01-17 17:49:58 -08002203 Mock::VerifyAndClearExpectations(GetSupplicantInterfaceProxy());
2204 EXPECT_CALL(*GetSupplicantInterfaceProxy(), Reassociate()).Times(0);
Paul Stewart3c508e12012-08-09 11:40:06 -07002205 EXPECT_CALL(log, Log(logging::LOG_ERROR, _,
2206 EndsWith("Cannot reassociate."))).Times(1);
2207 OnLinkMonitorFailure();
2208}
2209
mukesh agrawalcf24a242012-05-21 16:46:11 -07002210TEST_F(WiFiMainTest, SuspectCredentialsOpen) {
Paul Stewart3c504012013-01-17 17:49:58 -08002211 MockWiFiServiceRefPtr service = MakeMockService(flimflam::kSecurityNone);
Paul Stewart0654ece2013-03-26 15:21:26 -07002212 ReportStateChanged(WPASupplicant::kInterfaceState4WayHandshake);
mukesh agrawalcf24a242012-05-21 16:46:11 -07002213 EXPECT_FALSE(service->has_ever_connected());
Paul Stewart1369c2b2013-01-11 05:41:26 -08002214 EXPECT_FALSE(SuspectCredentials(*service, NULL));
mukesh agrawalcf24a242012-05-21 16:46:11 -07002215}
2216
2217TEST_F(WiFiMainTest, SuspectCredentialsWPANeverConnected) {
Paul Stewart3c504012013-01-17 17:49:58 -08002218 MockWiFiServiceRefPtr service = MakeMockService(flimflam::kSecurityWpa);
Paul Stewart0654ece2013-03-26 15:21:26 -07002219 ReportStateChanged(WPASupplicant::kInterfaceState4WayHandshake);
mukesh agrawalcf24a242012-05-21 16:46:11 -07002220 EXPECT_FALSE(service->has_ever_connected());
Paul Stewart1369c2b2013-01-11 05:41:26 -08002221 Service::ConnectFailure failure;
2222 EXPECT_TRUE(SuspectCredentials(*service, &failure));
2223 EXPECT_EQ(Service::kFailureBadPassphrase, failure);
mukesh agrawalcf24a242012-05-21 16:46:11 -07002224}
2225
2226TEST_F(WiFiMainTest, SuspectCredentialsWPAPreviouslyConnected) {
Paul Stewart3c504012013-01-17 17:49:58 -08002227 MockWiFiServiceRefPtr service = MakeMockService(flimflam::kSecurityWpa);
Paul Stewart0654ece2013-03-26 15:21:26 -07002228 ReportStateChanged(WPASupplicant::kInterfaceState4WayHandshake);
mukesh agrawalcf24a242012-05-21 16:46:11 -07002229 service->has_ever_connected_ = true;
Paul Stewart1369c2b2013-01-11 05:41:26 -08002230 EXPECT_FALSE(SuspectCredentials(*service, NULL));
mukesh agrawalcf24a242012-05-21 16:46:11 -07002231}
2232
Paul Stewart1369c2b2013-01-11 05:41:26 -08002233TEST_F(WiFiMainTest, SuspectCredentialsEAPInProgress) {
Paul Stewart3c504012013-01-17 17:49:58 -08002234 MockWiFiServiceRefPtr service = MakeMockService(flimflam::kSecurity8021x);
Paul Stewart735eab52013-03-29 09:19:23 -07002235 EXPECT_CALL(*eap_state_handler_, is_eap_in_progress())
2236 .WillOnce(Return(false))
2237 .WillOnce(Return(true))
2238 .WillOnce(Return(false))
2239 .WillOnce(Return(true));
Paul Stewart1369c2b2013-01-11 05:41:26 -08002240 service->has_ever_connected_ = false;
2241 EXPECT_FALSE(SuspectCredentials(*service, NULL));
Paul Stewart1369c2b2013-01-11 05:41:26 -08002242 Service::ConnectFailure failure;
2243 EXPECT_TRUE(SuspectCredentials(*service, &failure));
2244 EXPECT_EQ(Service::kFailureEAPAuthentication, failure);
Paul Stewart735eab52013-03-29 09:19:23 -07002245 service->has_ever_connected_ = true;
2246 EXPECT_FALSE(SuspectCredentials(*service, NULL));
Paul Stewart1369c2b2013-01-11 05:41:26 -08002247 EXPECT_FALSE(SuspectCredentials(*service, NULL));
2248}
2249
2250TEST_F(WiFiMainTest, SuspectCredentialsYieldFailureWPA) {
mukesh agrawalcf24a242012-05-21 16:46:11 -07002251 MockWiFiServiceRefPtr service = MakeMockService(flimflam::kSecurityWpa);
2252 SetPendingService(service);
Paul Stewart0654ece2013-03-26 15:21:26 -07002253 ReportStateChanged(WPASupplicant::kInterfaceState4WayHandshake);
mukesh agrawalcf24a242012-05-21 16:46:11 -07002254 EXPECT_FALSE(service->has_ever_connected());
2255
Paul Stewartf2d60912012-07-15 08:37:30 -07002256 EXPECT_CALL(*service, SetFailure(Service::kFailureBadPassphrase));
mukesh agrawal56e32202012-07-26 16:32:11 -07002257 EXPECT_CALL(*service, SetFailureSilent(_)).Times(0);
2258 EXPECT_CALL(*service, SetState(_)).Times(0);
Paul Stewart735eab52013-03-29 09:19:23 -07002259 ScopedMockLog log;
mukesh agrawalcf24a242012-05-21 16:46:11 -07002260 EXPECT_CALL(log, Log(_, _, _)).Times(AnyNumber());
Paul Stewart1369c2b2013-01-11 05:41:26 -08002261 EXPECT_CALL(log, Log(logging::LOG_ERROR, _,
2262 EndsWith(flimflam::kErrorBadPassphrase)));
Paul Stewart0654ece2013-03-26 15:21:26 -07002263 ReportCurrentBSSChanged(WPASupplicant::kCurrentBSSNull);
Paul Stewart1369c2b2013-01-11 05:41:26 -08002264}
2265
2266TEST_F(WiFiMainTest, SuspectCredentialsYieldFailureEAP) {
Paul Stewart1369c2b2013-01-11 05:41:26 -08002267 MockWiFiServiceRefPtr service = MakeMockService(flimflam::kSecurity8021x);
2268 SetCurrentService(service);
Paul Stewart1369c2b2013-01-11 05:41:26 -08002269 EXPECT_FALSE(service->has_ever_connected());
2270
Paul Stewart735eab52013-03-29 09:19:23 -07002271 ScopedMockLog log;
2272 EXPECT_CALL(log, Log(_, _, _)).Times(AnyNumber());
Paul Stewart1369c2b2013-01-11 05:41:26 -08002273 EXPECT_CALL(*service, SetFailureSilent(_)).Times(0);
2274 EXPECT_CALL(*service, SetState(_)).Times(0);
Paul Stewart735eab52013-03-29 09:19:23 -07002275 // Ensure that we retrieve is_eap_in_progress() before resetting the
2276 // EAP handler's state.
2277 InSequence seq;
2278 EXPECT_CALL(*eap_state_handler_, is_eap_in_progress())
2279 .WillOnce(Return(true));
2280 EXPECT_CALL(*service, SetFailure(Service::kFailureEAPAuthentication));
Paul Stewart1369c2b2013-01-11 05:41:26 -08002281 EXPECT_CALL(log, Log(logging::LOG_ERROR, _,
2282 EndsWith(shill::kErrorEapAuthenticationFailed)));
Paul Stewart735eab52013-03-29 09:19:23 -07002283 EXPECT_CALL(*eap_state_handler_, Reset());
Paul Stewart0654ece2013-03-26 15:21:26 -07002284 ReportCurrentBSSChanged(WPASupplicant::kCurrentBSSNull);
mukesh agrawalcf24a242012-05-21 16:46:11 -07002285}
2286
Paul Stewarte369ece2012-05-22 09:11:03 -07002287// Scanning tests will use a mock of the event dispatcher instead of a real
2288// one.
Paul Stewart1aff7302012-08-04 20:04:47 -07002289class WiFiTimerTest : public WiFiObjectTest {
Paul Stewarte369ece2012-05-22 09:11:03 -07002290 public:
Paul Stewart1aff7302012-08-04 20:04:47 -07002291 WiFiTimerTest() : WiFiObjectTest(&mock_dispatcher_) {}
Paul Stewarte369ece2012-05-22 09:11:03 -07002292
2293 protected:
2294 void ExpectInitialScanSequence();
2295
2296 StrictMock<MockEventDispatcher> mock_dispatcher_;
2297};
2298
Paul Stewart1aff7302012-08-04 20:04:47 -07002299void WiFiTimerTest::ExpectInitialScanSequence() {
Paul Stewarte369ece2012-05-22 09:11:03 -07002300 // Choose a number of iterations some multiple higher than the fast scan
2301 // count.
2302 const int kScanTimes = WiFi::kNumFastScanAttempts * 4;
2303
2304 // Each time we call FireScanTimer() below, WiFi will post a task to actually
2305 // run Scan() on the wpa_supplicant proxy.
2306 EXPECT_CALL(mock_dispatcher_, PostTask(_))
2307 .Times(kScanTimes);
2308 {
2309 InSequence seq;
2310 // The scans immediately after the initial scan should happen at the short
2311 // interval. If we add the initial scan (not invoked in this function) to
2312 // the ones in the expectation below, we get WiFi::kNumFastScanAttempts at
2313 // the fast scan interval.
2314 EXPECT_CALL(mock_dispatcher_, PostDelayedTask(
2315 _, WiFi::kFastScanIntervalSeconds * 1000))
2316 .Times(WiFi::kNumFastScanAttempts - 1)
2317 .WillRepeatedly(Return(true));
2318
2319 // After this, the WiFi device should use the normal scan interval.
2320 EXPECT_CALL(mock_dispatcher_, PostDelayedTask(
2321 _, GetScanInterval() * 1000))
2322 .Times(kScanTimes - WiFi::kNumFastScanAttempts + 1)
2323 .WillRepeatedly(Return(true));
2324
2325 for (int i = 0; i < kScanTimes; i++) {
2326 FireScanTimer();
2327 }
2328 }
2329}
2330
Paul Stewart1aff7302012-08-04 20:04:47 -07002331TEST_F(WiFiTimerTest, FastRescan) {
Paul Stewarte369ece2012-05-22 09:11:03 -07002332 // This PostTask is a result of the call to Scan(NULL), and is meant to
2333 // post a task to call Scan() on the wpa_supplicant proxy immediately.
2334 EXPECT_CALL(mock_dispatcher_, PostTask(_));
2335 EXPECT_CALL(mock_dispatcher_, PostDelayedTask(
2336 _, WiFi::kFastScanIntervalSeconds * 1000))
2337 .WillOnce(Return(true));
2338 StartWiFi();
2339
2340 ExpectInitialScanSequence();
2341
2342 // If we end up disconnecting, the sequence should repeat.
2343 EXPECT_CALL(mock_dispatcher_, PostDelayedTask(
2344 _, WiFi::kFastScanIntervalSeconds * 1000))
2345 .WillOnce(Return(true));
2346 RestartFastScanAttempts();
2347
2348 ExpectInitialScanSequence();
2349}
2350
Paul Stewart1aff7302012-08-04 20:04:47 -07002351TEST_F(WiFiTimerTest, ReconnectTimer) {
2352 EXPECT_CALL(mock_dispatcher_, PostTask(_)).Times(AnyNumber());
2353 EXPECT_CALL(mock_dispatcher_, PostDelayedTask(_, _)).Times(AnyNumber());
Paul Stewart3c504012013-01-17 17:49:58 -08002354 StartWiFi();
2355 SetupConnectedService(DBus::Path(), NULL, NULL);
Paul Stewart1aff7302012-08-04 20:04:47 -07002356 Mock::VerifyAndClearExpectations(&mock_dispatcher_);
2357
2358 EXPECT_CALL(mock_dispatcher_, PostDelayedTask(
2359 _, GetReconnectTimeoutSeconds() * 1000)).Times(1);
2360 StartReconnectTimer();
2361 Mock::VerifyAndClearExpectations(&mock_dispatcher_);
2362 StopReconnectTimer();
2363
2364 EXPECT_CALL(mock_dispatcher_, PostDelayedTask(
2365 _, GetReconnectTimeoutSeconds() * 1000)).Times(1);
2366 StartReconnectTimer();
2367 Mock::VerifyAndClearExpectations(&mock_dispatcher_);
2368 GetReconnectTimeoutCallback().callback().Run();
2369
2370 EXPECT_CALL(mock_dispatcher_, PostDelayedTask(
2371 _, GetReconnectTimeoutSeconds() * 1000)).Times(1);
2372 StartReconnectTimer();
2373 Mock::VerifyAndClearExpectations(&mock_dispatcher_);
2374
2375 EXPECT_CALL(mock_dispatcher_, PostDelayedTask(
2376 _, GetReconnectTimeoutSeconds() * 1000)).Times(0);
2377 StartReconnectTimer();
2378}
2379
Paul Stewartbc6e7392012-05-24 07:07:48 -07002380TEST_F(WiFiMainTest, EAPCertification) {
2381 MockWiFiServiceRefPtr service = MakeMockService(flimflam::kSecurity8021x);
2382 EXPECT_CALL(*service, AddEAPCertification(_, _)).Times(0);
2383
2384 ScopedMockLog log;
2385 EXPECT_CALL(log, Log(logging::LOG_ERROR, _, EndsWith("no current service.")));
2386 map<string, ::DBus::Variant> args;
2387 ReportCertification(args);
2388 Mock::VerifyAndClearExpectations(&log);
2389
2390 SetCurrentService(service);
2391 EXPECT_CALL(log, Log(logging::LOG_ERROR, _, EndsWith("no depth parameter.")));
2392 ReportCertification(args);
2393 Mock::VerifyAndClearExpectations(&log);
2394
2395 const uint32 kDepth = 123;
Paul Stewart0654ece2013-03-26 15:21:26 -07002396 args[WPASupplicant::kInterfacePropertyDepth].writer().append_uint32(kDepth);
Paul Stewartbc6e7392012-05-24 07:07:48 -07002397
2398 EXPECT_CALL(log,
2399 Log(logging::LOG_ERROR, _, EndsWith("no subject parameter.")));
2400 ReportCertification(args);
2401 Mock::VerifyAndClearExpectations(&log);
2402
2403 const string kSubject("subject");
Paul Stewart0654ece2013-03-26 15:21:26 -07002404 args[WPASupplicant::kInterfacePropertySubject].writer()
Paul Stewartbc6e7392012-05-24 07:07:48 -07002405 .append_string(kSubject.c_str());
2406 EXPECT_CALL(*service, AddEAPCertification(kSubject, kDepth)).Times(1);
2407 ReportCertification(args);
2408}
2409
Paul Stewartdb0f9172012-11-30 16:48:09 -08002410TEST_F(WiFiMainTest, EAPEvent) {
Paul Stewartdb0f9172012-11-30 16:48:09 -08002411 ScopedMockLog log;
2412 EXPECT_CALL(log, Log(logging::LOG_ERROR, _, EndsWith("no current service.")));
Paul Stewart735eab52013-03-29 09:19:23 -07002413 EXPECT_CALL(*eap_state_handler_, ParseStatus(_, _, _)).Times(0);
2414 const string kEAPStatus("eap-status");
2415 const string kEAPParameter("eap-parameter");
2416 ReportEAPEvent(kEAPStatus, kEAPParameter);
Paul Stewartdb0f9172012-11-30 16:48:09 -08002417 Mock::VerifyAndClearExpectations(&log);
2418 EXPECT_CALL(log, Log(_, _, _)).Times(AnyNumber());
2419
Paul Stewart735eab52013-03-29 09:19:23 -07002420 MockWiFiServiceRefPtr service = MakeMockService(flimflam::kSecurity8021x);
Paul Stewartdb0f9172012-11-30 16:48:09 -08002421 EXPECT_CALL(*service, SetFailure(_)).Times(0);
Paul Stewart735eab52013-03-29 09:19:23 -07002422 EXPECT_CALL(*eap_state_handler_, ParseStatus(kEAPStatus, kEAPParameter, _));
Paul Stewartdb0f9172012-11-30 16:48:09 -08002423 SetCurrentService(service);
Paul Stewart735eab52013-03-29 09:19:23 -07002424 ReportEAPEvent(kEAPStatus, kEAPParameter);
2425 Mock::VerifyAndClearExpectations(service);
2426 Mock::VerifyAndClearExpectations(eap_state_handler_);
Paul Stewartdb0f9172012-11-30 16:48:09 -08002427
Paul Stewart735eab52013-03-29 09:19:23 -07002428 EXPECT_CALL(*eap_state_handler_, ParseStatus(kEAPStatus, kEAPParameter, _))
2429 .WillOnce(DoAll(SetArgumentPointee<2>(Service::kFailureOutOfRange),
2430 Return(false)));
2431 EXPECT_CALL(*service, DisconnectWithFailure(Service::kFailureOutOfRange, _));
2432 ReportEAPEvent(kEAPStatus, kEAPParameter);
Paul Stewartdb0f9172012-11-30 16:48:09 -08002433}
2434
mukesh agrawalc4f368f2012-06-04 19:45:52 -07002435TEST_F(WiFiMainTest, PendingScanDoesNotCrashAfterStop) {
2436 // Scan is one task that should be skipped after Stop. Others are
2437 // skipped by the same mechanism (invalidating weak pointers), so we
2438 // don't test them individually.
2439 //
2440 // Note that we can't test behavior by setting expectations on the
2441 // supplicant_interface_proxy_, since that is destroyed when we StopWiFi().
2442 StartWiFi();
2443 StopWiFi();
2444 dispatcher_.DispatchPendingEvents();
2445}
2446
Darin Petkov60ceaf32012-10-18 10:36:01 +02002447TEST_F(WiFiMainTest, VerifyPaths) {
Paul Stewart196f50f2013-03-27 18:02:11 -07002448 string path(WPASupplicant::kSupplicantConfPath);
Darin Petkov60ceaf32012-10-18 10:36:01 +02002449 TrimString(path, FilePath::kSeparators, &path);
2450 EXPECT_TRUE(file_util::PathExists(FilePath(SYSROOT).Append(path)));
2451}
2452
Gaurav Shah6d2c72d2012-10-16 16:30:44 -07002453struct BSS {
2454 string bsspath;
2455 string ssid;
2456 string bssid;
2457 int16_t signal_strength;
2458 uint16 frequency;
2459 const char* mode;
2460};
2461
2462TEST_F(WiFiMainTest, GetGeolocationObjects) {
2463 BSS bsses[] = {
2464 {"bssid1", "ssid1", "00:00:00:00:00:00", 5, Metrics::kWiFiFrequency2412,
2465 kNetworkModeInfrastructure},
2466 {"bssid2", "ssid2", "01:00:00:00:00:00", 30, Metrics::kWiFiFrequency5170,
2467 kNetworkModeInfrastructure},
2468 // Same SSID but different BSSID is an additional geolocation object.
2469 {"bssid3", "ssid1", "02:00:00:00:00:00", 100, 0,
2470 kNetworkModeInfrastructure}
2471 };
2472 StartWiFi();
2473 vector<GeolocationInfo> objects;
2474 EXPECT_EQ(objects.size(), 0);
2475
2476 for (size_t i = 0; i < arraysize(bsses); ++i) {
2477 ReportBSS(bsses[i].bsspath, bsses[i].ssid, bsses[i].bssid,
2478 bsses[i].signal_strength, bsses[i].frequency, bsses[i].mode);
2479 objects = wifi()->GetGeolocationObjects();
2480 EXPECT_EQ(objects.size(), i + 1);
2481
2482 GeolocationInfo expected_info;
2483 expected_info.AddField(kGeoMacAddressProperty, bsses[i].bssid);
2484 expected_info.AddField(kGeoSignalStrengthProperty,
2485 StringPrintf("%d", bsses[i].signal_strength));
2486 expected_info.AddField(kGeoChannelProperty, StringPrintf(
2487 "%d", Metrics::WiFiFrequencyToChannel(bsses[i].frequency)));
2488 EXPECT_TRUE(objects[i].Equals(expected_info));
Wade Guthrie7347bf22013-04-30 11:21:51 -07002489 }
2490}
Gaurav Shah6d2c72d2012-10-16 16:30:44 -07002491
Paul Stewart5581d072012-12-17 17:30:20 -08002492TEST_F(WiFiMainTest, SetSupplicantDebugLevel) {
2493 MockSupplicantProcessProxy *process_proxy = supplicant_process_proxy_.get();
2494
2495 // With WiFi not yet started, nothing interesting (including a crash) should
2496 // happen.
2497 EXPECT_CALL(*process_proxy, GetDebugLevel()).Times(0);
2498 EXPECT_CALL(*process_proxy, SetDebugLevel(_)).Times(0);
2499 ReportWiFiDebugScopeChanged(true);
2500
2501 // This unit test turns on WiFi debugging, so when we start WiFi, we should
2502 // check but not set the debug level if we return the "debug" level.
2503 EXPECT_CALL(*process_proxy, GetDebugLevel())
Paul Stewart0654ece2013-03-26 15:21:26 -07002504 .WillOnce(Return(WPASupplicant::kDebugLevelDebug));
Paul Stewart5581d072012-12-17 17:30:20 -08002505 EXPECT_CALL(*process_proxy, SetDebugLevel(_)).Times(0);
2506 StartWiFi();
2507 Mock::VerifyAndClearExpectations(process_proxy);
2508
2509 // If WiFi debugging is toggled and wpa_supplicant reports debugging
2510 // is set to some unmanaged level, WiFi should leave it alone.
2511 EXPECT_CALL(*process_proxy, GetDebugLevel())
Paul Stewart0654ece2013-03-26 15:21:26 -07002512 .WillOnce(Return(WPASupplicant::kDebugLevelError))
2513 .WillOnce(Return(WPASupplicant::kDebugLevelError))
2514 .WillOnce(Return(WPASupplicant::kDebugLevelExcessive))
2515 .WillOnce(Return(WPASupplicant::kDebugLevelExcessive))
2516 .WillOnce(Return(WPASupplicant::kDebugLevelMsgDump))
2517 .WillOnce(Return(WPASupplicant::kDebugLevelMsgDump))
2518 .WillOnce(Return(WPASupplicant::kDebugLevelWarning))
2519 .WillOnce(Return(WPASupplicant::kDebugLevelWarning));
Paul Stewart5581d072012-12-17 17:30:20 -08002520 EXPECT_CALL(*process_proxy, SetDebugLevel(_)).Times(0);
2521 ReportWiFiDebugScopeChanged(true);
2522 ReportWiFiDebugScopeChanged(false);
2523 ReportWiFiDebugScopeChanged(true);
2524 ReportWiFiDebugScopeChanged(false);
2525 ReportWiFiDebugScopeChanged(true);
2526 ReportWiFiDebugScopeChanged(false);
2527 ReportWiFiDebugScopeChanged(true);
2528 ReportWiFiDebugScopeChanged(false);
2529 Mock::VerifyAndClearExpectations(process_proxy);
2530
2531 // If WiFi debugging is turned off and wpa_supplicant reports debugging
2532 // is turned on, WiFi should turn supplicant debugging off.
2533 EXPECT_CALL(*process_proxy, GetDebugLevel())
Paul Stewart0654ece2013-03-26 15:21:26 -07002534 .WillOnce(Return(WPASupplicant::kDebugLevelDebug));
2535 EXPECT_CALL(*process_proxy, SetDebugLevel(WPASupplicant::kDebugLevelInfo))
Paul Stewart5581d072012-12-17 17:30:20 -08002536 .Times(1);
2537 ReportWiFiDebugScopeChanged(false);
2538 Mock::VerifyAndClearExpectations(process_proxy);
2539
2540 // If WiFi debugging is turned on and wpa_supplicant reports debugging
2541 // is turned off, WiFi should turn supplicant debugging on.
2542 EXPECT_CALL(*process_proxy, GetDebugLevel())
Paul Stewart0654ece2013-03-26 15:21:26 -07002543 .WillOnce(Return(WPASupplicant::kDebugLevelInfo));
2544 EXPECT_CALL(*process_proxy, SetDebugLevel(WPASupplicant::kDebugLevelDebug))
Paul Stewart5581d072012-12-17 17:30:20 -08002545 .Times(1);
2546 ReportWiFiDebugScopeChanged(true);
2547 Mock::VerifyAndClearExpectations(process_proxy);
2548
2549 // If WiFi debugging is already in the correct state, it should not be
2550 // changed.
2551 EXPECT_CALL(*process_proxy, GetDebugLevel())
Paul Stewart0654ece2013-03-26 15:21:26 -07002552 .WillOnce(Return(WPASupplicant::kDebugLevelDebug))
2553 .WillOnce(Return(WPASupplicant::kDebugLevelInfo));
Paul Stewart5581d072012-12-17 17:30:20 -08002554 EXPECT_CALL(*process_proxy, SetDebugLevel(_)).Times(0);
2555 ReportWiFiDebugScopeChanged(true);
2556 ReportWiFiDebugScopeChanged(false);
2557
2558 // After WiFi is stopped, we shouldn't be calling the proxy.
2559 EXPECT_CALL(*process_proxy, GetDebugLevel()).Times(0);
2560 EXPECT_CALL(*process_proxy, SetDebugLevel(_)).Times(0);
2561 StopWiFi();
2562 ReportWiFiDebugScopeChanged(true);
2563 ReportWiFiDebugScopeChanged(false);
2564}
2565
Darin Petkov50cb78a2013-02-06 16:17:49 +01002566TEST_F(WiFiMainTest, LogSSID) {
2567 EXPECT_EQ("[SSID=]", WiFi::LogSSID(""));
2568 EXPECT_EQ("[SSID=foo\\x5b\\x09\\x5dbar]", WiFi::LogSSID("foo[\t]bar"));
2569}
2570
mukesh agrawalbebf1b82013-04-23 15:06:33 -07002571// Custom property setters should return false, and make no changes, if
2572// the new value is the same as the old value.
2573TEST_F(WiFiMainTest, CustomSetterNoopChange) {
2574 // SetBgscanShortInterval
2575 {
2576 Error error;
2577 static const uint16 kKnownScanInterval = 4;
2578 // Set to known value.
2579 EXPECT_TRUE(SetBgscanShortInterval(kKnownScanInterval, &error));
2580 EXPECT_TRUE(error.IsSuccess());
2581 // Set to same value.
2582 EXPECT_FALSE(SetBgscanShortInterval(kKnownScanInterval, &error));
2583 EXPECT_TRUE(error.IsSuccess());
2584 }
2585
2586 // SetBgscanSignalThreshold
2587 {
2588 Error error;
2589 static const int32 kKnownSignalThreshold = 4;
2590 // Set to known value.
2591 EXPECT_TRUE(SetBgscanSignalThreshold(kKnownSignalThreshold, &error));
2592 EXPECT_TRUE(error.IsSuccess());
2593 // Set to same value.
2594 EXPECT_FALSE(SetBgscanSignalThreshold(kKnownSignalThreshold, &error));
2595 EXPECT_TRUE(error.IsSuccess());
2596 }
2597
2598 // SetScanInterval
2599 {
2600 Error error;
2601 EXPECT_FALSE(SetScanInterval(GetScanInterval(), &error));
2602 EXPECT_TRUE(error.IsSuccess());
2603 }
2604}
2605
Chris Masone853b81b2011-06-24 14:11:41 -07002606} // namespace shill