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.2/IWifi.h>
23 #include <android/hardware/wifi/1.2/IWifiStaIface.h>
24 #include <gtest/gtest.h>
25 #include <hidl/GtestPrinter.h>
26 #include <hidl/ServiceManagement.h>
27 
28 #include "wifi_hidl_call_util.h"
29 #include "wifi_hidl_test_utils.h"
30 
31 using ::android::sp;
32 using ::android::hardware::wifi::V1_0::CommandId;
33 using ::android::hardware::wifi::V1_0::WifiStatusCode;
34 using ::android::hardware::wifi::V1_2::IWifiStaIface;
35 
36 /**
37  * Fixture to use for all STA Iface HIDL interface tests.
38  */
39 class WifiStaIfaceHidlTest : public ::testing::TestWithParam<std::string> {
40    public:
41     virtual void SetUp() override {
42         // Make sure to start with a clean state
43         stopWifi(GetInstanceName());
44 
45         wifi_sta_iface_ =
46             IWifiStaIface::castFrom(getWifiStaIface(GetInstanceName()));
47         ASSERT_NE(nullptr, wifi_sta_iface_.get());
48     }
49 
50     virtual void TearDown() override { stopWifi(GetInstanceName()); }
51 
52    protected:
53     bool isCapabilitySupported(IWifiStaIface::StaIfaceCapabilityMask cap_mask) {
54         const auto& status_and_caps =
55             HIDL_INVOKE(wifi_sta_iface_, getCapabilities);
56         EXPECT_EQ(WifiStatusCode::SUCCESS, status_and_caps.first.code);
57         return (status_and_caps.second & cap_mask) != 0;
58     }
59 
60     sp<IWifiStaIface> wifi_sta_iface_;
61 
62    private:
63     std::string GetInstanceName() { return GetParam(); }
64 };
65 
66 /*
67  * SetMacAddress:
68  * Ensures that calls to set MAC address will return a success status
69  * code.
70  */
71 TEST_P(WifiStaIfaceHidlTest, SetMacAddress) {
72     const android::hardware::hidl_array<uint8_t, 6> kMac{
73         std::array<uint8_t, 6>{{0x12, 0x22, 0x33, 0x52, 0x10, 0x41}}};
74     EXPECT_EQ(WifiStatusCode::SUCCESS,
75               HIDL_INVOKE(wifi_sta_iface_, setMacAddress, kMac).code);
76 }
77 
78 /*
79  * ReadApfPacketFilterData:
80  * Ensures that we can read the APF working memory when supported.
81  *
82  * TODO: Test disabled because we can't even test reading and writing the APF
83  * memory while the interface is in disconnected state (b/73804303#comment25).
84  * There's a pending bug on VTS infra to add such support (b/32974062).
85  * TODO: We can't execute APF opcodes from this test because there's no way
86  * to loop test packets through the wifi firmware (b/73804303#comment29).
87  */
88 TEST_P(WifiStaIfaceHidlTest, DISABLED_ReadApfPacketFilterData) {
89     if (!isCapabilitySupported(IWifiStaIface::StaIfaceCapabilityMask::APF)) {
90         // Disable test if APF packet filer is not supported.
91         LOG(WARNING) << "TEST SKIPPED: APF packet filtering not supported";
92         return;
93     }
94 
95     const auto& status_and_caps =
96         HIDL_INVOKE(wifi_sta_iface_, getApfPacketFilterCapabilities);
97     EXPECT_EQ(WifiStatusCode::SUCCESS, status_and_caps.first.code);
98     LOG(WARNING) << "StaApfPacketFilterCapabilities: version="
99                  << status_and_caps.second.version
100                  << " maxLength=" << status_and_caps.second.maxLength;
101 
102     const CommandId kCmd = 0;  // Matches what WifiVendorHal.java uses.
103     const uint32_t kDataSize =
104         std::min(status_and_caps.second.maxLength, static_cast<uint32_t>(500));
105 
106     // Create a buffer and fill it with some values.
107     std::vector<uint8_t> data(kDataSize);
108     std::iota(data.begin(), data.end(), 0);
109 
110     EXPECT_EQ(
111         HIDL_INVOKE(wifi_sta_iface_, installApfPacketFilter, kCmd, data).code,
112         WifiStatusCode::SUCCESS);
113     const auto& status_and_data =
114         HIDL_INVOKE(wifi_sta_iface_, readApfPacketFilterData);
115     EXPECT_EQ(WifiStatusCode::SUCCESS, status_and_data.first.code);
116 
117     EXPECT_EQ(status_and_data.second, data);
118 }
119 
120 GTEST_ALLOW_UNINSTANTIATED_PARAMETERIZED_TEST(WifiStaIfaceHidlTest);
121 INSTANTIATE_TEST_SUITE_P(
122     PerInstance, WifiStaIfaceHidlTest,
123     testing::ValuesIn(android::hardware::getAllHalInstanceNames(
124         ::android::hardware::wifi::V1_2::IWifi::descriptor)),
125     android::hardware::PrintInstanceNameToString);
126