blob: 37ff1b207a41f78bf10d71cda50b94bf0d7f71ac [file] [log] [blame]
Steven Moreland6fe69542022-11-03 17:42:32 +00001/*
2 * Copyright (C) 2022 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 <aidl/Gtest.h>
18#include <aidl/Vintf.h>
19#include <aidl/android/hardware/secure_element/BnSecureElementCallback.h>
20#include <aidl/android/hardware/secure_element/ISecureElement.h>
Henri Chataingcf06db32023-01-06 09:20:05 +000021#include <android-base/logging.h>
Steven Moreland6fe69542022-11-03 17:42:32 +000022#include <android/binder_manager.h>
23#include <android/binder_process.h>
24#include <gmock/gmock.h>
25#include <gtest/gtest.h>
26
27#include <chrono>
28#include <condition_variable>
29#include <mutex>
30
31using namespace std::chrono_literals;
32
33using aidl::android::hardware::secure_element::BnSecureElementCallback;
34using aidl::android::hardware::secure_element::ISecureElement;
35using aidl::android::hardware::secure_element::LogicalChannelResponse;
36using ndk::ScopedAStatus;
37using ndk::SharedRefBase;
38using ndk::SpAIBinder;
39using testing::ElementsAre;
40using testing::ElementsAreArray;
41
42#define EXPECT_OK(status) \
43 do { \
44 auto status_impl = (status); \
45 EXPECT_TRUE(status_impl.isOk()) << status_impl.getDescription(); \
46 } while (false)
47
Henri Chataingcf06db32023-01-06 09:20:05 +000048#define EXPECT_ERR(status) \
49 do { \
50 auto status_impl = (status); \
51 EXPECT_FALSE(status_impl.isOk()) << status_impl.getDescription(); \
52 } while (false)
53
54// APDU defined in CTS tests.
55// The applet selected with kSelectableAid will return 256 bytes of data
56// in response.
57static const std::vector<uint8_t> kDataApdu = {
58 0x00, 0x08, 0x00, 0x00, 0x00,
59};
60
61// Selectable test AID defined in CTS tests.
62static const std::vector<uint8_t> kSelectableAid = {
63 0xA0, 0x00, 0x00, 0x04, 0x76, 0x41, 0x6E, 0x64,
64 0x72, 0x6F, 0x69, 0x64, 0x43, 0x54, 0x53, 0x31,
65};
66// Non-selectable test AID defined in CTS tests.
67static const std::vector<uint8_t> kNonSelectableAid = {
68 0xA0, 0x00, 0x00, 0x04, 0x76, 0x41, 0x6E, 0x64,
69 0x72, 0x6F, 0x69, 0x64, 0x43, 0x54, 0x53, 0xFF,
70};
Steven Moreland6fe69542022-11-03 17:42:32 +000071
72class MySecureElementCallback : public BnSecureElementCallback {
73 public:
74 ScopedAStatus onStateChange(bool state, const std::string& debugReason) override {
75 {
76 std::unique_lock<std::mutex> l(m);
77 (void)debugReason;
78 history.push_back(state);
79 }
80 cv.notify_one();
81 return ScopedAStatus::ok();
82 };
83
84 void expectCallbackHistory(std::vector<bool>&& want) {
85 std::unique_lock<std::mutex> l(m);
86 cv.wait_for(l, 2s, [&]() { return history.size() >= want.size(); });
87 EXPECT_THAT(history, ElementsAreArray(want));
88 }
89
90 private:
91 std::mutex m; // guards history
92 std::condition_variable cv;
93 std::vector<bool> history;
94};
95
96class SecureElementAidl : public ::testing::TestWithParam<std::string> {
97 public:
Henri Chataingcf06db32023-01-06 09:20:05 +000098 void SetUp() override {
Steven Moreland6fe69542022-11-03 17:42:32 +000099 SpAIBinder binder = SpAIBinder(AServiceManager_waitForService(GetParam().c_str()));
Steven Moreland6fe69542022-11-03 17:42:32 +0000100
Henri Chataingcf06db32023-01-06 09:20:05 +0000101 secure_element_ = ISecureElement::fromBinder(binder);
102 ASSERT_NE(secure_element_, nullptr);
Steven Moreland6fe69542022-11-03 17:42:32 +0000103
Henri Chataingcf06db32023-01-06 09:20:05 +0000104 secure_element_callback_ = SharedRefBase::make<MySecureElementCallback>();
105 ASSERT_NE(secure_element_callback_, nullptr);
106
107 EXPECT_OK(secure_element_->init(secure_element_callback_));
108 secure_element_callback_->expectCallbackHistory({true});
Steven Moreland6fe69542022-11-03 17:42:32 +0000109 }
110
Henri Chataingcf06db32023-01-06 09:20:05 +0000111 void TearDown() override {
Henri Chataing4b780eb2023-01-31 18:22:57 +0000112 EXPECT_OK(secure_element_->reset());
Henri Chataingcf06db32023-01-06 09:20:05 +0000113 secure_element_ = nullptr;
114 secure_element_callback_ = nullptr;
115 }
116
117 // Call transmit with kDataApdu and the selected channel number.
118 // Return the response sstatus code.
119 uint16_t transmit(uint8_t channel_number) {
120 std::vector<uint8_t> apdu = kDataApdu;
121 std::vector<uint8_t> response;
122
123 // Edit the channel number into the CLA header byte.
124 if (channel_number < 4) {
125 apdu[0] |= channel_number;
126 } else {
127 apdu[0] |= (channel_number - 4) | 0x40;
128 }
129
130 EXPECT_OK(secure_element_->transmit(apdu, &response));
131 EXPECT_GE(response.size(), 2u);
132 uint16_t status =
133 (response[response.size() - 2] << 8) | (response[response.size() - 1] << 0);
134
135 // When the command is successful the response
136 // must contain 256 bytes of data.
137 if (status == 0x9000) {
138 EXPECT_EQ(response.size(), 258);
139 }
140
141 return status;
142 }
143
144 std::shared_ptr<ISecureElement> secure_element_;
145 std::shared_ptr<MySecureElementCallback> secure_element_callback_;
Steven Moreland6fe69542022-11-03 17:42:32 +0000146};
147
Henri Chataingcf06db32023-01-06 09:20:05 +0000148TEST_P(SecureElementAidl, init) {
149 // init(nullptr) shall fail.
150 EXPECT_ERR(secure_element_->init(nullptr));
151
152 // init with a valid callback pointer shall succeed.
153 EXPECT_OK(secure_element_->init(secure_element_callback_));
154 secure_element_callback_->expectCallbackHistory({true, true});
155}
156
157TEST_P(SecureElementAidl, reset) {
158 std::vector<uint8_t> basic_channel_response;
159 LogicalChannelResponse logical_channel_response;
160
161 // reset called after init shall succeed.
162 EXPECT_OK(secure_element_->openBasicChannel(kSelectableAid, 0x00, &basic_channel_response));
163 EXPECT_OK(secure_element_->openLogicalChannel(kSelectableAid, 0x00, &logical_channel_response));
164
165 EXPECT_OK(secure_element_->reset());
166 secure_element_callback_->expectCallbackHistory({true, false, true});
167
168 // All opened channels must be closed.
169 EXPECT_NE(transmit(0), 0x9000);
170 EXPECT_NE(transmit(logical_channel_response.channelNumber), 0x9000);
171}
172
Steven Moreland6fe69542022-11-03 17:42:32 +0000173TEST_P(SecureElementAidl, isCardPresent) {
174 bool res = false;
Henri Chataingcf06db32023-01-06 09:20:05 +0000175
176 // isCardPresent called after init shall succeed.
177 EXPECT_OK(secure_element_->isCardPresent(&res));
Steven Moreland6fe69542022-11-03 17:42:32 +0000178 EXPECT_TRUE(res);
179}
180
Steven Moreland6fe69542022-11-03 17:42:32 +0000181TEST_P(SecureElementAidl, getAtr) {
182 std::vector<uint8_t> atr;
Henri Chataingcf06db32023-01-06 09:20:05 +0000183
184 // getAtr called after init shall succeed.
185 // The ATR has size between 0 and 32 bytes.
186 EXPECT_OK(secure_element_->getAtr(&atr));
Steven Moreland6fe69542022-11-03 17:42:32 +0000187 EXPECT_LE(atr.size(), 32u);
Steven Moreland6fe69542022-11-03 17:42:32 +0000188}
189
Henri Chataingcf06db32023-01-06 09:20:05 +0000190TEST_P(SecureElementAidl, openBasicChannel) {
191 std::vector<uint8_t> response;
192
193 // openBasicChannel called with an invalid AID shall fail.
194 EXPECT_ERR(secure_element_->openBasicChannel(kNonSelectableAid, 0x00, &response));
195
196 // openBasicChannel called after init shall succeed.
197 // The response size must be larger than 2 bytes as it includes the
198 // status code.
199 EXPECT_OK(secure_element_->openBasicChannel(kSelectableAid, 0x00, &response));
200 EXPECT_GE(response.size(), 2u);
201
202 // tramsmit called on the basic channel should succeed.
203 EXPECT_EQ(transmit(0), 0x9000);
204
205 // openBasicChannel called a second time shall fail.
206 // The basic channel can only be opened once.
207 EXPECT_ERR(secure_element_->openBasicChannel(kSelectableAid, 0x00, &response));
208
209 // openBasicChannel called after closing the basic channel shall succeed.
210 EXPECT_OK(secure_element_->closeChannel(0));
211 EXPECT_OK(secure_element_->openBasicChannel(kSelectableAid, 0x00, &response));
212}
213
214TEST_P(SecureElementAidl, openLogicalChannel) {
Steven Moreland6fe69542022-11-03 17:42:32 +0000215 LogicalChannelResponse response;
Henri Chataingcf06db32023-01-06 09:20:05 +0000216
217 // openLogicalChannel called with an invalid AID shall fail.
218 EXPECT_ERR(secure_element_->openLogicalChannel(kNonSelectableAid, 0x00, &response));
219
220 // openLogicalChannel called after init shall succeed.
221 // The response size must be larger than 2 bytes as it includes the
222 // status code. The channel number must be in the range 1-19.
223 EXPECT_OK(secure_element_->openLogicalChannel(kSelectableAid, 0x00, &response));
Steven Moreland6fe69542022-11-03 17:42:32 +0000224 EXPECT_GE(response.selectResponse.size(), 2u);
Henri Chataingcf06db32023-01-06 09:20:05 +0000225 EXPECT_GE(response.channelNumber, 1u);
226 EXPECT_LE(response.channelNumber, 19u);
227
228 // tramsmit called on the logical channel should succeed.
229 EXPECT_EQ(transmit(response.channelNumber), 0x9000);
Steven Moreland6fe69542022-11-03 17:42:32 +0000230}
231
Henri Chataingcf06db32023-01-06 09:20:05 +0000232TEST_P(SecureElementAidl, closeChannel) {
233 std::vector<uint8_t> basic_channel_response;
234 LogicalChannelResponse logical_channel_response;
235
Henri Chataing4b780eb2023-01-31 18:22:57 +0000236 // closeChannel called on non-existing basic or logical channel
237 // shall fail.
238 EXPECT_ERR(secure_element_->closeChannel(0));
239 EXPECT_ERR(secure_element_->closeChannel(1));
Henri Chataingcf06db32023-01-06 09:20:05 +0000240
241 // closeChannel called on basic channel closes the basic channel.
242 EXPECT_OK(secure_element_->openBasicChannel(kSelectableAid, 0x00, &basic_channel_response));
243 EXPECT_OK(secure_element_->closeChannel(0));
244
245 // tramsmit called on the basic channel should fail.
246 EXPECT_NE(transmit(0), 0x9000);
247
248 // closeChannel called on logical channel closes the logical channel.
249 EXPECT_OK(secure_element_->openLogicalChannel(kSelectableAid, 0x00, &logical_channel_response));
250 EXPECT_OK(secure_element_->closeChannel(logical_channel_response.channelNumber));
251
252 // tramsmit called on the basic channel should fail.
253 EXPECT_NE(transmit(logical_channel_response.channelNumber), 0x9000);
Steven Moreland6fe69542022-11-03 17:42:32 +0000254}
255
Henri Chataingcf06db32023-01-06 09:20:05 +0000256TEST_P(SecureElementAidl, transmit) {
257 std::vector<uint8_t> response;
258
259 // transmit called after init shall succeed.
260 // Note: no channel is opened for this test and the transmit
261 // response will have the status SW_LOGICAL_CHANNEL_NOT_SUPPORTED.
262 // The transmit response shall be larger than 2 bytes as it includes the
263 // status code.
264 EXPECT_OK(secure_element_->transmit(kDataApdu, &response));
265 EXPECT_GE(response.size(), 2u);
Steven Moreland6fe69542022-11-03 17:42:32 +0000266}
267
268GTEST_ALLOW_UNINSTANTIATED_PARAMETERIZED_TEST(SecureElementAidl);
269INSTANTIATE_TEST_SUITE_P(
270 SecureElement, SecureElementAidl,
271 testing::ValuesIn(android::getAidlHalInstanceNames(ISecureElement::descriptor)),
272 android::PrintInstanceNameToString);
273
274int main(int argc, char** argv) {
275 ::testing::InitGoogleTest(&argc, argv);
276 ABinderProcess_setThreadPoolMaxThreadCount(1);
277 ABinderProcess_startThreadPool();
278 return RUN_ALL_TESTS();
279}