• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021 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 
16 #include <gtest/gtest.h>
17 
18 #include "softbus_errcode.h"
19 #include "softbus_utils.h"
20 
21 using namespace testing::ext;
22 
23 namespace OHOS {
24 class SoftBusUtilsTest : public testing::Test {
25 public:
SetUpTestCase(void)26     static void SetUpTestCase(void) {}
TearDownTestCase(void)27     static void TearDownTestCase(void) {}
28 };
29 
MockSoftBusTimer(void)30 void MockSoftBusTimer(void)
31 {
32 }
33 
34 /**
35  * @tc.name: SoftBusUtilsTest_CreateSoftBusList_001
36  * @tc.desc: Normal destroy softbus list test.
37  * @tc.type: FUNC
38  * @tc.require:
39  */
40 HWTEST_F(SoftBusUtilsTest, SoftBusUtilsTest_DestroySoftBusList_001, TestSize.Level1)
41 {
42     SoftBusList *list = CreateSoftBusList();
43     EXPECT_TRUE(NULL != list);
44     DestroySoftBusList(list);
45 }
46 
47 /**
48  * @tc.name: SoftBusUtilsTest_CreateSoftBusList_001
49  * @tc.desc: Error register timeout callback test.
50  * @tc.type: FUNC
51  * @tc.require:
52  */
53 HWTEST_F(SoftBusUtilsTest, SoftBusUtilsTest_RegisterTimeoutCallback_001, TestSize.Level1)
54 {
55     int32_t timerFunId = SOFTBUS_CONN_TIMER_FUN;
56     TimerFunCallback callbac = NULL;
57     int32_t ret = RegisterTimeoutCallback(timerFunId, callbac);
58     EXPECT_EQ(SOFTBUS_ERR, ret);
59 
60     callbac = MockSoftBusTimer;
61     timerFunId = SOFTBUS_CONN_TIMER_FUN - 1;
62     ret = RegisterTimeoutCallback(timerFunId, callbac);
63     EXPECT_EQ(SOFTBUS_ERR, ret);
64 
65     timerFunId = SOFTBUS_MAX_TIMER_FUN_NUM;
66     ret = RegisterTimeoutCallback(timerFunId, callbac);
67     EXPECT_EQ(SOFTBUS_ERR, ret);
68 }
69 
70 /**
71  * @tc.name: SoftBusUtilsTest_RegisterTimeoutCallback_002
72  * @tc.desc: Normal register timeout callback test.
73  * @tc.type: FUNC
74  * @tc.require:
75  */
76 HWTEST_F(SoftBusUtilsTest, SoftBusUtilsTest_RegisterTimeoutCallback_002, TestSize.Level1)
77 {
78     int32_t timerFunId = SOFTBUS_CONN_TIMER_FUN;
79     TimerFunCallback callbac = MockSoftBusTimer;
80     int32_t ret = RegisterTimeoutCallback(timerFunId, callbac);
81     EXPECT_EQ(SOFTBUS_OK, ret);
82 
83     ret = RegisterTimeoutCallback(timerFunId, callbac);
84     EXPECT_EQ(SOFTBUS_OK, ret);
85 
86     timerFunId = SOFTBUS_CONN_TIMER_FUN + 1;
87     ret = RegisterTimeoutCallback(timerFunId, callbac);
88     EXPECT_EQ(SOFTBUS_OK, ret);
89 }
90 
91 /**
92  * @tc.name: SoftBusUtilsTest_SoftBusTimerInit_001
93  * @tc.desc: Normal timer init.
94  * @tc.type: FUNC
95  * @tc.require:
96  */
97 HWTEST_F(SoftBusUtilsTest, SoftBusUtilsTest_SoftBusTimerInit_001, TestSize.Level1)
98 {
99     int32_t ret = SoftBusTimerInit();
100     EXPECT_EQ(SOFTBUS_OK, ret);
101 
102     ret = SoftBusTimerInit();
103     EXPECT_EQ(SOFTBUS_OK, ret);
104 }
105 
106 /**
107  * @tc.name: SoftBusUtilsTest_SoftBusTimerDeInit_001
108  * @tc.desc: Normal timer deinit.
109  * @tc.type: FUNC
110  * @tc.require:
111  */
112 HWTEST_F(SoftBusUtilsTest, SoftBusUtilsTest_SoftBusTimerDeInit_001, TestSize.Level1)
113 {
114     int32_t ret = SoftBusTimerInit();
115     EXPECT_EQ(SOFTBUS_OK, ret);
116     SoftBusTimerDeInit();
117 }
118 
119 /**
120  * @tc.name: SoftBusUtilsTest_ConvertHexStringToBytes_001
121  * @tc.desc: Parameter error when convert hex string to bytes.
122  * @tc.type: FUNC
123  * @tc.require:
124  */
125 HWTEST_F(SoftBusUtilsTest, SoftBusUtilsTest_ConvertHexStringToBytes_001, TestSize.Level1)
126 {
127     unsigned char *outBuf = NULL;
128     uint32_t outBufLen = 0;
129     const char *inBuf = "41424344";
130     uint32_t inLen = 8;
131     int32_t ret = ConvertHexStringToBytes(outBuf, outBufLen, inBuf, inLen);
132     EXPECT_EQ(SOFTBUS_ERR, ret);
133 
134     unsigned char outBufArray[5] = "\0";
135     outBuf = outBufArray;
136     outBufLen = 5;
137     inBuf = NULL;
138     inLen = 0;
139     ret = ConvertHexStringToBytes(outBuf, outBufLen, inBuf, inLen);
140     EXPECT_EQ(SOFTBUS_ERR, ret);
141 
142     outBuf = outBufArray;
143     outBufLen = 5;
144     inBuf = "414243444";
145     inLen = 9;
146     ret = ConvertHexStringToBytes(outBuf, outBufLen, inBuf, inLen);
147     EXPECT_EQ(SOFTBUS_ERR, ret);
148 
149     outBuf = outBufArray;
150     outBufLen = 5;
151     inBuf = "414243FG";
152     inLen = 8;
153     ret = ConvertHexStringToBytes(outBuf, outBufLen, inBuf, inLen);
154     EXPECT_EQ(SOFTBUS_ERR, ret);
155 
156     outBuf = outBufArray;
157     outBufLen = 5;
158     inBuf = "414243GF";
159     inLen = 8;
160     ret = ConvertHexStringToBytes(outBuf, outBufLen, inBuf, inLen);
161     EXPECT_EQ(SOFTBUS_ERR, ret);
162 }
163 
164 /**
165  * @tc.name: SoftBusUtilsTest_ConvertHexStringToBytes_002
166  * @tc.desc: Normal convert hex string to bytes.
167  * @tc.type: FUNC
168  * @tc.require:
169  */
170 HWTEST_F(SoftBusUtilsTest, SoftBusUtilsTest_ConvertHexStringToBytes_002, TestSize.Level1)
171 {
172     unsigned char outBuf[5] = "\0";
173     uint32_t outBufLen = 5;
174     const char *inBuf = "41424344";
175     uint32_t inLen = 8;
176     int32_t ret = ConvertHexStringToBytes(outBuf, outBufLen, inBuf, inLen);
177     EXPECT_EQ(SOFTBUS_OK, ret);
178 
179     const unsigned char expect[5] = "ABCD";
180     for (int i = 0; i < 5; i++) {
181         EXPECT_EQ(expect[i], outBuf[i]);
182     }
183 }
184 
185 /**
186  * @tc.name: SoftBusUtilsTest_ConvertBytesToHexString_001
187  * @tc.desc: Parameter error when convert bytes to hex string.
188  * @tc.type: FUNC
189  * @tc.require:
190  */
191 HWTEST_F(SoftBusUtilsTest, SoftBusUtilsTest_ConvertBytesToHexString_001, TestSize.Level1)
192 {
193     char *outBuf = NULL;
194     uint32_t outBufLen = 0;
195     const unsigned char inBuf[5] = "ABCD";
196     uint32_t inLen = 4;
197     int32_t ret = ConvertBytesToHexString(outBuf, outBufLen, inBuf, inLen);
198     EXPECT_EQ(SOFTBUS_ERR, ret);
199 
200     char outBufArray[5] = "\0";
201     outBuf = outBufArray;
202     outBufLen = 4;
203     inLen = 8;
204     ret = ConvertBytesToHexString(outBuf, outBufLen, inBuf, inLen);
205     EXPECT_EQ(SOFTBUS_ERR, ret);
206 
207     outBufLen = 9;
208     const unsigned char *inBuf2 = NULL;
209     inLen = 0;
210     ret = ConvertBytesToHexString(outBuf, outBufLen, inBuf2, inLen);
211     EXPECT_EQ(SOFTBUS_ERR, ret);
212 }
213 
214 /**
215  * @tc.name: SoftBusUtilsTest_ConvertBytesToHexString_002
216  * @tc.desc: Normal convert bytes to hex string.
217  * @tc.type: FUNC
218  * @tc.require:
219  */
220 HWTEST_F(SoftBusUtilsTest, SoftBusUtilsTest_ConvertBytesToHexString_002, TestSize.Level1)
221 {
222     char outBuf[9] = "\0";
223     uint32_t outBufLen = 9;
224     unsigned char inBuf[5] = "abcd";
225     uint32_t inLen = 4;
226     int32_t ret = ConvertBytesToHexString(outBuf, outBufLen, inBuf, inLen);
227     EXPECT_EQ(SOFTBUS_OK, ret);
228 
229     const char *expect = "61626364";
230     EXPECT_STREQ(expect, outBuf);
231 }
232 
233 /**
234  * @tc.name: SoftBusUtilsTest_GenerateRandomStr_001
235  * @tc.desc: Parameter error when generate random string.
236  * @tc.type: FUNC
237  * @tc.require:
238  */
239 HWTEST_F(SoftBusUtilsTest, SoftBusUtilsTest_GenerateRandomStr_001, TestSize.Level1)
240 {
241     char *str = NULL;
242     uint32_t len = 4;
243     int32_t ret = GenerateRandomStr(str, len);
244     EXPECT_EQ(SOFTBUS_INVALID_PARAM, ret);
245 
246     char str2[5] = "\0";
247     len = 1;
248     ret = GenerateRandomStr(str2, len);
249     EXPECT_EQ(SOFTBUS_INVALID_PARAM, ret);
250 }
251 
252 /**
253  * @tc.name: SoftBusUtilsTest_IsValidString_001
254  * @tc.desc: Check string valid.
255  * @tc.type: FUNC
256  * @tc.require:
257  */
258 HWTEST_F(SoftBusUtilsTest, SoftBusUtilsTest_IsValidString_001, TestSize.Level1)
259 {
260     const char *input = NULL;
261     uint32_t maxLen = 4;
262     EXPECT_FALSE(IsValidString(input, maxLen));
263 
264     input = "ABCD";
265     maxLen = 4;
266     EXPECT_FALSE(IsValidString(input, maxLen));
267 
268     input = "ABCD";
269     maxLen = 5;
270     EXPECT_TRUE(IsValidString(input, maxLen));
271 }
272 
273 /**
274  * @tc.name: SoftBusUtilsTest_ConvertBtMacToBinary_001
275  * @tc.desc: Parameter error when convert bt mac to binary.
276  * @tc.type: FUNC
277  * @tc.require:
278  */
279 HWTEST_F(SoftBusUtilsTest, SoftBusUtilsTest_ConvertBtMacToBinary_001, TestSize.Level1)
280 {
281     const char *strMac = NULL;
282     uint32_t strMacLen = 0;
283     uint8_t *binMac = NULL;
284     uint32_t binMacLen = 0;
285     int32_t ret = ConvertBtMacToBinary(strMac, strMacLen, binMac, binMacLen);
286     EXPECT_EQ(SOFTBUS_INVALID_PARAM, ret);
287 }
288 
289 /**
290  * @tc.name: SoftBusUtilsTest_ConvertBtMacToStr_001
291  * @tc.desc: Parameter error when convert binary to bt mac.
292  * @tc.type: FUNC
293  * @tc.require:
294  */
295 HWTEST_F(SoftBusUtilsTest, SoftBusUtilsTest_ConvertBtMacToStr_001, TestSize.Level1)
296 {
297     char *strMac = NULL;
298     uint32_t strMacLen = 0;
299     const uint8_t *binMac = NULL;
300     uint32_t binMacLen = 0;
301     int32_t ret = ConvertBtMacToStr(strMac, strMacLen, binMac, binMacLen);
302     EXPECT_EQ(SOFTBUS_INVALID_PARAM, ret);
303 }
304 
305 /**
306  * @tc.name: SoftBusUtilsTest_ConvertBtMacToStr_002
307  * @tc.desc: Normal convert binary to bt mac.
308  * @tc.type: FUNC
309  * @tc.require:
310  */
311 HWTEST_F(SoftBusUtilsTest, SoftBusUtilsTest_ConvertBtMacToStr_002, TestSize.Level1)
312 {
313     char strMac[19] = "\0";
314     uint32_t strMacLen = 18;
315     const uint8_t binMac[6] = { 101, 102, 103, 104, 105, 106 };
316     uint32_t binMacLen = 6;
317     int32_t ret = ConvertBtMacToStr(strMac, strMacLen, binMac, binMacLen);
318     EXPECT_EQ(SOFTBUS_OK, ret);
319 
320     const char *expect = "65:66:67:68:69:6a";
321     EXPECT_STREQ(expect, strMac);
322 }
323 } // namespace OHOS