1 /*
2  * Copyright (C) 2018 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 <numeric>
18 #include <vector>
19 
20 #include <android-base/logging.h>
21 
22 #include <android/hardware/wifi/1.3/IWifi.h>
23 #include <android/hardware/wifi/1.3/IWifiStaIface.h>
24 #include <android/hardware/wifi/1.5/IWifiStaIface.h>
25 #include <gtest/gtest.h>
26 #include <hidl/GtestPrinter.h>
27 #include <hidl/ServiceManagement.h>
28 
29 #include "wifi_hidl_call_util.h"
30 #include "wifi_hidl_test_utils.h"
31 
32 using ::android::sp;
33 using ::android::hardware::hidl_array;
34 using ::android::hardware::wifi::V1_0::WifiStatus;
35 using ::android::hardware::wifi::V1_0::WifiStatusCode;
36 using ::android::hardware::wifi::V1_3::IWifiStaIface;
37 
38 /**
39  * Fixture to use for all STA Iface HIDL interface tests.
40  */
41 class WifiStaIfaceHidlTest : public ::testing::TestWithParam<std::string> {
42    public:
SetUp()43     virtual void SetUp() override {
44         // Make sure to start with a clean state
45         stopWifi(GetInstanceName());
46 
47         wifi_sta_iface_ =
48             IWifiStaIface::castFrom(getWifiStaIface(GetInstanceName()));
49         ASSERT_NE(nullptr, wifi_sta_iface_.get());
50     }
51 
TearDown()52     virtual void TearDown() override { stopWifi(GetInstanceName()); }
53 
54    protected:
isCapabilitySupported(IWifiStaIface::StaIfaceCapabilityMask cap_mask)55     bool isCapabilitySupported(IWifiStaIface::StaIfaceCapabilityMask cap_mask) {
56         const auto& status_and_caps =
57             HIDL_INVOKE(wifi_sta_iface_, getCapabilities);
58         EXPECT_EQ(WifiStatusCode::SUCCESS, status_and_caps.first.code);
59         return (status_and_caps.second & cap_mask) != 0;
60     }
61 
62     sp<IWifiStaIface> wifi_sta_iface_;
63 
64    private:
GetInstanceName()65     std::string GetInstanceName() { return GetParam(); }
66 };
67 
68 /*
69  * GetFactoryMacAddress:
70  * Ensures that calls to get factory MAC address will retrieve a non-zero MAC
71  * and return a success status code.
72  */
TEST_P(WifiStaIfaceHidlTest,GetFactoryMacAddress)73 TEST_P(WifiStaIfaceHidlTest, GetFactoryMacAddress) {
74     std::pair<WifiStatus, hidl_array<uint8_t, 6> > status_and_mac =
75         HIDL_INVOKE(wifi_sta_iface_, getFactoryMacAddress);
76     EXPECT_EQ(WifiStatusCode::SUCCESS, status_and_mac.first.code);
77     hidl_array<uint8_t, 6> all_zero{};
78     EXPECT_NE(all_zero, status_and_mac.second);
79 }
80 
81 /*
82  * GetLinkLayerStats_1_3
83  * Ensures that calls to get link layer stats V1_3 will retrieve a non-empty
84  * StaLinkLayerStats after link layer stats collection is enabled.
85  */
TEST_P(WifiStaIfaceHidlTest,GetLinkLayerStats_1_3)86 TEST_P(WifiStaIfaceHidlTest, GetLinkLayerStats_1_3) {
87     if (!isCapabilitySupported(
88             IWifiStaIface::StaIfaceCapabilityMask::LINK_LAYER_STATS)) {
89         // No-op if link layer stats is not supported.
90         return;
91     }
92     // Enable link layer stats collection.
93     EXPECT_EQ(WifiStatusCode::SUCCESS,
94               HIDL_INVOKE(wifi_sta_iface_, enableLinkLayerStatsCollection, true)
95                   .code);
96     // Retrieve link layer stats.
97     const auto& status_and_stats =
98         HIDL_INVOKE(wifi_sta_iface_, getLinkLayerStats_1_3);
99     sp<android::hardware::wifi::V1_5::IWifiStaIface> staIface1_5 =
100         android::hardware::wifi::V1_5::IWifiStaIface::castFrom(wifi_sta_iface_);
101     if (staIface1_5.get() == nullptr) {
102         EXPECT_EQ(WifiStatusCode::SUCCESS, status_and_stats.first.code);
103         EXPECT_GT(status_and_stats.second.timeStampInMs, 0u);
104     } else {
105         // not supported on 1.5 HAL.
106         EXPECT_EQ(WifiStatusCode::ERROR_NOT_SUPPORTED,
107                   status_and_stats.first.code);
108     }
109 
110     // Disable link layer stats collection.
111     EXPECT_EQ(
112         WifiStatusCode::SUCCESS,
113         HIDL_INVOKE(wifi_sta_iface_, disableLinkLayerStatsCollection).code);
114 }
115 
116 GTEST_ALLOW_UNINSTANTIATED_PARAMETERIZED_TEST(WifiStaIfaceHidlTest);
117 INSTANTIATE_TEST_SUITE_P(
118     PerInstance, WifiStaIfaceHidlTest,
119     testing::ValuesIn(android::hardware::getAllHalInstanceNames(
120         ::android::hardware::wifi::V1_3::IWifi::descriptor)),
121     android::hardware::PrintInstanceNameToString);
122