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