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 "nfc_controller_impl.h"
19 #include "nfc_service.h"
20 
21 namespace OHOS {
22 namespace NFC {
23 namespace TEST {
24 using namespace testing::ext;
25 using namespace OHOS::NFC;
26 class NfcControllerImplTest : public testing::Test {
27 public:
28     static void SetUpTestCase();
29     static void TearDownTestCase();
30     void SetUp();
31     void TearDown();
32 };
33 
SetUpTestCase()34 void NfcControllerImplTest::SetUpTestCase()
35 {
36     std::cout << " SetUpTestCase NfcControllerImplTest." << std::endl;
37 }
38 
TearDownTestCase()39 void NfcControllerImplTest::TearDownTestCase()
40 {
41     std::cout << " TearDownTestCase NfcControllerImplTest." << std::endl;
42 }
43 
SetUp()44 void NfcControllerImplTest::SetUp()
45 {
46     std::cout << " SetUp NfcControllerImplTest." << std::endl;
47 }
48 
TearDown()49 void NfcControllerImplTest::TearDown()
50 {
51     std::cout << " TearDown NfcControllerImplTest." << std::endl;
52 }
53 
54 /**
55  * @tc.name: GetState001
56  * @tc.desc: Test NfcControllerImplTest GetState.
57  * @tc.type: FUNC
58  */
59 HWTEST_F(NfcControllerImplTest, GetState001, TestSize.Level1)
60 {
61     std::shared_ptr<NfcService> nfcService = nullptr;
62     std::shared_ptr<NfcControllerImpl> nfcControllerImpl = std::make_shared<NfcControllerImpl>(nfcService);
63     int getState = nfcControllerImpl->GetState();
64     ASSERT_TRUE(getState == KITS::ERR_NFC_PARAMETERS);
65 }
66 
67 /**
68  * @tc.name: TurnOn001
69  * @tc.desc: Test NfcControllerImplTest TurnOn.
70  * @tc.type: FUNC
71  */
72 HWTEST_F(NfcControllerImplTest, TurnOn001, TestSize.Level1)
73 {
74     std::shared_ptr<NfcService> nfcService = nullptr;
75     std::shared_ptr<NfcControllerImpl> nfcControllerImpl = std::make_shared<NfcControllerImpl>(nfcService);
76     int turnOn = nfcControllerImpl->TurnOn();
77     ASSERT_TRUE(turnOn == KITS::ERR_NFC_PARAMETERS);
78 }
79 
80 /**
81  * @tc.name: TurnOff001
82  * @tc.desc: Test NfcControllerImplTest TurnOff.
83  * @tc.type: FUNC
84  */
85 HWTEST_F(NfcControllerImplTest, TurnOff001, TestSize.Level1)
86 {
87     std::shared_ptr<NfcService> nfcService = nullptr;
88     std::shared_ptr<NfcControllerImpl> nfcControllerImpl = std::make_shared<NfcControllerImpl>(nfcService);
89     int turnOff = nfcControllerImpl->TurnOff();
90     ASSERT_TRUE(turnOff == KITS::ERR_NFC_PARAMETERS);
91 }
92 
93 /**
94  * @tc.name: IsNfcOpen001
95  * @tc.desc: Test NfcControllerImplTest IsNfcOpen.
96  * @tc.type: FUNC
97  */
98 HWTEST_F(NfcControllerImplTest, IsNfcOpen001, TestSize.Level1)
99 {
100     bool isOpen = false;
101     std::shared_ptr<NfcService> nfcService = nullptr;
102     std::shared_ptr<NfcControllerImpl> nfcControllerImpl = std::make_shared<NfcControllerImpl>(nfcService);
103     int isNfcOpen = nfcControllerImpl->IsNfcOpen(isOpen);
104     ASSERT_TRUE(isNfcOpen == KITS::ERR_NFC_PARAMETERS);
105 }
106 
107 /**
108  * @tc.name: RegisterCallBack001
109  * @tc.desc: Test NfcControllerImplTest RegisterCallBack.
110  * @tc.type: FUNC
111  */
112 HWTEST_F(NfcControllerImplTest, RegisterCallBack001, TestSize.Level1)
113 {
114     sptr<INfcControllerCallback> callback = nullptr;
115     std::string type = "";
116     Security::AccessToken::AccessTokenID callerToken = 0;
117     std::shared_ptr<NfcService> nfcService = nullptr;
118     std::shared_ptr<NfcControllerImpl> nfcControllerImpl = std::make_shared<NfcControllerImpl>(nfcService);
119     KITS::ErrorCode error = nfcControllerImpl->RegisterCallBack(callback, type, callerToken);
120     ASSERT_TRUE(error == KITS::ERR_NFC_PARAMETERS);
121 }
122 
123 /**
124  * @tc.name: UnRegisterCallBack001
125  * @tc.desc: Test NfcControllerImplTest UnRegisterCallBack.
126  * @tc.type: FUNC
127  */
128 HWTEST_F(NfcControllerImplTest, UnRegisterCallBack001, TestSize.Level1)
129 {
130     std::string type = "";
131     Security::AccessToken::AccessTokenID callerToken = 0;
132     std::shared_ptr<NfcService> nfcService = nullptr;
133     std::shared_ptr<NfcControllerImpl> nfcControllerImpl = std::make_shared<NfcControllerImpl>(nfcService);
134     KITS::ErrorCode error = nfcControllerImpl->UnRegisterCallBack(type, callerToken);
135     ASSERT_TRUE(error == KITS::ERR_NFC_PARAMETERS);
136 }
137 
138 /**
139  * @tc.name: UnRegisterCallBack002
140  * @tc.desc: Test NfcControllerImplTest UnRegisterCallBack.
141  * @tc.type: FUNC
142  */
143 HWTEST_F(NfcControllerImplTest, UnRegisterCallBack002, TestSize.Level1)
144 {
145     std::string type = "";
146     Security::AccessToken::AccessTokenID callerToken = 0;
147     std::shared_ptr<NfcService> nfcService = std::make_shared<NfcService>();
148     std::shared_ptr<NfcControllerImpl> nfcControllerImpl = std::make_shared<NfcControllerImpl>(nfcService);
149     KITS::ErrorCode error = nfcControllerImpl->UnRegisterCallBack(type, callerToken);
150     ASSERT_TRUE(error == KITS::ERR_NFC_PARAMETERS);
151 }
152 
153 /**
154  * @tc.name: UnRegisterAllCallBack001
155  * @tc.desc: Test NfcControllerImplTest UnRegisterAllCallBack.
156  * @tc.type: FUNC
157  */
158 HWTEST_F(NfcControllerImplTest, UnRegisterAllCallBack001, TestSize.Level1)
159 {
160     Security::AccessToken::AccessTokenID callerToken = 0;
161     std::shared_ptr<NfcService> nfcService = nullptr;
162     std::shared_ptr<NfcControllerImpl> nfcControllerImpl = std::make_shared<NfcControllerImpl>(nfcService);
163     KITS::ErrorCode error = nfcControllerImpl->UnRegisterAllCallBack(callerToken);
164     ASSERT_TRUE(error == KITS::ERR_NFC_PARAMETERS);
165 }
166 
167 /**
168  * @tc.name: UnRegisterAllCallBack002
169  * @tc.desc: Test NfcControllerImplTest UnRegisterAllCallBack.
170  * @tc.type: FUNC
171  */
172 HWTEST_F(NfcControllerImplTest, UnRegisterAllCallBack002, TestSize.Level1)
173 {
174     Security::AccessToken::AccessTokenID callerToken = 0;
175     std::shared_ptr<NfcService> nfcService = std::make_shared<NfcService>();
176     std::shared_ptr<NfcControllerImpl> nfcControllerImpl = std::make_shared<NfcControllerImpl>(nfcService);
177     KITS::ErrorCode error = nfcControllerImpl->UnRegisterAllCallBack(callerToken);
178     ASSERT_TRUE(error == KITS::ERR_NFC_PARAMETERS);
179 }
180 
181 /**
182  * @tc.name: GetTagServiceIface001
183  * @tc.desc: Test NfcControllerImplTest GetTagServiceIface.
184  * @tc.type: FUNC
185  */
186 HWTEST_F(NfcControllerImplTest, GetTagServiceIface001, TestSize.Level1)
187 {
188     std::shared_ptr<NfcService> nfcService = nullptr;
189     std::shared_ptr<NfcControllerImpl> nfcControllerImpl = std::make_shared<NfcControllerImpl>(nfcService);
190     sptr<IRemoteObject> iRemoteObject = nfcControllerImpl->GetTagServiceIface();
191     ASSERT_TRUE(iRemoteObject == nullptr);
192 }
193 
194 /**
195  * @tc.name: GetTagServiceIface002
196  * @tc.desc: Test NfcControllerImplTest GetTagServiceIface.
197  * @tc.type: FUNC
198  */
199 HWTEST_F(NfcControllerImplTest, GetTagServiceIface002, TestSize.Level1)
200 {
201     std::shared_ptr<NfcService> nfcService = std::make_shared<NfcService>();
202     std::shared_ptr<NfcControllerImpl> nfcControllerImpl = std::make_shared<NfcControllerImpl>(nfcService);
203     sptr<IRemoteObject> iRemoteObject = nfcControllerImpl->GetTagServiceIface();
204     ASSERT_TRUE(iRemoteObject == nullptr);
205 }
206 
207 /**
208  * @tc.name: RegNdefMsgCallback001
209  * @tc.desc: Test NfcControllerImplTest RegNdefMsgCallback.
210  * @tc.type: FUNC
211  */
212 HWTEST_F(NfcControllerImplTest, RegNdefMsgCallback001, TestSize.Level1)
213 {
214     sptr<INdefMsgCallback> callback = nullptr;
215     std::shared_ptr<NfcService> nfcService = nullptr;
216     std::shared_ptr<NfcControllerImpl> nfcControllerImpl = std::make_shared<NfcControllerImpl>(nfcService);
217     KITS::ErrorCode error = nfcControllerImpl->RegNdefMsgCallback(callback);
218     ASSERT_TRUE(error == KITS::ERR_NFC_PARAMETERS);
219 }
220 #ifdef VENDOR_APPLICATIONS_ENABLED
221 /**
222  * @tc.name: RegQueryApplicationCb001
223  * @tc.desc: Test NfcControllerImplTest RegQueryApplicationCb.
224  * @tc.type: FUNC
225  */
226 HWTEST_F(NfcControllerImplTest, RegQueryApplicationCb001, TestSize.Level1)
227 {
228     sptr<IQueryAppInfoCallback> callback = nullptr;
229     std::shared_ptr<NfcService> nfcService = nullptr;
230     std::shared_ptr<NfcControllerImpl> nfcControllerImpl = std::make_shared<NfcControllerImpl>(nfcService);
231     KITS::ErrorCode error = nfcControllerImpl->RegQueryApplicationCb(callback);
232     ASSERT_TRUE(error == KITS::ERR_NONE);
233 }
234 
235 /**
236  * @tc.name: RegCardEmulationNotifyCb001
237  * @tc.desc: Test NfcControllerImplTest RegCardEmulationNotifyCb.
238  * @tc.type: FUNC
239  */
240 HWTEST_F(NfcControllerImplTest, RegCardEmulationNotifyCb001, TestSize.Level1)
241 {
242     sptr<IOnCardEmulationNotifyCb> callback = nullptr;
243     std::shared_ptr<NfcService> nfcService = nullptr;
244     std::shared_ptr<NfcControllerImpl> nfcControllerImpl = std::make_shared<NfcControllerImpl>(nfcService);
245     KITS::ErrorCode error = nfcControllerImpl->RegCardEmulationNotifyCb(callback);
246     ASSERT_TRUE(error == KITS::ERR_NONE);
247 }
248 #endif
249 /**
250  * @tc.name: GetHceServiceIface001
251  * @tc.desc: Test NfcControllerImplTest GetHceServiceIface.
252  * @tc.type: FUNC
253  */
254 HWTEST_F(NfcControllerImplTest, GetHceServiceIface001, TestSize.Level1)
255 {
256     std::shared_ptr<NfcService> nfcService = nullptr;
257     std::shared_ptr<NfcControllerImpl> nfcControllerImpl = std::make_shared<NfcControllerImpl>(nfcService);
258     sptr<IRemoteObject> iRemoteObject = nfcControllerImpl->GetHceServiceIface();
259     ASSERT_TRUE(iRemoteObject == nullptr);
260 }
261 
262 /**
263  * @tc.name: GetHceServiceIface002
264  * @tc.desc: Test NfcControllerImplTest GetHceServiceIface.
265  * @tc.type: FUNC
266  */
267 HWTEST_F(NfcControllerImplTest, GetHceServiceIface002, TestSize.Level1)
268 {
269     std::shared_ptr<NfcService> nfcService = std::make_shared<NfcService>();
270     std::shared_ptr<NfcControllerImpl> nfcControllerImpl = std::make_shared<NfcControllerImpl>(nfcService);
271     sptr<IRemoteObject> iRemoteObject = nfcControllerImpl->GetHceServiceIface();
272     ASSERT_TRUE(iRemoteObject == nullptr);
273 }
274 
275 /**
276  * @tc.name: Dump001
277  * @tc.desc: Test NfcControllerImplTest Dump.
278  * @tc.type: FUNC
279  */
280 HWTEST_F(NfcControllerImplTest, Dump001, TestSize.Level1)
281 {
282     int32_t fd = 0;
283     std::vector<std::u16string> args;
284     std::shared_ptr<NfcService> nfcService = nullptr;
285     std::shared_ptr<NfcControllerImpl> nfcControllerImpl = std::make_shared<NfcControllerImpl>(nfcService);
286     int dump = nfcControllerImpl->Dump(fd, args);
287     ASSERT_TRUE(dump == KITS::ERR_NFC_PARAMETERS);
288 }
289 
290 /**
291  * @tc.name: Dump002
292  * @tc.desc: Test NfcControllerImplTest Dump.
293  * @tc.type: FUNC
294  */
295 HWTEST_F(NfcControllerImplTest, Dump002, TestSize.Level1)
296 {
297     int32_t fd = 0;
298     std::vector<std::u16string> args;
299     std::shared_ptr<NfcService> nfcService = std::make_shared<NfcService>();
300     std::shared_ptr<NfcControllerImpl> nfcControllerImpl = std::make_shared<NfcControllerImpl>(nfcService);
301     int dump = nfcControllerImpl->Dump(fd, args);
302     ASSERT_TRUE(dump == KITS::ERR_NFC_PARAMETERS);
303 }
304 }
305 }
306 }