• 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 
16 #include <arpa/inet.h>
17 #include "functionalext.h"
18 
19 typedef void (*TEST_FUN)();
20 
21 /*
22  * @tc.name      : inet_ntop_0100
23  * @tc.desc      : Verify that the data format is converted to IP address format (the address cluster is AF_INET).
24  * @tc.level     : Level 0
25  */
inet_ntop_0100(void)26 void inet_ntop_0100(void)
27 {
28     char ip4test[] = "10.10.0.1";
29     struct in_addr sin_addr;
30     inet_pton(AF_INET, ip4test, &sin_addr);
31     const char *ptr = inet_ntop(AF_INET, &sin_addr, ip4test, sizeof(ip4test));
32     EXPECT_PTRNE("inet_ntop_0100", ptr, NULL);
33     EXPECT_STREQ("inet_ntop_0100", ptr, ip4test);
34 }
35 
36 /*
37  * @tc.name      : inet_ntop_0200
38  * @tc.desc      : Verify that the data format is converted to the IP address format
39  *                 (the address cluster is AF_INET6, and the metadata is colon and hexadecimal notation)
40  * @tc.level     : Level 0
41  */
inet_ntop_0200(void)42 void inet_ntop_0200(void)
43 {
44     char ip6test[] = "A157:CD01:3579:1526:DBAC:EF21:4356:7879";
45     struct in_addr sin_addr;
46     inet_pton(AF_INET6, ip6test, &sin_addr);
47     const char *ptr = inet_ntop(AF_INET6, &sin_addr, ip6test, sizeof(ip6test));
48     EXPECT_PTRNE("inet_ntop_0200", ptr, NULL);
49     EXPECT_STREQ("inet_ntop_0200", ptr, ip6test);
50 }
51 
52 /*
53  * @tc.name      : inet_ntop_0300
54  * @tc.desc      : Verify data format is converted to IP address format
55  *                 (address cluster is AF_INET6, metadata is 0-bit compressed notation)
56  * @tc.level     : Level 0
57  */
inet_ntop_0300(void)58 void inet_ntop_0300(void)
59 {
60     char ip6test[] = "fe80::bed5:4695:6cac:bef8";
61     struct in_addr sin_addr;
62     inet_pton(AF_INET6, ip6test, &sin_addr);
63     const char *ptr = inet_ntop(AF_INET6, &sin_addr, ip6test, sizeof(ip6test));
64     EXPECT_PTRNE("inet_ntop_0300", ptr, NULL);
65     EXPECT_STREQ("inet_ntop_0300", ptr, ip6test);
66 }
67 
68 /*
69  * @tc.name      : inet_ntop_0400
70  * @tc.desc      : Verify that the data format is converted to IP address format
71  *                 (the address cluster is AF_INET6, and the metadata is the embedded Ipv address notation)
72  * @tc.level     : Level 0
73  */
inet_ntop_0400(void)74 void inet_ntop_0400(void)
75 {
76     char ip6test[] = "::10.10.0.1";
77     struct in_addr sin_addr;
78     inet_pton(AF_INET6, ip6test, &sin_addr);
79     const char *ptr = inet_ntop(AF_INET6, &sin_addr, ip6test, sizeof(ip6test));
80     EXPECT_PTRNE("inet_ntop_0400", ptr, NULL);
81     EXPECT_EQ("inet_ntop_0400", ptr, ip6test);
82 }
83 
84 /*
85  * @tc.name      : inet_ntop_0500
86  * @tc.desc      : Verify that the data format is converted to IP address format (the address cluster is AF_UNIX)
87  * @tc.level     : Level 2
88  */
inet_ntop_0500(void)89 void inet_ntop_0500(void)
90 {
91     char ip4test[] = "10.10.0.1";
92     struct in_addr sin_addr;
93     inet_pton(AF_UNIX, ip4test, &sin_addr);
94     const char *ptr = inet_ntop(AF_UNIX, &sin_addr, ip4test, sizeof(ip4test));
95     EXPECT_PTREQ("inet_ntop_0500", ptr, NULL);
96 }
97 
98 /*
99  * @tc.name      : inet_ntop_0600
100  * @tc.desc      : Verify that the data format is converted to IP address format (the address cluster is AF_INET6)
101  * @tc.level     : Level 2
102  */
inet_ntop_0600(void)103 void inet_ntop_0600(void)
104 {
105     char ip6test[] = "10.10.0.1";
106     struct in_addr sin_addr;
107     inet_pton(AF_INET6, ip6test, &sin_addr);
108     const char *ptr = inet_ntop(AF_INET6, &sin_addr, ip6test, sizeof(ip6test));
109     EXPECT_PTREQ("inet_ntop_0600", ptr, NULL);
110 }
111 
112 TEST_FUN G_Fun_Array[] = {
113     inet_ntop_0100,
114     inet_ntop_0200,
115     inet_ntop_0300,
116     inet_ntop_0400,
117     inet_ntop_0500,
118     inet_ntop_0600,
119 };
120 
main(int argc,char * argv[])121 int main(int argc, char *argv[])
122 {
123     int num = sizeof(G_Fun_Array) / sizeof(TEST_FUN);
124     for (int pos = 0; pos < num; ++pos) {
125         G_Fun_Array[pos]();
126     }
127 
128     return t_status;
129 }