1 /*
2  * Copyright (C) 2021 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 <VtsCoreUtil.h>
18 #include <aidl/Gtest.h>
19 #include <aidl/Vintf.h>
20 #include <aidl/android/hardware/wifi/supplicant/BnSupplicant.h>
21 #include <aidl/android/hardware/wifi/supplicant/BnSupplicantStaIfaceCallback.h>
22 #include <android/binder_manager.h>
23 #include <android/binder_status.h>
24 #include <binder/IServiceManager.h>
25 #include <binder/ProcessState.h>
26 #include <cutils/properties.h>
27 
28 #include "supplicant_test_utils.h"
29 
30 using aidl::android::hardware::wifi::supplicant::AnqpInfoId;
31 using aidl::android::hardware::wifi::supplicant::BnSupplicantStaIfaceCallback;
32 using aidl::android::hardware::wifi::supplicant::BtCoexistenceMode;
33 using aidl::android::hardware::wifi::supplicant::ConnectionCapabilities;
34 using aidl::android::hardware::wifi::supplicant::DebugLevel;
35 using aidl::android::hardware::wifi::supplicant::DppAkm;
36 using aidl::android::hardware::wifi::supplicant::DppConnectionKeys;
37 using aidl::android::hardware::wifi::supplicant::DppCurve;
38 using aidl::android::hardware::wifi::supplicant::DppNetRole;
39 using aidl::android::hardware::wifi::supplicant::DppResponderBootstrapInfo;
40 using aidl::android::hardware::wifi::supplicant::Hs20AnqpSubtypes;
41 using aidl::android::hardware::wifi::supplicant::IfaceType;
42 using aidl::android::hardware::wifi::supplicant::ISupplicant;
43 using aidl::android::hardware::wifi::supplicant::ISupplicantStaIface;
44 using aidl::android::hardware::wifi::supplicant::ISupplicantStaNetwork;
45 using aidl::android::hardware::wifi::supplicant::KeyMgmtMask;
46 using aidl::android::hardware::wifi::supplicant::MscsParams;
47 using aidl::android::hardware::wifi::supplicant::QosCharacteristics;
48 using aidl::android::hardware::wifi::supplicant::QosPolicyScsData;
49 using aidl::android::hardware::wifi::supplicant::QosPolicyScsRequestStatus;
50 using aidl::android::hardware::wifi::supplicant::WpaDriverCapabilitiesMask;
51 using aidl::android::hardware::wifi::supplicant::WpsConfigMethods;
52 using android::ProcessState;
53 
54 static constexpr int TIMEOUT_PERIOD = 60;
55 class IfaceDppCallback;
56 
57 namespace {
58 const std::vector<uint8_t> kTestMacAddr = {0x56, 0x67, 0x67, 0xf4, 0x56, 0x92};
59 const std::string kTestUri =
60     "DPP:C:81/1,117/"
61     "40;M:48d6d5bd1de1;I:G1197843;K:MDkwEwYHKoZIzj0CAQYIKoZIzj"
62     "0DAQcDIgAD0edY4X3N//HhMFYsZfMbQJTiNFtNIWF/cIwMB/gzqOM=;;";
63 }  // namespace
64 
65 class SupplicantStaIfaceCallback : public BnSupplicantStaIfaceCallback {
66    public:
67     SupplicantStaIfaceCallback() = default;
68 
onAnqpQueryDone(const std::vector<uint8_t> &,const::aidl::android::hardware::wifi::supplicant::AnqpData &,const::aidl::android::hardware::wifi::supplicant::Hs20AnqpData &)69     ::ndk::ScopedAStatus onAnqpQueryDone(
70         const std::vector<uint8_t>& /* bssid */,
71         const ::aidl::android::hardware::wifi::supplicant::AnqpData& /* data */,
72         const ::aidl::android::hardware::wifi::supplicant::
73             Hs20AnqpData& /* hs20Data */) override {
74         return ndk::ScopedAStatus::ok();
75     }
onAssociationRejected(const::aidl::android::hardware::wifi::supplicant::AssociationRejectionData &)76     ::ndk::ScopedAStatus onAssociationRejected(
77         const ::aidl::android::hardware::wifi::supplicant::
78             AssociationRejectionData& /* assocRejectData */) override {
79         return ndk::ScopedAStatus::ok();
80     }
onAuthenticationTimeout(const std::vector<uint8_t> &)81     ::ndk::ScopedAStatus onAuthenticationTimeout(
82         const std::vector<uint8_t>& /* bssid */) override {
83         return ndk::ScopedAStatus::ok();
84     }
onAuxiliarySupplicantEvent(::aidl::android::hardware::wifi::supplicant::AuxiliarySupplicantEventCode,const std::vector<uint8_t> &,const std::string &)85     ::ndk::ScopedAStatus onAuxiliarySupplicantEvent(
86             ::aidl::android::hardware::wifi::supplicant ::
87                     AuxiliarySupplicantEventCode /* eventCode */,
88             const std::vector<uint8_t>& /* bssid */,
89             const std::string& /* reasonString */) override {
90         return ndk::ScopedAStatus::ok();
91     }
onBssTmHandlingDone(const::aidl::android::hardware::wifi::supplicant::BssTmData &)92     ::ndk::ScopedAStatus onBssTmHandlingDone(
93         const ::aidl::android::hardware::wifi::supplicant::
94             BssTmData& /* tmData */) override {
95         return ndk::ScopedAStatus::ok();
96     }
onBssidChanged(::aidl::android::hardware::wifi::supplicant::BssidChangeReason,const std::vector<uint8_t> &)97     ::ndk::ScopedAStatus onBssidChanged(
98         ::aidl::android::hardware::wifi::supplicant::
99             BssidChangeReason /* reason */,
100         const std::vector<uint8_t>& /* bssid */) override {
101         return ndk::ScopedAStatus::ok();
102     }
onDisconnected(const std::vector<uint8_t> &,bool,::aidl::android::hardware::wifi::supplicant::StaIfaceReasonCode)103     ::ndk::ScopedAStatus onDisconnected(
104         const std::vector<uint8_t>& /* bssid */, bool /* locallyGenerated */,
105         ::aidl::android::hardware::wifi::supplicant::
106             StaIfaceReasonCode /* reasonCode */) override {
107         return ndk::ScopedAStatus::ok();
108     }
onDppFailure(::aidl::android::hardware::wifi::supplicant::DppFailureCode,const std::string &,const std::string &,const std::vector<char16_t> &)109     ::ndk::ScopedAStatus onDppFailure(
110         ::aidl::android::hardware::wifi::supplicant::DppFailureCode /* code */,
111         const std::string& /* ssid */, const std::string& /* channelList */,
112         const std::vector<char16_t>& /* bandList */) override {
113         return ndk::ScopedAStatus::ok();
114     }
onDppProgress(::aidl::android::hardware::wifi::supplicant::DppProgressCode)115     ::ndk::ScopedAStatus onDppProgress(
116         ::aidl::android::hardware::wifi::supplicant::DppProgressCode /* code */)
117         override {
118         return ndk::ScopedAStatus::ok();
119     }
onDppSuccess(::aidl::android::hardware::wifi::supplicant::DppEventType)120     ::ndk::ScopedAStatus onDppSuccess(
121         ::aidl::android::hardware::wifi::supplicant::DppEventType /* type */)
122         override {
123         return ndk::ScopedAStatus::ok();
124     }
onDppSuccessConfigReceived(const std::vector<uint8_t> &,const std::string &,const std::vector<uint8_t> &,::aidl::android::hardware::wifi::supplicant::DppAkm,const::aidl::android::hardware::wifi::supplicant::DppConnectionKeys &)125     ::ndk::ScopedAStatus onDppSuccessConfigReceived(
126             const std::vector<uint8_t>& /* ssid */, const std::string& /* password */,
127             const std::vector<uint8_t>& /* psk */,
128             ::aidl::android::hardware::wifi::supplicant::DppAkm /* securityAkm */,
129             const ::aidl::android::hardware::wifi::supplicant::
130                     DppConnectionKeys& /* DppConnectionKeys */) override {
131         return ndk::ScopedAStatus::ok();
132     }
onDppSuccessConfigSent()133     ::ndk::ScopedAStatus onDppSuccessConfigSent() override {
134         return ndk::ScopedAStatus::ok();
135     }
onEapFailure(const std::vector<uint8_t> &,int32_t)136     ::ndk::ScopedAStatus onEapFailure(const std::vector<uint8_t>& /* bssid */,
137                                       int32_t /* errorCode */) override {
138         return ndk::ScopedAStatus::ok();
139     }
onExtRadioWorkStart(int32_t)140     ::ndk::ScopedAStatus onExtRadioWorkStart(int32_t /* id */) override {
141         return ndk::ScopedAStatus::ok();
142     }
onExtRadioWorkTimeout(int32_t)143     ::ndk::ScopedAStatus onExtRadioWorkTimeout(int32_t /* id */) override {
144         return ndk::ScopedAStatus::ok();
145     }
onHs20DeauthImminentNotice(const std::vector<uint8_t> &,int32_t,int32_t,const std::string &)146     ::ndk::ScopedAStatus onHs20DeauthImminentNotice(
147         const std::vector<uint8_t>& /* bssid */, int32_t /* reasonCode */,
148         int32_t /* reAuthDelayInSec */, const std::string& /* url */) override {
149         return ndk::ScopedAStatus::ok();
150     }
onHs20IconQueryDone(const std::vector<uint8_t> &,const std::string &,const std::vector<uint8_t> &)151     ::ndk::ScopedAStatus onHs20IconQueryDone(
152         const std::vector<uint8_t>& /* bssid */,
153         const std::string& /* fileName */,
154         const std::vector<uint8_t>& /* data */) override {
155         return ndk::ScopedAStatus::ok();
156     }
onHs20SubscriptionRemediation(const std::vector<uint8_t> &,::aidl::android::hardware::wifi::supplicant::OsuMethod,const std::string &)157     ::ndk::ScopedAStatus onHs20SubscriptionRemediation(
158         const std::vector<uint8_t>& /* bssid */,
159         ::aidl::android::hardware::wifi::supplicant::OsuMethod /* osuMethod */,
160         const std::string& /* url */) override {
161         return ndk::ScopedAStatus::ok();
162     }
163     ::ndk::ScopedAStatus
onHs20TermsAndConditionsAcceptanceRequestedNotification(const std::vector<uint8_t> &,const std::string &)164     onHs20TermsAndConditionsAcceptanceRequestedNotification(
165         const std::vector<uint8_t>& /* bssid */,
166         const std::string& /* url */) override {
167         return ndk::ScopedAStatus::ok();
168     }
onNetworkAdded(int32_t)169     ::ndk::ScopedAStatus onNetworkAdded(int32_t /* id */) override {
170         return ndk::ScopedAStatus::ok();
171     }
onNetworkNotFound(const std::vector<uint8_t> &)172     ::ndk::ScopedAStatus onNetworkNotFound(
173         const std::vector<uint8_t>& /* ssid */) override {
174         return ndk::ScopedAStatus::ok();
175     }
onNetworkRemoved(int32_t)176     ::ndk::ScopedAStatus onNetworkRemoved(int32_t /* id */) override {
177         return ndk::ScopedAStatus::ok();
178     }
onPmkCacheAdded(int64_t,const std::vector<uint8_t> &)179     ::ndk::ScopedAStatus onPmkCacheAdded(
180         int64_t /* expirationTimeInSec */,
181         const std::vector<uint8_t>& /* serializedEntry */) override {
182         return ndk::ScopedAStatus::ok();
183     }
onStateChanged(::aidl::android::hardware::wifi::supplicant::StaIfaceCallbackState,const std::vector<uint8_t> &,int32_t,const std::vector<uint8_t> &,bool)184     ::ndk::ScopedAStatus onStateChanged(
185         ::aidl::android::hardware::wifi::supplicant::
186             StaIfaceCallbackState /* newState */,
187         const std::vector<uint8_t>& /* bssid */, int32_t /* id */,
188         const std::vector<uint8_t>& /* ssid */,
189         bool /* filsHlpSent */) override {
190         return ndk::ScopedAStatus::ok();
191     }
onWpsEventFail(const std::vector<uint8_t> &,::aidl::android::hardware::wifi::supplicant::WpsConfigError,::aidl::android::hardware::wifi::supplicant::WpsErrorIndication)192     ::ndk::ScopedAStatus onWpsEventFail(
193         const std::vector<uint8_t>& /* bssid */,
194         ::aidl::android::hardware::wifi::supplicant::
195             WpsConfigError /* configError */,
196         ::aidl::android::hardware::wifi::supplicant::
197             WpsErrorIndication /* errorInd */) override {
198         return ndk::ScopedAStatus::ok();
199     }
onWpsEventPbcOverlap()200     ::ndk::ScopedAStatus onWpsEventPbcOverlap() override {
201         return ndk::ScopedAStatus::ok();
202     }
onWpsEventSuccess()203     ::ndk::ScopedAStatus onWpsEventSuccess() override {
204         return ndk::ScopedAStatus::ok();
205     }
onQosPolicyReset()206     ::ndk::ScopedAStatus onQosPolicyReset() override { return ndk::ScopedAStatus::ok(); }
onQosPolicyRequest(int32_t,const std::vector<::aidl::android::hardware::wifi::supplicant::QosPolicyData> &)207     ::ndk::ScopedAStatus onQosPolicyRequest(
208             int32_t /* qosPolicyRequestId */,
209             const std::vector<::aidl::android::hardware::wifi::supplicant ::
210                                       QosPolicyData /* qosPolicyData */>&) override {
211         return ndk::ScopedAStatus::ok();
212     }
onSupplicantStateChanged(const::aidl::android::hardware::wifi::supplicant::SupplicantStateChangeData &)213     ::ndk::ScopedAStatus onSupplicantStateChanged(
214             const ::aidl::android::hardware::wifi::supplicant::
215                     SupplicantStateChangeData& /* stateChangeData */) override {
216         return ndk::ScopedAStatus::ok();
217     }
onMloLinksInfoChanged(::aidl::android::hardware::wifi::supplicant::ISupplicantStaIfaceCallback::MloLinkInfoChangeReason)218     ::ndk::ScopedAStatus onMloLinksInfoChanged(
219             ::aidl::android::hardware::wifi::supplicant::ISupplicantStaIfaceCallback::
220                     MloLinkInfoChangeReason /* reason */) override {
221         return ndk::ScopedAStatus::ok();
222     }
onDppConfigReceived(const::aidl::android::hardware::wifi::supplicant::DppConfigurationData &)223     ::ndk::ScopedAStatus onDppConfigReceived(
224             const ::aidl::android::hardware::wifi::supplicant::
225                     DppConfigurationData& /* configData */) override {
226         return ndk::ScopedAStatus::ok();
227     }
onDppConnectionStatusResultSent(::aidl::android::hardware::wifi::supplicant::DppStatusErrorCode)228     ::ndk::ScopedAStatus onDppConnectionStatusResultSent(
229             ::aidl::android::hardware::wifi::supplicant::DppStatusErrorCode /* code */) override {
230         return ndk::ScopedAStatus::ok();
231     }
onBssFrequencyChanged(int32_t)232     ::ndk::ScopedAStatus onBssFrequencyChanged(int32_t /* frequencyMhz */) override {
233         return ndk::ScopedAStatus::ok();
234     }
onQosPolicyResponseForScs(const std::vector<::aidl::android::hardware::wifi::supplicant::QosPolicyScsResponseStatus> &)235     ::ndk::ScopedAStatus onQosPolicyResponseForScs(
236             const std::vector<::aidl::android::hardware::wifi::supplicant::
237                                       QosPolicyScsResponseStatus>& /* qosPolicyScsResponseStatus */)
238             override {
239         return ndk::ScopedAStatus::ok();
240     }
onPmkSaCacheAdded(const::aidl::android::hardware::wifi::supplicant::PmkSaCacheData &)241     ::ndk::ScopedAStatus onPmkSaCacheAdded(
242             const ::aidl::android::hardware::wifi::supplicant::PmkSaCacheData& /* pmkSaData */)
243             override {
244         return ndk::ScopedAStatus::ok();
245     }
246 };
247 
248 class SupplicantStaIfaceAidlTest : public testing::TestWithParam<std::string> {
249    public:
SetUp()250     void SetUp() override {
251         initializeService();
252         supplicant_ = getSupplicant(GetParam().c_str());
253         ASSERT_NE(supplicant_, nullptr);
254         ASSERT_TRUE(supplicant_
255                         ->setDebugParams(DebugLevel::EXCESSIVE,
256                                          true,  // show timestamps
257                                          true)
258                         .isOk());
259         ASSERT_TRUE(supplicant_->getInterfaceVersion(&interface_version_).isOk());
260         EXPECT_TRUE(supplicant_->getStaInterface(getStaIfaceName(), &sta_iface_)
261                         .isOk());
262         ASSERT_NE(sta_iface_, nullptr);
263     }
264 
TearDown()265     void TearDown() override {
266         stopSupplicantService();
267         startWifiFramework();
268     }
269 
270     enum DppCallbackType {
271         ANY_CALLBACK = -2,
272         INVALID = -1,
273         EVENT_SUCCESS = 0,
274         EVENT_PROGRESS,
275         EVENT_FAILURE,
276     };
277 
278     DppCallbackType dppCallbackType;
279     uint32_t code;
280 
281     // Used as a mechanism to inform the test about data/event callback
notify()282     inline void notify() {
283         std::unique_lock<std::mutex> lock(mtx_);
284         cv_.notify_one();
285     }
286 
287     // Test code calls this function to wait for data/event callback
wait(DppCallbackType waitForCallbackType)288     inline std::cv_status wait(DppCallbackType waitForCallbackType) {
289         std::unique_lock<std::mutex> lock(mtx_);
290         EXPECT_NE(INVALID, waitForCallbackType);  // can't ASSERT in a
291                                                   // non-void-returning method
292         auto now = std::chrono::system_clock::now();
293         std::cv_status status =
294             cv_.wait_until(lock, now + std::chrono::seconds(TIMEOUT_PERIOD));
295         return status;
296     }
297 
298    protected:
299     std::shared_ptr<ISupplicant> supplicant_;
300     std::shared_ptr<ISupplicantStaIface> sta_iface_;
301     int interface_version_;
302 
303    private:
304     // synchronization objects
305     std::mutex mtx_;
306     std::condition_variable cv_;
307 };
308 
309 /*
310  * RegisterCallback
311  */
TEST_P(SupplicantStaIfaceAidlTest,RegisterCallback)312 TEST_P(SupplicantStaIfaceAidlTest, RegisterCallback) {
313     std::shared_ptr<SupplicantStaIfaceCallback> callback =
314         ndk::SharedRefBase::make<SupplicantStaIfaceCallback>();
315     ASSERT_NE(callback, nullptr);
316     EXPECT_TRUE(sta_iface_->registerCallback(callback).isOk());
317 }
318 
319 /*
320  * GetConnectionCapabilities
321  */
TEST_P(SupplicantStaIfaceAidlTest,GetConnectionCapabilities)322 TEST_P(SupplicantStaIfaceAidlTest, GetConnectionCapabilities) {
323     ConnectionCapabilities cap;
324     EXPECT_TRUE(sta_iface_->getConnectionCapabilities(&cap).isOk());
325 }
326 
327 /*
328  * GetWpaDriverCapabilities
329  */
TEST_P(SupplicantStaIfaceAidlTest,GetWpaDriverCapabilities)330 TEST_P(SupplicantStaIfaceAidlTest, GetWpaDriverCapabilities) {
331     WpaDriverCapabilitiesMask cap;
332     EXPECT_TRUE(sta_iface_->getWpaDriverCapabilities(&cap).isOk());
333 }
334 
335 /*
336  * GetKeyMgmtCapabilities
337  */
TEST_P(SupplicantStaIfaceAidlTest,GetKeyMgmtCapabilities)338 TEST_P(SupplicantStaIfaceAidlTest, GetKeyMgmtCapabilities) {
339     KeyMgmtMask cap;
340     EXPECT_TRUE(sta_iface_->getKeyMgmtCapabilities(&cap).isOk());
341 
342     // Even though capabilities vary, these two are always set.
343     EXPECT_TRUE(!!(static_cast<uint32_t>(cap) &
344                    static_cast<uint32_t>(KeyMgmtMask::NONE)));
345     EXPECT_TRUE(!!(static_cast<uint32_t>(cap) &
346                    static_cast<uint32_t>(KeyMgmtMask::IEEE8021X)));
347 }
348 
349 /*
350  * GetName
351  */
TEST_P(SupplicantStaIfaceAidlTest,GetName)352 TEST_P(SupplicantStaIfaceAidlTest, GetName) {
353     std::string name;
354     EXPECT_TRUE(sta_iface_->getName(&name).isOk());
355     EXPECT_NE(name.size(), 0);
356 }
357 
358 /*
359  * GetType
360  */
TEST_P(SupplicantStaIfaceAidlTest,GetType)361 TEST_P(SupplicantStaIfaceAidlTest, GetType) {
362     IfaceType type;
363     EXPECT_TRUE(sta_iface_->getType(&type).isOk());
364     EXPECT_EQ(type, IfaceType::STA);
365 }
366 
367 /*
368  * GetMacAddress
369  */
TEST_P(SupplicantStaIfaceAidlTest,GetMacAddress)370 TEST_P(SupplicantStaIfaceAidlTest, GetMacAddress) {
371     std::vector<uint8_t> macAddr;
372     EXPECT_TRUE(sta_iface_->getMacAddress(&macAddr).isOk());
373     EXPECT_EQ(macAddr.size(), 6);
374 }
375 
376 /*
377  * ListNetworks
378  */
TEST_P(SupplicantStaIfaceAidlTest,ListNetworks)379 TEST_P(SupplicantStaIfaceAidlTest, ListNetworks) {
380     std::vector<int32_t> networks;
381     EXPECT_TRUE(sta_iface_->listNetworks(&networks).isOk());
382 }
383 
384 /*
385  * SetBtCoexistenceMode
386  */
TEST_P(SupplicantStaIfaceAidlTest,SetBtCoexistenceMode)387 TEST_P(SupplicantStaIfaceAidlTest, SetBtCoexistenceMode) {
388     EXPECT_TRUE(
389         sta_iface_->setBtCoexistenceMode(BtCoexistenceMode::ENABLED).isOk());
390     EXPECT_TRUE(
391         sta_iface_->setBtCoexistenceMode(BtCoexistenceMode::DISABLED).isOk());
392     EXPECT_TRUE(
393         sta_iface_->setBtCoexistenceMode(BtCoexistenceMode::SENSE).isOk());
394 }
395 
396 /*
397  * SetBtCoexistenceScanModeEnabled
398  */
TEST_P(SupplicantStaIfaceAidlTest,SetBtCoexistenceScanModeEnabled)399 TEST_P(SupplicantStaIfaceAidlTest, SetBtCoexistenceScanModeEnabled) {
400     EXPECT_TRUE(sta_iface_->setBtCoexistenceScanModeEnabled(true).isOk());
401     EXPECT_TRUE(sta_iface_->setBtCoexistenceScanModeEnabled(false).isOk());
402 }
403 
404 /*
405  * SetSuspendModeEnabled
406  */
TEST_P(SupplicantStaIfaceAidlTest,SetSuspendModeEnabled)407 TEST_P(SupplicantStaIfaceAidlTest, SetSuspendModeEnabled) {
408     EXPECT_TRUE(sta_iface_->setSuspendModeEnabled(true).isOk());
409     EXPECT_TRUE(sta_iface_->setSuspendModeEnabled(false).isOk());
410 }
411 
412 /*
413  * SetCountryCode
414  */
TEST_P(SupplicantStaIfaceAidlTest,SetCountryCode)415 TEST_P(SupplicantStaIfaceAidlTest, SetCountryCode) {
416     const std::vector<uint8_t> countryCode = {'U', 'S'};
417     EXPECT_TRUE(sta_iface_->setCountryCode(countryCode).isOk());
418 }
419 
420 /*
421  * SetWpsDeviceName
422  */
TEST_P(SupplicantStaIfaceAidlTest,SetWpsDeviceName)423 TEST_P(SupplicantStaIfaceAidlTest, SetWpsDeviceName) {
424     const std::string deviceName = "TestWpsDeviceName";
425     EXPECT_TRUE(sta_iface_->setWpsDeviceName(deviceName).isOk());
426 }
427 
428 /*
429  * SetWpsDeviceType
430  */
TEST_P(SupplicantStaIfaceAidlTest,SetWpsDeviceType)431 TEST_P(SupplicantStaIfaceAidlTest, SetWpsDeviceType) {
432     const std::vector<uint8_t> deviceType = {8, 0x1};
433     EXPECT_TRUE(sta_iface_->setWpsDeviceType(deviceType).isOk());
434 }
435 
436 /*
437  * SetWpsManufacturer
438  */
TEST_P(SupplicantStaIfaceAidlTest,SetWpsManufacturer)439 TEST_P(SupplicantStaIfaceAidlTest, SetWpsManufacturer) {
440     const std::string wpsManufacturer = "TestManufacturer";
441     EXPECT_TRUE(sta_iface_->setWpsManufacturer(wpsManufacturer).isOk());
442 }
443 
444 /*
445  * SetWpsModelName
446  */
TEST_P(SupplicantStaIfaceAidlTest,SetWpsModelName)447 TEST_P(SupplicantStaIfaceAidlTest, SetWpsModelName) {
448     const std::string modelName = "TestModelName";
449     EXPECT_TRUE(sta_iface_->setWpsModelName(modelName).isOk());
450 }
451 
452 /*
453  * SetWpsModelNumber
454  */
TEST_P(SupplicantStaIfaceAidlTest,SetWpsModelNumber)455 TEST_P(SupplicantStaIfaceAidlTest, SetWpsModelNumber) {
456     const std::string modelNumber = "TestModelNumber";
457     EXPECT_TRUE(sta_iface_->setWpsModelNumber(modelNumber).isOk());
458 }
459 
460 /*
461  * SetWpsSerialNumber
462  */
TEST_P(SupplicantStaIfaceAidlTest,SetWpsSerialNumber)463 TEST_P(SupplicantStaIfaceAidlTest, SetWpsSerialNumber) {
464     const std::string serialNumber = "TestSerialNumber";
465     EXPECT_TRUE(sta_iface_->setWpsSerialNumber(serialNumber).isOk());
466 }
467 
468 /*
469  * SetWpsConfigMethods
470  */
TEST_P(SupplicantStaIfaceAidlTest,SetWpsConfigMethods)471 TEST_P(SupplicantStaIfaceAidlTest, SetWpsConfigMethods) {
472     const WpsConfigMethods configMethods = WpsConfigMethods::KEYPAD;
473     EXPECT_TRUE(sta_iface_->setWpsConfigMethods(configMethods).isOk());
474 }
475 
476 /*
477  * SetExternalSim
478  */
TEST_P(SupplicantStaIfaceAidlTest,SetExternalSim)479 TEST_P(SupplicantStaIfaceAidlTest, SetExternalSim) {
480     EXPECT_TRUE(sta_iface_->setExternalSim(true).isOk());
481     EXPECT_TRUE(sta_iface_->setExternalSim(false).isOk());
482 }
483 
484 /*
485  * SetMboCellularDataStatus
486  */
TEST_P(SupplicantStaIfaceAidlTest,SetMboCellularDataStatus)487 TEST_P(SupplicantStaIfaceAidlTest, SetMboCellularDataStatus) {
488     WpaDriverCapabilitiesMask cap;
489     EXPECT_TRUE(sta_iface_->getWpaDriverCapabilities(&cap).isOk());
490 
491     // Operation should succeed if MBO is supported, or fail if it's not.
492     bool mboSupported =
493         !!(static_cast<uint32_t>(cap) &
494            static_cast<uint32_t>(WpaDriverCapabilitiesMask::MBO));
495     EXPECT_EQ(mboSupported, sta_iface_->setMboCellularDataStatus(true).isOk());
496 }
497 
498 /*
499  * InitiateTdlsDiscover
500  */
TEST_P(SupplicantStaIfaceAidlTest,InitiateTdlsDiscover)501 TEST_P(SupplicantStaIfaceAidlTest, InitiateTdlsDiscover) {
502     EXPECT_TRUE(sta_iface_->initiateTdlsDiscover(kTestMacAddr).isOk());
503 }
504 
505 /*
506  * InitiateTdlsSetup
507  */
TEST_P(SupplicantStaIfaceAidlTest,InitiateTdlsSetup)508 TEST_P(SupplicantStaIfaceAidlTest, InitiateTdlsSetup) {
509     EXPECT_TRUE(sta_iface_->initiateTdlsSetup(kTestMacAddr).isOk());
510 }
511 
512 /*
513  * InitiateTdlsTeardown
514  */
TEST_P(SupplicantStaIfaceAidlTest,InitiateTdlsTeardown)515 TEST_P(SupplicantStaIfaceAidlTest, InitiateTdlsTeardown) {
516     EXPECT_TRUE(sta_iface_->initiateTdlsTeardown(kTestMacAddr).isOk());
517 }
518 
519 /*
520  * InitiateAnqpQuery
521  */
TEST_P(SupplicantStaIfaceAidlTest,InitiateAnqpQuery)522 TEST_P(SupplicantStaIfaceAidlTest, InitiateAnqpQuery) {
523     const std::vector<AnqpInfoId> anqpIds = {
524         AnqpInfoId::VENUE_NAME, AnqpInfoId::NAI_REALM, AnqpInfoId::DOMAIN_NAME};
525     const std::vector<Hs20AnqpSubtypes> hsTypes = {
526         Hs20AnqpSubtypes::WAN_METRICS,
527         Hs20AnqpSubtypes::OPERATOR_FRIENDLY_NAME};
528 
529     // Request should fail since the BSSID mentioned
530     // is not present in the scan results
531     EXPECT_FALSE(
532         sta_iface_->initiateAnqpQuery(kTestMacAddr, anqpIds, hsTypes).isOk());
533 }
534 
535 /*
536  * InitiateHs20IconQuery
537  */
TEST_P(SupplicantStaIfaceAidlTest,InitiateHs20IconQuery)538 TEST_P(SupplicantStaIfaceAidlTest, InitiateHs20IconQuery) {
539     // Request should fail since the BSSID mentioned
540     // is not present in the scan results
541     const std::string hs20IconFile = "TestFile";
542     EXPECT_FALSE(
543         sta_iface_->initiateHs20IconQuery(kTestMacAddr, hs20IconFile).isOk());
544 }
545 
546 /*
547  * InitiateVenueUrlAnqpQuery.
548  */
TEST_P(SupplicantStaIfaceAidlTest,InitiateVenueUrlAnqpQuery)549 TEST_P(SupplicantStaIfaceAidlTest, InitiateVenueUrlAnqpQuery) {
550     // Request should fail since the BSSID mentioned
551     // is not present in the scan results
552     EXPECT_FALSE(sta_iface_->initiateVenueUrlAnqpQuery(kTestMacAddr).isOk());
553 }
554 
555 /*
556  * Reassociate
557  */
TEST_P(SupplicantStaIfaceAidlTest,Reassociate)558 TEST_P(SupplicantStaIfaceAidlTest, Reassociate) {
559     EXPECT_TRUE(sta_iface_->reassociate().isOk());
560 }
561 
562 /*
563  * Reconnect
564  */
TEST_P(SupplicantStaIfaceAidlTest,Reconnect)565 TEST_P(SupplicantStaIfaceAidlTest, Reconnect) {
566     EXPECT_FALSE(sta_iface_->reconnect().isOk());
567 }
568 
569 /*
570  * Disconnect
571  */
TEST_P(SupplicantStaIfaceAidlTest,Disconnect)572 TEST_P(SupplicantStaIfaceAidlTest, Disconnect) {
573     EXPECT_TRUE(sta_iface_->disconnect().isOk());
574 }
575 
576 /*
577  * SetPowerSave
578  */
TEST_P(SupplicantStaIfaceAidlTest,SetPowerSave)579 TEST_P(SupplicantStaIfaceAidlTest, SetPowerSave) {
580     EXPECT_TRUE(sta_iface_->setPowerSave(true).isOk());
581     EXPECT_TRUE(sta_iface_->setPowerSave(false).isOk());
582 }
583 
584 /*
585  * AddExtRadioWork
586  */
TEST_P(SupplicantStaIfaceAidlTest,AddExtRadioWork)587 TEST_P(SupplicantStaIfaceAidlTest, AddExtRadioWork) {
588     const std::string radioWorkName = "TestRadioWork";
589     const int32_t radioWorkFreq = 2412;
590     const int32_t radioWorkTimeout = 8;
591     int32_t radioWorkId;
592     EXPECT_TRUE(sta_iface_
593                     ->addExtRadioWork(radioWorkName, radioWorkFreq,
594                                       radioWorkTimeout, &radioWorkId)
595                     .isOk());
596     // removeExtRadio only succeeds if the added radio work hasn't started yet,
597     // so there is no guaranteed result from calling removeExtRadioWork here.
598     // Given that, we can't currently test removeExtRadioWork following
599     // a call to addExtRadioWork.
600 }
601 
602 /*
603  * RemoveExtRadioWork
604  */
TEST_P(SupplicantStaIfaceAidlTest,RemoveExtRadioWork)605 TEST_P(SupplicantStaIfaceAidlTest, RemoveExtRadioWork) {
606     // This fails because there is no ongoing radio work with radioWorkId
607     const int32_t radioWorkId = 16;
608     EXPECT_FALSE(sta_iface_->removeExtRadioWork(radioWorkId).isOk());
609 }
610 
611 /*
612  * Add/Remove DppPeerUri
613  */
TEST_P(SupplicantStaIfaceAidlTest,AddRemoveDppPeerUri)614 TEST_P(SupplicantStaIfaceAidlTest, AddRemoveDppPeerUri) {
615     if (!keyMgmtSupported(sta_iface_, KeyMgmtMask::DPP)) {
616         GTEST_SKIP() << "Missing DPP support";
617     }
618     // Add a peer URI and then remove it.
619     int32_t peerId;
620     EXPECT_TRUE(sta_iface_->addDppPeerUri(kTestUri, &peerId).isOk());
621     EXPECT_TRUE(sta_iface_->removeDppUri(peerId).isOk());
622 }
623 
624 /*
625  * FilsHlpAddRequest
626  */
TEST_P(SupplicantStaIfaceAidlTest,FilsHlpAddRequest)627 TEST_P(SupplicantStaIfaceAidlTest, FilsHlpAddRequest) {
628     if (!isFilsSupported(sta_iface_)) {
629         GTEST_SKIP()
630             << "Skipping test since driver/supplicant doesn't support FILS";
631     }
632     const std::vector<uint8_t> destMacAddr = {0x00, 0x11, 0x22,
633                                               0x33, 0x44, 0x55};
634     const std::vector<uint8_t> pktBuffer = std::vector<uint8_t>(300, 0x3a);
635     EXPECT_TRUE(sta_iface_->filsHlpAddRequest(destMacAddr, pktBuffer).isOk());
636 }
637 
638 /*
639  * FilsHlpFlushRequest
640  */
TEST_P(SupplicantStaIfaceAidlTest,FilsHlpFlushRequest)641 TEST_P(SupplicantStaIfaceAidlTest, FilsHlpFlushRequest) {
642     if (!isFilsSupported(sta_iface_)) {
643         GTEST_SKIP()
644             << "Skipping test since driver/supplicant doesn't support FILS";
645     }
646     EXPECT_TRUE(sta_iface_->filsHlpFlushRequest().isOk());
647 }
648 
649 /*
650  * StartDppEnrolleeResponder
651  */
TEST_P(SupplicantStaIfaceAidlTest,StartDppEnrolleeResponder)652 TEST_P(SupplicantStaIfaceAidlTest, StartDppEnrolleeResponder) {
653     if (!keyMgmtSupported(sta_iface_, KeyMgmtMask::DPP)) {
654         GTEST_SKIP() << "Missing DPP support";
655     }
656 
657     const std::string deviceInfo = "DPP_Responder_Mode_VTS_Test";
658     const std::vector<uint8_t> mac_address = {0x22, 0x33, 0x44,
659                                               0x55, 0x66, 0x77};
660 
661     // Generate DPP bootstrap information.
662     DppResponderBootstrapInfo bootstrapInfo;
663     EXPECT_TRUE(
664         sta_iface_
665             ->generateDppBootstrapInfoForResponder(
666                 mac_address, deviceInfo, DppCurve::PRIME256V1, &bootstrapInfo)
667             .isOk());
668     EXPECT_NE(-1, bootstrapInfo.bootstrapId);
669     EXPECT_NE(0, bootstrapInfo.bootstrapId);
670     EXPECT_NE(0, bootstrapInfo.listenChannel);
671     const uint32_t bootstrap_id = bootstrapInfo.bootstrapId;
672     const uint32_t listen_channel = bootstrapInfo.listenChannel;
673 
674     // Start DPP as Enrollee-Responder.
675     EXPECT_TRUE(sta_iface_->startDppEnrolleeResponder(listen_channel).isOk());
676 
677     // Stop DPP Enrollee-Responder mode, ie remove the URI and stop listen.
678     EXPECT_TRUE(sta_iface_->stopDppResponder(bootstrap_id).isOk());
679 }
680 
681 class IfaceDppCallback : public SupplicantStaIfaceCallback {
682     SupplicantStaIfaceAidlTest& parent_;
onDppSuccess(::aidl::android::hardware::wifi::supplicant::DppEventType event)683     ::ndk::ScopedAStatus onDppSuccess(
684         ::aidl::android::hardware::wifi::supplicant::DppEventType event)
685         override {
686         parent_.code = (uint32_t)event;
687         parent_.dppCallbackType =
688             SupplicantStaIfaceAidlTest::DppCallbackType::EVENT_SUCCESS;
689         parent_.notify();
690         return ndk::ScopedAStatus::ok();
691     }
onDppProgress(aidl::android::hardware::wifi::supplicant::DppProgressCode code)692     ::ndk::ScopedAStatus onDppProgress(
693         aidl::android::hardware::wifi::supplicant::DppProgressCode code)
694         override {
695         parent_.code = (uint32_t)code;
696         parent_.dppCallbackType =
697             SupplicantStaIfaceAidlTest::DppCallbackType::EVENT_PROGRESS;
698         parent_.notify();
699         return ndk::ScopedAStatus::ok();
700     }
onDppFailure(aidl::android::hardware::wifi::supplicant::DppFailureCode code,const std::string & ssid,const std::string & channelList,const std::vector<char16_t> & bandList)701     ::ndk::ScopedAStatus onDppFailure(
702         aidl::android::hardware::wifi::supplicant::DppFailureCode code,
703         const std::string& ssid __attribute__((unused)),
704         const std::string& channelList __attribute__((unused)),
705         const std::vector<char16_t>& bandList
706         __attribute__((unused))) override {
707         parent_.code = (uint32_t)code;
708         parent_.dppCallbackType =
709             SupplicantStaIfaceAidlTest::DppCallbackType::EVENT_FAILURE;
710         parent_.notify();
711         return ndk::ScopedAStatus::ok();
712     }
713 
714    public:
IfaceDppCallback(SupplicantStaIfaceAidlTest & parent)715     IfaceDppCallback(SupplicantStaIfaceAidlTest& parent) : parent_(parent){};
716 };
717 
718 /*
719  * StartDppEnrolleeInitiator
720  */
TEST_P(SupplicantStaIfaceAidlTest,StartDppEnrolleeInitiator)721 TEST_P(SupplicantStaIfaceAidlTest, StartDppEnrolleeInitiator) {
722     if (!keyMgmtSupported(sta_iface_, KeyMgmtMask::DPP)) {
723         GTEST_SKIP() << "Missing DPP support";
724     }
725 
726     // Register callback
727     std::shared_ptr<IfaceDppCallback> callback =
728         ndk::SharedRefBase::make<IfaceDppCallback>(*this);
729     EXPECT_NE(callback, nullptr);
730     EXPECT_TRUE(sta_iface_->registerCallback(callback).isOk());
731 
732     // Add a peer URI
733     int32_t peer_id = 0;
734     EXPECT_TRUE(sta_iface_->addDppPeerUri(kTestUri, &peer_id).isOk());
735     EXPECT_NE(0, peer_id);
736     EXPECT_NE(-1, peer_id);
737 
738     // Start DPP as Enrollee-Initiator. Since this operation requires two
739     // devices, we start the operation and expect a timeout.
740     EXPECT_TRUE(sta_iface_->startDppEnrolleeInitiator(peer_id, 0).isOk());
741 
742     // Wait for the timeout callback
743     EXPECT_EQ(std::cv_status::no_timeout,
744               wait(SupplicantStaIfaceAidlTest::DppCallbackType::EVENT_FAILURE));
745     EXPECT_EQ(SupplicantStaIfaceAidlTest::DppCallbackType::EVENT_FAILURE,
746               dppCallbackType);
747 
748     // ...and then remove the peer URI.
749     EXPECT_TRUE(sta_iface_->removeDppUri(peer_id).isOk());
750 }
751 
752 /*
753  * StartDppConfiguratorInitiator
754  */
TEST_P(SupplicantStaIfaceAidlTest,StartDppConfiguratorInitiator)755 TEST_P(SupplicantStaIfaceAidlTest, StartDppConfiguratorInitiator) {
756     if (!keyMgmtSupported(sta_iface_, KeyMgmtMask::DPP)) {
757         GTEST_SKIP() << "Missing DPP support";
758     }
759 
760     // Register callback
761     std::shared_ptr<IfaceDppCallback> callback =
762         ndk::SharedRefBase::make<IfaceDppCallback>(*this);
763     EXPECT_NE(callback, nullptr);
764     EXPECT_TRUE(sta_iface_->registerCallback(callback).isOk());
765 
766     // Add a peer URI
767     int32_t peer_id = 0;
768     EXPECT_TRUE(sta_iface_->addDppPeerUri(kTestUri, &peer_id).isOk());
769     EXPECT_NE(0, peer_id);
770     EXPECT_NE(-1, peer_id);
771 
772     const std::string ssid =
773         "6D795F746573745F73736964";  // 'my_test_ssid' encoded in hex
774     const std::string password =
775         "746F70736563726574";  // 'topsecret' encoded in hex
776     const std::vector<uint8_t> eckey_in = {0x2, 0x3, 0x4};
777     std::vector<uint8_t> eckey_out = {};
778 
779     // Start DPP as Configurator-Initiator. Since this operation requires two
780     // devices, we start the operation and expect a timeout.
781     EXPECT_TRUE(sta_iface_
782                         ->startDppConfiguratorInitiator(peer_id, 0, ssid, password, "",
783                                                         DppNetRole::STA, DppAkm::PSK, eckey_in,
784                                                         &eckey_out)
785                         .isOk());
786 
787     // Wait for the timeout callback
788     ASSERT_EQ(std::cv_status::no_timeout,
789               wait(SupplicantStaIfaceAidlTest::DppCallbackType::EVENT_FAILURE));
790     ASSERT_EQ(SupplicantStaIfaceAidlTest::DppCallbackType::EVENT_FAILURE,
791               dppCallbackType);
792 
793     // ...and then remove the peer URI.
794     EXPECT_TRUE(sta_iface_->removeDppUri(peer_id).isOk());
795 }
796 
797 /*
798  * Configure and Disable MSCS
799  */
TEST_P(SupplicantStaIfaceAidlTest,ConfigureAndDisableMscs)800 TEST_P(SupplicantStaIfaceAidlTest, ConfigureAndDisableMscs) {
801     if (interface_version_ < 3) {
802         GTEST_SKIP() << "MSCS configure/disable is available as of Supplicant V3";
803     }
804     MscsParams params;
805     params.upBitmap = 0;
806     params.upLimit = 7;
807     params.streamTimeoutUs = 1000;  // 1 ms
808     params.frameClassifierMask = 0;
809     EXPECT_TRUE(sta_iface_->configureMscs(params).isOk());
810     EXPECT_TRUE(sta_iface_->disableMscs().isOk());
811 }
812 
813 /*
814  * Add and remove QoS policy with traffic characteristics
815  */
TEST_P(SupplicantStaIfaceAidlTest,AddAndRemoveQosWithTrafficChars)816 TEST_P(SupplicantStaIfaceAidlTest, AddAndRemoveQosWithTrafficChars) {
817     if (interface_version_ < 3) {
818         GTEST_SKIP() << "QosCharacteristics is available as of Supplicant V3";
819     }
820 
821     QosCharacteristics qosChars;
822     qosChars.minServiceIntervalUs = 2000;
823     qosChars.maxServiceIntervalUs = 5000;
824     qosChars.minDataRateKbps = 500;
825     qosChars.delayBoundUs = 200;
826     qosChars.optionalFieldMask = 0;  // no optional fields
827 
828     uint8_t policyId = 5;
829     QosPolicyScsData qosPolicy;
830     qosPolicy.policyId = policyId;
831     qosPolicy.direction = QosPolicyScsData::LinkDirection::UPLINK;
832     qosPolicy.QosCharacteristics = qosChars;
833 
834     std::vector<uint8_t> policyIdList{policyId};
835     std::vector<QosPolicyScsData> policyList{qosPolicy};
836     std::vector<QosPolicyScsRequestStatus> responseList;
837 
838     // Check that we receive some reply for this request.
839     // Policy may not be accepted (ex. policy with this id already exists).
840     EXPECT_TRUE(sta_iface_->addQosPolicyRequestForScs(policyList, &responseList).isOk());
841     EXPECT_EQ(1, responseList.size());
842     EXPECT_TRUE(sta_iface_->removeQosPolicyForScs(policyIdList, &responseList).isOk());
843     EXPECT_EQ(1, responseList.size());
844 }
845 
846 GTEST_ALLOW_UNINSTANTIATED_PARAMETERIZED_TEST(SupplicantStaIfaceAidlTest);
847 INSTANTIATE_TEST_SUITE_P(Supplicant, SupplicantStaIfaceAidlTest,
848                          testing::ValuesIn(android::getAidlHalInstanceNames(
849                              ISupplicant::descriptor)),
850                          android::PrintInstanceNameToString);
851 
main(int argc,char ** argv)852 int main(int argc, char** argv) {
853     ::testing::InitGoogleTest(&argc, argv);
854     ProcessState::self()->setThreadPoolMaxThreadCount(1);
855     ProcessState::self()->startThreadPool();
856     return RUN_ALL_TESTS();
857 }
858