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