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 #include <gtest/gtest.h>
16 #include <thread>
17 #include <string>
18 #include <vector>
19 #include <securec.h>
20 #include <sstream>
21 #include <hdf_log.h>
22 
23 #include "se_impl.h"
24 #include "se_vendor_adaptions.h"
25 #include "mock.h"
26 
27 namespace OHOS {
28 namespace HDI {
29 namespace SecureElement {
30 namespace TEST {
31 using namespace testing;
32 using namespace testing::ext;
33 
34 class SeVendorAdaptionsTest : public testing::Test {
35 public:
36     static void SetUpTestCase();
37     static void TearDownTestCase();
38     void SetUp();
39     void TearDown();
40     MockTee mockTee_;
41 };
42 
SetUpTestCase()43 void SeVendorAdaptionsTest::SetUpTestCase()
44 {
45     HDF_LOGD("SetUpTestCase SeVendorAdaptionsTest");
46 }
47 
TearDownTestCase()48 void SeVendorAdaptionsTest::TearDownTestCase()
49 {
50     HDF_LOGD("TearDownTestCase SeVendorAdaptionsTest");
51 }
52 
SetUp()53 void SeVendorAdaptionsTest::SetUp()
54 {
55     MockTee::SetMockTee(mockTee_);
56     HDF_LOGD("SetUp SeVendorAdaptionsTest");
57 }
58 
TearDown()59 void SeVendorAdaptionsTest::TearDown()
60 {
61     MockTee::ResetMockTee();
62     HDF_LOGD("TearDown SeVendorAdaptionsTest");
63 }
64 
65 /**
66  * @tc.name: getAtr001
67  * @tc.desc: Test SeVendorAdaptionsTest getAtr.
68  * @tc.type: FUNC
69  */
70 HWTEST_F(SeVendorAdaptionsTest, getAtr001, TestSize.Level1)
71 {
72     std::vector<uint8_t> response = {0x01, 0x02, 0x03};
73     EXPECT_CALL(mockTee_, VendorSecureElementCaGetAtr(_, _))
74         .WillOnce(Return(0));
75     std::shared_ptr<SeVendorAdaptions> seVendorAdaptions = std::make_shared<SeVendorAdaptions>();
76     int ret = seVendorAdaptions->getAtr(response);
77     ASSERT_TRUE(ret == HDF_SUCCESS);
78 }
79 
80 /**
81  * @tc.name: openLogicalChannel002
82  * @tc.desc: Test SeVendorAdaptionsTest openLogicalChannel.
83  * @tc.type: FUNC
84  */
85 HWTEST_F(SeVendorAdaptionsTest, openLogicalChannel002, TestSize.Level1)
86 {
87     std::vector<uint8_t> aid = {0x001, 0x002, 0x003};
88     uint8_t p2 = 0;
89     std::vector<uint8_t> response;
90     uint8_t channelNumber = 0;
91     SecureElementStatus status;
92     EXPECT_CALL(mockTee_, VendorSecureElementCaOpenLogicalChannel(_, _, _, _, _, _))
93         .WillOnce(Return(0));
94     std::shared_ptr<SeVendorAdaptions> seVendorAdaptions = std::make_shared<SeVendorAdaptions>();
95     int ret = seVendorAdaptions->openLogicalChannel(aid, p2, response, channelNumber, status);
96     ASSERT_TRUE(ret == HDF_SUCCESS);
97 }
98 
99 /**
100  * @tc.name: openBasicChannel002
101  * @tc.desc: Test SeVendorAdaptionsTest openBasicChannel.
102  * @tc.type: FUNC
103  */
104 HWTEST_F(SeVendorAdaptionsTest, openBasicChannel002, TestSize.Level1)
105 {
106     EXPECT_CALL(mockTee_, VendorSecureElementCaOpenBasicChannel(_, _, _, _))
107         .WillOnce(Return(0));
108     const std::vector<uint8_t> aid = {0x001, 0x002, 0x003};
109     uint8_t p2 = 0;
110     std::vector<uint8_t> response;
111     SecureElementStatus status;
112     std::shared_ptr<SeVendorAdaptions> seVendorAdaptions = std::make_shared<SeVendorAdaptions>();
113     int ret = seVendorAdaptions->openBasicChannel(aid, p2, response, status);
114     ASSERT_TRUE(ret == HDF_SUCCESS);
115 }
116 
117 /**
118  * @tc.name: closeChannel001
119  * @tc.desc: Test SeVendorAdaptionsTest closeChannel.
120  * @tc.type: FUNC
121  */
122 HWTEST_F(SeVendorAdaptionsTest, closeChannel001, TestSize.Level1)
123 {
124     EXPECT_CALL(mockTee_, VendorSecureElementCaCloseChannel(_))
125         .WillOnce(Return(0));
126     EXPECT_CALL(mockTee_, VendorSecureElementCaUninit())
127         .WillOnce(Return(0));
128     uint8_t channelNumber = 0;
129     SecureElementStatus status;
130     std::shared_ptr<SeVendorAdaptions> seVendorAdaptions = std::make_shared<SeVendorAdaptions>();
131     int ret = seVendorAdaptions->closeChannel(channelNumber, status);
132     ASSERT_TRUE(ret == HDF_SUCCESS);
133 }
134 
135 /**
136  * @tc.name: transmit001
137  * @tc.desc: Test SeVendorAdaptionsTest transmit.
138  * @tc.type: FUNC
139  */
140 HWTEST_F(SeVendorAdaptionsTest, transmit001, TestSize.Level1)
141 {
142     EXPECT_CALL(mockTee_, VendorSecureElementCaTransmit(_, _, _, _))
143         .WillOnce(Return(0));
144     const std::vector<uint8_t> command;
145     std::vector<uint8_t> response;
146     SecureElementStatus status;
147     std::shared_ptr<SeVendorAdaptions> seVendorAdaptions = std::make_shared<SeVendorAdaptions>();
148     int ret = seVendorAdaptions->transmit(command, response, status);
149     ASSERT_TRUE(ret == HDF_SUCCESS);
150 }
151 }
152 }
153 }
154 }