Chris Masone | 34af218 | 2011-08-22 11:59:36 -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_service.h" |
| 6 | |
mukesh agrawal | d835b20 | 2011-10-07 15:26:47 -0700 | [diff] [blame] | 7 | #include <map> |
Chris Masone | 34af218 | 2011-08-22 11:59:36 -0700 | [diff] [blame] | 8 | #include <string> |
| 9 | #include <vector> |
| 10 | |
| 11 | #include <base/string_util.h> |
| 12 | #include <chromeos/dbus/service_constants.h> |
| 13 | #include <gmock/gmock.h> |
| 14 | #include <gtest/gtest.h> |
| 15 | |
Paul Stewart | 26b327e | 2011-10-19 11:38:09 -0700 | [diff] [blame] | 16 | #include "shill/event_dispatcher.h" |
Chris Masone | 34af218 | 2011-08-22 11:59:36 -0700 | [diff] [blame] | 17 | #include "shill/manager.h" |
| 18 | #include "shill/mock_adaptors.h" |
| 19 | #include "shill/mock_control.h" |
| 20 | #include "shill/mock_service.h" |
| 21 | #include "shill/mock_store.h" |
mukesh agrawal | 6e27777 | 2011-09-29 15:04:23 -0700 | [diff] [blame] | 22 | #include "shill/mock_wifi.h" |
Chris Masone | 34af218 | 2011-08-22 11:59:36 -0700 | [diff] [blame] | 23 | #include "shill/property_store_unittest.h" |
mukesh agrawal | 6e27777 | 2011-09-29 15:04:23 -0700 | [diff] [blame] | 24 | #include "shill/wpa_supplicant.h" |
Chris Masone | 34af218 | 2011-08-22 11:59:36 -0700 | [diff] [blame] | 25 | |
mukesh agrawal | d835b20 | 2011-10-07 15:26:47 -0700 | [diff] [blame] | 26 | using std::map; |
Chris Masone | 34af218 | 2011-08-22 11:59:36 -0700 | [diff] [blame] | 27 | using std::string; |
| 28 | using std::vector; |
| 29 | |
| 30 | namespace shill { |
Paul Stewart | d08f443 | 2011-11-04 07:48:20 -0700 | [diff] [blame] | 31 | using ::testing::_; |
| 32 | using ::testing::DoAll; |
mukesh agrawal | 6e27777 | 2011-09-29 15:04:23 -0700 | [diff] [blame] | 33 | using ::testing::NiceMock; |
Paul Stewart | d08f443 | 2011-11-04 07:48:20 -0700 | [diff] [blame] | 34 | using ::testing::Return; |
| 35 | using ::testing::SetArgumentPointee; |
| 36 | using ::testing::StrEq; |
Chris Masone | 34af218 | 2011-08-22 11:59:36 -0700 | [diff] [blame] | 37 | |
| 38 | class WiFiServiceTest : public PropertyStoreTest { |
| 39 | public: |
mukesh agrawal | 6e27777 | 2011-09-29 15:04:23 -0700 | [diff] [blame] | 40 | WiFiServiceTest() : wifi_( |
| 41 | new NiceMock<MockWiFi>( |
| 42 | control_interface(), |
| 43 | dispatcher(), |
| 44 | manager(), |
| 45 | "wifi", |
| 46 | fake_mac, |
| 47 | 0)) {} |
Chris Masone | 34af218 | 2011-08-22 11:59:36 -0700 | [diff] [blame] | 48 | virtual ~WiFiServiceTest() {} |
mukesh agrawal | 6e27777 | 2011-09-29 15:04:23 -0700 | [diff] [blame] | 49 | |
| 50 | protected: |
| 51 | static const char fake_mac[]; |
| 52 | scoped_refptr<MockWiFi> wifi() { return wifi_; } |
| 53 | |
| 54 | private: |
| 55 | scoped_refptr<MockWiFi> wifi_; |
Chris Masone | 34af218 | 2011-08-22 11:59:36 -0700 | [diff] [blame] | 56 | }; |
| 57 | |
mukesh agrawal | 6e27777 | 2011-09-29 15:04:23 -0700 | [diff] [blame] | 58 | // static |
| 59 | const char WiFiServiceTest::fake_mac[] = "AaBBcCDDeeFF"; |
| 60 | |
Paul Stewart | d08f443 | 2011-11-04 07:48:20 -0700 | [diff] [blame] | 61 | class WiFiServiceSecurityTest : public WiFiServiceTest { |
| 62 | public: |
| 63 | WiFiServiceRefPtr CreateServiceWithSecurity(const string &security) { |
| 64 | vector<uint8_t> ssid(5, 0); |
| 65 | ssid.push_back(0xff); |
| 66 | |
| 67 | return new WiFiService(control_interface(), |
| 68 | dispatcher(), |
| 69 | manager(), |
| 70 | wifi(), |
| 71 | ssid, |
| 72 | flimflam::kModeManaged, |
| 73 | security); |
| 74 | } |
| 75 | |
| 76 | bool TestStorageSecurityIs(WiFiServiceRefPtr wifi_service, |
| 77 | const string &security) { |
| 78 | string id = wifi_service->GetStorageIdentifier(); |
| 79 | size_t mac_pos = id.find(StringToLowerASCII(string(fake_mac))); |
| 80 | EXPECT_NE(mac_pos, string::npos); |
| 81 | size_t mode_pos = id.find(string(flimflam::kModeManaged), mac_pos); |
| 82 | EXPECT_NE(mode_pos, string::npos); |
| 83 | return id.find(string(security), mode_pos) != string::npos; |
| 84 | } |
| 85 | |
| 86 | // Test that a service that is created with security |from_security| |
| 87 | // gets by default a storage identifier with |to_security| as its |
| 88 | // security component. |
| 89 | bool TestStorageMapping(const string &from_security, |
| 90 | const string &to_security) { |
| 91 | WiFiServiceRefPtr wifi_service = CreateServiceWithSecurity(from_security); |
| 92 | return TestStorageSecurityIs(wifi_service, to_security); |
| 93 | } |
| 94 | |
| 95 | // Test whether a service of type |service_security| can load from a |
| 96 | // storage interface containing an entry for |storage_security|. |
| 97 | // Make sure the result meets |expectation|. If |expectation| is |
| 98 | // true, also make sure the service storage identifier changes to |
| 99 | // match |storage_security|. |
| 100 | bool TestLoadMapping(const string &service_security, |
| 101 | const string &storage_security, |
| 102 | bool expectation) { |
| 103 | WiFiServiceRefPtr wifi_service = |
| 104 | CreateServiceWithSecurity(service_security); |
| 105 | NiceMock<MockStore> mock_store; |
| 106 | const string storage_id = |
| 107 | wifi_service->GetStorageIdentifierForSecurity(storage_security); |
| 108 | EXPECT_CALL(mock_store, ContainsGroup(_)) |
| 109 | .WillRepeatedly(Return(false)); |
| 110 | EXPECT_CALL(mock_store, ContainsGroup(StrEq(storage_id))) |
| 111 | .WillRepeatedly(Return(true)); |
| 112 | bool is_loadable = wifi_service->IsLoadableFrom(&mock_store); |
| 113 | EXPECT_EQ(expectation, is_loadable); |
| 114 | bool is_loaded = wifi_service->Load(&mock_store); |
| 115 | EXPECT_EQ(expectation, is_loaded); |
| 116 | |
| 117 | if (expectation != is_loadable || expectation != is_loaded) { |
| 118 | return false; |
| 119 | } else if (!expectation) { |
| 120 | return true; |
| 121 | } else { |
| 122 | return TestStorageSecurityIs(wifi_service, storage_security); |
| 123 | } |
| 124 | } |
| 125 | }; |
| 126 | |
mukesh agrawal | 6e27777 | 2011-09-29 15:04:23 -0700 | [diff] [blame] | 127 | MATCHER(WPASecurityArgs, "") { |
| 128 | return ContainsKey(arg, wpa_supplicant::kPropertySecurityProtocol) && |
| 129 | ContainsKey(arg, wpa_supplicant::kPropertyPreSharedKey); |
| 130 | } |
| 131 | |
Chris Masone | 34af218 | 2011-08-22 11:59:36 -0700 | [diff] [blame] | 132 | TEST_F(WiFiServiceTest, StorageId) { |
| 133 | vector<uint8_t> ssid(5, 0); |
| 134 | ssid.push_back(0xff); |
Chris Masone | 9d77993 | 2011-08-25 16:33:41 -0700 | [diff] [blame] | 135 | |
Chris Masone | 2176a88 | 2011-09-14 22:29:15 -0700 | [diff] [blame] | 136 | WiFiServiceRefPtr wifi_service = new WiFiService(control_interface(), |
| 137 | dispatcher(), |
Chris Masone | 9d77993 | 2011-08-25 16:33:41 -0700 | [diff] [blame] | 138 | manager(), |
mukesh agrawal | 6e27777 | 2011-09-29 15:04:23 -0700 | [diff] [blame] | 139 | wifi(), |
Chris Masone | 9d77993 | 2011-08-25 16:33:41 -0700 | [diff] [blame] | 140 | ssid, |
| 141 | flimflam::kModeManaged, |
Paul Stewart | d08f443 | 2011-11-04 07:48:20 -0700 | [diff] [blame] | 142 | flimflam::kSecurityNone); |
Chris Masone | 9d77993 | 2011-08-25 16:33:41 -0700 | [diff] [blame] | 143 | string id = wifi_service->GetStorageIdentifier(); |
Chris Masone | 34af218 | 2011-08-22 11:59:36 -0700 | [diff] [blame] | 144 | for (uint i = 0; i < id.length(); ++i) { |
| 145 | EXPECT_TRUE(id[i] == '_' || |
| 146 | isxdigit(id[i]) || |
| 147 | (isalpha(id[i]) && islower(id[i]))); |
| 148 | } |
Paul Stewart | 22aa71b | 2011-09-16 12:15:11 -0700 | [diff] [blame] | 149 | EXPECT_TRUE(wifi_service->TechnologyIs(Technology::kWifi)); |
Chris Masone | 34af218 | 2011-08-22 11:59:36 -0700 | [diff] [blame] | 150 | size_t mac_pos = id.find(StringToLowerASCII(string(fake_mac))); |
| 151 | EXPECT_NE(mac_pos, string::npos); |
| 152 | EXPECT_NE(id.find(string(flimflam::kModeManaged), mac_pos), string::npos); |
| 153 | } |
| 154 | |
mukesh agrawal | d835b20 | 2011-10-07 15:26:47 -0700 | [diff] [blame] | 155 | TEST_F(WiFiServiceTest, NonUTF8SSID) { |
| 156 | vector<uint8_t> ssid; |
| 157 | |
| 158 | ssid.push_back(0xff); // not a valid UTF-8 byte-sequence |
| 159 | WiFiServiceRefPtr wifi_service = new WiFiService(control_interface(), |
| 160 | dispatcher(), |
| 161 | manager(), |
| 162 | wifi(), |
| 163 | ssid, |
| 164 | flimflam::kModeManaged, |
Paul Stewart | d08f443 | 2011-11-04 07:48:20 -0700 | [diff] [blame] | 165 | flimflam::kSecurityNone); |
mukesh agrawal | d835b20 | 2011-10-07 15:26:47 -0700 | [diff] [blame] | 166 | map<string, ::DBus::Variant> properties; |
| 167 | // if service doesn't propertly sanitize SSID, this will generate SIGABRT. |
| 168 | DBusAdaptor::GetProperties(wifi_service->store(), &properties, NULL); |
| 169 | } |
| 170 | |
mukesh agrawal | f2fd745 | 2011-10-03 16:38:47 -0700 | [diff] [blame] | 171 | TEST_F(WiFiServiceTest, ConnectTaskWPA) { |
mukesh agrawal | 6e27777 | 2011-09-29 15:04:23 -0700 | [diff] [blame] | 172 | vector<uint8_t> ssid(5, 0); |
| 173 | WiFiServiceRefPtr wifi_service = new WiFiService(control_interface(), |
| 174 | dispatcher(), |
| 175 | manager(), |
| 176 | wifi(), |
| 177 | ssid, |
| 178 | flimflam::kModeManaged, |
| 179 | flimflam::kSecurityWpa); |
| 180 | EXPECT_CALL(*wifi(), |
| 181 | ConnectTo(wifi_service.get(), WPASecurityArgs())); |
| 182 | wifi_service->ConnectTask(); |
| 183 | } |
| 184 | |
mukesh agrawal | f2fd745 | 2011-10-03 16:38:47 -0700 | [diff] [blame] | 185 | TEST_F(WiFiServiceTest, ConnectTaskRSN) { |
| 186 | vector<uint8_t> ssid(5, 0); |
| 187 | WiFiServiceRefPtr wifi_service = new WiFiService(control_interface(), |
| 188 | dispatcher(), |
| 189 | manager(), |
| 190 | wifi(), |
| 191 | ssid, |
| 192 | flimflam::kModeManaged, |
| 193 | flimflam::kSecurityRsn); |
| 194 | EXPECT_CALL(*wifi(), |
| 195 | ConnectTo(wifi_service.get(), WPASecurityArgs())); |
| 196 | wifi_service->ConnectTask(); |
| 197 | } |
| 198 | |
Paul Stewart | d08f443 | 2011-11-04 07:48:20 -0700 | [diff] [blame] | 199 | TEST_F(WiFiServiceTest, LoadHidden) { |
| 200 | vector<uint8_t> ssid(5, 0); |
| 201 | ssid.push_back(0xff); |
| 202 | |
| 203 | WiFiServiceRefPtr service = new WiFiService(control_interface(), |
| 204 | dispatcher(), |
| 205 | manager(), |
| 206 | wifi(), |
| 207 | ssid, |
| 208 | flimflam::kModeManaged, |
| 209 | flimflam::kSecurityNone); |
| 210 | ASSERT_FALSE(service->hidden_ssid_); |
| 211 | NiceMock<MockStore> mock_store; |
| 212 | const string storage_id = service->GetStorageIdentifier(); |
| 213 | EXPECT_CALL(mock_store, ContainsGroup(StrEq(storage_id))) |
| 214 | .WillRepeatedly(Return(true)); |
| 215 | EXPECT_CALL(mock_store, GetBool(_, _, _)) |
| 216 | .WillRepeatedly(Return(false)); |
| 217 | EXPECT_CALL(mock_store, |
| 218 | GetBool(StrEq(storage_id), WiFiService::kStorageHiddenSSID, _)) |
| 219 | .WillRepeatedly(DoAll(SetArgumentPointee<2>(true), Return(true))); |
| 220 | EXPECT_TRUE(service->Load(&mock_store)); |
| 221 | EXPECT_TRUE(service->hidden_ssid_); |
| 222 | } |
| 223 | |
| 224 | TEST_F(WiFiServiceSecurityTest, WPAMapping) { |
| 225 | EXPECT_TRUE(TestStorageMapping(flimflam::kSecurityRsn, |
| 226 | flimflam::kSecurityPsk)); |
| 227 | EXPECT_TRUE(TestStorageMapping(flimflam::kSecurityWpa, |
| 228 | flimflam::kSecurityPsk)); |
| 229 | EXPECT_TRUE(TestStorageMapping(flimflam::kSecurityPsk, |
| 230 | flimflam::kSecurityPsk)); |
| 231 | EXPECT_TRUE(TestStorageMapping(flimflam::kSecurityWep, |
| 232 | flimflam::kSecurityWep)); |
| 233 | EXPECT_TRUE(TestStorageMapping(flimflam::kSecurityNone, |
| 234 | flimflam::kSecurityNone)); |
| 235 | // TODO(pstew): 802.1x is in a NOTIMPLEMENTED block in wifi_service.cc |
| 236 | // EXPECT_TRUE(TestStorageMapping(flimflam::kSecurity8021x, |
| 237 | // flimflam::kSecurity8021x)); |
| 238 | } |
| 239 | |
| 240 | TEST_F(WiFiServiceSecurityTest, LoadMapping) { |
| 241 | EXPECT_TRUE(TestLoadMapping(flimflam::kSecurityRsn, |
| 242 | flimflam::kSecurityPsk, |
| 243 | true)); |
| 244 | EXPECT_TRUE(TestLoadMapping(flimflam::kSecurityRsn, |
| 245 | flimflam::kSecurityRsn, |
| 246 | true)); |
| 247 | EXPECT_TRUE(TestLoadMapping(flimflam::kSecurityRsn, |
| 248 | flimflam::kSecurityWpa, |
| 249 | false)); |
| 250 | EXPECT_TRUE(TestLoadMapping(flimflam::kSecurityWpa, |
| 251 | flimflam::kSecurityPsk, |
| 252 | true)); |
| 253 | EXPECT_TRUE(TestLoadMapping(flimflam::kSecurityWpa, |
| 254 | flimflam::kSecurityWpa, |
| 255 | true)); |
| 256 | EXPECT_TRUE(TestLoadMapping(flimflam::kSecurityWpa, |
| 257 | flimflam::kSecurityRsn, |
| 258 | false)); |
| 259 | EXPECT_TRUE(TestLoadMapping(flimflam::kSecurityWep, |
| 260 | flimflam::kSecurityWep, |
| 261 | true)); |
| 262 | EXPECT_TRUE(TestLoadMapping(flimflam::kSecurityWep, |
| 263 | flimflam::kSecurityPsk, |
| 264 | false)); |
| 265 | } |
| 266 | |
Chris Masone | 34af218 | 2011-08-22 11:59:36 -0700 | [diff] [blame] | 267 | } // namespace shill |