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/ISupplicantStaNetwork.h>
22 #include <android/hardware/wifi/supplicant/1.3/ISupplicantStaNetwork.h>
23 #include <gtest/gtest.h>
24 #include <hidl/GtestPrinter.h>
25 #include <hidl/ServiceManagement.h>
26 
27 #include "supplicant_hidl_call_util.h"
28 #include "supplicant_hidl_test_utils.h"
29 #include "wifi_hidl_test_utils.h"
30 
31 using ::android::sp;
32 using ::android::hardware::hidl_array;
33 using ::android::hardware::hidl_string;
34 using ::android::hardware::hidl_vec;
35 using ::android::hardware::Return;
36 using ::android::hardware::Void;
37 using ::android::hardware::wifi::supplicant::V1_0::IfaceType;
38 using ::android::hardware::wifi::supplicant::V1_0::ISupplicant;
39 using ::android::hardware::wifi::supplicant::V1_0::ISupplicantStaIface;
40 using ::android::hardware::wifi::supplicant::V1_0::ISupplicantStaNetwork;
41 using ::android::hardware::wifi::supplicant::V1_0::
42     ISupplicantStaNetworkCallback;
43 using ::android::hardware::wifi::supplicant::V1_0::SupplicantStatus;
44 using ::android::hardware::wifi::supplicant::V1_0::SupplicantStatusCode;
45 using ::android::hardware::wifi::V1_0::IWifi;
46 
47 namespace {
48 constexpr char kTestSsidStr[] = "TestSsid1234";
49 constexpr char kTestPskPassphrase[] = "TestPsk123";
50 constexpr char kTestIdStr[] = "TestIdstr";
51 constexpr char kTestEapPasswdStr[] = "TestEapPasswd1234";
52 constexpr char kTestEapCert[] = "keystore://CERT";
53 constexpr char kTestEapPrivateKeyId[] = "key_id";
54 constexpr char kTestEapMatch[] = "match";
55 constexpr char kTestEapEngineID[] = "engine_id";
56 constexpr uint8_t kTestBssid[] = {0x56, 0x67, 0x67, 0xf4, 0x56, 0x92};
57 constexpr uint8_t kTestWepKey[] = {0x56, 0x67, 0x67, 0xf4, 0x56};
58 constexpr uint8_t kTestKc[] = {0x56, 0x67, 0x67, 0xf4, 0x76, 0x87, 0x98, 0x12};
59 constexpr uint8_t kTestSres[] = {0x56, 0x67, 0x67, 0xf4};
60 constexpr uint8_t kTestRes[] = {0x56, 0x67, 0x67, 0xf4, 0x67};
61 constexpr uint8_t kTestIk[] = {[0 ... 15] = 0x65};
62 constexpr uint8_t kTestCk[] = {[0 ... 15] = 0x45};
63 constexpr uint8_t kTestIdentity[] = {0x45, 0x67, 0x98, 0x67, 0x56};
64 constexpr uint8_t kTestPsk[] = {[0 ... 31] = 0x12};
65 constexpr uint8_t kTestAutParam[] = {[0 ... 13] = 0xe1};
66 constexpr uint32_t kTestWepTxKeyIdx = 2;
67 constexpr uint32_t kTestUpdateIdentifier = 21;
68 constexpr uint32_t kTestKeyMgmt = (ISupplicantStaNetwork::KeyMgmtMask::WPA_PSK |
69                                    ISupplicantStaNetwork::KeyMgmtMask::WPA_EAP);
70 constexpr uint32_t kTestProto = (ISupplicantStaNetwork::ProtoMask::OSEN |
71                                  ISupplicantStaNetwork::ProtoMask::RSN);
72 constexpr uint32_t kTestAuthAlg = (ISupplicantStaNetwork::AuthAlgMask::OPEN |
73                                    ISupplicantStaNetwork::AuthAlgMask::SHARED);
74 constexpr uint32_t kTestGroupCipher =
75     (ISupplicantStaNetwork::GroupCipherMask::CCMP |
76      ISupplicantStaNetwork::GroupCipherMask::WEP104);
77 constexpr uint32_t kTestPairwiseCipher =
78     (ISupplicantStaNetwork::PairwiseCipherMask::CCMP |
79      ISupplicantStaNetwork::PairwiseCipherMask::TKIP);
80 }  // namespace
81 
82 class SupplicantStaNetworkHidlTest
83     : public ::testing::TestWithParam<std::tuple<std::string, std::string>> {
84    public:
SetUp()85     virtual void SetUp() override {
86         wifi_instance_name_ = std::get<0>(GetParam());
87         supplicant_instance_name_ = std::get<1>(GetParam());
88         stopSupplicant(wifi_instance_name_);
89         startSupplicantAndWaitForHidlService(wifi_instance_name_,
90                                              supplicant_instance_name_);
91         isP2pOn_ =
92             testing::deviceSupportsFeature("android.hardware.wifi.direct");
93         supplicant_ = getSupplicant(supplicant_instance_name_, isP2pOn_);
94         EXPECT_TRUE(turnOnExcessiveLogging(supplicant_));
95         sta_network_ = createSupplicantStaNetwork(supplicant_);
96         ASSERT_NE(sta_network_.get(), nullptr);
97         /* variable used to check if the underlying HAL version is 1.3 or
98          * higher. This is to skip tests which are using deprecated methods.
99          */
100         v1_3 = ::android::hardware::wifi::supplicant::V1_3::
101             ISupplicantStaNetwork::castFrom(sta_network_);
102 
103         ssid_.assign(kTestSsidStr, kTestSsidStr + strlen(kTestSsidStr));
104     }
105 
TearDown()106     virtual void TearDown() override { stopSupplicant(wifi_instance_name_); }
107 
108    protected:
removeNetwork()109     void removeNetwork() {
110         sp<ISupplicantStaIface> sta_iface = getSupplicantStaIface(supplicant_);
111         ASSERT_NE(nullptr, sta_iface.get());
112         uint32_t net_id;
113         sta_network_->getId(
114             [&](const SupplicantStatus& status, int network_id) {
115                 ASSERT_EQ(SupplicantStatusCode::SUCCESS, status.code);
116                 net_id = network_id;
117             });
118         sta_iface->removeNetwork(net_id, [](const SupplicantStatus& status) {
119             ASSERT_EQ(SupplicantStatusCode::SUCCESS, status.code);
120         });
121     }
122 
123     sp<::android::hardware::wifi::supplicant::V1_3::ISupplicantStaNetwork>
124         v1_3 = nullptr;
125     bool isP2pOn_ = false;
126     sp<ISupplicant> supplicant_;
127     // ISupplicantStaNetwork object used for all tests in this fixture.
128     sp<ISupplicantStaNetwork> sta_network_;
129     // SSID to use for various tests.
130     std::vector<uint8_t> ssid_;
131     std::string wifi_instance_name_;
132     std::string supplicant_instance_name_;
133 };
134 
135 class NetworkCallback : public ISupplicantStaNetworkCallback {
onNetworkEapSimGsmAuthRequest(const ISupplicantStaNetworkCallback::NetworkRequestEapSimGsmAuthParams &)136     Return<void> onNetworkEapSimGsmAuthRequest(
137         const ISupplicantStaNetworkCallback::NetworkRequestEapSimGsmAuthParams&
138         /* params */) override {
139         return Void();
140     }
onNetworkEapSimUmtsAuthRequest(const ISupplicantStaNetworkCallback::NetworkRequestEapSimUmtsAuthParams &)141     Return<void> onNetworkEapSimUmtsAuthRequest(
142         const ISupplicantStaNetworkCallback::NetworkRequestEapSimUmtsAuthParams&
143         /* params */) override {
144         return Void();
145     }
onNetworkEapIdentityRequest()146     Return<void> onNetworkEapIdentityRequest() override { return Void(); }
147 };
148 
149 /*
150  * Create:
151  * Ensures that an instance of the ISupplicantStaNetwork proxy object is
152  * successfully created.
153  */
TEST_P(SupplicantStaNetworkHidlTest,Create)154 TEST_P(SupplicantStaNetworkHidlTest, Create) {
155     stopSupplicant(wifi_instance_name_);
156     startSupplicantAndWaitForHidlService(wifi_instance_name_,
157                                          supplicant_instance_name_);
158     sp<ISupplicant> supplicant =
159         getSupplicant(supplicant_instance_name_, isP2pOn_);
160     EXPECT_TRUE(turnOnExcessiveLogging(supplicant));
161     EXPECT_NE(nullptr, createSupplicantStaNetwork(supplicant).get());
162 }
163 
164 /*
165  * RegisterCallback
166  */
TEST_P(SupplicantStaNetworkHidlTest,RegisterCallback)167 TEST_P(SupplicantStaNetworkHidlTest, RegisterCallback) {
168     sta_network_->registerCallback(
169         new NetworkCallback(), [](const SupplicantStatus& status) {
170             EXPECT_EQ(SupplicantStatusCode::SUCCESS, status.code);
171         });
172 }
173 
174 /*
175  * GetInterfaceName
176  */
TEST_P(SupplicantStaNetworkHidlTest,GetInterfaceName)177 TEST_P(SupplicantStaNetworkHidlTest, GetInterfaceName) {
178     const auto& status_and_interface_name =
179         HIDL_INVOKE(sta_network_, getInterfaceName);
180     EXPECT_EQ(SupplicantStatusCode::SUCCESS,
181               status_and_interface_name.first.code);
182     EXPECT_FALSE(std::string(status_and_interface_name.second).empty());
183 }
184 
185 /*
186  * GetType
187  */
TEST_P(SupplicantStaNetworkHidlTest,GetType)188 TEST_P(SupplicantStaNetworkHidlTest, GetType) {
189     const auto& status_and_interface_type = HIDL_INVOKE(sta_network_, getType);
190     EXPECT_EQ(SupplicantStatusCode::SUCCESS,
191               status_and_interface_type.first.code);
192     EXPECT_EQ(status_and_interface_type.second, IfaceType::STA);
193 }
194 
195 /* Tests out the various setter/getter methods. */
196 /*
197  * SetGetSsid
198  */
TEST_P(SupplicantStaNetworkHidlTest,SetGetSsid)199 TEST_P(SupplicantStaNetworkHidlTest, SetGetSsid) {
200     sta_network_->setSsid(ssid_, [](const SupplicantStatus& status) {
201         EXPECT_EQ(SupplicantStatusCode::SUCCESS, status.code);
202     });
203     sta_network_->getSsid(
204         [&](const SupplicantStatus& status, const hidl_vec<uint8_t>& get_ssid) {
205             EXPECT_EQ(SupplicantStatusCode::SUCCESS, status.code);
206             EXPECT_EQ(ssid_, std::vector<uint8_t>(get_ssid));
207         });
208 }
209 
210 /*
211  * SetGetBssid
212  */
TEST_P(SupplicantStaNetworkHidlTest,SetGetBssid)213 TEST_P(SupplicantStaNetworkHidlTest, SetGetBssid) {
214     std::array<uint8_t, 6> set_bssid;
215     memcpy(set_bssid.data(), kTestBssid, set_bssid.size());
216     sta_network_->setBssid(set_bssid, [](const SupplicantStatus& status) {
217         EXPECT_EQ(SupplicantStatusCode::SUCCESS, status.code);
218     });
219     sta_network_->getBssid([&](const SupplicantStatus& status,
220                                const hidl_array<uint8_t, 6>& get_bssid_hidl) {
221         EXPECT_EQ(SupplicantStatusCode::SUCCESS, status.code);
222         std::array<uint8_t, 6> get_bssid;
223         memcpy(get_bssid.data(), get_bssid_hidl.data(), get_bssid.size());
224         EXPECT_EQ(set_bssid, get_bssid);
225     });
226 }
227 
228 /*
229  * SetGetKeyMgmt
230  */
TEST_P(SupplicantStaNetworkHidlTest,SetGetKeyMgmt)231 TEST_P(SupplicantStaNetworkHidlTest, SetGetKeyMgmt) {
232     if (v1_3 != nullptr) {
233         GTEST_SKIP() << "Skipping test since HAL is 1.3 or higher";
234     }
235     sta_network_->setKeyMgmt(kTestKeyMgmt, [](const SupplicantStatus& status) {
236         EXPECT_EQ(SupplicantStatusCode::SUCCESS, status.code);
237     });
238     sta_network_->getKeyMgmt(
239         [&](const SupplicantStatus& status, uint32_t key_mgmt) {
240             EXPECT_EQ(SupplicantStatusCode::SUCCESS, status.code);
241             EXPECT_EQ(key_mgmt, kTestKeyMgmt);
242         });
243 }
244 
245 /*
246  * SetGetProto
247  */
TEST_P(SupplicantStaNetworkHidlTest,SetGetProto)248 TEST_P(SupplicantStaNetworkHidlTest, SetGetProto) {
249     if (v1_3 != nullptr) {
250         GTEST_SKIP() << "Skipping test since HAL is 1.3 or higher";
251     }
252     sta_network_->setProto(kTestProto, [](const SupplicantStatus& status) {
253         EXPECT_EQ(SupplicantStatusCode::SUCCESS, status.code);
254     });
255     sta_network_->getProto([&](const SupplicantStatus& status, uint32_t proto) {
256         EXPECT_EQ(SupplicantStatusCode::SUCCESS, status.code);
257         EXPECT_EQ(proto, kTestProto);
258     });
259 }
260 
261 /*
262  * SetGetKeyAuthAlg
263  */
TEST_P(SupplicantStaNetworkHidlTest,SetGetAuthAlg)264 TEST_P(SupplicantStaNetworkHidlTest, SetGetAuthAlg) {
265     sta_network_->setAuthAlg(kTestAuthAlg, [](const SupplicantStatus& status) {
266         EXPECT_EQ(SupplicantStatusCode::SUCCESS, status.code);
267     });
268     sta_network_->getAuthAlg(
269         [&](const SupplicantStatus& status, uint32_t auth_alg) {
270             EXPECT_EQ(SupplicantStatusCode::SUCCESS, status.code);
271             EXPECT_EQ(auth_alg, kTestAuthAlg);
272         });
273 }
274 
275 /*
276  * SetGetGroupCipher
277  */
TEST_P(SupplicantStaNetworkHidlTest,SetGetGroupCipher)278 TEST_P(SupplicantStaNetworkHidlTest, SetGetGroupCipher) {
279     if (v1_3 != nullptr) {
280         GTEST_SKIP() << "Skipping test since HAL is 1.3 or higher";
281     }
282     sta_network_->setGroupCipher(
283         kTestGroupCipher, [](const SupplicantStatus& status) {
284             EXPECT_EQ(SupplicantStatusCode::SUCCESS, status.code);
285         });
286     sta_network_->getGroupCipher(
287         [&](const SupplicantStatus& status, uint32_t group_cipher) {
288             EXPECT_EQ(SupplicantStatusCode::SUCCESS, status.code);
289             EXPECT_EQ(group_cipher, kTestGroupCipher);
290         });
291 }
292 
293 /*
294  * SetGetPairwiseCipher
295  */
TEST_P(SupplicantStaNetworkHidlTest,SetGetPairwiseCipher)296 TEST_P(SupplicantStaNetworkHidlTest, SetGetPairwiseCipher) {
297     if (v1_3 != nullptr) {
298         GTEST_SKIP() << "Skipping test since HAL is 1.3 or higher";
299     }
300     sta_network_->setPairwiseCipher(
301         kTestPairwiseCipher, [](const SupplicantStatus& status) {
302             EXPECT_EQ(SupplicantStatusCode::SUCCESS, status.code);
303         });
304     sta_network_->getPairwiseCipher(
305         [&](const SupplicantStatus& status, uint32_t pairwise_cipher) {
306             EXPECT_EQ(SupplicantStatusCode::SUCCESS, status.code);
307             EXPECT_EQ(pairwise_cipher, kTestPairwiseCipher);
308         });
309 }
310 
311 /*
312  * SetGetPskPassphrase
313  */
TEST_P(SupplicantStaNetworkHidlTest,SetGetPskPassphrase)314 TEST_P(SupplicantStaNetworkHidlTest, SetGetPskPassphrase) {
315     sta_network_->setPskPassphrase(
316         kTestPskPassphrase, [](const SupplicantStatus& status) {
317             EXPECT_EQ(SupplicantStatusCode::SUCCESS, status.code);
318         });
319     sta_network_->getPskPassphrase(
320         [&](const SupplicantStatus& status, const hidl_string& psk) {
321             EXPECT_EQ(SupplicantStatusCode::SUCCESS, status.code);
322             EXPECT_EQ(kTestPskPassphrase, std::string(psk.c_str()));
323         });
324 }
325 
326 /*
327  * SetGetPsk
328  */
TEST_P(SupplicantStaNetworkHidlTest,SetGetPsk)329 TEST_P(SupplicantStaNetworkHidlTest, SetGetPsk) {
330     EXPECT_EQ(SupplicantStatusCode::SUCCESS,
331               HIDL_INVOKE(sta_network_, setPsk, kTestPsk).code);
332     const auto& status_and_psk = HIDL_INVOKE(sta_network_, getPsk);
333     EXPECT_EQ(SupplicantStatusCode::SUCCESS, status_and_psk.first.code);
334     hidl_array<uint8_t, 32> expected_psk(kTestPsk);
335     EXPECT_EQ(expected_psk, status_and_psk.second);
336 }
337 
338 /*
339  * SetGetWepKeys
340  */
TEST_P(SupplicantStaNetworkHidlTest,SetGetWepTxKeyIdx)341 TEST_P(SupplicantStaNetworkHidlTest, SetGetWepTxKeyIdx) {
342     sta_network_->setWepTxKeyIdx(
343         kTestWepTxKeyIdx, [](const SupplicantStatus& status) {
344             EXPECT_EQ(SupplicantStatusCode::SUCCESS, status.code);
345         });
346     sta_network_->getWepTxKeyIdx(
347         [&](const SupplicantStatus& status, uint32_t key_idx) {
348             EXPECT_EQ(SupplicantStatusCode::SUCCESS, status.code);
349             EXPECT_EQ(kTestWepTxKeyIdx, key_idx);
350         });
351 }
352 
353 /*
354  * SetGetWepKeys
355  */
TEST_P(SupplicantStaNetworkHidlTest,SetGetWepKeys)356 TEST_P(SupplicantStaNetworkHidlTest, SetGetWepKeys) {
357     for (uint32_t i = 0;
358          i < static_cast<uint32_t>(
359                  ISupplicantStaNetwork::ParamSizeLimits::WEP_KEYS_MAX_NUM);
360          i++) {
361         std::vector<uint8_t> set_wep_key(std::begin(kTestWepKey),
362                                          std::end(kTestWepKey));
363         sta_network_->setWepKey(
364             i, set_wep_key, [](const SupplicantStatus& status) {
365                 EXPECT_EQ(SupplicantStatusCode::SUCCESS, status.code);
366             });
367         sta_network_->getWepKey(i, [&](const SupplicantStatus& status,
368                                        const hidl_vec<uint8_t>& get_wep_key) {
369             EXPECT_EQ(SupplicantStatusCode::SUCCESS, status.code);
370             EXPECT_EQ(set_wep_key, std::vector<uint8_t>(get_wep_key));
371         });
372     }
373 }
374 
375 /*
376  * SetGetScanSsid
377  */
TEST_P(SupplicantStaNetworkHidlTest,SetGetScanSsid)378 TEST_P(SupplicantStaNetworkHidlTest, SetGetScanSsid) {
379     sta_network_->setScanSsid(
380         true, [](const SupplicantStatus& status) {
381             EXPECT_EQ(SupplicantStatusCode::SUCCESS, status.code);
382         });
383     sta_network_->getScanSsid(
384         [&](const SupplicantStatus& status, bool scan_ssid) {
385             EXPECT_EQ(SupplicantStatusCode::SUCCESS, status.code);
386             EXPECT_EQ(true, scan_ssid);
387         });
388 }
389 
390 /*
391  * SetGetRequirePmf
392  */
TEST_P(SupplicantStaNetworkHidlTest,SetGetRequirePmf)393 TEST_P(SupplicantStaNetworkHidlTest, SetGetRequirePmf) {
394     sta_network_->setRequirePmf(
395         true, [](const SupplicantStatus& status) {
396             EXPECT_EQ(SupplicantStatusCode::SUCCESS, status.code);
397         });
398     sta_network_->getRequirePmf(
399         [&](const SupplicantStatus& status, bool require_pmf) {
400             EXPECT_EQ(SupplicantStatusCode::SUCCESS, status.code);
401             EXPECT_EQ(true, require_pmf);
402         });
403 }
404 
405 /*
406  * SetGetIdStr
407  */
TEST_P(SupplicantStaNetworkHidlTest,SetGetIdStr)408 TEST_P(SupplicantStaNetworkHidlTest, SetGetIdStr) {
409     sta_network_->setIdStr(
410         kTestIdStr, [](const SupplicantStatus& status) {
411             EXPECT_EQ(SupplicantStatusCode::SUCCESS, status.code);
412         });
413     sta_network_->getIdStr(
414         [&](const SupplicantStatus& status, const hidl_string& id_str) {
415             EXPECT_EQ(SupplicantStatusCode::SUCCESS, status.code);
416             EXPECT_EQ(kTestIdStr, std::string(id_str.c_str()));
417         });
418 }
419 
420 /*
421  * SetGetEapMethod
422  */
TEST_P(SupplicantStaNetworkHidlTest,SetGetEapMethod)423 TEST_P(SupplicantStaNetworkHidlTest, SetGetEapMethod) {
424     ISupplicantStaNetwork::EapMethod set_eap_method =
425         ISupplicantStaNetwork::EapMethod::PEAP;
426     sta_network_->setEapMethod(
427         set_eap_method, [](const SupplicantStatus& status) {
428             EXPECT_EQ(SupplicantStatusCode::SUCCESS, status.code);
429         });
430     sta_network_->getEapMethod(
431         [&](const SupplicantStatus& status,
432             ISupplicantStaNetwork::EapMethod eap_method) {
433             EXPECT_EQ(SupplicantStatusCode::SUCCESS, status.code);
434             EXPECT_EQ(set_eap_method, eap_method);
435         });
436 }
437 
438 /*
439  * SetGetEapPhase2Method
440  */
TEST_P(SupplicantStaNetworkHidlTest,SetGetEapPhase2Method)441 TEST_P(SupplicantStaNetworkHidlTest, SetGetEapPhase2Method) {
442     ISupplicantStaNetwork::EapMethod set_eap_method =
443         ISupplicantStaNetwork::EapMethod::PEAP;
444     sta_network_->setEapMethod(
445         set_eap_method, [](const SupplicantStatus& status) {
446             EXPECT_EQ(SupplicantStatusCode::SUCCESS, status.code);
447         });
448     ISupplicantStaNetwork::EapPhase2Method set_eap_phase2_method =
449         ISupplicantStaNetwork::EapPhase2Method::NONE;
450     sta_network_->setEapPhase2Method(
451         set_eap_phase2_method, [](const SupplicantStatus& status) {
452             EXPECT_EQ(SupplicantStatusCode::SUCCESS, status.code);
453         });
454     sta_network_->getEapPhase2Method(
455         [&](const SupplicantStatus& status,
456             ISupplicantStaNetwork::EapPhase2Method eap_phase2_method) {
457             EXPECT_EQ(SupplicantStatusCode::SUCCESS, status.code);
458             EXPECT_EQ(set_eap_phase2_method, eap_phase2_method);
459         });
460 }
461 
462 /*
463  * SetGetEapIdentity
464  */
TEST_P(SupplicantStaNetworkHidlTest,SetGetEapIdentity)465 TEST_P(SupplicantStaNetworkHidlTest, SetGetEapIdentity) {
466     std::vector<uint8_t> set_identity(kTestIdentity, kTestIdentity + sizeof(kTestIdentity));
467     sta_network_->setEapIdentity(
468         set_identity, [](const SupplicantStatus& status) {
469             EXPECT_EQ(SupplicantStatusCode::SUCCESS, status.code);
470         });
471     sta_network_->getEapIdentity(
472         [&](const SupplicantStatus& status, const std::vector<uint8_t>& identity) {
473             EXPECT_EQ(SupplicantStatusCode::SUCCESS, status.code);
474             EXPECT_EQ(set_identity, identity);
475         });
476 }
477 
478 /*
479  * SetGetEapAnonymousIdentity
480  */
TEST_P(SupplicantStaNetworkHidlTest,SetGetEapAnonymousIdentity)481 TEST_P(SupplicantStaNetworkHidlTest, SetGetEapAnonymousIdentity) {
482     std::vector<uint8_t> set_identity(kTestIdentity, kTestIdentity + sizeof(kTestIdentity));
483     sta_network_->setEapAnonymousIdentity(
484         set_identity, [](const SupplicantStatus& status) {
485             EXPECT_EQ(SupplicantStatusCode::SUCCESS, status.code);
486         });
487     sta_network_->getEapAnonymousIdentity(
488         [&](const SupplicantStatus& status, const std::vector<uint8_t>& identity) {
489             EXPECT_EQ(SupplicantStatusCode::SUCCESS, status.code);
490             EXPECT_EQ(set_identity, identity);
491         });
492 }
493 
494 /*
495  * SetGetEapPassword
496  */
TEST_P(SupplicantStaNetworkHidlTest,SetGetEapPassword)497 TEST_P(SupplicantStaNetworkHidlTest, SetGetEapPassword) {
498     std::vector<uint8_t> set_eap_passwd(
499         kTestEapPasswdStr, kTestEapPasswdStr + strlen(kTestEapPasswdStr));
500     sta_network_->setEapPassword(
501         set_eap_passwd, [](const SupplicantStatus& status) {
502             EXPECT_EQ(SupplicantStatusCode::SUCCESS, status.code);
503         });
504     sta_network_->getEapPassword([&](const SupplicantStatus& status,
505                                      const hidl_vec<uint8_t>& eap_passwd) {
506         EXPECT_EQ(SupplicantStatusCode::SUCCESS, status.code);
507         EXPECT_EQ(set_eap_passwd, std::vector<uint8_t>(eap_passwd));
508     });
509 }
510 
511 /*
512  * SetGetEapCACert
513  */
TEST_P(SupplicantStaNetworkHidlTest,SetGetEapCACert)514 TEST_P(SupplicantStaNetworkHidlTest, SetGetEapCACert) {
515     sta_network_->setEapCACert(
516         kTestEapCert, [](const SupplicantStatus& status) {
517             EXPECT_EQ(SupplicantStatusCode::SUCCESS, status.code);
518         });
519     sta_network_->getEapCACert([&](const SupplicantStatus& status,
520                                    const hidl_string& eap_cert) {
521         EXPECT_EQ(SupplicantStatusCode::SUCCESS, status.code);
522         EXPECT_EQ(kTestEapCert, std::string(eap_cert.c_str()));
523     });
524 }
525 
526 /*
527  * SetGetEapCAPath
528  */
TEST_P(SupplicantStaNetworkHidlTest,SetGetEapCAPath)529 TEST_P(SupplicantStaNetworkHidlTest, SetGetEapCAPath) {
530     sta_network_->setEapCAPath(
531         kTestEapCert, [](const SupplicantStatus& status) {
532             EXPECT_EQ(SupplicantStatusCode::SUCCESS, status.code);
533         });
534     sta_network_->getEapCAPath([&](const SupplicantStatus& status,
535                                    const hidl_string& eap_cert) {
536         EXPECT_EQ(SupplicantStatusCode::SUCCESS, status.code);
537         EXPECT_EQ(kTestEapCert, std::string(eap_cert.c_str()));
538     });
539 }
540 
541 /*
542  * SetGetEapClientCert
543  */
TEST_P(SupplicantStaNetworkHidlTest,SetGetEapClientCert)544 TEST_P(SupplicantStaNetworkHidlTest, SetGetEapClientCert) {
545     sta_network_->setEapClientCert(
546         kTestEapCert, [](const SupplicantStatus& status) {
547             EXPECT_EQ(SupplicantStatusCode::SUCCESS, status.code);
548         });
549     sta_network_->getEapClientCert([&](const SupplicantStatus& status,
550                                        const hidl_string& eap_cert) {
551         EXPECT_EQ(SupplicantStatusCode::SUCCESS, status.code);
552         EXPECT_EQ(kTestEapCert, std::string(eap_cert.c_str()));
553     });
554 }
555 
556 /*
557  * SetGetEapPrivateKeyId
558  */
TEST_P(SupplicantStaNetworkHidlTest,SetGetEapPrivateKeyId)559 TEST_P(SupplicantStaNetworkHidlTest, SetGetEapPrivateKeyId) {
560     sta_network_->setEapPrivateKeyId(
561         kTestEapPrivateKeyId, [](const SupplicantStatus& status) {
562             EXPECT_EQ(SupplicantStatusCode::SUCCESS, status.code);
563         });
564     sta_network_->getEapPrivateKeyId([&](const SupplicantStatus& status,
565                                          const hidl_string& key_id) {
566         EXPECT_EQ(SupplicantStatusCode::SUCCESS, status.code);
567         EXPECT_EQ(kTestEapPrivateKeyId, std::string(key_id.c_str()));
568     });
569 }
570 
571 /*
572  * SetGetEapAltSubjectMatch
573  */
TEST_P(SupplicantStaNetworkHidlTest,SetGetEapAltSubjectMatch)574 TEST_P(SupplicantStaNetworkHidlTest, SetGetEapAltSubjectMatch) {
575     sta_network_->setEapAltSubjectMatch(
576         kTestEapMatch, [](const SupplicantStatus& status) {
577             EXPECT_EQ(SupplicantStatusCode::SUCCESS, status.code);
578         });
579     sta_network_->getEapAltSubjectMatch([&](const SupplicantStatus& status,
580                                             const hidl_string& match) {
581         EXPECT_EQ(SupplicantStatusCode::SUCCESS, status.code);
582         EXPECT_EQ(kTestEapMatch, std::string(match.c_str()));
583     });
584 }
585 
586 /*
587  * SetGetEapSubjectMatch
588  */
TEST_P(SupplicantStaNetworkHidlTest,SetGetEapSubjectMatch)589 TEST_P(SupplicantStaNetworkHidlTest, SetGetEapSubjectMatch) {
590     EXPECT_EQ(
591         SupplicantStatusCode::SUCCESS,
592         HIDL_INVOKE(sta_network_, setEapSubjectMatch, kTestEapMatch).code);
593     const auto& status_and_subject_match =
594         HIDL_INVOKE(sta_network_, getEapSubjectMatch);
595     EXPECT_EQ(SupplicantStatusCode::SUCCESS,
596               status_and_subject_match.first.code);
597     EXPECT_EQ(kTestEapMatch,
598               std::string(status_and_subject_match.second.c_str()));
599 }
600 
601 /*
602  * SetGetEapDomainSuffixMatch
603  */
TEST_P(SupplicantStaNetworkHidlTest,SetGetEapDomainSuffixMatch)604 TEST_P(SupplicantStaNetworkHidlTest, SetGetEapDomainSuffixMatch) {
605     sta_network_->setEapDomainSuffixMatch(
606         kTestEapMatch, [](const SupplicantStatus& status) {
607             EXPECT_EQ(SupplicantStatusCode::SUCCESS, status.code);
608         });
609     sta_network_->getEapDomainSuffixMatch([&](const SupplicantStatus& status,
610                                               const hidl_string& match) {
611         EXPECT_EQ(SupplicantStatusCode::SUCCESS, status.code);
612         EXPECT_EQ(kTestEapMatch, std::string(match.c_str()));
613     });
614 }
615 
616 /*
617  * SetGetEapEngine
618  */
TEST_P(SupplicantStaNetworkHidlTest,SetGetEapEngine)619 TEST_P(SupplicantStaNetworkHidlTest, SetGetEapEngine) {
620     sta_network_->setEapEngine(
621         true, [](const SupplicantStatus& status) {
622             EXPECT_EQ(SupplicantStatusCode::SUCCESS, status.code);
623         });
624     sta_network_->getEapEngine([&](const SupplicantStatus& status,
625                                    bool enable) {
626         EXPECT_EQ(SupplicantStatusCode::SUCCESS, status.code);
627         EXPECT_EQ(true, enable);
628     });
629 }
630 
631 /*
632  * SetGetEapEngineID
633  */
TEST_P(SupplicantStaNetworkHidlTest,SetGetEapEngineID)634 TEST_P(SupplicantStaNetworkHidlTest, SetGetEapEngineID) {
635     sta_network_->setEapEngineID(
636         kTestEapEngineID, [](const SupplicantStatus& status) {
637             EXPECT_EQ(SupplicantStatusCode::SUCCESS, status.code);
638         });
639     sta_network_->getEapEngineID([&](const SupplicantStatus& status,
640                                      const hidl_string& id) {
641         EXPECT_EQ(SupplicantStatusCode::SUCCESS, status.code);
642         EXPECT_EQ(kTestEapEngineID, std::string(id.c_str()));
643     });
644 }
645 
646 /*
647  * Enable
648  */
TEST_P(SupplicantStaNetworkHidlTest,Enable)649 TEST_P(SupplicantStaNetworkHidlTest, Enable) {
650     if (v1_3 != nullptr) {
651         GTEST_SKIP() << "Skipping test since HAL is 1.3 or higher";
652     }
653     // wpa_supplicant doesn't perform any connection initiation
654     // unless atleast the Ssid and Ket mgmt params are set.
655     sta_network_->setSsid(ssid_, [](const SupplicantStatus& status) {
656         EXPECT_EQ(SupplicantStatusCode::SUCCESS, status.code);
657     });
658     sta_network_->setKeyMgmt(kTestKeyMgmt, [](const SupplicantStatus& status) {
659         EXPECT_EQ(SupplicantStatusCode::SUCCESS, status.code);
660     });
661 
662     sta_network_->enable(false, [](const SupplicantStatus& status) {
663         EXPECT_EQ(SupplicantStatusCode::SUCCESS, status.code);
664     });
665     sta_network_->enable(true, [](const SupplicantStatus& status) {
666         EXPECT_EQ(SupplicantStatusCode::SUCCESS, status.code);
667     });
668 
669     // Now remove the network and ensure that the calls fail.
670     removeNetwork();
671     sta_network_->enable(true, [](const SupplicantStatus& status) {
672         EXPECT_EQ(SupplicantStatusCode::FAILURE_NETWORK_INVALID, status.code);
673     });
674 }
675 
676 /*
677  * Disable
678  */
TEST_P(SupplicantStaNetworkHidlTest,Disable)679 TEST_P(SupplicantStaNetworkHidlTest, Disable) {
680     if (v1_3 != nullptr) {
681         GTEST_SKIP() << "Skipping test since HAL is 1.3 or higher";
682     }
683     // wpa_supplicant doesn't perform any connection initiation
684     // unless atleast the Ssid and Ket mgmt params are set.
685     sta_network_->setSsid(ssid_, [](const SupplicantStatus& status) {
686         EXPECT_EQ(SupplicantStatusCode::SUCCESS, status.code);
687     });
688     sta_network_->setKeyMgmt(kTestKeyMgmt, [](const SupplicantStatus& status) {
689         EXPECT_EQ(SupplicantStatusCode::SUCCESS, status.code);
690     });
691 
692     sta_network_->disable([](const SupplicantStatus& status) {
693         EXPECT_EQ(SupplicantStatusCode::SUCCESS, status.code);
694     });
695     // Now remove the network and ensure that the calls fail.
696     removeNetwork();
697     sta_network_->disable([](const SupplicantStatus& status) {
698         EXPECT_EQ(SupplicantStatusCode::FAILURE_NETWORK_INVALID, status.code);
699     });
700 }
701 
702 /*
703  * Select.
704  */
TEST_P(SupplicantStaNetworkHidlTest,Select)705 TEST_P(SupplicantStaNetworkHidlTest, Select) {
706     if (v1_3 != nullptr) {
707         GTEST_SKIP() << "Skipping test since HAL is 1.3 or higher";
708     }
709     // wpa_supplicant doesn't perform any connection initiation
710     // unless atleast the Ssid and Ket mgmt params are set.
711     sta_network_->setSsid(ssid_, [](const SupplicantStatus& status) {
712         EXPECT_EQ(SupplicantStatusCode::SUCCESS, status.code);
713     });
714     sta_network_->setKeyMgmt(kTestKeyMgmt, [](const SupplicantStatus& status) {
715         EXPECT_EQ(SupplicantStatusCode::SUCCESS, status.code);
716     });
717 
718     sta_network_->select([](const SupplicantStatus& status) {
719         EXPECT_EQ(SupplicantStatusCode::SUCCESS, status.code);
720     });
721     // Now remove the network and ensure that the calls fail.
722     removeNetwork();
723     sta_network_->select([](const SupplicantStatus& status) {
724         EXPECT_EQ(SupplicantStatusCode::FAILURE_NETWORK_INVALID, status.code);
725     });
726 }
727 
728 /*
729  * SendNetworkEapSimGsmAuthResponse
730  */
TEST_P(SupplicantStaNetworkHidlTest,SendNetworkEapSimGsmAuthResponse)731 TEST_P(SupplicantStaNetworkHidlTest, SendNetworkEapSimGsmAuthResponse) {
732     std::vector<ISupplicantStaNetwork::NetworkResponseEapSimGsmAuthParams>
733         params;
734     ISupplicantStaNetwork::NetworkResponseEapSimGsmAuthParams param;
735     memcpy(param.kc.data(), kTestKc, param.kc.size());
736     memcpy(param.sres.data(), kTestSres, param.sres.size());
737     params.push_back(param);
738     sta_network_->sendNetworkEapSimGsmAuthResponse(
739         params, [](const SupplicantStatus& status) {
740             EXPECT_EQ(SupplicantStatusCode::SUCCESS, status.code);
741         });
742 }
743 
744 /*
745  * SendNetworkEapSimGsmAuthFailure
746  */
TEST_P(SupplicantStaNetworkHidlTest,SendNetworkEapSimGsmAuthFailure)747 TEST_P(SupplicantStaNetworkHidlTest, SendNetworkEapSimGsmAuthFailure) {
748     EXPECT_EQ(SupplicantStatusCode::SUCCESS,
749               HIDL_INVOKE(sta_network_, sendNetworkEapSimGsmAuthFailure).code);
750 }
751 
752 /*
753  * SendNetworkEapSimUmtsAuthResponse
754  */
TEST_P(SupplicantStaNetworkHidlTest,SendNetworkEapSimUmtsAuthResponse)755 TEST_P(SupplicantStaNetworkHidlTest, SendNetworkEapSimUmtsAuthResponse) {
756     ISupplicantStaNetwork::NetworkResponseEapSimUmtsAuthParams params;
757     params.res = std::vector<uint8_t>(kTestRes, kTestRes + sizeof(kTestRes));
758     memcpy(params.ik.data(), kTestIk, params.ik.size());
759     memcpy(params.ck.data(), kTestCk, params.ck.size());
760     sta_network_->sendNetworkEapSimUmtsAuthResponse(
761         params, [](const SupplicantStatus& status) {
762             EXPECT_EQ(SupplicantStatusCode::SUCCESS, status.code);
763         });
764 }
765 
766 /*
767  * SendNetworkEapSimUmtsAuthFailure
768  */
TEST_P(SupplicantStaNetworkHidlTest,SendNetworkEapSimUmtsAuthFailure)769 TEST_P(SupplicantStaNetworkHidlTest, SendNetworkEapSimUmtsAuthFailure) {
770     EXPECT_EQ(SupplicantStatusCode::SUCCESS,
771               HIDL_INVOKE(sta_network_, sendNetworkEapSimUmtsAuthFailure).code);
772 }
773 
774 /*
775  * SendNetworkEapSimUmtsAutsResponse
776  */
TEST_P(SupplicantStaNetworkHidlTest,SendNetworkEapSimUmtsAutsResponse)777 TEST_P(SupplicantStaNetworkHidlTest, SendNetworkEapSimUmtsAutsResponse) {
778     EXPECT_EQ(SupplicantStatusCode::SUCCESS,
779               HIDL_INVOKE(sta_network_, sendNetworkEapSimUmtsAutsResponse,
780                           kTestAutParam)
781                   .code);
782 }
783 
784 /*
785  * SendNetworkEapIdentityResponse
786  */
TEST_P(SupplicantStaNetworkHidlTest,SendNetworkEapIdentityResponse)787 TEST_P(SupplicantStaNetworkHidlTest, SendNetworkEapIdentityResponse) {
788     sta_network_->sendNetworkEapIdentityResponse(
789         std::vector<uint8_t>(kTestIdentity,
790                              kTestIdentity + sizeof(kTestIdentity)),
791         [](const SupplicantStatus& status) {
792             EXPECT_EQ(SupplicantStatusCode::SUCCESS, status.code);
793         });
794 }
795 
796 /*
797  * SetUpdateIdentifier
798  */
TEST_P(SupplicantStaNetworkHidlTest,SetUpdateIdentifier)799 TEST_P(SupplicantStaNetworkHidlTest, SetUpdateIdentifier) {
800     EXPECT_EQ(
801         SupplicantStatusCode::SUCCESS,
802         HIDL_INVOKE(sta_network_, setUpdateIdentifier, kTestUpdateIdentifier)
803             .code);
804 }
805 
806 /*
807  * SetProactiveKeyCaching
808  */
TEST_P(SupplicantStaNetworkHidlTest,SetProactiveKeyCaching)809 TEST_P(SupplicantStaNetworkHidlTest, SetProactiveKeyCaching) {
810     EXPECT_EQ(SupplicantStatusCode::SUCCESS,
811               HIDL_INVOKE(sta_network_, setProactiveKeyCaching, true).code);
812     EXPECT_EQ(SupplicantStatusCode::SUCCESS,
813               HIDL_INVOKE(sta_network_, setProactiveKeyCaching, false).code);
814 }
815 
816 /*
817  * GetWpsNfcConfigurationToken
818  */
TEST_P(SupplicantStaNetworkHidlTest,GetWpsNfcConfigurationToken)819 TEST_P(SupplicantStaNetworkHidlTest, GetWpsNfcConfigurationToken) {
820     if (v1_3 != nullptr) {
821         GTEST_SKIP() << "Skipping test since HAL is 1.3 or higher";
822     }
823     ASSERT_EQ(SupplicantStatusCode::SUCCESS,
824               HIDL_INVOKE(sta_network_, setSsid, ssid_).code);
825     ASSERT_EQ(SupplicantStatusCode::SUCCESS,
826               HIDL_INVOKE(sta_network_, setKeyMgmt, kTestKeyMgmt).code);
827     ASSERT_EQ(
828         SupplicantStatusCode::SUCCESS,
829         HIDL_INVOKE(sta_network_, setPskPassphrase, kTestPskPassphrase).code);
830     const auto& status_and_token =
831         HIDL_INVOKE(sta_network_, getWpsNfcConfigurationToken);
832     EXPECT_EQ(SupplicantStatusCode::SUCCESS, status_and_token.first.code);
833     EXPECT_FALSE(0 == status_and_token.second.size());
834 }
835 
836 INSTANTIATE_TEST_CASE_P(
837     PerInstance, SupplicantStaNetworkHidlTest,
838     testing::Combine(
839         testing::ValuesIn(
840             android::hardware::getAllHalInstanceNames(IWifi::descriptor)),
841         testing::ValuesIn(android::hardware::getAllHalInstanceNames(
842             ISupplicant::descriptor))),
843     android::hardware::PrintInstanceTupleNameToString<>);
844