1 /*
2 * Copyright (C) 2021 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 #ifndef SUPPLICANT_TEST_UTILS_H
18 #define SUPPLICANT_TEST_UTILS_H
19
20 #include <VtsCoreUtil.h>
21 #include <android-base/logging.h>
22 #include <android/hardware/wifi/1.0/IWifi.h>
23 #include <hidl/ServiceManagement.h>
24 #include <supplicant_hidl_test_utils.h>
25 #include <wifi_system/supplicant_manager.h>
26
27 using aidl::android::hardware::wifi::supplicant::IfaceInfo;
28 using aidl::android::hardware::wifi::supplicant::ISupplicant;
29 using aidl::android::hardware::wifi::supplicant::ISupplicantP2pIface;
30 using aidl::android::hardware::wifi::supplicant::ISupplicantStaIface;
31 using aidl::android::hardware::wifi::supplicant::KeyMgmtMask;
32 using android::wifi_system::SupplicantManager;
33
getStaIfaceName()34 std::string getStaIfaceName() {
35 std::array<char, PROPERTY_VALUE_MAX> buffer;
36 property_get("wifi.interface", buffer.data(), "wlan0");
37 return std::string(buffer.data());
38 }
39
getP2pIfaceName()40 std::string getP2pIfaceName() {
41 std::array<char, PROPERTY_VALUE_MAX> buffer;
42 property_get("wifi.direct.interface", buffer.data(), "p2p0");
43 return std::string(buffer.data());
44 }
45
getWifiInstanceName()46 std::string getWifiInstanceName() {
47 const std::vector<std::string> instances =
48 android::hardware::getAllHalInstanceNames(
49 ::android::hardware::wifi::V1_0::IWifi::descriptor);
50 EXPECT_NE(0, instances.size());
51 return instances.size() != 0 ? instances[0] : "";
52 }
53
keyMgmtSupported(std::shared_ptr<ISupplicantStaIface> iface,KeyMgmtMask expected)54 bool keyMgmtSupported(std::shared_ptr<ISupplicantStaIface> iface,
55 KeyMgmtMask expected) {
56 KeyMgmtMask caps;
57 if (!iface->getKeyMgmtCapabilities(&caps).isOk()) {
58 return false;
59 }
60 return !!(static_cast<uint32_t>(caps) & static_cast<uint32_t>(expected));
61 }
62
isFilsSupported(std::shared_ptr<ISupplicantStaIface> iface)63 bool isFilsSupported(std::shared_ptr<ISupplicantStaIface> iface) {
64 KeyMgmtMask filsMask = static_cast<KeyMgmtMask>(
65 static_cast<uint32_t>(KeyMgmtMask::FILS_SHA256) |
66 static_cast<uint32_t>(KeyMgmtMask::FILS_SHA384));
67 return keyMgmtSupported(iface, filsMask);
68 }
69
startSupplicant()70 void startSupplicant() {
71 initializeDriverAndFirmware(getWifiInstanceName());
72 SupplicantManager supplicant_manager;
73 ASSERT_TRUE(supplicant_manager.StartSupplicant());
74 ASSERT_TRUE(supplicant_manager.IsSupplicantRunning());
75 }
76
77 // Wrapper around the implementation in supplicant_hidl_test_util.
stopSupplicantService()78 void stopSupplicantService() { stopSupplicant(getWifiInstanceName()); }
79
initializeService()80 void initializeService() {
81 ASSERT_TRUE(stopWifiFramework(getWifiInstanceName()));
82 std::system("/system/bin/start");
83 ASSERT_TRUE(waitForFrameworkReady());
84 stopSupplicantService();
85 startSupplicant();
86 }
87
addStaIface(const std::shared_ptr<ISupplicant> supplicant)88 void addStaIface(const std::shared_ptr<ISupplicant> supplicant) {
89 ASSERT_TRUE(supplicant.get());
90 std::shared_ptr<ISupplicantStaIface> iface;
91 ASSERT_TRUE(supplicant->addStaInterface(getStaIfaceName(), &iface).isOk());
92 }
93
addP2pIface(const std::shared_ptr<ISupplicant> supplicant)94 void addP2pIface(const std::shared_ptr<ISupplicant> supplicant) {
95 ASSERT_TRUE(supplicant.get());
96 std::shared_ptr<ISupplicantP2pIface> iface;
97 ASSERT_TRUE(supplicant->addP2pInterface(getP2pIfaceName(), &iface).isOk());
98 }
99
getSupplicant(const char * supplicant_name)100 std::shared_ptr<ISupplicant> getSupplicant(const char* supplicant_name) {
101 std::shared_ptr<ISupplicant> supplicant = ISupplicant::fromBinder(
102 ndk::SpAIBinder(AServiceManager_waitForService(supplicant_name)));
103 addStaIface(supplicant);
104 if (testing::deviceSupportsFeature("android.hardware.wifi.direct")) {
105 addP2pIface(supplicant);
106 }
107 return supplicant;
108 }
109
110 #endif // SUPPLICANT_TEST_UTILS_H