1 /*
2  * Copyright (C) 2019 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 #include <android/hardware/wifi/1.0/IWifi.h>
19 #include <android/hardware/wifi/1.0/IWifiChip.h>
20 #include <android/hardware/wifi/hostapd/1.0/IHostapd.h>
21 #include <gtest/gtest.h>
22 #include <hidl/GtestPrinter.h>
23 #include <hidl/ServiceManagement.h>
24 
25 #include "wifi_hidl_call_util.h"
26 #include "wifi_hidl_test_utils.h"
27 
28 using ::android::sp;
29 using ::android::hardware::wifi::hostapd::V1_0::IHostapd;
30 using ::android::hardware::wifi::V1_0::ChipModeId;
31 using ::android::hardware::wifi::V1_0::IfaceType;
32 using ::android::hardware::wifi::V1_0::IWifi;
33 using ::android::hardware::wifi::V1_0::IWifiApIface;
34 using ::android::hardware::wifi::V1_0::IWifiChip;
35 using ::android::hardware::wifi::V1_0::IWifiIface;
36 using ::android::hardware::wifi::V1_0::WifiStatus;
37 using ::android::hardware::wifi::V1_0::WifiStatusCode;
38 
39 /**
40  * Fixture for IWifiChip tests that are conditioned on SoftAP support.
41  */
42 class WifiChipHidlApTest : public ::testing::TestWithParam<std::string> {
43    public:
SetUp()44     virtual void SetUp() override {
45         if (android::hardware::getAllHalInstanceNames(IHostapd::descriptor)
46                 .empty()) {
47             GTEST_SKIP() << "Device does not support AP";
48         }
49         // Make sure test starts with a clean state
50         stopWifi(GetInstanceName());
51 
52         wifi_chip_ = getWifiChip(GetInstanceName());
53         ASSERT_NE(nullptr, wifi_chip_.get());
54     }
55 
TearDown()56     virtual void TearDown() override { stopWifi(GetInstanceName()); }
57 
58    protected:
59     // Helper function to configure the Chip in one of the supported modes.
60     // Most of the non-mode-configuration-related methods require chip
61     // to be first configured.
configureChipForIfaceType(IfaceType type,bool expectSuccess)62     ChipModeId configureChipForIfaceType(IfaceType type, bool expectSuccess) {
63         ChipModeId mode_id;
64         EXPECT_EQ(expectSuccess,
65                   configureChipToSupportIfaceType(wifi_chip_, type, &mode_id));
66         return mode_id;
67     }
68 
getIfaceName(const sp<IWifiIface> & iface)69     std::string getIfaceName(const sp<IWifiIface>& iface) {
70         const auto& status_and_name = HIDL_INVOKE(iface, getName);
71         EXPECT_EQ(WifiStatusCode::SUCCESS, status_and_name.first.code);
72         return status_and_name.second;
73     }
74 
createApIface(sp<IWifiApIface> * ap_iface)75     WifiStatusCode createApIface(sp<IWifiApIface>* ap_iface) {
76         const auto& status_and_iface = HIDL_INVOKE(wifi_chip_, createApIface);
77         *ap_iface = status_and_iface.second;
78         return status_and_iface.first.code;
79     }
80 
removeApIface(const std::string & name)81     WifiStatusCode removeApIface(const std::string& name) {
82         return HIDL_INVOKE(wifi_chip_, removeApIface, name).code;
83     }
84 
85     sp<IWifiChip> wifi_chip_;
86 
87    private:
GetInstanceName()88     std::string GetInstanceName() { return GetParam(); }
89 };
90 
91 /*
92  * CreateApIface
93  * Configures the chip in AP mode and ensures that at least 1 iface creation
94  * succeeds.
95  */
TEST_P(WifiChipHidlApTest,CreateApIface)96 TEST_P(WifiChipHidlApTest, CreateApIface) {
97     configureChipForIfaceType(IfaceType::AP, true);
98 
99     sp<IWifiApIface> iface;
100     EXPECT_EQ(WifiStatusCode::SUCCESS, createApIface(&iface));
101     EXPECT_NE(nullptr, iface.get());
102 }
103 
104 /*
105  * GetApIfaceNames
106  * Configures the chip in AP mode and ensures that the iface list is empty
107  * before creating the iface. Then, create the iface and ensure that
108  * iface name is returned via the list.
109  */
TEST_P(WifiChipHidlApTest,GetApIfaceNames)110 TEST_P(WifiChipHidlApTest, GetApIfaceNames) {
111     configureChipForIfaceType(IfaceType::AP, true);
112 
113     const auto& status_and_iface_names1 =
114         HIDL_INVOKE(wifi_chip_, getApIfaceNames);
115     EXPECT_EQ(WifiStatusCode::SUCCESS, status_and_iface_names1.first.code);
116     EXPECT_EQ(0u, status_and_iface_names1.second.size());
117 
118     sp<IWifiApIface> iface;
119     EXPECT_EQ(WifiStatusCode::SUCCESS, createApIface(&iface));
120     EXPECT_NE(nullptr, iface.get());
121 
122     std::string iface_name = getIfaceName(iface);
123     const auto& status_and_iface_names2 =
124         HIDL_INVOKE(wifi_chip_, getApIfaceNames);
125     EXPECT_EQ(WifiStatusCode::SUCCESS, status_and_iface_names2.first.code);
126     EXPECT_EQ(1u, status_and_iface_names2.second.size());
127     EXPECT_EQ(iface_name, status_and_iface_names2.second[0]);
128 
129     EXPECT_EQ(WifiStatusCode::SUCCESS, removeApIface(iface_name));
130     const auto& status_and_iface_names3 =
131         HIDL_INVOKE(wifi_chip_, getApIfaceNames);
132     EXPECT_EQ(WifiStatusCode::SUCCESS, status_and_iface_names3.first.code);
133     EXPECT_EQ(0u, status_and_iface_names3.second.size());
134 }
135 
136 /*
137  * GetApIface
138  * Configures the chip in AP mode and create an iface. Then, retrieve
139  * the iface object using the correct name and ensure any other name
140  * doesn't retrieve an iface object.
141  */
TEST_P(WifiChipHidlApTest,GetApIface)142 TEST_P(WifiChipHidlApTest, GetApIface) {
143     configureChipForIfaceType(IfaceType::AP, true);
144 
145     sp<IWifiApIface> ap_iface;
146     EXPECT_EQ(WifiStatusCode::SUCCESS, createApIface(&ap_iface));
147     EXPECT_NE(nullptr, ap_iface.get());
148 
149     std::string iface_name = getIfaceName(ap_iface);
150     const auto& status_and_iface1 =
151         HIDL_INVOKE(wifi_chip_, getApIface, iface_name);
152     EXPECT_EQ(WifiStatusCode::SUCCESS, status_and_iface1.first.code);
153     EXPECT_NE(nullptr, status_and_iface1.second.get());
154 
155     std::string invalid_name = iface_name + "0";
156     const auto& status_and_iface2 =
157         HIDL_INVOKE(wifi_chip_, getApIface, invalid_name);
158     EXPECT_EQ(WifiStatusCode::ERROR_INVALID_ARGS, status_and_iface2.first.code);
159     EXPECT_EQ(nullptr, status_and_iface2.second.get());
160 }
161 
162 /*
163  * RemoveApIface
164  * Configures the chip in AP mode and create an iface. Then, remove
165  * the iface object using the correct name and ensure any other name
166  * doesn't remove the iface.
167  */
TEST_P(WifiChipHidlApTest,RemoveApIface)168 TEST_P(WifiChipHidlApTest, RemoveApIface) {
169     configureChipForIfaceType(IfaceType::AP, true);
170 
171     sp<IWifiApIface> ap_iface;
172     EXPECT_EQ(WifiStatusCode::SUCCESS, createApIface(&ap_iface));
173     EXPECT_NE(nullptr, ap_iface.get());
174 
175     std::string iface_name = getIfaceName(ap_iface);
176     std::string invalid_name = iface_name + "0";
177     EXPECT_EQ(WifiStatusCode::ERROR_INVALID_ARGS, removeApIface(invalid_name));
178     EXPECT_EQ(WifiStatusCode::SUCCESS, removeApIface(iface_name));
179 
180     // No such iface exists now. So, this should return failure.
181     EXPECT_EQ(WifiStatusCode::ERROR_INVALID_ARGS, removeApIface(iface_name));
182 }
183 
184 GTEST_ALLOW_UNINSTANTIATED_PARAMETERIZED_TEST(WifiChipHidlApTest);
185 INSTANTIATE_TEST_SUITE_P(
186     PerInstance, WifiChipHidlApTest,
187     testing::ValuesIn(
188         android::hardware::getAllHalInstanceNames(IWifi::descriptor)),
189     android::hardware::PrintInstanceNameToString);
190