blob: 93db1794264b47083802f6b6119b282cc550ebac [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 Qiuf9335402015-11-16 12:09:16 -080032#include "apmanager/fake_device_adaptor.h"
33#include "apmanager/mock_control.h"
Peter Qiufb39ba42014-11-21 09:09:59 -080034#include "apmanager/mock_device.h"
35#include "apmanager/mock_manager.h"
36
Peter Qiu8e785b92014-11-24 10:01:08 -080037using ::testing::_;
Peter Qiufb39ba42014-11-21 09:09:59 -080038using ::testing::Mock;
39using ::testing::Return;
Peter Qiuf9335402015-11-16 12:09:16 -080040using ::testing::ReturnNew;
Peter Qiu8e785b92014-11-24 10:01:08 -080041using ::testing::SetArgumentPointee;
Peter Qiuf0731732014-11-11 09:46:41 -080042namespace apmanager {
43
44namespace {
45
46const char kServicePath[] = "/manager/services/0";
47const char kSsid[] = "TestSsid";
48const char kInterface[] = "uap0";
Peter Qiu68303292014-12-10 10:42:13 -080049const char kBridgeInterface[] = "br0";
Peter Qiubfd410e2015-01-09 15:14:20 -080050const char kControlInterfacePath[] = "/var/run/apmanager/hostapd/ctrl_iface";
Peter Qiuf0731732014-11-11 09:46:41 -080051const char kPassphrase[] = "Passphrase";
Peter Qiu8e785b92014-11-24 10:01:08 -080052const char k24GHzHTCapab[] = "[LDPC SMPS-STATIC GF SHORT-GI-20]";
53const char k5GHzHTCapab[] =
54 "[LDPC HT40+ SMPS-STATIC GF SHORT-GI-20 SHORT-GI-40]";
55
Peter Qiuf0731732014-11-11 09:46:41 -080056const uint16_t k24GHzChannel = 6;
57const uint16_t k5GHzChannel = 36;
58
59const char kExpected80211gConfigContent[] = "ssid=TestSsid\n"
60 "channel=6\n"
Peter Qiuf0731732014-11-11 09:46:41 -080061 "interface=uap0\n"
Peter Qiu8e785b92014-11-24 10:01:08 -080062 "hw_mode=g\n"
Peter Qiuf0731732014-11-11 09:46:41 -080063 "driver=nl80211\n"
64 "fragm_threshold=2346\n"
65 "rts_threshold=2347\n";
66
Peter Qiu68303292014-12-10 10:42:13 -080067const char kExpected80211gBridgeConfigContent[] = "ssid=TestSsid\n"
68 "bridge=br0\n"
69 "channel=6\n"
70 "interface=uap0\n"
71 "hw_mode=g\n"
72 "driver=nl80211\n"
73 "fragm_threshold=2346\n"
74 "rts_threshold=2347\n";
75
Peter Qiubfd410e2015-01-09 15:14:20 -080076const char kExpected80211gCtrlIfaceConfigContent[] =
77 "ssid=TestSsid\n"
78 "channel=6\n"
79 "interface=uap0\n"
80 "hw_mode=g\n"
81 "ctrl_interface=/var/run/apmanager/hostapd/ctrl_iface\n"
Peter Qiu015a4992015-10-06 13:30:32 -070082#if !defined(__ANDROID__)
Peter Qiubfd410e2015-01-09 15:14:20 -080083 "ctrl_interface_group=apmanager\n"
Peter Qiu015a4992015-10-06 13:30:32 -070084#else
85 "ctrl_interface_group=system\n"
86#endif // __ANDROID__
Peter Qiubfd410e2015-01-09 15:14:20 -080087 "driver=nl80211\n"
88 "fragm_threshold=2346\n"
89 "rts_threshold=2347\n";
90
Peter Qiu8e785b92014-11-24 10:01:08 -080091const char kExpected80211n5GHzConfigContent[] =
92 "ssid=TestSsid\n"
93 "channel=36\n"
94 "interface=uap0\n"
95 "ieee80211n=1\n"
96 "ht_capab=[LDPC HT40+ SMPS-STATIC GF SHORT-GI-20 SHORT-GI-40]\n"
97 "hw_mode=a\n"
98 "driver=nl80211\n"
99 "fragm_threshold=2346\n"
100 "rts_threshold=2347\n";
Peter Qiuf0731732014-11-11 09:46:41 -0800101
Peter Qiu8e785b92014-11-24 10:01:08 -0800102const char kExpected80211n24GHzConfigContent[] =
103 "ssid=TestSsid\n"
104 "channel=6\n"
105 "interface=uap0\n"
106 "ieee80211n=1\n"
107 "ht_capab=[LDPC SMPS-STATIC GF SHORT-GI-20]\n"
108 "hw_mode=g\n"
109 "driver=nl80211\n"
110 "fragm_threshold=2346\n"
111 "rts_threshold=2347\n";
Peter Qiuf0731732014-11-11 09:46:41 -0800112
113const char kExpectedRsnConfigContent[] = "ssid=TestSsid\n"
114 "channel=6\n"
Peter Qiuf0731732014-11-11 09:46:41 -0800115 "interface=uap0\n"
Peter Qiu8e785b92014-11-24 10:01:08 -0800116 "hw_mode=g\n"
Peter Qiuf0731732014-11-11 09:46:41 -0800117 "wpa=2\n"
118 "rsn_pairwise=CCMP\n"
119 "wpa_key_mgmt=WPA-PSK\n"
120 "wpa_passphrase=Passphrase\n"
121 "driver=nl80211\n"
122 "fragm_threshold=2346\n"
123 "rts_threshold=2347\n";
124
125} // namespace
126
127class ConfigTest : public testing::Test {
128 public:
Peter Qiuf9335402015-11-16 12:09:16 -0800129 ConfigTest()
130 : manager_(&control_interface_),
131 config_(&manager_, kServicePath) {
132 ON_CALL(control_interface_, CreateDeviceAdaptorRaw())
133 .WillByDefault(ReturnNew<FakeDeviceAdaptor>());
134 }
Peter Qiufb39ba42014-11-21 09:09:59 -0800135
136 void SetupDevice(const std::string& interface) {
137 // Setup mock device.
Peter Qiuf9335402015-11-16 12:09:16 -0800138 device_ = new MockDevice(&manager_);
Peter Qiu8e785b92014-11-24 10:01:08 -0800139 device_->SetPreferredApInterface(interface);
Peter Qiufb39ba42014-11-21 09:09:59 -0800140 EXPECT_CALL(manager_, GetDeviceFromInterfaceName(interface))
Peter Qiu8e785b92014-11-24 10:01:08 -0800141 .WillRepeatedly(Return(device_));
Peter Qiufb39ba42014-11-21 09:09:59 -0800142 }
Peter Qiuf0731732014-11-11 09:46:41 -0800143
144 protected:
Peter Qiuf9335402015-11-16 12:09:16 -0800145 MockControl control_interface_;
Peter Qiufb39ba42014-11-21 09:09:59 -0800146 MockManager manager_;
Peter Qiu8e785b92014-11-24 10:01:08 -0800147 scoped_refptr<MockDevice> device_;
Peter Qiuf9335402015-11-16 12:09:16 -0800148 Config config_;
Peter Qiuf0731732014-11-11 09:46:41 -0800149};
150
Peter Qiu376e4042014-11-13 09:40:28 -0800151MATCHER_P(IsConfigErrorStartingWith, message, "") {
Peter Qiuf0731732014-11-11 09:46:41 -0800152 return arg != nullptr &&
Alex Vakulenko8d0c31b2015-10-13 09:14:24 -0700153 arg->GetDomain() == brillo::errors::dbus::kDomain &&
Alex Vakulenkoe8f58982015-06-15 12:53:22 -0700154 arg->GetCode() == kConfigError &&
155 base::StartsWithASCII(arg->GetMessage(), message, false);
Peter Qiuf0731732014-11-11 09:46:41 -0800156}
157
Peter Qiu8e785b92014-11-24 10:01:08 -0800158TEST_F(ConfigTest, GetFrequencyFromChannel) {
159 uint32_t frequency;
160 // Invalid channel.
161 EXPECT_FALSE(Config::GetFrequencyFromChannel(0, &frequency));
162 EXPECT_FALSE(Config::GetFrequencyFromChannel(166, &frequency));
163 EXPECT_FALSE(Config::GetFrequencyFromChannel(14, &frequency));
164 EXPECT_FALSE(Config::GetFrequencyFromChannel(33, &frequency));
165
166 // Valid channel.
167 const uint32_t kChannel1Frequency = 2412;
168 const uint32_t kChannel13Frequency = 2472;
169 const uint32_t kChannel34Frequency = 5170;
170 const uint32_t kChannel165Frequency = 5825;
171 EXPECT_TRUE(Config::GetFrequencyFromChannel(1, &frequency));
172 EXPECT_EQ(kChannel1Frequency, frequency);
173 EXPECT_TRUE(Config::GetFrequencyFromChannel(13, &frequency));
174 EXPECT_EQ(kChannel13Frequency, frequency);
175 EXPECT_TRUE(Config::GetFrequencyFromChannel(34, &frequency));
176 EXPECT_EQ(kChannel34Frequency, frequency);
177 EXPECT_TRUE(Config::GetFrequencyFromChannel(165, &frequency));
178 EXPECT_EQ(kChannel165Frequency, frequency);
179}
180
Peter Qiu68303292014-12-10 10:42:13 -0800181TEST_F(ConfigTest, ValidateSsid) {
Alex Vakulenko8d0c31b2015-10-13 09:14:24 -0700182 brillo::ErrorPtr error;
Peter Qiu68303292014-12-10 10:42:13 -0800183 // SSID must contain between 1 and 32 characters.
184 EXPECT_TRUE(config_.ValidateSsid(&error, "s"));
185 EXPECT_TRUE(config_.ValidateSsid(&error, std::string(32, 'c')));
186 EXPECT_FALSE(config_.ValidateSsid(&error, ""));
187 EXPECT_FALSE(config_.ValidateSsid(&error, std::string(33, 'c')));
188}
189
190TEST_F(ConfigTest, ValidateSecurityMode) {
Alex Vakulenko8d0c31b2015-10-13 09:14:24 -0700191 brillo::ErrorPtr error;
Peter Qiu68303292014-12-10 10:42:13 -0800192 EXPECT_TRUE(config_.ValidateSecurityMode(&error, kSecurityModeNone));
193 EXPECT_TRUE(config_.ValidateSecurityMode(&error, kSecurityModeRSN));
194 EXPECT_FALSE(config_.ValidateSecurityMode(&error, "InvalidSecurityMode"));
195}
196
197TEST_F(ConfigTest, ValidatePassphrase) {
Alex Vakulenko8d0c31b2015-10-13 09:14:24 -0700198 brillo::ErrorPtr error;
Peter Qiu68303292014-12-10 10:42:13 -0800199 // Passpharse must contain between 8 and 63 characters.
200 EXPECT_TRUE(config_.ValidatePassphrase(&error, std::string(8, 'c')));
201 EXPECT_TRUE(config_.ValidatePassphrase(&error, std::string(63, 'c')));
202 EXPECT_FALSE(config_.ValidatePassphrase(&error, std::string(7, 'c')));
203 EXPECT_FALSE(config_.ValidatePassphrase(&error, std::string(64, 'c')));
204}
205
206TEST_F(ConfigTest, ValidateHwMode) {
Alex Vakulenko8d0c31b2015-10-13 09:14:24 -0700207 brillo::ErrorPtr error;
Peter Qiu68303292014-12-10 10:42:13 -0800208 EXPECT_TRUE(config_.ValidateHwMode(&error, kHwMode80211a));
209 EXPECT_TRUE(config_.ValidateHwMode(&error, kHwMode80211b));
210 EXPECT_TRUE(config_.ValidateHwMode(&error, kHwMode80211g));
211 EXPECT_TRUE(config_.ValidateHwMode(&error, kHwMode80211n));
212 EXPECT_TRUE(config_.ValidateHwMode(&error, kHwMode80211ac));
213 EXPECT_FALSE(config_.ValidateSecurityMode(&error, "InvalidHwMode"));
214}
215
216TEST_F(ConfigTest, ValidateOperationMode) {
Alex Vakulenko8d0c31b2015-10-13 09:14:24 -0700217 brillo::ErrorPtr error;
Peter Qiu68303292014-12-10 10:42:13 -0800218 EXPECT_TRUE(config_.ValidateOperationMode(&error, kOperationModeServer));
219 EXPECT_TRUE(config_.ValidateOperationMode(&error, kOperationModeBridge));
220 EXPECT_FALSE(config_.ValidateOperationMode(&error, "InvalidMode"));
221}
222
223TEST_F(ConfigTest, ValidateChannel) {
Alex Vakulenko8d0c31b2015-10-13 09:14:24 -0700224 brillo::ErrorPtr error;
Peter Qiu68303292014-12-10 10:42:13 -0800225 EXPECT_TRUE(config_.ValidateChannel(&error, 1));
226 EXPECT_TRUE(config_.ValidateChannel(&error, 13));
227 EXPECT_TRUE(config_.ValidateChannel(&error, 34));
228 EXPECT_TRUE(config_.ValidateChannel(&error, 165));
229 EXPECT_FALSE(config_.ValidateChannel(&error, 0));
230 EXPECT_FALSE(config_.ValidateChannel(&error, 14));
231 EXPECT_FALSE(config_.ValidateChannel(&error, 33));
232 EXPECT_FALSE(config_.ValidateChannel(&error, 166));
233}
234
Peter Qiuf0731732014-11-11 09:46:41 -0800235TEST_F(ConfigTest, NoSsid) {
236 config_.SetChannel(k24GHzChannel);
237 config_.SetHwMode(kHwMode80211g);
238 config_.SetInterfaceName(kInterface);
239
240 std::string config_content;
Alex Vakulenko8d0c31b2015-10-13 09:14:24 -0700241 brillo::ErrorPtr error;
Peter Qiuf0731732014-11-11 09:46:41 -0800242 EXPECT_FALSE(config_.GenerateConfigFile(&error, &config_content));
Peter Qiu376e4042014-11-13 09:40:28 -0800243 EXPECT_THAT(error, IsConfigErrorStartingWith("SSID not specified"));
Peter Qiuf0731732014-11-11 09:46:41 -0800244}
245
Peter Qiufb39ba42014-11-21 09:09:59 -0800246TEST_F(ConfigTest, NoInterface) {
247 // Basic 80211.g configuration.
248 config_.SetSsid(kSsid);
249 config_.SetChannel(k24GHzChannel);
250 config_.SetHwMode(kHwMode80211g);
251
252 // No device available, fail to generate config file.
Alex Vakulenko8d0c31b2015-10-13 09:14:24 -0700253 brillo::ErrorPtr error;
Peter Qiufb39ba42014-11-21 09:09:59 -0800254 std::string config_content;
255 EXPECT_CALL(manager_, GetAvailableDevice()).WillOnce(Return(nullptr));
256 EXPECT_FALSE(config_.GenerateConfigFile(&error, &config_content));
257 EXPECT_THAT(error, IsConfigErrorStartingWith("No device available"));
258 Mock::VerifyAndClearExpectations(&manager_);
259
260 // Device available, config file should be generated without any problem.
Peter Qiuf9335402015-11-16 12:09:16 -0800261 scoped_refptr<MockDevice> device = new MockDevice(&manager_);
Peter Qiufb39ba42014-11-21 09:09:59 -0800262 device->SetPreferredApInterface(kInterface);
Alex Vakulenko8d0c31b2015-10-13 09:14:24 -0700263 brillo::ErrorPtr error1;
Peter Qiufb39ba42014-11-21 09:09:59 -0800264 EXPECT_CALL(manager_, GetAvailableDevice()).WillOnce(Return(device));
265 EXPECT_TRUE(config_.GenerateConfigFile(&error1, &config_content));
266 EXPECT_NE(std::string::npos, config_content.find(
267 kExpected80211gConfigContent))
268 << "Expected to find the following config...\n"
269 << kExpected80211gConfigContent << "..within content...\n"
270 << config_content;
271 EXPECT_EQ(nullptr, error1.get());
272 Mock::VerifyAndClearExpectations(&manager_);
273}
274
Peter Qiu68303292014-12-10 10:42:13 -0800275TEST_F(ConfigTest, InvalidInterface) {
276 // Basic 80211.g configuration.
277 config_.SetSsid(kSsid);
278 config_.SetChannel(k24GHzChannel);
279 config_.SetHwMode(kHwMode80211g);
280 config_.SetInterfaceName(kInterface);
281
282 // No device available, fail to generate config file.
Alex Vakulenko8d0c31b2015-10-13 09:14:24 -0700283 brillo::ErrorPtr error;
Peter Qiu68303292014-12-10 10:42:13 -0800284 std::string config_content;
285 EXPECT_CALL(manager_, GetDeviceFromInterfaceName(kInterface))
286 .WillOnce(Return(nullptr));
287 EXPECT_FALSE(config_.GenerateConfigFile(&error, &config_content));
288 EXPECT_THAT(error,
289 IsConfigErrorStartingWith(
290 "Unable to find device for the specified interface"));
291 Mock::VerifyAndClearExpectations(&manager_);
292}
293
294TEST_F(ConfigTest, BridgeMode) {
295 config_.SetSsid(kSsid);
296 config_.SetChannel(k24GHzChannel);
297 config_.SetHwMode(kHwMode80211g);
298 config_.SetInterfaceName(kInterface);
299 config_.SetOperationMode(kOperationModeBridge);
300
301 // Bridge interface required for bridge mode.
Alex Vakulenko8d0c31b2015-10-13 09:14:24 -0700302 brillo::ErrorPtr error;
Peter Qiu68303292014-12-10 10:42:13 -0800303 std::string config_content;
304 EXPECT_FALSE(config_.GenerateConfigFile(&error, &config_content));
305 EXPECT_THAT(error,
306 IsConfigErrorStartingWith("Bridge interface not specified"));
307
308 // Set bridge interface, config file should be generated without error.
309 config_.SetBridgeInterface(kBridgeInterface);
310 // Setup mock device.
311 SetupDevice(kInterface);
Alex Vakulenko8d0c31b2015-10-13 09:14:24 -0700312 brillo::ErrorPtr error1;
Peter Qiu68303292014-12-10 10:42:13 -0800313 std::string config_content1;
314 EXPECT_TRUE(config_.GenerateConfigFile(&error1, &config_content1));
315 EXPECT_NE(std::string::npos, config_content1.find(
316 kExpected80211gBridgeConfigContent))
317 << "Expected to find the following config...\n"
318 << kExpected80211gBridgeConfigContent << "..within content...\n"
319 << config_content1;
320 EXPECT_EQ(nullptr, error1.get());
321}
322
Peter Qiuf0731732014-11-11 09:46:41 -0800323TEST_F(ConfigTest, 80211gConfig) {
324 config_.SetSsid(kSsid);
325 config_.SetChannel(k24GHzChannel);
326 config_.SetHwMode(kHwMode80211g);
327 config_.SetInterfaceName(kInterface);
328
Peter Qiufb39ba42014-11-21 09:09:59 -0800329 // Setup mock device.
330 SetupDevice(kInterface);
331
Peter Qiuf0731732014-11-11 09:46:41 -0800332 std::string config_content;
Alex Vakulenko8d0c31b2015-10-13 09:14:24 -0700333 brillo::ErrorPtr error;
Peter Qiuf0731732014-11-11 09:46:41 -0800334 EXPECT_TRUE(config_.GenerateConfigFile(&error, &config_content));
335 EXPECT_NE(std::string::npos, config_content.find(
336 kExpected80211gConfigContent))
337 << "Expected to find the following config...\n"
338 << kExpected80211gConfigContent << "..within content...\n"
339 << config_content;
340 EXPECT_EQ(nullptr, error.get());
341}
342
Peter Qiubfd410e2015-01-09 15:14:20 -0800343TEST_F(ConfigTest, 80211gConfigWithControlInterface) {
344 config_.SetSsid(kSsid);
345 config_.SetChannel(k24GHzChannel);
346 config_.SetHwMode(kHwMode80211g);
347 config_.SetInterfaceName(kInterface);
348 config_.set_control_interface(kControlInterfacePath);
349
350 // Setup mock device.
351 SetupDevice(kInterface);
352
353 std::string config_content;
Alex Vakulenko8d0c31b2015-10-13 09:14:24 -0700354 brillo::ErrorPtr error;
Peter Qiubfd410e2015-01-09 15:14:20 -0800355 EXPECT_TRUE(config_.GenerateConfigFile(&error, &config_content));
356 EXPECT_NE(std::string::npos, config_content.find(
357 kExpected80211gCtrlIfaceConfigContent))
358 << "Expected to find the following config...\n"
359 << kExpected80211gCtrlIfaceConfigContent << "..within content...\n"
360 << config_content;
361 EXPECT_EQ(nullptr, error.get());
362}
363
Peter Qiuf0731732014-11-11 09:46:41 -0800364TEST_F(ConfigTest, 80211nConfig) {
365 config_.SetSsid(kSsid);
366 config_.SetHwMode(kHwMode80211n);
367 config_.SetInterfaceName(kInterface);
368
Peter Qiufb39ba42014-11-21 09:09:59 -0800369 // Setup mock device.
370 SetupDevice(kInterface);
371
Peter Qiuf0731732014-11-11 09:46:41 -0800372 // 5GHz channel.
373 config_.SetChannel(k5GHzChannel);
374 std::string ghz5_config_content;
Alex Vakulenko8d0c31b2015-10-13 09:14:24 -0700375 brillo::ErrorPtr error;
Peter Qiu8e785b92014-11-24 10:01:08 -0800376 std::string ht_capab_5ghz(k5GHzHTCapab);
377 EXPECT_CALL(*device_.get(), GetHTCapability(k5GHzChannel, _))
378 .WillOnce(DoAll(SetArgumentPointee<1>(ht_capab_5ghz), Return(true)));
Peter Qiuf0731732014-11-11 09:46:41 -0800379 EXPECT_TRUE(config_.GenerateConfigFile(&error, &ghz5_config_content));
380 EXPECT_NE(std::string::npos, ghz5_config_content.find(
381 kExpected80211n5GHzConfigContent))
382 << "Expected to find the following config...\n"
383 << kExpected80211n5GHzConfigContent << "..within content...\n"
384 << ghz5_config_content;
385 EXPECT_EQ(nullptr, error.get());
Peter Qiu8e785b92014-11-24 10:01:08 -0800386 Mock::VerifyAndClearExpectations(device_.get());
Peter Qiuf0731732014-11-11 09:46:41 -0800387
388 // 2.4GHz channel.
389 config_.SetChannel(k24GHzChannel);
390 std::string ghz24_config_content;
Alex Vakulenko8d0c31b2015-10-13 09:14:24 -0700391 brillo::ErrorPtr error1;
Peter Qiu8e785b92014-11-24 10:01:08 -0800392 std::string ht_capab_24ghz(k24GHzHTCapab);
393 EXPECT_CALL(*device_.get(), GetHTCapability(k24GHzChannel, _))
394 .WillOnce(DoAll(SetArgumentPointee<1>(ht_capab_24ghz), Return(true)));
Peter Qiuf0731732014-11-11 09:46:41 -0800395 EXPECT_TRUE(config_.GenerateConfigFile(&error1, &ghz24_config_content));
396 EXPECT_NE(std::string::npos, ghz24_config_content.find(
397 kExpected80211n24GHzConfigContent))
398 << "Expected to find the following config...\n"
399 << kExpected80211n24GHzConfigContent << "..within content...\n"
400 << ghz24_config_content;
401 EXPECT_EQ(nullptr, error.get());
Peter Qiu8e785b92014-11-24 10:01:08 -0800402 Mock::VerifyAndClearExpectations(device_.get());
Peter Qiuf0731732014-11-11 09:46:41 -0800403}
404
405TEST_F(ConfigTest, RsnConfig) {
406 config_.SetSsid(kSsid);
407 config_.SetChannel(k24GHzChannel);
408 config_.SetHwMode(kHwMode80211g);
409 config_.SetInterfaceName(kInterface);
410 config_.SetSecurityMode(kSecurityModeRSN);
411
Peter Qiufb39ba42014-11-21 09:09:59 -0800412 // Setup mock device.
413 SetupDevice(kInterface);
414
Peter Qiuf0731732014-11-11 09:46:41 -0800415 // Failed due to no passphrase specified.
416 std::string config_content;
Alex Vakulenko8d0c31b2015-10-13 09:14:24 -0700417 brillo::ErrorPtr error;
Peter Qiuf0731732014-11-11 09:46:41 -0800418 EXPECT_FALSE(config_.GenerateConfigFile(&error, &config_content));
Peter Qiu376e4042014-11-13 09:40:28 -0800419 EXPECT_THAT(error, IsConfigErrorStartingWith(
Peter Qiuf0731732014-11-11 09:46:41 -0800420 base::StringPrintf("Passphrase not set for security mode: %s",
421 kSecurityModeRSN)));
422
Alex Vakulenko8d0c31b2015-10-13 09:14:24 -0700423 brillo::ErrorPtr error1;
Peter Qiuf0731732014-11-11 09:46:41 -0800424 config_.SetPassphrase(kPassphrase);
425 EXPECT_TRUE(config_.GenerateConfigFile(&error1, &config_content));
426 EXPECT_NE(std::string::npos, config_content.find(
427 kExpectedRsnConfigContent))
428 << "Expected to find the following config...\n"
429 << kExpectedRsnConfigContent << "..within content...\n"
430 << config_content;
431 EXPECT_EQ(nullptr, error1.get());
432}
433
434} // namespace apmanager