Peter Qiu | f073173 | 2014-11-11 09:46:41 -0800 | [diff] [blame] | 1 | // Copyright 2014 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 "apmanager/config.h" |
| 6 | |
Peter Qiu | fb39ba4 | 2014-11-21 09:09:59 -0800 | [diff] [blame] | 7 | #include <string> |
| 8 | |
Peter Qiu | f073173 | 2014-11-11 09:46:41 -0800 | [diff] [blame] | 9 | #include <base/strings/string_util.h> |
| 10 | #include <base/strings/stringprintf.h> |
| 11 | #include <chromeos/dbus/service_constants.h> |
| 12 | #include <gmock/gmock.h> |
| 13 | #include <gtest/gtest.h> |
| 14 | |
Peter Qiu | fb39ba4 | 2014-11-21 09:09:59 -0800 | [diff] [blame] | 15 | #include "apmanager/mock_device.h" |
| 16 | #include "apmanager/mock_manager.h" |
| 17 | |
Peter Qiu | 8e785b9 | 2014-11-24 10:01:08 -0800 | [diff] [blame^] | 18 | using ::testing::_; |
Peter Qiu | fb39ba4 | 2014-11-21 09:09:59 -0800 | [diff] [blame] | 19 | using ::testing::Mock; |
| 20 | using ::testing::Return; |
Peter Qiu | 8e785b9 | 2014-11-24 10:01:08 -0800 | [diff] [blame^] | 21 | using ::testing::SetArgumentPointee; |
Peter Qiu | f073173 | 2014-11-11 09:46:41 -0800 | [diff] [blame] | 22 | namespace apmanager { |
| 23 | |
| 24 | namespace { |
| 25 | |
| 26 | const char kServicePath[] = "/manager/services/0"; |
| 27 | const char kSsid[] = "TestSsid"; |
| 28 | const char kInterface[] = "uap0"; |
| 29 | const char kPassphrase[] = "Passphrase"; |
Peter Qiu | 8e785b9 | 2014-11-24 10:01:08 -0800 | [diff] [blame^] | 30 | const char k24GHzHTCapab[] = "[LDPC SMPS-STATIC GF SHORT-GI-20]"; |
| 31 | const char k5GHzHTCapab[] = |
| 32 | "[LDPC HT40+ SMPS-STATIC GF SHORT-GI-20 SHORT-GI-40]"; |
| 33 | |
Peter Qiu | f073173 | 2014-11-11 09:46:41 -0800 | [diff] [blame] | 34 | const uint16_t k24GHzChannel = 6; |
| 35 | const uint16_t k5GHzChannel = 36; |
| 36 | |
| 37 | const char kExpected80211gConfigContent[] = "ssid=TestSsid\n" |
| 38 | "channel=6\n" |
Peter Qiu | f073173 | 2014-11-11 09:46:41 -0800 | [diff] [blame] | 39 | "interface=uap0\n" |
Peter Qiu | 8e785b9 | 2014-11-24 10:01:08 -0800 | [diff] [blame^] | 40 | "hw_mode=g\n" |
Peter Qiu | f073173 | 2014-11-11 09:46:41 -0800 | [diff] [blame] | 41 | "driver=nl80211\n" |
| 42 | "fragm_threshold=2346\n" |
| 43 | "rts_threshold=2347\n"; |
| 44 | |
Peter Qiu | 8e785b9 | 2014-11-24 10:01:08 -0800 | [diff] [blame^] | 45 | const char kExpected80211n5GHzConfigContent[] = |
| 46 | "ssid=TestSsid\n" |
| 47 | "channel=36\n" |
| 48 | "interface=uap0\n" |
| 49 | "ieee80211n=1\n" |
| 50 | "ht_capab=[LDPC HT40+ SMPS-STATIC GF SHORT-GI-20 SHORT-GI-40]\n" |
| 51 | "hw_mode=a\n" |
| 52 | "driver=nl80211\n" |
| 53 | "fragm_threshold=2346\n" |
| 54 | "rts_threshold=2347\n"; |
Peter Qiu | f073173 | 2014-11-11 09:46:41 -0800 | [diff] [blame] | 55 | |
Peter Qiu | 8e785b9 | 2014-11-24 10:01:08 -0800 | [diff] [blame^] | 56 | const char kExpected80211n24GHzConfigContent[] = |
| 57 | "ssid=TestSsid\n" |
| 58 | "channel=6\n" |
| 59 | "interface=uap0\n" |
| 60 | "ieee80211n=1\n" |
| 61 | "ht_capab=[LDPC SMPS-STATIC GF SHORT-GI-20]\n" |
| 62 | "hw_mode=g\n" |
| 63 | "driver=nl80211\n" |
| 64 | "fragm_threshold=2346\n" |
| 65 | "rts_threshold=2347\n"; |
Peter Qiu | f073173 | 2014-11-11 09:46:41 -0800 | [diff] [blame] | 66 | |
| 67 | const char kExpectedRsnConfigContent[] = "ssid=TestSsid\n" |
| 68 | "channel=6\n" |
Peter Qiu | f073173 | 2014-11-11 09:46:41 -0800 | [diff] [blame] | 69 | "interface=uap0\n" |
Peter Qiu | 8e785b9 | 2014-11-24 10:01:08 -0800 | [diff] [blame^] | 70 | "hw_mode=g\n" |
Peter Qiu | f073173 | 2014-11-11 09:46:41 -0800 | [diff] [blame] | 71 | "wpa=2\n" |
| 72 | "rsn_pairwise=CCMP\n" |
| 73 | "wpa_key_mgmt=WPA-PSK\n" |
| 74 | "wpa_passphrase=Passphrase\n" |
| 75 | "driver=nl80211\n" |
| 76 | "fragm_threshold=2346\n" |
| 77 | "rts_threshold=2347\n"; |
| 78 | |
| 79 | } // namespace |
| 80 | |
| 81 | class ConfigTest : public testing::Test { |
| 82 | public: |
Peter Qiu | fb39ba4 | 2014-11-21 09:09:59 -0800 | [diff] [blame] | 83 | ConfigTest() : config_(&manager_, kServicePath) {} |
| 84 | |
| 85 | void SetupDevice(const std::string& interface) { |
| 86 | // Setup mock device. |
Peter Qiu | 8e785b9 | 2014-11-24 10:01:08 -0800 | [diff] [blame^] | 87 | device_ = new MockDevice(); |
| 88 | device_->SetPreferredApInterface(interface); |
Peter Qiu | fb39ba4 | 2014-11-21 09:09:59 -0800 | [diff] [blame] | 89 | EXPECT_CALL(manager_, GetDeviceFromInterfaceName(interface)) |
Peter Qiu | 8e785b9 | 2014-11-24 10:01:08 -0800 | [diff] [blame^] | 90 | .WillRepeatedly(Return(device_)); |
Peter Qiu | fb39ba4 | 2014-11-21 09:09:59 -0800 | [diff] [blame] | 91 | } |
Peter Qiu | f073173 | 2014-11-11 09:46:41 -0800 | [diff] [blame] | 92 | |
| 93 | protected: |
| 94 | Config config_; |
Peter Qiu | fb39ba4 | 2014-11-21 09:09:59 -0800 | [diff] [blame] | 95 | MockManager manager_; |
Peter Qiu | 8e785b9 | 2014-11-24 10:01:08 -0800 | [diff] [blame^] | 96 | scoped_refptr<MockDevice> device_; |
Peter Qiu | f073173 | 2014-11-11 09:46:41 -0800 | [diff] [blame] | 97 | }; |
| 98 | |
Peter Qiu | 376e404 | 2014-11-13 09:40:28 -0800 | [diff] [blame] | 99 | MATCHER_P(IsConfigErrorStartingWith, message, "") { |
Peter Qiu | f073173 | 2014-11-11 09:46:41 -0800 | [diff] [blame] | 100 | return arg != nullptr && |
| 101 | arg->GetDomain() == chromeos::errors::dbus::kDomain && |
| 102 | arg->GetCode() == kConfigError && |
Peter Qiu | 376e404 | 2014-11-13 09:40:28 -0800 | [diff] [blame] | 103 | StartsWithASCII(arg->GetMessage(), message, false); |
Peter Qiu | f073173 | 2014-11-11 09:46:41 -0800 | [diff] [blame] | 104 | } |
| 105 | |
Peter Qiu | 8e785b9 | 2014-11-24 10:01:08 -0800 | [diff] [blame^] | 106 | TEST_F(ConfigTest, GetFrequencyFromChannel) { |
| 107 | uint32_t frequency; |
| 108 | // Invalid channel. |
| 109 | EXPECT_FALSE(Config::GetFrequencyFromChannel(0, &frequency)); |
| 110 | EXPECT_FALSE(Config::GetFrequencyFromChannel(166, &frequency)); |
| 111 | EXPECT_FALSE(Config::GetFrequencyFromChannel(14, &frequency)); |
| 112 | EXPECT_FALSE(Config::GetFrequencyFromChannel(33, &frequency)); |
| 113 | |
| 114 | // Valid channel. |
| 115 | const uint32_t kChannel1Frequency = 2412; |
| 116 | const uint32_t kChannel13Frequency = 2472; |
| 117 | const uint32_t kChannel34Frequency = 5170; |
| 118 | const uint32_t kChannel165Frequency = 5825; |
| 119 | EXPECT_TRUE(Config::GetFrequencyFromChannel(1, &frequency)); |
| 120 | EXPECT_EQ(kChannel1Frequency, frequency); |
| 121 | EXPECT_TRUE(Config::GetFrequencyFromChannel(13, &frequency)); |
| 122 | EXPECT_EQ(kChannel13Frequency, frequency); |
| 123 | EXPECT_TRUE(Config::GetFrequencyFromChannel(34, &frequency)); |
| 124 | EXPECT_EQ(kChannel34Frequency, frequency); |
| 125 | EXPECT_TRUE(Config::GetFrequencyFromChannel(165, &frequency)); |
| 126 | EXPECT_EQ(kChannel165Frequency, frequency); |
| 127 | } |
| 128 | |
Peter Qiu | f073173 | 2014-11-11 09:46:41 -0800 | [diff] [blame] | 129 | TEST_F(ConfigTest, NoSsid) { |
| 130 | config_.SetChannel(k24GHzChannel); |
| 131 | config_.SetHwMode(kHwMode80211g); |
| 132 | config_.SetInterfaceName(kInterface); |
| 133 | |
| 134 | std::string config_content; |
| 135 | chromeos::ErrorPtr error; |
| 136 | EXPECT_FALSE(config_.GenerateConfigFile(&error, &config_content)); |
Peter Qiu | 376e404 | 2014-11-13 09:40:28 -0800 | [diff] [blame] | 137 | EXPECT_THAT(error, IsConfigErrorStartingWith("SSID not specified")); |
Peter Qiu | f073173 | 2014-11-11 09:46:41 -0800 | [diff] [blame] | 138 | } |
| 139 | |
Peter Qiu | fb39ba4 | 2014-11-21 09:09:59 -0800 | [diff] [blame] | 140 | TEST_F(ConfigTest, NoInterface) { |
| 141 | // Basic 80211.g configuration. |
| 142 | config_.SetSsid(kSsid); |
| 143 | config_.SetChannel(k24GHzChannel); |
| 144 | config_.SetHwMode(kHwMode80211g); |
| 145 | |
| 146 | // No device available, fail to generate config file. |
| 147 | chromeos::ErrorPtr error; |
| 148 | std::string config_content; |
| 149 | EXPECT_CALL(manager_, GetAvailableDevice()).WillOnce(Return(nullptr)); |
| 150 | EXPECT_FALSE(config_.GenerateConfigFile(&error, &config_content)); |
| 151 | EXPECT_THAT(error, IsConfigErrorStartingWith("No device available")); |
| 152 | Mock::VerifyAndClearExpectations(&manager_); |
| 153 | |
| 154 | // Device available, config file should be generated without any problem. |
| 155 | scoped_refptr<MockDevice> device = new MockDevice(); |
| 156 | device->SetPreferredApInterface(kInterface); |
| 157 | chromeos::ErrorPtr error1; |
| 158 | EXPECT_CALL(manager_, GetAvailableDevice()).WillOnce(Return(device)); |
| 159 | EXPECT_TRUE(config_.GenerateConfigFile(&error1, &config_content)); |
| 160 | EXPECT_NE(std::string::npos, config_content.find( |
| 161 | kExpected80211gConfigContent)) |
| 162 | << "Expected to find the following config...\n" |
| 163 | << kExpected80211gConfigContent << "..within content...\n" |
| 164 | << config_content; |
| 165 | EXPECT_EQ(nullptr, error1.get()); |
| 166 | Mock::VerifyAndClearExpectations(&manager_); |
| 167 | } |
| 168 | |
Peter Qiu | f073173 | 2014-11-11 09:46:41 -0800 | [diff] [blame] | 169 | TEST_F(ConfigTest, 80211gConfig) { |
| 170 | config_.SetSsid(kSsid); |
| 171 | config_.SetChannel(k24GHzChannel); |
| 172 | config_.SetHwMode(kHwMode80211g); |
| 173 | config_.SetInterfaceName(kInterface); |
| 174 | |
Peter Qiu | fb39ba4 | 2014-11-21 09:09:59 -0800 | [diff] [blame] | 175 | // Setup mock device. |
| 176 | SetupDevice(kInterface); |
| 177 | |
Peter Qiu | f073173 | 2014-11-11 09:46:41 -0800 | [diff] [blame] | 178 | std::string config_content; |
| 179 | chromeos::ErrorPtr error; |
| 180 | EXPECT_TRUE(config_.GenerateConfigFile(&error, &config_content)); |
| 181 | EXPECT_NE(std::string::npos, config_content.find( |
| 182 | kExpected80211gConfigContent)) |
| 183 | << "Expected to find the following config...\n" |
| 184 | << kExpected80211gConfigContent << "..within content...\n" |
| 185 | << config_content; |
| 186 | EXPECT_EQ(nullptr, error.get()); |
| 187 | } |
| 188 | |
| 189 | TEST_F(ConfigTest, 80211nConfig) { |
| 190 | config_.SetSsid(kSsid); |
| 191 | config_.SetHwMode(kHwMode80211n); |
| 192 | config_.SetInterfaceName(kInterface); |
| 193 | |
Peter Qiu | fb39ba4 | 2014-11-21 09:09:59 -0800 | [diff] [blame] | 194 | // Setup mock device. |
| 195 | SetupDevice(kInterface); |
| 196 | |
Peter Qiu | f073173 | 2014-11-11 09:46:41 -0800 | [diff] [blame] | 197 | // 5GHz channel. |
| 198 | config_.SetChannel(k5GHzChannel); |
| 199 | std::string ghz5_config_content; |
| 200 | chromeos::ErrorPtr error; |
Peter Qiu | 8e785b9 | 2014-11-24 10:01:08 -0800 | [diff] [blame^] | 201 | std::string ht_capab_5ghz(k5GHzHTCapab); |
| 202 | EXPECT_CALL(*device_.get(), GetHTCapability(k5GHzChannel, _)) |
| 203 | .WillOnce(DoAll(SetArgumentPointee<1>(ht_capab_5ghz), Return(true))); |
Peter Qiu | f073173 | 2014-11-11 09:46:41 -0800 | [diff] [blame] | 204 | EXPECT_TRUE(config_.GenerateConfigFile(&error, &ghz5_config_content)); |
| 205 | EXPECT_NE(std::string::npos, ghz5_config_content.find( |
| 206 | kExpected80211n5GHzConfigContent)) |
| 207 | << "Expected to find the following config...\n" |
| 208 | << kExpected80211n5GHzConfigContent << "..within content...\n" |
| 209 | << ghz5_config_content; |
| 210 | EXPECT_EQ(nullptr, error.get()); |
Peter Qiu | 8e785b9 | 2014-11-24 10:01:08 -0800 | [diff] [blame^] | 211 | Mock::VerifyAndClearExpectations(device_.get()); |
Peter Qiu | f073173 | 2014-11-11 09:46:41 -0800 | [diff] [blame] | 212 | |
| 213 | // 2.4GHz channel. |
| 214 | config_.SetChannel(k24GHzChannel); |
| 215 | std::string ghz24_config_content; |
| 216 | chromeos::ErrorPtr error1; |
Peter Qiu | 8e785b9 | 2014-11-24 10:01:08 -0800 | [diff] [blame^] | 217 | std::string ht_capab_24ghz(k24GHzHTCapab); |
| 218 | EXPECT_CALL(*device_.get(), GetHTCapability(k24GHzChannel, _)) |
| 219 | .WillOnce(DoAll(SetArgumentPointee<1>(ht_capab_24ghz), Return(true))); |
Peter Qiu | f073173 | 2014-11-11 09:46:41 -0800 | [diff] [blame] | 220 | EXPECT_TRUE(config_.GenerateConfigFile(&error1, &ghz24_config_content)); |
| 221 | EXPECT_NE(std::string::npos, ghz24_config_content.find( |
| 222 | kExpected80211n24GHzConfigContent)) |
| 223 | << "Expected to find the following config...\n" |
| 224 | << kExpected80211n24GHzConfigContent << "..within content...\n" |
| 225 | << ghz24_config_content; |
| 226 | EXPECT_EQ(nullptr, error.get()); |
Peter Qiu | 8e785b9 | 2014-11-24 10:01:08 -0800 | [diff] [blame^] | 227 | Mock::VerifyAndClearExpectations(device_.get()); |
Peter Qiu | f073173 | 2014-11-11 09:46:41 -0800 | [diff] [blame] | 228 | } |
| 229 | |
| 230 | TEST_F(ConfigTest, RsnConfig) { |
| 231 | config_.SetSsid(kSsid); |
| 232 | config_.SetChannel(k24GHzChannel); |
| 233 | config_.SetHwMode(kHwMode80211g); |
| 234 | config_.SetInterfaceName(kInterface); |
| 235 | config_.SetSecurityMode(kSecurityModeRSN); |
| 236 | |
Peter Qiu | fb39ba4 | 2014-11-21 09:09:59 -0800 | [diff] [blame] | 237 | // Setup mock device. |
| 238 | SetupDevice(kInterface); |
| 239 | |
Peter Qiu | f073173 | 2014-11-11 09:46:41 -0800 | [diff] [blame] | 240 | // Failed due to no passphrase specified. |
| 241 | std::string config_content; |
| 242 | chromeos::ErrorPtr error; |
| 243 | EXPECT_FALSE(config_.GenerateConfigFile(&error, &config_content)); |
Peter Qiu | 376e404 | 2014-11-13 09:40:28 -0800 | [diff] [blame] | 244 | EXPECT_THAT(error, IsConfigErrorStartingWith( |
Peter Qiu | f073173 | 2014-11-11 09:46:41 -0800 | [diff] [blame] | 245 | base::StringPrintf("Passphrase not set for security mode: %s", |
| 246 | kSecurityModeRSN))); |
| 247 | |
| 248 | chromeos::ErrorPtr error1; |
| 249 | config_.SetPassphrase(kPassphrase); |
| 250 | EXPECT_TRUE(config_.GenerateConfigFile(&error1, &config_content)); |
| 251 | EXPECT_NE(std::string::npos, config_content.find( |
| 252 | kExpectedRsnConfigContent)) |
| 253 | << "Expected to find the following config...\n" |
| 254 | << kExpectedRsnConfigContent << "..within content...\n" |
| 255 | << config_content; |
| 256 | EXPECT_EQ(nullptr, error1.get()); |
| 257 | } |
| 258 | |
| 259 | } // namespace apmanager |