mukesh agrawal | 8a3188d | 2011-12-01 20:56:44 +0000 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium OS Authors. All rights reserved. |
Chris Masone | 853b81b | 2011-06-24 14:11:41 -0700 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #include "shill/wifi.h" |
| 6 | |
mukesh agrawal | f2f68a5 | 2011-09-01 12:15:48 -0700 | [diff] [blame] | 7 | #include <netinet/ether.h> |
| 8 | #include <linux/if.h> |
mukesh agrawal | 5c4dd0b | 2011-09-14 13:53:14 -0700 | [diff] [blame] | 9 | #include <sys/socket.h> |
| 10 | #include <linux/netlink.h> // Needs typedefs from sys/socket.h. |
mukesh agrawal | f2f68a5 | 2011-09-01 12:15:48 -0700 | [diff] [blame] | 11 | |
Chris Masone | 853b81b | 2011-06-24 14:11:41 -0700 | [diff] [blame] | 12 | #include <map> |
Paul Stewart | a41e38d | 2011-11-11 07:47:29 -0800 | [diff] [blame] | 13 | #include <set> |
Chris Masone | 853b81b | 2011-06-24 14:11:41 -0700 | [diff] [blame] | 14 | #include <string> |
| 15 | #include <vector> |
| 16 | |
mukesh agrawal | 7ec7131 | 2011-11-10 02:08:26 +0000 | [diff] [blame] | 17 | #include <base/memory/ref_counted.h> |
mukesh agrawal | dc42bb3 | 2011-07-28 10:40:26 -0700 | [diff] [blame] | 18 | #include <base/memory/scoped_ptr.h> |
Paul Stewart | a41e38d | 2011-11-11 07:47:29 -0800 | [diff] [blame] | 19 | #include <base/stringprintf.h> |
mukesh agrawal | 3195024 | 2011-07-14 11:53:38 -0700 | [diff] [blame] | 20 | #include <base/string_number_conversions.h> |
| 21 | #include <base/string_util.h> |
Chris Masone | 853b81b | 2011-06-24 14:11:41 -0700 | [diff] [blame] | 22 | #include <chromeos/dbus/service_constants.h> |
mukesh agrawal | 1830fa1 | 2011-09-26 14:31:40 -0700 | [diff] [blame] | 23 | #include <dbus-c++/dbus.h> |
Chris Masone | 7156c92 | 2011-08-23 20:36:21 -0700 | [diff] [blame] | 24 | #include <gmock/gmock.h> |
Chris Masone | 2ae797d | 2011-08-23 20:41:00 -0700 | [diff] [blame] | 25 | #include <gtest/gtest.h> |
Chris Masone | 853b81b | 2011-06-24 14:11:41 -0700 | [diff] [blame] | 26 | |
| 27 | #include "shill/dbus_adaptor.h" |
Paul Stewart | 26b327e | 2011-10-19 11:38:09 -0700 | [diff] [blame] | 28 | #include "shill/event_dispatcher.h" |
mukesh agrawal | 8ede052 | 2011-10-03 14:57:44 -0700 | [diff] [blame] | 29 | #include "shill/ieee80211.h" |
mukesh agrawal | 7a4e400 | 2011-09-06 11:26:05 -0700 | [diff] [blame] | 30 | #include "shill/key_value_store.h" |
Chris Masone | 853b81b | 2011-06-24 14:11:41 -0700 | [diff] [blame] | 31 | #include "shill/manager.h" |
Chris Masone | 853b81b | 2011-06-24 14:11:41 -0700 | [diff] [blame] | 32 | #include "shill/mock_device.h" |
mukesh agrawal | c01f398 | 2012-01-24 13:48:39 -0800 | [diff] [blame] | 33 | #include "shill/mock_device_info.h" |
mukesh agrawal | f2f68a5 | 2011-09-01 12:15:48 -0700 | [diff] [blame] | 34 | #include "shill/mock_dhcp_config.h" |
| 35 | #include "shill/mock_dhcp_provider.h" |
Paul Stewart | e369ece | 2012-05-22 09:11:03 -0700 | [diff] [blame] | 36 | #include "shill/mock_event_dispatcher.h" |
mukesh agrawal | cf24a24 | 2012-05-21 16:46:11 -0700 | [diff] [blame] | 37 | #include "shill/mock_log.h" |
Chris Masone | 2ae797d | 2011-08-23 20:41:00 -0700 | [diff] [blame] | 38 | #include "shill/mock_manager.h" |
Thieu Le | 3426c8f | 2012-01-11 17:35:11 -0800 | [diff] [blame] | 39 | #include "shill/mock_metrics.h" |
Gary Morain | ac1bdb4 | 2012-02-16 17:42:29 -0800 | [diff] [blame] | 40 | #include "shill/mock_power_manager.h" |
| 41 | #include "shill/mock_power_manager_proxy.h" |
Paul Stewart | 10ccbb3 | 2012-04-26 15:59:30 -0700 | [diff] [blame] | 42 | #include "shill/mock_profile.h" |
mukesh agrawal | 5c4dd0b | 2011-09-14 13:53:14 -0700 | [diff] [blame] | 43 | #include "shill/mock_rtnl_handler.h" |
Paul Stewart | a41e38d | 2011-11-11 07:47:29 -0800 | [diff] [blame] | 44 | #include "shill/mock_store.h" |
mukesh agrawal | b20776f | 2012-02-10 16:00:36 -0800 | [diff] [blame] | 45 | #include "shill/mock_supplicant_bss_proxy.h" |
mukesh agrawal | 3195024 | 2011-07-14 11:53:38 -0700 | [diff] [blame] | 46 | #include "shill/mock_supplicant_interface_proxy.h" |
| 47 | #include "shill/mock_supplicant_process_proxy.h" |
mukesh agrawal | 5c05b29 | 2012-03-07 10:12:52 -0800 | [diff] [blame] | 48 | #include "shill/mock_time.h" |
mukesh agrawal | 7ec7131 | 2011-11-10 02:08:26 +0000 | [diff] [blame] | 49 | #include "shill/mock_wifi_service.h" |
mukesh agrawal | dc42bb3 | 2011-07-28 10:40:26 -0700 | [diff] [blame] | 50 | #include "shill/nice_mock_control.h" |
Chris Masone | 853b81b | 2011-06-24 14:11:41 -0700 | [diff] [blame] | 51 | #include "shill/property_store_unittest.h" |
mukesh agrawal | 3195024 | 2011-07-14 11:53:38 -0700 | [diff] [blame] | 52 | #include "shill/proxy_factory.h" |
| 53 | #include "shill/wifi_endpoint.h" |
mukesh agrawal | 3239932 | 2011-09-01 10:53:43 -0700 | [diff] [blame] | 54 | #include "shill/wifi_service.h" |
Paul Stewart | ced6a0b | 2011-11-08 15:32:04 -0800 | [diff] [blame] | 55 | #include "shill/wpa_supplicant.h" |
Chris Masone | 853b81b | 2011-06-24 14:11:41 -0700 | [diff] [blame] | 56 | |
Gary Morain | ac1bdb4 | 2012-02-16 17:42:29 -0800 | [diff] [blame] | 57 | |
Chris Masone | 853b81b | 2011-06-24 14:11:41 -0700 | [diff] [blame] | 58 | using std::map; |
Paul Stewart | a41e38d | 2011-11-11 07:47:29 -0800 | [diff] [blame] | 59 | using std::set; |
Chris Masone | 853b81b | 2011-06-24 14:11:41 -0700 | [diff] [blame] | 60 | using std::string; |
| 61 | using std::vector; |
| 62 | using ::testing::_; |
mukesh agrawal | 3195024 | 2011-07-14 11:53:38 -0700 | [diff] [blame] | 63 | using ::testing::AnyNumber; |
| 64 | using ::testing::DefaultValue; |
Paul Stewart | a41e38d | 2011-11-11 07:47:29 -0800 | [diff] [blame] | 65 | using ::testing::DoAll; |
mukesh agrawal | cf24a24 | 2012-05-21 16:46:11 -0700 | [diff] [blame] | 66 | using ::testing::EndsWith; |
mukesh agrawal | 3195024 | 2011-07-14 11:53:38 -0700 | [diff] [blame] | 67 | using ::testing::InSequence; |
mukesh agrawal | b20776f | 2012-02-10 16:00:36 -0800 | [diff] [blame] | 68 | using ::testing::Invoke; |
mukesh agrawal | 7ec7131 | 2011-11-10 02:08:26 +0000 | [diff] [blame] | 69 | using ::testing::Mock; |
mukesh agrawal | dc42bb3 | 2011-07-28 10:40:26 -0700 | [diff] [blame] | 70 | using ::testing::NiceMock; |
Chris Masone | 853b81b | 2011-06-24 14:11:41 -0700 | [diff] [blame] | 71 | using ::testing::Return; |
Gary Morain | ac1bdb4 | 2012-02-16 17:42:29 -0800 | [diff] [blame] | 72 | using ::testing::SaveArg; |
Paul Stewart | a41e38d | 2011-11-11 07:47:29 -0800 | [diff] [blame] | 73 | using ::testing::SetArgumentPointee; |
| 74 | using ::testing::StrEq; |
| 75 | using ::testing::StrictMock; |
Chris Masone | 853b81b | 2011-06-24 14:11:41 -0700 | [diff] [blame] | 76 | using ::testing::Test; |
mukesh agrawal | 3195024 | 2011-07-14 11:53:38 -0700 | [diff] [blame] | 77 | using ::testing::Throw; |
mukesh agrawal | 8ede052 | 2011-10-03 14:57:44 -0700 | [diff] [blame] | 78 | using ::testing::Values; |
Chris Masone | 853b81b | 2011-06-24 14:11:41 -0700 | [diff] [blame] | 79 | |
| 80 | namespace shill { |
| 81 | |
mukesh agrawal | 3195024 | 2011-07-14 11:53:38 -0700 | [diff] [blame] | 82 | class WiFiPropertyTest : public PropertyStoreTest { |
Chris Masone | 853b81b | 2011-06-24 14:11:41 -0700 | [diff] [blame] | 83 | public: |
mukesh agrawal | 3195024 | 2011-07-14 11:53:38 -0700 | [diff] [blame] | 84 | WiFiPropertyTest() |
Thieu Le | 3426c8f | 2012-01-11 17:35:11 -0800 | [diff] [blame] | 85 | : device_(new WiFi(control_interface(), |
| 86 | NULL, NULL, NULL, "wifi", "", 0)) { |
Chris Masone | 853b81b | 2011-06-24 14:11:41 -0700 | [diff] [blame] | 87 | } |
mukesh agrawal | 3195024 | 2011-07-14 11:53:38 -0700 | [diff] [blame] | 88 | virtual ~WiFiPropertyTest() {} |
Chris Masone | 853b81b | 2011-06-24 14:11:41 -0700 | [diff] [blame] | 89 | |
| 90 | protected: |
mukesh agrawal | 8abd2f6 | 2012-01-30 14:56:14 -0800 | [diff] [blame] | 91 | WiFiRefPtr device_; |
Chris Masone | 853b81b | 2011-06-24 14:11:41 -0700 | [diff] [blame] | 92 | }; |
| 93 | |
mukesh agrawal | 3195024 | 2011-07-14 11:53:38 -0700 | [diff] [blame] | 94 | TEST_F(WiFiPropertyTest, Contains) { |
mukesh agrawal | de29fa8 | 2011-09-16 16:16:36 -0700 | [diff] [blame] | 95 | EXPECT_TRUE(device_->store().Contains(flimflam::kNameProperty)); |
| 96 | EXPECT_FALSE(device_->store().Contains("")); |
Chris Masone | 853b81b | 2011-06-24 14:11:41 -0700 | [diff] [blame] | 97 | } |
| 98 | |
mukesh agrawal | 6bb9e7c | 2012-01-30 14:57:54 -0800 | [diff] [blame] | 99 | TEST_F(WiFiPropertyTest, SetProperty) { |
Chris Masone | a8a2c25 | 2011-06-27 22:16:30 -0700 | [diff] [blame] | 100 | { |
| 101 | ::DBus::Error error; |
mukesh agrawal | 6bb9e7c | 2012-01-30 14:57:54 -0800 | [diff] [blame] | 102 | EXPECT_TRUE(DBusAdaptor::SetProperty( |
mukesh agrawal | de29fa8 | 2011-09-16 16:16:36 -0700 | [diff] [blame] | 103 | device_->mutable_store(), |
Chris Masone | a8a2c25 | 2011-06-27 22:16:30 -0700 | [diff] [blame] | 104 | flimflam::kBgscanSignalThresholdProperty, |
| 105 | PropertyStoreTest::kInt32V, |
| 106 | &error)); |
| 107 | } |
| 108 | { |
| 109 | ::DBus::Error error; |
mukesh agrawal | 6bb9e7c | 2012-01-30 14:57:54 -0800 | [diff] [blame] | 110 | EXPECT_TRUE(DBusAdaptor::SetProperty(device_->mutable_store(), |
| 111 | flimflam::kScanIntervalProperty, |
| 112 | PropertyStoreTest::kUint16V, |
| 113 | &error)); |
Chris Masone | a8a2c25 | 2011-06-27 22:16:30 -0700 | [diff] [blame] | 114 | } |
Chris Masone | 853b81b | 2011-06-24 14:11:41 -0700 | [diff] [blame] | 115 | // Ensure that an attempt to write a R/O property returns InvalidArgs error. |
Chris Masone | a8a2c25 | 2011-06-27 22:16:30 -0700 | [diff] [blame] | 116 | { |
| 117 | ::DBus::Error error; |
mukesh agrawal | 6bb9e7c | 2012-01-30 14:57:54 -0800 | [diff] [blame] | 118 | EXPECT_FALSE(DBusAdaptor::SetProperty(device_->mutable_store(), |
| 119 | flimflam::kScanningProperty, |
| 120 | PropertyStoreTest::kBoolV, |
| 121 | &error)); |
Chris Masone | 9d77993 | 2011-08-25 16:33:41 -0700 | [diff] [blame] | 122 | EXPECT_EQ(invalid_args(), error.name()); |
Chris Masone | a8a2c25 | 2011-06-27 22:16:30 -0700 | [diff] [blame] | 123 | } |
Chris Masone | 853b81b | 2011-06-24 14:11:41 -0700 | [diff] [blame] | 124 | |
mukesh agrawal | 4d0401c | 2012-01-06 16:05:31 -0800 | [diff] [blame] | 125 | { |
| 126 | ::DBus::Error error; |
mukesh agrawal | 6bb9e7c | 2012-01-30 14:57:54 -0800 | [diff] [blame] | 127 | EXPECT_TRUE(DBusAdaptor::SetProperty( |
mukesh agrawal | 4d0401c | 2012-01-06 16:05:31 -0800 | [diff] [blame] | 128 | device_->mutable_store(), |
| 129 | flimflam::kBgscanMethodProperty, |
| 130 | DBusAdaptor::StringToVariant( |
| 131 | wpa_supplicant::kNetworkBgscanMethodSimple), |
| 132 | &error)); |
| 133 | } |
| 134 | |
| 135 | { |
| 136 | ::DBus::Error error; |
mukesh agrawal | 6bb9e7c | 2012-01-30 14:57:54 -0800 | [diff] [blame] | 137 | EXPECT_FALSE(DBusAdaptor::SetProperty( |
mukesh agrawal | 4d0401c | 2012-01-06 16:05:31 -0800 | [diff] [blame] | 138 | device_->mutable_store(), |
| 139 | flimflam::kBgscanMethodProperty, |
| 140 | DBusAdaptor::StringToVariant("not a real scan method"), |
| 141 | &error)); |
| 142 | } |
| 143 | } |
| 144 | |
mukesh agrawal | 8abd2f6 | 2012-01-30 14:56:14 -0800 | [diff] [blame] | 145 | TEST_F(WiFiPropertyTest, ClearDerivedProperty) { |
| 146 | EXPECT_NE(wpa_supplicant::kNetworkBgscanMethodLearn, |
| 147 | WiFi::kDefaultBgscanMethod); |
| 148 | EXPECT_EQ(WiFi::kDefaultBgscanMethod, device_->bgscan_method_); |
| 149 | |
| 150 | ::DBus::Error error; |
mukesh agrawal | 6bb9e7c | 2012-01-30 14:57:54 -0800 | [diff] [blame] | 151 | EXPECT_TRUE(DBusAdaptor::SetProperty( |
mukesh agrawal | 8abd2f6 | 2012-01-30 14:56:14 -0800 | [diff] [blame] | 152 | device_->mutable_store(), |
| 153 | flimflam::kBgscanMethodProperty, |
| 154 | DBusAdaptor::StringToVariant( |
| 155 | wpa_supplicant::kNetworkBgscanMethodLearn), |
| 156 | &error)); |
| 157 | EXPECT_EQ(wpa_supplicant::kNetworkBgscanMethodLearn, device_->bgscan_method_); |
| 158 | |
| 159 | EXPECT_TRUE(DBusAdaptor::ClearProperty( |
| 160 | device_->mutable_store(), flimflam::kBgscanMethodProperty, &error)); |
| 161 | EXPECT_EQ(WiFi::kDefaultBgscanMethod, device_->bgscan_method_); |
| 162 | } |
| 163 | |
Paul Stewart | e369ece | 2012-05-22 09:11:03 -0700 | [diff] [blame] | 164 | class WiFiObjectTest : public ::testing::TestWithParam<string> { |
mukesh agrawal | 3195024 | 2011-07-14 11:53:38 -0700 | [diff] [blame] | 165 | public: |
Paul Stewart | e369ece | 2012-05-22 09:11:03 -0700 | [diff] [blame] | 166 | WiFiObjectTest(EventDispatcher *dispatcher) |
| 167 | : event_dispatcher_(dispatcher), |
| 168 | manager_(&control_interface_, NULL, &metrics_, &glib_), |
| 169 | device_info_(&control_interface_, dispatcher, &metrics_, &manager_), |
Chris Masone | 626719f | 2011-08-18 16:58:48 -0700 | [diff] [blame] | 170 | wifi_(new WiFi(&control_interface_, |
Paul Stewart | e369ece | 2012-05-22 09:11:03 -0700 | [diff] [blame] | 171 | dispatcher, |
Thieu Le | 3426c8f | 2012-01-11 17:35:11 -0800 | [diff] [blame] | 172 | &metrics_, |
Chris Masone | 626719f | 2011-08-18 16:58:48 -0700 | [diff] [blame] | 173 | &manager_, |
| 174 | kDeviceName, |
| 175 | kDeviceAddress, |
| 176 | 0)), |
mukesh agrawal | dc42bb3 | 2011-07-28 10:40:26 -0700 | [diff] [blame] | 177 | supplicant_process_proxy_(new NiceMock<MockSupplicantProcessProxy>()), |
| 178 | supplicant_interface_proxy_( |
| 179 | new NiceMock<MockSupplicantInterfaceProxy>(wifi_)), |
mukesh agrawal | b20776f | 2012-02-10 16:00:36 -0800 | [diff] [blame] | 180 | supplicant_bss_proxy_( |
| 181 | new NiceMock<MockSupplicantBSSProxy>()), |
mukesh agrawal | f2f68a5 | 2011-09-01 12:15:48 -0700 | [diff] [blame] | 182 | dhcp_config_(new MockDHCPConfig(&control_interface_, |
Paul Stewart | d408fdf | 2012-05-07 17:15:57 -0700 | [diff] [blame] | 183 | kDeviceName)), |
Gary Morain | ac1bdb4 | 2012-02-16 17:42:29 -0800 | [diff] [blame] | 184 | proxy_factory_(this), |
Eric Shienbrood | 3e20a23 | 2012-02-16 11:35:56 -0500 | [diff] [blame] | 185 | power_manager_(new MockPowerManager(&proxy_factory_)) { |
mukesh agrawal | 3195024 | 2011-07-14 11:53:38 -0700 | [diff] [blame] | 186 | ::testing::DefaultValue< ::DBus::Path>::Set("/default/path"); |
mukesh agrawal | 4eb4d78 | 2011-12-05 17:34:37 +0000 | [diff] [blame] | 187 | // Except for WiFiServices created via WiFi::GetService, we expect |
| 188 | // that any WiFiService has been registered with the Manager. So |
| 189 | // default Manager.HasService to true, to make the common case |
| 190 | // easy. |
| 191 | ON_CALL(manager_, HasService(_)). |
| 192 | WillByDefault(Return(true)); |
mukesh agrawal | c01f398 | 2012-01-24 13:48:39 -0800 | [diff] [blame] | 193 | |
Paul Stewart | d408fdf | 2012-05-07 17:15:57 -0700 | [diff] [blame] | 194 | ON_CALL(dhcp_provider_, CreateConfig(_, _, _, _)). |
mukesh agrawal | c01f398 | 2012-01-24 13:48:39 -0800 | [diff] [blame] | 195 | WillByDefault(Return(dhcp_config_)); |
| 196 | ON_CALL(*dhcp_config_.get(), RequestIP()). |
| 197 | WillByDefault(Return(true)); |
Gary Morain | ac1bdb4 | 2012-02-16 17:42:29 -0800 | [diff] [blame] | 198 | |
| 199 | // |manager_| takes ownership of |power_manager_|. |
| 200 | manager_.set_power_manager(power_manager_); |
mukesh agrawal | 5c05b29 | 2012-03-07 10:12:52 -0800 | [diff] [blame] | 201 | |
| 202 | wifi_->time_ = &time_; |
mukesh agrawal | 3195024 | 2011-07-14 11:53:38 -0700 | [diff] [blame] | 203 | } |
mukesh agrawal | 5c4dd0b | 2011-09-14 13:53:14 -0700 | [diff] [blame] | 204 | |
| 205 | virtual void SetUp() { |
Darin Petkov | ab565bb | 2011-10-06 02:55:51 -0700 | [diff] [blame] | 206 | wifi_->proxy_factory_ = &proxy_factory_; |
mukesh agrawal | 5c4dd0b | 2011-09-14 13:53:14 -0700 | [diff] [blame] | 207 | static_cast<Device *>(wifi_)->rtnl_handler_ = &rtnl_handler_; |
| 208 | wifi_->set_dhcp_provider(&dhcp_provider_); |
mukesh agrawal | c01f398 | 2012-01-24 13:48:39 -0800 | [diff] [blame] | 209 | ON_CALL(manager_, device_info()). |
| 210 | WillByDefault(Return(&device_info_)); |
Chris Masone | b9c0059 | 2011-10-06 13:10:39 -0700 | [diff] [blame] | 211 | EXPECT_CALL(manager_, DeregisterService(_)).Times(AnyNumber()); |
mukesh agrawal | b20776f | 2012-02-10 16:00:36 -0800 | [diff] [blame] | 212 | EXPECT_CALL(*supplicant_bss_proxy_, Die()).Times(AnyNumber()); |
mukesh agrawal | 5c4dd0b | 2011-09-14 13:53:14 -0700 | [diff] [blame] | 213 | } |
| 214 | |
| 215 | virtual void TearDown() { |
Gary Morain | d9f893f | 2012-02-06 10:03:40 -0800 | [diff] [blame] | 216 | EXPECT_CALL(*manager(), UpdateService(_)).Times(AnyNumber()); |
Gary Morain | 9100112 | 2012-02-29 16:22:26 -0800 | [diff] [blame] | 217 | EXPECT_CALL(*power_manager_, RemoveStateChangeCallback(wifi_->UniqueName())) |
| 218 | .Times(AnyNumber()); |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 219 | wifi_->SelectService(NULL); |
mukesh agrawal | b20776f | 2012-02-10 16:00:36 -0800 | [diff] [blame] | 220 | if (supplicant_bss_proxy_.get()) { |
| 221 | EXPECT_CALL(*supplicant_bss_proxy_, Die()); |
| 222 | } |
Darin Petkov | ab565bb | 2011-10-06 02:55:51 -0700 | [diff] [blame] | 223 | wifi_->proxy_factory_ = NULL; |
mukesh agrawal | 3195024 | 2011-07-14 11:53:38 -0700 | [diff] [blame] | 224 | // must Stop WiFi instance, to clear its list of services. |
| 225 | // otherwise, the WiFi instance will not be deleted. (because |
| 226 | // services reference a WiFi instance, creating a cycle.) |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 227 | wifi_->Stop(NULL, ResultCallback()); |
mukesh agrawal | 5c4dd0b | 2011-09-14 13:53:14 -0700 | [diff] [blame] | 228 | wifi_->set_dhcp_provider(NULL); |
mukesh agrawal | f2f68a5 | 2011-09-01 12:15:48 -0700 | [diff] [blame] | 229 | } |
| 230 | |
mukesh agrawal | 3195024 | 2011-07-14 11:53:38 -0700 | [diff] [blame] | 231 | protected: |
mukesh agrawal | 7ec7131 | 2011-11-10 02:08:26 +0000 | [diff] [blame] | 232 | typedef scoped_refptr<MockWiFiService> MockWiFiServiceRefPtr; |
| 233 | |
mukesh agrawal | 3195024 | 2011-07-14 11:53:38 -0700 | [diff] [blame] | 234 | class TestProxyFactory : public ProxyFactory { |
| 235 | public: |
Paul Stewart | e369ece | 2012-05-22 09:11:03 -0700 | [diff] [blame] | 236 | explicit TestProxyFactory(WiFiObjectTest *test); |
mukesh agrawal | 3195024 | 2011-07-14 11:53:38 -0700 | [diff] [blame] | 237 | |
| 238 | virtual SupplicantProcessProxyInterface *CreateSupplicantProcessProxy( |
mukesh agrawal | 1830fa1 | 2011-09-26 14:31:40 -0700 | [diff] [blame] | 239 | const char */*dbus_path*/, const char */*dbus_addr*/) { |
mukesh agrawal | dc42bb3 | 2011-07-28 10:40:26 -0700 | [diff] [blame] | 240 | return test_->supplicant_process_proxy_.release(); |
mukesh agrawal | 3195024 | 2011-07-14 11:53:38 -0700 | [diff] [blame] | 241 | } |
| 242 | |
| 243 | virtual SupplicantInterfaceProxyInterface *CreateSupplicantInterfaceProxy( |
mukesh agrawal | 1830fa1 | 2011-09-26 14:31:40 -0700 | [diff] [blame] | 244 | const WiFiRefPtr &/*wifi*/, |
| 245 | const DBus::Path &/*object_path*/, |
| 246 | const char */*dbus_addr*/) { |
mukesh agrawal | dc42bb3 | 2011-07-28 10:40:26 -0700 | [diff] [blame] | 247 | return test_->supplicant_interface_proxy_.release(); |
mukesh agrawal | 3195024 | 2011-07-14 11:53:38 -0700 | [diff] [blame] | 248 | } |
| 249 | |
mukesh agrawal | b20776f | 2012-02-10 16:00:36 -0800 | [diff] [blame] | 250 | MOCK_METHOD3(CreateSupplicantBSSProxy, |
| 251 | SupplicantBSSProxyInterface *( |
| 252 | WiFiEndpoint *wifi_endpoint, |
| 253 | const DBus::Path &object_path, |
| 254 | const char *dbus_addr)); |
| 255 | |
Gary Morain | ac1bdb4 | 2012-02-16 17:42:29 -0800 | [diff] [blame] | 256 | virtual PowerManagerProxyInterface *CreatePowerManagerProxy( |
| 257 | PowerManagerProxyDelegate */*delegate*/) { |
| 258 | return new MockPowerManagerProxy(); |
| 259 | } |
| 260 | |
mukesh agrawal | 3195024 | 2011-07-14 11:53:38 -0700 | [diff] [blame] | 261 | private: |
mukesh agrawal | b20776f | 2012-02-10 16:00:36 -0800 | [diff] [blame] | 262 | SupplicantBSSProxyInterface *CreateSupplicantBSSProxyInternal( |
| 263 | WiFiEndpoint */*wifi_endpoint*/, |
| 264 | const DBus::Path &/*object_path*/, |
| 265 | const char */*dbus_addr*/) { |
| 266 | return test_->supplicant_bss_proxy_.release(); |
| 267 | } |
| 268 | |
Paul Stewart | e369ece | 2012-05-22 09:11:03 -0700 | [diff] [blame] | 269 | WiFiObjectTest *test_; |
mukesh agrawal | 3195024 | 2011-07-14 11:53:38 -0700 | [diff] [blame] | 270 | }; |
| 271 | |
mukesh agrawal | b66c646 | 2012-05-07 11:45:25 -0700 | [diff] [blame] | 272 | void CancelScanTimer() { |
| 273 | wifi_->scan_timer_callback_.Cancel(); |
| 274 | } |
mukesh agrawal | 1590839 | 2011-11-16 18:29:25 +0000 | [diff] [blame] | 275 | WiFiServiceRefPtr CreateServiceForEndpoint(const WiFiEndpoint &endpoint) { |
| 276 | bool hidden_ssid = false; |
| 277 | return wifi_->CreateServiceForEndpoint(endpoint, hidden_ssid); |
| 278 | } |
mukesh agrawal | b66c646 | 2012-05-07 11:45:25 -0700 | [diff] [blame] | 279 | void FireScanTimer() { |
| 280 | wifi_->ScanTimerHandler(); |
| 281 | } |
mukesh agrawal | 1590839 | 2011-11-16 18:29:25 +0000 | [diff] [blame] | 282 | const WiFiServiceRefPtr &GetCurrentService() { |
| 283 | return wifi_->current_service_; |
| 284 | } |
Paul Stewart | bc6e739 | 2012-05-24 07:07:48 -0700 | [diff] [blame] | 285 | void SetCurrentService(const WiFiServiceRefPtr &service) { |
| 286 | wifi_->current_service_ = service; |
| 287 | } |
mukesh agrawal | 3195024 | 2011-07-14 11:53:38 -0700 | [diff] [blame] | 288 | const WiFi::EndpointMap &GetEndpointMap() { |
mukesh agrawal | 165e614 | 2011-11-22 02:22:56 +0000 | [diff] [blame] | 289 | return wifi_->endpoint_by_rpcid_; |
mukesh agrawal | 3195024 | 2011-07-14 11:53:38 -0700 | [diff] [blame] | 290 | } |
mukesh agrawal | 1590839 | 2011-11-16 18:29:25 +0000 | [diff] [blame] | 291 | const WiFiServiceRefPtr &GetPendingService() { |
| 292 | return wifi_->pending_service_; |
| 293 | } |
mukesh agrawal | b66c646 | 2012-05-07 11:45:25 -0700 | [diff] [blame] | 294 | const base::CancelableClosure &GetScanTimer() { |
| 295 | return wifi_->scan_timer_callback_; |
| 296 | } |
mukesh agrawal | 165e614 | 2011-11-22 02:22:56 +0000 | [diff] [blame] | 297 | const vector<WiFiServiceRefPtr> &GetServices() { |
| 298 | return wifi_->services_; |
mukesh agrawal | 3195024 | 2011-07-14 11:53:38 -0700 | [diff] [blame] | 299 | } |
| 300 | // note: the tests need the proxies referenced by WiFi (not the |
Paul Stewart | e369ece | 2012-05-22 09:11:03 -0700 | [diff] [blame] | 301 | // proxies instantiated by WiFiObjectTest), to ensure that WiFi |
mukesh agrawal | 3195024 | 2011-07-14 11:53:38 -0700 | [diff] [blame] | 302 | // sets up its proxies correctly. |
| 303 | SupplicantProcessProxyInterface *GetSupplicantProcessProxy() { |
| 304 | return wifi_->supplicant_process_proxy_.get(); |
| 305 | } |
mukesh agrawal | 8a3188d | 2011-12-01 20:56:44 +0000 | [diff] [blame] | 306 | MockSupplicantInterfaceProxy *GetSupplicantInterfaceProxy() { |
| 307 | return dynamic_cast<MockSupplicantInterfaceProxy *>( |
| 308 | wifi_->supplicant_interface_proxy_.get()); |
mukesh agrawal | 3195024 | 2011-07-14 11:53:38 -0700 | [diff] [blame] | 309 | } |
mukesh agrawal | 7ec7131 | 2011-11-10 02:08:26 +0000 | [diff] [blame] | 310 | const string &GetSupplicantState() { |
| 311 | return wifi_->supplicant_state_; |
| 312 | } |
| 313 | void InitiateConnect(WiFiServiceRefPtr service) { |
mukesh agrawal | 6e27777 | 2011-09-29 15:04:23 -0700 | [diff] [blame] | 314 | map<string, ::DBus::Variant> params; |
| 315 | wifi_->ConnectTo(service, params); |
mukesh agrawal | 3195024 | 2011-07-14 11:53:38 -0700 | [diff] [blame] | 316 | } |
mukesh agrawal | 0ed0f2e | 2011-12-05 20:36:17 +0000 | [diff] [blame] | 317 | void InitiateDisconnect(WiFiServiceRefPtr service) { |
| 318 | wifi_->DisconnectFrom(service); |
| 319 | } |
mukesh agrawal | 1590839 | 2011-11-16 18:29:25 +0000 | [diff] [blame] | 320 | WiFiEndpointRefPtr MakeEndpoint(const string &ssid, const string &bssid) { |
mukesh agrawal | e1d90e9 | 2012-02-15 17:36:08 -0800 | [diff] [blame] | 321 | return WiFiEndpoint::MakeOpenEndpoint( |
| 322 | &proxy_factory_, NULL, ssid, bssid, 0, 0); |
mukesh agrawal | 1590839 | 2011-11-16 18:29:25 +0000 | [diff] [blame] | 323 | } |
mukesh agrawal | cf24a24 | 2012-05-21 16:46:11 -0700 | [diff] [blame] | 324 | MockWiFiServiceRefPtr MakeMockService(const std::string &security) { |
mukesh agrawal | 7ec7131 | 2011-11-10 02:08:26 +0000 | [diff] [blame] | 325 | vector<uint8_t> ssid(1, 'a'); |
| 326 | return new MockWiFiService( |
| 327 | &control_interface_, |
Paul Stewart | e369ece | 2012-05-22 09:11:03 -0700 | [diff] [blame] | 328 | event_dispatcher_, |
Thieu Le | 3426c8f | 2012-01-11 17:35:11 -0800 | [diff] [blame] | 329 | &metrics_, |
mukesh agrawal | 7ec7131 | 2011-11-10 02:08:26 +0000 | [diff] [blame] | 330 | &manager_, |
| 331 | wifi_, |
| 332 | ssid, |
| 333 | flimflam::kModeManaged, |
mukesh agrawal | cf24a24 | 2012-05-21 16:46:11 -0700 | [diff] [blame] | 334 | security, |
mukesh agrawal | 7ec7131 | 2011-11-10 02:08:26 +0000 | [diff] [blame] | 335 | false); |
| 336 | } |
mukesh agrawal | 261daca | 2011-12-02 18:56:56 +0000 | [diff] [blame] | 337 | void RemoveBSS(const ::DBus::Path &bss_path); |
mukesh agrawal | 3195024 | 2011-07-14 11:53:38 -0700 | [diff] [blame] | 338 | void ReportBSS(const ::DBus::Path &bss_path, |
| 339 | const string &ssid, |
| 340 | const string &bssid, |
| 341 | int16_t signal_strength, |
Thieu Le | e41a72d | 2012-02-06 20:46:51 +0000 | [diff] [blame] | 342 | uint16 frequency, |
mukesh agrawal | 3195024 | 2011-07-14 11:53:38 -0700 | [diff] [blame] | 343 | const char *mode); |
Paul Stewart | 66c8600 | 2012-01-30 18:00:52 -0800 | [diff] [blame] | 344 | void ClearCachedCredentials() { |
| 345 | wifi_->ClearCachedCredentials(); |
| 346 | } |
mukesh agrawal | c01f398 | 2012-01-24 13:48:39 -0800 | [diff] [blame] | 347 | void ReportIPConfigComplete() { |
Darin Petkov | 79d74c9 | 2012-03-07 17:20:32 +0100 | [diff] [blame] | 348 | wifi_->OnIPConfigUpdated(dhcp_config_, true); |
mukesh agrawal | c01f398 | 2012-01-24 13:48:39 -0800 | [diff] [blame] | 349 | } |
mukesh agrawal | f2f68a5 | 2011-09-01 12:15:48 -0700 | [diff] [blame] | 350 | void ReportLinkUp() { |
| 351 | wifi_->LinkEvent(IFF_LOWER_UP, IFF_LOWER_UP); |
| 352 | } |
mukesh agrawal | 3195024 | 2011-07-14 11:53:38 -0700 | [diff] [blame] | 353 | void ReportScanDone() { |
mukesh agrawal | dc42bb3 | 2011-07-28 10:40:26 -0700 | [diff] [blame] | 354 | wifi_->ScanDoneTask(); |
mukesh agrawal | 3195024 | 2011-07-14 11:53:38 -0700 | [diff] [blame] | 355 | } |
mukesh agrawal | 1590839 | 2011-11-16 18:29:25 +0000 | [diff] [blame] | 356 | void ReportCurrentBSSChanged(const string &new_bss) { |
| 357 | wifi_->CurrentBSSChanged(new_bss); |
| 358 | } |
mukesh agrawal | 7ec7131 | 2011-11-10 02:08:26 +0000 | [diff] [blame] | 359 | void ReportStateChanged(const string &new_state) { |
| 360 | wifi_->StateChanged(new_state); |
| 361 | } |
mukesh agrawal | cf24a24 | 2012-05-21 16:46:11 -0700 | [diff] [blame] | 362 | void SetPendingService(const WiFiServiceRefPtr &service) { |
| 363 | wifi_->pending_service_ = service; |
| 364 | } |
mukesh agrawal | b66c646 | 2012-05-07 11:45:25 -0700 | [diff] [blame] | 365 | void SetScanInterval(uint16_t interval_seconds) { |
| 366 | wifi_->SetScanInterval(interval_seconds, NULL); |
| 367 | } |
Paul Stewart | e369ece | 2012-05-22 09:11:03 -0700 | [diff] [blame] | 368 | uint16_t GetScanInterval() { |
| 369 | return wifi_->GetScanInterval(NULL); |
| 370 | } |
mukesh agrawal | 3195024 | 2011-07-14 11:53:38 -0700 | [diff] [blame] | 371 | void StartWiFi() { |
Gary Morain | ac1bdb4 | 2012-02-16 17:42:29 -0800 | [diff] [blame] | 372 | EXPECT_CALL(*power_manager_, AddStateChangeCallback(wifi_->UniqueName(), _)) |
| 373 | .WillOnce(SaveArg<1>(&power_state_callback_)); |
mukesh agrawal | b66c646 | 2012-05-07 11:45:25 -0700 | [diff] [blame] | 374 | wifi_->SetEnabled(true); // Start(NULL, ResultCallback()); |
mukesh agrawal | 3195024 | 2011-07-14 11:53:38 -0700 | [diff] [blame] | 375 | } |
| 376 | void StopWiFi() { |
Gary Morain | 9100112 | 2012-02-29 16:22:26 -0800 | [diff] [blame] | 377 | EXPECT_CALL(*power_manager_, |
| 378 | RemoveStateChangeCallback(wifi_->UniqueName())); |
mukesh agrawal | b66c646 | 2012-05-07 11:45:25 -0700 | [diff] [blame] | 379 | wifi_->SetEnabled(false); // Stop(NULL, ResultCallback()); |
mukesh agrawal | 3195024 | 2011-07-14 11:53:38 -0700 | [diff] [blame] | 380 | } |
mukesh agrawal | cf24a24 | 2012-05-21 16:46:11 -0700 | [diff] [blame] | 381 | WiFiServiceRefPtr GetOpenService(const char *service_type, |
| 382 | const char *ssid, |
| 383 | const char *mode, |
| 384 | Error *result) { |
| 385 | return GetServiceInner(service_type, ssid, mode, NULL, NULL, false, result); |
mukesh agrawal | 7a4e400 | 2011-09-06 11:26:05 -0700 | [diff] [blame] | 386 | } |
mukesh agrawal | cf24a24 | 2012-05-21 16:46:11 -0700 | [diff] [blame] | 387 | WiFiServiceRefPtr GetService(const char *service_type, |
| 388 | const char *ssid, |
| 389 | const char *mode, |
| 390 | const char *security, |
| 391 | const char *passphrase, |
| 392 | Error *result) { |
| 393 | return GetServiceInner(service_type, ssid, mode, security, passphrase, |
| 394 | false, result); |
Paul Stewart | ced6a0b | 2011-11-08 15:32:04 -0800 | [diff] [blame] | 395 | } |
| 396 | WiFiServiceRefPtr GetServiceInner(const char *service_type, |
| 397 | const char *ssid, |
| 398 | const char *mode, |
| 399 | const char *security, |
| 400 | const char *passphrase, |
Paul Stewart | a41e38d | 2011-11-11 07:47:29 -0800 | [diff] [blame] | 401 | bool allow_hidden, |
Paul Stewart | ced6a0b | 2011-11-08 15:32:04 -0800 | [diff] [blame] | 402 | Error *result) { |
mukesh agrawal | 7a4e400 | 2011-09-06 11:26:05 -0700 | [diff] [blame] | 403 | map<string, ::DBus::Variant> args; |
mukesh agrawal | 7a4e400 | 2011-09-06 11:26:05 -0700 | [diff] [blame] | 404 | // in general, we want to avoid D-Bus specific code for any RPCs |
| 405 | // that come in via adaptors. we make an exception here, because |
| 406 | // calls to GetWifiService are rerouted from the Manager object to |
| 407 | // the Wifi class. |
| 408 | if (service_type != NULL) |
| 409 | args[flimflam::kTypeProperty].writer().append_string(service_type); |
| 410 | if (ssid != NULL) |
| 411 | args[flimflam::kSSIDProperty].writer().append_string(ssid); |
| 412 | if (mode != NULL) |
| 413 | args[flimflam::kModeProperty].writer().append_string(mode); |
| 414 | if (security != NULL) |
| 415 | args[flimflam::kSecurityProperty].writer().append_string(security); |
| 416 | if (passphrase != NULL) |
| 417 | args[flimflam::kPassphraseProperty].writer().append_string(passphrase); |
Paul Stewart | a41e38d | 2011-11-11 07:47:29 -0800 | [diff] [blame] | 418 | if (!allow_hidden) |
| 419 | args[flimflam::kWifiHiddenSsid].writer().append_bool(false); |
mukesh agrawal | 7a4e400 | 2011-09-06 11:26:05 -0700 | [diff] [blame] | 420 | |
Paul Stewart | ced6a0b | 2011-11-08 15:32:04 -0800 | [diff] [blame] | 421 | Error e; |
| 422 | KeyValueStore args_kv; |
mukesh agrawal | 7a4e400 | 2011-09-06 11:26:05 -0700 | [diff] [blame] | 423 | DBusAdaptor::ArgsToKeyValueStore(args, &args_kv, &e); |
Paul Stewart | ced6a0b | 2011-11-08 15:32:04 -0800 | [diff] [blame] | 424 | return wifi_->GetService(args_kv, result); |
mukesh agrawal | 7a4e400 | 2011-09-06 11:26:05 -0700 | [diff] [blame] | 425 | } |
Paul Stewart | cb59fed | 2012-03-21 21:14:46 -0700 | [diff] [blame] | 426 | |
| 427 | WiFiServiceRefPtr GetServiceWithKeyValues(const KeyValueStore &args, |
| 428 | Error *result) { |
| 429 | return wifi_->GetService(args, result); |
| 430 | } |
| 431 | |
Paul Stewart | a41e38d | 2011-11-11 07:47:29 -0800 | [diff] [blame] | 432 | WiFiServiceRefPtr FindService(const vector<uint8_t> &ssid, |
| 433 | const string &mode, |
| 434 | const string &security) { |
| 435 | return wifi_->FindService(ssid, mode, security); |
| 436 | } |
| 437 | bool LoadHiddenServices(StoreInterface *storage) { |
| 438 | return wifi_->LoadHiddenServices(storage); |
| 439 | } |
| 440 | void SetupHiddenStorage(MockStore *storage, const string &ssid, string *id) { |
| 441 | const string hex_ssid = base::HexEncode(ssid.data(), ssid.size()); |
| 442 | *id = StringToLowerASCII(base::StringPrintf("%s_%s_%s_%s_%s", |
| 443 | flimflam::kTypeWifi, |
| 444 | kDeviceAddress, |
| 445 | hex_ssid.c_str(), |
| 446 | flimflam::kModeManaged, |
| 447 | flimflam::kSecurityNone)); |
| 448 | const char *groups[] = { id->c_str() }; |
| 449 | EXPECT_CALL(*storage, GetGroupsWithKey(flimflam::kWifiHiddenSsid)) |
| 450 | .WillRepeatedly(Return(set<string>(groups, groups + 1))); |
| 451 | EXPECT_CALL(*storage, GetBool(StrEq(*id), flimflam::kWifiHiddenSsid, _)) |
| 452 | .WillRepeatedly(DoAll(SetArgumentPointee<2>(true), Return(true))); |
| 453 | EXPECT_CALL(*storage, GetString(StrEq(*id), flimflam::kSSIDProperty, _)) |
| 454 | .WillRepeatedly(DoAll(SetArgumentPointee<2>(hex_ssid), Return(true))); |
| 455 | } |
Paul Stewart | bc6e739 | 2012-05-24 07:07:48 -0700 | [diff] [blame] | 456 | void ReportCertification(const map<string, ::DBus::Variant> &properties) { |
| 457 | wifi_->CertificationTask(properties); |
| 458 | } |
Paul Stewart | 10ccbb3 | 2012-04-26 15:59:30 -0700 | [diff] [blame] | 459 | |
Paul Stewart | e369ece | 2012-05-22 09:11:03 -0700 | [diff] [blame] | 460 | void RestartFastScanAttempts() { |
| 461 | wifi_->RestartFastScanAttempts(); |
| 462 | } |
| 463 | |
Paul Stewart | 10ccbb3 | 2012-04-26 15:59:30 -0700 | [diff] [blame] | 464 | NiceMockControl *control_interface() { |
| 465 | return &control_interface_; |
| 466 | } |
| 467 | |
mukesh agrawal | 3239932 | 2011-09-01 10:53:43 -0700 | [diff] [blame] | 468 | MockManager *manager() { |
| 469 | return &manager_; |
| 470 | } |
Gary Morain | d9f893f | 2012-02-06 10:03:40 -0800 | [diff] [blame] | 471 | |
| 472 | MockDeviceInfo *device_info() { |
| 473 | return &device_info_; |
| 474 | } |
| 475 | |
| 476 | MockDHCPProvider *dhcp_provider() { |
| 477 | return &dhcp_provider_; |
| 478 | } |
| 479 | |
Eric Shienbrood | 3e20a23 | 2012-02-16 11:35:56 -0500 | [diff] [blame] | 480 | const PowerManager::PowerStateCallback &power_state_callback() const { |
Gary Morain | ac1bdb4 | 2012-02-16 17:42:29 -0800 | [diff] [blame] | 481 | return power_state_callback_; |
| 482 | } |
| 483 | |
mukesh agrawal | 3239932 | 2011-09-01 10:53:43 -0700 | [diff] [blame] | 484 | const WiFiConstRefPtr wifi() const { |
| 485 | return wifi_; |
| 486 | } |
| 487 | |
mukesh agrawal | b20776f | 2012-02-10 16:00:36 -0800 | [diff] [blame] | 488 | TestProxyFactory *proxy_factory() { |
| 489 | return &proxy_factory_; |
| 490 | } |
| 491 | |
Paul Stewart | e369ece | 2012-05-22 09:11:03 -0700 | [diff] [blame] | 492 | EventDispatcher *event_dispatcher_; |
mukesh agrawal | 5c4dd0b | 2011-09-14 13:53:14 -0700 | [diff] [blame] | 493 | NiceMock<MockRTNLHandler> rtnl_handler_; |
mukesh agrawal | 5c05b29 | 2012-03-07 10:12:52 -0800 | [diff] [blame] | 494 | MockTime time_; |
mukesh agrawal | 3195024 | 2011-07-14 11:53:38 -0700 | [diff] [blame] | 495 | |
| 496 | private: |
mukesh agrawal | dc42bb3 | 2011-07-28 10:40:26 -0700 | [diff] [blame] | 497 | NiceMockControl control_interface_; |
Thieu Le | 3426c8f | 2012-01-11 17:35:11 -0800 | [diff] [blame] | 498 | MockMetrics metrics_; |
mukesh agrawal | f2f68a5 | 2011-09-01 12:15:48 -0700 | [diff] [blame] | 499 | MockGLib glib_; |
Chris Masone | 2ae797d | 2011-08-23 20:41:00 -0700 | [diff] [blame] | 500 | MockManager manager_; |
mukesh agrawal | c01f398 | 2012-01-24 13:48:39 -0800 | [diff] [blame] | 501 | MockDeviceInfo device_info_; |
mukesh agrawal | 3195024 | 2011-07-14 11:53:38 -0700 | [diff] [blame] | 502 | WiFiRefPtr wifi_; |
mukesh agrawal | 3195024 | 2011-07-14 11:53:38 -0700 | [diff] [blame] | 503 | |
| 504 | // protected fields interspersed between private fields, due to |
| 505 | // initialization order |
| 506 | protected: |
| 507 | static const char kDeviceName[]; |
Chris Masone | 626719f | 2011-08-18 16:58:48 -0700 | [diff] [blame] | 508 | static const char kDeviceAddress[]; |
mukesh agrawal | 3195024 | 2011-07-14 11:53:38 -0700 | [diff] [blame] | 509 | static const char kNetworkModeAdHoc[]; |
| 510 | static const char kNetworkModeInfrastructure[]; |
| 511 | |
mukesh agrawal | dc42bb3 | 2011-07-28 10:40:26 -0700 | [diff] [blame] | 512 | scoped_ptr<MockSupplicantProcessProxy> supplicant_process_proxy_; |
| 513 | scoped_ptr<MockSupplicantInterfaceProxy> supplicant_interface_proxy_; |
mukesh agrawal | b20776f | 2012-02-10 16:00:36 -0800 | [diff] [blame] | 514 | scoped_ptr<MockSupplicantBSSProxy> supplicant_bss_proxy_; |
mukesh agrawal | f2f68a5 | 2011-09-01 12:15:48 -0700 | [diff] [blame] | 515 | MockDHCPProvider dhcp_provider_; |
| 516 | scoped_refptr<MockDHCPConfig> dhcp_config_; |
mukesh agrawal | 3195024 | 2011-07-14 11:53:38 -0700 | [diff] [blame] | 517 | |
| 518 | private: |
| 519 | TestProxyFactory proxy_factory_; |
Gary Morain | ac1bdb4 | 2012-02-16 17:42:29 -0800 | [diff] [blame] | 520 | MockPowerManager *power_manager_; |
Eric Shienbrood | 3e20a23 | 2012-02-16 11:35:56 -0500 | [diff] [blame] | 521 | PowerManager::PowerStateCallback power_state_callback_; |
mukesh agrawal | 3195024 | 2011-07-14 11:53:38 -0700 | [diff] [blame] | 522 | }; |
| 523 | |
Paul Stewart | e369ece | 2012-05-22 09:11:03 -0700 | [diff] [blame] | 524 | const char WiFiObjectTest::kDeviceName[] = "wlan0"; |
| 525 | const char WiFiObjectTest::kDeviceAddress[] = "000102030405"; |
| 526 | const char WiFiObjectTest::kNetworkModeAdHoc[] = "ad-hoc"; |
| 527 | const char WiFiObjectTest::kNetworkModeInfrastructure[] = "infrastructure"; |
mukesh agrawal | 3195024 | 2011-07-14 11:53:38 -0700 | [diff] [blame] | 528 | |
Paul Stewart | e369ece | 2012-05-22 09:11:03 -0700 | [diff] [blame] | 529 | void WiFiObjectTest::RemoveBSS(const ::DBus::Path &bss_path) { |
mukesh agrawal | b4bc57d | 2011-12-07 01:07:47 +0000 | [diff] [blame] | 530 | wifi_->BSSRemovedTask(bss_path); |
mukesh agrawal | 261daca | 2011-12-02 18:56:56 +0000 | [diff] [blame] | 531 | } |
| 532 | |
Paul Stewart | e369ece | 2012-05-22 09:11:03 -0700 | [diff] [blame] | 533 | void WiFiObjectTest::ReportBSS(const ::DBus::Path &bss_path, |
mukesh agrawal | 3195024 | 2011-07-14 11:53:38 -0700 | [diff] [blame] | 534 | const string &ssid, |
| 535 | const string &bssid, |
| 536 | int16_t signal_strength, |
Thieu Le | e41a72d | 2012-02-06 20:46:51 +0000 | [diff] [blame] | 537 | uint16 frequency, |
mukesh agrawal | 3195024 | 2011-07-14 11:53:38 -0700 | [diff] [blame] | 538 | const char *mode) { |
| 539 | map<string, ::DBus::Variant> bss_properties; |
| 540 | |
| 541 | { |
| 542 | DBus::MessageIter writer(bss_properties["SSID"].writer()); |
| 543 | writer << vector<uint8_t>(ssid.begin(), ssid.end()); |
| 544 | } |
| 545 | { |
| 546 | string bssid_nosep; |
| 547 | vector<uint8_t> bssid_bytes; |
| 548 | RemoveChars(bssid, ":", &bssid_nosep); |
| 549 | base::HexStringToBytes(bssid_nosep, &bssid_bytes); |
| 550 | |
| 551 | DBus::MessageIter writer(bss_properties["BSSID"].writer()); |
| 552 | writer << bssid_bytes; |
| 553 | } |
Thieu Le | e41a72d | 2012-02-06 20:46:51 +0000 | [diff] [blame] | 554 | bss_properties[wpa_supplicant::kBSSPropertySignal].writer(). |
| 555 | append_int16(signal_strength); |
| 556 | bss_properties[wpa_supplicant::kBSSPropertyFrequency].writer(). |
| 557 | append_uint16(frequency); |
| 558 | bss_properties[wpa_supplicant::kBSSPropertyMode].writer().append_string(mode); |
mukesh agrawal | b4bc57d | 2011-12-07 01:07:47 +0000 | [diff] [blame] | 559 | wifi_->BSSAddedTask(bss_path, bss_properties); |
mukesh agrawal | 3195024 | 2011-07-14 11:53:38 -0700 | [diff] [blame] | 560 | } |
| 561 | |
Paul Stewart | e369ece | 2012-05-22 09:11:03 -0700 | [diff] [blame] | 562 | WiFiObjectTest::TestProxyFactory::TestProxyFactory(WiFiObjectTest *test) |
mukesh agrawal | b20776f | 2012-02-10 16:00:36 -0800 | [diff] [blame] | 563 | : test_(test) { |
| 564 | EXPECT_CALL(*this, CreateSupplicantBSSProxy(_, _, _)).Times(AnyNumber()); |
| 565 | ON_CALL(*this, CreateSupplicantBSSProxy(_, _, _)) |
| 566 | .WillByDefault( |
| 567 | Invoke(this, (&TestProxyFactory::CreateSupplicantBSSProxyInternal))); |
| 568 | } |
| 569 | |
Paul Stewart | e369ece | 2012-05-22 09:11:03 -0700 | [diff] [blame] | 570 | // Most of our tests involve using a real EventDispatcher object. |
| 571 | class WiFiMainTest : public WiFiObjectTest { |
| 572 | public: |
| 573 | WiFiMainTest() : WiFiObjectTest(&dispatcher_) {} |
| 574 | |
| 575 | protected: |
| 576 | EventDispatcher dispatcher_; |
| 577 | }; |
| 578 | |
mukesh agrawal | 3195024 | 2011-07-14 11:53:38 -0700 | [diff] [blame] | 579 | TEST_F(WiFiMainTest, ProxiesSetUpDuringStart) { |
| 580 | EXPECT_TRUE(GetSupplicantProcessProxy() == NULL); |
| 581 | EXPECT_TRUE(GetSupplicantInterfaceProxy() == NULL); |
| 582 | |
| 583 | StartWiFi(); |
| 584 | EXPECT_FALSE(GetSupplicantProcessProxy() == NULL); |
| 585 | EXPECT_FALSE(GetSupplicantInterfaceProxy() == NULL); |
| 586 | } |
| 587 | |
| 588 | TEST_F(WiFiMainTest, CleanStart) { |
| 589 | EXPECT_CALL(*supplicant_process_proxy_, CreateInterface(_)); |
| 590 | EXPECT_CALL(*supplicant_process_proxy_, GetInterface(_)) |
| 591 | .Times(AnyNumber()) |
| 592 | .WillRepeatedly(Throw( |
| 593 | DBus::Error( |
| 594 | "fi.w1.wpa_supplicant1.InterfaceUnknown", |
| 595 | "test threw fi.w1.wpa_supplicant1.InterfaceUnknown"))); |
| 596 | EXPECT_CALL(*supplicant_interface_proxy_, Scan(_)); |
mukesh agrawal | b66c646 | 2012-05-07 11:45:25 -0700 | [diff] [blame] | 597 | EXPECT_TRUE(GetScanTimer().IsCancelled()); |
mukesh agrawal | 3195024 | 2011-07-14 11:53:38 -0700 | [diff] [blame] | 598 | StartWiFi(); |
mukesh agrawal | 3239932 | 2011-09-01 10:53:43 -0700 | [diff] [blame] | 599 | dispatcher_.DispatchPendingEvents(); |
mukesh agrawal | b66c646 | 2012-05-07 11:45:25 -0700 | [diff] [blame] | 600 | EXPECT_FALSE(GetScanTimer().IsCancelled()); |
mukesh agrawal | 3195024 | 2011-07-14 11:53:38 -0700 | [diff] [blame] | 601 | } |
| 602 | |
| 603 | TEST_F(WiFiMainTest, Restart) { |
| 604 | EXPECT_CALL(*supplicant_process_proxy_, CreateInterface(_)) |
| 605 | .Times(AnyNumber()) |
| 606 | .WillRepeatedly(Throw( |
| 607 | DBus::Error( |
| 608 | "fi.w1.wpa_supplicant1.InterfaceExists", |
| 609 | "test thew fi.w1.wpa_supplicant1.InterfaceExists"))); |
| 610 | EXPECT_CALL(*supplicant_process_proxy_, GetInterface(_)); |
| 611 | EXPECT_CALL(*supplicant_interface_proxy_, Scan(_)); |
| 612 | StartWiFi(); |
mukesh agrawal | 3239932 | 2011-09-01 10:53:43 -0700 | [diff] [blame] | 613 | dispatcher_.DispatchPendingEvents(); |
mukesh agrawal | 3195024 | 2011-07-14 11:53:38 -0700 | [diff] [blame] | 614 | } |
| 615 | |
| 616 | TEST_F(WiFiMainTest, StartClearsState) { |
| 617 | EXPECT_CALL(*supplicant_interface_proxy_, RemoveAllNetworks()); |
| 618 | EXPECT_CALL(*supplicant_interface_proxy_, FlushBSS(_)); |
| 619 | StartWiFi(); |
| 620 | } |
| 621 | |
Gary Morain | ac1bdb4 | 2012-02-16 17:42:29 -0800 | [diff] [blame] | 622 | TEST_F(WiFiMainTest, PowerChangeToResumeStartsScanWhenIdle) { |
| 623 | EXPECT_CALL(*supplicant_interface_proxy_, Scan(_)); |
| 624 | StartWiFi(); |
| 625 | dispatcher_.DispatchPendingEvents(); |
| 626 | Mock::VerifyAndClearExpectations(&supplicant_interface_proxy_); |
mukesh agrawal | 5c05b29 | 2012-03-07 10:12:52 -0800 | [diff] [blame] | 627 | ReportScanDone(); |
Eric Shienbrood | 3e20a23 | 2012-02-16 11:35:56 -0500 | [diff] [blame] | 628 | ASSERT_FALSE(power_state_callback().is_null()); |
Gary Morain | ac1bdb4 | 2012-02-16 17:42:29 -0800 | [diff] [blame] | 629 | ASSERT_TRUE(wifi()->IsIdle()); |
| 630 | EXPECT_CALL(*GetSupplicantInterfaceProxy(), Scan(_)); |
Eric Shienbrood | 3e20a23 | 2012-02-16 11:35:56 -0500 | [diff] [blame] | 631 | power_state_callback().Run(PowerManagerProxyDelegate::kOn); |
Gary Morain | ac1bdb4 | 2012-02-16 17:42:29 -0800 | [diff] [blame] | 632 | dispatcher_.DispatchPendingEvents(); |
| 633 | } |
| 634 | |
| 635 | TEST_F(WiFiMainTest, PowerChangeToSuspendDoesNotStartScan) { |
| 636 | EXPECT_CALL(*supplicant_interface_proxy_, Scan(_)); |
| 637 | StartWiFi(); |
| 638 | dispatcher_.DispatchPendingEvents(); |
| 639 | Mock::VerifyAndClearExpectations(&supplicant_interface_proxy_); |
Eric Shienbrood | 3e20a23 | 2012-02-16 11:35:56 -0500 | [diff] [blame] | 640 | ASSERT_FALSE(power_state_callback().is_null()); |
Gary Morain | ac1bdb4 | 2012-02-16 17:42:29 -0800 | [diff] [blame] | 641 | ASSERT_TRUE(wifi()->IsIdle()); |
| 642 | EXPECT_CALL(*GetSupplicantInterfaceProxy(), Scan(_)).Times(0); |
Eric Shienbrood | 3e20a23 | 2012-02-16 11:35:56 -0500 | [diff] [blame] | 643 | power_state_callback().Run(PowerManagerProxyDelegate::kStandby); |
Gary Morain | ac1bdb4 | 2012-02-16 17:42:29 -0800 | [diff] [blame] | 644 | dispatcher_.DispatchPendingEvents(); |
Eric Shienbrood | 3e20a23 | 2012-02-16 11:35:56 -0500 | [diff] [blame] | 645 | power_state_callback().Run(PowerManagerProxyDelegate::kMem); |
Gary Morain | ac1bdb4 | 2012-02-16 17:42:29 -0800 | [diff] [blame] | 646 | dispatcher_.DispatchPendingEvents(); |
Eric Shienbrood | 3e20a23 | 2012-02-16 11:35:56 -0500 | [diff] [blame] | 647 | power_state_callback().Run(PowerManagerProxyDelegate::kDisk); |
Gary Morain | ac1bdb4 | 2012-02-16 17:42:29 -0800 | [diff] [blame] | 648 | dispatcher_.DispatchPendingEvents(); |
| 649 | } |
| 650 | |
| 651 | TEST_F(WiFiMainTest, PowerChangeDoesNotStartScanWhenNotIdle) { |
| 652 | EXPECT_CALL(*supplicant_interface_proxy_, Scan(_)); |
| 653 | StartWiFi(); |
| 654 | |
| 655 | WiFiEndpointRefPtr ap = MakeEndpoint("an_ssid", "00:01:02:03:04:05"); |
| 656 | WiFiServiceRefPtr service = CreateServiceForEndpoint(*ap); |
| 657 | Error error; |
| 658 | service->AddEndpoint(ap); |
| 659 | service->AutoConnect(); |
| 660 | EXPECT_FALSE(wifi()->IsIdle()); |
| 661 | dispatcher_.DispatchPendingEvents(); |
| 662 | Mock::VerifyAndClearExpectations(&supplicant_interface_proxy_); |
Eric Shienbrood | 3e20a23 | 2012-02-16 11:35:56 -0500 | [diff] [blame] | 663 | ASSERT_FALSE(power_state_callback().is_null()); |
Gary Morain | ac1bdb4 | 2012-02-16 17:42:29 -0800 | [diff] [blame] | 664 | ASSERT_FALSE(wifi()->IsIdle()); |
| 665 | EXPECT_CALL(*GetSupplicantInterfaceProxy(), Scan(_)).Times(0); |
Eric Shienbrood | 3e20a23 | 2012-02-16 11:35:56 -0500 | [diff] [blame] | 666 | power_state_callback().Run(PowerManagerProxyDelegate::kOn); |
Gary Morain | ac1bdb4 | 2012-02-16 17:42:29 -0800 | [diff] [blame] | 667 | dispatcher_.DispatchPendingEvents(); |
| 668 | } |
| 669 | |
mukesh agrawal | 3195024 | 2011-07-14 11:53:38 -0700 | [diff] [blame] | 670 | TEST_F(WiFiMainTest, ScanResults) { |
Gary Morain | d9f893f | 2012-02-06 10:03:40 -0800 | [diff] [blame] | 671 | EXPECT_CALL(*manager(), RegisterService(_)).Times(AnyNumber()); |
mukesh agrawal | 3195024 | 2011-07-14 11:53:38 -0700 | [diff] [blame] | 672 | StartWiFi(); |
Thieu Le | e41a72d | 2012-02-06 20:46:51 +0000 | [diff] [blame] | 673 | ReportBSS("bss0", "ssid0", "00:00:00:00:00:00", 0, 0, kNetworkModeAdHoc); |
mukesh agrawal | 3195024 | 2011-07-14 11:53:38 -0700 | [diff] [blame] | 674 | ReportBSS( |
Thieu Le | e41a72d | 2012-02-06 20:46:51 +0000 | [diff] [blame] | 675 | "bss1", "ssid1", "00:00:00:00:00:01", 1, 0, kNetworkModeInfrastructure); |
mukesh agrawal | 3195024 | 2011-07-14 11:53:38 -0700 | [diff] [blame] | 676 | ReportBSS( |
Thieu Le | e41a72d | 2012-02-06 20:46:51 +0000 | [diff] [blame] | 677 | "bss2", "ssid2", "00:00:00:00:00:02", 2, 0, kNetworkModeInfrastructure); |
mukesh agrawal | 3195024 | 2011-07-14 11:53:38 -0700 | [diff] [blame] | 678 | ReportBSS( |
Thieu Le | e41a72d | 2012-02-06 20:46:51 +0000 | [diff] [blame] | 679 | "bss3", "ssid3", "00:00:00:00:00:03", 3, 0, kNetworkModeInfrastructure); |
| 680 | const uint16 frequency = 2412; |
| 681 | ReportBSS("bss4", "ssid4", "00:00:00:00:00:04", 4, frequency, |
| 682 | kNetworkModeAdHoc); |
| 683 | |
| 684 | const WiFi::EndpointMap &endpoints_by_rpcid = GetEndpointMap(); |
| 685 | EXPECT_EQ(5, endpoints_by_rpcid.size()); |
| 686 | |
| 687 | WiFi::EndpointMap::const_iterator i; |
| 688 | WiFiEndpointRefPtr endpoint; |
| 689 | for (i = endpoints_by_rpcid.begin(); |
| 690 | i != endpoints_by_rpcid.end(); |
| 691 | ++i) { |
| 692 | if (i->second->bssid_string() == "00:00:00:00:00:04") |
| 693 | break; |
| 694 | } |
| 695 | ASSERT_TRUE(i != endpoints_by_rpcid.end()); |
| 696 | EXPECT_EQ(4, i->second->signal_strength()); |
| 697 | EXPECT_EQ(frequency, i->second->frequency()); |
| 698 | EXPECT_EQ("adhoc", i->second->network_mode()); |
mukesh agrawal | 3195024 | 2011-07-14 11:53:38 -0700 | [diff] [blame] | 699 | } |
| 700 | |
| 701 | TEST_F(WiFiMainTest, ScanResultsWithUpdates) { |
Gary Morain | d9f893f | 2012-02-06 10:03:40 -0800 | [diff] [blame] | 702 | EXPECT_CALL(*manager(), RegisterService(_)).Times(AnyNumber()); |
| 703 | EXPECT_CALL(*manager(), HasService(_)).Times(AnyNumber()); |
| 704 | EXPECT_CALL(*manager(), UpdateService(_)).Times(AnyNumber()); |
mukesh agrawal | 3195024 | 2011-07-14 11:53:38 -0700 | [diff] [blame] | 705 | StartWiFi(); |
Thieu Le | e41a72d | 2012-02-06 20:46:51 +0000 | [diff] [blame] | 706 | ReportBSS("bss0", "ssid0", "00:00:00:00:00:00", 0, 0, kNetworkModeAdHoc); |
mukesh agrawal | 3195024 | 2011-07-14 11:53:38 -0700 | [diff] [blame] | 707 | ReportBSS( |
Thieu Le | e41a72d | 2012-02-06 20:46:51 +0000 | [diff] [blame] | 708 | "bss1", "ssid1", "00:00:00:00:00:01", 1, 0, kNetworkModeInfrastructure); |
mukesh agrawal | 3195024 | 2011-07-14 11:53:38 -0700 | [diff] [blame] | 709 | ReportBSS( |
Thieu Le | e41a72d | 2012-02-06 20:46:51 +0000 | [diff] [blame] | 710 | "bss2", "ssid2", "00:00:00:00:00:02", 2, 0, kNetworkModeInfrastructure); |
mukesh agrawal | 3195024 | 2011-07-14 11:53:38 -0700 | [diff] [blame] | 711 | ReportBSS( |
Thieu Le | e41a72d | 2012-02-06 20:46:51 +0000 | [diff] [blame] | 712 | "bss1", "ssid1", "00:00:00:00:00:01", 3, 0, kNetworkModeInfrastructure); |
| 713 | ReportBSS("bss0", "ssid0", "00:00:00:00:00:00", 4, 0, kNetworkModeAdHoc); |
mukesh agrawal | 165e614 | 2011-11-22 02:22:56 +0000 | [diff] [blame] | 714 | |
| 715 | const WiFi::EndpointMap &endpoints_by_rpcid = GetEndpointMap(); |
| 716 | EXPECT_EQ(3, endpoints_by_rpcid.size()); |
| 717 | |
| 718 | WiFi::EndpointMap::const_iterator i; |
| 719 | WiFiEndpointRefPtr endpoint; |
| 720 | for (i = endpoints_by_rpcid.begin(); |
| 721 | i != endpoints_by_rpcid.end(); |
| 722 | ++i) { |
| 723 | if (i->second->bssid_string() == "00:00:00:00:00:00") |
| 724 | break; |
| 725 | } |
| 726 | ASSERT_TRUE(i != endpoints_by_rpcid.end()); |
| 727 | EXPECT_EQ(4, i->second->signal_strength()); |
mukesh agrawal | 3195024 | 2011-07-14 11:53:38 -0700 | [diff] [blame] | 728 | } |
| 729 | |
| 730 | TEST_F(WiFiMainTest, ScanCompleted) { |
| 731 | StartWiFi(); |
mukesh agrawal | 261daca | 2011-12-02 18:56:56 +0000 | [diff] [blame] | 732 | EXPECT_CALL(*manager(), RegisterService(_)) |
| 733 | .Times(3); |
Thieu Le | e41a72d | 2012-02-06 20:46:51 +0000 | [diff] [blame] | 734 | ReportBSS("bss0", "ssid0", "00:00:00:00:00:00", 0, 0, kNetworkModeAdHoc); |
mukesh agrawal | 3195024 | 2011-07-14 11:53:38 -0700 | [diff] [blame] | 735 | ReportBSS( |
Thieu Le | e41a72d | 2012-02-06 20:46:51 +0000 | [diff] [blame] | 736 | "bss1", "ssid1", "00:00:00:00:00:01", 1, 0, kNetworkModeInfrastructure); |
mukesh agrawal | 3195024 | 2011-07-14 11:53:38 -0700 | [diff] [blame] | 737 | ReportBSS( |
Thieu Le | e41a72d | 2012-02-06 20:46:51 +0000 | [diff] [blame] | 738 | "bss2", "ssid2", "00:00:00:00:00:02", 2, 0, kNetworkModeInfrastructure); |
mukesh agrawal | 3195024 | 2011-07-14 11:53:38 -0700 | [diff] [blame] | 739 | ReportScanDone(); |
mukesh agrawal | 165e614 | 2011-11-22 02:22:56 +0000 | [diff] [blame] | 740 | EXPECT_EQ(3, GetServices().size()); |
mukesh agrawal | b385761 | 2012-01-18 16:23:29 -0800 | [diff] [blame] | 741 | |
mukesh agrawal | e9adda1 | 2012-02-09 18:33:48 -0800 | [diff] [blame] | 742 | // BSSes with SSIDs that start with NULL should be filtered. |
Thieu Le | e41a72d | 2012-02-06 20:46:51 +0000 | [diff] [blame] | 743 | ReportBSS("bss3", string(1, 0), "00:00:00:00:00:03", 3, 0, kNetworkModeAdHoc); |
mukesh agrawal | e9adda1 | 2012-02-09 18:33:48 -0800 | [diff] [blame] | 744 | EXPECT_EQ(3, GetEndpointMap().size()); |
mukesh agrawal | b385761 | 2012-01-18 16:23:29 -0800 | [diff] [blame] | 745 | EXPECT_EQ(3, GetServices().size()); |
| 746 | |
mukesh agrawal | e9adda1 | 2012-02-09 18:33:48 -0800 | [diff] [blame] | 747 | // BSSes with empty SSIDs should be filtered. |
| 748 | ReportBSS("bss3", string(), "00:00:00:00:00:03", 3, 0, kNetworkModeAdHoc); |
| 749 | EXPECT_EQ(3, GetEndpointMap().size()); |
| 750 | EXPECT_EQ(3, GetServices().size()); |
mukesh agrawal | 165e614 | 2011-11-22 02:22:56 +0000 | [diff] [blame] | 751 | } |
| 752 | |
| 753 | TEST_F(WiFiMainTest, EndpointGroupingTogether) { |
| 754 | StartWiFi(); |
mukesh agrawal | 261daca | 2011-12-02 18:56:56 +0000 | [diff] [blame] | 755 | |
| 756 | InSequence s; |
| 757 | EXPECT_CALL(*manager(), RegisterService(_)); |
Gary Morain | d9f893f | 2012-02-06 10:03:40 -0800 | [diff] [blame] | 758 | EXPECT_CALL(*manager(), HasService(_)); |
mukesh agrawal | 261daca | 2011-12-02 18:56:56 +0000 | [diff] [blame] | 759 | EXPECT_CALL(*manager(), UpdateService(_)); |
Thieu Le | e41a72d | 2012-02-06 20:46:51 +0000 | [diff] [blame] | 760 | ReportBSS("bss0", "ssid", "00:00:00:00:00:00", 0, 0, kNetworkModeAdHoc); |
| 761 | ReportBSS("bss1", "ssid", "00:00:00:00:00:01", 0, 0, kNetworkModeAdHoc); |
mukesh agrawal | 165e614 | 2011-11-22 02:22:56 +0000 | [diff] [blame] | 762 | ReportScanDone(); |
| 763 | EXPECT_EQ(1, GetServices().size()); |
| 764 | } |
| 765 | |
| 766 | TEST_F(WiFiMainTest, EndpointGroupingDifferentSSID) { |
| 767 | StartWiFi(); |
mukesh agrawal | 261daca | 2011-12-02 18:56:56 +0000 | [diff] [blame] | 768 | EXPECT_CALL(*manager(), RegisterService(_)) |
| 769 | .Times(2); |
Thieu Le | e41a72d | 2012-02-06 20:46:51 +0000 | [diff] [blame] | 770 | ReportBSS("bss0", "ssid1", "00:00:00:00:00:00", 0, 0, kNetworkModeAdHoc); |
| 771 | ReportBSS("bss1", "ssid2", "00:00:00:00:00:01", 0, 0, kNetworkModeAdHoc); |
mukesh agrawal | 165e614 | 2011-11-22 02:22:56 +0000 | [diff] [blame] | 772 | ReportScanDone(); |
| 773 | EXPECT_EQ(2, GetServices().size()); |
| 774 | } |
| 775 | |
| 776 | TEST_F(WiFiMainTest, EndpointGroupingDifferentMode) { |
| 777 | StartWiFi(); |
mukesh agrawal | 261daca | 2011-12-02 18:56:56 +0000 | [diff] [blame] | 778 | EXPECT_CALL(*manager(), RegisterService(_)) |
| 779 | .Times(2); |
Thieu Le | e41a72d | 2012-02-06 20:46:51 +0000 | [diff] [blame] | 780 | ReportBSS("bss0", "ssid", "00:00:00:00:00:00", 0, 0, kNetworkModeAdHoc); |
| 781 | ReportBSS("bss1", "ssid", "00:00:00:00:00:01", 0, 0, |
| 782 | kNetworkModeInfrastructure); |
mukesh agrawal | 165e614 | 2011-11-22 02:22:56 +0000 | [diff] [blame] | 783 | ReportScanDone(); |
| 784 | EXPECT_EQ(2, GetServices().size()); |
mukesh agrawal | 3195024 | 2011-07-14 11:53:38 -0700 | [diff] [blame] | 785 | } |
| 786 | |
mukesh agrawal | 261daca | 2011-12-02 18:56:56 +0000 | [diff] [blame] | 787 | TEST_F(WiFiMainTest, NonExistentBSSRemoved) { |
| 788 | // Removal of non-existent BSS should not cause a crash. |
| 789 | StartWiFi(); |
| 790 | RemoveBSS("bss0"); |
| 791 | EXPECT_EQ(0, GetServices().size()); |
| 792 | } |
| 793 | |
mukesh agrawal | e9adda1 | 2012-02-09 18:33:48 -0800 | [diff] [blame] | 794 | TEST_F(WiFiMainTest, BSSWithEmptySSIDRemoved) { |
| 795 | // Removal of BSS with an empty SSID should not cause a crash. |
| 796 | ReportBSS("bss", string(), "00:00:00:00:00:01", 0, 0, kNetworkModeAdHoc); |
| 797 | StartWiFi(); |
| 798 | RemoveBSS("bss"); |
| 799 | EXPECT_EQ(0, GetServices().size()); |
| 800 | } |
| 801 | |
| 802 | TEST_F(WiFiMainTest, BSSWithNullSSIDRemoved) { |
| 803 | // Removal of BSS with a NULL SSID should not cause a crash. |
| 804 | ReportBSS("bss", string(1, 0), "00:00:00:00:00:01", 0, 0, kNetworkModeAdHoc); |
| 805 | StartWiFi(); |
| 806 | RemoveBSS("bss"); |
| 807 | EXPECT_EQ(0, GetServices().size()); |
| 808 | } |
| 809 | |
mukesh agrawal | 261daca | 2011-12-02 18:56:56 +0000 | [diff] [blame] | 810 | TEST_F(WiFiMainTest, LoneBSSRemoved) { |
Gary Morain | d9f893f | 2012-02-06 10:03:40 -0800 | [diff] [blame] | 811 | EXPECT_CALL(*manager(), RegisterService(_)).Times(AnyNumber()); |
mukesh agrawal | 261daca | 2011-12-02 18:56:56 +0000 | [diff] [blame] | 812 | StartWiFi(); |
Thieu Le | e41a72d | 2012-02-06 20:46:51 +0000 | [diff] [blame] | 813 | ReportBSS("bss0", "ssid", "00:00:00:00:00:00", 0, 0, kNetworkModeAdHoc); |
mukesh agrawal | 261daca | 2011-12-02 18:56:56 +0000 | [diff] [blame] | 814 | ReportScanDone(); |
| 815 | EXPECT_EQ(1, GetServices().size()); |
| 816 | EXPECT_TRUE(GetServices().front()->IsVisible()); |
| 817 | |
mukesh agrawal | 8a3188d | 2011-12-01 20:56:44 +0000 | [diff] [blame] | 818 | EXPECT_CALL(*manager(), DeregisterService(_)); |
mukesh agrawal | 261daca | 2011-12-02 18:56:56 +0000 | [diff] [blame] | 819 | RemoveBSS("bss0"); |
mukesh agrawal | 8a3188d | 2011-12-01 20:56:44 +0000 | [diff] [blame] | 820 | EXPECT_TRUE(GetServices().empty()); |
| 821 | } |
| 822 | |
| 823 | TEST_F(WiFiMainTest, LoneBSSRemovedWhileConnected) { |
Gary Morain | d9f893f | 2012-02-06 10:03:40 -0800 | [diff] [blame] | 824 | EXPECT_CALL(*manager(), RegisterService(_)).Times(AnyNumber()); |
mukesh agrawal | 8a3188d | 2011-12-01 20:56:44 +0000 | [diff] [blame] | 825 | StartWiFi(); |
Thieu Le | e41a72d | 2012-02-06 20:46:51 +0000 | [diff] [blame] | 826 | ReportBSS("bss0", "ssid", "00:00:00:00:00:00", 0, 0, kNetworkModeAdHoc); |
mukesh agrawal | 8a3188d | 2011-12-01 20:56:44 +0000 | [diff] [blame] | 827 | ReportScanDone(); |
| 828 | ReportCurrentBSSChanged("bss0"); |
| 829 | |
| 830 | EXPECT_CALL(*GetSupplicantInterfaceProxy(), Disconnect()); |
| 831 | EXPECT_CALL(*manager(), DeregisterService(_)); |
| 832 | RemoveBSS("bss0"); |
| 833 | EXPECT_TRUE(GetServices().empty()); |
| 834 | } |
| 835 | |
| 836 | TEST_F(WiFiMainTest, LoneBSSRemovedWhileConnectedToHidden) { |
Gary Morain | d9f893f | 2012-02-06 10:03:40 -0800 | [diff] [blame] | 837 | EXPECT_CALL(*manager(), UpdateService(_)).Times(AnyNumber()); |
| 838 | EXPECT_CALL(*manager(), HasService(_)).Times(AnyNumber()); |
mukesh agrawal | 8a3188d | 2011-12-01 20:56:44 +0000 | [diff] [blame] | 839 | StartWiFi(); |
| 840 | |
| 841 | Error e; |
| 842 | WiFiServiceRefPtr service = |
| 843 | GetServiceInner(flimflam::kTypeWifi, "ssid", flimflam::kModeManaged, |
| 844 | NULL, NULL, true, &e); |
mukesh agrawal | 261daca | 2011-12-02 18:56:56 +0000 | [diff] [blame] | 845 | EXPECT_EQ(1, GetServices().size()); |
mukesh agrawal | 8a3188d | 2011-12-01 20:56:44 +0000 | [diff] [blame] | 846 | |
Thieu Le | e41a72d | 2012-02-06 20:46:51 +0000 | [diff] [blame] | 847 | ReportBSS("bss", "ssid", "00:00:00:00:00:01", 0, 0, |
| 848 | kNetworkModeInfrastructure); |
mukesh agrawal | 8a3188d | 2011-12-01 20:56:44 +0000 | [diff] [blame] | 849 | ReportScanDone(); |
| 850 | ReportCurrentBSSChanged("bss"); |
| 851 | EXPECT_EQ(1, GetServices().size()); |
| 852 | |
| 853 | EXPECT_CALL(*GetSupplicantInterfaceProxy(), Disconnect()); |
mukesh agrawal | 8a3188d | 2011-12-01 20:56:44 +0000 | [diff] [blame] | 854 | RemoveBSS("bss"); |
mukesh agrawal | 8a3188d | 2011-12-01 20:56:44 +0000 | [diff] [blame] | 855 | EXPECT_EQ(1, GetServices().size()); |
| 856 | // Verify expectations now, because WiFi may call UpdateService when |
| 857 | // WiFi is Stop()-ed (during TearDown()). |
| 858 | Mock::VerifyAndClearExpectations(manager()); |
Gary Morain | d9f893f | 2012-02-06 10:03:40 -0800 | [diff] [blame] | 859 | EXPECT_CALL(*manager(), DeregisterService(_)).Times(AnyNumber()); |
mukesh agrawal | 261daca | 2011-12-02 18:56:56 +0000 | [diff] [blame] | 860 | } |
| 861 | |
| 862 | TEST_F(WiFiMainTest, NonSolitaryBSSRemoved) { |
Gary Morain | d9f893f | 2012-02-06 10:03:40 -0800 | [diff] [blame] | 863 | EXPECT_CALL(*manager(), RegisterService(_)); |
| 864 | EXPECT_CALL(*manager(), HasService(_)); |
| 865 | EXPECT_CALL(*manager(), UpdateService(_)); |
mukesh agrawal | 261daca | 2011-12-02 18:56:56 +0000 | [diff] [blame] | 866 | StartWiFi(); |
Thieu Le | e41a72d | 2012-02-06 20:46:51 +0000 | [diff] [blame] | 867 | ReportBSS("bss0", "ssid", "00:00:00:00:00:00", 0, 0, kNetworkModeAdHoc); |
| 868 | ReportBSS("bss1", "ssid", "00:00:00:00:00:00", 0, 0, kNetworkModeAdHoc); |
mukesh agrawal | 261daca | 2011-12-02 18:56:56 +0000 | [diff] [blame] | 869 | ReportScanDone(); |
| 870 | EXPECT_EQ(1, GetServices().size()); |
| 871 | EXPECT_TRUE(GetServices().front()->IsVisible()); |
| 872 | |
| 873 | EXPECT_CALL(*manager(), UpdateService(_)); |
| 874 | RemoveBSS("bss0"); |
| 875 | EXPECT_TRUE(GetServices().front()->IsVisible()); |
| 876 | EXPECT_EQ(1, GetServices().size()); |
| 877 | } |
| 878 | |
mukesh agrawal | 3195024 | 2011-07-14 11:53:38 -0700 | [diff] [blame] | 879 | TEST_F(WiFiMainTest, Connect) { |
Gary Morain | d9f893f | 2012-02-06 10:03:40 -0800 | [diff] [blame] | 880 | EXPECT_CALL(*manager(), RegisterService(_)).Times(AnyNumber()); |
mukesh agrawal | dc42bb3 | 2011-07-28 10:40:26 -0700 | [diff] [blame] | 881 | MockSupplicantInterfaceProxy &supplicant_interface_proxy = |
| 882 | *supplicant_interface_proxy_; |
| 883 | |
mukesh agrawal | 3195024 | 2011-07-14 11:53:38 -0700 | [diff] [blame] | 884 | StartWiFi(); |
Thieu Le | e41a72d | 2012-02-06 20:46:51 +0000 | [diff] [blame] | 885 | ReportBSS("bss0", "ssid0", "00:00:00:00:00:00", 0, 0, kNetworkModeAdHoc); |
mukesh agrawal | 3195024 | 2011-07-14 11:53:38 -0700 | [diff] [blame] | 886 | ReportScanDone(); |
| 887 | |
| 888 | { |
| 889 | InSequence s; |
| 890 | DBus::Path fake_path("/fake/path"); |
mukesh agrawal | 165e614 | 2011-11-22 02:22:56 +0000 | [diff] [blame] | 891 | WiFiService *service(GetServices().begin()->get()); |
mukesh agrawal | 3195024 | 2011-07-14 11:53:38 -0700 | [diff] [blame] | 892 | |
mukesh agrawal | dc42bb3 | 2011-07-28 10:40:26 -0700 | [diff] [blame] | 893 | EXPECT_CALL(supplicant_interface_proxy, AddNetwork(_)) |
mukesh agrawal | 3195024 | 2011-07-14 11:53:38 -0700 | [diff] [blame] | 894 | .WillOnce(Return(fake_path)); |
mukesh agrawal | dc42bb3 | 2011-07-28 10:40:26 -0700 | [diff] [blame] | 895 | EXPECT_CALL(supplicant_interface_proxy, SelectNetwork(fake_path)); |
mukesh agrawal | 3239932 | 2011-09-01 10:53:43 -0700 | [diff] [blame] | 896 | InitiateConnect(service); |
mukesh agrawal | f2f68a5 | 2011-09-01 12:15:48 -0700 | [diff] [blame] | 897 | EXPECT_EQ(static_cast<Service *>(service), |
| 898 | wifi()->selected_service_.get()); |
mukesh agrawal | 3195024 | 2011-07-14 11:53:38 -0700 | [diff] [blame] | 899 | } |
| 900 | } |
| 901 | |
mukesh agrawal | 0ed0f2e | 2011-12-05 20:36:17 +0000 | [diff] [blame] | 902 | TEST_F(WiFiMainTest, DisconnectPendingService) { |
Gary Morain | d9f893f | 2012-02-06 10:03:40 -0800 | [diff] [blame] | 903 | EXPECT_CALL(*manager(), RegisterService(_)).Times(AnyNumber()); |
mukesh agrawal | 0ed0f2e | 2011-12-05 20:36:17 +0000 | [diff] [blame] | 904 | MockSupplicantInterfaceProxy &supplicant_interface_proxy = |
| 905 | *supplicant_interface_proxy_; |
| 906 | |
| 907 | StartWiFi(); |
Thieu Le | e41a72d | 2012-02-06 20:46:51 +0000 | [diff] [blame] | 908 | ReportBSS("bss0", "ssid0", "00:00:00:00:00:00", 0, 0, kNetworkModeAdHoc); |
mukesh agrawal | 0ed0f2e | 2011-12-05 20:36:17 +0000 | [diff] [blame] | 909 | WiFiService *service(GetServices().begin()->get()); |
| 910 | InitiateConnect(service); |
| 911 | |
| 912 | EXPECT_FALSE(GetPendingService() == NULL); |
| 913 | EXPECT_CALL(supplicant_interface_proxy, Disconnect()); |
| 914 | InitiateDisconnect(service); |
| 915 | |
| 916 | EXPECT_TRUE(GetPendingService() == NULL); |
| 917 | } |
| 918 | |
| 919 | TEST_F(WiFiMainTest, DisconnectPendingServiceWithCurrent) { |
Gary Morain | d9f893f | 2012-02-06 10:03:40 -0800 | [diff] [blame] | 920 | EXPECT_CALL(*manager(), RegisterService(_)).Times(AnyNumber()); |
Paul Stewart | d408fdf | 2012-05-07 17:15:57 -0700 | [diff] [blame] | 921 | EXPECT_CALL(*dhcp_provider(), CreateConfig(_, _, _, _)).Times(AnyNumber()); |
Gary Morain | d9f893f | 2012-02-06 10:03:40 -0800 | [diff] [blame] | 922 | EXPECT_CALL(*dhcp_config_.get(), RequestIP()).Times(AnyNumber()); |
| 923 | EXPECT_CALL(*manager(), UpdateService(_)).Times(AnyNumber()); |
mukesh agrawal | 0ed0f2e | 2011-12-05 20:36:17 +0000 | [diff] [blame] | 924 | MockSupplicantInterfaceProxy &supplicant_interface_proxy = |
| 925 | *supplicant_interface_proxy_; |
| 926 | |
| 927 | StartWiFi(); |
Thieu Le | e41a72d | 2012-02-06 20:46:51 +0000 | [diff] [blame] | 928 | ReportBSS("bss0", "ssid0", "00:00:00:00:00:00", 0, 0, kNetworkModeAdHoc); |
| 929 | ReportBSS("bss1", "ssid1", "00:00:00:00:00:01", 0, 0, kNetworkModeAdHoc); |
mukesh agrawal | 0ed0f2e | 2011-12-05 20:36:17 +0000 | [diff] [blame] | 930 | WiFiService *service0(GetServices()[0].get()); |
| 931 | WiFiService *service1(GetServices()[1].get()); |
| 932 | |
| 933 | InitiateConnect(service0); |
| 934 | ReportCurrentBSSChanged("bss0"); |
| 935 | ReportStateChanged(wpa_supplicant::kInterfaceStateCompleted); |
| 936 | InitiateConnect(service1); |
| 937 | |
| 938 | EXPECT_EQ(service0, GetCurrentService()); |
| 939 | EXPECT_EQ(service1, GetPendingService()); |
| 940 | EXPECT_CALL(supplicant_interface_proxy, Disconnect()); |
| 941 | InitiateDisconnect(service1); |
| 942 | |
| 943 | // |current_service_| will be unchanged until supplicant signals |
| 944 | // that CurrentBSS has changed. |
| 945 | EXPECT_EQ(service0, GetCurrentService()); |
| 946 | // |pending_service_| is updated immediately. |
| 947 | EXPECT_TRUE(GetPendingService() == NULL); |
| 948 | } |
| 949 | |
| 950 | TEST_F(WiFiMainTest, DisconnectCurrentService) { |
Gary Morain | d9f893f | 2012-02-06 10:03:40 -0800 | [diff] [blame] | 951 | EXPECT_CALL(*manager(), RegisterService(_)).Times(AnyNumber()); |
Paul Stewart | d408fdf | 2012-05-07 17:15:57 -0700 | [diff] [blame] | 952 | EXPECT_CALL(*dhcp_provider(), CreateConfig(_, _, _, _)).Times(AnyNumber()); |
Gary Morain | d9f893f | 2012-02-06 10:03:40 -0800 | [diff] [blame] | 953 | EXPECT_CALL(*dhcp_config_.get(), RequestIP()).Times(AnyNumber()); |
| 954 | EXPECT_CALL(*manager(), UpdateService(_)).Times(AnyNumber()); |
mukesh agrawal | 0ed0f2e | 2011-12-05 20:36:17 +0000 | [diff] [blame] | 955 | MockSupplicantInterfaceProxy &supplicant_interface_proxy = |
| 956 | *supplicant_interface_proxy_; |
| 957 | |
| 958 | StartWiFi(); |
Thieu Le | e41a72d | 2012-02-06 20:46:51 +0000 | [diff] [blame] | 959 | ReportBSS("bss0", "ssid0", "00:00:00:00:00:00", 0, 0, kNetworkModeAdHoc); |
mukesh agrawal | 0ed0f2e | 2011-12-05 20:36:17 +0000 | [diff] [blame] | 960 | WiFiService *service(GetServices().begin()->get()); |
| 961 | InitiateConnect(service); |
| 962 | ReportCurrentBSSChanged("bss0"); |
| 963 | ReportStateChanged(wpa_supplicant::kInterfaceStateCompleted); |
| 964 | |
| 965 | EXPECT_EQ(service, GetCurrentService()); |
| 966 | EXPECT_CALL(supplicant_interface_proxy, Disconnect()); |
| 967 | InitiateDisconnect(service); |
| 968 | |
| 969 | // |current_service_| should not change until supplicant reports |
| 970 | // a BSS change. |
| 971 | EXPECT_EQ(service, GetCurrentService()); |
| 972 | } |
| 973 | |
| 974 | TEST_F(WiFiMainTest, DisconnectCurrentServiceWithPending) { |
Gary Morain | d9f893f | 2012-02-06 10:03:40 -0800 | [diff] [blame] | 975 | EXPECT_CALL(*manager(), RegisterService(_)).Times(AnyNumber()); |
Paul Stewart | d408fdf | 2012-05-07 17:15:57 -0700 | [diff] [blame] | 976 | EXPECT_CALL(*dhcp_provider(), CreateConfig(_, _, _, _)).Times(AnyNumber()); |
Gary Morain | d9f893f | 2012-02-06 10:03:40 -0800 | [diff] [blame] | 977 | EXPECT_CALL(*dhcp_config_.get(), RequestIP()).Times(AnyNumber()); |
| 978 | EXPECT_CALL(*manager(), UpdateService(_)).Times(AnyNumber()); |
mukesh agrawal | 0ed0f2e | 2011-12-05 20:36:17 +0000 | [diff] [blame] | 979 | MockSupplicantInterfaceProxy &supplicant_interface_proxy = |
| 980 | *supplicant_interface_proxy_; |
| 981 | |
| 982 | StartWiFi(); |
Thieu Le | e41a72d | 2012-02-06 20:46:51 +0000 | [diff] [blame] | 983 | ReportBSS("bss0", "ssid0", "00:00:00:00:00:00", 0, 0, kNetworkModeAdHoc); |
| 984 | ReportBSS("bss1", "ssid1", "00:00:00:00:00:01", 0, 0, kNetworkModeAdHoc); |
mukesh agrawal | 0ed0f2e | 2011-12-05 20:36:17 +0000 | [diff] [blame] | 985 | WiFiService *service0(GetServices()[0].get()); |
| 986 | WiFiService *service1(GetServices()[1].get()); |
| 987 | |
| 988 | InitiateConnect(service0); |
| 989 | ReportCurrentBSSChanged("bss0"); |
| 990 | ReportStateChanged(wpa_supplicant::kInterfaceStateCompleted); |
| 991 | InitiateConnect(service1); |
| 992 | |
| 993 | EXPECT_EQ(service0, GetCurrentService()); |
| 994 | EXPECT_EQ(service1, GetPendingService()); |
| 995 | EXPECT_CALL(supplicant_interface_proxy, Disconnect()) |
| 996 | .Times(0); |
| 997 | InitiateDisconnect(service0); |
| 998 | |
| 999 | EXPECT_EQ(service0, GetCurrentService()); |
| 1000 | EXPECT_EQ(service1, GetPendingService()); |
| 1001 | } |
| 1002 | |
| 1003 | TEST_F(WiFiMainTest, DisconnectInvalidService) { |
Gary Morain | d9f893f | 2012-02-06 10:03:40 -0800 | [diff] [blame] | 1004 | EXPECT_CALL(*manager(), RegisterService(_)).Times(AnyNumber()); |
mukesh agrawal | 0ed0f2e | 2011-12-05 20:36:17 +0000 | [diff] [blame] | 1005 | MockSupplicantInterfaceProxy &supplicant_interface_proxy = |
| 1006 | *supplicant_interface_proxy_; |
| 1007 | |
| 1008 | StartWiFi(); |
Thieu Le | e41a72d | 2012-02-06 20:46:51 +0000 | [diff] [blame] | 1009 | ReportBSS("bss0", "ssid0", "00:00:00:00:00:00", 0, 0, kNetworkModeAdHoc); |
mukesh agrawal | 0ed0f2e | 2011-12-05 20:36:17 +0000 | [diff] [blame] | 1010 | WiFiService *service(GetServices().begin()->get()); |
| 1011 | EXPECT_CALL(supplicant_interface_proxy, Disconnect()) |
| 1012 | .Times(0); |
| 1013 | InitiateDisconnect(service); |
| 1014 | } |
| 1015 | |
| 1016 | TEST_F(WiFiMainTest, DisconnectCurrentServiceFailure) { |
Gary Morain | d9f893f | 2012-02-06 10:03:40 -0800 | [diff] [blame] | 1017 | EXPECT_CALL(*manager(), RegisterService(_)).Times(AnyNumber()); |
Paul Stewart | d408fdf | 2012-05-07 17:15:57 -0700 | [diff] [blame] | 1018 | EXPECT_CALL(*dhcp_provider(), CreateConfig(_, _, _, _)).Times(AnyNumber()); |
Gary Morain | d9f893f | 2012-02-06 10:03:40 -0800 | [diff] [blame] | 1019 | EXPECT_CALL(*dhcp_config_.get(), RequestIP()).Times(AnyNumber()); |
| 1020 | EXPECT_CALL(*manager(), UpdateService(_)).Times(AnyNumber()); |
mukesh agrawal | 0ed0f2e | 2011-12-05 20:36:17 +0000 | [diff] [blame] | 1021 | MockSupplicantInterfaceProxy &supplicant_interface_proxy = |
| 1022 | *supplicant_interface_proxy_; |
| 1023 | |
| 1024 | StartWiFi(); |
Thieu Le | e41a72d | 2012-02-06 20:46:51 +0000 | [diff] [blame] | 1025 | ReportBSS("bss0", "ssid0", "00:00:00:00:00:00", 0, 0, kNetworkModeAdHoc); |
mukesh agrawal | 0ed0f2e | 2011-12-05 20:36:17 +0000 | [diff] [blame] | 1026 | |
| 1027 | WiFiService *service(GetServices().begin()->get()); |
| 1028 | DBus::Path fake_path("/fake/path"); |
| 1029 | EXPECT_CALL(supplicant_interface_proxy, AddNetwork(_)) |
| 1030 | .WillOnce(Return(fake_path)); |
| 1031 | InitiateConnect(service); |
| 1032 | ReportCurrentBSSChanged("bss0"); |
| 1033 | ReportStateChanged(wpa_supplicant::kInterfaceStateCompleted); |
| 1034 | |
| 1035 | EXPECT_EQ(service, GetCurrentService()); |
| 1036 | EXPECT_CALL(supplicant_interface_proxy, Disconnect()) |
| 1037 | .WillRepeatedly(Throw( |
| 1038 | DBus::Error( |
| 1039 | "fi.w1.wpa_supplicant1.NotConnected", |
| 1040 | "test threw fi.w1.wpa_supplicant1.NotConnected"))); |
| 1041 | EXPECT_CALL(supplicant_interface_proxy, RemoveNetwork(fake_path)); |
| 1042 | InitiateDisconnect(service); |
| 1043 | |
| 1044 | EXPECT_TRUE(GetCurrentService() == NULL); |
| 1045 | } |
| 1046 | |
mukesh agrawal | 5c4dd0b | 2011-09-14 13:53:14 -0700 | [diff] [blame] | 1047 | TEST_F(WiFiMainTest, Stop) { |
Gary Morain | d9f893f | 2012-02-06 10:03:40 -0800 | [diff] [blame] | 1048 | EXPECT_CALL(*manager(), RegisterService(_)).Times(AnyNumber()); |
mukesh agrawal | c01f398 | 2012-01-24 13:48:39 -0800 | [diff] [blame] | 1049 | StartWiFi(); |
Thieu Le | e41a72d | 2012-02-06 20:46:51 +0000 | [diff] [blame] | 1050 | ReportBSS("bss0", "ssid0", "00:00:00:00:00:00", 0, 0, kNetworkModeAdHoc); |
mukesh agrawal | c01f398 | 2012-01-24 13:48:39 -0800 | [diff] [blame] | 1051 | ReportScanDone(); |
mukesh agrawal | 5c4dd0b | 2011-09-14 13:53:14 -0700 | [diff] [blame] | 1052 | |
mukesh agrawal | c01f398 | 2012-01-24 13:48:39 -0800 | [diff] [blame] | 1053 | EXPECT_CALL(*manager(), DeregisterService(_)); |
| 1054 | StopWiFi(); |
mukesh agrawal | b66c646 | 2012-05-07 11:45:25 -0700 | [diff] [blame] | 1055 | EXPECT_TRUE(GetScanTimer().IsCancelled()); |
mukesh agrawal | 5c4dd0b | 2011-09-14 13:53:14 -0700 | [diff] [blame] | 1056 | } |
| 1057 | |
mukesh agrawal | 7a4e400 | 2011-09-06 11:26:05 -0700 | [diff] [blame] | 1058 | TEST_F(WiFiMainTest, GetWifiServiceOpen) { |
| 1059 | Error e; |
Paul Stewart | a41e38d | 2011-11-11 07:47:29 -0800 | [diff] [blame] | 1060 | GetOpenService(flimflam::kTypeWifi, "an_ssid", flimflam::kModeManaged, &e); |
mukesh agrawal | 7a4e400 | 2011-09-06 11:26:05 -0700 | [diff] [blame] | 1061 | EXPECT_TRUE(e.IsSuccess()); |
| 1062 | } |
| 1063 | |
mukesh agrawal | 7a4e400 | 2011-09-06 11:26:05 -0700 | [diff] [blame] | 1064 | TEST_F(WiFiMainTest, GetWifiServiceOpenNoSSID) { |
| 1065 | Error e; |
Paul Stewart | a41e38d | 2011-11-11 07:47:29 -0800 | [diff] [blame] | 1066 | GetOpenService(flimflam::kTypeWifi, NULL, flimflam::kModeManaged, &e); |
mukesh agrawal | 7a4e400 | 2011-09-06 11:26:05 -0700 | [diff] [blame] | 1067 | EXPECT_EQ(Error::kInvalidArguments, e.type()); |
| 1068 | EXPECT_EQ("must specify SSID", e.message()); |
| 1069 | } |
| 1070 | |
| 1071 | TEST_F(WiFiMainTest, GetWifiServiceOpenLongSSID) { |
| 1072 | Error e; |
| 1073 | GetOpenService( |
Paul Stewart | a41e38d | 2011-11-11 07:47:29 -0800 | [diff] [blame] | 1074 | flimflam::kTypeWifi, "123456789012345678901234567890123", |
| 1075 | flimflam::kModeManaged, &e); |
mukesh agrawal | 7a4e400 | 2011-09-06 11:26:05 -0700 | [diff] [blame] | 1076 | EXPECT_EQ(Error::kInvalidNetworkName, e.type()); |
| 1077 | EXPECT_EQ("SSID is too long", e.message()); |
| 1078 | } |
| 1079 | |
| 1080 | TEST_F(WiFiMainTest, GetWifiServiceOpenShortSSID) { |
| 1081 | Error e; |
Paul Stewart | a41e38d | 2011-11-11 07:47:29 -0800 | [diff] [blame] | 1082 | GetOpenService(flimflam::kTypeWifi, "", flimflam::kModeManaged, &e); |
mukesh agrawal | 7a4e400 | 2011-09-06 11:26:05 -0700 | [diff] [blame] | 1083 | EXPECT_EQ(Error::kInvalidNetworkName, e.type()); |
| 1084 | EXPECT_EQ("SSID is too short", e.message()); |
| 1085 | } |
| 1086 | |
| 1087 | TEST_F(WiFiMainTest, GetWifiServiceOpenBadMode) { |
| 1088 | Error e; |
Paul Stewart | a41e38d | 2011-11-11 07:47:29 -0800 | [diff] [blame] | 1089 | GetOpenService(flimflam::kTypeWifi, "an_ssid", "ad-hoc", &e); |
mukesh agrawal | 7a4e400 | 2011-09-06 11:26:05 -0700 | [diff] [blame] | 1090 | EXPECT_EQ(Error::kNotSupported, e.type()); |
| 1091 | EXPECT_EQ("service mode is unsupported", e.message()); |
| 1092 | } |
| 1093 | |
| 1094 | TEST_F(WiFiMainTest, GetWifiServiceOpenNoMode) { |
| 1095 | Error e; |
Paul Stewart | a41e38d | 2011-11-11 07:47:29 -0800 | [diff] [blame] | 1096 | GetOpenService(flimflam::kTypeWifi, "an_ssid", NULL, &e); |
mukesh agrawal | 7a4e400 | 2011-09-06 11:26:05 -0700 | [diff] [blame] | 1097 | EXPECT_TRUE(e.IsSuccess()); |
| 1098 | } |
| 1099 | |
| 1100 | TEST_F(WiFiMainTest, GetWifiServiceRSN) { |
| 1101 | Error e; |
Paul Stewart | a41e38d | 2011-11-11 07:47:29 -0800 | [diff] [blame] | 1102 | GetService(flimflam::kTypeWifi, "an_ssid", flimflam::kModeManaged, |
| 1103 | flimflam::kSecurityRsn, "secure password", &e); |
mukesh agrawal | 7a4e400 | 2011-09-06 11:26:05 -0700 | [diff] [blame] | 1104 | EXPECT_TRUE(e.IsSuccess()); |
| 1105 | } |
| 1106 | |
| 1107 | TEST_F(WiFiMainTest, GetWifiServiceRSNNoPassword) { |
mukesh agrawal | b172e4a | 2012-04-25 18:15:30 -0700 | [diff] [blame] | 1108 | // When configuring hidden networks, Chrome expects to be able to |
| 1109 | // GetService w/o a password, and supply the password with |
| 1110 | // SetProperty afterwards. |
mukesh agrawal | 7a4e400 | 2011-09-06 11:26:05 -0700 | [diff] [blame] | 1111 | Error e; |
Paul Stewart | a41e38d | 2011-11-11 07:47:29 -0800 | [diff] [blame] | 1112 | GetService(flimflam::kTypeWifi, "an_ssid", flimflam::kModeManaged, |
| 1113 | flimflam::kSecurityRsn, NULL, &e); |
mukesh agrawal | b172e4a | 2012-04-25 18:15:30 -0700 | [diff] [blame] | 1114 | EXPECT_TRUE(e.IsSuccess()); |
mukesh agrawal | 7a4e400 | 2011-09-06 11:26:05 -0700 | [diff] [blame] | 1115 | } |
| 1116 | |
| 1117 | TEST_F(WiFiMainTest, GetWifiServiceBadSecurity) { |
| 1118 | Error e; |
Paul Stewart | a41e38d | 2011-11-11 07:47:29 -0800 | [diff] [blame] | 1119 | GetService(flimflam::kTypeWifi, "an_ssid", flimflam::kModeManaged, "rot-13", |
| 1120 | NULL, &e); |
mukesh agrawal | 7a4e400 | 2011-09-06 11:26:05 -0700 | [diff] [blame] | 1121 | EXPECT_EQ(Error::kNotSupported, e.type()); |
| 1122 | EXPECT_EQ("security mode is unsupported", e.message()); |
| 1123 | } |
| 1124 | |
| 1125 | TEST_F(WiFiMainTest, GetWifiServiceWEPNoPassword) { |
mukesh agrawal | b172e4a | 2012-04-25 18:15:30 -0700 | [diff] [blame] | 1126 | // When configuring hidden networks, Chrome expects to be able to |
| 1127 | // GetService w/o a password, and supply the password with |
| 1128 | // SetProperty afterwards. |
mukesh agrawal | 7a4e400 | 2011-09-06 11:26:05 -0700 | [diff] [blame] | 1129 | Error e; |
Paul Stewart | a41e38d | 2011-11-11 07:47:29 -0800 | [diff] [blame] | 1130 | GetService(flimflam::kTypeWifi, "an_ssid", flimflam::kModeManaged, |
| 1131 | flimflam::kSecurityWep, NULL, &e); |
mukesh agrawal | b172e4a | 2012-04-25 18:15:30 -0700 | [diff] [blame] | 1132 | EXPECT_TRUE(e.IsSuccess()); |
mukesh agrawal | 7a4e400 | 2011-09-06 11:26:05 -0700 | [diff] [blame] | 1133 | } |
| 1134 | |
| 1135 | TEST_F(WiFiMainTest, GetWifiServiceWEPEmptyPassword) { |
| 1136 | Error e; |
Paul Stewart | a41e38d | 2011-11-11 07:47:29 -0800 | [diff] [blame] | 1137 | GetService(flimflam::kTypeWifi, "an_ssid", flimflam::kModeManaged, |
| 1138 | flimflam::kSecurityWep, "", &e); |
mukesh agrawal | 7a4e400 | 2011-09-06 11:26:05 -0700 | [diff] [blame] | 1139 | EXPECT_EQ(Error::kInvalidPassphrase, e.type()); |
| 1140 | } |
| 1141 | |
| 1142 | TEST_F(WiFiMainTest, GetWifiServiceWEP40ASCII) { |
| 1143 | Error e; |
Paul Stewart | a41e38d | 2011-11-11 07:47:29 -0800 | [diff] [blame] | 1144 | GetService(flimflam::kTypeWifi, "an_ssid", flimflam::kModeManaged, |
| 1145 | flimflam::kSecurityWep, "abcde", &e); |
mukesh agrawal | 7a4e400 | 2011-09-06 11:26:05 -0700 | [diff] [blame] | 1146 | EXPECT_TRUE(e.IsSuccess()); |
| 1147 | } |
| 1148 | |
| 1149 | TEST_F(WiFiMainTest, GetWifiServiceWEP104ASCII) { |
| 1150 | Error e; |
Paul Stewart | a41e38d | 2011-11-11 07:47:29 -0800 | [diff] [blame] | 1151 | GetService(flimflam::kTypeWifi, "an_ssid", flimflam::kModeManaged, |
| 1152 | flimflam::kSecurityWep, "abcdefghijklm", &e); |
mukesh agrawal | 7a4e400 | 2011-09-06 11:26:05 -0700 | [diff] [blame] | 1153 | EXPECT_TRUE(e.IsSuccess()); |
| 1154 | } |
| 1155 | |
| 1156 | TEST_F(WiFiMainTest, GetWifiServiceWEP40ASCIIWithKeyIndex) { |
| 1157 | Error e; |
Paul Stewart | a41e38d | 2011-11-11 07:47:29 -0800 | [diff] [blame] | 1158 | GetService(flimflam::kTypeWifi, "an_ssid", flimflam::kModeManaged, |
| 1159 | flimflam::kSecurityWep, "0:abcdefghijklm", &e); |
mukesh agrawal | 7a4e400 | 2011-09-06 11:26:05 -0700 | [diff] [blame] | 1160 | EXPECT_TRUE(e.IsSuccess()); |
| 1161 | } |
| 1162 | |
| 1163 | TEST_F(WiFiMainTest, GetWifiServiceWEP40Hex) { |
| 1164 | Error e; |
Paul Stewart | a41e38d | 2011-11-11 07:47:29 -0800 | [diff] [blame] | 1165 | GetService(flimflam::kTypeWifi, "an_ssid", flimflam::kModeManaged, |
| 1166 | flimflam::kSecurityWep, "0102030405", &e); |
mukesh agrawal | 7a4e400 | 2011-09-06 11:26:05 -0700 | [diff] [blame] | 1167 | EXPECT_TRUE(e.IsSuccess()); |
| 1168 | } |
| 1169 | |
| 1170 | TEST_F(WiFiMainTest, GetWifiServiceWEP40HexBadPassphrase) { |
| 1171 | Error e; |
Paul Stewart | a41e38d | 2011-11-11 07:47:29 -0800 | [diff] [blame] | 1172 | GetService(flimflam::kTypeWifi, "an_ssid", flimflam::kModeManaged, |
| 1173 | flimflam::kSecurityWep, "O102030405", &e); |
mukesh agrawal | 7a4e400 | 2011-09-06 11:26:05 -0700 | [diff] [blame] | 1174 | EXPECT_EQ(Error::kInvalidPassphrase, e.type()); |
| 1175 | } |
| 1176 | |
| 1177 | TEST_F(WiFiMainTest, GetWifiServiceWEP40HexWithKeyIndexBadPassphrase) { |
| 1178 | Error e; |
Paul Stewart | a41e38d | 2011-11-11 07:47:29 -0800 | [diff] [blame] | 1179 | GetService(flimflam::kTypeWifi, "an_ssid", flimflam::kModeManaged, |
| 1180 | flimflam::kSecurityWep, "1:O102030405", &e); |
mukesh agrawal | 7a4e400 | 2011-09-06 11:26:05 -0700 | [diff] [blame] | 1181 | EXPECT_EQ(Error::kInvalidPassphrase, e.type()); |
| 1182 | } |
| 1183 | |
| 1184 | TEST_F(WiFiMainTest, GetWifiServiceWEP40HexWithKeyIndexAndBaseBadPassphrase) { |
| 1185 | Error e; |
Paul Stewart | a41e38d | 2011-11-11 07:47:29 -0800 | [diff] [blame] | 1186 | GetService(flimflam::kTypeWifi, "an_ssid", flimflam::kModeManaged, |
| 1187 | flimflam::kSecurityWep, "1:0xO102030405", &e); |
mukesh agrawal | 7a4e400 | 2011-09-06 11:26:05 -0700 | [diff] [blame] | 1188 | EXPECT_EQ(Error::kInvalidPassphrase, e.type()); |
| 1189 | } |
| 1190 | |
| 1191 | TEST_F(WiFiMainTest, GetWifiServiceWEP40HexWithBaseBadPassphrase) { |
| 1192 | Error e; |
Paul Stewart | a41e38d | 2011-11-11 07:47:29 -0800 | [diff] [blame] | 1193 | GetService(flimflam::kTypeWifi, "an_ssid", flimflam::kModeManaged, |
| 1194 | flimflam::kSecurityWep, "0xO102030405", &e); |
mukesh agrawal | 7a4e400 | 2011-09-06 11:26:05 -0700 | [diff] [blame] | 1195 | EXPECT_EQ(Error::kInvalidPassphrase, e.type()); |
| 1196 | } |
| 1197 | |
| 1198 | TEST_F(WiFiMainTest, GetWifiServiceWEP104Hex) { |
| 1199 | Error e; |
Paul Stewart | a41e38d | 2011-11-11 07:47:29 -0800 | [diff] [blame] | 1200 | GetService(flimflam::kTypeWifi, "an_ssid", flimflam::kModeManaged, |
| 1201 | flimflam::kSecurityWep, "0102030405060708090a0b0c0d", &e); |
mukesh agrawal | 7a4e400 | 2011-09-06 11:26:05 -0700 | [diff] [blame] | 1202 | EXPECT_TRUE(e.IsSuccess()); |
| 1203 | } |
| 1204 | |
| 1205 | TEST_F(WiFiMainTest, GetWifiServiceWEP104HexUppercase) { |
| 1206 | Error e; |
Paul Stewart | a41e38d | 2011-11-11 07:47:29 -0800 | [diff] [blame] | 1207 | GetService(flimflam::kTypeWifi, "an_ssid", flimflam::kModeManaged, |
| 1208 | flimflam::kSecurityWep, "0102030405060708090A0B0C0D", &e); |
mukesh agrawal | 7a4e400 | 2011-09-06 11:26:05 -0700 | [diff] [blame] | 1209 | EXPECT_TRUE(e.IsSuccess()); |
| 1210 | } |
| 1211 | |
| 1212 | TEST_F(WiFiMainTest, GetWifiServiceWEP104HexWithKeyIndex) { |
| 1213 | Error e; |
Paul Stewart | a41e38d | 2011-11-11 07:47:29 -0800 | [diff] [blame] | 1214 | GetService(flimflam::kTypeWifi, "an_ssid", flimflam::kModeManaged, |
| 1215 | flimflam::kSecurityWep, "0:0102030405060708090a0b0c0d", &e); |
mukesh agrawal | 7a4e400 | 2011-09-06 11:26:05 -0700 | [diff] [blame] | 1216 | EXPECT_TRUE(e.IsSuccess()); |
| 1217 | } |
| 1218 | |
| 1219 | TEST_F(WiFiMainTest, GetWifiServiceWEP104HexWithKeyIndexAndBase) { |
| 1220 | Error e; |
Paul Stewart | a41e38d | 2011-11-11 07:47:29 -0800 | [diff] [blame] | 1221 | GetService(flimflam::kTypeWifi, "an_ssid", flimflam::kModeManaged, |
| 1222 | flimflam::kSecurityWep, "0:0x0102030405060708090a0b0c0d", &e); |
mukesh agrawal | 7a4e400 | 2011-09-06 11:26:05 -0700 | [diff] [blame] | 1223 | EXPECT_TRUE(e.IsSuccess()); |
| 1224 | } |
| 1225 | |
mukesh agrawal | 8ede052 | 2011-10-03 14:57:44 -0700 | [diff] [blame] | 1226 | class WiFiGetServiceSuccessTest : public WiFiMainTest {}; |
| 1227 | class WiFiGetServiceFailureTest : public WiFiMainTest {}; |
| 1228 | |
| 1229 | TEST_P(WiFiGetServiceSuccessTest, Passphrase) { |
| 1230 | Error e; |
Paul Stewart | a41e38d | 2011-11-11 07:47:29 -0800 | [diff] [blame] | 1231 | GetService(flimflam::kTypeWifi, "an_ssid", flimflam::kModeManaged, |
| 1232 | flimflam::kSecurityWpa, GetParam().c_str(), &e); |
mukesh agrawal | 8ede052 | 2011-10-03 14:57:44 -0700 | [diff] [blame] | 1233 | EXPECT_TRUE(e.IsSuccess()); |
| 1234 | } |
| 1235 | |
| 1236 | TEST_P(WiFiGetServiceFailureTest, Passphrase) { |
| 1237 | Error e; |
Paul Stewart | a41e38d | 2011-11-11 07:47:29 -0800 | [diff] [blame] | 1238 | GetService(flimflam::kTypeWifi, "an_ssid", flimflam::kModeManaged, |
| 1239 | flimflam::kSecurityWpa, GetParam().c_str(), &e); |
mukesh agrawal | 8ede052 | 2011-10-03 14:57:44 -0700 | [diff] [blame] | 1240 | EXPECT_EQ(Error::kInvalidPassphrase, e.type()); |
| 1241 | } |
| 1242 | |
| 1243 | INSTANTIATE_TEST_CASE_P( |
| 1244 | WiFiGetServiceSuccessTestInstance, |
| 1245 | WiFiGetServiceSuccessTest, |
| 1246 | Values( |
| 1247 | string(IEEE_80211::kWPAAsciiMinLen, 'Z'), |
| 1248 | string(IEEE_80211::kWPAAsciiMaxLen, 'Z'), |
| 1249 | // subtle: invalid length for hex key, but valid as ascii passphrase |
| 1250 | string(IEEE_80211::kWPAHexLen-1, '1'), |
| 1251 | string(IEEE_80211::kWPAHexLen, '1'))); |
| 1252 | |
| 1253 | INSTANTIATE_TEST_CASE_P( |
| 1254 | WiFiGetServiceFailureTestInstance, |
| 1255 | WiFiGetServiceFailureTest, |
| 1256 | Values( |
| 1257 | string(IEEE_80211::kWPAAsciiMinLen-1, 'Z'), |
| 1258 | string(IEEE_80211::kWPAAsciiMaxLen+1, 'Z'), |
| 1259 | string(IEEE_80211::kWPAHexLen+1, '1'))); |
| 1260 | |
Paul Stewart | 6ab23a9 | 2011-11-09 17:17:47 -0800 | [diff] [blame] | 1261 | TEST_F(WiFiMainTest, FindServiceWEP) { |
| 1262 | const string ssid("an_ssid"); |
| 1263 | { |
| 1264 | Error e; |
Paul Stewart | a41e38d | 2011-11-11 07:47:29 -0800 | [diff] [blame] | 1265 | GetService(flimflam::kTypeWifi, ssid.c_str(), flimflam::kModeManaged, |
Paul Stewart | 6ab23a9 | 2011-11-09 17:17:47 -0800 | [diff] [blame] | 1266 | flimflam::kSecurityWep, "abcde", &e); |
| 1267 | EXPECT_TRUE(e.IsSuccess()); |
| 1268 | } |
| 1269 | vector<uint8_t> ssid_bytes(ssid.begin(), ssid.end()); |
| 1270 | |
Paul Stewart | a41e38d | 2011-11-11 07:47:29 -0800 | [diff] [blame] | 1271 | EXPECT_TRUE(FindService(ssid_bytes, flimflam::kModeManaged, |
| 1272 | flimflam::kSecurityWep).get()); |
| 1273 | EXPECT_FALSE(FindService(ssid_bytes, flimflam::kModeManaged, |
| 1274 | flimflam::kSecurityWpa).get()); |
Paul Stewart | 6ab23a9 | 2011-11-09 17:17:47 -0800 | [diff] [blame] | 1275 | } |
| 1276 | |
| 1277 | TEST_F(WiFiMainTest, FindServiceWPA) { |
| 1278 | const string ssid("an_ssid"); |
| 1279 | { |
| 1280 | Error e; |
Paul Stewart | a41e38d | 2011-11-11 07:47:29 -0800 | [diff] [blame] | 1281 | GetService(flimflam::kTypeWifi, ssid.c_str(), flimflam::kModeManaged, |
Paul Stewart | 6ab23a9 | 2011-11-09 17:17:47 -0800 | [diff] [blame] | 1282 | flimflam::kSecurityRsn, "abcdefgh", &e); |
| 1283 | EXPECT_TRUE(e.IsSuccess()); |
| 1284 | } |
| 1285 | vector<uint8_t> ssid_bytes(ssid.begin(), ssid.end()); |
Paul Stewart | a41e38d | 2011-11-11 07:47:29 -0800 | [diff] [blame] | 1286 | WiFiServiceRefPtr wpa_service(FindService(ssid_bytes, flimflam::kModeManaged, |
| 1287 | flimflam::kSecurityWpa)); |
Paul Stewart | 6ab23a9 | 2011-11-09 17:17:47 -0800 | [diff] [blame] | 1288 | EXPECT_TRUE(wpa_service.get()); |
Paul Stewart | a41e38d | 2011-11-11 07:47:29 -0800 | [diff] [blame] | 1289 | WiFiServiceRefPtr rsn_service(FindService(ssid_bytes, flimflam::kModeManaged, |
| 1290 | flimflam::kSecurityRsn)); |
Paul Stewart | 6ab23a9 | 2011-11-09 17:17:47 -0800 | [diff] [blame] | 1291 | EXPECT_TRUE(rsn_service.get()); |
| 1292 | EXPECT_EQ(wpa_service.get(), rsn_service.get()); |
Paul Stewart | a41e38d | 2011-11-11 07:47:29 -0800 | [diff] [blame] | 1293 | WiFiServiceRefPtr psk_service(FindService(ssid_bytes, flimflam::kModeManaged, |
| 1294 | flimflam::kSecurityPsk)); |
Paul Stewart | 6ab23a9 | 2011-11-09 17:17:47 -0800 | [diff] [blame] | 1295 | EXPECT_EQ(wpa_service.get(), psk_service.get()); |
| 1296 | // Indirectly test FindService by doing a GetService on something that |
| 1297 | // already exists. |
| 1298 | { |
| 1299 | Error e; |
| 1300 | WiFiServiceRefPtr wpa_service2( |
Paul Stewart | a41e38d | 2011-11-11 07:47:29 -0800 | [diff] [blame] | 1301 | GetServiceInner(flimflam::kTypeWifi, ssid.c_str(), |
| 1302 | flimflam::kModeManaged, flimflam::kSecurityWpa, |
| 1303 | "abcdefgh", false, &e)); |
Paul Stewart | 6ab23a9 | 2011-11-09 17:17:47 -0800 | [diff] [blame] | 1304 | EXPECT_TRUE(e.IsSuccess()); |
| 1305 | EXPECT_EQ(wpa_service.get(), wpa_service2.get()); |
| 1306 | } |
| 1307 | } |
| 1308 | |
Paul Stewart | cb59fed | 2012-03-21 21:14:46 -0700 | [diff] [blame] | 1309 | TEST_F(WiFiMainTest, GetServiceWithGUID) { |
| 1310 | // Perform a GetService that also configures properties in the base Service |
| 1311 | // class using Service::Configure(). |
| 1312 | KeyValueStore args; |
| 1313 | args.SetString(flimflam::kTypeProperty, flimflam::kTypeWifi); |
| 1314 | args.SetString(flimflam::kSSIDProperty, "ssid"); |
| 1315 | args.SetString(flimflam::kSecurityProperty, flimflam::kSecurityNone); |
| 1316 | const string kGUID = "aguid"; // Stored as a registered Service property. |
| 1317 | args.SetString(flimflam::kGuidProperty, kGUID); |
| 1318 | Error e; |
| 1319 | WiFiServiceRefPtr service = GetServiceWithKeyValues(args, &e); |
| 1320 | EXPECT_TRUE(e.IsSuccess()); |
| 1321 | EXPECT_EQ(kGUID, service->guid()); |
| 1322 | } |
| 1323 | |
Paul Stewart | ced6a0b | 2011-11-08 15:32:04 -0800 | [diff] [blame] | 1324 | MATCHER_P(HasHiddenSSID, ssid, "") { |
Paul Stewart | a41e38d | 2011-11-11 07:47:29 -0800 | [diff] [blame] | 1325 | map<string, DBus::Variant>::const_iterator it = |
Paul Stewart | ced6a0b | 2011-11-08 15:32:04 -0800 | [diff] [blame] | 1326 | arg.find(wpa_supplicant::kPropertyScanSSIDs); |
| 1327 | if (it == arg.end()) { |
| 1328 | return false; |
| 1329 | } |
| 1330 | |
| 1331 | const DBus::Variant &ssids_variant = it->second; |
| 1332 | EXPECT_TRUE(DBusAdaptor::IsByteArrays(ssids_variant.signature())); |
| 1333 | const ByteArrays &ssids = it->second.operator ByteArrays(); |
| 1334 | // A valid Scan containing a single hidden SSID should contain |
| 1335 | // two SSID entries: one containing the SSID we are looking for, |
| 1336 | // and an empty entry, signifying that we also want to do a |
| 1337 | // broadcast probe request for all non-hidden APs as well. |
| 1338 | return ssids.size() == 2 && |
| 1339 | string(ssids[0].begin(), ssids[0].end()) == ssid && |
| 1340 | ssids[1].empty(); |
| 1341 | } |
| 1342 | |
| 1343 | TEST_F(WiFiMainTest, ScanHidden) { |
| 1344 | EXPECT_CALL(*supplicant_process_proxy_, CreateInterface(_)); |
| 1345 | EXPECT_CALL(*supplicant_process_proxy_, GetInterface(_)) |
| 1346 | .Times(AnyNumber()) |
| 1347 | .WillRepeatedly(Throw( |
| 1348 | DBus::Error( |
| 1349 | "fi.w1.wpa_supplicant1.InterfaceUnknown", |
| 1350 | "test threw fi.w1.wpa_supplicant1.InterfaceUnknown"))); |
Paul Stewart | 10ccbb3 | 2012-04-26 15:59:30 -0700 | [diff] [blame] | 1351 | scoped_refptr<MockProfile> profile( |
| 1352 | new NiceMock<MockProfile>(control_interface(), manager(), "")); |
Paul Stewart | a41e38d | 2011-11-11 07:47:29 -0800 | [diff] [blame] | 1353 | { |
Paul Stewart | 10ccbb3 | 2012-04-26 15:59:30 -0700 | [diff] [blame] | 1354 | // Create a hidden service with an associated profile. |
Paul Stewart | a41e38d | 2011-11-11 07:47:29 -0800 | [diff] [blame] | 1355 | Error e; |
| 1356 | WiFiServiceRefPtr service = |
| 1357 | GetServiceInner(flimflam::kTypeWifi, "ssid0", flimflam::kModeManaged, |
| 1358 | NULL, NULL, true, &e); |
| 1359 | EXPECT_TRUE(e.IsSuccess()); |
| 1360 | EXPECT_TRUE(service->hidden_ssid()); |
Paul Stewart | 10ccbb3 | 2012-04-26 15:59:30 -0700 | [diff] [blame] | 1361 | service->set_profile(profile); |
Paul Stewart | a41e38d | 2011-11-11 07:47:29 -0800 | [diff] [blame] | 1362 | } |
| 1363 | { |
Paul Stewart | 10ccbb3 | 2012-04-26 15:59:30 -0700 | [diff] [blame] | 1364 | // Create a hidden service without an associated profile. |
Paul Stewart | a41e38d | 2011-11-11 07:47:29 -0800 | [diff] [blame] | 1365 | Error e; |
| 1366 | WiFiServiceRefPtr service = |
| 1367 | GetServiceInner(flimflam::kTypeWifi, "ssid1", flimflam::kModeManaged, |
| 1368 | NULL, NULL, true, &e); |
| 1369 | EXPECT_TRUE(e.IsSuccess()); |
| 1370 | EXPECT_TRUE(service->hidden_ssid()); |
| 1371 | } |
| 1372 | { |
Paul Stewart | 10ccbb3 | 2012-04-26 15:59:30 -0700 | [diff] [blame] | 1373 | // Create a non-hidden service with an associated profile. |
Paul Stewart | a41e38d | 2011-11-11 07:47:29 -0800 | [diff] [blame] | 1374 | Error e; |
| 1375 | WiFiServiceRefPtr service = |
| 1376 | GetServiceInner(flimflam::kTypeWifi, "ssid2", flimflam::kModeManaged, |
| 1377 | NULL, NULL, false, &e); |
| 1378 | EXPECT_TRUE(e.IsSuccess()); |
| 1379 | EXPECT_FALSE(service->hidden_ssid()); |
Paul Stewart | 10ccbb3 | 2012-04-26 15:59:30 -0700 | [diff] [blame] | 1380 | service->set_profile(profile); |
Paul Stewart | a41e38d | 2011-11-11 07:47:29 -0800 | [diff] [blame] | 1381 | } |
| 1382 | EXPECT_CALL(*supplicant_interface_proxy_, Scan(HasHiddenSSID("ssid0"))); |
Paul Stewart | ced6a0b | 2011-11-08 15:32:04 -0800 | [diff] [blame] | 1383 | StartWiFi(); |
| 1384 | dispatcher_.DispatchPendingEvents(); |
| 1385 | } |
| 1386 | |
mukesh agrawal | 7ec7131 | 2011-11-10 02:08:26 +0000 | [diff] [blame] | 1387 | TEST_F(WiFiMainTest, InitialSupplicantState) { |
| 1388 | EXPECT_EQ(WiFi::kInterfaceStateUnknown, GetSupplicantState()); |
| 1389 | } |
| 1390 | |
| 1391 | TEST_F(WiFiMainTest, StateChangeNoService) { |
| 1392 | // State change should succeed even if there is no pending Service. |
| 1393 | ReportStateChanged(wpa_supplicant::kInterfaceStateScanning); |
| 1394 | EXPECT_EQ(wpa_supplicant::kInterfaceStateScanning, GetSupplicantState()); |
| 1395 | } |
| 1396 | |
| 1397 | TEST_F(WiFiMainTest, StateChangeWithService) { |
| 1398 | // Forward transition should trigger a Service state change. |
| 1399 | StartWiFi(); |
| 1400 | dispatcher_.DispatchPendingEvents(); |
mukesh agrawal | cf24a24 | 2012-05-21 16:46:11 -0700 | [diff] [blame] | 1401 | MockWiFiServiceRefPtr service = MakeMockService(flimflam::kSecurityNone); |
mukesh agrawal | 7ec7131 | 2011-11-10 02:08:26 +0000 | [diff] [blame] | 1402 | InitiateConnect(service); |
| 1403 | EXPECT_CALL(*service.get(), SetState(Service::kStateAssociating)); |
| 1404 | ReportStateChanged(wpa_supplicant::kInterfaceStateAssociated); |
| 1405 | // Verify expectations now, because WiFi may report other state changes |
| 1406 | // when WiFi is Stop()-ed (during TearDown()). |
| 1407 | Mock::VerifyAndClearExpectations(service.get()); |
Gary Morain | d9f893f | 2012-02-06 10:03:40 -0800 | [diff] [blame] | 1408 | EXPECT_CALL(*service.get(), SetState(_)).Times(AnyNumber()); |
mukesh agrawal | 7ec7131 | 2011-11-10 02:08:26 +0000 | [diff] [blame] | 1409 | } |
| 1410 | |
| 1411 | TEST_F(WiFiMainTest, StateChangeBackwardsWithService) { |
| 1412 | // Some backwards transitions should not trigger a Service state change. |
| 1413 | // Supplicant state should still be updated, however. |
Paul Stewart | d408fdf | 2012-05-07 17:15:57 -0700 | [diff] [blame] | 1414 | EXPECT_CALL(*dhcp_provider(), CreateConfig(_, _, _, _)).Times(AnyNumber()); |
Gary Morain | d9f893f | 2012-02-06 10:03:40 -0800 | [diff] [blame] | 1415 | EXPECT_CALL(*dhcp_config_.get(), RequestIP()).Times(AnyNumber()); |
mukesh agrawal | 7ec7131 | 2011-11-10 02:08:26 +0000 | [diff] [blame] | 1416 | StartWiFi(); |
| 1417 | dispatcher_.DispatchPendingEvents(); |
mukesh agrawal | cf24a24 | 2012-05-21 16:46:11 -0700 | [diff] [blame] | 1418 | MockWiFiServiceRefPtr service = MakeMockService(flimflam::kSecurityNone); |
Gary Morain | d9f893f | 2012-02-06 10:03:40 -0800 | [diff] [blame] | 1419 | EXPECT_CALL(*service.get(), SetState(Service::kStateConfiguring)); |
mukesh agrawal | 7ec7131 | 2011-11-10 02:08:26 +0000 | [diff] [blame] | 1420 | InitiateConnect(service); |
| 1421 | ReportStateChanged(wpa_supplicant::kInterfaceStateCompleted); |
mukesh agrawal | 7ec7131 | 2011-11-10 02:08:26 +0000 | [diff] [blame] | 1422 | ReportStateChanged(wpa_supplicant::kInterfaceStateAuthenticating); |
| 1423 | EXPECT_EQ(wpa_supplicant::kInterfaceStateAuthenticating, |
| 1424 | GetSupplicantState()); |
| 1425 | // Verify expectations now, because WiFi may report other state changes |
| 1426 | // when WiFi is Stop()-ed (during TearDown()). |
| 1427 | Mock::VerifyAndClearExpectations(service.get()); |
Gary Morain | d9f893f | 2012-02-06 10:03:40 -0800 | [diff] [blame] | 1428 | EXPECT_CALL(*service.get(), SetState(_)).Times(AnyNumber()); |
mukesh agrawal | 7ec7131 | 2011-11-10 02:08:26 +0000 | [diff] [blame] | 1429 | } |
| 1430 | |
Paul Stewart | a41e38d | 2011-11-11 07:47:29 -0800 | [diff] [blame] | 1431 | TEST_F(WiFiMainTest, LoadHiddenServicesFailWithNoGroups) { |
| 1432 | StrictMock<MockStore> storage; |
| 1433 | EXPECT_CALL(storage, GetGroupsWithKey(flimflam::kWifiHiddenSsid)) |
| 1434 | .WillOnce(Return(set<string>())); |
| 1435 | EXPECT_FALSE(LoadHiddenServices(&storage)); |
| 1436 | } |
| 1437 | |
| 1438 | TEST_F(WiFiMainTest, LoadHiddenServicesFailWithMissingHidden) { |
| 1439 | string id; |
| 1440 | StrictMock<MockStore> storage; |
| 1441 | SetupHiddenStorage(&storage, "an_ssid", &id); |
| 1442 | // Missing "Hidden" property. |
| 1443 | EXPECT_CALL(storage, GetBool(StrEq(id), flimflam::kWifiHiddenSsid, _)) |
| 1444 | .WillOnce(Return(false)); |
| 1445 | EXPECT_FALSE(LoadHiddenServices(&storage)); |
| 1446 | } |
| 1447 | |
| 1448 | TEST_F(WiFiMainTest, LoadHiddenServicesFailWithFalseHidden) { |
| 1449 | string id; |
| 1450 | StrictMock<MockStore> storage; |
| 1451 | SetupHiddenStorage(&storage, "an_ssid", &id); |
| 1452 | // "Hidden" property set to "false". |
| 1453 | EXPECT_CALL(storage, GetBool(StrEq(id), flimflam::kWifiHiddenSsid, _)) |
| 1454 | .WillOnce(DoAll(SetArgumentPointee<2>(true), Return(false))); |
| 1455 | EXPECT_FALSE(LoadHiddenServices(&storage)); |
| 1456 | } |
| 1457 | |
| 1458 | TEST_F(WiFiMainTest, LoadHiddenServicesFailWithMissingSSID) { |
| 1459 | string id; |
| 1460 | StrictMock<MockStore> storage; |
| 1461 | SetupHiddenStorage(&storage, "an_ssid", &id); |
| 1462 | // Missing "SSID" property. |
| 1463 | EXPECT_CALL(storage, GetString(StrEq(id), flimflam::kSSIDProperty, _)) |
| 1464 | .WillOnce(Return(false)); |
| 1465 | EXPECT_FALSE(LoadHiddenServices(&storage)); |
| 1466 | } |
| 1467 | |
| 1468 | |
| 1469 | TEST_F(WiFiMainTest, LoadHiddenServicesFailWithFoundService) { |
| 1470 | StrictMock<MockStore> storage; |
| 1471 | string id; |
| 1472 | SetupHiddenStorage(&storage, "an_ssid", &id); |
| 1473 | Error e; |
| 1474 | GetOpenService(flimflam::kTypeWifi, "an_ssid", NULL, &e); |
| 1475 | ASSERT_TRUE(e.IsSuccess()); |
| 1476 | EXPECT_FALSE(LoadHiddenServices(&storage)); |
| 1477 | } |
| 1478 | |
| 1479 | TEST_F(WiFiMainTest, LoadHiddenServicesSuccess) { |
Gary Morain | d9f893f | 2012-02-06 10:03:40 -0800 | [diff] [blame] | 1480 | EXPECT_CALL(*manager(), RegisterService(_)).Times(AnyNumber()); |
Paul Stewart | a41e38d | 2011-11-11 07:47:29 -0800 | [diff] [blame] | 1481 | StrictMock<MockStore> storage; |
| 1482 | string ssid("an_ssid"); |
| 1483 | string id; |
| 1484 | SetupHiddenStorage(&storage, ssid, &id); |
| 1485 | EXPECT_TRUE(LoadHiddenServices(&storage)); |
| 1486 | vector<uint8_t> ssid_bytes(ssid.begin(), ssid.end()); |
| 1487 | EXPECT_TRUE(FindService(ssid_bytes, flimflam::kModeManaged, |
| 1488 | flimflam::kSecurityNone).get()); |
| 1489 | } |
| 1490 | |
mukesh agrawal | 1590839 | 2011-11-16 18:29:25 +0000 | [diff] [blame] | 1491 | TEST_F(WiFiMainTest, CurrentBSSChangeConnectedToDisconnected) { |
Gary Morain | d9f893f | 2012-02-06 10:03:40 -0800 | [diff] [blame] | 1492 | EXPECT_CALL(*manager(), HasService(_)).Times(AnyNumber()); |
Paul Stewart | d408fdf | 2012-05-07 17:15:57 -0700 | [diff] [blame] | 1493 | EXPECT_CALL(*dhcp_provider(), CreateConfig(_, _, _, _)).Times(AnyNumber()); |
Gary Morain | d9f893f | 2012-02-06 10:03:40 -0800 | [diff] [blame] | 1494 | EXPECT_CALL(*dhcp_config_.get(), RequestIP()).Times(AnyNumber()); |
| 1495 | EXPECT_CALL(*manager(), UpdateService(_)).Times(AnyNumber()); |
mukesh agrawal | 1590839 | 2011-11-16 18:29:25 +0000 | [diff] [blame] | 1496 | WiFiEndpointRefPtr ap = MakeEndpoint("an_ssid", "00:01:02:03:04:05"); |
| 1497 | WiFiServiceRefPtr service = CreateServiceForEndpoint(*ap); |
| 1498 | |
| 1499 | // Note that the BSS handle used in this test ("an_ap") is not |
| 1500 | // intended to reflect the format used by supplicant. It's just |
| 1501 | // convenient for testing. |
| 1502 | |
| 1503 | StartWiFi(); |
Thieu Le | e41a72d | 2012-02-06 20:46:51 +0000 | [diff] [blame] | 1504 | ReportBSS("an_ap", ap->ssid_string(), ap->bssid_string(), 0, 0, |
mukesh agrawal | 1590839 | 2011-11-16 18:29:25 +0000 | [diff] [blame] | 1505 | kNetworkModeInfrastructure); |
| 1506 | InitiateConnect(service); |
| 1507 | EXPECT_EQ(service, GetPendingService().get()); |
| 1508 | |
| 1509 | ReportCurrentBSSChanged("an_ap"); |
| 1510 | ReportStateChanged(wpa_supplicant::kInterfaceStateCompleted); |
| 1511 | EXPECT_EQ(Service::kStateConfiguring, service->state()); |
| 1512 | EXPECT_EQ(service, GetCurrentService().get()); |
| 1513 | EXPECT_EQ(NULL, GetPendingService().get()); |
| 1514 | |
| 1515 | ReportCurrentBSSChanged(wpa_supplicant::kCurrentBSSNull); |
mukesh agrawal | 568b5c6 | 2012-02-28 14:44:47 -0800 | [diff] [blame] | 1516 | EXPECT_EQ(Service::kStateIdle, service->state()); |
| 1517 | EXPECT_TRUE(service->IsFailed()); |
mukesh agrawal | 1590839 | 2011-11-16 18:29:25 +0000 | [diff] [blame] | 1518 | EXPECT_EQ(NULL, GetCurrentService().get()); |
| 1519 | EXPECT_EQ(NULL, GetPendingService().get()); |
| 1520 | } |
| 1521 | |
| 1522 | TEST_F(WiFiMainTest, CurrentBSSChangeConnectedToConnectedNewService) { |
Gary Morain | d9f893f | 2012-02-06 10:03:40 -0800 | [diff] [blame] | 1523 | EXPECT_CALL(*manager(), HasService(_)).Times(AnyNumber()); |
Paul Stewart | d408fdf | 2012-05-07 17:15:57 -0700 | [diff] [blame] | 1524 | EXPECT_CALL(*dhcp_provider(), CreateConfig(_, _, _, _)).Times(AnyNumber()); |
Gary Morain | d9f893f | 2012-02-06 10:03:40 -0800 | [diff] [blame] | 1525 | EXPECT_CALL(*dhcp_config_.get(), RequestIP()).Times(AnyNumber()); |
| 1526 | EXPECT_CALL(*manager(), UpdateService(_)).Times(AnyNumber()); |
mukesh agrawal | 1590839 | 2011-11-16 18:29:25 +0000 | [diff] [blame] | 1527 | WiFiEndpointRefPtr ap1 = MakeEndpoint("an_ssid", "00:01:02:03:04:05"); |
| 1528 | WiFiEndpointRefPtr ap2 = MakeEndpoint("another_ssid", "01:02:03:04:05:06"); |
| 1529 | WiFiServiceRefPtr service1 = CreateServiceForEndpoint(*ap1); |
| 1530 | WiFiServiceRefPtr service2 = CreateServiceForEndpoint(*ap2); |
| 1531 | |
| 1532 | // Note that the BSS handles used in this test ("ap1", "ap2") are |
| 1533 | // not intended to reflect the format used by supplicant. They're |
| 1534 | // just convenient for testing. |
| 1535 | |
| 1536 | StartWiFi(); |
Thieu Le | e41a72d | 2012-02-06 20:46:51 +0000 | [diff] [blame] | 1537 | ReportBSS("ap1", ap1->ssid_string(), ap1->bssid_string(), 0, 0, |
mukesh agrawal | 1590839 | 2011-11-16 18:29:25 +0000 | [diff] [blame] | 1538 | kNetworkModeInfrastructure); |
Thieu Le | e41a72d | 2012-02-06 20:46:51 +0000 | [diff] [blame] | 1539 | ReportBSS("ap2", ap2->ssid_string(), ap2->bssid_string(), 0, 0, |
mukesh agrawal | 1590839 | 2011-11-16 18:29:25 +0000 | [diff] [blame] | 1540 | kNetworkModeInfrastructure); |
| 1541 | InitiateConnect(service1); |
| 1542 | ReportCurrentBSSChanged("ap1"); |
| 1543 | ReportStateChanged(wpa_supplicant::kInterfaceStateCompleted); |
| 1544 | EXPECT_EQ(service1.get(), GetCurrentService().get()); |
| 1545 | |
mukesh agrawal | c01f398 | 2012-01-24 13:48:39 -0800 | [diff] [blame] | 1546 | // Note that we deliberately omit intermediate supplicant states |
| 1547 | // (e.g. kInterfaceStateAssociating), on the theory that they are |
| 1548 | // unreliable. Specifically, they may be quashed if the association |
| 1549 | // completes before supplicant flushes its changed properties. |
mukesh agrawal | 1590839 | 2011-11-16 18:29:25 +0000 | [diff] [blame] | 1550 | ReportCurrentBSSChanged("ap2"); |
| 1551 | ReportStateChanged(wpa_supplicant::kInterfaceStateCompleted); |
| 1552 | EXPECT_EQ(service2.get(), GetCurrentService().get()); |
| 1553 | EXPECT_EQ(Service::kStateIdle, service1->state()); |
| 1554 | EXPECT_EQ(Service::kStateConfiguring, service2->state()); |
| 1555 | } |
| 1556 | |
| 1557 | TEST_F(WiFiMainTest, CurrentBSSChangeDisconnectedToConnected) { |
Gary Morain | d9f893f | 2012-02-06 10:03:40 -0800 | [diff] [blame] | 1558 | EXPECT_CALL(*manager(), HasService(_)).Times(AnyNumber()); |
| 1559 | EXPECT_CALL(*manager(), UpdateService(_)).Times(AnyNumber()); |
Paul Stewart | d408fdf | 2012-05-07 17:15:57 -0700 | [diff] [blame] | 1560 | EXPECT_CALL(*dhcp_provider(), CreateConfig(_, _, _, _)).Times(AnyNumber()); |
Gary Morain | d9f893f | 2012-02-06 10:03:40 -0800 | [diff] [blame] | 1561 | EXPECT_CALL(*dhcp_config_.get(), RequestIP()).Times(AnyNumber()); |
mukesh agrawal | 1590839 | 2011-11-16 18:29:25 +0000 | [diff] [blame] | 1562 | WiFiEndpointRefPtr ap = MakeEndpoint("an_ssid", "00:01:02:03:04:05"); |
| 1563 | WiFiServiceRefPtr service = CreateServiceForEndpoint(*ap); |
| 1564 | |
| 1565 | // Note that the BSS handle used in this test ("an_ap") is not |
| 1566 | // intended to reflect the format used by supplicant. It's just |
| 1567 | // convenient for testing. |
| 1568 | |
| 1569 | StartWiFi(); |
Thieu Le | e41a72d | 2012-02-06 20:46:51 +0000 | [diff] [blame] | 1570 | ReportBSS("an_ap", ap->ssid_string(), ap->bssid_string(), 0, 0, |
mukesh agrawal | 1590839 | 2011-11-16 18:29:25 +0000 | [diff] [blame] | 1571 | kNetworkModeInfrastructure); |
| 1572 | InitiateConnect(service); |
| 1573 | ReportCurrentBSSChanged("an_ap"); |
| 1574 | ReportStateChanged(wpa_supplicant::kInterfaceStateCompleted); |
| 1575 | EXPECT_EQ(service.get(), GetCurrentService().get()); |
| 1576 | EXPECT_EQ(Service::kStateConfiguring, service->state()); |
| 1577 | } |
| 1578 | |
Thieu Le | e41a72d | 2012-02-06 20:46:51 +0000 | [diff] [blame] | 1579 | TEST_F(WiFiMainTest, CurrentBSSChangedUpdateServiceEndpoint) { |
mukesh agrawal | b20776f | 2012-02-10 16:00:36 -0800 | [diff] [blame] | 1580 | EXPECT_CALL(*manager(), RegisterService(_)).Times(AnyNumber()); |
| 1581 | EXPECT_CALL(*manager(), HasService(_)).Times(AnyNumber()); |
| 1582 | EXPECT_CALL(*manager(), UpdateService(_)).Times(AnyNumber()); |
| 1583 | |
Thieu Le | e41a72d | 2012-02-06 20:46:51 +0000 | [diff] [blame] | 1584 | const uint16 frequency1 = 2412; |
| 1585 | const uint16 frequency2 = 2442; |
| 1586 | StartWiFi(); |
| 1587 | ReportBSS("bss1", "ssid1", "00:00:00:00:00:01", 1, frequency1, |
| 1588 | kNetworkModeInfrastructure); |
| 1589 | ReportBSS("bss2", "ssid1", "00:00:00:00:00:02", 1, frequency2, |
| 1590 | kNetworkModeInfrastructure); |
| 1591 | EXPECT_EQ(1, GetServices().size()); |
| 1592 | WiFiService *service(GetServices()[0].get()); |
| 1593 | InitiateConnect(service); |
| 1594 | ReportCurrentBSSChanged("bss1"); |
| 1595 | EXPECT_EQ(frequency1, service->frequency_); |
| 1596 | ReportCurrentBSSChanged("bss2"); |
| 1597 | EXPECT_EQ(frequency2, service->frequency_); |
| 1598 | } |
| 1599 | |
mukesh agrawal | 4eb4d78 | 2011-12-05 17:34:37 +0000 | [diff] [blame] | 1600 | TEST_F(WiFiMainTest, ConfiguredServiceRegistration) { |
| 1601 | Error e; |
| 1602 | EXPECT_CALL(*manager(), RegisterService(_)) |
| 1603 | .Times(0); |
| 1604 | EXPECT_CALL(*manager(), HasService(_)) |
| 1605 | .WillOnce(Return(false)); |
| 1606 | GetOpenService(flimflam::kTypeWifi, "an_ssid", flimflam::kModeManaged, &e); |
| 1607 | EXPECT_CALL(*manager(), RegisterService(_)); |
Thieu Le | e41a72d | 2012-02-06 20:46:51 +0000 | [diff] [blame] | 1608 | ReportBSS("ap0", "an_ssid", "00:00:00:00:00:00", 0, 0, |
mukesh agrawal | 4eb4d78 | 2011-12-05 17:34:37 +0000 | [diff] [blame] | 1609 | kNetworkModeInfrastructure); |
| 1610 | } |
| 1611 | |
mukesh agrawal | 8a3188d | 2011-12-01 20:56:44 +0000 | [diff] [blame] | 1612 | TEST_F(WiFiMainTest, NewConnectPreemptsPending) { |
Gary Morain | d9f893f | 2012-02-06 10:03:40 -0800 | [diff] [blame] | 1613 | EXPECT_CALL(*manager(), HasService(_)).Times(AnyNumber()); |
| 1614 | EXPECT_CALL(*manager(), UpdateService(_)).Times(AnyNumber()); |
mukesh agrawal | 8a3188d | 2011-12-01 20:56:44 +0000 | [diff] [blame] | 1615 | WiFiEndpointRefPtr ap1 = MakeEndpoint("an_ssid", "00:01:02:03:04:05"); |
| 1616 | WiFiEndpointRefPtr ap2 = MakeEndpoint("another_ssid", "01:02:03:04:05:06"); |
| 1617 | WiFiServiceRefPtr service1 = CreateServiceForEndpoint(*ap1); |
| 1618 | WiFiServiceRefPtr service2 = CreateServiceForEndpoint(*ap2); |
| 1619 | |
| 1620 | StartWiFi(); |
Thieu Le | e41a72d | 2012-02-06 20:46:51 +0000 | [diff] [blame] | 1621 | ReportBSS("ap1", ap1->ssid_string(), ap1->bssid_string(), 0, 0, |
mukesh agrawal | 8a3188d | 2011-12-01 20:56:44 +0000 | [diff] [blame] | 1622 | kNetworkModeInfrastructure); |
Thieu Le | e41a72d | 2012-02-06 20:46:51 +0000 | [diff] [blame] | 1623 | ReportBSS("ap2", ap2->ssid_string(), ap2->bssid_string(), 0, 0, |
mukesh agrawal | 8a3188d | 2011-12-01 20:56:44 +0000 | [diff] [blame] | 1624 | kNetworkModeInfrastructure); |
| 1625 | InitiateConnect(service1); |
| 1626 | EXPECT_EQ(service1.get(), GetPendingService().get()); |
| 1627 | |
| 1628 | EXPECT_CALL(*GetSupplicantInterfaceProxy(), Disconnect()); |
| 1629 | EXPECT_CALL(*GetSupplicantInterfaceProxy(), AddNetwork(_)); |
| 1630 | InitiateConnect(service2); |
| 1631 | EXPECT_EQ(service2.get(), GetPendingService().get()); |
| 1632 | } |
| 1633 | |
| 1634 | TEST_F(WiFiMainTest, IsIdle) { |
| 1635 | StartWiFi(); |
| 1636 | EXPECT_TRUE(wifi()->IsIdle()); |
| 1637 | |
| 1638 | WiFiEndpointRefPtr ap = MakeEndpoint("an_ssid", "00:01:02:03:04:05"); |
| 1639 | WiFiServiceRefPtr service = CreateServiceForEndpoint(*ap); |
| 1640 | Error error; |
| 1641 | service->AddEndpoint(ap); |
| 1642 | service->AutoConnect(); |
| 1643 | EXPECT_FALSE(wifi()->IsIdle()); |
| 1644 | } |
| 1645 | |
mukesh agrawal | 4d0401c | 2012-01-06 16:05:31 -0800 | [diff] [blame] | 1646 | MATCHER(WiFiAddedArgs, "") { |
| 1647 | return ContainsKey(arg, wpa_supplicant::kNetworkPropertyScanSSID) && |
| 1648 | ContainsKey(arg, wpa_supplicant::kNetworkPropertyBgscan); |
| 1649 | } |
| 1650 | |
| 1651 | TEST_F(WiFiMainTest, AddNetworkArgs) { |
Gary Morain | d9f893f | 2012-02-06 10:03:40 -0800 | [diff] [blame] | 1652 | EXPECT_CALL(*manager(), RegisterService(_)).Times(AnyNumber()); |
mukesh agrawal | 4d0401c | 2012-01-06 16:05:31 -0800 | [diff] [blame] | 1653 | MockSupplicantInterfaceProxy &supplicant_interface_proxy = |
| 1654 | *supplicant_interface_proxy_; |
| 1655 | |
| 1656 | StartWiFi(); |
Thieu Le | e41a72d | 2012-02-06 20:46:51 +0000 | [diff] [blame] | 1657 | ReportBSS("bss0", "ssid0", "00:00:00:00:00:00", 0, 0, kNetworkModeAdHoc); |
mukesh agrawal | 4d0401c | 2012-01-06 16:05:31 -0800 | [diff] [blame] | 1658 | WiFiService *service(GetServices().begin()->get()); |
| 1659 | EXPECT_CALL(supplicant_interface_proxy, AddNetwork(WiFiAddedArgs())); |
| 1660 | InitiateConnect(service); |
| 1661 | } |
| 1662 | |
mukesh agrawal | c01f398 | 2012-01-24 13:48:39 -0800 | [diff] [blame] | 1663 | TEST_F(WiFiMainTest, StateAndIPIgnoreLinkEvent) { |
| 1664 | StartWiFi(); |
mukesh agrawal | cf24a24 | 2012-05-21 16:46:11 -0700 | [diff] [blame] | 1665 | MockWiFiServiceRefPtr service = MakeMockService(flimflam::kSecurityNone); |
mukesh agrawal | c01f398 | 2012-01-24 13:48:39 -0800 | [diff] [blame] | 1666 | InitiateConnect(service); |
| 1667 | EXPECT_CALL(*service.get(), SetState(_)).Times(0); |
| 1668 | EXPECT_CALL(*dhcp_config_.get(), RequestIP()).Times(0); |
| 1669 | ReportLinkUp(); |
| 1670 | |
| 1671 | // Verify expectations now, because WiFi may cause |service| state |
| 1672 | // changes during TearDown(). |
| 1673 | Mock::VerifyAndClearExpectations(service); |
Gary Morain | d9f893f | 2012-02-06 10:03:40 -0800 | [diff] [blame] | 1674 | EXPECT_CALL(*service.get(), SetState(_)).Times(AnyNumber()); |
mukesh agrawal | c01f398 | 2012-01-24 13:48:39 -0800 | [diff] [blame] | 1675 | } |
| 1676 | |
| 1677 | TEST_F(WiFiMainTest, SupplicantCompleted) { |
Gary Morain | d9f893f | 2012-02-06 10:03:40 -0800 | [diff] [blame] | 1678 | EXPECT_CALL(*manager(), UpdateService(_)).Times(AnyNumber()); |
Paul Stewart | d408fdf | 2012-05-07 17:15:57 -0700 | [diff] [blame] | 1679 | EXPECT_CALL(*dhcp_provider(), CreateConfig(_, _, _, _)).Times(AnyNumber()); |
Gary Morain | d9f893f | 2012-02-06 10:03:40 -0800 | [diff] [blame] | 1680 | EXPECT_CALL(*manager(), HasService(_)).Times(AnyNumber()); |
mukesh agrawal | c01f398 | 2012-01-24 13:48:39 -0800 | [diff] [blame] | 1681 | WiFiEndpointRefPtr ap = MakeEndpoint("an_ssid", "00:01:02:03:04:05"); |
| 1682 | WiFiServiceRefPtr service = CreateServiceForEndpoint(*ap); |
| 1683 | |
| 1684 | StartWiFi(); |
Thieu Le | e41a72d | 2012-02-06 20:46:51 +0000 | [diff] [blame] | 1685 | ReportBSS("ap", ap->ssid_string(), ap->bssid_string(), 0, 0, |
mukesh agrawal | c01f398 | 2012-01-24 13:48:39 -0800 | [diff] [blame] | 1686 | kNetworkModeInfrastructure); |
| 1687 | InitiateConnect(service); |
| 1688 | |
| 1689 | EXPECT_CALL(*dhcp_config_.get(), RequestIP()); |
| 1690 | ReportCurrentBSSChanged("ap"); |
| 1691 | ReportStateChanged(wpa_supplicant::kInterfaceStateCompleted); |
| 1692 | EXPECT_EQ(Service::kStateConfiguring, service->state()); |
| 1693 | } |
| 1694 | |
| 1695 | TEST_F(WiFiMainTest, SupplicantCompletedAlreadyConnected) { |
Gary Morain | d9f893f | 2012-02-06 10:03:40 -0800 | [diff] [blame] | 1696 | EXPECT_CALL(*dhcp_config_.get(), RequestIP()); |
| 1697 | EXPECT_CALL(*manager(), UpdateService(_)).Times(AnyNumber()); |
| 1698 | EXPECT_CALL(*device_info(), FlushAddresses(_)).Times(AnyNumber()); |
| 1699 | EXPECT_CALL(*manager(), device_info()).Times(AnyNumber()); |
Paul Stewart | d408fdf | 2012-05-07 17:15:57 -0700 | [diff] [blame] | 1700 | EXPECT_CALL(*dhcp_provider(), CreateConfig(_, _, _, _)).Times(AnyNumber()); |
Gary Morain | d9f893f | 2012-02-06 10:03:40 -0800 | [diff] [blame] | 1701 | EXPECT_CALL(*manager(), HasService(_)).Times(AnyNumber()); |
Gary Morain | ac1bdb4 | 2012-02-16 17:42:29 -0800 | [diff] [blame] | 1702 | EXPECT_CALL(*manager(), IsPortalDetectionEnabled(_)).Times(AnyNumber()); |
mukesh agrawal | c01f398 | 2012-01-24 13:48:39 -0800 | [diff] [blame] | 1703 | WiFiEndpointRefPtr ap = MakeEndpoint("an_ssid", "00:01:02:03:04:05"); |
| 1704 | WiFiServiceRefPtr service = CreateServiceForEndpoint(*ap); |
| 1705 | |
mukesh agrawal | c01f398 | 2012-01-24 13:48:39 -0800 | [diff] [blame] | 1706 | StartWiFi(); |
Thieu Le | e41a72d | 2012-02-06 20:46:51 +0000 | [diff] [blame] | 1707 | ReportBSS("ap", ap->ssid_string(), ap->bssid_string(), 0, 0, |
mukesh agrawal | c01f398 | 2012-01-24 13:48:39 -0800 | [diff] [blame] | 1708 | kNetworkModeInfrastructure); |
| 1709 | InitiateConnect(service); |
| 1710 | ReportCurrentBSSChanged("ap"); |
| 1711 | ReportStateChanged(wpa_supplicant::kInterfaceStateCompleted); |
| 1712 | ReportIPConfigComplete(); |
| 1713 | Mock::VerifyAndClearExpectations(service); |
| 1714 | |
| 1715 | EXPECT_CALL(*dhcp_config_.get(), RequestIP()).Times(0); |
| 1716 | ReportStateChanged(wpa_supplicant::kInterfaceStateCompleted); |
| 1717 | } |
| 1718 | |
Paul Stewart | 66c8600 | 2012-01-30 18:00:52 -0800 | [diff] [blame] | 1719 | TEST_F(WiFiMainTest, ClearCachedCredentials) { |
| 1720 | MockSupplicantInterfaceProxy &supplicant_interface_proxy = |
| 1721 | *supplicant_interface_proxy_; |
| 1722 | |
| 1723 | StartWiFi(); |
| 1724 | |
| 1725 | // Ensure call to the proxy is deferred. |
| 1726 | EXPECT_CALL(supplicant_interface_proxy, ClearCachedCredentials()) |
| 1727 | .Times(0); |
| 1728 | ClearCachedCredentials(); |
| 1729 | |
| 1730 | Mock::VerifyAndClearExpectations(&supplicant_interface_proxy); |
| 1731 | |
| 1732 | EXPECT_CALL(supplicant_interface_proxy, ClearCachedCredentials()) |
| 1733 | .Times(1); |
| 1734 | dispatcher_.DispatchPendingEvents(); |
| 1735 | |
| 1736 | Mock::VerifyAndClearExpectations(&supplicant_interface_proxy); |
| 1737 | |
| 1738 | EXPECT_CALL(supplicant_interface_proxy, ClearCachedCredentials()) |
| 1739 | .Times(0); |
| 1740 | ClearCachedCredentials(); |
| 1741 | ClearCachedCredentials(); |
| 1742 | |
| 1743 | Mock::VerifyAndClearExpectations(&supplicant_interface_proxy); |
| 1744 | |
| 1745 | // Ensure multiple calls to ClearCachedCredentials() results in only |
| 1746 | // one call to the proxy. |
| 1747 | EXPECT_CALL(supplicant_interface_proxy, ClearCachedCredentials()) |
| 1748 | .Times(1); |
| 1749 | dispatcher_.DispatchPendingEvents(); |
| 1750 | } |
| 1751 | |
mukesh agrawal | b20776f | 2012-02-10 16:00:36 -0800 | [diff] [blame] | 1752 | TEST_F(WiFiMainTest, BSSAddedCreatesBSSProxy) { |
| 1753 | EXPECT_CALL(*manager(), RegisterService(_)).Times(AnyNumber()); |
| 1754 | // TODO(quiche): Consider using a factory for WiFiEndpoints, so that |
| 1755 | // we can test the interaction between WiFi and WiFiEndpoint. (Right |
| 1756 | // now, we're testing across multiple layers.) |
| 1757 | EXPECT_CALL(*supplicant_bss_proxy_, Die()).Times(AnyNumber()); |
| 1758 | EXPECT_CALL(*proxy_factory(), CreateSupplicantBSSProxy(_, _, _)); |
| 1759 | StartWiFi(); |
| 1760 | ReportBSS("bss0", "ssid0", "00:00:00:00:00:00", 0, 0, kNetworkModeAdHoc); |
| 1761 | } |
| 1762 | |
| 1763 | TEST_F(WiFiMainTest, BSSRemovedDestroysBSSProxy) { |
| 1764 | // TODO(quiche): As for BSSAddedCreatesBSSProxy, consider using a |
| 1765 | // factory for WiFiEndpoints. |
| 1766 | EXPECT_CALL(*manager(), RegisterService(_)).Times(AnyNumber()); |
| 1767 | |
| 1768 | // Get the pointer before we transfer ownership. |
| 1769 | MockSupplicantBSSProxy *proxy = supplicant_bss_proxy_.get(); |
| 1770 | EXPECT_CALL(*proxy, Die()); |
| 1771 | StartWiFi(); |
| 1772 | ReportBSS("bss0", "ssid0", "00:00:00:00:00:00", 0, 0, kNetworkModeAdHoc); |
| 1773 | RemoveBSS("bss0"); |
| 1774 | // Check this now, to make sure RemoveBSS killed the proxy (rather |
| 1775 | // than TearDown). |
| 1776 | Mock::VerifyAndClearExpectations(proxy); |
| 1777 | } |
| 1778 | |
mukesh agrawal | 5c05b29 | 2012-03-07 10:12:52 -0800 | [diff] [blame] | 1779 | TEST_F(WiFiMainTest, FlushBSSOnResume) { |
| 1780 | const struct timeval resume_time = {1, 0}; |
| 1781 | const struct timeval scan_done_time = {6, 0}; |
| 1782 | MockSupplicantInterfaceProxy &supplicant_interface_proxy = |
| 1783 | *supplicant_interface_proxy_; |
| 1784 | |
| 1785 | StartWiFi(); |
| 1786 | |
| 1787 | EXPECT_CALL(time_, GetTimeMonotonic(_)) |
| 1788 | .WillOnce(DoAll(SetArgumentPointee<0>(resume_time), Return(0))) |
| 1789 | .WillOnce(DoAll(SetArgumentPointee<0>(scan_done_time), Return(0))); |
| 1790 | EXPECT_CALL(supplicant_interface_proxy, |
| 1791 | FlushBSS(WiFi::kMaxBSSResumeAgeSeconds + 5)); |
Eric Shienbrood | 3e20a23 | 2012-02-16 11:35:56 -0500 | [diff] [blame] | 1792 | power_state_callback().Run(PowerManagerProxyDelegate::kOn); |
mukesh agrawal | 5c05b29 | 2012-03-07 10:12:52 -0800 | [diff] [blame] | 1793 | ReportScanDone(); |
| 1794 | } |
| 1795 | |
mukesh agrawal | b66c646 | 2012-05-07 11:45:25 -0700 | [diff] [blame] | 1796 | TEST_F(WiFiMainTest, ScanTimerIdle) { |
| 1797 | StartWiFi(); |
| 1798 | dispatcher_.DispatchPendingEvents(); |
| 1799 | ReportScanDone(); |
| 1800 | CancelScanTimer(); |
| 1801 | EXPECT_TRUE(GetScanTimer().IsCancelled()); |
| 1802 | |
| 1803 | EXPECT_CALL(*GetSupplicantInterfaceProxy(), Scan(_)); |
| 1804 | FireScanTimer(); |
| 1805 | dispatcher_.DispatchPendingEvents(); |
| 1806 | EXPECT_FALSE(GetScanTimer().IsCancelled()); // Automatically re-armed. |
| 1807 | } |
| 1808 | |
| 1809 | TEST_F(WiFiMainTest, ScanTimerScanning) { |
| 1810 | StartWiFi(); |
| 1811 | dispatcher_.DispatchPendingEvents(); |
| 1812 | CancelScanTimer(); |
| 1813 | EXPECT_TRUE(GetScanTimer().IsCancelled()); |
| 1814 | |
| 1815 | // Should not call Scan, since we're already scanning. |
| 1816 | // (Scanning is triggered by StartWiFi.) |
| 1817 | EXPECT_CALL(*GetSupplicantInterfaceProxy(), Scan(_)).Times(0); |
| 1818 | FireScanTimer(); |
| 1819 | dispatcher_.DispatchPendingEvents(); |
| 1820 | EXPECT_FALSE(GetScanTimer().IsCancelled()); // Automatically re-armed. |
| 1821 | } |
| 1822 | |
| 1823 | TEST_F(WiFiMainTest, ScanTimerConnecting) { |
| 1824 | StartWiFi(); |
| 1825 | dispatcher_.DispatchPendingEvents(); |
| 1826 | ReportBSS("bss0", "ssid0", "00:00:00:00:00:00", 0, 0, kNetworkModeAdHoc); |
| 1827 | ReportScanDone(); |
| 1828 | WiFiService *service(GetServices().begin()->get()); |
| 1829 | InitiateConnect(service); |
| 1830 | CancelScanTimer(); |
| 1831 | EXPECT_TRUE(GetScanTimer().IsCancelled()); |
| 1832 | |
| 1833 | EXPECT_CALL(*GetSupplicantInterfaceProxy(), Scan(_)).Times(0); |
| 1834 | FireScanTimer(); |
| 1835 | dispatcher_.DispatchPendingEvents(); |
| 1836 | EXPECT_FALSE(GetScanTimer().IsCancelled()); // Automatically re-armed. |
| 1837 | } |
| 1838 | |
| 1839 | TEST_F(WiFiMainTest, ScanTimerReconfigured) { |
| 1840 | StartWiFi(); |
| 1841 | CancelScanTimer(); |
| 1842 | EXPECT_TRUE(GetScanTimer().IsCancelled()); |
| 1843 | |
| 1844 | SetScanInterval(1); |
| 1845 | EXPECT_FALSE(GetScanTimer().IsCancelled()); |
| 1846 | } |
| 1847 | |
| 1848 | TEST_F(WiFiMainTest, ScanTimerResetOnScanDone) { |
| 1849 | StartWiFi(); |
| 1850 | CancelScanTimer(); |
| 1851 | EXPECT_TRUE(GetScanTimer().IsCancelled()); |
| 1852 | |
| 1853 | ReportScanDone(); |
| 1854 | EXPECT_FALSE(GetScanTimer().IsCancelled()); |
| 1855 | } |
| 1856 | |
| 1857 | TEST_F(WiFiMainTest, ScanTimerStopOnZeroInterval) { |
| 1858 | StartWiFi(); |
| 1859 | EXPECT_FALSE(GetScanTimer().IsCancelled()); |
| 1860 | |
| 1861 | SetScanInterval(0); |
| 1862 | EXPECT_TRUE(GetScanTimer().IsCancelled()); |
| 1863 | } |
| 1864 | |
| 1865 | TEST_F(WiFiMainTest, ScanOnDisconnectWithHidden) { |
| 1866 | Error e; |
| 1867 | scoped_refptr<MockProfile> profile( |
| 1868 | new NiceMock<MockProfile>(control_interface(), manager(), "")); |
| 1869 | WiFiServiceRefPtr hidden_service = |
| 1870 | GetServiceInner(flimflam::kTypeWifi, "hidden_ssid", |
| 1871 | flimflam::kModeManaged, NULL, NULL, true, &e); |
| 1872 | hidden_service->set_profile(profile); |
| 1873 | |
| 1874 | StartWiFi(); |
| 1875 | WiFiEndpointRefPtr ap = MakeEndpoint("an_ssid", "00:01:02:03:04:05"); |
| 1876 | WiFiServiceRefPtr service = CreateServiceForEndpoint(*ap); |
| 1877 | ReportBSS("an_ap", ap->ssid_string(), ap->bssid_string(), 0, 0, |
| 1878 | kNetworkModeInfrastructure); |
| 1879 | InitiateConnect(service); |
| 1880 | ReportCurrentBSSChanged("an_ap"); |
| 1881 | ReportStateChanged(wpa_supplicant::kInterfaceStateCompleted); |
| 1882 | dispatcher_.DispatchPendingEvents(); |
| 1883 | |
| 1884 | EXPECT_CALL(*GetSupplicantInterfaceProxy(), |
| 1885 | Scan(HasHiddenSSID("hidden_ssid"))); |
| 1886 | ReportCurrentBSSChanged(wpa_supplicant::kCurrentBSSNull); |
| 1887 | dispatcher_.DispatchPendingEvents(); |
| 1888 | } |
| 1889 | |
| 1890 | TEST_F(WiFiMainTest, NoScanOnDisconnectWithoutHidden) { |
| 1891 | StartWiFi(); |
| 1892 | WiFiEndpointRefPtr ap = MakeEndpoint("an_ssid", "00:01:02:03:04:05"); |
| 1893 | WiFiServiceRefPtr service = CreateServiceForEndpoint(*ap); |
| 1894 | ReportBSS("an_ap", ap->ssid_string(), ap->bssid_string(), 0, 0, |
| 1895 | kNetworkModeInfrastructure); |
| 1896 | InitiateConnect(service); |
| 1897 | ReportCurrentBSSChanged("an_ap"); |
| 1898 | ReportStateChanged(wpa_supplicant::kInterfaceStateCompleted); |
| 1899 | dispatcher_.DispatchPendingEvents(); |
| 1900 | |
| 1901 | EXPECT_CALL(*GetSupplicantInterfaceProxy(), Scan(_)).Times(0); |
| 1902 | ReportCurrentBSSChanged(wpa_supplicant::kCurrentBSSNull); |
| 1903 | dispatcher_.DispatchPendingEvents(); |
| 1904 | } |
| 1905 | |
mukesh agrawal | cf24a24 | 2012-05-21 16:46:11 -0700 | [diff] [blame] | 1906 | TEST_F(WiFiMainTest, SuspectCredentialsOpen) { |
| 1907 | Error e; |
| 1908 | WiFiServiceRefPtr service = GetOpenService( |
| 1909 | flimflam::kTypeWifi, "an_ssid", flimflam::kModeManaged, &e); |
| 1910 | ReportStateChanged(wpa_supplicant::kInterfaceState4WayHandshake); |
| 1911 | EXPECT_FALSE(service->has_ever_connected()); |
| 1912 | EXPECT_FALSE(wifi()->SuspectCredentials(*service)); |
| 1913 | } |
| 1914 | |
| 1915 | TEST_F(WiFiMainTest, SuspectCredentialsWPANeverConnected) { |
| 1916 | Error e; |
| 1917 | WiFiServiceRefPtr service = |
| 1918 | GetService(flimflam::kTypeWifi, "an_ssid", flimflam::kModeManaged, |
| 1919 | flimflam::kSecurityWpa, "abcdefgh", &e); |
| 1920 | ReportStateChanged(wpa_supplicant::kInterfaceState4WayHandshake); |
| 1921 | EXPECT_FALSE(service->has_ever_connected()); |
| 1922 | EXPECT_TRUE(wifi()->SuspectCredentials(*service)); |
| 1923 | } |
| 1924 | |
| 1925 | TEST_F(WiFiMainTest, SuspectCredentialsWPAPreviouslyConnected) { |
| 1926 | Error e; |
| 1927 | WiFiServiceRefPtr service = |
| 1928 | GetService(flimflam::kTypeWifi, "an_ssid", flimflam::kModeManaged, |
| 1929 | flimflam::kSecurityWpa, "abcdefgh", &e); |
| 1930 | ReportStateChanged(wpa_supplicant::kInterfaceState4WayHandshake); |
| 1931 | service->has_ever_connected_ = true; |
| 1932 | EXPECT_FALSE(wifi()->SuspectCredentials(*service)); |
| 1933 | } |
| 1934 | |
| 1935 | TEST_F(WiFiMainTest, SuspectCredentialsYieldFailure) { |
| 1936 | ScopedMockLog log; |
| 1937 | Error e; |
| 1938 | MockWiFiServiceRefPtr service = MakeMockService(flimflam::kSecurityWpa); |
| 1939 | SetPendingService(service); |
| 1940 | ReportStateChanged(wpa_supplicant::kInterfaceState4WayHandshake); |
| 1941 | EXPECT_FALSE(service->has_ever_connected()); |
| 1942 | |
| 1943 | EXPECT_CALL(*service, SetFailure(Service::kFailureBadCredentials)); |
| 1944 | EXPECT_CALL(log, Log(_, _, _)).Times(AnyNumber()); |
| 1945 | EXPECT_CALL(log, Log(logging::LOG_ERROR, _, EndsWith("Bad passphrase?"))); |
| 1946 | ReportCurrentBSSChanged(wpa_supplicant::kCurrentBSSNull); |
| 1947 | EXPECT_EQ(Service::kStateIdle, service->state()); |
| 1948 | EXPECT_TRUE(service->IsFailed()); |
| 1949 | } |
| 1950 | |
Paul Stewart | bc6e739 | 2012-05-24 07:07:48 -0700 | [diff] [blame] | 1951 | |
Paul Stewart | e369ece | 2012-05-22 09:11:03 -0700 | [diff] [blame] | 1952 | // Scanning tests will use a mock of the event dispatcher instead of a real |
| 1953 | // one. |
| 1954 | class WiFiScanTest : public WiFiObjectTest { |
| 1955 | public: |
| 1956 | WiFiScanTest() : WiFiObjectTest(&mock_dispatcher_) {} |
| 1957 | |
| 1958 | protected: |
| 1959 | void ExpectInitialScanSequence(); |
| 1960 | |
| 1961 | StrictMock<MockEventDispatcher> mock_dispatcher_; |
| 1962 | }; |
| 1963 | |
| 1964 | void WiFiScanTest::ExpectInitialScanSequence() { |
| 1965 | // Choose a number of iterations some multiple higher than the fast scan |
| 1966 | // count. |
| 1967 | const int kScanTimes = WiFi::kNumFastScanAttempts * 4; |
| 1968 | |
| 1969 | // Each time we call FireScanTimer() below, WiFi will post a task to actually |
| 1970 | // run Scan() on the wpa_supplicant proxy. |
| 1971 | EXPECT_CALL(mock_dispatcher_, PostTask(_)) |
| 1972 | .Times(kScanTimes); |
| 1973 | { |
| 1974 | InSequence seq; |
| 1975 | // The scans immediately after the initial scan should happen at the short |
| 1976 | // interval. If we add the initial scan (not invoked in this function) to |
| 1977 | // the ones in the expectation below, we get WiFi::kNumFastScanAttempts at |
| 1978 | // the fast scan interval. |
| 1979 | EXPECT_CALL(mock_dispatcher_, PostDelayedTask( |
| 1980 | _, WiFi::kFastScanIntervalSeconds * 1000)) |
| 1981 | .Times(WiFi::kNumFastScanAttempts - 1) |
| 1982 | .WillRepeatedly(Return(true)); |
| 1983 | |
| 1984 | // After this, the WiFi device should use the normal scan interval. |
| 1985 | EXPECT_CALL(mock_dispatcher_, PostDelayedTask( |
| 1986 | _, GetScanInterval() * 1000)) |
| 1987 | .Times(kScanTimes - WiFi::kNumFastScanAttempts + 1) |
| 1988 | .WillRepeatedly(Return(true)); |
| 1989 | |
| 1990 | for (int i = 0; i < kScanTimes; i++) { |
| 1991 | FireScanTimer(); |
| 1992 | } |
| 1993 | } |
| 1994 | } |
| 1995 | |
| 1996 | TEST_F(WiFiScanTest, FastRescan) { |
| 1997 | // This PostTask is a result of the call to Scan(NULL), and is meant to |
| 1998 | // post a task to call Scan() on the wpa_supplicant proxy immediately. |
| 1999 | EXPECT_CALL(mock_dispatcher_, PostTask(_)); |
| 2000 | EXPECT_CALL(mock_dispatcher_, PostDelayedTask( |
| 2001 | _, WiFi::kFastScanIntervalSeconds * 1000)) |
| 2002 | .WillOnce(Return(true)); |
| 2003 | StartWiFi(); |
| 2004 | |
| 2005 | ExpectInitialScanSequence(); |
| 2006 | |
| 2007 | // If we end up disconnecting, the sequence should repeat. |
| 2008 | EXPECT_CALL(mock_dispatcher_, PostDelayedTask( |
| 2009 | _, WiFi::kFastScanIntervalSeconds * 1000)) |
| 2010 | .WillOnce(Return(true)); |
| 2011 | RestartFastScanAttempts(); |
| 2012 | |
| 2013 | ExpectInitialScanSequence(); |
| 2014 | } |
| 2015 | |
Paul Stewart | bc6e739 | 2012-05-24 07:07:48 -0700 | [diff] [blame] | 2016 | TEST_F(WiFiMainTest, EAPCertification) { |
| 2017 | MockWiFiServiceRefPtr service = MakeMockService(flimflam::kSecurity8021x); |
| 2018 | EXPECT_CALL(*service, AddEAPCertification(_, _)).Times(0); |
| 2019 | |
| 2020 | ScopedMockLog log; |
| 2021 | EXPECT_CALL(log, Log(logging::LOG_ERROR, _, EndsWith("no current service."))); |
| 2022 | map<string, ::DBus::Variant> args; |
| 2023 | ReportCertification(args); |
| 2024 | Mock::VerifyAndClearExpectations(&log); |
| 2025 | |
| 2026 | SetCurrentService(service); |
| 2027 | EXPECT_CALL(log, Log(logging::LOG_ERROR, _, EndsWith("no depth parameter."))); |
| 2028 | ReportCertification(args); |
| 2029 | Mock::VerifyAndClearExpectations(&log); |
| 2030 | |
| 2031 | const uint32 kDepth = 123; |
| 2032 | args[wpa_supplicant::kInterfacePropertyDepth].writer().append_uint32(kDepth); |
| 2033 | |
| 2034 | EXPECT_CALL(log, |
| 2035 | Log(logging::LOG_ERROR, _, EndsWith("no subject parameter."))); |
| 2036 | ReportCertification(args); |
| 2037 | Mock::VerifyAndClearExpectations(&log); |
| 2038 | |
| 2039 | const string kSubject("subject"); |
| 2040 | args[wpa_supplicant::kInterfacePropertySubject].writer() |
| 2041 | .append_string(kSubject.c_str()); |
| 2042 | EXPECT_CALL(*service, AddEAPCertification(kSubject, kDepth)).Times(1); |
| 2043 | ReportCertification(args); |
| 2044 | } |
| 2045 | |
Chris Masone | 853b81b | 2011-06-24 14:11:41 -0700 | [diff] [blame] | 2046 | } // namespace shill |