Chris Masone | 853b81b | 2011-06-24 14:11:41 -0700 | [diff] [blame] | 1 | // Copyright (c) 2011 The Chromium OS Authors. All rights reserved. |
| 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 | |
| 7 | #include <map> |
| 8 | #include <string> |
| 9 | #include <vector> |
| 10 | |
mukesh agrawal | dc42bb3 | 2011-07-28 10:40:26 -0700 | [diff] [blame] | 11 | #include <base/memory/scoped_ptr.h> |
mukesh agrawal | 3195024 | 2011-07-14 11:53:38 -0700 | [diff] [blame] | 12 | #include <base/string_number_conversions.h> |
| 13 | #include <base/string_util.h> |
Chris Masone | 853b81b | 2011-06-24 14:11:41 -0700 | [diff] [blame] | 14 | #include <dbus-c++/dbus.h> |
| 15 | #include <chromeos/dbus/service_constants.h> |
Chris Masone | 7156c92 | 2011-08-23 20:36:21 -0700 | [diff] [blame] | 16 | #include <gmock/gmock.h> |
Chris Masone | 2ae797d | 2011-08-23 20:41:00 -0700 | [diff] [blame] | 17 | #include <gtest/gtest.h> |
Chris Masone | 853b81b | 2011-06-24 14:11:41 -0700 | [diff] [blame] | 18 | |
| 19 | #include "shill/dbus_adaptor.h" |
| 20 | #include "shill/manager.h" |
Chris Masone | 853b81b | 2011-06-24 14:11:41 -0700 | [diff] [blame] | 21 | #include "shill/mock_device.h" |
Chris Masone | 2ae797d | 2011-08-23 20:41:00 -0700 | [diff] [blame] | 22 | #include "shill/mock_manager.h" |
mukesh agrawal | 3195024 | 2011-07-14 11:53:38 -0700 | [diff] [blame] | 23 | #include "shill/mock_supplicant_interface_proxy.h" |
| 24 | #include "shill/mock_supplicant_process_proxy.h" |
mukesh agrawal | dc42bb3 | 2011-07-28 10:40:26 -0700 | [diff] [blame] | 25 | #include "shill/nice_mock_control.h" |
Chris Masone | 853b81b | 2011-06-24 14:11:41 -0700 | [diff] [blame] | 26 | #include "shill/property_store_unittest.h" |
mukesh agrawal | 3195024 | 2011-07-14 11:53:38 -0700 | [diff] [blame] | 27 | #include "shill/proxy_factory.h" |
| 28 | #include "shill/wifi_endpoint.h" |
| 29 | #include "shill/wifi.h" |
Chris Masone | 853b81b | 2011-06-24 14:11:41 -0700 | [diff] [blame] | 30 | |
| 31 | using std::map; |
| 32 | using std::string; |
| 33 | using std::vector; |
| 34 | using ::testing::_; |
mukesh agrawal | 3195024 | 2011-07-14 11:53:38 -0700 | [diff] [blame] | 35 | using ::testing::AnyNumber; |
| 36 | using ::testing::DefaultValue; |
| 37 | using ::testing::InSequence; |
mukesh agrawal | dc42bb3 | 2011-07-28 10:40:26 -0700 | [diff] [blame] | 38 | using ::testing::NiceMock; |
Chris Masone | 853b81b | 2011-06-24 14:11:41 -0700 | [diff] [blame] | 39 | using ::testing::Return; |
| 40 | using ::testing::Test; |
mukesh agrawal | 3195024 | 2011-07-14 11:53:38 -0700 | [diff] [blame] | 41 | using ::testing::Throw; |
Chris Masone | 853b81b | 2011-06-24 14:11:41 -0700 | [diff] [blame] | 42 | |
| 43 | namespace shill { |
| 44 | |
mukesh agrawal | 3195024 | 2011-07-14 11:53:38 -0700 | [diff] [blame] | 45 | class WiFiPropertyTest : public PropertyStoreTest { |
Chris Masone | 853b81b | 2011-06-24 14:11:41 -0700 | [diff] [blame] | 46 | public: |
mukesh agrawal | 3195024 | 2011-07-14 11:53:38 -0700 | [diff] [blame] | 47 | WiFiPropertyTest() |
Chris Masone | 626719f | 2011-08-18 16:58:48 -0700 | [diff] [blame] | 48 | : device_(new WiFi(&control_interface_, NULL, NULL, "wifi", "", 0)) { |
Chris Masone | 853b81b | 2011-06-24 14:11:41 -0700 | [diff] [blame] | 49 | } |
mukesh agrawal | 3195024 | 2011-07-14 11:53:38 -0700 | [diff] [blame] | 50 | virtual ~WiFiPropertyTest() {} |
Chris Masone | 853b81b | 2011-06-24 14:11:41 -0700 | [diff] [blame] | 51 | |
| 52 | protected: |
| 53 | DeviceRefPtr device_; |
| 54 | }; |
| 55 | |
mukesh agrawal | 3195024 | 2011-07-14 11:53:38 -0700 | [diff] [blame] | 56 | TEST_F(WiFiPropertyTest, Contains) { |
Chris Masone | 27c4aa5 | 2011-07-02 13:10:14 -0700 | [diff] [blame] | 57 | EXPECT_TRUE(device_->store()->Contains(flimflam::kNameProperty)); |
| 58 | EXPECT_FALSE(device_->store()->Contains("")); |
Chris Masone | 853b81b | 2011-06-24 14:11:41 -0700 | [diff] [blame] | 59 | } |
| 60 | |
mukesh agrawal | 3195024 | 2011-07-14 11:53:38 -0700 | [diff] [blame] | 61 | TEST_F(WiFiPropertyTest, Dispatch) { |
Chris Masone | a8a2c25 | 2011-06-27 22:16:30 -0700 | [diff] [blame] | 62 | { |
| 63 | ::DBus::Error error; |
Chris Masone | 27c4aa5 | 2011-07-02 13:10:14 -0700 | [diff] [blame] | 64 | EXPECT_TRUE(DBusAdaptor::DispatchOnType(device_->store(), |
Chris Masone | a8a2c25 | 2011-06-27 22:16:30 -0700 | [diff] [blame] | 65 | flimflam::kBgscanMethodProperty, |
| 66 | PropertyStoreTest::kStringV, |
| 67 | &error)); |
| 68 | } |
| 69 | { |
| 70 | ::DBus::Error error; |
| 71 | EXPECT_TRUE(DBusAdaptor::DispatchOnType( |
Chris Masone | 27c4aa5 | 2011-07-02 13:10:14 -0700 | [diff] [blame] | 72 | device_->store(), |
Chris Masone | a8a2c25 | 2011-06-27 22:16:30 -0700 | [diff] [blame] | 73 | flimflam::kBgscanSignalThresholdProperty, |
| 74 | PropertyStoreTest::kInt32V, |
| 75 | &error)); |
| 76 | } |
| 77 | { |
| 78 | ::DBus::Error error; |
Chris Masone | 27c4aa5 | 2011-07-02 13:10:14 -0700 | [diff] [blame] | 79 | EXPECT_TRUE(DBusAdaptor::DispatchOnType(device_->store(), |
Chris Masone | a8a2c25 | 2011-06-27 22:16:30 -0700 | [diff] [blame] | 80 | flimflam::kScanIntervalProperty, |
| 81 | PropertyStoreTest::kUint16V, |
| 82 | &error)); |
| 83 | } |
Chris Masone | 853b81b | 2011-06-24 14:11:41 -0700 | [diff] [blame] | 84 | // 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] | 85 | { |
| 86 | ::DBus::Error error; |
Chris Masone | 27c4aa5 | 2011-07-02 13:10:14 -0700 | [diff] [blame] | 87 | EXPECT_FALSE(DBusAdaptor::DispatchOnType(device_->store(), |
Chris Masone | a8a2c25 | 2011-06-27 22:16:30 -0700 | [diff] [blame] | 88 | flimflam::kScanningProperty, |
| 89 | PropertyStoreTest::kBoolV, |
| 90 | &error)); |
| 91 | EXPECT_EQ(invalid_args_, error.name()); |
| 92 | } |
Chris Masone | 853b81b | 2011-06-24 14:11:41 -0700 | [diff] [blame] | 93 | } |
| 94 | |
mukesh agrawal | 3195024 | 2011-07-14 11:53:38 -0700 | [diff] [blame] | 95 | class WiFiMainTest : public Test { |
| 96 | public: |
| 97 | WiFiMainTest() |
| 98 | : manager_(&control_interface_, NULL, NULL), |
Chris Masone | 626719f | 2011-08-18 16:58:48 -0700 | [diff] [blame] | 99 | wifi_(new WiFi(&control_interface_, |
| 100 | NULL, |
| 101 | &manager_, |
| 102 | kDeviceName, |
| 103 | kDeviceAddress, |
| 104 | 0)), |
mukesh agrawal | dc42bb3 | 2011-07-28 10:40:26 -0700 | [diff] [blame] | 105 | supplicant_process_proxy_(new NiceMock<MockSupplicantProcessProxy>()), |
| 106 | supplicant_interface_proxy_( |
| 107 | new NiceMock<MockSupplicantInterfaceProxy>(wifi_)), |
| 108 | proxy_factory_(this) { |
mukesh agrawal | 3195024 | 2011-07-14 11:53:38 -0700 | [diff] [blame] | 109 | ProxyFactory::set_factory(&proxy_factory_); |
| 110 | ::testing::DefaultValue< ::DBus::Path>::Set("/default/path"); |
| 111 | } |
| 112 | virtual ~WiFiMainTest() { |
| 113 | // must Stop WiFi instance, to clear its list of services. |
| 114 | // otherwise, the WiFi instance will not be deleted. (because |
| 115 | // services reference a WiFi instance, creating a cycle.) |
| 116 | wifi_->Stop(); |
| 117 | } |
| 118 | |
mukesh agrawal | 3195024 | 2011-07-14 11:53:38 -0700 | [diff] [blame] | 119 | protected: |
| 120 | class TestProxyFactory : public ProxyFactory { |
| 121 | public: |
Paul Stewart | 7355ce1 | 2011-09-02 10:47:01 -0700 | [diff] [blame^] | 122 | explicit TestProxyFactory(WiFiMainTest *test) : test_(test) {} |
mukesh agrawal | 3195024 | 2011-07-14 11:53:38 -0700 | [diff] [blame] | 123 | |
| 124 | virtual SupplicantProcessProxyInterface *CreateSupplicantProcessProxy( |
| 125 | const char *dbus_path, const char *dbus_addr) { |
mukesh agrawal | dc42bb3 | 2011-07-28 10:40:26 -0700 | [diff] [blame] | 126 | return test_->supplicant_process_proxy_.release(); |
mukesh agrawal | 3195024 | 2011-07-14 11:53:38 -0700 | [diff] [blame] | 127 | } |
| 128 | |
| 129 | virtual SupplicantInterfaceProxyInterface *CreateSupplicantInterfaceProxy( |
| 130 | const WiFiRefPtr &wifi, |
| 131 | const DBus::Path &object_path, |
| 132 | const char *dbus_addr) { |
mukesh agrawal | dc42bb3 | 2011-07-28 10:40:26 -0700 | [diff] [blame] | 133 | return test_->supplicant_interface_proxy_.release(); |
mukesh agrawal | 3195024 | 2011-07-14 11:53:38 -0700 | [diff] [blame] | 134 | } |
| 135 | |
| 136 | private: |
mukesh agrawal | dc42bb3 | 2011-07-28 10:40:26 -0700 | [diff] [blame] | 137 | WiFiMainTest *test_; |
mukesh agrawal | 3195024 | 2011-07-14 11:53:38 -0700 | [diff] [blame] | 138 | }; |
| 139 | |
| 140 | const WiFi::EndpointMap &GetEndpointMap() { |
| 141 | return wifi_->endpoint_by_bssid_; |
| 142 | } |
| 143 | const WiFi::ServiceMap &GetServiceMap() { |
| 144 | return wifi_->service_by_private_id_; |
| 145 | } |
| 146 | // note: the tests need the proxies referenced by WiFi (not the |
| 147 | // proxies instantiated by WiFiMainTest), to ensure that WiFi |
| 148 | // sets up its proxies correctly. |
| 149 | SupplicantProcessProxyInterface *GetSupplicantProcessProxy() { |
| 150 | return wifi_->supplicant_process_proxy_.get(); |
| 151 | } |
| 152 | SupplicantInterfaceProxyInterface *GetSupplicantInterfaceProxy() { |
| 153 | return wifi_->supplicant_interface_proxy_.get(); |
| 154 | } |
| 155 | void InitiateConnect(const WiFiService &service) { |
| 156 | wifi_->ConnectTo(service); |
| 157 | } |
| 158 | void ReportBSS(const ::DBus::Path &bss_path, |
| 159 | const string &ssid, |
| 160 | const string &bssid, |
| 161 | int16_t signal_strength, |
| 162 | const char *mode); |
| 163 | void ReportScanDone() { |
mukesh agrawal | dc42bb3 | 2011-07-28 10:40:26 -0700 | [diff] [blame] | 164 | wifi_->ScanDoneTask(); |
mukesh agrawal | 3195024 | 2011-07-14 11:53:38 -0700 | [diff] [blame] | 165 | } |
| 166 | void StartWiFi() { |
| 167 | wifi_->Start(); |
mukesh agrawal | 3195024 | 2011-07-14 11:53:38 -0700 | [diff] [blame] | 168 | } |
| 169 | void StopWiFi() { |
| 170 | wifi_->Stop(); |
| 171 | } |
| 172 | |
| 173 | private: |
mukesh agrawal | dc42bb3 | 2011-07-28 10:40:26 -0700 | [diff] [blame] | 174 | NiceMockControl control_interface_; |
Chris Masone | 2ae797d | 2011-08-23 20:41:00 -0700 | [diff] [blame] | 175 | MockManager manager_; |
mukesh agrawal | 3195024 | 2011-07-14 11:53:38 -0700 | [diff] [blame] | 176 | WiFiRefPtr wifi_; |
mukesh agrawal | 3195024 | 2011-07-14 11:53:38 -0700 | [diff] [blame] | 177 | |
| 178 | // protected fields interspersed between private fields, due to |
| 179 | // initialization order |
| 180 | protected: |
| 181 | static const char kDeviceName[]; |
Chris Masone | 626719f | 2011-08-18 16:58:48 -0700 | [diff] [blame] | 182 | static const char kDeviceAddress[]; |
mukesh agrawal | 3195024 | 2011-07-14 11:53:38 -0700 | [diff] [blame] | 183 | static const char kNetworkModeAdHoc[]; |
| 184 | static const char kNetworkModeInfrastructure[]; |
| 185 | |
mukesh agrawal | dc42bb3 | 2011-07-28 10:40:26 -0700 | [diff] [blame] | 186 | scoped_ptr<MockSupplicantProcessProxy> supplicant_process_proxy_; |
| 187 | scoped_ptr<MockSupplicantInterfaceProxy> supplicant_interface_proxy_; |
mukesh agrawal | 3195024 | 2011-07-14 11:53:38 -0700 | [diff] [blame] | 188 | |
| 189 | private: |
| 190 | TestProxyFactory proxy_factory_; |
mukesh agrawal | 3195024 | 2011-07-14 11:53:38 -0700 | [diff] [blame] | 191 | }; |
| 192 | |
| 193 | const char WiFiMainTest::kDeviceName[] = "wlan0"; |
Chris Masone | 626719f | 2011-08-18 16:58:48 -0700 | [diff] [blame] | 194 | const char WiFiMainTest::kDeviceAddress[] = "00:01:02:03:04:05"; |
mukesh agrawal | 3195024 | 2011-07-14 11:53:38 -0700 | [diff] [blame] | 195 | const char WiFiMainTest::kNetworkModeAdHoc[] = "ad-hoc"; |
| 196 | const char WiFiMainTest::kNetworkModeInfrastructure[] = "infrastructure"; |
| 197 | |
| 198 | void WiFiMainTest::ReportBSS(const ::DBus::Path &bss_path, |
| 199 | const string &ssid, |
| 200 | const string &bssid, |
| 201 | int16_t signal_strength, |
| 202 | const char *mode) { |
| 203 | map<string, ::DBus::Variant> bss_properties; |
| 204 | |
| 205 | { |
| 206 | DBus::MessageIter writer(bss_properties["SSID"].writer()); |
| 207 | writer << vector<uint8_t>(ssid.begin(), ssid.end()); |
| 208 | } |
| 209 | { |
| 210 | string bssid_nosep; |
| 211 | vector<uint8_t> bssid_bytes; |
| 212 | RemoveChars(bssid, ":", &bssid_nosep); |
| 213 | base::HexStringToBytes(bssid_nosep, &bssid_bytes); |
| 214 | |
| 215 | DBus::MessageIter writer(bss_properties["BSSID"].writer()); |
| 216 | writer << bssid_bytes; |
| 217 | } |
| 218 | bss_properties["Signal"].writer().append_int16(signal_strength); |
| 219 | bss_properties["Mode"].writer().append_string(mode); |
| 220 | wifi_->BSSAdded(bss_path, bss_properties); |
| 221 | } |
| 222 | |
| 223 | TEST_F(WiFiMainTest, ProxiesSetUpDuringStart) { |
| 224 | EXPECT_TRUE(GetSupplicantProcessProxy() == NULL); |
| 225 | EXPECT_TRUE(GetSupplicantInterfaceProxy() == NULL); |
| 226 | |
| 227 | StartWiFi(); |
| 228 | EXPECT_FALSE(GetSupplicantProcessProxy() == NULL); |
| 229 | EXPECT_FALSE(GetSupplicantInterfaceProxy() == NULL); |
| 230 | } |
| 231 | |
| 232 | TEST_F(WiFiMainTest, CleanStart) { |
| 233 | EXPECT_CALL(*supplicant_process_proxy_, CreateInterface(_)); |
| 234 | EXPECT_CALL(*supplicant_process_proxy_, GetInterface(_)) |
| 235 | .Times(AnyNumber()) |
| 236 | .WillRepeatedly(Throw( |
| 237 | DBus::Error( |
| 238 | "fi.w1.wpa_supplicant1.InterfaceUnknown", |
| 239 | "test threw fi.w1.wpa_supplicant1.InterfaceUnknown"))); |
| 240 | EXPECT_CALL(*supplicant_interface_proxy_, Scan(_)); |
| 241 | StartWiFi(); |
| 242 | } |
| 243 | |
| 244 | TEST_F(WiFiMainTest, Restart) { |
| 245 | EXPECT_CALL(*supplicant_process_proxy_, CreateInterface(_)) |
| 246 | .Times(AnyNumber()) |
| 247 | .WillRepeatedly(Throw( |
| 248 | DBus::Error( |
| 249 | "fi.w1.wpa_supplicant1.InterfaceExists", |
| 250 | "test thew fi.w1.wpa_supplicant1.InterfaceExists"))); |
| 251 | EXPECT_CALL(*supplicant_process_proxy_, GetInterface(_)); |
| 252 | EXPECT_CALL(*supplicant_interface_proxy_, Scan(_)); |
| 253 | StartWiFi(); |
| 254 | } |
| 255 | |
| 256 | TEST_F(WiFiMainTest, StartClearsState) { |
| 257 | EXPECT_CALL(*supplicant_interface_proxy_, RemoveAllNetworks()); |
| 258 | EXPECT_CALL(*supplicant_interface_proxy_, FlushBSS(_)); |
| 259 | StartWiFi(); |
| 260 | } |
| 261 | |
| 262 | TEST_F(WiFiMainTest, ScanResults) { |
| 263 | StartWiFi(); |
| 264 | ReportBSS("bss0", "ssid0", "00:00:00:00:00:00", 0, kNetworkModeAdHoc); |
| 265 | ReportBSS( |
| 266 | "bss1", "ssid1", "00:00:00:00:00:01", 1, kNetworkModeInfrastructure); |
| 267 | ReportBSS( |
| 268 | "bss2", "ssid2", "00:00:00:00:00:02", 2, kNetworkModeInfrastructure); |
| 269 | ReportBSS( |
| 270 | "bss3", "ssid3", "00:00:00:00:00:03", 3, kNetworkModeInfrastructure); |
| 271 | ReportBSS("bss4", "ssid4", "00:00:00:00:00:04", 4, kNetworkModeAdHoc); |
| 272 | EXPECT_EQ(5, GetEndpointMap().size()); |
| 273 | } |
| 274 | |
| 275 | TEST_F(WiFiMainTest, ScanResultsWithUpdates) { |
| 276 | StartWiFi(); |
| 277 | ReportBSS("bss0", "ssid0", "00:00:00:00:00:00", 0, kNetworkModeAdHoc); |
| 278 | ReportBSS( |
| 279 | "bss1", "ssid1", "00:00:00:00:00:01", 1, kNetworkModeInfrastructure); |
| 280 | ReportBSS( |
| 281 | "bss2", "ssid2", "00:00:00:00:00:02", 2, kNetworkModeInfrastructure); |
| 282 | ReportBSS( |
| 283 | "bss1", "ssid1", "00:00:00:00:00:01", 3, kNetworkModeInfrastructure); |
| 284 | ReportBSS("bss0", "ssid0", "00:00:00:00:00:00", 4, kNetworkModeAdHoc); |
| 285 | EXPECT_EQ(3, GetEndpointMap().size()); |
| 286 | ASSERT_TRUE(ContainsKey(GetEndpointMap(), "000000000000")); |
| 287 | EXPECT_EQ(4, GetEndpointMap().find("000000000000")->second-> |
| 288 | signal_strength()); |
| 289 | } |
| 290 | |
| 291 | TEST_F(WiFiMainTest, ScanCompleted) { |
| 292 | StartWiFi(); |
| 293 | ReportBSS("bss0", "ssid0", "00:00:00:00:00:00", 0, kNetworkModeAdHoc); |
| 294 | ReportBSS( |
| 295 | "bss1", "ssid1", "00:00:00:00:00:01", 1, kNetworkModeInfrastructure); |
| 296 | ReportBSS( |
| 297 | "bss2", "ssid2", "00:00:00:00:00:02", 2, kNetworkModeInfrastructure); |
| 298 | ReportScanDone(); |
| 299 | EXPECT_EQ(3, GetServiceMap().size()); |
| 300 | } |
| 301 | |
| 302 | TEST_F(WiFiMainTest, Connect) { |
mukesh agrawal | dc42bb3 | 2011-07-28 10:40:26 -0700 | [diff] [blame] | 303 | MockSupplicantInterfaceProxy &supplicant_interface_proxy = |
| 304 | *supplicant_interface_proxy_; |
| 305 | |
mukesh agrawal | 3195024 | 2011-07-14 11:53:38 -0700 | [diff] [blame] | 306 | StartWiFi(); |
| 307 | ReportBSS("bss0", "ssid0", "00:00:00:00:00:00", 0, kNetworkModeAdHoc); |
| 308 | ReportScanDone(); |
| 309 | |
| 310 | { |
| 311 | InSequence s; |
| 312 | DBus::Path fake_path("/fake/path"); |
| 313 | |
mukesh agrawal | dc42bb3 | 2011-07-28 10:40:26 -0700 | [diff] [blame] | 314 | EXPECT_CALL(supplicant_interface_proxy, AddNetwork(_)) |
mukesh agrawal | 3195024 | 2011-07-14 11:53:38 -0700 | [diff] [blame] | 315 | .WillOnce(Return(fake_path)); |
mukesh agrawal | dc42bb3 | 2011-07-28 10:40:26 -0700 | [diff] [blame] | 316 | EXPECT_CALL(supplicant_interface_proxy, SelectNetwork(fake_path)); |
mukesh agrawal | 3195024 | 2011-07-14 11:53:38 -0700 | [diff] [blame] | 317 | InitiateConnect(*(GetServiceMap().begin()->second)); |
| 318 | } |
| 319 | } |
| 320 | |
Chris Masone | 853b81b | 2011-06-24 14:11:41 -0700 | [diff] [blame] | 321 | } // namespace shill |