1 /*
2 * Copyright (C) 2019 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 */
16
17 #include <android-base/logging.h>
18 #include <cutils/properties.h>
19
20 #include <VtsHalHidlTargetTestBase.h>
21
22 #include <android/hardware/wifi/hostapd/1.1/IHostapd.h>
23
24 #include "hostapd_hidl_call_util.h"
25 #include "hostapd_hidl_test_utils.h"
26 #include "hostapd_hidl_test_utils_1_1.h"
27
28 using ::android::sp;
29 using ::android::hardware::hidl_string;
30 using ::android::hardware::Return;
31 using ::android::hardware::Void;
32 using ::android::hardware::wifi::hostapd::V1_0::HostapdStatus;
33 using ::android::hardware::wifi::hostapd::V1_0::HostapdStatusCode;
34 using ::android::hardware::wifi::hostapd::V1_1::IHostapd;
35 using ::android::hardware::wifi::hostapd::V1_1::IHostapdCallback;
36
37 namespace {
38 constexpr unsigned char kNwSsid[] = {'t', 'e', 's', 't', '1',
39 '2', '3', '4', '5'};
40 constexpr char kNwPassphrase[] = "test12345";
41 constexpr int kIfaceChannel = 6;
42 constexpr int kIfaceInvalidChannel = 567;
43 } // namespace
44
45 class HostapdHidlTest : public ::testing::VtsHalHidlTargetTestBase {
46 public:
SetUp()47 virtual void SetUp() override {
48 stopSupplicantIfNeeded();
49 startHostapdAndWaitForHidlService();
50 hostapd_ = getHostapd_1_1();
51 ASSERT_NE(hostapd_.get(), nullptr);
52 }
53
TearDown()54 virtual void TearDown() override { stopHostapd(); }
55
56 protected:
getPrimaryWlanIfaceName()57 std::string getPrimaryWlanIfaceName() {
58 std::array<char, PROPERTY_VALUE_MAX> buffer;
59 property_get("wifi.interface", buffer.data(), "wlan0");
60 return buffer.data();
61 }
62
getIfaceParamsWithAcs()63 IHostapd::IfaceParams getIfaceParamsWithAcs() {
64 ::android::hardware::wifi::hostapd::V1_0::IHostapd::IfaceParams
65 iface_params;
66 IHostapd::IfaceParams iface_params_1_1;
67
68 iface_params.ifaceName = getPrimaryWlanIfaceName();
69 iface_params.hwModeParams.enable80211N = true;
70 iface_params.hwModeParams.enable80211AC = false;
71 iface_params.channelParams.enableAcs = true;
72 iface_params.channelParams.acsShouldExcludeDfs = true;
73 iface_params.channelParams.channel = 0;
74 iface_params.channelParams.band = IHostapd::Band::BAND_ANY;
75 iface_params_1_1.V1_0 = iface_params;
76 return iface_params_1_1;
77 }
78
getIfaceParamsWithAcsAndChannelRange()79 IHostapd::IfaceParams getIfaceParamsWithAcsAndChannelRange() {
80 IHostapd::IfaceParams iface_params_1_1 = getIfaceParamsWithAcs();
81 IHostapd::ChannelParams channelParams;
82 IHostapd::AcsChannelRange acsChannelRange;
83 acsChannelRange.start = 1;
84 acsChannelRange.end = 11;
85 std::vector<IHostapd::AcsChannelRange> vec_acsChannelRange;
86 vec_acsChannelRange.push_back(acsChannelRange);
87 channelParams.acsChannelRanges = vec_acsChannelRange;
88 iface_params_1_1.channelParams = channelParams;
89 return iface_params_1_1;
90 }
91
getIfaceParamsWithAcsAndInvalidChannelRange()92 IHostapd::IfaceParams getIfaceParamsWithAcsAndInvalidChannelRange() {
93 IHostapd::IfaceParams iface_params_1_1 =
94 getIfaceParamsWithAcsAndChannelRange();
95 iface_params_1_1.channelParams.acsChannelRanges[0].start = 222;
96 iface_params_1_1.channelParams.acsChannelRanges[0].end = 999;
97 return iface_params_1_1;
98 }
99
getIfaceParamsWithoutAcs()100 IHostapd::IfaceParams getIfaceParamsWithoutAcs() {
101 ::android::hardware::wifi::hostapd::V1_0::IHostapd::IfaceParams
102 iface_params;
103 IHostapd::IfaceParams iface_params_1_1;
104
105 iface_params.ifaceName = getPrimaryWlanIfaceName();
106 iface_params.hwModeParams.enable80211N = true;
107 iface_params.hwModeParams.enable80211AC = false;
108 iface_params.channelParams.enableAcs = false;
109 iface_params.channelParams.acsShouldExcludeDfs = false;
110 iface_params.channelParams.channel = kIfaceChannel;
111 iface_params.channelParams.band = IHostapd::Band::BAND_2_4_GHZ;
112 iface_params_1_1.V1_0 = iface_params;
113 return iface_params_1_1;
114 }
115
getIfaceParamsWithInvalidChannel()116 IHostapd::IfaceParams getIfaceParamsWithInvalidChannel() {
117 IHostapd::IfaceParams iface_params_1_1 = getIfaceParamsWithoutAcs();
118 iface_params_1_1.V1_0.channelParams.channel = kIfaceInvalidChannel;
119 return iface_params_1_1;
120 }
121
getPskNwParams()122 IHostapd::NetworkParams getPskNwParams() {
123 IHostapd::NetworkParams nw_params;
124 nw_params.ssid =
125 std::vector<uint8_t>(kNwSsid, kNwSsid + sizeof(kNwSsid));
126 nw_params.isHidden = false;
127 nw_params.encryptionType = IHostapd::EncryptionType::WPA2;
128 nw_params.pskPassphrase = kNwPassphrase;
129 return nw_params;
130 }
131
getInvalidPskNwParams()132 IHostapd::NetworkParams getInvalidPskNwParams() {
133 IHostapd::NetworkParams nw_params;
134 nw_params.ssid =
135 std::vector<uint8_t>(kNwSsid, kNwSsid + sizeof(kNwSsid));
136 nw_params.isHidden = false;
137 nw_params.encryptionType = IHostapd::EncryptionType::WPA2;
138 return nw_params;
139 }
140
getOpenNwParams()141 IHostapd::NetworkParams getOpenNwParams() {
142 IHostapd::NetworkParams nw_params;
143 nw_params.ssid =
144 std::vector<uint8_t>(kNwSsid, kNwSsid + sizeof(kNwSsid));
145 nw_params.isHidden = false;
146 nw_params.encryptionType = IHostapd::EncryptionType::NONE;
147 return nw_params;
148 }
149
150 // IHostapd object used for all tests in this fixture.
151 sp<IHostapd> hostapd_;
152 };
153
154 class IfaceCallback : public IHostapdCallback {
onFailure(const hidl_string &)155 Return<void> onFailure(
156 const hidl_string& /* Name of the interface */) override {
157 return Void();
158 }
159 };
160
161 /*
162 * RegisterCallback
163 */
TEST_F(HostapdHidlTest,registerCallback)164 TEST_F(HostapdHidlTest, registerCallback) {
165 hostapd_->registerCallback(
166 new IfaceCallback(), [](const HostapdStatus& status) {
167 EXPECT_EQ(HostapdStatusCode::SUCCESS, status.code);
168 });
169 }
170
171 /**
172 * Adds an access point with PSK network config & ACS enabled.
173 * Access point creation should pass.
174 */
TEST_F(HostapdHidlTest,AddPskAccessPointWithAcs)175 TEST_F(HostapdHidlTest, AddPskAccessPointWithAcs) {
176 auto status = HIDL_INVOKE(hostapd_, addAccessPoint_1_1,
177 getIfaceParamsWithAcs(), getPskNwParams());
178 EXPECT_EQ(HostapdStatusCode::SUCCESS, status.code);
179 }
180
181 /**
182 * Adds an access point with PSK network config, ACS enabled & channel Range.
183 * Access point creation should pass.
184 */
TEST_F(HostapdHidlTest,AddPskAccessPointWithAcsAndChannelRange)185 TEST_F(HostapdHidlTest, AddPskAccessPointWithAcsAndChannelRange) {
186 auto status =
187 HIDL_INVOKE(hostapd_, addAccessPoint_1_1,
188 getIfaceParamsWithAcsAndChannelRange(), getPskNwParams());
189 EXPECT_EQ(HostapdStatusCode::SUCCESS, status.code);
190 }
191
192 /**
193 * Adds an access point with invalid channel range.
194 * Access point creation should fail.
195 */
TEST_F(HostapdHidlTest,AddPskAccessPointWithAcsAndInvalidChannelRange)196 TEST_F(HostapdHidlTest, AddPskAccessPointWithAcsAndInvalidChannelRange) {
197 auto status = HIDL_INVOKE(hostapd_, addAccessPoint_1_1,
198 getIfaceParamsWithAcsAndInvalidChannelRange(),
199 getPskNwParams());
200 EXPECT_NE(HostapdStatusCode::SUCCESS, status.code);
201 }
202
203 /**
204 * Adds an access point with Open network config & ACS enabled.
205 * Access point creation should pass.
206 */
TEST_F(HostapdHidlTest,AddOpenAccessPointWithAcs)207 TEST_F(HostapdHidlTest, AddOpenAccessPointWithAcs) {
208 auto status = HIDL_INVOKE(hostapd_, addAccessPoint_1_1,
209 getIfaceParamsWithAcs(), getOpenNwParams());
210 EXPECT_EQ(HostapdStatusCode::SUCCESS, status.code);
211 }
212
213 /**
214 * Adds an access point with PSK network config & ACS disabled.
215 * Access point creation should pass.
216 */
TEST_F(HostapdHidlTest,AddPskAccessPointWithoutAcs)217 TEST_F(HostapdHidlTest, AddPskAccessPointWithoutAcs) {
218 auto status = HIDL_INVOKE(hostapd_, addAccessPoint_1_1,
219 getIfaceParamsWithoutAcs(), getPskNwParams());
220 EXPECT_EQ(HostapdStatusCode::SUCCESS, status.code);
221 }
222
223 /**
224 * Adds an access point with Open network config & ACS disabled.
225 * Access point creation should pass.
226 */
TEST_F(HostapdHidlTest,AddOpenAccessPointWithoutAcs)227 TEST_F(HostapdHidlTest, AddOpenAccessPointWithoutAcs) {
228 auto status = HIDL_INVOKE(hostapd_, addAccessPoint_1_1,
229 getIfaceParamsWithoutAcs(), getOpenNwParams());
230 EXPECT_EQ(HostapdStatusCode::SUCCESS, status.code);
231 }
232
233 /**
234 * Adds & then removes an access point with PSK network config & ACS enabled.
235 * Access point creation & removal should pass.
236 */
TEST_F(HostapdHidlTest,RemoveAccessPointWithAcs)237 TEST_F(HostapdHidlTest, RemoveAccessPointWithAcs) {
238 auto status = HIDL_INVOKE(hostapd_, addAccessPoint_1_1,
239 getIfaceParamsWithAcs(), getPskNwParams());
240 EXPECT_EQ(HostapdStatusCode::SUCCESS, status.code);
241 status =
242 HIDL_INVOKE(hostapd_, removeAccessPoint, getPrimaryWlanIfaceName());
243 EXPECT_EQ(HostapdStatusCode::SUCCESS, status.code);
244 }
245
246 /**
247 * Adds & then removes an access point with PSK network config & ACS disabled.
248 * Access point creation & removal should pass.
249 */
TEST_F(HostapdHidlTest,RemoveAccessPointWithoutAcs)250 TEST_F(HostapdHidlTest, RemoveAccessPointWithoutAcs) {
251 auto status = HIDL_INVOKE(hostapd_, addAccessPoint_1_1,
252 getIfaceParamsWithoutAcs(), getPskNwParams());
253 EXPECT_EQ(HostapdStatusCode::SUCCESS, status.code);
254 status =
255 HIDL_INVOKE(hostapd_, removeAccessPoint, getPrimaryWlanIfaceName());
256 EXPECT_EQ(HostapdStatusCode::SUCCESS, status.code);
257 }
258
259 /**
260 * Adds an access point with invalid channel.
261 * Access point creation should fail.
262 */
TEST_F(HostapdHidlTest,AddPskAccessPointWithInvalidChannel)263 TEST_F(HostapdHidlTest, AddPskAccessPointWithInvalidChannel) {
264 auto status =
265 HIDL_INVOKE(hostapd_, addAccessPoint_1_1,
266 getIfaceParamsWithInvalidChannel(), getPskNwParams());
267 EXPECT_NE(HostapdStatusCode::SUCCESS, status.code);
268 }
269
270 /**
271 * Adds an access point with invalid PSK network config.
272 * Access point creation should fail.
273 */
TEST_F(HostapdHidlTest,AddInvalidPskAccessPointWithoutAcs)274 TEST_F(HostapdHidlTest, AddInvalidPskAccessPointWithoutAcs) {
275 auto status =
276 HIDL_INVOKE(hostapd_, addAccessPoint_1_1, getIfaceParamsWithoutAcs(),
277 getInvalidPskNwParams());
278 EXPECT_NE(HostapdStatusCode::SUCCESS, status.code);
279 }
280