1 /*
2  * Copyright (C) 2016 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 <android/hardware/wifi/1.0/IWifi.h>
21 #include <android/hardware/wifi/hostapd/1.0/IHostapd.h>
22 
23 #include <gtest/gtest.h>
24 #include <hidl/GtestPrinter.h>
25 #include <hidl/ServiceManagement.h>
26 
27 #include "hostapd_hidl_call_util.h"
28 #include "hostapd_hidl_test_utils.h"
29 
30 using ::android::sp;
31 using ::android::hardware::hidl_vec;
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_0::IHostapd;
35 using ::android::hardware::wifi::V1_0::IWifi;
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
46     : public ::testing::TestWithParam<std::tuple<std::string, std::string>> {
47    public:
SetUp()48     virtual void SetUp() override {
49         wifi_instance_name_ = std::get<0>(GetParam());
50         hostapd_instance_name_ = std::get<1>(GetParam());
51         stopSupplicantIfNeeded(wifi_instance_name_);
52         startHostapdAndWaitForHidlService(wifi_instance_name_,
53                                           hostapd_instance_name_);
54         hostapd_ = IHostapd::getService(hostapd_instance_name_);
55         ASSERT_NE(hostapd_.get(), nullptr);
56     }
57 
TearDown()58     virtual void TearDown() override {
59         HIDL_INVOKE_VOID_WITHOUT_ARGUMENTS(hostapd_, terminate);
60         stopHostapd(wifi_instance_name_);
61     }
62 
63    protected:
getPrimaryWlanIfaceName()64     std::string getPrimaryWlanIfaceName() {
65         std::array<char, PROPERTY_VALUE_MAX> buffer;
66         property_get("wifi.interface", buffer.data(), "wlan0");
67         return buffer.data();
68     }
69 
getIfaceParamsWithAcs()70     IHostapd::IfaceParams getIfaceParamsWithAcs() {
71         IHostapd::IfaceParams iface_params;
72         iface_params.ifaceName = getPrimaryWlanIfaceName();
73         iface_params.hwModeParams.enable80211N = true;
74         iface_params.hwModeParams.enable80211AC = false;
75         iface_params.channelParams.enableAcs = true;
76         iface_params.channelParams.acsShouldExcludeDfs = true;
77         iface_params.channelParams.channel = 0;
78         iface_params.channelParams.band = IHostapd::Band::BAND_ANY;
79         return iface_params;
80     }
81 
getIfaceParamsWithoutAcs()82     IHostapd::IfaceParams getIfaceParamsWithoutAcs() {
83         IHostapd::IfaceParams iface_params;
84         iface_params.ifaceName = getPrimaryWlanIfaceName();
85         iface_params.hwModeParams.enable80211N = true;
86         iface_params.hwModeParams.enable80211AC = false;
87         iface_params.channelParams.enableAcs = false;
88         iface_params.channelParams.acsShouldExcludeDfs = false;
89         iface_params.channelParams.channel = kIfaceChannel;
90         iface_params.channelParams.band = IHostapd::Band::BAND_2_4_GHZ;
91         return iface_params;
92     }
93 
getIfaceParamsWithInvalidChannel()94     IHostapd::IfaceParams getIfaceParamsWithInvalidChannel() {
95         IHostapd::IfaceParams iface_params;
96         iface_params.ifaceName = getPrimaryWlanIfaceName();
97         iface_params.hwModeParams.enable80211N = true;
98         iface_params.hwModeParams.enable80211AC = false;
99         iface_params.channelParams.enableAcs = false;
100         iface_params.channelParams.acsShouldExcludeDfs = false;
101         iface_params.channelParams.channel = kIfaceInvalidChannel;
102         iface_params.channelParams.band = IHostapd::Band::BAND_2_4_GHZ;
103         return iface_params;
104     }
105 
getPskNwParams()106     IHostapd::NetworkParams getPskNwParams() {
107         IHostapd::NetworkParams nw_params;
108         nw_params.ssid =
109             std::vector<uint8_t>(kNwSsid, kNwSsid + sizeof(kNwSsid));
110         nw_params.isHidden = false;
111         nw_params.encryptionType = IHostapd::EncryptionType::WPA2;
112         nw_params.pskPassphrase = kNwPassphrase;
113         return nw_params;
114     }
115 
getInvalidPskNwParams()116     IHostapd::NetworkParams getInvalidPskNwParams() {
117         IHostapd::NetworkParams nw_params;
118         nw_params.ssid =
119             std::vector<uint8_t>(kNwSsid, kNwSsid + sizeof(kNwSsid));
120         nw_params.isHidden = false;
121         nw_params.encryptionType = IHostapd::EncryptionType::WPA2;
122         return nw_params;
123     }
124 
getOpenNwParams()125     IHostapd::NetworkParams getOpenNwParams() {
126         IHostapd::NetworkParams nw_params;
127         nw_params.ssid =
128             std::vector<uint8_t>(kNwSsid, kNwSsid + sizeof(kNwSsid));
129         nw_params.isHidden = false;
130         nw_params.encryptionType = IHostapd::EncryptionType::NONE;
131         return nw_params;
132     }
133     // IHostapd object used for all tests in this fixture.
134     sp<IHostapd> hostapd_;
135     std::string wifi_instance_name_;
136     std::string hostapd_instance_name_;
137 };
138 
139 /*
140  * Create:
141  * Ensures that an instance of the IHostapd proxy object is
142  * successfully created.
143  */
TEST_P(HostapdHidlTest,Create)144 TEST_P(HostapdHidlTest, Create) {
145     stopHostapd(wifi_instance_name_);
146     startHostapdAndWaitForHidlService(wifi_instance_name_,
147                                       hostapd_instance_name_);
148     hostapd_ = IHostapd::getService(hostapd_instance_name_);
149     EXPECT_NE(nullptr, hostapd_.get());
150 }
151 
152 /**
153  * Adds an access point with PSK network config & ACS enabled.
154  * Access point creation should pass.
155  */
TEST_P(HostapdHidlTest,AddPskAccessPointWithAcs)156 TEST_P(HostapdHidlTest, AddPskAccessPointWithAcs) {
157     if (!is_1_1(hostapd_)) {
158         auto status = HIDL_INVOKE(hostapd_, addAccessPoint,
159                                   getIfaceParamsWithAcs(), getPskNwParams());
160         // TODO: b/140172237, fix this in R
161         // EXPECT_EQ(HostapdStatusCode::SUCCESS, status.code);
162     }
163 }
164 
165 /**
166  * Adds an access point with Open network config & ACS enabled.
167  * Access point creation should pass.
168  */
TEST_P(HostapdHidlTest,AddOpenAccessPointWithAcs)169 TEST_P(HostapdHidlTest, AddOpenAccessPointWithAcs) {
170     if (!is_1_1(hostapd_)) {
171         auto status = HIDL_INVOKE(hostapd_, addAccessPoint,
172                                   getIfaceParamsWithAcs(), getOpenNwParams());
173         // TODO: b/140172237, fix this in R
174         // EXPECT_EQ(HostapdStatusCode::SUCCESS, status.code);
175     }
176 }
177 
178 /**
179  * Adds an access point with PSK network config & ACS disabled.
180  * Access point creation should pass.
181  */
TEST_P(HostapdHidlTest,AddPskAccessPointWithoutAcs)182 TEST_P(HostapdHidlTest, AddPskAccessPointWithoutAcs) {
183     if (!is_1_1(hostapd_)) {
184         auto status = HIDL_INVOKE(hostapd_, addAccessPoint,
185                                   getIfaceParamsWithoutAcs(), getPskNwParams());
186         EXPECT_EQ(HostapdStatusCode::SUCCESS, status.code);
187     }
188 }
189 
190 /**
191  * Adds an access point with Open network config & ACS disabled.
192  * Access point creation should pass.
193  */
TEST_P(HostapdHidlTest,AddOpenAccessPointWithoutAcs)194 TEST_P(HostapdHidlTest, AddOpenAccessPointWithoutAcs) {
195     if (!is_1_1(hostapd_)) {
196         auto status =
197             HIDL_INVOKE(hostapd_, addAccessPoint, getIfaceParamsWithoutAcs(),
198                         getOpenNwParams());
199         EXPECT_EQ(HostapdStatusCode::SUCCESS, status.code);
200     }
201 }
202 
203 /**
204  * Adds & then removes an access point with PSK network config & ACS enabled.
205  * Access point creation & removal should pass.
206  */
TEST_P(HostapdHidlTest,RemoveAccessPointWithAcs)207 TEST_P(HostapdHidlTest, RemoveAccessPointWithAcs) {
208     if (!is_1_1(hostapd_)) {
209         auto status = HIDL_INVOKE(hostapd_, addAccessPoint,
210                                   getIfaceParamsWithAcs(), getPskNwParams());
211         // TODO: b/140172237, fix this in R
212         /*
213         EXPECT_EQ(HostapdStatusCode::SUCCESS, status.code);
214         status =
215             HIDL_INVOKE(hostapd_, removeAccessPoint, getPrimaryWlanIfaceName());
216         EXPECT_EQ(HostapdStatusCode::SUCCESS, status.code);
217         */
218     }
219 }
220 
221 /**
222  * Adds & then removes an access point with PSK network config & ACS disabled.
223  * Access point creation & removal should pass.
224  */
TEST_P(HostapdHidlTest,RemoveAccessPointWithoutAcs)225 TEST_P(HostapdHidlTest, RemoveAccessPointWithoutAcs) {
226     if (!is_1_1(hostapd_)) {
227         auto status = HIDL_INVOKE(hostapd_, addAccessPoint,
228                                   getIfaceParamsWithoutAcs(), getPskNwParams());
229         EXPECT_EQ(HostapdStatusCode::SUCCESS, status.code);
230         status =
231             HIDL_INVOKE(hostapd_, removeAccessPoint, getPrimaryWlanIfaceName());
232         EXPECT_EQ(HostapdStatusCode::SUCCESS, status.code);
233     }
234 }
235 
236 /**
237  * Adds an access point with invalid channel.
238  * Access point creation should fail.
239  */
TEST_P(HostapdHidlTest,AddPskAccessPointWithInvalidChannel)240 TEST_P(HostapdHidlTest, AddPskAccessPointWithInvalidChannel) {
241     if (!is_1_1(hostapd_)) {
242         auto status =
243             HIDL_INVOKE(hostapd_, addAccessPoint,
244                         getIfaceParamsWithInvalidChannel(), getPskNwParams());
245         EXPECT_NE(HostapdStatusCode::SUCCESS, status.code);
246     }
247 }
248 
249 /**
250  * Adds an access point with invalid PSK network config.
251  * Access point creation should fail.
252  */
TEST_P(HostapdHidlTest,AddInvalidPskAccessPointWithoutAcs)253 TEST_P(HostapdHidlTest, AddInvalidPskAccessPointWithoutAcs) {
254     if (!is_1_1(hostapd_)) {
255         auto status =
256             HIDL_INVOKE(hostapd_, addAccessPoint, getIfaceParamsWithoutAcs(),
257                         getInvalidPskNwParams());
258         EXPECT_NE(HostapdStatusCode::SUCCESS, status.code);
259     }
260 }
261 
262 /*
263  * Terminate
264  * This terminates the service.
265  */
TEST_P(HostapdHidlTest,Terminate)266 TEST_P(HostapdHidlTest, Terminate) { hostapd_->terminate(); }
267 
268 GTEST_ALLOW_UNINSTANTIATED_PARAMETERIZED_TEST(HostapdHidlTest);
269 INSTANTIATE_TEST_SUITE_P(
270     PerInstance, HostapdHidlTest,
271     testing::Combine(
272         testing::ValuesIn(
273             android::hardware::getAllHalInstanceNames(IWifi::descriptor)),
274         testing::ValuesIn(
275             android::hardware::getAllHalInstanceNames(IHostapd::descriptor))),
276     android::hardware::PrintInstanceTupleNameToString<>);
277