1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * Copyright (c) International Business Machines Corp., 2001
4 * Copyright (c) 2017 Petr Vorel <pvorel@suse.cz>
5 * Author: David L Stevens
6 *
7 * Verify that in6 and sockaddr fields are present.
8 */
9
10 #include <errno.h>
11 #include <arpa/inet.h>
12 #include <netinet/in.h>
13 #include <sys/socket.h>
14
15 #include "tst_test.h"
16 #include "tst_safe_macros.h"
17
18 static struct {
19 char *addr;
20 int ismap;
21 } maptab[] = {
22 { "2002::1", 0 },
23 { "::ffff:10.0.0.1", 1 },
24 { "::fffe:10.0.0.1", 0 },
25 { "::7fff:10.0.0.1", 0 },
26 { "0:0:0:0:0:0:ffff:a001", 0 },
27 { "0:0:1:0:0:0:ffff:a001", 0 },
28 };
29
30 static struct {
31 char *addr;
32 } sstab[] = {
33 { "2002::1" },
34 { "10.0.0.1" },
35 { "::ffff:10.0.0.1" },
36 { "::1" },
37 { "::" },
38 };
39
40 static void test_in6_addr(void);
41 static void test_sockaddr_in6(void);
42 static void test_global_in6_def(void);
43 static void test_in6_is_addr_v4mapped(void);
44 static void test_sockaddr_storage(void);
45
46 static void (*testfunc[])(void) = { test_in6_addr,
47 test_sockaddr_in6, test_global_in6_def,
48 test_in6_is_addr_v4mapped, test_sockaddr_storage };
49
50 /* struct in6_addr tests */
test_in6_addr(void)51 static void test_in6_addr(void)
52 {
53 uint8_t ui8 = 1;
54 struct in6_addr in6;
55
56 in6.s6_addr[0] = ui8;
57 tst_res(TINFO, "type of in6.s6_addr[0] is uint8_t");
58 if (sizeof(in6.s6_addr) != 16)
59 tst_res(TFAIL, "sizeof(in6.s6_addr) != 16");
60 else
61 tst_res(TPASS, "sizeof(in6.s6_addr) == 16");
62 }
63
64 /* struct sockaddr_in6 tests */
test_sockaddr_in6(void)65 static void test_sockaddr_in6(void)
66 {
67 uint8_t ui8 = 1;
68 uint32_t ui16 = 2;
69 uint32_t ui32 = 3;
70 struct in6_addr in6;
71 struct sockaddr_in6 sin6;
72 int sd;
73
74 in6.s6_addr[0] = ui8;
75 sin6.sin6_family = AF_INET6;
76 sin6.sin6_port = ui16;
77 sin6.sin6_flowinfo = ui32;
78 sin6.sin6_addr = in6;
79 sin6.sin6_scope_id = ui32;
80
81 sd = SAFE_SOCKET(AF_INET6, SOCK_STREAM, 0);
82 bind(sd, (struct sockaddr *)&sin6, sizeof(sin6));
83 SAFE_CLOSE(sd);
84
85 tst_res(TPASS, "all sockaddr_in6 fields present and correct");
86 }
87
88 /* initializers and global in6 definitions tests */
test_global_in6_def(void)89 static void test_global_in6_def(void)
90 {
91 struct in6_addr ina6 = IN6ADDR_ANY_INIT;
92 struct in6_addr inl6 = IN6ADDR_LOOPBACK_INIT;
93
94 tst_res(TINFO, "IN6ADDR_ANY_INIT present");
95 if (memcmp(&ina6, &in6addr_any, sizeof(ina6)) == 0)
96 tst_res(TINFO, "in6addr_any present and correct");
97 else {
98 tst_res(TFAIL, "in6addr_any incorrect value");
99 return;
100 }
101
102 tst_res(TINFO, "IN6ADDR_LOOPBACK_INIT present");
103 if (memcmp(&inl6, &in6addr_loopback, sizeof(inl6)) == 0)
104 tst_res(TINFO, "in6addr_loopback present and correct");
105 else {
106 tst_res(TFAIL, "in6addr_loopback incorrect value");
107 return;
108 }
109
110 if (inet_pton(AF_INET6, "::1", &inl6) <= 0)
111 tst_brk(TBROK | TERRNO, "inet_pton(\"::1\")");
112
113 if (memcmp(&inl6, &in6addr_loopback, sizeof(inl6)) == 0)
114 tst_res(TINFO, "in6addr_loopback in network byte order");
115 else {
116 tst_res(TFAIL, "in6addr_loopback has wrong byte order");
117 return;
118 }
119
120 tst_res(TPASS, "global in6 definitions tests succeed");
121 }
122
123 /* IN6_IS_ADDR_V4MAPPED tests */
test_in6_is_addr_v4mapped(void)124 static void test_in6_is_addr_v4mapped(void)
125 {
126 unsigned int i;
127 struct in6_addr in6;
128
129 for (i = 0; i < ARRAY_SIZE(maptab); ++i) {
130 if (inet_pton(AF_INET6, maptab[i].addr, &in6) <= 0)
131 tst_brk(TBROK | TERRNO,
132 "\"%s\" is not a valid IPv6 address",
133 maptab[i].addr);
134 TEST(IN6_IS_ADDR_V4MAPPED(in6.s6_addr));
135 if (maptab[i].ismap == TST_RET)
136 tst_res(TINFO, "IN6_IS_ADDR_V4MAPPED(\"%s\") %ld",
137 maptab[i].addr, TST_RET);
138 else {
139 tst_res(TFAIL, "IN6_IS_ADDR_V4MAPPED(\"%s\") %ld",
140 maptab[i].addr, TST_RET);
141 return;
142 }
143 }
144
145 tst_res(TPASS, "IN6_IS_ADDR_V4MAPPED tests succeed");
146 }
147
148 /* sockaddr_storage tests */
test_sockaddr_storage(void)149 static void test_sockaddr_storage(void)
150 {
151 unsigned int i;
152 struct sockaddr_storage ss;
153
154 if (sizeof(ss) <= sizeof(struct sockaddr_in) ||
155 sizeof(ss) <= sizeof(struct sockaddr_in6))
156 tst_brk(TBROK, "sockaddr_storage too small");
157
158 for (i = 0; i < ARRAY_SIZE(sstab); ++i) {
159 struct sockaddr_in *psin = (struct sockaddr_in *)&ss;
160 struct sockaddr_in6 *psin6 = (struct sockaddr_in6 *)&ss;
161 int rv;
162 uint8_t af;
163
164 af = psin->sin_family = AF_INET;
165 rv = inet_pton(AF_INET, sstab[i].addr, &psin->sin_addr);
166 if (rv == 0) {
167 af = psin6->sin6_family = AF_INET6;
168 rv = inet_pton(AF_INET6, sstab[i].addr,
169 &psin6->sin6_addr);
170 }
171 if (rv <= 0)
172 tst_brk(TBROK,
173 "\"%s\" is not a valid address", sstab[i].addr);
174 if (ss.ss_family == af)
175 tst_res(TINFO, "\"%s\" is AF_INET%s",
176 sstab[i].addr, af == AF_INET ? "" : "6");
177 else
178 tst_res(TFAIL, "\"%s\" ss_family (%d) != AF_INET%s",
179 sstab[i].addr, af, af == AF_INET ? "" : "6");
180 }
181
182 tst_res(TPASS, "sockaddr_storage tests succeed");
183 }
184
do_test(unsigned int i)185 static void do_test(unsigned int i)
186 {
187 testfunc[i]();
188 }
189
190 static struct tst_test test = {
191 .tcnt = ARRAY_SIZE(testfunc),
192 .test = do_test,
193 };
194