blob: d7a499b9f395343142df291e7686aa0b80698221 [file] [log] [blame]
mridge7c0f5e52005-01-10 23:12:45 +00001/*
2 *
3 * Copyright (c) International Business Machines Corp., 2001
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
13 * the GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
Wanlong Gao4548c6c2012-10-19 18:03:36 +080017 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
mridge7c0f5e52005-01-10 23:12:45 +000018 */
19
20/*
21 * Test Name: in6_01
22 *
23 * Test Description:
24 * Verify that in6 and sockaddr fields are present. Most of these are
25 * "PASS" if they just compile.
26 *
27 * Usage: <for command-line>
28 * in6_01
29 *
30 * HISTORY
31 * 05/2004 written by David L Stevens
32 *
33 * RESTRICTIONS:
34 * None.
35 *
36 */
37
38#include <stdio.h>
39#include <unistd.h>
40#include <errno.h>
41
42#include <netinet/in.h>
subrata_modakaa588ae2008-09-18 13:18:36 +000043#include <arpa/inet.h>
mridge7c0f5e52005-01-10 23:12:45 +000044
45#include "test.h"
mridge7c0f5e52005-01-10 23:12:45 +000046
Wanlong Gao354ebb42012-12-07 10:10:04 +080047char *TCID = "in6_01"; /* Test program identifier. */
mridge7c0f5e52005-01-10 23:12:45 +000048
49void setup(void), cleanup(void);
50
51struct {
Wanlong Gao354ebb42012-12-07 10:10:04 +080052 char *addr;
53 int ismap;
mridge7c0f5e52005-01-10 23:12:45 +000054} maptab[] = {
Wanlong Gao354ebb42012-12-07 10:10:04 +080055 {
56 "2002::1", 0}, {
57 "::ffff:10.0.0.1", 1}, {
58 "::fffe:10.0.0.1", 0}, {
59 "::7fff:10.0.0.1", 0}, {
60 "0:0:0:0:0:0:ffff:0a001", 1}, {
61"0:0:1:0:0:0:ffff:0a001", 0},};
mridge7c0f5e52005-01-10 23:12:45 +000062
63#define MAPSIZE (sizeof(maptab)/sizeof(maptab[0]))
64
65struct {
66 char *addr;
67} sstab[] = {
Wanlong Gao354ebb42012-12-07 10:10:04 +080068 {
69 "2002::1"}, {
70 "10.0.0.1"}, {
71 "::ffff:10.0.0.1"}, {
72 "::1"}, {
73"::"},};
mridge7c0f5e52005-01-10 23:12:45 +000074
75#define SSSIZE (sizeof(sstab)/sizeof(sstab[0]))
76
77int TST_TOTAL = 9 + MAPSIZE + SSSIZE;
78
Wanlong Gao354ebb42012-12-07 10:10:04 +080079int main(int argc, char *argv[])
mridge7c0f5e52005-01-10 23:12:45 +000080{
Wanlong Gao354ebb42012-12-07 10:10:04 +080081 uint8_t ui8 = 1;
mridge7c0f5e52005-01-10 23:12:45 +000082 uint32_t ui16 = 2;
83 uint32_t ui32 = 3;
Wanlong Gao354ebb42012-12-07 10:10:04 +080084 struct in6_addr in6;
mridge7c0f5e52005-01-10 23:12:45 +000085 struct in6_addr ina6 = IN6ADDR_ANY_INIT;
86 struct in6_addr inl6 = IN6ADDR_LOOPBACK_INIT;
87 struct sockaddr_in6 sin6;
88 struct sockaddr_storage ss;
89 int i;
Cyril Hrubis0b9589f2014-05-27 17:40:33 +020090 const char *msg;
mridge7c0f5e52005-01-10 23:12:45 +000091
92 /* Parse standard options given to run the test. */
Garrett Cooperdf3eb162010-11-28 22:44:32 -080093 msg = parse_opts(argc, argv, NULL, NULL);
94 if (msg != NULL) {
Garrett Cooper53740502010-12-16 00:04:01 -080095 tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg);
mridge7c0f5e52005-01-10 23:12:45 +000096 }
97
98 setup();
99
100 /* struct in6_addr tests */
101 in6.s6_addr[0] = ui8;
102 tst_resm(TPASS, "type of in6.s6_addr[0] is uint8_t");
103 if (sizeof(in6.s6_addr) != 16)
104 tst_resm(TFAIL, "sizeof(in6.s6_addr) != 16");
105 else
106 tst_resm(TPASS, "sizeof(in6.s6_addr) == 16");
107
108 /* struct sockaddr_in6 tests */
109 sin6.sin6_family = AF_INET6;
110 sin6.sin6_port = ui16;
111 sin6.sin6_flowinfo = ui32;
112 sin6.sin6_addr = in6;
113 sin6.sin6_scope_id = ui32;
114 tst_resm(TPASS, "all sockaddr_in6 fields present and correct");
115
116 /* initializers and global in6 definitions tests */
117 tst_resm(TPASS, "IN6ADDR_ANY_INIT present");
118 if (memcmp(&ina6, &in6addr_any, sizeof(ina6)) == 0)
119 tst_resm(TPASS, "in6addr_any present and correct");
120 else
121 tst_resm(TFAIL, "in6addr_any incorrect value");
122 tst_resm(TPASS, "IN6ADDR_LOOPBACK_INIT present");
123 if (memcmp(&inl6, &in6addr_loopback, sizeof(inl6)) == 0)
124 tst_resm(TPASS, "in6addr_loopback present and correct");
125 else
126 tst_resm(TFAIL, "in6addr_loopback incorrect value");
127 if (inet_pton(AF_INET6, "::1", &inl6) <= 0)
128 tst_resm(TBROK, "inet_pton(\"::1\")");
129 else if (memcmp(&inl6, &in6addr_loopback, sizeof(inl6)) == 0)
130 tst_resm(TPASS, "in6addr_loopback in network byte order");
131 else
132 tst_resm(TFAIL, "in6addr_loopback has wrong byte order");
133
134 /* IN6_IS_ADDR_V4MAPPED tests */
135
Wanlong Gao354ebb42012-12-07 10:10:04 +0800136 for (i = 0; i < MAPSIZE; ++i) {
mridge7c0f5e52005-01-10 23:12:45 +0000137 if (inet_pton(AF_INET6, maptab[i].addr, &in6) <= 0) {
138 tst_resm(TBROK, "\"%s\" is not a valid IPv6 address",
Wanlong Gao354ebb42012-12-07 10:10:04 +0800139 maptab[i].addr);
mridge7c0f5e52005-01-10 23:12:45 +0000140 continue;
141 }
142 TEST(IN6_IS_ADDR_V4MAPPED(in6.s6_addr));
143 if (TEST_RETURN == maptab[i].ismap)
Wanlong Gao354ebb42012-12-07 10:10:04 +0800144 tst_resm(TEST_RETURN == maptab[i].ismap ? TPASS : TFAIL,
145 "IN6_IS_ADDR_V4MAPPED(\"%s\") %ld",
146 maptab[i].addr, TEST_RETURN);
mridge7c0f5e52005-01-10 23:12:45 +0000147 }
148
149 /* sockaddr_storage tests */
150
151 if (sizeof(ss) <= sizeof(struct sockaddr_in) ||
152 sizeof(ss) <= sizeof(struct sockaddr_in6))
153 tst_resm(TFAIL, "sockaddr_storage too small");
154 else
155 tst_resm(TPASS, "sockaddr_storage size ok");
Wanlong Gao354ebb42012-12-07 10:10:04 +0800156 for (i = 0; i < SSSIZE; ++i) {
mridge7c0f5e52005-01-10 23:12:45 +0000157 struct sockaddr_in *psin = (struct sockaddr_in *)&ss;
158 struct sockaddr_in6 *psin6 = (struct sockaddr_in6 *)&ss;
159 int rv;
160 uint8_t af;
161
162 af = psin->sin_family = AF_INET;
163 rv = inet_pton(AF_INET, sstab[i].addr, &psin->sin_addr);
164 if (rv == 0) {
165 af = psin6->sin6_family = AF_INET6;
166 rv = inet_pton(AF_INET6, sstab[i].addr,
Wanlong Gao354ebb42012-12-07 10:10:04 +0800167 &psin6->sin6_addr);
mridge7c0f5e52005-01-10 23:12:45 +0000168 }
169 if (rv <= 0) {
170 tst_resm(TBROK, "\"%s\" is not a valid address",
Wanlong Gao354ebb42012-12-07 10:10:04 +0800171 sstab[i].addr);
mridge7c0f5e52005-01-10 23:12:45 +0000172 continue;
173 }
174 if (ss.ss_family == af)
175 tst_resm(TPASS, "\"%s\" is AF_INET%s",
Wanlong Gao354ebb42012-12-07 10:10:04 +0800176 sstab[i].addr, af == AF_INET ? "" : "6");
mridge7c0f5e52005-01-10 23:12:45 +0000177 else
178 tst_resm(TFAIL, "\"%s\" ss_family (%d) != AF_INET%s",
Wanlong Gao354ebb42012-12-07 10:10:04 +0800179 sstab[i].addr, af, af == AF_INET ? "" : "6");
mridge7c0f5e52005-01-10 23:12:45 +0000180 }
181
182 cleanup();
Garrett Cooper2c282152010-12-16 00:55:50 -0800183
Wanlong Gao354ebb42012-12-07 10:10:04 +0800184 return (0);
Garrett Cooper2c282152010-12-16 00:55:50 -0800185}
mridge7c0f5e52005-01-10 23:12:45 +0000186
187pid_t pid;
188
Wanlong Gao354ebb42012-12-07 10:10:04 +0800189void setup(void)
mridge7c0f5e52005-01-10 23:12:45 +0000190{
Wanlong Gao354ebb42012-12-07 10:10:04 +0800191 TEST_PAUSE; /* if -P option specified */
mridge7c0f5e52005-01-10 23:12:45 +0000192}
193
Wanlong Gao354ebb42012-12-07 10:10:04 +0800194void cleanup(void)
mridge7c0f5e52005-01-10 23:12:45 +0000195{
Chris Dearmanec6edca2012-10-17 19:54:01 -0700196}