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