Steven Moreland | 6fe6954 | 2022-11-03 17:42:32 +0000 | [diff] [blame] | 1 | /* |
| 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 Chataing | cf06db3 | 2023-01-06 09:20:05 +0000 | [diff] [blame^] | 21 | #include <android-base/logging.h> |
Steven Moreland | 6fe6954 | 2022-11-03 17:42:32 +0000 | [diff] [blame] | 22 | #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 | |
| 31 | using namespace std::chrono_literals; |
| 32 | |
| 33 | using aidl::android::hardware::secure_element::BnSecureElementCallback; |
| 34 | using aidl::android::hardware::secure_element::ISecureElement; |
| 35 | using aidl::android::hardware::secure_element::LogicalChannelResponse; |
| 36 | using ndk::ScopedAStatus; |
| 37 | using ndk::SharedRefBase; |
| 38 | using ndk::SpAIBinder; |
| 39 | using testing::ElementsAre; |
| 40 | using 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 Chataing | cf06db3 | 2023-01-06 09:20:05 +0000 | [diff] [blame^] | 48 | #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. |
| 57 | static const std::vector<uint8_t> kDataApdu = { |
| 58 | 0x00, 0x08, 0x00, 0x00, 0x00, |
| 59 | }; |
| 60 | |
| 61 | // Selectable test AID defined in CTS tests. |
| 62 | static 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. |
| 67 | static 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 Moreland | 6fe6954 | 2022-11-03 17:42:32 +0000 | [diff] [blame] | 71 | |
| 72 | class 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 | |
| 96 | class SecureElementAidl : public ::testing::TestWithParam<std::string> { |
| 97 | public: |
Henri Chataing | cf06db3 | 2023-01-06 09:20:05 +0000 | [diff] [blame^] | 98 | void SetUp() override { |
Steven Moreland | 6fe6954 | 2022-11-03 17:42:32 +0000 | [diff] [blame] | 99 | SpAIBinder binder = SpAIBinder(AServiceManager_waitForService(GetParam().c_str())); |
Steven Moreland | 6fe6954 | 2022-11-03 17:42:32 +0000 | [diff] [blame] | 100 | |
Henri Chataing | cf06db3 | 2023-01-06 09:20:05 +0000 | [diff] [blame^] | 101 | secure_element_ = ISecureElement::fromBinder(binder); |
| 102 | ASSERT_NE(secure_element_, nullptr); |
Steven Moreland | 6fe6954 | 2022-11-03 17:42:32 +0000 | [diff] [blame] | 103 | |
Henri Chataing | cf06db3 | 2023-01-06 09:20:05 +0000 | [diff] [blame^] | 104 | 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 Moreland | 6fe6954 | 2022-11-03 17:42:32 +0000 | [diff] [blame] | 109 | } |
| 110 | |
Henri Chataing | cf06db3 | 2023-01-06 09:20:05 +0000 | [diff] [blame^] | 111 | 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 Moreland | 6fe6954 | 2022-11-03 17:42:32 +0000 | [diff] [blame] | 145 | }; |
| 146 | |
Henri Chataing | cf06db3 | 2023-01-06 09:20:05 +0000 | [diff] [blame^] | 147 | TEST_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 | |
| 156 | TEST_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 Moreland | 6fe6954 | 2022-11-03 17:42:32 +0000 | [diff] [blame] | 172 | TEST_P(SecureElementAidl, isCardPresent) { |
| 173 | bool res = false; |
Henri Chataing | cf06db3 | 2023-01-06 09:20:05 +0000 | [diff] [blame^] | 174 | |
| 175 | // isCardPresent called after init shall succeed. |
| 176 | EXPECT_OK(secure_element_->isCardPresent(&res)); |
Steven Moreland | 6fe6954 | 2022-11-03 17:42:32 +0000 | [diff] [blame] | 177 | EXPECT_TRUE(res); |
| 178 | } |
| 179 | |
Steven Moreland | 6fe6954 | 2022-11-03 17:42:32 +0000 | [diff] [blame] | 180 | TEST_P(SecureElementAidl, getAtr) { |
| 181 | std::vector<uint8_t> atr; |
Henri Chataing | cf06db3 | 2023-01-06 09:20:05 +0000 | [diff] [blame^] | 182 | |
| 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 Moreland | 6fe6954 | 2022-11-03 17:42:32 +0000 | [diff] [blame] | 186 | EXPECT_LE(atr.size(), 32u); |
Steven Moreland | 6fe6954 | 2022-11-03 17:42:32 +0000 | [diff] [blame] | 187 | } |
| 188 | |
Henri Chataing | cf06db3 | 2023-01-06 09:20:05 +0000 | [diff] [blame^] | 189 | TEST_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 | |
| 213 | TEST_P(SecureElementAidl, openLogicalChannel) { |
Steven Moreland | 6fe6954 | 2022-11-03 17:42:32 +0000 | [diff] [blame] | 214 | LogicalChannelResponse response; |
Henri Chataing | cf06db3 | 2023-01-06 09:20:05 +0000 | [diff] [blame^] | 215 | |
| 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 Moreland | 6fe6954 | 2022-11-03 17:42:32 +0000 | [diff] [blame] | 223 | EXPECT_GE(response.selectResponse.size(), 2u); |
Henri Chataing | cf06db3 | 2023-01-06 09:20:05 +0000 | [diff] [blame^] | 224 | 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 Moreland | 6fe6954 | 2022-11-03 17:42:32 +0000 | [diff] [blame] | 229 | } |
| 230 | |
Henri Chataing | cf06db3 | 2023-01-06 09:20:05 +0000 | [diff] [blame^] | 231 | TEST_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 Moreland | 6fe6954 | 2022-11-03 17:42:32 +0000 | [diff] [blame] | 253 | } |
| 254 | |
Henri Chataing | cf06db3 | 2023-01-06 09:20:05 +0000 | [diff] [blame^] | 255 | TEST_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 Moreland | 6fe6954 | 2022-11-03 17:42:32 +0000 | [diff] [blame] | 265 | } |
| 266 | |
| 267 | GTEST_ALLOW_UNINSTANTIATED_PARAMETERIZED_TEST(SecureElementAidl); |
| 268 | INSTANTIATE_TEST_SUITE_P( |
| 269 | SecureElement, SecureElementAidl, |
| 270 | testing::ValuesIn(android::getAidlHalInstanceNames(ISecureElement::descriptor)), |
| 271 | android::PrintInstanceNameToString); |
| 272 | |
| 273 | int main(int argc, char** argv) { |
| 274 | ::testing::InitGoogleTest(&argc, argv); |
| 275 | ABinderProcess_setThreadPoolMaxThreadCount(1); |
| 276 | ABinderProcess_startThreadPool(); |
| 277 | return RUN_ALL_TESTS(); |
| 278 | } |