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 
25 namespace OHOS {
26 namespace HDI {
27 namespace SecureElement {
28 namespace TEST {
29 using namespace testing;
30 using namespace testing::ext;
31 
32 class SeImplTest : public testing::Test {
33 public:
34     static void SetUpTestCase();
35     static void TearDownTestCase();
36     void SetUp();
37     void TearDown();
38 };
39 
SetUpTestCase()40 void SeImplTest::SetUpTestCase()
41 {
42     HDF_LOGD("SetUpTestCase SeImplTest");
43 }
44 
TearDownTestCase()45 void SeImplTest::TearDownTestCase()
46 {
47     HDF_LOGD("TearDownTestCase SeImplTest");
48 }
49 
SetUp()50 void SeImplTest::SetUp()
51 {
52     HDF_LOGD("SetUp SeImplTest");
53 }
54 
TearDown()55 void SeImplTest::TearDown()
56 {
57     HDF_LOGD("TearDown SeImplTest");
58 }
59 
60 /**
61  * @tc.name: init001
62  * @tc.desc: Test SeImplTest init.
63  * @tc.type: FUNC
64  */
65 HWTEST_F(SeImplTest, init001, TestSize.Level1)
66 {
67     const sptr<ISecureElementCallback> clientCallback = nullptr;
68     SecureElementStatus status;
69     std::shared_ptr<SeImpl> seImpl = std::make_shared<SeImpl>();
70     int ret = seImpl->init(clientCallback, status);
71     ASSERT_TRUE(ret == HDF_ERR_INVALID_PARAM);
72 }
73 
74 /**
75  * @tc.name: getAtr001
76  * @tc.desc: Test SeImplTest getAtr.
77  * @tc.type: FUNC
78  */
79 HWTEST_F(SeImplTest, getAtr001, TestSize.Level1)
80 {
81     std::vector<uint8_t> response;
82     std::shared_ptr<SeImpl> seImpl = std::make_shared<SeImpl>();
83     int ret = seImpl->getAtr(response);
84     ASSERT_TRUE(ret == HDF_SUCCESS);
85 }
86 
87 /**
88  * @tc.name: isSecureElementPresent001
89  * @tc.desc: Test SeImplTest isSecureElementPresent.
90  * @tc.type: FUNC
91  */
92 HWTEST_F(SeImplTest, isSecureElementPresent001, TestSize.Level1)
93 {
94     bool present = false;
95     std::shared_ptr<SeImpl> seImpl = std::make_shared<SeImpl>();
96     int ret = seImpl->isSecureElementPresent(present);
97     ASSERT_TRUE(ret == HDF_SUCCESS);
98 }
99 
100 /**
101  * @tc.name: openLogicalChannel001
102  * @tc.desc: Test SeImplTest openLogicalChannel.
103  * @tc.type: FUNC
104  */
105 HWTEST_F(SeImplTest, openLogicalChannel001, TestSize.Level1)
106 {
107     std::vector<uint8_t> aid;
108     uint8_t p2 = 0;
109     std::vector<uint8_t> response;
110     uint8_t channelNumber = 0;
111     SecureElementStatus status;
112     std::shared_ptr<SeImpl> seImpl = std::make_shared<SeImpl>();
113     int ret = seImpl->openLogicalChannel(aid, p2, response, channelNumber, status);
114     ASSERT_TRUE(ret == HDF_ERR_INVALID_PARAM);
115 }
116 
117 /**
118  * @tc.name: openLogicalChannel002
119  * @tc.desc: Test SeImplTest openLogicalChannel.
120  * @tc.type: FUNC
121  */
122 HWTEST_F(SeImplTest, openLogicalChannel002, TestSize.Level1)
123 {
124     std::vector<uint8_t> aid = {0x001, 0x002, 0x003};
125     uint8_t p2 = 0;
126     std::vector<uint8_t> response;
127     uint8_t channelNumber = 0;
128     SecureElementStatus status;
129     std::shared_ptr<SeImpl> seImpl = std::make_shared<SeImpl>();
130     int ret = seImpl->openLogicalChannel(aid, p2, response, channelNumber, status);
131     ASSERT_TRUE(ret == HDF_SUCCESS);
132 }
133 
134 /**
135  * @tc.name: openBasicChannel001
136  * @tc.desc: Test SeImplTest openBasicChannel.
137  * @tc.type: FUNC
138  */
139 HWTEST_F(SeImplTest, openBasicChannel001, TestSize.Level1)
140 {
141     const std::vector<uint8_t> aid;
142     uint8_t p2 = 0;
143     std::vector<uint8_t> response;
144     SecureElementStatus status;
145     std::shared_ptr<SeImpl> seImpl = std::make_shared<SeImpl>();
146     int ret = seImpl->openBasicChannel(aid, p2, response, status);
147     ASSERT_TRUE(ret == HDF_ERR_INVALID_PARAM);
148 }
149 
150 /**
151  * @tc.name: openBasicChannel002
152  * @tc.desc: Test SeImplTest openBasicChannel.
153  * @tc.type: FUNC
154  */
155 HWTEST_F(SeImplTest, openBasicChannel002, TestSize.Level1)
156 {
157     const std::vector<uint8_t> aid = {0x001, 0x002, 0x003};
158     uint8_t p2 = 0;
159     std::vector<uint8_t> response;
160     SecureElementStatus status;
161     std::shared_ptr<SeImpl> seImpl = std::make_shared<SeImpl>();
162     int ret = seImpl->openBasicChannel(aid, p2, response, status);
163     ASSERT_TRUE(ret == HDF_SUCCESS);
164 }
165 
166 /**
167  * @tc.name: closeChannel001
168  * @tc.desc: Test SeImplTest closeChannel.
169  * @tc.type: FUNC
170  */
171 HWTEST_F(SeImplTest, closeChannel001, TestSize.Level1)
172 {
173     uint8_t channelNumber = 0;
174     SecureElementStatus status;
175     std::shared_ptr<SeImpl> seImpl = std::make_shared<SeImpl>();
176     int ret = seImpl->closeChannel(channelNumber, status);
177     ASSERT_TRUE(ret == HDF_SUCCESS);
178 }
179 
180 /**
181  * @tc.name: transmit001
182  * @tc.desc: Test SeImplTest transmit.
183  * @tc.type: FUNC
184  */
185 HWTEST_F(SeImplTest, transmit001, TestSize.Level1)
186 {
187     const std::vector<uint8_t> command;
188     std::vector<uint8_t> response;
189     SecureElementStatus status;
190     std::shared_ptr<SeImpl> seImpl = std::make_shared<SeImpl>();
191     int ret = seImpl->transmit(command, response, status);
192     ASSERT_TRUE(ret == HDF_SUCCESS);
193 }
194 
195 /**
196  * @tc.name: reset001
197  * @tc.desc: Test SeImplTest reset.
198  * @tc.type: FUNC
199  */
200 HWTEST_F(SeImplTest, reset001, TestSize.Level1)
201 {
202     SecureElementStatus status;
203     std::shared_ptr<SeImpl> seImpl = std::make_shared<SeImpl>();
204     int ret = seImpl->reset(status);
205     ASSERT_TRUE(ret == HDF_SUCCESS);
206 }
207 }
208 }
209 }
210 }