1 /*
2  * Copyright (C) 2019 The Android Open Source Project
3  *
4  * Licensed under the Staache 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/hardware/wifi/1.4/IWifi.h>
18 #include <android/hardware/wifi/1.4/IWifiApIface.h>
19 #include <android/hardware/wifi/hostapd/1.0/IHostapd.h>
20 #include <gtest/gtest.h>
21 #include <hidl/GtestPrinter.h>
22 #include <hidl/ServiceManagement.h>
23 
24 #include "wifi_hidl_call_util.h"
25 #include "wifi_hidl_test_utils.h"
26 
27 using ::android::sp;
28 using ::android::hardware::hidl_array;
29 using ::android::hardware::wifi::hostapd::V1_0::IHostapd;
30 using ::android::hardware::wifi::V1_0::WifiStatus;
31 using ::android::hardware::wifi::V1_0::WifiStatusCode;
32 using ::android::hardware::wifi::V1_4::IWifi;
33 using ::android::hardware::wifi::V1_4::IWifiApIface;
34 
35 /**
36  * Fixture to use for all STA Iface HIDL interface tests.
37  */
38 class WifiApIfaceHidlTest : public ::testing::TestWithParam<std::string> {
39    public:
SetUp()40     virtual void SetUp() override {
41         if (android::hardware::getAllHalInstanceNames(IHostapd::descriptor)
42                 .empty()) {
43             GTEST_SKIP() << "Device does not support AP";
44         }
45         // Make sure to start with a clean state
46         stopWifi(GetInstanceName());
47 
48         wifi_ap_iface_ =
49             IWifiApIface::castFrom(getWifiApIface(GetInstanceName()));
50         ASSERT_NE(nullptr, wifi_ap_iface_.get());
51     }
52 
TearDown()53     virtual void TearDown() override { stopWifi(GetInstanceName()); }
54 
55    protected:
56     sp<IWifiApIface> wifi_ap_iface_;
57 
58    private:
GetInstanceName()59     std::string GetInstanceName() { return GetParam(); }
60 };
61 
62 /*
63  * SetMacAddress:
64  * Ensures that calls to set MAC address will return a success status
65  * code.
66  */
TEST_P(WifiApIfaceHidlTest,SetMacAddress)67 TEST_P(WifiApIfaceHidlTest, SetMacAddress) {
68     const hidl_array<uint8_t, 6> kMac{{0x12, 0x22, 0x33, 0x52, 0x10, 0x44}};
69     EXPECT_EQ(WifiStatusCode::SUCCESS,
70               HIDL_INVOKE(wifi_ap_iface_, setMacAddress, kMac).code);
71 }
72 
73 /*
74  * GetFactoryMacAddress:
75  * Ensures that calls to get factory MAC address will retrieve a non-zero MAC
76  * and return a success status code.
77  */
TEST_P(WifiApIfaceHidlTest,GetFactoryMacAddress)78 TEST_P(WifiApIfaceHidlTest, GetFactoryMacAddress) {
79     std::pair<WifiStatus, hidl_array<uint8_t, 6> > status_and_mac =
80         HIDL_INVOKE(wifi_ap_iface_, getFactoryMacAddress);
81     EXPECT_EQ(WifiStatusCode::SUCCESS, status_and_mac.first.code);
82     hidl_array<uint8_t, 6> all_zero{};
83     EXPECT_NE(all_zero, status_and_mac.second);
84 }
85 
86 GTEST_ALLOW_UNINSTANTIATED_PARAMETERIZED_TEST(WifiApIfaceHidlTest);
87 INSTANTIATE_TEST_SUITE_P(
88     PerInstance, WifiApIfaceHidlTest,
89     testing::ValuesIn(
90         android::hardware::getAllHalInstanceNames(IWifi::descriptor)),
91     android::hardware::PrintInstanceNameToString);
92