blob: f2a0fca5c3724570a7f0f5fa661202a71c449fbf [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) {
Ben Chanf024ef42013-09-20 14:21:38 -0700126 EXPECT_TRUE(device_->store().Contains(kNameProperty));
mukesh agrawalde29fa82011-09-16 16:16:36 -0700127 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(),
Ben Chanf024ef42013-09-20 14:21:38 -0700135 kBgscanSignalThresholdProperty,
Chris Masonea8a2c252011-06-27 22:16:30 -0700136 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(),
Ben Chanf024ef42013-09-20 14:21:38 -0700142 kScanIntervalProperty,
mukesh agrawal6bb9e7c2012-01-30 14:57:54 -0800143 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(),
Ben Chanf024ef42013-09-20 14:21:38 -0700150 kScanningProperty,
mukesh agrawal6bb9e7c2012-01-30 14:57:54 -0800151 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(),
Ben Chanf024ef42013-09-20 14:21:38 -0700161 kBgscanMethodProperty,
mukesh agrawal4d0401c2012-01-06 16:05:31 -0800162 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(),
Ben Chanf024ef42013-09-20 14:21:38 -0700171 kBgscanMethodProperty,
mukesh agrawal4d0401c2012-01-06 16:05:31 -0800172 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(
Ben Chanf024ef42013-09-20 14:21:38 -0700185 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(),
Ben Chanf024ef42013-09-20 14:21:38 -0700192 kBgscanMethodProperty,
193 DBusAdaptor::StringToVariant(WPASupplicant::kNetworkBgscanMethodLearn),
mukesh agrawal8abd2f62012-01-30 14:56:14 -0800194 &error));
Paul Stewart0654ece2013-03-26 15:21:26 -0700195 EXPECT_EQ(WPASupplicant::kNetworkBgscanMethodLearn, device_->bgscan_method_);
Paul Stewarte6e8e492013-01-17 11:00:50 -0800196 EXPECT_TRUE(device_->store().GetStringProperty(
Ben Chanf024ef42013-09-20 14:21:38 -0700197 kBgscanMethodProperty, &method, &unused_error));
Paul Stewart0654ece2013-03-26 15:21:26 -0700198 EXPECT_EQ(WPASupplicant::kNetworkBgscanMethodLearn, method);
mukesh agrawal8abd2f62012-01-30 14:56:14 -0800199
200 EXPECT_TRUE(DBusAdaptor::ClearProperty(
Ben Chanf024ef42013-09-20 14:21:38 -0700201 device_->mutable_store(), kBgscanMethodProperty, &error));
Paul Stewarte6e8e492013-01-17 11:00:50 -0800202 EXPECT_TRUE(device_->store().GetStringProperty(
Ben Chanf024ef42013-09-20 14:21:38 -0700203 kBgscanMethodProperty, &method, &unused_error));
Darin Petkov4a66cc52012-06-15 10:08:29 +0200204 EXPECT_EQ(WiFi::kDefaultBgscanMethod, method);
205 EXPECT_TRUE(device_->bgscan_method_.empty());
mukesh agrawal8abd2f62012-01-30 14:56:14 -0800206}
207
Paul Stewart3c504012013-01-17 17:49:58 -0800208MATCHER_P(EndpointMatch, endpoint, "") {
209 return
210 arg->ssid() == endpoint->ssid() &&
211 arg->network_mode() == endpoint->network_mode() &&
212 arg->security_mode() == endpoint->security_mode();
213}
214
Wade Guthrie5a4e2ef2013-04-30 12:51:39 -0700215
Paul Stewarte369ece2012-05-22 09:11:03 -0700216class WiFiObjectTest : public ::testing::TestWithParam<string> {
mukesh agrawal31950242011-07-14 11:53:38 -0700217 public:
Wade Guthriebb9fca22013-04-10 17:21:42 -0700218 explicit WiFiObjectTest(EventDispatcher *dispatcher)
Paul Stewarte369ece2012-05-22 09:11:03 -0700219 : event_dispatcher_(dispatcher),
Thieu Le6c1e3bb2013-02-06 15:20:35 -0800220 metrics_(NULL),
Paul Stewarte369ece2012-05-22 09:11:03 -0700221 manager_(&control_interface_, NULL, &metrics_, &glib_),
222 device_info_(&control_interface_, dispatcher, &metrics_, &manager_),
Chris Masone626719f2011-08-18 16:58:48 -0700223 wifi_(new WiFi(&control_interface_,
Paul Stewarte369ece2012-05-22 09:11:03 -0700224 dispatcher,
Thieu Le3426c8f2012-01-11 17:35:11 -0800225 &metrics_,
Chris Masone626719f2011-08-18 16:58:48 -0700226 &manager_,
227 kDeviceName,
228 kDeviceAddress,
229 0)),
Paul Stewart3c504012013-01-17 17:49:58 -0800230 bss_counter_(0),
mukesh agrawaldc42bb32011-07-28 10:40:26 -0700231 supplicant_process_proxy_(new NiceMock<MockSupplicantProcessProxy>()),
mukesh agrawalb20776f2012-02-10 16:00:36 -0800232 supplicant_bss_proxy_(
233 new NiceMock<MockSupplicantBSSProxy>()),
mukesh agrawalf2f68a52011-09-01 12:15:48 -0700234 dhcp_config_(new MockDHCPConfig(&control_interface_,
Paul Stewartd408fdf2012-05-07 17:15:57 -0700235 kDeviceName)),
Darin Petkov2b8e44e2012-06-25 15:13:26 +0200236 dbus_manager_(new NiceMock<MockDBusManager>()),
Wade Guthrie0cf3c982013-05-29 09:11:35 -0700237 adaptor_(new DeviceMockAdaptor()),
Paul Stewart735eab52013-03-29 09:19:23 -0700238 eap_state_handler_(new NiceMock<MockSupplicantEAPStateHandler>()),
Paul Stewart3c504012013-01-17 17:49:58 -0800239 supplicant_interface_proxy_(
Paul Stewart9413bcc2013-04-04 16:12:43 -0700240 new NiceMock<MockSupplicantInterfaceProxy>()),
mukesh agrawal2f9df4e2012-08-08 12:29:20 -0700241 proxy_factory_(this) {
Wade Guthrie5a4e2ef2013-04-30 12:51:39 -0700242 InstallMockScanSession();
mukesh agrawal31950242011-07-14 11:53:38 -0700243 ::testing::DefaultValue< ::DBus::Path>::Set("/default/path");
mukesh agrawalc01f3982012-01-24 13:48:39 -0800244
Paul Stewartd408fdf2012-05-07 17:15:57 -0700245 ON_CALL(dhcp_provider_, CreateConfig(_, _, _, _)).
mukesh agrawalc01f3982012-01-24 13:48:39 -0800246 WillByDefault(Return(dhcp_config_));
247 ON_CALL(*dhcp_config_.get(), RequestIP()).
248 WillByDefault(Return(true));
Paul Stewart835934a2012-12-06 19:27:09 -0800249 ON_CALL(proxy_factory_, CreateSupplicantNetworkProxy(_, _)).
250 WillByDefault(InvokeWithoutArgs(
251 this, &WiFiObjectTest::CreateSupplicantNetworkProxy));
Wade Guthrie318445d2013-05-16 14:05:28 -0700252 Nl80211Message::SetMessageType(kNl80211FamilyId);
Gary Morainac1bdb42012-02-16 17:42:29 -0800253
Paul Stewart735eab52013-03-29 09:19:23 -0700254 // Transfers ownership.
255 manager_.dbus_manager_.reset(dbus_manager_);
256 wifi_->eap_state_handler_.reset(eap_state_handler_);
Darin Petkov2b8e44e2012-06-25 15:13:26 +0200257
Paul Stewart3c504012013-01-17 17:49:58 -0800258 wifi_->provider_ = &wifi_provider_;
mukesh agrawal5c05b292012-03-07 10:12:52 -0800259 wifi_->time_ = &time_;
Wade Guthriebb9fca22013-04-10 17:21:42 -0700260 wifi_->netlink_manager_ = &netlink_manager_;
Wade Guthrie5a4e2ef2013-04-30 12:51:39 -0700261 wifi_->progressive_scan_enabled_ = true;
Wade Guthrie0cf3c982013-05-29 09:11:35 -0700262 wifi_->adaptor_.reset(adaptor_); // Transfers ownership.
Wade Guthrie5a4e2ef2013-04-30 12:51:39 -0700263
264 // The following is only useful when a real |ScanSession| is used; it is
265 // ignored by |MockScanSession|.
266 wifi_->all_scan_frequencies_.insert(kRandomScanFrequency1);
267 wifi_->all_scan_frequencies_.insert(kRandomScanFrequency2);
268 wifi_->all_scan_frequencies_.insert(kRandomScanFrequency3);
mukesh agrawal31950242011-07-14 11:53:38 -0700269 }
mukesh agrawal5c4dd0b2011-09-14 13:53:14 -0700270
271 virtual void SetUp() {
mukesh agrawal3ab56ac2012-05-31 14:41:43 -0700272 // EnableScopes... so that we can EXPECT_CALL for scoped log messages.
273 ScopeLogger::GetInstance()->EnableScopesByName("wifi");
Christopher Wileyd34a47c2012-08-01 15:42:45 -0700274 ScopeLogger::GetInstance()->set_verbose_level(3);
Darin Petkovab565bb2011-10-06 02:55:51 -0700275 wifi_->proxy_factory_ = &proxy_factory_;
mukesh agrawal5c4dd0b2011-09-14 13:53:14 -0700276 static_cast<Device *>(wifi_)->rtnl_handler_ = &rtnl_handler_;
277 wifi_->set_dhcp_provider(&dhcp_provider_);
Paul Stewart3c504012013-01-17 17:49:58 -0800278 ON_CALL(manager_, device_info()).WillByDefault(Return(&device_info_));
Darin Petkov2b8e44e2012-06-25 15:13:26 +0200279 EXPECT_CALL(manager_, UpdateEnabledTechnologies()).Times(AnyNumber());
mukesh agrawalb20776f2012-02-10 16:00:36 -0800280 EXPECT_CALL(*supplicant_bss_proxy_, Die()).Times(AnyNumber());
mukesh agrawal5c4dd0b2011-09-14 13:53:14 -0700281 }
282
283 virtual void TearDown() {
Paul Stewart3c504012013-01-17 17:49:58 -0800284 EXPECT_CALL(*wifi_provider(), OnEndpointRemoved(_))
285 .WillRepeatedly(Return(reinterpret_cast<WiFiService *>(NULL)));
Eric Shienbrood9a245532012-03-07 14:20:39 -0500286 wifi_->SelectService(NULL);
mukesh agrawalb20776f2012-02-10 16:00:36 -0800287 if (supplicant_bss_proxy_.get()) {
288 EXPECT_CALL(*supplicant_bss_proxy_, Die());
289 }
Darin Petkovab565bb2011-10-06 02:55:51 -0700290 wifi_->proxy_factory_ = NULL;
mukesh agrawal31950242011-07-14 11:53:38 -0700291 // must Stop WiFi instance, to clear its list of services.
292 // otherwise, the WiFi instance will not be deleted. (because
293 // services reference a WiFi instance, creating a cycle.)
Eric Shienbrood9a245532012-03-07 14:20:39 -0500294 wifi_->Stop(NULL, ResultCallback());
mukesh agrawal5c4dd0b2011-09-14 13:53:14 -0700295 wifi_->set_dhcp_provider(NULL);
mukesh agrawal3ab56ac2012-05-31 14:41:43 -0700296 // Reset scope logging, to avoid interfering with other tests.
297 ScopeLogger::GetInstance()->EnableScopesByName("-wifi");
Christopher Wileyd34a47c2012-08-01 15:42:45 -0700298 ScopeLogger::GetInstance()->set_verbose_level(0);
mukesh agrawalf2f68a52011-09-01 12:15:48 -0700299 }
300
Paul Stewart549d44c2012-07-03 12:40:25 -0700301 // Needs to be public since it is called via Invoke().
302 void StopWiFi() {
Paul Stewart549d44c2012-07-03 12:40:25 -0700303 wifi_->SetEnabled(false); // Stop(NULL, ResultCallback());
304 }
305
Christopher Wileyc6184482012-10-24 15:31:56 -0700306 // Needs to be public since it is called via Invoke().
307 void ThrowDBusError() {
308 throw DBus::Error("SomeDBusType", "A handy message");
309 }
Paul Stewart17d90652013-04-04 15:09:11 -0700310 void ResetPendingService() {
311 SetPendingService(NULL);
312 }
Christopher Wileyc6184482012-10-24 15:31:56 -0700313
Wade Guthrie5a4e2ef2013-04-30 12:51:39 -0700314 size_t GetScanFrequencyCount() const {
315 return wifi_->all_scan_frequencies_.size();
316 }
317
318 void SetScanSize(int min, int max) {
319 wifi_->min_frequencies_to_scan_ = min;
320 wifi_->max_frequencies_to_scan_ = max;
321 }
322
323 // This clears WiFi::scan_session_, thereby allowing WiFi::Scan to create a
324 // real scan session.
325 void ClearScanSession() {
326 wifi_->scan_session_.reset();
327 }
328
329 bool IsScanSessionNull() {
330 return !wifi_->scan_session_;
331 }
332
333 void InstallMockScanSession() {
334 WiFiProvider::FrequencyCountList previous_frequencies;
335 std::set<uint16_t> available_frequencies;
336 ScanSession::FractionList fractions;
337 ScanSession::OnScanFailed null_callback;
338 scan_session_ = new MockScanSession(&netlink_manager_,
339 event_dispatcher_,
340 previous_frequencies,
341 available_frequencies,
342 0,
343 fractions,
344 0,
345 0,
Wade Guthrief22681f2013-05-31 11:46:31 -0700346 null_callback,
347 NULL);
Wade Guthrie5a4e2ef2013-04-30 12:51:39 -0700348 wifi_->scan_session_.reset(scan_session_);
349 }
350
351 // Or DisableProgressiveScan()...
352 void EnableFullScan() {
353 wifi_->progressive_scan_enabled_ = false;
354 }
355
Wade Guthrie7347bf22013-04-30 11:21:51 -0700356 void OnTriggerScanResponse(const Nl80211Message &message) {
Wade Guthrie5a4e2ef2013-04-30 12:51:39 -0700357 wifi_->scan_session_->OnTriggerScanResponse(message);
358 }
359
Wade Guthrieb0def9f2013-07-12 13:49:18 -0700360 void SetScanState(WiFi::ScanState new_state,
361 WiFi::ScanMethod new_method,
362 const char *reason) {
363 wifi_->SetScanState(new_state, new_method, reason);
Wade Guthrieb9e0ee72013-05-31 09:23:30 -0700364 }
365
Wade Guthrie9f4aa152013-07-29 16:03:14 -0700366 void VerifyScanState(WiFi::ScanState state, WiFi::ScanMethod method) const {
Wade Guthrie0cf3c982013-05-29 09:11:35 -0700367 EXPECT_EQ(state, wifi_->scan_state_);
368 EXPECT_EQ(method, wifi_->scan_method_);
369 }
370
mukesh agrawal31950242011-07-14 11:53:38 -0700371 protected:
mukesh agrawal7ec71312011-11-10 02:08:26 +0000372 typedef scoped_refptr<MockWiFiService> MockWiFiServiceRefPtr;
373
mukesh agrawal31950242011-07-14 11:53:38 -0700374 class TestProxyFactory : public ProxyFactory {
375 public:
Paul Stewarte369ece2012-05-22 09:11:03 -0700376 explicit TestProxyFactory(WiFiObjectTest *test);
mukesh agrawal31950242011-07-14 11:53:38 -0700377
378 virtual SupplicantProcessProxyInterface *CreateSupplicantProcessProxy(
mukesh agrawal1830fa12011-09-26 14:31:40 -0700379 const char */*dbus_path*/, const char */*dbus_addr*/) {
mukesh agrawaldc42bb32011-07-28 10:40:26 -0700380 return test_->supplicant_process_proxy_.release();
mukesh agrawal31950242011-07-14 11:53:38 -0700381 }
382
383 virtual SupplicantInterfaceProxyInterface *CreateSupplicantInterfaceProxy(
Paul Stewart196f50f2013-03-27 18:02:11 -0700384 SupplicantEventDelegateInterface */*delegate*/,
mukesh agrawal1830fa12011-09-26 14:31:40 -0700385 const DBus::Path &/*object_path*/,
386 const char */*dbus_addr*/) {
mukesh agrawaldc42bb32011-07-28 10:40:26 -0700387 return test_->supplicant_interface_proxy_.release();
mukesh agrawal31950242011-07-14 11:53:38 -0700388 }
389
mukesh agrawalb20776f2012-02-10 16:00:36 -0800390 MOCK_METHOD3(CreateSupplicantBSSProxy,
391 SupplicantBSSProxyInterface *(
392 WiFiEndpoint *wifi_endpoint,
393 const DBus::Path &object_path,
394 const char *dbus_addr));
395
Paul Stewart835934a2012-12-06 19:27:09 -0800396 MOCK_METHOD2(CreateSupplicantNetworkProxy,
397 SupplicantNetworkProxyInterface *(
398 const DBus::Path &object_path,
399 const char *dbus_addr));
400
mukesh agrawal31950242011-07-14 11:53:38 -0700401 private:
mukesh agrawalb20776f2012-02-10 16:00:36 -0800402 SupplicantBSSProxyInterface *CreateSupplicantBSSProxyInternal(
403 WiFiEndpoint */*wifi_endpoint*/,
404 const DBus::Path &/*object_path*/,
405 const char */*dbus_addr*/) {
406 return test_->supplicant_bss_proxy_.release();
407 }
408
Paul Stewarte369ece2012-05-22 09:11:03 -0700409 WiFiObjectTest *test_;
mukesh agrawal31950242011-07-14 11:53:38 -0700410 };
411
Paul Stewart3c504012013-01-17 17:49:58 -0800412 // Simulate the course of events when the last enpoint of a service is
413 // removed.
414 class EndpointRemovalHandler {
Wade Guthriebb9fca22013-04-10 17:21:42 -0700415 public:
416 EndpointRemovalHandler(WiFiRefPtr wifi, const WiFiServiceRefPtr &service)
417 : wifi_(wifi), service_(service) {}
418 virtual ~EndpointRemovalHandler() {}
Paul Stewart3c504012013-01-17 17:49:58 -0800419
Wade Guthriebb9fca22013-04-10 17:21:42 -0700420 WiFiServiceRefPtr OnEndpointRemoved(
421 const WiFiEndpointConstRefPtr &endpoint) {
422 wifi_->DisassociateFromService(service_);
423 return service_;
424 }
Paul Stewart3c504012013-01-17 17:49:58 -0800425
426 private:
427 WiFiRefPtr wifi_;
428 WiFiServiceRefPtr service_;
429 };
430
431 EndpointRemovalHandler *MakeEndpointRemovalHandler(
Wade Guthriebb9fca22013-04-10 17:21:42 -0700432 const WiFiServiceRefPtr &service) {
Paul Stewart3c504012013-01-17 17:49:58 -0800433 return new EndpointRemovalHandler(wifi_, service);
434 }
mukesh agrawalb66c6462012-05-07 11:45:25 -0700435 void CancelScanTimer() {
436 wifi_->scan_timer_callback_.Cancel();
437 }
Paul Stewart3c504012013-01-17 17:49:58 -0800438 // This function creates a new endpoint with a mode set to |mode|. We
439 // synthesize new |path| and |bssid| values, since we don't really care
440 // what they are for unit tests. If "use_ssid" is true, we used the
441 // passed-in ssid, otherwise we create a synthesized value for it as well.
442 WiFiEndpointRefPtr MakeNewEndpoint(const char *mode,
443 bool use_ssid,
444 string *ssid,
445 string *path,
446 string *bssid) {
447 bss_counter_++;
448 if (!use_ssid) {
449 *ssid = base::StringPrintf("ssid%d", bss_counter_);
450 }
451 *path = base::StringPrintf("/interface/bss%d", bss_counter_);
452 *bssid = base::StringPrintf("00:00:00:00:00:%02x", bss_counter_);
453 WiFiEndpointRefPtr endpoint = MakeEndpointWithMode(*ssid, *bssid, mode);
454 EXPECT_CALL(wifi_provider_,
455 OnEndpointAdded(EndpointMatch(endpoint))).Times(1);
456 return endpoint;
457 }
458 WiFiEndpointRefPtr MakeEndpoint(const string &ssid, const string &bssid) {
459 return MakeEndpointWithMode(ssid, bssid, kNetworkModeInfrastructure);
460 }
461 WiFiEndpointRefPtr MakeEndpointWithMode(
462 const string &ssid, const string &bssid, const string &mode) {
463 return WiFiEndpoint::MakeOpenEndpoint(
464 &proxy_factory_, NULL, ssid, bssid, mode, 0, 0);
465 }
466 MockWiFiServiceRefPtr MakeMockServiceWithSSID(
467 vector<uint8_t> ssid, const std::string &security) {
468 return new NiceMock<MockWiFiService>(
469 &control_interface_,
470 event_dispatcher_,
471 &metrics_,
472 &manager_,
473 &wifi_provider_,
474 ssid,
Ben Chanf024ef42013-09-20 14:21:38 -0700475 kModeManaged,
Paul Stewart3c504012013-01-17 17:49:58 -0800476 security,
477 false);
478 }
479 MockWiFiServiceRefPtr MakeMockService(const std::string &security) {
480 return MakeMockServiceWithSSID(vector<uint8_t>(1, 'a'), security);
481 }
482 ::DBus::Path MakeNewEndpointAndService(int16_t signal_strength,
483 uint16 frequency,
484 const char *mode,
485 WiFiEndpointRefPtr *endpoint_ptr,
486 MockWiFiServiceRefPtr *service_ptr) {
487 string ssid;
488 ::DBus::Path path;
489 string bssid;
490 WiFiEndpointRefPtr endpoint =
491 MakeNewEndpoint(mode, false, &ssid, &path, &bssid);
492 MockWiFiServiceRefPtr service =
493 MakeMockServiceWithSSID(endpoint->ssid(), endpoint->security_mode());
494 EXPECT_CALL(wifi_provider_, FindServiceForEndpoint(EndpointMatch(endpoint)))
495 .WillRepeatedly(Return(service));
496 ON_CALL(*service, GetEndpointCount()).WillByDefault(Return(1));
497 ReportBSS(path, ssid, bssid, signal_strength, frequency, mode);
498 if (service_ptr) {
499 *service_ptr = service;
500 }
501 if (endpoint_ptr) {
502 *endpoint_ptr = endpoint;
503 }
504 return path;
505 }
506 ::DBus::Path AddEndpointToService(
507 WiFiServiceRefPtr service,
508 int16_t signal_strength,
509 uint16 frequency,
510 const char *mode,
511 WiFiEndpointRefPtr *endpoint_ptr) {
512 string ssid(service->ssid().begin(), service->ssid().end());
513 ::DBus::Path path;
514 string bssid;
515 WiFiEndpointRefPtr endpoint =
516 MakeNewEndpoint(mode, true, &ssid, &path, &bssid);
517 EXPECT_CALL(wifi_provider_, FindServiceForEndpoint(EndpointMatch(endpoint)))
518 .WillRepeatedly(Return(service));
519 ReportBSS(path, ssid, bssid, signal_strength, frequency, mode);
520 if (endpoint_ptr) {
521 *endpoint_ptr = endpoint;
522 }
523 return path;
524 }
525 void InitiateConnect(WiFiServiceRefPtr service) {
526 map<string, ::DBus::Variant> params;
527 wifi_->ConnectTo(service, params);
528 }
529 void InitiateDisconnect(WiFiServiceRefPtr service) {
530 wifi_->DisconnectFrom(service);
531 }
532 MockWiFiServiceRefPtr SetupConnectingService(
533 const DBus::Path &network_path,
534 WiFiEndpointRefPtr *endpoint_ptr,
535 ::DBus::Path *bss_path_ptr) {
536 if (!network_path.empty()) {
537 EXPECT_CALL(*GetSupplicantInterfaceProxy(), AddNetwork(_))
538 .WillOnce(Return(network_path));
539 EXPECT_CALL(*GetSupplicantInterfaceProxy(), SelectNetwork(network_path));
540 }
541
542 MockWiFiServiceRefPtr service;
543 WiFiEndpointRefPtr endpoint;
544 ::DBus::Path bss_path(MakeNewEndpointAndService(
545 0, 0, kNetworkModeAdHoc, &endpoint, &service));
546 EXPECT_CALL(*service, SetState(Service::kStateAssociating));
547 InitiateConnect(service);
548 Mock::VerifyAndClearExpectations(service);
549 EXPECT_FALSE(GetPendingTimeout().IsCancelled());
550 if (endpoint_ptr) {
551 *endpoint_ptr = endpoint;
552 }
553 if (bss_path_ptr) {
554 *bss_path_ptr = bss_path;
555 }
556 return service;
557 }
558
559 MockWiFiServiceRefPtr SetupConnectedService(
560 const DBus::Path &network_path,
561 WiFiEndpointRefPtr *endpoint_ptr,
562 ::DBus::Path *bss_path_ptr) {
563 WiFiEndpointRefPtr endpoint;
564 ::DBus::Path bss_path;
565 MockWiFiServiceRefPtr service =
566 SetupConnectingService(network_path, &endpoint, &bss_path);
567 if (endpoint_ptr) {
568 *endpoint_ptr = endpoint;
569 }
570 if (bss_path_ptr) {
571 *bss_path_ptr = bss_path;
572 }
573 EXPECT_CALL(*service, NotifyCurrentEndpoint(EndpointMatch(endpoint)));
574 ReportCurrentBSSChanged(bss_path);
575 EXPECT_TRUE(GetPendingTimeout().IsCancelled());
576 Mock::VerifyAndClearExpectations(service);
577
578 EXPECT_CALL(*service, SetState(Service::kStateConfiguring));
Paul Stewartbca08f82013-07-09 16:32:37 -0700579 EXPECT_CALL(*service, ResetSuspectedCredentialFailures());
Paul Stewart3c504012013-01-17 17:49:58 -0800580 EXPECT_CALL(*dhcp_provider(), CreateConfig(_, _, _, _)).Times(AnyNumber());
581 EXPECT_CALL(*dhcp_config_.get(), RequestIP()).Times(AnyNumber());
mukesh agrawala5dda0e2013-08-16 11:53:10 -0700582 EXPECT_CALL(wifi_provider_, IncrementConnectCount(_));
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 Guthrie4823f4f2013-07-25 10:03:03 -0700593 wifi_->Scan(Device::kFullScan, NULL, __func__);
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 Stewart7cd45722013-08-12 14:50:14 -0700613 const string &GetSupplicantBSS() {
614 return wifi_->supplicant_bss_;
615 }
616 void SetSupplicantBSS(const string &bss) {
617 wifi_->supplicant_bss_ = bss;
618 }
Paul Stewart1aff7302012-08-04 20:04:47 -0700619 int GetReconnectTimeoutSeconds() {
620 return WiFi::kReconnectTimeoutSeconds;
621 }
mukesh agrawalb66c6462012-05-07 11:45:25 -0700622 const base::CancelableClosure &GetScanTimer() {
623 return wifi_->scan_timer_callback_;
624 }
mukesh agrawal31950242011-07-14 11:53:38 -0700625 // note: the tests need the proxies referenced by WiFi (not the
Paul Stewarte369ece2012-05-22 09:11:03 -0700626 // proxies instantiated by WiFiObjectTest), to ensure that WiFi
mukesh agrawal31950242011-07-14 11:53:38 -0700627 // sets up its proxies correctly.
628 SupplicantProcessProxyInterface *GetSupplicantProcessProxy() {
629 return wifi_->supplicant_process_proxy_.get();
630 }
Paul Stewart3c504012013-01-17 17:49:58 -0800631 MockSupplicantInterfaceProxy *GetSupplicantInterfaceProxyFromWiFi() {
mukesh agrawal8a3188d2011-12-01 20:56:44 +0000632 return dynamic_cast<MockSupplicantInterfaceProxy *>(
633 wifi_->supplicant_interface_proxy_.get());
mukesh agrawal31950242011-07-14 11:53:38 -0700634 }
Paul Stewart3c504012013-01-17 17:49:58 -0800635 // This function returns the supplicant interface proxy whether
636 // or not we have passed the instantiated object to the WiFi instance
637 // from WiFiObjectTest, so tests don't need to worry about when they
638 // set expectations relative to StartWiFi().
639 MockSupplicantInterfaceProxy *GetSupplicantInterfaceProxy() {
640 MockSupplicantInterfaceProxy *proxy = GetSupplicantInterfaceProxyFromWiFi();
641 return proxy ? proxy : supplicant_interface_proxy_.get();
642 }
Paul Stewart835934a2012-12-06 19:27:09 -0800643 MockSupplicantNetworkProxy *CreateSupplicantNetworkProxy() {
644 return new NiceMock<MockSupplicantNetworkProxy>();
645 }
mukesh agrawal7ec71312011-11-10 02:08:26 +0000646 const string &GetSupplicantState() {
647 return wifi_->supplicant_state_;
648 }
Paul Stewart835934a2012-12-06 19:27:09 -0800649 void ClearCachedCredentials(const WiFiService *service) {
650 return wifi_->ClearCachedCredentials(service);
651 }
Paul Stewart0427cc12013-03-25 13:50:39 -0700652 void NotifyEndpointChanged(const WiFiEndpointConstRefPtr &endpoint) {
653 wifi_->NotifyEndpointChanged(endpoint);
654 }
Ben Chan381fdcc2012-10-14 21:10:36 -0700655 bool RemoveNetwork(const ::DBus::Path &network) {
656 return wifi_->RemoveNetwork(network);
657 }
mukesh agrawal261daca2011-12-02 18:56:56 +0000658 void RemoveBSS(const ::DBus::Path &bss_path);
mukesh agrawal31950242011-07-14 11:53:38 -0700659 void ReportBSS(const ::DBus::Path &bss_path,
660 const string &ssid,
661 const string &bssid,
662 int16_t signal_strength,
Thieu Lee41a72d2012-02-06 20:46:51 +0000663 uint16 frequency,
mukesh agrawal31950242011-07-14 11:53:38 -0700664 const char *mode);
mukesh agrawalc01f3982012-01-24 13:48:39 -0800665 void ReportIPConfigComplete() {
Darin Petkov79d74c92012-03-07 17:20:32 +0100666 wifi_->OnIPConfigUpdated(dhcp_config_, true);
mukesh agrawalc01f3982012-01-24 13:48:39 -0800667 }
Paul Stewartf6f96482013-07-12 12:49:15 -0700668 void ReportIPConfigFailure() {
669 wifi_->OnIPConfigFailure();
670 }
671 void ReportConnected() {
672 wifi_->OnConnected();
673 }
mukesh agrawalf2f68a52011-09-01 12:15:48 -0700674 void ReportLinkUp() {
675 wifi_->LinkEvent(IFF_LOWER_UP, IFF_LOWER_UP);
676 }
mukesh agrawal31950242011-07-14 11:53:38 -0700677 void ReportScanDone() {
Wade Guthrie5a4e2ef2013-04-30 12:51:39 -0700678 // Eliminate |scan_session| so |ScanDoneTask| doesn't launch another scan.
679 wifi_->scan_session_.reset();
680 wifi_->ScanDoneTask();
681 // Make a new |scan_session| so that future scanning is done with the mock.
682 InstallMockScanSession();
683 }
684 void ReportScanDoneKeepScanSession() {
mukesh agrawaldc42bb32011-07-28 10:40:26 -0700685 wifi_->ScanDoneTask();
mukesh agrawal31950242011-07-14 11:53:38 -0700686 }
mukesh agrawal15908392011-11-16 18:29:25 +0000687 void ReportCurrentBSSChanged(const string &new_bss) {
688 wifi_->CurrentBSSChanged(new_bss);
689 }
mukesh agrawal7ec71312011-11-10 02:08:26 +0000690 void ReportStateChanged(const string &new_state) {
691 wifi_->StateChanged(new_state);
692 }
Paul Stewart5581d072012-12-17 17:30:20 -0800693 void ReportWiFiDebugScopeChanged(bool enabled) {
694 wifi_->OnWiFiDebugScopeChanged(enabled);
695 }
Paul Stewart7cd45722013-08-12 14:50:14 -0700696 void RequestStationInfo() {
697 wifi_->RequestStationInfo();
698 }
699 void ReportReceivedStationInfo(const Nl80211Message &nl80211_message) {
700 wifi_->OnReceivedStationInfo(nl80211_message);
701 }
mukesh agrawalcf24a242012-05-21 16:46:11 -0700702 void SetPendingService(const WiFiServiceRefPtr &service) {
Wade Guthriedf6d61b2013-07-17 11:43:55 -0700703 wifi_->SetPendingService(service);
mukesh agrawalcf24a242012-05-21 16:46:11 -0700704 }
mukesh agrawalbebf1b82013-04-23 15:06:33 -0700705 bool SetScanInterval(uint16_t interval_seconds, Error *error) {
706 return wifi_->SetScanInterval(interval_seconds, error);
mukesh agrawalb66c6462012-05-07 11:45:25 -0700707 }
Paul Stewarte369ece2012-05-22 09:11:03 -0700708 uint16_t GetScanInterval() {
709 return wifi_->GetScanInterval(NULL);
710 }
Darin Petkov2b8e44e2012-06-25 15:13:26 +0200711 void StartWiFi(bool supplicant_present) {
Wade Guthriea5995cf2013-07-30 09:31:16 -0700712 EXPECT_CALL(netlink_manager_, SubscribeToEvents(
713 Nl80211Message::kMessageTypeString,
714 NetlinkManager::kEventTypeConfig));
715 EXPECT_CALL(netlink_manager_, SubscribeToEvents(
716 Nl80211Message::kMessageTypeString,
717 NetlinkManager::kEventTypeScan));
718 EXPECT_CALL(netlink_manager_, SubscribeToEvents(
719 Nl80211Message::kMessageTypeString,
720 NetlinkManager::kEventTypeRegulatory));
721 EXPECT_CALL(netlink_manager_, SubscribeToEvents(
722 Nl80211Message::kMessageTypeString,
723 NetlinkManager::kEventTypeMlme));
724 EXPECT_CALL(netlink_manager_, SendNl80211Message(
725 IsNl80211Command(kNl80211FamilyId, NL80211_CMD_GET_WIPHY), _, _));
726
Darin Petkov2b8e44e2012-06-25 15:13:26 +0200727 wifi_->supplicant_present_ = supplicant_present;
mukesh agrawalb66c6462012-05-07 11:45:25 -0700728 wifi_->SetEnabled(true); // Start(NULL, ResultCallback());
mukesh agrawal31950242011-07-14 11:53:38 -0700729 }
Darin Petkov2b8e44e2012-06-25 15:13:26 +0200730 void StartWiFi() {
731 StartWiFi(true);
732 }
mukesh agrawal2f9df4e2012-08-08 12:29:20 -0700733 void OnAfterResume() {
734 wifi_->OnAfterResume();
735 }
736 void OnBeforeSuspend() {
737 wifi_->OnBeforeSuspend();
738 }
Darin Petkov2b8e44e2012-06-25 15:13:26 +0200739 void OnSupplicantAppear() {
740 wifi_->OnSupplicantAppear(":1.7");
741 EXPECT_TRUE(wifi_->supplicant_present_);
742 }
743 void OnSupplicantVanish() {
744 wifi_->OnSupplicantVanish();
745 EXPECT_FALSE(wifi_->supplicant_present_);
746 }
747 bool GetSupplicantPresent() {
748 return wifi_->supplicant_present_;
749 }
Darin Petkov4a66cc52012-06-15 10:08:29 +0200750 bool SetBgscanMethod(const string &method) {
751 ::DBus::Error error;
752 return DBusAdaptor::SetProperty(
753 wifi_->mutable_store(),
Ben Chanf024ef42013-09-20 14:21:38 -0700754 kBgscanMethodProperty,
Darin Petkov4a66cc52012-06-15 10:08:29 +0200755 DBusAdaptor::StringToVariant(method),
756 &error);
757 }
758
759 void AppendBgscan(WiFiService *service,
760 std::map<std::string, DBus::Variant> *service_params) {
761 wifi_->AppendBgscan(service, service_params);
762 }
763
Paul Stewartbc6e7392012-05-24 07:07:48 -0700764 void ReportCertification(const map<string, ::DBus::Variant> &properties) {
765 wifi_->CertificationTask(properties);
766 }
Paul Stewart10ccbb32012-04-26 15:59:30 -0700767
Paul Stewartdb0f9172012-11-30 16:48:09 -0800768 void ReportEAPEvent(const string &status, const string &parameter) {
769 wifi_->EAPEventTask(status, parameter);
770 }
771
Paul Stewarte369ece2012-05-22 09:11:03 -0700772 void RestartFastScanAttempts() {
773 wifi_->RestartFastScanAttempts();
774 }
775
Paul Stewart1aff7302012-08-04 20:04:47 -0700776 void StartReconnectTimer() {
777 wifi_->StartReconnectTimer();
778 }
779
780 void StopReconnectTimer() {
781 wifi_->StopReconnectTimer();
782 }
783
Paul Stewart3c508e12012-08-09 11:40:06 -0700784 void SetLinkMonitor(LinkMonitor *link_monitor) {
785 wifi_->set_link_monitor(link_monitor);
786 }
787
Paul Stewartbca08f82013-07-09 16:32:37 -0700788 bool SuspectCredentials(const WiFiServiceRefPtr &service,
Paul Stewart1369c2b2013-01-11 05:41:26 -0800789 Service::ConnectFailure *failure) {
790 return wifi_->SuspectCredentials(service, failure);
791 }
792
Paul Stewart3c508e12012-08-09 11:40:06 -0700793 void OnLinkMonitorFailure() {
794 wifi_->OnLinkMonitorFailure();
795 }
796
mukesh agrawalbebf1b82013-04-23 15:06:33 -0700797 bool SetBgscanShortInterval(const uint16 &interval, Error *error) {
798 return wifi_->SetBgscanShortInterval(interval, error);
799 }
800
801 bool SetBgscanSignalThreshold(const int32 &threshold, Error *error) {
802 return wifi_->SetBgscanSignalThreshold(threshold, error);
803 }
804
Wade Guthriedf6d61b2013-07-17 11:43:55 -0700805 void TimeoutPendingConnection() {
806 wifi_->PendingTimeoutHandler();
807 }
808
Paul Stewart10ccbb32012-04-26 15:59:30 -0700809 NiceMockControl *control_interface() {
810 return &control_interface_;
811 }
812
Paul Stewart85aea152013-01-22 09:31:56 -0800813 MockMetrics *metrics() {
814 return &metrics_;
815 }
816
mukesh agrawal32399322011-09-01 10:53:43 -0700817 MockManager *manager() {
818 return &manager_;
819 }
Gary Moraind9f893f2012-02-06 10:03:40 -0800820
821 MockDeviceInfo *device_info() {
822 return &device_info_;
823 }
824
825 MockDHCPProvider *dhcp_provider() {
826 return &dhcp_provider_;
827 }
828
mukesh agrawal32399322011-09-01 10:53:43 -0700829 const WiFiConstRefPtr wifi() const {
830 return wifi_;
831 }
832
mukesh agrawalb20776f2012-02-10 16:00:36 -0800833 TestProxyFactory *proxy_factory() {
834 return &proxy_factory_;
835 }
836
Paul Stewart3c504012013-01-17 17:49:58 -0800837 MockWiFiProvider *wifi_provider() {
838 return &wifi_provider_;
839 }
840
Paul Stewarte369ece2012-05-22 09:11:03 -0700841 EventDispatcher *event_dispatcher_;
Wade Guthrie5a4e2ef2013-04-30 12:51:39 -0700842 MockScanSession *scan_session_; // Owned by |wifi_|.
mukesh agrawal5c4dd0b2011-09-14 13:53:14 -0700843 NiceMock<MockRTNLHandler> rtnl_handler_;
mukesh agrawal5c05b292012-03-07 10:12:52 -0800844 MockTime time_;
mukesh agrawal31950242011-07-14 11:53:38 -0700845
846 private:
mukesh agrawaldc42bb32011-07-28 10:40:26 -0700847 NiceMockControl control_interface_;
Thieu Le3426c8f2012-01-11 17:35:11 -0800848 MockMetrics metrics_;
mukesh agrawalf2f68a52011-09-01 12:15:48 -0700849 MockGLib glib_;
Chris Masone2ae797d2011-08-23 20:41:00 -0700850 MockManager manager_;
mukesh agrawalc01f3982012-01-24 13:48:39 -0800851 MockDeviceInfo device_info_;
mukesh agrawal31950242011-07-14 11:53:38 -0700852 WiFiRefPtr wifi_;
Paul Stewart3c504012013-01-17 17:49:58 -0800853 NiceMock<MockWiFiProvider> wifi_provider_;
854 int bss_counter_;
mukesh agrawal31950242011-07-14 11:53:38 -0700855
856 // protected fields interspersed between private fields, due to
857 // initialization order
858 protected:
859 static const char kDeviceName[];
Chris Masone626719f2011-08-18 16:58:48 -0700860 static const char kDeviceAddress[];
mukesh agrawal31950242011-07-14 11:53:38 -0700861 static const char kNetworkModeAdHoc[];
862 static const char kNetworkModeInfrastructure[];
Paul Stewart44663922012-07-30 11:03:03 -0700863 static const char kBSSName[];
864 static const char kSSIDName[];
mukesh agrawal31950242011-07-14 11:53:38 -0700865
mukesh agrawaldc42bb32011-07-28 10:40:26 -0700866 scoped_ptr<MockSupplicantProcessProxy> supplicant_process_proxy_;
mukesh agrawalb20776f2012-02-10 16:00:36 -0800867 scoped_ptr<MockSupplicantBSSProxy> supplicant_bss_proxy_;
mukesh agrawalf2f68a52011-09-01 12:15:48 -0700868 MockDHCPProvider dhcp_provider_;
869 scoped_refptr<MockDHCPConfig> dhcp_config_;
Paul Stewart735eab52013-03-29 09:19:23 -0700870
871 // These pointers track mock objects owned by the WiFi device instance
872 // and manager so we can perform expectations against them.
873 MockDBusManager *dbus_manager_;
Wade Guthrie0cf3c982013-05-29 09:11:35 -0700874 DeviceMockAdaptor *adaptor_;
Paul Stewart735eab52013-03-29 09:19:23 -0700875 MockSupplicantEAPStateHandler *eap_state_handler_;
Wade Guthriebb9fca22013-04-10 17:21:42 -0700876 MockNetlinkManager netlink_manager_;
mukesh agrawal31950242011-07-14 11:53:38 -0700877
878 private:
Paul Stewart3c504012013-01-17 17:49:58 -0800879 scoped_ptr<MockSupplicantInterfaceProxy> supplicant_interface_proxy_;
Paul Stewart835934a2012-12-06 19:27:09 -0800880 NiceMock<TestProxyFactory> proxy_factory_;
mukesh agrawal31950242011-07-14 11:53:38 -0700881};
882
Paul Stewarte369ece2012-05-22 09:11:03 -0700883const char WiFiObjectTest::kDeviceName[] = "wlan0";
884const char WiFiObjectTest::kDeviceAddress[] = "000102030405";
885const char WiFiObjectTest::kNetworkModeAdHoc[] = "ad-hoc";
886const char WiFiObjectTest::kNetworkModeInfrastructure[] = "infrastructure";
Paul Stewart44663922012-07-30 11:03:03 -0700887const char WiFiObjectTest::kBSSName[] = "bss0";
888const char WiFiObjectTest::kSSIDName[] = "ssid0";
mukesh agrawal31950242011-07-14 11:53:38 -0700889
Paul Stewarte369ece2012-05-22 09:11:03 -0700890void WiFiObjectTest::RemoveBSS(const ::DBus::Path &bss_path) {
mukesh agrawalb4bc57d2011-12-07 01:07:47 +0000891 wifi_->BSSRemovedTask(bss_path);
mukesh agrawal261daca2011-12-02 18:56:56 +0000892}
893
Paul Stewarte369ece2012-05-22 09:11:03 -0700894void WiFiObjectTest::ReportBSS(const ::DBus::Path &bss_path,
mukesh agrawal31950242011-07-14 11:53:38 -0700895 const string &ssid,
896 const string &bssid,
897 int16_t signal_strength,
Thieu Lee41a72d2012-02-06 20:46:51 +0000898 uint16 frequency,
mukesh agrawal31950242011-07-14 11:53:38 -0700899 const char *mode) {
900 map<string, ::DBus::Variant> bss_properties;
901
902 {
903 DBus::MessageIter writer(bss_properties["SSID"].writer());
904 writer << vector<uint8_t>(ssid.begin(), ssid.end());
905 }
906 {
907 string bssid_nosep;
908 vector<uint8_t> bssid_bytes;
909 RemoveChars(bssid, ":", &bssid_nosep);
910 base::HexStringToBytes(bssid_nosep, &bssid_bytes);
911
912 DBus::MessageIter writer(bss_properties["BSSID"].writer());
913 writer << bssid_bytes;
914 }
Paul Stewart0654ece2013-03-26 15:21:26 -0700915 bss_properties[WPASupplicant::kBSSPropertySignal].writer().
Thieu Lee41a72d2012-02-06 20:46:51 +0000916 append_int16(signal_strength);
Paul Stewart0654ece2013-03-26 15:21:26 -0700917 bss_properties[WPASupplicant::kBSSPropertyFrequency].writer().
Thieu Lee41a72d2012-02-06 20:46:51 +0000918 append_uint16(frequency);
Paul Stewart0654ece2013-03-26 15:21:26 -0700919 bss_properties[WPASupplicant::kBSSPropertyMode].writer().append_string(mode);
mukesh agrawalb4bc57d2011-12-07 01:07:47 +0000920 wifi_->BSSAddedTask(bss_path, bss_properties);
mukesh agrawal31950242011-07-14 11:53:38 -0700921}
922
Paul Stewarte369ece2012-05-22 09:11:03 -0700923WiFiObjectTest::TestProxyFactory::TestProxyFactory(WiFiObjectTest *test)
mukesh agrawalb20776f2012-02-10 16:00:36 -0800924 : test_(test) {
925 EXPECT_CALL(*this, CreateSupplicantBSSProxy(_, _, _)).Times(AnyNumber());
926 ON_CALL(*this, CreateSupplicantBSSProxy(_, _, _))
927 .WillByDefault(
928 Invoke(this, (&TestProxyFactory::CreateSupplicantBSSProxyInternal)));
929}
930
Paul Stewarte369ece2012-05-22 09:11:03 -0700931// Most of our tests involve using a real EventDispatcher object.
932class WiFiMainTest : public WiFiObjectTest {
933 public:
934 WiFiMainTest() : WiFiObjectTest(&dispatcher_) {}
935
936 protected:
Wade Guthriea5995cf2013-07-30 09:31:16 -0700937 // A progressive scan requests one or more scans, each of which asks about a
938 // different batch of frequencies/channels.
939 enum WhichBatchOfProgressiveScan {
940 kFirstProgressiveScanBatch,
941 kNotFirstProgressiveScanBatch
942 };
Wade Guthrie9f4aa152013-07-29 16:03:14 -0700943 void StartScan(WiFi::ScanMethod method) {
944 if (method == WiFi::kScanMethodFull) {
945 EnableFullScan();
Wade Guthrie9f4aa152013-07-29 16:03:14 -0700946 }
947 VerifyScanState(WiFi::kScanIdle, WiFi::kScanMethodNone);
Ben Chanf024ef42013-09-20 14:21:38 -0700948 EXPECT_CALL(*adaptor_, EmitBoolChanged(kPoweredProperty, _)).
Wade Guthriea5995cf2013-07-30 09:31:16 -0700949 Times(AnyNumber());
950 ExpectScanStart(kFirstProgressiveScanBatch, method);
Wade Guthrie9f4aa152013-07-29 16:03:14 -0700951 StartWiFi();
Wade Guthrie9f4aa152013-07-29 16:03:14 -0700952 dispatcher_.DispatchPendingEvents();
953 VerifyScanState(WiFi::kScanScanning, method);
954 }
955
956 MockWiFiServiceRefPtr AttemptConnection(WiFi::ScanMethod method,
957 WiFiEndpointRefPtr *endpoint,
958 ::DBus::Path *bss_path) {
959 WiFiEndpointRefPtr dummy_endpoint;
960 if (!endpoint) {
961 endpoint = &dummy_endpoint; // If caller doesn't care about endpoint.
962 }
963
964 ::DBus::Path dummy_bss_path;
965 if (!bss_path) {
966 bss_path = &dummy_bss_path; // If caller doesn't care about bss_path.
967 }
968
Wade Guthriea5995cf2013-07-30 09:31:16 -0700969 ExpectScanStop();
970 ExpectConnecting();
Wade Guthrie9f4aa152013-07-29 16:03:14 -0700971 MockWiFiServiceRefPtr service =
972 SetupConnectingService(DBus::Path(), endpoint, bss_path);
973 ReportScanDoneKeepScanSession();
974 dispatcher_.DispatchPendingEvents();
975 VerifyScanState(WiFi::kScanConnecting, method);
976
977 return service;
978 }
979
Wade Guthriea5995cf2013-07-30 09:31:16 -0700980 void ExpectScanStart(WhichBatchOfProgressiveScan which_batch,
981 WiFi::ScanMethod method) {
982 if (method == WiFi::kScanMethodProgressive) {
983 EXPECT_CALL(*scan_session_, HasMoreFrequencies());
984 EXPECT_CALL(*scan_session_, InitiateScan());
985 }
986 if (which_batch == kFirstProgressiveScanBatch) {
Ben Chanf024ef42013-09-20 14:21:38 -0700987 EXPECT_CALL(*adaptor_, EmitBoolChanged(kScanningProperty,
Wade Guthriea5995cf2013-07-30 09:31:16 -0700988 true));
989 EXPECT_CALL(*metrics(), NotifyDeviceScanStarted(_));
990 }
991 }
992
993 // Scanning can stop for any reason (including transitioning to connecting).
994 void ExpectScanStop() {
Ben Chanf024ef42013-09-20 14:21:38 -0700995 EXPECT_CALL(*adaptor_, EmitBoolChanged(kScanningProperty, false));
Wade Guthriea5995cf2013-07-30 09:31:16 -0700996 }
997
998 void ExpectConnecting() {
999 EXPECT_CALL(*metrics(), NotifyDeviceScanFinished(_));
1000 EXPECT_CALL(*metrics(), NotifyDeviceConnectStarted(_, _));
1001 }
1002
1003 void ExpectConnected() {
1004 EXPECT_CALL(*metrics(), NotifyDeviceConnectFinished(_));
1005 ExpectScanIdle();
1006 }
1007
1008 void ExpectFoundNothing() {
1009 EXPECT_CALL(*metrics(), NotifyDeviceScanFinished(_));
1010 EXPECT_CALL(*metrics(), ResetConnectTimer(_));
1011 ExpectScanIdle();
1012 }
1013
1014 void ExpectScanIdle() {
1015 EXPECT_CALL(*metrics(), ResetScanTimer(_));
1016 EXPECT_CALL(*metrics(), ResetConnectTimer(_)).RetiresOnSaturation();
1017 }
1018
Paul Stewarte369ece2012-05-22 09:11:03 -07001019 EventDispatcher dispatcher_;
1020};
1021
mukesh agrawal31950242011-07-14 11:53:38 -07001022TEST_F(WiFiMainTest, ProxiesSetUpDuringStart) {
1023 EXPECT_TRUE(GetSupplicantProcessProxy() == NULL);
Paul Stewart3c504012013-01-17 17:49:58 -08001024 EXPECT_TRUE(GetSupplicantInterfaceProxyFromWiFi() == NULL);
mukesh agrawal31950242011-07-14 11:53:38 -07001025
1026 StartWiFi();
1027 EXPECT_FALSE(GetSupplicantProcessProxy() == NULL);
Paul Stewart3c504012013-01-17 17:49:58 -08001028 EXPECT_FALSE(GetSupplicantInterfaceProxyFromWiFi() == NULL);
mukesh agrawal31950242011-07-14 11:53:38 -07001029}
1030
Darin Petkov2b8e44e2012-06-25 15:13:26 +02001031TEST_F(WiFiMainTest, SupplicantPresent) {
1032 EXPECT_FALSE(GetSupplicantPresent());
1033}
1034
1035TEST_F(WiFiMainTest, OnSupplicantAppearStarted) {
1036 EXPECT_TRUE(GetSupplicantProcessProxy() == NULL);
1037
Paul Stewart0654ece2013-03-26 15:21:26 -07001038 EXPECT_CALL(*dbus_manager_, WatchName(WPASupplicant::kDBusAddr, _, _));
Darin Petkov2b8e44e2012-06-25 15:13:26 +02001039 StartWiFi(false); // No supplicant present.
1040 EXPECT_TRUE(GetSupplicantProcessProxy() == NULL);
1041
1042 OnSupplicantAppear();
1043 EXPECT_FALSE(GetSupplicantProcessProxy() == NULL);
Darin Petkov9cd7ca12012-07-03 11:06:40 +02001044
1045 // If supplicant reappears while the device is started, the device should be
1046 // restarted.
1047 EXPECT_CALL(*manager(), DeregisterDevice(_));
1048 EXPECT_CALL(*manager(), RegisterDevice(_));
1049 OnSupplicantAppear();
Darin Petkov2b8e44e2012-06-25 15:13:26 +02001050}
1051
1052TEST_F(WiFiMainTest, OnSupplicantAppearStopped) {
1053 EXPECT_TRUE(GetSupplicantProcessProxy() == NULL);
1054
1055 OnSupplicantAppear();
1056 EXPECT_TRUE(GetSupplicantProcessProxy() == NULL);
Darin Petkov9cd7ca12012-07-03 11:06:40 +02001057
1058 // If supplicant reappears while the device is stopped, the device should not
1059 // be restarted.
1060 EXPECT_CALL(*manager(), DeregisterDevice(_)).Times(0);
1061 OnSupplicantAppear();
Darin Petkov2b8e44e2012-06-25 15:13:26 +02001062}
1063
1064TEST_F(WiFiMainTest, OnSupplicantVanishStarted) {
1065 EXPECT_TRUE(GetSupplicantProcessProxy() == NULL);
1066
1067 StartWiFi();
1068 EXPECT_FALSE(GetSupplicantProcessProxy() == NULL);
1069 EXPECT_TRUE(GetSupplicantPresent());
1070
1071 EXPECT_CALL(*manager(), DeregisterDevice(_));
1072 EXPECT_CALL(*manager(), RegisterDevice(_));
1073 OnSupplicantVanish();
Darin Petkov2b8e44e2012-06-25 15:13:26 +02001074}
1075
1076TEST_F(WiFiMainTest, OnSupplicantVanishStopped) {
1077 OnSupplicantAppear();
1078 EXPECT_TRUE(GetSupplicantPresent());
1079 EXPECT_CALL(*manager(), DeregisterDevice(_)).Times(0);
1080 OnSupplicantVanish();
1081}
1082
Paul Stewart549d44c2012-07-03 12:40:25 -07001083TEST_F(WiFiMainTest, OnSupplicantVanishedWhileConnected) {
Paul Stewart3c504012013-01-17 17:49:58 -08001084 StartWiFi();
1085 WiFiEndpointRefPtr endpoint;
1086 WiFiServiceRefPtr service(
1087 SetupConnectedService(DBus::Path(), &endpoint, NULL));
Paul Stewart549d44c2012-07-03 12:40:25 -07001088 ScopedMockLog log;
1089 EXPECT_CALL(log, Log(_, _, _)).Times(AnyNumber());
Christopher Wileyc6184482012-10-24 15:31:56 -07001090 EXPECT_CALL(log, Log(logging::LOG_ERROR, _,
Paul Stewart549d44c2012-07-03 12:40:25 -07001091 EndsWith("silently resetting current_service_.")));
1092 EXPECT_CALL(*manager(), DeregisterDevice(_))
1093 .WillOnce(InvokeWithoutArgs(this, &WiFiObjectTest::StopWiFi));
Paul Stewart3c504012013-01-17 17:49:58 -08001094 scoped_ptr<EndpointRemovalHandler> handler(
1095 MakeEndpointRemovalHandler(service));
1096 EXPECT_CALL(*wifi_provider(), OnEndpointRemoved(EndpointMatch(endpoint)))
1097 .WillOnce(Invoke(handler.get(),
1098 &EndpointRemovalHandler::OnEndpointRemoved));
1099 EXPECT_CALL(*GetSupplicantInterfaceProxy(), Disconnect()).Times(0);
Paul Stewart549d44c2012-07-03 12:40:25 -07001100 EXPECT_CALL(*manager(), RegisterDevice(_));
1101 OnSupplicantVanish();
1102 EXPECT_TRUE(GetCurrentService() == NULL);
1103}
1104
Wade Guthrie5a4e2ef2013-04-30 12:51:39 -07001105TEST_F(WiFiMainTest, CleanStart_FullScan) {
1106 EnableFullScan();
mukesh agrawal31950242011-07-14 11:53:38 -07001107 EXPECT_CALL(*supplicant_process_proxy_, CreateInterface(_));
1108 EXPECT_CALL(*supplicant_process_proxy_, GetInterface(_))
1109 .Times(AnyNumber())
1110 .WillRepeatedly(Throw(
1111 DBus::Error(
1112 "fi.w1.wpa_supplicant1.InterfaceUnknown",
1113 "test threw fi.w1.wpa_supplicant1.InterfaceUnknown")));
mukesh agrawalb66c6462012-05-07 11:45:25 -07001114 EXPECT_TRUE(GetScanTimer().IsCancelled());
mukesh agrawal31950242011-07-14 11:53:38 -07001115 StartWiFi();
Paul Stewart3c504012013-01-17 17:49:58 -08001116 EXPECT_CALL(*GetSupplicantInterfaceProxy(), Scan(_));
mukesh agrawal32399322011-09-01 10:53:43 -07001117 dispatcher_.DispatchPendingEvents();
mukesh agrawalb66c6462012-05-07 11:45:25 -07001118 EXPECT_FALSE(GetScanTimer().IsCancelled());
mukesh agrawal31950242011-07-14 11:53:38 -07001119}
1120
Wade Guthrie5a4e2ef2013-04-30 12:51:39 -07001121TEST_F(WiFiMainTest, CleanStart) {
1122 EXPECT_CALL(*supplicant_process_proxy_, CreateInterface(_));
1123 EXPECT_CALL(*supplicant_process_proxy_, GetInterface(_))
1124 .Times(AnyNumber())
1125 .WillRepeatedly(Throw(
1126 DBus::Error(
1127 "fi.w1.wpa_supplicant1.InterfaceUnknown",
1128 "test threw fi.w1.wpa_supplicant1.InterfaceUnknown")));
1129 EXPECT_TRUE(GetScanTimer().IsCancelled());
1130 StartWiFi();
1131 EXPECT_CALL(*scan_session_, InitiateScan());
1132 dispatcher_.DispatchPendingEvents();
1133 EXPECT_FALSE(GetScanTimer().IsCancelled());
1134}
1135
Paul Stewart835934a2012-12-06 19:27:09 -08001136TEST_F(WiFiMainTest, ClearCachedCredentials) {
Paul Stewart3c504012013-01-17 17:49:58 -08001137 StartWiFi();
Paul Stewart835934a2012-12-06 19:27:09 -08001138 DBus::Path network = "/test/path";
Paul Stewart3c504012013-01-17 17:49:58 -08001139 WiFiServiceRefPtr service(SetupConnectedService(network, NULL, NULL));
1140 EXPECT_CALL(*GetSupplicantInterfaceProxy(), RemoveNetwork(network));
Paul Stewart835934a2012-12-06 19:27:09 -08001141 ClearCachedCredentials(service);
1142}
1143
Paul Stewart0427cc12013-03-25 13:50:39 -07001144TEST_F(WiFiMainTest, NotifyEndpointChanged) {
1145 WiFiEndpointRefPtr endpoint =
1146 MakeEndpointWithMode("ssid", "00:00:00:00:00:00", kNetworkModeAdHoc);
1147 EXPECT_CALL(*wifi_provider(), OnEndpointUpdated(EndpointMatch(endpoint)));
1148 NotifyEndpointChanged(endpoint);
1149}
1150
Ben Chan381fdcc2012-10-14 21:10:36 -07001151TEST_F(WiFiMainTest, RemoveNetwork) {
1152 DBus::Path network = "/test/path";
Ben Chan381fdcc2012-10-14 21:10:36 -07001153 StartWiFi();
Paul Stewart3c504012013-01-17 17:49:58 -08001154 EXPECT_CALL(*GetSupplicantInterfaceProxy(), RemoveNetwork(network));
Ben Chan381fdcc2012-10-14 21:10:36 -07001155 EXPECT_TRUE(RemoveNetwork(network));
1156}
1157
1158TEST_F(WiFiMainTest, RemoveNetworkWhenSupplicantReturnsNetworkUnknown) {
1159 DBus::Path network = "/test/path";
Paul Stewart3c504012013-01-17 17:49:58 -08001160 EXPECT_CALL(*GetSupplicantInterfaceProxy(), RemoveNetwork(network))
Ben Chan381fdcc2012-10-14 21:10:36 -07001161 .WillRepeatedly(Throw(
1162 DBus::Error(
1163 "fi.w1.wpa_supplicant1.NetworkUnknown",
1164 "test threw fi.w1.wpa_supplicant1.NetworkUnknown")));
1165 StartWiFi();
1166 EXPECT_TRUE(RemoveNetwork(network));
1167}
1168
Arman Ugurayed8e6102012-11-29 14:47:20 -08001169TEST_F(WiFiMainTest, UseArpGateway) {
1170 EXPECT_CALL(dhcp_provider_, CreateConfig(kDeviceName, _, _, true))
1171 .WillOnce(Return(dhcp_config_));
1172 const_cast<WiFi *>(wifi().get())->AcquireIPConfig();
1173}
1174
Ben Chan381fdcc2012-10-14 21:10:36 -07001175TEST_F(WiFiMainTest, RemoveNetworkWhenSupplicantReturnsInvalidArgs) {
1176 DBus::Path network = "/test/path";
Paul Stewart3c504012013-01-17 17:49:58 -08001177 EXPECT_CALL(*GetSupplicantInterfaceProxy(), RemoveNetwork(network))
Ben Chan381fdcc2012-10-14 21:10:36 -07001178 .WillRepeatedly(Throw(
1179 DBus::Error(
1180 "fi.w1.wpa_supplicant1.InvalidArgs",
1181 "test threw fi.w1.wpa_supplicant1.InvalidArgs")));
1182 StartWiFi();
1183 EXPECT_FALSE(RemoveNetwork(network));
1184}
1185
1186TEST_F(WiFiMainTest, RemoveNetworkWhenSupplicantReturnsUnknownError) {
1187 DBus::Path network = "/test/path";
Paul Stewart3c504012013-01-17 17:49:58 -08001188 EXPECT_CALL(*GetSupplicantInterfaceProxy(), RemoveNetwork(network))
Ben Chan381fdcc2012-10-14 21:10:36 -07001189 .WillRepeatedly(Throw(
1190 DBus::Error(
1191 "fi.w1.wpa_supplicant1.UnknownError",
1192 "test threw fi.w1.wpa_supplicant1.UnknownError")));
1193 StartWiFi();
1194 EXPECT_FALSE(RemoveNetwork(network));
1195}
1196
Wade Guthrie5a4e2ef2013-04-30 12:51:39 -07001197TEST_F(WiFiMainTest, Restart_FullScan) {
1198 EnableFullScan();
mukesh agrawal31950242011-07-14 11:53:38 -07001199 EXPECT_CALL(*supplicant_process_proxy_, CreateInterface(_))
1200 .Times(AnyNumber())
1201 .WillRepeatedly(Throw(
1202 DBus::Error(
1203 "fi.w1.wpa_supplicant1.InterfaceExists",
Darin Petkov2b8e44e2012-06-25 15:13:26 +02001204 "test threw fi.w1.wpa_supplicant1.InterfaceExists")));
mukesh agrawal31950242011-07-14 11:53:38 -07001205 EXPECT_CALL(*supplicant_process_proxy_, GetInterface(_));
Paul Stewart3c504012013-01-17 17:49:58 -08001206 EXPECT_CALL(*GetSupplicantInterfaceProxy(), Scan(_));
mukesh agrawal31950242011-07-14 11:53:38 -07001207 StartWiFi();
mukesh agrawal32399322011-09-01 10:53:43 -07001208 dispatcher_.DispatchPendingEvents();
mukesh agrawal31950242011-07-14 11:53:38 -07001209}
1210
Wade Guthrie5a4e2ef2013-04-30 12:51:39 -07001211TEST_F(WiFiMainTest, Restart) {
1212 EXPECT_CALL(*supplicant_process_proxy_, CreateInterface(_))
1213 .Times(AnyNumber())
1214 .WillRepeatedly(Throw(
1215 DBus::Error(
1216 "fi.w1.wpa_supplicant1.InterfaceExists",
1217 "test threw fi.w1.wpa_supplicant1.InterfaceExists")));
1218 EXPECT_CALL(*scan_session_, InitiateScan());
1219 StartWiFi();
1220 dispatcher_.DispatchPendingEvents();
1221}
1222
mukesh agrawal31950242011-07-14 11:53:38 -07001223TEST_F(WiFiMainTest, StartClearsState) {
Paul Stewart3c504012013-01-17 17:49:58 -08001224 EXPECT_CALL(*GetSupplicantInterfaceProxy(), RemoveAllNetworks());
1225 EXPECT_CALL(*GetSupplicantInterfaceProxy(), FlushBSS(_));
mukesh agrawal31950242011-07-14 11:53:38 -07001226 StartWiFi();
1227}
1228
Wade Guthrie5a4e2ef2013-04-30 12:51:39 -07001229TEST_F(WiFiMainTest, NoScansWhileConnecting_FullScan) {
1230 EnableFullScan();
Christopher Wileyc68c8672012-11-20 16:52:21 -08001231 StartWiFi();
Paul Stewart3c504012013-01-17 17:49:58 -08001232 EXPECT_CALL(*GetSupplicantInterfaceProxy(), Scan(_)).Times(1);
1233 dispatcher_.DispatchPendingEvents();
1234 Mock::VerifyAndClearExpectations(GetSupplicantInterfaceProxy());
Ben Chanf024ef42013-09-20 14:21:38 -07001235 MockWiFiServiceRefPtr service = MakeMockService(kSecurityNone);
Christopher Wileyc68c8672012-11-20 16:52:21 -08001236 SetPendingService(service);
1237 // If we're connecting, we ignore scan requests to stay on channel.
Paul Stewart3c504012013-01-17 17:49:58 -08001238 EXPECT_CALL(*service, IsConnecting()).WillOnce(Return(true));
Christopher Wileyc68c8672012-11-20 16:52:21 -08001239 EXPECT_CALL(*GetSupplicantInterfaceProxy(), Scan(_)).Times(0);
Wade Guthrie5a4e2ef2013-04-30 12:51:39 -07001240 TriggerFullScan();
Christopher Wileyc68c8672012-11-20 16:52:21 -08001241 dispatcher_.DispatchPendingEvents();
1242 Mock::VerifyAndClearExpectations(GetSupplicantInterfaceProxy());
Paul Stewart3c504012013-01-17 17:49:58 -08001243 Mock::VerifyAndClearExpectations(service);
1244 EXPECT_CALL(*service, IsConnecting()).WillOnce(Return(false));
Christopher Wileyc68c8672012-11-20 16:52:21 -08001245 EXPECT_CALL(*GetSupplicantInterfaceProxy(), Scan(_)).Times(1);
Wade Guthrie5a4e2ef2013-04-30 12:51:39 -07001246 TriggerFullScan();
Christopher Wileyc68c8672012-11-20 16:52:21 -08001247 dispatcher_.DispatchPendingEvents();
1248 Mock::VerifyAndClearExpectations(GetSupplicantInterfaceProxy());
Paul Stewart3c504012013-01-17 17:49:58 -08001249 Mock::VerifyAndClearExpectations(service);
Christopher Wileyc68c8672012-11-20 16:52:21 -08001250 // Similarly, ignore scans when our connected service is reconnecting.
1251 SetPendingService(NULL);
1252 SetCurrentService(service);
Paul Stewart3c504012013-01-17 17:49:58 -08001253 EXPECT_CALL(*service, IsConnecting()).WillOnce(Return(true));
Christopher Wileyc68c8672012-11-20 16:52:21 -08001254 EXPECT_CALL(*GetSupplicantInterfaceProxy(), Scan(_)).Times(0);
Wade Guthrie5a4e2ef2013-04-30 12:51:39 -07001255 TriggerFullScan();
Christopher Wileyc68c8672012-11-20 16:52:21 -08001256 dispatcher_.DispatchPendingEvents();
1257 Mock::VerifyAndClearExpectations(GetSupplicantInterfaceProxy());
Paul Stewart3c504012013-01-17 17:49:58 -08001258 Mock::VerifyAndClearExpectations(service);
Christopher Wileyc68c8672012-11-20 16:52:21 -08001259 // But otherwise we'll honor the request.
Paul Stewart3c504012013-01-17 17:49:58 -08001260 EXPECT_CALL(*service, IsConnecting()).WillOnce(Return(false));
Christopher Wileyc68c8672012-11-20 16:52:21 -08001261 EXPECT_CALL(*GetSupplicantInterfaceProxy(), Scan(_)).Times(1);
Wade Guthrie5a4e2ef2013-04-30 12:51:39 -07001262 TriggerFullScan();
Christopher Wileyc68c8672012-11-20 16:52:21 -08001263 dispatcher_.DispatchPendingEvents();
1264 Mock::VerifyAndClearExpectations(GetSupplicantInterfaceProxy());
Paul Stewart3c504012013-01-17 17:49:58 -08001265 Mock::VerifyAndClearExpectations(service);
Christopher Wileyc68c8672012-11-20 16:52:21 -08001266}
1267
Wade Guthrie5a4e2ef2013-04-30 12:51:39 -07001268TEST_F(WiFiMainTest, NoScansWhileConnecting) {
1269 StartWiFi();
1270 EXPECT_CALL(*scan_session_, InitiateScan());
1271 dispatcher_.DispatchPendingEvents();
1272 Mock::VerifyAndClearExpectations(GetSupplicantInterfaceProxy());
Ben Chanf024ef42013-09-20 14:21:38 -07001273 MockWiFiServiceRefPtr service = MakeMockService(kSecurityNone);
Wade Guthrie5a4e2ef2013-04-30 12:51:39 -07001274 SetPendingService(service);
1275 // If we're connecting, we ignore scan requests to stay on channel.
1276 EXPECT_CALL(*service, IsConnecting()).WillOnce(Return(true));
1277 EXPECT_CALL(*GetSupplicantInterfaceProxy(), Scan(_)).Times(0);
1278 EXPECT_CALL(*scan_session_, InitiateScan()).Times(0);
1279 TriggerFullScan();
1280 dispatcher_.DispatchPendingEvents();
1281 Mock::VerifyAndClearExpectations(GetSupplicantInterfaceProxy());
1282 Mock::VerifyAndClearExpectations(service);
1283 EXPECT_CALL(*service, IsConnecting()).WillOnce(Return(false));
1284 EXPECT_CALL(*GetSupplicantInterfaceProxy(), Scan(_)).Times(1);
1285 EXPECT_CALL(*scan_session_, InitiateScan()).Times(0);
1286 TriggerFullScan();
1287 dispatcher_.DispatchPendingEvents();
1288 Mock::VerifyAndClearExpectations(GetSupplicantInterfaceProxy());
1289 Mock::VerifyAndClearExpectations(service);
1290 // Similarly, ignore scans when our connected service is reconnecting.
1291 SetPendingService(NULL);
1292 SetCurrentService(service);
1293 EXPECT_CALL(*service, IsConnecting()).WillOnce(Return(true));
1294 EXPECT_CALL(*GetSupplicantInterfaceProxy(), Scan(_)).Times(0);
Wade Guthriedf6d61b2013-07-17 11:43:55 -07001295 EXPECT_TRUE(IsScanSessionNull());
Wade Guthrie5a4e2ef2013-04-30 12:51:39 -07001296 TriggerFullScan();
1297 dispatcher_.DispatchPendingEvents();
1298 Mock::VerifyAndClearExpectations(GetSupplicantInterfaceProxy());
1299 Mock::VerifyAndClearExpectations(service);
1300 // But otherwise we'll honor the request.
1301 EXPECT_CALL(*service, IsConnecting()).WillOnce(Return(false));
1302 EXPECT_CALL(*GetSupplicantInterfaceProxy(), Scan(_)).Times(1);
Wade Guthriedf6d61b2013-07-17 11:43:55 -07001303 EXPECT_TRUE(IsScanSessionNull());
Wade Guthrie5a4e2ef2013-04-30 12:51:39 -07001304 TriggerFullScan();
1305 dispatcher_.DispatchPendingEvents();
1306 Mock::VerifyAndClearExpectations(GetSupplicantInterfaceProxy());
1307 Mock::VerifyAndClearExpectations(service);
1308}
1309
1310TEST_F(WiFiMainTest, ResumeStartsScanWhenIdle_FullScan) {
1311 EnableFullScan();
Paul Stewart3c504012013-01-17 17:49:58 -08001312 EXPECT_CALL(*GetSupplicantInterfaceProxy(), Scan(_));
Gary Morainac1bdb42012-02-16 17:42:29 -08001313 StartWiFi();
1314 dispatcher_.DispatchPendingEvents();
Paul Stewart3c504012013-01-17 17:49:58 -08001315 Mock::VerifyAndClearExpectations(GetSupplicantInterfaceProxy());
mukesh agrawal5c05b292012-03-07 10:12:52 -08001316 ReportScanDone();
Gary Morainac1bdb42012-02-16 17:42:29 -08001317 ASSERT_TRUE(wifi()->IsIdle());
1318 EXPECT_CALL(*GetSupplicantInterfaceProxy(), Scan(_));
mukesh agrawal2f9df4e2012-08-08 12:29:20 -07001319 OnAfterResume();
Gary Morainac1bdb42012-02-16 17:42:29 -08001320 dispatcher_.DispatchPendingEvents();
1321}
1322
Wade Guthrie5a4e2ef2013-04-30 12:51:39 -07001323TEST_F(WiFiMainTest, ResumeStartsScanWhenIdle) {
1324 EXPECT_CALL(*scan_session_, InitiateScan());
1325 StartWiFi();
1326 dispatcher_.DispatchPendingEvents();
1327 Mock::VerifyAndClearExpectations(GetSupplicantInterfaceProxy());
1328 ReportScanDone();
1329 ASSERT_TRUE(wifi()->IsIdle());
1330 dispatcher_.DispatchPendingEvents();
Wade Guthrie5a4e2ef2013-04-30 12:51:39 -07001331 OnAfterResume();
Wade Guthrie0cf3c982013-05-29 09:11:35 -07001332 EXPECT_TRUE(scan_session_ != NULL);
1333 InstallMockScanSession();
1334 EXPECT_CALL(*scan_session_, InitiateScan());
Wade Guthrie5a4e2ef2013-04-30 12:51:39 -07001335 dispatcher_.DispatchPendingEvents();
1336}
1337
1338TEST_F(WiFiMainTest, SuspendDoesNotStartScan_FullScan) {
1339 EnableFullScan();
Paul Stewart3c504012013-01-17 17:49:58 -08001340 EXPECT_CALL(*GetSupplicantInterfaceProxy(), Scan(_));
Gary Morainac1bdb42012-02-16 17:42:29 -08001341 StartWiFi();
1342 dispatcher_.DispatchPendingEvents();
Paul Stewart3c504012013-01-17 17:49:58 -08001343 Mock::VerifyAndClearExpectations(GetSupplicantInterfaceProxy());
Gary Morainac1bdb42012-02-16 17:42:29 -08001344 ASSERT_TRUE(wifi()->IsIdle());
1345 EXPECT_CALL(*GetSupplicantInterfaceProxy(), Scan(_)).Times(0);
mukesh agrawal2f9df4e2012-08-08 12:29:20 -07001346 OnBeforeSuspend();
Gary Morainac1bdb42012-02-16 17:42:29 -08001347 dispatcher_.DispatchPendingEvents();
1348}
1349
Wade Guthrie5a4e2ef2013-04-30 12:51:39 -07001350TEST_F(WiFiMainTest, SuspendDoesNotStartScan) {
1351 EXPECT_CALL(*scan_session_, InitiateScan());
1352 StartWiFi();
1353 dispatcher_.DispatchPendingEvents();
1354 Mock::VerifyAndClearExpectations(GetSupplicantInterfaceProxy());
1355 ASSERT_TRUE(wifi()->IsIdle());
1356 EXPECT_CALL(*GetSupplicantInterfaceProxy(), Scan(_)).Times(0);
1357 EXPECT_CALL(*scan_session_, InitiateScan()).Times(0);
1358 OnBeforeSuspend();
1359 dispatcher_.DispatchPendingEvents();
1360}
1361
1362TEST_F(WiFiMainTest, ResumeDoesNotStartScanWhenNotIdle_FullScan) {
1363 EnableFullScan();
Paul Stewart3c504012013-01-17 17:49:58 -08001364 EXPECT_CALL(*GetSupplicantInterfaceProxy(), Scan(_));
Gary Morainac1bdb42012-02-16 17:42:29 -08001365 StartWiFi();
Gary Morainac1bdb42012-02-16 17:42:29 -08001366 dispatcher_.DispatchPendingEvents();
Paul Stewart3c504012013-01-17 17:49:58 -08001367 Mock::VerifyAndClearExpectations(GetSupplicantInterfaceProxy());
1368 WiFiServiceRefPtr service(SetupConnectedService(DBus::Path(), NULL, NULL));
1369 EXPECT_FALSE(wifi()->IsIdle());
1370 ScopedMockLog log;
mukesh agrawal3ab56ac2012-05-31 14:41:43 -07001371 EXPECT_CALL(log, Log(_, _, _)).Times(AnyNumber());
Wade Guthrie0cf3c982013-05-29 09:11:35 -07001372 EXPECT_CALL(log, Log(_, _, EndsWith("already connecting or connected.")));
Gary Morainac1bdb42012-02-16 17:42:29 -08001373 EXPECT_CALL(*GetSupplicantInterfaceProxy(), Scan(_)).Times(0);
mukesh agrawal2f9df4e2012-08-08 12:29:20 -07001374 OnAfterResume();
Gary Morainac1bdb42012-02-16 17:42:29 -08001375 dispatcher_.DispatchPendingEvents();
1376}
1377
Wade Guthrie5a4e2ef2013-04-30 12:51:39 -07001378TEST_F(WiFiMainTest, ResumeDoesNotStartScanWhenNotIdle) {
1379 EXPECT_CALL(*scan_session_, InitiateScan());
1380 StartWiFi();
1381 dispatcher_.DispatchPendingEvents();
1382 Mock::VerifyAndClearExpectations(GetSupplicantInterfaceProxy());
1383 WiFiServiceRefPtr service(SetupConnectedService(DBus::Path(), NULL, NULL));
1384 EXPECT_FALSE(wifi()->IsIdle());
1385 ScopedMockLog log;
1386 EXPECT_CALL(log, Log(_, _, _)).Times(AnyNumber());
Wade Guthrie0cf3c982013-05-29 09:11:35 -07001387 EXPECT_CALL(log, Log(_, _, EndsWith("already connecting or connected.")));
Wade Guthrie5a4e2ef2013-04-30 12:51:39 -07001388 EXPECT_CALL(*GetSupplicantInterfaceProxy(), Scan(_)).Times(0);
Wade Guthriedf6d61b2013-07-17 11:43:55 -07001389 EXPECT_TRUE(IsScanSessionNull());
Wade Guthrie5a4e2ef2013-04-30 12:51:39 -07001390 OnAfterResume();
1391 dispatcher_.DispatchPendingEvents();
1392}
1393
mukesh agrawal31950242011-07-14 11:53:38 -07001394TEST_F(WiFiMainTest, ScanResults) {
Paul Stewart3c504012013-01-17 17:49:58 -08001395 EXPECT_CALL(*wifi_provider(), OnEndpointAdded(_)).Times(5);
mukesh agrawal31950242011-07-14 11:53:38 -07001396 StartWiFi();
Thieu Lee41a72d2012-02-06 20:46:51 +00001397 ReportBSS("bss0", "ssid0", "00:00:00:00:00:00", 0, 0, kNetworkModeAdHoc);
mukesh agrawal31950242011-07-14 11:53:38 -07001398 ReportBSS(
Thieu Lee41a72d2012-02-06 20:46:51 +00001399 "bss1", "ssid1", "00:00:00:00:00:01", 1, 0, kNetworkModeInfrastructure);
mukesh agrawal31950242011-07-14 11:53:38 -07001400 ReportBSS(
Thieu Lee41a72d2012-02-06 20:46:51 +00001401 "bss2", "ssid2", "00:00:00:00:00:02", 2, 0, kNetworkModeInfrastructure);
mukesh agrawal31950242011-07-14 11:53:38 -07001402 ReportBSS(
Thieu Lee41a72d2012-02-06 20:46:51 +00001403 "bss3", "ssid3", "00:00:00:00:00:03", 3, 0, kNetworkModeInfrastructure);
1404 const uint16 frequency = 2412;
1405 ReportBSS("bss4", "ssid4", "00:00:00:00:00:04", 4, frequency,
1406 kNetworkModeAdHoc);
1407
1408 const WiFi::EndpointMap &endpoints_by_rpcid = GetEndpointMap();
1409 EXPECT_EQ(5, endpoints_by_rpcid.size());
1410
1411 WiFi::EndpointMap::const_iterator i;
1412 WiFiEndpointRefPtr endpoint;
1413 for (i = endpoints_by_rpcid.begin();
1414 i != endpoints_by_rpcid.end();
1415 ++i) {
1416 if (i->second->bssid_string() == "00:00:00:00:00:04")
1417 break;
1418 }
1419 ASSERT_TRUE(i != endpoints_by_rpcid.end());
1420 EXPECT_EQ(4, i->second->signal_strength());
1421 EXPECT_EQ(frequency, i->second->frequency());
1422 EXPECT_EQ("adhoc", i->second->network_mode());
mukesh agrawal31950242011-07-14 11:53:38 -07001423}
1424
mukesh agrawal31950242011-07-14 11:53:38 -07001425TEST_F(WiFiMainTest, ScanCompleted) {
1426 StartWiFi();
Paul Stewart3c504012013-01-17 17:49:58 -08001427 WiFiEndpointRefPtr ap0 = MakeEndpointWithMode("ssid0", "00:00:00:00:00:00",
1428 kNetworkModeAdHoc);
1429 WiFiEndpointRefPtr ap1 = MakeEndpoint("ssid1", "00:00:00:00:00:01");
1430 WiFiEndpointRefPtr ap2 = MakeEndpoint("ssid2", "00:00:00:00:00:02");
1431 EXPECT_CALL(*wifi_provider(), OnEndpointAdded(EndpointMatch(ap0))).Times(1);
1432 EXPECT_CALL(*wifi_provider(), OnEndpointAdded(EndpointMatch(ap1))).Times(1);
1433 EXPECT_CALL(*wifi_provider(), OnEndpointAdded(EndpointMatch(ap2))).Times(1);
1434 ReportBSS("bss0", ap0->ssid_string(), ap0->bssid_string(), 0, 0,
1435 kNetworkModeAdHoc);
1436 ReportBSS("bss1", ap1->ssid_string(), ap1->bssid_string(), 0, 0,
1437 kNetworkModeInfrastructure);
1438 ReportBSS("bss2", ap2->ssid_string(), ap2->bssid_string(), 0, 0,
1439 kNetworkModeInfrastructure);
mukesh agrawal31950242011-07-14 11:53:38 -07001440 ReportScanDone();
Paul Stewart3c504012013-01-17 17:49:58 -08001441 Mock::VerifyAndClearExpectations(wifi_provider());
1442
1443 EXPECT_CALL(*wifi_provider(), OnEndpointAdded(_)).Times(0);
mukesh agrawalb3857612012-01-18 16:23:29 -08001444
mukesh agrawale9adda12012-02-09 18:33:48 -08001445 // BSSes with SSIDs that start with NULL should be filtered.
Thieu Lee41a72d2012-02-06 20:46:51 +00001446 ReportBSS("bss3", string(1, 0), "00:00:00:00:00:03", 3, 0, kNetworkModeAdHoc);
mukesh agrawalb3857612012-01-18 16:23:29 -08001447
mukesh agrawale9adda12012-02-09 18:33:48 -08001448 // BSSes with empty SSIDs should be filtered.
1449 ReportBSS("bss3", string(), "00:00:00:00:00:03", 3, 0, kNetworkModeAdHoc);
mukesh agrawal8a3188d2011-12-01 20:56:44 +00001450}
1451
1452TEST_F(WiFiMainTest, LoneBSSRemovedWhileConnected) {
1453 StartWiFi();
Paul Stewart3c504012013-01-17 17:49:58 -08001454 WiFiEndpointRefPtr endpoint;
1455 DBus::Path bss_path;
1456 WiFiServiceRefPtr service(
1457 SetupConnectedService(DBus::Path(), &endpoint, &bss_path));
1458 scoped_ptr<EndpointRemovalHandler> handler(
1459 MakeEndpointRemovalHandler(service));
1460 EXPECT_CALL(*wifi_provider(), OnEndpointRemoved(EndpointMatch(endpoint)))
1461 .WillOnce(Invoke(handler.get(),
1462 &EndpointRemovalHandler::OnEndpointRemoved));
mukesh agrawal8a3188d2011-12-01 20:56:44 +00001463 EXPECT_CALL(*GetSupplicantInterfaceProxy(), Disconnect());
Paul Stewart3c504012013-01-17 17:49:58 -08001464 RemoveBSS(bss_path);
mukesh agrawal261daca2011-12-02 18:56:56 +00001465}
1466
1467TEST_F(WiFiMainTest, NonSolitaryBSSRemoved) {
1468 StartWiFi();
Paul Stewart3c504012013-01-17 17:49:58 -08001469 WiFiEndpointRefPtr endpoint;
1470 DBus::Path bss_path;
1471 WiFiServiceRefPtr service(
1472 SetupConnectedService(DBus::Path(), &endpoint, &bss_path));
1473 EXPECT_CALL(*wifi_provider(), OnEndpointRemoved(EndpointMatch(endpoint)))
1474 .WillOnce(Return(reinterpret_cast<WiFiService *>(NULL)));
1475 EXPECT_CALL(*GetSupplicantInterfaceProxy(), Disconnect()).Times(0);
1476 RemoveBSS(bss_path);
mukesh agrawal31950242011-07-14 11:53:38 -07001477}
1478
Paul Stewart835934a2012-12-06 19:27:09 -08001479TEST_F(WiFiMainTest, ReconnectPreservesDBusPath) {
Paul Stewart835934a2012-12-06 19:27:09 -08001480 StartWiFi();
Paul Stewart3c504012013-01-17 17:49:58 -08001481 DBus::Path kPath = "/test/path";
1482 WiFiServiceRefPtr service(SetupConnectedService(kPath, NULL, NULL));
Paul Stewart835934a2012-12-06 19:27:09 -08001483
1484 // Return the service to a connectable state.
Paul Stewart3c504012013-01-17 17:49:58 -08001485 EXPECT_CALL(*GetSupplicantInterfaceProxy(), Disconnect());
Paul Stewart835934a2012-12-06 19:27:09 -08001486 InitiateDisconnect(service);
Paul Stewart3c504012013-01-17 17:49:58 -08001487 Mock::VerifyAndClearExpectations(GetSupplicantInterfaceProxy());
1488
1489 // Complete the disconnection by reporting a BSS change.
Paul Stewart0654ece2013-03-26 15:21:26 -07001490 ReportCurrentBSSChanged(WPASupplicant::kCurrentBSSNull);
Paul Stewart835934a2012-12-06 19:27:09 -08001491
1492 // A second connection attempt should remember the DBus path associated
1493 // with this service.
Paul Stewart3c504012013-01-17 17:49:58 -08001494 EXPECT_CALL(*GetSupplicantInterfaceProxy(), AddNetwork(_)).Times(0);
1495 EXPECT_CALL(*GetSupplicantInterfaceProxy(), SelectNetwork(kPath));
Paul Stewart835934a2012-12-06 19:27:09 -08001496 InitiateConnect(service);
1497}
1498
mukesh agrawal0ed0f2e2011-12-05 20:36:17 +00001499TEST_F(WiFiMainTest, DisconnectPendingService) {
mukesh agrawal0ed0f2e2011-12-05 20:36:17 +00001500 StartWiFi();
Paul Stewart3c504012013-01-17 17:49:58 -08001501 MockWiFiServiceRefPtr service(
1502 SetupConnectingService(DBus::Path(), NULL, NULL));
1503 EXPECT_TRUE(GetPendingService() == service.get());
1504 EXPECT_CALL(*GetSupplicantInterfaceProxy(), Disconnect());
1505 EXPECT_CALL(*service, SetState(Service::kStateIdle)).Times(AtLeast(1));
mukesh agrawal0ed0f2e2011-12-05 20:36:17 +00001506 InitiateDisconnect(service);
Paul Stewart3c504012013-01-17 17:49:58 -08001507 Mock::VerifyAndClearExpectations(service.get());
mukesh agrawal0ed0f2e2011-12-05 20:36:17 +00001508 EXPECT_TRUE(GetPendingService() == NULL);
1509}
1510
1511TEST_F(WiFiMainTest, DisconnectPendingServiceWithCurrent) {
mukesh agrawal0ed0f2e2011-12-05 20:36:17 +00001512 StartWiFi();
Paul Stewart3c504012013-01-17 17:49:58 -08001513 MockWiFiServiceRefPtr service0(
1514 SetupConnectedService(DBus::Path(), NULL, NULL));
1515 EXPECT_EQ(service0, GetCurrentService());
1516 EXPECT_EQ(NULL, GetPendingService().get());
mukesh agrawal0ed0f2e2011-12-05 20:36:17 +00001517
Paul Stewart3c504012013-01-17 17:49:58 -08001518 // We don't explicitly call Disconnect() while transitioning to a new
1519 // service. Instead, we use the side-effect of SelectNetwork (verified in
1520 // SetupConnectingService).
1521 EXPECT_CALL(*GetSupplicantInterfaceProxy(), Disconnect()).Times(0);
1522 MockWiFiServiceRefPtr service1(
1523 SetupConnectingService("/new/path", NULL, NULL));
1524 Mock::VerifyAndClearExpectations(GetSupplicantInterfaceProxy());
mukesh agrawal0ed0f2e2011-12-05 20:36:17 +00001525
1526 EXPECT_EQ(service0, GetCurrentService());
1527 EXPECT_EQ(service1, GetPendingService());
Paul Stewart3c504012013-01-17 17:49:58 -08001528 EXPECT_CALL(*service1, SetState(Service::kStateIdle)).Times(AtLeast(1));
1529 EXPECT_CALL(*GetSupplicantInterfaceProxy(), Disconnect());
mukesh agrawal0ed0f2e2011-12-05 20:36:17 +00001530 InitiateDisconnect(service1);
Paul Stewart3c504012013-01-17 17:49:58 -08001531 Mock::VerifyAndClearExpectations(service1.get());
mukesh agrawal0ed0f2e2011-12-05 20:36:17 +00001532
1533 // |current_service_| will be unchanged until supplicant signals
1534 // that CurrentBSS has changed.
1535 EXPECT_EQ(service0, GetCurrentService());
1536 // |pending_service_| is updated immediately.
Paul Stewart3c504012013-01-17 17:49:58 -08001537 EXPECT_EQ(NULL, GetPendingService().get());
Paul Stewart2b05e622012-07-13 20:38:44 -07001538 EXPECT_TRUE(GetPendingTimeout().IsCancelled());
mukesh agrawal0ed0f2e2011-12-05 20:36:17 +00001539}
1540
1541TEST_F(WiFiMainTest, DisconnectCurrentService) {
Paul Stewart3c504012013-01-17 17:49:58 -08001542 StartWiFi();
1543 ::DBus::Path kPath("/fake/path");
1544 MockWiFiServiceRefPtr service(SetupConnectedService(kPath, NULL, NULL));
1545 EXPECT_CALL(*GetSupplicantInterfaceProxy(), Disconnect());
mukesh agrawal0ed0f2e2011-12-05 20:36:17 +00001546 InitiateDisconnect(service);
1547
1548 // |current_service_| should not change until supplicant reports
1549 // a BSS change.
1550 EXPECT_EQ(service, GetCurrentService());
Paul Stewart835934a2012-12-06 19:27:09 -08001551
1552 // Expect that the entry associated with this network will be disabled.
1553 MockSupplicantNetworkProxy *network_proxy = CreateSupplicantNetworkProxy();
1554 EXPECT_CALL(*proxy_factory(), CreateSupplicantNetworkProxy(
Paul Stewart0654ece2013-03-26 15:21:26 -07001555 kPath, WPASupplicant::kDBusAddr))
Paul Stewart835934a2012-12-06 19:27:09 -08001556 .WillOnce(Return(network_proxy));
1557 EXPECT_CALL(*network_proxy, SetEnabled(false));
Paul Stewart735eab52013-03-29 09:19:23 -07001558 EXPECT_CALL(*eap_state_handler_, Reset());
Paul Stewart3c504012013-01-17 17:49:58 -08001559 EXPECT_CALL(*GetSupplicantInterfaceProxy(), RemoveNetwork(kPath)).Times(0);
Paul Stewart0654ece2013-03-26 15:21:26 -07001560 ReportCurrentBSSChanged(WPASupplicant::kCurrentBSSNull);
Paul Stewart3c504012013-01-17 17:49:58 -08001561 EXPECT_EQ(NULL, GetCurrentService().get());
1562 Mock::VerifyAndClearExpectations(GetSupplicantInterfaceProxy());
mukesh agrawal0ed0f2e2011-12-05 20:36:17 +00001563}
1564
Christopher Wileyc6184482012-10-24 15:31:56 -07001565TEST_F(WiFiMainTest, DisconnectCurrentServiceWithErrors) {
Paul Stewart3c504012013-01-17 17:49:58 -08001566 StartWiFi();
1567 DBus::Path kPath("/fake/path");
1568 WiFiServiceRefPtr service(SetupConnectedService(kPath, NULL, NULL));
1569 EXPECT_CALL(*GetSupplicantInterfaceProxy(), Disconnect())
Christopher Wileyc6184482012-10-24 15:31:56 -07001570 .WillOnce(InvokeWithoutArgs(this, (&WiFiMainTest::ThrowDBusError)));
Paul Stewart3c504012013-01-17 17:49:58 -08001571 EXPECT_CALL(*GetSupplicantInterfaceProxy(), RemoveNetwork(kPath)).Times(1);
Christopher Wileyc6184482012-10-24 15:31:56 -07001572 InitiateDisconnect(service);
1573
1574 // We may sometimes fail to disconnect via supplicant, and we patch up some
1575 // state when this happens.
1576 EXPECT_EQ(NULL, GetCurrentService().get());
1577 EXPECT_EQ(NULL, wifi()->selected_service().get());
1578}
1579
mukesh agrawal0ed0f2e2011-12-05 20:36:17 +00001580TEST_F(WiFiMainTest, DisconnectCurrentServiceWithPending) {
mukesh agrawal0ed0f2e2011-12-05 20:36:17 +00001581 StartWiFi();
Paul Stewart3c504012013-01-17 17:49:58 -08001582 WiFiServiceRefPtr service0(SetupConnectedService(DBus::Path(), NULL, NULL));
1583 WiFiServiceRefPtr service1(SetupConnectingService(DBus::Path(), NULL, NULL));
mukesh agrawal0ed0f2e2011-12-05 20:36:17 +00001584 EXPECT_EQ(service0, GetCurrentService());
1585 EXPECT_EQ(service1, GetPendingService());
Paul Stewart3c504012013-01-17 17:49:58 -08001586 EXPECT_CALL(*GetSupplicantInterfaceProxy(), Disconnect()).Times(0);
mukesh agrawal0ed0f2e2011-12-05 20:36:17 +00001587 InitiateDisconnect(service0);
1588
1589 EXPECT_EQ(service0, GetCurrentService());
1590 EXPECT_EQ(service1, GetPendingService());
Paul Stewart2b05e622012-07-13 20:38:44 -07001591 EXPECT_FALSE(GetPendingTimeout().IsCancelled());
1592}
1593
Paul Stewart17d90652013-04-04 15:09:11 -07001594TEST_F(WiFiMainTest, TimeoutPendingServiceWithEndpoints) {
Wade Guthrie9f4aa152013-07-29 16:03:14 -07001595 StartScan(WiFi::kScanMethodProgressive);
Paul Stewart2b05e622012-07-13 20:38:44 -07001596 const base::CancelableClosure &pending_timeout = GetPendingTimeout();
1597 EXPECT_TRUE(pending_timeout.IsCancelled());
Wade Guthrie9f4aa152013-07-29 16:03:14 -07001598 MockWiFiServiceRefPtr service = AttemptConnection(
1599 WiFi::kScanMethodProgressive, nullptr, nullptr);
Wade Guthrie44f290d2013-05-28 10:16:25 -07001600
1601 // Timeout the connection attempt.
Paul Stewart2b05e622012-07-13 20:38:44 -07001602 EXPECT_FALSE(pending_timeout.IsCancelled());
1603 EXPECT_EQ(service, GetPendingService());
Paul Stewart17d90652013-04-04 15:09:11 -07001604 // Simulate a service with a wifi_ reference calling DisconnectFrom().
1605 EXPECT_CALL(*service, DisconnectWithFailure(Service::kFailureOutOfRange, _))
1606 .WillOnce(InvokeWithoutArgs(this, &WiFiObjectTest::ResetPendingService));
1607 EXPECT_CALL(*service, HasEndpoints()).Times(0);
1608 // DisconnectFrom() should not be called directly from WiFi.
1609 EXPECT_CALL(*service, SetState(Service::kStateIdle)).Times(0);
1610 EXPECT_CALL(*GetSupplicantInterfaceProxy(), Disconnect()).Times(0);
Wade Guthrie0cf3c982013-05-29 09:11:35 -07001611
Wade Guthrie44f290d2013-05-28 10:16:25 -07001612 // Innocuous redundant call to NotifyDeviceScanFinished.
Wade Guthriea5995cf2013-07-30 09:31:16 -07001613 ExpectFoundNothing();
Wade Guthrie44f290d2013-05-28 10:16:25 -07001614 EXPECT_CALL(*metrics(), NotifyDeviceConnectFinished(_)).Times(0);
Wade Guthriea5995cf2013-07-30 09:31:16 -07001615 NiceScopedMockLog log;
1616 ScopeLogger::GetInstance()->EnableScopesByName("wifi");
1617 ScopeLogger::GetInstance()->set_verbose_level(10);
Wade Guthrie0cf3c982013-05-29 09:11:35 -07001618 EXPECT_CALL(log, Log(_, _, _)).Times(AnyNumber());
1619 EXPECT_CALL(log, Log(_, _,
1620 HasSubstr("-> PROGRESSIVE_FINISHED_NOCONNECTION")));
Paul Stewart3c504012013-01-17 17:49:58 -08001621 pending_timeout.callback().Run();
Wade Guthrie0cf3c982013-05-29 09:11:35 -07001622 VerifyScanState(WiFi::kScanIdle, WiFi::kScanMethodNone);
Paul Stewart17d90652013-04-04 15:09:11 -07001623 Mock::VerifyAndClearExpectations(service);
Wade Guthrie0cf3c982013-05-29 09:11:35 -07001624
1625 ScopeLogger::GetInstance()->set_verbose_level(0);
1626 ScopeLogger::GetInstance()->EnableScopesByName("-wifi");
Paul Stewart17d90652013-04-04 15:09:11 -07001627}
1628
1629TEST_F(WiFiMainTest, TimeoutPendingServiceWithoutEndpoints) {
1630 StartWiFi();
1631 const base::CancelableClosure &pending_timeout = GetPendingTimeout();
1632 EXPECT_TRUE(pending_timeout.IsCancelled());
1633 MockWiFiServiceRefPtr service(
1634 SetupConnectingService(DBus::Path(), NULL, NULL));
1635 EXPECT_FALSE(pending_timeout.IsCancelled());
1636 EXPECT_EQ(service, GetPendingService());
1637 // We expect the service to get a disconnect call, but in this scenario
1638 // the service does nothing.
1639 EXPECT_CALL(*service, DisconnectWithFailure(Service::kFailureOutOfRange, _));
1640 EXPECT_CALL(*service, HasEndpoints()).WillOnce(Return(false));
1641 // DisconnectFrom() should be called directly from WiFi.
1642 EXPECT_CALL(*service, SetState(Service::kStateIdle)).Times(AtLeast(1));
1643 EXPECT_CALL(*GetSupplicantInterfaceProxy(), Disconnect());
1644 pending_timeout.callback().Run();
1645 EXPECT_EQ(NULL, GetPendingService().get());
mukesh agrawal0ed0f2e2011-12-05 20:36:17 +00001646}
1647
1648TEST_F(WiFiMainTest, DisconnectInvalidService) {
mukesh agrawal0ed0f2e2011-12-05 20:36:17 +00001649 StartWiFi();
Paul Stewart3c504012013-01-17 17:49:58 -08001650 MockWiFiServiceRefPtr service;
1651 MakeNewEndpointAndService(0, 0, kNetworkModeAdHoc, NULL, &service);
1652 EXPECT_CALL(*GetSupplicantInterfaceProxy(), Disconnect()).Times(0);
mukesh agrawal0ed0f2e2011-12-05 20:36:17 +00001653 InitiateDisconnect(service);
1654}
1655
1656TEST_F(WiFiMainTest, DisconnectCurrentServiceFailure) {
Paul Stewart3c504012013-01-17 17:49:58 -08001657 StartWiFi();
1658 DBus::Path kPath("/fake/path");
1659 WiFiServiceRefPtr service(SetupConnectedService(kPath, NULL, NULL));
1660 EXPECT_CALL(*GetSupplicantInterfaceProxy(), Disconnect())
mukesh agrawal0ed0f2e2011-12-05 20:36:17 +00001661 .WillRepeatedly(Throw(
1662 DBus::Error(
1663 "fi.w1.wpa_supplicant1.NotConnected",
1664 "test threw fi.w1.wpa_supplicant1.NotConnected")));
Paul Stewart3c504012013-01-17 17:49:58 -08001665 EXPECT_CALL(*GetSupplicantInterfaceProxy(), RemoveNetwork(kPath));
mukesh agrawal0ed0f2e2011-12-05 20:36:17 +00001666 InitiateDisconnect(service);
Paul Stewart3c504012013-01-17 17:49:58 -08001667 EXPECT_EQ(NULL, GetCurrentService().get());
mukesh agrawal0ed0f2e2011-12-05 20:36:17 +00001668}
1669
mukesh agrawal5c4dd0b2011-09-14 13:53:14 -07001670TEST_F(WiFiMainTest, Stop) {
mukesh agrawalc01f3982012-01-24 13:48:39 -08001671 StartWiFi();
Paul Stewart3c504012013-01-17 17:49:58 -08001672 WiFiEndpointRefPtr endpoint0;
1673 ::DBus::Path kPath("/fake/path");
1674 WiFiServiceRefPtr service0(SetupConnectedService(kPath, &endpoint0, NULL));
1675 WiFiEndpointRefPtr endpoint1;
1676 MakeNewEndpointAndService(0, 0, kNetworkModeAdHoc, &endpoint1, NULL);
mukesh agrawal5c4dd0b2011-09-14 13:53:14 -07001677
Paul Stewart3c504012013-01-17 17:49:58 -08001678 EXPECT_CALL(*wifi_provider(), OnEndpointRemoved(EndpointMatch(endpoint0)))
1679 .WillOnce(Return(reinterpret_cast<WiFiService *>(NULL)));
1680 EXPECT_CALL(*wifi_provider(), OnEndpointRemoved(EndpointMatch(endpoint1)))
1681 .WillOnce(Return(reinterpret_cast<WiFiService *>(NULL)));
1682 EXPECT_CALL(*GetSupplicantInterfaceProxy(), RemoveNetwork(kPath)).Times(1);
mukesh agrawalc01f3982012-01-24 13:48:39 -08001683 StopWiFi();
mukesh agrawalb66c6462012-05-07 11:45:25 -07001684 EXPECT_TRUE(GetScanTimer().IsCancelled());
mukesh agrawalc4f368f2012-06-04 19:45:52 -07001685 EXPECT_FALSE(wifi()->weak_ptr_factory_.HasWeakPtrs());
mukesh agrawal5c4dd0b2011-09-14 13:53:14 -07001686}
1687
Paul Stewart549d44c2012-07-03 12:40:25 -07001688TEST_F(WiFiMainTest, StopWhileConnected) {
Paul Stewart3c504012013-01-17 17:49:58 -08001689 StartWiFi();
1690 WiFiEndpointRefPtr endpoint;
1691 WiFiServiceRefPtr service(
1692 SetupConnectedService(DBus::Path(), &endpoint, NULL));
1693 scoped_ptr<EndpointRemovalHandler> handler(
1694 MakeEndpointRemovalHandler(service));
1695 EXPECT_CALL(*wifi_provider(), OnEndpointRemoved(EndpointMatch(endpoint)))
1696 .WillOnce(Invoke(handler.get(),
1697 &EndpointRemovalHandler::OnEndpointRemoved));
1698 EXPECT_CALL(*GetSupplicantInterfaceProxy(), Disconnect());
Paul Stewart549d44c2012-07-03 12:40:25 -07001699 StopWiFi();
1700 EXPECT_TRUE(GetCurrentService() == NULL);
1701}
1702
Paul Stewart44663922012-07-30 11:03:03 -07001703TEST_F(WiFiMainTest, ReconnectTimer) {
Paul Stewart3c504012013-01-17 17:49:58 -08001704 StartWiFi();
1705 MockWiFiServiceRefPtr service(
1706 SetupConnectedService(DBus::Path(), NULL, NULL));
1707 EXPECT_CALL(*service, IsConnected()).WillRepeatedly(Return(true));
Paul Stewart1aff7302012-08-04 20:04:47 -07001708 EXPECT_TRUE(GetReconnectTimeoutCallback().IsCancelled());
Paul Stewart0654ece2013-03-26 15:21:26 -07001709 ReportStateChanged(WPASupplicant::kInterfaceStateDisconnected);
Paul Stewart1aff7302012-08-04 20:04:47 -07001710 EXPECT_FALSE(GetReconnectTimeoutCallback().IsCancelled());
Paul Stewart0654ece2013-03-26 15:21:26 -07001711 ReportStateChanged(WPASupplicant::kInterfaceStateCompleted);
Paul Stewart1aff7302012-08-04 20:04:47 -07001712 EXPECT_TRUE(GetReconnectTimeoutCallback().IsCancelled());
Paul Stewart0654ece2013-03-26 15:21:26 -07001713 ReportStateChanged(WPASupplicant::kInterfaceStateDisconnected);
Paul Stewart1aff7302012-08-04 20:04:47 -07001714 EXPECT_FALSE(GetReconnectTimeoutCallback().IsCancelled());
Paul Stewart44663922012-07-30 11:03:03 -07001715 ReportCurrentBSSChanged(kBSSName);
Paul Stewart1aff7302012-08-04 20:04:47 -07001716 EXPECT_TRUE(GetReconnectTimeoutCallback().IsCancelled());
Paul Stewart0654ece2013-03-26 15:21:26 -07001717 ReportStateChanged(WPASupplicant::kInterfaceStateDisconnected);
Paul Stewart1aff7302012-08-04 20:04:47 -07001718 EXPECT_FALSE(GetReconnectTimeoutCallback().IsCancelled());
Paul Stewart3c504012013-01-17 17:49:58 -08001719 EXPECT_CALL(*GetSupplicantInterfaceProxy(), Disconnect());
Paul Stewart1aff7302012-08-04 20:04:47 -07001720 GetReconnectTimeoutCallback().callback().Run();
Paul Stewart3c504012013-01-17 17:49:58 -08001721 Mock::VerifyAndClearExpectations(GetSupplicantInterfaceProxy());
Paul Stewart1aff7302012-08-04 20:04:47 -07001722 EXPECT_TRUE(GetReconnectTimeoutCallback().IsCancelled());
Paul Stewart44663922012-07-30 11:03:03 -07001723}
1724
Paul Stewartcb59fed2012-03-21 21:14:46 -07001725
Wade Guthrie5a4e2ef2013-04-30 12:51:39 -07001726MATCHER_P(HasHiddenSSID_FullScan, ssid, "") {
Paul Stewarta41e38d2011-11-11 07:47:29 -08001727 map<string, DBus::Variant>::const_iterator it =
Paul Stewart0654ece2013-03-26 15:21:26 -07001728 arg.find(WPASupplicant::kPropertyScanSSIDs);
Paul Stewartced6a0b2011-11-08 15:32:04 -08001729 if (it == arg.end()) {
1730 return false;
1731 }
1732
1733 const DBus::Variant &ssids_variant = it->second;
1734 EXPECT_TRUE(DBusAdaptor::IsByteArrays(ssids_variant.signature()));
1735 const ByteArrays &ssids = it->second.operator ByteArrays();
1736 // A valid Scan containing a single hidden SSID should contain
1737 // two SSID entries: one containing the SSID we are looking for,
1738 // and an empty entry, signifying that we also want to do a
1739 // broadcast probe request for all non-hidden APs as well.
Paul Stewart3c504012013-01-17 17:49:58 -08001740 return ssids.size() == 2 && ssids[0] == ssid && ssids[1].empty();
1741}
1742
Wade Guthrie5a4e2ef2013-04-30 12:51:39 -07001743MATCHER(HasNoHiddenSSID_FullScan, "") {
Paul Stewart3c504012013-01-17 17:49:58 -08001744 map<string, DBus::Variant>::const_iterator it =
Paul Stewart0654ece2013-03-26 15:21:26 -07001745 arg.find(WPASupplicant::kPropertyScanSSIDs);
Paul Stewart3c504012013-01-17 17:49:58 -08001746 return it == arg.end();
Paul Stewartced6a0b2011-11-08 15:32:04 -08001747}
1748
Wade Guthrie5a4e2ef2013-04-30 12:51:39 -07001749TEST_F(WiFiMainTest, ScanHidden_FullScan) {
1750 EnableFullScan();
Paul Stewart3c504012013-01-17 17:49:58 -08001751 vector<uint8_t>kSSID(1, 'a');
1752 ByteArrays ssids;
1753 ssids.push_back(kSSID);
1754
Paul Stewartced6a0b2011-11-08 15:32:04 -08001755 StartWiFi();
Paul Stewart3c504012013-01-17 17:49:58 -08001756 EXPECT_CALL(*wifi_provider(), GetHiddenSSIDList()).WillOnce(Return(ssids));
Wade Guthrie5a4e2ef2013-04-30 12:51:39 -07001757 EXPECT_CALL(*GetSupplicantInterfaceProxy(),
1758 Scan(HasHiddenSSID_FullScan(kSSID)));
Paul Stewart3c504012013-01-17 17:49:58 -08001759 dispatcher_.DispatchPendingEvents();
1760}
1761
Wade Guthrie5a4e2ef2013-04-30 12:51:39 -07001762// This test is slightly different from the test in scan_session_unittest.cc
1763// because this tests the piece of WiFi that builds the SSID list.
1764TEST_F(WiFiMainTest, ScanHidden) {
1765 // Clear the Mock ScanSession because hidden SSIDs are added when wifi
1766 // instantiates a new ScanSession (and it won't instantiate a new ScanSession
1767 // if there's already one there).
1768 ClearScanSession();
1769 vector<uint8_t>kSSID(1, 'a');
1770 ByteArrays ssids;
1771 ssids.push_back(kSSID);
1772
Wade Guthrie5a4e2ef2013-04-30 12:51:39 -07001773 EXPECT_CALL(*wifi_provider(), GetHiddenSSIDList()).WillOnce(Return(ssids));
1774 StartWiFi();
1775 EXPECT_CALL(netlink_manager_,
Wade Guthrie7347bf22013-04-30 11:21:51 -07001776 SendNl80211Message(HasHiddenSSID(kNl80211FamilyId), _, _));
Wade Guthrie5a4e2ef2013-04-30 12:51:39 -07001777 dispatcher_.DispatchPendingEvents();
1778}
1779
1780TEST_F(WiFiMainTest, ScanNoHidden_FullScan) {
1781 EnableFullScan();
Paul Stewart3c504012013-01-17 17:49:58 -08001782 StartWiFi();
1783 EXPECT_CALL(*wifi_provider(), GetHiddenSSIDList())
1784 .WillOnce(Return(ByteArrays()));
Wade Guthrie5a4e2ef2013-04-30 12:51:39 -07001785 EXPECT_CALL(*GetSupplicantInterfaceProxy(), Scan(HasNoHiddenSSID_FullScan()));
1786 dispatcher_.DispatchPendingEvents();
1787}
1788
1789// This test is slightly different from the test in scan_session_unittest.cc
1790// because this tests the piece of WiFi that builds the SSID list.
1791TEST_F(WiFiMainTest, ScanNoHidden) {
1792 // Clear the Mock ScanSession because hidden SSIDs are added when wifi
1793 // instantiates a new ScanSession (and it won't instantiate a new ScanSession
1794 // if there's already one there).
1795 ClearScanSession();
Wade Guthrie5a4e2ef2013-04-30 12:51:39 -07001796 EXPECT_CALL(*wifi_provider(), GetHiddenSSIDList())
1797 .WillOnce(Return(ByteArrays()));
1798 StartWiFi();
1799 EXPECT_CALL(netlink_manager_,
Wade Guthrie7347bf22013-04-30 11:21:51 -07001800 SendNl80211Message(HasNoHiddenSSID(kNl80211FamilyId), _, _));
Paul Stewartced6a0b2011-11-08 15:32:04 -08001801 dispatcher_.DispatchPendingEvents();
1802}
1803
Paul Stewartfae4dae2012-09-13 07:43:32 -07001804TEST_F(WiFiMainTest, ScanWiFiDisabledAfterResume) {
1805 ScopedMockLog log;
1806 EXPECT_CALL(log, Log(_, _, _)).Times(AnyNumber());
1807 EXPECT_CALL(log, Log(_, _, EndsWith(
1808 "Ignoring scan request while device is not enabled."))).Times(1);
Paul Stewart3c504012013-01-17 17:49:58 -08001809 EXPECT_CALL(*GetSupplicantInterfaceProxy(), Scan(_)).Times(0);
Wade Guthrie5a4e2ef2013-04-30 12:51:39 -07001810 EXPECT_CALL(*scan_session_, InitiateScan()).Times(0);
Paul Stewartfae4dae2012-09-13 07:43:32 -07001811 StartWiFi();
1812 StopWiFi();
1813 // A scan is queued when WiFi resumes.
1814 OnAfterResume();
1815 dispatcher_.DispatchPendingEvents();
1816}
1817
Wade Guthrie5a4e2ef2013-04-30 12:51:39 -07001818TEST_F(WiFiMainTest, ProgressiveScanFound) {
1819 // Set min & max scan frequency count to 1 so each scan will be of a single
1820 // frequency.
1821 SetScanSize(1, 1);
1822
1823 // Do the first scan (finds nothing).
Wade Guthrie9f4aa152013-07-29 16:03:14 -07001824 StartScan(WiFi::kScanMethodProgressive);
Paul Stewart7de7e022013-08-28 09:42:50 -07001825 EXPECT_CALL(*manager(), OnDeviceGeolocationInfoUpdated(_)).Times(0);
Wade Guthrie5a4e2ef2013-04-30 12:51:39 -07001826 ReportScanDoneKeepScanSession();
1827
1828 // Do the second scan (connects afterwards).
Wade Guthriea5995cf2013-07-30 09:31:16 -07001829 ExpectScanStart(kNotFirstProgressiveScanBatch, WiFi::kScanMethodProgressive);
Wade Guthrie5a4e2ef2013-04-30 12:51:39 -07001830 dispatcher_.DispatchPendingEvents();
Wade Guthrie0cf3c982013-05-29 09:11:35 -07001831 VerifyScanState(WiFi::kScanScanning, WiFi::kScanMethodProgressive);
Wade Guthrie5a4e2ef2013-04-30 12:51:39 -07001832 ReportScanDoneKeepScanSession();
1833
1834 // Connect after second scan.
Ben Chanf024ef42013-09-20 14:21:38 -07001835 MockWiFiServiceRefPtr service = MakeMockService(kSecurityNone);
Wade Guthriedf6d61b2013-07-17 11:43:55 -07001836 EXPECT_CALL(*metrics(), NotifyDeviceScanFinished(_));
1837 EXPECT_CALL(*scan_session_, InitiateScan()).Times(0);
1838 EXPECT_CALL(*GetSupplicantInterfaceProxy(), Scan(_)).Times(0);
Ben Chanf024ef42013-09-20 14:21:38 -07001839 EXPECT_CALL(*adaptor_, EmitBoolChanged(kScanningProperty, false));
Wade Guthrie5a4e2ef2013-04-30 12:51:39 -07001840 SetPendingService(service);
1841
1842 // Verify that the third scan aborts and there is no further scan.
1843 ScopedMockLog log;
1844 EXPECT_CALL(log, Log(_, _, _)).Times(AnyNumber());
1845 EXPECT_CALL(log, Log(_, _, EndsWith(
1846 "Ignoring scan request while connecting to an AP."))).Times(1);
Wade Guthrie5a4e2ef2013-04-30 12:51:39 -07001847 dispatcher_.DispatchPendingEvents();
Wade Guthrie0cf3c982013-05-29 09:11:35 -07001848 VerifyScanState(WiFi::kScanConnecting, WiFi::kScanMethodProgressive);
Wade Guthrie5a4e2ef2013-04-30 12:51:39 -07001849}
1850
1851TEST_F(WiFiMainTest, ProgressiveScanNotFound) {
1852 // Set min & max scan frequency count to 1 so each scan will be of a single
1853 // frequency.
1854 SetScanSize(1, 1);
1855
Wade Guthrie44f290d2013-05-28 10:16:25 -07001856 // This test never connects
1857 EXPECT_CALL(*metrics(), NotifyDeviceConnectStarted(_, _)).Times(0);
1858 EXPECT_CALL(*metrics(), NotifyDeviceConnectFinished(_)).Times(0);
1859
Wade Guthrie5a4e2ef2013-04-30 12:51:39 -07001860 // Do the first scan (finds nothing).
Wade Guthrie9f4aa152013-07-29 16:03:14 -07001861 StartScan(WiFi::kScanMethodProgressive);
Wade Guthrie5a4e2ef2013-04-30 12:51:39 -07001862 ReportScanDoneKeepScanSession();
1863
1864 // Do the second scan (finds nothing).
Wade Guthriea5995cf2013-07-30 09:31:16 -07001865 ExpectScanStart(kNotFirstProgressiveScanBatch, WiFi::kScanMethodProgressive);
Paul Stewart7de7e022013-08-28 09:42:50 -07001866 EXPECT_CALL(*manager(), OnDeviceGeolocationInfoUpdated(_)).Times(0);
Wade Guthrie5a4e2ef2013-04-30 12:51:39 -07001867 dispatcher_.DispatchPendingEvents();
Wade Guthrie0cf3c982013-05-29 09:11:35 -07001868 VerifyScanState(WiFi::kScanScanning, WiFi::kScanMethodProgressive);
Wade Guthrie5a4e2ef2013-04-30 12:51:39 -07001869 ReportScanDoneKeepScanSession();
1870
1871 // Do the third scan. After (simulated) exhausting of search frequencies,
1872 // verify that this scan uses supplicant rather than internal (progressive)
1873 // scan.
1874 EXPECT_CALL(*scan_session_, HasMoreFrequencies()).WillOnce(Return(false));
1875 EXPECT_CALL(*scan_session_, InitiateScan()).Times(0);
1876 EXPECT_CALL(*GetSupplicantInterfaceProxy(), Scan(_));
1877 dispatcher_.DispatchPendingEvents();
Wade Guthrie0cf3c982013-05-29 09:11:35 -07001878 VerifyScanState(WiFi::kScanScanning,
1879 WiFi::kScanMethodProgressiveFinishedToFull);
Wade Guthrie5a4e2ef2013-04-30 12:51:39 -07001880
1881 // And verify that ScanDone reports a complete scan (i.e., the
1882 // wifi_::scan_session_ has truly been cleared).
Wade Guthriea5995cf2013-07-30 09:31:16 -07001883 ExpectScanStop();
1884 ExpectFoundNothing();
Wade Guthrie5a4e2ef2013-04-30 12:51:39 -07001885 ReportScanDoneKeepScanSession();
Wade Guthrie0cf3c982013-05-29 09:11:35 -07001886 dispatcher_.DispatchPendingEvents(); // Launch UpdateScanStateAfterScanDone
1887 VerifyScanState(WiFi::kScanIdle, WiFi::kScanMethodNone);
Wade Guthrie5a4e2ef2013-04-30 12:51:39 -07001888}
1889
1890TEST_F(WiFiMainTest, ProgressiveScanError) {
Wade Guthrie0cf3c982013-05-29 09:11:35 -07001891 VerifyScanState(WiFi::kScanIdle, WiFi::kScanMethodNone);
1892 ClearScanSession(); // Clear Mock ScanSession to get an actual ScanSession.
Wade Guthrie5a4e2ef2013-04-30 12:51:39 -07001893 StartWiFi();
1894
1895 // This should call |Scan| which posts |ScanTask|
1896 NewScanResultsMessage not_supposed_to_get_this_message;
1897 OnTriggerScanResponse(not_supposed_to_get_this_message);
Wade Guthrie0cf3c982013-05-29 09:11:35 -07001898 VerifyScanState(WiFi::kScanScanning, WiFi::kScanMethodProgressiveErrorToFull);
Wade Guthrie5a4e2ef2013-04-30 12:51:39 -07001899
1900 EXPECT_TRUE(IsScanSessionNull());
1901 EXPECT_CALL(*GetSupplicantInterfaceProxy(), Scan(_)).Times(1);
1902 dispatcher_.DispatchPendingEvents();
Wade Guthrie0cf3c982013-05-29 09:11:35 -07001903
1904 ReportScanDoneKeepScanSession();
Paul Stewart7de7e022013-08-28 09:42:50 -07001905 EXPECT_CALL(*manager(), OnDeviceGeolocationInfoUpdated(_));
Wade Guthrie0cf3c982013-05-29 09:11:35 -07001906 dispatcher_.DispatchPendingEvents(); // Launch UpdateScanStateAfterScanDone
1907 VerifyScanState(WiFi::kScanIdle, WiFi::kScanMethodNone);
Wade Guthrie5a4e2ef2013-04-30 12:51:39 -07001908}
1909
mukesh agrawal7ec71312011-11-10 02:08:26 +00001910TEST_F(WiFiMainTest, InitialSupplicantState) {
1911 EXPECT_EQ(WiFi::kInterfaceStateUnknown, GetSupplicantState());
1912}
1913
1914TEST_F(WiFiMainTest, StateChangeNoService) {
1915 // State change should succeed even if there is no pending Service.
Paul Stewart0654ece2013-03-26 15:21:26 -07001916 ReportStateChanged(WPASupplicant::kInterfaceStateScanning);
1917 EXPECT_EQ(WPASupplicant::kInterfaceStateScanning, GetSupplicantState());
mukesh agrawal7ec71312011-11-10 02:08:26 +00001918}
1919
1920TEST_F(WiFiMainTest, StateChangeWithService) {
1921 // Forward transition should trigger a Service state change.
1922 StartWiFi();
1923 dispatcher_.DispatchPendingEvents();
Ben Chanf024ef42013-09-20 14:21:38 -07001924 MockWiFiServiceRefPtr service = MakeMockService(kSecurityNone);
mukesh agrawal7ec71312011-11-10 02:08:26 +00001925 InitiateConnect(service);
1926 EXPECT_CALL(*service.get(), SetState(Service::kStateAssociating));
Paul Stewart0654ece2013-03-26 15:21:26 -07001927 ReportStateChanged(WPASupplicant::kInterfaceStateAssociated);
mukesh agrawal7ec71312011-11-10 02:08:26 +00001928 // Verify expectations now, because WiFi may report other state changes
1929 // when WiFi is Stop()-ed (during TearDown()).
1930 Mock::VerifyAndClearExpectations(service.get());
Gary Moraind9f893f2012-02-06 10:03:40 -08001931 EXPECT_CALL(*service.get(), SetState(_)).Times(AnyNumber());
mukesh agrawal7ec71312011-11-10 02:08:26 +00001932}
1933
1934TEST_F(WiFiMainTest, StateChangeBackwardsWithService) {
1935 // Some backwards transitions should not trigger a Service state change.
1936 // Supplicant state should still be updated, however.
Paul Stewartd408fdf2012-05-07 17:15:57 -07001937 EXPECT_CALL(*dhcp_provider(), CreateConfig(_, _, _, _)).Times(AnyNumber());
Gary Moraind9f893f2012-02-06 10:03:40 -08001938 EXPECT_CALL(*dhcp_config_.get(), RequestIP()).Times(AnyNumber());
mukesh agrawal7ec71312011-11-10 02:08:26 +00001939 StartWiFi();
1940 dispatcher_.DispatchPendingEvents();
Ben Chanf024ef42013-09-20 14:21:38 -07001941 MockWiFiServiceRefPtr service = MakeMockService(kSecurityNone);
Paul Stewartbca08f82013-07-09 16:32:37 -07001942 EXPECT_CALL(*service, SetState(Service::kStateAssociating));
1943 EXPECT_CALL(*service, SetState(Service::kStateConfiguring));
1944 EXPECT_CALL(*service, ResetSuspectedCredentialFailures());
mukesh agrawal7ec71312011-11-10 02:08:26 +00001945 InitiateConnect(service);
Paul Stewart0654ece2013-03-26 15:21:26 -07001946 ReportStateChanged(WPASupplicant::kInterfaceStateCompleted);
1947 ReportStateChanged(WPASupplicant::kInterfaceStateAuthenticating);
1948 EXPECT_EQ(WPASupplicant::kInterfaceStateAuthenticating,
mukesh agrawal7ec71312011-11-10 02:08:26 +00001949 GetSupplicantState());
1950 // Verify expectations now, because WiFi may report other state changes
1951 // when WiFi is Stop()-ed (during TearDown()).
Paul Stewart3c504012013-01-17 17:49:58 -08001952 Mock::VerifyAndClearExpectations(service);
1953 EXPECT_CALL(*service, SetState(_)).Times(AnyNumber());
mukesh agrawal7ec71312011-11-10 02:08:26 +00001954}
1955
Paul Stewarta47c3c62012-12-18 12:14:29 -08001956TEST_F(WiFiMainTest, ConnectToServiceWithoutRecentIssues) {
1957 MockSupplicantProcessProxy *process_proxy = supplicant_process_proxy_.get();
1958 StartWiFi();
1959 dispatcher_.DispatchPendingEvents();
Ben Chanf024ef42013-09-20 14:21:38 -07001960 MockWiFiServiceRefPtr service = MakeMockService(kSecurityNone);
Paul Stewarta47c3c62012-12-18 12:14:29 -08001961 EXPECT_CALL(*process_proxy, GetDebugLevel()).Times(0);
1962 EXPECT_CALL(*process_proxy, SetDebugLevel(_)).Times(0);
1963 EXPECT_CALL(*service.get(), HasRecentConnectionIssues())
1964 .WillOnce(Return(false));
1965 InitiateConnect(service);
1966}
1967
1968TEST_F(WiFiMainTest, ConnectToServiceWithRecentIssues) {
1969 // Turn of WiFi debugging, so the only reason we will turn on supplicant
1970 // debugging will be to debug a problematic connection.
1971 ScopeLogger::GetInstance()->EnableScopesByName("-wifi");
1972
1973 MockSupplicantProcessProxy *process_proxy = supplicant_process_proxy_.get();
1974 StartWiFi();
1975 dispatcher_.DispatchPendingEvents();
Ben Chanf024ef42013-09-20 14:21:38 -07001976 MockWiFiServiceRefPtr service = MakeMockService(kSecurityNone);
Paul Stewarta47c3c62012-12-18 12:14:29 -08001977 EXPECT_CALL(*process_proxy, GetDebugLevel())
Paul Stewart0654ece2013-03-26 15:21:26 -07001978 .WillOnce(Return(WPASupplicant::kDebugLevelInfo));
1979 EXPECT_CALL(*process_proxy, SetDebugLevel(WPASupplicant::kDebugLevelDebug))
Paul Stewarta47c3c62012-12-18 12:14:29 -08001980 .Times(1);
1981 EXPECT_CALL(*service.get(), HasRecentConnectionIssues())
1982 .WillOnce(Return(true));
1983 InitiateConnect(service);
1984 Mock::VerifyAndClearExpectations(process_proxy);
1985
1986 SetPendingService(NULL);
1987 SetCurrentService(service);
1988
1989 // When we disconnect from the troubled service, we should reduce the
1990 // level of supplciant debugging.
1991 EXPECT_CALL(*process_proxy, GetDebugLevel())
Paul Stewart0654ece2013-03-26 15:21:26 -07001992 .WillOnce(Return(WPASupplicant::kDebugLevelDebug));
1993 EXPECT_CALL(*process_proxy, SetDebugLevel(WPASupplicant::kDebugLevelInfo))
Paul Stewarta47c3c62012-12-18 12:14:29 -08001994 .Times(1);
Paul Stewart0654ece2013-03-26 15:21:26 -07001995 ReportCurrentBSSChanged(WPASupplicant::kCurrentBSSNull);
Paul Stewarta47c3c62012-12-18 12:14:29 -08001996}
1997
mukesh agrawal15908392011-11-16 18:29:25 +00001998TEST_F(WiFiMainTest, CurrentBSSChangeConnectedToDisconnected) {
mukesh agrawal15908392011-11-16 18:29:25 +00001999 StartWiFi();
Paul Stewart3c504012013-01-17 17:49:58 -08002000 WiFiEndpointRefPtr endpoint;
2001 MockWiFiServiceRefPtr service =
2002 SetupConnectedService(DBus::Path(), &endpoint, NULL);
mukesh agrawal15908392011-11-16 18:29:25 +00002003
Paul Stewart3c504012013-01-17 17:49:58 -08002004 EXPECT_CALL(*service, SetState(Service::kStateIdle));
2005 EXPECT_CALL(*service, SetFailureSilent(Service::kFailureUnknown));
Paul Stewart0654ece2013-03-26 15:21:26 -07002006 ReportCurrentBSSChanged(WPASupplicant::kCurrentBSSNull);
mukesh agrawal15908392011-11-16 18:29:25 +00002007 EXPECT_EQ(NULL, GetCurrentService().get());
2008 EXPECT_EQ(NULL, GetPendingService().get());
2009}
2010
2011TEST_F(WiFiMainTest, CurrentBSSChangeConnectedToConnectedNewService) {
mukesh agrawal15908392011-11-16 18:29:25 +00002012 StartWiFi();
Paul Stewart3c504012013-01-17 17:49:58 -08002013 MockWiFiServiceRefPtr service0 =
2014 SetupConnectedService(DBus::Path(), NULL, NULL);
2015 MockWiFiServiceRefPtr service1;
2016 ::DBus::Path bss_path1(MakeNewEndpointAndService(
2017 0, 0, kNetworkModeAdHoc, NULL, &service1));
2018 EXPECT_EQ(service0.get(), GetCurrentService().get());
mukesh agrawal15908392011-11-16 18:29:25 +00002019
mukesh agrawalc01f3982012-01-24 13:48:39 -08002020 // Note that we deliberately omit intermediate supplicant states
2021 // (e.g. kInterfaceStateAssociating), on the theory that they are
2022 // unreliable. Specifically, they may be quashed if the association
2023 // completes before supplicant flushes its changed properties.
Paul Stewart3c504012013-01-17 17:49:58 -08002024 EXPECT_CALL(*service0, SetState(Service::kStateIdle)).Times(AtLeast(1));
2025 ReportCurrentBSSChanged(bss_path1);
2026 EXPECT_CALL(*service1, SetState(Service::kStateConfiguring));
Paul Stewartbca08f82013-07-09 16:32:37 -07002027 EXPECT_CALL(*service1, ResetSuspectedCredentialFailures());
mukesh agrawala5dda0e2013-08-16 11:53:10 -07002028 EXPECT_CALL(*wifi_provider(), IncrementConnectCount(_));
Paul Stewart0654ece2013-03-26 15:21:26 -07002029 ReportStateChanged(WPASupplicant::kInterfaceStateCompleted);
Paul Stewart3c504012013-01-17 17:49:58 -08002030 EXPECT_EQ(service1.get(), GetCurrentService().get());
2031 Mock::VerifyAndClearExpectations(service0);
2032 Mock::VerifyAndClearExpectations(service1);
mukesh agrawal15908392011-11-16 18:29:25 +00002033}
2034
Thieu Lee41a72d2012-02-06 20:46:51 +00002035TEST_F(WiFiMainTest, CurrentBSSChangedUpdateServiceEndpoint) {
Thieu Lee41a72d2012-02-06 20:46:51 +00002036 StartWiFi();
Wade Guthrie0cf3c982013-05-29 09:11:35 -07002037 dispatcher_.DispatchPendingEvents();
2038 VerifyScanState(WiFi::kScanScanning, WiFi::kScanMethodProgressive);
2039
Paul Stewart3c504012013-01-17 17:49:58 -08002040 MockWiFiServiceRefPtr service =
2041 SetupConnectedService(DBus::Path(), NULL, NULL);
2042 WiFiEndpointRefPtr endpoint;
2043 ::DBus::Path bss_path =
2044 AddEndpointToService(service, 0, 0, kNetworkModeAdHoc, &endpoint);
2045 EXPECT_CALL(*service, NotifyCurrentEndpoint(EndpointMatch(endpoint)));
2046 ReportCurrentBSSChanged(bss_path);
Wade Guthrie0cf3c982013-05-29 09:11:35 -07002047 VerifyScanState(WiFi::kScanIdle, WiFi::kScanMethodNone);
mukesh agrawal4eb4d782011-12-05 17:34:37 +00002048}
2049
mukesh agrawal8a3188d2011-12-01 20:56:44 +00002050TEST_F(WiFiMainTest, NewConnectPreemptsPending) {
mukesh agrawal8a3188d2011-12-01 20:56:44 +00002051 StartWiFi();
Paul Stewart3c504012013-01-17 17:49:58 -08002052 MockWiFiServiceRefPtr service0(
2053 SetupConnectingService(DBus::Path(), NULL, NULL));
2054 EXPECT_EQ(service0.get(), GetPendingService().get());
mukesh agrawal8a3188d2011-12-01 20:56:44 +00002055 EXPECT_CALL(*GetSupplicantInterfaceProxy(), Disconnect());
Paul Stewart3c504012013-01-17 17:49:58 -08002056 MockWiFiServiceRefPtr service1(
2057 SetupConnectingService(DBus::Path(), NULL, NULL));
2058 EXPECT_EQ(service1.get(), GetPendingService().get());
2059 EXPECT_EQ(NULL, GetCurrentService().get());
mukesh agrawal8a3188d2011-12-01 20:56:44 +00002060}
2061
2062TEST_F(WiFiMainTest, IsIdle) {
2063 StartWiFi();
2064 EXPECT_TRUE(wifi()->IsIdle());
Paul Stewart3c504012013-01-17 17:49:58 -08002065 MockWiFiServiceRefPtr service(
2066 SetupConnectingService(DBus::Path(), NULL, NULL));
mukesh agrawal8a3188d2011-12-01 20:56:44 +00002067 EXPECT_FALSE(wifi()->IsIdle());
2068}
2069
Darin Petkov4a66cc52012-06-15 10:08:29 +02002070MATCHER_P(WiFiAddedArgs, bgscan, "") {
Paul Stewart0654ece2013-03-26 15:21:26 -07002071 return ContainsKey(arg, WPASupplicant::kNetworkPropertyScanSSID) &&
2072 ContainsKey(arg, WPASupplicant::kNetworkPropertyBgscan) == bgscan;
mukesh agrawal4d0401c2012-01-06 16:05:31 -08002073}
2074
2075TEST_F(WiFiMainTest, AddNetworkArgs) {
mukesh agrawal4d0401c2012-01-06 16:05:31 -08002076 StartWiFi();
Paul Stewart3c504012013-01-17 17:49:58 -08002077 MockWiFiServiceRefPtr service;
2078 MakeNewEndpointAndService(0, 0, kNetworkModeAdHoc, NULL, &service);
2079 EXPECT_CALL(*GetSupplicantInterfaceProxy(), AddNetwork(WiFiAddedArgs(true)));
Paul Stewart0654ece2013-03-26 15:21:26 -07002080 EXPECT_TRUE(SetBgscanMethod(WPASupplicant::kNetworkBgscanMethodSimple));
mukesh agrawal4d0401c2012-01-06 16:05:31 -08002081 InitiateConnect(service);
2082}
2083
Darin Petkov4a66cc52012-06-15 10:08:29 +02002084TEST_F(WiFiMainTest, AddNetworkArgsNoBgscan) {
Darin Petkov4a66cc52012-06-15 10:08:29 +02002085 StartWiFi();
Paul Stewart3c504012013-01-17 17:49:58 -08002086 MockWiFiServiceRefPtr service;
2087 MakeNewEndpointAndService(0, 0, kNetworkModeAdHoc, NULL, &service);
2088 EXPECT_CALL(*GetSupplicantInterfaceProxy(), AddNetwork(WiFiAddedArgs(false)));
Darin Petkov4a66cc52012-06-15 10:08:29 +02002089 InitiateConnect(service);
2090}
2091
2092TEST_F(WiFiMainTest, AppendBgscan) {
2093 StartWiFi();
Ben Chanf024ef42013-09-20 14:21:38 -07002094 MockWiFiServiceRefPtr service = MakeMockService(kSecurityNone);
Darin Petkov4a66cc52012-06-15 10:08:29 +02002095 {
2096 // 1 endpoint, default bgscan method -- background scan disabled.
2097 std::map<std::string, DBus::Variant> params;
Paul Stewart3c504012013-01-17 17:49:58 -08002098 EXPECT_CALL(*service, GetEndpointCount()).WillOnce(Return(1));
2099 AppendBgscan(service, &params);
2100 Mock::VerifyAndClearExpectations(service);
Paul Stewart0654ece2013-03-26 15:21:26 -07002101 EXPECT_FALSE(ContainsKey(params, WPASupplicant::kNetworkPropertyBgscan));
Darin Petkov4a66cc52012-06-15 10:08:29 +02002102 }
Darin Petkov4a66cc52012-06-15 10:08:29 +02002103 {
2104 // 2 endpoints, default bgscan method -- background scan frequency reduced.
2105 map<string, DBus::Variant> params;
Paul Stewart3c504012013-01-17 17:49:58 -08002106 EXPECT_CALL(*service, GetEndpointCount()).WillOnce(Return(2));
2107 AppendBgscan(service, &params);
2108 Mock::VerifyAndClearExpectations(service);
Darin Petkov4a66cc52012-06-15 10:08:29 +02002109 string config_string;
2110 EXPECT_TRUE(
2111 DBusProperties::GetString(params,
Paul Stewart0654ece2013-03-26 15:21:26 -07002112 WPASupplicant::kNetworkPropertyBgscan,
Darin Petkov4a66cc52012-06-15 10:08:29 +02002113 &config_string));
2114 vector<string> elements;
2115 base::SplitString(config_string, ':', &elements);
2116 ASSERT_EQ(4, elements.size());
2117 EXPECT_EQ(WiFi::kDefaultBgscanMethod, elements[0]);
2118 EXPECT_EQ(StringPrintf("%d", WiFi::kBackgroundScanIntervalSeconds),
2119 elements[3]);
2120 }
2121 {
2122 // Explicit bgscan method -- regular background scan frequency.
Paul Stewart0654ece2013-03-26 15:21:26 -07002123 EXPECT_TRUE(SetBgscanMethod(WPASupplicant::kNetworkBgscanMethodSimple));
Darin Petkov4a66cc52012-06-15 10:08:29 +02002124 std::map<std::string, DBus::Variant> params;
Paul Stewart3c504012013-01-17 17:49:58 -08002125 EXPECT_CALL(*service, GetEndpointCount()).Times(0);
2126 AppendBgscan(service, &params);
2127 Mock::VerifyAndClearExpectations(service);
Darin Petkov4a66cc52012-06-15 10:08:29 +02002128 string config_string;
2129 EXPECT_TRUE(
2130 DBusProperties::GetString(params,
Paul Stewart0654ece2013-03-26 15:21:26 -07002131 WPASupplicant::kNetworkPropertyBgscan,
Darin Petkov4a66cc52012-06-15 10:08:29 +02002132 &config_string));
2133 vector<string> elements;
2134 base::SplitString(config_string, ':', &elements);
2135 ASSERT_EQ(4, elements.size());
2136 EXPECT_EQ(StringPrintf("%d", WiFi::kDefaultScanIntervalSeconds),
2137 elements[3]);
2138 }
Christopher Wiley5c929e22012-07-16 14:06:04 -07002139 {
2140 // No scan method, simply returns without appending properties
Paul Stewart0654ece2013-03-26 15:21:26 -07002141 EXPECT_TRUE(SetBgscanMethod(WPASupplicant::kNetworkBgscanMethodNone));
Christopher Wiley5c929e22012-07-16 14:06:04 -07002142 std::map<std::string, DBus::Variant> params;
Paul Stewart3c504012013-01-17 17:49:58 -08002143 EXPECT_CALL(*service, GetEndpointCount()).Times(0);
Christopher Wiley5c929e22012-07-16 14:06:04 -07002144 AppendBgscan(service.get(), &params);
Paul Stewart3c504012013-01-17 17:49:58 -08002145 Mock::VerifyAndClearExpectations(service);
Christopher Wiley5c929e22012-07-16 14:06:04 -07002146 string config_string;
2147 EXPECT_FALSE(
2148 DBusProperties::GetString(params,
Paul Stewart0654ece2013-03-26 15:21:26 -07002149 WPASupplicant::kNetworkPropertyBgscan,
Christopher Wiley5c929e22012-07-16 14:06:04 -07002150 &config_string));
2151 }
Darin Petkov4a66cc52012-06-15 10:08:29 +02002152}
2153
mukesh agrawalc01f3982012-01-24 13:48:39 -08002154TEST_F(WiFiMainTest, StateAndIPIgnoreLinkEvent) {
2155 StartWiFi();
Paul Stewart3c504012013-01-17 17:49:58 -08002156 MockWiFiServiceRefPtr service(
2157 SetupConnectingService(DBus::Path(), NULL, NULL));
mukesh agrawalc01f3982012-01-24 13:48:39 -08002158 EXPECT_CALL(*service.get(), SetState(_)).Times(0);
2159 EXPECT_CALL(*dhcp_config_.get(), RequestIP()).Times(0);
2160 ReportLinkUp();
2161
2162 // Verify expectations now, because WiFi may cause |service| state
2163 // changes during TearDown().
2164 Mock::VerifyAndClearExpectations(service);
mukesh agrawalc01f3982012-01-24 13:48:39 -08002165}
2166
2167TEST_F(WiFiMainTest, SupplicantCompletedAlreadyConnected) {
mukesh agrawalc01f3982012-01-24 13:48:39 -08002168 StartWiFi();
Paul Stewart3c504012013-01-17 17:49:58 -08002169 MockWiFiServiceRefPtr service(
2170 SetupConnectedService(DBus::Path(), NULL, NULL));
Christopher Wiley8f81e2a2012-10-17 16:51:32 -07002171 Mock::VerifyAndClearExpectations(dhcp_config_.get());
mukesh agrawalc01f3982012-01-24 13:48:39 -08002172 EXPECT_CALL(*dhcp_config_.get(), RequestIP()).Times(0);
Christopher Wiley8f81e2a2012-10-17 16:51:32 -07002173 // Simulate a rekeying event from the AP. These show as transitions from
2174 // completed->completed from wpa_supplicant.
Paul Stewart0654ece2013-03-26 15:21:26 -07002175 ReportStateChanged(WPASupplicant::kInterfaceStateCompleted);
Christopher Wiley5519e9e2013-01-08 16:55:56 -08002176 // When we get an IP, WiFi should enable high bitrates on the interface again.
Paul Stewart3c504012013-01-17 17:49:58 -08002177 Mock::VerifyAndClearExpectations(GetSupplicantInterfaceProxy());
2178 EXPECT_CALL(*GetSupplicantInterfaceProxy(), EnableHighBitrates()).Times(1);
2179 EXPECT_CALL(*manager(), device_info()).WillOnce(Return(device_info()));
Prathmesh Prabhuba99b592013-04-17 15:13:14 -07002180 string portal_url;
2181 EXPECT_CALL(*manager(), GetPortalCheckURL()).WillOnce(ReturnRef(portal_url));
Christopher Wiley8f81e2a2012-10-17 16:51:32 -07002182 ReportIPConfigComplete();
2183 // Similarly, rekeying events after we have an IP don't trigger L3
Christopher Wiley5519e9e2013-01-08 16:55:56 -08002184 // configuration. However, we treat all transitions to completed as potential
2185 // reassociations, so we will reenable high rates again here.
Paul Stewart3c504012013-01-17 17:49:58 -08002186 Mock::VerifyAndClearExpectations(GetSupplicantInterfaceProxy());
2187 EXPECT_CALL(*service, IsConnected()).WillOnce(Return(true));
2188 EXPECT_CALL(*GetSupplicantInterfaceProxy(), EnableHighBitrates()).Times(1);
Paul Stewart0654ece2013-03-26 15:21:26 -07002189 ReportStateChanged(WPASupplicant::kInterfaceStateCompleted);
mukesh agrawalc01f3982012-01-24 13:48:39 -08002190}
2191
mukesh agrawalb20776f2012-02-10 16:00:36 -08002192TEST_F(WiFiMainTest, BSSAddedCreatesBSSProxy) {
mukesh agrawalb20776f2012-02-10 16:00:36 -08002193 // TODO(quiche): Consider using a factory for WiFiEndpoints, so that
2194 // we can test the interaction between WiFi and WiFiEndpoint. (Right
2195 // now, we're testing across multiple layers.)
2196 EXPECT_CALL(*supplicant_bss_proxy_, Die()).Times(AnyNumber());
2197 EXPECT_CALL(*proxy_factory(), CreateSupplicantBSSProxy(_, _, _));
2198 StartWiFi();
2199 ReportBSS("bss0", "ssid0", "00:00:00:00:00:00", 0, 0, kNetworkModeAdHoc);
2200}
2201
2202TEST_F(WiFiMainTest, BSSRemovedDestroysBSSProxy) {
2203 // TODO(quiche): As for BSSAddedCreatesBSSProxy, consider using a
2204 // factory for WiFiEndpoints.
mukesh agrawalb20776f2012-02-10 16:00:36 -08002205 // Get the pointer before we transfer ownership.
2206 MockSupplicantBSSProxy *proxy = supplicant_bss_proxy_.get();
2207 EXPECT_CALL(*proxy, Die());
2208 StartWiFi();
Paul Stewart3c504012013-01-17 17:49:58 -08002209 DBus::Path bss_path(
2210 MakeNewEndpointAndService(0, 0, kNetworkModeAdHoc, NULL, NULL));
2211 EXPECT_CALL(*wifi_provider(), OnEndpointRemoved(_))
2212 .WillOnce(Return(reinterpret_cast<WiFiService *>(NULL)));
2213 RemoveBSS(bss_path);
mukesh agrawalb20776f2012-02-10 16:00:36 -08002214 // Check this now, to make sure RemoveBSS killed the proxy (rather
2215 // than TearDown).
2216 Mock::VerifyAndClearExpectations(proxy);
2217}
2218
mukesh agrawal5c05b292012-03-07 10:12:52 -08002219TEST_F(WiFiMainTest, FlushBSSOnResume) {
2220 const struct timeval resume_time = {1, 0};
2221 const struct timeval scan_done_time = {6, 0};
mukesh agrawal5c05b292012-03-07 10:12:52 -08002222
2223 StartWiFi();
2224
2225 EXPECT_CALL(time_, GetTimeMonotonic(_))
2226 .WillOnce(DoAll(SetArgumentPointee<0>(resume_time), Return(0)))
2227 .WillOnce(DoAll(SetArgumentPointee<0>(scan_done_time), Return(0)));
Paul Stewart3c504012013-01-17 17:49:58 -08002228 EXPECT_CALL(*GetSupplicantInterfaceProxy(),
mukesh agrawal5c05b292012-03-07 10:12:52 -08002229 FlushBSS(WiFi::kMaxBSSResumeAgeSeconds + 5));
mukesh agrawal2f9df4e2012-08-08 12:29:20 -07002230 OnAfterResume();
mukesh agrawal5c05b292012-03-07 10:12:52 -08002231 ReportScanDone();
2232}
2233
Wade Guthrie5a4e2ef2013-04-30 12:51:39 -07002234TEST_F(WiFiMainTest, ScanTimerIdle_FullScan) {
2235 EnableFullScan();
mukesh agrawalb66c6462012-05-07 11:45:25 -07002236 StartWiFi();
2237 dispatcher_.DispatchPendingEvents();
2238 ReportScanDone();
2239 CancelScanTimer();
2240 EXPECT_TRUE(GetScanTimer().IsCancelled());
2241
Paul Stewart7de7e022013-08-28 09:42:50 -07002242 EXPECT_CALL(*manager(), OnDeviceGeolocationInfoUpdated(_));
Wade Guthrie0cf3c982013-05-29 09:11:35 -07002243 dispatcher_.DispatchPendingEvents();
mukesh agrawalb66c6462012-05-07 11:45:25 -07002244 EXPECT_CALL(*GetSupplicantInterfaceProxy(), Scan(_));
2245 FireScanTimer();
2246 dispatcher_.DispatchPendingEvents();
2247 EXPECT_FALSE(GetScanTimer().IsCancelled()); // Automatically re-armed.
2248}
2249
Wade Guthrie5a4e2ef2013-04-30 12:51:39 -07002250TEST_F(WiFiMainTest, ScanTimerIdle) {
2251 StartWiFi();
2252 dispatcher_.DispatchPendingEvents();
2253 ReportScanDone();
2254 CancelScanTimer();
2255 EXPECT_TRUE(GetScanTimer().IsCancelled());
Wade Guthrie0cf3c982013-05-29 09:11:35 -07002256 dispatcher_.DispatchPendingEvents();
Wade Guthriedf6d61b2013-07-17 11:43:55 -07002257 InstallMockScanSession();
Wade Guthrie5a4e2ef2013-04-30 12:51:39 -07002258 EXPECT_CALL(*scan_session_, InitiateScan());
2259 FireScanTimer();
2260 dispatcher_.DispatchPendingEvents();
2261 EXPECT_FALSE(GetScanTimer().IsCancelled()); // Automatically re-armed.
2262}
2263
mukesh agrawalb66c6462012-05-07 11:45:25 -07002264TEST_F(WiFiMainTest, ScanTimerScanning) {
2265 StartWiFi();
2266 dispatcher_.DispatchPendingEvents();
2267 CancelScanTimer();
2268 EXPECT_TRUE(GetScanTimer().IsCancelled());
2269
2270 // Should not call Scan, since we're already scanning.
2271 // (Scanning is triggered by StartWiFi.)
2272 EXPECT_CALL(*GetSupplicantInterfaceProxy(), Scan(_)).Times(0);
Wade Guthrie5a4e2ef2013-04-30 12:51:39 -07002273 EXPECT_CALL(*scan_session_, InitiateScan()).Times(0);
mukesh agrawalb66c6462012-05-07 11:45:25 -07002274 FireScanTimer();
2275 dispatcher_.DispatchPendingEvents();
2276 EXPECT_FALSE(GetScanTimer().IsCancelled()); // Automatically re-armed.
2277}
2278
2279TEST_F(WiFiMainTest, ScanTimerConnecting) {
2280 StartWiFi();
2281 dispatcher_.DispatchPendingEvents();
Paul Stewart3c504012013-01-17 17:49:58 -08002282 MockWiFiServiceRefPtr service =
2283 SetupConnectingService(DBus::Path(), NULL, NULL);
mukesh agrawalb66c6462012-05-07 11:45:25 -07002284 CancelScanTimer();
2285 EXPECT_TRUE(GetScanTimer().IsCancelled());
2286
2287 EXPECT_CALL(*GetSupplicantInterfaceProxy(), Scan(_)).Times(0);
Wade Guthrie5a4e2ef2013-04-30 12:51:39 -07002288 EXPECT_CALL(*scan_session_, InitiateScan()).Times(0);
mukesh agrawalb66c6462012-05-07 11:45:25 -07002289 FireScanTimer();
2290 dispatcher_.DispatchPendingEvents();
2291 EXPECT_FALSE(GetScanTimer().IsCancelled()); // Automatically re-armed.
2292}
2293
2294TEST_F(WiFiMainTest, ScanTimerReconfigured) {
2295 StartWiFi();
2296 CancelScanTimer();
2297 EXPECT_TRUE(GetScanTimer().IsCancelled());
2298
mukesh agrawalbebf1b82013-04-23 15:06:33 -07002299 SetScanInterval(1, NULL);
mukesh agrawalb66c6462012-05-07 11:45:25 -07002300 EXPECT_FALSE(GetScanTimer().IsCancelled());
2301}
2302
2303TEST_F(WiFiMainTest, ScanTimerResetOnScanDone) {
2304 StartWiFi();
2305 CancelScanTimer();
2306 EXPECT_TRUE(GetScanTimer().IsCancelled());
2307
2308 ReportScanDone();
2309 EXPECT_FALSE(GetScanTimer().IsCancelled());
2310}
2311
2312TEST_F(WiFiMainTest, ScanTimerStopOnZeroInterval) {
2313 StartWiFi();
2314 EXPECT_FALSE(GetScanTimer().IsCancelled());
2315
mukesh agrawalbebf1b82013-04-23 15:06:33 -07002316 SetScanInterval(0, NULL);
mukesh agrawalb66c6462012-05-07 11:45:25 -07002317 EXPECT_TRUE(GetScanTimer().IsCancelled());
2318}
2319
Wade Guthrie5a4e2ef2013-04-30 12:51:39 -07002320TEST_F(WiFiMainTest, ScanOnDisconnectWithHidden_FullScan) {
2321 EnableFullScan();
mukesh agrawalb66c6462012-05-07 11:45:25 -07002322 StartWiFi();
mukesh agrawalb66c6462012-05-07 11:45:25 -07002323 dispatcher_.DispatchPendingEvents();
Paul Stewart3c504012013-01-17 17:49:58 -08002324 SetupConnectedService(DBus::Path(), NULL, NULL);
2325 vector<uint8_t>kSSID(1, 'a');
2326 ByteArrays ssids;
2327 ssids.push_back(kSSID);
Wade Guthriea5995cf2013-07-30 09:31:16 -07002328 ExpectScanIdle();
Paul Stewart3c504012013-01-17 17:49:58 -08002329 EXPECT_CALL(*wifi_provider(), GetHiddenSSIDList())
2330 .WillRepeatedly(Return(ssids));
Wade Guthrie5a4e2ef2013-04-30 12:51:39 -07002331 EXPECT_CALL(*GetSupplicantInterfaceProxy(),
2332 Scan(HasHiddenSSID_FullScan(kSSID)));
2333 ReportCurrentBSSChanged(WPASupplicant::kCurrentBSSNull);
2334 dispatcher_.DispatchPendingEvents();
2335}
2336
2337TEST_F(WiFiMainTest, ScanOnDisconnectWithHidden) {
2338 StartWiFi();
2339 dispatcher_.DispatchPendingEvents();
2340 ReportScanDone();
2341 SetupConnectedService(DBus::Path(), NULL, NULL);
Wade Guthriedf6d61b2013-07-17 11:43:55 -07002342 InstallMockScanSession();
Wade Guthrie5a4e2ef2013-04-30 12:51:39 -07002343 vector<uint8_t>kSSID(1, 'a');
2344 ByteArrays ssids;
2345 ssids.push_back(kSSID);
Wade Guthriea5995cf2013-07-30 09:31:16 -07002346 ExpectScanIdle();
Wade Guthrie5a4e2ef2013-04-30 12:51:39 -07002347 EXPECT_CALL(*wifi_provider(), GetHiddenSSIDList())
2348 .WillRepeatedly(Return(ssids));
2349 EXPECT_CALL(*scan_session_, InitiateScan());
Paul Stewart0654ece2013-03-26 15:21:26 -07002350 ReportCurrentBSSChanged(WPASupplicant::kCurrentBSSNull);
mukesh agrawalb66c6462012-05-07 11:45:25 -07002351 dispatcher_.DispatchPendingEvents();
2352}
2353
2354TEST_F(WiFiMainTest, NoScanOnDisconnectWithoutHidden) {
2355 StartWiFi();
mukesh agrawalb66c6462012-05-07 11:45:25 -07002356 dispatcher_.DispatchPendingEvents();
Paul Stewart3c504012013-01-17 17:49:58 -08002357 SetupConnectedService(DBus::Path(), NULL, NULL);
mukesh agrawalb66c6462012-05-07 11:45:25 -07002358 EXPECT_CALL(*GetSupplicantInterfaceProxy(), Scan(_)).Times(0);
Wade Guthriedf6d61b2013-07-17 11:43:55 -07002359 EXPECT_TRUE(IsScanSessionNull());
Paul Stewart3c504012013-01-17 17:49:58 -08002360 EXPECT_CALL(*wifi_provider(), GetHiddenSSIDList())
2361 .WillRepeatedly(Return(ByteArrays()));
Paul Stewart0654ece2013-03-26 15:21:26 -07002362 ReportCurrentBSSChanged(WPASupplicant::kCurrentBSSNull);
mukesh agrawalb66c6462012-05-07 11:45:25 -07002363 dispatcher_.DispatchPendingEvents();
2364}
2365
Paul Stewart3c508e12012-08-09 11:40:06 -07002366TEST_F(WiFiMainTest, LinkMonitorFailure) {
2367 StartWiFi();
2368 ScopedMockLog log;
2369 EXPECT_CALL(log, Log(_, _, _)).Times(AnyNumber());
2370 MockLinkMonitor *link_monitor = new StrictMock<MockLinkMonitor>();
2371 SetLinkMonitor(link_monitor);
2372 EXPECT_CALL(*link_monitor, IsGatewayFound())
2373 .WillOnce(Return(false))
2374 .WillRepeatedly(Return(true));
2375 EXPECT_CALL(log, Log(logging::LOG_INFO, _,
2376 EndsWith("gateway was never found."))).Times(1);
2377 EXPECT_CALL(*GetSupplicantInterfaceProxy(), Reassociate()).Times(0);
2378 OnLinkMonitorFailure();
2379 EXPECT_CALL(log, Log(logging::LOG_INFO, _,
2380 EndsWith("Called Reassociate()."))).Times(1);
2381 EXPECT_CALL(*GetSupplicantInterfaceProxy(), Reassociate()).Times(1);
2382 OnLinkMonitorFailure();
2383 OnSupplicantVanish();
Paul Stewart3c504012013-01-17 17:49:58 -08002384 Mock::VerifyAndClearExpectations(GetSupplicantInterfaceProxy());
2385 EXPECT_CALL(*GetSupplicantInterfaceProxy(), Reassociate()).Times(0);
Paul Stewart3c508e12012-08-09 11:40:06 -07002386 EXPECT_CALL(log, Log(logging::LOG_ERROR, _,
2387 EndsWith("Cannot reassociate."))).Times(1);
2388 OnLinkMonitorFailure();
2389}
2390
mukesh agrawalcf24a242012-05-21 16:46:11 -07002391TEST_F(WiFiMainTest, SuspectCredentialsOpen) {
Ben Chanf024ef42013-09-20 14:21:38 -07002392 MockWiFiServiceRefPtr service = MakeMockService(kSecurityNone);
Paul Stewartbca08f82013-07-09 16:32:37 -07002393 EXPECT_CALL(*service, AddSuspectedCredentialFailure()).Times(0);
2394 EXPECT_FALSE(SuspectCredentials(service, NULL));
mukesh agrawalcf24a242012-05-21 16:46:11 -07002395}
2396
Paul Stewartbca08f82013-07-09 16:32:37 -07002397TEST_F(WiFiMainTest, SuspectCredentialsWPA) {
Ben Chanf024ef42013-09-20 14:21:38 -07002398 MockWiFiServiceRefPtr service = MakeMockService(kSecurityWpa);
Paul Stewart0654ece2013-03-26 15:21:26 -07002399 ReportStateChanged(WPASupplicant::kInterfaceState4WayHandshake);
Paul Stewartbca08f82013-07-09 16:32:37 -07002400 EXPECT_CALL(*service, AddSuspectedCredentialFailure())
2401 .WillOnce(Return(false))
2402 .WillOnce(Return(true));
2403 EXPECT_FALSE(SuspectCredentials(service, NULL));
Paul Stewart1369c2b2013-01-11 05:41:26 -08002404 Service::ConnectFailure failure;
Paul Stewartbca08f82013-07-09 16:32:37 -07002405 EXPECT_TRUE(SuspectCredentials(service, &failure));
Paul Stewart1369c2b2013-01-11 05:41:26 -08002406 EXPECT_EQ(Service::kFailureBadPassphrase, failure);
mukesh agrawalcf24a242012-05-21 16:46:11 -07002407}
2408
Paul Stewartf6f96482013-07-12 12:49:15 -07002409TEST_F(WiFiMainTest, SuspectCredentialsWEP) {
2410 StartWiFi();
2411 dispatcher_.DispatchPendingEvents();
Ben Chanf024ef42013-09-20 14:21:38 -07002412 MockWiFiServiceRefPtr service = MakeMockService(kSecurityWep);
Wade Guthriea5995cf2013-07-30 09:31:16 -07002413 ExpectConnecting();
Paul Stewartf6f96482013-07-12 12:49:15 -07002414 InitiateConnect(service);
2415 SetCurrentService(service);
2416
2417 // These expectations are very much like SetupConnectedService except
2418 // that we verify that ResetSupsectCredentialFailures() is not called
2419 // on the service just because supplicant entered the Completed state.
2420 EXPECT_CALL(*service, SetState(Service::kStateConfiguring));
2421 EXPECT_CALL(*service, ResetSuspectedCredentialFailures()).Times(0);
2422 EXPECT_CALL(*dhcp_provider(), CreateConfig(_, _, _, _)).Times(AnyNumber());
2423 EXPECT_CALL(*dhcp_config_.get(), RequestIP()).Times(AnyNumber());
2424 EXPECT_CALL(*manager(), device_info()).WillRepeatedly(Return(device_info()));
2425 EXPECT_CALL(*device_info(), GetByteCounts(_, _, _))
2426 .WillOnce(DoAll(SetArgumentPointee<2>(0LL), Return(true)));
2427 ReportStateChanged(WPASupplicant::kInterfaceStateCompleted);
2428
2429 Mock::VerifyAndClearExpectations(device_info());
2430 Mock::VerifyAndClearExpectations(service);
2431
2432 // Successful connect.
2433 EXPECT_CALL(*GetSupplicantInterfaceProxy(), EnableHighBitrates()).Times(1);
2434 EXPECT_CALL(*service, ResetSuspectedCredentialFailures());
2435 ReportConnected();
2436
2437 EXPECT_CALL(*device_info(), GetByteCounts(_, _, _))
2438 .WillOnce(DoAll(SetArgumentPointee<2>(1LL), Return(true)))
2439 .WillOnce(DoAll(SetArgumentPointee<2>(0LL), Return(true)))
2440 .WillOnce(DoAll(SetArgumentPointee<2>(0LL), Return(true)));
2441
2442 // If there was an increased byte-count while we were timing out DHCP,
2443 // this should be considered a DHCP failure and not a credential failure.
2444 EXPECT_CALL(*service, ResetSuspectedCredentialFailures()).Times(0);
2445 EXPECT_CALL(*service, DisconnectWithFailure(Service::kFailureDHCP, _));
2446 ReportIPConfigFailure();
2447 Mock::VerifyAndClearExpectations(service);
2448
2449 // Connection failed during DHCP but service does not (yet) believe this is
2450 // due to a passphrase issue.
2451 EXPECT_CALL(*service, AddSuspectedCredentialFailure())
2452 .WillOnce(Return(false));
2453 EXPECT_CALL(*service, DisconnectWithFailure(Service::kFailureDHCP, _));
2454 ReportIPConfigFailure();
2455 Mock::VerifyAndClearExpectations(service);
2456
2457 // Connection failed during DHCP and service believes this is due to a
2458 // passphrase issue.
2459 EXPECT_CALL(*service, AddSuspectedCredentialFailure())
2460 .WillOnce(Return(true));
2461 EXPECT_CALL(*service,
2462 DisconnectWithFailure(Service::kFailureBadPassphrase, _));
2463 ReportIPConfigFailure();
2464}
2465
Paul Stewart1369c2b2013-01-11 05:41:26 -08002466TEST_F(WiFiMainTest, SuspectCredentialsEAPInProgress) {
Ben Chanf024ef42013-09-20 14:21:38 -07002467 MockWiFiServiceRefPtr service = MakeMockService(kSecurity8021x);
Paul Stewart735eab52013-03-29 09:19:23 -07002468 EXPECT_CALL(*eap_state_handler_, is_eap_in_progress())
2469 .WillOnce(Return(false))
2470 .WillOnce(Return(true))
2471 .WillOnce(Return(false))
2472 .WillOnce(Return(true));
Paul Stewartbca08f82013-07-09 16:32:37 -07002473 EXPECT_CALL(*service, AddSuspectedCredentialFailure()).Times(0);
2474 EXPECT_FALSE(SuspectCredentials(service, NULL));
2475 Mock::VerifyAndClearExpectations(service);
2476
2477 EXPECT_CALL(*service, AddSuspectedCredentialFailure()).WillOnce(Return(true));
Paul Stewart1369c2b2013-01-11 05:41:26 -08002478 Service::ConnectFailure failure;
Paul Stewartbca08f82013-07-09 16:32:37 -07002479 EXPECT_TRUE(SuspectCredentials(service, &failure));
Paul Stewart1369c2b2013-01-11 05:41:26 -08002480 EXPECT_EQ(Service::kFailureEAPAuthentication, failure);
Paul Stewartbca08f82013-07-09 16:32:37 -07002481 Mock::VerifyAndClearExpectations(service);
2482
2483 EXPECT_CALL(*service, AddSuspectedCredentialFailure()).Times(0);
2484 EXPECT_FALSE(SuspectCredentials(service, NULL));
2485 Mock::VerifyAndClearExpectations(service);
2486
2487 EXPECT_CALL(*service, AddSuspectedCredentialFailure())
2488 .WillOnce(Return(false));
2489 EXPECT_FALSE(SuspectCredentials(service, NULL));
Paul Stewart1369c2b2013-01-11 05:41:26 -08002490}
2491
2492TEST_F(WiFiMainTest, SuspectCredentialsYieldFailureWPA) {
Ben Chanf024ef42013-09-20 14:21:38 -07002493 MockWiFiServiceRefPtr service = MakeMockService(kSecurityWpa);
mukesh agrawalcf24a242012-05-21 16:46:11 -07002494 SetPendingService(service);
Paul Stewart0654ece2013-03-26 15:21:26 -07002495 ReportStateChanged(WPASupplicant::kInterfaceState4WayHandshake);
mukesh agrawalcf24a242012-05-21 16:46:11 -07002496
Wade Guthriea5995cf2013-07-30 09:31:16 -07002497 ExpectScanIdle();
Paul Stewartbca08f82013-07-09 16:32:37 -07002498 EXPECT_CALL(*service, AddSuspectedCredentialFailure()).WillOnce(Return(true));
Paul Stewartf2d60912012-07-15 08:37:30 -07002499 EXPECT_CALL(*service, SetFailure(Service::kFailureBadPassphrase));
mukesh agrawal56e32202012-07-26 16:32:11 -07002500 EXPECT_CALL(*service, SetFailureSilent(_)).Times(0);
2501 EXPECT_CALL(*service, SetState(_)).Times(0);
Paul Stewart735eab52013-03-29 09:19:23 -07002502 ScopedMockLog log;
mukesh agrawalcf24a242012-05-21 16:46:11 -07002503 EXPECT_CALL(log, Log(_, _, _)).Times(AnyNumber());
Ben Chanf024ef42013-09-20 14:21:38 -07002504 EXPECT_CALL(log, Log(logging::LOG_ERROR, _, EndsWith(kErrorBadPassphrase)));
Paul Stewart0654ece2013-03-26 15:21:26 -07002505 ReportCurrentBSSChanged(WPASupplicant::kCurrentBSSNull);
Paul Stewart1369c2b2013-01-11 05:41:26 -08002506}
2507
2508TEST_F(WiFiMainTest, SuspectCredentialsYieldFailureEAP) {
Ben Chanf024ef42013-09-20 14:21:38 -07002509 MockWiFiServiceRefPtr service = MakeMockService(kSecurity8021x);
Paul Stewart1369c2b2013-01-11 05:41:26 -08002510 SetCurrentService(service);
Paul Stewart1369c2b2013-01-11 05:41:26 -08002511
Paul Stewart735eab52013-03-29 09:19:23 -07002512 ScopedMockLog log;
2513 EXPECT_CALL(log, Log(_, _, _)).Times(AnyNumber());
Paul Stewart1369c2b2013-01-11 05:41:26 -08002514 EXPECT_CALL(*service, SetFailureSilent(_)).Times(0);
2515 EXPECT_CALL(*service, SetState(_)).Times(0);
Paul Stewart735eab52013-03-29 09:19:23 -07002516 // Ensure that we retrieve is_eap_in_progress() before resetting the
2517 // EAP handler's state.
2518 InSequence seq;
2519 EXPECT_CALL(*eap_state_handler_, is_eap_in_progress())
2520 .WillOnce(Return(true));
Paul Stewartbca08f82013-07-09 16:32:37 -07002521 EXPECT_CALL(*service, AddSuspectedCredentialFailure()).WillOnce(Return(true));
Paul Stewart735eab52013-03-29 09:19:23 -07002522 EXPECT_CALL(*service, SetFailure(Service::kFailureEAPAuthentication));
Paul Stewart1369c2b2013-01-11 05:41:26 -08002523 EXPECT_CALL(log, Log(logging::LOG_ERROR, _,
Ben Chan39a7beb2013-09-21 11:28:00 -07002524 EndsWith(kErrorEapAuthenticationFailed)));
Paul Stewart735eab52013-03-29 09:19:23 -07002525 EXPECT_CALL(*eap_state_handler_, Reset());
Paul Stewart0654ece2013-03-26 15:21:26 -07002526 ReportCurrentBSSChanged(WPASupplicant::kCurrentBSSNull);
mukesh agrawalcf24a242012-05-21 16:46:11 -07002527}
2528
Paul Stewarte369ece2012-05-22 09:11:03 -07002529// Scanning tests will use a mock of the event dispatcher instead of a real
2530// one.
Paul Stewart1aff7302012-08-04 20:04:47 -07002531class WiFiTimerTest : public WiFiObjectTest {
Paul Stewarte369ece2012-05-22 09:11:03 -07002532 public:
Paul Stewart1aff7302012-08-04 20:04:47 -07002533 WiFiTimerTest() : WiFiObjectTest(&mock_dispatcher_) {}
Paul Stewarte369ece2012-05-22 09:11:03 -07002534
2535 protected:
2536 void ExpectInitialScanSequence();
2537
2538 StrictMock<MockEventDispatcher> mock_dispatcher_;
2539};
2540
Paul Stewart1aff7302012-08-04 20:04:47 -07002541void WiFiTimerTest::ExpectInitialScanSequence() {
Paul Stewarte369ece2012-05-22 09:11:03 -07002542 // Choose a number of iterations some multiple higher than the fast scan
2543 // count.
2544 const int kScanTimes = WiFi::kNumFastScanAttempts * 4;
2545
2546 // Each time we call FireScanTimer() below, WiFi will post a task to actually
2547 // run Scan() on the wpa_supplicant proxy.
2548 EXPECT_CALL(mock_dispatcher_, PostTask(_))
2549 .Times(kScanTimes);
2550 {
2551 InSequence seq;
2552 // The scans immediately after the initial scan should happen at the short
2553 // interval. If we add the initial scan (not invoked in this function) to
2554 // the ones in the expectation below, we get WiFi::kNumFastScanAttempts at
2555 // the fast scan interval.
2556 EXPECT_CALL(mock_dispatcher_, PostDelayedTask(
2557 _, WiFi::kFastScanIntervalSeconds * 1000))
2558 .Times(WiFi::kNumFastScanAttempts - 1)
2559 .WillRepeatedly(Return(true));
2560
2561 // After this, the WiFi device should use the normal scan interval.
2562 EXPECT_CALL(mock_dispatcher_, PostDelayedTask(
2563 _, GetScanInterval() * 1000))
2564 .Times(kScanTimes - WiFi::kNumFastScanAttempts + 1)
2565 .WillRepeatedly(Return(true));
2566
2567 for (int i = 0; i < kScanTimes; i++) {
2568 FireScanTimer();
2569 }
2570 }
2571}
2572
Paul Stewart1aff7302012-08-04 20:04:47 -07002573TEST_F(WiFiTimerTest, FastRescan) {
Paul Stewarte369ece2012-05-22 09:11:03 -07002574 // This PostTask is a result of the call to Scan(NULL), and is meant to
2575 // post a task to call Scan() on the wpa_supplicant proxy immediately.
2576 EXPECT_CALL(mock_dispatcher_, PostTask(_));
2577 EXPECT_CALL(mock_dispatcher_, PostDelayedTask(
2578 _, WiFi::kFastScanIntervalSeconds * 1000))
2579 .WillOnce(Return(true));
2580 StartWiFi();
2581
2582 ExpectInitialScanSequence();
2583
2584 // If we end up disconnecting, the sequence should repeat.
2585 EXPECT_CALL(mock_dispatcher_, PostDelayedTask(
2586 _, WiFi::kFastScanIntervalSeconds * 1000))
2587 .WillOnce(Return(true));
2588 RestartFastScanAttempts();
2589
2590 ExpectInitialScanSequence();
2591}
2592
Paul Stewart1aff7302012-08-04 20:04:47 -07002593TEST_F(WiFiTimerTest, ReconnectTimer) {
2594 EXPECT_CALL(mock_dispatcher_, PostTask(_)).Times(AnyNumber());
2595 EXPECT_CALL(mock_dispatcher_, PostDelayedTask(_, _)).Times(AnyNumber());
Paul Stewart3c504012013-01-17 17:49:58 -08002596 StartWiFi();
2597 SetupConnectedService(DBus::Path(), NULL, NULL);
Paul Stewart1aff7302012-08-04 20:04:47 -07002598 Mock::VerifyAndClearExpectations(&mock_dispatcher_);
2599
2600 EXPECT_CALL(mock_dispatcher_, PostDelayedTask(
2601 _, GetReconnectTimeoutSeconds() * 1000)).Times(1);
2602 StartReconnectTimer();
2603 Mock::VerifyAndClearExpectations(&mock_dispatcher_);
2604 StopReconnectTimer();
2605
2606 EXPECT_CALL(mock_dispatcher_, PostDelayedTask(
2607 _, GetReconnectTimeoutSeconds() * 1000)).Times(1);
2608 StartReconnectTimer();
2609 Mock::VerifyAndClearExpectations(&mock_dispatcher_);
2610 GetReconnectTimeoutCallback().callback().Run();
2611
2612 EXPECT_CALL(mock_dispatcher_, PostDelayedTask(
2613 _, GetReconnectTimeoutSeconds() * 1000)).Times(1);
2614 StartReconnectTimer();
2615 Mock::VerifyAndClearExpectations(&mock_dispatcher_);
2616
2617 EXPECT_CALL(mock_dispatcher_, PostDelayedTask(
2618 _, GetReconnectTimeoutSeconds() * 1000)).Times(0);
2619 StartReconnectTimer();
2620}
2621
Paul Stewart7cd45722013-08-12 14:50:14 -07002622TEST_F(WiFiTimerTest, RequestStationInfo) {
2623 EXPECT_CALL(mock_dispatcher_, PostTask(_)).Times(AnyNumber());
2624 EXPECT_CALL(mock_dispatcher_, PostDelayedTask(_, _)).Times(AnyNumber());
2625
2626 // Setup a connected service here while we have the expectations above set.
2627 StartWiFi();
2628 MockWiFiServiceRefPtr service =
2629 SetupConnectedService(DBus::Path(), NULL, NULL);
2630 string connected_bss = GetSupplicantBSS();
2631 Mock::VerifyAndClearExpectations(&mock_dispatcher_);
2632
2633 EXPECT_CALL(netlink_manager_, SendNl80211Message(_, _, _)).Times(0);
2634 EXPECT_CALL(mock_dispatcher_, PostDelayedTask(_, _)).Times(0);
2635 NiceScopedMockLog log;
2636
2637 // There is no current_service_.
2638 EXPECT_CALL(log, Log(_, _, HasSubstr("we are not connected")));
2639 SetCurrentService(NULL);
2640 RequestStationInfo();
2641
2642 // current_service_ is not connnected.
2643 EXPECT_CALL(*service, IsConnected()).WillOnce(Return(false));
2644 SetCurrentService(service);
2645 EXPECT_CALL(log, Log(_, _, HasSubstr("we are not connected")));
2646 RequestStationInfo();
2647
2648 // Endpoint does not exist in endpoint_by_rpcid_.
2649 EXPECT_CALL(*service, IsConnected()).WillRepeatedly(Return(true));
2650 SetSupplicantBSS("/some/path/that/does/not/exist/in/endpoint_by_rpcid");
2651 EXPECT_CALL(log, Log(_, _, HasSubstr(
2652 "Can't get endpoint for current supplicant BSS")));
2653 RequestStationInfo();
2654 Mock::VerifyAndClearExpectations(&netlink_manager_);
2655 Mock::VerifyAndClearExpectations(&mock_dispatcher_);
2656
2657 // We successfully trigger a request to get the station and start a timer
2658 // for the next call.
2659 EXPECT_CALL(netlink_manager_, SendNl80211Message(
2660 IsNl80211Command(kNl80211FamilyId, NL80211_CMD_GET_STATION), _, _));
2661 EXPECT_CALL(mock_dispatcher_, PostDelayedTask(
2662 _, WiFi::kRequestStationInfoPeriodSeconds * 1000));
2663 SetSupplicantBSS(connected_bss);
2664 RequestStationInfo();
2665
2666 // Now test that a properly formatted New Station message updates strength.
2667 NewStationMessage new_station;
2668 new_station.attributes()->CreateRawAttribute(NL80211_ATTR_MAC, "BSSID");
2669
2670 // Use a reference to the endpoint instance in the WiFi device instead of
2671 // the copy returned by SetupConnectedService().
2672 WiFiEndpointRefPtr endpoint = GetEndpointMap().begin()->second;
2673 new_station.attributes()->SetRawAttributeValue(
2674 NL80211_ATTR_MAC, ByteString::CreateFromHexString(endpoint->bssid_hex()));
2675 new_station.attributes()->CreateNestedAttribute(
2676 NL80211_ATTR_STA_INFO, "Station Info");
2677 AttributeListRefPtr station_info;
2678 new_station.attributes()->GetNestedAttributeList(
2679 NL80211_ATTR_STA_INFO, &station_info);
2680 station_info->CreateU8Attribute(NL80211_STA_INFO_SIGNAL, "Signal");
2681 const int16_t kSignalValue = -20;
2682 station_info->SetU8AttributeValue(NL80211_STA_INFO_SIGNAL, kSignalValue);
2683 new_station.attributes()->SetNestedAttributeHasAValue(NL80211_ATTR_STA_INFO);
2684
2685 EXPECT_NE(kSignalValue, endpoint->signal_strength());
2686 EXPECT_CALL(*wifi_provider(), OnEndpointUpdated(EndpointMatch(endpoint)));
2687 AttributeListConstRefPtr station_info_prime;
2688 ReportReceivedStationInfo(new_station);
2689 EXPECT_EQ(kSignalValue, endpoint->signal_strength());
2690}
2691
Paul Stewartbc6e7392012-05-24 07:07:48 -07002692TEST_F(WiFiMainTest, EAPCertification) {
Ben Chanf024ef42013-09-20 14:21:38 -07002693 MockWiFiServiceRefPtr service = MakeMockService(kSecurity8021x);
Paul Stewartbc6e7392012-05-24 07:07:48 -07002694 EXPECT_CALL(*service, AddEAPCertification(_, _)).Times(0);
2695
2696 ScopedMockLog log;
2697 EXPECT_CALL(log, Log(logging::LOG_ERROR, _, EndsWith("no current service.")));
2698 map<string, ::DBus::Variant> args;
2699 ReportCertification(args);
2700 Mock::VerifyAndClearExpectations(&log);
2701
2702 SetCurrentService(service);
2703 EXPECT_CALL(log, Log(logging::LOG_ERROR, _, EndsWith("no depth parameter.")));
2704 ReportCertification(args);
2705 Mock::VerifyAndClearExpectations(&log);
2706
2707 const uint32 kDepth = 123;
Paul Stewart0654ece2013-03-26 15:21:26 -07002708 args[WPASupplicant::kInterfacePropertyDepth].writer().append_uint32(kDepth);
Paul Stewartbc6e7392012-05-24 07:07:48 -07002709
2710 EXPECT_CALL(log,
2711 Log(logging::LOG_ERROR, _, EndsWith("no subject parameter.")));
2712 ReportCertification(args);
2713 Mock::VerifyAndClearExpectations(&log);
2714
2715 const string kSubject("subject");
Paul Stewart0654ece2013-03-26 15:21:26 -07002716 args[WPASupplicant::kInterfacePropertySubject].writer()
Paul Stewartbc6e7392012-05-24 07:07:48 -07002717 .append_string(kSubject.c_str());
2718 EXPECT_CALL(*service, AddEAPCertification(kSubject, kDepth)).Times(1);
2719 ReportCertification(args);
2720}
2721
Paul Stewartdb0f9172012-11-30 16:48:09 -08002722TEST_F(WiFiMainTest, EAPEvent) {
Paul Stewartdb0f9172012-11-30 16:48:09 -08002723 ScopedMockLog log;
2724 EXPECT_CALL(log, Log(logging::LOG_ERROR, _, EndsWith("no current service.")));
Paul Stewart735eab52013-03-29 09:19:23 -07002725 EXPECT_CALL(*eap_state_handler_, ParseStatus(_, _, _)).Times(0);
2726 const string kEAPStatus("eap-status");
2727 const string kEAPParameter("eap-parameter");
2728 ReportEAPEvent(kEAPStatus, kEAPParameter);
Paul Stewartdb0f9172012-11-30 16:48:09 -08002729 Mock::VerifyAndClearExpectations(&log);
2730 EXPECT_CALL(log, Log(_, _, _)).Times(AnyNumber());
2731
Ben Chanf024ef42013-09-20 14:21:38 -07002732 MockWiFiServiceRefPtr service = MakeMockService(kSecurity8021x);
Paul Stewartdb0f9172012-11-30 16:48:09 -08002733 EXPECT_CALL(*service, SetFailure(_)).Times(0);
Paul Stewart735eab52013-03-29 09:19:23 -07002734 EXPECT_CALL(*eap_state_handler_, ParseStatus(kEAPStatus, kEAPParameter, _));
Paul Stewartdb0f9172012-11-30 16:48:09 -08002735 SetCurrentService(service);
Paul Stewart735eab52013-03-29 09:19:23 -07002736 ReportEAPEvent(kEAPStatus, kEAPParameter);
2737 Mock::VerifyAndClearExpectations(service);
2738 Mock::VerifyAndClearExpectations(eap_state_handler_);
Paul Stewartdb0f9172012-11-30 16:48:09 -08002739
Paul Stewart735eab52013-03-29 09:19:23 -07002740 EXPECT_CALL(*eap_state_handler_, ParseStatus(kEAPStatus, kEAPParameter, _))
2741 .WillOnce(DoAll(SetArgumentPointee<2>(Service::kFailureOutOfRange),
2742 Return(false)));
2743 EXPECT_CALL(*service, DisconnectWithFailure(Service::kFailureOutOfRange, _));
2744 ReportEAPEvent(kEAPStatus, kEAPParameter);
Paul Stewartdb0f9172012-11-30 16:48:09 -08002745}
2746
mukesh agrawalc4f368f2012-06-04 19:45:52 -07002747TEST_F(WiFiMainTest, PendingScanDoesNotCrashAfterStop) {
2748 // Scan is one task that should be skipped after Stop. Others are
2749 // skipped by the same mechanism (invalidating weak pointers), so we
2750 // don't test them individually.
2751 //
2752 // Note that we can't test behavior by setting expectations on the
2753 // supplicant_interface_proxy_, since that is destroyed when we StopWiFi().
2754 StartWiFi();
2755 StopWiFi();
2756 dispatcher_.DispatchPendingEvents();
2757}
2758
Darin Petkov60ceaf32012-10-18 10:36:01 +02002759TEST_F(WiFiMainTest, VerifyPaths) {
Paul Stewart196f50f2013-03-27 18:02:11 -07002760 string path(WPASupplicant::kSupplicantConfPath);
Darin Petkov60ceaf32012-10-18 10:36:01 +02002761 TrimString(path, FilePath::kSeparators, &path);
2762 EXPECT_TRUE(file_util::PathExists(FilePath(SYSROOT).Append(path)));
2763}
2764
Gaurav Shah6d2c72d2012-10-16 16:30:44 -07002765struct BSS {
2766 string bsspath;
2767 string ssid;
2768 string bssid;
2769 int16_t signal_strength;
2770 uint16 frequency;
2771 const char* mode;
2772};
2773
2774TEST_F(WiFiMainTest, GetGeolocationObjects) {
2775 BSS bsses[] = {
2776 {"bssid1", "ssid1", "00:00:00:00:00:00", 5, Metrics::kWiFiFrequency2412,
2777 kNetworkModeInfrastructure},
2778 {"bssid2", "ssid2", "01:00:00:00:00:00", 30, Metrics::kWiFiFrequency5170,
2779 kNetworkModeInfrastructure},
2780 // Same SSID but different BSSID is an additional geolocation object.
2781 {"bssid3", "ssid1", "02:00:00:00:00:00", 100, 0,
2782 kNetworkModeInfrastructure}
2783 };
2784 StartWiFi();
2785 vector<GeolocationInfo> objects;
2786 EXPECT_EQ(objects.size(), 0);
2787
2788 for (size_t i = 0; i < arraysize(bsses); ++i) {
2789 ReportBSS(bsses[i].bsspath, bsses[i].ssid, bsses[i].bssid,
2790 bsses[i].signal_strength, bsses[i].frequency, bsses[i].mode);
2791 objects = wifi()->GetGeolocationObjects();
2792 EXPECT_EQ(objects.size(), i + 1);
2793
2794 GeolocationInfo expected_info;
2795 expected_info.AddField(kGeoMacAddressProperty, bsses[i].bssid);
2796 expected_info.AddField(kGeoSignalStrengthProperty,
2797 StringPrintf("%d", bsses[i].signal_strength));
2798 expected_info.AddField(kGeoChannelProperty, StringPrintf(
2799 "%d", Metrics::WiFiFrequencyToChannel(bsses[i].frequency)));
2800 EXPECT_TRUE(objects[i].Equals(expected_info));
Wade Guthrie7347bf22013-04-30 11:21:51 -07002801 }
2802}
Gaurav Shah6d2c72d2012-10-16 16:30:44 -07002803
Paul Stewart5581d072012-12-17 17:30:20 -08002804TEST_F(WiFiMainTest, SetSupplicantDebugLevel) {
2805 MockSupplicantProcessProxy *process_proxy = supplicant_process_proxy_.get();
2806
2807 // With WiFi not yet started, nothing interesting (including a crash) should
2808 // happen.
2809 EXPECT_CALL(*process_proxy, GetDebugLevel()).Times(0);
2810 EXPECT_CALL(*process_proxy, SetDebugLevel(_)).Times(0);
2811 ReportWiFiDebugScopeChanged(true);
2812
2813 // This unit test turns on WiFi debugging, so when we start WiFi, we should
2814 // check but not set the debug level if we return the "debug" level.
2815 EXPECT_CALL(*process_proxy, GetDebugLevel())
Paul Stewart0654ece2013-03-26 15:21:26 -07002816 .WillOnce(Return(WPASupplicant::kDebugLevelDebug));
Paul Stewart5581d072012-12-17 17:30:20 -08002817 EXPECT_CALL(*process_proxy, SetDebugLevel(_)).Times(0);
2818 StartWiFi();
2819 Mock::VerifyAndClearExpectations(process_proxy);
2820
2821 // If WiFi debugging is toggled and wpa_supplicant reports debugging
2822 // is set to some unmanaged level, WiFi should leave it alone.
2823 EXPECT_CALL(*process_proxy, GetDebugLevel())
Paul Stewart0654ece2013-03-26 15:21:26 -07002824 .WillOnce(Return(WPASupplicant::kDebugLevelError))
2825 .WillOnce(Return(WPASupplicant::kDebugLevelError))
2826 .WillOnce(Return(WPASupplicant::kDebugLevelExcessive))
2827 .WillOnce(Return(WPASupplicant::kDebugLevelExcessive))
2828 .WillOnce(Return(WPASupplicant::kDebugLevelMsgDump))
2829 .WillOnce(Return(WPASupplicant::kDebugLevelMsgDump))
2830 .WillOnce(Return(WPASupplicant::kDebugLevelWarning))
2831 .WillOnce(Return(WPASupplicant::kDebugLevelWarning));
Paul Stewart5581d072012-12-17 17:30:20 -08002832 EXPECT_CALL(*process_proxy, SetDebugLevel(_)).Times(0);
2833 ReportWiFiDebugScopeChanged(true);
2834 ReportWiFiDebugScopeChanged(false);
2835 ReportWiFiDebugScopeChanged(true);
2836 ReportWiFiDebugScopeChanged(false);
2837 ReportWiFiDebugScopeChanged(true);
2838 ReportWiFiDebugScopeChanged(false);
2839 ReportWiFiDebugScopeChanged(true);
2840 ReportWiFiDebugScopeChanged(false);
2841 Mock::VerifyAndClearExpectations(process_proxy);
2842
2843 // If WiFi debugging is turned off and wpa_supplicant reports debugging
2844 // is turned on, WiFi should turn supplicant debugging off.
2845 EXPECT_CALL(*process_proxy, GetDebugLevel())
Paul Stewart0654ece2013-03-26 15:21:26 -07002846 .WillOnce(Return(WPASupplicant::kDebugLevelDebug));
2847 EXPECT_CALL(*process_proxy, SetDebugLevel(WPASupplicant::kDebugLevelInfo))
Paul Stewart5581d072012-12-17 17:30:20 -08002848 .Times(1);
2849 ReportWiFiDebugScopeChanged(false);
2850 Mock::VerifyAndClearExpectations(process_proxy);
2851
2852 // If WiFi debugging is turned on and wpa_supplicant reports debugging
2853 // is turned off, WiFi should turn supplicant debugging on.
2854 EXPECT_CALL(*process_proxy, GetDebugLevel())
Paul Stewart0654ece2013-03-26 15:21:26 -07002855 .WillOnce(Return(WPASupplicant::kDebugLevelInfo));
2856 EXPECT_CALL(*process_proxy, SetDebugLevel(WPASupplicant::kDebugLevelDebug))
Paul Stewart5581d072012-12-17 17:30:20 -08002857 .Times(1);
2858 ReportWiFiDebugScopeChanged(true);
2859 Mock::VerifyAndClearExpectations(process_proxy);
2860
2861 // If WiFi debugging is already in the correct state, it should not be
2862 // changed.
2863 EXPECT_CALL(*process_proxy, GetDebugLevel())
Paul Stewart0654ece2013-03-26 15:21:26 -07002864 .WillOnce(Return(WPASupplicant::kDebugLevelDebug))
2865 .WillOnce(Return(WPASupplicant::kDebugLevelInfo));
Paul Stewart5581d072012-12-17 17:30:20 -08002866 EXPECT_CALL(*process_proxy, SetDebugLevel(_)).Times(0);
2867 ReportWiFiDebugScopeChanged(true);
2868 ReportWiFiDebugScopeChanged(false);
2869
2870 // After WiFi is stopped, we shouldn't be calling the proxy.
2871 EXPECT_CALL(*process_proxy, GetDebugLevel()).Times(0);
2872 EXPECT_CALL(*process_proxy, SetDebugLevel(_)).Times(0);
2873 StopWiFi();
2874 ReportWiFiDebugScopeChanged(true);
2875 ReportWiFiDebugScopeChanged(false);
2876}
2877
Darin Petkov50cb78a2013-02-06 16:17:49 +01002878TEST_F(WiFiMainTest, LogSSID) {
2879 EXPECT_EQ("[SSID=]", WiFi::LogSSID(""));
2880 EXPECT_EQ("[SSID=foo\\x5b\\x09\\x5dbar]", WiFi::LogSSID("foo[\t]bar"));
2881}
2882
mukesh agrawalbebf1b82013-04-23 15:06:33 -07002883// Custom property setters should return false, and make no changes, if
2884// the new value is the same as the old value.
2885TEST_F(WiFiMainTest, CustomSetterNoopChange) {
2886 // SetBgscanShortInterval
2887 {
2888 Error error;
2889 static const uint16 kKnownScanInterval = 4;
2890 // Set to known value.
2891 EXPECT_TRUE(SetBgscanShortInterval(kKnownScanInterval, &error));
2892 EXPECT_TRUE(error.IsSuccess());
2893 // Set to same value.
2894 EXPECT_FALSE(SetBgscanShortInterval(kKnownScanInterval, &error));
2895 EXPECT_TRUE(error.IsSuccess());
2896 }
2897
2898 // SetBgscanSignalThreshold
2899 {
2900 Error error;
2901 static const int32 kKnownSignalThreshold = 4;
2902 // Set to known value.
2903 EXPECT_TRUE(SetBgscanSignalThreshold(kKnownSignalThreshold, &error));
2904 EXPECT_TRUE(error.IsSuccess());
2905 // Set to same value.
2906 EXPECT_FALSE(SetBgscanSignalThreshold(kKnownSignalThreshold, &error));
2907 EXPECT_TRUE(error.IsSuccess());
2908 }
2909
2910 // SetScanInterval
2911 {
2912 Error error;
2913 EXPECT_FALSE(SetScanInterval(GetScanInterval(), &error));
2914 EXPECT_TRUE(error.IsSuccess());
2915 }
2916}
2917
Wade Guthrie0cf3c982013-05-29 09:11:35 -07002918// The following tests check the scan_state_ / scan_method_ state machine.
2919
2920TEST_F(WiFiMainTest, FullScanFindsNothing) {
Wade Guthrie9f4aa152013-07-29 16:03:14 -07002921 StartScan(WiFi::kScanMethodFull);
Wade Guthrie0cf3c982013-05-29 09:11:35 -07002922 ReportScanDone();
Wade Guthriea5995cf2013-07-30 09:31:16 -07002923 ExpectScanStop();
2924 ExpectFoundNothing();
2925 NiceScopedMockLog log;
2926 ScopeLogger::GetInstance()->EnableScopesByName("wifi");
2927 ScopeLogger::GetInstance()->set_verbose_level(10);
Wade Guthrie0cf3c982013-05-29 09:11:35 -07002928 EXPECT_CALL(log, Log(_, _, _)).Times(AnyNumber());
2929 EXPECT_CALL(log, Log(_, _, HasSubstr("FULL_NOCONNECTION ->")));
Paul Stewart7de7e022013-08-28 09:42:50 -07002930 EXPECT_CALL(*manager(), OnDeviceGeolocationInfoUpdated(_));
Wade Guthrie0cf3c982013-05-29 09:11:35 -07002931 dispatcher_.DispatchPendingEvents(); // Launch UpdateScanStateAfterScanDone
2932 VerifyScanState(WiFi::kScanIdle, WiFi::kScanMethodNone);
2933
2934 ScopeLogger::GetInstance()->set_verbose_level(0);
2935 ScopeLogger::GetInstance()->EnableScopesByName("-wifi");
2936}
2937
2938TEST_F(WiFiMainTest, FullScanConnectingToConnected) {
Wade Guthrie9f4aa152013-07-29 16:03:14 -07002939 StartScan(WiFi::kScanMethodFull);
Wade Guthrie0cf3c982013-05-29 09:11:35 -07002940 WiFiEndpointRefPtr endpoint;
2941 ::DBus::Path bss_path;
Wade Guthrie9f4aa152013-07-29 16:03:14 -07002942 MockWiFiServiceRefPtr service = AttemptConnection(WiFi::kScanMethodFull,
2943 &endpoint,
2944 &bss_path);
Wade Guthrie0cf3c982013-05-29 09:11:35 -07002945
2946 // Complete the connection.
Wade Guthriea5995cf2013-07-30 09:31:16 -07002947 ExpectConnected();
Wade Guthrie0cf3c982013-05-29 09:11:35 -07002948 EXPECT_CALL(*service, NotifyCurrentEndpoint(EndpointMatch(endpoint)));
Wade Guthriea5995cf2013-07-30 09:31:16 -07002949 NiceScopedMockLog log;
2950 ScopeLogger::GetInstance()->EnableScopesByName("wifi");
2951 ScopeLogger::GetInstance()->set_verbose_level(10);
Wade Guthrie0cf3c982013-05-29 09:11:35 -07002952 EXPECT_CALL(log, Log(_, _, _)).Times(AnyNumber());
2953 EXPECT_CALL(log, Log(_, _, HasSubstr("-> FULL_CONNECTED")));
2954 ReportCurrentBSSChanged(bss_path);
2955 VerifyScanState(WiFi::kScanIdle, WiFi::kScanMethodNone);
2956
2957 ScopeLogger::GetInstance()->set_verbose_level(0);
2958 ScopeLogger::GetInstance()->EnableScopesByName("-wifi");
2959}
2960
2961TEST_F(WiFiMainTest, ProgressiveScanConnectingToConnected) {
Wade Guthrie9f4aa152013-07-29 16:03:14 -07002962 StartScan(WiFi::kScanMethodProgressive);
Wade Guthrie0cf3c982013-05-29 09:11:35 -07002963 WiFiEndpointRefPtr endpoint;
2964 ::DBus::Path bss_path;
Wade Guthrie9f4aa152013-07-29 16:03:14 -07002965 MockWiFiServiceRefPtr service = AttemptConnection(
2966 WiFi::kScanMethodProgressive, &endpoint, &bss_path);
Wade Guthrie0cf3c982013-05-29 09:11:35 -07002967
2968 // Complete the connection.
Wade Guthriea5995cf2013-07-30 09:31:16 -07002969 ExpectConnected();
Wade Guthrie0cf3c982013-05-29 09:11:35 -07002970 EXPECT_CALL(*service, NotifyCurrentEndpoint(EndpointMatch(endpoint)));
Wade Guthriea5995cf2013-07-30 09:31:16 -07002971 NiceScopedMockLog log;
2972 ScopeLogger::GetInstance()->EnableScopesByName("wifi");
2973 ScopeLogger::GetInstance()->set_verbose_level(10);
Wade Guthrie0cf3c982013-05-29 09:11:35 -07002974 EXPECT_CALL(log, Log(_, _, _)).Times(AnyNumber());
2975 EXPECT_CALL(log, Log(_, _, HasSubstr("-> PROGRESSIVE_CONNECTED")));
2976 ReportCurrentBSSChanged(bss_path);
2977 VerifyScanState(WiFi::kScanIdle, WiFi::kScanMethodNone);
2978
2979 ScopeLogger::GetInstance()->set_verbose_level(0);
2980 ScopeLogger::GetInstance()->EnableScopesByName("-wifi");
2981}
2982
Wade Guthriedf6d61b2013-07-17 11:43:55 -07002983TEST_F(WiFiMainTest, ProgressiveScanConnectingToNotFound) {
Wade Guthrie9f4aa152013-07-29 16:03:14 -07002984 StartScan(WiFi::kScanMethodProgressive);
Wade Guthriedf6d61b2013-07-17 11:43:55 -07002985 WiFiEndpointRefPtr endpoint;
Wade Guthrie9f4aa152013-07-29 16:03:14 -07002986 MockWiFiServiceRefPtr service = AttemptConnection(
2987 WiFi::kScanMethodProgressive, &endpoint, nullptr);
Wade Guthriedf6d61b2013-07-17 11:43:55 -07002988
2989 // Simulate connection timeout.
Wade Guthriea5995cf2013-07-30 09:31:16 -07002990 ExpectFoundNothing();
2991 EXPECT_CALL(*service,
2992 NotifyCurrentEndpoint(EndpointMatch(endpoint))).Times(0);
Wade Guthrie9f4aa152013-07-29 16:03:14 -07002993 NiceScopedMockLog log;
Wade Guthriedf6d61b2013-07-17 11:43:55 -07002994 ScopeLogger::GetInstance()->EnableScopesByName("wifi");
2995 ScopeLogger::GetInstance()->set_verbose_level(10);
Wade Guthriedf6d61b2013-07-17 11:43:55 -07002996 EXPECT_CALL(log, Log(_, _, _)).Times(AnyNumber());
2997 EXPECT_CALL(log,
2998 Log(_, _, HasSubstr("-> PROGRESSIVE_FINISHED_NOCONNECTION")));
2999 EXPECT_CALL(*metrics(), NotifyDeviceConnectFinished(_)).Times(0);
Wade Guthriedf6d61b2013-07-17 11:43:55 -07003000 TimeoutPendingConnection();
3001 ScopeLogger::GetInstance()->set_verbose_level(0);
3002 ScopeLogger::GetInstance()->EnableScopesByName("-wifi");
3003 VerifyScanState(WiFi::kScanIdle, WiFi::kScanMethodNone);
3004}
3005
Wade Guthrieb9e0ee72013-05-31 09:23:30 -07003006TEST_F(WiFiMainTest, ScanStateUma) {
3007 EXPECT_CALL(*metrics(), SendEnumToUMA(Metrics::kMetricScanResult, _, _)).
3008 Times(0);
Wade Guthriedf6d61b2013-07-17 11:43:55 -07003009 EXPECT_CALL(*metrics(), NotifyDeviceScanStarted(_));
Wade Guthrieb0def9f2013-07-12 13:49:18 -07003010 SetScanState(WiFi::kScanScanning, WiFi::kScanMethodProgressive, __func__);
Wade Guthrieb9e0ee72013-05-31 09:23:30 -07003011
Wade Guthriedf6d61b2013-07-17 11:43:55 -07003012 EXPECT_CALL(*metrics(), NotifyDeviceScanFinished(_));
3013 EXPECT_CALL(*metrics(), NotifyDeviceConnectStarted(_, _));
3014 SetScanState(WiFi::kScanConnecting, WiFi::kScanMethodProgressive, __func__);
3015
Wade Guthriea5995cf2013-07-30 09:31:16 -07003016 ExpectScanIdle(); // After connected.
Wade Guthriedf6d61b2013-07-17 11:43:55 -07003017 EXPECT_CALL(*metrics(), NotifyDeviceConnectFinished(_));
Wade Guthrieb9e0ee72013-05-31 09:23:30 -07003018 EXPECT_CALL(*metrics(), SendEnumToUMA(Metrics::kMetricScanResult, _, _));
Wade Guthrieb0def9f2013-07-12 13:49:18 -07003019 SetScanState(WiFi::kScanConnected, WiFi::kScanMethodProgressive, __func__);
Wade Guthrieb9e0ee72013-05-31 09:23:30 -07003020}
3021
Wade Guthriedf6d61b2013-07-17 11:43:55 -07003022TEST_F(WiFiMainTest, ScanStateNotScanningNoUma) {
3023 EXPECT_CALL(*metrics(), NotifyDeviceScanStarted(_)).Times(0);
3024 EXPECT_CALL(*metrics(), NotifyDeviceConnectStarted(_, _));
3025 SetScanState(WiFi::kScanConnecting, WiFi::kScanMethodNone, __func__);
3026
Wade Guthriea5995cf2013-07-30 09:31:16 -07003027 ExpectScanIdle(); // After connected.
Wade Guthriedf6d61b2013-07-17 11:43:55 -07003028 EXPECT_CALL(*metrics(), NotifyDeviceConnectFinished(_));
3029 EXPECT_CALL(*metrics(), SendEnumToUMA(Metrics::kMetricScanResult, _, _)).
3030 Times(0);
3031 SetScanState(WiFi::kScanConnected, WiFi::kScanMethodNone, __func__);
3032}
3033
3034TEST_F(WiFiMainTest, ConnectToServiceNotPending) {
3035 // Test for SetPendingService(NULL), condition a)
3036 // |ConnectTo|->|DisconnectFrom|.
Wade Guthrie9f4aa152013-07-29 16:03:14 -07003037 StartScan(WiFi::kScanMethodProgressive);
Wade Guthriedf6d61b2013-07-17 11:43:55 -07003038
3039 // Setup pending service.
Wade Guthriea5995cf2013-07-30 09:31:16 -07003040 ExpectScanStop();
3041 ExpectConnecting();
Wade Guthriedf6d61b2013-07-17 11:43:55 -07003042 MockWiFiServiceRefPtr service_pending(
3043 SetupConnectingService(DBus::Path(), NULL, NULL));
3044 EXPECT_EQ(service_pending.get(), GetPendingService().get());
3045
3046 // ConnectTo a different service than the pending one.
Wade Guthriea5995cf2013-07-30 09:31:16 -07003047 ExpectConnecting();
3048 EXPECT_CALL(*GetSupplicantInterfaceProxy(), Disconnect());
Wade Guthrie9f4aa152013-07-29 16:03:14 -07003049 NiceScopedMockLog log;
Wade Guthriedf6d61b2013-07-17 11:43:55 -07003050 ScopeLogger::GetInstance()->EnableScopesByName("wifi");
3051 ScopeLogger::GetInstance()->set_verbose_level(10);
Wade Guthriedf6d61b2013-07-17 11:43:55 -07003052 EXPECT_CALL(log, Log(_, _, _)).Times(AnyNumber());
3053 EXPECT_CALL(log, Log(_, _, HasSubstr("-> TRANSITION_TO_CONNECTING")));
3054 EXPECT_CALL(log, Log(_, _, HasSubstr("-> PROGRESSIVE_CONNECTING")));
3055 MockWiFiServiceRefPtr service_connecting(
3056 SetupConnectingService(DBus::Path(), NULL, NULL));
3057 ScopeLogger::GetInstance()->set_verbose_level(0);
3058 ScopeLogger::GetInstance()->EnableScopesByName("-wifi");
3059 EXPECT_EQ(service_connecting.get(), GetPendingService().get());
3060 EXPECT_EQ(NULL, GetCurrentService().get());
3061 VerifyScanState(WiFi::kScanConnecting, WiFi::kScanMethodProgressive);
Wade Guthriea5995cf2013-07-30 09:31:16 -07003062
3063 ExpectScanIdle(); // To silence messages from the destructor.
Wade Guthriedf6d61b2013-07-17 11:43:55 -07003064}
3065
3066TEST_F(WiFiMainTest, ConnectToWithError) {
Wade Guthrie9f4aa152013-07-29 16:03:14 -07003067 StartScan(WiFi::kScanMethodProgressive);
Wade Guthriedf6d61b2013-07-17 11:43:55 -07003068
Wade Guthriea5995cf2013-07-30 09:31:16 -07003069 ExpectScanIdle();
Wade Guthriedf6d61b2013-07-17 11:43:55 -07003070 EXPECT_CALL(*GetSupplicantInterfaceProxy(), AddNetwork(_)).
3071 WillOnce(Throw(
3072 DBus::Error(
3073 "fi.w1.wpa_supplicant1.InterfaceUnknown",
3074 "test threw fi.w1.wpa_supplicant1.InterfaceUnknown")));
3075 EXPECT_CALL(*metrics(), NotifyDeviceScanFinished(_)).Times(0);
3076 EXPECT_CALL(*metrics(), SendEnumToUMA(Metrics::kMetricScanResult, _, _)).
3077 Times(0);
Ben Chanf024ef42013-09-20 14:21:38 -07003078 EXPECT_CALL(*adaptor_, EmitBoolChanged(kScanningProperty, false));
3079 MockWiFiServiceRefPtr service = MakeMockService(kSecurityNone);
Wade Guthriedf6d61b2013-07-17 11:43:55 -07003080 InitiateConnect(service);
3081 VerifyScanState(WiFi::kScanIdle, WiFi::kScanMethodNone);
3082 EXPECT_TRUE(IsScanSessionNull());
3083}
3084
3085TEST_F(WiFiMainTest, ScanStateHandleDisconnect) {
3086 // Test for SetPendingService(NULL), condition d) Disconnect while scanning.
3087 // Start scanning.
Wade Guthrie9f4aa152013-07-29 16:03:14 -07003088 StartScan(WiFi::kScanMethodProgressive);
Wade Guthriedf6d61b2013-07-17 11:43:55 -07003089
3090 // Set the pending service.
3091 ReportScanDoneKeepScanSession();
Wade Guthriea5995cf2013-07-30 09:31:16 -07003092 ExpectScanStop();
3093 ExpectConnecting();
Ben Chanf024ef42013-09-20 14:21:38 -07003094 MockWiFiServiceRefPtr service = MakeMockService(kSecurityNone);
Wade Guthriedf6d61b2013-07-17 11:43:55 -07003095 SetPendingService(service);
3096 VerifyScanState(WiFi::kScanConnecting, WiFi::kScanMethodProgressive);
3097
3098 // Disconnect from the pending service.
Wade Guthriea5995cf2013-07-30 09:31:16 -07003099 ExpectScanIdle();
Wade Guthriedf6d61b2013-07-17 11:43:55 -07003100 EXPECT_CALL(*metrics(), NotifyDeviceScanFinished(_)).Times(0);
3101 EXPECT_CALL(*metrics(), SendEnumToUMA(Metrics::kMetricScanResult, _, _)).
3102 Times(0);
3103 ReportCurrentBSSChanged(WPASupplicant::kCurrentBSSNull);
3104 VerifyScanState(WiFi::kScanIdle, WiFi::kScanMethodNone);
3105}
3106
3107TEST_F(WiFiMainTest, ConnectWhileNotScanning) {
Wade Guthriedf6d61b2013-07-17 11:43:55 -07003108 // Setup WiFi but terminate scan.
Ben Chanf024ef42013-09-20 14:21:38 -07003109 EXPECT_CALL(*adaptor_, EmitBoolChanged(kPoweredProperty, _)).
Wade Guthriea5995cf2013-07-30 09:31:16 -07003110 Times(AnyNumber());
3111
3112 ExpectScanStart(kFirstProgressiveScanBatch, WiFi::kScanMethodProgressive);
Wade Guthriedf6d61b2013-07-17 11:43:55 -07003113 StartWiFi();
Wade Guthriea5995cf2013-07-30 09:31:16 -07003114 dispatcher_.DispatchPendingEvents();
3115
3116 ExpectScanStop();
3117 ExpectFoundNothing();
Wade Guthriedf6d61b2013-07-17 11:43:55 -07003118 ReportScanDone();
3119 dispatcher_.DispatchPendingEvents();
Wade Guthriea5995cf2013-07-30 09:31:16 -07003120 VerifyScanState(WiFi::kScanIdle, WiFi::kScanMethodNone);
Wade Guthriedf6d61b2013-07-17 11:43:55 -07003121
3122 // Connecting.
Wade Guthriea5995cf2013-07-30 09:31:16 -07003123 ExpectConnecting();
Wade Guthriedf6d61b2013-07-17 11:43:55 -07003124 EXPECT_CALL(*metrics(), NotifyDeviceScanStarted(_)).Times(0);
3125 WiFiEndpointRefPtr endpoint;
3126 ::DBus::Path bss_path;
Wade Guthriea5995cf2013-07-30 09:31:16 -07003127 NiceScopedMockLog log;
Wade Guthriedf6d61b2013-07-17 11:43:55 -07003128 ScopeLogger::GetInstance()->EnableScopesByName("wifi");
3129 ScopeLogger::GetInstance()->set_verbose_level(10);
3130 EXPECT_CALL(log, Log(_, _, _)).Times(AnyNumber());
3131 EXPECT_CALL(log, Log(_, _, HasSubstr("-> TRANSITION_TO_CONNECTING"))).
3132 Times(0);
3133 EXPECT_CALL(log, Log(_, _, HasSubstr("-> CONNECTING (not scan related)")));
3134 MockWiFiServiceRefPtr service =
3135 SetupConnectingService(DBus::Path(), &endpoint, &bss_path);
3136
3137 // Connected.
Wade Guthriea5995cf2013-07-30 09:31:16 -07003138 ExpectConnected();
Wade Guthriedf6d61b2013-07-17 11:43:55 -07003139 EXPECT_CALL(log, Log(_, _, HasSubstr("-> CONNECTED (not scan related")));
3140 ReportCurrentBSSChanged(bss_path);
3141 ScopeLogger::GetInstance()->set_verbose_level(0);
3142 ScopeLogger::GetInstance()->EnableScopesByName("-wifi");
3143 VerifyScanState(WiFi::kScanIdle, WiFi::kScanMethodNone);
3144}
3145
Paul Stewart3bdd3aa2013-08-20 13:20:54 -07003146TEST_F(WiFiMainTest, BackgroundScan) {
3147 StartWiFi();
3148 SetupConnectedService(DBus::Path(), NULL, NULL);
3149 VerifyScanState(WiFi::kScanIdle, WiFi::kScanMethodNone);
3150
3151 EXPECT_CALL(*GetSupplicantInterfaceProxy(), Scan(_)).Times(1);
3152 TriggerFullScan();
3153 dispatcher_.DispatchPendingEvents();
3154 VerifyScanState(WiFi::kScanBackgroundScanning, WiFi::kScanMethodFull);
3155
3156 ReportScanDone();
Paul Stewart7de7e022013-08-28 09:42:50 -07003157 EXPECT_CALL(*manager(), OnDeviceGeolocationInfoUpdated(_));
Paul Stewart3bdd3aa2013-08-20 13:20:54 -07003158 dispatcher_.DispatchPendingEvents(); // Launch UpdateScanStateAfterScanDone
3159 VerifyScanState(WiFi::kScanIdle, WiFi::kScanMethodNone);
3160}
3161
Chris Masone853b81b2011-06-24 14:11:41 -07003162} // namespace shill