1/*
2 * Copyright (c) 2024 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 *     http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16#include "common_utils.h"
17#include "nfc_chip_type_parser.h"
18#include "se_impl.h"
19#include "v1_0/infc_interface.h"
20#include "v1_0/isecure_element_callback.h"
21#include "v1_0/isecure_element_interface.h"
22#include "v1_0/nfc_types.h"
23#include "v1_0/secure_element_types.h"
24#include <gtest/gtest.h>
25#include <hdf_base.h>
26#include <hdf_device_desc.h>
27#include <hdf_log.h>
28#include <hdf_sbuf_ipc.h>
29#include <vector>
30
31using namespace OHOS::HDI::SecureElement::V1_0;
32using namespace testing::ext;
33using namespace std;
34using namespace OHOS::HDI::Nfc;
35using ISeHdiV1_0 = OHOS::HDI::SecureElement::V1_0::ISecureElementInterface;
36using OHOS::HDI::SecureElement::V1_0::ISecureElementCallback;
37using OHOS::HDI::SecureElement::V1_0::SecureElementStatus;
38using namespace OHOS::NFC;
39using namespace OHOS::SE;
40std::vector<uint8_t> vecCommand = {128, 202, 159, 127, 0};
41std::recursive_mutex mLock_{};
42
43class SeClientCallback : public ISecureElementCallback {
44public:
45    explicit SeClientCallback() {}
46
47    int32_t OnSeStateChanged(bool connected) override { return HDF_SUCCESS; }
48};
49
50OHOS::sptr<ISeHdiV1_0> mSeHdi_ = nullptr;
51OHOS::sptr<ISecureElementCallback> mSeHdiCallback = nullptr;
52
53class HdfNfcHdiTestAdditional : public testing::Test {
54public:
55    static void SetUpTestCase();
56    static void TearDownTestCase();
57    void SetUp();
58    void TearDown();
59};
60void HdfNfcHdiTestAdditional::SetUpTestCase()
61{
62    mSeHdi_ = ISeHdiV1_0::Get();
63    mSeHdiCallback = new SeClientCallback();
64}
65void HdfNfcHdiTestAdditional::TearDownTestCase() {}
66void HdfNfcHdiTestAdditional::SetUp() {}
67void HdfNfcHdiTestAdditional::TearDown() {}
68
69/**
70 * @tc.number : SUB_Communication_Secureelement_GetAtr_0200
71 * @tc.name   : testgetAtr001
72 * @tc.desc   : Enables the nfc controller and initialize the nfc core.Cycle 10 times.
73 */
74HWTEST_F(HdfNfcHdiTestAdditional, testgetAtr001, TestSize.Level1)
75{
76    if (!NfcChipTypeParser::IsSupportedChipType()) {
77        printf("SeTerminal SUB_DriverSystem_secureElement_0200 1 ");
78        EXPECT_EQ(HDF_SUCCESS, 0);
79    } else {
80        if (mSeHdi_ == nullptr) {
81            printf("SeTerminal SUB_DriverSystem_secureElement_0200 2");
82            ASSERT_NE(nullptr, mSeHdi_);
83            return;
84        }
85        std::vector<uint8_t> vecResponse(255, 0);
86        int32_t ret;
87        for (int i = 0; i < 10; i++) {
88            ret = mSeHdi_->getAtr(vecResponse);
89            printf("SeTerminal SUB_DriverSystem_secureElement_0200 =  %d", ret);
90            EXPECT_EQ(0, ret);
91        }
92    }
93}
94
95/**
96 * @tc.number : SUB_Communication_Secureelement_OpenLogicalChannel_0200
97 * @tc.name   : testOpenLogicalChannel001
98 * @tc.desc   : check openLogicalChannel,vecAid = 0
99 */
100HWTEST_F(HdfNfcHdiTestAdditional, testOpenLogicalChannel001, TestSize.Level1)
101{
102    if (!NfcChipTypeParser::IsSupportedChipType()) {
103        EXPECT_EQ(HDF_SUCCESS, 0);
104    } else {
105        if (mSeHdi_ == nullptr) {
106            ASSERT_NE(nullptr, mSeHdi_);
107            return;
108        }
109        std::lock_guard<std::recursive_mutex> lock(mLock_);
110        SecureElementStatus status = SecureElementStatus::SE_GENERAL_ERROR;
111        uint8_t channelNumber = 0;
112        std::vector<uint8_t> vecAid;
113        vecAid.push_back(0);
114        int p2 = 0x00;
115        static const std::string aid = "A000000151000000";
116        std::vector<uint8_t> vecResponse(255, 0);
117        CommonUtils::HexStringToBytesArray(aid, vecAid);
118        int32_t ret = mSeHdi_->openLogicalChannel(vecAid, p2, vecResponse, channelNumber, status);
119        EXPECT_EQ(0, ret);
120    }
121}
122
123/**
124 * @tc.number : SUB_Communication_Secureelement_OpenLogicalChannel_0300
125 * @tc.name   : testOpenLogicalChannel002
126 * @tc.desc   : check openLogicalChannel,vecAid = 1
127 */
128HWTEST_F(HdfNfcHdiTestAdditional, testOpenLogicalChannel002, TestSize.Level1)
129{
130    if (!NfcChipTypeParser::IsSupportedChipType()) {
131        EXPECT_EQ(HDF_SUCCESS, 0);
132    } else {
133        if (mSeHdi_ == nullptr) {
134            ASSERT_NE(nullptr, mSeHdi_);
135            return;
136        }
137        std::lock_guard<std::recursive_mutex> lock(mLock_);
138        SecureElementStatus status = SecureElementStatus::SE_GENERAL_ERROR;
139        uint8_t channelNumber = 0;
140        std::vector<uint8_t> vecAid;
141        vecAid.push_back(1);
142        int p2 = 0x00;
143        static const std::string aid = "A000000151000000";
144        std::vector<uint8_t> vecResponse(255, 0);
145        CommonUtils::HexStringToBytesArray(aid, vecAid);
146        int32_t ret = mSeHdi_->openLogicalChannel(vecAid, p2, vecResponse, channelNumber, status);
147        EXPECT_EQ(0, ret);
148    }
149}
150
151/**
152 * @tc.number : SUB_Communication_Secureelement_OpenLogicalChannel_0400
153 * @tc.name   : testOpenLogicalChannel003
154 * @tc.desc   : check openLogicalChannel,vecAid = 0,cycle 10 times.
155 */
156HWTEST_F(HdfNfcHdiTestAdditional, testOpenLogicalChannel003, TestSize.Level1)
157{
158    if (!NfcChipTypeParser::IsSupportedChipType()) {
159        EXPECT_EQ(HDF_SUCCESS, 0);
160    } else {
161        if (mSeHdi_ == nullptr) {
162            ASSERT_NE(nullptr, mSeHdi_);
163            return;
164        }
165        std::lock_guard<std::recursive_mutex> lock(mLock_);
166        SecureElementStatus status = SecureElementStatus::SE_GENERAL_ERROR;
167        uint8_t channelNumber = 0;
168        std::vector<uint8_t> vecAid;
169        vecAid.push_back(0);
170        int p2 = 0x00;
171        static const std::string aid = "A000000151000000";
172        std::vector<uint8_t> vecResponse(255, 0);
173        CommonUtils::HexStringToBytesArray(aid, vecAid);
174        int32_t ret;
175        for (int i = 0; i < 10; i++) {
176            ret = mSeHdi_->openLogicalChannel(vecAid, p2, vecResponse, channelNumber, status);
177            EXPECT_EQ(0, ret);
178        }
179    }
180}
181
182/**
183 * @tc.number : SUB_Communication_Secureelement_OpenLogicalChannel_0300
184 * @tc.name   : testOpenLogicalChannel004
185 * @tc.desc   : check openLogicalChannel,vecAid = 1,cycle 10 times.
186 */
187HWTEST_F(HdfNfcHdiTestAdditional, testOpenLogicalChannel004, TestSize.Level1)
188{
189    if (!NfcChipTypeParser::IsSupportedChipType()) {
190        EXPECT_EQ(HDF_SUCCESS, 0);
191    } else {
192        if (mSeHdi_ == nullptr) {
193            ASSERT_NE(nullptr, mSeHdi_);
194            return;
195        }
196        std::lock_guard<std::recursive_mutex> lock(mLock_);
197        SecureElementStatus status = SecureElementStatus::SE_GENERAL_ERROR;
198        uint8_t channelNumber = 0;
199        std::vector<uint8_t> vecAid;
200        vecAid.push_back(1);
201        int p2 = 0x00;
202        static const std::string aid = "A000000151000000";
203        std::vector<uint8_t> vecResponse(255, 0);
204        CommonUtils::HexStringToBytesArray(aid, vecAid);
205        int32_t ret;
206        for (int i = 0; i < 10; i++) {
207            ret = mSeHdi_->openLogicalChannel(vecAid, p2, vecResponse, channelNumber, status);
208            EXPECT_EQ(0, ret);
209        }
210    }
211}
212
213/**
214 * @tc.number : SUB_Communication_Secureelement_OpenBasicChannel_0200
215 * @tc.name   : testOpenBasicChannel001
216 * @tc.desc   : check openBasicChannel,vecAid = 0
217 */
218HWTEST_F(HdfNfcHdiTestAdditional, testOpenBasicChannel001, TestSize.Level1)
219{
220    if (!NfcChipTypeParser::IsSupportedChipType()) {
221        EXPECT_EQ(HDF_SUCCESS, 0);
222    } else {
223        if (mSeHdi_ == nullptr) {
224            ASSERT_NE(nullptr, mSeHdi_);
225            return;
226        }
227        std::lock_guard<std::recursive_mutex> lock(mLock_);
228        int p2 = 0x00;
229        static const std::string aid = "A000000151000000";
230        std::vector<uint8_t> vecAid;
231        vecAid.push_back(0);
232        SecureElementStatus status = SecureElementStatus::SE_GENERAL_ERROR;
233        std::vector<uint8_t> vecResponse(255, 0);
234        CommonUtils::HexStringToBytesArray(aid, vecAid);
235        int32_t ret = mSeHdi_->openBasicChannel(vecAid, p2, vecResponse, status);
236        EXPECT_EQ(0, ret);
237    }
238}
239
240/**
241 * @tc.number : SUB_Communication_Secureelement_OpenBasicChannel_0300
242 * @tc.name   : testOpenBasicChannel002
243 * @tc.desc   : check openBasicChannel,vecAid = 1
244 */
245HWTEST_F(HdfNfcHdiTestAdditional, testOpenBasicChannel002, TestSize.Level1)
246{
247    if (!NfcChipTypeParser::IsSupportedChipType()) {
248        EXPECT_EQ(HDF_SUCCESS, 0);
249    } else {
250        if (mSeHdi_ == nullptr) {
251            ASSERT_NE(nullptr, mSeHdi_);
252            return;
253        }
254        std::lock_guard<std::recursive_mutex> lock(mLock_);
255        int p2 = 0x00;
256        static const std::string aid = "A000000151000000";
257        std::vector<uint8_t> vecAid;
258        vecAid.push_back(1);
259        SecureElementStatus status = SecureElementStatus::SE_GENERAL_ERROR;
260        std::vector<uint8_t> vecResponse(255, 0);
261        CommonUtils::HexStringToBytesArray(aid, vecAid);
262        int32_t ret = mSeHdi_->openBasicChannel(vecAid, p2, vecResponse, status);
263        EXPECT_EQ(0, ret);
264    }
265}
266
267/**
268 * @tc.number : SUB_Communication_Secureelement_OpenBasicChannel_0400
269 * @tc.name   : testOpenBasicChannel003
270 * @tc.desc   : check openBasicChannel,vecAid = 0,cycle 10 times
271 */
272HWTEST_F(HdfNfcHdiTestAdditional, testOpenBasicChannel003, TestSize.Level1)
273{
274    if (!NfcChipTypeParser::IsSupportedChipType()) {
275        EXPECT_EQ(HDF_SUCCESS, 0);
276    } else {
277        if (mSeHdi_ == nullptr) {
278            ASSERT_NE(nullptr, mSeHdi_);
279            return;
280        }
281        std::lock_guard<std::recursive_mutex> lock(mLock_);
282        int p2 = 0x00;
283        static const std::string aid = "A000000151000000";
284        std::vector<uint8_t> vecAid;
285        vecAid.push_back(0);
286        SecureElementStatus status = SecureElementStatus::SE_GENERAL_ERROR;
287        std::vector<uint8_t> vecResponse(255, 0);
288        CommonUtils::HexStringToBytesArray(aid, vecAid);
289        int32_t ret;
290        for (int i = 0; i < 10; i++) {
291            ret = mSeHdi_->openBasicChannel(vecAid, p2, vecResponse, status);
292            EXPECT_EQ(0, ret);
293        }
294    }
295}
296
297/**
298 * @tc.number : SUB_Communication_Secureelement_OpenBasicChannel_0500
299 * @tc.name   : testOpenBasicChannel004
300 * @tc.desc   : check openBasicChannel,vecAid = 1,cycle 10 times
301 */
302HWTEST_F(HdfNfcHdiTestAdditional, testOpenBasicChannel004, TestSize.Level1)
303{
304    if (!NfcChipTypeParser::IsSupportedChipType()) {
305        EXPECT_EQ(HDF_SUCCESS, 0);
306    } else {
307        if (mSeHdi_ == nullptr) {
308            ASSERT_NE(nullptr, mSeHdi_);
309            return;
310        }
311        std::lock_guard<std::recursive_mutex> lock(mLock_);
312        int p2 = 0x00;
313        static const std::string aid = "A000000151000000";
314        std::vector<uint8_t> vecAid;
315        vecAid.push_back(1);
316        SecureElementStatus status = SecureElementStatus::SE_GENERAL_ERROR;
317        std::vector<uint8_t> vecResponse(255, 0);
318        CommonUtils::HexStringToBytesArray(aid, vecAid);
319        int32_t ret;
320        for (int i = 0; i < 10; i++) {
321            ret = mSeHdi_->openBasicChannel(vecAid, p2, vecResponse, status);
322            EXPECT_EQ(0, ret);
323        }
324    }
325}
326
327/**
328 * @tc.number : SUB_Communication_Secureelement_IsSecureElementPresent_0200
329 * @tc.name   : testIsSecureElementPresent001
330 * @tc.desc   : Specifically configures the nfc chip before starting RF discovering.
331 */
332HWTEST_F(HdfNfcHdiTestAdditional, testIsSecureElementPresent001, TestSize.Level1)
333{
334    if (!NfcChipTypeParser::IsSupportedChipType()) {
335        printf("SeTerminal SUB_DriverSystem_secureElement_0300 1 ");
336        EXPECT_EQ(HDF_SUCCESS, 0);
337    } else {
338        if (mSeHdi_ == nullptr) {
339            printf("SeTerminal SUB_DriverSystem_secureElement_0300 2 ");
340            ASSERT_NE(nullptr, mSeHdi_);
341            return;
342        }
343        bool present = false;
344        int32_t ret;
345        for (int i = 0; i < 10; i++) {
346            ret = mSeHdi_->isSecureElementPresent(present);
347            printf("SeTerminal SUB_DriverSystem_secureElement_0300 =  %d", ret);
348            EXPECT_EQ(0, ret);
349        }
350    }
351}
352
353/**
354 * @tc.number : SUB_Communication_Secureelement_CloseChannel_0200
355 * @tc.name   : testCloseChannel001
356 * @tc.desc   : check closeChannel,channelNumber = 1
357 */
358HWTEST_F(HdfNfcHdiTestAdditional, testCloseChannel001, TestSize.Level1)
359{
360    if (!NfcChipTypeParser::IsSupportedChipType()) {
361        EXPECT_EQ(HDF_SUCCESS, 0);
362    } else {
363        if (mSeHdi_ == nullptr) {
364            ASSERT_NE(nullptr, mSeHdi_);
365            return;
366        }
367        uint8_t channelNumber = 1;
368        SecureElementStatus status = SecureElementStatus::SE_GENERAL_ERROR;
369        int32_t ret = mSeHdi_->closeChannel(channelNumber, status);
370        EXPECT_EQ(0, ret);
371    }
372}
373
374/**
375 * @tc.number : SUB_Communication_Secureelement_CloseChannel_0300
376 * @tc.name   : testCloseChannel002
377 * @tc.desc   : check closeChannel,channelNumber = 0,cycle 10 times
378 */
379HWTEST_F(HdfNfcHdiTestAdditional, testCloseChannel002, TestSize.Level1)
380{
381    if (!NfcChipTypeParser::IsSupportedChipType()) {
382        EXPECT_EQ(HDF_SUCCESS, 0);
383    } else {
384        if (mSeHdi_ == nullptr) {
385            ASSERT_NE(nullptr, mSeHdi_);
386            return;
387        }
388        uint8_t channelNumber = 0;
389        SecureElementStatus status = SecureElementStatus::SE_GENERAL_ERROR;
390        int32_t ret;
391        for (int i = 0; i < 10; i++) {
392            ret = mSeHdi_->closeChannel(channelNumber, status);
393            EXPECT_EQ(0, ret);
394        }
395    }
396}
397
398/**
399 * @tc.number : SUB_Communication_Secureelement_CloseChannel_0400
400 * @tc.name   : testCloseChannel003
401 * @tc.desc   : check closeChannel,channelNumber = 1,cycle 10 times
402 */
403HWTEST_F(HdfNfcHdiTestAdditional, testCloseChannel003, TestSize.Level1)
404{
405    if (!NfcChipTypeParser::IsSupportedChipType()) {
406        EXPECT_EQ(HDF_SUCCESS, 0);
407    } else {
408        if (mSeHdi_ == nullptr) {
409            ASSERT_NE(nullptr, mSeHdi_);
410            return;
411        }
412        uint8_t channelNumber = 1;
413        SecureElementStatus status = SecureElementStatus::SE_GENERAL_ERROR;
414        int32_t ret;
415        for (int i = 0; i < 10; i++) {
416            ret = mSeHdi_->closeChannel(channelNumber, status);
417            EXPECT_EQ(0, ret);
418        }
419    }
420}
421
422/**
423 * @tc.number : SUB_Communication_Secureelement_Transmit_0200
424 * @tc.name   : testTransmit001
425 * @tc.desc   : check transmit,vecCommand = 0
426 */
427HWTEST_F(HdfNfcHdiTestAdditional, testTransmit001, TestSize.Level1)
428{
429    if (!NfcChipTypeParser::IsSupportedChipType()) {
430        EXPECT_EQ(HDF_SUCCESS, 0);
431    } else {
432        if (mSeHdi_ == nullptr) {
433            ASSERT_NE(nullptr, mSeHdi_);
434            return;
435        }
436        static const std::string aid = "A000000151000000";
437        std::vector<uint8_t> vecCommand;
438        vecCommand.push_back(0);
439        std::vector<uint8_t> vecResponse(255, 0);
440        CommonUtils::HexStringToBytesArray(aid, vecCommand);
441        SecureElementStatus status = SecureElementStatus::SE_GENERAL_ERROR;
442        int32_t ret = mSeHdi_->transmit(vecCommand, vecResponse, status);
443        EXPECT_EQ(0, ret);
444    }
445}
446
447/**
448 * @tc.number : SUB_Communication_Secureelement_Transmit_0300
449 * @tc.name   : testTransmit002
450 * @tc.desc   : check transmit,vecCommand = 1
451 */
452HWTEST_F(HdfNfcHdiTestAdditional, testTransmit002, TestSize.Level1)
453{
454    if (!NfcChipTypeParser::IsSupportedChipType()) {
455        EXPECT_EQ(HDF_SUCCESS, 0);
456    } else {
457        if (mSeHdi_ == nullptr) {
458            ASSERT_NE(nullptr, mSeHdi_);
459            return;
460        }
461        static const std::string aid = "A000000151000000";
462        std::vector<uint8_t> vecCommand;
463        vecCommand.push_back(1);
464        std::vector<uint8_t> vecResponse(255, 0);
465        CommonUtils::HexStringToBytesArray(aid, vecCommand);
466        SecureElementStatus status = SecureElementStatus::SE_GENERAL_ERROR;
467        int32_t ret = mSeHdi_->transmit(vecCommand, vecResponse, status);
468        EXPECT_EQ(0, ret);
469    }
470}
471
472/**
473 * @tc.number : SUB_Communication_Secureelement_Transmit_0400
474 * @tc.name   : testTransmit003
475 * @tc.desc   : check transmit,vecCommand = 0,cycle 10 times
476 */
477HWTEST_F(HdfNfcHdiTestAdditional, testTransmit003, TestSize.Level1)
478{
479    if (!NfcChipTypeParser::IsSupportedChipType()) {
480        EXPECT_EQ(HDF_SUCCESS, 0);
481    } else {
482        if (mSeHdi_ == nullptr) {
483            ASSERT_NE(nullptr, mSeHdi_);
484            return;
485        }
486        static const std::string aid = "A000000151000000";
487        std::vector<uint8_t> vecCommand;
488        vecCommand.push_back(0);
489        std::vector<uint8_t> vecResponse(255, 0);
490        CommonUtils::HexStringToBytesArray(aid, vecCommand);
491        SecureElementStatus status = SecureElementStatus::SE_GENERAL_ERROR;
492        int32_t ret;
493        for (int i = 0; i < 10; i++) {
494            ret = mSeHdi_->transmit(vecCommand, vecResponse, status);
495            EXPECT_EQ(0, ret);
496        }
497    }
498}
499
500/**
501 * @tc.number : SUB_Communication_Secureelement_Transmit_0500
502 * @tc.name   : testTransmit004
503 * @tc.desc   : check transmit,vecCommand = 0,cycle 10 times
504 */
505HWTEST_F(HdfNfcHdiTestAdditional, testTransmit004, TestSize.Level1)
506{
507    if (!NfcChipTypeParser::IsSupportedChipType()) {
508        EXPECT_EQ(HDF_SUCCESS, 0);
509    } else {
510        if (mSeHdi_ == nullptr) {
511            ASSERT_NE(nullptr, mSeHdi_);
512            return;
513        }
514        static const std::string aid = "A000000151000000";
515        std::vector<uint8_t> vecCommand;
516        vecCommand.push_back(1);
517        std::vector<uint8_t> vecResponse(255, 0);
518        CommonUtils::HexStringToBytesArray(aid, vecCommand);
519        SecureElementStatus status = SecureElementStatus::SE_GENERAL_ERROR;
520        int32_t ret;
521        for (int i = 0; i < 10; i++) {
522            ret = mSeHdi_->transmit(vecCommand, vecResponse, status);
523            EXPECT_EQ(0, ret);
524        }
525    }
526}
527
528/**
529 * @tc.number : SUB_Communication_Secureelement_Reset_0200
530 * @tc.name   : testReset001
531 * @tc.desc   : Disables the nfc controller and releases the resource,cycle 10 times
532 */
533HWTEST_F(HdfNfcHdiTestAdditional, testReset001, TestSize.Level1)
534{
535    if (!NfcChipTypeParser::IsSupportedChipType()) {
536        EXPECT_EQ(HDF_SUCCESS, 0);
537    } else {
538        if (mSeHdi_ == nullptr) {
539            ASSERT_NE(nullptr, mSeHdi_);
540            return;
541        }
542        SecureElementStatus status = SecureElementStatus::SE_GENERAL_ERROR;
543        int32_t ret;
544        for (int i = 0; i < 10; i++) {
545            ret = mSeHdi_->reset(status);
546            EXPECT_EQ(0, ret);
547        }
548    }
549}