blob: c2655793ba93d08acc94b0f2f4681decade68f3b [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 {
112 secure_element_ = nullptr;
113 secure_element_callback_ = nullptr;
114 }
115
116 // Call transmit with kDataApdu and the selected channel number.
117 // Return the response sstatus code.
118 uint16_t transmit(uint8_t channel_number) {
119 std::vector<uint8_t> apdu = kDataApdu;
120 std::vector<uint8_t> response;
121
122 // Edit the channel number into the CLA header byte.
123 if (channel_number < 4) {
124 apdu[0] |= channel_number;
125 } else {
126 apdu[0] |= (channel_number - 4) | 0x40;
127 }
128
129 EXPECT_OK(secure_element_->transmit(apdu, &response));
130 EXPECT_GE(response.size(), 2u);
131 uint16_t status =
132 (response[response.size() - 2] << 8) | (response[response.size() - 1] << 0);
133
134 // When the command is successful the response
135 // must contain 256 bytes of data.
136 if (status == 0x9000) {
137 EXPECT_EQ(response.size(), 258);
138 }
139
140 return status;
141 }
142
143 std::shared_ptr<ISecureElement> secure_element_;
144 std::shared_ptr<MySecureElementCallback> secure_element_callback_;
Steven Moreland6fe69542022-11-03 17:42:32 +0000145};
146
Henri Chataingcf06db32023-01-06 09:20:05 +0000147TEST_P(SecureElementAidl, init) {
148 // init(nullptr) shall fail.
149 EXPECT_ERR(secure_element_->init(nullptr));
150
151 // init with a valid callback pointer shall succeed.
152 EXPECT_OK(secure_element_->init(secure_element_callback_));
153 secure_element_callback_->expectCallbackHistory({true, true});
154}
155
156TEST_P(SecureElementAidl, reset) {
157 std::vector<uint8_t> basic_channel_response;
158 LogicalChannelResponse logical_channel_response;
159
160 // reset called after init shall succeed.
161 EXPECT_OK(secure_element_->openBasicChannel(kSelectableAid, 0x00, &basic_channel_response));
162 EXPECT_OK(secure_element_->openLogicalChannel(kSelectableAid, 0x00, &logical_channel_response));
163
164 EXPECT_OK(secure_element_->reset());
165 secure_element_callback_->expectCallbackHistory({true, false, true});
166
167 // All opened channels must be closed.
168 EXPECT_NE(transmit(0), 0x9000);
169 EXPECT_NE(transmit(logical_channel_response.channelNumber), 0x9000);
170}
171
Steven Moreland6fe69542022-11-03 17:42:32 +0000172TEST_P(SecureElementAidl, isCardPresent) {
173 bool res = false;
Henri Chataingcf06db32023-01-06 09:20:05 +0000174
175 // isCardPresent called after init shall succeed.
176 EXPECT_OK(secure_element_->isCardPresent(&res));
Steven Moreland6fe69542022-11-03 17:42:32 +0000177 EXPECT_TRUE(res);
178}
179
Steven Moreland6fe69542022-11-03 17:42:32 +0000180TEST_P(SecureElementAidl, getAtr) {
181 std::vector<uint8_t> atr;
Henri Chataingcf06db32023-01-06 09:20:05 +0000182
183 // getAtr called after init shall succeed.
184 // The ATR has size between 0 and 32 bytes.
185 EXPECT_OK(secure_element_->getAtr(&atr));
Steven Moreland6fe69542022-11-03 17:42:32 +0000186 EXPECT_LE(atr.size(), 32u);
Steven Moreland6fe69542022-11-03 17:42:32 +0000187}
188
Henri Chataingcf06db32023-01-06 09:20:05 +0000189TEST_P(SecureElementAidl, openBasicChannel) {
190 std::vector<uint8_t> response;
191
192 // openBasicChannel called with an invalid AID shall fail.
193 EXPECT_ERR(secure_element_->openBasicChannel(kNonSelectableAid, 0x00, &response));
194
195 // openBasicChannel called after init shall succeed.
196 // The response size must be larger than 2 bytes as it includes the
197 // status code.
198 EXPECT_OK(secure_element_->openBasicChannel(kSelectableAid, 0x00, &response));
199 EXPECT_GE(response.size(), 2u);
200
201 // tramsmit called on the basic channel should succeed.
202 EXPECT_EQ(transmit(0), 0x9000);
203
204 // openBasicChannel called a second time shall fail.
205 // The basic channel can only be opened once.
206 EXPECT_ERR(secure_element_->openBasicChannel(kSelectableAid, 0x00, &response));
207
208 // openBasicChannel called after closing the basic channel shall succeed.
209 EXPECT_OK(secure_element_->closeChannel(0));
210 EXPECT_OK(secure_element_->openBasicChannel(kSelectableAid, 0x00, &response));
211}
212
213TEST_P(SecureElementAidl, openLogicalChannel) {
Steven Moreland6fe69542022-11-03 17:42:32 +0000214 LogicalChannelResponse response;
Henri Chataingcf06db32023-01-06 09:20:05 +0000215
216 // openLogicalChannel called with an invalid AID shall fail.
217 EXPECT_ERR(secure_element_->openLogicalChannel(kNonSelectableAid, 0x00, &response));
218
219 // openLogicalChannel called after init shall succeed.
220 // The response size must be larger than 2 bytes as it includes the
221 // status code. The channel number must be in the range 1-19.
222 EXPECT_OK(secure_element_->openLogicalChannel(kSelectableAid, 0x00, &response));
Steven Moreland6fe69542022-11-03 17:42:32 +0000223 EXPECT_GE(response.selectResponse.size(), 2u);
Henri Chataingcf06db32023-01-06 09:20:05 +0000224 EXPECT_GE(response.channelNumber, 1u);
225 EXPECT_LE(response.channelNumber, 19u);
226
227 // tramsmit called on the logical channel should succeed.
228 EXPECT_EQ(transmit(response.channelNumber), 0x9000);
Steven Moreland6fe69542022-11-03 17:42:32 +0000229}
230
Henri Chataingcf06db32023-01-06 09:20:05 +0000231TEST_P(SecureElementAidl, closeChannel) {
232 std::vector<uint8_t> basic_channel_response;
233 LogicalChannelResponse logical_channel_response;
234
235 // closeChannel called on non-existing basic or logical channel is a no-op
236 // and shall succeed.
237 EXPECT_OK(secure_element_->closeChannel(0));
238 EXPECT_OK(secure_element_->closeChannel(1));
239
240 // closeChannel called on basic channel closes the basic channel.
241 EXPECT_OK(secure_element_->openBasicChannel(kSelectableAid, 0x00, &basic_channel_response));
242 EXPECT_OK(secure_element_->closeChannel(0));
243
244 // tramsmit called on the basic channel should fail.
245 EXPECT_NE(transmit(0), 0x9000);
246
247 // closeChannel called on logical channel closes the logical channel.
248 EXPECT_OK(secure_element_->openLogicalChannel(kSelectableAid, 0x00, &logical_channel_response));
249 EXPECT_OK(secure_element_->closeChannel(logical_channel_response.channelNumber));
250
251 // tramsmit called on the basic channel should fail.
252 EXPECT_NE(transmit(logical_channel_response.channelNumber), 0x9000);
Steven Moreland6fe69542022-11-03 17:42:32 +0000253}
254
Henri Chataingcf06db32023-01-06 09:20:05 +0000255TEST_P(SecureElementAidl, transmit) {
256 std::vector<uint8_t> response;
257
258 // transmit called after init shall succeed.
259 // Note: no channel is opened for this test and the transmit
260 // response will have the status SW_LOGICAL_CHANNEL_NOT_SUPPORTED.
261 // The transmit response shall be larger than 2 bytes as it includes the
262 // status code.
263 EXPECT_OK(secure_element_->transmit(kDataApdu, &response));
264 EXPECT_GE(response.size(), 2u);
Steven Moreland6fe69542022-11-03 17:42:32 +0000265}
266
267GTEST_ALLOW_UNINSTANTIATED_PARAMETERIZED_TEST(SecureElementAidl);
268INSTANTIATE_TEST_SUITE_P(
269 SecureElement, SecureElementAidl,
270 testing::ValuesIn(android::getAidlHalInstanceNames(ISecureElement::descriptor)),
271 android::PrintInstanceNameToString);
272
273int main(int argc, char** argv) {
274 ::testing::InitGoogleTest(&argc, argv);
275 ABinderProcess_setThreadPoolMaxThreadCount(1);
276 ABinderProcess_startThreadPool();
277 return RUN_ALL_TESTS();
278}