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