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