1 /*
2  * Copyright (C) 2022 The Android Open Source Project
3  *
4  * Licensed under the Staache 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 <vector>
18 
19 #include <VtsCoreUtil.h>
20 #include <aidl/Gtest.h>
21 #include <aidl/Vintf.h>
22 #include <aidl/android/hardware/wifi/BnWifi.h>
23 #include <aidl/android/hardware/wifi/BnWifiNanIfaceEventCallback.h>
24 #include <aidl/android/hardware/wifi/NanBandIndex.h>
25 #include <android-base/logging.h>
26 #include <android/binder_manager.h>
27 #include <android/binder_status.h>
28 #include <binder/IServiceManager.h>
29 #include <binder/ProcessState.h>
30 
31 #include "wifi_aidl_test_utils.h"
32 
33 using aidl::android::hardware::wifi::BnWifiNanIfaceEventCallback;
34 using aidl::android::hardware::wifi::IWifiNanIface;
35 using aidl::android::hardware::wifi::NanBandIndex;
36 using aidl::android::hardware::wifi::NanBandSpecificConfig;
37 using aidl::android::hardware::wifi::NanBootstrappingConfirmInd;
38 using aidl::android::hardware::wifi::NanBootstrappingRequestInd;
39 using aidl::android::hardware::wifi::NanCapabilities;
40 using aidl::android::hardware::wifi::NanClusterEventInd;
41 using aidl::android::hardware::wifi::NanConfigRequest;
42 using aidl::android::hardware::wifi::NanConfigRequestSupplemental;
43 using aidl::android::hardware::wifi::NanDataPathConfirmInd;
44 using aidl::android::hardware::wifi::NanDataPathRequestInd;
45 using aidl::android::hardware::wifi::NanDataPathScheduleUpdateInd;
46 using aidl::android::hardware::wifi::NanDataPathSecurityType;
47 using aidl::android::hardware::wifi::NanEnableRequest;
48 using aidl::android::hardware::wifi::NanFollowupReceivedInd;
49 using aidl::android::hardware::wifi::NanInitiateDataPathRequest;
50 using aidl::android::hardware::wifi::NanMatchAlg;
51 using aidl::android::hardware::wifi::NanMatchInd;
52 using aidl::android::hardware::wifi::NanPairingConfirmInd;
53 using aidl::android::hardware::wifi::NanPairingRequestInd;
54 using aidl::android::hardware::wifi::NanPublishRequest;
55 using aidl::android::hardware::wifi::NanPublishType;
56 using aidl::android::hardware::wifi::NanRespondToDataPathIndicationRequest;
57 using aidl::android::hardware::wifi::NanStatus;
58 using aidl::android::hardware::wifi::NanStatusCode;
59 using aidl::android::hardware::wifi::NanSuspensionModeChangeInd;
60 using aidl::android::hardware::wifi::NanTxType;
61 
62 #define TIMEOUT_PERIOD 10
63 
64 namespace {
65 const auto& kTestVendorDataOptional = generateOuiKeyedDataListOptional(5);
66 }
67 
68 class WifiNanIfaceAidlTest : public testing::TestWithParam<std::string> {
69   public:
SetUp()70     void SetUp() override {
71         if (!::testing::deviceSupportsFeature("android.hardware.wifi.aware"))
72             GTEST_SKIP() << "Skipping this test since NAN is not supported.";
73         stopWifiService(getInstanceName());
74 
75         wifi_nan_iface_ = getWifiNanIface(getInstanceName());
76         ASSERT_NE(nullptr, wifi_nan_iface_.get());
77         std::shared_ptr<WifiNanIfaceEventCallback> callback =
78                 ndk::SharedRefBase::make<WifiNanIfaceEventCallback>(*this);
79         EXPECT_TRUE(wifi_nan_iface_->registerEventCallback(callback).isOk());
80         EXPECT_TRUE(wifi_nan_iface_->getInterfaceVersion(&interface_version_).isOk());
81     }
82 
TearDown()83     void TearDown() override { stopWifiService(getInstanceName()); }
84 
85     enum CallbackType {
86         INVALID = 0,
87 
88         NOTIFY_CAPABILITIES_RESPONSE = 1,
89         NOTIFY_ENABLE_RESPONSE,
90         NOTIFY_CONFIG_RESPONSE,
91         NOTIFY_DISABLE_RESPONSE,
92         NOTIFY_START_PUBLISH_RESPONSE,
93         NOTIFY_STOP_PUBLISH_RESPONSE,
94         NOTIFY_START_SUBSCRIBE_RESPONSE,
95         NOTIFY_STOP_SUBSCRIBE_RESPONSE,
96         NOTIFY_TRANSMIT_FOLLOWUP_RESPONSE,
97         NOTIFY_CREATE_DATA_INTERFACE_RESPONSE,
98         NOTIFY_DELETE_DATA_INTERFACE_RESPONSE,
99         NOTIFY_INITIATE_DATA_PATH_RESPONSE,
100         NOTIFY_RESPOND_TO_DATA_PATH_INDICATION_RESPONSE,
101         NOTIFY_TERMINATE_DATA_PATH_RESPONSE,
102         NOTIFY_INITIATE_PAIRING_RESPONSE,
103         NOTIFY_RESPOND_TO_PAIRING_INDICATION_RESPONSE,
104         NOTIFY_INITIATE_BOOTSTRAPPING_RESPONSE,
105         NOTIFY_RESPOND_TO_BOOTSTRAPPING_INDICATION_RESPONSE,
106         NOTIFY_SUSPEND_RESPONSE,
107         NOTIFY_RESUME_RESPONSE,
108         NOTIFY_TERMINATE_PAIRING_RESPONSE,
109 
110         EVENT_CLUSTER_EVENT,
111         EVENT_DISABLED,
112         EVENT_PUBLISH_TERMINATED,
113         EVENT_SUBSCRIBE_TERMINATED,
114         EVENT_MATCH,
115         EVENT_MATCH_EXPIRED,
116         EVENT_FOLLOWUP_RECEIVED,
117         EVENT_TRANSMIT_FOLLOWUP,
118         EVENT_DATA_PATH_REQUEST,
119         EVENT_DATA_PATH_CONFIRM,
120         EVENT_DATA_PATH_TERMINATED,
121         EVENT_DATA_PATH_SCHEDULE_UPDATE,
122         EVENT_PAIRING_REQUEST,
123         EVENT_PAIRING_CONFIRM,
124         EVENT_BOOTSTRAPPING_REQUEST,
125         EVENT_BOOTSTRAPPING_CONFIRM,
126         EVENT_SUSPENSION_MODE_CHANGE,
127     };
128 
129     // Used as a mechanism to inform the test about data/event callbacks.
notify(CallbackType callbackType)130     inline void notify(CallbackType callbackType) {
131         std::unique_lock<std::mutex> lock(mtx_);
132         callback_event_bitmap_ |= (UINT64_C(0x1) << callbackType);
133         cv_.notify_one();
134     }
135 
136     // Test code calls this function to wait for data/event callback.
137     // Must set callback_event_bitmap_ to 0 before calling this function.
wait(CallbackType waitForCallbackType)138     inline std::cv_status wait(CallbackType waitForCallbackType) {
139         std::unique_lock<std::mutex> lock(mtx_);
140         EXPECT_NE(INVALID, waitForCallbackType);
141 
142         std::cv_status status = std::cv_status::no_timeout;
143         auto now = std::chrono::system_clock::now();
144         while (!(receivedCallback(waitForCallbackType))) {
145             status = cv_.wait_until(lock, now + std::chrono::seconds(TIMEOUT_PERIOD));
146             if (status == std::cv_status::timeout) return status;
147         }
148         return status;
149     }
150 
receivedCallback(CallbackType waitForCallbackType)151     inline bool receivedCallback(CallbackType waitForCallbackType) {
152         return callback_event_bitmap_ & (UINT64_C(0x1) << waitForCallbackType);
153     }
154 
155     class WifiNanIfaceEventCallback : public BnWifiNanIfaceEventCallback {
156       public:
WifiNanIfaceEventCallback(WifiNanIfaceAidlTest & parent)157         WifiNanIfaceEventCallback(WifiNanIfaceAidlTest& parent) : parent_(parent){};
158 
eventClusterEvent(const NanClusterEventInd & event)159         ::ndk::ScopedAStatus eventClusterEvent(const NanClusterEventInd& event) override {
160             parent_.nan_cluster_event_ind_ = event;
161             parent_.notify(EVENT_CLUSTER_EVENT);
162             return ndk::ScopedAStatus::ok();
163         }
eventDataPathConfirm(const NanDataPathConfirmInd & event)164         ::ndk::ScopedAStatus eventDataPathConfirm(const NanDataPathConfirmInd& event) override {
165             parent_.nan_data_path_confirm_ind_ = event;
166             parent_.notify(EVENT_DATA_PATH_CONFIRM);
167             return ndk::ScopedAStatus::ok();
168         }
eventDataPathRequest(const NanDataPathRequestInd & event)169         ::ndk::ScopedAStatus eventDataPathRequest(const NanDataPathRequestInd& event) override {
170             parent_.nan_data_path_request_ind_ = event;
171             parent_.notify(EVENT_DATA_PATH_REQUEST);
172             return ndk::ScopedAStatus::ok();
173         }
eventDataPathScheduleUpdate(const NanDataPathScheduleUpdateInd & event)174         ::ndk::ScopedAStatus eventDataPathScheduleUpdate(
175                 const NanDataPathScheduleUpdateInd& event) override {
176             parent_.nan_data_path_schedule_update_ind_ = event;
177             parent_.notify(EVENT_DATA_PATH_SCHEDULE_UPDATE);
178             return ndk::ScopedAStatus::ok();
179         }
eventDataPathTerminated(int32_t ndpInstanceId)180         ::ndk::ScopedAStatus eventDataPathTerminated(int32_t ndpInstanceId) override {
181             parent_.ndp_instance_id_ = ndpInstanceId;
182             parent_.notify(EVENT_DATA_PATH_TERMINATED);
183             return ndk::ScopedAStatus::ok();
184         }
eventDisabled(const NanStatus & status)185         ::ndk::ScopedAStatus eventDisabled(const NanStatus& status) override {
186             parent_.status_ = status;
187             parent_.notify(EVENT_DISABLED);
188             return ndk::ScopedAStatus::ok();
189         }
eventFollowupReceived(const NanFollowupReceivedInd & event)190         ::ndk::ScopedAStatus eventFollowupReceived(const NanFollowupReceivedInd& event) override {
191             parent_.nan_followup_received_ind_ = event;
192             parent_.notify(EVENT_FOLLOWUP_RECEIVED);
193             return ndk::ScopedAStatus::ok();
194         }
eventMatch(const NanMatchInd & event)195         ::ndk::ScopedAStatus eventMatch(const NanMatchInd& event) override {
196             parent_.nan_match_ind_ = event;
197             parent_.notify(EVENT_MATCH);
198             return ndk::ScopedAStatus::ok();
199         }
eventMatchExpired(int8_t discoverySessionId,int32_t peerId)200         ::ndk::ScopedAStatus eventMatchExpired(int8_t discoverySessionId, int32_t peerId) override {
201             parent_.session_id_ = discoverySessionId;
202             parent_.peer_id_ = peerId;
203             parent_.notify(EVENT_MATCH_EXPIRED);
204             return ndk::ScopedAStatus::ok();
205         }
eventPublishTerminated(int8_t sessionId,const NanStatus & status)206         ::ndk::ScopedAStatus eventPublishTerminated(int8_t sessionId,
207                                                     const NanStatus& status) override {
208             parent_.session_id_ = sessionId;
209             parent_.status_ = status;
210             parent_.notify(EVENT_PUBLISH_TERMINATED);
211             return ndk::ScopedAStatus::ok();
212         }
eventSubscribeTerminated(int8_t sessionId,const NanStatus & status)213         ::ndk::ScopedAStatus eventSubscribeTerminated(int8_t sessionId,
214                                                       const NanStatus& status) override {
215             parent_.session_id_ = sessionId;
216             parent_.status_ = status;
217             parent_.notify(EVENT_SUBSCRIBE_TERMINATED);
218             return ndk::ScopedAStatus::ok();
219         }
eventTransmitFollowup(char16_t id,const NanStatus & status)220         ::ndk::ScopedAStatus eventTransmitFollowup(char16_t id, const NanStatus& status) override {
221             parent_.id_ = id;
222             parent_.status_ = status;
223             parent_.notify(EVENT_TRANSMIT_FOLLOWUP);
224             return ndk::ScopedAStatus::ok();
225         }
eventPairingConfirm(const NanPairingConfirmInd & event)226         ::ndk::ScopedAStatus eventPairingConfirm(const NanPairingConfirmInd& event) override {
227             parent_.nan_pairing_confirm_ind_ = event;
228             parent_.notify(EVENT_PAIRING_CONFIRM);
229             return ndk::ScopedAStatus::ok();
230         }
eventPairingRequest(const NanPairingRequestInd & event)231         ::ndk::ScopedAStatus eventPairingRequest(const NanPairingRequestInd& event) override {
232             parent_.nan_pairing_request_ind_ = event;
233             parent_.notify(EVENT_PAIRING_REQUEST);
234             return ndk::ScopedAStatus::ok();
235         }
eventBootstrappingConfirm(const NanBootstrappingConfirmInd & event)236         ::ndk::ScopedAStatus eventBootstrappingConfirm(
237                 const NanBootstrappingConfirmInd& event) override {
238             parent_.nan_bootstrapping_confirm_ind_ = event;
239             parent_.notify(EVENT_BOOTSTRAPPING_CONFIRM);
240             return ndk::ScopedAStatus::ok();
241         }
eventBootstrappingRequest(const NanBootstrappingRequestInd & event)242         ::ndk::ScopedAStatus eventBootstrappingRequest(
243                 const NanBootstrappingRequestInd& event) override {
244             parent_.nan_bootstrapping_request_ind_ = event;
245             parent_.notify(EVENT_BOOTSTRAPPING_REQUEST);
246             return ndk::ScopedAStatus::ok();
247         }
eventSuspensionModeChanged(const NanSuspensionModeChangeInd & event)248         ::ndk::ScopedAStatus eventSuspensionModeChanged(
249                 const NanSuspensionModeChangeInd& event) override {
250             parent_.nan_suspension_mode_change_ind_ = event;
251             parent_.notify(EVENT_SUSPENSION_MODE_CHANGE);
252             return ndk::ScopedAStatus::ok();
253         }
notifyCapabilitiesResponse(char16_t id,const NanStatus & status,const NanCapabilities & capabilities)254         ::ndk::ScopedAStatus notifyCapabilitiesResponse(
255                 char16_t id, const NanStatus& status,
256                 const NanCapabilities& capabilities) override {
257             parent_.id_ = id;
258             parent_.status_ = status;
259             parent_.capabilities_ = capabilities;
260             parent_.notify(NOTIFY_CAPABILITIES_RESPONSE);
261             return ndk::ScopedAStatus::ok();
262         }
notifyConfigResponse(char16_t id,const NanStatus & status)263         ::ndk::ScopedAStatus notifyConfigResponse(char16_t id, const NanStatus& status) override {
264             parent_.id_ = id;
265             parent_.status_ = status;
266             parent_.notify(NOTIFY_CONFIG_RESPONSE);
267             return ndk::ScopedAStatus::ok();
268         }
notifyCreateDataInterfaceResponse(char16_t id,const NanStatus & status)269         ::ndk::ScopedAStatus notifyCreateDataInterfaceResponse(char16_t id,
270                                                                const NanStatus& status) override {
271             parent_.id_ = id;
272             parent_.status_ = status;
273             parent_.notify(NOTIFY_CREATE_DATA_INTERFACE_RESPONSE);
274             return ndk::ScopedAStatus::ok();
275         }
notifyDeleteDataInterfaceResponse(char16_t id,const NanStatus & status)276         ::ndk::ScopedAStatus notifyDeleteDataInterfaceResponse(char16_t id,
277                                                                const NanStatus& status) override {
278             parent_.id_ = id;
279             parent_.status_ = status;
280             parent_.notify(NOTIFY_DELETE_DATA_INTERFACE_RESPONSE);
281             return ndk::ScopedAStatus::ok();
282         }
notifyDisableResponse(char16_t id,const NanStatus & status)283         ::ndk::ScopedAStatus notifyDisableResponse(char16_t id, const NanStatus& status) override {
284             parent_.id_ = id;
285             parent_.status_ = status;
286             parent_.notify(NOTIFY_DISABLE_RESPONSE);
287             return ndk::ScopedAStatus::ok();
288         }
notifyEnableResponse(char16_t id,const NanStatus & status)289         ::ndk::ScopedAStatus notifyEnableResponse(char16_t id, const NanStatus& status) override {
290             parent_.id_ = id;
291             parent_.status_ = status;
292             parent_.notify(NOTIFY_ENABLE_RESPONSE);
293             return ndk::ScopedAStatus::ok();
294         }
notifyInitiateDataPathResponse(char16_t id,const NanStatus & status,int32_t ndpInstanceId)295         ::ndk::ScopedAStatus notifyInitiateDataPathResponse(char16_t id, const NanStatus& status,
296                                                             int32_t ndpInstanceId) override {
297             parent_.id_ = id;
298             parent_.status_ = status;
299             parent_.ndp_instance_id_ = ndpInstanceId;
300             parent_.notify(NOTIFY_INITIATE_DATA_PATH_RESPONSE);
301             return ndk::ScopedAStatus::ok();
302         }
notifyRespondToDataPathIndicationResponse(char16_t id,const NanStatus & status)303         ::ndk::ScopedAStatus notifyRespondToDataPathIndicationResponse(
304                 char16_t id, const NanStatus& status) override {
305             parent_.id_ = id;
306             parent_.status_ = status;
307             parent_.notify(NOTIFY_RESPOND_TO_DATA_PATH_INDICATION_RESPONSE);
308             return ndk::ScopedAStatus::ok();
309         }
notifyStartPublishResponse(char16_t id,const NanStatus & status,int8_t sessionId)310         ::ndk::ScopedAStatus notifyStartPublishResponse(char16_t id, const NanStatus& status,
311                                                         int8_t sessionId) override {
312             parent_.id_ = id;
313             parent_.status_ = status;
314             parent_.session_id_ = sessionId;
315             parent_.notify(NOTIFY_START_PUBLISH_RESPONSE);
316             return ndk::ScopedAStatus::ok();
317         }
notifyStartSubscribeResponse(char16_t id,const NanStatus & status,int8_t sessionId)318         ::ndk::ScopedAStatus notifyStartSubscribeResponse(char16_t id, const NanStatus& status,
319                                                           int8_t sessionId) override {
320             parent_.id_ = id;
321             parent_.status_ = status;
322             parent_.session_id_ = sessionId;
323             parent_.notify(NOTIFY_START_SUBSCRIBE_RESPONSE);
324             return ndk::ScopedAStatus::ok();
325         }
notifyStopPublishResponse(char16_t id,const NanStatus & status)326         ::ndk::ScopedAStatus notifyStopPublishResponse(char16_t id,
327                                                        const NanStatus& status) override {
328             parent_.id_ = id;
329             parent_.status_ = status;
330             parent_.notify(NOTIFY_STOP_PUBLISH_RESPONSE);
331             return ndk::ScopedAStatus::ok();
332         }
notifyStopSubscribeResponse(char16_t id,const NanStatus & status)333         ::ndk::ScopedAStatus notifyStopSubscribeResponse(char16_t id,
334                                                          const NanStatus& status) override {
335             parent_.id_ = id;
336             parent_.status_ = status;
337             parent_.notify(NOTIFY_STOP_SUBSCRIBE_RESPONSE);
338             return ndk::ScopedAStatus::ok();
339         }
notifyTerminateDataPathResponse(char16_t id,const NanStatus & status)340         ::ndk::ScopedAStatus notifyTerminateDataPathResponse(char16_t id,
341                                                              const NanStatus& status) override {
342             parent_.id_ = id;
343             parent_.status_ = status;
344             parent_.notify(NOTIFY_TERMINATE_DATA_PATH_RESPONSE);
345             return ndk::ScopedAStatus::ok();
346         }
notifySuspendResponse(char16_t id,const NanStatus & status)347         ::ndk::ScopedAStatus notifySuspendResponse(char16_t id, const NanStatus& status) override {
348             parent_.id_ = id;
349             parent_.status_ = status;
350             parent_.notify(NOTIFY_SUSPEND_RESPONSE);
351             return ndk::ScopedAStatus::ok();
352         }
notifyResumeResponse(char16_t id,const NanStatus & status)353         ::ndk::ScopedAStatus notifyResumeResponse(char16_t id, const NanStatus& status) override {
354             parent_.id_ = id;
355             parent_.status_ = status;
356             parent_.notify(NOTIFY_RESUME_RESPONSE);
357             return ndk::ScopedAStatus::ok();
358         }
notifyTransmitFollowupResponse(char16_t id,const NanStatus & status)359         ::ndk::ScopedAStatus notifyTransmitFollowupResponse(char16_t id,
360                                                             const NanStatus& status) override {
361             parent_.id_ = id;
362             parent_.status_ = status;
363             parent_.notify(NOTIFY_TRANSMIT_FOLLOWUP_RESPONSE);
364             return ndk::ScopedAStatus::ok();
365         }
notifyInitiatePairingResponse(char16_t id,const NanStatus & status,int32_t pairingInstanceId)366         ::ndk::ScopedAStatus notifyInitiatePairingResponse(char16_t id, const NanStatus& status,
367                                                            int32_t pairingInstanceId) override {
368             parent_.id_ = id;
369             parent_.status_ = status;
370             parent_.pairing_instance_id_ = pairingInstanceId;
371             parent_.notify(NOTIFY_INITIATE_PAIRING_RESPONSE);
372             return ndk::ScopedAStatus::ok();
373         }
notifyRespondToPairingIndicationResponse(char16_t id,const NanStatus & status)374         ::ndk::ScopedAStatus notifyRespondToPairingIndicationResponse(
375                 char16_t id, const NanStatus& status) override {
376             parent_.id_ = id;
377             parent_.status_ = status;
378             parent_.notify(NOTIFY_RESPOND_TO_PAIRING_INDICATION_RESPONSE);
379             return ndk::ScopedAStatus::ok();
380         }
notifyInitiateBootstrappingResponse(char16_t id,const NanStatus & status,int32_t bootstrapppingInstanceId)381         ::ndk::ScopedAStatus notifyInitiateBootstrappingResponse(
382                 char16_t id, const NanStatus& status, int32_t bootstrapppingInstanceId) override {
383             parent_.id_ = id;
384             parent_.status_ = status;
385             parent_.bootstrappping_instance_id_ = bootstrapppingInstanceId;
386             parent_.notify(NOTIFY_INITIATE_BOOTSTRAPPING_RESPONSE);
387             return ndk::ScopedAStatus::ok();
388         }
notifyRespondToBootstrappingIndicationResponse(char16_t id,const NanStatus & status)389         ::ndk::ScopedAStatus notifyRespondToBootstrappingIndicationResponse(
390                 char16_t id, const NanStatus& status) override {
391             parent_.id_ = id;
392             parent_.status_ = status;
393             parent_.notify(NOTIFY_RESPOND_TO_BOOTSTRAPPING_INDICATION_RESPONSE);
394             return ndk::ScopedAStatus::ok();
395         }
notifyTerminatePairingResponse(char16_t id,const NanStatus & status)396         ::ndk::ScopedAStatus notifyTerminatePairingResponse(char16_t id,
397                                                             const NanStatus& status) override {
398             parent_.id_ = id;
399             parent_.status_ = status;
400             parent_.notify(NOTIFY_TERMINATE_PAIRING_RESPONSE);
401             return ndk::ScopedAStatus::ok();
402         }
403 
404       private:
405         WifiNanIfaceAidlTest& parent_;
406     };
407 
408   protected:
409     std::shared_ptr<IWifiNanIface> wifi_nan_iface_;
410     int interface_version_;
411     uint64_t callback_event_bitmap_;
412     uint16_t id_;
413     uint8_t session_id_;
414     uint32_t ndp_instance_id_;
415     uint32_t pairing_instance_id_;
416     uint32_t bootstrappping_instance_id_;
417     uint32_t peer_id_;
418     NanCapabilities capabilities_;
419     NanClusterEventInd nan_cluster_event_ind_;
420     NanDataPathConfirmInd nan_data_path_confirm_ind_;
421     NanDataPathRequestInd nan_data_path_request_ind_;
422     NanDataPathScheduleUpdateInd nan_data_path_schedule_update_ind_;
423     NanFollowupReceivedInd nan_followup_received_ind_;
424     NanMatchInd nan_match_ind_;
425     NanStatus status_;
426     NanPairingRequestInd nan_pairing_request_ind_;
427     NanPairingConfirmInd nan_pairing_confirm_ind_;
428     NanBootstrappingRequestInd nan_bootstrapping_request_ind_;
429     NanBootstrappingConfirmInd nan_bootstrapping_confirm_ind_;
430     NanSuspensionModeChangeInd nan_suspension_mode_change_ind_;
431 
getInstanceName()432     const char* getInstanceName() { return GetParam().c_str(); }
433 
434   private:
435     // synchronization objects
436     std::mutex mtx_;
437     std::condition_variable cv_;
438 };
439 
440 /*
441  * FailOnIfaceInvalid
442  * Ensure that API calls to an interface fail with code ERROR_WIFI_IFACE_INVALID
443  * after wifi is disabled.
444  */
TEST_P(WifiNanIfaceAidlTest,FailOnIfaceInvalid)445 TEST_P(WifiNanIfaceAidlTest, FailOnIfaceInvalid) {
446     stopWifiService(getInstanceName());
447     sleep(5);  // Ensure that all chips/interfaces are invalidated.
448     auto status = wifi_nan_iface_->getCapabilitiesRequest(0);
449     ASSERT_TRUE(checkStatusCode(&status, WifiStatusCode::ERROR_WIFI_IFACE_INVALID));
450 }
451 
452 /*
453  * EnableRequest - Invalid Args
454  */
TEST_P(WifiNanIfaceAidlTest,EnableRequest_InvalidArgs)455 TEST_P(WifiNanIfaceAidlTest, EnableRequest_InvalidArgs) {
456     uint16_t inputCmdId = 10;
457     callback_event_bitmap_ = 0;
458     NanEnableRequest nanEnableRequest = {};
459     NanConfigRequestSupplemental nanConfigRequestSupp = {};
460     auto status =
461             wifi_nan_iface_->enableRequest(inputCmdId, nanEnableRequest, nanConfigRequestSupp);
462     if (!checkStatusCode(&status, WifiStatusCode::ERROR_NOT_SUPPORTED)) {
463         ASSERT_TRUE(status.isOk());
464 
465         // Wait for a callback.
466         ASSERT_EQ(std::cv_status::no_timeout, wait(NOTIFY_ENABLE_RESPONSE));
467         ASSERT_TRUE(receivedCallback(NOTIFY_ENABLE_RESPONSE));
468         ASSERT_EQ(id_, inputCmdId);
469         ASSERT_EQ(status_.status, NanStatusCode::INVALID_ARGS);
470     }
471 }
472 
473 /*
474  * ConfigRequest - Invalid Args
475  */
TEST_P(WifiNanIfaceAidlTest,ConfigRequest_InvalidArgs)476 TEST_P(WifiNanIfaceAidlTest, ConfigRequest_InvalidArgs) {
477     uint16_t inputCmdId = 10;
478     callback_event_bitmap_ = 0;
479     NanConfigRequest nanConfigRequest = {};
480     NanConfigRequestSupplemental nanConfigRequestSupp = {};
481     auto status =
482             wifi_nan_iface_->configRequest(inputCmdId, nanConfigRequest, nanConfigRequestSupp);
483 
484     if (!checkStatusCode(&status, WifiStatusCode::ERROR_NOT_SUPPORTED)) {
485         ASSERT_TRUE(status.isOk());
486 
487         // Wait for a callback.
488         ASSERT_EQ(std::cv_status::no_timeout, wait(NOTIFY_CONFIG_RESPONSE));
489         ASSERT_TRUE(receivedCallback(NOTIFY_CONFIG_RESPONSE));
490         ASSERT_EQ(id_, inputCmdId);
491         ASSERT_EQ(status_.status, NanStatusCode::INVALID_ARGS);
492     }
493 }
494 
495 /*
496  * EnableRequest - Invalid Args in Shim Conversion
497  */
TEST_P(WifiNanIfaceAidlTest,EnableRequest_InvalidShimArgs)498 TEST_P(WifiNanIfaceAidlTest, EnableRequest_InvalidShimArgs) {
499     uint16_t inputCmdId = 10;
500     NanEnableRequest nanEnableRequest = {};
501     nanEnableRequest.configParams.numberOfPublishServiceIdsInBeacon = -15;  // must be > 0
502     NanConfigRequestSupplemental nanConfigRequestSupp = {};
503     auto status =
504             wifi_nan_iface_->enableRequest(inputCmdId, nanEnableRequest, nanConfigRequestSupp);
505     if (!checkStatusCode(&status, WifiStatusCode::ERROR_NOT_SUPPORTED)) {
506         ASSERT_TRUE(checkStatusCode(&status, WifiStatusCode::ERROR_INVALID_ARGS));
507     }
508 }
509 
510 /*
511  * ConfigRequest - Invalid Args in Shim Conversion
512  */
TEST_P(WifiNanIfaceAidlTest,ConfigRequest_InvalidShimArgs)513 TEST_P(WifiNanIfaceAidlTest, ConfigRequest_InvalidShimArgs) {
514     uint16_t inputCmdId = 10;
515     NanConfigRequest nanConfigRequest = {};
516     nanConfigRequest.numberOfPublishServiceIdsInBeacon = -15;  // must be > 0
517     NanConfigRequestSupplemental nanConfigRequestSupp = {};
518     auto status =
519             wifi_nan_iface_->configRequest(inputCmdId, nanConfigRequest, nanConfigRequestSupp);
520     if (!checkStatusCode(&status, WifiStatusCode::ERROR_NOT_SUPPORTED)) {
521         ASSERT_TRUE(checkStatusCode(&status, WifiStatusCode::ERROR_INVALID_ARGS));
522     }
523 }
524 
525 /*
526  * NotifyCapabilitiesResponse
527  */
TEST_P(WifiNanIfaceAidlTest,NotifyCapabilitiesResponse)528 TEST_P(WifiNanIfaceAidlTest, NotifyCapabilitiesResponse) {
529     uint16_t inputCmdId = 10;
530     callback_event_bitmap_ = 0;
531     EXPECT_TRUE(wifi_nan_iface_->getCapabilitiesRequest(inputCmdId).isOk());
532 
533     // Wait for a callback.
534     ASSERT_EQ(std::cv_status::no_timeout, wait(NOTIFY_CAPABILITIES_RESPONSE));
535     ASSERT_TRUE(receivedCallback(NOTIFY_CAPABILITIES_RESPONSE));
536     ASSERT_EQ(id_, inputCmdId);
537     ASSERT_EQ(status_.status, NanStatusCode::SUCCESS);
538 
539     // Check for reasonable capability values.
540     EXPECT_GT(capabilities_.maxConcurrentClusters, 0);
541     EXPECT_GT(capabilities_.maxPublishes, 0);
542     EXPECT_GT(capabilities_.maxSubscribes, 0);
543     EXPECT_EQ(capabilities_.maxServiceNameLen, 255);
544     EXPECT_EQ(capabilities_.maxMatchFilterLen, 255);
545     EXPECT_GT(capabilities_.maxTotalMatchFilterLen, 255);
546     EXPECT_EQ(capabilities_.maxServiceSpecificInfoLen, 255);
547     EXPECT_GE(capabilities_.maxExtendedServiceSpecificInfoLen, 255);
548     EXPECT_GT(capabilities_.maxNdiInterfaces, 0);
549     EXPECT_GT(capabilities_.maxNdpSessions, 0);
550     EXPECT_GT(capabilities_.maxAppInfoLen, 0);
551     EXPECT_GT(capabilities_.maxQueuedTransmitFollowupMsgs, 0);
552     EXPECT_GT(capabilities_.maxSubscribeInterfaceAddresses, 0);
553     EXPECT_NE(static_cast<int32_t>(capabilities_.supportedCipherSuites), 0);
554 }
555 
556 /*
557  * StartPublishRequest
558  */
TEST_P(WifiNanIfaceAidlTest,StartPublishRequest)559 TEST_P(WifiNanIfaceAidlTest, StartPublishRequest) {
560     uint16_t inputCmdId = 10;
561     NanBandSpecificConfig config24 = {};
562     config24.rssiClose = 60;
563     config24.rssiMiddle = 70;
564     config24.rssiCloseProximity = 60;
565     config24.dwellTimeMs = 200;
566     config24.scanPeriodSec = 20;
567     config24.validDiscoveryWindowIntervalVal = false;
568     config24.discoveryWindowIntervalVal = 0;
569 
570     NanBandSpecificConfig config5 = {};
571     config5.rssiClose = 60;
572     config5.rssiMiddle = 75;
573     config5.rssiCloseProximity = 60;
574     config5.dwellTimeMs = 200;
575     config5.scanPeriodSec = 20;
576     config5.validDiscoveryWindowIntervalVal = false;
577     config5.discoveryWindowIntervalVal = 0;
578 
579     NanEnableRequest req = {};
580     req.operateInBand[static_cast<int32_t>(NanBandIndex::NAN_BAND_24GHZ)] = true;
581     req.operateInBand[static_cast<int32_t>(NanBandIndex::NAN_BAND_5GHZ)] = false;
582     req.hopCountMax = 2;
583     req.configParams.masterPref = 0;
584     req.configParams.disableDiscoveryAddressChangeIndication = true;
585     req.configParams.disableStartedClusterIndication = true;
586     req.configParams.disableJoinedClusterIndication = true;
587     req.configParams.includePublishServiceIdsInBeacon = true;
588     req.configParams.numberOfPublishServiceIdsInBeacon = 0;
589     req.configParams.includeSubscribeServiceIdsInBeacon = true;
590     req.configParams.numberOfSubscribeServiceIdsInBeacon = 0;
591     req.configParams.rssiWindowSize = 8;
592     req.configParams.macAddressRandomizationIntervalSec = 1800;
593     req.configParams.bandSpecificConfig[static_cast<int32_t>(NanBandIndex::NAN_BAND_24GHZ)] =
594             config24;
595     req.configParams.bandSpecificConfig[static_cast<int32_t>(NanBandIndex::NAN_BAND_5GHZ)] =
596             config5;
597 
598     req.debugConfigs.validClusterIdVals = true;
599     req.debugConfigs.clusterIdTopRangeVal = 65535;
600     req.debugConfigs.clusterIdBottomRangeVal = 0;
601     req.debugConfigs.validIntfAddrVal = false;
602     req.debugConfigs.validOuiVal = false;
603     req.debugConfigs.ouiVal = 0;
604     req.debugConfigs.validRandomFactorForceVal = false;
605     req.debugConfigs.randomFactorForceVal = 0;
606     req.debugConfigs.validHopCountForceVal = false;
607     req.debugConfigs.hopCountForceVal = 0;
608     req.debugConfigs.validDiscoveryChannelVal = false;
609     req.debugConfigs.discoveryChannelMhzVal[static_cast<int32_t>(NanBandIndex::NAN_BAND_24GHZ)] = 0;
610     req.debugConfigs.discoveryChannelMhzVal[static_cast<int32_t>(NanBandIndex::NAN_BAND_5GHZ)] = 0;
611     req.debugConfigs.validUseBeaconsInBandVal = false;
612     req.debugConfigs.useBeaconsInBandVal[static_cast<int32_t>(NanBandIndex::NAN_BAND_24GHZ)] = true;
613     req.debugConfigs.useBeaconsInBandVal[static_cast<int32_t>(NanBandIndex::NAN_BAND_5GHZ)] = true;
614     req.debugConfigs.validUseSdfInBandVal = false;
615     req.debugConfigs.useSdfInBandVal[static_cast<int32_t>(NanBandIndex::NAN_BAND_24GHZ)] = true;
616     req.debugConfigs.useSdfInBandVal[static_cast<int32_t>(NanBandIndex::NAN_BAND_5GHZ)] = true;
617 
618     NanConfigRequestSupplemental nanConfigRequestSupp = {};
619     nanConfigRequestSupp.discoveryBeaconIntervalMs = 20;
620     nanConfigRequestSupp.numberOfSpatialStreamsInDiscovery = 0;
621     nanConfigRequestSupp.enableDiscoveryWindowEarlyTermination = false;
622 
623     callback_event_bitmap_ = 0;
624     auto status = wifi_nan_iface_->enableRequest(inputCmdId, req, nanConfigRequestSupp);
625     if (!checkStatusCode(&status, WifiStatusCode::ERROR_NOT_SUPPORTED)) {
626         ASSERT_TRUE(status.isOk());
627 
628         // Wait for a callback.
629         ASSERT_EQ(std::cv_status::no_timeout, wait(NOTIFY_ENABLE_RESPONSE));
630         ASSERT_TRUE(receivedCallback(NOTIFY_ENABLE_RESPONSE));
631         ASSERT_EQ(id_, inputCmdId);
632         ASSERT_EQ(status_.status, NanStatusCode::SUCCESS);
633     }
634 
635     NanPublishRequest nanPublishRequest = {};
636     nanPublishRequest.baseConfigs.sessionId = 0;
637     nanPublishRequest.baseConfigs.ttlSec = 0;
638     nanPublishRequest.baseConfigs.discoveryWindowPeriod = 1;
639     nanPublishRequest.baseConfigs.discoveryCount = 0;
640     nanPublishRequest.baseConfigs.serviceName = {97};
641     nanPublishRequest.baseConfigs.discoveryMatchIndicator = NanMatchAlg::MATCH_NEVER;
642     nanPublishRequest.baseConfigs.useRssiThreshold = false;
643     nanPublishRequest.baseConfigs.disableDiscoveryTerminationIndication = false;
644     nanPublishRequest.baseConfigs.disableMatchExpirationIndication = true;
645     nanPublishRequest.baseConfigs.disableFollowupReceivedIndication = false;
646     nanPublishRequest.baseConfigs.securityConfig.securityType = NanDataPathSecurityType::OPEN;
647     nanPublishRequest.autoAcceptDataPathRequests = false;
648     nanPublishRequest.publishType = NanPublishType::UNSOLICITED;
649     nanPublishRequest.txType = NanTxType::BROADCAST;
650     if (interface_version_ >= 2) {
651         LOG(INFO) << "Including vendor data in Publish request";
652         nanPublishRequest.vendorData = kTestVendorDataOptional;
653     }
654 
655     status = wifi_nan_iface_->startPublishRequest(inputCmdId + 1, nanPublishRequest);
656     if (!checkStatusCode(&status, WifiStatusCode::ERROR_NOT_SUPPORTED)) {
657         ASSERT_TRUE(status.isOk());
658 
659         // Wait for a callback.
660         ASSERT_EQ(std::cv_status::no_timeout, wait(NOTIFY_START_PUBLISH_RESPONSE));
661         ASSERT_TRUE(receivedCallback(NOTIFY_START_PUBLISH_RESPONSE));
662         ASSERT_EQ(id_, inputCmdId + 1);
663         ASSERT_EQ(status_.status, NanStatusCode::SUCCESS);
664     }
665 }
666 
667 /*
668  * RespondToDataPathIndicationRequest - Invalid Args
669  */
TEST_P(WifiNanIfaceAidlTest,RespondToDataPathIndicationRequest_InvalidArgs)670 TEST_P(WifiNanIfaceAidlTest, RespondToDataPathIndicationRequest_InvalidArgs) {
671     uint16_t inputCmdId = 10;
672     callback_event_bitmap_ = 0;
673     NanRespondToDataPathIndicationRequest nanRespondToDataPathIndicationRequest = {};
674     nanRespondToDataPathIndicationRequest.ifaceName = "AwareInterfaceNameTooLong";
675     auto status = wifi_nan_iface_->respondToDataPathIndicationRequest(
676             inputCmdId, nanRespondToDataPathIndicationRequest);
677 
678     if (!checkStatusCode(&status, WifiStatusCode::ERROR_NOT_SUPPORTED)) {
679         ASSERT_EQ(status.getServiceSpecificError(),
680                   static_cast<int32_t>(WifiStatusCode::ERROR_INVALID_ARGS));
681     }
682 }
683 
684 /*
685  * InitiateDataPathRequest - Invalid Args
686  */
TEST_P(WifiNanIfaceAidlTest,InitiateDataPathRequest_InvalidArgs)687 TEST_P(WifiNanIfaceAidlTest, InitiateDataPathRequest_InvalidArgs) {
688     uint16_t inputCmdId = 10;
689     callback_event_bitmap_ = 0;
690     NanInitiateDataPathRequest nanInitiateDataPathRequest = {};
691     nanInitiateDataPathRequest.ifaceName = "AwareInterfaceNameTooLong";
692     auto status = wifi_nan_iface_->initiateDataPathRequest(inputCmdId, nanInitiateDataPathRequest);
693 
694     if (!checkStatusCode(&status, WifiStatusCode::ERROR_NOT_SUPPORTED)) {
695         ASSERT_EQ(status.getServiceSpecificError(),
696                   static_cast<int32_t>(WifiStatusCode::ERROR_INVALID_ARGS));
697     }
698 }
699 
700 GTEST_ALLOW_UNINSTANTIATED_PARAMETERIZED_TEST(WifiNanIfaceAidlTest);
701 INSTANTIATE_TEST_SUITE_P(WifiTest, WifiNanIfaceAidlTest,
702                          testing::ValuesIn(android::getAidlHalInstanceNames(IWifi::descriptor)),
703                          android::PrintInstanceNameToString);
704 
main(int argc,char ** argv)705 int main(int argc, char** argv) {
706     ::testing::InitGoogleTest(&argc, argv);
707     android::ProcessState::self()->setThreadPoolMaxThreadCount(1);
708     android::ProcessState::self()->startThreadPool();
709     return RUN_ALL_TESTS();
710 }
711