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