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: SoftBusAdapter_ReadFullFileTest_001
102 * @tc.desc: Read File
103 * @tc.type: FUNC
104 * @tc.require: 1
105 */
106 HWTEST_F(AdapterDsoftbusOtherTest, SoftBusReadFullFileTest001, TestSize.Level0)
107 {
108 const char *writeBuf = "abcdef";
109 char readbuf[1024] = { "\0" };
110 int32_t maxLen = 100;
111 int32_t ret = SoftBusWriteFile(g_FileName, writeBuf, strlen(writeBuf));
112 EXPECT_EQ(SOFTBUS_OK, ret);
113
114 ret = SoftBusReadFullFile(g_FileName, readbuf, maxLen);
115 EXPECT_EQ(SOFTBUS_OK, ret);
116 }
117
118 /**
119 * @tc.name: SoftBusAdapter_ReadFullFileTest_002
120 * @tc.desc: g_FileName is null
121 * @tc.type: FUNC
122 * @tc.require: 1
123 */
124 HWTEST_F(AdapterDsoftbusOtherTest, SoftBusReadFullFileTest002, TestSize.Level0)
125 {
126 char readbuf[1024] = { "\0" };
127 int32_t maxLen = 100;
128 int32_t ret = SoftBusReadFullFile(nullptr, readbuf, maxLen);
129 EXPECT_EQ(SOFTBUS_FILE_ERR, ret);
130
131 ret = SoftBusReadFullFile(g_FileName, nullptr, maxLen);
132 EXPECT_EQ(SOFTBUS_FILE_ERR, ret);
133 }
134
135 /**
136 * @tc.name: SoftBusAdapter_ReadFullFileTest_003
137 * @tc.desc: maxLen is ivaild param
138 * @tc.type: FUNC
139 * @tc.require: 1
140 */
141 HWTEST_F(AdapterDsoftbusOtherTest, SoftBusReadFullFileTest003, TestSize.Level0)
142 {
143 char readbuf[1024] = { "\0" };
144 int32_t maxLen = 0;
145 int32_t ret = SoftBusReadFullFile(g_FileName, readbuf, maxLen);
146 EXPECT_EQ(SOFTBUS_FILE_ERR, ret);
147 }
148
149 /**
150 * @tc.name: SoftBusAdapter_WriterFileTest_001
151 * @tc.desc: writeBuf isn't nullptr
152 * @tc.type: FUNC
153 * @tc.require: 1
154 */
155 HWTEST_F(AdapterDsoftbusOtherTest, SoftBusWriterFileTest001, TestSize.Level0)
156 {
157 const char *writeBuf = "abcdef";
158 int32_t ret = SoftBusWriteFile(g_FileName, writeBuf, strlen(writeBuf));
159 EXPECT_EQ(SOFTBUS_OK, ret);
160 }
161
162 /**
163 * @tc.name: SoftBusAdapter_WriterFileTest_002
164 * @tc.desc: g_FileName and writeBuf is null
165 * @tc.type: FUNC
166 * @tc.require: 1
167 */
168 HWTEST_F(AdapterDsoftbusOtherTest, SoftBusWriterFileTest002, TestSize.Level0)
169 {
170 const char *writeBuf = "abcdef";
171 int32_t ret = SoftBusWriteFile(nullptr, writeBuf, strlen(writeBuf));
172 EXPECT_EQ(SOFTBUS_FILE_ERR, ret);
173
174 ret = SoftBusWriteFile(g_FileName, nullptr, strlen(writeBuf));
175 EXPECT_EQ(SOFTBUS_FILE_ERR, ret);
176 }
177
178 /**
179 * @tc.name: SoftBusAdapter_WriterFileTest_003
180 * @tc.desc: len is illegal
181 * @tc.type: FUNC
182 * @tc.require: 1
183 */
184 HWTEST_F(AdapterDsoftbusOtherTest, SoftBusWriterFileTest003, TestSize.Level0)
185 {
186 const char *writeBuf = "abcdef";
187 int32_t len = 0;
188 int32_t ret = SoftBusWriteFile(g_FileName, writeBuf, len);
189 EXPECT_EQ(SOFTBUS_FILE_ERR, ret);
190
191 int32_t len1 = -10;
192 ret = SoftBusWriteFile(g_FileName, writeBuf, len1);
193 EXPECT_EQ(SOFTBUS_FILE_ERR, ret);
194 }
195
196 /**
197 * @tc.name: SoftBusAdapter_MallocTest_001
198 * @tc.desc: size is zero
199 * @tc.type: FUNC
200 * @tc.require: 1
201 */
202 HWTEST_F(AdapterDsoftbusOtherTest, SoftBusMallocTest001, TestSize.Level0)
203 {
204 void *ret = SoftBusMalloc(0);
205 EXPECT_TRUE(ret != nullptr);
206 SoftBusFree(ret);
207 }
208
209 /**
210 * @tc.name: SoftBusAdapter_MallocTest_002
211 * @tc.desc: size is MAX_MALLOC_SIZE+1
212 * @tc.type: FUNC
213 * @tc.require: 1
214 */
215 HWTEST_F(AdapterDsoftbusOtherTest, SoftBusMallocTest002, TestSize.Level0)
216 {
217 void *ret = SoftBusMalloc(MAX_MALLOC_SIZE + 1);
218 EXPECT_EQ(nullptr, ret);
219 }
220
221 /**
222 * @tc.name: SoftBusAdapter_MallocTest_003
223 * @tc.desc: size is -1
224 * @tc.type: FUNC
225 * @tc.require: 1
226 */
227 HWTEST_F(AdapterDsoftbusOtherTest, SoftBusMallocTest003, TestSize.Level0)
228 {
229 void *ret = SoftBusMalloc(-1);
230 EXPECT_EQ(nullptr, ret);
231 }
232
233 /**
234 * @tc.name: SoftBusAdapter_MallocTest_004
235 * @tc.desc: size is MAX_MALLOC_SIZE
236 * @tc.type: FUNC
237 * @tc.require: 1
238 */
239 HWTEST_F(AdapterDsoftbusOtherTest, SoftBusMallocTest004, TestSize.Level0)
240 {
241 void *ret = SoftBusMalloc(12);
242 EXPECT_TRUE(ret != nullptr);
243 SoftBusFree(ret);
244 }
245
246 /**
247 * @tc.name: SoftBusAdapter_FreeTest_001
248 * @tc.desc: malloc size is 256
249 * @tc.type: FUNC
250 * @tc.require: 1
251 */
252 HWTEST_F(AdapterDsoftbusOtherTest, SoftBusFreeTest001, TestSize.Level0)
253 {
254 void *ret = SoftBusMalloc(256);
255 EXPECT_TRUE(ret != nullptr);
256 SoftBusFree(ret);
257 }
258
259 /**
260 * @tc.name: SoftBusAdapter_CallocTest_001
261 * @tc.desc: calloc size is zero
262 * @tc.type: FUNC
263 * @tc.require: 1
264 */
265 HWTEST_F(AdapterDsoftbusOtherTest, SoftBusCallocTest001, TestSize.Level0)
266 {
267 void *ret = SoftBusCalloc(0);
268 EXPECT_TRUE(ret != nullptr);
269 SoftBusFree(ret);
270 }
271
272 /**
273 * @tc.name: SoftBusAdapter_CallocTest_002
274 * @tc.desc: calloc size is 22
275 * @tc.type: FUNC
276 * @tc.require: 1
277 */
278 HWTEST_F(AdapterDsoftbusOtherTest, SoftBusCallocTest002, TestSize.Level0)
279 {
280 void *ret = SoftBusCalloc(22);
281 EXPECT_TRUE(ret != nullptr);
282 SoftBusFree(ret);
283 }
284
285 /**
286 * @tc.name: SoftBusAdapter_CallocTest_003
287 * @tc.desc: calloc size is 256
288 * @tc.type: FUNC
289 * @tc.require: 1
290 */
291 HWTEST_F(AdapterDsoftbusOtherTest, SoftBusCallocTest003, TestSize.Level0)
292 {
293 void *ret = SoftBusCalloc(-1);
294 EXPECT_EQ(nullptr, ret);
295 }
296
297 /**
298 * @tc.name: SoftBusAdapter_CallocTest_004
299 * @tc.desc: calloc size is 256
300 * @tc.type: FUNC
301 * @tc.require: 1
302 */
303 HWTEST_F(AdapterDsoftbusOtherTest, SoftBusCallocTest004, TestSize.Level0)
304 {
305 void *ret = SoftBusCalloc(MAX_MALLOC_SIZE + 1);
306 EXPECT_EQ(nullptr, ret);
307 }
308
309 } // namespace OHOS
310