1 /*
2 * Copyright (C) 2019 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 <string>
18
19 #define LOG_TAG "secure_element_hidl_hal_test"
20 #include <android-base/logging.h>
21
22 #include <android/hardware/secure_element/1.0/types.h>
23 #include <android/hardware/secure_element/1.1/ISecureElement.h>
24 #include <android/hardware/secure_element/1.1/ISecureElementHalCallback.h>
25 #include <gtest/gtest.h>
26 #include <hidl/GtestPrinter.h>
27 #include <hidl/ServiceManagement.h>
28
29 #include <VtsHalHidlTargetCallbackBase.h>
30
31 using ::android::sp;
32 using ::android::hardware::hidl_string;
33 using ::android::hardware::Return;
34 using ::android::hardware::Void;
35 using ::android::hardware::secure_element::V1_1::ISecureElement;
36 using ::android::hardware::secure_element::V1_1::ISecureElementHalCallback;
37
38 constexpr char kCallbackNameOnStateChange[] = "onStateChange";
39
40 class SecureElementCallbackArgs {
41 public:
42 bool state_;
43 hidl_string reason_;
44 };
45
46 class SecureElementHalCallback
47 : public ::testing::VtsHalHidlTargetCallbackBase<SecureElementCallbackArgs>,
48 public ISecureElementHalCallback {
49 public:
50 virtual ~SecureElementHalCallback() = default;
51
onStateChange_1_1(bool state,const hidl_string & reason)52 Return<void> onStateChange_1_1(bool state, const hidl_string& reason) override {
53 SecureElementCallbackArgs args;
54 args.state_ = state;
55 args.reason_ = reason;
56 NotifyFromCallback(kCallbackNameOnStateChange, args);
57 return Void();
58 };
59
onStateChange(bool state)60 Return<void> onStateChange(__attribute__((unused)) bool state) override { return Void(); }
61 };
62
63 class SecureElementHidlTest : public ::testing::TestWithParam<std::string> {
64 public:
SetUp()65 virtual void SetUp() override {
66 LOG(INFO) << "get service with name:" << GetParam();
67 se_ = ISecureElement::getService(GetParam());
68 ASSERT_NE(se_, nullptr);
69
70 se_cb_ = new SecureElementHalCallback();
71 ASSERT_NE(se_cb_, nullptr);
72 se_->init_1_1(se_cb_);
73 auto res = se_cb_->WaitForCallback(kCallbackNameOnStateChange);
74 EXPECT_TRUE(res.no_timeout);
75 EXPECT_TRUE(res.args->state_);
76 EXPECT_NE(res.args->reason_, "");
77 }
78
79 sp<ISecureElement> se_;
80 sp<SecureElementHalCallback> se_cb_;
81 };
82
83 /*
84 * isCardPresent:
85 * Expects the card to be present
86 */
TEST_P(SecureElementHidlTest,isCardPresent)87 TEST_P(SecureElementHidlTest, isCardPresent) {
88 EXPECT_TRUE(se_->isCardPresent());
89 }
90
91 GTEST_ALLOW_UNINSTANTIATED_PARAMETERIZED_TEST(SecureElementHidlTest);
92 INSTANTIATE_TEST_SUITE_P(
93 PerInstance, SecureElementHidlTest,
94 testing::ValuesIn(android::hardware::getAllHalInstanceNames(ISecureElement::descriptor)),
95 android::hardware::PrintInstanceNameToString);
96