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
16 #include <cstring>
17 #include <fcntl.h>
18
19 #include "bus_center_adapter.h"
20 #include "lnn_ip_utils_adapter.h"
21 #include "softbus_adapter_file.h"
22 #include "softbus_adapter_mem.h"
23 #include "softbus_error_code.h"
24 #include "gtest/gtest.h"
25
26 using namespace std;
27 using namespace testing::ext;
28
29 namespace OHOS {
30 const char *g_FileName = "example.txt";
31
32 class AdapterDsoftbusOtherTest : public testing::Test {
33 protected:
34 static void SetUpTestCase(void);
35 static void TearDownTestCase(void);
36 void SetUp();
37 void TearDown();
38 };
SetUpTestCase(void)39 void AdapterDsoftbusOtherTest::SetUpTestCase(void) { }
TearDownTestCase(void)40 void AdapterDsoftbusOtherTest::TearDownTestCase(void)
41 {
42 int32_t ret = remove(g_FileName);
43 if (ret == 0) {
44 return;
45 }
46 }
SetUp(void)47 void AdapterDsoftbusOtherTest::SetUp(void) { }
TearDown(void)48 void AdapterDsoftbusOtherTest::TearDown(void) { }
49
50 /*
51 * @tc.name: GetNetworkIpByIfName001
52 * @tc.desc: ifName is illegal
53 * @tc.type: FUNC
54 * @tc.require: 1
55 */
56 HWTEST_F(AdapterDsoftbusOtherTest, GetNetworkIpByIfName001, TestSize.Level0)
57 {
58 const char *ifName = "abcdefgh";
59 char netmask[] = "abcdefd";
60 char ip[32] = "0";
61 int32_t len = 10;
62 int32_t ret = GetNetworkIpByIfName(ifName, ip, netmask, len);
63 EXPECT_EQ(SOFTBUS_NETWORK_IOCTL_FAIL, ret);
64 }
65
66 /*
67 * @tc.name: GetNetworkIpByIfName002
68 * @tc.desc: ifName is nullptr
69 * @tc.type: FUNC
70 * @tc.require: 1
71 */
72 HWTEST_F(AdapterDsoftbusOtherTest, GetNetworkIpByIfName002, TestSize.Level0)
73 {
74 const char *ifName = "abcdefgh";
75 char netmask[] = "abcdefd";
76 char ip[32] = "0";
77 int32_t len = 10;
78 int32_t ret = GetNetworkIpByIfName(nullptr, ip, netmask, len);
79 EXPECT_EQ(SOFTBUS_INVALID_PARAM, ret);
80
81 ret = GetNetworkIpByIfName(ifName, nullptr, netmask, len);
82 EXPECT_EQ(SOFTBUS_INVALID_PARAM, ret);
83 }
84
85 /*
86 * @tc.name: GetNetworkIpByIfName003
87 * @tc.desc: netmask is nullptr
88 * @tc.type: FUNC
89 * @tc.require: 1
90 */
91 HWTEST_F(AdapterDsoftbusOtherTest, GetNetworkIpByIfName003, TestSize.Level0)
92 {
93 const char *ifName = "abcdefgh";
94 char ip[32] = "0";
95 int32_t len = 10;
96 int32_t ret = GetNetworkIpByIfName(ifName, ip, nullptr, len);
97 EXPECT_EQ(SOFTBUS_NETWORK_IOCTL_FAIL, ret);
98 }
99
100 /*
101 * @tc.name: GetNetworkIpv6ByIfName001
102 * @tc.desc: ifname is nullptr
103 * @tc.type: FUNC
104 * @tc.require: 1
105 */
106 HWTEST_F(AdapterDsoftbusOtherTest, GetNetworkIpv6ByIfName001, TestSize.Level1)
107 {
108 char ip[46] = "0";
109 uint32_t len = 46;
110 int32_t ret = GetNetworkIpv6ByIfName(nullptr, ip, len);
111 EXPECT_EQ(SOFTBUS_INVALID_PARAM, ret);
112 }
113
114 /*
115 * @tc.name: GetNetworkIpv6ByIfName002
116 * @tc.desc: ip is nullptr
117 * @tc.type: FUNC
118 * @tc.require: 1
119 */
120 HWTEST_F(AdapterDsoftbusOtherTest, GetNetworkIpv6ByIfName002, TestSize.Level1)
121 {
122 const char *ifName = "wlan0";
123 uint32_t len = 46;
124 int32_t ret = GetNetworkIpv6ByIfName(ifName, nullptr, len);
125 EXPECT_EQ(SOFTBUS_INVALID_PARAM, ret);
126 }
127
128 /*
129 * @tc.name: GetNetworkIpv6ByIfName003
130 * @tc.desc: len is illegal
131 * @tc.type: FUNC
132 * @tc.require: 1
133 */
134 HWTEST_F(AdapterDsoftbusOtherTest, GetNetworkIpv6ByIfName003, TestSize.Level1)
135 {
136 const char *ifName = "wlan0";
137 char ip[46] = "0";
138 uint32_t len = 40;
139 int32_t ret = GetNetworkIpv6ByIfName(ifName, ip, len);
140 EXPECT_EQ(SOFTBUS_INVALID_PARAM, ret);
141 }
142
143 /*
144 * @tc.name: GetNetworkIpv6ByIfName004
145 * @tc.desc: ifname is illegal
146 * @tc.type: FUNC
147 * @tc.require: 1
148 */
149 HWTEST_F(AdapterDsoftbusOtherTest, GetNetworkIpv6ByIfName004, TestSize.Level1)
150 {
151 const char *ifName = "abcde";
152 char ip[46] = "0";
153 uint32_t len = 46;
154 int32_t ret = GetNetworkIpv6ByIfName(ifName, ip, len);
155 EXPECT_EQ(SOFTBUS_NETWORK_GET_IP_ADDR_FAILED, ret);
156 }
157
158 /*
159 * @tc.name: GetLinkUpStateByIfName001
160 * @tc.desc: ifname is nullptr
161 * @tc.type: FUNC
162 * @tc.require: 1
163 */
164 HWTEST_F(AdapterDsoftbusOtherTest, GetLinkUpStateByIfName001, TestSize.Level1)
165 {
166 const char *ifName = nullptr;
167 bool ret = GetLinkUpStateByIfName(ifName);
168 EXPECT_FALSE(ret);
169 }
170
171 /*
172 * @tc.name: GetLinkUpStateByIfName002
173 * @tc.desc: ifname is illegal
174 * @tc.type: FUNC
175 * @tc.require: 1
176 */
177 HWTEST_F(AdapterDsoftbusOtherTest, GetLinkUpStateByIfName002, TestSize.Level1)
178 {
179 const char *ifName = "abcde";
180 bool ret = GetLinkUpStateByIfName(ifName);
181 EXPECT_FALSE(ret);
182 }
183
184 /**
185 * @tc.name: SoftBusAdapter_ReadFullFileTest_001
186 * @tc.desc: Read File
187 * @tc.type: FUNC
188 * @tc.require: 1
189 */
190 HWTEST_F(AdapterDsoftbusOtherTest, SoftBusReadFullFileTest001, TestSize.Level0)
191 {
192 const char *writeBuf = "abcdef";
193 char readbuf[1024] = { "\0" };
194 int32_t maxLen = 100;
195 int32_t ret = SoftBusWriteFile(g_FileName, writeBuf, strlen(writeBuf));
196 EXPECT_EQ(SOFTBUS_OK, ret);
197
198 ret = SoftBusReadFullFile(g_FileName, readbuf, maxLen);
199 EXPECT_EQ(SOFTBUS_OK, ret);
200 }
201
202 /**
203 * @tc.name: SoftBusAdapter_ReadFullFileTest_002
204 * @tc.desc: g_FileName is null
205 * @tc.type: FUNC
206 * @tc.require: 1
207 */
208 HWTEST_F(AdapterDsoftbusOtherTest, SoftBusReadFullFileTest002, TestSize.Level0)
209 {
210 char readbuf[1024] = { "\0" };
211 int32_t maxLen = 100;
212 int32_t ret = SoftBusReadFullFile(nullptr, readbuf, maxLen);
213 EXPECT_EQ(SOFTBUS_FILE_ERR, ret);
214
215 ret = SoftBusReadFullFile(g_FileName, nullptr, maxLen);
216 EXPECT_EQ(SOFTBUS_FILE_ERR, ret);
217 }
218
219 /**
220 * @tc.name: SoftBusAdapter_ReadFullFileTest_003
221 * @tc.desc: maxLen is ivaild param
222 * @tc.type: FUNC
223 * @tc.require: 1
224 */
225 HWTEST_F(AdapterDsoftbusOtherTest, SoftBusReadFullFileTest003, TestSize.Level0)
226 {
227 char readbuf[1024] = { "\0" };
228 int32_t maxLen = 0;
229 int32_t ret = SoftBusReadFullFile(g_FileName, readbuf, maxLen);
230 EXPECT_EQ(SOFTBUS_FILE_ERR, ret);
231 }
232
233 /**
234 * @tc.name: SoftBusAdapter_WriterFileTest_001
235 * @tc.desc: writeBuf isn't nullptr
236 * @tc.type: FUNC
237 * @tc.require: 1
238 */
239 HWTEST_F(AdapterDsoftbusOtherTest, SoftBusWriterFileTest001, TestSize.Level0)
240 {
241 const char *writeBuf = "abcdef";
242 int32_t ret = SoftBusWriteFile(g_FileName, writeBuf, strlen(writeBuf));
243 EXPECT_EQ(SOFTBUS_OK, ret);
244 }
245
246 /**
247 * @tc.name: SoftBusAdapter_WriterFileTest_002
248 * @tc.desc: g_FileName and writeBuf is null
249 * @tc.type: FUNC
250 * @tc.require: 1
251 */
252 HWTEST_F(AdapterDsoftbusOtherTest, SoftBusWriterFileTest002, TestSize.Level0)
253 {
254 const char *writeBuf = "abcdef";
255 int32_t ret = SoftBusWriteFile(nullptr, writeBuf, strlen(writeBuf));
256 EXPECT_EQ(SOFTBUS_FILE_ERR, ret);
257
258 ret = SoftBusWriteFile(g_FileName, nullptr, strlen(writeBuf));
259 EXPECT_EQ(SOFTBUS_FILE_ERR, ret);
260 }
261
262 /**
263 * @tc.name: SoftBusAdapter_WriterFileTest_003
264 * @tc.desc: len is illegal
265 * @tc.type: FUNC
266 * @tc.require: 1
267 */
268 HWTEST_F(AdapterDsoftbusOtherTest, SoftBusWriterFileTest003, TestSize.Level0)
269 {
270 const char *writeBuf = "abcdef";
271 int32_t len = 0;
272 int32_t ret = SoftBusWriteFile(g_FileName, writeBuf, len);
273 EXPECT_EQ(SOFTBUS_FILE_ERR, ret);
274
275 int32_t len1 = -10;
276 ret = SoftBusWriteFile(g_FileName, writeBuf, len1);
277 EXPECT_EQ(SOFTBUS_FILE_ERR, ret);
278 }
279
280 /**
281 * @tc.name: SoftBusAdapter_MallocTest_001
282 * @tc.desc: size is zero
283 * @tc.type: FUNC
284 * @tc.require: 1
285 */
286 HWTEST_F(AdapterDsoftbusOtherTest, SoftBusMallocTest001, TestSize.Level0)
287 {
288 void *ret = SoftBusMalloc(0);
289 EXPECT_TRUE(ret != nullptr);
290 SoftBusFree(ret);
291 }
292
293 /**
294 * @tc.name: SoftBusAdapter_MallocTest_002
295 * @tc.desc: size is MAX_MALLOC_SIZE+1
296 * @tc.type: FUNC
297 * @tc.require: 1
298 */
299 HWTEST_F(AdapterDsoftbusOtherTest, SoftBusMallocTest002, TestSize.Level0)
300 {
301 void *ret = SoftBusMalloc(MAX_MALLOC_SIZE + 1);
302 EXPECT_EQ(nullptr, ret);
303 }
304
305 /**
306 * @tc.name: SoftBusAdapter_MallocTest_003
307 * @tc.desc: size is -1
308 * @tc.type: FUNC
309 * @tc.require: 1
310 */
311 HWTEST_F(AdapterDsoftbusOtherTest, SoftBusMallocTest003, TestSize.Level0)
312 {
313 void *ret = SoftBusMalloc(-1);
314 EXPECT_EQ(nullptr, ret);
315 }
316
317 /**
318 * @tc.name: SoftBusAdapter_MallocTest_004
319 * @tc.desc: size is MAX_MALLOC_SIZE
320 * @tc.type: FUNC
321 * @tc.require: 1
322 */
323 HWTEST_F(AdapterDsoftbusOtherTest, SoftBusMallocTest004, TestSize.Level0)
324 {
325 void *ret = SoftBusMalloc(12);
326 EXPECT_TRUE(ret != nullptr);
327 SoftBusFree(ret);
328 }
329
330 /**
331 * @tc.name: SoftBusAdapter_FreeTest_001
332 * @tc.desc: malloc size is 256
333 * @tc.type: FUNC
334 * @tc.require: 1
335 */
336 HWTEST_F(AdapterDsoftbusOtherTest, SoftBusFreeTest001, TestSize.Level0)
337 {
338 void *ret = SoftBusMalloc(256);
339 EXPECT_TRUE(ret != nullptr);
340 SoftBusFree(ret);
341 }
342
343 /**
344 * @tc.name: SoftBusAdapter_CallocTest_001
345 * @tc.desc: calloc size is zero
346 * @tc.type: FUNC
347 * @tc.require: 1
348 */
349 HWTEST_F(AdapterDsoftbusOtherTest, SoftBusCallocTest001, TestSize.Level0)
350 {
351 void *ret = SoftBusCalloc(0);
352 EXPECT_TRUE(ret != nullptr);
353 SoftBusFree(ret);
354 }
355
356 /**
357 * @tc.name: SoftBusAdapter_CallocTest_002
358 * @tc.desc: calloc size is 22
359 * @tc.type: FUNC
360 * @tc.require: 1
361 */
362 HWTEST_F(AdapterDsoftbusOtherTest, SoftBusCallocTest002, TestSize.Level0)
363 {
364 void *ret = SoftBusCalloc(22);
365 EXPECT_TRUE(ret != nullptr);
366 SoftBusFree(ret);
367 }
368
369 /**
370 * @tc.name: SoftBusAdapter_CallocTest_003
371 * @tc.desc: calloc size is 256
372 * @tc.type: FUNC
373 * @tc.require: 1
374 */
375 HWTEST_F(AdapterDsoftbusOtherTest, SoftBusCallocTest003, TestSize.Level0)
376 {
377 void *ret = SoftBusCalloc(-1);
378 EXPECT_EQ(nullptr, ret);
379 }
380
381 /**
382 * @tc.name: SoftBusAdapter_CallocTest_004
383 * @tc.desc: calloc size is 256
384 * @tc.type: FUNC
385 * @tc.require: 1
386 */
387 HWTEST_F(AdapterDsoftbusOtherTest, SoftBusCallocTest004, TestSize.Level0)
388 {
389 void *ret = SoftBusCalloc(MAX_MALLOC_SIZE + 1);
390 EXPECT_EQ(nullptr, ret);
391 }
392
393 } // namespace OHOS
394