1 /*
2  * Copyright (c) 2023 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 
18 #include "hce_session.h"
19 
20 namespace OHOS {
21 namespace NFC {
22 namespace TEST {
23 using namespace testing::ext;
24 using namespace OHOS::NFC::KITS;
25 class HceSessionTest : public testing::Test {
26 public:
27     static void SetUpTestCase();
28     static void TearDownTestCase();
29     void SetUp();
30     void TearDown();
31 };
32 
SetUpTestCase()33 void HceSessionTest::SetUpTestCase()
34 {
35     std::cout << " SetUpTestCase HceSessionTest." << std::endl;
36 }
37 
TearDownTestCase()38 void HceSessionTest::TearDownTestCase()
39 {
40     std::cout << " TearDownTestCase HceSessionTest." << std::endl;
41 }
42 
SetUp()43 void HceSessionTest::SetUp()
44 {
45     std::cout << " SetUp HceSessionTest." << std::endl;
46 }
47 
TearDown()48 void HceSessionTest::TearDown()
49 {
50     std::cout << " TearDown HceSessionTest." << std::endl;
51 }
52 
53 /**
54  * @tc.name: RegHceCmdCallbackByToken001
55  * @tc.desc: Test HceSessionTest RegHceCmdCallbackByToken.
56  * @tc.type: FUNC
57  */
58 HWTEST_F(HceSessionTest, RegHceCmdCallbackByToken001, TestSize.Level1)
59 {
60     std::shared_ptr<OHOS::NFC::NfcService> nfcService = std::make_shared<OHOS::NFC::NfcService>();
61     sptr<KITS::IHceCmdCallback> callback = nullptr;
62     std::string type = "";
63     std::shared_ptr<HCE::HceSession> hceSession = std::make_shared<HCE::HceSession>(nfcService);
64     Security::AccessToken::AccessTokenID callerToken = 0;
65     KITS::ErrorCode regHceCmdCallback = hceSession->RegHceCmdCallbackByToken(callback, type, callerToken);
66     ASSERT_TRUE(regHceCmdCallback == NFC::KITS::ErrorCode::ERR_HCE_PARAMETERS);
67 }
68 
69 /**
70  * @tc.name: RegHceCmdCallbackByToken002
71  * @tc.desc: Test HceSessionTest RegHceCmdCallbackByToken.
72  * @tc.type: FUNC
73  */
74 HWTEST_F(HceSessionTest, RegHceCmdCallbackByToken002, TestSize.Level1)
75 {
76     std::shared_ptr<OHOS::NFC::NfcService> nfcService = std::make_shared<OHOS::NFC::NfcService>();
77     nfcService->Initialize();
78     sptr<KITS::IHceCmdCallback> callback = nullptr;
79     std::string type = "";
80     std::shared_ptr<HCE::HceSession> hceSession = std::make_shared<HCE::HceSession>(nfcService);
81     Security::AccessToken::AccessTokenID callerToken = 0;
82     KITS::ErrorCode regHceCmdCallback = hceSession->RegHceCmdCallbackByToken(callback, type, callerToken);
83     ASSERT_TRUE(regHceCmdCallback == NFC::KITS::ErrorCode::ERR_NFC_PARAMETERS);
84 }
85 
86 /**
87  * @tc.name: SendRawFrameByToken001
88  * @tc.desc: Test HceSessionTest SendRawFrameByToken.
89  * @tc.type: FUNC
90  */
91 HWTEST_F(HceSessionTest, SendRawFrameByToken001, TestSize.Level1)
92 {
93     std::shared_ptr<OHOS::NFC::NfcService> nfcService = std::make_shared<OHOS::NFC::NfcService>();
94     std::string hexCmdData = "";
95     bool raw = false;
96     std::string hexRespData = "";
97     std::shared_ptr<HCE::HceSession> hceSession = std::make_shared<HCE::HceSession>(nfcService);
98     Security::AccessToken::AccessTokenID callerToken = 0;
99     int sendRawFrame = hceSession->SendRawFrameByToken(hexCmdData, raw, hexRespData, callerToken);
100     ASSERT_TRUE(sendRawFrame == NFC::KITS::ErrorCode::ERR_HCE_PARAMETERS);
101 }
102 
103 /**
104  * @tc.name: SendRawFrameByToken002
105  * @tc.desc: Test HceSessionTest SendRawFrameByToken.
106  * @tc.type: FUNC
107  */
108 HWTEST_F(HceSessionTest, SendRawFrameByToken002, TestSize.Level1)
109 {
110     std::shared_ptr<OHOS::NFC::NfcService> nfcService = std::make_shared<OHOS::NFC::NfcService>();
111     nfcService->Initialize();
112     std::string hexCmdData = "";
113     bool raw = false;
114     std::string hexRespData = "";
115     std::shared_ptr<HCE::HceSession> hceSession = std::make_shared<HCE::HceSession>(nfcService);
116     Security::AccessToken::AccessTokenID callerToken = 0;
117     int sendRawFrame = hceSession->SendRawFrameByToken(hexCmdData, raw, hexRespData, callerToken);
118     ASSERT_TRUE(sendRawFrame == NFC::KITS::ErrorCode::ERR_HCE_STATE_IO_FAILED);
119 }
120 
121 /**
122  * @tc.name: Dump001
123  * @tc.desc: Test HceSessionTest Dump.
124  * @tc.type: FUNC
125  */
126 HWTEST_F(HceSessionTest, Dump001, TestSize.Level1)
127 {
128     std::shared_ptr<OHOS::NFC::NfcService> nfcService = std::make_shared<OHOS::NFC::NfcService>();
129     int32_t fd = 0;
130     std::vector<std::u16string> args;
131     std::shared_ptr<HCE::HceSession> hceSession = std::make_shared<HCE::HceSession>(nfcService);
132     int dump = hceSession->Dump(fd, args);
133     ASSERT_TRUE(dump == NFC::KITS::ErrorCode::ERR_HCE_PARAMETERS);
134 }
135 
136 /**
137  * @tc.name: UnRegHceCmdCallback001
138  * @tc.desc: Test HceSessionTest UnRegHceCmdCallback.
139  * @tc.type: FUNC
140  */
141 HWTEST_F(HceSessionTest, UnRegHceCmdCallback001, TestSize.Level1)
142 {
143     std::shared_ptr<OHOS::NFC::NfcService> nfcService = std::make_shared<OHOS::NFC::NfcService>();
144     const std::string type = "";
145     Security::AccessToken::AccessTokenID callerToken = 0;
146     std::shared_ptr<HCE::HceSession> hceSession = std::make_shared<HCE::HceSession>(nfcService);
147     KITS::ErrorCode errorCode = hceSession->UnRegHceCmdCallback(type, callerToken);
148     ASSERT_TRUE(errorCode == NFC::KITS::ErrorCode::ERR_HCE_PARAMETERS);
149 }
150 
151 /**
152  * @tc.name: UnRegHceCmdCallback002
153  * @tc.desc: Test HceSessionTest UnRegHceCmdCallback.
154  * @tc.type: FUNC
155  */
156 HWTEST_F(HceSessionTest, UnRegHceCmdCallback002, TestSize.Level1)
157 {
158     std::shared_ptr<OHOS::NFC::NfcService> nfcService = std::make_shared<OHOS::NFC::NfcService>();
159     nfcService->Initialize();
160     const std::string type = "";
161     Security::AccessToken::AccessTokenID callerToken = 0;
162     std::shared_ptr<HCE::HceSession> hceSession = std::make_shared<HCE::HceSession>(nfcService);
163     KITS::ErrorCode errorCode = hceSession->UnRegHceCmdCallback(type, callerToken);
164     ASSERT_TRUE(errorCode == NFC::KITS::ErrorCode::ERR_HCE_PARAMETERS);
165 }
166 
167 /**
168  * @tc.name: UnRegAllCallback001
169  * @tc.desc: Test HceSessionTest UnRegAllCallback.
170  * @tc.type: FUNC
171  */
172 HWTEST_F(HceSessionTest, UnRegAllCallback001, TestSize.Level1)
173 {
174     std::shared_ptr<OHOS::NFC::NfcService> nfcService = std::make_shared<OHOS::NFC::NfcService>();
175     Security::AccessToken::AccessTokenID callerToken = 0;
176     std::shared_ptr<HCE::HceSession> hceSession = std::make_shared<HCE::HceSession>(nfcService);
177     KITS::ErrorCode errorCode = hceSession->UnRegAllCallback(callerToken);
178     ASSERT_TRUE(errorCode == NFC::KITS::ErrorCode::ERR_HCE_PARAMETERS);
179 }
180 
181 /**
182  * @tc.name: UnRegAllCallback002
183  * @tc.desc: Test HceSessionTest UnRegAllCallback.
184  * @tc.type: FUNC
185  */
186 HWTEST_F(HceSessionTest, UnRegAllCallback002, TestSize.Level1)
187 {
188     std::shared_ptr<OHOS::NFC::NfcService> nfcService = std::make_shared<OHOS::NFC::NfcService>();
189     nfcService->Initialize();
190     Security::AccessToken::AccessTokenID callerToken = 0;
191     std::shared_ptr<HCE::HceSession> hceSession = std::make_shared<HCE::HceSession>(nfcService);
192     KITS::ErrorCode errorCode = hceSession->UnRegAllCallback(callerToken);
193     ASSERT_TRUE(errorCode == NFC::KITS::ErrorCode::ERR_HCE_PARAMETERS);
194 }
195 
196 /**
197  * @tc.name: IsDefaultService001
198  * @tc.desc: Test HceSessionTest IsDefaultService.
199  * @tc.type: FUNC
200  */
201 HWTEST_F(HceSessionTest, IsDefaultService001, TestSize.Level1)
202 {
203     std::shared_ptr<OHOS::NFC::NfcService> nfcService = std::make_shared<OHOS::NFC::NfcService>();
204     ElementName element;
205     const std::string type = "";
206     bool isDefaultService = false;
207     std::shared_ptr<HCE::HceSession> hceSession = std::make_shared<HCE::HceSession>(nfcService);
208     KITS::ErrorCode errorCode = hceSession->IsDefaultService(element, type, isDefaultService);
209     ASSERT_TRUE(errorCode == NFC::KITS::ErrorCode::ERR_HCE_PARAMETERS);
210 }
211 
212 /**
213  * @tc.name: IsDefaultService002
214  * @tc.desc: Test HceSessionTest IsDefaultService.
215  * @tc.type: FUNC
216  */
217 HWTEST_F(HceSessionTest, IsDefaultService002, TestSize.Level1)
218 {
219     std::shared_ptr<OHOS::NFC::NfcService> nfcService = std::make_shared<OHOS::NFC::NfcService>();
220     nfcService->Initialize();
221     ElementName element;
222     const std::string type = "";
223     bool isDefaultService = false;
224     std::shared_ptr<HCE::HceSession> hceSession = std::make_shared<HCE::HceSession>(nfcService);
225     KITS::ErrorCode errorCode = hceSession->IsDefaultService(element, type, isDefaultService);
226     ASSERT_TRUE(errorCode == NFC::KITS::ErrorCode::ERR_NONE);
227 }
228 
229 /**
230  * @tc.name: HandleWhenRemoteDie001
231  * @tc.desc: Test HceSessionTest HandleWhenRemoteDie.
232  * @tc.type: FUNC
233  */
234 HWTEST_F(HceSessionTest, HandleWhenRemoteDie001, TestSize.Level1)
235 {
236     std::shared_ptr<OHOS::NFC::NfcService> nfcService = std::make_shared<OHOS::NFC::NfcService>();
237     Security::AccessToken::AccessTokenID callerToken = 0;
238     std::shared_ptr<HCE::HceSession> hceSession = std::make_shared<HCE::HceSession>(nfcService);
239     KITS::ErrorCode errorCode = hceSession->HandleWhenRemoteDie(callerToken);
240     ASSERT_TRUE(errorCode == NFC::KITS::ErrorCode::ERR_HCE_PARAMETERS);
241 }
242 
243 /**
244  * @tc.name: HandleWhenRemoteDie002
245  * @tc.desc: Test HceSessionTest HandleWhenRemoteDie.
246  * @tc.type: FUNC
247  */
248 HWTEST_F(HceSessionTest, HandleWhenRemoteDie002, TestSize.Level1)
249 {
250     std::shared_ptr<OHOS::NFC::NfcService> nfcService = std::make_shared<OHOS::NFC::NfcService>();
251     nfcService->Initialize();
252     Security::AccessToken::AccessTokenID callerToken = 0;
253     std::shared_ptr<HCE::HceSession> hceSession = std::make_shared<HCE::HceSession>(nfcService);
254     KITS::ErrorCode errorCode = hceSession->HandleWhenRemoteDie(callerToken);
255     ASSERT_TRUE(errorCode == NFC::KITS::ErrorCode::ERR_HCE_PARAMETERS);
256 }
257 
258 /**
259  * @tc.name: StartHce001
260  * @tc.desc: Test HceSessionTest StartHce.
261  * @tc.type: FUNC
262  */
263 HWTEST_F(HceSessionTest, StartHce001, TestSize.Level1)
264 {
265     std::shared_ptr<OHOS::NFC::NfcService> nfcService = std::make_shared<OHOS::NFC::NfcService>();
266     ElementName element;
267     std::vector<std::string> aids;
268     std::shared_ptr<HCE::HceSession> hceSession = std::make_shared<HCE::HceSession>(nfcService);
269     KITS::ErrorCode errorCode = hceSession->StartHce(element, aids);
270     ASSERT_TRUE(errorCode == NFC::KITS::ErrorCode::ERR_HCE_PARAMETERS);
271 }
272 
273 /**
274  * @tc.name: StartHce002
275  * @tc.desc: Test HceSessionTest StartHce.
276  * @tc.type: FUNC
277  */
278 HWTEST_F(HceSessionTest, StartHce002, TestSize.Level1)
279 {
280     std::shared_ptr<OHOS::NFC::NfcService> nfcService = std::make_shared<OHOS::NFC::NfcService>();
281     nfcService->Initialize();
282     ElementName element;
283     std::vector<std::string> aids;
284     std::shared_ptr<HCE::HceSession> hceSession = std::make_shared<HCE::HceSession>(nfcService);
285     KITS::ErrorCode errorCode = hceSession->StartHce(element, aids);
286     ASSERT_TRUE(errorCode == NFC::KITS::ErrorCode::ERR_HCE_PARAMETERS);
287 }
288 
289 /**
290  * @tc.name: StopHce001
291  * @tc.desc: Test HceSessionTest StopHce.
292  * @tc.type: FUNC
293  */
294 HWTEST_F(HceSessionTest, StopHce001, TestSize.Level1)
295 {
296     std::shared_ptr<OHOS::NFC::NfcService> nfcService = std::make_shared<OHOS::NFC::NfcService>();
297     ElementName element;
298     Security::AccessToken::AccessTokenID callerToken = 0;
299     std::shared_ptr<HCE::HceSession> hceSession = std::make_shared<HCE::HceSession>(nfcService);
300     KITS::ErrorCode errorCode = hceSession->StopHce(element, callerToken);
301     ASSERT_TRUE(errorCode == NFC::KITS::ErrorCode::ERR_HCE_PARAMETERS);
302 }
303 
304 /**
305  * @tc.name: StopHce002
306  * @tc.desc: Test HceSessionTest StopHce.
307  * @tc.type: FUNC
308  */
309 HWTEST_F(HceSessionTest, StopHce002, TestSize.Level1)
310 {
311     std::shared_ptr<OHOS::NFC::NfcService> nfcService = std::make_shared<OHOS::NFC::NfcService>();
312     nfcService->Initialize();
313     std::weak_ptr<NFC::CeService> ceService = nfcService->GetCeService();
314     ceService.lock()->Initialize();
315     ElementName element;
316     Security::AccessToken::AccessTokenID callerToken = 0;
317     std::shared_ptr<HCE::HceSession> hceSession = std::make_shared<HCE::HceSession>(nfcService);
318     KITS::ErrorCode errorCode = hceSession->StopHce(element, callerToken);
319     ASSERT_TRUE(errorCode == NFC::KITS::ErrorCode::ERR_HCE_PARAMETERS);
320 }
321 }
322 }
323 }