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 <VtsHalHidlTargetTestBase.h>
21 
22 #include <android/hardware/wifi/hostapd/1.0/IHostapd.h>
23 
24 #include "hostapd_hidl_call_util.h"
25 #include "hostapd_hidl_test_utils.h"
26 
27 using ::android::sp;
28 using ::android::hardware::hidl_vec;
29 using ::android::hardware::wifi::hostapd::V1_0::IHostapd;
30 using ::android::hardware::wifi::hostapd::V1_0::HostapdStatus;
31 using ::android::hardware::wifi::hostapd::V1_0::HostapdStatusCode;
32 
33 namespace {
34 constexpr unsigned char kNwSsid[] = {'t', 'e', 's', 't', '1',
35                                      '2', '3', '4', '5'};
36 constexpr char kNwPassphrase[] = "test12345";
37 constexpr int kIfaceChannel = 6;
38 constexpr int kIfaceInvalidChannel = 567;
39 }  // namespace
40 
41 class HostapdHidlTest : public ::testing::VtsHalHidlTargetTestBase {
42    public:
SetUp()43     virtual void SetUp() override {
44         startHostapdAndWaitForHidlService();
45         hostapd_ = getHostapd();
46         ASSERT_NE(hostapd_.get(), nullptr);
47     }
48 
TearDown()49     virtual void TearDown() override { stopHostapd(); }
50 
51    protected:
getPrimaryWlanIfaceName()52     std::string getPrimaryWlanIfaceName() {
53         std::array<char, PROPERTY_VALUE_MAX> buffer;
54         property_get("wifi.interface", buffer.data(), "wlan0");
55         return buffer.data();
56     }
57 
getIfaceParamsWithAcs()58     IHostapd::IfaceParams getIfaceParamsWithAcs() {
59         IHostapd::IfaceParams iface_params;
60         iface_params.ifaceName = getPrimaryWlanIfaceName();
61         iface_params.hwModeParams.enable80211N = true;
62         iface_params.hwModeParams.enable80211AC = false;
63         iface_params.channelParams.enableAcs = true;
64         iface_params.channelParams.acsShouldExcludeDfs = true;
65         iface_params.channelParams.channel = 0;
66         iface_params.channelParams.band = IHostapd::Band::BAND_ANY;
67         return iface_params;
68     }
69 
getIfaceParamsWithoutAcs()70     IHostapd::IfaceParams getIfaceParamsWithoutAcs() {
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 = false;
76         iface_params.channelParams.acsShouldExcludeDfs = false;
77         iface_params.channelParams.channel = kIfaceChannel;
78         iface_params.channelParams.band = IHostapd::Band::BAND_2_4_GHZ;
79         return iface_params;
80     }
81 
getIfaceParamsWithInvalidChannel()82     IHostapd::IfaceParams getIfaceParamsWithInvalidChannel() {
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 = kIfaceInvalidChannel;
90         iface_params.channelParams.band = IHostapd::Band::BAND_2_4_GHZ;
91         return iface_params;
92     }
93 
getPskNwParams()94     IHostapd::NetworkParams getPskNwParams() {
95         IHostapd::NetworkParams nw_params;
96         nw_params.ssid =
97             std::vector<uint8_t>(kNwSsid, kNwSsid + sizeof(kNwSsid));
98         nw_params.isHidden = false;
99         nw_params.encryptionType = IHostapd::EncryptionType::WPA2;
100         nw_params.pskPassphrase = kNwPassphrase;
101         return nw_params;
102     }
103 
getInvalidPskNwParams()104     IHostapd::NetworkParams getInvalidPskNwParams() {
105         IHostapd::NetworkParams nw_params;
106         nw_params.ssid =
107             std::vector<uint8_t>(kNwSsid, kNwSsid + sizeof(kNwSsid));
108         nw_params.isHidden = false;
109         nw_params.encryptionType = IHostapd::EncryptionType::WPA2;
110         return nw_params;
111     }
112 
getOpenNwParams()113     IHostapd::NetworkParams getOpenNwParams() {
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::NONE;
119         return nw_params;
120     }
121     // IHostapd object used for all tests in this fixture.
122     sp<IHostapd> hostapd_;
123 };
124 
125 /*
126  * Create:
127  * Ensures that an instance of the IHostapd proxy object is
128  * successfully created.
129  */
TEST(HostapdHidlTestNoFixture,Create)130 TEST(HostapdHidlTestNoFixture, Create) {
131     startHostapdAndWaitForHidlService();
132     EXPECT_NE(nullptr, getHostapd().get());
133     stopHostapd();
134 }
135 
136 /**
137  * Adds an access point with PSK network config & ACS enabled.
138  * Access point creation should pass.
139  */
TEST_F(HostapdHidlTest,AddPskAccessPointWithAcs)140 TEST_F(HostapdHidlTest, AddPskAccessPointWithAcs) {
141     auto status = HIDL_INVOKE(hostapd_, addAccessPoint, getIfaceParamsWithAcs(),
142                               getPskNwParams());
143     EXPECT_EQ(HostapdStatusCode::SUCCESS, status.code);
144 }
145 
146 /**
147  * Adds an access point with Open network config & ACS enabled.
148  * Access point creation should pass.
149  */
TEST_F(HostapdHidlTest,AddOpenAccessPointWithAcs)150 TEST_F(HostapdHidlTest, AddOpenAccessPointWithAcs) {
151     auto status = HIDL_INVOKE(hostapd_, addAccessPoint, getIfaceParamsWithAcs(),
152                               getOpenNwParams());
153     EXPECT_EQ(HostapdStatusCode::SUCCESS, status.code);
154 }
155 
156 /**
157  * Adds an access point with PSK network config & ACS disabled.
158  * Access point creation should pass.
159  */
TEST_F(HostapdHidlTest,AddPskAccessPointWithoutAcs)160 TEST_F(HostapdHidlTest, AddPskAccessPointWithoutAcs) {
161     auto status = HIDL_INVOKE(hostapd_, addAccessPoint,
162                               getIfaceParamsWithoutAcs(), getPskNwParams());
163     EXPECT_EQ(HostapdStatusCode::SUCCESS, status.code);
164 }
165 
166 /**
167  * Adds an access point with Open network config & ACS disabled.
168  * Access point creation should pass.
169  */
TEST_F(HostapdHidlTest,AddOpenAccessPointWithoutAcs)170 TEST_F(HostapdHidlTest, AddOpenAccessPointWithoutAcs) {
171     auto status = HIDL_INVOKE(hostapd_, addAccessPoint,
172                               getIfaceParamsWithoutAcs(), getOpenNwParams());
173     EXPECT_EQ(HostapdStatusCode::SUCCESS, status.code);
174 }
175 
176 /**
177  * Adds & then removes an access point with PSK network config & ACS enabled.
178  * Access point creation & removal should pass.
179  */
TEST_F(HostapdHidlTest,RemoveAccessPointWithAcs)180 TEST_F(HostapdHidlTest, RemoveAccessPointWithAcs) {
181     auto status = HIDL_INVOKE(hostapd_, addAccessPoint, getIfaceParamsWithAcs(),
182                               getPskNwParams());
183     EXPECT_EQ(HostapdStatusCode::SUCCESS, status.code);
184     status =
185         HIDL_INVOKE(hostapd_, removeAccessPoint, getPrimaryWlanIfaceName());
186     EXPECT_EQ(HostapdStatusCode::SUCCESS, status.code);
187 }
188 
189 /**
190  * Adds & then removes an access point with PSK network config & ACS disabled.
191  * Access point creation & removal should pass.
192  */
TEST_F(HostapdHidlTest,RemoveAccessPointWithoutAcs)193 TEST_F(HostapdHidlTest, RemoveAccessPointWithoutAcs) {
194     auto status = HIDL_INVOKE(hostapd_, addAccessPoint,
195                               getIfaceParamsWithoutAcs(), getPskNwParams());
196     EXPECT_EQ(HostapdStatusCode::SUCCESS, status.code);
197     status =
198         HIDL_INVOKE(hostapd_, removeAccessPoint, getPrimaryWlanIfaceName());
199     EXPECT_EQ(HostapdStatusCode::SUCCESS, status.code);
200 }
201 
202 /**
203  * Adds an access point with invalid channel.
204  * Access point creation should fail.
205  */
TEST_F(HostapdHidlTest,AddPskAccessPointWithInvalidChannel)206 TEST_F(HostapdHidlTest, AddPskAccessPointWithInvalidChannel) {
207     auto status =
208         HIDL_INVOKE(hostapd_, addAccessPoint,
209                     getIfaceParamsWithInvalidChannel(), getPskNwParams());
210     EXPECT_NE(HostapdStatusCode::SUCCESS, status.code);
211 }
212 
213 /**
214  * Adds an access point with invalid PSK network config.
215  * Access point creation should fail.
216  */
TEST_F(HostapdHidlTest,AddInvalidPskAccessPointWithoutAcs)217 TEST_F(HostapdHidlTest, AddInvalidPskAccessPointWithoutAcs) {
218     auto status =
219         HIDL_INVOKE(hostapd_, addAccessPoint, getIfaceParamsWithoutAcs(),
220                     getInvalidPskNwParams());
221     EXPECT_NE(HostapdStatusCode::SUCCESS, status.code);
222 }
223 
224 /*
225  * Terminate
226  * This terminates the service.
227  */
TEST_F(HostapdHidlTest,Terminate)228 TEST_F(HostapdHidlTest, Terminate) {
229     hostapd_->terminate();
230 }
231