blob: 59516af0c9eab6290f730abd922237ba36b457e5 [file] [log] [blame]
Peter Qiu326b6cf2015-09-02 11:11:42 -07001//
2// Copyright (C) 2014 The Android Open Source Project
3//
4// Licensed under the Apache License, Version 2.0 (the "License");
5// you may not use this file except in compliance with the License.
6// You may obtain a copy of the License at
7//
8// http://www.apache.org/licenses/LICENSE-2.0
9//
10// Unless required by applicable law or agreed to in writing, software
11// distributed under the License is distributed on an "AS IS" BASIS,
12// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13// See the License for the specific language governing permissions and
14// limitations under the License.
15//
Peter Qiuf0731732014-11-11 09:46:41 -080016
17#include "apmanager/config.h"
18
Peter Qiufb39ba42014-11-21 09:09:59 -080019#include <string>
20
Peter Qiuf0731732014-11-11 09:46:41 -080021#include <base/strings/string_util.h>
22#include <base/strings/stringprintf.h>
Peter Qiuf0731732014-11-11 09:46:41 -080023#include <gmock/gmock.h>
24#include <gtest/gtest.h>
25
Peter Qiu7a420d32015-09-22 11:25:15 -070026#if !defined(__ANDROID__)
27#include <chromeos/dbus/service_constants.h>
28#else
29#include "dbus/apmanager/dbus-constants.h"
30#endif
31
Peter Qiud9c79aa2015-11-19 15:36:00 -080032#include "apmanager/error.h"
33#include "apmanager/fake_config_adaptor.h"
Peter Qiuf9335402015-11-16 12:09:16 -080034#include "apmanager/fake_device_adaptor.h"
35#include "apmanager/mock_control.h"
Peter Qiufb39ba42014-11-21 09:09:59 -080036#include "apmanager/mock_device.h"
37#include "apmanager/mock_manager.h"
38
Peter Qiu8e785b92014-11-24 10:01:08 -080039using ::testing::_;
Peter Qiufb39ba42014-11-21 09:09:59 -080040using ::testing::Mock;
41using ::testing::Return;
Peter Qiuf9335402015-11-16 12:09:16 -080042using ::testing::ReturnNew;
Peter Qiu8e785b92014-11-24 10:01:08 -080043using ::testing::SetArgumentPointee;
Peter Qiuf0731732014-11-11 09:46:41 -080044namespace apmanager {
45
46namespace {
47
Peter Qiuf0731732014-11-11 09:46:41 -080048const char kSsid[] = "TestSsid";
49const char kInterface[] = "uap0";
Peter Qiu68303292014-12-10 10:42:13 -080050const char kBridgeInterface[] = "br0";
Peter Qiubfd410e2015-01-09 15:14:20 -080051const char kControlInterfacePath[] = "/var/run/apmanager/hostapd/ctrl_iface";
Peter Qiuf0731732014-11-11 09:46:41 -080052const char kPassphrase[] = "Passphrase";
Peter Qiu8e785b92014-11-24 10:01:08 -080053const char k24GHzHTCapab[] = "[LDPC SMPS-STATIC GF SHORT-GI-20]";
54const char k5GHzHTCapab[] =
55 "[LDPC HT40+ SMPS-STATIC GF SHORT-GI-20 SHORT-GI-40]";
56
Peter Qiuf0731732014-11-11 09:46:41 -080057const uint16_t k24GHzChannel = 6;
58const uint16_t k5GHzChannel = 36;
59
60const char kExpected80211gConfigContent[] = "ssid=TestSsid\n"
61 "channel=6\n"
Peter Qiuf0731732014-11-11 09:46:41 -080062 "interface=uap0\n"
Peter Qiu8e785b92014-11-24 10:01:08 -080063 "hw_mode=g\n"
Peter Qiuf0731732014-11-11 09:46:41 -080064 "driver=nl80211\n"
65 "fragm_threshold=2346\n"
66 "rts_threshold=2347\n";
67
Peter Qiu68303292014-12-10 10:42:13 -080068const char kExpected80211gBridgeConfigContent[] = "ssid=TestSsid\n"
69 "bridge=br0\n"
70 "channel=6\n"
71 "interface=uap0\n"
72 "hw_mode=g\n"
73 "driver=nl80211\n"
74 "fragm_threshold=2346\n"
75 "rts_threshold=2347\n";
76
Peter Qiubfd410e2015-01-09 15:14:20 -080077const char kExpected80211gCtrlIfaceConfigContent[] =
78 "ssid=TestSsid\n"
79 "channel=6\n"
80 "interface=uap0\n"
81 "hw_mode=g\n"
82 "ctrl_interface=/var/run/apmanager/hostapd/ctrl_iface\n"
Peter Qiu015a4992015-10-06 13:30:32 -070083#if !defined(__ANDROID__)
Peter Qiubfd410e2015-01-09 15:14:20 -080084 "ctrl_interface_group=apmanager\n"
Peter Qiu015a4992015-10-06 13:30:32 -070085#else
86 "ctrl_interface_group=system\n"
87#endif // __ANDROID__
Peter Qiubfd410e2015-01-09 15:14:20 -080088 "driver=nl80211\n"
89 "fragm_threshold=2346\n"
90 "rts_threshold=2347\n";
91
Peter Qiu8e785b92014-11-24 10:01:08 -080092const char kExpected80211n5GHzConfigContent[] =
93 "ssid=TestSsid\n"
94 "channel=36\n"
95 "interface=uap0\n"
96 "ieee80211n=1\n"
97 "ht_capab=[LDPC HT40+ SMPS-STATIC GF SHORT-GI-20 SHORT-GI-40]\n"
98 "hw_mode=a\n"
99 "driver=nl80211\n"
100 "fragm_threshold=2346\n"
101 "rts_threshold=2347\n";
Peter Qiuf0731732014-11-11 09:46:41 -0800102
Peter Qiu8e785b92014-11-24 10:01:08 -0800103const char kExpected80211n24GHzConfigContent[] =
104 "ssid=TestSsid\n"
105 "channel=6\n"
106 "interface=uap0\n"
107 "ieee80211n=1\n"
108 "ht_capab=[LDPC SMPS-STATIC GF SHORT-GI-20]\n"
109 "hw_mode=g\n"
110 "driver=nl80211\n"
111 "fragm_threshold=2346\n"
112 "rts_threshold=2347\n";
Peter Qiuf0731732014-11-11 09:46:41 -0800113
114const char kExpectedRsnConfigContent[] = "ssid=TestSsid\n"
115 "channel=6\n"
Peter Qiuf0731732014-11-11 09:46:41 -0800116 "interface=uap0\n"
Peter Qiu8e785b92014-11-24 10:01:08 -0800117 "hw_mode=g\n"
Peter Qiuf0731732014-11-11 09:46:41 -0800118 "wpa=2\n"
119 "rsn_pairwise=CCMP\n"
120 "wpa_key_mgmt=WPA-PSK\n"
121 "wpa_passphrase=Passphrase\n"
122 "driver=nl80211\n"
123 "fragm_threshold=2346\n"
124 "rts_threshold=2347\n";
125
126} // namespace
127
128class ConfigTest : public testing::Test {
129 public:
Peter Qiuf9335402015-11-16 12:09:16 -0800130 ConfigTest()
Peter Qiud9c79aa2015-11-19 15:36:00 -0800131 : manager_(&control_interface_) {
Peter Qiuf9335402015-11-16 12:09:16 -0800132 ON_CALL(control_interface_, CreateDeviceAdaptorRaw())
133 .WillByDefault(ReturnNew<FakeDeviceAdaptor>());
Peter Qiud9c79aa2015-11-19 15:36:00 -0800134 ON_CALL(control_interface_, CreateConfigAdaptorRaw())
135 .WillByDefault(ReturnNew<FakeConfigAdaptor>());
136 // Defer creation of Config object to allow ControlInterface to setup
137 // expectations for generating fake adaptors.
138 config_.reset(new Config(&manager_, 0));
Peter Qiuf9335402015-11-16 12:09:16 -0800139 }
Peter Qiufb39ba42014-11-21 09:09:59 -0800140
141 void SetupDevice(const std::string& interface) {
142 // Setup mock device.
Peter Qiuf9335402015-11-16 12:09:16 -0800143 device_ = new MockDevice(&manager_);
Peter Qiu8e785b92014-11-24 10:01:08 -0800144 device_->SetPreferredApInterface(interface);
Peter Qiufb39ba42014-11-21 09:09:59 -0800145 EXPECT_CALL(manager_, GetDeviceFromInterfaceName(interface))
Peter Qiu8e785b92014-11-24 10:01:08 -0800146 .WillRepeatedly(Return(device_));
Peter Qiufb39ba42014-11-21 09:09:59 -0800147 }
Peter Qiuf0731732014-11-11 09:46:41 -0800148
Peter Qiud9c79aa2015-11-19 15:36:00 -0800149 void VerifyError(const Error& error,
150 Error::Type expected_type,
151 const std::string& expected_message_start) {
152 EXPECT_EQ(expected_type, error.type());
Alex Vakulenkofca478e2016-01-20 07:53:12 -0800153 EXPECT_TRUE(base::StartsWith(error.message(), expected_message_start,
154 base::CompareCase::INSENSITIVE_ASCII));
Peter Qiud9c79aa2015-11-19 15:36:00 -0800155 }
156
Peter Qiuf0731732014-11-11 09:46:41 -0800157 protected:
Peter Qiuf9335402015-11-16 12:09:16 -0800158 MockControl control_interface_;
Peter Qiufb39ba42014-11-21 09:09:59 -0800159 MockManager manager_;
Peter Qiu8e785b92014-11-24 10:01:08 -0800160 scoped_refptr<MockDevice> device_;
Peter Qiud9c79aa2015-11-19 15:36:00 -0800161 std::unique_ptr<Config> config_;
Peter Qiuf0731732014-11-11 09:46:41 -0800162};
163
Peter Qiu8e785b92014-11-24 10:01:08 -0800164TEST_F(ConfigTest, GetFrequencyFromChannel) {
165 uint32_t frequency;
166 // Invalid channel.
167 EXPECT_FALSE(Config::GetFrequencyFromChannel(0, &frequency));
168 EXPECT_FALSE(Config::GetFrequencyFromChannel(166, &frequency));
169 EXPECT_FALSE(Config::GetFrequencyFromChannel(14, &frequency));
170 EXPECT_FALSE(Config::GetFrequencyFromChannel(33, &frequency));
171
172 // Valid channel.
173 const uint32_t kChannel1Frequency = 2412;
174 const uint32_t kChannel13Frequency = 2472;
175 const uint32_t kChannel34Frequency = 5170;
176 const uint32_t kChannel165Frequency = 5825;
177 EXPECT_TRUE(Config::GetFrequencyFromChannel(1, &frequency));
178 EXPECT_EQ(kChannel1Frequency, frequency);
179 EXPECT_TRUE(Config::GetFrequencyFromChannel(13, &frequency));
180 EXPECT_EQ(kChannel13Frequency, frequency);
181 EXPECT_TRUE(Config::GetFrequencyFromChannel(34, &frequency));
182 EXPECT_EQ(kChannel34Frequency, frequency);
183 EXPECT_TRUE(Config::GetFrequencyFromChannel(165, &frequency));
184 EXPECT_EQ(kChannel165Frequency, frequency);
185}
186
Peter Qiu68303292014-12-10 10:42:13 -0800187TEST_F(ConfigTest, ValidateSsid) {
Peter Qiud9c79aa2015-11-19 15:36:00 -0800188 Error error;
Peter Qiu68303292014-12-10 10:42:13 -0800189 // SSID must contain between 1 and 32 characters.
Peter Qiud9c79aa2015-11-19 15:36:00 -0800190 EXPECT_TRUE(config_->ValidateSsid(&error, "s"));
191 EXPECT_TRUE(config_->ValidateSsid(&error, std::string(32, 'c')));
192 EXPECT_FALSE(config_->ValidateSsid(&error, ""));
193 EXPECT_FALSE(config_->ValidateSsid(&error, std::string(33, 'c')));
Peter Qiu68303292014-12-10 10:42:13 -0800194}
195
196TEST_F(ConfigTest, ValidateSecurityMode) {
Peter Qiud9c79aa2015-11-19 15:36:00 -0800197 Error error;
198 EXPECT_TRUE(config_->ValidateSecurityMode(&error, kSecurityModeNone));
199 EXPECT_TRUE(config_->ValidateSecurityMode(&error, kSecurityModeRSN));
200 EXPECT_FALSE(config_->ValidateSecurityMode(&error, "InvalidSecurityMode"));
Peter Qiu68303292014-12-10 10:42:13 -0800201}
202
203TEST_F(ConfigTest, ValidatePassphrase) {
Peter Qiud9c79aa2015-11-19 15:36:00 -0800204 Error error;
Peter Qiu68303292014-12-10 10:42:13 -0800205 // Passpharse must contain between 8 and 63 characters.
Peter Qiud9c79aa2015-11-19 15:36:00 -0800206 EXPECT_TRUE(config_->ValidatePassphrase(&error, std::string(8, 'c')));
207 EXPECT_TRUE(config_->ValidatePassphrase(&error, std::string(63, 'c')));
208 EXPECT_FALSE(config_->ValidatePassphrase(&error, std::string(7, 'c')));
209 EXPECT_FALSE(config_->ValidatePassphrase(&error, std::string(64, 'c')));
Peter Qiu68303292014-12-10 10:42:13 -0800210}
211
212TEST_F(ConfigTest, ValidateHwMode) {
Peter Qiud9c79aa2015-11-19 15:36:00 -0800213 Error error;
214 EXPECT_TRUE(config_->ValidateHwMode(&error, kHwMode80211a));
215 EXPECT_TRUE(config_->ValidateHwMode(&error, kHwMode80211b));
216 EXPECT_TRUE(config_->ValidateHwMode(&error, kHwMode80211g));
217 EXPECT_TRUE(config_->ValidateHwMode(&error, kHwMode80211n));
218 EXPECT_TRUE(config_->ValidateHwMode(&error, kHwMode80211ac));
219 EXPECT_FALSE(config_->ValidateSecurityMode(&error, "InvalidHwMode"));
Peter Qiu68303292014-12-10 10:42:13 -0800220}
221
222TEST_F(ConfigTest, ValidateOperationMode) {
Peter Qiud9c79aa2015-11-19 15:36:00 -0800223 Error error;
224 EXPECT_TRUE(config_->ValidateOperationMode(&error, kOperationModeServer));
225 EXPECT_TRUE(config_->ValidateOperationMode(&error, kOperationModeBridge));
226 EXPECT_FALSE(config_->ValidateOperationMode(&error, "InvalidMode"));
Peter Qiu68303292014-12-10 10:42:13 -0800227}
228
229TEST_F(ConfigTest, ValidateChannel) {
Peter Qiud9c79aa2015-11-19 15:36:00 -0800230 Error error;
231 EXPECT_TRUE(config_->ValidateChannel(&error, 1));
232 EXPECT_TRUE(config_->ValidateChannel(&error, 13));
233 EXPECT_TRUE(config_->ValidateChannel(&error, 34));
234 EXPECT_TRUE(config_->ValidateChannel(&error, 165));
235 EXPECT_FALSE(config_->ValidateChannel(&error, 0));
236 EXPECT_FALSE(config_->ValidateChannel(&error, 14));
237 EXPECT_FALSE(config_->ValidateChannel(&error, 33));
238 EXPECT_FALSE(config_->ValidateChannel(&error, 166));
Peter Qiu68303292014-12-10 10:42:13 -0800239}
240
Peter Qiuf0731732014-11-11 09:46:41 -0800241TEST_F(ConfigTest, NoSsid) {
Peter Qiud9c79aa2015-11-19 15:36:00 -0800242 config_->SetChannel(k24GHzChannel);
243 config_->SetHwMode(kHwMode80211g);
244 config_->SetInterfaceName(kInterface);
Peter Qiuf0731732014-11-11 09:46:41 -0800245
246 std::string config_content;
Peter Qiud9c79aa2015-11-19 15:36:00 -0800247 Error error;
248 EXPECT_FALSE(config_->GenerateConfigFile(&error, &config_content));
249 VerifyError(error, Error::kInvalidConfiguration, "SSID not specified");
Peter Qiuf0731732014-11-11 09:46:41 -0800250}
251
Peter Qiufb39ba42014-11-21 09:09:59 -0800252TEST_F(ConfigTest, NoInterface) {
253 // Basic 80211.g configuration.
Peter Qiud9c79aa2015-11-19 15:36:00 -0800254 config_->SetSsid(kSsid);
255 config_->SetChannel(k24GHzChannel);
256 config_->SetHwMode(kHwMode80211g);
Peter Qiufb39ba42014-11-21 09:09:59 -0800257
258 // No device available, fail to generate config file.
Peter Qiud9c79aa2015-11-19 15:36:00 -0800259 Error error;
Peter Qiufb39ba42014-11-21 09:09:59 -0800260 std::string config_content;
261 EXPECT_CALL(manager_, GetAvailableDevice()).WillOnce(Return(nullptr));
Peter Qiud9c79aa2015-11-19 15:36:00 -0800262 EXPECT_FALSE(config_->GenerateConfigFile(&error, &config_content));
263 VerifyError(error, Error::kInternalError, "No device available");
Peter Qiufb39ba42014-11-21 09:09:59 -0800264 Mock::VerifyAndClearExpectations(&manager_);
265
266 // Device available, config file should be generated without any problem.
Peter Qiuf9335402015-11-16 12:09:16 -0800267 scoped_refptr<MockDevice> device = new MockDevice(&manager_);
Peter Qiufb39ba42014-11-21 09:09:59 -0800268 device->SetPreferredApInterface(kInterface);
Peter Qiud9c79aa2015-11-19 15:36:00 -0800269 error.Reset();
Peter Qiufb39ba42014-11-21 09:09:59 -0800270 EXPECT_CALL(manager_, GetAvailableDevice()).WillOnce(Return(device));
Peter Qiud9c79aa2015-11-19 15:36:00 -0800271 EXPECT_TRUE(config_->GenerateConfigFile(&error, &config_content));
Peter Qiufb39ba42014-11-21 09:09:59 -0800272 EXPECT_NE(std::string::npos, config_content.find(
273 kExpected80211gConfigContent))
274 << "Expected to find the following config...\n"
275 << kExpected80211gConfigContent << "..within content...\n"
276 << config_content;
Peter Qiud9c79aa2015-11-19 15:36:00 -0800277 EXPECT_TRUE(error.IsSuccess());
Peter Qiufb39ba42014-11-21 09:09:59 -0800278 Mock::VerifyAndClearExpectations(&manager_);
279}
280
Peter Qiu68303292014-12-10 10:42:13 -0800281TEST_F(ConfigTest, InvalidInterface) {
282 // Basic 80211.g configuration.
Peter Qiud9c79aa2015-11-19 15:36:00 -0800283 config_->SetSsid(kSsid);
284 config_->SetChannel(k24GHzChannel);
285 config_->SetHwMode(kHwMode80211g);
286 config_->SetInterfaceName(kInterface);
Peter Qiu68303292014-12-10 10:42:13 -0800287
Peter Qiud9c79aa2015-11-19 15:36:00 -0800288 // Unable to find the device, fail to generate config file.
289 Error error;
Peter Qiu68303292014-12-10 10:42:13 -0800290 std::string config_content;
291 EXPECT_CALL(manager_, GetDeviceFromInterfaceName(kInterface))
292 .WillOnce(Return(nullptr));
Peter Qiud9c79aa2015-11-19 15:36:00 -0800293 EXPECT_FALSE(config_->GenerateConfigFile(&error, &config_content));
294 VerifyError(error,
295 Error::kInvalidConfiguration,
296 "Unable to find device for the specified interface");
Peter Qiu68303292014-12-10 10:42:13 -0800297 Mock::VerifyAndClearExpectations(&manager_);
298}
299
300TEST_F(ConfigTest, BridgeMode) {
Peter Qiud9c79aa2015-11-19 15:36:00 -0800301 config_->SetSsid(kSsid);
302 config_->SetChannel(k24GHzChannel);
303 config_->SetHwMode(kHwMode80211g);
304 config_->SetInterfaceName(kInterface);
305 config_->SetOperationMode(kOperationModeBridge);
Peter Qiu68303292014-12-10 10:42:13 -0800306
307 // Bridge interface required for bridge mode.
Peter Qiud9c79aa2015-11-19 15:36:00 -0800308 Error error;
Peter Qiu68303292014-12-10 10:42:13 -0800309 std::string config_content;
Peter Qiud9c79aa2015-11-19 15:36:00 -0800310 EXPECT_FALSE(config_->GenerateConfigFile(&error, &config_content));
311 VerifyError(
312 error, Error::kInvalidConfiguration, "Bridge interface not specified");
Peter Qiu68303292014-12-10 10:42:13 -0800313
314 // Set bridge interface, config file should be generated without error.
Peter Qiud9c79aa2015-11-19 15:36:00 -0800315 config_->SetBridgeInterface(kBridgeInterface);
Peter Qiu68303292014-12-10 10:42:13 -0800316 // Setup mock device.
317 SetupDevice(kInterface);
Peter Qiud9c79aa2015-11-19 15:36:00 -0800318 error.Reset();
Peter Qiu68303292014-12-10 10:42:13 -0800319 std::string config_content1;
Peter Qiud9c79aa2015-11-19 15:36:00 -0800320 EXPECT_TRUE(config_->GenerateConfigFile(&error, &config_content1));
Peter Qiu68303292014-12-10 10:42:13 -0800321 EXPECT_NE(std::string::npos, config_content1.find(
322 kExpected80211gBridgeConfigContent))
323 << "Expected to find the following config...\n"
324 << kExpected80211gBridgeConfigContent << "..within content...\n"
325 << config_content1;
Peter Qiud9c79aa2015-11-19 15:36:00 -0800326 EXPECT_TRUE(error.IsSuccess());
Peter Qiu68303292014-12-10 10:42:13 -0800327}
328
Peter Qiuf0731732014-11-11 09:46:41 -0800329TEST_F(ConfigTest, 80211gConfig) {
Peter Qiud9c79aa2015-11-19 15:36:00 -0800330 config_->SetSsid(kSsid);
331 config_->SetChannel(k24GHzChannel);
332 config_->SetHwMode(kHwMode80211g);
333 config_->SetInterfaceName(kInterface);
Peter Qiuf0731732014-11-11 09:46:41 -0800334
Peter Qiufb39ba42014-11-21 09:09:59 -0800335 // Setup mock device.
336 SetupDevice(kInterface);
337
Peter Qiuf0731732014-11-11 09:46:41 -0800338 std::string config_content;
Peter Qiud9c79aa2015-11-19 15:36:00 -0800339 Error error;
340 EXPECT_TRUE(config_->GenerateConfigFile(&error, &config_content));
Peter Qiuf0731732014-11-11 09:46:41 -0800341 EXPECT_NE(std::string::npos, config_content.find(
342 kExpected80211gConfigContent))
343 << "Expected to find the following config...\n"
344 << kExpected80211gConfigContent << "..within content...\n"
345 << config_content;
Peter Qiud9c79aa2015-11-19 15:36:00 -0800346 EXPECT_TRUE(error.IsSuccess());
Peter Qiuf0731732014-11-11 09:46:41 -0800347}
348
Peter Qiubfd410e2015-01-09 15:14:20 -0800349TEST_F(ConfigTest, 80211gConfigWithControlInterface) {
Peter Qiud9c79aa2015-11-19 15:36:00 -0800350 config_->SetSsid(kSsid);
351 config_->SetChannel(k24GHzChannel);
352 config_->SetHwMode(kHwMode80211g);
353 config_->SetInterfaceName(kInterface);
354 config_->set_control_interface(kControlInterfacePath);
Peter Qiubfd410e2015-01-09 15:14:20 -0800355
356 // Setup mock device.
357 SetupDevice(kInterface);
358
359 std::string config_content;
Peter Qiud9c79aa2015-11-19 15:36:00 -0800360 Error error;
361 EXPECT_TRUE(config_->GenerateConfigFile(&error, &config_content));
Peter Qiubfd410e2015-01-09 15:14:20 -0800362 EXPECT_NE(std::string::npos, config_content.find(
363 kExpected80211gCtrlIfaceConfigContent))
364 << "Expected to find the following config...\n"
365 << kExpected80211gCtrlIfaceConfigContent << "..within content...\n"
366 << config_content;
Peter Qiud9c79aa2015-11-19 15:36:00 -0800367 EXPECT_TRUE(error.IsSuccess());
Peter Qiubfd410e2015-01-09 15:14:20 -0800368}
369
Peter Qiuf0731732014-11-11 09:46:41 -0800370TEST_F(ConfigTest, 80211nConfig) {
Peter Qiud9c79aa2015-11-19 15:36:00 -0800371 config_->SetSsid(kSsid);
372 config_->SetHwMode(kHwMode80211n);
373 config_->SetInterfaceName(kInterface);
Peter Qiuf0731732014-11-11 09:46:41 -0800374
Peter Qiufb39ba42014-11-21 09:09:59 -0800375 // Setup mock device.
376 SetupDevice(kInterface);
377
Peter Qiuf0731732014-11-11 09:46:41 -0800378 // 5GHz channel.
Peter Qiud9c79aa2015-11-19 15:36:00 -0800379 config_->SetChannel(k5GHzChannel);
Peter Qiuf0731732014-11-11 09:46:41 -0800380 std::string ghz5_config_content;
Peter Qiud9c79aa2015-11-19 15:36:00 -0800381 Error error;
Peter Qiu8e785b92014-11-24 10:01:08 -0800382 std::string ht_capab_5ghz(k5GHzHTCapab);
383 EXPECT_CALL(*device_.get(), GetHTCapability(k5GHzChannel, _))
384 .WillOnce(DoAll(SetArgumentPointee<1>(ht_capab_5ghz), Return(true)));
Peter Qiud9c79aa2015-11-19 15:36:00 -0800385 EXPECT_TRUE(config_->GenerateConfigFile(&error, &ghz5_config_content));
Peter Qiuf0731732014-11-11 09:46:41 -0800386 EXPECT_NE(std::string::npos, ghz5_config_content.find(
387 kExpected80211n5GHzConfigContent))
388 << "Expected to find the following config...\n"
389 << kExpected80211n5GHzConfigContent << "..within content...\n"
390 << ghz5_config_content;
Peter Qiud9c79aa2015-11-19 15:36:00 -0800391 EXPECT_TRUE(error.IsSuccess());
Peter Qiu8e785b92014-11-24 10:01:08 -0800392 Mock::VerifyAndClearExpectations(device_.get());
Peter Qiuf0731732014-11-11 09:46:41 -0800393
394 // 2.4GHz channel.
Peter Qiud9c79aa2015-11-19 15:36:00 -0800395 config_->SetChannel(k24GHzChannel);
Peter Qiuf0731732014-11-11 09:46:41 -0800396 std::string ghz24_config_content;
Peter Qiud9c79aa2015-11-19 15:36:00 -0800397 error.Reset();
Peter Qiu8e785b92014-11-24 10:01:08 -0800398 std::string ht_capab_24ghz(k24GHzHTCapab);
399 EXPECT_CALL(*device_.get(), GetHTCapability(k24GHzChannel, _))
400 .WillOnce(DoAll(SetArgumentPointee<1>(ht_capab_24ghz), Return(true)));
Peter Qiud9c79aa2015-11-19 15:36:00 -0800401 EXPECT_TRUE(config_->GenerateConfigFile(&error, &ghz24_config_content));
Peter Qiuf0731732014-11-11 09:46:41 -0800402 EXPECT_NE(std::string::npos, ghz24_config_content.find(
403 kExpected80211n24GHzConfigContent))
404 << "Expected to find the following config...\n"
405 << kExpected80211n24GHzConfigContent << "..within content...\n"
406 << ghz24_config_content;
Peter Qiud9c79aa2015-11-19 15:36:00 -0800407 EXPECT_TRUE(error.IsSuccess());
Peter Qiu8e785b92014-11-24 10:01:08 -0800408 Mock::VerifyAndClearExpectations(device_.get());
Peter Qiuf0731732014-11-11 09:46:41 -0800409}
410
411TEST_F(ConfigTest, RsnConfig) {
Peter Qiud9c79aa2015-11-19 15:36:00 -0800412 config_->SetSsid(kSsid);
413 config_->SetChannel(k24GHzChannel);
414 config_->SetHwMode(kHwMode80211g);
415 config_->SetInterfaceName(kInterface);
416 config_->SetSecurityMode(kSecurityModeRSN);
Peter Qiuf0731732014-11-11 09:46:41 -0800417
Peter Qiufb39ba42014-11-21 09:09:59 -0800418 // Setup mock device.
419 SetupDevice(kInterface);
420
Peter Qiuf0731732014-11-11 09:46:41 -0800421 // Failed due to no passphrase specified.
422 std::string config_content;
Peter Qiud9c79aa2015-11-19 15:36:00 -0800423 Error error;
424 EXPECT_FALSE(config_->GenerateConfigFile(&error, &config_content));
425 VerifyError(
426 error,
427 Error::kInvalidConfiguration,
Peter Qiuf0731732014-11-11 09:46:41 -0800428 base::StringPrintf("Passphrase not set for security mode: %s",
Peter Qiud9c79aa2015-11-19 15:36:00 -0800429 kSecurityModeRSN));
Peter Qiuf0731732014-11-11 09:46:41 -0800430
Peter Qiud9c79aa2015-11-19 15:36:00 -0800431 error.Reset();
432 config_->SetPassphrase(kPassphrase);
433 EXPECT_TRUE(config_->GenerateConfigFile(&error, &config_content));
Peter Qiuf0731732014-11-11 09:46:41 -0800434 EXPECT_NE(std::string::npos, config_content.find(
435 kExpectedRsnConfigContent))
436 << "Expected to find the following config...\n"
437 << kExpectedRsnConfigContent << "..within content...\n"
438 << config_content;
Peter Qiud9c79aa2015-11-19 15:36:00 -0800439 EXPECT_TRUE(error.IsSuccess());
Peter Qiuf0731732014-11-11 09:46:41 -0800440}
441
442} // namespace apmanager