mukesh agrawal | 8a3188d | 2011-12-01 20:56:44 +0000 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium OS Authors. All rights reserved. |
Chris Masone | 853b81b | 2011-06-24 14:11:41 -0700 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #include "shill/wifi.h" |
| 6 | |
mukesh agrawal | f2f68a5 | 2011-09-01 12:15:48 -0700 | [diff] [blame] | 7 | #include <netinet/ether.h> |
| 8 | #include <linux/if.h> |
mukesh agrawal | 5c4dd0b | 2011-09-14 13:53:14 -0700 | [diff] [blame] | 9 | #include <sys/socket.h> |
| 10 | #include <linux/netlink.h> // Needs typedefs from sys/socket.h. |
mukesh agrawal | f2f68a5 | 2011-09-01 12:15:48 -0700 | [diff] [blame] | 11 | |
Chris Masone | 853b81b | 2011-06-24 14:11:41 -0700 | [diff] [blame] | 12 | #include <map> |
Paul Stewart | a41e38d | 2011-11-11 07:47:29 -0800 | [diff] [blame] | 13 | #include <set> |
Chris Masone | 853b81b | 2011-06-24 14:11:41 -0700 | [diff] [blame] | 14 | #include <string> |
| 15 | #include <vector> |
| 16 | |
mukesh agrawal | 7ec7131 | 2011-11-10 02:08:26 +0000 | [diff] [blame] | 17 | #include <base/memory/ref_counted.h> |
mukesh agrawal | dc42bb3 | 2011-07-28 10:40:26 -0700 | [diff] [blame] | 18 | #include <base/memory/scoped_ptr.h> |
Paul Stewart | a41e38d | 2011-11-11 07:47:29 -0800 | [diff] [blame] | 19 | #include <base/stringprintf.h> |
mukesh agrawal | 3195024 | 2011-07-14 11:53:38 -0700 | [diff] [blame] | 20 | #include <base/string_number_conversions.h> |
| 21 | #include <base/string_util.h> |
Chris Masone | 853b81b | 2011-06-24 14:11:41 -0700 | [diff] [blame] | 22 | #include <chromeos/dbus/service_constants.h> |
mukesh agrawal | 1830fa1 | 2011-09-26 14:31:40 -0700 | [diff] [blame] | 23 | #include <dbus-c++/dbus.h> |
Chris Masone | 7156c92 | 2011-08-23 20:36:21 -0700 | [diff] [blame] | 24 | #include <gmock/gmock.h> |
Chris Masone | 2ae797d | 2011-08-23 20:41:00 -0700 | [diff] [blame] | 25 | #include <gtest/gtest.h> |
Chris Masone | 853b81b | 2011-06-24 14:11:41 -0700 | [diff] [blame] | 26 | |
| 27 | #include "shill/dbus_adaptor.h" |
Paul Stewart | 26b327e | 2011-10-19 11:38:09 -0700 | [diff] [blame] | 28 | #include "shill/event_dispatcher.h" |
mukesh agrawal | 8ede052 | 2011-10-03 14:57:44 -0700 | [diff] [blame] | 29 | #include "shill/ieee80211.h" |
mukesh agrawal | 7a4e400 | 2011-09-06 11:26:05 -0700 | [diff] [blame] | 30 | #include "shill/key_value_store.h" |
Chris Masone | 853b81b | 2011-06-24 14:11:41 -0700 | [diff] [blame] | 31 | #include "shill/manager.h" |
Chris Masone | 853b81b | 2011-06-24 14:11:41 -0700 | [diff] [blame] | 32 | #include "shill/mock_device.h" |
mukesh agrawal | f2f68a5 | 2011-09-01 12:15:48 -0700 | [diff] [blame] | 33 | #include "shill/mock_dhcp_config.h" |
| 34 | #include "shill/mock_dhcp_provider.h" |
Chris Masone | 2ae797d | 2011-08-23 20:41:00 -0700 | [diff] [blame] | 35 | #include "shill/mock_manager.h" |
mukesh agrawal | 5c4dd0b | 2011-09-14 13:53:14 -0700 | [diff] [blame] | 36 | #include "shill/mock_rtnl_handler.h" |
Paul Stewart | a41e38d | 2011-11-11 07:47:29 -0800 | [diff] [blame] | 37 | #include "shill/mock_store.h" |
mukesh agrawal | 3195024 | 2011-07-14 11:53:38 -0700 | [diff] [blame] | 38 | #include "shill/mock_supplicant_interface_proxy.h" |
| 39 | #include "shill/mock_supplicant_process_proxy.h" |
mukesh agrawal | 7ec7131 | 2011-11-10 02:08:26 +0000 | [diff] [blame] | 40 | #include "shill/mock_wifi_service.h" |
mukesh agrawal | dc42bb3 | 2011-07-28 10:40:26 -0700 | [diff] [blame] | 41 | #include "shill/nice_mock_control.h" |
Chris Masone | 853b81b | 2011-06-24 14:11:41 -0700 | [diff] [blame] | 42 | #include "shill/property_store_unittest.h" |
mukesh agrawal | 3195024 | 2011-07-14 11:53:38 -0700 | [diff] [blame] | 43 | #include "shill/proxy_factory.h" |
| 44 | #include "shill/wifi_endpoint.h" |
| 45 | #include "shill/wifi.h" |
mukesh agrawal | 3239932 | 2011-09-01 10:53:43 -0700 | [diff] [blame] | 46 | #include "shill/wifi_service.h" |
Paul Stewart | ced6a0b | 2011-11-08 15:32:04 -0800 | [diff] [blame] | 47 | #include "shill/wpa_supplicant.h" |
Chris Masone | 853b81b | 2011-06-24 14:11:41 -0700 | [diff] [blame] | 48 | |
| 49 | using std::map; |
Paul Stewart | a41e38d | 2011-11-11 07:47:29 -0800 | [diff] [blame] | 50 | using std::set; |
Chris Masone | 853b81b | 2011-06-24 14:11:41 -0700 | [diff] [blame] | 51 | using std::string; |
| 52 | using std::vector; |
| 53 | using ::testing::_; |
mukesh agrawal | 3195024 | 2011-07-14 11:53:38 -0700 | [diff] [blame] | 54 | using ::testing::AnyNumber; |
| 55 | using ::testing::DefaultValue; |
Paul Stewart | a41e38d | 2011-11-11 07:47:29 -0800 | [diff] [blame] | 56 | using ::testing::DoAll; |
mukesh agrawal | 3195024 | 2011-07-14 11:53:38 -0700 | [diff] [blame] | 57 | using ::testing::InSequence; |
mukesh agrawal | 7ec7131 | 2011-11-10 02:08:26 +0000 | [diff] [blame] | 58 | using ::testing::Mock; |
mukesh agrawal | dc42bb3 | 2011-07-28 10:40:26 -0700 | [diff] [blame] | 59 | using ::testing::NiceMock; |
Chris Masone | 853b81b | 2011-06-24 14:11:41 -0700 | [diff] [blame] | 60 | using ::testing::Return; |
Paul Stewart | a41e38d | 2011-11-11 07:47:29 -0800 | [diff] [blame] | 61 | using ::testing::SetArgumentPointee; |
| 62 | using ::testing::StrEq; |
| 63 | using ::testing::StrictMock; |
Chris Masone | 853b81b | 2011-06-24 14:11:41 -0700 | [diff] [blame] | 64 | using ::testing::Test; |
mukesh agrawal | 3195024 | 2011-07-14 11:53:38 -0700 | [diff] [blame] | 65 | using ::testing::Throw; |
mukesh agrawal | 8ede052 | 2011-10-03 14:57:44 -0700 | [diff] [blame] | 66 | using ::testing::Values; |
Chris Masone | 853b81b | 2011-06-24 14:11:41 -0700 | [diff] [blame] | 67 | |
| 68 | namespace shill { |
| 69 | |
mukesh agrawal | 3195024 | 2011-07-14 11:53:38 -0700 | [diff] [blame] | 70 | class WiFiPropertyTest : public PropertyStoreTest { |
Chris Masone | 853b81b | 2011-06-24 14:11:41 -0700 | [diff] [blame] | 71 | public: |
mukesh agrawal | 3195024 | 2011-07-14 11:53:38 -0700 | [diff] [blame] | 72 | WiFiPropertyTest() |
Chris Masone | 2176a88 | 2011-09-14 22:29:15 -0700 | [diff] [blame] | 73 | : device_(new WiFi(control_interface(), NULL, NULL, "wifi", "", 0)) { |
Chris Masone | 853b81b | 2011-06-24 14:11:41 -0700 | [diff] [blame] | 74 | } |
mukesh agrawal | 3195024 | 2011-07-14 11:53:38 -0700 | [diff] [blame] | 75 | virtual ~WiFiPropertyTest() {} |
Chris Masone | 853b81b | 2011-06-24 14:11:41 -0700 | [diff] [blame] | 76 | |
| 77 | protected: |
| 78 | DeviceRefPtr device_; |
| 79 | }; |
| 80 | |
mukesh agrawal | 3195024 | 2011-07-14 11:53:38 -0700 | [diff] [blame] | 81 | TEST_F(WiFiPropertyTest, Contains) { |
mukesh agrawal | de29fa8 | 2011-09-16 16:16:36 -0700 | [diff] [blame] | 82 | EXPECT_TRUE(device_->store().Contains(flimflam::kNameProperty)); |
| 83 | EXPECT_FALSE(device_->store().Contains("")); |
Chris Masone | 853b81b | 2011-06-24 14:11:41 -0700 | [diff] [blame] | 84 | } |
| 85 | |
mukesh agrawal | 3195024 | 2011-07-14 11:53:38 -0700 | [diff] [blame] | 86 | TEST_F(WiFiPropertyTest, Dispatch) { |
Chris Masone | a8a2c25 | 2011-06-27 22:16:30 -0700 | [diff] [blame] | 87 | { |
| 88 | ::DBus::Error error; |
Chris Masone | a8a2c25 | 2011-06-27 22:16:30 -0700 | [diff] [blame] | 89 | EXPECT_TRUE(DBusAdaptor::DispatchOnType( |
mukesh agrawal | de29fa8 | 2011-09-16 16:16:36 -0700 | [diff] [blame] | 90 | device_->mutable_store(), |
Chris Masone | a8a2c25 | 2011-06-27 22:16:30 -0700 | [diff] [blame] | 91 | flimflam::kBgscanSignalThresholdProperty, |
| 92 | PropertyStoreTest::kInt32V, |
| 93 | &error)); |
| 94 | } |
| 95 | { |
| 96 | ::DBus::Error error; |
mukesh agrawal | de29fa8 | 2011-09-16 16:16:36 -0700 | [diff] [blame] | 97 | EXPECT_TRUE(DBusAdaptor::DispatchOnType(device_->mutable_store(), |
Chris Masone | a8a2c25 | 2011-06-27 22:16:30 -0700 | [diff] [blame] | 98 | flimflam::kScanIntervalProperty, |
| 99 | PropertyStoreTest::kUint16V, |
| 100 | &error)); |
| 101 | } |
Chris Masone | 853b81b | 2011-06-24 14:11:41 -0700 | [diff] [blame] | 102 | // 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] | 103 | { |
| 104 | ::DBus::Error error; |
mukesh agrawal | de29fa8 | 2011-09-16 16:16:36 -0700 | [diff] [blame] | 105 | EXPECT_FALSE(DBusAdaptor::DispatchOnType(device_->mutable_store(), |
Chris Masone | a8a2c25 | 2011-06-27 22:16:30 -0700 | [diff] [blame] | 106 | flimflam::kScanningProperty, |
| 107 | PropertyStoreTest::kBoolV, |
| 108 | &error)); |
Chris Masone | 9d77993 | 2011-08-25 16:33:41 -0700 | [diff] [blame] | 109 | EXPECT_EQ(invalid_args(), error.name()); |
Chris Masone | a8a2c25 | 2011-06-27 22:16:30 -0700 | [diff] [blame] | 110 | } |
Chris Masone | 853b81b | 2011-06-24 14:11:41 -0700 | [diff] [blame] | 111 | } |
| 112 | |
mukesh agrawal | 4d0401c | 2012-01-06 16:05:31 -0800 | [diff] [blame] | 113 | TEST_F(WiFiPropertyTest, BgscanMethod) { |
| 114 | { |
| 115 | ::DBus::Error error; |
| 116 | EXPECT_TRUE(DBusAdaptor::DispatchOnType( |
| 117 | device_->mutable_store(), |
| 118 | flimflam::kBgscanMethodProperty, |
| 119 | DBusAdaptor::StringToVariant( |
| 120 | wpa_supplicant::kNetworkBgscanMethodSimple), |
| 121 | &error)); |
| 122 | } |
| 123 | |
| 124 | { |
| 125 | ::DBus::Error error; |
| 126 | EXPECT_FALSE(DBusAdaptor::DispatchOnType( |
| 127 | device_->mutable_store(), |
| 128 | flimflam::kBgscanMethodProperty, |
| 129 | DBusAdaptor::StringToVariant("not a real scan method"), |
| 130 | &error)); |
| 131 | } |
| 132 | } |
| 133 | |
mukesh agrawal | 8ede052 | 2011-10-03 14:57:44 -0700 | [diff] [blame] | 134 | class WiFiMainTest : public ::testing::TestWithParam<string> { |
mukesh agrawal | 3195024 | 2011-07-14 11:53:38 -0700 | [diff] [blame] | 135 | public: |
| 136 | WiFiMainTest() |
Chris Masone | b9c0059 | 2011-10-06 13:10:39 -0700 | [diff] [blame] | 137 | : manager_(&control_interface_, NULL, &glib_), |
Chris Masone | 626719f | 2011-08-18 16:58:48 -0700 | [diff] [blame] | 138 | wifi_(new WiFi(&control_interface_, |
mukesh agrawal | 3239932 | 2011-09-01 10:53:43 -0700 | [diff] [blame] | 139 | &dispatcher_, |
Chris Masone | 626719f | 2011-08-18 16:58:48 -0700 | [diff] [blame] | 140 | &manager_, |
| 141 | kDeviceName, |
| 142 | kDeviceAddress, |
| 143 | 0)), |
mukesh agrawal | dc42bb3 | 2011-07-28 10:40:26 -0700 | [diff] [blame] | 144 | supplicant_process_proxy_(new NiceMock<MockSupplicantProcessProxy>()), |
| 145 | supplicant_interface_proxy_( |
| 146 | new NiceMock<MockSupplicantInterfaceProxy>(wifi_)), |
mukesh agrawal | f2f68a5 | 2011-09-01 12:15:48 -0700 | [diff] [blame] | 147 | dhcp_config_(new MockDHCPConfig(&control_interface_, |
| 148 | &dispatcher_, |
| 149 | &dhcp_provider_, |
| 150 | kDeviceName, |
| 151 | &glib_)), |
mukesh agrawal | dc42bb3 | 2011-07-28 10:40:26 -0700 | [diff] [blame] | 152 | proxy_factory_(this) { |
mukesh agrawal | 3195024 | 2011-07-14 11:53:38 -0700 | [diff] [blame] | 153 | ::testing::DefaultValue< ::DBus::Path>::Set("/default/path"); |
mukesh agrawal | 4eb4d78 | 2011-12-05 17:34:37 +0000 | [diff] [blame] | 154 | // Except for WiFiServices created via WiFi::GetService, we expect |
| 155 | // that any WiFiService has been registered with the Manager. So |
| 156 | // default Manager.HasService to true, to make the common case |
| 157 | // easy. |
| 158 | ON_CALL(manager_, HasService(_)). |
| 159 | WillByDefault(Return(true)); |
mukesh agrawal | 3195024 | 2011-07-14 11:53:38 -0700 | [diff] [blame] | 160 | } |
mukesh agrawal | 5c4dd0b | 2011-09-14 13:53:14 -0700 | [diff] [blame] | 161 | |
| 162 | virtual void SetUp() { |
Darin Petkov | ab565bb | 2011-10-06 02:55:51 -0700 | [diff] [blame] | 163 | wifi_->proxy_factory_ = &proxy_factory_; |
mukesh agrawal | 5c4dd0b | 2011-09-14 13:53:14 -0700 | [diff] [blame] | 164 | static_cast<Device *>(wifi_)->rtnl_handler_ = &rtnl_handler_; |
| 165 | wifi_->set_dhcp_provider(&dhcp_provider_); |
Chris Masone | b9c0059 | 2011-10-06 13:10:39 -0700 | [diff] [blame] | 166 | EXPECT_CALL(manager_, DeregisterService(_)).Times(AnyNumber()); |
mukesh agrawal | 5c4dd0b | 2011-09-14 13:53:14 -0700 | [diff] [blame] | 167 | } |
| 168 | |
| 169 | virtual void TearDown() { |
Darin Petkov | ab565bb | 2011-10-06 02:55:51 -0700 | [diff] [blame] | 170 | wifi_->proxy_factory_ = NULL; |
mukesh agrawal | 3195024 | 2011-07-14 11:53:38 -0700 | [diff] [blame] | 171 | // must Stop WiFi instance, to clear its list of services. |
| 172 | // otherwise, the WiFi instance will not be deleted. (because |
| 173 | // services reference a WiFi instance, creating a cycle.) |
| 174 | wifi_->Stop(); |
mukesh agrawal | 5c4dd0b | 2011-09-14 13:53:14 -0700 | [diff] [blame] | 175 | wifi_->set_dhcp_provider(NULL); |
mukesh agrawal | f2f68a5 | 2011-09-01 12:15:48 -0700 | [diff] [blame] | 176 | } |
| 177 | |
mukesh agrawal | 3195024 | 2011-07-14 11:53:38 -0700 | [diff] [blame] | 178 | protected: |
mukesh agrawal | 7ec7131 | 2011-11-10 02:08:26 +0000 | [diff] [blame] | 179 | typedef scoped_refptr<MockWiFiService> MockWiFiServiceRefPtr; |
| 180 | |
mukesh agrawal | 3195024 | 2011-07-14 11:53:38 -0700 | [diff] [blame] | 181 | class TestProxyFactory : public ProxyFactory { |
| 182 | public: |
Paul Stewart | 7355ce1 | 2011-09-02 10:47:01 -0700 | [diff] [blame] | 183 | explicit TestProxyFactory(WiFiMainTest *test) : test_(test) {} |
mukesh agrawal | 3195024 | 2011-07-14 11:53:38 -0700 | [diff] [blame] | 184 | |
| 185 | virtual SupplicantProcessProxyInterface *CreateSupplicantProcessProxy( |
mukesh agrawal | 1830fa1 | 2011-09-26 14:31:40 -0700 | [diff] [blame] | 186 | const char */*dbus_path*/, const char */*dbus_addr*/) { |
mukesh agrawal | dc42bb3 | 2011-07-28 10:40:26 -0700 | [diff] [blame] | 187 | return test_->supplicant_process_proxy_.release(); |
mukesh agrawal | 3195024 | 2011-07-14 11:53:38 -0700 | [diff] [blame] | 188 | } |
| 189 | |
| 190 | virtual SupplicantInterfaceProxyInterface *CreateSupplicantInterfaceProxy( |
mukesh agrawal | 1830fa1 | 2011-09-26 14:31:40 -0700 | [diff] [blame] | 191 | const WiFiRefPtr &/*wifi*/, |
| 192 | const DBus::Path &/*object_path*/, |
| 193 | const char */*dbus_addr*/) { |
mukesh agrawal | dc42bb3 | 2011-07-28 10:40:26 -0700 | [diff] [blame] | 194 | return test_->supplicant_interface_proxy_.release(); |
mukesh agrawal | 3195024 | 2011-07-14 11:53:38 -0700 | [diff] [blame] | 195 | } |
| 196 | |
| 197 | private: |
mukesh agrawal | dc42bb3 | 2011-07-28 10:40:26 -0700 | [diff] [blame] | 198 | WiFiMainTest *test_; |
mukesh agrawal | 3195024 | 2011-07-14 11:53:38 -0700 | [diff] [blame] | 199 | }; |
| 200 | |
mukesh agrawal | 1590839 | 2011-11-16 18:29:25 +0000 | [diff] [blame] | 201 | WiFiServiceRefPtr CreateServiceForEndpoint(const WiFiEndpoint &endpoint) { |
| 202 | bool hidden_ssid = false; |
| 203 | return wifi_->CreateServiceForEndpoint(endpoint, hidden_ssid); |
| 204 | } |
| 205 | const WiFiServiceRefPtr &GetCurrentService() { |
| 206 | return wifi_->current_service_; |
| 207 | } |
mukesh agrawal | 3195024 | 2011-07-14 11:53:38 -0700 | [diff] [blame] | 208 | const WiFi::EndpointMap &GetEndpointMap() { |
mukesh agrawal | 165e614 | 2011-11-22 02:22:56 +0000 | [diff] [blame] | 209 | return wifi_->endpoint_by_rpcid_; |
mukesh agrawal | 3195024 | 2011-07-14 11:53:38 -0700 | [diff] [blame] | 210 | } |
mukesh agrawal | 1590839 | 2011-11-16 18:29:25 +0000 | [diff] [blame] | 211 | const WiFiServiceRefPtr &GetPendingService() { |
| 212 | return wifi_->pending_service_; |
| 213 | } |
mukesh agrawal | 165e614 | 2011-11-22 02:22:56 +0000 | [diff] [blame] | 214 | const vector<WiFiServiceRefPtr> &GetServices() { |
| 215 | return wifi_->services_; |
mukesh agrawal | 3195024 | 2011-07-14 11:53:38 -0700 | [diff] [blame] | 216 | } |
| 217 | // note: the tests need the proxies referenced by WiFi (not the |
| 218 | // proxies instantiated by WiFiMainTest), to ensure that WiFi |
| 219 | // sets up its proxies correctly. |
| 220 | SupplicantProcessProxyInterface *GetSupplicantProcessProxy() { |
| 221 | return wifi_->supplicant_process_proxy_.get(); |
| 222 | } |
mukesh agrawal | 8a3188d | 2011-12-01 20:56:44 +0000 | [diff] [blame] | 223 | MockSupplicantInterfaceProxy *GetSupplicantInterfaceProxy() { |
| 224 | return dynamic_cast<MockSupplicantInterfaceProxy *>( |
| 225 | wifi_->supplicant_interface_proxy_.get()); |
mukesh agrawal | 3195024 | 2011-07-14 11:53:38 -0700 | [diff] [blame] | 226 | } |
mukesh agrawal | 7ec7131 | 2011-11-10 02:08:26 +0000 | [diff] [blame] | 227 | const string &GetSupplicantState() { |
| 228 | return wifi_->supplicant_state_; |
| 229 | } |
| 230 | void InitiateConnect(WiFiServiceRefPtr service) { |
mukesh agrawal | 6e27777 | 2011-09-29 15:04:23 -0700 | [diff] [blame] | 231 | map<string, ::DBus::Variant> params; |
| 232 | wifi_->ConnectTo(service, params); |
mukesh agrawal | 3195024 | 2011-07-14 11:53:38 -0700 | [diff] [blame] | 233 | } |
mukesh agrawal | 0ed0f2e | 2011-12-05 20:36:17 +0000 | [diff] [blame] | 234 | void InitiateDisconnect(WiFiServiceRefPtr service) { |
| 235 | wifi_->DisconnectFrom(service); |
| 236 | } |
mukesh agrawal | f2f68a5 | 2011-09-01 12:15:48 -0700 | [diff] [blame] | 237 | bool IsLinkUp() { |
| 238 | return wifi_->link_up_; |
| 239 | } |
mukesh agrawal | 1590839 | 2011-11-16 18:29:25 +0000 | [diff] [blame] | 240 | WiFiEndpointRefPtr MakeEndpoint(const string &ssid, const string &bssid) { |
mukesh agrawal | 8a3188d | 2011-12-01 20:56:44 +0000 | [diff] [blame] | 241 | return WiFiEndpoint::MakeOpenEndpoint(ssid, bssid); |
mukesh agrawal | 1590839 | 2011-11-16 18:29:25 +0000 | [diff] [blame] | 242 | } |
mukesh agrawal | 7ec7131 | 2011-11-10 02:08:26 +0000 | [diff] [blame] | 243 | MockWiFiServiceRefPtr MakeMockService() { |
| 244 | vector<uint8_t> ssid(1, 'a'); |
| 245 | return new MockWiFiService( |
| 246 | &control_interface_, |
| 247 | &dispatcher_, |
| 248 | &manager_, |
| 249 | wifi_, |
| 250 | ssid, |
| 251 | flimflam::kModeManaged, |
| 252 | flimflam::kSecurityNone, |
| 253 | false); |
| 254 | } |
mukesh agrawal | 261daca | 2011-12-02 18:56:56 +0000 | [diff] [blame] | 255 | void RemoveBSS(const ::DBus::Path &bss_path); |
mukesh agrawal | 3195024 | 2011-07-14 11:53:38 -0700 | [diff] [blame] | 256 | void ReportBSS(const ::DBus::Path &bss_path, |
| 257 | const string &ssid, |
| 258 | const string &bssid, |
| 259 | int16_t signal_strength, |
| 260 | const char *mode); |
mukesh agrawal | f2f68a5 | 2011-09-01 12:15:48 -0700 | [diff] [blame] | 261 | void ReportLinkUp() { |
| 262 | wifi_->LinkEvent(IFF_LOWER_UP, IFF_LOWER_UP); |
| 263 | } |
mukesh agrawal | 3195024 | 2011-07-14 11:53:38 -0700 | [diff] [blame] | 264 | void ReportScanDone() { |
mukesh agrawal | dc42bb3 | 2011-07-28 10:40:26 -0700 | [diff] [blame] | 265 | wifi_->ScanDoneTask(); |
mukesh agrawal | 3195024 | 2011-07-14 11:53:38 -0700 | [diff] [blame] | 266 | } |
mukesh agrawal | 1590839 | 2011-11-16 18:29:25 +0000 | [diff] [blame] | 267 | void ReportCurrentBSSChanged(const string &new_bss) { |
| 268 | wifi_->CurrentBSSChanged(new_bss); |
| 269 | } |
mukesh agrawal | 7ec7131 | 2011-11-10 02:08:26 +0000 | [diff] [blame] | 270 | void ReportStateChanged(const string &new_state) { |
| 271 | wifi_->StateChanged(new_state); |
| 272 | } |
mukesh agrawal | 3195024 | 2011-07-14 11:53:38 -0700 | [diff] [blame] | 273 | void StartWiFi() { |
| 274 | wifi_->Start(); |
mukesh agrawal | 3195024 | 2011-07-14 11:53:38 -0700 | [diff] [blame] | 275 | } |
| 276 | void StopWiFi() { |
| 277 | wifi_->Stop(); |
| 278 | } |
Paul Stewart | a41e38d | 2011-11-11 07:47:29 -0800 | [diff] [blame] | 279 | void GetOpenService(const char *service_type, |
mukesh agrawal | 7a4e400 | 2011-09-06 11:26:05 -0700 | [diff] [blame] | 280 | const char *ssid, |
| 281 | const char *mode, |
Paul Stewart | ced6a0b | 2011-11-08 15:32:04 -0800 | [diff] [blame] | 282 | Error *result) { |
| 283 | GetServiceInner(service_type, ssid, mode, NULL, NULL, false, result); |
mukesh agrawal | 7a4e400 | 2011-09-06 11:26:05 -0700 | [diff] [blame] | 284 | } |
| 285 | void GetService(const char *service_type, |
| 286 | const char *ssid, |
| 287 | const char *mode, |
| 288 | const char *security, |
| 289 | const char *passphrase, |
Paul Stewart | ced6a0b | 2011-11-08 15:32:04 -0800 | [diff] [blame] | 290 | Error *result) { |
| 291 | GetServiceInner(service_type, ssid, mode, security, passphrase, false, |
| 292 | result); |
| 293 | } |
| 294 | WiFiServiceRefPtr GetServiceInner(const char *service_type, |
| 295 | const char *ssid, |
| 296 | const char *mode, |
| 297 | const char *security, |
| 298 | const char *passphrase, |
Paul Stewart | a41e38d | 2011-11-11 07:47:29 -0800 | [diff] [blame] | 299 | bool allow_hidden, |
Paul Stewart | ced6a0b | 2011-11-08 15:32:04 -0800 | [diff] [blame] | 300 | Error *result) { |
mukesh agrawal | 7a4e400 | 2011-09-06 11:26:05 -0700 | [diff] [blame] | 301 | map<string, ::DBus::Variant> args; |
mukesh agrawal | 7a4e400 | 2011-09-06 11:26:05 -0700 | [diff] [blame] | 302 | // in general, we want to avoid D-Bus specific code for any RPCs |
| 303 | // that come in via adaptors. we make an exception here, because |
| 304 | // calls to GetWifiService are rerouted from the Manager object to |
| 305 | // the Wifi class. |
| 306 | if (service_type != NULL) |
| 307 | args[flimflam::kTypeProperty].writer().append_string(service_type); |
| 308 | if (ssid != NULL) |
| 309 | args[flimflam::kSSIDProperty].writer().append_string(ssid); |
| 310 | if (mode != NULL) |
| 311 | args[flimflam::kModeProperty].writer().append_string(mode); |
| 312 | if (security != NULL) |
| 313 | args[flimflam::kSecurityProperty].writer().append_string(security); |
| 314 | if (passphrase != NULL) |
| 315 | args[flimflam::kPassphraseProperty].writer().append_string(passphrase); |
Paul Stewart | a41e38d | 2011-11-11 07:47:29 -0800 | [diff] [blame] | 316 | if (!allow_hidden) |
| 317 | args[flimflam::kWifiHiddenSsid].writer().append_bool(false); |
mukesh agrawal | 7a4e400 | 2011-09-06 11:26:05 -0700 | [diff] [blame] | 318 | |
Paul Stewart | ced6a0b | 2011-11-08 15:32:04 -0800 | [diff] [blame] | 319 | Error e; |
| 320 | KeyValueStore args_kv; |
mukesh agrawal | 7a4e400 | 2011-09-06 11:26:05 -0700 | [diff] [blame] | 321 | DBusAdaptor::ArgsToKeyValueStore(args, &args_kv, &e); |
Paul Stewart | ced6a0b | 2011-11-08 15:32:04 -0800 | [diff] [blame] | 322 | return wifi_->GetService(args_kv, result); |
mukesh agrawal | 7a4e400 | 2011-09-06 11:26:05 -0700 | [diff] [blame] | 323 | } |
Paul Stewart | a41e38d | 2011-11-11 07:47:29 -0800 | [diff] [blame] | 324 | WiFiServiceRefPtr FindService(const vector<uint8_t> &ssid, |
| 325 | const string &mode, |
| 326 | const string &security) { |
| 327 | return wifi_->FindService(ssid, mode, security); |
| 328 | } |
| 329 | bool LoadHiddenServices(StoreInterface *storage) { |
| 330 | return wifi_->LoadHiddenServices(storage); |
| 331 | } |
| 332 | void SetupHiddenStorage(MockStore *storage, const string &ssid, string *id) { |
| 333 | const string hex_ssid = base::HexEncode(ssid.data(), ssid.size()); |
| 334 | *id = StringToLowerASCII(base::StringPrintf("%s_%s_%s_%s_%s", |
| 335 | flimflam::kTypeWifi, |
| 336 | kDeviceAddress, |
| 337 | hex_ssid.c_str(), |
| 338 | flimflam::kModeManaged, |
| 339 | flimflam::kSecurityNone)); |
| 340 | const char *groups[] = { id->c_str() }; |
| 341 | EXPECT_CALL(*storage, GetGroupsWithKey(flimflam::kWifiHiddenSsid)) |
| 342 | .WillRepeatedly(Return(set<string>(groups, groups + 1))); |
| 343 | EXPECT_CALL(*storage, GetBool(StrEq(*id), flimflam::kWifiHiddenSsid, _)) |
| 344 | .WillRepeatedly(DoAll(SetArgumentPointee<2>(true), Return(true))); |
| 345 | EXPECT_CALL(*storage, GetString(StrEq(*id), flimflam::kSSIDProperty, _)) |
| 346 | .WillRepeatedly(DoAll(SetArgumentPointee<2>(hex_ssid), Return(true))); |
| 347 | } |
mukesh agrawal | 3239932 | 2011-09-01 10:53:43 -0700 | [diff] [blame] | 348 | MockManager *manager() { |
| 349 | return &manager_; |
| 350 | } |
| 351 | const WiFiConstRefPtr wifi() const { |
| 352 | return wifi_; |
| 353 | } |
| 354 | |
| 355 | EventDispatcher dispatcher_; |
mukesh agrawal | 5c4dd0b | 2011-09-14 13:53:14 -0700 | [diff] [blame] | 356 | NiceMock<MockRTNLHandler> rtnl_handler_; |
mukesh agrawal | 3195024 | 2011-07-14 11:53:38 -0700 | [diff] [blame] | 357 | |
| 358 | private: |
mukesh agrawal | dc42bb3 | 2011-07-28 10:40:26 -0700 | [diff] [blame] | 359 | NiceMockControl control_interface_; |
mukesh agrawal | f2f68a5 | 2011-09-01 12:15:48 -0700 | [diff] [blame] | 360 | MockGLib glib_; |
Chris Masone | 2ae797d | 2011-08-23 20:41:00 -0700 | [diff] [blame] | 361 | MockManager manager_; |
mukesh agrawal | 3195024 | 2011-07-14 11:53:38 -0700 | [diff] [blame] | 362 | WiFiRefPtr wifi_; |
mukesh agrawal | 3195024 | 2011-07-14 11:53:38 -0700 | [diff] [blame] | 363 | |
| 364 | // protected fields interspersed between private fields, due to |
| 365 | // initialization order |
| 366 | protected: |
| 367 | static const char kDeviceName[]; |
Chris Masone | 626719f | 2011-08-18 16:58:48 -0700 | [diff] [blame] | 368 | static const char kDeviceAddress[]; |
mukesh agrawal | 3195024 | 2011-07-14 11:53:38 -0700 | [diff] [blame] | 369 | static const char kNetworkModeAdHoc[]; |
| 370 | static const char kNetworkModeInfrastructure[]; |
| 371 | |
mukesh agrawal | dc42bb3 | 2011-07-28 10:40:26 -0700 | [diff] [blame] | 372 | scoped_ptr<MockSupplicantProcessProxy> supplicant_process_proxy_; |
| 373 | scoped_ptr<MockSupplicantInterfaceProxy> supplicant_interface_proxy_; |
mukesh agrawal | f2f68a5 | 2011-09-01 12:15:48 -0700 | [diff] [blame] | 374 | MockDHCPProvider dhcp_provider_; |
| 375 | scoped_refptr<MockDHCPConfig> dhcp_config_; |
mukesh agrawal | 3195024 | 2011-07-14 11:53:38 -0700 | [diff] [blame] | 376 | |
| 377 | private: |
| 378 | TestProxyFactory proxy_factory_; |
mukesh agrawal | 3195024 | 2011-07-14 11:53:38 -0700 | [diff] [blame] | 379 | }; |
| 380 | |
| 381 | const char WiFiMainTest::kDeviceName[] = "wlan0"; |
Paul Stewart | a41e38d | 2011-11-11 07:47:29 -0800 | [diff] [blame] | 382 | const char WiFiMainTest::kDeviceAddress[] = "000102030405"; |
mukesh agrawal | 3195024 | 2011-07-14 11:53:38 -0700 | [diff] [blame] | 383 | const char WiFiMainTest::kNetworkModeAdHoc[] = "ad-hoc"; |
| 384 | const char WiFiMainTest::kNetworkModeInfrastructure[] = "infrastructure"; |
| 385 | |
mukesh agrawal | 261daca | 2011-12-02 18:56:56 +0000 | [diff] [blame] | 386 | void WiFiMainTest::RemoveBSS(const ::DBus::Path &bss_path) { |
mukesh agrawal | b4bc57d | 2011-12-07 01:07:47 +0000 | [diff] [blame] | 387 | wifi_->BSSRemovedTask(bss_path); |
mukesh agrawal | 261daca | 2011-12-02 18:56:56 +0000 | [diff] [blame] | 388 | } |
| 389 | |
mukesh agrawal | 3195024 | 2011-07-14 11:53:38 -0700 | [diff] [blame] | 390 | void WiFiMainTest::ReportBSS(const ::DBus::Path &bss_path, |
| 391 | const string &ssid, |
| 392 | const string &bssid, |
| 393 | int16_t signal_strength, |
| 394 | const char *mode) { |
| 395 | map<string, ::DBus::Variant> bss_properties; |
| 396 | |
| 397 | { |
| 398 | DBus::MessageIter writer(bss_properties["SSID"].writer()); |
| 399 | writer << vector<uint8_t>(ssid.begin(), ssid.end()); |
| 400 | } |
| 401 | { |
| 402 | string bssid_nosep; |
| 403 | vector<uint8_t> bssid_bytes; |
| 404 | RemoveChars(bssid, ":", &bssid_nosep); |
| 405 | base::HexStringToBytes(bssid_nosep, &bssid_bytes); |
| 406 | |
| 407 | DBus::MessageIter writer(bss_properties["BSSID"].writer()); |
| 408 | writer << bssid_bytes; |
| 409 | } |
| 410 | bss_properties["Signal"].writer().append_int16(signal_strength); |
| 411 | bss_properties["Mode"].writer().append_string(mode); |
mukesh agrawal | b4bc57d | 2011-12-07 01:07:47 +0000 | [diff] [blame] | 412 | wifi_->BSSAddedTask(bss_path, bss_properties); |
mukesh agrawal | 3195024 | 2011-07-14 11:53:38 -0700 | [diff] [blame] | 413 | } |
| 414 | |
| 415 | TEST_F(WiFiMainTest, ProxiesSetUpDuringStart) { |
| 416 | EXPECT_TRUE(GetSupplicantProcessProxy() == NULL); |
| 417 | EXPECT_TRUE(GetSupplicantInterfaceProxy() == NULL); |
| 418 | |
| 419 | StartWiFi(); |
| 420 | EXPECT_FALSE(GetSupplicantProcessProxy() == NULL); |
| 421 | EXPECT_FALSE(GetSupplicantInterfaceProxy() == NULL); |
| 422 | } |
| 423 | |
| 424 | TEST_F(WiFiMainTest, CleanStart) { |
| 425 | EXPECT_CALL(*supplicant_process_proxy_, CreateInterface(_)); |
| 426 | EXPECT_CALL(*supplicant_process_proxy_, GetInterface(_)) |
| 427 | .Times(AnyNumber()) |
| 428 | .WillRepeatedly(Throw( |
| 429 | DBus::Error( |
| 430 | "fi.w1.wpa_supplicant1.InterfaceUnknown", |
| 431 | "test threw fi.w1.wpa_supplicant1.InterfaceUnknown"))); |
| 432 | EXPECT_CALL(*supplicant_interface_proxy_, Scan(_)); |
| 433 | StartWiFi(); |
mukesh agrawal | 3239932 | 2011-09-01 10:53:43 -0700 | [diff] [blame] | 434 | dispatcher_.DispatchPendingEvents(); |
mukesh agrawal | 3195024 | 2011-07-14 11:53:38 -0700 | [diff] [blame] | 435 | } |
| 436 | |
| 437 | TEST_F(WiFiMainTest, Restart) { |
| 438 | EXPECT_CALL(*supplicant_process_proxy_, CreateInterface(_)) |
| 439 | .Times(AnyNumber()) |
| 440 | .WillRepeatedly(Throw( |
| 441 | DBus::Error( |
| 442 | "fi.w1.wpa_supplicant1.InterfaceExists", |
| 443 | "test thew fi.w1.wpa_supplicant1.InterfaceExists"))); |
| 444 | EXPECT_CALL(*supplicant_process_proxy_, GetInterface(_)); |
| 445 | EXPECT_CALL(*supplicant_interface_proxy_, Scan(_)); |
| 446 | StartWiFi(); |
mukesh agrawal | 3239932 | 2011-09-01 10:53:43 -0700 | [diff] [blame] | 447 | dispatcher_.DispatchPendingEvents(); |
mukesh agrawal | 3195024 | 2011-07-14 11:53:38 -0700 | [diff] [blame] | 448 | } |
| 449 | |
| 450 | TEST_F(WiFiMainTest, StartClearsState) { |
| 451 | EXPECT_CALL(*supplicant_interface_proxy_, RemoveAllNetworks()); |
| 452 | EXPECT_CALL(*supplicant_interface_proxy_, FlushBSS(_)); |
| 453 | StartWiFi(); |
| 454 | } |
| 455 | |
| 456 | TEST_F(WiFiMainTest, ScanResults) { |
| 457 | StartWiFi(); |
| 458 | ReportBSS("bss0", "ssid0", "00:00:00:00:00:00", 0, kNetworkModeAdHoc); |
| 459 | ReportBSS( |
| 460 | "bss1", "ssid1", "00:00:00:00:00:01", 1, kNetworkModeInfrastructure); |
| 461 | ReportBSS( |
| 462 | "bss2", "ssid2", "00:00:00:00:00:02", 2, kNetworkModeInfrastructure); |
| 463 | ReportBSS( |
| 464 | "bss3", "ssid3", "00:00:00:00:00:03", 3, kNetworkModeInfrastructure); |
| 465 | ReportBSS("bss4", "ssid4", "00:00:00:00:00:04", 4, kNetworkModeAdHoc); |
| 466 | EXPECT_EQ(5, GetEndpointMap().size()); |
| 467 | } |
| 468 | |
| 469 | TEST_F(WiFiMainTest, ScanResultsWithUpdates) { |
| 470 | StartWiFi(); |
| 471 | ReportBSS("bss0", "ssid0", "00:00:00:00:00:00", 0, kNetworkModeAdHoc); |
| 472 | ReportBSS( |
| 473 | "bss1", "ssid1", "00:00:00:00:00:01", 1, kNetworkModeInfrastructure); |
| 474 | ReportBSS( |
| 475 | "bss2", "ssid2", "00:00:00:00:00:02", 2, kNetworkModeInfrastructure); |
| 476 | ReportBSS( |
| 477 | "bss1", "ssid1", "00:00:00:00:00:01", 3, kNetworkModeInfrastructure); |
| 478 | ReportBSS("bss0", "ssid0", "00:00:00:00:00:00", 4, kNetworkModeAdHoc); |
mukesh agrawal | 165e614 | 2011-11-22 02:22:56 +0000 | [diff] [blame] | 479 | |
| 480 | const WiFi::EndpointMap &endpoints_by_rpcid = GetEndpointMap(); |
| 481 | EXPECT_EQ(3, endpoints_by_rpcid.size()); |
| 482 | |
| 483 | WiFi::EndpointMap::const_iterator i; |
| 484 | WiFiEndpointRefPtr endpoint; |
| 485 | for (i = endpoints_by_rpcid.begin(); |
| 486 | i != endpoints_by_rpcid.end(); |
| 487 | ++i) { |
| 488 | if (i->second->bssid_string() == "00:00:00:00:00:00") |
| 489 | break; |
| 490 | } |
| 491 | ASSERT_TRUE(i != endpoints_by_rpcid.end()); |
| 492 | EXPECT_EQ(4, i->second->signal_strength()); |
mukesh agrawal | 3195024 | 2011-07-14 11:53:38 -0700 | [diff] [blame] | 493 | } |
| 494 | |
| 495 | TEST_F(WiFiMainTest, ScanCompleted) { |
| 496 | StartWiFi(); |
mukesh agrawal | 261daca | 2011-12-02 18:56:56 +0000 | [diff] [blame] | 497 | EXPECT_CALL(*manager(), RegisterService(_)) |
| 498 | .Times(3); |
mukesh agrawal | 3195024 | 2011-07-14 11:53:38 -0700 | [diff] [blame] | 499 | ReportBSS("bss0", "ssid0", "00:00:00:00:00:00", 0, kNetworkModeAdHoc); |
| 500 | ReportBSS( |
| 501 | "bss1", "ssid1", "00:00:00:00:00:01", 1, kNetworkModeInfrastructure); |
| 502 | ReportBSS( |
| 503 | "bss2", "ssid2", "00:00:00:00:00:02", 2, kNetworkModeInfrastructure); |
| 504 | ReportScanDone(); |
mukesh agrawal | 165e614 | 2011-11-22 02:22:56 +0000 | [diff] [blame] | 505 | EXPECT_EQ(3, GetServices().size()); |
| 506 | } |
| 507 | |
| 508 | TEST_F(WiFiMainTest, EndpointGroupingTogether) { |
| 509 | StartWiFi(); |
mukesh agrawal | 261daca | 2011-12-02 18:56:56 +0000 | [diff] [blame] | 510 | |
| 511 | InSequence s; |
| 512 | EXPECT_CALL(*manager(), RegisterService(_)); |
| 513 | EXPECT_CALL(*manager(), UpdateService(_)); |
mukesh agrawal | 165e614 | 2011-11-22 02:22:56 +0000 | [diff] [blame] | 514 | ReportBSS("bss0", "ssid", "00:00:00:00:00:00", 0, kNetworkModeAdHoc); |
| 515 | ReportBSS("bss1", "ssid", "00:00:00:00:00:01", 0, kNetworkModeAdHoc); |
| 516 | ReportScanDone(); |
| 517 | EXPECT_EQ(1, GetServices().size()); |
| 518 | } |
| 519 | |
| 520 | TEST_F(WiFiMainTest, EndpointGroupingDifferentSSID) { |
| 521 | StartWiFi(); |
mukesh agrawal | 261daca | 2011-12-02 18:56:56 +0000 | [diff] [blame] | 522 | EXPECT_CALL(*manager(), RegisterService(_)) |
| 523 | .Times(2); |
mukesh agrawal | 165e614 | 2011-11-22 02:22:56 +0000 | [diff] [blame] | 524 | ReportBSS("bss0", "ssid1", "00:00:00:00:00:00", 0, kNetworkModeAdHoc); |
| 525 | ReportBSS("bss1", "ssid2", "00:00:00:00:00:01", 0, kNetworkModeAdHoc); |
| 526 | ReportScanDone(); |
| 527 | EXPECT_EQ(2, GetServices().size()); |
| 528 | } |
| 529 | |
| 530 | TEST_F(WiFiMainTest, EndpointGroupingDifferentMode) { |
| 531 | StartWiFi(); |
mukesh agrawal | 261daca | 2011-12-02 18:56:56 +0000 | [diff] [blame] | 532 | EXPECT_CALL(*manager(), RegisterService(_)) |
| 533 | .Times(2); |
mukesh agrawal | 165e614 | 2011-11-22 02:22:56 +0000 | [diff] [blame] | 534 | ReportBSS("bss0", "ssid", "00:00:00:00:00:00", 0, kNetworkModeAdHoc); |
| 535 | ReportBSS("bss1", "ssid", "00:00:00:00:00:01", 0, kNetworkModeInfrastructure); |
| 536 | ReportScanDone(); |
| 537 | EXPECT_EQ(2, GetServices().size()); |
mukesh agrawal | 3195024 | 2011-07-14 11:53:38 -0700 | [diff] [blame] | 538 | } |
| 539 | |
mukesh agrawal | 261daca | 2011-12-02 18:56:56 +0000 | [diff] [blame] | 540 | TEST_F(WiFiMainTest, NonExistentBSSRemoved) { |
| 541 | // Removal of non-existent BSS should not cause a crash. |
| 542 | StartWiFi(); |
| 543 | RemoveBSS("bss0"); |
| 544 | EXPECT_EQ(0, GetServices().size()); |
| 545 | } |
| 546 | |
| 547 | TEST_F(WiFiMainTest, LoneBSSRemoved) { |
| 548 | StartWiFi(); |
| 549 | ReportBSS("bss0", "ssid", "00:00:00:00:00:00", 0, kNetworkModeAdHoc); |
| 550 | ReportScanDone(); |
| 551 | EXPECT_EQ(1, GetServices().size()); |
| 552 | EXPECT_TRUE(GetServices().front()->IsVisible()); |
| 553 | |
mukesh agrawal | 8a3188d | 2011-12-01 20:56:44 +0000 | [diff] [blame] | 554 | EXPECT_CALL(*manager(), DeregisterService(_)); |
mukesh agrawal | 261daca | 2011-12-02 18:56:56 +0000 | [diff] [blame] | 555 | RemoveBSS("bss0"); |
mukesh agrawal | 8a3188d | 2011-12-01 20:56:44 +0000 | [diff] [blame] | 556 | EXPECT_TRUE(GetServices().empty()); |
| 557 | } |
| 558 | |
| 559 | TEST_F(WiFiMainTest, LoneBSSRemovedWhileConnected) { |
| 560 | StartWiFi(); |
| 561 | ReportBSS("bss0", "ssid", "00:00:00:00:00:00", 0, kNetworkModeAdHoc); |
| 562 | ReportScanDone(); |
| 563 | ReportCurrentBSSChanged("bss0"); |
| 564 | |
| 565 | EXPECT_CALL(*GetSupplicantInterfaceProxy(), Disconnect()); |
| 566 | EXPECT_CALL(*manager(), DeregisterService(_)); |
| 567 | RemoveBSS("bss0"); |
| 568 | EXPECT_TRUE(GetServices().empty()); |
| 569 | } |
| 570 | |
| 571 | TEST_F(WiFiMainTest, LoneBSSRemovedWhileConnectedToHidden) { |
| 572 | StartWiFi(); |
| 573 | |
| 574 | Error e; |
| 575 | WiFiServiceRefPtr service = |
| 576 | GetServiceInner(flimflam::kTypeWifi, "ssid", flimflam::kModeManaged, |
| 577 | NULL, NULL, true, &e); |
mukesh agrawal | 261daca | 2011-12-02 18:56:56 +0000 | [diff] [blame] | 578 | EXPECT_EQ(1, GetServices().size()); |
mukesh agrawal | 8a3188d | 2011-12-01 20:56:44 +0000 | [diff] [blame] | 579 | |
| 580 | ReportBSS("bss", "ssid", "00:00:00:00:00:01", 0, kNetworkModeInfrastructure); |
| 581 | ReportScanDone(); |
| 582 | ReportCurrentBSSChanged("bss"); |
| 583 | EXPECT_EQ(1, GetServices().size()); |
| 584 | |
| 585 | EXPECT_CALL(*GetSupplicantInterfaceProxy(), Disconnect()); |
| 586 | EXPECT_CALL(*manager(), UpdateService(_)); |
| 587 | RemoveBSS("bss"); |
| 588 | EXPECT_TRUE(manager()->HasService(service)); |
| 589 | EXPECT_EQ(1, GetServices().size()); |
| 590 | // Verify expectations now, because WiFi may call UpdateService when |
| 591 | // WiFi is Stop()-ed (during TearDown()). |
| 592 | Mock::VerifyAndClearExpectations(manager()); |
mukesh agrawal | 261daca | 2011-12-02 18:56:56 +0000 | [diff] [blame] | 593 | } |
| 594 | |
| 595 | TEST_F(WiFiMainTest, NonSolitaryBSSRemoved) { |
| 596 | StartWiFi(); |
| 597 | ReportBSS("bss0", "ssid", "00:00:00:00:00:00", 0, kNetworkModeAdHoc); |
| 598 | ReportBSS("bss1", "ssid", "00:00:00:00:00:00", 0, kNetworkModeAdHoc); |
| 599 | ReportScanDone(); |
| 600 | EXPECT_EQ(1, GetServices().size()); |
| 601 | EXPECT_TRUE(GetServices().front()->IsVisible()); |
| 602 | |
| 603 | EXPECT_CALL(*manager(), UpdateService(_)); |
| 604 | RemoveBSS("bss0"); |
| 605 | EXPECT_TRUE(GetServices().front()->IsVisible()); |
| 606 | EXPECT_EQ(1, GetServices().size()); |
| 607 | } |
| 608 | |
mukesh agrawal | 3195024 | 2011-07-14 11:53:38 -0700 | [diff] [blame] | 609 | TEST_F(WiFiMainTest, Connect) { |
mukesh agrawal | dc42bb3 | 2011-07-28 10:40:26 -0700 | [diff] [blame] | 610 | MockSupplicantInterfaceProxy &supplicant_interface_proxy = |
| 611 | *supplicant_interface_proxy_; |
| 612 | |
mukesh agrawal | 3195024 | 2011-07-14 11:53:38 -0700 | [diff] [blame] | 613 | StartWiFi(); |
| 614 | ReportBSS("bss0", "ssid0", "00:00:00:00:00:00", 0, kNetworkModeAdHoc); |
| 615 | ReportScanDone(); |
| 616 | |
| 617 | { |
| 618 | InSequence s; |
| 619 | DBus::Path fake_path("/fake/path"); |
mukesh agrawal | 165e614 | 2011-11-22 02:22:56 +0000 | [diff] [blame] | 620 | WiFiService *service(GetServices().begin()->get()); |
mukesh agrawal | 3195024 | 2011-07-14 11:53:38 -0700 | [diff] [blame] | 621 | |
mukesh agrawal | dc42bb3 | 2011-07-28 10:40:26 -0700 | [diff] [blame] | 622 | EXPECT_CALL(supplicant_interface_proxy, AddNetwork(_)) |
mukesh agrawal | 3195024 | 2011-07-14 11:53:38 -0700 | [diff] [blame] | 623 | .WillOnce(Return(fake_path)); |
mukesh agrawal | dc42bb3 | 2011-07-28 10:40:26 -0700 | [diff] [blame] | 624 | EXPECT_CALL(supplicant_interface_proxy, SelectNetwork(fake_path)); |
mukesh agrawal | 3239932 | 2011-09-01 10:53:43 -0700 | [diff] [blame] | 625 | InitiateConnect(service); |
mukesh agrawal | f2f68a5 | 2011-09-01 12:15:48 -0700 | [diff] [blame] | 626 | EXPECT_EQ(static_cast<Service *>(service), |
| 627 | wifi()->selected_service_.get()); |
mukesh agrawal | 3195024 | 2011-07-14 11:53:38 -0700 | [diff] [blame] | 628 | } |
| 629 | } |
| 630 | |
mukesh agrawal | 0ed0f2e | 2011-12-05 20:36:17 +0000 | [diff] [blame] | 631 | TEST_F(WiFiMainTest, DisconnectPendingService) { |
| 632 | MockSupplicantInterfaceProxy &supplicant_interface_proxy = |
| 633 | *supplicant_interface_proxy_; |
| 634 | |
| 635 | StartWiFi(); |
| 636 | ReportBSS("bss0", "ssid0", "00:00:00:00:00:00", 0, kNetworkModeAdHoc); |
| 637 | WiFiService *service(GetServices().begin()->get()); |
| 638 | InitiateConnect(service); |
| 639 | |
| 640 | EXPECT_FALSE(GetPendingService() == NULL); |
| 641 | EXPECT_CALL(supplicant_interface_proxy, Disconnect()); |
| 642 | InitiateDisconnect(service); |
| 643 | |
| 644 | EXPECT_TRUE(GetPendingService() == NULL); |
| 645 | } |
| 646 | |
| 647 | TEST_F(WiFiMainTest, DisconnectPendingServiceWithCurrent) { |
| 648 | MockSupplicantInterfaceProxy &supplicant_interface_proxy = |
| 649 | *supplicant_interface_proxy_; |
| 650 | |
| 651 | StartWiFi(); |
| 652 | ReportBSS("bss0", "ssid0", "00:00:00:00:00:00", 0, kNetworkModeAdHoc); |
| 653 | ReportBSS("bss1", "ssid1", "00:00:00:00:00:01", 0, kNetworkModeAdHoc); |
| 654 | WiFiService *service0(GetServices()[0].get()); |
| 655 | WiFiService *service1(GetServices()[1].get()); |
| 656 | |
| 657 | InitiateConnect(service0); |
| 658 | ReportCurrentBSSChanged("bss0"); |
| 659 | ReportStateChanged(wpa_supplicant::kInterfaceStateCompleted); |
| 660 | InitiateConnect(service1); |
| 661 | |
| 662 | EXPECT_EQ(service0, GetCurrentService()); |
| 663 | EXPECT_EQ(service1, GetPendingService()); |
| 664 | EXPECT_CALL(supplicant_interface_proxy, Disconnect()); |
| 665 | InitiateDisconnect(service1); |
| 666 | |
| 667 | // |current_service_| will be unchanged until supplicant signals |
| 668 | // that CurrentBSS has changed. |
| 669 | EXPECT_EQ(service0, GetCurrentService()); |
| 670 | // |pending_service_| is updated immediately. |
| 671 | EXPECT_TRUE(GetPendingService() == NULL); |
| 672 | } |
| 673 | |
| 674 | TEST_F(WiFiMainTest, DisconnectCurrentService) { |
| 675 | MockSupplicantInterfaceProxy &supplicant_interface_proxy = |
| 676 | *supplicant_interface_proxy_; |
| 677 | |
| 678 | StartWiFi(); |
| 679 | ReportBSS("bss0", "ssid0", "00:00:00:00:00:00", 0, kNetworkModeAdHoc); |
| 680 | WiFiService *service(GetServices().begin()->get()); |
| 681 | InitiateConnect(service); |
| 682 | ReportCurrentBSSChanged("bss0"); |
| 683 | ReportStateChanged(wpa_supplicant::kInterfaceStateCompleted); |
| 684 | |
| 685 | EXPECT_EQ(service, GetCurrentService()); |
| 686 | EXPECT_CALL(supplicant_interface_proxy, Disconnect()); |
| 687 | InitiateDisconnect(service); |
| 688 | |
| 689 | // |current_service_| should not change until supplicant reports |
| 690 | // a BSS change. |
| 691 | EXPECT_EQ(service, GetCurrentService()); |
| 692 | } |
| 693 | |
| 694 | TEST_F(WiFiMainTest, DisconnectCurrentServiceWithPending) { |
| 695 | MockSupplicantInterfaceProxy &supplicant_interface_proxy = |
| 696 | *supplicant_interface_proxy_; |
| 697 | |
| 698 | StartWiFi(); |
| 699 | ReportBSS("bss0", "ssid0", "00:00:00:00:00:00", 0, kNetworkModeAdHoc); |
| 700 | ReportBSS("bss1", "ssid1", "00:00:00:00:00:01", 0, kNetworkModeAdHoc); |
| 701 | WiFiService *service0(GetServices()[0].get()); |
| 702 | WiFiService *service1(GetServices()[1].get()); |
| 703 | |
| 704 | InitiateConnect(service0); |
| 705 | ReportCurrentBSSChanged("bss0"); |
| 706 | ReportStateChanged(wpa_supplicant::kInterfaceStateCompleted); |
| 707 | InitiateConnect(service1); |
| 708 | |
| 709 | EXPECT_EQ(service0, GetCurrentService()); |
| 710 | EXPECT_EQ(service1, GetPendingService()); |
| 711 | EXPECT_CALL(supplicant_interface_proxy, Disconnect()) |
| 712 | .Times(0); |
| 713 | InitiateDisconnect(service0); |
| 714 | |
| 715 | EXPECT_EQ(service0, GetCurrentService()); |
| 716 | EXPECT_EQ(service1, GetPendingService()); |
| 717 | } |
| 718 | |
| 719 | TEST_F(WiFiMainTest, DisconnectInvalidService) { |
| 720 | MockSupplicantInterfaceProxy &supplicant_interface_proxy = |
| 721 | *supplicant_interface_proxy_; |
| 722 | |
| 723 | StartWiFi(); |
| 724 | ReportBSS("bss0", "ssid0", "00:00:00:00:00:00", 0, kNetworkModeAdHoc); |
| 725 | WiFiService *service(GetServices().begin()->get()); |
| 726 | EXPECT_CALL(supplicant_interface_proxy, Disconnect()) |
| 727 | .Times(0); |
| 728 | InitiateDisconnect(service); |
| 729 | } |
| 730 | |
| 731 | TEST_F(WiFiMainTest, DisconnectCurrentServiceFailure) { |
| 732 | MockSupplicantInterfaceProxy &supplicant_interface_proxy = |
| 733 | *supplicant_interface_proxy_; |
| 734 | |
| 735 | StartWiFi(); |
| 736 | ReportBSS("bss0", "ssid0", "00:00:00:00:00:00", 0, kNetworkModeAdHoc); |
| 737 | |
| 738 | WiFiService *service(GetServices().begin()->get()); |
| 739 | DBus::Path fake_path("/fake/path"); |
| 740 | EXPECT_CALL(supplicant_interface_proxy, AddNetwork(_)) |
| 741 | .WillOnce(Return(fake_path)); |
| 742 | InitiateConnect(service); |
| 743 | ReportCurrentBSSChanged("bss0"); |
| 744 | ReportStateChanged(wpa_supplicant::kInterfaceStateCompleted); |
| 745 | |
| 746 | EXPECT_EQ(service, GetCurrentService()); |
| 747 | EXPECT_CALL(supplicant_interface_proxy, Disconnect()) |
| 748 | .WillRepeatedly(Throw( |
| 749 | DBus::Error( |
| 750 | "fi.w1.wpa_supplicant1.NotConnected", |
| 751 | "test threw fi.w1.wpa_supplicant1.NotConnected"))); |
| 752 | EXPECT_CALL(supplicant_interface_proxy, RemoveNetwork(fake_path)); |
| 753 | InitiateDisconnect(service); |
| 754 | |
| 755 | EXPECT_TRUE(GetCurrentService() == NULL); |
| 756 | } |
| 757 | |
mukesh agrawal | f2f68a5 | 2011-09-01 12:15:48 -0700 | [diff] [blame] | 758 | TEST_F(WiFiMainTest, LinkEvent) { |
| 759 | EXPECT_FALSE(IsLinkUp()); |
| 760 | EXPECT_CALL(dhcp_provider_, CreateConfig(_)). |
| 761 | WillOnce(Return(dhcp_config_)); |
| 762 | ReportLinkUp(); |
| 763 | } |
| 764 | |
mukesh agrawal | 5c4dd0b | 2011-09-14 13:53:14 -0700 | [diff] [blame] | 765 | TEST_F(WiFiMainTest, Stop) { |
| 766 | { |
| 767 | InSequence s; |
| 768 | |
| 769 | StartWiFi(); |
| 770 | ReportBSS("bss0", "ssid0", "00:00:00:00:00:00", 0, kNetworkModeAdHoc); |
| 771 | ReportScanDone(); |
| 772 | EXPECT_CALL(dhcp_provider_, CreateConfig(_)). |
| 773 | WillOnce(Return(dhcp_config_)); |
| 774 | ReportLinkUp(); |
| 775 | } |
| 776 | |
| 777 | { |
| 778 | EXPECT_CALL(*manager(), DeregisterService(_)); |
| 779 | StopWiFi(); |
| 780 | } |
| 781 | } |
| 782 | |
mukesh agrawal | 7a4e400 | 2011-09-06 11:26:05 -0700 | [diff] [blame] | 783 | TEST_F(WiFiMainTest, GetWifiServiceOpen) { |
| 784 | Error e; |
Paul Stewart | a41e38d | 2011-11-11 07:47:29 -0800 | [diff] [blame] | 785 | GetOpenService(flimflam::kTypeWifi, "an_ssid", flimflam::kModeManaged, &e); |
mukesh agrawal | 7a4e400 | 2011-09-06 11:26:05 -0700 | [diff] [blame] | 786 | EXPECT_TRUE(e.IsSuccess()); |
| 787 | } |
| 788 | |
| 789 | TEST_F(WiFiMainTest, GetWifiServiceOpenNoType) { |
| 790 | Error e; |
Paul Stewart | 6ab23a9 | 2011-11-09 17:17:47 -0800 | [diff] [blame] | 791 | GetOpenService(NULL, "an_ssid", flimflam::kModeManaged, &e); |
mukesh agrawal | 7a4e400 | 2011-09-06 11:26:05 -0700 | [diff] [blame] | 792 | EXPECT_EQ(Error::kInvalidArguments, e.type()); |
| 793 | EXPECT_EQ("must specify service type", e.message()); |
| 794 | } |
| 795 | |
| 796 | TEST_F(WiFiMainTest, GetWifiServiceOpenNoSSID) { |
| 797 | Error e; |
Paul Stewart | a41e38d | 2011-11-11 07:47:29 -0800 | [diff] [blame] | 798 | GetOpenService(flimflam::kTypeWifi, NULL, flimflam::kModeManaged, &e); |
mukesh agrawal | 7a4e400 | 2011-09-06 11:26:05 -0700 | [diff] [blame] | 799 | EXPECT_EQ(Error::kInvalidArguments, e.type()); |
| 800 | EXPECT_EQ("must specify SSID", e.message()); |
| 801 | } |
| 802 | |
| 803 | TEST_F(WiFiMainTest, GetWifiServiceOpenLongSSID) { |
| 804 | Error e; |
| 805 | GetOpenService( |
Paul Stewart | a41e38d | 2011-11-11 07:47:29 -0800 | [diff] [blame] | 806 | flimflam::kTypeWifi, "123456789012345678901234567890123", |
| 807 | flimflam::kModeManaged, &e); |
mukesh agrawal | 7a4e400 | 2011-09-06 11:26:05 -0700 | [diff] [blame] | 808 | EXPECT_EQ(Error::kInvalidNetworkName, e.type()); |
| 809 | EXPECT_EQ("SSID is too long", e.message()); |
| 810 | } |
| 811 | |
| 812 | TEST_F(WiFiMainTest, GetWifiServiceOpenShortSSID) { |
| 813 | Error e; |
Paul Stewart | a41e38d | 2011-11-11 07:47:29 -0800 | [diff] [blame] | 814 | GetOpenService(flimflam::kTypeWifi, "", flimflam::kModeManaged, &e); |
mukesh agrawal | 7a4e400 | 2011-09-06 11:26:05 -0700 | [diff] [blame] | 815 | EXPECT_EQ(Error::kInvalidNetworkName, e.type()); |
| 816 | EXPECT_EQ("SSID is too short", e.message()); |
| 817 | } |
| 818 | |
| 819 | TEST_F(WiFiMainTest, GetWifiServiceOpenBadMode) { |
| 820 | Error e; |
Paul Stewart | a41e38d | 2011-11-11 07:47:29 -0800 | [diff] [blame] | 821 | GetOpenService(flimflam::kTypeWifi, "an_ssid", "ad-hoc", &e); |
mukesh agrawal | 7a4e400 | 2011-09-06 11:26:05 -0700 | [diff] [blame] | 822 | EXPECT_EQ(Error::kNotSupported, e.type()); |
| 823 | EXPECT_EQ("service mode is unsupported", e.message()); |
| 824 | } |
| 825 | |
| 826 | TEST_F(WiFiMainTest, GetWifiServiceOpenNoMode) { |
| 827 | Error e; |
Paul Stewart | a41e38d | 2011-11-11 07:47:29 -0800 | [diff] [blame] | 828 | GetOpenService(flimflam::kTypeWifi, "an_ssid", NULL, &e); |
mukesh agrawal | 7a4e400 | 2011-09-06 11:26:05 -0700 | [diff] [blame] | 829 | EXPECT_TRUE(e.IsSuccess()); |
| 830 | } |
| 831 | |
| 832 | TEST_F(WiFiMainTest, GetWifiServiceRSN) { |
| 833 | Error e; |
Paul Stewart | a41e38d | 2011-11-11 07:47:29 -0800 | [diff] [blame] | 834 | GetService(flimflam::kTypeWifi, "an_ssid", flimflam::kModeManaged, |
| 835 | flimflam::kSecurityRsn, "secure password", &e); |
mukesh agrawal | 7a4e400 | 2011-09-06 11:26:05 -0700 | [diff] [blame] | 836 | EXPECT_TRUE(e.IsSuccess()); |
| 837 | } |
| 838 | |
| 839 | TEST_F(WiFiMainTest, GetWifiServiceRSNNoPassword) { |
| 840 | Error e; |
Paul Stewart | a41e38d | 2011-11-11 07:47:29 -0800 | [diff] [blame] | 841 | GetService(flimflam::kTypeWifi, "an_ssid", flimflam::kModeManaged, |
| 842 | flimflam::kSecurityRsn, NULL, &e); |
mukesh agrawal | 7a4e400 | 2011-09-06 11:26:05 -0700 | [diff] [blame] | 843 | EXPECT_EQ(Error::kInvalidArguments, e.type()); |
| 844 | EXPECT_EQ("must specify passphrase", e.message()); |
| 845 | } |
| 846 | |
| 847 | TEST_F(WiFiMainTest, GetWifiServiceBadSecurity) { |
| 848 | Error e; |
Paul Stewart | a41e38d | 2011-11-11 07:47:29 -0800 | [diff] [blame] | 849 | GetService(flimflam::kTypeWifi, "an_ssid", flimflam::kModeManaged, "rot-13", |
| 850 | NULL, &e); |
mukesh agrawal | 7a4e400 | 2011-09-06 11:26:05 -0700 | [diff] [blame] | 851 | EXPECT_EQ(Error::kNotSupported, e.type()); |
| 852 | EXPECT_EQ("security mode is unsupported", e.message()); |
| 853 | } |
| 854 | |
| 855 | TEST_F(WiFiMainTest, GetWifiServiceWEPNoPassword) { |
| 856 | Error e; |
Paul Stewart | a41e38d | 2011-11-11 07:47:29 -0800 | [diff] [blame] | 857 | GetService(flimflam::kTypeWifi, "an_ssid", flimflam::kModeManaged, |
| 858 | flimflam::kSecurityWep, NULL, &e); |
mukesh agrawal | 7a4e400 | 2011-09-06 11:26:05 -0700 | [diff] [blame] | 859 | EXPECT_EQ(Error::kInvalidArguments, e.type()); |
| 860 | EXPECT_EQ("must specify passphrase", e.message()); |
| 861 | } |
| 862 | |
| 863 | TEST_F(WiFiMainTest, GetWifiServiceWEPEmptyPassword) { |
| 864 | Error e; |
Paul Stewart | a41e38d | 2011-11-11 07:47:29 -0800 | [diff] [blame] | 865 | GetService(flimflam::kTypeWifi, "an_ssid", flimflam::kModeManaged, |
| 866 | flimflam::kSecurityWep, "", &e); |
mukesh agrawal | 7a4e400 | 2011-09-06 11:26:05 -0700 | [diff] [blame] | 867 | EXPECT_EQ(Error::kInvalidPassphrase, e.type()); |
| 868 | } |
| 869 | |
| 870 | TEST_F(WiFiMainTest, GetWifiServiceWEP40ASCII) { |
| 871 | Error e; |
Paul Stewart | a41e38d | 2011-11-11 07:47:29 -0800 | [diff] [blame] | 872 | GetService(flimflam::kTypeWifi, "an_ssid", flimflam::kModeManaged, |
| 873 | flimflam::kSecurityWep, "abcde", &e); |
mukesh agrawal | 7a4e400 | 2011-09-06 11:26:05 -0700 | [diff] [blame] | 874 | EXPECT_TRUE(e.IsSuccess()); |
| 875 | } |
| 876 | |
| 877 | TEST_F(WiFiMainTest, GetWifiServiceWEP104ASCII) { |
| 878 | Error e; |
Paul Stewart | a41e38d | 2011-11-11 07:47:29 -0800 | [diff] [blame] | 879 | GetService(flimflam::kTypeWifi, "an_ssid", flimflam::kModeManaged, |
| 880 | flimflam::kSecurityWep, "abcdefghijklm", &e); |
mukesh agrawal | 7a4e400 | 2011-09-06 11:26:05 -0700 | [diff] [blame] | 881 | EXPECT_TRUE(e.IsSuccess()); |
| 882 | } |
| 883 | |
| 884 | TEST_F(WiFiMainTest, GetWifiServiceWEP40ASCIIWithKeyIndex) { |
| 885 | Error e; |
Paul Stewart | a41e38d | 2011-11-11 07:47:29 -0800 | [diff] [blame] | 886 | GetService(flimflam::kTypeWifi, "an_ssid", flimflam::kModeManaged, |
| 887 | flimflam::kSecurityWep, "0:abcdefghijklm", &e); |
mukesh agrawal | 7a4e400 | 2011-09-06 11:26:05 -0700 | [diff] [blame] | 888 | EXPECT_TRUE(e.IsSuccess()); |
| 889 | } |
| 890 | |
| 891 | TEST_F(WiFiMainTest, GetWifiServiceWEP40Hex) { |
| 892 | Error e; |
Paul Stewart | a41e38d | 2011-11-11 07:47:29 -0800 | [diff] [blame] | 893 | GetService(flimflam::kTypeWifi, "an_ssid", flimflam::kModeManaged, |
| 894 | flimflam::kSecurityWep, "0102030405", &e); |
mukesh agrawal | 7a4e400 | 2011-09-06 11:26:05 -0700 | [diff] [blame] | 895 | EXPECT_TRUE(e.IsSuccess()); |
| 896 | } |
| 897 | |
| 898 | TEST_F(WiFiMainTest, GetWifiServiceWEP40HexBadPassphrase) { |
| 899 | Error e; |
Paul Stewart | a41e38d | 2011-11-11 07:47:29 -0800 | [diff] [blame] | 900 | GetService(flimflam::kTypeWifi, "an_ssid", flimflam::kModeManaged, |
| 901 | flimflam::kSecurityWep, "O102030405", &e); |
mukesh agrawal | 7a4e400 | 2011-09-06 11:26:05 -0700 | [diff] [blame] | 902 | EXPECT_EQ(Error::kInvalidPassphrase, e.type()); |
| 903 | } |
| 904 | |
| 905 | TEST_F(WiFiMainTest, GetWifiServiceWEP40HexWithKeyIndexBadPassphrase) { |
| 906 | Error e; |
Paul Stewart | a41e38d | 2011-11-11 07:47:29 -0800 | [diff] [blame] | 907 | GetService(flimflam::kTypeWifi, "an_ssid", flimflam::kModeManaged, |
| 908 | flimflam::kSecurityWep, "1:O102030405", &e); |
mukesh agrawal | 7a4e400 | 2011-09-06 11:26:05 -0700 | [diff] [blame] | 909 | EXPECT_EQ(Error::kInvalidPassphrase, e.type()); |
| 910 | } |
| 911 | |
| 912 | TEST_F(WiFiMainTest, GetWifiServiceWEP40HexWithKeyIndexAndBaseBadPassphrase) { |
| 913 | Error e; |
Paul Stewart | a41e38d | 2011-11-11 07:47:29 -0800 | [diff] [blame] | 914 | GetService(flimflam::kTypeWifi, "an_ssid", flimflam::kModeManaged, |
| 915 | flimflam::kSecurityWep, "1:0xO102030405", &e); |
mukesh agrawal | 7a4e400 | 2011-09-06 11:26:05 -0700 | [diff] [blame] | 916 | EXPECT_EQ(Error::kInvalidPassphrase, e.type()); |
| 917 | } |
| 918 | |
| 919 | TEST_F(WiFiMainTest, GetWifiServiceWEP40HexWithBaseBadPassphrase) { |
| 920 | Error e; |
Paul Stewart | a41e38d | 2011-11-11 07:47:29 -0800 | [diff] [blame] | 921 | GetService(flimflam::kTypeWifi, "an_ssid", flimflam::kModeManaged, |
| 922 | flimflam::kSecurityWep, "0xO102030405", &e); |
mukesh agrawal | 7a4e400 | 2011-09-06 11:26:05 -0700 | [diff] [blame] | 923 | EXPECT_EQ(Error::kInvalidPassphrase, e.type()); |
| 924 | } |
| 925 | |
| 926 | TEST_F(WiFiMainTest, GetWifiServiceWEP104Hex) { |
| 927 | Error e; |
Paul Stewart | a41e38d | 2011-11-11 07:47:29 -0800 | [diff] [blame] | 928 | GetService(flimflam::kTypeWifi, "an_ssid", flimflam::kModeManaged, |
| 929 | flimflam::kSecurityWep, "0102030405060708090a0b0c0d", &e); |
mukesh agrawal | 7a4e400 | 2011-09-06 11:26:05 -0700 | [diff] [blame] | 930 | EXPECT_TRUE(e.IsSuccess()); |
| 931 | } |
| 932 | |
| 933 | TEST_F(WiFiMainTest, GetWifiServiceWEP104HexUppercase) { |
| 934 | Error e; |
Paul Stewart | a41e38d | 2011-11-11 07:47:29 -0800 | [diff] [blame] | 935 | GetService(flimflam::kTypeWifi, "an_ssid", flimflam::kModeManaged, |
| 936 | flimflam::kSecurityWep, "0102030405060708090A0B0C0D", &e); |
mukesh agrawal | 7a4e400 | 2011-09-06 11:26:05 -0700 | [diff] [blame] | 937 | EXPECT_TRUE(e.IsSuccess()); |
| 938 | } |
| 939 | |
| 940 | TEST_F(WiFiMainTest, GetWifiServiceWEP104HexWithKeyIndex) { |
| 941 | Error e; |
Paul Stewart | a41e38d | 2011-11-11 07:47:29 -0800 | [diff] [blame] | 942 | GetService(flimflam::kTypeWifi, "an_ssid", flimflam::kModeManaged, |
| 943 | flimflam::kSecurityWep, "0:0102030405060708090a0b0c0d", &e); |
mukesh agrawal | 7a4e400 | 2011-09-06 11:26:05 -0700 | [diff] [blame] | 944 | EXPECT_TRUE(e.IsSuccess()); |
| 945 | } |
| 946 | |
| 947 | TEST_F(WiFiMainTest, GetWifiServiceWEP104HexWithKeyIndexAndBase) { |
| 948 | Error e; |
Paul Stewart | a41e38d | 2011-11-11 07:47:29 -0800 | [diff] [blame] | 949 | GetService(flimflam::kTypeWifi, "an_ssid", flimflam::kModeManaged, |
| 950 | flimflam::kSecurityWep, "0:0x0102030405060708090a0b0c0d", &e); |
mukesh agrawal | 7a4e400 | 2011-09-06 11:26:05 -0700 | [diff] [blame] | 951 | EXPECT_TRUE(e.IsSuccess()); |
| 952 | } |
| 953 | |
mukesh agrawal | 8ede052 | 2011-10-03 14:57:44 -0700 | [diff] [blame] | 954 | class WiFiGetServiceSuccessTest : public WiFiMainTest {}; |
| 955 | class WiFiGetServiceFailureTest : public WiFiMainTest {}; |
| 956 | |
| 957 | TEST_P(WiFiGetServiceSuccessTest, Passphrase) { |
| 958 | Error e; |
Paul Stewart | a41e38d | 2011-11-11 07:47:29 -0800 | [diff] [blame] | 959 | GetService(flimflam::kTypeWifi, "an_ssid", flimflam::kModeManaged, |
| 960 | flimflam::kSecurityWpa, GetParam().c_str(), &e); |
mukesh agrawal | 8ede052 | 2011-10-03 14:57:44 -0700 | [diff] [blame] | 961 | EXPECT_TRUE(e.IsSuccess()); |
| 962 | } |
| 963 | |
| 964 | TEST_P(WiFiGetServiceFailureTest, Passphrase) { |
| 965 | Error e; |
Paul Stewart | a41e38d | 2011-11-11 07:47:29 -0800 | [diff] [blame] | 966 | GetService(flimflam::kTypeWifi, "an_ssid", flimflam::kModeManaged, |
| 967 | flimflam::kSecurityWpa, GetParam().c_str(), &e); |
mukesh agrawal | 8ede052 | 2011-10-03 14:57:44 -0700 | [diff] [blame] | 968 | EXPECT_EQ(Error::kInvalidPassphrase, e.type()); |
| 969 | } |
| 970 | |
| 971 | INSTANTIATE_TEST_CASE_P( |
| 972 | WiFiGetServiceSuccessTestInstance, |
| 973 | WiFiGetServiceSuccessTest, |
| 974 | Values( |
| 975 | string(IEEE_80211::kWPAAsciiMinLen, 'Z'), |
| 976 | string(IEEE_80211::kWPAAsciiMaxLen, 'Z'), |
| 977 | // subtle: invalid length for hex key, but valid as ascii passphrase |
| 978 | string(IEEE_80211::kWPAHexLen-1, '1'), |
| 979 | string(IEEE_80211::kWPAHexLen, '1'))); |
| 980 | |
| 981 | INSTANTIATE_TEST_CASE_P( |
| 982 | WiFiGetServiceFailureTestInstance, |
| 983 | WiFiGetServiceFailureTest, |
| 984 | Values( |
| 985 | string(IEEE_80211::kWPAAsciiMinLen-1, 'Z'), |
| 986 | string(IEEE_80211::kWPAAsciiMaxLen+1, 'Z'), |
| 987 | string(IEEE_80211::kWPAHexLen+1, '1'))); |
| 988 | |
Paul Stewart | 6ab23a9 | 2011-11-09 17:17:47 -0800 | [diff] [blame] | 989 | TEST_F(WiFiMainTest, FindServiceWEP) { |
| 990 | const string ssid("an_ssid"); |
| 991 | { |
| 992 | Error e; |
Paul Stewart | a41e38d | 2011-11-11 07:47:29 -0800 | [diff] [blame] | 993 | GetService(flimflam::kTypeWifi, ssid.c_str(), flimflam::kModeManaged, |
Paul Stewart | 6ab23a9 | 2011-11-09 17:17:47 -0800 | [diff] [blame] | 994 | flimflam::kSecurityWep, "abcde", &e); |
| 995 | EXPECT_TRUE(e.IsSuccess()); |
| 996 | } |
| 997 | vector<uint8_t> ssid_bytes(ssid.begin(), ssid.end()); |
| 998 | |
Paul Stewart | a41e38d | 2011-11-11 07:47:29 -0800 | [diff] [blame] | 999 | EXPECT_TRUE(FindService(ssid_bytes, flimflam::kModeManaged, |
| 1000 | flimflam::kSecurityWep).get()); |
| 1001 | EXPECT_FALSE(FindService(ssid_bytes, flimflam::kModeManaged, |
| 1002 | flimflam::kSecurityWpa).get()); |
Paul Stewart | 6ab23a9 | 2011-11-09 17:17:47 -0800 | [diff] [blame] | 1003 | } |
| 1004 | |
| 1005 | TEST_F(WiFiMainTest, FindServiceWPA) { |
| 1006 | const string ssid("an_ssid"); |
| 1007 | { |
| 1008 | Error e; |
Paul Stewart | a41e38d | 2011-11-11 07:47:29 -0800 | [diff] [blame] | 1009 | GetService(flimflam::kTypeWifi, ssid.c_str(), flimflam::kModeManaged, |
Paul Stewart | 6ab23a9 | 2011-11-09 17:17:47 -0800 | [diff] [blame] | 1010 | flimflam::kSecurityRsn, "abcdefgh", &e); |
| 1011 | EXPECT_TRUE(e.IsSuccess()); |
| 1012 | } |
| 1013 | vector<uint8_t> ssid_bytes(ssid.begin(), ssid.end()); |
Paul Stewart | a41e38d | 2011-11-11 07:47:29 -0800 | [diff] [blame] | 1014 | WiFiServiceRefPtr wpa_service(FindService(ssid_bytes, flimflam::kModeManaged, |
| 1015 | flimflam::kSecurityWpa)); |
Paul Stewart | 6ab23a9 | 2011-11-09 17:17:47 -0800 | [diff] [blame] | 1016 | EXPECT_TRUE(wpa_service.get()); |
Paul Stewart | a41e38d | 2011-11-11 07:47:29 -0800 | [diff] [blame] | 1017 | WiFiServiceRefPtr rsn_service(FindService(ssid_bytes, flimflam::kModeManaged, |
| 1018 | flimflam::kSecurityRsn)); |
Paul Stewart | 6ab23a9 | 2011-11-09 17:17:47 -0800 | [diff] [blame] | 1019 | EXPECT_TRUE(rsn_service.get()); |
| 1020 | EXPECT_EQ(wpa_service.get(), rsn_service.get()); |
Paul Stewart | a41e38d | 2011-11-11 07:47:29 -0800 | [diff] [blame] | 1021 | WiFiServiceRefPtr psk_service(FindService(ssid_bytes, flimflam::kModeManaged, |
| 1022 | flimflam::kSecurityPsk)); |
Paul Stewart | 6ab23a9 | 2011-11-09 17:17:47 -0800 | [diff] [blame] | 1023 | EXPECT_EQ(wpa_service.get(), psk_service.get()); |
| 1024 | // Indirectly test FindService by doing a GetService on something that |
| 1025 | // already exists. |
| 1026 | { |
| 1027 | Error e; |
| 1028 | WiFiServiceRefPtr wpa_service2( |
Paul Stewart | a41e38d | 2011-11-11 07:47:29 -0800 | [diff] [blame] | 1029 | GetServiceInner(flimflam::kTypeWifi, ssid.c_str(), |
| 1030 | flimflam::kModeManaged, flimflam::kSecurityWpa, |
| 1031 | "abcdefgh", false, &e)); |
Paul Stewart | 6ab23a9 | 2011-11-09 17:17:47 -0800 | [diff] [blame] | 1032 | EXPECT_TRUE(e.IsSuccess()); |
| 1033 | EXPECT_EQ(wpa_service.get(), wpa_service2.get()); |
| 1034 | } |
| 1035 | } |
| 1036 | |
Paul Stewart | ced6a0b | 2011-11-08 15:32:04 -0800 | [diff] [blame] | 1037 | MATCHER_P(HasHiddenSSID, ssid, "") { |
Paul Stewart | a41e38d | 2011-11-11 07:47:29 -0800 | [diff] [blame] | 1038 | map<string, DBus::Variant>::const_iterator it = |
Paul Stewart | ced6a0b | 2011-11-08 15:32:04 -0800 | [diff] [blame] | 1039 | arg.find(wpa_supplicant::kPropertyScanSSIDs); |
| 1040 | if (it == arg.end()) { |
| 1041 | return false; |
| 1042 | } |
| 1043 | |
| 1044 | const DBus::Variant &ssids_variant = it->second; |
| 1045 | EXPECT_TRUE(DBusAdaptor::IsByteArrays(ssids_variant.signature())); |
| 1046 | const ByteArrays &ssids = it->second.operator ByteArrays(); |
| 1047 | // A valid Scan containing a single hidden SSID should contain |
| 1048 | // two SSID entries: one containing the SSID we are looking for, |
| 1049 | // and an empty entry, signifying that we also want to do a |
| 1050 | // broadcast probe request for all non-hidden APs as well. |
| 1051 | return ssids.size() == 2 && |
| 1052 | string(ssids[0].begin(), ssids[0].end()) == ssid && |
| 1053 | ssids[1].empty(); |
| 1054 | } |
| 1055 | |
| 1056 | TEST_F(WiFiMainTest, ScanHidden) { |
| 1057 | EXPECT_CALL(*supplicant_process_proxy_, CreateInterface(_)); |
| 1058 | EXPECT_CALL(*supplicant_process_proxy_, GetInterface(_)) |
| 1059 | .Times(AnyNumber()) |
| 1060 | .WillRepeatedly(Throw( |
| 1061 | DBus::Error( |
| 1062 | "fi.w1.wpa_supplicant1.InterfaceUnknown", |
| 1063 | "test threw fi.w1.wpa_supplicant1.InterfaceUnknown"))); |
Paul Stewart | a41e38d | 2011-11-11 07:47:29 -0800 | [diff] [blame] | 1064 | { |
| 1065 | // Create a hidden, favorite service. |
| 1066 | Error e; |
| 1067 | WiFiServiceRefPtr service = |
| 1068 | GetServiceInner(flimflam::kTypeWifi, "ssid0", flimflam::kModeManaged, |
| 1069 | NULL, NULL, true, &e); |
| 1070 | EXPECT_TRUE(e.IsSuccess()); |
| 1071 | EXPECT_TRUE(service->hidden_ssid()); |
mukesh agrawal | 00917ce | 2011-11-22 23:56:55 +0000 | [diff] [blame] | 1072 | service->MakeFavorite(); |
Paul Stewart | a41e38d | 2011-11-11 07:47:29 -0800 | [diff] [blame] | 1073 | } |
| 1074 | { |
| 1075 | // Create a hidden, non-favorite service. |
| 1076 | Error e; |
| 1077 | WiFiServiceRefPtr service = |
| 1078 | GetServiceInner(flimflam::kTypeWifi, "ssid1", flimflam::kModeManaged, |
| 1079 | NULL, NULL, true, &e); |
| 1080 | EXPECT_TRUE(e.IsSuccess()); |
| 1081 | EXPECT_TRUE(service->hidden_ssid()); |
| 1082 | } |
| 1083 | { |
| 1084 | // Create a non-hidden, favorite service. |
| 1085 | Error e; |
| 1086 | WiFiServiceRefPtr service = |
| 1087 | GetServiceInner(flimflam::kTypeWifi, "ssid2", flimflam::kModeManaged, |
| 1088 | NULL, NULL, false, &e); |
| 1089 | EXPECT_TRUE(e.IsSuccess()); |
| 1090 | EXPECT_FALSE(service->hidden_ssid()); |
mukesh agrawal | 00917ce | 2011-11-22 23:56:55 +0000 | [diff] [blame] | 1091 | service->MakeFavorite(); |
Paul Stewart | a41e38d | 2011-11-11 07:47:29 -0800 | [diff] [blame] | 1092 | } |
| 1093 | EXPECT_CALL(*supplicant_interface_proxy_, Scan(HasHiddenSSID("ssid0"))); |
Paul Stewart | ced6a0b | 2011-11-08 15:32:04 -0800 | [diff] [blame] | 1094 | StartWiFi(); |
| 1095 | dispatcher_.DispatchPendingEvents(); |
| 1096 | } |
| 1097 | |
mukesh agrawal | 7ec7131 | 2011-11-10 02:08:26 +0000 | [diff] [blame] | 1098 | TEST_F(WiFiMainTest, InitialSupplicantState) { |
| 1099 | EXPECT_EQ(WiFi::kInterfaceStateUnknown, GetSupplicantState()); |
| 1100 | } |
| 1101 | |
| 1102 | TEST_F(WiFiMainTest, StateChangeNoService) { |
| 1103 | // State change should succeed even if there is no pending Service. |
| 1104 | ReportStateChanged(wpa_supplicant::kInterfaceStateScanning); |
| 1105 | EXPECT_EQ(wpa_supplicant::kInterfaceStateScanning, GetSupplicantState()); |
| 1106 | } |
| 1107 | |
| 1108 | TEST_F(WiFiMainTest, StateChangeWithService) { |
| 1109 | // Forward transition should trigger a Service state change. |
| 1110 | StartWiFi(); |
| 1111 | dispatcher_.DispatchPendingEvents(); |
| 1112 | MockWiFiServiceRefPtr service = MakeMockService(); |
| 1113 | InitiateConnect(service); |
| 1114 | EXPECT_CALL(*service.get(), SetState(Service::kStateAssociating)); |
| 1115 | ReportStateChanged(wpa_supplicant::kInterfaceStateAssociated); |
| 1116 | // Verify expectations now, because WiFi may report other state changes |
| 1117 | // when WiFi is Stop()-ed (during TearDown()). |
| 1118 | Mock::VerifyAndClearExpectations(service.get()); |
| 1119 | } |
| 1120 | |
| 1121 | TEST_F(WiFiMainTest, StateChangeBackwardsWithService) { |
| 1122 | // Some backwards transitions should not trigger a Service state change. |
| 1123 | // Supplicant state should still be updated, however. |
| 1124 | StartWiFi(); |
| 1125 | dispatcher_.DispatchPendingEvents(); |
| 1126 | MockWiFiServiceRefPtr service = MakeMockService(); |
| 1127 | InitiateConnect(service); |
| 1128 | ReportStateChanged(wpa_supplicant::kInterfaceStateCompleted); |
| 1129 | EXPECT_CALL(*service.get(), SetState(_)).Times(0); |
| 1130 | ReportStateChanged(wpa_supplicant::kInterfaceStateAuthenticating); |
| 1131 | EXPECT_EQ(wpa_supplicant::kInterfaceStateAuthenticating, |
| 1132 | GetSupplicantState()); |
| 1133 | // Verify expectations now, because WiFi may report other state changes |
| 1134 | // when WiFi is Stop()-ed (during TearDown()). |
| 1135 | Mock::VerifyAndClearExpectations(service.get()); |
| 1136 | } |
| 1137 | |
Paul Stewart | a41e38d | 2011-11-11 07:47:29 -0800 | [diff] [blame] | 1138 | TEST_F(WiFiMainTest, LoadHiddenServicesFailWithNoGroups) { |
| 1139 | StrictMock<MockStore> storage; |
| 1140 | EXPECT_CALL(storage, GetGroupsWithKey(flimflam::kWifiHiddenSsid)) |
| 1141 | .WillOnce(Return(set<string>())); |
| 1142 | EXPECT_FALSE(LoadHiddenServices(&storage)); |
| 1143 | } |
| 1144 | |
| 1145 | TEST_F(WiFiMainTest, LoadHiddenServicesFailWithMissingHidden) { |
| 1146 | string id; |
| 1147 | StrictMock<MockStore> storage; |
| 1148 | SetupHiddenStorage(&storage, "an_ssid", &id); |
| 1149 | // Missing "Hidden" property. |
| 1150 | EXPECT_CALL(storage, GetBool(StrEq(id), flimflam::kWifiHiddenSsid, _)) |
| 1151 | .WillOnce(Return(false)); |
| 1152 | EXPECT_FALSE(LoadHiddenServices(&storage)); |
| 1153 | } |
| 1154 | |
| 1155 | TEST_F(WiFiMainTest, LoadHiddenServicesFailWithFalseHidden) { |
| 1156 | string id; |
| 1157 | StrictMock<MockStore> storage; |
| 1158 | SetupHiddenStorage(&storage, "an_ssid", &id); |
| 1159 | // "Hidden" property set to "false". |
| 1160 | EXPECT_CALL(storage, GetBool(StrEq(id), flimflam::kWifiHiddenSsid, _)) |
| 1161 | .WillOnce(DoAll(SetArgumentPointee<2>(true), Return(false))); |
| 1162 | EXPECT_FALSE(LoadHiddenServices(&storage)); |
| 1163 | } |
| 1164 | |
| 1165 | TEST_F(WiFiMainTest, LoadHiddenServicesFailWithMissingSSID) { |
| 1166 | string id; |
| 1167 | StrictMock<MockStore> storage; |
| 1168 | SetupHiddenStorage(&storage, "an_ssid", &id); |
| 1169 | // Missing "SSID" property. |
| 1170 | EXPECT_CALL(storage, GetString(StrEq(id), flimflam::kSSIDProperty, _)) |
| 1171 | .WillOnce(Return(false)); |
| 1172 | EXPECT_FALSE(LoadHiddenServices(&storage)); |
| 1173 | } |
| 1174 | |
| 1175 | |
| 1176 | TEST_F(WiFiMainTest, LoadHiddenServicesFailWithFoundService) { |
| 1177 | StrictMock<MockStore> storage; |
| 1178 | string id; |
| 1179 | SetupHiddenStorage(&storage, "an_ssid", &id); |
| 1180 | Error e; |
| 1181 | GetOpenService(flimflam::kTypeWifi, "an_ssid", NULL, &e); |
| 1182 | ASSERT_TRUE(e.IsSuccess()); |
| 1183 | EXPECT_FALSE(LoadHiddenServices(&storage)); |
| 1184 | } |
| 1185 | |
| 1186 | TEST_F(WiFiMainTest, LoadHiddenServicesSuccess) { |
| 1187 | StrictMock<MockStore> storage; |
| 1188 | string ssid("an_ssid"); |
| 1189 | string id; |
| 1190 | SetupHiddenStorage(&storage, ssid, &id); |
| 1191 | EXPECT_TRUE(LoadHiddenServices(&storage)); |
| 1192 | vector<uint8_t> ssid_bytes(ssid.begin(), ssid.end()); |
| 1193 | EXPECT_TRUE(FindService(ssid_bytes, flimflam::kModeManaged, |
| 1194 | flimflam::kSecurityNone).get()); |
| 1195 | } |
| 1196 | |
mukesh agrawal | 1590839 | 2011-11-16 18:29:25 +0000 | [diff] [blame] | 1197 | TEST_F(WiFiMainTest, CurrentBSSChangeConnectedToDisconnected) { |
| 1198 | WiFiEndpointRefPtr ap = MakeEndpoint("an_ssid", "00:01:02:03:04:05"); |
| 1199 | WiFiServiceRefPtr service = CreateServiceForEndpoint(*ap); |
| 1200 | |
| 1201 | // Note that the BSS handle used in this test ("an_ap") is not |
| 1202 | // intended to reflect the format used by supplicant. It's just |
| 1203 | // convenient for testing. |
| 1204 | |
| 1205 | StartWiFi(); |
| 1206 | ReportBSS("an_ap", ap->ssid_string(), ap->bssid_string(), 0, |
| 1207 | kNetworkModeInfrastructure); |
| 1208 | InitiateConnect(service); |
| 1209 | EXPECT_EQ(service, GetPendingService().get()); |
| 1210 | |
| 1211 | ReportCurrentBSSChanged("an_ap"); |
| 1212 | ReportStateChanged(wpa_supplicant::kInterfaceStateCompleted); |
| 1213 | EXPECT_EQ(Service::kStateConfiguring, service->state()); |
| 1214 | EXPECT_EQ(service, GetCurrentService().get()); |
| 1215 | EXPECT_EQ(NULL, GetPendingService().get()); |
| 1216 | |
| 1217 | ReportCurrentBSSChanged(wpa_supplicant::kCurrentBSSNull); |
mukesh agrawal | 8a3188d | 2011-12-01 20:56:44 +0000 | [diff] [blame] | 1218 | EXPECT_EQ(Service::kStateFailure, service->state()); |
mukesh agrawal | 1590839 | 2011-11-16 18:29:25 +0000 | [diff] [blame] | 1219 | EXPECT_EQ(NULL, GetCurrentService().get()); |
| 1220 | EXPECT_EQ(NULL, GetPendingService().get()); |
| 1221 | } |
| 1222 | |
| 1223 | TEST_F(WiFiMainTest, CurrentBSSChangeConnectedToConnectedNewService) { |
| 1224 | WiFiEndpointRefPtr ap1 = MakeEndpoint("an_ssid", "00:01:02:03:04:05"); |
| 1225 | WiFiEndpointRefPtr ap2 = MakeEndpoint("another_ssid", "01:02:03:04:05:06"); |
| 1226 | WiFiServiceRefPtr service1 = CreateServiceForEndpoint(*ap1); |
| 1227 | WiFiServiceRefPtr service2 = CreateServiceForEndpoint(*ap2); |
| 1228 | |
| 1229 | // Note that the BSS handles used in this test ("ap1", "ap2") are |
| 1230 | // not intended to reflect the format used by supplicant. They're |
| 1231 | // just convenient for testing. |
| 1232 | |
| 1233 | StartWiFi(); |
| 1234 | ReportBSS("ap1", ap1->ssid_string(), ap1->bssid_string(), 0, |
| 1235 | kNetworkModeInfrastructure); |
| 1236 | ReportBSS("ap2", ap2->ssid_string(), ap2->bssid_string(), 0, |
| 1237 | kNetworkModeInfrastructure); |
| 1238 | InitiateConnect(service1); |
| 1239 | ReportCurrentBSSChanged("ap1"); |
| 1240 | ReportStateChanged(wpa_supplicant::kInterfaceStateCompleted); |
| 1241 | EXPECT_EQ(service1.get(), GetCurrentService().get()); |
| 1242 | |
| 1243 | ReportCurrentBSSChanged("ap2"); |
| 1244 | ReportStateChanged(wpa_supplicant::kInterfaceStateCompleted); |
| 1245 | EXPECT_EQ(service2.get(), GetCurrentService().get()); |
| 1246 | EXPECT_EQ(Service::kStateIdle, service1->state()); |
| 1247 | EXPECT_EQ(Service::kStateConfiguring, service2->state()); |
| 1248 | } |
| 1249 | |
| 1250 | TEST_F(WiFiMainTest, CurrentBSSChangeDisconnectedToConnected) { |
| 1251 | WiFiEndpointRefPtr ap = MakeEndpoint("an_ssid", "00:01:02:03:04:05"); |
| 1252 | WiFiServiceRefPtr service = CreateServiceForEndpoint(*ap); |
| 1253 | |
| 1254 | // Note that the BSS handle used in this test ("an_ap") is not |
| 1255 | // intended to reflect the format used by supplicant. It's just |
| 1256 | // convenient for testing. |
| 1257 | |
| 1258 | StartWiFi(); |
| 1259 | ReportBSS("an_ap", ap->ssid_string(), ap->bssid_string(), 0, |
| 1260 | kNetworkModeInfrastructure); |
| 1261 | InitiateConnect(service); |
| 1262 | ReportCurrentBSSChanged("an_ap"); |
| 1263 | ReportStateChanged(wpa_supplicant::kInterfaceStateCompleted); |
| 1264 | EXPECT_EQ(service.get(), GetCurrentService().get()); |
| 1265 | EXPECT_EQ(Service::kStateConfiguring, service->state()); |
| 1266 | } |
| 1267 | |
mukesh agrawal | 4eb4d78 | 2011-12-05 17:34:37 +0000 | [diff] [blame] | 1268 | TEST_F(WiFiMainTest, ConfiguredServiceRegistration) { |
| 1269 | Error e; |
| 1270 | EXPECT_CALL(*manager(), RegisterService(_)) |
| 1271 | .Times(0); |
| 1272 | EXPECT_CALL(*manager(), HasService(_)) |
| 1273 | .WillOnce(Return(false)); |
| 1274 | GetOpenService(flimflam::kTypeWifi, "an_ssid", flimflam::kModeManaged, &e); |
| 1275 | EXPECT_CALL(*manager(), RegisterService(_)); |
| 1276 | ReportBSS("ap0", "an_ssid", "00:00:00:00:00:00", 0, |
| 1277 | kNetworkModeInfrastructure); |
| 1278 | } |
| 1279 | |
mukesh agrawal | 8a3188d | 2011-12-01 20:56:44 +0000 | [diff] [blame] | 1280 | TEST_F(WiFiMainTest, NewConnectPreemptsPending) { |
| 1281 | WiFiEndpointRefPtr ap1 = MakeEndpoint("an_ssid", "00:01:02:03:04:05"); |
| 1282 | WiFiEndpointRefPtr ap2 = MakeEndpoint("another_ssid", "01:02:03:04:05:06"); |
| 1283 | WiFiServiceRefPtr service1 = CreateServiceForEndpoint(*ap1); |
| 1284 | WiFiServiceRefPtr service2 = CreateServiceForEndpoint(*ap2); |
| 1285 | |
| 1286 | StartWiFi(); |
| 1287 | ReportBSS("ap1", ap1->ssid_string(), ap1->bssid_string(), 0, |
| 1288 | kNetworkModeInfrastructure); |
| 1289 | ReportBSS("ap2", ap2->ssid_string(), ap2->bssid_string(), 0, |
| 1290 | kNetworkModeInfrastructure); |
| 1291 | InitiateConnect(service1); |
| 1292 | EXPECT_EQ(service1.get(), GetPendingService().get()); |
| 1293 | |
| 1294 | EXPECT_CALL(*GetSupplicantInterfaceProxy(), Disconnect()); |
| 1295 | EXPECT_CALL(*GetSupplicantInterfaceProxy(), AddNetwork(_)); |
| 1296 | InitiateConnect(service2); |
| 1297 | EXPECT_EQ(service2.get(), GetPendingService().get()); |
| 1298 | } |
| 1299 | |
| 1300 | TEST_F(WiFiMainTest, IsIdle) { |
| 1301 | StartWiFi(); |
| 1302 | EXPECT_TRUE(wifi()->IsIdle()); |
| 1303 | |
| 1304 | WiFiEndpointRefPtr ap = MakeEndpoint("an_ssid", "00:01:02:03:04:05"); |
| 1305 | WiFiServiceRefPtr service = CreateServiceForEndpoint(*ap); |
| 1306 | Error error; |
| 1307 | service->AddEndpoint(ap); |
| 1308 | service->AutoConnect(); |
| 1309 | EXPECT_FALSE(wifi()->IsIdle()); |
| 1310 | } |
| 1311 | |
mukesh agrawal | 4d0401c | 2012-01-06 16:05:31 -0800 | [diff] [blame] | 1312 | MATCHER(WiFiAddedArgs, "") { |
| 1313 | return ContainsKey(arg, wpa_supplicant::kNetworkPropertyScanSSID) && |
| 1314 | ContainsKey(arg, wpa_supplicant::kNetworkPropertyBgscan); |
| 1315 | } |
| 1316 | |
| 1317 | TEST_F(WiFiMainTest, AddNetworkArgs) { |
| 1318 | MockSupplicantInterfaceProxy &supplicant_interface_proxy = |
| 1319 | *supplicant_interface_proxy_; |
| 1320 | |
| 1321 | StartWiFi(); |
| 1322 | ReportBSS("bss0", "ssid0", "00:00:00:00:00:00", 0, kNetworkModeAdHoc); |
| 1323 | WiFiService *service(GetServices().begin()->get()); |
| 1324 | EXPECT_CALL(supplicant_interface_proxy, AddNetwork(WiFiAddedArgs())); |
| 1325 | InitiateConnect(service); |
| 1326 | } |
| 1327 | |
Chris Masone | 853b81b | 2011-06-24 14:11:41 -0700 | [diff] [blame] | 1328 | } // namespace shill |