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/ISecureElementHalCallback.h>
24 #include <android/hardware/secure_element/1.2/ISecureElement.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_0::SecureElementStatus;
36 using ::android::hardware::secure_element::V1_1::ISecureElementHalCallback;
37 using ::android::hardware::secure_element::V1_2::ISecureElement;
38
39 constexpr char kCallbackNameOnStateChange[] = "onStateChange";
40
41 class SecureElementCallbackArgs {
42 public:
43 bool state_;
44 hidl_string reason_;
45 };
46
47 class SecureElementHalCallback
48 : public ::testing::VtsHalHidlTargetCallbackBase<SecureElementCallbackArgs>,
49 public ISecureElementHalCallback {
50 public:
51 virtual ~SecureElementHalCallback() = default;
52
onStateChange_1_1(bool state,const hidl_string & reason)53 Return<void> onStateChange_1_1(bool state, const hidl_string& reason) override {
54 SecureElementCallbackArgs args;
55 args.state_ = state;
56 args.reason_ = reason;
57 NotifyFromCallback(kCallbackNameOnStateChange, args);
58 return Void();
59 };
60
onStateChange(bool state)61 Return<void> onStateChange(__attribute__((unused)) bool state) override { return Void(); }
62 };
63
64 class SecureElementHidlTest : public ::testing::TestWithParam<std::string> {
65 public:
SetUp()66 virtual void SetUp() override {
67 LOG(INFO) << "get service with name:" << GetParam();
68 se_ = ISecureElement::getService(GetParam());
69 ASSERT_NE(se_, nullptr);
70
71 se_cb_ = new SecureElementHalCallback();
72 ASSERT_NE(se_cb_, nullptr);
73 se_->init_1_1(se_cb_);
74 auto res = se_cb_->WaitForCallback(kCallbackNameOnStateChange);
75 EXPECT_TRUE(res.no_timeout);
76 EXPECT_TRUE(res.args->state_);
77 EXPECT_NE(res.args->reason_, "");
78 }
79
80 sp<ISecureElement> se_;
81 sp<SecureElementHalCallback> se_cb_;
82 };
83
84 /*
85 * Reset:
86 * Calls reset()
87 * Checks status
88 * Check onStateChange is received with connected state set to false
89 * Check onStateChange is received with connected state set to true
90 */
TEST_P(SecureElementHidlTest,Reset)91 TEST_P(SecureElementHidlTest, Reset) {
92 EXPECT_EQ(SecureElementStatus::SUCCESS, se_->reset());
93
94 auto res = se_cb_->WaitForCallback(kCallbackNameOnStateChange);
95 EXPECT_TRUE(res.no_timeout);
96 EXPECT_FALSE(res.args->state_);
97
98 res = se_cb_->WaitForCallback(kCallbackNameOnStateChange);
99 EXPECT_TRUE(res.no_timeout);
100 EXPECT_TRUE(res.args->state_);
101 }
102
103 GTEST_ALLOW_UNINSTANTIATED_PARAMETERIZED_TEST(SecureElementHidlTest);
104 INSTANTIATE_TEST_SUITE_P(
105 PerInstance, SecureElementHidlTest,
106 testing::ValuesIn(android::hardware::getAllHalInstanceNames(ISecureElement::descriptor)),
107 android::hardware::PrintInstanceNameToString);
108