blob: 4c9e3bbf3c737ee78ee6140676b781638a971cd0 [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"
Wade Guthrie0cf3c982013-05-29 09:11:35 -070035#include "shill/mock_adaptors.h"
Darin Petkov2b8e44e2012-06-25 15:13:26 +020036#include "shill/mock_dbus_manager.h"
Chris Masone853b81b2011-06-24 14:11:41 -070037#include "shill/mock_device.h"
mukesh agrawalc01f3982012-01-24 13:48:39 -080038#include "shill/mock_device_info.h"
mukesh agrawalf2f68a52011-09-01 12:15:48 -070039#include "shill/mock_dhcp_config.h"
40#include "shill/mock_dhcp_provider.h"
Paul Stewarte369ece2012-05-22 09:11:03 -070041#include "shill/mock_event_dispatcher.h"
Paul Stewart3c508e12012-08-09 11:40:06 -070042#include "shill/mock_link_monitor.h"
mukesh agrawalcf24a242012-05-21 16:46:11 -070043#include "shill/mock_log.h"
Chris Masone2ae797d2011-08-23 20:41:00 -070044#include "shill/mock_manager.h"
Thieu Le3426c8f2012-01-11 17:35:11 -080045#include "shill/mock_metrics.h"
Wade Guthriebb9fca22013-04-10 17:21:42 -070046#include "shill/mock_netlink_manager.h"
Paul Stewart10ccbb32012-04-26 15:59:30 -070047#include "shill/mock_profile.h"
mukesh agrawal5c4dd0b2011-09-14 13:53:14 -070048#include "shill/mock_rtnl_handler.h"
Wade Guthrie5a4e2ef2013-04-30 12:51:39 -070049#include "shill/mock_scan_session.h"
Paul Stewarta41e38d2011-11-11 07:47:29 -080050#include "shill/mock_store.h"
mukesh agrawalb20776f2012-02-10 16:00:36 -080051#include "shill/mock_supplicant_bss_proxy.h"
Paul Stewart735eab52013-03-29 09:19:23 -070052#include "shill/mock_supplicant_eap_state_handler.h"
mukesh agrawal31950242011-07-14 11:53:38 -070053#include "shill/mock_supplicant_interface_proxy.h"
Paul Stewart835934a2012-12-06 19:27:09 -080054#include "shill/mock_supplicant_network_proxy.h"
mukesh agrawal31950242011-07-14 11:53:38 -070055#include "shill/mock_supplicant_process_proxy.h"
mukesh agrawal5c05b292012-03-07 10:12:52 -080056#include "shill/mock_time.h"
Paul Stewart3c504012013-01-17 17:49:58 -080057#include "shill/mock_wifi_provider.h"
mukesh agrawal7ec71312011-11-10 02:08:26 +000058#include "shill/mock_wifi_service.h"
Wade Guthrie5a4e2ef2013-04-30 12:51:39 -070059#include "shill/netlink_message_matchers.h"
mukesh agrawaldc42bb32011-07-28 10:40:26 -070060#include "shill/nice_mock_control.h"
Wade Guthrie5a4e2ef2013-04-30 12:51:39 -070061#include "shill/nl80211_message.h"
Chris Masone853b81b2011-06-24 14:11:41 -070062#include "shill/property_store_unittest.h"
mukesh agrawal31950242011-07-14 11:53:38 -070063#include "shill/proxy_factory.h"
Wade Guthrie5a4e2ef2013-04-30 12:51:39 -070064#include "shill/scan_session.h"
Paul Stewart85aea152013-01-22 09:31:56 -080065#include "shill/technology.h"
mukesh agrawal31950242011-07-14 11:53:38 -070066#include "shill/wifi_endpoint.h"
mukesh agrawal32399322011-09-01 10:53:43 -070067#include "shill/wifi_service.h"
Paul Stewartced6a0b2011-11-08 15:32:04 -080068#include "shill/wpa_supplicant.h"
Chris Masone853b81b2011-06-24 14:11:41 -070069
Gary Morainac1bdb42012-02-16 17:42:29 -080070
Albert Chaulk0e1cdea2013-02-27 15:32:55 -080071using base::FilePath;
Chris Masone853b81b2011-06-24 14:11:41 -070072using std::map;
73using std::string;
74using std::vector;
75using ::testing::_;
mukesh agrawal31950242011-07-14 11:53:38 -070076using ::testing::AnyNumber;
Paul Stewart3c504012013-01-17 17:49:58 -080077using ::testing::AtLeast;
mukesh agrawal31950242011-07-14 11:53:38 -070078using ::testing::DefaultValue;
Paul Stewarta41e38d2011-11-11 07:47:29 -080079using ::testing::DoAll;
mukesh agrawalcf24a242012-05-21 16:46:11 -070080using ::testing::EndsWith;
Wade Guthrie0cf3c982013-05-29 09:11:35 -070081using ::testing::HasSubstr;
mukesh agrawal31950242011-07-14 11:53:38 -070082using ::testing::InSequence;
mukesh agrawalb20776f2012-02-10 16:00:36 -080083using ::testing::Invoke;
Paul Stewart549d44c2012-07-03 12:40:25 -070084using ::testing::InvokeWithoutArgs;
Wade Guthrie5a4e2ef2013-04-30 12:51:39 -070085using ::testing::MakeMatcher;
86using ::testing::Matcher;
87using ::testing::MatcherInterface;
88using ::testing::MatchResultListener;
mukesh agrawal7ec71312011-11-10 02:08:26 +000089using ::testing::Mock;
mukesh agrawaldc42bb32011-07-28 10:40:26 -070090using ::testing::NiceMock;
mukesh agrawald4dc0832013-03-25 14:38:26 -070091using ::testing::NotNull;
Chris Masone853b81b2011-06-24 14:11:41 -070092using ::testing::Return;
Prathmesh Prabhuba99b592013-04-17 15:13:14 -070093using ::testing::ReturnRef;
Gary Morainac1bdb42012-02-16 17:42:29 -080094using ::testing::SaveArg;
Paul Stewarta41e38d2011-11-11 07:47:29 -080095using ::testing::SetArgumentPointee;
96using ::testing::StrEq;
97using ::testing::StrictMock;
Chris Masone853b81b2011-06-24 14:11:41 -070098using ::testing::Test;
mukesh agrawal31950242011-07-14 11:53:38 -070099using ::testing::Throw;
mukesh agrawal8ede0522011-10-03 14:57:44 -0700100using ::testing::Values;
Chris Masone853b81b2011-06-24 14:11:41 -0700101
102namespace shill {
103
Wade Guthrie5a4e2ef2013-04-30 12:51:39 -0700104namespace {
105
106const uint16_t kNl80211FamilyId = 0x13;
107const uint16_t kRandomScanFrequency1 = 5600;
108const uint16_t kRandomScanFrequency2 = 5560;
109const uint16_t kRandomScanFrequency3 = 2422;
110
111} // namespace
112
mukesh agrawal31950242011-07-14 11:53:38 -0700113class WiFiPropertyTest : public PropertyStoreTest {
Chris Masone853b81b2011-06-24 14:11:41 -0700114 public:
mukesh agrawal31950242011-07-14 11:53:38 -0700115 WiFiPropertyTest()
Thieu Le3426c8f2012-01-11 17:35:11 -0800116 : device_(new WiFi(control_interface(),
Paul Stewart3c504012013-01-17 17:49:58 -0800117 NULL, NULL, manager(), "wifi", "", 0)) {
Chris Masone853b81b2011-06-24 14:11:41 -0700118 }
mukesh agrawal31950242011-07-14 11:53:38 -0700119 virtual ~WiFiPropertyTest() {}
Chris Masone853b81b2011-06-24 14:11:41 -0700120
121 protected:
mukesh agrawal8abd2f62012-01-30 14:56:14 -0800122 WiFiRefPtr device_;
Chris Masone853b81b2011-06-24 14:11:41 -0700123};
124
mukesh agrawal31950242011-07-14 11:53:38 -0700125TEST_F(WiFiPropertyTest, Contains) {
mukesh agrawalde29fa82011-09-16 16:16:36 -0700126 EXPECT_TRUE(device_->store().Contains(flimflam::kNameProperty));
127 EXPECT_FALSE(device_->store().Contains(""));
Chris Masone853b81b2011-06-24 14:11:41 -0700128}
129
mukesh agrawal6bb9e7c2012-01-30 14:57:54 -0800130TEST_F(WiFiPropertyTest, SetProperty) {
Chris Masonea8a2c252011-06-27 22:16:30 -0700131 {
132 ::DBus::Error error;
mukesh agrawal6bb9e7c2012-01-30 14:57:54 -0800133 EXPECT_TRUE(DBusAdaptor::SetProperty(
mukesh agrawalde29fa82011-09-16 16:16:36 -0700134 device_->mutable_store(),
Chris Masonea8a2c252011-06-27 22:16:30 -0700135 flimflam::kBgscanSignalThresholdProperty,
136 PropertyStoreTest::kInt32V,
137 &error));
138 }
139 {
140 ::DBus::Error error;
mukesh agrawal6bb9e7c2012-01-30 14:57:54 -0800141 EXPECT_TRUE(DBusAdaptor::SetProperty(device_->mutable_store(),
142 flimflam::kScanIntervalProperty,
143 PropertyStoreTest::kUint16V,
144 &error));
Chris Masonea8a2c252011-06-27 22:16:30 -0700145 }
Chris Masone853b81b2011-06-24 14:11:41 -0700146 // Ensure that an attempt to write a R/O property returns InvalidArgs error.
Chris Masonea8a2c252011-06-27 22:16:30 -0700147 {
148 ::DBus::Error error;
mukesh agrawal6bb9e7c2012-01-30 14:57:54 -0800149 EXPECT_FALSE(DBusAdaptor::SetProperty(device_->mutable_store(),
150 flimflam::kScanningProperty,
151 PropertyStoreTest::kBoolV,
152 &error));
mukesh agrawalbebf1b82013-04-23 15:06:33 -0700153 ASSERT_TRUE(error.is_set()); // name() may be invalid otherwise
Chris Masone9d779932011-08-25 16:33:41 -0700154 EXPECT_EQ(invalid_args(), error.name());
Chris Masonea8a2c252011-06-27 22:16:30 -0700155 }
Chris Masone853b81b2011-06-24 14:11:41 -0700156
mukesh agrawal4d0401c2012-01-06 16:05:31 -0800157 {
158 ::DBus::Error error;
mukesh agrawal6bb9e7c2012-01-30 14:57:54 -0800159 EXPECT_TRUE(DBusAdaptor::SetProperty(
mukesh agrawal4d0401c2012-01-06 16:05:31 -0800160 device_->mutable_store(),
161 flimflam::kBgscanMethodProperty,
162 DBusAdaptor::StringToVariant(
Paul Stewart0654ece2013-03-26 15:21:26 -0700163 WPASupplicant::kNetworkBgscanMethodSimple),
mukesh agrawal4d0401c2012-01-06 16:05:31 -0800164 &error));
165 }
166
167 {
168 ::DBus::Error error;
mukesh agrawal6bb9e7c2012-01-30 14:57:54 -0800169 EXPECT_FALSE(DBusAdaptor::SetProperty(
mukesh agrawal4d0401c2012-01-06 16:05:31 -0800170 device_->mutable_store(),
171 flimflam::kBgscanMethodProperty,
172 DBusAdaptor::StringToVariant("not a real scan method"),
173 &error));
174 }
175}
176
Darin Petkov4a66cc52012-06-15 10:08:29 +0200177TEST_F(WiFiPropertyTest, BgscanMethodProperty) {
Paul Stewart0654ece2013-03-26 15:21:26 -0700178 EXPECT_NE(WPASupplicant::kNetworkBgscanMethodLearn,
mukesh agrawal8abd2f62012-01-30 14:56:14 -0800179 WiFi::kDefaultBgscanMethod);
Darin Petkov4a66cc52012-06-15 10:08:29 +0200180 EXPECT_TRUE(device_->bgscan_method_.empty());
181
182 string method;
Paul Stewarte6e8e492013-01-17 11:00:50 -0800183 Error unused_error;
184 EXPECT_TRUE(device_->store().GetStringProperty(
185 flimflam::kBgscanMethodProperty, &method, &unused_error));
Darin Petkov4a66cc52012-06-15 10:08:29 +0200186 EXPECT_EQ(WiFi::kDefaultBgscanMethod, method);
Paul Stewart0654ece2013-03-26 15:21:26 -0700187 EXPECT_EQ(WPASupplicant::kNetworkBgscanMethodSimple, method);
mukesh agrawal8abd2f62012-01-30 14:56:14 -0800188
189 ::DBus::Error error;
mukesh agrawal6bb9e7c2012-01-30 14:57:54 -0800190 EXPECT_TRUE(DBusAdaptor::SetProperty(
mukesh agrawal8abd2f62012-01-30 14:56:14 -0800191 device_->mutable_store(),
192 flimflam::kBgscanMethodProperty,
193 DBusAdaptor::StringToVariant(
Paul Stewart0654ece2013-03-26 15:21:26 -0700194 WPASupplicant::kNetworkBgscanMethodLearn),
mukesh agrawal8abd2f62012-01-30 14:56:14 -0800195 &error));
Paul Stewart0654ece2013-03-26 15:21:26 -0700196 EXPECT_EQ(WPASupplicant::kNetworkBgscanMethodLearn, device_->bgscan_method_);
Paul Stewarte6e8e492013-01-17 11:00:50 -0800197 EXPECT_TRUE(device_->store().GetStringProperty(
198 flimflam::kBgscanMethodProperty, &method, &unused_error));
Paul Stewart0654ece2013-03-26 15:21:26 -0700199 EXPECT_EQ(WPASupplicant::kNetworkBgscanMethodLearn, method);
mukesh agrawal8abd2f62012-01-30 14:56:14 -0800200
201 EXPECT_TRUE(DBusAdaptor::ClearProperty(
202 device_->mutable_store(), flimflam::kBgscanMethodProperty, &error));
Paul Stewarte6e8e492013-01-17 11:00:50 -0800203 EXPECT_TRUE(device_->store().GetStringProperty(
204 flimflam::kBgscanMethodProperty, &method, &unused_error));
Darin Petkov4a66cc52012-06-15 10:08:29 +0200205 EXPECT_EQ(WiFi::kDefaultBgscanMethod, method);
206 EXPECT_TRUE(device_->bgscan_method_.empty());
mukesh agrawal8abd2f62012-01-30 14:56:14 -0800207}
208
Paul Stewart3c504012013-01-17 17:49:58 -0800209MATCHER_P(EndpointMatch, endpoint, "") {
210 return
211 arg->ssid() == endpoint->ssid() &&
212 arg->network_mode() == endpoint->network_mode() &&
213 arg->security_mode() == endpoint->security_mode();
214}
215
Wade Guthrie5a4e2ef2013-04-30 12:51:39 -0700216
Paul Stewarte369ece2012-05-22 09:11:03 -0700217class WiFiObjectTest : public ::testing::TestWithParam<string> {
mukesh agrawal31950242011-07-14 11:53:38 -0700218 public:
Wade Guthriebb9fca22013-04-10 17:21:42 -0700219 explicit WiFiObjectTest(EventDispatcher *dispatcher)
Paul Stewarte369ece2012-05-22 09:11:03 -0700220 : event_dispatcher_(dispatcher),
Thieu Le6c1e3bb2013-02-06 15:20:35 -0800221 metrics_(NULL),
Paul Stewarte369ece2012-05-22 09:11:03 -0700222 manager_(&control_interface_, NULL, &metrics_, &glib_),
223 device_info_(&control_interface_, dispatcher, &metrics_, &manager_),
Chris Masone626719f2011-08-18 16:58:48 -0700224 wifi_(new WiFi(&control_interface_,
Paul Stewarte369ece2012-05-22 09:11:03 -0700225 dispatcher,
Thieu Le3426c8f2012-01-11 17:35:11 -0800226 &metrics_,
Chris Masone626719f2011-08-18 16:58:48 -0700227 &manager_,
228 kDeviceName,
229 kDeviceAddress,
230 0)),
Paul Stewart3c504012013-01-17 17:49:58 -0800231 bss_counter_(0),
mukesh agrawaldc42bb32011-07-28 10:40:26 -0700232 supplicant_process_proxy_(new NiceMock<MockSupplicantProcessProxy>()),
mukesh agrawalb20776f2012-02-10 16:00:36 -0800233 supplicant_bss_proxy_(
234 new NiceMock<MockSupplicantBSSProxy>()),
mukesh agrawalf2f68a52011-09-01 12:15:48 -0700235 dhcp_config_(new MockDHCPConfig(&control_interface_,
Paul Stewartd408fdf2012-05-07 17:15:57 -0700236 kDeviceName)),
Darin Petkov2b8e44e2012-06-25 15:13:26 +0200237 dbus_manager_(new NiceMock<MockDBusManager>()),
Wade Guthrie0cf3c982013-05-29 09:11:35 -0700238 adaptor_(new DeviceMockAdaptor()),
Paul Stewart735eab52013-03-29 09:19:23 -0700239 eap_state_handler_(new NiceMock<MockSupplicantEAPStateHandler>()),
Paul Stewart3c504012013-01-17 17:49:58 -0800240 supplicant_interface_proxy_(
Paul Stewart9413bcc2013-04-04 16:12:43 -0700241 new NiceMock<MockSupplicantInterfaceProxy>()),
mukesh agrawal2f9df4e2012-08-08 12:29:20 -0700242 proxy_factory_(this) {
Wade Guthrie5a4e2ef2013-04-30 12:51:39 -0700243 InstallMockScanSession();
mukesh agrawal31950242011-07-14 11:53:38 -0700244 ::testing::DefaultValue< ::DBus::Path>::Set("/default/path");
mukesh agrawalc01f3982012-01-24 13:48:39 -0800245
Paul Stewartd408fdf2012-05-07 17:15:57 -0700246 ON_CALL(dhcp_provider_, CreateConfig(_, _, _, _)).
mukesh agrawalc01f3982012-01-24 13:48:39 -0800247 WillByDefault(Return(dhcp_config_));
248 ON_CALL(*dhcp_config_.get(), RequestIP()).
249 WillByDefault(Return(true));
Paul Stewart835934a2012-12-06 19:27:09 -0800250 ON_CALL(proxy_factory_, CreateSupplicantNetworkProxy(_, _)).
251 WillByDefault(InvokeWithoutArgs(
252 this, &WiFiObjectTest::CreateSupplicantNetworkProxy));
Wade Guthrie318445d2013-05-16 14:05:28 -0700253 Nl80211Message::SetMessageType(kNl80211FamilyId);
Gary Morainac1bdb42012-02-16 17:42:29 -0800254
Paul Stewart735eab52013-03-29 09:19:23 -0700255 // Transfers ownership.
256 manager_.dbus_manager_.reset(dbus_manager_);
257 wifi_->eap_state_handler_.reset(eap_state_handler_);
Darin Petkov2b8e44e2012-06-25 15:13:26 +0200258
Paul Stewart3c504012013-01-17 17:49:58 -0800259 wifi_->provider_ = &wifi_provider_;
mukesh agrawal5c05b292012-03-07 10:12:52 -0800260 wifi_->time_ = &time_;
Wade Guthriebb9fca22013-04-10 17:21:42 -0700261 wifi_->netlink_manager_ = &netlink_manager_;
Wade Guthrie5a4e2ef2013-04-30 12:51:39 -0700262 wifi_->progressive_scan_enabled_ = true;
Wade Guthrie0cf3c982013-05-29 09:11:35 -0700263 wifi_->adaptor_.reset(adaptor_); // Transfers ownership.
Wade Guthrie5a4e2ef2013-04-30 12:51:39 -0700264
265 // The following is only useful when a real |ScanSession| is used; it is
266 // ignored by |MockScanSession|.
267 wifi_->all_scan_frequencies_.insert(kRandomScanFrequency1);
268 wifi_->all_scan_frequencies_.insert(kRandomScanFrequency2);
269 wifi_->all_scan_frequencies_.insert(kRandomScanFrequency3);
mukesh agrawal31950242011-07-14 11:53:38 -0700270 }
mukesh agrawal5c4dd0b2011-09-14 13:53:14 -0700271
272 virtual void SetUp() {
mukesh agrawal3ab56ac2012-05-31 14:41:43 -0700273 // EnableScopes... so that we can EXPECT_CALL for scoped log messages.
274 ScopeLogger::GetInstance()->EnableScopesByName("wifi");
Christopher Wileyd34a47c2012-08-01 15:42:45 -0700275 ScopeLogger::GetInstance()->set_verbose_level(3);
Darin Petkovab565bb2011-10-06 02:55:51 -0700276 wifi_->proxy_factory_ = &proxy_factory_;
mukesh agrawal5c4dd0b2011-09-14 13:53:14 -0700277 static_cast<Device *>(wifi_)->rtnl_handler_ = &rtnl_handler_;
278 wifi_->set_dhcp_provider(&dhcp_provider_);
Paul Stewart3c504012013-01-17 17:49:58 -0800279 ON_CALL(manager_, device_info()).WillByDefault(Return(&device_info_));
Darin Petkov2b8e44e2012-06-25 15:13:26 +0200280 EXPECT_CALL(manager_, UpdateEnabledTechnologies()).Times(AnyNumber());
mukesh agrawalb20776f2012-02-10 16:00:36 -0800281 EXPECT_CALL(*supplicant_bss_proxy_, Die()).Times(AnyNumber());
mukesh agrawal5c4dd0b2011-09-14 13:53:14 -0700282 }
283
284 virtual void TearDown() {
Paul Stewart3c504012013-01-17 17:49:58 -0800285 EXPECT_CALL(*wifi_provider(), OnEndpointRemoved(_))
286 .WillRepeatedly(Return(reinterpret_cast<WiFiService *>(NULL)));
Eric Shienbrood9a245532012-03-07 14:20:39 -0500287 wifi_->SelectService(NULL);
mukesh agrawalb20776f2012-02-10 16:00:36 -0800288 if (supplicant_bss_proxy_.get()) {
289 EXPECT_CALL(*supplicant_bss_proxy_, Die());
290 }
Darin Petkovab565bb2011-10-06 02:55:51 -0700291 wifi_->proxy_factory_ = NULL;
mukesh agrawal31950242011-07-14 11:53:38 -0700292 // must Stop WiFi instance, to clear its list of services.
293 // otherwise, the WiFi instance will not be deleted. (because
294 // services reference a WiFi instance, creating a cycle.)
Eric Shienbrood9a245532012-03-07 14:20:39 -0500295 wifi_->Stop(NULL, ResultCallback());
mukesh agrawal5c4dd0b2011-09-14 13:53:14 -0700296 wifi_->set_dhcp_provider(NULL);
mukesh agrawal3ab56ac2012-05-31 14:41:43 -0700297 // Reset scope logging, to avoid interfering with other tests.
298 ScopeLogger::GetInstance()->EnableScopesByName("-wifi");
Christopher Wileyd34a47c2012-08-01 15:42:45 -0700299 ScopeLogger::GetInstance()->set_verbose_level(0);
mukesh agrawalf2f68a52011-09-01 12:15:48 -0700300 }
301
Paul Stewart549d44c2012-07-03 12:40:25 -0700302 // Needs to be public since it is called via Invoke().
303 void StopWiFi() {
Paul Stewart549d44c2012-07-03 12:40:25 -0700304 wifi_->SetEnabled(false); // Stop(NULL, ResultCallback());
305 }
306
Christopher Wileyc6184482012-10-24 15:31:56 -0700307 // Needs to be public since it is called via Invoke().
308 void ThrowDBusError() {
309 throw DBus::Error("SomeDBusType", "A handy message");
310 }
Paul Stewart17d90652013-04-04 15:09:11 -0700311 void ResetPendingService() {
312 SetPendingService(NULL);
313 }
Christopher Wileyc6184482012-10-24 15:31:56 -0700314
Wade Guthrie5a4e2ef2013-04-30 12:51:39 -0700315 size_t GetScanFrequencyCount() const {
316 return wifi_->all_scan_frequencies_.size();
317 }
318
319 void SetScanSize(int min, int max) {
320 wifi_->min_frequencies_to_scan_ = min;
321 wifi_->max_frequencies_to_scan_ = max;
322 }
323
324 // This clears WiFi::scan_session_, thereby allowing WiFi::Scan to create a
325 // real scan session.
326 void ClearScanSession() {
327 wifi_->scan_session_.reset();
328 }
329
330 bool IsScanSessionNull() {
331 return !wifi_->scan_session_;
332 }
333
334 void InstallMockScanSession() {
335 WiFiProvider::FrequencyCountList previous_frequencies;
336 std::set<uint16_t> available_frequencies;
337 ScanSession::FractionList fractions;
338 ScanSession::OnScanFailed null_callback;
339 scan_session_ = new MockScanSession(&netlink_manager_,
340 event_dispatcher_,
341 previous_frequencies,
342 available_frequencies,
343 0,
344 fractions,
345 0,
346 0,
Wade Guthrief22681f2013-05-31 11:46:31 -0700347 null_callback,
348 NULL);
Wade Guthrie5a4e2ef2013-04-30 12:51:39 -0700349 wifi_->scan_session_.reset(scan_session_);
350 }
351
352 // Or DisableProgressiveScan()...
353 void EnableFullScan() {
354 wifi_->progressive_scan_enabled_ = false;
355 }
356
Wade Guthrie7347bf22013-04-30 11:21:51 -0700357 void OnTriggerScanResponse(const Nl80211Message &message) {
Wade Guthrie5a4e2ef2013-04-30 12:51:39 -0700358 wifi_->scan_session_->OnTriggerScanResponse(message);
359 }
360
Wade Guthrieb0def9f2013-07-12 13:49:18 -0700361 void SetScanState(WiFi::ScanState new_state,
362 WiFi::ScanMethod new_method,
363 const char *reason) {
364 wifi_->SetScanState(new_state, new_method, reason);
Wade Guthrieb9e0ee72013-05-31 09:23:30 -0700365 }
366
Wade Guthrie0cf3c982013-05-29 09:11:35 -0700367 void VerifyScanState(WiFi::ScanState state, WiFi::ScanMethod method) {
368 EXPECT_EQ(state, wifi_->scan_state_);
369 EXPECT_EQ(method, wifi_->scan_method_);
370 }
371
mukesh agrawal31950242011-07-14 11:53:38 -0700372 protected:
mukesh agrawal7ec71312011-11-10 02:08:26 +0000373 typedef scoped_refptr<MockWiFiService> MockWiFiServiceRefPtr;
374
mukesh agrawal31950242011-07-14 11:53:38 -0700375 class TestProxyFactory : public ProxyFactory {
376 public:
Paul Stewarte369ece2012-05-22 09:11:03 -0700377 explicit TestProxyFactory(WiFiObjectTest *test);
mukesh agrawal31950242011-07-14 11:53:38 -0700378
379 virtual SupplicantProcessProxyInterface *CreateSupplicantProcessProxy(
mukesh agrawal1830fa12011-09-26 14:31:40 -0700380 const char */*dbus_path*/, const char */*dbus_addr*/) {
mukesh agrawaldc42bb32011-07-28 10:40:26 -0700381 return test_->supplicant_process_proxy_.release();
mukesh agrawal31950242011-07-14 11:53:38 -0700382 }
383
384 virtual SupplicantInterfaceProxyInterface *CreateSupplicantInterfaceProxy(
Paul Stewart196f50f2013-03-27 18:02:11 -0700385 SupplicantEventDelegateInterface */*delegate*/,
mukesh agrawal1830fa12011-09-26 14:31:40 -0700386 const DBus::Path &/*object_path*/,
387 const char */*dbus_addr*/) {
mukesh agrawaldc42bb32011-07-28 10:40:26 -0700388 return test_->supplicant_interface_proxy_.release();
mukesh agrawal31950242011-07-14 11:53:38 -0700389 }
390
mukesh agrawalb20776f2012-02-10 16:00:36 -0800391 MOCK_METHOD3(CreateSupplicantBSSProxy,
392 SupplicantBSSProxyInterface *(
393 WiFiEndpoint *wifi_endpoint,
394 const DBus::Path &object_path,
395 const char *dbus_addr));
396
Paul Stewart835934a2012-12-06 19:27:09 -0800397 MOCK_METHOD2(CreateSupplicantNetworkProxy,
398 SupplicantNetworkProxyInterface *(
399 const DBus::Path &object_path,
400 const char *dbus_addr));
401
mukesh agrawal31950242011-07-14 11:53:38 -0700402 private:
mukesh agrawalb20776f2012-02-10 16:00:36 -0800403 SupplicantBSSProxyInterface *CreateSupplicantBSSProxyInternal(
404 WiFiEndpoint */*wifi_endpoint*/,
405 const DBus::Path &/*object_path*/,
406 const char */*dbus_addr*/) {
407 return test_->supplicant_bss_proxy_.release();
408 }
409
Paul Stewarte369ece2012-05-22 09:11:03 -0700410 WiFiObjectTest *test_;
mukesh agrawal31950242011-07-14 11:53:38 -0700411 };
412
Paul Stewart3c504012013-01-17 17:49:58 -0800413 // Simulate the course of events when the last enpoint of a service is
414 // removed.
415 class EndpointRemovalHandler {
Wade Guthriebb9fca22013-04-10 17:21:42 -0700416 public:
417 EndpointRemovalHandler(WiFiRefPtr wifi, const WiFiServiceRefPtr &service)
418 : wifi_(wifi), service_(service) {}
419 virtual ~EndpointRemovalHandler() {}
Paul Stewart3c504012013-01-17 17:49:58 -0800420
Wade Guthriebb9fca22013-04-10 17:21:42 -0700421 WiFiServiceRefPtr OnEndpointRemoved(
422 const WiFiEndpointConstRefPtr &endpoint) {
423 wifi_->DisassociateFromService(service_);
424 return service_;
425 }
Paul Stewart3c504012013-01-17 17:49:58 -0800426
427 private:
428 WiFiRefPtr wifi_;
429 WiFiServiceRefPtr service_;
430 };
431
432 EndpointRemovalHandler *MakeEndpointRemovalHandler(
Wade Guthriebb9fca22013-04-10 17:21:42 -0700433 const WiFiServiceRefPtr &service) {
Paul Stewart3c504012013-01-17 17:49:58 -0800434 return new EndpointRemovalHandler(wifi_, service);
435 }
mukesh agrawalb66c6462012-05-07 11:45:25 -0700436 void CancelScanTimer() {
437 wifi_->scan_timer_callback_.Cancel();
438 }
Paul Stewart3c504012013-01-17 17:49:58 -0800439 // This function creates a new endpoint with a mode set to |mode|. We
440 // synthesize new |path| and |bssid| values, since we don't really care
441 // what they are for unit tests. If "use_ssid" is true, we used the
442 // passed-in ssid, otherwise we create a synthesized value for it as well.
443 WiFiEndpointRefPtr MakeNewEndpoint(const char *mode,
444 bool use_ssid,
445 string *ssid,
446 string *path,
447 string *bssid) {
448 bss_counter_++;
449 if (!use_ssid) {
450 *ssid = base::StringPrintf("ssid%d", bss_counter_);
451 }
452 *path = base::StringPrintf("/interface/bss%d", bss_counter_);
453 *bssid = base::StringPrintf("00:00:00:00:00:%02x", bss_counter_);
454 WiFiEndpointRefPtr endpoint = MakeEndpointWithMode(*ssid, *bssid, mode);
455 EXPECT_CALL(wifi_provider_,
456 OnEndpointAdded(EndpointMatch(endpoint))).Times(1);
457 return endpoint;
458 }
459 WiFiEndpointRefPtr MakeEndpoint(const string &ssid, const string &bssid) {
460 return MakeEndpointWithMode(ssid, bssid, kNetworkModeInfrastructure);
461 }
462 WiFiEndpointRefPtr MakeEndpointWithMode(
463 const string &ssid, const string &bssid, const string &mode) {
464 return WiFiEndpoint::MakeOpenEndpoint(
465 &proxy_factory_, NULL, ssid, bssid, mode, 0, 0);
466 }
467 MockWiFiServiceRefPtr MakeMockServiceWithSSID(
468 vector<uint8_t> ssid, const std::string &security) {
469 return new NiceMock<MockWiFiService>(
470 &control_interface_,
471 event_dispatcher_,
472 &metrics_,
473 &manager_,
474 &wifi_provider_,
475 ssid,
476 flimflam::kModeManaged,
477 security,
478 false);
479 }
480 MockWiFiServiceRefPtr MakeMockService(const std::string &security) {
481 return MakeMockServiceWithSSID(vector<uint8_t>(1, 'a'), security);
482 }
483 ::DBus::Path MakeNewEndpointAndService(int16_t signal_strength,
484 uint16 frequency,
485 const char *mode,
486 WiFiEndpointRefPtr *endpoint_ptr,
487 MockWiFiServiceRefPtr *service_ptr) {
488 string ssid;
489 ::DBus::Path path;
490 string bssid;
491 WiFiEndpointRefPtr endpoint =
492 MakeNewEndpoint(mode, false, &ssid, &path, &bssid);
493 MockWiFiServiceRefPtr service =
494 MakeMockServiceWithSSID(endpoint->ssid(), endpoint->security_mode());
495 EXPECT_CALL(wifi_provider_, FindServiceForEndpoint(EndpointMatch(endpoint)))
496 .WillRepeatedly(Return(service));
497 ON_CALL(*service, GetEndpointCount()).WillByDefault(Return(1));
498 ReportBSS(path, ssid, bssid, signal_strength, frequency, mode);
499 if (service_ptr) {
500 *service_ptr = service;
501 }
502 if (endpoint_ptr) {
503 *endpoint_ptr = endpoint;
504 }
505 return path;
506 }
507 ::DBus::Path AddEndpointToService(
508 WiFiServiceRefPtr service,
509 int16_t signal_strength,
510 uint16 frequency,
511 const char *mode,
512 WiFiEndpointRefPtr *endpoint_ptr) {
513 string ssid(service->ssid().begin(), service->ssid().end());
514 ::DBus::Path path;
515 string bssid;
516 WiFiEndpointRefPtr endpoint =
517 MakeNewEndpoint(mode, true, &ssid, &path, &bssid);
518 EXPECT_CALL(wifi_provider_, FindServiceForEndpoint(EndpointMatch(endpoint)))
519 .WillRepeatedly(Return(service));
520 ReportBSS(path, ssid, bssid, signal_strength, frequency, mode);
521 if (endpoint_ptr) {
522 *endpoint_ptr = endpoint;
523 }
524 return path;
525 }
526 void InitiateConnect(WiFiServiceRefPtr service) {
527 map<string, ::DBus::Variant> params;
528 wifi_->ConnectTo(service, params);
529 }
530 void InitiateDisconnect(WiFiServiceRefPtr service) {
531 wifi_->DisconnectFrom(service);
532 }
533 MockWiFiServiceRefPtr SetupConnectingService(
534 const DBus::Path &network_path,
535 WiFiEndpointRefPtr *endpoint_ptr,
536 ::DBus::Path *bss_path_ptr) {
537 if (!network_path.empty()) {
538 EXPECT_CALL(*GetSupplicantInterfaceProxy(), AddNetwork(_))
539 .WillOnce(Return(network_path));
540 EXPECT_CALL(*GetSupplicantInterfaceProxy(), SelectNetwork(network_path));
541 }
542
543 MockWiFiServiceRefPtr service;
544 WiFiEndpointRefPtr endpoint;
545 ::DBus::Path bss_path(MakeNewEndpointAndService(
546 0, 0, kNetworkModeAdHoc, &endpoint, &service));
547 EXPECT_CALL(*service, SetState(Service::kStateAssociating));
548 InitiateConnect(service);
549 Mock::VerifyAndClearExpectations(service);
550 EXPECT_FALSE(GetPendingTimeout().IsCancelled());
551 if (endpoint_ptr) {
552 *endpoint_ptr = endpoint;
553 }
554 if (bss_path_ptr) {
555 *bss_path_ptr = bss_path;
556 }
557 return service;
558 }
559
560 MockWiFiServiceRefPtr SetupConnectedService(
561 const DBus::Path &network_path,
562 WiFiEndpointRefPtr *endpoint_ptr,
563 ::DBus::Path *bss_path_ptr) {
564 WiFiEndpointRefPtr endpoint;
565 ::DBus::Path bss_path;
566 MockWiFiServiceRefPtr service =
567 SetupConnectingService(network_path, &endpoint, &bss_path);
568 if (endpoint_ptr) {
569 *endpoint_ptr = endpoint;
570 }
571 if (bss_path_ptr) {
572 *bss_path_ptr = bss_path;
573 }
574 EXPECT_CALL(*service, NotifyCurrentEndpoint(EndpointMatch(endpoint)));
575 ReportCurrentBSSChanged(bss_path);
576 EXPECT_TRUE(GetPendingTimeout().IsCancelled());
577 Mock::VerifyAndClearExpectations(service);
578
579 EXPECT_CALL(*service, SetState(Service::kStateConfiguring));
Paul Stewartbca08f82013-07-09 16:32:37 -0700580 EXPECT_CALL(*service, ResetSuspectedCredentialFailures());
Paul Stewart3c504012013-01-17 17:49:58 -0800581 EXPECT_CALL(*dhcp_provider(), CreateConfig(_, _, _, _)).Times(AnyNumber());
582 EXPECT_CALL(*dhcp_config_.get(), RequestIP()).Times(AnyNumber());
Paul Stewart0654ece2013-03-26 15:21:26 -0700583 ReportStateChanged(WPASupplicant::kInterfaceStateCompleted);
Paul Stewart3c504012013-01-17 17:49:58 -0800584 Mock::VerifyAndClearExpectations(service);
585
586 EXPECT_EQ(service, GetCurrentService());
587 return service;
mukesh agrawal15908392011-11-16 18:29:25 +0000588 }
mukesh agrawalb66c6462012-05-07 11:45:25 -0700589 void FireScanTimer() {
590 wifi_->ScanTimerHandler();
591 }
Wade Guthrie5a4e2ef2013-04-30 12:51:39 -0700592 void TriggerFullScan() {
Wade Guthrie68d41092013-04-02 12:56:02 -0700593 wifi_->Scan(Device::kFullScan, NULL);
Christopher Wileyc68c8672012-11-20 16:52:21 -0800594 }
mukesh agrawal15908392011-11-16 18:29:25 +0000595 const WiFiServiceRefPtr &GetCurrentService() {
596 return wifi_->current_service_;
597 }
Paul Stewartbc6e7392012-05-24 07:07:48 -0700598 void SetCurrentService(const WiFiServiceRefPtr &service) {
599 wifi_->current_service_ = service;
600 }
mukesh agrawal31950242011-07-14 11:53:38 -0700601 const WiFi::EndpointMap &GetEndpointMap() {
mukesh agrawal165e6142011-11-22 02:22:56 +0000602 return wifi_->endpoint_by_rpcid_;
mukesh agrawal31950242011-07-14 11:53:38 -0700603 }
mukesh agrawal15908392011-11-16 18:29:25 +0000604 const WiFiServiceRefPtr &GetPendingService() {
605 return wifi_->pending_service_;
606 }
Paul Stewart2b05e622012-07-13 20:38:44 -0700607 const base::CancelableClosure &GetPendingTimeout() {
608 return wifi_->pending_timeout_callback_;
609 }
Paul Stewart1aff7302012-08-04 20:04:47 -0700610 const base::CancelableClosure &GetReconnectTimeoutCallback() {
Paul Stewart44663922012-07-30 11:03:03 -0700611 return wifi_->reconnect_timeout_callback_;
612 }
Paul Stewart1aff7302012-08-04 20:04:47 -0700613 int GetReconnectTimeoutSeconds() {
614 return WiFi::kReconnectTimeoutSeconds;
615 }
mukesh agrawalb66c6462012-05-07 11:45:25 -0700616 const base::CancelableClosure &GetScanTimer() {
617 return wifi_->scan_timer_callback_;
618 }
mukesh agrawal31950242011-07-14 11:53:38 -0700619 // note: the tests need the proxies referenced by WiFi (not the
Paul Stewarte369ece2012-05-22 09:11:03 -0700620 // proxies instantiated by WiFiObjectTest), to ensure that WiFi
mukesh agrawal31950242011-07-14 11:53:38 -0700621 // sets up its proxies correctly.
622 SupplicantProcessProxyInterface *GetSupplicantProcessProxy() {
623 return wifi_->supplicant_process_proxy_.get();
624 }
Paul Stewart3c504012013-01-17 17:49:58 -0800625 MockSupplicantInterfaceProxy *GetSupplicantInterfaceProxyFromWiFi() {
mukesh agrawal8a3188d2011-12-01 20:56:44 +0000626 return dynamic_cast<MockSupplicantInterfaceProxy *>(
627 wifi_->supplicant_interface_proxy_.get());
mukesh agrawal31950242011-07-14 11:53:38 -0700628 }
Paul Stewart3c504012013-01-17 17:49:58 -0800629 // This function returns the supplicant interface proxy whether
630 // or not we have passed the instantiated object to the WiFi instance
631 // from WiFiObjectTest, so tests don't need to worry about when they
632 // set expectations relative to StartWiFi().
633 MockSupplicantInterfaceProxy *GetSupplicantInterfaceProxy() {
634 MockSupplicantInterfaceProxy *proxy = GetSupplicantInterfaceProxyFromWiFi();
635 return proxy ? proxy : supplicant_interface_proxy_.get();
636 }
Paul Stewart835934a2012-12-06 19:27:09 -0800637 MockSupplicantNetworkProxy *CreateSupplicantNetworkProxy() {
638 return new NiceMock<MockSupplicantNetworkProxy>();
639 }
mukesh agrawal7ec71312011-11-10 02:08:26 +0000640 const string &GetSupplicantState() {
641 return wifi_->supplicant_state_;
642 }
Paul Stewart835934a2012-12-06 19:27:09 -0800643 void ClearCachedCredentials(const WiFiService *service) {
644 return wifi_->ClearCachedCredentials(service);
645 }
Paul Stewart0427cc12013-03-25 13:50:39 -0700646 void NotifyEndpointChanged(const WiFiEndpointConstRefPtr &endpoint) {
647 wifi_->NotifyEndpointChanged(endpoint);
648 }
Ben Chan381fdcc2012-10-14 21:10:36 -0700649 bool RemoveNetwork(const ::DBus::Path &network) {
650 return wifi_->RemoveNetwork(network);
651 }
mukesh agrawal261daca2011-12-02 18:56:56 +0000652 void RemoveBSS(const ::DBus::Path &bss_path);
mukesh agrawal31950242011-07-14 11:53:38 -0700653 void ReportBSS(const ::DBus::Path &bss_path,
654 const string &ssid,
655 const string &bssid,
656 int16_t signal_strength,
Thieu Lee41a72d2012-02-06 20:46:51 +0000657 uint16 frequency,
mukesh agrawal31950242011-07-14 11:53:38 -0700658 const char *mode);
mukesh agrawalc01f3982012-01-24 13:48:39 -0800659 void ReportIPConfigComplete() {
Darin Petkov79d74c92012-03-07 17:20:32 +0100660 wifi_->OnIPConfigUpdated(dhcp_config_, true);
mukesh agrawalc01f3982012-01-24 13:48:39 -0800661 }
Paul Stewartf6f96482013-07-12 12:49:15 -0700662 void ReportIPConfigFailure() {
663 wifi_->OnIPConfigFailure();
664 }
665 void ReportConnected() {
666 wifi_->OnConnected();
667 }
mukesh agrawalf2f68a52011-09-01 12:15:48 -0700668 void ReportLinkUp() {
669 wifi_->LinkEvent(IFF_LOWER_UP, IFF_LOWER_UP);
670 }
mukesh agrawal31950242011-07-14 11:53:38 -0700671 void ReportScanDone() {
Wade Guthrie5a4e2ef2013-04-30 12:51:39 -0700672 // Eliminate |scan_session| so |ScanDoneTask| doesn't launch another scan.
673 wifi_->scan_session_.reset();
674 wifi_->ScanDoneTask();
675 // Make a new |scan_session| so that future scanning is done with the mock.
676 InstallMockScanSession();
677 }
678 void ReportScanDoneKeepScanSession() {
mukesh agrawaldc42bb32011-07-28 10:40:26 -0700679 wifi_->ScanDoneTask();
mukesh agrawal31950242011-07-14 11:53:38 -0700680 }
mukesh agrawal15908392011-11-16 18:29:25 +0000681 void ReportCurrentBSSChanged(const string &new_bss) {
682 wifi_->CurrentBSSChanged(new_bss);
683 }
mukesh agrawal7ec71312011-11-10 02:08:26 +0000684 void ReportStateChanged(const string &new_state) {
685 wifi_->StateChanged(new_state);
686 }
Paul Stewart5581d072012-12-17 17:30:20 -0800687 void ReportWiFiDebugScopeChanged(bool enabled) {
688 wifi_->OnWiFiDebugScopeChanged(enabled);
689 }
mukesh agrawalcf24a242012-05-21 16:46:11 -0700690 void SetPendingService(const WiFiServiceRefPtr &service) {
Wade Guthriedf6d61b2013-07-17 11:43:55 -0700691 wifi_->SetPendingService(service);
mukesh agrawalcf24a242012-05-21 16:46:11 -0700692 }
mukesh agrawalbebf1b82013-04-23 15:06:33 -0700693 bool SetScanInterval(uint16_t interval_seconds, Error *error) {
694 return wifi_->SetScanInterval(interval_seconds, error);
mukesh agrawalb66c6462012-05-07 11:45:25 -0700695 }
Paul Stewarte369ece2012-05-22 09:11:03 -0700696 uint16_t GetScanInterval() {
697 return wifi_->GetScanInterval(NULL);
698 }
Darin Petkov2b8e44e2012-06-25 15:13:26 +0200699 void StartWiFi(bool supplicant_present) {
Darin Petkov2b8e44e2012-06-25 15:13:26 +0200700 wifi_->supplicant_present_ = supplicant_present;
mukesh agrawalb66c6462012-05-07 11:45:25 -0700701 wifi_->SetEnabled(true); // Start(NULL, ResultCallback());
mukesh agrawal31950242011-07-14 11:53:38 -0700702 }
Darin Petkov2b8e44e2012-06-25 15:13:26 +0200703 void StartWiFi() {
704 StartWiFi(true);
705 }
mukesh agrawal2f9df4e2012-08-08 12:29:20 -0700706 void OnAfterResume() {
707 wifi_->OnAfterResume();
708 }
709 void OnBeforeSuspend() {
710 wifi_->OnBeforeSuspend();
711 }
Darin Petkov2b8e44e2012-06-25 15:13:26 +0200712 void OnSupplicantAppear() {
713 wifi_->OnSupplicantAppear(":1.7");
714 EXPECT_TRUE(wifi_->supplicant_present_);
715 }
716 void OnSupplicantVanish() {
717 wifi_->OnSupplicantVanish();
718 EXPECT_FALSE(wifi_->supplicant_present_);
719 }
720 bool GetSupplicantPresent() {
721 return wifi_->supplicant_present_;
722 }
Darin Petkov4a66cc52012-06-15 10:08:29 +0200723 bool SetBgscanMethod(const string &method) {
724 ::DBus::Error error;
725 return DBusAdaptor::SetProperty(
726 wifi_->mutable_store(),
727 flimflam::kBgscanMethodProperty,
728 DBusAdaptor::StringToVariant(method),
729 &error);
730 }
731
732 void AppendBgscan(WiFiService *service,
733 std::map<std::string, DBus::Variant> *service_params) {
734 wifi_->AppendBgscan(service, service_params);
735 }
736
Paul Stewartbc6e7392012-05-24 07:07:48 -0700737 void ReportCertification(const map<string, ::DBus::Variant> &properties) {
738 wifi_->CertificationTask(properties);
739 }
Paul Stewart10ccbb32012-04-26 15:59:30 -0700740
Paul Stewartdb0f9172012-11-30 16:48:09 -0800741 void ReportEAPEvent(const string &status, const string &parameter) {
742 wifi_->EAPEventTask(status, parameter);
743 }
744
Paul Stewarte369ece2012-05-22 09:11:03 -0700745 void RestartFastScanAttempts() {
746 wifi_->RestartFastScanAttempts();
747 }
748
Paul Stewart1aff7302012-08-04 20:04:47 -0700749 void StartReconnectTimer() {
750 wifi_->StartReconnectTimer();
751 }
752
753 void StopReconnectTimer() {
754 wifi_->StopReconnectTimer();
755 }
756
Paul Stewart3c508e12012-08-09 11:40:06 -0700757 void SetLinkMonitor(LinkMonitor *link_monitor) {
758 wifi_->set_link_monitor(link_monitor);
759 }
760
Paul Stewartbca08f82013-07-09 16:32:37 -0700761 bool SuspectCredentials(const WiFiServiceRefPtr &service,
Paul Stewart1369c2b2013-01-11 05:41:26 -0800762 Service::ConnectFailure *failure) {
763 return wifi_->SuspectCredentials(service, failure);
764 }
765
Paul Stewart3c508e12012-08-09 11:40:06 -0700766 void OnLinkMonitorFailure() {
767 wifi_->OnLinkMonitorFailure();
768 }
769
mukesh agrawalbebf1b82013-04-23 15:06:33 -0700770 bool SetBgscanShortInterval(const uint16 &interval, Error *error) {
771 return wifi_->SetBgscanShortInterval(interval, error);
772 }
773
774 bool SetBgscanSignalThreshold(const int32 &threshold, Error *error) {
775 return wifi_->SetBgscanSignalThreshold(threshold, error);
776 }
777
Wade Guthriedf6d61b2013-07-17 11:43:55 -0700778 void TimeoutPendingConnection() {
779 wifi_->PendingTimeoutHandler();
780 }
781
Paul Stewart10ccbb32012-04-26 15:59:30 -0700782 NiceMockControl *control_interface() {
783 return &control_interface_;
784 }
785
Paul Stewart85aea152013-01-22 09:31:56 -0800786 MockMetrics *metrics() {
787 return &metrics_;
788 }
789
mukesh agrawal32399322011-09-01 10:53:43 -0700790 MockManager *manager() {
791 return &manager_;
792 }
Gary Moraind9f893f2012-02-06 10:03:40 -0800793
794 MockDeviceInfo *device_info() {
795 return &device_info_;
796 }
797
798 MockDHCPProvider *dhcp_provider() {
799 return &dhcp_provider_;
800 }
801
mukesh agrawal32399322011-09-01 10:53:43 -0700802 const WiFiConstRefPtr wifi() const {
803 return wifi_;
804 }
805
mukesh agrawalb20776f2012-02-10 16:00:36 -0800806 TestProxyFactory *proxy_factory() {
807 return &proxy_factory_;
808 }
809
Paul Stewart3c504012013-01-17 17:49:58 -0800810 MockWiFiProvider *wifi_provider() {
811 return &wifi_provider_;
812 }
813
Paul Stewarte369ece2012-05-22 09:11:03 -0700814 EventDispatcher *event_dispatcher_;
Wade Guthrie5a4e2ef2013-04-30 12:51:39 -0700815 MockScanSession *scan_session_; // Owned by |wifi_|.
mukesh agrawal5c4dd0b2011-09-14 13:53:14 -0700816 NiceMock<MockRTNLHandler> rtnl_handler_;
mukesh agrawal5c05b292012-03-07 10:12:52 -0800817 MockTime time_;
mukesh agrawal31950242011-07-14 11:53:38 -0700818
819 private:
mukesh agrawaldc42bb32011-07-28 10:40:26 -0700820 NiceMockControl control_interface_;
Thieu Le3426c8f2012-01-11 17:35:11 -0800821 MockMetrics metrics_;
mukesh agrawalf2f68a52011-09-01 12:15:48 -0700822 MockGLib glib_;
Chris Masone2ae797d2011-08-23 20:41:00 -0700823 MockManager manager_;
mukesh agrawalc01f3982012-01-24 13:48:39 -0800824 MockDeviceInfo device_info_;
mukesh agrawal31950242011-07-14 11:53:38 -0700825 WiFiRefPtr wifi_;
Paul Stewart3c504012013-01-17 17:49:58 -0800826 NiceMock<MockWiFiProvider> wifi_provider_;
827 int bss_counter_;
mukesh agrawal31950242011-07-14 11:53:38 -0700828
829 // protected fields interspersed between private fields, due to
830 // initialization order
831 protected:
832 static const char kDeviceName[];
Chris Masone626719f2011-08-18 16:58:48 -0700833 static const char kDeviceAddress[];
mukesh agrawal31950242011-07-14 11:53:38 -0700834 static const char kNetworkModeAdHoc[];
835 static const char kNetworkModeInfrastructure[];
Paul Stewart44663922012-07-30 11:03:03 -0700836 static const char kBSSName[];
837 static const char kSSIDName[];
mukesh agrawal31950242011-07-14 11:53:38 -0700838
mukesh agrawaldc42bb32011-07-28 10:40:26 -0700839 scoped_ptr<MockSupplicantProcessProxy> supplicant_process_proxy_;
mukesh agrawalb20776f2012-02-10 16:00:36 -0800840 scoped_ptr<MockSupplicantBSSProxy> supplicant_bss_proxy_;
mukesh agrawalf2f68a52011-09-01 12:15:48 -0700841 MockDHCPProvider dhcp_provider_;
842 scoped_refptr<MockDHCPConfig> dhcp_config_;
Paul Stewart735eab52013-03-29 09:19:23 -0700843
844 // These pointers track mock objects owned by the WiFi device instance
845 // and manager so we can perform expectations against them.
846 MockDBusManager *dbus_manager_;
Wade Guthrie0cf3c982013-05-29 09:11:35 -0700847 DeviceMockAdaptor *adaptor_;
Paul Stewart735eab52013-03-29 09:19:23 -0700848 MockSupplicantEAPStateHandler *eap_state_handler_;
Wade Guthriebb9fca22013-04-10 17:21:42 -0700849 MockNetlinkManager netlink_manager_;
mukesh agrawal31950242011-07-14 11:53:38 -0700850
851 private:
Paul Stewart3c504012013-01-17 17:49:58 -0800852 scoped_ptr<MockSupplicantInterfaceProxy> supplicant_interface_proxy_;
Paul Stewart835934a2012-12-06 19:27:09 -0800853 NiceMock<TestProxyFactory> proxy_factory_;
mukesh agrawal31950242011-07-14 11:53:38 -0700854};
855
Paul Stewarte369ece2012-05-22 09:11:03 -0700856const char WiFiObjectTest::kDeviceName[] = "wlan0";
857const char WiFiObjectTest::kDeviceAddress[] = "000102030405";
858const char WiFiObjectTest::kNetworkModeAdHoc[] = "ad-hoc";
859const char WiFiObjectTest::kNetworkModeInfrastructure[] = "infrastructure";
Paul Stewart44663922012-07-30 11:03:03 -0700860const char WiFiObjectTest::kBSSName[] = "bss0";
861const char WiFiObjectTest::kSSIDName[] = "ssid0";
mukesh agrawal31950242011-07-14 11:53:38 -0700862
Paul Stewarte369ece2012-05-22 09:11:03 -0700863void WiFiObjectTest::RemoveBSS(const ::DBus::Path &bss_path) {
mukesh agrawalb4bc57d2011-12-07 01:07:47 +0000864 wifi_->BSSRemovedTask(bss_path);
mukesh agrawal261daca2011-12-02 18:56:56 +0000865}
866
Paul Stewarte369ece2012-05-22 09:11:03 -0700867void WiFiObjectTest::ReportBSS(const ::DBus::Path &bss_path,
mukesh agrawal31950242011-07-14 11:53:38 -0700868 const string &ssid,
869 const string &bssid,
870 int16_t signal_strength,
Thieu Lee41a72d2012-02-06 20:46:51 +0000871 uint16 frequency,
mukesh agrawal31950242011-07-14 11:53:38 -0700872 const char *mode) {
873 map<string, ::DBus::Variant> bss_properties;
874
875 {
876 DBus::MessageIter writer(bss_properties["SSID"].writer());
877 writer << vector<uint8_t>(ssid.begin(), ssid.end());
878 }
879 {
880 string bssid_nosep;
881 vector<uint8_t> bssid_bytes;
882 RemoveChars(bssid, ":", &bssid_nosep);
883 base::HexStringToBytes(bssid_nosep, &bssid_bytes);
884
885 DBus::MessageIter writer(bss_properties["BSSID"].writer());
886 writer << bssid_bytes;
887 }
Paul Stewart0654ece2013-03-26 15:21:26 -0700888 bss_properties[WPASupplicant::kBSSPropertySignal].writer().
Thieu Lee41a72d2012-02-06 20:46:51 +0000889 append_int16(signal_strength);
Paul Stewart0654ece2013-03-26 15:21:26 -0700890 bss_properties[WPASupplicant::kBSSPropertyFrequency].writer().
Thieu Lee41a72d2012-02-06 20:46:51 +0000891 append_uint16(frequency);
Paul Stewart0654ece2013-03-26 15:21:26 -0700892 bss_properties[WPASupplicant::kBSSPropertyMode].writer().append_string(mode);
mukesh agrawalb4bc57d2011-12-07 01:07:47 +0000893 wifi_->BSSAddedTask(bss_path, bss_properties);
mukesh agrawal31950242011-07-14 11:53:38 -0700894}
895
Paul Stewarte369ece2012-05-22 09:11:03 -0700896WiFiObjectTest::TestProxyFactory::TestProxyFactory(WiFiObjectTest *test)
mukesh agrawalb20776f2012-02-10 16:00:36 -0800897 : test_(test) {
898 EXPECT_CALL(*this, CreateSupplicantBSSProxy(_, _, _)).Times(AnyNumber());
899 ON_CALL(*this, CreateSupplicantBSSProxy(_, _, _))
900 .WillByDefault(
901 Invoke(this, (&TestProxyFactory::CreateSupplicantBSSProxyInternal)));
902}
903
Paul Stewarte369ece2012-05-22 09:11:03 -0700904// Most of our tests involve using a real EventDispatcher object.
905class WiFiMainTest : public WiFiObjectTest {
906 public:
907 WiFiMainTest() : WiFiObjectTest(&dispatcher_) {}
908
909 protected:
910 EventDispatcher dispatcher_;
911};
912
mukesh agrawal31950242011-07-14 11:53:38 -0700913TEST_F(WiFiMainTest, ProxiesSetUpDuringStart) {
914 EXPECT_TRUE(GetSupplicantProcessProxy() == NULL);
Paul Stewart3c504012013-01-17 17:49:58 -0800915 EXPECT_TRUE(GetSupplicantInterfaceProxyFromWiFi() == NULL);
mukesh agrawal31950242011-07-14 11:53:38 -0700916
917 StartWiFi();
918 EXPECT_FALSE(GetSupplicantProcessProxy() == NULL);
Paul Stewart3c504012013-01-17 17:49:58 -0800919 EXPECT_FALSE(GetSupplicantInterfaceProxyFromWiFi() == NULL);
mukesh agrawal31950242011-07-14 11:53:38 -0700920}
921
Darin Petkov2b8e44e2012-06-25 15:13:26 +0200922TEST_F(WiFiMainTest, SupplicantPresent) {
923 EXPECT_FALSE(GetSupplicantPresent());
924}
925
926TEST_F(WiFiMainTest, OnSupplicantAppearStarted) {
927 EXPECT_TRUE(GetSupplicantProcessProxy() == NULL);
928
Paul Stewart0654ece2013-03-26 15:21:26 -0700929 EXPECT_CALL(*dbus_manager_, WatchName(WPASupplicant::kDBusAddr, _, _));
Darin Petkov2b8e44e2012-06-25 15:13:26 +0200930 StartWiFi(false); // No supplicant present.
931 EXPECT_TRUE(GetSupplicantProcessProxy() == NULL);
932
933 OnSupplicantAppear();
934 EXPECT_FALSE(GetSupplicantProcessProxy() == NULL);
Darin Petkov9cd7ca12012-07-03 11:06:40 +0200935
936 // If supplicant reappears while the device is started, the device should be
937 // restarted.
938 EXPECT_CALL(*manager(), DeregisterDevice(_));
939 EXPECT_CALL(*manager(), RegisterDevice(_));
940 OnSupplicantAppear();
Darin Petkov2b8e44e2012-06-25 15:13:26 +0200941}
942
943TEST_F(WiFiMainTest, OnSupplicantAppearStopped) {
944 EXPECT_TRUE(GetSupplicantProcessProxy() == NULL);
945
946 OnSupplicantAppear();
947 EXPECT_TRUE(GetSupplicantProcessProxy() == NULL);
Darin Petkov9cd7ca12012-07-03 11:06:40 +0200948
949 // If supplicant reappears while the device is stopped, the device should not
950 // be restarted.
951 EXPECT_CALL(*manager(), DeregisterDevice(_)).Times(0);
952 OnSupplicantAppear();
Darin Petkov2b8e44e2012-06-25 15:13:26 +0200953}
954
955TEST_F(WiFiMainTest, OnSupplicantVanishStarted) {
956 EXPECT_TRUE(GetSupplicantProcessProxy() == NULL);
957
958 StartWiFi();
959 EXPECT_FALSE(GetSupplicantProcessProxy() == NULL);
960 EXPECT_TRUE(GetSupplicantPresent());
961
962 EXPECT_CALL(*manager(), DeregisterDevice(_));
963 EXPECT_CALL(*manager(), RegisterDevice(_));
964 OnSupplicantVanish();
Darin Petkov2b8e44e2012-06-25 15:13:26 +0200965}
966
967TEST_F(WiFiMainTest, OnSupplicantVanishStopped) {
968 OnSupplicantAppear();
969 EXPECT_TRUE(GetSupplicantPresent());
970 EXPECT_CALL(*manager(), DeregisterDevice(_)).Times(0);
971 OnSupplicantVanish();
972}
973
Paul Stewart549d44c2012-07-03 12:40:25 -0700974TEST_F(WiFiMainTest, OnSupplicantVanishedWhileConnected) {
Paul Stewart3c504012013-01-17 17:49:58 -0800975 StartWiFi();
976 WiFiEndpointRefPtr endpoint;
977 WiFiServiceRefPtr service(
978 SetupConnectedService(DBus::Path(), &endpoint, NULL));
Paul Stewart549d44c2012-07-03 12:40:25 -0700979 ScopedMockLog log;
980 EXPECT_CALL(log, Log(_, _, _)).Times(AnyNumber());
Christopher Wileyc6184482012-10-24 15:31:56 -0700981 EXPECT_CALL(log, Log(logging::LOG_ERROR, _,
Paul Stewart549d44c2012-07-03 12:40:25 -0700982 EndsWith("silently resetting current_service_.")));
983 EXPECT_CALL(*manager(), DeregisterDevice(_))
984 .WillOnce(InvokeWithoutArgs(this, &WiFiObjectTest::StopWiFi));
Paul Stewart3c504012013-01-17 17:49:58 -0800985 scoped_ptr<EndpointRemovalHandler> handler(
986 MakeEndpointRemovalHandler(service));
987 EXPECT_CALL(*wifi_provider(), OnEndpointRemoved(EndpointMatch(endpoint)))
988 .WillOnce(Invoke(handler.get(),
989 &EndpointRemovalHandler::OnEndpointRemoved));
990 EXPECT_CALL(*GetSupplicantInterfaceProxy(), Disconnect()).Times(0);
Paul Stewart549d44c2012-07-03 12:40:25 -0700991 EXPECT_CALL(*manager(), RegisterDevice(_));
992 OnSupplicantVanish();
993 EXPECT_TRUE(GetCurrentService() == NULL);
994}
995
Wade Guthrie5a4e2ef2013-04-30 12:51:39 -0700996TEST_F(WiFiMainTest, CleanStart_FullScan) {
997 EnableFullScan();
mukesh agrawal31950242011-07-14 11:53:38 -0700998 EXPECT_CALL(*supplicant_process_proxy_, CreateInterface(_));
999 EXPECT_CALL(*supplicant_process_proxy_, GetInterface(_))
1000 .Times(AnyNumber())
1001 .WillRepeatedly(Throw(
1002 DBus::Error(
1003 "fi.w1.wpa_supplicant1.InterfaceUnknown",
1004 "test threw fi.w1.wpa_supplicant1.InterfaceUnknown")));
mukesh agrawalb66c6462012-05-07 11:45:25 -07001005 EXPECT_TRUE(GetScanTimer().IsCancelled());
mukesh agrawal31950242011-07-14 11:53:38 -07001006 StartWiFi();
Paul Stewart3c504012013-01-17 17:49:58 -08001007 EXPECT_CALL(*GetSupplicantInterfaceProxy(), Scan(_));
mukesh agrawal32399322011-09-01 10:53:43 -07001008 dispatcher_.DispatchPendingEvents();
mukesh agrawalb66c6462012-05-07 11:45:25 -07001009 EXPECT_FALSE(GetScanTimer().IsCancelled());
mukesh agrawal31950242011-07-14 11:53:38 -07001010}
1011
Wade Guthrie5a4e2ef2013-04-30 12:51:39 -07001012TEST_F(WiFiMainTest, CleanStart) {
1013 EXPECT_CALL(*supplicant_process_proxy_, CreateInterface(_));
1014 EXPECT_CALL(*supplicant_process_proxy_, GetInterface(_))
1015 .Times(AnyNumber())
1016 .WillRepeatedly(Throw(
1017 DBus::Error(
1018 "fi.w1.wpa_supplicant1.InterfaceUnknown",
1019 "test threw fi.w1.wpa_supplicant1.InterfaceUnknown")));
1020 EXPECT_TRUE(GetScanTimer().IsCancelled());
1021 StartWiFi();
1022 EXPECT_CALL(*scan_session_, InitiateScan());
1023 dispatcher_.DispatchPendingEvents();
1024 EXPECT_FALSE(GetScanTimer().IsCancelled());
1025}
1026
Paul Stewart835934a2012-12-06 19:27:09 -08001027TEST_F(WiFiMainTest, ClearCachedCredentials) {
Paul Stewart3c504012013-01-17 17:49:58 -08001028 StartWiFi();
Paul Stewart835934a2012-12-06 19:27:09 -08001029 DBus::Path network = "/test/path";
Paul Stewart3c504012013-01-17 17:49:58 -08001030 WiFiServiceRefPtr service(SetupConnectedService(network, NULL, NULL));
1031 EXPECT_CALL(*GetSupplicantInterfaceProxy(), RemoveNetwork(network));
Paul Stewart835934a2012-12-06 19:27:09 -08001032 ClearCachedCredentials(service);
1033}
1034
Paul Stewart0427cc12013-03-25 13:50:39 -07001035TEST_F(WiFiMainTest, NotifyEndpointChanged) {
1036 WiFiEndpointRefPtr endpoint =
1037 MakeEndpointWithMode("ssid", "00:00:00:00:00:00", kNetworkModeAdHoc);
1038 EXPECT_CALL(*wifi_provider(), OnEndpointUpdated(EndpointMatch(endpoint)));
1039 NotifyEndpointChanged(endpoint);
1040}
1041
Ben Chan381fdcc2012-10-14 21:10:36 -07001042TEST_F(WiFiMainTest, RemoveNetwork) {
1043 DBus::Path network = "/test/path";
Ben Chan381fdcc2012-10-14 21:10:36 -07001044 StartWiFi();
Paul Stewart3c504012013-01-17 17:49:58 -08001045 EXPECT_CALL(*GetSupplicantInterfaceProxy(), RemoveNetwork(network));
Ben Chan381fdcc2012-10-14 21:10:36 -07001046 EXPECT_TRUE(RemoveNetwork(network));
1047}
1048
1049TEST_F(WiFiMainTest, RemoveNetworkWhenSupplicantReturnsNetworkUnknown) {
1050 DBus::Path network = "/test/path";
Paul Stewart3c504012013-01-17 17:49:58 -08001051 EXPECT_CALL(*GetSupplicantInterfaceProxy(), RemoveNetwork(network))
Ben Chan381fdcc2012-10-14 21:10:36 -07001052 .WillRepeatedly(Throw(
1053 DBus::Error(
1054 "fi.w1.wpa_supplicant1.NetworkUnknown",
1055 "test threw fi.w1.wpa_supplicant1.NetworkUnknown")));
1056 StartWiFi();
1057 EXPECT_TRUE(RemoveNetwork(network));
1058}
1059
Arman Ugurayed8e6102012-11-29 14:47:20 -08001060TEST_F(WiFiMainTest, UseArpGateway) {
1061 EXPECT_CALL(dhcp_provider_, CreateConfig(kDeviceName, _, _, true))
1062 .WillOnce(Return(dhcp_config_));
1063 const_cast<WiFi *>(wifi().get())->AcquireIPConfig();
1064}
1065
Ben Chan381fdcc2012-10-14 21:10:36 -07001066TEST_F(WiFiMainTest, RemoveNetworkWhenSupplicantReturnsInvalidArgs) {
1067 DBus::Path network = "/test/path";
Paul Stewart3c504012013-01-17 17:49:58 -08001068 EXPECT_CALL(*GetSupplicantInterfaceProxy(), RemoveNetwork(network))
Ben Chan381fdcc2012-10-14 21:10:36 -07001069 .WillRepeatedly(Throw(
1070 DBus::Error(
1071 "fi.w1.wpa_supplicant1.InvalidArgs",
1072 "test threw fi.w1.wpa_supplicant1.InvalidArgs")));
1073 StartWiFi();
1074 EXPECT_FALSE(RemoveNetwork(network));
1075}
1076
1077TEST_F(WiFiMainTest, RemoveNetworkWhenSupplicantReturnsUnknownError) {
1078 DBus::Path network = "/test/path";
Paul Stewart3c504012013-01-17 17:49:58 -08001079 EXPECT_CALL(*GetSupplicantInterfaceProxy(), RemoveNetwork(network))
Ben Chan381fdcc2012-10-14 21:10:36 -07001080 .WillRepeatedly(Throw(
1081 DBus::Error(
1082 "fi.w1.wpa_supplicant1.UnknownError",
1083 "test threw fi.w1.wpa_supplicant1.UnknownError")));
1084 StartWiFi();
1085 EXPECT_FALSE(RemoveNetwork(network));
1086}
1087
Wade Guthrie5a4e2ef2013-04-30 12:51:39 -07001088TEST_F(WiFiMainTest, Restart_FullScan) {
1089 EnableFullScan();
mukesh agrawal31950242011-07-14 11:53:38 -07001090 EXPECT_CALL(*supplicant_process_proxy_, CreateInterface(_))
1091 .Times(AnyNumber())
1092 .WillRepeatedly(Throw(
1093 DBus::Error(
1094 "fi.w1.wpa_supplicant1.InterfaceExists",
Darin Petkov2b8e44e2012-06-25 15:13:26 +02001095 "test threw fi.w1.wpa_supplicant1.InterfaceExists")));
mukesh agrawal31950242011-07-14 11:53:38 -07001096 EXPECT_CALL(*supplicant_process_proxy_, GetInterface(_));
Paul Stewart3c504012013-01-17 17:49:58 -08001097 EXPECT_CALL(*GetSupplicantInterfaceProxy(), Scan(_));
mukesh agrawal31950242011-07-14 11:53:38 -07001098 StartWiFi();
mukesh agrawal32399322011-09-01 10:53:43 -07001099 dispatcher_.DispatchPendingEvents();
mukesh agrawal31950242011-07-14 11:53:38 -07001100}
1101
Wade Guthrie5a4e2ef2013-04-30 12:51:39 -07001102TEST_F(WiFiMainTest, Restart) {
1103 EXPECT_CALL(*supplicant_process_proxy_, CreateInterface(_))
1104 .Times(AnyNumber())
1105 .WillRepeatedly(Throw(
1106 DBus::Error(
1107 "fi.w1.wpa_supplicant1.InterfaceExists",
1108 "test threw fi.w1.wpa_supplicant1.InterfaceExists")));
1109 EXPECT_CALL(*scan_session_, InitiateScan());
1110 StartWiFi();
1111 dispatcher_.DispatchPendingEvents();
1112}
1113
mukesh agrawal31950242011-07-14 11:53:38 -07001114TEST_F(WiFiMainTest, StartClearsState) {
Paul Stewart3c504012013-01-17 17:49:58 -08001115 EXPECT_CALL(*GetSupplicantInterfaceProxy(), RemoveAllNetworks());
1116 EXPECT_CALL(*GetSupplicantInterfaceProxy(), FlushBSS(_));
mukesh agrawal31950242011-07-14 11:53:38 -07001117 StartWiFi();
1118}
1119
Wade Guthrie5a4e2ef2013-04-30 12:51:39 -07001120TEST_F(WiFiMainTest, NoScansWhileConnecting_FullScan) {
1121 EnableFullScan();
Christopher Wileyc68c8672012-11-20 16:52:21 -08001122 StartWiFi();
Paul Stewart3c504012013-01-17 17:49:58 -08001123 EXPECT_CALL(*GetSupplicantInterfaceProxy(), Scan(_)).Times(1);
1124 dispatcher_.DispatchPendingEvents();
1125 Mock::VerifyAndClearExpectations(GetSupplicantInterfaceProxy());
1126 MockWiFiServiceRefPtr service = MakeMockService(flimflam::kSecurityNone);
Christopher Wileyc68c8672012-11-20 16:52:21 -08001127 SetPendingService(service);
1128 // If we're connecting, we ignore scan requests to stay on channel.
Paul Stewart3c504012013-01-17 17:49:58 -08001129 EXPECT_CALL(*service, IsConnecting()).WillOnce(Return(true));
Christopher Wileyc68c8672012-11-20 16:52:21 -08001130 EXPECT_CALL(*GetSupplicantInterfaceProxy(), Scan(_)).Times(0);
Wade Guthrie5a4e2ef2013-04-30 12:51:39 -07001131 TriggerFullScan();
Christopher Wileyc68c8672012-11-20 16:52:21 -08001132 dispatcher_.DispatchPendingEvents();
1133 Mock::VerifyAndClearExpectations(GetSupplicantInterfaceProxy());
Paul Stewart3c504012013-01-17 17:49:58 -08001134 Mock::VerifyAndClearExpectations(service);
1135 EXPECT_CALL(*service, IsConnecting()).WillOnce(Return(false));
Christopher Wileyc68c8672012-11-20 16:52:21 -08001136 EXPECT_CALL(*GetSupplicantInterfaceProxy(), Scan(_)).Times(1);
Wade Guthrie5a4e2ef2013-04-30 12:51:39 -07001137 TriggerFullScan();
Christopher Wileyc68c8672012-11-20 16:52:21 -08001138 dispatcher_.DispatchPendingEvents();
1139 Mock::VerifyAndClearExpectations(GetSupplicantInterfaceProxy());
Paul Stewart3c504012013-01-17 17:49:58 -08001140 Mock::VerifyAndClearExpectations(service);
Christopher Wileyc68c8672012-11-20 16:52:21 -08001141 // Similarly, ignore scans when our connected service is reconnecting.
1142 SetPendingService(NULL);
1143 SetCurrentService(service);
Paul Stewart3c504012013-01-17 17:49:58 -08001144 EXPECT_CALL(*service, IsConnecting()).WillOnce(Return(true));
Christopher Wileyc68c8672012-11-20 16:52:21 -08001145 EXPECT_CALL(*GetSupplicantInterfaceProxy(), Scan(_)).Times(0);
Wade Guthrie5a4e2ef2013-04-30 12:51:39 -07001146 TriggerFullScan();
Christopher Wileyc68c8672012-11-20 16:52:21 -08001147 dispatcher_.DispatchPendingEvents();
1148 Mock::VerifyAndClearExpectations(GetSupplicantInterfaceProxy());
Paul Stewart3c504012013-01-17 17:49:58 -08001149 Mock::VerifyAndClearExpectations(service);
Christopher Wileyc68c8672012-11-20 16:52:21 -08001150 // But otherwise we'll honor the request.
Paul Stewart3c504012013-01-17 17:49:58 -08001151 EXPECT_CALL(*service, IsConnecting()).WillOnce(Return(false));
Christopher Wileyc68c8672012-11-20 16:52:21 -08001152 EXPECT_CALL(*GetSupplicantInterfaceProxy(), Scan(_)).Times(1);
Wade Guthrie5a4e2ef2013-04-30 12:51:39 -07001153 TriggerFullScan();
Christopher Wileyc68c8672012-11-20 16:52:21 -08001154 dispatcher_.DispatchPendingEvents();
1155 Mock::VerifyAndClearExpectations(GetSupplicantInterfaceProxy());
Paul Stewart3c504012013-01-17 17:49:58 -08001156 Mock::VerifyAndClearExpectations(service);
Christopher Wileyc68c8672012-11-20 16:52:21 -08001157}
1158
Wade Guthrie5a4e2ef2013-04-30 12:51:39 -07001159TEST_F(WiFiMainTest, NoScansWhileConnecting) {
1160 StartWiFi();
1161 EXPECT_CALL(*scan_session_, InitiateScan());
1162 dispatcher_.DispatchPendingEvents();
1163 Mock::VerifyAndClearExpectations(GetSupplicantInterfaceProxy());
1164 MockWiFiServiceRefPtr service = MakeMockService(flimflam::kSecurityNone);
1165 SetPendingService(service);
1166 // If we're connecting, we ignore scan requests to stay on channel.
1167 EXPECT_CALL(*service, IsConnecting()).WillOnce(Return(true));
1168 EXPECT_CALL(*GetSupplicantInterfaceProxy(), Scan(_)).Times(0);
1169 EXPECT_CALL(*scan_session_, InitiateScan()).Times(0);
1170 TriggerFullScan();
1171 dispatcher_.DispatchPendingEvents();
1172 Mock::VerifyAndClearExpectations(GetSupplicantInterfaceProxy());
1173 Mock::VerifyAndClearExpectations(service);
1174 EXPECT_CALL(*service, IsConnecting()).WillOnce(Return(false));
1175 EXPECT_CALL(*GetSupplicantInterfaceProxy(), Scan(_)).Times(1);
1176 EXPECT_CALL(*scan_session_, InitiateScan()).Times(0);
1177 TriggerFullScan();
1178 dispatcher_.DispatchPendingEvents();
1179 Mock::VerifyAndClearExpectations(GetSupplicantInterfaceProxy());
1180 Mock::VerifyAndClearExpectations(service);
1181 // Similarly, ignore scans when our connected service is reconnecting.
1182 SetPendingService(NULL);
1183 SetCurrentService(service);
1184 EXPECT_CALL(*service, IsConnecting()).WillOnce(Return(true));
1185 EXPECT_CALL(*GetSupplicantInterfaceProxy(), Scan(_)).Times(0);
Wade Guthriedf6d61b2013-07-17 11:43:55 -07001186 EXPECT_TRUE(IsScanSessionNull());
Wade Guthrie5a4e2ef2013-04-30 12:51:39 -07001187 TriggerFullScan();
1188 dispatcher_.DispatchPendingEvents();
1189 Mock::VerifyAndClearExpectations(GetSupplicantInterfaceProxy());
1190 Mock::VerifyAndClearExpectations(service);
1191 // But otherwise we'll honor the request.
1192 EXPECT_CALL(*service, IsConnecting()).WillOnce(Return(false));
1193 EXPECT_CALL(*GetSupplicantInterfaceProxy(), Scan(_)).Times(1);
Wade Guthriedf6d61b2013-07-17 11:43:55 -07001194 EXPECT_TRUE(IsScanSessionNull());
Wade Guthrie5a4e2ef2013-04-30 12:51:39 -07001195 TriggerFullScan();
1196 dispatcher_.DispatchPendingEvents();
1197 Mock::VerifyAndClearExpectations(GetSupplicantInterfaceProxy());
1198 Mock::VerifyAndClearExpectations(service);
1199}
1200
1201TEST_F(WiFiMainTest, ResumeStartsScanWhenIdle_FullScan) {
1202 EnableFullScan();
Paul Stewart3c504012013-01-17 17:49:58 -08001203 EXPECT_CALL(*GetSupplicantInterfaceProxy(), Scan(_));
Gary Morainac1bdb42012-02-16 17:42:29 -08001204 StartWiFi();
1205 dispatcher_.DispatchPendingEvents();
Paul Stewart3c504012013-01-17 17:49:58 -08001206 Mock::VerifyAndClearExpectations(GetSupplicantInterfaceProxy());
mukesh agrawal5c05b292012-03-07 10:12:52 -08001207 ReportScanDone();
Gary Morainac1bdb42012-02-16 17:42:29 -08001208 ASSERT_TRUE(wifi()->IsIdle());
1209 EXPECT_CALL(*GetSupplicantInterfaceProxy(), Scan(_));
mukesh agrawal2f9df4e2012-08-08 12:29:20 -07001210 OnAfterResume();
Gary Morainac1bdb42012-02-16 17:42:29 -08001211 dispatcher_.DispatchPendingEvents();
1212}
1213
Wade Guthrie5a4e2ef2013-04-30 12:51:39 -07001214TEST_F(WiFiMainTest, ResumeStartsScanWhenIdle) {
1215 EXPECT_CALL(*scan_session_, InitiateScan());
1216 StartWiFi();
1217 dispatcher_.DispatchPendingEvents();
1218 Mock::VerifyAndClearExpectations(GetSupplicantInterfaceProxy());
1219 ReportScanDone();
1220 ASSERT_TRUE(wifi()->IsIdle());
1221 dispatcher_.DispatchPendingEvents();
Wade Guthrie5a4e2ef2013-04-30 12:51:39 -07001222 OnAfterResume();
Wade Guthrie0cf3c982013-05-29 09:11:35 -07001223 EXPECT_TRUE(scan_session_ != NULL);
1224 InstallMockScanSession();
1225 EXPECT_CALL(*scan_session_, InitiateScan());
Wade Guthrie5a4e2ef2013-04-30 12:51:39 -07001226 dispatcher_.DispatchPendingEvents();
1227}
1228
1229TEST_F(WiFiMainTest, SuspendDoesNotStartScan_FullScan) {
1230 EnableFullScan();
Paul Stewart3c504012013-01-17 17:49:58 -08001231 EXPECT_CALL(*GetSupplicantInterfaceProxy(), Scan(_));
Gary Morainac1bdb42012-02-16 17:42:29 -08001232 StartWiFi();
1233 dispatcher_.DispatchPendingEvents();
Paul Stewart3c504012013-01-17 17:49:58 -08001234 Mock::VerifyAndClearExpectations(GetSupplicantInterfaceProxy());
Gary Morainac1bdb42012-02-16 17:42:29 -08001235 ASSERT_TRUE(wifi()->IsIdle());
1236 EXPECT_CALL(*GetSupplicantInterfaceProxy(), Scan(_)).Times(0);
mukesh agrawal2f9df4e2012-08-08 12:29:20 -07001237 OnBeforeSuspend();
Gary Morainac1bdb42012-02-16 17:42:29 -08001238 dispatcher_.DispatchPendingEvents();
1239}
1240
Wade Guthrie5a4e2ef2013-04-30 12:51:39 -07001241TEST_F(WiFiMainTest, SuspendDoesNotStartScan) {
1242 EXPECT_CALL(*scan_session_, InitiateScan());
1243 StartWiFi();
1244 dispatcher_.DispatchPendingEvents();
1245 Mock::VerifyAndClearExpectations(GetSupplicantInterfaceProxy());
1246 ASSERT_TRUE(wifi()->IsIdle());
1247 EXPECT_CALL(*GetSupplicantInterfaceProxy(), Scan(_)).Times(0);
1248 EXPECT_CALL(*scan_session_, InitiateScan()).Times(0);
1249 OnBeforeSuspend();
1250 dispatcher_.DispatchPendingEvents();
1251}
1252
1253TEST_F(WiFiMainTest, ResumeDoesNotStartScanWhenNotIdle_FullScan) {
1254 EnableFullScan();
Paul Stewart3c504012013-01-17 17:49:58 -08001255 EXPECT_CALL(*GetSupplicantInterfaceProxy(), Scan(_));
Gary Morainac1bdb42012-02-16 17:42:29 -08001256 StartWiFi();
Gary Morainac1bdb42012-02-16 17:42:29 -08001257 dispatcher_.DispatchPendingEvents();
Paul Stewart3c504012013-01-17 17:49:58 -08001258 Mock::VerifyAndClearExpectations(GetSupplicantInterfaceProxy());
1259 WiFiServiceRefPtr service(SetupConnectedService(DBus::Path(), NULL, NULL));
1260 EXPECT_FALSE(wifi()->IsIdle());
1261 ScopedMockLog log;
mukesh agrawal3ab56ac2012-05-31 14:41:43 -07001262 EXPECT_CALL(log, Log(_, _, _)).Times(AnyNumber());
Wade Guthrie0cf3c982013-05-29 09:11:35 -07001263 EXPECT_CALL(log, Log(_, _, EndsWith("already connecting or connected.")));
Gary Morainac1bdb42012-02-16 17:42:29 -08001264 EXPECT_CALL(*GetSupplicantInterfaceProxy(), Scan(_)).Times(0);
mukesh agrawal2f9df4e2012-08-08 12:29:20 -07001265 OnAfterResume();
Gary Morainac1bdb42012-02-16 17:42:29 -08001266 dispatcher_.DispatchPendingEvents();
1267}
1268
Wade Guthrie5a4e2ef2013-04-30 12:51:39 -07001269TEST_F(WiFiMainTest, ResumeDoesNotStartScanWhenNotIdle) {
1270 EXPECT_CALL(*scan_session_, InitiateScan());
1271 StartWiFi();
1272 dispatcher_.DispatchPendingEvents();
1273 Mock::VerifyAndClearExpectations(GetSupplicantInterfaceProxy());
1274 WiFiServiceRefPtr service(SetupConnectedService(DBus::Path(), NULL, NULL));
1275 EXPECT_FALSE(wifi()->IsIdle());
1276 ScopedMockLog log;
1277 EXPECT_CALL(log, Log(_, _, _)).Times(AnyNumber());
Wade Guthrie0cf3c982013-05-29 09:11:35 -07001278 EXPECT_CALL(log, Log(_, _, EndsWith("already connecting or connected.")));
Wade Guthrie5a4e2ef2013-04-30 12:51:39 -07001279 EXPECT_CALL(*GetSupplicantInterfaceProxy(), Scan(_)).Times(0);
Wade Guthriedf6d61b2013-07-17 11:43:55 -07001280 EXPECT_TRUE(IsScanSessionNull());
Wade Guthrie5a4e2ef2013-04-30 12:51:39 -07001281 OnAfterResume();
1282 dispatcher_.DispatchPendingEvents();
1283}
1284
mukesh agrawal31950242011-07-14 11:53:38 -07001285TEST_F(WiFiMainTest, ScanResults) {
Paul Stewart3c504012013-01-17 17:49:58 -08001286 EXPECT_CALL(*wifi_provider(), OnEndpointAdded(_)).Times(5);
mukesh agrawal31950242011-07-14 11:53:38 -07001287 StartWiFi();
Thieu Lee41a72d2012-02-06 20:46:51 +00001288 ReportBSS("bss0", "ssid0", "00:00:00:00:00:00", 0, 0, kNetworkModeAdHoc);
mukesh agrawal31950242011-07-14 11:53:38 -07001289 ReportBSS(
Thieu Lee41a72d2012-02-06 20:46:51 +00001290 "bss1", "ssid1", "00:00:00:00:00:01", 1, 0, kNetworkModeInfrastructure);
mukesh agrawal31950242011-07-14 11:53:38 -07001291 ReportBSS(
Thieu Lee41a72d2012-02-06 20:46:51 +00001292 "bss2", "ssid2", "00:00:00:00:00:02", 2, 0, kNetworkModeInfrastructure);
mukesh agrawal31950242011-07-14 11:53:38 -07001293 ReportBSS(
Thieu Lee41a72d2012-02-06 20:46:51 +00001294 "bss3", "ssid3", "00:00:00:00:00:03", 3, 0, kNetworkModeInfrastructure);
1295 const uint16 frequency = 2412;
1296 ReportBSS("bss4", "ssid4", "00:00:00:00:00:04", 4, frequency,
1297 kNetworkModeAdHoc);
1298
1299 const WiFi::EndpointMap &endpoints_by_rpcid = GetEndpointMap();
1300 EXPECT_EQ(5, endpoints_by_rpcid.size());
1301
1302 WiFi::EndpointMap::const_iterator i;
1303 WiFiEndpointRefPtr endpoint;
1304 for (i = endpoints_by_rpcid.begin();
1305 i != endpoints_by_rpcid.end();
1306 ++i) {
1307 if (i->second->bssid_string() == "00:00:00:00:00:04")
1308 break;
1309 }
1310 ASSERT_TRUE(i != endpoints_by_rpcid.end());
1311 EXPECT_EQ(4, i->second->signal_strength());
1312 EXPECT_EQ(frequency, i->second->frequency());
1313 EXPECT_EQ("adhoc", i->second->network_mode());
mukesh agrawal31950242011-07-14 11:53:38 -07001314}
1315
mukesh agrawal31950242011-07-14 11:53:38 -07001316TEST_F(WiFiMainTest, ScanCompleted) {
1317 StartWiFi();
Paul Stewart3c504012013-01-17 17:49:58 -08001318 WiFiEndpointRefPtr ap0 = MakeEndpointWithMode("ssid0", "00:00:00:00:00:00",
1319 kNetworkModeAdHoc);
1320 WiFiEndpointRefPtr ap1 = MakeEndpoint("ssid1", "00:00:00:00:00:01");
1321 WiFiEndpointRefPtr ap2 = MakeEndpoint("ssid2", "00:00:00:00:00:02");
1322 EXPECT_CALL(*wifi_provider(), OnEndpointAdded(EndpointMatch(ap0))).Times(1);
1323 EXPECT_CALL(*wifi_provider(), OnEndpointAdded(EndpointMatch(ap1))).Times(1);
1324 EXPECT_CALL(*wifi_provider(), OnEndpointAdded(EndpointMatch(ap2))).Times(1);
1325 ReportBSS("bss0", ap0->ssid_string(), ap0->bssid_string(), 0, 0,
1326 kNetworkModeAdHoc);
1327 ReportBSS("bss1", ap1->ssid_string(), ap1->bssid_string(), 0, 0,
1328 kNetworkModeInfrastructure);
1329 ReportBSS("bss2", ap2->ssid_string(), ap2->bssid_string(), 0, 0,
1330 kNetworkModeInfrastructure);
mukesh agrawal31950242011-07-14 11:53:38 -07001331 ReportScanDone();
Paul Stewart3c504012013-01-17 17:49:58 -08001332 Mock::VerifyAndClearExpectations(wifi_provider());
1333
1334 EXPECT_CALL(*wifi_provider(), OnEndpointAdded(_)).Times(0);
mukesh agrawalb3857612012-01-18 16:23:29 -08001335
mukesh agrawale9adda12012-02-09 18:33:48 -08001336 // BSSes with SSIDs that start with NULL should be filtered.
Thieu Lee41a72d2012-02-06 20:46:51 +00001337 ReportBSS("bss3", string(1, 0), "00:00:00:00:00:03", 3, 0, kNetworkModeAdHoc);
mukesh agrawalb3857612012-01-18 16:23:29 -08001338
mukesh agrawale9adda12012-02-09 18:33:48 -08001339 // BSSes with empty SSIDs should be filtered.
1340 ReportBSS("bss3", string(), "00:00:00:00:00:03", 3, 0, kNetworkModeAdHoc);
mukesh agrawal8a3188d2011-12-01 20:56:44 +00001341}
1342
1343TEST_F(WiFiMainTest, LoneBSSRemovedWhileConnected) {
1344 StartWiFi();
Paul Stewart3c504012013-01-17 17:49:58 -08001345 WiFiEndpointRefPtr endpoint;
1346 DBus::Path bss_path;
1347 WiFiServiceRefPtr service(
1348 SetupConnectedService(DBus::Path(), &endpoint, &bss_path));
1349 scoped_ptr<EndpointRemovalHandler> handler(
1350 MakeEndpointRemovalHandler(service));
1351 EXPECT_CALL(*wifi_provider(), OnEndpointRemoved(EndpointMatch(endpoint)))
1352 .WillOnce(Invoke(handler.get(),
1353 &EndpointRemovalHandler::OnEndpointRemoved));
mukesh agrawal8a3188d2011-12-01 20:56:44 +00001354 EXPECT_CALL(*GetSupplicantInterfaceProxy(), Disconnect());
Paul Stewart3c504012013-01-17 17:49:58 -08001355 RemoveBSS(bss_path);
mukesh agrawal261daca2011-12-02 18:56:56 +00001356}
1357
1358TEST_F(WiFiMainTest, NonSolitaryBSSRemoved) {
1359 StartWiFi();
Paul Stewart3c504012013-01-17 17:49:58 -08001360 WiFiEndpointRefPtr endpoint;
1361 DBus::Path bss_path;
1362 WiFiServiceRefPtr service(
1363 SetupConnectedService(DBus::Path(), &endpoint, &bss_path));
1364 EXPECT_CALL(*wifi_provider(), OnEndpointRemoved(EndpointMatch(endpoint)))
1365 .WillOnce(Return(reinterpret_cast<WiFiService *>(NULL)));
1366 EXPECT_CALL(*GetSupplicantInterfaceProxy(), Disconnect()).Times(0);
1367 RemoveBSS(bss_path);
mukesh agrawal31950242011-07-14 11:53:38 -07001368}
1369
Paul Stewart835934a2012-12-06 19:27:09 -08001370TEST_F(WiFiMainTest, ReconnectPreservesDBusPath) {
Paul Stewart835934a2012-12-06 19:27:09 -08001371 StartWiFi();
Paul Stewart3c504012013-01-17 17:49:58 -08001372 DBus::Path kPath = "/test/path";
1373 WiFiServiceRefPtr service(SetupConnectedService(kPath, NULL, NULL));
Paul Stewart835934a2012-12-06 19:27:09 -08001374
1375 // Return the service to a connectable state.
Paul Stewart3c504012013-01-17 17:49:58 -08001376 EXPECT_CALL(*GetSupplicantInterfaceProxy(), Disconnect());
Paul Stewart835934a2012-12-06 19:27:09 -08001377 InitiateDisconnect(service);
Paul Stewart3c504012013-01-17 17:49:58 -08001378 Mock::VerifyAndClearExpectations(GetSupplicantInterfaceProxy());
1379
1380 // Complete the disconnection by reporting a BSS change.
Paul Stewart0654ece2013-03-26 15:21:26 -07001381 ReportCurrentBSSChanged(WPASupplicant::kCurrentBSSNull);
Paul Stewart835934a2012-12-06 19:27:09 -08001382
1383 // A second connection attempt should remember the DBus path associated
1384 // with this service.
Paul Stewart3c504012013-01-17 17:49:58 -08001385 EXPECT_CALL(*GetSupplicantInterfaceProxy(), AddNetwork(_)).Times(0);
1386 EXPECT_CALL(*GetSupplicantInterfaceProxy(), SelectNetwork(kPath));
Paul Stewart835934a2012-12-06 19:27:09 -08001387 InitiateConnect(service);
1388}
1389
mukesh agrawal0ed0f2e2011-12-05 20:36:17 +00001390TEST_F(WiFiMainTest, DisconnectPendingService) {
mukesh agrawal0ed0f2e2011-12-05 20:36:17 +00001391 StartWiFi();
Paul Stewart3c504012013-01-17 17:49:58 -08001392 MockWiFiServiceRefPtr service(
1393 SetupConnectingService(DBus::Path(), NULL, NULL));
1394 EXPECT_TRUE(GetPendingService() == service.get());
1395 EXPECT_CALL(*GetSupplicantInterfaceProxy(), Disconnect());
1396 EXPECT_CALL(*service, SetState(Service::kStateIdle)).Times(AtLeast(1));
mukesh agrawal0ed0f2e2011-12-05 20:36:17 +00001397 InitiateDisconnect(service);
Paul Stewart3c504012013-01-17 17:49:58 -08001398 Mock::VerifyAndClearExpectations(service.get());
mukesh agrawal0ed0f2e2011-12-05 20:36:17 +00001399 EXPECT_TRUE(GetPendingService() == NULL);
1400}
1401
1402TEST_F(WiFiMainTest, DisconnectPendingServiceWithCurrent) {
mukesh agrawal0ed0f2e2011-12-05 20:36:17 +00001403 StartWiFi();
Paul Stewart3c504012013-01-17 17:49:58 -08001404 MockWiFiServiceRefPtr service0(
1405 SetupConnectedService(DBus::Path(), NULL, NULL));
1406 EXPECT_EQ(service0, GetCurrentService());
1407 EXPECT_EQ(NULL, GetPendingService().get());
mukesh agrawal0ed0f2e2011-12-05 20:36:17 +00001408
Paul Stewart3c504012013-01-17 17:49:58 -08001409 // We don't explicitly call Disconnect() while transitioning to a new
1410 // service. Instead, we use the side-effect of SelectNetwork (verified in
1411 // SetupConnectingService).
1412 EXPECT_CALL(*GetSupplicantInterfaceProxy(), Disconnect()).Times(0);
1413 MockWiFiServiceRefPtr service1(
1414 SetupConnectingService("/new/path", NULL, NULL));
1415 Mock::VerifyAndClearExpectations(GetSupplicantInterfaceProxy());
mukesh agrawal0ed0f2e2011-12-05 20:36:17 +00001416
1417 EXPECT_EQ(service0, GetCurrentService());
1418 EXPECT_EQ(service1, GetPendingService());
Paul Stewart3c504012013-01-17 17:49:58 -08001419 EXPECT_CALL(*service1, SetState(Service::kStateIdle)).Times(AtLeast(1));
1420 EXPECT_CALL(*GetSupplicantInterfaceProxy(), Disconnect());
mukesh agrawal0ed0f2e2011-12-05 20:36:17 +00001421 InitiateDisconnect(service1);
Paul Stewart3c504012013-01-17 17:49:58 -08001422 Mock::VerifyAndClearExpectations(service1.get());
mukesh agrawal0ed0f2e2011-12-05 20:36:17 +00001423
1424 // |current_service_| will be unchanged until supplicant signals
1425 // that CurrentBSS has changed.
1426 EXPECT_EQ(service0, GetCurrentService());
1427 // |pending_service_| is updated immediately.
Paul Stewart3c504012013-01-17 17:49:58 -08001428 EXPECT_EQ(NULL, GetPendingService().get());
Paul Stewart2b05e622012-07-13 20:38:44 -07001429 EXPECT_TRUE(GetPendingTimeout().IsCancelled());
mukesh agrawal0ed0f2e2011-12-05 20:36:17 +00001430}
1431
1432TEST_F(WiFiMainTest, DisconnectCurrentService) {
Paul Stewart3c504012013-01-17 17:49:58 -08001433 StartWiFi();
1434 ::DBus::Path kPath("/fake/path");
1435 MockWiFiServiceRefPtr service(SetupConnectedService(kPath, NULL, NULL));
1436 EXPECT_CALL(*GetSupplicantInterfaceProxy(), Disconnect());
mukesh agrawal0ed0f2e2011-12-05 20:36:17 +00001437 InitiateDisconnect(service);
1438
1439 // |current_service_| should not change until supplicant reports
1440 // a BSS change.
1441 EXPECT_EQ(service, GetCurrentService());
Paul Stewart835934a2012-12-06 19:27:09 -08001442
1443 // Expect that the entry associated with this network will be disabled.
1444 MockSupplicantNetworkProxy *network_proxy = CreateSupplicantNetworkProxy();
1445 EXPECT_CALL(*proxy_factory(), CreateSupplicantNetworkProxy(
Paul Stewart0654ece2013-03-26 15:21:26 -07001446 kPath, WPASupplicant::kDBusAddr))
Paul Stewart835934a2012-12-06 19:27:09 -08001447 .WillOnce(Return(network_proxy));
1448 EXPECT_CALL(*network_proxy, SetEnabled(false));
Paul Stewart735eab52013-03-29 09:19:23 -07001449 EXPECT_CALL(*eap_state_handler_, Reset());
Paul Stewart3c504012013-01-17 17:49:58 -08001450 EXPECT_CALL(*GetSupplicantInterfaceProxy(), RemoveNetwork(kPath)).Times(0);
Paul Stewart0654ece2013-03-26 15:21:26 -07001451 ReportCurrentBSSChanged(WPASupplicant::kCurrentBSSNull);
Paul Stewart3c504012013-01-17 17:49:58 -08001452 EXPECT_EQ(NULL, GetCurrentService().get());
1453 Mock::VerifyAndClearExpectations(GetSupplicantInterfaceProxy());
mukesh agrawal0ed0f2e2011-12-05 20:36:17 +00001454}
1455
Christopher Wileyc6184482012-10-24 15:31:56 -07001456TEST_F(WiFiMainTest, DisconnectCurrentServiceWithErrors) {
Paul Stewart3c504012013-01-17 17:49:58 -08001457 StartWiFi();
1458 DBus::Path kPath("/fake/path");
1459 WiFiServiceRefPtr service(SetupConnectedService(kPath, NULL, NULL));
1460 EXPECT_CALL(*GetSupplicantInterfaceProxy(), Disconnect())
Christopher Wileyc6184482012-10-24 15:31:56 -07001461 .WillOnce(InvokeWithoutArgs(this, (&WiFiMainTest::ThrowDBusError)));
Paul Stewart3c504012013-01-17 17:49:58 -08001462 EXPECT_CALL(*GetSupplicantInterfaceProxy(), RemoveNetwork(kPath)).Times(1);
Christopher Wileyc6184482012-10-24 15:31:56 -07001463 InitiateDisconnect(service);
1464
1465 // We may sometimes fail to disconnect via supplicant, and we patch up some
1466 // state when this happens.
1467 EXPECT_EQ(NULL, GetCurrentService().get());
1468 EXPECT_EQ(NULL, wifi()->selected_service().get());
1469}
1470
mukesh agrawal0ed0f2e2011-12-05 20:36:17 +00001471TEST_F(WiFiMainTest, DisconnectCurrentServiceWithPending) {
mukesh agrawal0ed0f2e2011-12-05 20:36:17 +00001472 StartWiFi();
Paul Stewart3c504012013-01-17 17:49:58 -08001473 WiFiServiceRefPtr service0(SetupConnectedService(DBus::Path(), NULL, NULL));
1474 WiFiServiceRefPtr service1(SetupConnectingService(DBus::Path(), NULL, NULL));
mukesh agrawal0ed0f2e2011-12-05 20:36:17 +00001475 EXPECT_EQ(service0, GetCurrentService());
1476 EXPECT_EQ(service1, GetPendingService());
Paul Stewart3c504012013-01-17 17:49:58 -08001477 EXPECT_CALL(*GetSupplicantInterfaceProxy(), Disconnect()).Times(0);
mukesh agrawal0ed0f2e2011-12-05 20:36:17 +00001478 InitiateDisconnect(service0);
1479
1480 EXPECT_EQ(service0, GetCurrentService());
1481 EXPECT_EQ(service1, GetPendingService());
Paul Stewart2b05e622012-07-13 20:38:44 -07001482 EXPECT_FALSE(GetPendingTimeout().IsCancelled());
1483}
1484
Paul Stewart17d90652013-04-04 15:09:11 -07001485TEST_F(WiFiMainTest, TimeoutPendingServiceWithEndpoints) {
Wade Guthrie0cf3c982013-05-29 09:11:35 -07001486 ScopedMockLog log;
1487 ScopeLogger::GetInstance()->EnableScopesByName("wifi");
1488 ScopeLogger::GetInstance()->set_verbose_level(10);
1489
Wade Guthrie44f290d2013-05-28 10:16:25 -07001490 // Scan.
Wade Guthrie0cf3c982013-05-29 09:11:35 -07001491 VerifyScanState(WiFi::kScanIdle, WiFi::kScanMethodNone);
Wade Guthrie44f290d2013-05-28 10:16:25 -07001492 EXPECT_CALL(*metrics(), NotifyDeviceScanStarted(_));
Paul Stewart2b05e622012-07-13 20:38:44 -07001493 StartWiFi();
Wade Guthrie0cf3c982013-05-29 09:11:35 -07001494 EXPECT_CALL(*adaptor_, EmitBoolChanged(_, _)).Times(AnyNumber());
1495 EXPECT_CALL(*adaptor_, EmitBoolChanged(flimflam::kScanningProperty, true));
1496 dispatcher_.DispatchPendingEvents();
1497 VerifyScanState(WiFi::kScanScanning, WiFi::kScanMethodProgressive);
Wade Guthrie44f290d2013-05-28 10:16:25 -07001498
Paul Stewart2b05e622012-07-13 20:38:44 -07001499 const base::CancelableClosure &pending_timeout = GetPendingTimeout();
1500 EXPECT_TRUE(pending_timeout.IsCancelled());
Wade Guthrie44f290d2013-05-28 10:16:25 -07001501
1502 // Initiate a connection.
1503 WiFiEndpointRefPtr endpoint;
1504 ::DBus::Path bss_path;
Wade Guthrie44f290d2013-05-28 10:16:25 -07001505 EXPECT_CALL(*metrics(), NotifyDeviceScanFinished(_));
1506 EXPECT_CALL(*metrics(), NotifyDeviceConnectStarted(_, _));
1507 EXPECT_CALL(*adaptor_, EmitBoolChanged(flimflam::kScanningProperty, false));
Wade Guthriedf6d61b2013-07-17 11:43:55 -07001508 MockWiFiServiceRefPtr service =
1509 SetupConnectingService(DBus::Path(), &endpoint, &bss_path);
1510 ReportScanDoneKeepScanSession();
Wade Guthrie44f290d2013-05-28 10:16:25 -07001511 dispatcher_.DispatchPendingEvents();
1512 VerifyScanState(WiFi::kScanConnecting, WiFi::kScanMethodProgressive);
1513
1514 // Timeout the connection attempt.
Paul Stewart2b05e622012-07-13 20:38:44 -07001515 EXPECT_FALSE(pending_timeout.IsCancelled());
1516 EXPECT_EQ(service, GetPendingService());
Paul Stewart17d90652013-04-04 15:09:11 -07001517 // Simulate a service with a wifi_ reference calling DisconnectFrom().
1518 EXPECT_CALL(*service, DisconnectWithFailure(Service::kFailureOutOfRange, _))
1519 .WillOnce(InvokeWithoutArgs(this, &WiFiObjectTest::ResetPendingService));
1520 EXPECT_CALL(*service, HasEndpoints()).Times(0);
1521 // DisconnectFrom() should not be called directly from WiFi.
1522 EXPECT_CALL(*service, SetState(Service::kStateIdle)).Times(0);
1523 EXPECT_CALL(*GetSupplicantInterfaceProxy(), Disconnect()).Times(0);
Wade Guthrie0cf3c982013-05-29 09:11:35 -07001524
Wade Guthrie44f290d2013-05-28 10:16:25 -07001525 // Innocuous redundant call to NotifyDeviceScanFinished.
1526 EXPECT_CALL(*metrics(), NotifyDeviceScanFinished(_)).Times(AnyNumber());
1527 EXPECT_CALL(*metrics(), ResetConnectTimer(_)).Times(AnyNumber());
1528 EXPECT_CALL(*metrics(), NotifyDeviceConnectFinished(_)).Times(0);
Wade Guthrie0cf3c982013-05-29 09:11:35 -07001529 EXPECT_CALL(log, Log(_, _, _)).Times(AnyNumber());
1530 EXPECT_CALL(log, Log(_, _,
1531 HasSubstr("-> PROGRESSIVE_FINISHED_NOCONNECTION")));
Paul Stewart3c504012013-01-17 17:49:58 -08001532 pending_timeout.callback().Run();
Wade Guthrie0cf3c982013-05-29 09:11:35 -07001533 VerifyScanState(WiFi::kScanIdle, WiFi::kScanMethodNone);
Paul Stewart17d90652013-04-04 15:09:11 -07001534 Mock::VerifyAndClearExpectations(service);
Wade Guthrie0cf3c982013-05-29 09:11:35 -07001535
1536 ScopeLogger::GetInstance()->set_verbose_level(0);
1537 ScopeLogger::GetInstance()->EnableScopesByName("-wifi");
Paul Stewart17d90652013-04-04 15:09:11 -07001538}
1539
1540TEST_F(WiFiMainTest, TimeoutPendingServiceWithoutEndpoints) {
1541 StartWiFi();
1542 const base::CancelableClosure &pending_timeout = GetPendingTimeout();
1543 EXPECT_TRUE(pending_timeout.IsCancelled());
1544 MockWiFiServiceRefPtr service(
1545 SetupConnectingService(DBus::Path(), NULL, NULL));
1546 EXPECT_FALSE(pending_timeout.IsCancelled());
1547 EXPECT_EQ(service, GetPendingService());
1548 // We expect the service to get a disconnect call, but in this scenario
1549 // the service does nothing.
1550 EXPECT_CALL(*service, DisconnectWithFailure(Service::kFailureOutOfRange, _));
1551 EXPECT_CALL(*service, HasEndpoints()).WillOnce(Return(false));
1552 // DisconnectFrom() should be called directly from WiFi.
1553 EXPECT_CALL(*service, SetState(Service::kStateIdle)).Times(AtLeast(1));
1554 EXPECT_CALL(*GetSupplicantInterfaceProxy(), Disconnect());
1555 pending_timeout.callback().Run();
1556 EXPECT_EQ(NULL, GetPendingService().get());
mukesh agrawal0ed0f2e2011-12-05 20:36:17 +00001557}
1558
1559TEST_F(WiFiMainTest, DisconnectInvalidService) {
mukesh agrawal0ed0f2e2011-12-05 20:36:17 +00001560 StartWiFi();
Paul Stewart3c504012013-01-17 17:49:58 -08001561 MockWiFiServiceRefPtr service;
1562 MakeNewEndpointAndService(0, 0, kNetworkModeAdHoc, NULL, &service);
1563 EXPECT_CALL(*GetSupplicantInterfaceProxy(), Disconnect()).Times(0);
mukesh agrawal0ed0f2e2011-12-05 20:36:17 +00001564 InitiateDisconnect(service);
1565}
1566
1567TEST_F(WiFiMainTest, DisconnectCurrentServiceFailure) {
Paul Stewart3c504012013-01-17 17:49:58 -08001568 StartWiFi();
1569 DBus::Path kPath("/fake/path");
1570 WiFiServiceRefPtr service(SetupConnectedService(kPath, NULL, NULL));
1571 EXPECT_CALL(*GetSupplicantInterfaceProxy(), Disconnect())
mukesh agrawal0ed0f2e2011-12-05 20:36:17 +00001572 .WillRepeatedly(Throw(
1573 DBus::Error(
1574 "fi.w1.wpa_supplicant1.NotConnected",
1575 "test threw fi.w1.wpa_supplicant1.NotConnected")));
Paul Stewart3c504012013-01-17 17:49:58 -08001576 EXPECT_CALL(*GetSupplicantInterfaceProxy(), RemoveNetwork(kPath));
mukesh agrawal0ed0f2e2011-12-05 20:36:17 +00001577 InitiateDisconnect(service);
Paul Stewart3c504012013-01-17 17:49:58 -08001578 EXPECT_EQ(NULL, GetCurrentService().get());
mukesh agrawal0ed0f2e2011-12-05 20:36:17 +00001579}
1580
mukesh agrawal5c4dd0b2011-09-14 13:53:14 -07001581TEST_F(WiFiMainTest, Stop) {
mukesh agrawalc01f3982012-01-24 13:48:39 -08001582 StartWiFi();
Paul Stewart3c504012013-01-17 17:49:58 -08001583 WiFiEndpointRefPtr endpoint0;
1584 ::DBus::Path kPath("/fake/path");
1585 WiFiServiceRefPtr service0(SetupConnectedService(kPath, &endpoint0, NULL));
1586 WiFiEndpointRefPtr endpoint1;
1587 MakeNewEndpointAndService(0, 0, kNetworkModeAdHoc, &endpoint1, NULL);
mukesh agrawal5c4dd0b2011-09-14 13:53:14 -07001588
Paul Stewart3c504012013-01-17 17:49:58 -08001589 EXPECT_CALL(*wifi_provider(), OnEndpointRemoved(EndpointMatch(endpoint0)))
1590 .WillOnce(Return(reinterpret_cast<WiFiService *>(NULL)));
1591 EXPECT_CALL(*wifi_provider(), OnEndpointRemoved(EndpointMatch(endpoint1)))
1592 .WillOnce(Return(reinterpret_cast<WiFiService *>(NULL)));
1593 EXPECT_CALL(*GetSupplicantInterfaceProxy(), RemoveNetwork(kPath)).Times(1);
mukesh agrawalc01f3982012-01-24 13:48:39 -08001594 StopWiFi();
mukesh agrawalb66c6462012-05-07 11:45:25 -07001595 EXPECT_TRUE(GetScanTimer().IsCancelled());
mukesh agrawalc4f368f2012-06-04 19:45:52 -07001596 EXPECT_FALSE(wifi()->weak_ptr_factory_.HasWeakPtrs());
mukesh agrawal5c4dd0b2011-09-14 13:53:14 -07001597}
1598
Paul Stewart549d44c2012-07-03 12:40:25 -07001599TEST_F(WiFiMainTest, StopWhileConnected) {
Paul Stewart3c504012013-01-17 17:49:58 -08001600 StartWiFi();
1601 WiFiEndpointRefPtr endpoint;
1602 WiFiServiceRefPtr service(
1603 SetupConnectedService(DBus::Path(), &endpoint, NULL));
1604 scoped_ptr<EndpointRemovalHandler> handler(
1605 MakeEndpointRemovalHandler(service));
1606 EXPECT_CALL(*wifi_provider(), OnEndpointRemoved(EndpointMatch(endpoint)))
1607 .WillOnce(Invoke(handler.get(),
1608 &EndpointRemovalHandler::OnEndpointRemoved));
1609 EXPECT_CALL(*GetSupplicantInterfaceProxy(), Disconnect());
Paul Stewart549d44c2012-07-03 12:40:25 -07001610 StopWiFi();
1611 EXPECT_TRUE(GetCurrentService() == NULL);
1612}
1613
Paul Stewart44663922012-07-30 11:03:03 -07001614TEST_F(WiFiMainTest, ReconnectTimer) {
Paul Stewart3c504012013-01-17 17:49:58 -08001615 StartWiFi();
1616 MockWiFiServiceRefPtr service(
1617 SetupConnectedService(DBus::Path(), NULL, NULL));
1618 EXPECT_CALL(*service, IsConnected()).WillRepeatedly(Return(true));
Paul Stewart1aff7302012-08-04 20:04:47 -07001619 EXPECT_TRUE(GetReconnectTimeoutCallback().IsCancelled());
Paul Stewart0654ece2013-03-26 15:21:26 -07001620 ReportStateChanged(WPASupplicant::kInterfaceStateDisconnected);
Paul Stewart1aff7302012-08-04 20:04:47 -07001621 EXPECT_FALSE(GetReconnectTimeoutCallback().IsCancelled());
Paul Stewart0654ece2013-03-26 15:21:26 -07001622 ReportStateChanged(WPASupplicant::kInterfaceStateCompleted);
Paul Stewart1aff7302012-08-04 20:04:47 -07001623 EXPECT_TRUE(GetReconnectTimeoutCallback().IsCancelled());
Paul Stewart0654ece2013-03-26 15:21:26 -07001624 ReportStateChanged(WPASupplicant::kInterfaceStateDisconnected);
Paul Stewart1aff7302012-08-04 20:04:47 -07001625 EXPECT_FALSE(GetReconnectTimeoutCallback().IsCancelled());
Paul Stewart44663922012-07-30 11:03:03 -07001626 ReportCurrentBSSChanged(kBSSName);
Paul Stewart1aff7302012-08-04 20:04:47 -07001627 EXPECT_TRUE(GetReconnectTimeoutCallback().IsCancelled());
Paul Stewart0654ece2013-03-26 15:21:26 -07001628 ReportStateChanged(WPASupplicant::kInterfaceStateDisconnected);
Paul Stewart1aff7302012-08-04 20:04:47 -07001629 EXPECT_FALSE(GetReconnectTimeoutCallback().IsCancelled());
Paul Stewart3c504012013-01-17 17:49:58 -08001630 EXPECT_CALL(*GetSupplicantInterfaceProxy(), Disconnect());
Paul Stewart1aff7302012-08-04 20:04:47 -07001631 GetReconnectTimeoutCallback().callback().Run();
Paul Stewart3c504012013-01-17 17:49:58 -08001632 Mock::VerifyAndClearExpectations(GetSupplicantInterfaceProxy());
Paul Stewart1aff7302012-08-04 20:04:47 -07001633 EXPECT_TRUE(GetReconnectTimeoutCallback().IsCancelled());
Paul Stewart44663922012-07-30 11:03:03 -07001634}
1635
Paul Stewartcb59fed2012-03-21 21:14:46 -07001636
Wade Guthrie5a4e2ef2013-04-30 12:51:39 -07001637MATCHER_P(HasHiddenSSID_FullScan, ssid, "") {
Paul Stewarta41e38d2011-11-11 07:47:29 -08001638 map<string, DBus::Variant>::const_iterator it =
Paul Stewart0654ece2013-03-26 15:21:26 -07001639 arg.find(WPASupplicant::kPropertyScanSSIDs);
Paul Stewartced6a0b2011-11-08 15:32:04 -08001640 if (it == arg.end()) {
1641 return false;
1642 }
1643
1644 const DBus::Variant &ssids_variant = it->second;
1645 EXPECT_TRUE(DBusAdaptor::IsByteArrays(ssids_variant.signature()));
1646 const ByteArrays &ssids = it->second.operator ByteArrays();
1647 // A valid Scan containing a single hidden SSID should contain
1648 // two SSID entries: one containing the SSID we are looking for,
1649 // and an empty entry, signifying that we also want to do a
1650 // broadcast probe request for all non-hidden APs as well.
Paul Stewart3c504012013-01-17 17:49:58 -08001651 return ssids.size() == 2 && ssids[0] == ssid && ssids[1].empty();
1652}
1653
Wade Guthrie5a4e2ef2013-04-30 12:51:39 -07001654MATCHER(HasNoHiddenSSID_FullScan, "") {
Paul Stewart3c504012013-01-17 17:49:58 -08001655 map<string, DBus::Variant>::const_iterator it =
Paul Stewart0654ece2013-03-26 15:21:26 -07001656 arg.find(WPASupplicant::kPropertyScanSSIDs);
Paul Stewart3c504012013-01-17 17:49:58 -08001657 return it == arg.end();
Paul Stewartced6a0b2011-11-08 15:32:04 -08001658}
1659
Wade Guthrie5a4e2ef2013-04-30 12:51:39 -07001660TEST_F(WiFiMainTest, ScanHidden_FullScan) {
1661 EnableFullScan();
Paul Stewart3c504012013-01-17 17:49:58 -08001662 vector<uint8_t>kSSID(1, 'a');
1663 ByteArrays ssids;
1664 ssids.push_back(kSSID);
1665
Paul Stewartced6a0b2011-11-08 15:32:04 -08001666 StartWiFi();
Paul Stewart3c504012013-01-17 17:49:58 -08001667 EXPECT_CALL(*wifi_provider(), GetHiddenSSIDList()).WillOnce(Return(ssids));
Wade Guthrie5a4e2ef2013-04-30 12:51:39 -07001668 EXPECT_CALL(*GetSupplicantInterfaceProxy(),
1669 Scan(HasHiddenSSID_FullScan(kSSID)));
Paul Stewart3c504012013-01-17 17:49:58 -08001670 dispatcher_.DispatchPendingEvents();
1671}
1672
Wade Guthrie5a4e2ef2013-04-30 12:51:39 -07001673// This test is slightly different from the test in scan_session_unittest.cc
1674// because this tests the piece of WiFi that builds the SSID list.
1675TEST_F(WiFiMainTest, ScanHidden) {
1676 // Clear the Mock ScanSession because hidden SSIDs are added when wifi
1677 // instantiates a new ScanSession (and it won't instantiate a new ScanSession
1678 // if there's already one there).
1679 ClearScanSession();
1680 vector<uint8_t>kSSID(1, 'a');
1681 ByteArrays ssids;
1682 ssids.push_back(kSSID);
1683
Wade Guthrie7347bf22013-04-30 11:21:51 -07001684 EXPECT_CALL(netlink_manager_, SendNl80211Message(
1685 IsNl80211Command(kNl80211FamilyId, NL80211_CMD_GET_WIPHY), _, _));
Wade Guthrie5a4e2ef2013-04-30 12:51:39 -07001686 EXPECT_CALL(*wifi_provider(), GetHiddenSSIDList()).WillOnce(Return(ssids));
1687 StartWiFi();
1688 EXPECT_CALL(netlink_manager_,
Wade Guthrie7347bf22013-04-30 11:21:51 -07001689 SendNl80211Message(HasHiddenSSID(kNl80211FamilyId), _, _));
Wade Guthrie5a4e2ef2013-04-30 12:51:39 -07001690 dispatcher_.DispatchPendingEvents();
1691}
1692
1693TEST_F(WiFiMainTest, ScanNoHidden_FullScan) {
1694 EnableFullScan();
Paul Stewart3c504012013-01-17 17:49:58 -08001695 StartWiFi();
1696 EXPECT_CALL(*wifi_provider(), GetHiddenSSIDList())
1697 .WillOnce(Return(ByteArrays()));
Wade Guthrie5a4e2ef2013-04-30 12:51:39 -07001698 EXPECT_CALL(*GetSupplicantInterfaceProxy(), Scan(HasNoHiddenSSID_FullScan()));
1699 dispatcher_.DispatchPendingEvents();
1700}
1701
1702// This test is slightly different from the test in scan_session_unittest.cc
1703// because this tests the piece of WiFi that builds the SSID list.
1704TEST_F(WiFiMainTest, ScanNoHidden) {
1705 // Clear the Mock ScanSession because hidden SSIDs are added when wifi
1706 // instantiates a new ScanSession (and it won't instantiate a new ScanSession
1707 // if there's already one there).
1708 ClearScanSession();
Wade Guthrie7347bf22013-04-30 11:21:51 -07001709 EXPECT_CALL(netlink_manager_, SendNl80211Message(
1710 IsNl80211Command(kNl80211FamilyId, NL80211_CMD_GET_WIPHY), _, _));
Wade Guthrie5a4e2ef2013-04-30 12:51:39 -07001711 EXPECT_CALL(*wifi_provider(), GetHiddenSSIDList())
1712 .WillOnce(Return(ByteArrays()));
1713 StartWiFi();
1714 EXPECT_CALL(netlink_manager_,
Wade Guthrie7347bf22013-04-30 11:21:51 -07001715 SendNl80211Message(HasNoHiddenSSID(kNl80211FamilyId), _, _));
Paul Stewartced6a0b2011-11-08 15:32:04 -08001716 dispatcher_.DispatchPendingEvents();
1717}
1718
Paul Stewartfae4dae2012-09-13 07:43:32 -07001719TEST_F(WiFiMainTest, ScanWiFiDisabledAfterResume) {
1720 ScopedMockLog log;
1721 EXPECT_CALL(log, Log(_, _, _)).Times(AnyNumber());
1722 EXPECT_CALL(log, Log(_, _, EndsWith(
1723 "Ignoring scan request while device is not enabled."))).Times(1);
Paul Stewart3c504012013-01-17 17:49:58 -08001724 EXPECT_CALL(*GetSupplicantInterfaceProxy(), Scan(_)).Times(0);
Wade Guthrie5a4e2ef2013-04-30 12:51:39 -07001725 EXPECT_CALL(*scan_session_, InitiateScan()).Times(0);
Paul Stewartfae4dae2012-09-13 07:43:32 -07001726 StartWiFi();
1727 StopWiFi();
1728 // A scan is queued when WiFi resumes.
1729 OnAfterResume();
1730 dispatcher_.DispatchPendingEvents();
1731}
1732
Wade Guthrie5a4e2ef2013-04-30 12:51:39 -07001733TEST_F(WiFiMainTest, ProgressiveScanFound) {
1734 // Set min & max scan frequency count to 1 so each scan will be of a single
1735 // frequency.
1736 SetScanSize(1, 1);
1737
1738 // Do the first scan (finds nothing).
1739 EXPECT_CALL(*scan_session_, InitiateScan());
Wade Guthrie0cf3c982013-05-29 09:11:35 -07001740 EXPECT_CALL(*adaptor_, EmitBoolChanged(_, _)).Times(AnyNumber());
1741 EXPECT_CALL(*adaptor_, EmitBoolChanged(flimflam::kScanningProperty, true));
1742 VerifyScanState(WiFi::kScanIdle, WiFi::kScanMethodNone);
Wade Guthrie5a4e2ef2013-04-30 12:51:39 -07001743 StartWiFi();
1744 dispatcher_.DispatchPendingEvents();
Wade Guthrie0cf3c982013-05-29 09:11:35 -07001745 VerifyScanState(WiFi::kScanScanning, WiFi::kScanMethodProgressive);
Wade Guthrie5a4e2ef2013-04-30 12:51:39 -07001746 ReportScanDoneKeepScanSession();
1747
1748 // Do the second scan (connects afterwards).
1749 EXPECT_CALL(*scan_session_, InitiateScan());
1750 dispatcher_.DispatchPendingEvents();
Wade Guthrie0cf3c982013-05-29 09:11:35 -07001751 VerifyScanState(WiFi::kScanScanning, WiFi::kScanMethodProgressive);
Wade Guthrie5a4e2ef2013-04-30 12:51:39 -07001752 ReportScanDoneKeepScanSession();
1753
1754 // Connect after second scan.
1755 MockWiFiServiceRefPtr service = MakeMockService(flimflam::kSecurityNone);
Wade Guthriedf6d61b2013-07-17 11:43:55 -07001756 EXPECT_CALL(*metrics(), NotifyDeviceScanFinished(_));
1757 EXPECT_CALL(*scan_session_, InitiateScan()).Times(0);
1758 EXPECT_CALL(*GetSupplicantInterfaceProxy(), Scan(_)).Times(0);
1759 EXPECT_CALL(*adaptor_, EmitBoolChanged(flimflam::kScanningProperty, false));
Wade Guthrie5a4e2ef2013-04-30 12:51:39 -07001760 SetPendingService(service);
1761
1762 // Verify that the third scan aborts and there is no further scan.
1763 ScopedMockLog log;
1764 EXPECT_CALL(log, Log(_, _, _)).Times(AnyNumber());
1765 EXPECT_CALL(log, Log(_, _, EndsWith(
1766 "Ignoring scan request while connecting to an AP."))).Times(1);
Wade Guthrie5a4e2ef2013-04-30 12:51:39 -07001767 dispatcher_.DispatchPendingEvents();
Wade Guthrie0cf3c982013-05-29 09:11:35 -07001768 VerifyScanState(WiFi::kScanConnecting, WiFi::kScanMethodProgressive);
Wade Guthrie5a4e2ef2013-04-30 12:51:39 -07001769}
1770
1771TEST_F(WiFiMainTest, ProgressiveScanNotFound) {
1772 // Set min & max scan frequency count to 1 so each scan will be of a single
1773 // frequency.
1774 SetScanSize(1, 1);
1775
Wade Guthrie44f290d2013-05-28 10:16:25 -07001776 // This test never connects
1777 EXPECT_CALL(*metrics(), NotifyDeviceConnectStarted(_, _)).Times(0);
1778 EXPECT_CALL(*metrics(), NotifyDeviceConnectFinished(_)).Times(0);
1779
Wade Guthrie5a4e2ef2013-04-30 12:51:39 -07001780 // Do the first scan (finds nothing).
1781 EXPECT_CALL(*scan_session_, InitiateScan());
Wade Guthrie0cf3c982013-05-29 09:11:35 -07001782 VerifyScanState(WiFi::kScanIdle, WiFi::kScanMethodNone);
Wade Guthrie44f290d2013-05-28 10:16:25 -07001783 EXPECT_CALL(*metrics(), NotifyDeviceScanStarted(_));
Wade Guthrie5a4e2ef2013-04-30 12:51:39 -07001784 StartWiFi();
Wade Guthrie0cf3c982013-05-29 09:11:35 -07001785 dispatcher_.DispatchPendingEvents(); // Launch ProgressiveScanTask
1786 VerifyScanState(WiFi::kScanScanning, WiFi::kScanMethodProgressive);
Wade Guthrie5a4e2ef2013-04-30 12:51:39 -07001787 ReportScanDoneKeepScanSession();
1788
1789 // Do the second scan (finds nothing).
Wade Guthrieb0def9f2013-07-12 13:49:18 -07001790 EXPECT_CALL(*metrics(), NotifyDeviceScanStarted(_));
Wade Guthrie5a4e2ef2013-04-30 12:51:39 -07001791 EXPECT_CALL(*scan_session_, InitiateScan());
1792 dispatcher_.DispatchPendingEvents();
Wade Guthrie0cf3c982013-05-29 09:11:35 -07001793 VerifyScanState(WiFi::kScanScanning, WiFi::kScanMethodProgressive);
Wade Guthrie5a4e2ef2013-04-30 12:51:39 -07001794 ReportScanDoneKeepScanSession();
1795
1796 // Do the third scan. After (simulated) exhausting of search frequencies,
1797 // verify that this scan uses supplicant rather than internal (progressive)
1798 // scan.
1799 EXPECT_CALL(*scan_session_, HasMoreFrequencies()).WillOnce(Return(false));
1800 EXPECT_CALL(*scan_session_, InitiateScan()).Times(0);
1801 EXPECT_CALL(*GetSupplicantInterfaceProxy(), Scan(_));
1802 dispatcher_.DispatchPendingEvents();
Wade Guthrie0cf3c982013-05-29 09:11:35 -07001803 VerifyScanState(WiFi::kScanScanning,
1804 WiFi::kScanMethodProgressiveFinishedToFull);
Wade Guthrie5a4e2ef2013-04-30 12:51:39 -07001805
1806 // And verify that ScanDone reports a complete scan (i.e., the
1807 // wifi_::scan_session_ has truly been cleared).
1808 EXPECT_CALL(*metrics(), NotifyDeviceScanFinished(_));
1809 ReportScanDoneKeepScanSession();
Wade Guthrie0cf3c982013-05-29 09:11:35 -07001810 dispatcher_.DispatchPendingEvents(); // Launch UpdateScanStateAfterScanDone
1811 VerifyScanState(WiFi::kScanIdle, WiFi::kScanMethodNone);
Wade Guthrie5a4e2ef2013-04-30 12:51:39 -07001812}
1813
1814TEST_F(WiFiMainTest, ProgressiveScanError) {
Wade Guthrie0cf3c982013-05-29 09:11:35 -07001815 VerifyScanState(WiFi::kScanIdle, WiFi::kScanMethodNone);
1816 ClearScanSession(); // Clear Mock ScanSession to get an actual ScanSession.
Wade Guthrie5a4e2ef2013-04-30 12:51:39 -07001817 StartWiFi();
1818
1819 // This should call |Scan| which posts |ScanTask|
1820 NewScanResultsMessage not_supposed_to_get_this_message;
1821 OnTriggerScanResponse(not_supposed_to_get_this_message);
Wade Guthrie0cf3c982013-05-29 09:11:35 -07001822 VerifyScanState(WiFi::kScanScanning, WiFi::kScanMethodProgressiveErrorToFull);
Wade Guthrie5a4e2ef2013-04-30 12:51:39 -07001823
1824 EXPECT_TRUE(IsScanSessionNull());
1825 EXPECT_CALL(*GetSupplicantInterfaceProxy(), Scan(_)).Times(1);
1826 dispatcher_.DispatchPendingEvents();
Wade Guthrie0cf3c982013-05-29 09:11:35 -07001827
1828 ReportScanDoneKeepScanSession();
1829 dispatcher_.DispatchPendingEvents(); // Launch UpdateScanStateAfterScanDone
1830 VerifyScanState(WiFi::kScanIdle, WiFi::kScanMethodNone);
Wade Guthrie5a4e2ef2013-04-30 12:51:39 -07001831}
1832
mukesh agrawal7ec71312011-11-10 02:08:26 +00001833TEST_F(WiFiMainTest, InitialSupplicantState) {
1834 EXPECT_EQ(WiFi::kInterfaceStateUnknown, GetSupplicantState());
1835}
1836
1837TEST_F(WiFiMainTest, StateChangeNoService) {
1838 // State change should succeed even if there is no pending Service.
Paul Stewart0654ece2013-03-26 15:21:26 -07001839 ReportStateChanged(WPASupplicant::kInterfaceStateScanning);
1840 EXPECT_EQ(WPASupplicant::kInterfaceStateScanning, GetSupplicantState());
mukesh agrawal7ec71312011-11-10 02:08:26 +00001841}
1842
1843TEST_F(WiFiMainTest, StateChangeWithService) {
1844 // Forward transition should trigger a Service state change.
1845 StartWiFi();
1846 dispatcher_.DispatchPendingEvents();
mukesh agrawalcf24a242012-05-21 16:46:11 -07001847 MockWiFiServiceRefPtr service = MakeMockService(flimflam::kSecurityNone);
mukesh agrawal7ec71312011-11-10 02:08:26 +00001848 InitiateConnect(service);
1849 EXPECT_CALL(*service.get(), SetState(Service::kStateAssociating));
Paul Stewart0654ece2013-03-26 15:21:26 -07001850 ReportStateChanged(WPASupplicant::kInterfaceStateAssociated);
mukesh agrawal7ec71312011-11-10 02:08:26 +00001851 // Verify expectations now, because WiFi may report other state changes
1852 // when WiFi is Stop()-ed (during TearDown()).
1853 Mock::VerifyAndClearExpectations(service.get());
Gary Moraind9f893f2012-02-06 10:03:40 -08001854 EXPECT_CALL(*service.get(), SetState(_)).Times(AnyNumber());
mukesh agrawal7ec71312011-11-10 02:08:26 +00001855}
1856
1857TEST_F(WiFiMainTest, StateChangeBackwardsWithService) {
1858 // Some backwards transitions should not trigger a Service state change.
1859 // Supplicant state should still be updated, however.
Paul Stewartd408fdf2012-05-07 17:15:57 -07001860 EXPECT_CALL(*dhcp_provider(), CreateConfig(_, _, _, _)).Times(AnyNumber());
Gary Moraind9f893f2012-02-06 10:03:40 -08001861 EXPECT_CALL(*dhcp_config_.get(), RequestIP()).Times(AnyNumber());
mukesh agrawal7ec71312011-11-10 02:08:26 +00001862 StartWiFi();
1863 dispatcher_.DispatchPendingEvents();
mukesh agrawalcf24a242012-05-21 16:46:11 -07001864 MockWiFiServiceRefPtr service = MakeMockService(flimflam::kSecurityNone);
Paul Stewartbca08f82013-07-09 16:32:37 -07001865 EXPECT_CALL(*service, SetState(Service::kStateAssociating));
1866 EXPECT_CALL(*service, SetState(Service::kStateConfiguring));
1867 EXPECT_CALL(*service, ResetSuspectedCredentialFailures());
mukesh agrawal7ec71312011-11-10 02:08:26 +00001868 InitiateConnect(service);
Paul Stewart0654ece2013-03-26 15:21:26 -07001869 ReportStateChanged(WPASupplicant::kInterfaceStateCompleted);
1870 ReportStateChanged(WPASupplicant::kInterfaceStateAuthenticating);
1871 EXPECT_EQ(WPASupplicant::kInterfaceStateAuthenticating,
mukesh agrawal7ec71312011-11-10 02:08:26 +00001872 GetSupplicantState());
1873 // Verify expectations now, because WiFi may report other state changes
1874 // when WiFi is Stop()-ed (during TearDown()).
Paul Stewart3c504012013-01-17 17:49:58 -08001875 Mock::VerifyAndClearExpectations(service);
1876 EXPECT_CALL(*service, SetState(_)).Times(AnyNumber());
mukesh agrawal7ec71312011-11-10 02:08:26 +00001877}
1878
Paul Stewarta47c3c62012-12-18 12:14:29 -08001879TEST_F(WiFiMainTest, ConnectToServiceWithoutRecentIssues) {
1880 MockSupplicantProcessProxy *process_proxy = supplicant_process_proxy_.get();
1881 StartWiFi();
1882 dispatcher_.DispatchPendingEvents();
1883 MockWiFiServiceRefPtr service = MakeMockService(flimflam::kSecurityNone);
1884 EXPECT_CALL(*process_proxy, GetDebugLevel()).Times(0);
1885 EXPECT_CALL(*process_proxy, SetDebugLevel(_)).Times(0);
1886 EXPECT_CALL(*service.get(), HasRecentConnectionIssues())
1887 .WillOnce(Return(false));
1888 InitiateConnect(service);
1889}
1890
1891TEST_F(WiFiMainTest, ConnectToServiceWithRecentIssues) {
1892 // Turn of WiFi debugging, so the only reason we will turn on supplicant
1893 // debugging will be to debug a problematic connection.
1894 ScopeLogger::GetInstance()->EnableScopesByName("-wifi");
1895
1896 MockSupplicantProcessProxy *process_proxy = supplicant_process_proxy_.get();
1897 StartWiFi();
1898 dispatcher_.DispatchPendingEvents();
1899 MockWiFiServiceRefPtr service = MakeMockService(flimflam::kSecurityNone);
1900 EXPECT_CALL(*process_proxy, GetDebugLevel())
Paul Stewart0654ece2013-03-26 15:21:26 -07001901 .WillOnce(Return(WPASupplicant::kDebugLevelInfo));
1902 EXPECT_CALL(*process_proxy, SetDebugLevel(WPASupplicant::kDebugLevelDebug))
Paul Stewarta47c3c62012-12-18 12:14:29 -08001903 .Times(1);
1904 EXPECT_CALL(*service.get(), HasRecentConnectionIssues())
1905 .WillOnce(Return(true));
1906 InitiateConnect(service);
1907 Mock::VerifyAndClearExpectations(process_proxy);
1908
1909 SetPendingService(NULL);
1910 SetCurrentService(service);
1911
1912 // When we disconnect from the troubled service, we should reduce the
1913 // level of supplciant debugging.
1914 EXPECT_CALL(*process_proxy, GetDebugLevel())
Paul Stewart0654ece2013-03-26 15:21:26 -07001915 .WillOnce(Return(WPASupplicant::kDebugLevelDebug));
1916 EXPECT_CALL(*process_proxy, SetDebugLevel(WPASupplicant::kDebugLevelInfo))
Paul Stewarta47c3c62012-12-18 12:14:29 -08001917 .Times(1);
Paul Stewart0654ece2013-03-26 15:21:26 -07001918 ReportCurrentBSSChanged(WPASupplicant::kCurrentBSSNull);
Paul Stewarta47c3c62012-12-18 12:14:29 -08001919}
1920
mukesh agrawal15908392011-11-16 18:29:25 +00001921TEST_F(WiFiMainTest, CurrentBSSChangeConnectedToDisconnected) {
mukesh agrawal15908392011-11-16 18:29:25 +00001922 StartWiFi();
Paul Stewart3c504012013-01-17 17:49:58 -08001923 WiFiEndpointRefPtr endpoint;
1924 MockWiFiServiceRefPtr service =
1925 SetupConnectedService(DBus::Path(), &endpoint, NULL);
mukesh agrawal15908392011-11-16 18:29:25 +00001926
Paul Stewart3c504012013-01-17 17:49:58 -08001927 EXPECT_CALL(*service, SetState(Service::kStateIdle));
1928 EXPECT_CALL(*service, SetFailureSilent(Service::kFailureUnknown));
Paul Stewart0654ece2013-03-26 15:21:26 -07001929 ReportCurrentBSSChanged(WPASupplicant::kCurrentBSSNull);
mukesh agrawal15908392011-11-16 18:29:25 +00001930 EXPECT_EQ(NULL, GetCurrentService().get());
1931 EXPECT_EQ(NULL, GetPendingService().get());
1932}
1933
1934TEST_F(WiFiMainTest, CurrentBSSChangeConnectedToConnectedNewService) {
mukesh agrawal15908392011-11-16 18:29:25 +00001935 StartWiFi();
Paul Stewart3c504012013-01-17 17:49:58 -08001936 MockWiFiServiceRefPtr service0 =
1937 SetupConnectedService(DBus::Path(), NULL, NULL);
1938 MockWiFiServiceRefPtr service1;
1939 ::DBus::Path bss_path1(MakeNewEndpointAndService(
1940 0, 0, kNetworkModeAdHoc, NULL, &service1));
1941 EXPECT_EQ(service0.get(), GetCurrentService().get());
mukesh agrawal15908392011-11-16 18:29:25 +00001942
mukesh agrawalc01f3982012-01-24 13:48:39 -08001943 // Note that we deliberately omit intermediate supplicant states
1944 // (e.g. kInterfaceStateAssociating), on the theory that they are
1945 // unreliable. Specifically, they may be quashed if the association
1946 // completes before supplicant flushes its changed properties.
Paul Stewart3c504012013-01-17 17:49:58 -08001947 EXPECT_CALL(*service0, SetState(Service::kStateIdle)).Times(AtLeast(1));
1948 ReportCurrentBSSChanged(bss_path1);
1949 EXPECT_CALL(*service1, SetState(Service::kStateConfiguring));
Paul Stewartbca08f82013-07-09 16:32:37 -07001950 EXPECT_CALL(*service1, ResetSuspectedCredentialFailures());
Paul Stewart0654ece2013-03-26 15:21:26 -07001951 ReportStateChanged(WPASupplicant::kInterfaceStateCompleted);
Paul Stewart3c504012013-01-17 17:49:58 -08001952 EXPECT_EQ(service1.get(), GetCurrentService().get());
1953 Mock::VerifyAndClearExpectations(service0);
1954 Mock::VerifyAndClearExpectations(service1);
mukesh agrawal15908392011-11-16 18:29:25 +00001955}
1956
Thieu Lee41a72d2012-02-06 20:46:51 +00001957TEST_F(WiFiMainTest, CurrentBSSChangedUpdateServiceEndpoint) {
Thieu Lee41a72d2012-02-06 20:46:51 +00001958 StartWiFi();
Wade Guthrie0cf3c982013-05-29 09:11:35 -07001959 dispatcher_.DispatchPendingEvents();
1960 VerifyScanState(WiFi::kScanScanning, WiFi::kScanMethodProgressive);
1961
Paul Stewart3c504012013-01-17 17:49:58 -08001962 MockWiFiServiceRefPtr service =
1963 SetupConnectedService(DBus::Path(), NULL, NULL);
1964 WiFiEndpointRefPtr endpoint;
1965 ::DBus::Path bss_path =
1966 AddEndpointToService(service, 0, 0, kNetworkModeAdHoc, &endpoint);
1967 EXPECT_CALL(*service, NotifyCurrentEndpoint(EndpointMatch(endpoint)));
1968 ReportCurrentBSSChanged(bss_path);
Wade Guthrie0cf3c982013-05-29 09:11:35 -07001969 VerifyScanState(WiFi::kScanIdle, WiFi::kScanMethodNone);
mukesh agrawal4eb4d782011-12-05 17:34:37 +00001970}
1971
mukesh agrawal8a3188d2011-12-01 20:56:44 +00001972TEST_F(WiFiMainTest, NewConnectPreemptsPending) {
mukesh agrawal8a3188d2011-12-01 20:56:44 +00001973 StartWiFi();
Paul Stewart3c504012013-01-17 17:49:58 -08001974 MockWiFiServiceRefPtr service0(
1975 SetupConnectingService(DBus::Path(), NULL, NULL));
1976 EXPECT_EQ(service0.get(), GetPendingService().get());
mukesh agrawal8a3188d2011-12-01 20:56:44 +00001977 EXPECT_CALL(*GetSupplicantInterfaceProxy(), Disconnect());
Paul Stewart3c504012013-01-17 17:49:58 -08001978 MockWiFiServiceRefPtr service1(
1979 SetupConnectingService(DBus::Path(), NULL, NULL));
1980 EXPECT_EQ(service1.get(), GetPendingService().get());
1981 EXPECT_EQ(NULL, GetCurrentService().get());
mukesh agrawal8a3188d2011-12-01 20:56:44 +00001982}
1983
1984TEST_F(WiFiMainTest, IsIdle) {
1985 StartWiFi();
1986 EXPECT_TRUE(wifi()->IsIdle());
Paul Stewart3c504012013-01-17 17:49:58 -08001987 MockWiFiServiceRefPtr service(
1988 SetupConnectingService(DBus::Path(), NULL, NULL));
mukesh agrawal8a3188d2011-12-01 20:56:44 +00001989 EXPECT_FALSE(wifi()->IsIdle());
1990}
1991
Darin Petkov4a66cc52012-06-15 10:08:29 +02001992MATCHER_P(WiFiAddedArgs, bgscan, "") {
Paul Stewart0654ece2013-03-26 15:21:26 -07001993 return ContainsKey(arg, WPASupplicant::kNetworkPropertyScanSSID) &&
1994 ContainsKey(arg, WPASupplicant::kNetworkPropertyBgscan) == bgscan;
mukesh agrawal4d0401c2012-01-06 16:05:31 -08001995}
1996
1997TEST_F(WiFiMainTest, AddNetworkArgs) {
mukesh agrawal4d0401c2012-01-06 16:05:31 -08001998 StartWiFi();
Paul Stewart3c504012013-01-17 17:49:58 -08001999 MockWiFiServiceRefPtr service;
2000 MakeNewEndpointAndService(0, 0, kNetworkModeAdHoc, NULL, &service);
2001 EXPECT_CALL(*GetSupplicantInterfaceProxy(), AddNetwork(WiFiAddedArgs(true)));
Paul Stewart0654ece2013-03-26 15:21:26 -07002002 EXPECT_TRUE(SetBgscanMethod(WPASupplicant::kNetworkBgscanMethodSimple));
mukesh agrawal4d0401c2012-01-06 16:05:31 -08002003 InitiateConnect(service);
2004}
2005
Darin Petkov4a66cc52012-06-15 10:08:29 +02002006TEST_F(WiFiMainTest, AddNetworkArgsNoBgscan) {
Darin Petkov4a66cc52012-06-15 10:08:29 +02002007 StartWiFi();
Paul Stewart3c504012013-01-17 17:49:58 -08002008 MockWiFiServiceRefPtr service;
2009 MakeNewEndpointAndService(0, 0, kNetworkModeAdHoc, NULL, &service);
2010 EXPECT_CALL(*GetSupplicantInterfaceProxy(), AddNetwork(WiFiAddedArgs(false)));
Darin Petkov4a66cc52012-06-15 10:08:29 +02002011 InitiateConnect(service);
2012}
2013
2014TEST_F(WiFiMainTest, AppendBgscan) {
2015 StartWiFi();
Paul Stewart3c504012013-01-17 17:49:58 -08002016 MockWiFiServiceRefPtr service = MakeMockService(flimflam::kSecurityNone);
Darin Petkov4a66cc52012-06-15 10:08:29 +02002017 {
2018 // 1 endpoint, default bgscan method -- background scan disabled.
2019 std::map<std::string, DBus::Variant> params;
Paul Stewart3c504012013-01-17 17:49:58 -08002020 EXPECT_CALL(*service, GetEndpointCount()).WillOnce(Return(1));
2021 AppendBgscan(service, &params);
2022 Mock::VerifyAndClearExpectations(service);
Paul Stewart0654ece2013-03-26 15:21:26 -07002023 EXPECT_FALSE(ContainsKey(params, WPASupplicant::kNetworkPropertyBgscan));
Darin Petkov4a66cc52012-06-15 10:08:29 +02002024 }
Darin Petkov4a66cc52012-06-15 10:08:29 +02002025 {
2026 // 2 endpoints, default bgscan method -- background scan frequency reduced.
2027 map<string, DBus::Variant> params;
Paul Stewart3c504012013-01-17 17:49:58 -08002028 EXPECT_CALL(*service, GetEndpointCount()).WillOnce(Return(2));
2029 AppendBgscan(service, &params);
2030 Mock::VerifyAndClearExpectations(service);
Darin Petkov4a66cc52012-06-15 10:08:29 +02002031 string config_string;
2032 EXPECT_TRUE(
2033 DBusProperties::GetString(params,
Paul Stewart0654ece2013-03-26 15:21:26 -07002034 WPASupplicant::kNetworkPropertyBgscan,
Darin Petkov4a66cc52012-06-15 10:08:29 +02002035 &config_string));
2036 vector<string> elements;
2037 base::SplitString(config_string, ':', &elements);
2038 ASSERT_EQ(4, elements.size());
2039 EXPECT_EQ(WiFi::kDefaultBgscanMethod, elements[0]);
2040 EXPECT_EQ(StringPrintf("%d", WiFi::kBackgroundScanIntervalSeconds),
2041 elements[3]);
2042 }
2043 {
2044 // Explicit bgscan method -- regular background scan frequency.
Paul Stewart0654ece2013-03-26 15:21:26 -07002045 EXPECT_TRUE(SetBgscanMethod(WPASupplicant::kNetworkBgscanMethodSimple));
Darin Petkov4a66cc52012-06-15 10:08:29 +02002046 std::map<std::string, DBus::Variant> params;
Paul Stewart3c504012013-01-17 17:49:58 -08002047 EXPECT_CALL(*service, GetEndpointCount()).Times(0);
2048 AppendBgscan(service, &params);
2049 Mock::VerifyAndClearExpectations(service);
Darin Petkov4a66cc52012-06-15 10:08:29 +02002050 string config_string;
2051 EXPECT_TRUE(
2052 DBusProperties::GetString(params,
Paul Stewart0654ece2013-03-26 15:21:26 -07002053 WPASupplicant::kNetworkPropertyBgscan,
Darin Petkov4a66cc52012-06-15 10:08:29 +02002054 &config_string));
2055 vector<string> elements;
2056 base::SplitString(config_string, ':', &elements);
2057 ASSERT_EQ(4, elements.size());
2058 EXPECT_EQ(StringPrintf("%d", WiFi::kDefaultScanIntervalSeconds),
2059 elements[3]);
2060 }
Christopher Wiley5c929e22012-07-16 14:06:04 -07002061 {
2062 // No scan method, simply returns without appending properties
Paul Stewart0654ece2013-03-26 15:21:26 -07002063 EXPECT_TRUE(SetBgscanMethod(WPASupplicant::kNetworkBgscanMethodNone));
Christopher Wiley5c929e22012-07-16 14:06:04 -07002064 std::map<std::string, DBus::Variant> params;
Paul Stewart3c504012013-01-17 17:49:58 -08002065 EXPECT_CALL(*service, GetEndpointCount()).Times(0);
Christopher Wiley5c929e22012-07-16 14:06:04 -07002066 AppendBgscan(service.get(), &params);
Paul Stewart3c504012013-01-17 17:49:58 -08002067 Mock::VerifyAndClearExpectations(service);
Christopher Wiley5c929e22012-07-16 14:06:04 -07002068 string config_string;
2069 EXPECT_FALSE(
2070 DBusProperties::GetString(params,
Paul Stewart0654ece2013-03-26 15:21:26 -07002071 WPASupplicant::kNetworkPropertyBgscan,
Christopher Wiley5c929e22012-07-16 14:06:04 -07002072 &config_string));
2073 }
Darin Petkov4a66cc52012-06-15 10:08:29 +02002074}
2075
mukesh agrawalc01f3982012-01-24 13:48:39 -08002076TEST_F(WiFiMainTest, StateAndIPIgnoreLinkEvent) {
2077 StartWiFi();
Paul Stewart3c504012013-01-17 17:49:58 -08002078 MockWiFiServiceRefPtr service(
2079 SetupConnectingService(DBus::Path(), NULL, NULL));
mukesh agrawalc01f3982012-01-24 13:48:39 -08002080 EXPECT_CALL(*service.get(), SetState(_)).Times(0);
2081 EXPECT_CALL(*dhcp_config_.get(), RequestIP()).Times(0);
2082 ReportLinkUp();
2083
2084 // Verify expectations now, because WiFi may cause |service| state
2085 // changes during TearDown().
2086 Mock::VerifyAndClearExpectations(service);
mukesh agrawalc01f3982012-01-24 13:48:39 -08002087}
2088
2089TEST_F(WiFiMainTest, SupplicantCompletedAlreadyConnected) {
mukesh agrawalc01f3982012-01-24 13:48:39 -08002090 StartWiFi();
Paul Stewart3c504012013-01-17 17:49:58 -08002091 MockWiFiServiceRefPtr service(
2092 SetupConnectedService(DBus::Path(), NULL, NULL));
Christopher Wiley8f81e2a2012-10-17 16:51:32 -07002093 Mock::VerifyAndClearExpectations(dhcp_config_.get());
mukesh agrawalc01f3982012-01-24 13:48:39 -08002094 EXPECT_CALL(*dhcp_config_.get(), RequestIP()).Times(0);
Christopher Wiley8f81e2a2012-10-17 16:51:32 -07002095 // Simulate a rekeying event from the AP. These show as transitions from
2096 // completed->completed from wpa_supplicant.
Paul Stewart0654ece2013-03-26 15:21:26 -07002097 ReportStateChanged(WPASupplicant::kInterfaceStateCompleted);
Christopher Wiley5519e9e2013-01-08 16:55:56 -08002098 // When we get an IP, WiFi should enable high bitrates on the interface again.
Paul Stewart3c504012013-01-17 17:49:58 -08002099 Mock::VerifyAndClearExpectations(GetSupplicantInterfaceProxy());
2100 EXPECT_CALL(*GetSupplicantInterfaceProxy(), EnableHighBitrates()).Times(1);
2101 EXPECT_CALL(*manager(), device_info()).WillOnce(Return(device_info()));
Prathmesh Prabhuba99b592013-04-17 15:13:14 -07002102 string portal_url;
2103 EXPECT_CALL(*manager(), GetPortalCheckURL()).WillOnce(ReturnRef(portal_url));
Christopher Wiley8f81e2a2012-10-17 16:51:32 -07002104 ReportIPConfigComplete();
2105 // Similarly, rekeying events after we have an IP don't trigger L3
Christopher Wiley5519e9e2013-01-08 16:55:56 -08002106 // configuration. However, we treat all transitions to completed as potential
2107 // reassociations, so we will reenable high rates again here.
Paul Stewart3c504012013-01-17 17:49:58 -08002108 Mock::VerifyAndClearExpectations(GetSupplicantInterfaceProxy());
2109 EXPECT_CALL(*service, IsConnected()).WillOnce(Return(true));
2110 EXPECT_CALL(*GetSupplicantInterfaceProxy(), EnableHighBitrates()).Times(1);
Paul Stewart0654ece2013-03-26 15:21:26 -07002111 ReportStateChanged(WPASupplicant::kInterfaceStateCompleted);
mukesh agrawalc01f3982012-01-24 13:48:39 -08002112}
2113
mukesh agrawalb20776f2012-02-10 16:00:36 -08002114TEST_F(WiFiMainTest, BSSAddedCreatesBSSProxy) {
mukesh agrawalb20776f2012-02-10 16:00:36 -08002115 // TODO(quiche): Consider using a factory for WiFiEndpoints, so that
2116 // we can test the interaction between WiFi and WiFiEndpoint. (Right
2117 // now, we're testing across multiple layers.)
2118 EXPECT_CALL(*supplicant_bss_proxy_, Die()).Times(AnyNumber());
2119 EXPECT_CALL(*proxy_factory(), CreateSupplicantBSSProxy(_, _, _));
2120 StartWiFi();
2121 ReportBSS("bss0", "ssid0", "00:00:00:00:00:00", 0, 0, kNetworkModeAdHoc);
2122}
2123
2124TEST_F(WiFiMainTest, BSSRemovedDestroysBSSProxy) {
2125 // TODO(quiche): As for BSSAddedCreatesBSSProxy, consider using a
2126 // factory for WiFiEndpoints.
mukesh agrawalb20776f2012-02-10 16:00:36 -08002127 // Get the pointer before we transfer ownership.
2128 MockSupplicantBSSProxy *proxy = supplicant_bss_proxy_.get();
2129 EXPECT_CALL(*proxy, Die());
2130 StartWiFi();
Paul Stewart3c504012013-01-17 17:49:58 -08002131 DBus::Path bss_path(
2132 MakeNewEndpointAndService(0, 0, kNetworkModeAdHoc, NULL, NULL));
2133 EXPECT_CALL(*wifi_provider(), OnEndpointRemoved(_))
2134 .WillOnce(Return(reinterpret_cast<WiFiService *>(NULL)));
2135 RemoveBSS(bss_path);
mukesh agrawalb20776f2012-02-10 16:00:36 -08002136 // Check this now, to make sure RemoveBSS killed the proxy (rather
2137 // than TearDown).
2138 Mock::VerifyAndClearExpectations(proxy);
2139}
2140
mukesh agrawal5c05b292012-03-07 10:12:52 -08002141TEST_F(WiFiMainTest, FlushBSSOnResume) {
2142 const struct timeval resume_time = {1, 0};
2143 const struct timeval scan_done_time = {6, 0};
mukesh agrawal5c05b292012-03-07 10:12:52 -08002144
2145 StartWiFi();
2146
2147 EXPECT_CALL(time_, GetTimeMonotonic(_))
2148 .WillOnce(DoAll(SetArgumentPointee<0>(resume_time), Return(0)))
2149 .WillOnce(DoAll(SetArgumentPointee<0>(scan_done_time), Return(0)));
Paul Stewart3c504012013-01-17 17:49:58 -08002150 EXPECT_CALL(*GetSupplicantInterfaceProxy(),
mukesh agrawal5c05b292012-03-07 10:12:52 -08002151 FlushBSS(WiFi::kMaxBSSResumeAgeSeconds + 5));
mukesh agrawal2f9df4e2012-08-08 12:29:20 -07002152 OnAfterResume();
mukesh agrawal5c05b292012-03-07 10:12:52 -08002153 ReportScanDone();
2154}
2155
Wade Guthrie5a4e2ef2013-04-30 12:51:39 -07002156TEST_F(WiFiMainTest, ScanTimerIdle_FullScan) {
2157 EnableFullScan();
mukesh agrawalb66c6462012-05-07 11:45:25 -07002158 StartWiFi();
2159 dispatcher_.DispatchPendingEvents();
2160 ReportScanDone();
2161 CancelScanTimer();
2162 EXPECT_TRUE(GetScanTimer().IsCancelled());
2163
Wade Guthrie0cf3c982013-05-29 09:11:35 -07002164 dispatcher_.DispatchPendingEvents();
mukesh agrawalb66c6462012-05-07 11:45:25 -07002165 EXPECT_CALL(*GetSupplicantInterfaceProxy(), Scan(_));
2166 FireScanTimer();
2167 dispatcher_.DispatchPendingEvents();
2168 EXPECT_FALSE(GetScanTimer().IsCancelled()); // Automatically re-armed.
2169}
2170
Wade Guthrie5a4e2ef2013-04-30 12:51:39 -07002171TEST_F(WiFiMainTest, ScanTimerIdle) {
2172 StartWiFi();
2173 dispatcher_.DispatchPendingEvents();
2174 ReportScanDone();
2175 CancelScanTimer();
2176 EXPECT_TRUE(GetScanTimer().IsCancelled());
Wade Guthrie0cf3c982013-05-29 09:11:35 -07002177 dispatcher_.DispatchPendingEvents();
Wade Guthriedf6d61b2013-07-17 11:43:55 -07002178 InstallMockScanSession();
Wade Guthrie5a4e2ef2013-04-30 12:51:39 -07002179 EXPECT_CALL(*scan_session_, InitiateScan());
2180 FireScanTimer();
2181 dispatcher_.DispatchPendingEvents();
2182 EXPECT_FALSE(GetScanTimer().IsCancelled()); // Automatically re-armed.
2183}
2184
mukesh agrawalb66c6462012-05-07 11:45:25 -07002185TEST_F(WiFiMainTest, ScanTimerScanning) {
2186 StartWiFi();
2187 dispatcher_.DispatchPendingEvents();
2188 CancelScanTimer();
2189 EXPECT_TRUE(GetScanTimer().IsCancelled());
2190
2191 // Should not call Scan, since we're already scanning.
2192 // (Scanning is triggered by StartWiFi.)
2193 EXPECT_CALL(*GetSupplicantInterfaceProxy(), Scan(_)).Times(0);
Wade Guthrie5a4e2ef2013-04-30 12:51:39 -07002194 EXPECT_CALL(*scan_session_, InitiateScan()).Times(0);
mukesh agrawalb66c6462012-05-07 11:45:25 -07002195 FireScanTimer();
2196 dispatcher_.DispatchPendingEvents();
2197 EXPECT_FALSE(GetScanTimer().IsCancelled()); // Automatically re-armed.
2198}
2199
2200TEST_F(WiFiMainTest, ScanTimerConnecting) {
2201 StartWiFi();
2202 dispatcher_.DispatchPendingEvents();
Paul Stewart3c504012013-01-17 17:49:58 -08002203 MockWiFiServiceRefPtr service =
2204 SetupConnectingService(DBus::Path(), NULL, NULL);
mukesh agrawalb66c6462012-05-07 11:45:25 -07002205 CancelScanTimer();
2206 EXPECT_TRUE(GetScanTimer().IsCancelled());
2207
2208 EXPECT_CALL(*GetSupplicantInterfaceProxy(), Scan(_)).Times(0);
Wade Guthrie5a4e2ef2013-04-30 12:51:39 -07002209 EXPECT_CALL(*scan_session_, InitiateScan()).Times(0);
mukesh agrawalb66c6462012-05-07 11:45:25 -07002210 FireScanTimer();
2211 dispatcher_.DispatchPendingEvents();
2212 EXPECT_FALSE(GetScanTimer().IsCancelled()); // Automatically re-armed.
2213}
2214
2215TEST_F(WiFiMainTest, ScanTimerReconfigured) {
2216 StartWiFi();
2217 CancelScanTimer();
2218 EXPECT_TRUE(GetScanTimer().IsCancelled());
2219
mukesh agrawalbebf1b82013-04-23 15:06:33 -07002220 SetScanInterval(1, NULL);
mukesh agrawalb66c6462012-05-07 11:45:25 -07002221 EXPECT_FALSE(GetScanTimer().IsCancelled());
2222}
2223
2224TEST_F(WiFiMainTest, ScanTimerResetOnScanDone) {
2225 StartWiFi();
2226 CancelScanTimer();
2227 EXPECT_TRUE(GetScanTimer().IsCancelled());
2228
2229 ReportScanDone();
2230 EXPECT_FALSE(GetScanTimer().IsCancelled());
2231}
2232
2233TEST_F(WiFiMainTest, ScanTimerStopOnZeroInterval) {
2234 StartWiFi();
2235 EXPECT_FALSE(GetScanTimer().IsCancelled());
2236
mukesh agrawalbebf1b82013-04-23 15:06:33 -07002237 SetScanInterval(0, NULL);
mukesh agrawalb66c6462012-05-07 11:45:25 -07002238 EXPECT_TRUE(GetScanTimer().IsCancelled());
2239}
2240
Wade Guthrie5a4e2ef2013-04-30 12:51:39 -07002241TEST_F(WiFiMainTest, ScanOnDisconnectWithHidden_FullScan) {
2242 EnableFullScan();
mukesh agrawalb66c6462012-05-07 11:45:25 -07002243 StartWiFi();
mukesh agrawalb66c6462012-05-07 11:45:25 -07002244 dispatcher_.DispatchPendingEvents();
Paul Stewart3c504012013-01-17 17:49:58 -08002245 SetupConnectedService(DBus::Path(), NULL, NULL);
2246 vector<uint8_t>kSSID(1, 'a');
2247 ByteArrays ssids;
2248 ssids.push_back(kSSID);
2249 EXPECT_CALL(*wifi_provider(), GetHiddenSSIDList())
2250 .WillRepeatedly(Return(ssids));
Wade Guthrie5a4e2ef2013-04-30 12:51:39 -07002251 EXPECT_CALL(*GetSupplicantInterfaceProxy(),
2252 Scan(HasHiddenSSID_FullScan(kSSID)));
2253 ReportCurrentBSSChanged(WPASupplicant::kCurrentBSSNull);
2254 dispatcher_.DispatchPendingEvents();
2255}
2256
2257TEST_F(WiFiMainTest, ScanOnDisconnectWithHidden) {
2258 StartWiFi();
2259 dispatcher_.DispatchPendingEvents();
2260 ReportScanDone();
2261 SetupConnectedService(DBus::Path(), NULL, NULL);
Wade Guthriedf6d61b2013-07-17 11:43:55 -07002262 InstallMockScanSession();
Wade Guthrie5a4e2ef2013-04-30 12:51:39 -07002263 vector<uint8_t>kSSID(1, 'a');
2264 ByteArrays ssids;
2265 ssids.push_back(kSSID);
2266 EXPECT_CALL(*wifi_provider(), GetHiddenSSIDList())
2267 .WillRepeatedly(Return(ssids));
2268 EXPECT_CALL(*scan_session_, InitiateScan());
Paul Stewart0654ece2013-03-26 15:21:26 -07002269 ReportCurrentBSSChanged(WPASupplicant::kCurrentBSSNull);
mukesh agrawalb66c6462012-05-07 11:45:25 -07002270 dispatcher_.DispatchPendingEvents();
2271}
2272
2273TEST_F(WiFiMainTest, NoScanOnDisconnectWithoutHidden) {
2274 StartWiFi();
mukesh agrawalb66c6462012-05-07 11:45:25 -07002275 dispatcher_.DispatchPendingEvents();
Paul Stewart3c504012013-01-17 17:49:58 -08002276 SetupConnectedService(DBus::Path(), NULL, NULL);
mukesh agrawalb66c6462012-05-07 11:45:25 -07002277 EXPECT_CALL(*GetSupplicantInterfaceProxy(), Scan(_)).Times(0);
Wade Guthriedf6d61b2013-07-17 11:43:55 -07002278 EXPECT_TRUE(IsScanSessionNull());
Paul Stewart3c504012013-01-17 17:49:58 -08002279 EXPECT_CALL(*wifi_provider(), GetHiddenSSIDList())
2280 .WillRepeatedly(Return(ByteArrays()));
Paul Stewart0654ece2013-03-26 15:21:26 -07002281 ReportCurrentBSSChanged(WPASupplicant::kCurrentBSSNull);
mukesh agrawalb66c6462012-05-07 11:45:25 -07002282 dispatcher_.DispatchPendingEvents();
2283}
2284
Paul Stewart3c508e12012-08-09 11:40:06 -07002285TEST_F(WiFiMainTest, LinkMonitorFailure) {
2286 StartWiFi();
2287 ScopedMockLog log;
2288 EXPECT_CALL(log, Log(_, _, _)).Times(AnyNumber());
2289 MockLinkMonitor *link_monitor = new StrictMock<MockLinkMonitor>();
2290 SetLinkMonitor(link_monitor);
2291 EXPECT_CALL(*link_monitor, IsGatewayFound())
2292 .WillOnce(Return(false))
2293 .WillRepeatedly(Return(true));
2294 EXPECT_CALL(log, Log(logging::LOG_INFO, _,
2295 EndsWith("gateway was never found."))).Times(1);
2296 EXPECT_CALL(*GetSupplicantInterfaceProxy(), Reassociate()).Times(0);
2297 OnLinkMonitorFailure();
2298 EXPECT_CALL(log, Log(logging::LOG_INFO, _,
2299 EndsWith("Called Reassociate()."))).Times(1);
2300 EXPECT_CALL(*GetSupplicantInterfaceProxy(), Reassociate()).Times(1);
2301 OnLinkMonitorFailure();
2302 OnSupplicantVanish();
Paul Stewart3c504012013-01-17 17:49:58 -08002303 Mock::VerifyAndClearExpectations(GetSupplicantInterfaceProxy());
2304 EXPECT_CALL(*GetSupplicantInterfaceProxy(), Reassociate()).Times(0);
Paul Stewart3c508e12012-08-09 11:40:06 -07002305 EXPECT_CALL(log, Log(logging::LOG_ERROR, _,
2306 EndsWith("Cannot reassociate."))).Times(1);
2307 OnLinkMonitorFailure();
2308}
2309
mukesh agrawalcf24a242012-05-21 16:46:11 -07002310TEST_F(WiFiMainTest, SuspectCredentialsOpen) {
Paul Stewart3c504012013-01-17 17:49:58 -08002311 MockWiFiServiceRefPtr service = MakeMockService(flimflam::kSecurityNone);
Paul Stewartbca08f82013-07-09 16:32:37 -07002312 EXPECT_CALL(*service, AddSuspectedCredentialFailure()).Times(0);
2313 EXPECT_FALSE(SuspectCredentials(service, NULL));
mukesh agrawalcf24a242012-05-21 16:46:11 -07002314}
2315
Paul Stewartbca08f82013-07-09 16:32:37 -07002316TEST_F(WiFiMainTest, SuspectCredentialsWPA) {
Paul Stewart3c504012013-01-17 17:49:58 -08002317 MockWiFiServiceRefPtr service = MakeMockService(flimflam::kSecurityWpa);
Paul Stewart0654ece2013-03-26 15:21:26 -07002318 ReportStateChanged(WPASupplicant::kInterfaceState4WayHandshake);
Paul Stewartbca08f82013-07-09 16:32:37 -07002319 EXPECT_CALL(*service, AddSuspectedCredentialFailure())
2320 .WillOnce(Return(false))
2321 .WillOnce(Return(true));
2322 EXPECT_FALSE(SuspectCredentials(service, NULL));
Paul Stewart1369c2b2013-01-11 05:41:26 -08002323 Service::ConnectFailure failure;
Paul Stewartbca08f82013-07-09 16:32:37 -07002324 EXPECT_TRUE(SuspectCredentials(service, &failure));
Paul Stewart1369c2b2013-01-11 05:41:26 -08002325 EXPECT_EQ(Service::kFailureBadPassphrase, failure);
mukesh agrawalcf24a242012-05-21 16:46:11 -07002326}
2327
Paul Stewartf6f96482013-07-12 12:49:15 -07002328TEST_F(WiFiMainTest, SuspectCredentialsWEP) {
2329 StartWiFi();
2330 dispatcher_.DispatchPendingEvents();
2331 MockWiFiServiceRefPtr service = MakeMockService(flimflam::kSecurityWep);
2332 InitiateConnect(service);
2333 SetCurrentService(service);
2334
2335 // These expectations are very much like SetupConnectedService except
2336 // that we verify that ResetSupsectCredentialFailures() is not called
2337 // on the service just because supplicant entered the Completed state.
2338 EXPECT_CALL(*service, SetState(Service::kStateConfiguring));
2339 EXPECT_CALL(*service, ResetSuspectedCredentialFailures()).Times(0);
2340 EXPECT_CALL(*dhcp_provider(), CreateConfig(_, _, _, _)).Times(AnyNumber());
2341 EXPECT_CALL(*dhcp_config_.get(), RequestIP()).Times(AnyNumber());
2342 EXPECT_CALL(*manager(), device_info()).WillRepeatedly(Return(device_info()));
2343 EXPECT_CALL(*device_info(), GetByteCounts(_, _, _))
2344 .WillOnce(DoAll(SetArgumentPointee<2>(0LL), Return(true)));
2345 ReportStateChanged(WPASupplicant::kInterfaceStateCompleted);
2346
2347 Mock::VerifyAndClearExpectations(device_info());
2348 Mock::VerifyAndClearExpectations(service);
2349
2350 // Successful connect.
2351 EXPECT_CALL(*GetSupplicantInterfaceProxy(), EnableHighBitrates()).Times(1);
2352 EXPECT_CALL(*service, ResetSuspectedCredentialFailures());
2353 ReportConnected();
2354
2355 EXPECT_CALL(*device_info(), GetByteCounts(_, _, _))
2356 .WillOnce(DoAll(SetArgumentPointee<2>(1LL), Return(true)))
2357 .WillOnce(DoAll(SetArgumentPointee<2>(0LL), Return(true)))
2358 .WillOnce(DoAll(SetArgumentPointee<2>(0LL), Return(true)));
2359
2360 // If there was an increased byte-count while we were timing out DHCP,
2361 // this should be considered a DHCP failure and not a credential failure.
2362 EXPECT_CALL(*service, ResetSuspectedCredentialFailures()).Times(0);
2363 EXPECT_CALL(*service, DisconnectWithFailure(Service::kFailureDHCP, _));
2364 ReportIPConfigFailure();
2365 Mock::VerifyAndClearExpectations(service);
2366
2367 // Connection failed during DHCP but service does not (yet) believe this is
2368 // due to a passphrase issue.
2369 EXPECT_CALL(*service, AddSuspectedCredentialFailure())
2370 .WillOnce(Return(false));
2371 EXPECT_CALL(*service, DisconnectWithFailure(Service::kFailureDHCP, _));
2372 ReportIPConfigFailure();
2373 Mock::VerifyAndClearExpectations(service);
2374
2375 // Connection failed during DHCP and service believes this is due to a
2376 // passphrase issue.
2377 EXPECT_CALL(*service, AddSuspectedCredentialFailure())
2378 .WillOnce(Return(true));
2379 EXPECT_CALL(*service,
2380 DisconnectWithFailure(Service::kFailureBadPassphrase, _));
2381 ReportIPConfigFailure();
2382}
2383
Paul Stewart1369c2b2013-01-11 05:41:26 -08002384TEST_F(WiFiMainTest, SuspectCredentialsEAPInProgress) {
Paul Stewart3c504012013-01-17 17:49:58 -08002385 MockWiFiServiceRefPtr service = MakeMockService(flimflam::kSecurity8021x);
Paul Stewart735eab52013-03-29 09:19:23 -07002386 EXPECT_CALL(*eap_state_handler_, is_eap_in_progress())
2387 .WillOnce(Return(false))
2388 .WillOnce(Return(true))
2389 .WillOnce(Return(false))
2390 .WillOnce(Return(true));
Paul Stewartbca08f82013-07-09 16:32:37 -07002391 EXPECT_CALL(*service, AddSuspectedCredentialFailure()).Times(0);
2392 EXPECT_FALSE(SuspectCredentials(service, NULL));
2393 Mock::VerifyAndClearExpectations(service);
2394
2395 EXPECT_CALL(*service, AddSuspectedCredentialFailure()).WillOnce(Return(true));
Paul Stewart1369c2b2013-01-11 05:41:26 -08002396 Service::ConnectFailure failure;
Paul Stewartbca08f82013-07-09 16:32:37 -07002397 EXPECT_TRUE(SuspectCredentials(service, &failure));
Paul Stewart1369c2b2013-01-11 05:41:26 -08002398 EXPECT_EQ(Service::kFailureEAPAuthentication, failure);
Paul Stewartbca08f82013-07-09 16:32:37 -07002399 Mock::VerifyAndClearExpectations(service);
2400
2401 EXPECT_CALL(*service, AddSuspectedCredentialFailure()).Times(0);
2402 EXPECT_FALSE(SuspectCredentials(service, NULL));
2403 Mock::VerifyAndClearExpectations(service);
2404
2405 EXPECT_CALL(*service, AddSuspectedCredentialFailure())
2406 .WillOnce(Return(false));
2407 EXPECT_FALSE(SuspectCredentials(service, NULL));
Paul Stewart1369c2b2013-01-11 05:41:26 -08002408}
2409
2410TEST_F(WiFiMainTest, SuspectCredentialsYieldFailureWPA) {
mukesh agrawalcf24a242012-05-21 16:46:11 -07002411 MockWiFiServiceRefPtr service = MakeMockService(flimflam::kSecurityWpa);
2412 SetPendingService(service);
Paul Stewart0654ece2013-03-26 15:21:26 -07002413 ReportStateChanged(WPASupplicant::kInterfaceState4WayHandshake);
mukesh agrawalcf24a242012-05-21 16:46:11 -07002414
Paul Stewartbca08f82013-07-09 16:32:37 -07002415 EXPECT_CALL(*service, AddSuspectedCredentialFailure()).WillOnce(Return(true));
Paul Stewartf2d60912012-07-15 08:37:30 -07002416 EXPECT_CALL(*service, SetFailure(Service::kFailureBadPassphrase));
mukesh agrawal56e32202012-07-26 16:32:11 -07002417 EXPECT_CALL(*service, SetFailureSilent(_)).Times(0);
2418 EXPECT_CALL(*service, SetState(_)).Times(0);
Paul Stewart735eab52013-03-29 09:19:23 -07002419 ScopedMockLog log;
mukesh agrawalcf24a242012-05-21 16:46:11 -07002420 EXPECT_CALL(log, Log(_, _, _)).Times(AnyNumber());
Paul Stewart1369c2b2013-01-11 05:41:26 -08002421 EXPECT_CALL(log, Log(logging::LOG_ERROR, _,
2422 EndsWith(flimflam::kErrorBadPassphrase)));
Paul Stewart0654ece2013-03-26 15:21:26 -07002423 ReportCurrentBSSChanged(WPASupplicant::kCurrentBSSNull);
Paul Stewart1369c2b2013-01-11 05:41:26 -08002424}
2425
2426TEST_F(WiFiMainTest, SuspectCredentialsYieldFailureEAP) {
Paul Stewart1369c2b2013-01-11 05:41:26 -08002427 MockWiFiServiceRefPtr service = MakeMockService(flimflam::kSecurity8021x);
2428 SetCurrentService(service);
Paul Stewart1369c2b2013-01-11 05:41:26 -08002429
Paul Stewart735eab52013-03-29 09:19:23 -07002430 ScopedMockLog log;
2431 EXPECT_CALL(log, Log(_, _, _)).Times(AnyNumber());
Paul Stewart1369c2b2013-01-11 05:41:26 -08002432 EXPECT_CALL(*service, SetFailureSilent(_)).Times(0);
2433 EXPECT_CALL(*service, SetState(_)).Times(0);
Paul Stewart735eab52013-03-29 09:19:23 -07002434 // Ensure that we retrieve is_eap_in_progress() before resetting the
2435 // EAP handler's state.
2436 InSequence seq;
2437 EXPECT_CALL(*eap_state_handler_, is_eap_in_progress())
2438 .WillOnce(Return(true));
Paul Stewartbca08f82013-07-09 16:32:37 -07002439 EXPECT_CALL(*service, AddSuspectedCredentialFailure()).WillOnce(Return(true));
Paul Stewart735eab52013-03-29 09:19:23 -07002440 EXPECT_CALL(*service, SetFailure(Service::kFailureEAPAuthentication));
Paul Stewart1369c2b2013-01-11 05:41:26 -08002441 EXPECT_CALL(log, Log(logging::LOG_ERROR, _,
2442 EndsWith(shill::kErrorEapAuthenticationFailed)));
Paul Stewart735eab52013-03-29 09:19:23 -07002443 EXPECT_CALL(*eap_state_handler_, Reset());
Paul Stewart0654ece2013-03-26 15:21:26 -07002444 ReportCurrentBSSChanged(WPASupplicant::kCurrentBSSNull);
mukesh agrawalcf24a242012-05-21 16:46:11 -07002445}
2446
Paul Stewarte369ece2012-05-22 09:11:03 -07002447// Scanning tests will use a mock of the event dispatcher instead of a real
2448// one.
Paul Stewart1aff7302012-08-04 20:04:47 -07002449class WiFiTimerTest : public WiFiObjectTest {
Paul Stewarte369ece2012-05-22 09:11:03 -07002450 public:
Paul Stewart1aff7302012-08-04 20:04:47 -07002451 WiFiTimerTest() : WiFiObjectTest(&mock_dispatcher_) {}
Paul Stewarte369ece2012-05-22 09:11:03 -07002452
2453 protected:
2454 void ExpectInitialScanSequence();
2455
2456 StrictMock<MockEventDispatcher> mock_dispatcher_;
2457};
2458
Paul Stewart1aff7302012-08-04 20:04:47 -07002459void WiFiTimerTest::ExpectInitialScanSequence() {
Paul Stewarte369ece2012-05-22 09:11:03 -07002460 // Choose a number of iterations some multiple higher than the fast scan
2461 // count.
2462 const int kScanTimes = WiFi::kNumFastScanAttempts * 4;
2463
2464 // Each time we call FireScanTimer() below, WiFi will post a task to actually
2465 // run Scan() on the wpa_supplicant proxy.
2466 EXPECT_CALL(mock_dispatcher_, PostTask(_))
2467 .Times(kScanTimes);
2468 {
2469 InSequence seq;
2470 // The scans immediately after the initial scan should happen at the short
2471 // interval. If we add the initial scan (not invoked in this function) to
2472 // the ones in the expectation below, we get WiFi::kNumFastScanAttempts at
2473 // the fast scan interval.
2474 EXPECT_CALL(mock_dispatcher_, PostDelayedTask(
2475 _, WiFi::kFastScanIntervalSeconds * 1000))
2476 .Times(WiFi::kNumFastScanAttempts - 1)
2477 .WillRepeatedly(Return(true));
2478
2479 // After this, the WiFi device should use the normal scan interval.
2480 EXPECT_CALL(mock_dispatcher_, PostDelayedTask(
2481 _, GetScanInterval() * 1000))
2482 .Times(kScanTimes - WiFi::kNumFastScanAttempts + 1)
2483 .WillRepeatedly(Return(true));
2484
2485 for (int i = 0; i < kScanTimes; i++) {
2486 FireScanTimer();
2487 }
2488 }
2489}
2490
Paul Stewart1aff7302012-08-04 20:04:47 -07002491TEST_F(WiFiTimerTest, FastRescan) {
Paul Stewarte369ece2012-05-22 09:11:03 -07002492 // This PostTask is a result of the call to Scan(NULL), and is meant to
2493 // post a task to call Scan() on the wpa_supplicant proxy immediately.
2494 EXPECT_CALL(mock_dispatcher_, PostTask(_));
2495 EXPECT_CALL(mock_dispatcher_, PostDelayedTask(
2496 _, WiFi::kFastScanIntervalSeconds * 1000))
2497 .WillOnce(Return(true));
2498 StartWiFi();
2499
2500 ExpectInitialScanSequence();
2501
2502 // If we end up disconnecting, the sequence should repeat.
2503 EXPECT_CALL(mock_dispatcher_, PostDelayedTask(
2504 _, WiFi::kFastScanIntervalSeconds * 1000))
2505 .WillOnce(Return(true));
2506 RestartFastScanAttempts();
2507
2508 ExpectInitialScanSequence();
2509}
2510
Paul Stewart1aff7302012-08-04 20:04:47 -07002511TEST_F(WiFiTimerTest, ReconnectTimer) {
2512 EXPECT_CALL(mock_dispatcher_, PostTask(_)).Times(AnyNumber());
2513 EXPECT_CALL(mock_dispatcher_, PostDelayedTask(_, _)).Times(AnyNumber());
Paul Stewart3c504012013-01-17 17:49:58 -08002514 StartWiFi();
2515 SetupConnectedService(DBus::Path(), NULL, NULL);
Paul Stewart1aff7302012-08-04 20:04:47 -07002516 Mock::VerifyAndClearExpectations(&mock_dispatcher_);
2517
2518 EXPECT_CALL(mock_dispatcher_, PostDelayedTask(
2519 _, GetReconnectTimeoutSeconds() * 1000)).Times(1);
2520 StartReconnectTimer();
2521 Mock::VerifyAndClearExpectations(&mock_dispatcher_);
2522 StopReconnectTimer();
2523
2524 EXPECT_CALL(mock_dispatcher_, PostDelayedTask(
2525 _, GetReconnectTimeoutSeconds() * 1000)).Times(1);
2526 StartReconnectTimer();
2527 Mock::VerifyAndClearExpectations(&mock_dispatcher_);
2528 GetReconnectTimeoutCallback().callback().Run();
2529
2530 EXPECT_CALL(mock_dispatcher_, PostDelayedTask(
2531 _, GetReconnectTimeoutSeconds() * 1000)).Times(1);
2532 StartReconnectTimer();
2533 Mock::VerifyAndClearExpectations(&mock_dispatcher_);
2534
2535 EXPECT_CALL(mock_dispatcher_, PostDelayedTask(
2536 _, GetReconnectTimeoutSeconds() * 1000)).Times(0);
2537 StartReconnectTimer();
2538}
2539
Paul Stewartbc6e7392012-05-24 07:07:48 -07002540TEST_F(WiFiMainTest, EAPCertification) {
2541 MockWiFiServiceRefPtr service = MakeMockService(flimflam::kSecurity8021x);
2542 EXPECT_CALL(*service, AddEAPCertification(_, _)).Times(0);
2543
2544 ScopedMockLog log;
2545 EXPECT_CALL(log, Log(logging::LOG_ERROR, _, EndsWith("no current service.")));
2546 map<string, ::DBus::Variant> args;
2547 ReportCertification(args);
2548 Mock::VerifyAndClearExpectations(&log);
2549
2550 SetCurrentService(service);
2551 EXPECT_CALL(log, Log(logging::LOG_ERROR, _, EndsWith("no depth parameter.")));
2552 ReportCertification(args);
2553 Mock::VerifyAndClearExpectations(&log);
2554
2555 const uint32 kDepth = 123;
Paul Stewart0654ece2013-03-26 15:21:26 -07002556 args[WPASupplicant::kInterfacePropertyDepth].writer().append_uint32(kDepth);
Paul Stewartbc6e7392012-05-24 07:07:48 -07002557
2558 EXPECT_CALL(log,
2559 Log(logging::LOG_ERROR, _, EndsWith("no subject parameter.")));
2560 ReportCertification(args);
2561 Mock::VerifyAndClearExpectations(&log);
2562
2563 const string kSubject("subject");
Paul Stewart0654ece2013-03-26 15:21:26 -07002564 args[WPASupplicant::kInterfacePropertySubject].writer()
Paul Stewartbc6e7392012-05-24 07:07:48 -07002565 .append_string(kSubject.c_str());
2566 EXPECT_CALL(*service, AddEAPCertification(kSubject, kDepth)).Times(1);
2567 ReportCertification(args);
2568}
2569
Paul Stewartdb0f9172012-11-30 16:48:09 -08002570TEST_F(WiFiMainTest, EAPEvent) {
Paul Stewartdb0f9172012-11-30 16:48:09 -08002571 ScopedMockLog log;
2572 EXPECT_CALL(log, Log(logging::LOG_ERROR, _, EndsWith("no current service.")));
Paul Stewart735eab52013-03-29 09:19:23 -07002573 EXPECT_CALL(*eap_state_handler_, ParseStatus(_, _, _)).Times(0);
2574 const string kEAPStatus("eap-status");
2575 const string kEAPParameter("eap-parameter");
2576 ReportEAPEvent(kEAPStatus, kEAPParameter);
Paul Stewartdb0f9172012-11-30 16:48:09 -08002577 Mock::VerifyAndClearExpectations(&log);
2578 EXPECT_CALL(log, Log(_, _, _)).Times(AnyNumber());
2579
Paul Stewart735eab52013-03-29 09:19:23 -07002580 MockWiFiServiceRefPtr service = MakeMockService(flimflam::kSecurity8021x);
Paul Stewartdb0f9172012-11-30 16:48:09 -08002581 EXPECT_CALL(*service, SetFailure(_)).Times(0);
Paul Stewart735eab52013-03-29 09:19:23 -07002582 EXPECT_CALL(*eap_state_handler_, ParseStatus(kEAPStatus, kEAPParameter, _));
Paul Stewartdb0f9172012-11-30 16:48:09 -08002583 SetCurrentService(service);
Paul Stewart735eab52013-03-29 09:19:23 -07002584 ReportEAPEvent(kEAPStatus, kEAPParameter);
2585 Mock::VerifyAndClearExpectations(service);
2586 Mock::VerifyAndClearExpectations(eap_state_handler_);
Paul Stewartdb0f9172012-11-30 16:48:09 -08002587
Paul Stewart735eab52013-03-29 09:19:23 -07002588 EXPECT_CALL(*eap_state_handler_, ParseStatus(kEAPStatus, kEAPParameter, _))
2589 .WillOnce(DoAll(SetArgumentPointee<2>(Service::kFailureOutOfRange),
2590 Return(false)));
2591 EXPECT_CALL(*service, DisconnectWithFailure(Service::kFailureOutOfRange, _));
2592 ReportEAPEvent(kEAPStatus, kEAPParameter);
Paul Stewartdb0f9172012-11-30 16:48:09 -08002593}
2594
mukesh agrawalc4f368f2012-06-04 19:45:52 -07002595TEST_F(WiFiMainTest, PendingScanDoesNotCrashAfterStop) {
2596 // Scan is one task that should be skipped after Stop. Others are
2597 // skipped by the same mechanism (invalidating weak pointers), so we
2598 // don't test them individually.
2599 //
2600 // Note that we can't test behavior by setting expectations on the
2601 // supplicant_interface_proxy_, since that is destroyed when we StopWiFi().
2602 StartWiFi();
2603 StopWiFi();
2604 dispatcher_.DispatchPendingEvents();
2605}
2606
Darin Petkov60ceaf32012-10-18 10:36:01 +02002607TEST_F(WiFiMainTest, VerifyPaths) {
Paul Stewart196f50f2013-03-27 18:02:11 -07002608 string path(WPASupplicant::kSupplicantConfPath);
Darin Petkov60ceaf32012-10-18 10:36:01 +02002609 TrimString(path, FilePath::kSeparators, &path);
2610 EXPECT_TRUE(file_util::PathExists(FilePath(SYSROOT).Append(path)));
2611}
2612
Gaurav Shah6d2c72d2012-10-16 16:30:44 -07002613struct BSS {
2614 string bsspath;
2615 string ssid;
2616 string bssid;
2617 int16_t signal_strength;
2618 uint16 frequency;
2619 const char* mode;
2620};
2621
2622TEST_F(WiFiMainTest, GetGeolocationObjects) {
2623 BSS bsses[] = {
2624 {"bssid1", "ssid1", "00:00:00:00:00:00", 5, Metrics::kWiFiFrequency2412,
2625 kNetworkModeInfrastructure},
2626 {"bssid2", "ssid2", "01:00:00:00:00:00", 30, Metrics::kWiFiFrequency5170,
2627 kNetworkModeInfrastructure},
2628 // Same SSID but different BSSID is an additional geolocation object.
2629 {"bssid3", "ssid1", "02:00:00:00:00:00", 100, 0,
2630 kNetworkModeInfrastructure}
2631 };
2632 StartWiFi();
2633 vector<GeolocationInfo> objects;
2634 EXPECT_EQ(objects.size(), 0);
2635
2636 for (size_t i = 0; i < arraysize(bsses); ++i) {
2637 ReportBSS(bsses[i].bsspath, bsses[i].ssid, bsses[i].bssid,
2638 bsses[i].signal_strength, bsses[i].frequency, bsses[i].mode);
2639 objects = wifi()->GetGeolocationObjects();
2640 EXPECT_EQ(objects.size(), i + 1);
2641
2642 GeolocationInfo expected_info;
2643 expected_info.AddField(kGeoMacAddressProperty, bsses[i].bssid);
2644 expected_info.AddField(kGeoSignalStrengthProperty,
2645 StringPrintf("%d", bsses[i].signal_strength));
2646 expected_info.AddField(kGeoChannelProperty, StringPrintf(
2647 "%d", Metrics::WiFiFrequencyToChannel(bsses[i].frequency)));
2648 EXPECT_TRUE(objects[i].Equals(expected_info));
Wade Guthrie7347bf22013-04-30 11:21:51 -07002649 }
2650}
Gaurav Shah6d2c72d2012-10-16 16:30:44 -07002651
Paul Stewart5581d072012-12-17 17:30:20 -08002652TEST_F(WiFiMainTest, SetSupplicantDebugLevel) {
2653 MockSupplicantProcessProxy *process_proxy = supplicant_process_proxy_.get();
2654
2655 // With WiFi not yet started, nothing interesting (including a crash) should
2656 // happen.
2657 EXPECT_CALL(*process_proxy, GetDebugLevel()).Times(0);
2658 EXPECT_CALL(*process_proxy, SetDebugLevel(_)).Times(0);
2659 ReportWiFiDebugScopeChanged(true);
2660
2661 // This unit test turns on WiFi debugging, so when we start WiFi, we should
2662 // check but not set the debug level if we return the "debug" level.
2663 EXPECT_CALL(*process_proxy, GetDebugLevel())
Paul Stewart0654ece2013-03-26 15:21:26 -07002664 .WillOnce(Return(WPASupplicant::kDebugLevelDebug));
Paul Stewart5581d072012-12-17 17:30:20 -08002665 EXPECT_CALL(*process_proxy, SetDebugLevel(_)).Times(0);
2666 StartWiFi();
2667 Mock::VerifyAndClearExpectations(process_proxy);
2668
2669 // If WiFi debugging is toggled and wpa_supplicant reports debugging
2670 // is set to some unmanaged level, WiFi should leave it alone.
2671 EXPECT_CALL(*process_proxy, GetDebugLevel())
Paul Stewart0654ece2013-03-26 15:21:26 -07002672 .WillOnce(Return(WPASupplicant::kDebugLevelError))
2673 .WillOnce(Return(WPASupplicant::kDebugLevelError))
2674 .WillOnce(Return(WPASupplicant::kDebugLevelExcessive))
2675 .WillOnce(Return(WPASupplicant::kDebugLevelExcessive))
2676 .WillOnce(Return(WPASupplicant::kDebugLevelMsgDump))
2677 .WillOnce(Return(WPASupplicant::kDebugLevelMsgDump))
2678 .WillOnce(Return(WPASupplicant::kDebugLevelWarning))
2679 .WillOnce(Return(WPASupplicant::kDebugLevelWarning));
Paul Stewart5581d072012-12-17 17:30:20 -08002680 EXPECT_CALL(*process_proxy, SetDebugLevel(_)).Times(0);
2681 ReportWiFiDebugScopeChanged(true);
2682 ReportWiFiDebugScopeChanged(false);
2683 ReportWiFiDebugScopeChanged(true);
2684 ReportWiFiDebugScopeChanged(false);
2685 ReportWiFiDebugScopeChanged(true);
2686 ReportWiFiDebugScopeChanged(false);
2687 ReportWiFiDebugScopeChanged(true);
2688 ReportWiFiDebugScopeChanged(false);
2689 Mock::VerifyAndClearExpectations(process_proxy);
2690
2691 // If WiFi debugging is turned off and wpa_supplicant reports debugging
2692 // is turned on, WiFi should turn supplicant debugging off.
2693 EXPECT_CALL(*process_proxy, GetDebugLevel())
Paul Stewart0654ece2013-03-26 15:21:26 -07002694 .WillOnce(Return(WPASupplicant::kDebugLevelDebug));
2695 EXPECT_CALL(*process_proxy, SetDebugLevel(WPASupplicant::kDebugLevelInfo))
Paul Stewart5581d072012-12-17 17:30:20 -08002696 .Times(1);
2697 ReportWiFiDebugScopeChanged(false);
2698 Mock::VerifyAndClearExpectations(process_proxy);
2699
2700 // If WiFi debugging is turned on and wpa_supplicant reports debugging
2701 // is turned off, WiFi should turn supplicant debugging on.
2702 EXPECT_CALL(*process_proxy, GetDebugLevel())
Paul Stewart0654ece2013-03-26 15:21:26 -07002703 .WillOnce(Return(WPASupplicant::kDebugLevelInfo));
2704 EXPECT_CALL(*process_proxy, SetDebugLevel(WPASupplicant::kDebugLevelDebug))
Paul Stewart5581d072012-12-17 17:30:20 -08002705 .Times(1);
2706 ReportWiFiDebugScopeChanged(true);
2707 Mock::VerifyAndClearExpectations(process_proxy);
2708
2709 // If WiFi debugging is already in the correct state, it should not be
2710 // changed.
2711 EXPECT_CALL(*process_proxy, GetDebugLevel())
Paul Stewart0654ece2013-03-26 15:21:26 -07002712 .WillOnce(Return(WPASupplicant::kDebugLevelDebug))
2713 .WillOnce(Return(WPASupplicant::kDebugLevelInfo));
Paul Stewart5581d072012-12-17 17:30:20 -08002714 EXPECT_CALL(*process_proxy, SetDebugLevel(_)).Times(0);
2715 ReportWiFiDebugScopeChanged(true);
2716 ReportWiFiDebugScopeChanged(false);
2717
2718 // After WiFi is stopped, we shouldn't be calling the proxy.
2719 EXPECT_CALL(*process_proxy, GetDebugLevel()).Times(0);
2720 EXPECT_CALL(*process_proxy, SetDebugLevel(_)).Times(0);
2721 StopWiFi();
2722 ReportWiFiDebugScopeChanged(true);
2723 ReportWiFiDebugScopeChanged(false);
2724}
2725
Darin Petkov50cb78a2013-02-06 16:17:49 +01002726TEST_F(WiFiMainTest, LogSSID) {
2727 EXPECT_EQ("[SSID=]", WiFi::LogSSID(""));
2728 EXPECT_EQ("[SSID=foo\\x5b\\x09\\x5dbar]", WiFi::LogSSID("foo[\t]bar"));
2729}
2730
mukesh agrawalbebf1b82013-04-23 15:06:33 -07002731// Custom property setters should return false, and make no changes, if
2732// the new value is the same as the old value.
2733TEST_F(WiFiMainTest, CustomSetterNoopChange) {
2734 // SetBgscanShortInterval
2735 {
2736 Error error;
2737 static const uint16 kKnownScanInterval = 4;
2738 // Set to known value.
2739 EXPECT_TRUE(SetBgscanShortInterval(kKnownScanInterval, &error));
2740 EXPECT_TRUE(error.IsSuccess());
2741 // Set to same value.
2742 EXPECT_FALSE(SetBgscanShortInterval(kKnownScanInterval, &error));
2743 EXPECT_TRUE(error.IsSuccess());
2744 }
2745
2746 // SetBgscanSignalThreshold
2747 {
2748 Error error;
2749 static const int32 kKnownSignalThreshold = 4;
2750 // Set to known value.
2751 EXPECT_TRUE(SetBgscanSignalThreshold(kKnownSignalThreshold, &error));
2752 EXPECT_TRUE(error.IsSuccess());
2753 // Set to same value.
2754 EXPECT_FALSE(SetBgscanSignalThreshold(kKnownSignalThreshold, &error));
2755 EXPECT_TRUE(error.IsSuccess());
2756 }
2757
2758 // SetScanInterval
2759 {
2760 Error error;
2761 EXPECT_FALSE(SetScanInterval(GetScanInterval(), &error));
2762 EXPECT_TRUE(error.IsSuccess());
2763 }
2764}
2765
Wade Guthrie0cf3c982013-05-29 09:11:35 -07002766// The following tests check the scan_state_ / scan_method_ state machine.
2767
2768TEST_F(WiFiMainTest, FullScanFindsNothing) {
2769 ScopedMockLog log;
2770 ScopeLogger::GetInstance()->EnableScopesByName("wifi");
2771 ScopeLogger::GetInstance()->set_verbose_level(10);
2772
2773 EXPECT_CALL(*adaptor_, EmitBoolChanged(_, _)).Times(AnyNumber());
2774 VerifyScanState(WiFi::kScanIdle, WiFi::kScanMethodNone);
2775 EnableFullScan();
2776 StartWiFi();
2777 EXPECT_CALL(*adaptor_, EmitBoolChanged(flimflam::kScanningProperty, true));
2778 EXPECT_CALL(*GetSupplicantInterfaceProxy(), Scan(_));
2779 dispatcher_.DispatchPendingEvents();
2780 VerifyScanState(WiFi::kScanScanning, WiFi::kScanMethodFull);
2781
2782 ReportScanDone();
2783 EXPECT_CALL(log, Log(_, _, _)).Times(AnyNumber());
2784 EXPECT_CALL(log, Log(_, _, HasSubstr("FULL_NOCONNECTION ->")));
2785 EXPECT_CALL(*adaptor_, EmitBoolChanged(flimflam::kScanningProperty, false));
2786 dispatcher_.DispatchPendingEvents(); // Launch UpdateScanStateAfterScanDone
2787 VerifyScanState(WiFi::kScanIdle, WiFi::kScanMethodNone);
2788
2789 ScopeLogger::GetInstance()->set_verbose_level(0);
2790 ScopeLogger::GetInstance()->EnableScopesByName("-wifi");
2791}
2792
2793TEST_F(WiFiMainTest, FullScanConnectingToConnected) {
2794 ScopedMockLog log;
2795 ScopeLogger::GetInstance()->EnableScopesByName("wifi");
2796 ScopeLogger::GetInstance()->set_verbose_level(10);
2797
2798 EXPECT_CALL(*adaptor_, EmitBoolChanged(_, _)).Times(AnyNumber());
2799 VerifyScanState(WiFi::kScanIdle, WiFi::kScanMethodNone);
2800 EnableFullScan();
2801 StartWiFi();
2802 EXPECT_CALL(*adaptor_, EmitBoolChanged(flimflam::kScanningProperty, true));
2803 dispatcher_.DispatchPendingEvents(); // Launch ScanTask
2804 VerifyScanState(WiFi::kScanScanning, WiFi::kScanMethodFull);
2805
2806 // Setup a service and ask to connect to it.
2807 WiFiEndpointRefPtr endpoint;
2808 ::DBus::Path bss_path;
Wade Guthriedf6d61b2013-07-17 11:43:55 -07002809 EXPECT_CALL(*adaptor_, EmitBoolChanged(flimflam::kScanningProperty, false));
Wade Guthrie0cf3c982013-05-29 09:11:35 -07002810 MockWiFiServiceRefPtr service =
2811 SetupConnectingService(DBus::Path(), &endpoint, &bss_path);
2812
2813 ReportScanDoneKeepScanSession();
Wade Guthrie0cf3c982013-05-29 09:11:35 -07002814 dispatcher_.DispatchPendingEvents(); // Launch UpdateScanStateAfterScanDone
2815 VerifyScanState(WiFi::kScanConnecting, WiFi::kScanMethodFull);
2816
2817 // Complete the connection.
2818 EXPECT_CALL(*service, NotifyCurrentEndpoint(EndpointMatch(endpoint)));
2819 EXPECT_CALL(log, Log(_, _, _)).Times(AnyNumber());
2820 EXPECT_CALL(log, Log(_, _, HasSubstr("-> FULL_CONNECTED")));
2821 ReportCurrentBSSChanged(bss_path);
2822 VerifyScanState(WiFi::kScanIdle, WiFi::kScanMethodNone);
2823
2824 ScopeLogger::GetInstance()->set_verbose_level(0);
2825 ScopeLogger::GetInstance()->EnableScopesByName("-wifi");
2826}
2827
2828TEST_F(WiFiMainTest, ProgressiveScanConnectingToConnected) {
2829 ScopedMockLog log;
2830 ScopeLogger::GetInstance()->EnableScopesByName("wifi");
2831 ScopeLogger::GetInstance()->set_verbose_level(10);
2832
2833 EXPECT_CALL(*adaptor_, EmitBoolChanged(_, _)).Times(AnyNumber());
Wade Guthrie44f290d2013-05-28 10:16:25 -07002834 EXPECT_CALL(*metrics(), NotifyDeviceScanStarted(_));
Wade Guthrie0cf3c982013-05-29 09:11:35 -07002835 VerifyScanState(WiFi::kScanIdle, WiFi::kScanMethodNone);
2836 StartWiFi();
2837 EXPECT_CALL(*adaptor_, EmitBoolChanged(flimflam::kScanningProperty, true));
2838 dispatcher_.DispatchPendingEvents(); // Runs ProgressiveScanTask
2839 VerifyScanState(WiFi::kScanScanning, WiFi::kScanMethodProgressive);
2840
2841 // Setup a service and ask to connect to it.
2842 WiFiEndpointRefPtr endpoint;
2843 ::DBus::Path bss_path;
Wade Guthriedf6d61b2013-07-17 11:43:55 -07002844 EXPECT_CALL(*adaptor_, EmitBoolChanged(flimflam::kScanningProperty, false));
2845 EXPECT_CALL(*metrics(), NotifyDeviceScanFinished(_));
2846 EXPECT_CALL(*metrics(), NotifyDeviceConnectStarted(_, _));
Wade Guthrie0cf3c982013-05-29 09:11:35 -07002847 MockWiFiServiceRefPtr service =
2848 SetupConnectingService(DBus::Path(), &endpoint, &bss_path);
2849
2850 ReportScanDoneKeepScanSession();
Wade Guthrie0cf3c982013-05-29 09:11:35 -07002851 dispatcher_.DispatchPendingEvents(); // Launch ProgressiveScanTask
2852 VerifyScanState(WiFi::kScanConnecting, WiFi::kScanMethodProgressive);
2853
2854 // Complete the connection.
2855 EXPECT_CALL(*service, NotifyCurrentEndpoint(EndpointMatch(endpoint)));
2856 EXPECT_CALL(log, Log(_, _, _)).Times(AnyNumber());
2857 EXPECT_CALL(log, Log(_, _, HasSubstr("-> PROGRESSIVE_CONNECTED")));
Wade Guthrie44f290d2013-05-28 10:16:25 -07002858 EXPECT_CALL(*metrics(), NotifyDeviceConnectFinished(_));
Wade Guthrie0cf3c982013-05-29 09:11:35 -07002859 ReportCurrentBSSChanged(bss_path);
2860 VerifyScanState(WiFi::kScanIdle, WiFi::kScanMethodNone);
2861
2862 ScopeLogger::GetInstance()->set_verbose_level(0);
2863 ScopeLogger::GetInstance()->EnableScopesByName("-wifi");
2864}
2865
Wade Guthriedf6d61b2013-07-17 11:43:55 -07002866TEST_F(WiFiMainTest, ProgressiveScanConnectingToNotFound) {
2867 NiceScopedMockLog log;
2868
2869 EXPECT_CALL(*adaptor_, EmitBoolChanged(_, _)).Times(AnyNumber());
2870 EXPECT_CALL(*metrics(), NotifyDeviceScanStarted(_));
2871 VerifyScanState(WiFi::kScanIdle, WiFi::kScanMethodNone);
2872 StartWiFi();
2873 EXPECT_CALL(*adaptor_, EmitBoolChanged(flimflam::kScanningProperty, true));
2874 dispatcher_.DispatchPendingEvents(); // Runs ProgressiveScanTask
2875 VerifyScanState(WiFi::kScanScanning, WiFi::kScanMethodProgressive);
2876
2877 // Setup a service and ask to connect to it.
2878 WiFiEndpointRefPtr endpoint;
2879 ::DBus::Path bss_path;
2880 EXPECT_CALL(*adaptor_, EmitBoolChanged(flimflam::kScanningProperty, false));
2881 EXPECT_CALL(*metrics(), NotifyDeviceScanFinished(_));
2882 EXPECT_CALL(*metrics(), NotifyDeviceConnectStarted(_, _));
2883 MockWiFiServiceRefPtr service =
2884 SetupConnectingService(DBus::Path(), &endpoint, &bss_path);
2885
2886 ReportScanDoneKeepScanSession();
2887 dispatcher_.DispatchPendingEvents(); // Launch ProgressiveScanTask
2888 VerifyScanState(WiFi::kScanConnecting, WiFi::kScanMethodProgressive);
2889
2890 // Simulate connection timeout.
2891 ScopeLogger::GetInstance()->EnableScopesByName("wifi");
2892 ScopeLogger::GetInstance()->set_verbose_level(10);
2893 EXPECT_CALL(*service,
2894 NotifyCurrentEndpoint(EndpointMatch(endpoint))).Times(0);
2895 EXPECT_CALL(log, Log(_, _, _)).Times(AnyNumber());
2896 EXPECT_CALL(log,
2897 Log(_, _, HasSubstr("-> PROGRESSIVE_FINISHED_NOCONNECTION")));
2898 EXPECT_CALL(*metrics(), NotifyDeviceConnectFinished(_)).Times(0);
2899 EXPECT_CALL(*metrics(), NotifyDeviceScanFinished(_));
2900 EXPECT_CALL(*metrics(), ResetConnectTimer(_)).Times(2);
2901 TimeoutPendingConnection();
2902 ScopeLogger::GetInstance()->set_verbose_level(0);
2903 ScopeLogger::GetInstance()->EnableScopesByName("-wifi");
2904 VerifyScanState(WiFi::kScanIdle, WiFi::kScanMethodNone);
2905}
2906
Wade Guthrieb9e0ee72013-05-31 09:23:30 -07002907TEST_F(WiFiMainTest, ScanStateUma) {
2908 EXPECT_CALL(*metrics(), SendEnumToUMA(Metrics::kMetricScanResult, _, _)).
2909 Times(0);
Wade Guthriedf6d61b2013-07-17 11:43:55 -07002910 EXPECT_CALL(*metrics(), NotifyDeviceScanStarted(_));
Wade Guthrieb0def9f2013-07-12 13:49:18 -07002911 SetScanState(WiFi::kScanScanning, WiFi::kScanMethodProgressive, __func__);
Wade Guthrieb9e0ee72013-05-31 09:23:30 -07002912
Wade Guthriedf6d61b2013-07-17 11:43:55 -07002913 EXPECT_CALL(*metrics(), NotifyDeviceScanFinished(_));
2914 EXPECT_CALL(*metrics(), NotifyDeviceConnectStarted(_, _));
2915 SetScanState(WiFi::kScanConnecting, WiFi::kScanMethodProgressive, __func__);
2916
2917 EXPECT_CALL(*metrics(), NotifyDeviceConnectFinished(_));
Wade Guthrieb9e0ee72013-05-31 09:23:30 -07002918 EXPECT_CALL(*metrics(), SendEnumToUMA(Metrics::kMetricScanResult, _, _));
Wade Guthrieb0def9f2013-07-12 13:49:18 -07002919 SetScanState(WiFi::kScanConnected, WiFi::kScanMethodProgressive, __func__);
Wade Guthrieb9e0ee72013-05-31 09:23:30 -07002920}
2921
Wade Guthriedf6d61b2013-07-17 11:43:55 -07002922TEST_F(WiFiMainTest, ScanStateNotScanningNoUma) {
2923 EXPECT_CALL(*metrics(), NotifyDeviceScanStarted(_)).Times(0);
2924 EXPECT_CALL(*metrics(), NotifyDeviceConnectStarted(_, _));
2925 SetScanState(WiFi::kScanConnecting, WiFi::kScanMethodNone, __func__);
2926
2927 EXPECT_CALL(*metrics(), NotifyDeviceConnectFinished(_));
2928 EXPECT_CALL(*metrics(), SendEnumToUMA(Metrics::kMetricScanResult, _, _)).
2929 Times(0);
2930 SetScanState(WiFi::kScanConnected, WiFi::kScanMethodNone, __func__);
2931}
2932
2933TEST_F(WiFiMainTest, ConnectToServiceNotPending) {
2934 // Test for SetPendingService(NULL), condition a)
2935 // |ConnectTo|->|DisconnectFrom|.
2936 NiceScopedMockLog log;
2937
2938 // Start scanning.
2939 StartWiFi();
2940 dispatcher_.DispatchPendingEvents(); // Runs ProgressiveScanTask
2941 VerifyScanState(WiFi::kScanScanning, WiFi::kScanMethodProgressive);
2942
2943 // Setup pending service.
2944 MockWiFiServiceRefPtr service_pending(
2945 SetupConnectingService(DBus::Path(), NULL, NULL));
2946 EXPECT_EQ(service_pending.get(), GetPendingService().get());
2947
2948 // ConnectTo a different service than the pending one.
2949 ScopeLogger::GetInstance()->EnableScopesByName("wifi");
2950 ScopeLogger::GetInstance()->set_verbose_level(10);
2951 EXPECT_CALL(*GetSupplicantInterfaceProxy(), Disconnect());
2952 EXPECT_CALL(log, Log(_, _, _)).Times(AnyNumber());
2953 EXPECT_CALL(log, Log(_, _, HasSubstr("-> TRANSITION_TO_CONNECTING")));
2954 EXPECT_CALL(log, Log(_, _, HasSubstr("-> PROGRESSIVE_CONNECTING")));
2955 MockWiFiServiceRefPtr service_connecting(
2956 SetupConnectingService(DBus::Path(), NULL, NULL));
2957 ScopeLogger::GetInstance()->set_verbose_level(0);
2958 ScopeLogger::GetInstance()->EnableScopesByName("-wifi");
2959 EXPECT_EQ(service_connecting.get(), GetPendingService().get());
2960 EXPECT_EQ(NULL, GetCurrentService().get());
2961 VerifyScanState(WiFi::kScanConnecting, WiFi::kScanMethodProgressive);
2962}
2963
2964TEST_F(WiFiMainTest, ConnectToWithError) {
2965 StartWiFi();
2966 dispatcher_.DispatchPendingEvents(); // Runs ProgressiveScanTask
2967 VerifyScanState(WiFi::kScanScanning, WiFi::kScanMethodProgressive);
2968
2969 EXPECT_CALL(*GetSupplicantInterfaceProxy(), AddNetwork(_)).
2970 WillOnce(Throw(
2971 DBus::Error(
2972 "fi.w1.wpa_supplicant1.InterfaceUnknown",
2973 "test threw fi.w1.wpa_supplicant1.InterfaceUnknown")));
2974 EXPECT_CALL(*metrics(), NotifyDeviceScanFinished(_)).Times(0);
2975 EXPECT_CALL(*metrics(), SendEnumToUMA(Metrics::kMetricScanResult, _, _)).
2976 Times(0);
2977 MockWiFiServiceRefPtr service = MakeMockService(flimflam::kSecurityNone);
2978 InitiateConnect(service);
2979 VerifyScanState(WiFi::kScanIdle, WiFi::kScanMethodNone);
2980 EXPECT_TRUE(IsScanSessionNull());
2981}
2982
2983TEST_F(WiFiMainTest, ScanStateHandleDisconnect) {
2984 // Test for SetPendingService(NULL), condition d) Disconnect while scanning.
2985 // Start scanning.
2986 StartWiFi();
2987 dispatcher_.DispatchPendingEvents(); // Runs ProgressiveScanTask
2988 VerifyScanState(WiFi::kScanScanning, WiFi::kScanMethodProgressive);
2989
2990 // Set the pending service.
2991 ReportScanDoneKeepScanSession();
2992 MockWiFiServiceRefPtr service = MakeMockService(flimflam::kSecurityNone);
2993 SetPendingService(service);
2994 VerifyScanState(WiFi::kScanConnecting, WiFi::kScanMethodProgressive);
2995
2996 // Disconnect from the pending service.
2997 EXPECT_CALL(*metrics(), NotifyDeviceScanFinished(_)).Times(0);
2998 EXPECT_CALL(*metrics(), SendEnumToUMA(Metrics::kMetricScanResult, _, _)).
2999 Times(0);
3000 ReportCurrentBSSChanged(WPASupplicant::kCurrentBSSNull);
3001 VerifyScanState(WiFi::kScanIdle, WiFi::kScanMethodNone);
3002}
3003
3004TEST_F(WiFiMainTest, ConnectWhileNotScanning) {
3005 NiceScopedMockLog log;
3006
3007 // Setup WiFi but terminate scan.
3008 StartWiFi();
3009 ReportScanDone();
3010 dispatcher_.DispatchPendingEvents();
3011 EXPECT_CALL(*metrics(), ResetConnectTimer(_));
3012 SetScanState(WiFi::kScanIdle, WiFi::kScanMethodNone, __func__);
3013
3014 // Connecting.
3015 EXPECT_CALL(*metrics(), NotifyDeviceScanStarted(_)).Times(0);
3016 WiFiEndpointRefPtr endpoint;
3017 ::DBus::Path bss_path;
3018 ScopeLogger::GetInstance()->EnableScopesByName("wifi");
3019 ScopeLogger::GetInstance()->set_verbose_level(10);
3020 EXPECT_CALL(log, Log(_, _, _)).Times(AnyNumber());
3021 EXPECT_CALL(log, Log(_, _, HasSubstr("-> TRANSITION_TO_CONNECTING"))).
3022 Times(0);
3023 EXPECT_CALL(log, Log(_, _, HasSubstr("-> CONNECTING (not scan related)")));
3024 MockWiFiServiceRefPtr service =
3025 SetupConnectingService(DBus::Path(), &endpoint, &bss_path);
3026
3027 // Connected.
3028 EXPECT_CALL(log, Log(_, _, HasSubstr("-> CONNECTED (not scan related")));
3029 ReportCurrentBSSChanged(bss_path);
3030 ScopeLogger::GetInstance()->set_verbose_level(0);
3031 ScopeLogger::GetInstance()->EnableScopesByName("-wifi");
3032 VerifyScanState(WiFi::kScanIdle, WiFi::kScanMethodNone);
3033}
3034
Chris Masone853b81b2011-06-24 14:11:41 -07003035} // namespace shill