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