• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 #include <arpa/inet.h>
2 #include <gtest/gtest.h>
3 using namespace testing::ext;
4 
5 class ArpaInetTest : public testing::Test {
SetUp()6     void SetUp() override {}
TearDown()7     void TearDown() override {}
8 };
9 
10 /**
11  * @tc.name: inet_pton_001
12  * @tc.desc: Test inet_pton function replaces addr dotted decimal string with an IPv4 address string
13  * @tc.type: FUNC
14  **/
15 HWTEST_F(ArpaInetTest, inet_pton_001, TestSize.Level1)
16 {
17     struct sockaddr_in addr;
18     int result = inet_pton(AF_INET, "127.0.0.1", &(addr.sin_addr));
19     EXPECT_EQ(1, result);
20     char buf[INET_ADDRSTRLEN];
21     memset(&buf, 0, sizeof(buf));
22     inet_ntop(AF_INET, &(addr.sin_addr), buf, INET_ADDRSTRLEN);
23     EXPECT_STREQ("127.0.0.1", buf);
24 }
25 
26 /**
27  * @tc.name: inet_ntoa_001
28  * @tc.desc: Verify that the inet_ntoa function correctly converts a valid IPv4 address in binary format to its
29  *           string representation.
30  * @tc.type: FUNC
31  **/
32 HWTEST_F(ArpaInetTest, inet_ntoa_001, TestSize.Level1)
33 {
34     in_addr addr = { (htonl)(0x7f000001) };
35     char* result = inet_ntoa(addr);
36     EXPECT_STREQ("127.0.0.1", result);
37 }
38 
39 /**
40  * @tc.name: inet_aton_001
41  * @tc.desc: Verify that the inet_aton function correctly converts a valid IPv4 address to binary format and stores
42  *           it in the s_addr field of the in_addr struct.
43  * @tc.type: FUNC
44  **/
45 HWTEST_F(ArpaInetTest, inet_aton_001, TestSize.Level1)
46 {
47     in_addr addr;
48     addr.s_addr = 0;
49     int result = inet_aton("127.9.8.7", &addr);
50     EXPECT_EQ(1, result);
51     EXPECT_EQ((htonl)(0x7f090807), addr.s_addr);
52 }
53 
54 /**
55  * @tc.name: inet_aton_002
56  * @tc.desc: Verify that the inet_aton function correctly converts a partial IPv4 address to binary format and stores
57  *           it in the s_addr field of the in_addr struct.
58  * @tc.type: FUNC
59  **/
60 HWTEST_F(ArpaInetTest, inet_aton_002, TestSize.Level1)
61 {
62     in_addr addr;
63     addr.s_addr = 0;
64     int result = inet_aton("127.9.8", &addr);
65     EXPECT_EQ(1, result);
66     EXPECT_EQ((htonl)(0x7f090008), addr.s_addr);
67 }
68 
69 /**
70  * @tc.name: inet_aton_003
71  * @tc.desc: Verify that the inet_aton function correctly converts a partial IPv4 address to binary format and stores
72  *           it in the s_addr field of the in_addr struct.
73  * @tc.type: FUNC
74  **/
75 HWTEST_F(ArpaInetTest, inet_aton_003, TestSize.Level1)
76 {
77     in_addr addr;
78     addr.s_addr = 0;
79     int result = inet_aton("127.9", &addr);
80     EXPECT_EQ(1, result);
81     EXPECT_EQ((htonl)(0x7f000009), addr.s_addr);
82 }
83 
84 /**
85  * @tc.name: inet_aton_004
86  * @tc.desc: Verify that the inet_aton function correctly converts a hexadecimal IPv4 address to binary format and
87  *           stores it in the s_addr field of the in_addr struct.
88  * @tc.type: FUNC
89  **/
90 HWTEST_F(ArpaInetTest, inet_aton_004, TestSize.Level1)
91 {
92     in_addr addr;
93     addr.s_addr = 0;
94     int result = inet_aton("0xFf.0.0.9", &addr);
95     EXPECT_EQ(1, result);
96     EXPECT_EQ((htonl)(0xff000009), addr.s_addr);
97 }
98 
99 /**
100  * @tc.name: inet_aton_005
101  * @tc.desc: Verify that the inet_aton function correctly converts a hexadecimal IPv4 address to binary format and
102  *           stores it in the s_addr field of the in_addr struct.
103  * @tc.type: FUNC
104  **/
105 HWTEST_F(ArpaInetTest, inet_aton_005, TestSize.Level1)
106 {
107     in_addr addr;
108     addr.s_addr = 0;
109     int result = inet_aton("0XfF.0.0.9", &addr);
110     EXPECT_EQ(1, result);
111     EXPECT_EQ((htonl)(0xff000009), addr.s_addr);
112 }
113 
114 /**
115  * @tc.name: inet_aton_006
116  * @tc.desc: Verify that the inet_aton function correctly converts an octal IPv4 address to binary format and stores
117  *           it in the s_addr field of the in_addr struct.
118  * @tc.type: FUNC
119  **/
120 HWTEST_F(ArpaInetTest, inet_aton_006, TestSize.Level1)
121 {
122     in_addr addr;
123     addr.s_addr = 0;
124     int result = inet_aton("0177.0.0.9", &addr);
125     EXPECT_EQ(1, result);
126     EXPECT_EQ((htonl)(0x7f000009), addr.s_addr);
127 }
128 
129 /**
130  * @tc.name: inet_aton_007
131  * @tc.desc: Verify that the inet_aton function correctly converts a decimal integer IPv4 address to binary format
132  *           and stores it in the s_addr field of the in_addr struct.
133  * @tc.type: FUNC
134  **/
135 HWTEST_F(ArpaInetTest, inet_aton_007, TestSize.Level1)
136 {
137     in_addr addr;
138     addr.s_addr = 0;
139     int result = inet_aton("369", &addr);
140     EXPECT_EQ(1, result);
141     EXPECT_EQ((htonl)(369u), addr.s_addr);
142 }
143 
144 /**
145  * @tc.name: inet_aton_008
146  * @tc.desc: Verify that the inet_aton function returns the expected values when given empty or valid IPv4 addresses,
147  *           indicating the correct behavior of converting the addresses from text to binary format.
148  * @tc.type: FUNC
149  **/
150 HWTEST_F(ArpaInetTest, inet_aton_008, TestSize.Level1)
151 {
152     in_addr addr;
153     addr.s_addr = 0;
154     int result1 = inet_aton("", nullptr);
155     EXPECT_EQ(0, result1);
156     int result2 = inet_aton("127.0.0.1", &addr);
157     EXPECT_EQ(1, result2);
158 }
159 
160 /**
161  * @tc.name: inet_aton_009
162  * @tc.desc: Verify that the inet_aton function returns 0 when given invalid IPv4 addresses with invalid characters,
163  *           spaces, or leading zeros, indicating a failure to convert the addresses from text to binary format.
164  * @tc.type: FUNC
165  **/
166 HWTEST_F(ArpaInetTest, inet_aton_009, TestSize.Level1)
167 {
168     in_addr addr;
169     addr.s_addr = 0;
170     int result1 = inet_aton(" ", nullptr);
171     int result2 = inet_aton("a", &addr);
172     int result3 = inet_aton("0ax.0.0.9", &addr);
173     int result4 = inet_aton("127.0.0.1a", &addr);
174     int result5 = inet_aton("09.0.0.9", &addr);
175     EXPECT_EQ(0, result1);
176     EXPECT_EQ(0, result2);
177     EXPECT_EQ(0, result3);
178     EXPECT_EQ(0, result4);
179     EXPECT_EQ(0, result5);
180 }
181 
182 /**
183  * @tc.name: inet_aton_010
184  * @tc.desc: Verify that the inet_aton function returns 0 when given invalid IPv4 addresses with too many octets or
185  *           trailing dots, indicating a failure to convert the addresses from text to binary format.
186  * @tc.type: FUNC
187  **/
188 HWTEST_F(ArpaInetTest, inet_aton_010, TestSize.Level1)
189 {
190     in_addr addr;
191     addr.s_addr = 0;
192     int result1 = inet_aton("9.8.7.6.5", &addr);
193     int result2 = inet_aton("9.8.7.6.", &addr);
194     EXPECT_EQ(0, result1);
195     EXPECT_EQ(0, result2);
196 }
197 
198 /**
199  * @tc.name: inet_aton_011
200  * @tc.desc: Verify that the inet_aton function returns 0 when given invalid IPv4 addresses with octets greater than
201  *           255, indicating a failure to convert the addresses from text to binary format.
202  * @tc.type: FUNC
203  **/
204 HWTEST_F(ArpaInetTest, inet_aton_011, TestSize.Level1)
205 {
206     in_addr addr;
207     addr.s_addr = 0;
208     int result1 = inet_aton("333.2.0.9", &addr);
209     int result2 = inet_aton("0.333.0.9", &addr);
210     int result3 = inet_aton("0.2.333.9", &addr);
211     int result4 = inet_aton("0.2.0.333", &addr);
212     EXPECT_EQ(0, result1);
213     EXPECT_EQ(0, result2);
214     EXPECT_EQ(0, result3);
215     EXPECT_EQ(0, result4);
216 }
217 
218 /**
219  * @tc.name: inet_aton_012
220  * @tc.desc: Verify that the inet_aton function returns 0 when given invalid IPv4 addresses, indicating a failure
221  *           to convert the addresses from text to binary format.
222  * @tc.type: FUNC
223  **/
224 HWTEST_F(ArpaInetTest, inet_aton_012, TestSize.Level1)
225 {
226     in_addr addr;
227     addr.s_addr = 0;
228     int result1 = inet_aton("260.0.0", &addr);
229     int result2 = inet_aton("0.260.0", &addr);
230     int result3 = inet_aton("0.0.0x10000", &addr);
231     EXPECT_EQ(0, result1);
232     EXPECT_EQ(0, result2);
233     EXPECT_EQ(0, result3);
234 }
235 
236 /**
237  * @tc.name: inet_aton_013
238  * @tc.desc: Verify that the inet_aton function returns 0 when given invalid IPv4 addresses, indicating a failure
239  *           to convert the addresses from text to binary format.
240  * @tc.type: FUNC
241  **/
242 HWTEST_F(ArpaInetTest, inet_aton_013, TestSize.Level1)
243 {
244     in_addr addr;
245     addr.s_addr = 0;
246     int result1 = inet_aton("260.0", &addr);
247     int result2 = inet_aton("0.0x1000000", &addr);
248     EXPECT_EQ(0, result1);
249     EXPECT_EQ(0, result2);
250 }
251 
252 /**
253  * @tc.name: inet_aton_014
254  * @tc.desc: Verify that the inet_aton function returns 0 when given an invalid IPv4 address, indicating a failure
255  *           to convert the address from text to binary format.
256  * @tc.type: FUNC
257  **/
258 HWTEST_F(ArpaInetTest, inet_aton_014, TestSize.Level1)
259 {
260     in_addr addr;
261     addr.s_addr = 0;
262     int result = inet_aton("0900.0.0.9", &addr);
263     EXPECT_EQ(0, result);
264 }
265 
266 /**
267  * @tc.name: inet_addr_001
268  * @tc.desc: Ensure that the "inet_addr" function behaves correctly for converting addr valid IPv4 address string into
269  *its binary representation
270  * @tc.type: FUNC
271  **/
272 HWTEST_F(ArpaInetTest, inet_addr_001, TestSize.Level1)
273 {
274     in_addr_t result = inet_addr("127.0.0.1");
275     EXPECT_EQ((htonl)(0x7f000001), result);
276 }
277 
278 /**
279  * @tc.name: inet_ntop_001
280  * @tc.desc: Verify that the inet_pton function correctly converts a string representation of an IPv4 address to its
281  *           binary format and that the inet_ntop function correctly converts a binary format IPv4 address to its
282  *           string representation.
283  * @tc.type: FUNC
284  **/
285 HWTEST_F(ArpaInetTest, inet_ntop_001, TestSize.Level1)
286 {
287     sockaddr_storage ss0;
288     EXPECT_EQ(1, inet_pton(AF_INET, "127.0.0.1", &ss0));
289     char s0[INET_ADDRSTRLEN];
290     memset(&s0, 0, sizeof(s0));
291     const char* result1 = inet_ntop(AF_INET, &ss0, s0, INET_ADDRSTRLEN);
292     const char* result2 = inet_ntop(AF_INET, &ss0, s0, 2 * INET_ADDRSTRLEN);
293     EXPECT_STREQ("127.0.0.1", result1);
294     EXPECT_STREQ("127.0.0.1", result2);
295 }
296 
297 /**
298  * @tc.name: inet_ntop_002
299  * @tc.desc: Verify the correctness and robustness of the inet_ntop function when dealing with IPv6 addresses.
300  * @tc.type: FUNC
301  **/
302 HWTEST_F(ArpaInetTest, inet_ntop_002, TestSize.Level1)
303 {
304     sockaddr_storage ss1;
305     EXPECT_EQ(1, inet_pton(AF_INET6, "::1", &ss1));
306     char s1[INET6_ADDRSTRLEN];
307     memset(&s1, 0, sizeof(s1));
308     const char* result1 = inet_ntop(AF_INET6, &ss1, s1, INET_ADDRSTRLEN);
309     const char* result2 = inet_ntop(AF_INET6, &ss1, s1, INET6_ADDRSTRLEN);
310     const char* result3 = inet_ntop(AF_INET6, &ss1, s1, 2 * INET6_ADDRSTRLEN);
311     EXPECT_STREQ("::1", result1);
312     EXPECT_STREQ("::1", result2);
313     EXPECT_STREQ("::1", result3);
314 }
315 
316 /**
317  * @tc.name: inet_network_001
318  * @tc.desc: Ensure that the "inet_network" function behaves correctly for converting IPv4 addresses in string format
319  *           to their corresponding binary form
320  * @tc.type: FUNC
321  **/
322 HWTEST_F(ArpaInetTest, inet_network_001, TestSize.Level1)
323 {
324     in_addr_t result = inet_network("");
325     EXPECT_EQ(~0U, result);
326 }
327 /**
328  * @tc.name: inet_network_002
329  * @tc.desc: Verify that the inet_network function correctly converts a string representation of
330  *           an IPv4 address to its binary format, and that the converted address matches the expected binary
331  *           representation of the IPv4 address "127.0.0.1".
332  * @tc.type: FUNC
333  **/
334 HWTEST_F(ArpaInetTest, inet_network_002, TestSize.Level1)
335 {
336     in_addr_t result = inet_network("127.0.0.1");
337     EXPECT_EQ(0x7f000001U, result);
338 }
339 /**
340  * @tc.name: inet_network_003
341  * @tc.desc: Verifiy that the inet_network function correctly converts a string representation of an
342  *           IPv4 address in hexadecimal format to its binary format, and that the converted address matches the
343  *           expected binary representation of the hexadecimal address "0x9f".
344  * @tc.type: FUNC
345  **/
346 HWTEST_F(ArpaInetTest, inet_network_003, TestSize.Level1)
347 {
348     in_addr_t result = inet_network("0x9f");
349     EXPECT_EQ(0x9fU, result);
350 }
351 
352 /**
353  * @tc.name: inet_lnaof_001
354  * @tc.desc: Ensure that the "inet_lnaof" function behaves correctly for extracting the host portion from
355  *           an IPv4 address
356  * @tc.type: FUNC
357  **/
358 HWTEST_F(ArpaInetTest, inet_lnaof_001, TestSize.Level1)
359 {
360     in_addr addr = { htonl(0x98765432) };
361     in_addr_t result = inet_lnaof(addr);
362     EXPECT_EQ(0x5432, result);
363 }
364 
365 /**
366  * @tc.name: inet_netof_001
367  * @tc.desc: Ensure that the "inet_netof" function behaves correctly for extracting the network portion from an
368  *           IPv4 address
369  * @tc.type: FUNC
370  **/
371 HWTEST_F(ArpaInetTest, inet_netof_001, TestSize.Level1)
372 {
373     in_addr addr = { htonl(0x98765432) };
374     in_addr_t result = inet_netof(addr);
375     EXPECT_EQ(0x9876, result);
376 }
377