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 
19 #include <android/hardware/wifi/1.0/IWifiChip.h>
20 #include <android/hardware/wifi/1.3/IWifiChip.h>
21 
22 #include <VtsHalHidlTargetTestBase.h>
23 
24 #include "wifi_hidl_call_util.h"
25 #include "wifi_hidl_test_utils.h"
26 
27 using ::android::sp;
28 using ::android::hardware::hidl_string;
29 using ::android::hardware::hidl_vec;
30 using ::android::hardware::wifi::V1_0::IfaceType;
31 using ::android::hardware::wifi::V1_0::ChipId;
32 using ::android::hardware::wifi::V1_0::ChipModeId;
33 using ::android::hardware::wifi::V1_0::WifiDebugRingBufferStatus;
34 using ::android::hardware::wifi::V1_0::WifiDebugRingBufferVerboseLevel;
35 using ::android::hardware::wifi::V1_0::WifiDebugHostWakeReasonStats;
36 using ::android::hardware::wifi::V1_0::WifiStatus;
37 using ::android::hardware::wifi::V1_0::WifiStatusCode;
38 using ::android::hardware::wifi::V1_0::IWifiChip;
39 using ::android::hardware::wifi::V1_0::IWifiApIface;
40 using ::android::hardware::wifi::V1_0::IWifiIface;
41 using ::android::hardware::wifi::V1_0::IWifiNanIface;
42 using ::android::hardware::wifi::V1_0::IWifiP2pIface;
43 using ::android::hardware::wifi::V1_0::IWifiRttController;
44 using ::android::hardware::wifi::V1_0::IWifiStaIface;
45 
46 extern WifiHidlEnvironment* gEnv;
47 
48 namespace {
49 constexpr WifiDebugRingBufferVerboseLevel kDebugRingBufferVerboseLvl =
50     WifiDebugRingBufferVerboseLevel::VERBOSE;
51 constexpr uint32_t kDebugRingBufferMaxInterval = 5;
52 constexpr uint32_t kDebugRingBufferMaxDataSize = 1024;
53 
54 /**
55  * Check if any of the ring buffer capabilities are set.
56  */
hasAnyRingBufferCapabilities(uint32_t caps)57 bool hasAnyRingBufferCapabilities(uint32_t caps) {
58     return (caps &
59             (IWifiChip::ChipCapabilityMask::DEBUG_RING_BUFFER_CONNECT_EVENT |
60              IWifiChip::ChipCapabilityMask::DEBUG_RING_BUFFER_POWER_EVENT |
61              IWifiChip::ChipCapabilityMask::DEBUG_RING_BUFFER_WAKELOCK_EVENT |
62              IWifiChip::ChipCapabilityMask::DEBUG_RING_BUFFER_VENDOR_DATA));
63 }
64 }  // namespace
65 
66 /**
67  * Fixture to use for all Wifi chip HIDL interface tests.
68  */
69 class WifiChipHidlTest : public ::testing::VtsHalHidlTargetTestBase {
70    public:
SetUp()71     virtual void SetUp() override {
72         wifi_chip_ = getWifiChip();
73         ASSERT_NE(nullptr, wifi_chip_.get());
74     }
75 
TearDown()76     virtual void TearDown() override { stopWifi(); }
77 
78    protected:
79     // Helper function to configure the Chip in one of the supported modes.
80     // Most of the non-mode-configuration-related methods require chip
81     // to be first configured.
configureChipForIfaceType(IfaceType type,bool expectSuccess)82     ChipModeId configureChipForIfaceType(IfaceType type, bool expectSuccess) {
83         ChipModeId mode_id;
84         EXPECT_EQ(expectSuccess,
85             configureChipToSupportIfaceType(wifi_chip_, type, &mode_id));
86         return mode_id;
87     }
88 
configureChipForStaIfaceAndGetCapabilities()89     uint32_t configureChipForStaIfaceAndGetCapabilities() {
90         configureChipForIfaceType(IfaceType::STA, true);
91 
92         sp<::android::hardware::wifi::V1_3::IWifiChip> chip_converted =
93             ::android::hardware::wifi::V1_3::IWifiChip::castFrom(wifi_chip_);
94 
95         std::pair<WifiStatus, uint32_t> status_and_caps;
96 
97         if (chip_converted != nullptr) {
98             // Call the newer HAL version
99             status_and_caps = HIDL_INVOKE(chip_converted, getCapabilities_1_3);
100         } else {
101             status_and_caps = HIDL_INVOKE(wifi_chip_, getCapabilities);
102         }
103 
104         if (status_and_caps.first.code != WifiStatusCode::SUCCESS) {
105             EXPECT_EQ(WifiStatusCode::ERROR_NOT_SUPPORTED, status_and_caps.first.code);
106             return 0;
107         }
108         return status_and_caps.second;
109     }
110 
getIfaceName(const sp<IWifiIface> & iface)111     std::string getIfaceName(const sp<IWifiIface>& iface) {
112         const auto& status_and_name = HIDL_INVOKE(iface, getName);
113         EXPECT_EQ(WifiStatusCode::SUCCESS, status_and_name.first.code);
114         return status_and_name.second;
115     }
116 
createApIface(sp<IWifiApIface> * ap_iface)117     WifiStatusCode createApIface(sp<IWifiApIface>* ap_iface) {
118         const auto& status_and_iface = HIDL_INVOKE(wifi_chip_, createApIface);
119         *ap_iface = status_and_iface.second;
120         return status_and_iface.first.code;
121     }
122 
removeApIface(const std::string & name)123     WifiStatusCode removeApIface(const std::string& name) {
124         return HIDL_INVOKE(wifi_chip_, removeApIface, name).code;
125     }
126 
createNanIface(sp<IWifiNanIface> * nan_iface)127     WifiStatusCode createNanIface(sp<IWifiNanIface>* nan_iface) {
128         const auto& status_and_iface = HIDL_INVOKE(wifi_chip_, createNanIface);
129         *nan_iface = status_and_iface.second;
130         return status_and_iface.first.code;
131     }
132 
removeNanIface(const std::string & name)133     WifiStatusCode removeNanIface(const std::string& name) {
134         return HIDL_INVOKE(wifi_chip_, removeNanIface, name).code;
135     }
136 
createP2pIface(sp<IWifiP2pIface> * p2p_iface)137     WifiStatusCode createP2pIface(sp<IWifiP2pIface>* p2p_iface) {
138         const auto& status_and_iface = HIDL_INVOKE(wifi_chip_, createP2pIface);
139         *p2p_iface = status_and_iface.second;
140         return status_and_iface.first.code;
141     }
142 
removeP2pIface(const std::string & name)143     WifiStatusCode removeP2pIface(const std::string& name) {
144         return HIDL_INVOKE(wifi_chip_, removeP2pIface, name).code;
145     }
146 
createStaIface(sp<IWifiStaIface> * sta_iface)147     WifiStatusCode createStaIface(sp<IWifiStaIface>* sta_iface) {
148         const auto& status_and_iface = HIDL_INVOKE(wifi_chip_, createStaIface);
149         *sta_iface = status_and_iface.second;
150         return status_and_iface.first.code;
151     }
152 
removeStaIface(const std::string & name)153     WifiStatusCode removeStaIface(const std::string& name) {
154         return HIDL_INVOKE(wifi_chip_, removeStaIface, name).code;
155     }
156 
157     sp<IWifiChip> wifi_chip_;
158 };
159 
160 /*
161  * Create:
162  * Ensures that an instance of the IWifiChip proxy object is
163  * successfully created.
164  */
TEST(WifiChipHidlTestNoFixture,Create)165 TEST(WifiChipHidlTestNoFixture, Create) {
166     EXPECT_NE(nullptr, getWifiChip().get());
167     stopWifi();
168 }
169 
170 /*
171  * GetId:
172  */
TEST_F(WifiChipHidlTest,GetId)173 TEST_F(WifiChipHidlTest, GetId) {
174     EXPECT_EQ(WifiStatusCode::SUCCESS,
175               HIDL_INVOKE(wifi_chip_, getId).first.code);
176 }
177 
178 /*
179  * GetAvailableMode:
180  */
TEST_F(WifiChipHidlTest,GetAvailableModes)181 TEST_F(WifiChipHidlTest, GetAvailableModes) {
182     const auto& status_and_modes = HIDL_INVOKE(wifi_chip_, getAvailableModes);
183     EXPECT_EQ(WifiStatusCode::SUCCESS, status_and_modes.first.code);
184     EXPECT_LT(0u, status_and_modes.second.size());
185 }
186 
187 /*
188  * ConfigureChip:
189  */
TEST_F(WifiChipHidlTest,ConfigureChip)190 TEST_F(WifiChipHidlTest, ConfigureChip) {
191     const auto& status_and_modes = HIDL_INVOKE(wifi_chip_, getAvailableModes);
192     EXPECT_EQ(WifiStatusCode::SUCCESS, status_and_modes.first.code);
193     EXPECT_LT(0u, status_and_modes.second.size());
194     for (const auto& mode : status_and_modes.second) {
195         // configureChip() requires to be called with a fresh IWifiChip object.
196         wifi_chip_ = getWifiChip();
197         ASSERT_NE(nullptr, wifi_chip_.get());
198         EXPECT_EQ(WifiStatusCode::SUCCESS,
199                   HIDL_INVOKE(wifi_chip_, configureChip, mode.id).code);
200         stopWifi();
201         // Sleep for 5 milliseconds between each wifi state toggle.
202         usleep(5000);
203     }
204 }
205 
206 /*
207  * GetCapabilities:
208  */
TEST_F(WifiChipHidlTest,GetCapabilities)209 TEST_F(WifiChipHidlTest, GetCapabilities) {
210     configureChipForIfaceType(IfaceType::STA, true);
211     const auto& status_and_caps = HIDL_INVOKE(wifi_chip_, getCapabilities);
212     if (status_and_caps.first.code != WifiStatusCode::SUCCESS) {
213         EXPECT_EQ(WifiStatusCode::ERROR_NOT_SUPPORTED, status_and_caps.first.code);
214         return;
215     }
216     EXPECT_NE(0u, status_and_caps.second);
217 }
218 
219 /*
220  * GetMode:
221  */
TEST_F(WifiChipHidlTest,GetMode)222 TEST_F(WifiChipHidlTest, GetMode) {
223     ChipModeId chip_mode_id = configureChipForIfaceType(IfaceType::STA, true);
224     const auto& status_and_mode = HIDL_INVOKE(wifi_chip_, getMode);
225     EXPECT_EQ(WifiStatusCode::SUCCESS, status_and_mode.first.code);
226     EXPECT_EQ(chip_mode_id, status_and_mode.second);
227 }
228 
229 /*
230  * RequestChipDebugInfo:
231  */
TEST_F(WifiChipHidlTest,RequestChipDebugInfo)232 TEST_F(WifiChipHidlTest, RequestChipDebugInfo) {
233     configureChipForIfaceType(IfaceType::STA, true);
234     const auto& status_and_chip_info =
235         HIDL_INVOKE(wifi_chip_, requestChipDebugInfo);
236     EXPECT_EQ(WifiStatusCode::SUCCESS, status_and_chip_info.first.code);
237     EXPECT_LT(0u, status_and_chip_info.second.driverDescription.size());
238     EXPECT_LT(0u, status_and_chip_info.second.firmwareDescription.size());
239 }
240 
241 /*
242  * RequestFirmwareDebugDump
243  */
TEST_F(WifiChipHidlTest,RequestFirmwareDebugDump)244 TEST_F(WifiChipHidlTest, RequestFirmwareDebugDump) {
245     uint32_t caps = configureChipForStaIfaceAndGetCapabilities();
246     const auto& status_and_firmware_dump =
247         HIDL_INVOKE(wifi_chip_, requestFirmwareDebugDump);
248     if (caps & IWifiChip::ChipCapabilityMask::DEBUG_MEMORY_FIRMWARE_DUMP) {
249         EXPECT_EQ(WifiStatusCode::SUCCESS, status_and_firmware_dump.first.code);
250     } else {
251         EXPECT_EQ(WifiStatusCode::ERROR_NOT_SUPPORTED,
252                   status_and_firmware_dump.first.code);
253     }
254 }
255 
256 /*
257  * RequestDriverDebugDump
258  */
TEST_F(WifiChipHidlTest,RequestDriverDebugDump)259 TEST_F(WifiChipHidlTest, RequestDriverDebugDump) {
260     uint32_t caps = configureChipForStaIfaceAndGetCapabilities();
261     const auto& status_and_driver_dump =
262         HIDL_INVOKE(wifi_chip_, requestDriverDebugDump);
263     if (caps & IWifiChip::ChipCapabilityMask::DEBUG_MEMORY_DRIVER_DUMP) {
264         EXPECT_EQ(WifiStatusCode::SUCCESS, status_and_driver_dump.first.code);
265     } else {
266       // API semantics (today) are such that function cannot be called if not capable!
267       //
268       //  EXPECT_EQ(WifiStatusCode::ERROR_NOT_SUPPORTED,
269       //            status_and_driver_dump.first.code);
270     }
271 }
272 
273 /*
274  * GetDebugRingBuffersStatus
275  */
TEST_F(WifiChipHidlTest,GetDebugRingBuffersStatus)276 TEST_F(WifiChipHidlTest, GetDebugRingBuffersStatus) {
277     uint32_t caps = configureChipForStaIfaceAndGetCapabilities();
278     const auto& status_and_ring_buffer_status =
279         HIDL_INVOKE(wifi_chip_, getDebugRingBuffersStatus);
280     if (hasAnyRingBufferCapabilities(caps)) {
281         EXPECT_EQ(WifiStatusCode::SUCCESS,
282                   status_and_ring_buffer_status.first.code);
283         for (const auto& ring_buffer : status_and_ring_buffer_status.second) {
284             EXPECT_LT(0u, ring_buffer.ringName.size());
285         }
286     } else {
287         EXPECT_EQ(WifiStatusCode::ERROR_NOT_SUPPORTED,
288                   status_and_ring_buffer_status.first.code);
289     }
290 }
291 
292 /*
293  * StartLoggingToDebugRingBuffer
294  */
TEST_F(WifiChipHidlTest,StartLoggingToDebugRingBuffer)295 TEST_F(WifiChipHidlTest, StartLoggingToDebugRingBuffer) {
296     uint32_t caps = configureChipForStaIfaceAndGetCapabilities();
297     std::string ring_name;
298     const auto& status_and_ring_buffer_status =
299         HIDL_INVOKE(wifi_chip_, getDebugRingBuffersStatus);
300     if (hasAnyRingBufferCapabilities(caps)) {
301         EXPECT_EQ(WifiStatusCode::SUCCESS,
302                   status_and_ring_buffer_status.first.code);
303         ASSERT_LT(0u, status_and_ring_buffer_status.second.size());
304         ring_name = status_and_ring_buffer_status.second[0].ringName.c_str();
305     } else {
306         EXPECT_EQ(WifiStatusCode::ERROR_NOT_SUPPORTED,
307                   status_and_ring_buffer_status.first.code);
308     }
309     const auto& status =
310         HIDL_INVOKE(wifi_chip_, startLoggingToDebugRingBuffer, ring_name,
311                     kDebugRingBufferVerboseLvl, kDebugRingBufferMaxInterval,
312                     kDebugRingBufferMaxDataSize);
313     if (hasAnyRingBufferCapabilities(caps)) {
314         EXPECT_EQ(WifiStatusCode::SUCCESS, status.code);
315     } else {
316         EXPECT_EQ(WifiStatusCode::ERROR_NOT_SUPPORTED, status.code);
317     }
318 }
319 
320 /*
321  * ForceDumpToDebugRingBuffer
322  */
TEST_F(WifiChipHidlTest,ForceDumpToDebugRingBuffer)323 TEST_F(WifiChipHidlTest, ForceDumpToDebugRingBuffer) {
324     uint32_t caps = configureChipForStaIfaceAndGetCapabilities();
325     std::string ring_name;
326     const auto& status_and_ring_buffer_status =
327         HIDL_INVOKE(wifi_chip_, getDebugRingBuffersStatus);
328     if (hasAnyRingBufferCapabilities(caps)) {
329         EXPECT_EQ(WifiStatusCode::SUCCESS,
330                   status_and_ring_buffer_status.first.code);
331         ASSERT_LT(0u, status_and_ring_buffer_status.second.size());
332         ring_name = status_and_ring_buffer_status.second[0].ringName.c_str();
333     } else {
334         EXPECT_EQ(WifiStatusCode::ERROR_NOT_SUPPORTED,
335                   status_and_ring_buffer_status.first.code);
336     }
337     const auto& status =
338         HIDL_INVOKE(wifi_chip_, forceDumpToDebugRingBuffer, ring_name);
339     if (hasAnyRingBufferCapabilities(caps)) {
340         EXPECT_EQ(WifiStatusCode::SUCCESS, status.code);
341     } else {
342         EXPECT_EQ(WifiStatusCode::ERROR_NOT_SUPPORTED, status.code);
343     }
344 }
345 
346 /*
347  * GetDebugHostWakeReasonStats
348  */
TEST_F(WifiChipHidlTest,GetDebugHostWakeReasonStats)349 TEST_F(WifiChipHidlTest, GetDebugHostWakeReasonStats) {
350     uint32_t caps = configureChipForStaIfaceAndGetCapabilities();
351     const auto& status_and_debug_wake_reason =
352         HIDL_INVOKE(wifi_chip_, getDebugHostWakeReasonStats);
353     if (caps & IWifiChip::ChipCapabilityMask::DEBUG_HOST_WAKE_REASON_STATS) {
354         EXPECT_EQ(WifiStatusCode::SUCCESS,
355                   status_and_debug_wake_reason.first.code);
356     } else {
357         EXPECT_EQ(WifiStatusCode::ERROR_NOT_SUPPORTED,
358                   status_and_debug_wake_reason.first.code);
359     }
360 }
361 
362 /*
363  * CreateApIface
364  * Configures the chip in AP mode and ensures that at least 1 iface creation
365  * succeeds.
366  */
TEST_F(WifiChipHidlTest,CreateApIface)367 TEST_F(WifiChipHidlTest, CreateApIface) {
368     if (!gEnv->isSoftApOn) return;
369     configureChipForIfaceType(IfaceType::AP, true);
370 
371     sp<IWifiApIface> iface;
372     EXPECT_EQ(WifiStatusCode::SUCCESS, createApIface(&iface));
373     EXPECT_NE(nullptr, iface.get());
374 }
375 
376 /*
377  * GetApIfaceNames
378  * Configures the chip in AP mode and ensures that the iface list is empty
379  * before creating the iface. Then, create the iface and ensure that
380  * iface name is returned via the list.
381  */
TEST_F(WifiChipHidlTest,GetApIfaceNames)382 TEST_F(WifiChipHidlTest, GetApIfaceNames) {
383     if (!gEnv->isSoftApOn) return;
384     configureChipForIfaceType(IfaceType::AP, true);
385 
386     const auto& status_and_iface_names1 =
387         HIDL_INVOKE(wifi_chip_, getApIfaceNames);
388     EXPECT_EQ(WifiStatusCode::SUCCESS, status_and_iface_names1.first.code);
389     EXPECT_EQ(0u, status_and_iface_names1.second.size());
390 
391     sp<IWifiApIface> iface;
392     EXPECT_EQ(WifiStatusCode::SUCCESS, createApIface(&iface));
393     EXPECT_NE(nullptr, iface.get());
394 
395     std::string iface_name = getIfaceName(iface);
396     const auto& status_and_iface_names2 =
397         HIDL_INVOKE(wifi_chip_, getApIfaceNames);
398     EXPECT_EQ(WifiStatusCode::SUCCESS, status_and_iface_names2.first.code);
399     EXPECT_EQ(1u, status_and_iface_names2.second.size());
400     EXPECT_EQ(iface_name, status_and_iface_names2.second[0]);
401 
402     EXPECT_EQ(WifiStatusCode::SUCCESS, removeApIface(iface_name));
403     const auto& status_and_iface_names3 =
404         HIDL_INVOKE(wifi_chip_, getApIfaceNames);
405     EXPECT_EQ(WifiStatusCode::SUCCESS, status_and_iface_names3.first.code);
406     EXPECT_EQ(0u, status_and_iface_names3.second.size());
407 }
408 
409 /*
410  * GetApIface
411  * Configures the chip in AP mode and create an iface. Then, retrieve
412  * the iface object using the correct name and ensure any other name
413  * doesn't retrieve an iface object.
414  */
TEST_F(WifiChipHidlTest,GetApIface)415 TEST_F(WifiChipHidlTest, GetApIface) {
416     if (!gEnv->isSoftApOn) return;
417     configureChipForIfaceType(IfaceType::AP, true);
418 
419     sp<IWifiApIface> ap_iface;
420     EXPECT_EQ(WifiStatusCode::SUCCESS, createApIface(&ap_iface));
421     EXPECT_NE(nullptr, ap_iface.get());
422 
423     std::string iface_name = getIfaceName(ap_iface);
424     const auto& status_and_iface1 =
425         HIDL_INVOKE(wifi_chip_, getApIface, iface_name);
426     EXPECT_EQ(WifiStatusCode::SUCCESS, status_and_iface1.first.code);
427     EXPECT_NE(nullptr, status_and_iface1.second.get());
428 
429     std::string invalid_name = iface_name + "0";
430     const auto& status_and_iface2 =
431         HIDL_INVOKE(wifi_chip_, getApIface, invalid_name);
432     EXPECT_EQ(WifiStatusCode::ERROR_INVALID_ARGS, status_and_iface2.first.code);
433     EXPECT_EQ(nullptr, status_and_iface2.second.get());
434 }
435 
436 /*
437  * RemoveApIface
438  * Configures the chip in AP mode and create an iface. Then, remove
439  * the iface object using the correct name and ensure any other name
440  * doesn't remove the iface.
441  */
TEST_F(WifiChipHidlTest,RemoveApIface)442 TEST_F(WifiChipHidlTest, RemoveApIface) {
443     if (!gEnv->isSoftApOn) return;
444     configureChipForIfaceType(IfaceType::AP, true);
445 
446     sp<IWifiApIface> ap_iface;
447     EXPECT_EQ(WifiStatusCode::SUCCESS, createApIface(&ap_iface));
448     EXPECT_NE(nullptr, ap_iface.get());
449 
450     std::string iface_name = getIfaceName(ap_iface);
451     std::string invalid_name = iface_name + "0";
452     EXPECT_EQ(WifiStatusCode::ERROR_INVALID_ARGS, removeApIface(invalid_name));
453     EXPECT_EQ(WifiStatusCode::SUCCESS, removeApIface(iface_name));
454 
455     // No such iface exists now. So, this should return failure.
456     EXPECT_EQ(WifiStatusCode::ERROR_INVALID_ARGS, removeApIface(iface_name));
457 }
458 
459 /*
460  * CreateNanIface
461  * Configures the chip in NAN mode and ensures that at least 1 iface creation
462  * succeeds.
463  */
TEST_F(WifiChipHidlTest,CreateNanIface)464 TEST_F(WifiChipHidlTest, CreateNanIface) {
465     if (!gEnv->isNanOn) return;
466     configureChipForIfaceType(IfaceType::NAN, gEnv->isNanOn);
467 
468     sp<IWifiNanIface> iface;
469     ASSERT_EQ(WifiStatusCode::SUCCESS, createNanIface(&iface));
470     EXPECT_NE(nullptr, iface.get());
471 }
472 
473 /*
474  * GetNanIfaceNames
475  * Configures the chip in NAN mode and ensures that the iface list is empty
476  * before creating the iface. Then, create the iface and ensure that
477  * iface name is returned via the list.
478  */
TEST_F(WifiChipHidlTest,GetNanIfaceNames)479 TEST_F(WifiChipHidlTest, GetNanIfaceNames) {
480     if (!gEnv->isNanOn) return;
481     configureChipForIfaceType(IfaceType::NAN, gEnv->isNanOn);
482 
483     const auto& status_and_iface_names1 =
484         HIDL_INVOKE(wifi_chip_, getNanIfaceNames);
485     ASSERT_EQ(WifiStatusCode::SUCCESS, status_and_iface_names1.first.code);
486     EXPECT_EQ(0u, status_and_iface_names1.second.size());
487 
488     sp<IWifiNanIface> iface;
489     EXPECT_EQ(WifiStatusCode::SUCCESS, createNanIface(&iface));
490     EXPECT_NE(nullptr, iface.get());
491 
492     std::string iface_name = getIfaceName(iface);
493     const auto& status_and_iface_names2 =
494         HIDL_INVOKE(wifi_chip_, getNanIfaceNames);
495     EXPECT_EQ(WifiStatusCode::SUCCESS, status_and_iface_names2.first.code);
496     EXPECT_EQ(1u, status_and_iface_names2.second.size());
497     EXPECT_EQ(iface_name, status_and_iface_names2.second[0]);
498 
499     EXPECT_EQ(WifiStatusCode::SUCCESS, removeNanIface(iface_name));
500     const auto& status_and_iface_names3 =
501         HIDL_INVOKE(wifi_chip_, getNanIfaceNames);
502     EXPECT_EQ(WifiStatusCode::SUCCESS, status_and_iface_names3.first.code);
503     EXPECT_EQ(0u, status_and_iface_names3.second.size());
504 }
505 
506 /*
507  * GetNanIface
508  * Configures the chip in NAN mode and create an iface. Then, retrieve
509  * the iface object using the correct name and ensure any other name
510  * doesn't retrieve an iface object.
511  */
TEST_F(WifiChipHidlTest,GetNanIface)512 TEST_F(WifiChipHidlTest, GetNanIface) {
513     if (!gEnv->isNanOn) return;
514     configureChipForIfaceType(IfaceType::NAN, gEnv->isNanOn);
515 
516     sp<IWifiNanIface> nan_iface;
517     EXPECT_EQ(WifiStatusCode::SUCCESS, createNanIface(&nan_iface));
518     EXPECT_NE(nullptr, nan_iface.get());
519 
520     std::string iface_name = getIfaceName(nan_iface);
521     const auto& status_and_iface1 =
522         HIDL_INVOKE(wifi_chip_, getNanIface, iface_name);
523     EXPECT_EQ(WifiStatusCode::SUCCESS, status_and_iface1.first.code);
524     EXPECT_NE(nullptr, status_and_iface1.second.get());
525 
526     std::string invalid_name = iface_name + "0";
527     const auto& status_and_iface2 =
528         HIDL_INVOKE(wifi_chip_, getNanIface, invalid_name);
529     EXPECT_EQ(WifiStatusCode::ERROR_INVALID_ARGS, status_and_iface2.first.code);
530     EXPECT_EQ(nullptr, status_and_iface2.second.get());
531 }
532 
533 /*
534  * RemoveNanIface
535  * Configures the chip in NAN mode and create an iface. Then, remove
536  * the iface object using the correct name and ensure any other name
537  * doesn't remove the iface.
538  */
TEST_F(WifiChipHidlTest,RemoveNanIface)539 TEST_F(WifiChipHidlTest, RemoveNanIface) {
540     if (!gEnv->isNanOn) return;
541     configureChipForIfaceType(IfaceType::NAN, gEnv->isNanOn);
542 
543     sp<IWifiNanIface> nan_iface;
544     EXPECT_EQ(WifiStatusCode::SUCCESS, createNanIface(&nan_iface));
545     EXPECT_NE(nullptr, nan_iface.get());
546 
547     std::string iface_name = getIfaceName(nan_iface);
548     std::string invalid_name = iface_name + "0";
549     EXPECT_EQ(WifiStatusCode::ERROR_INVALID_ARGS, removeNanIface(invalid_name));
550 
551     EXPECT_EQ(WifiStatusCode::SUCCESS, removeNanIface(iface_name));
552 
553     // No such iface exists now. So, this should return failure.
554     EXPECT_EQ(WifiStatusCode::ERROR_INVALID_ARGS, removeNanIface(iface_name));
555 }
556 
557 /*
558  * CreateP2pIface
559  * Configures the chip in P2P mode and ensures that at least 1 iface creation
560  * succeeds.
561  */
TEST_F(WifiChipHidlTest,CreateP2pIface)562 TEST_F(WifiChipHidlTest, CreateP2pIface) {
563     configureChipForIfaceType(IfaceType::P2P, true);
564 
565     sp<IWifiP2pIface> iface;
566     EXPECT_EQ(WifiStatusCode::SUCCESS, createP2pIface(&iface));
567     EXPECT_NE(nullptr, iface.get());
568 }
569 
570 /*
571  * GetP2pIfaceNames
572  * Configures the chip in P2P mode and ensures that the iface list is empty
573  * before creating the iface. Then, create the iface and ensure that
574  * iface name is returned via the list.
575  */
TEST_F(WifiChipHidlTest,GetP2pIfaceNames)576 TEST_F(WifiChipHidlTest, GetP2pIfaceNames) {
577     configureChipForIfaceType(IfaceType::P2P, true);
578 
579     const auto& status_and_iface_names1 =
580         HIDL_INVOKE(wifi_chip_, getP2pIfaceNames);
581     EXPECT_EQ(WifiStatusCode::SUCCESS, status_and_iface_names1.first.code);
582     EXPECT_EQ(0u, status_and_iface_names1.second.size());
583 
584     sp<IWifiP2pIface> iface;
585     EXPECT_EQ(WifiStatusCode::SUCCESS, createP2pIface(&iface));
586     EXPECT_NE(nullptr, iface.get());
587 
588     std::string iface_name = getIfaceName(iface);
589     const auto& status_and_iface_names2 =
590         HIDL_INVOKE(wifi_chip_, getP2pIfaceNames);
591     EXPECT_EQ(WifiStatusCode::SUCCESS, status_and_iface_names2.first.code);
592     EXPECT_EQ(1u, status_and_iface_names2.second.size());
593     EXPECT_EQ(iface_name, status_and_iface_names2.second[0]);
594 
595     EXPECT_EQ(WifiStatusCode::SUCCESS, removeP2pIface(iface_name));
596     const auto& status_and_iface_names3 =
597         HIDL_INVOKE(wifi_chip_, getP2pIfaceNames);
598     EXPECT_EQ(WifiStatusCode::SUCCESS, status_and_iface_names3.first.code);
599     EXPECT_EQ(0u, status_and_iface_names3.second.size());
600 }
601 
602 /*
603  * GetP2pIface
604  * Configures the chip in P2P mode and create an iface. Then, retrieve
605  * the iface object using the correct name and ensure any other name
606  * doesn't retrieve an iface object.
607  */
TEST_F(WifiChipHidlTest,GetP2pIface)608 TEST_F(WifiChipHidlTest, GetP2pIface) {
609     configureChipForIfaceType(IfaceType::P2P, true);
610 
611     sp<IWifiP2pIface> p2p_iface;
612     EXPECT_EQ(WifiStatusCode::SUCCESS, createP2pIface(&p2p_iface));
613     EXPECT_NE(nullptr, p2p_iface.get());
614 
615     std::string iface_name = getIfaceName(p2p_iface);
616     const auto& status_and_iface1 =
617         HIDL_INVOKE(wifi_chip_, getP2pIface, iface_name);
618     EXPECT_EQ(WifiStatusCode::SUCCESS, status_and_iface1.first.code);
619     EXPECT_NE(nullptr, status_and_iface1.second.get());
620 
621     std::string invalid_name = iface_name + "0";
622     const auto& status_and_iface2 =
623         HIDL_INVOKE(wifi_chip_, getP2pIface, invalid_name);
624     EXPECT_EQ(WifiStatusCode::ERROR_INVALID_ARGS, status_and_iface2.first.code);
625     EXPECT_EQ(nullptr, status_and_iface2.second.get());
626 }
627 
628 /*
629  * RemoveP2pIface
630  * Configures the chip in P2P mode and create an iface. Then, remove
631  * the iface object using the correct name and ensure any other name
632  * doesn't remove the iface.
633  */
TEST_F(WifiChipHidlTest,RemoveP2pIface)634 TEST_F(WifiChipHidlTest, RemoveP2pIface) {
635     configureChipForIfaceType(IfaceType::P2P, true);
636 
637     sp<IWifiP2pIface> p2p_iface;
638     EXPECT_EQ(WifiStatusCode::SUCCESS, createP2pIface(&p2p_iface));
639     EXPECT_NE(nullptr, p2p_iface.get());
640 
641     std::string iface_name = getIfaceName(p2p_iface);
642     std::string invalid_name = iface_name + "0";
643     EXPECT_EQ(WifiStatusCode::ERROR_INVALID_ARGS, removeP2pIface(invalid_name));
644     EXPECT_EQ(WifiStatusCode::SUCCESS, removeP2pIface(iface_name));
645 
646     // No such iface exists now. So, this should return failure.
647     EXPECT_EQ(WifiStatusCode::ERROR_INVALID_ARGS, removeP2pIface(iface_name));
648 }
649 
650 /*
651  * CreateStaIface
652  * Configures the chip in STA mode and ensures that at least 1 iface creation
653  * succeeds.
654  */
TEST_F(WifiChipHidlTest,CreateStaIface)655 TEST_F(WifiChipHidlTest, CreateStaIface) {
656     configureChipForIfaceType(IfaceType::STA, true);
657 
658     sp<IWifiStaIface> iface;
659     EXPECT_EQ(WifiStatusCode::SUCCESS, createStaIface(&iface));
660     EXPECT_NE(nullptr, iface.get());
661 }
662 
663 /*
664  * GetStaIfaceNames
665  * Configures the chip in STA mode and ensures that the iface list is empty
666  * before creating the iface. Then, create the iface and ensure that
667  * iface name is returned via the list.
668  */
TEST_F(WifiChipHidlTest,GetStaIfaceNames)669 TEST_F(WifiChipHidlTest, GetStaIfaceNames) {
670     configureChipForIfaceType(IfaceType::STA, true);
671 
672     const auto& status_and_iface_names1 =
673         HIDL_INVOKE(wifi_chip_, getStaIfaceNames);
674     EXPECT_EQ(WifiStatusCode::SUCCESS, status_and_iface_names1.first.code);
675     EXPECT_EQ(0u, status_and_iface_names1.second.size());
676 
677     sp<IWifiStaIface> iface;
678     EXPECT_EQ(WifiStatusCode::SUCCESS, createStaIface(&iface));
679     EXPECT_NE(nullptr, iface.get());
680 
681     std::string iface_name = getIfaceName(iface);
682     const auto& status_and_iface_names2 =
683         HIDL_INVOKE(wifi_chip_, getStaIfaceNames);
684     EXPECT_EQ(WifiStatusCode::SUCCESS, status_and_iface_names2.first.code);
685     EXPECT_EQ(1u, status_and_iface_names2.second.size());
686     EXPECT_EQ(iface_name, status_and_iface_names2.second[0]);
687 
688     EXPECT_EQ(WifiStatusCode::SUCCESS, removeStaIface(iface_name));
689     const auto& status_and_iface_names3 =
690         HIDL_INVOKE(wifi_chip_, getStaIfaceNames);
691     EXPECT_EQ(WifiStatusCode::SUCCESS, status_and_iface_names3.first.code);
692     EXPECT_EQ(0u, status_and_iface_names3.second.size());
693 }
694 
695 /*
696  * GetStaIface
697  * Configures the chip in STA mode and create an iface. Then, retrieve
698  * the iface object using the correct name and ensure any other name
699  * doesn't retrieve an iface object.
700  */
TEST_F(WifiChipHidlTest,GetStaIface)701 TEST_F(WifiChipHidlTest, GetStaIface) {
702     configureChipForIfaceType(IfaceType::STA, true);
703 
704     sp<IWifiStaIface> sta_iface;
705     EXPECT_EQ(WifiStatusCode::SUCCESS, createStaIface(&sta_iface));
706     EXPECT_NE(nullptr, sta_iface.get());
707 
708     std::string iface_name = getIfaceName(sta_iface);
709     const auto& status_and_iface1 =
710         HIDL_INVOKE(wifi_chip_, getStaIface, iface_name);
711     EXPECT_EQ(WifiStatusCode::SUCCESS, status_and_iface1.first.code);
712     EXPECT_NE(nullptr, status_and_iface1.second.get());
713 
714     std::string invalid_name = iface_name + "0";
715     const auto& status_and_iface2 =
716         HIDL_INVOKE(wifi_chip_, getStaIface, invalid_name);
717     EXPECT_EQ(WifiStatusCode::ERROR_INVALID_ARGS, status_and_iface2.first.code);
718     EXPECT_EQ(nullptr, status_and_iface2.second.get());
719 }
720 
721 /*
722  * RemoveStaIface
723  * Configures the chip in STA mode and create an iface. Then, remove
724  * the iface object using the correct name and ensure any other name
725  * doesn't remove the iface.
726  */
TEST_F(WifiChipHidlTest,RemoveStaIface)727 TEST_F(WifiChipHidlTest, RemoveStaIface) {
728     configureChipForIfaceType(IfaceType::STA, true);
729 
730     sp<IWifiStaIface> sta_iface;
731     EXPECT_EQ(WifiStatusCode::SUCCESS, createStaIface(&sta_iface));
732     EXPECT_NE(nullptr, sta_iface.get());
733 
734     std::string iface_name = getIfaceName(sta_iface);
735     std::string invalid_name = iface_name + "0";
736     EXPECT_EQ(WifiStatusCode::ERROR_INVALID_ARGS, removeStaIface(invalid_name));
737     EXPECT_EQ(WifiStatusCode::SUCCESS, removeStaIface(iface_name));
738 
739     // No such iface exists now. So, this should return failure.
740     EXPECT_EQ(WifiStatusCode::ERROR_INVALID_ARGS, removeStaIface(iface_name));
741 }
742 
743 /*
744  * CreateRttController
745  */
TEST_F(WifiChipHidlTest,CreateRttController)746 TEST_F(WifiChipHidlTest, CreateRttController) {
747     configureChipForIfaceType(IfaceType::STA, true);
748 
749     sp<IWifiStaIface> iface;
750     EXPECT_EQ(WifiStatusCode::SUCCESS, createStaIface(&iface));
751     EXPECT_NE(nullptr, iface.get());
752 
753     const auto& status_and_rtt_controller =
754         HIDL_INVOKE(wifi_chip_, createRttController, iface);
755     EXPECT_EQ(WifiStatusCode::SUCCESS, status_and_rtt_controller.first.code);
756     EXPECT_NE(nullptr, status_and_rtt_controller.second.get());
757 }
758