1/*
2 * Copyright (C) 2018 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#define LOG_TAG "secure_element_hidl_hal_test"
18#include <android-base/logging.h>
19
20#include <android/hardware/secure_element/1.0/ISecureElement.h>
21#include <android/hardware/secure_element/1.0/ISecureElementHalCallback.h>
22#include <android/hardware/secure_element/1.0/types.h>
23
24#include <VtsHalHidlTargetCallbackBase.h>
25#include <VtsHalHidlTargetTestBase.h>
26#include <VtsHalHidlTargetTestEnvBase.h>
27
28using ::android::hardware::secure_element::V1_0::ISecureElement;
29using ::android::hardware::secure_element::V1_0::ISecureElementHalCallback;
30using ::android::hardware::secure_element::V1_0::SecureElementStatus;
31using ::android::hardware::secure_element::V1_0::LogicalChannelResponse;
32using ::android::hardware::Return;
33using ::android::hardware::Void;
34using ::android::sp;
35using ::testing::VtsHalHidlTargetTestEnvBase;
36
37#define DATA_APDU \
38    { 0x00, 0x08, 0x00, 0x00, 0x00 }
39#define ANDROID_TEST_AID                                                                          \
40    {                                                                                             \
41        0xA0, 0x00, 0x00, 0x04, 0x76, 0x41, 0x6E, 0x64, 0x72, 0x6F, 0x69, 0x64, 0x43, 0x54, 0x53, \
42            0x31                                                                                  \
43    }
44
45constexpr char kCallbackNameOnStateChange[] = "onStateChange";
46
47class SecureElementCallbackArgs {
48   public:
49    bool state_;
50};
51
52class SecureElementHalCallback
53    : public ::testing::VtsHalHidlTargetCallbackBase<SecureElementCallbackArgs>,
54      public ISecureElementHalCallback {
55   public:
56    virtual ~SecureElementHalCallback() = default;
57
58    Return<void> onStateChange(bool state) override {
59        SecureElementCallbackArgs args;
60        args.state_ = state;
61        NotifyFromCallback(kCallbackNameOnStateChange, args);
62        return Void();
63    };
64};
65
66class SecureElementHidlEnvironment : public VtsHalHidlTargetTestEnvBase {
67   public:
68    // get the test environment singleton
69    static SecureElementHidlEnvironment* Instance() {
70        static SecureElementHidlEnvironment* instance = new SecureElementHidlEnvironment;
71        return instance;
72    }
73
74    virtual void registerTestServices() override { registerTestService<ISecureElement>(); }
75
76   private:
77    SecureElementHidlEnvironment() {}
78
79    GTEST_DISALLOW_COPY_AND_ASSIGN_(SecureElementHidlEnvironment);
80};
81
82class SecureElementHidlTest : public ::testing::VtsHalHidlTargetTestBase {
83   public:
84    virtual void SetUp() override {
85        std::string serviceName =
86            SecureElementHidlEnvironment::Instance()->getServiceName<ISecureElement>("eSE1");
87        LOG(INFO) << "get service with name:" << serviceName;
88        ASSERT_FALSE(serviceName.empty());
89        se_ = ::testing::VtsHalHidlTargetTestBase::getService<ISecureElement>(serviceName);
90        ASSERT_NE(se_, nullptr);
91
92        se_cb_ = new SecureElementHalCallback();
93        ASSERT_NE(se_cb_, nullptr);
94        se_->init(se_cb_);
95        auto res = se_cb_->WaitForCallback(kCallbackNameOnStateChange);
96        EXPECT_TRUE(res.no_timeout);
97        EXPECT_TRUE(res.args->state_);
98    }
99
100    sp<ISecureElement> se_;
101    sp<SecureElementHalCallback> se_cb_;
102};
103
104/*
105 * isCardPresent:
106 * Expects the card to be present
107 */
108TEST_F(SecureElementHidlTest, isCardPresent) {
109    EXPECT_TRUE(se_->isCardPresent());
110}
111
112/*
113 * transmit:
114 * Check status word in the response
115 */
116TEST_F(SecureElementHidlTest, transmit) {
117    std::vector<uint8_t> aid = ANDROID_TEST_AID;
118    SecureElementStatus statusReturned;
119    LogicalChannelResponse response;
120    se_->openLogicalChannel(
121        aid, 0x00,
122        [&statusReturned, &response](LogicalChannelResponse channelResponse,
123                                     SecureElementStatus status) {
124            statusReturned = status;
125            if (status == SecureElementStatus::SUCCESS) {
126                response.channelNumber = channelResponse.channelNumber;
127                response.selectResponse.resize(channelResponse.selectResponse.size());
128                for (size_t i = 0; i < channelResponse.selectResponse.size(); i++) {
129                    response.selectResponse[i] = channelResponse.selectResponse[i];
130                }
131            }
132        });
133    EXPECT_EQ(SecureElementStatus::SUCCESS, statusReturned);
134    EXPECT_LE((unsigned int)2, response.selectResponse.size());
135    EXPECT_LE(1, response.channelNumber);
136    std::vector<uint8_t> command = DATA_APDU;
137    command[0] |= response.channelNumber;
138    std::vector<uint8_t> transmitResponse;
139    se_->transmit(command, [&transmitResponse](std::vector<uint8_t> res) {
140        transmitResponse.resize(res.size());
141        for (size_t i = 0; i < res.size(); i++) {
142            transmitResponse[i] = res[i];
143        }
144    });
145    EXPECT_LE((unsigned int)3, transmitResponse.size());
146    EXPECT_EQ(0x90, transmitResponse[transmitResponse.size() - 2]);
147    EXPECT_EQ(0x00, transmitResponse[transmitResponse.size() - 1]);
148    EXPECT_EQ(SecureElementStatus::SUCCESS, se_->closeChannel(response.channelNumber));
149}
150
151/*
152 * OpenCloseBasicChannel:
153 * If the secure element allows opening of basic channel:
154 *  open channel, check the length of selectResponse and close the channel
155 */
156TEST_F(SecureElementHidlTest, openBasicChannel) {
157    std::vector<uint8_t> aid = ANDROID_TEST_AID;
158    SecureElementStatus statusReturned;
159    std::vector<uint8_t> response;
160    se_->openBasicChannel(aid, 0x00,
161                          [&statusReturned, &response](std::vector<uint8_t> selectResponse,
162                                                       SecureElementStatus status) {
163                              statusReturned = status;
164                              if (status == SecureElementStatus::SUCCESS) {
165                                  response.resize(selectResponse.size());
166                                  for (size_t i = 0; i < selectResponse.size(); i++) {
167                                      response[i] = selectResponse[i];
168                                  }
169                              }
170                          });
171    if (statusReturned == SecureElementStatus::SUCCESS) {
172        EXPECT_LE((unsigned int)2, response.size());
173        se_->closeChannel(0);
174        return;
175    }
176    EXPECT_EQ(SecureElementStatus::UNSUPPORTED_OPERATION, statusReturned);
177}
178
179/*
180 * GetATR
181 */
182TEST_F(SecureElementHidlTest, getAtr) {
183    std::vector<uint8_t> atr;
184    se_->getAtr([&atr](std::vector<uint8_t> atrReturned) {
185        atr.resize(atrReturned.size());
186        for (size_t i = 0; i < atrReturned.size(); i++) {
187            atr[i] = atrReturned[i];
188        }
189    });
190    if (atr.size() == 0) {
191        return;
192    }
193    EXPECT_GE((unsigned int)32, atr.size());
194    EXPECT_LE((unsigned int)1, atr.size());
195}
196
197/*
198 * OpenCloseLogicalChannel:
199 * Open Channel
200 * Check status
201 * Close Channel
202 */
203TEST_F(SecureElementHidlTest, openCloseLogicalChannel) {
204    std::vector<uint8_t> aid = ANDROID_TEST_AID;
205    SecureElementStatus statusReturned;
206    LogicalChannelResponse response;
207    se_->openLogicalChannel(
208        aid, 0x00,
209        [&statusReturned, &response](LogicalChannelResponse channelResponse,
210                                     SecureElementStatus status) {
211            statusReturned = status;
212            if (status == SecureElementStatus::SUCCESS) {
213                response.channelNumber = channelResponse.channelNumber;
214                response.selectResponse.resize(channelResponse.selectResponse.size());
215                for (size_t i = 0; i < channelResponse.selectResponse.size(); i++) {
216                    response.selectResponse[i] = channelResponse.selectResponse[i];
217                }
218            }
219        });
220    EXPECT_EQ(SecureElementStatus::SUCCESS, statusReturned);
221    EXPECT_LE((unsigned int)2, response.selectResponse.size());
222    EXPECT_LE(1, response.channelNumber);
223    EXPECT_EQ(SecureElementStatus::SUCCESS, se_->closeChannel(response.channelNumber));
224}
225
226int main(int argc, char** argv) {
227    ::testing::AddGlobalTestEnvironment(SecureElementHidlEnvironment::Instance());
228    ::testing::InitGoogleTest(&argc, argv);
229    SecureElementHidlEnvironment::Instance()->init(&argc, argv);
230    int status = RUN_ALL_TESTS();
231    return status;
232}
233