blob: c138846c6d6c778fb34f974446ec32f2118f1e81 [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
18using ::testing::Mock;
19using ::testing::Return;
20
Peter Qiuf0731732014-11-11 09:46:41 -080021namespace apmanager {
22
23namespace {
24
25const char kServicePath[] = "/manager/services/0";
26const char kSsid[] = "TestSsid";
27const char kInterface[] = "uap0";
28const char kPassphrase[] = "Passphrase";
29const uint16_t k24GHzChannel = 6;
30const uint16_t k5GHzChannel = 36;
31
32const 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
40const 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
49const 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
58const 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
72class ConfigTest : public testing::Test {
73 public:
Peter Qiufb39ba42014-11-21 09:09:59 -080074 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 Qiuf0731732014-11-11 09:46:41 -080083
84 protected:
85 Config config_;
Peter Qiufb39ba42014-11-21 09:09:59 -080086 MockManager manager_;
Peter Qiuf0731732014-11-11 09:46:41 -080087};
88
Peter Qiu376e4042014-11-13 09:40:28 -080089MATCHER_P(IsConfigErrorStartingWith, message, "") {
Peter Qiuf0731732014-11-11 09:46:41 -080090 return arg != nullptr &&
91 arg->GetDomain() == chromeos::errors::dbus::kDomain &&
92 arg->GetCode() == kConfigError &&
Peter Qiu376e4042014-11-13 09:40:28 -080093 StartsWithASCII(arg->GetMessage(), message, false);
Peter Qiuf0731732014-11-11 09:46:41 -080094}
95
96TEST_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 Qiu376e4042014-11-13 09:40:28 -0800104 EXPECT_THAT(error, IsConfigErrorStartingWith("SSID not specified"));
Peter Qiuf0731732014-11-11 09:46:41 -0800105}
106
Peter Qiufb39ba42014-11-21 09:09:59 -0800107TEST_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 Qiuf0731732014-11-11 09:46:41 -0800136TEST_F(ConfigTest, 80211gConfig) {
137 config_.SetSsid(kSsid);
138 config_.SetChannel(k24GHzChannel);
139 config_.SetHwMode(kHwMode80211g);
140 config_.SetInterfaceName(kInterface);
141
Peter Qiufb39ba42014-11-21 09:09:59 -0800142 // Setup mock device.
143 SetupDevice(kInterface);
144
Peter Qiuf0731732014-11-11 09:46:41 -0800145 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
156TEST_F(ConfigTest, 80211nConfig) {
157 config_.SetSsid(kSsid);
158 config_.SetHwMode(kHwMode80211n);
159 config_.SetInterfaceName(kInterface);
160
Peter Qiufb39ba42014-11-21 09:09:59 -0800161 // Setup mock device.
162 SetupDevice(kInterface);
163
Peter Qiuf0731732014-11-11 09:46:41 -0800164 // 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
189TEST_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 Qiufb39ba42014-11-21 09:09:59 -0800196 // Setup mock device.
197 SetupDevice(kInterface);
198
Peter Qiuf0731732014-11-11 09:46:41 -0800199 // Failed due to no passphrase specified.
200 std::string config_content;
201 chromeos::ErrorPtr error;
202 EXPECT_FALSE(config_.GenerateConfigFile(&error, &config_content));
Peter Qiu376e4042014-11-13 09:40:28 -0800203 EXPECT_THAT(error, IsConfigErrorStartingWith(
Peter Qiuf0731732014-11-11 09:46:41 -0800204 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