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