1 /*
2 * Copyright (c) 2022 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_sdk_common.h"
19 #include "nfc_timer.h"
20 #include "nfc_preferences.h"
21 #include "nfc_permission_checker.h"
22 #include "nfc_watch_dog.h"
23 #include "synchronize_event.h"
24
25 namespace OHOS {
26 namespace NFC {
27 namespace TEST {
28 using namespace testing::ext;
29 using namespace OHOS::NFC::KITS;
30 class NfcPublicTest : public testing::Test {
31 public:
32 static void SetUpTestCase();
33 static void TearDownTestCase();
34 void SetUp();
35 void TearDown();
36 public:
37
38 // An test array of byte types with a length of 6
39 static constexpr char TEST_DATA_ANY_CHARS[4] = {'b', 'c', '0', '1'};
40
41 // The length of the array to be tested is 4
42 static constexpr auto TEST_DATA_ANY_HEX_STRING_BYTE_LEN = 4;
43
44 // The hexadecimal string of the ascll table corresponding to the array to be tested is 62633031
45 static constexpr auto TEST_DATA_ANY_HEX_STRING = "62633031";
46
47 // A string "11111" used to test the conversion of characters to ints
48 static constexpr auto TEST_DATA_STR = "11111";
49
50 // "11111" to int is 825307441
51 static constexpr auto TEST_DATA_STR_TO_INT = 825307441;
52 static constexpr unsigned char TEST_DATA_UNSIGNED_CHAR = '0';
53
54 // The character 0 corresponds to the number 30 of the hexadecimal ascll table
55 static constexpr auto TEST_DATA_UNSIGNED_CHAR_STR = "30";
56 };
57
SetUpTestCase()58 void NfcPublicTest::SetUpTestCase()
59 {
60 std::cout << " SetUpTestCase PublicTest." << std::endl;
61 }
62
TearDownTestCase()63 void NfcPublicTest::TearDownTestCase()
64 {
65 std::cout << " TearDownTestCase PublicTest." << std::endl;
66 }
67
SetUp()68 void NfcPublicTest::SetUp() {}
69
TearDown()70 void NfcPublicTest::TearDown() {}
71
72 /**
73 * @tc.name: IsLittleEndian001
74 * @tc.desc: Test NfcPublic IsLittleEndian.
75 * @tc.type: FUNC
76 */
77 HWTEST_F(NfcPublicTest, IsLittleEndian001, TestSize.Level1)
78 {
79 bool isLittleEndian = NfcSdkCommon::IsLittleEndian();
80 ASSERT_TRUE(isLittleEndian == true);
81 }
82
83 /**
84 * @tc.name: BytesVecToHexString001
85 * @tc.desc: Test NfcPublic BytesVecToHexString.
86 * @tc.type: FUNC
87 */
88 HWTEST_F(NfcPublicTest, BytesVecToHexString001, TestSize.Level1)
89 {
90 unsigned char *bytes = new unsigned char[0];
91 std::string hexString = NfcSdkCommon::BytesVecToHexString(bytes, 0);
92 ASSERT_TRUE(strcmp(hexString.c_str(), "") == 0);
93 }
94
95 /**
96 * @tc.name: BytesVecToHexString002
97 * @tc.desc: Test NfcPublic BytesVecToHexString.
98 * @tc.type: FUNC
99 */
100 HWTEST_F(NfcPublicTest, BytesVecToHexString002, TestSize.Level1)
101 {
102 unsigned char bytes[4] = {'b', 'c', '0', '1'};
103 uint32_t size = sizeof(bytes) / sizeof(bytes[0]);
104 std::string hexString = NfcSdkCommon::BytesVecToHexString(bytes, size);
105 ASSERT_TRUE(strcmp(hexString.c_str(), TEST_DATA_ANY_HEX_STRING) == 0);
106 }
107
108 /**
109 * @tc.name: UnsignedCharToHexString001
110 * @tc.desc: Test NfcPublic UnsignedCharToHexString.
111 * @tc.type: FUNC
112 */
113 HWTEST_F(NfcPublicTest, UnsignedCharToHexString001, TestSize.Level1)
114 {
115 std::string hexString = NfcSdkCommon::UnsignedCharToHexString(TEST_DATA_UNSIGNED_CHAR);
116 ASSERT_TRUE(strcmp(hexString.c_str(), TEST_DATA_UNSIGNED_CHAR_STR) == 0);
117 }
118
119 /**
120 * @tc.name: GetHexStrBytesLen001
121 * @tc.desc: Test NfcPublic GetHexStrBytesLen.
122 * @tc.type: FUNC
123 */
124 HWTEST_F(NfcPublicTest, GetHexStrBytesLen001, TestSize.Level1)
125 {
126 std::string src = "";
127 uint32_t hexStrBytesLen = NfcSdkCommon::GetHexStrBytesLen(src);
128 ASSERT_TRUE(hexStrBytesLen == 0);
129 }
130
131 /**
132 * @tc.name: GetHexStrBytesLen002
133 * @tc.desc: Test NfcPublic GetHexStrBytesLen.
134 * @tc.type: FUNC
135 */
136 HWTEST_F(NfcPublicTest, GetHexStrBytesLen002, TestSize.Level1)
137 {
138 uint32_t hexStrBytesLen = NfcSdkCommon::GetHexStrBytesLen(TEST_DATA_ANY_HEX_STRING);
139 ASSERT_TRUE(hexStrBytesLen == TEST_DATA_ANY_HEX_STRING_BYTE_LEN);
140 }
141
142 /**
143 * @tc.name: GetByteFromHexStr001
144 * @tc.desc: Test NfcPublic GetByteFromHexStr.
145 * @tc.type: FUNC
146 */
147 HWTEST_F(NfcPublicTest, GetByteFromHexStr001, TestSize.Level1)
148 {
149 uint32_t index = 0;
150 unsigned char byteFromHexStr = NfcSdkCommon::GetByteFromHexStr(TEST_DATA_UNSIGNED_CHAR_STR, index);
151 ASSERT_TRUE(byteFromHexStr == TEST_DATA_UNSIGNED_CHAR);
152 }
153
154 /**
155 * @tc.name: StringToInt001
156 * @tc.desc: Test NfcPublic StringToInt.
157 * @tc.type: FUNC
158 */
159 HWTEST_F(NfcPublicTest, StringToInt001, TestSize.Level1)
160 {
161 bool bLittleEndian = true;
162 uint32_t srcToInt = NfcSdkCommon::StringToInt(TEST_DATA_STR, bLittleEndian);
163 ASSERT_TRUE(srcToInt == TEST_DATA_STR_TO_INT);
164 }
165
166 /**
167 * @tc.name: StringToInt002
168 * @tc.desc: Test NfcPublic StringToInt.
169 * @tc.type: FUNC
170 */
171 HWTEST_F(NfcPublicTest, StringToInt002, TestSize.Level1)
172 {
173 bool bLittleEndian = false;
174 uint32_t srcToInt = NfcSdkCommon::StringToInt(TEST_DATA_STR, bLittleEndian);
175 ASSERT_TRUE(srcToInt == TEST_DATA_STR_TO_INT);
176 }
177
178 /**
179 * @tc.name: IntToHexString001
180 * @tc.desc: Test NfcPublic IntToHexString.
181 * @tc.type: FUNC
182 */
183 HWTEST_F(NfcPublicTest, IntToHexString001, TestSize.Level1)
184 {
185 // 255 corresponds to hexadecimal a
186 uint32_t num = 255;
187 std::string intToStr = NfcSdkCommon::IntToHexString(num);
188 ASSERT_TRUE(intToStr == "FF");
189 }
190
191 /**
192 * @tc.name: StringToHexString001
193 * @tc.desc: Test NfcPublic StringToHexString.
194 * @tc.type: FUNC
195 */
196 HWTEST_F(NfcPublicTest, StringToHexString001, TestSize.Level1)
197 {
198 std::string str = "0";
199 std::string strToHexStr = NfcSdkCommon::StringToHexString(str);
200 ASSERT_TRUE(strcmp(strToHexStr.c_str(), TEST_DATA_UNSIGNED_CHAR_STR) == 0);
201 }
202 /**
203 * @tc.name: HexStringToBytes001
204 * @tc.desc: Test NfcPublic HexStringToBytes.
205 * @tc.type: FUNC
206 */
207 HWTEST_F(NfcPublicTest, HexStringToBytes001, TestSize.Level1)
208 {
209 const std::string src = "";
210 std::vector<unsigned char> bytes;
211 NfcSdkCommon::HexStringToBytes(src, bytes);
212 ASSERT_TRUE(bytes.empty() == true);
213 }
214 /**
215 * @tc.name: HexStringToBytes002
216 * @tc.desc: Test NfcPublic HexStringToBytes.
217 * @tc.type: FUNC
218 */
219 HWTEST_F(NfcPublicTest, HexStringToBytes002, TestSize.Level1)
220 {
221 const std::string src = "01";
222 std::vector<unsigned char> bytes;
223 NfcSdkCommon::HexStringToBytes(src, bytes);
224 ASSERT_TRUE(bytes.size() == FeatureType::UICC);
225 }
226 /**
227 * @tc.name: GetHexStrBytesLen003
228 * @tc.desc: Test NfcPublic GetHexStrBytesLen.
229 * @tc.type: FUNC
230 */
231 HWTEST_F(NfcPublicTest, GetHexStrBytesLen003, TestSize.Level1)
232 {
233 const std::string src = "43020";
234 uint32_t hexStrBytesLen = NfcSdkCommon::GetHexStrBytesLen(src);
235 ASSERT_TRUE(hexStrBytesLen == 3);
236 }
237 /**
238 * @tc.name: GetByteFromHexStr002
239 * @tc.desc: Test NfcPublic GetByteFromHexStr.
240 * @tc.type: FUNC
241 */
242 HWTEST_F(NfcPublicTest, GetByteFromHexStr002, TestSize.Level1)
243 {
244 const std::string src = "%x";
245 uint32_t index = 0;
246 unsigned char byteFromHexStr = NfcSdkCommon::GetByteFromHexStr(src, index);
247 ASSERT_TRUE(byteFromHexStr == 0);
248 }
249 /**
250 * @tc.name: GetCurrentTime001
251 * @tc.desc: Test NfcPublic GetCurrentTime.
252 * @tc.type: FUNC
253 */
254 HWTEST_F(NfcPublicTest, GetCurrentTime001, TestSize.Level1)
255 {
256 uint64_t getCurrentTime = NfcSdkCommon::GetCurrentTime();
257 ASSERT_TRUE(getCurrentTime != 0);
258 }
259 /**
260 * @tc.name: GetByteFromHexStr003
261 * @tc.desc: Test NfcPublic GetByteFromHexStr.
262 * @tc.type: FUNC
263 */
264 HWTEST_F(NfcPublicTest, GetByteFromHexStr003, TestSize.Level1)
265 {
266 const std::string src = TEST_DATA_ANY_HEX_STRING;
267 uint32_t index = TEST_DATA_ANY_HEX_STRING_BYTE_LEN;
268 unsigned char byteFromHexStr = NfcSdkCommon::GetByteFromHexStr(src, index);
269 ASSERT_TRUE(byteFromHexStr == 0);
270 }
271 /**
272 * @tc.name: IntToHexString002
273 * @tc.desc: Test NfcPublic IntToHexString.
274 * @tc.type: FUNC
275 */
276 HWTEST_F(NfcPublicTest, IntToHexString002, TestSize.Level1)
277 {
278 uint32_t num = ErrorCode::ERR_NONE;
279 std::string intToStr = NfcSdkCommon::IntToHexString(num);
280 ASSERT_TRUE(intToStr == "00");
281 }
282 /**
283 * @tc.name: HexStringToBytes003
284 * @tc.desc: Test NfcPublic HexStringToBytes.
285 * @tc.type: FUNC
286 */
287 HWTEST_F(NfcPublicTest, HexStringToBytes003, TestSize.Level1)
288 {
289 const std::string src = "%x";
290 std::vector<unsigned char> bytes;
291 NfcSdkCommon::HexStringToBytes(src, bytes);
292 ASSERT_TRUE(bytes.empty() == true);
293 }
294
295 /**
296 * @tc.name: CodeMiddlePart001
297 * @tc.desc: Test NfcPublic CodeMiddlePart001.
298 * @tc.type: FUNC
299 */
300 HWTEST_F(NfcPublicTest, CodeMiddlePart001, TestSize.Level1)
301 {
302 std::string src = "";
303 std::string res = NfcSdkCommon::CodeMiddlePart(src);
304 ASSERT_TRUE(res.empty() == true);
305 }
306
307 /**
308 * @tc.name: CodeMiddlePart002
309 * @tc.desc: Test NfcPublic CodeMiddlePart002.
310 * @tc.type: FUNC
311 */
312 HWTEST_F(NfcPublicTest, CodeMiddlePart002, TestSize.Level1)
313 {
314 std::string src(1025, 'a');
315 std::string res = NfcSdkCommon::CodeMiddlePart(src);
316 ASSERT_TRUE(res.empty() == true);
317 }
318
319 /**
320 * @tc.name: CodeMiddlePart003
321 * @tc.desc: Test NfcPublic CodeMiddlePart003.
322 * @tc.type: FUNC
323 */
324 HWTEST_F(NfcPublicTest, CodeMiddlePart003, TestSize.Level1)
325 {
326 std::string src = "CodeMiddlePart";
327 std::string res = NfcSdkCommon::CodeMiddlePart(src);
328 ASSERT_TRUE(res.empty() == false);
329 }
330
331 /**
332 * @tc.name: NfcPrefImpl001
333 * @tc.desc: Test NfcPublic NfcPrefImpl001.
334 * @tc.type: FUNC
335 */
336 HWTEST_F(NfcPublicTest, NfcPrefImpl001, TestSize.Level1)
337 {
338 OHOS::NFC::NfcPreferences *impl = new OHOS::NFC::NfcPreferences();
339 impl->SetString("test_key", "test_value");
340 std::string value = impl->GetString("test_key");
341 ASSERT_TRUE(value == "test_value");
342
343 impl->Delete("test_key");
344 value = impl->GetString("test_key");
345 ASSERT_TRUE(value == "");
346
347 impl->Clear();
348 value = impl->GetString("test_key");
349 delete impl;
350 ASSERT_TRUE(value == "");
351 }
352
353 /**
354 * @tc.name: NfcPermissionChecker001
355 * @tc.desc: Test NfcPermissionChecker001
356 * @tc.type: FUNC
357 */
358 HWTEST_F(NfcPublicTest, NfcPermissionChecker001, TestSize.Level1)
359 {
360 bool granted = NfcPermissionChecker::IsGranted("unitest.permission.nfc");
361 ASSERT_TRUE(granted == true);
362 }
363
364 /**
365 * @tc.name: NfcWatchDog001
366 * @tc.desc: Test NfcWatchDog001
367 * @tc.type: FUNC
368 */
369 HWTEST_F(NfcPublicTest, NfcWatchDog001, TestSize.Level1)
370 {
371 NfcTimer::GetInstance()->UnRegister(0);
372 std::shared_ptr<NCI::INciNfccInterface> nciNfccProxy = nullptr;
373 NfcWatchDog nfcWatchDog("Test", 90 * 1000, nciNfccProxy);
374 nfcWatchDog.Run();
375 nfcWatchDog.Cancel();
376 uint64_t currentTime = NfcSdkCommon::GetCurrentTime();
377 ASSERT_TRUE(currentTime != 0);
378 }
379
380 /**
381 * @tc.name: NfcWatchDog002
382 * @tc.desc: Test NfcWatchDog002
383 * @tc.type: FUNC
384 */
385 HWTEST_F(NfcPublicTest, NfcWatchDog002, TestSize.Level1)
386 {
387 NfcTimer::GetInstance()->UnRegister(0);
388 std::shared_ptr<NCI::INciNfccInterface> nciNfccProxy = nullptr;
389 NfcWatchDog nfcWatchDog("DoTurnOn", 90 * 1000, nciNfccProxy);
390 nfcWatchDog.Run();
391 nfcWatchDog.Cancel();
392 uint64_t currentTime = NfcSdkCommon::GetCurrentTime();
393 ASSERT_TRUE(currentTime != 0);
394 }
395 }
396 }
397 }
398