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 #include <cutils/properties.h>
19 
20 #include <VtsHalHidlTargetTestBase.h>
21 
22 #include <android/hardware/wifi/supplicant/1.0/types.h>
23 #include <android/hardware/wifi/supplicant/1.1/ISupplicant.h>
24 
25 #include "supplicant_hidl_test_utils.h"
26 #include "supplicant_hidl_test_utils_1_1.h"
27 
28 using ::android::hardware::hidl_vec;
29 using ::android::hardware::wifi::supplicant::V1_0::ISupplicantIface;
30 using ::android::hardware::wifi::supplicant::V1_0::SupplicantStatus;
31 using ::android::hardware::wifi::supplicant::V1_0::SupplicantStatusCode;
32 using ::android::hardware::wifi::supplicant::V1_0::IfaceType;
33 using ::android::hardware::wifi::supplicant::V1_1::ISupplicant;
34 using ::android::sp;
35 
36 extern WifiSupplicantHidlEnvironment* gEnv;
37 
38 class SupplicantHidlTest : public ::testing::VtsHalHidlTargetTestBase {
39    public:
SetUp()40     virtual void SetUp() override {
41         startSupplicantAndWaitForHidlService();
42         supplicant_ = getSupplicant_1_1();
43         ASSERT_NE(supplicant_.get(), nullptr);
44     }
45 
TearDown()46     virtual void TearDown() override { stopSupplicant(); }
47 
48    protected:
49     // ISupplicant object used for all tests in this fixture.
50     sp<ISupplicant> supplicant_;
51 
getWlan0IfaceName()52     std::string getWlan0IfaceName() {
53         std::array<char, PROPERTY_VALUE_MAX> buffer;
54         property_get("wifi.interface", buffer.data(), "wlan0");
55         return buffer.data();
56     }
57 
getP2pIfaceName()58     std::string getP2pIfaceName() {
59         std::array<char, PROPERTY_VALUE_MAX> buffer;
60         property_get("wifi.direct.interface", buffer.data(), "p2p0");
61         return buffer.data();
62     }
63 };
64 
65 /*
66  * AddStaInterface
67  */
TEST_F(SupplicantHidlTest,AddStaInterface)68 TEST_F(SupplicantHidlTest, AddStaInterface) {
69     ISupplicant::IfaceInfo iface_info;
70     iface_info.name = getWlan0IfaceName();
71     iface_info.type = IfaceType::STA;
72     supplicant_->addInterface(
73         iface_info,
74         [&](const SupplicantStatus& status, const sp<ISupplicantIface>& iface) {
75             EXPECT_TRUE(
76                 (status.code == SupplicantStatusCode::SUCCESS) ||
77                 (status.code == SupplicantStatusCode::FAILURE_IFACE_EXISTS));
78             EXPECT_NE(nullptr, iface.get());
79         });
80 }
81 
82 /*
83  * AddP2pInterface
84  */
TEST_F(SupplicantHidlTest,AddP2pInterface)85 TEST_F(SupplicantHidlTest, AddP2pInterface) {
86     if (!gEnv->isP2pOn) return;
87     ISupplicant::IfaceInfo iface_info;
88     iface_info.name = getP2pIfaceName();
89     iface_info.type = IfaceType::P2P;
90     supplicant_->addInterface(
91         iface_info,
92         [&](const SupplicantStatus& status, const sp<ISupplicantIface>& iface) {
93             EXPECT_TRUE(
94                 (status.code == SupplicantStatusCode::SUCCESS) ||
95                 (status.code == SupplicantStatusCode::FAILURE_IFACE_EXISTS));
96             EXPECT_NE(nullptr, iface.get());
97         });
98 }
99 
100 /*
101  * RemoveStaInterface
102  */
TEST_F(SupplicantHidlTest,RemoveStaInterface)103 TEST_F(SupplicantHidlTest, RemoveStaInterface) {
104     ISupplicant::IfaceInfo iface_info;
105     iface_info.name = getWlan0IfaceName();
106     iface_info.type = IfaceType::STA;
107 
108     supplicant_->addInterface(
109         iface_info,
110         [&](const SupplicantStatus& status, const sp<ISupplicantIface>& iface) {
111             EXPECT_TRUE(
112                 (status.code == SupplicantStatusCode::SUCCESS) ||
113                 (status.code == SupplicantStatusCode::FAILURE_IFACE_EXISTS));
114             EXPECT_NE(nullptr, iface.get());
115         });
116     supplicant_->removeInterface(
117         iface_info, [&](const SupplicantStatus& status) {
118             EXPECT_EQ(SupplicantStatusCode::SUCCESS, status.code);
119         });
120 }
121 
122 /*
123  * RemoveP2pInterface
124  */
TEST_F(SupplicantHidlTest,RemoveP2pInterface)125 TEST_F(SupplicantHidlTest, RemoveP2pInterface) {
126     if (!gEnv->isP2pOn) return;
127     ISupplicant::IfaceInfo iface_info;
128     iface_info.name = getP2pIfaceName();
129     iface_info.type = IfaceType::P2P;
130 
131     supplicant_->addInterface(
132         iface_info,
133         [&](const SupplicantStatus& status, const sp<ISupplicantIface>& iface) {
134             EXPECT_TRUE(
135                 (status.code == SupplicantStatusCode::SUCCESS) ||
136                 (status.code == SupplicantStatusCode::FAILURE_IFACE_EXISTS));
137             EXPECT_NE(nullptr, iface.get());
138         });
139     supplicant_->removeInterface(
140         iface_info, [&](const SupplicantStatus& status) {
141             EXPECT_EQ(SupplicantStatusCode::SUCCESS, status.code);
142         });
143 }
144 
145 /*
146  * Terminate
147  * This terminates the service.
148  */
TEST_F(SupplicantHidlTest,Terminate)149 TEST_F(SupplicantHidlTest, Terminate) {
150     supplicant_->terminate();
151 }
152