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