blob: f2297f6e2b4f3f2801dd0ba1c3e75a31e5f70778 [file] [log] [blame]
Peter Qiuf0731732014-11-11 09:46:41 -08001// 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 Qiufb39ba42014-11-21 09:09:59 -08007#include <string>
8
Peter Qiuf0731732014-11-11 09:46:41 -08009#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 Qiufb39ba42014-11-21 09:09:59 -080015#include "apmanager/mock_device.h"
16#include "apmanager/mock_manager.h"
17
Peter Qiu8e785b92014-11-24 10:01:08 -080018using ::testing::_;
Peter Qiufb39ba42014-11-21 09:09:59 -080019using ::testing::Mock;
20using ::testing::Return;
Peter Qiu8e785b92014-11-24 10:01:08 -080021using ::testing::SetArgumentPointee;
Peter Qiuf0731732014-11-11 09:46:41 -080022namespace apmanager {
23
24namespace {
25
26const char kServicePath[] = "/manager/services/0";
27const char kSsid[] = "TestSsid";
28const char kInterface[] = "uap0";
29const char kPassphrase[] = "Passphrase";
Peter Qiu8e785b92014-11-24 10:01:08 -080030const char k24GHzHTCapab[] = "[LDPC SMPS-STATIC GF SHORT-GI-20]";
31const char k5GHzHTCapab[] =
32 "[LDPC HT40+ SMPS-STATIC GF SHORT-GI-20 SHORT-GI-40]";
33
Peter Qiuf0731732014-11-11 09:46:41 -080034const uint16_t k24GHzChannel = 6;
35const uint16_t k5GHzChannel = 36;
36
37const char kExpected80211gConfigContent[] = "ssid=TestSsid\n"
38 "channel=6\n"
Peter Qiuf0731732014-11-11 09:46:41 -080039 "interface=uap0\n"
Peter Qiu8e785b92014-11-24 10:01:08 -080040 "hw_mode=g\n"
Peter Qiuf0731732014-11-11 09:46:41 -080041 "driver=nl80211\n"
42 "fragm_threshold=2346\n"
43 "rts_threshold=2347\n";
44
Peter Qiu8e785b92014-11-24 10:01:08 -080045const 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 Qiuf0731732014-11-11 09:46:41 -080055
Peter Qiu8e785b92014-11-24 10:01:08 -080056const 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 Qiuf0731732014-11-11 09:46:41 -080066
67const char kExpectedRsnConfigContent[] = "ssid=TestSsid\n"
68 "channel=6\n"
Peter Qiuf0731732014-11-11 09:46:41 -080069 "interface=uap0\n"
Peter Qiu8e785b92014-11-24 10:01:08 -080070 "hw_mode=g\n"
Peter Qiuf0731732014-11-11 09:46:41 -080071 "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
81class ConfigTest : public testing::Test {
82 public:
Peter Qiufb39ba42014-11-21 09:09:59 -080083 ConfigTest() : config_(&manager_, kServicePath) {}
84
85 void SetupDevice(const std::string& interface) {
86 // Setup mock device.
Peter Qiu8e785b92014-11-24 10:01:08 -080087 device_ = new MockDevice();
88 device_->SetPreferredApInterface(interface);
Peter Qiufb39ba42014-11-21 09:09:59 -080089 EXPECT_CALL(manager_, GetDeviceFromInterfaceName(interface))
Peter Qiu8e785b92014-11-24 10:01:08 -080090 .WillRepeatedly(Return(device_));
Peter Qiufb39ba42014-11-21 09:09:59 -080091 }
Peter Qiuf0731732014-11-11 09:46:41 -080092
93 protected:
94 Config config_;
Peter Qiufb39ba42014-11-21 09:09:59 -080095 MockManager manager_;
Peter Qiu8e785b92014-11-24 10:01:08 -080096 scoped_refptr<MockDevice> device_;
Peter Qiuf0731732014-11-11 09:46:41 -080097};
98
Peter Qiu376e4042014-11-13 09:40:28 -080099MATCHER_P(IsConfigErrorStartingWith, message, "") {
Peter Qiuf0731732014-11-11 09:46:41 -0800100 return arg != nullptr &&
101 arg->GetDomain() == chromeos::errors::dbus::kDomain &&
102 arg->GetCode() == kConfigError &&
Peter Qiu376e4042014-11-13 09:40:28 -0800103 StartsWithASCII(arg->GetMessage(), message, false);
Peter Qiuf0731732014-11-11 09:46:41 -0800104}
105
Peter Qiu8e785b92014-11-24 10:01:08 -0800106TEST_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 Qiuf0731732014-11-11 09:46:41 -0800129TEST_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 Qiu376e4042014-11-13 09:40:28 -0800137 EXPECT_THAT(error, IsConfigErrorStartingWith("SSID not specified"));
Peter Qiuf0731732014-11-11 09:46:41 -0800138}
139
Peter Qiufb39ba42014-11-21 09:09:59 -0800140TEST_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 Qiuf0731732014-11-11 09:46:41 -0800169TEST_F(ConfigTest, 80211gConfig) {
170 config_.SetSsid(kSsid);
171 config_.SetChannel(k24GHzChannel);
172 config_.SetHwMode(kHwMode80211g);
173 config_.SetInterfaceName(kInterface);
174
Peter Qiufb39ba42014-11-21 09:09:59 -0800175 // Setup mock device.
176 SetupDevice(kInterface);
177
Peter Qiuf0731732014-11-11 09:46:41 -0800178 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
189TEST_F(ConfigTest, 80211nConfig) {
190 config_.SetSsid(kSsid);
191 config_.SetHwMode(kHwMode80211n);
192 config_.SetInterfaceName(kInterface);
193
Peter Qiufb39ba42014-11-21 09:09:59 -0800194 // Setup mock device.
195 SetupDevice(kInterface);
196
Peter Qiuf0731732014-11-11 09:46:41 -0800197 // 5GHz channel.
198 config_.SetChannel(k5GHzChannel);
199 std::string ghz5_config_content;
200 chromeos::ErrorPtr error;
Peter Qiu8e785b92014-11-24 10:01:08 -0800201 std::string ht_capab_5ghz(k5GHzHTCapab);
202 EXPECT_CALL(*device_.get(), GetHTCapability(k5GHzChannel, _))
203 .WillOnce(DoAll(SetArgumentPointee<1>(ht_capab_5ghz), Return(true)));
Peter Qiuf0731732014-11-11 09:46:41 -0800204 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 Qiu8e785b92014-11-24 10:01:08 -0800211 Mock::VerifyAndClearExpectations(device_.get());
Peter Qiuf0731732014-11-11 09:46:41 -0800212
213 // 2.4GHz channel.
214 config_.SetChannel(k24GHzChannel);
215 std::string ghz24_config_content;
216 chromeos::ErrorPtr error1;
Peter Qiu8e785b92014-11-24 10:01:08 -0800217 std::string ht_capab_24ghz(k24GHzHTCapab);
218 EXPECT_CALL(*device_.get(), GetHTCapability(k24GHzChannel, _))
219 .WillOnce(DoAll(SetArgumentPointee<1>(ht_capab_24ghz), Return(true)));
Peter Qiuf0731732014-11-11 09:46:41 -0800220 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 Qiu8e785b92014-11-24 10:01:08 -0800227 Mock::VerifyAndClearExpectations(device_.get());
Peter Qiuf0731732014-11-11 09:46:41 -0800228}
229
230TEST_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 Qiufb39ba42014-11-21 09:09:59 -0800237 // Setup mock device.
238 SetupDevice(kInterface);
239
Peter Qiuf0731732014-11-11 09:46:41 -0800240 // Failed due to no passphrase specified.
241 std::string config_content;
242 chromeos::ErrorPtr error;
243 EXPECT_FALSE(config_.GenerateConfigFile(&error, &config_content));
Peter Qiu376e4042014-11-13 09:40:28 -0800244 EXPECT_THAT(error, IsConfigErrorStartingWith(
Peter Qiuf0731732014-11-11 09:46:41 -0800245 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