Home | History | Annotate | Download | only in lib6
      1 /*
      2  *
      3  *   Copyright (c) International Business Machines  Corp., 2001
      4  *   Author: David L Stevens
      5  *
      6  *   This program is free software;  you can redistribute it and/or modify
      7  *   it under the terms of the GNU General Public License as published by
      8  *   the Free Software Foundation; either version 2 of the License, or
      9  *   (at your option) any later version.
     10  *
     11  *   This program is distributed in the hope that it will be useful,
     12  *   but WITHOUT ANY WARRANTY;  without even the implied warranty of
     13  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See
     14  *   the GNU General Public License for more details.
     15  *
     16  *   You should have received a copy of the GNU General Public License
     17  *   along with this program;  if not, write to the Free Software Foundation,
     18  *   Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
     19  */
     20 /*
     21  *   Description:
     22  *     Verify that in6 and sockaddr fields are present. Most of these are
     23  *     "PASS" if they just compile.
     24  */
     25 
     26 #include <stdio.h>
     27 #include <unistd.h>
     28 #include <errno.h>
     29 
     30 #include <netinet/in.h>
     31 #include <arpa/inet.h>
     32 
     33 #include "test.h"
     34 
     35 static struct {
     36 	char *addr;
     37 	int ismap;
     38 } maptab[] = {
     39 	{ "2002::1", 0 },
     40 	{ "::ffff:10.0.0.1", 1 },
     41 	{ "::fffe:10.0.0.1", 0 },
     42 	{ "::7fff:10.0.0.1", 0 },
     43 	{ "0:0:0:0:0:0:ffff:0a001", 0 },
     44 	{ "0:0:1:0:0:0:ffff:0a001", 0 },
     45 };
     46 
     47 #define MAPSIZE (sizeof(maptab)/sizeof(maptab[0]))
     48 
     49 static struct {
     50 	char *addr;
     51 } sstab[] = {
     52 	{ "2002::1" },
     53 	{ "10.0.0.1" },
     54 	{ "::ffff:10.0.0.1" },
     55 	{ "::1" },
     56 	{ "::" },
     57 };
     58 
     59 #define SSSIZE (sizeof(sstab)/sizeof(sstab[0]))
     60 
     61 static void setup(void);
     62 static void test_in6_addr(void);
     63 static void test_sockaddr_in6(void);
     64 static void test_global_in6_def(void);
     65 static void test_in6_is_addr_v4mapped(void);
     66 static void test_sockaddr_storage(void);
     67 
     68 static void (*testfunc[])(void) = { test_in6_addr,
     69 	test_sockaddr_in6, test_global_in6_def,
     70 	test_in6_is_addr_v4mapped, test_sockaddr_storage };
     71 
     72 char *TCID = "in6_01";
     73 int TST_TOTAL = ARRAY_SIZE(testfunc);
     74 
     75 int main(int argc, char *argv[])
     76 {
     77 	int lc;
     78 	int i;
     79 
     80 	tst_parse_opts(argc, argv, NULL, NULL);
     81 
     82 	setup();
     83 
     84 	for (lc = 0; TEST_LOOPING(lc); lc++) {
     85 		tst_count = 0;
     86 
     87 		for (i = 0; i < TST_TOTAL; i++)
     88 			(*testfunc[i])();
     89 	}
     90 
     91 	tst_exit();
     92 }
     93 
     94 static void setup(void)
     95 {
     96 	TEST_PAUSE;
     97 }
     98 
     99 /* struct in6_addr tests */
    100 static void test_in6_addr(void)
    101 {
    102 	uint8_t ui8 = 1;
    103 	struct in6_addr in6;
    104 
    105 	in6.s6_addr[0] = ui8;
    106 	tst_resm(TINFO, "type of in6.s6_addr[0] is uint8_t");
    107 	if (sizeof(in6.s6_addr) != 16)
    108 		tst_resm(TFAIL, "sizeof(in6.s6_addr) != 16");
    109 	else
    110 		tst_resm(TPASS, "sizeof(in6.s6_addr) == 16");
    111 }
    112 
    113 /* struct sockaddr_in6 tests */
    114 static void test_sockaddr_in6(void)
    115 {
    116 	uint8_t ui8 = 1;
    117 	uint32_t ui16 = 2;
    118 	uint32_t ui32 = 3;
    119 	struct in6_addr in6;
    120 	struct sockaddr_in6 sin6;
    121 
    122 	in6.s6_addr[0] = ui8;
    123 	sin6.sin6_family = AF_INET6;
    124 	sin6.sin6_port = ui16;
    125 	sin6.sin6_flowinfo = ui32;
    126 	sin6.sin6_addr = in6;
    127 	sin6.sin6_scope_id = ui32;
    128 	tst_resm(TPASS, "all sockaddr_in6 fields present and correct");
    129 }
    130 
    131 /* initializers and global in6 definitions tests */
    132 static void test_global_in6_def(void)
    133 {
    134 	struct in6_addr ina6 = IN6ADDR_ANY_INIT;
    135 	struct in6_addr inl6 = IN6ADDR_LOOPBACK_INIT;
    136 
    137 	tst_resm(TINFO, "IN6ADDR_ANY_INIT present");
    138 	if (memcmp(&ina6, &in6addr_any, sizeof(ina6)) == 0) {
    139 		tst_resm(TINFO, "in6addr_any present and correct");
    140 	} else {
    141 		tst_resm(TFAIL, "in6addr_any incorrect value");
    142 		return;
    143 	}
    144 
    145 	tst_resm(TINFO, "IN6ADDR_LOOPBACK_INIT present");
    146 	if (memcmp(&inl6, &in6addr_loopback, sizeof(inl6)) == 0) {
    147 		tst_resm(TINFO, "in6addr_loopback present and correct");
    148 	} else {
    149 		tst_resm(TFAIL, "in6addr_loopback incorrect value");
    150 		return;
    151 	}
    152 	if (inet_pton(AF_INET6, "::1", &inl6) <= 0)
    153 		tst_brkm(TBROK | TERRNO, NULL, "inet_pton(\"::1\")");
    154 	if (memcmp(&inl6, &in6addr_loopback, sizeof(inl6)) == 0) {
    155 		tst_resm(TINFO, "in6addr_loopback in network byte order");
    156 	} else {
    157 		tst_resm(TFAIL, "in6addr_loopback has wrong byte order");
    158 		return;
    159 	}
    160 
    161 	tst_resm(TPASS, "global in6 definitions tests succeed");
    162 }
    163 
    164 /* IN6_IS_ADDR_V4MAPPED tests */
    165 static void test_in6_is_addr_v4mapped(void)
    166 {
    167 	unsigned int i;
    168 	struct in6_addr in6;
    169 
    170 	for (i = 0; i < MAPSIZE; ++i) {
    171 		if (inet_pton(AF_INET6, maptab[i].addr, &in6) <= 0) {
    172 			tst_brkm(TBROK | TERRNO, NULL,
    173 				"\"%s\" is not a valid IPv6 address",
    174 				maptab[i].addr);
    175 		}
    176 		TEST(IN6_IS_ADDR_V4MAPPED(in6.s6_addr));
    177 		if (TEST_RETURN == maptab[i].ismap) {
    178 			tst_resm(TINFO, "IN6_IS_ADDR_V4MAPPED(\"%s\") %ld",
    179 				maptab[i].addr, TEST_RETURN);
    180 		} else {
    181 			tst_resm(TFAIL, "IN6_IS_ADDR_V4MAPPED(\"%s\") %ld",
    182 				maptab[i].addr, TEST_RETURN);
    183 			return;
    184 		}
    185 	}
    186 
    187 	tst_resm(TPASS, "IN6_IS_ADDR_V4MAPPED tests succeed");
    188 }
    189 
    190 /* sockaddr_storage tests */
    191 static void test_sockaddr_storage(void)
    192 {
    193 	unsigned int i;
    194 	struct sockaddr_storage ss;
    195 
    196 	if (sizeof(ss) <= sizeof(struct sockaddr_in) ||
    197 		sizeof(ss) <= sizeof(struct sockaddr_in6))
    198 		tst_brkm(TBROK, NULL, "sockaddr_storage too small");
    199 
    200 	for (i = 0; i < SSSIZE; ++i) {
    201 		struct sockaddr_in *psin = (struct sockaddr_in *)&ss;
    202 		struct sockaddr_in6 *psin6 = (struct sockaddr_in6 *)&ss;
    203 		int rv;
    204 		uint8_t af;
    205 
    206 		af = psin->sin_family = AF_INET;
    207 		rv = inet_pton(AF_INET, sstab[i].addr, &psin->sin_addr);
    208 		if (rv == 0) {
    209 			af = psin6->sin6_family = AF_INET6;
    210 			rv = inet_pton(AF_INET6, sstab[i].addr,
    211 				&psin6->sin6_addr);
    212 		}
    213 		if (rv <= 0) {
    214 			tst_brkm(TBROK, NULL,
    215 				"\"%s\" is not a valid address", sstab[i].addr);
    216 		}
    217 		if (ss.ss_family == af) {
    218 			tst_resm(TINFO, "\"%s\" is AF_INET%s",
    219 				sstab[i].addr, af == AF_INET ? "" : "6");
    220 		} else {
    221 			tst_resm(TFAIL, "\"%s\" ss_family (%d) != AF_INET%s",
    222 				sstab[i].addr, af, af == AF_INET ? "" : "6");
    223 		}
    224 	}
    225 
    226 	tst_resm(TPASS, "sockaddr_storage tests succeed");
    227 }
    228