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 <VtsCoreUtil.h>
20 #include <android/hardware/wifi/1.0/IWifi.h>
21 #include <android/hardware/wifi/supplicant/1.0/ISupplicant.h>
22 #include <gtest/gtest.h>
23 #include <hidl/GtestPrinter.h>
24 #include <hidl/ServiceManagement.h>
25 
26 #include "supplicant_hidl_test_utils.h"
27 
28 using ::android::sp;
29 using ::android::hardware::hidl_vec;
30 using ::android::hardware::wifi::supplicant::V1_0::IfaceType;
31 using ::android::hardware::wifi::supplicant::V1_0::ISupplicant;
32 using ::android::hardware::wifi::supplicant::V1_0::ISupplicantIface;
33 using ::android::hardware::wifi::supplicant::V1_0::SupplicantStatus;
34 using ::android::hardware::wifi::supplicant::V1_0::SupplicantStatusCode;
35 using ::android::hardware::wifi::V1_0::IWifi;
36 
37 class SupplicantHidlTest
38     : public ::testing::TestWithParam<std::tuple<std::string, std::string>> {
39    public:
SetUp()40     virtual void SetUp() override {
41         // Stop Wi-Fi
42         ASSERT_TRUE(stopWifiFramework());  // stop & wait for wifi to shutdown.
43 
44         wifi_instance_name_ = std::get<0>(GetParam());
45         supplicant_instance_name_ = std::get<1>(GetParam());
46         std::system("/system/bin/start");
47         ASSERT_TRUE(waitForFrameworkReady());
48         isP2pOn_ =
49             testing::deviceSupportsFeature("android.hardware.wifi.direct");
50         stopSupplicant(wifi_instance_name_);
51         startSupplicantAndWaitForHidlService(wifi_instance_name_,
52                                              supplicant_instance_name_);
53         supplicant_ = getSupplicant(supplicant_instance_name_, isP2pOn_);
54         ASSERT_NE(supplicant_.get(), nullptr);
55     }
56 
TearDown()57     virtual void TearDown() override {
58         stopSupplicant(wifi_instance_name_);
59         // Start Wi-Fi
60         startWifiFramework();
61     }
62 
63    protected:
64     // ISupplicant object used for all tests in this fixture.
65     sp<ISupplicant> supplicant_;
66     bool isP2pOn_ = false;
67     std::string wifi_instance_name_;
68     std::string supplicant_instance_name_;
69 };
70 
71 /*
72  * Create:
73  * Ensures that an instance of the ISupplicant proxy object is
74  * successfully created.
75  */
TEST_P(SupplicantHidlTest,Create)76 TEST_P(SupplicantHidlTest, Create) {
77     // Stop the proxy object created in setup.
78     stopSupplicant(wifi_instance_name_);
79     startSupplicantAndWaitForHidlService(wifi_instance_name_,
80                                          supplicant_instance_name_);
81     EXPECT_NE(nullptr,
82               getSupplicant(supplicant_instance_name_, isP2pOn_).get());
83 }
84 
85 /*
86  * ListInterfaces
87  */
TEST_P(SupplicantHidlTest,ListInterfaces)88 TEST_P(SupplicantHidlTest, ListInterfaces) {
89     std::vector<ISupplicant::IfaceInfo> ifaces;
90     supplicant_->listInterfaces(
91         [&](const SupplicantStatus& status,
92             const hidl_vec<ISupplicant::IfaceInfo>& hidl_ifaces) {
93             EXPECT_EQ(SupplicantStatusCode::SUCCESS, status.code);
94             ifaces = hidl_ifaces;
95         });
96 
97     EXPECT_NE(ifaces.end(),
98               std::find_if(ifaces.begin(), ifaces.end(), [](const auto& iface) {
99                   return iface.type == IfaceType::STA;
100               }));
101     if (isP2pOn_) {
102         EXPECT_NE(
103             ifaces.end(),
104             std::find_if(ifaces.begin(), ifaces.end(), [](const auto& iface) {
105                 return iface.type == IfaceType::P2P;
106             }));
107     }
108 }
109 
110 /*
111  * GetInterface
112  */
TEST_P(SupplicantHidlTest,GetInterface)113 TEST_P(SupplicantHidlTest, GetInterface) {
114     std::vector<ISupplicant::IfaceInfo> ifaces;
115     supplicant_->listInterfaces(
116         [&](const SupplicantStatus& status,
117             const hidl_vec<ISupplicant::IfaceInfo>& hidl_ifaces) {
118             EXPECT_EQ(SupplicantStatusCode::SUCCESS, status.code);
119             ifaces = hidl_ifaces;
120         });
121 
122     ASSERT_NE(0u, ifaces.size());
123     supplicant_->getInterface(
124         ifaces[0],
125         [&](const SupplicantStatus& status, const sp<ISupplicantIface>& iface) {
126             EXPECT_EQ(SupplicantStatusCode::SUCCESS, status.code);
127             EXPECT_NE(nullptr, iface.get());
128         });
129 }
130 
131 /*
132  * SetDebugParams
133  */
TEST_P(SupplicantHidlTest,SetDebugParams)134 TEST_P(SupplicantHidlTest, SetDebugParams) {
135     bool show_timestamp = true;
136     bool show_keys = true;
137     ISupplicant::DebugLevel level = ISupplicant::DebugLevel::EXCESSIVE;
138 
139     supplicant_->setDebugParams(level,
140                                 show_timestamp,  // show timestamps
141                                 show_keys,       // show keys
142                                 [](const SupplicantStatus& status) {
143                                     EXPECT_EQ(SupplicantStatusCode::SUCCESS,
144                                               status.code);
145                                 });
146 }
147 
148 /*
149  * GetDebugLevel
150  */
TEST_P(SupplicantHidlTest,GetDebugLevel)151 TEST_P(SupplicantHidlTest, GetDebugLevel) {
152     bool show_timestamp = true;
153     bool show_keys = true;
154     ISupplicant::DebugLevel level = ISupplicant::DebugLevel::EXCESSIVE;
155 
156     supplicant_->setDebugParams(level,
157                                 show_timestamp,  // show timestamps
158                                 show_keys,       // show keys
159                                 [](const SupplicantStatus& status) {
160                                     EXPECT_EQ(SupplicantStatusCode::SUCCESS,
161                                               status.code);
162                                 });
163     EXPECT_EQ(level, supplicant_->getDebugLevel());
164 }
165 
166 /*
167  * IsDebugShowTimestampEnabled
168  */
TEST_P(SupplicantHidlTest,IsDebugShowTimestampEnabled)169 TEST_P(SupplicantHidlTest, IsDebugShowTimestampEnabled) {
170     bool show_timestamp = true;
171     bool show_keys = true;
172     ISupplicant::DebugLevel level = ISupplicant::DebugLevel::EXCESSIVE;
173 
174     supplicant_->setDebugParams(level,
175                                 show_timestamp,  // show timestamps
176                                 show_keys,       // show keys
177                                 [](const SupplicantStatus& status) {
178                                     EXPECT_EQ(SupplicantStatusCode::SUCCESS,
179                                               status.code);
180                                 });
181     EXPECT_EQ(show_timestamp, supplicant_->isDebugShowTimestampEnabled());
182 }
183 
184 /*
185  * IsDebugShowKeysEnabled
186  */
TEST_P(SupplicantHidlTest,IsDebugShowKeysEnabled)187 TEST_P(SupplicantHidlTest, IsDebugShowKeysEnabled) {
188     bool show_timestamp = true;
189     bool show_keys = true;
190     ISupplicant::DebugLevel level = ISupplicant::DebugLevel::EXCESSIVE;
191 
192     supplicant_->setDebugParams(level,
193                                 show_timestamp,  // show timestamps
194                                 show_keys,       // show keys
195                                 [](const SupplicantStatus& status) {
196                                     EXPECT_EQ(SupplicantStatusCode::SUCCESS,
197                                               status.code);
198                                 });
199     EXPECT_EQ(show_keys, supplicant_->isDebugShowKeysEnabled());
200 }
201 
202 /*
203  * SetConcurrenyPriority
204  */
TEST_P(SupplicantHidlTest,SetConcurrencyPriority)205 TEST_P(SupplicantHidlTest, SetConcurrencyPriority) {
206     supplicant_->setConcurrencyPriority(
207         IfaceType::STA, [](const SupplicantStatus& status) {
208             EXPECT_EQ(SupplicantStatusCode::SUCCESS, status.code);
209         });
210     if (isP2pOn_) {
211         supplicant_->setConcurrencyPriority(
212             IfaceType::P2P, [](const SupplicantStatus& status) {
213                 EXPECT_EQ(SupplicantStatusCode::SUCCESS, status.code);
214             });
215     }
216 }
217 
218 GTEST_ALLOW_UNINSTANTIATED_PARAMETERIZED_TEST(SupplicantHidlTest);
219 INSTANTIATE_TEST_CASE_P(
220     PerInstance, SupplicantHidlTest,
221     testing::Combine(
222         testing::ValuesIn(
223             android::hardware::getAllHalInstanceNames(IWifi::descriptor)),
224         testing::ValuesIn(android::hardware::getAllHalInstanceNames(
225             ISupplicant::descriptor))),
226     android::hardware::PrintInstanceTupleNameToString<>);
227