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 #define LOG_TAG "wifi_offload_hidl_hal_test"
18 
19 #include <android-base/logging.h>
20 #include <android/hardware/wifi/offload/1.0/IOffload.h>
21 #include <android/hardware/wifi/offload/1.0/IOffloadCallback.h>
22 #include <android/hardware/wifi/offload/1.0/types.h>
23 #include <gtest/gtest.h>
24 #include <hidl/GtestPrinter.h>
25 #include <hidl/ServiceManagement.h>
26 
27 #include <VtsHalHidlTargetCallbackBase.h>
28 
29 #include <vector>
30 
31 #include "hidl_call_util.h"
32 
33 using ::android::hardware::wifi::offload::V1_0::IOffload;
34 using ::android::hardware::wifi::offload::V1_0::IOffloadCallback;
35 using ::android::hardware::wifi::offload::V1_0::ScanResult;
36 using ::android::hardware::wifi::offload::V1_0::ScanParam;
37 using ::android::hardware::wifi::offload::V1_0::Ssid;
38 using ::android::hardware::wifi::offload::V1_0::NetworkInfo;
39 using ::android::hardware::wifi::offload::V1_0::ScanFilter;
40 using ::android::hardware::wifi::offload::V1_0::ScanStats;
41 using ::android::hardware::wifi::offload::V1_0::OffloadStatus;
42 using ::android::hardware::wifi::offload::V1_0::OffloadStatusCode;
43 using ::android::hardware::Return;
44 using ::android::hardware::Void;
45 using ::android::hardware::hidl_vec;
46 using ::android::sp;
47 
48 constexpr char kOffloadCallbackSendScanResult[] = "onScanResult";
49 constexpr char kOffloadCallbackSendError[] = "onError";
50 
51 namespace {
52 const uint8_t kSsid1[] = {'G', 'o', 'o', 'g', 'l', 'e'};
53 const uint8_t kSsid2[] = {'X', 'f', 'i', 'n', 'i', 't', 'y'};
54 const uint8_t kBssid[6] = {0x12, 0xef, 0xa1, 0x2c, 0x97, 0x8b};
55 const int16_t kRssi = -60;
56 const uint32_t kFrequency = 2412;
57 const uint8_t kBssidSize = 6;
58 const uint64_t kTsf = 0;
59 const uint16_t kCapability = 0;
60 const uint8_t kNetworkFlags = 0;
61 const uint32_t kFrequency1 = 2412;
62 const uint32_t kFrequency2 = 2437;
63 const uint32_t kDisconnectedModeScanIntervalMs = 5000;
64 const int16_t kRssiThreshold = -76;
65 }
66 
67 class OffloadCallbackArgs {
68    public:
69     hidl_vec<ScanResult> scan_results_;
70     OffloadStatus error_code_;
71 };
72 
73 // The main test class for WifiOffload HIDL HAL.
74 class WifiOffloadHidlTest : public ::testing::TestWithParam<std::string> {
75    public:
SetUp()76     virtual void SetUp() override {
77         wifi_offload_ = IOffload::getService(GetParam());
78         ASSERT_NE(wifi_offload_, nullptr);
79 
80         wifi_offload_cb_ = new OffloadCallback();
81         ASSERT_NE(wifi_offload_cb_, nullptr);
82     }
83 
TearDown()84     virtual void TearDown() override {}
85 
86     /* Callback class for Offload HAL. */
87     class OffloadCallback
88         : public ::testing::VtsHalHidlTargetCallbackBase<OffloadCallbackArgs>,
89           public IOffloadCallback {
90        public:
OffloadCallback()91         OffloadCallback(){};
92 
93         virtual ~OffloadCallback() = default;
94 
onScanResult(const hidl_vec<ScanResult> & scan_result)95         Return<void> onScanResult(
96             const hidl_vec<ScanResult>& scan_result) override {
97             OffloadCallbackArgs args;
98             args.scan_results_ = scan_result;
99             NotifyFromCallback(kOffloadCallbackSendScanResult, args);
100             return Void();
101         };
102 
onError(const OffloadStatus & status)103         Return<void> onError(const OffloadStatus& status) override {
104             OffloadCallbackArgs args;
105             args.error_code_ = status;
106             NotifyFromCallback(kOffloadCallbackSendError, args);
107             return Void();
108         }
109     };
110 
111     sp<IOffload> wifi_offload_;
112     sp<OffloadCallback> wifi_offload_cb_;
113 };
114 
115 /*
116  * Verify that setEventCallback method returns without errors
117  */
TEST_P(WifiOffloadHidlTest,setEventCallback)118 TEST_P(WifiOffloadHidlTest, setEventCallback) {
119     auto returnObject = wifi_offload_->setEventCallback(wifi_offload_cb_);
120     ASSERT_EQ(true, returnObject.isOk());
121 }
122 
123 /*
124  * Verify that subscribeScanResults method returns without errors
125  */
TEST_P(WifiOffloadHidlTest,subscribeScanResults)126 TEST_P(WifiOffloadHidlTest, subscribeScanResults) {
127     const auto& result = HIDL_INVOKE(wifi_offload_, subscribeScanResults, 0);
128     ASSERT_EQ(OffloadStatusCode::OK, result.code);
129 }
130 
131 /*
132  * Verify that unsubscribeScanResults method returns without errors
133  */
TEST_P(WifiOffloadHidlTest,unsubscribeScanResults)134 TEST_P(WifiOffloadHidlTest, unsubscribeScanResults) {
135     auto returnObject = wifi_offload_->unsubscribeScanResults();
136     ASSERT_EQ(true, returnObject.isOk());
137 }
138 
139 /*
140  * Verify that configureScans method returns without errors
141  */
TEST_P(WifiOffloadHidlTest,configureScans)142 TEST_P(WifiOffloadHidlTest, configureScans) {
143     ScanParam* pScanParam = new ScanParam();
144     std::vector<uint32_t> frequencyList = {kFrequency1, kFrequency2};
145     pScanParam->disconnectedModeScanIntervalMs =
146         kDisconnectedModeScanIntervalMs;
147     pScanParam->frequencyList = frequencyList;
148     std::vector<Ssid> ssidList;
149     std::vector<std::vector<uint8_t>> ssids{kSsid1, kSsid2};
150     for (const auto& ssid : ssids) {
151         Ssid tmp = ssid;
152         ssidList.push_back(tmp);
153     }
154     pScanParam->ssidList = ssidList;
155     ScanFilter* pScanFilter = new ScanFilter();
156     pScanFilter->rssiThreshold = kRssiThreshold;
157     std::vector<std::vector<uint8_t>> match_ssids{kSsid1, kSsid2};
158     std::vector<uint8_t> security_flags{kNetworkFlags, kNetworkFlags};
159     std::vector<NetworkInfo> preferredNetworksList;
160     for (size_t i = 0; i < security_flags.size(); i++) {
161         NetworkInfo nwInfo;
162         nwInfo.ssid = match_ssids[i];
163         nwInfo.flags = security_flags[i];
164         preferredNetworksList.push_back(nwInfo);
165     }
166     const auto& result =
167         HIDL_INVOKE(wifi_offload_, configureScans, *pScanParam, *pScanFilter);
168     ASSERT_EQ(OffloadStatusCode::OK, result.code);
169 }
170 
171 /*
172  * Verify that getScanStats returns without any errors
173  */
TEST_P(WifiOffloadHidlTest,getScanStats)174 TEST_P(WifiOffloadHidlTest, getScanStats) {
175     const auto& result = HIDL_INVOKE(wifi_offload_, getScanStats);
176     OffloadStatus status = result.first;
177     ASSERT_EQ(OffloadStatusCode::OK, status.code);
178 }
179 
180 /*
181  * Verify that onScanResult callback is invoked
182  */
TEST_P(WifiOffloadHidlTest,getScanResults)183 TEST_P(WifiOffloadHidlTest, getScanResults) {
184     wifi_offload_->setEventCallback(wifi_offload_cb_);
185     std::vector<ScanResult> scan_results;
186     std::vector<uint8_t> ssid(kSsid1, kSsid1 + sizeof(kSsid1));
187     ScanResult scan_result;
188     scan_result.tsf = kTsf;
189     scan_result.rssi = kRssi;
190     scan_result.frequency = kFrequency;
191     scan_result.capability = kCapability;
192     memcpy(&scan_result.bssid[0], &kBssid[0], kBssidSize);
193     scan_result.networkInfo.ssid = ssid;
194     scan_result.networkInfo.flags = kNetworkFlags;
195     scan_results.push_back(scan_result);
196     wifi_offload_cb_->onScanResult(scan_results);
197     auto res =
198         wifi_offload_cb_->WaitForCallback(kOffloadCallbackSendScanResult);
199     ASSERT_EQ(true, res.no_timeout);
200 }
201 
202 /*
203  * Verify that onError callback is invoked
204  */
TEST_P(WifiOffloadHidlTest,getError)205 TEST_P(WifiOffloadHidlTest, getError) {
206     wifi_offload_->setEventCallback(wifi_offload_cb_);
207     OffloadStatus status = {OffloadStatusCode::ERROR, ""};
208     wifi_offload_cb_->onError(status);
209     auto res = wifi_offload_cb_->WaitForCallback(kOffloadCallbackSendError);
210     ASSERT_EQ(true, res.no_timeout);
211 }
212 
213 GTEST_ALLOW_UNINSTANTIATED_PARAMETERIZED_TEST(WifiOffloadHidlTest);
214 INSTANTIATE_TEST_SUITE_P(
215     PerInstance, WifiOffloadHidlTest,
216     testing::ValuesIn(
217         android::hardware::getAllHalInstanceNames(IOffload::descriptor)),
218     android::hardware::PrintInstanceNameToString);