2 * Copyright (C) 2013 Michael Brown <mbrown@fensystems.co.uk>.
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License as
6 * published by the Free Software Foundation; either version 2 of the
7 * License, or (at your option) any later version.
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19 * You can also choose to distribute this program under the terms of
20 * the Unmodified Binary Distribution Licence (as given in the file
21 * COPYING.UBDL), provided that you have satisfied its requirements.
24 FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
32 /* Forcibly enable assertions */
38 #include <ipxe/ipv6.h>
39 #include <ipxe/test.h>
41 /** Define inline IPv6 address */
42 #define IPV6(...) { __VA_ARGS__ }
44 /** The unspecified IPv6 address */
45 static const struct in6_addr sample_unspecified = {
46 .s6_addr = IPV6 ( 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
47 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 ),
50 /** A sample link-local IPv6 address */
51 static const struct in6_addr sample_link_local = {
52 .s6_addr = IPV6 ( 0xfe, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
53 0x00, 0x00, 0x69, 0xff, 0xfe, 0x50, 0x58, 0x45 ),
56 /** A sample global IPv6 address */
57 static const struct in6_addr sample_global = {
58 .s6_addr = IPV6 ( 0x20, 0x01, 0x0b, 0xa8, 0x00, 0x00, 0x01, 0xd4,
59 0x00, 0x00, 0x00, 0x00, 0x69, 0x50, 0x58, 0x45 ),
62 /** A sample multicast IPv6 address */
63 static const struct in6_addr sample_multicast = {
64 .s6_addr = IPV6 ( 0xff, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
65 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01 ),
69 * Report an inet6_ntoa() test result
71 * @v addr IPv6 address
72 * @v text Expected textual representation
74 #define inet6_ntoa_ok( addr, text ) do { \
75 static const struct in6_addr in = { \
78 static const char expected[] = text; \
81 actual = inet6_ntoa ( &in ); \
82 DBG ( "inet6_ntoa ( %04x:%04x:%04x:%04x:%04x:%04x:%04x:%04x ) " \
83 "= %s\n", ntohs ( in.s6_addr16[0] ), \
84 ntohs ( in.s6_addr16[1] ), ntohs ( in.s6_addr16[2] ), \
85 ntohs ( in.s6_addr16[3] ), ntohs ( in.s6_addr16[4] ), \
86 ntohs ( in.s6_addr16[5] ), ntohs ( in.s6_addr16[6] ), \
87 ntohs ( in.s6_addr16[7] ), actual ); \
88 ok ( strcmp ( actual, expected ) == 0 ); \
92 * Report an inet6_aton() test result
94 * @v text Textual representation
95 * @v addr Expected IPv6 address
97 #define inet6_aton_ok( text, addr ) do { \
98 static const char string[] = text; \
99 static const struct in6_addr expected = { \
102 struct in6_addr actual; \
104 ok ( inet6_aton ( string, &actual ) == 0 ); \
105 DBG ( "inet6_aton ( \"%s\" ) = %s\n", string, \
106 inet6_ntoa ( &actual ) ); \
107 ok ( memcmp ( &actual, &expected, sizeof ( actual ) ) == 0 ); \
111 * Report an inet6_aton() failure test result
113 * @v text Textual representation
115 #define inet6_aton_fail_ok( text ) do { \
116 static const char string[] = text; \
117 struct in6_addr dummy; \
119 ok ( inet6_aton ( string, &dummy ) != 0 ); \
123 * Perform IPv6 self-tests
126 static void ipv6_test_exec ( void ) {
128 /* Address testing macros */
129 ok ( IN6_IS_ADDR_UNSPECIFIED ( &sample_unspecified ) );
130 ok ( ! IN6_IS_ADDR_UNSPECIFIED ( &sample_link_local ) );
131 ok ( ! IN6_IS_ADDR_UNSPECIFIED ( &sample_global ) );
132 ok ( ! IN6_IS_ADDR_UNSPECIFIED ( &sample_multicast ) );
133 ok ( ! IN6_IS_ADDR_MULTICAST ( &sample_unspecified ) );
134 ok ( ! IN6_IS_ADDR_MULTICAST ( &sample_link_local ) );
135 ok ( ! IN6_IS_ADDR_MULTICAST ( &sample_global ) );
136 ok ( IN6_IS_ADDR_MULTICAST ( &sample_multicast ) );
137 ok ( ! IN6_IS_ADDR_LINKLOCAL ( &sample_unspecified ) );
138 ok ( IN6_IS_ADDR_LINKLOCAL ( &sample_link_local ) );
139 ok ( ! IN6_IS_ADDR_LINKLOCAL ( &sample_global ) );
140 ok ( ! IN6_IS_ADDR_LINKLOCAL ( &sample_multicast ) );
142 /* inet6_ntoa() tests */
143 inet6_ntoa_ok ( IPV6 ( 0x20, 0x01, 0x0b, 0xa8, 0x00, 0x00, 0x01, 0xd4,
144 0x00, 0x00, 0x00, 0x00, 0x69, 0x50, 0x58, 0x45 ),
145 "2001:ba8:0:1d4::6950:5845" );
147 inet6_ntoa_ok ( IPV6 ( 0x20, 0x01, 0x0d, 0xb8, 0x00, 0x01, 0x00, 0x01,
148 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01 ),
149 "2001:db8:1:1:1:1:1:1" );
151 inet6_ntoa_ok ( IPV6 ( 0x20, 0x01, 0x0d, 0xb8, 0x00, 0x00, 0x00, 0x00,
152 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01 ),
154 /* No "::" for single zero */
155 inet6_ntoa_ok ( IPV6 ( 0x20, 0x01, 0x0d, 0xb8, 0x00, 0x00, 0x00, 0x01,
156 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01 ),
157 "2001:db8:0:1:1:1:1:1" );
158 /* Use "::" for longest run of zeros */
159 inet6_ntoa_ok ( IPV6 ( 0x20, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01,
160 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01 ),
162 /* Use "::" for leftmost equal-length run of zeros */
163 inet6_ntoa_ok ( IPV6 ( 0x20, 0x01, 0x0d, 0xb8, 0x00, 0x00, 0x00, 0x00,
164 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01 ),
165 "2001:db8::1:0:0:1" );
166 /* Trailing run of zeros */
167 inet6_ntoa_ok ( IPV6 ( 0xfe, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
168 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 ),
170 /* Leading run of zeros */
171 inet6_ntoa_ok ( IPV6 ( 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
172 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01 ),
175 inet6_ntoa_ok ( IPV6 ( 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
176 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 ),
179 inet6_ntoa_ok ( IPV6 ( 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
180 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff ),
181 "ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff" );
183 /* inet6_aton() tests */
184 inet6_aton_ok ( "2001:ba8:0:1d4::6950:5845",
185 IPV6 ( 0x20, 0x01, 0x0b, 0xa8, 0x00, 0x00, 0x01, 0xd4,
186 0x00, 0x00, 0x00, 0x00, 0x69, 0x50, 0x58, 0x45));
188 inet6_aton_ok ( "2001:db8:1:1:1:1:1:1",
189 IPV6 ( 0x20, 0x01, 0x0d, 0xb8, 0x00, 0x01, 0x00, 0x01,
190 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01));
191 /* All intervening zeros */
192 inet6_aton_ok ( "fe80::1",
193 IPV6 ( 0xfe, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
194 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01));
195 /* Trailing run of zeros */
196 inet6_aton_ok ( "fe80::",
197 IPV6 ( 0xfe, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
198 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00));
199 /* Leading run of zeros */
200 inet6_aton_ok ( "::1",
201 IPV6 ( 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
202 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01));
204 inet6_aton_ok ( "::",
205 IPV6 ( 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
206 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00));
208 /* inet6_aton() failure tests */
209 inet6_aton_fail_ok ( "20012:ba8:0:1d4::6950:5845" );
210 inet6_aton_fail_ok ( "200z:ba8:0:1d4::6950:5845" );
211 inet6_aton_fail_ok ( "2001.ba8:0:1d4::6950:5845" );
212 inet6_aton_fail_ok ( "2001:db8:1:1:1:1:1" );
213 inet6_aton_fail_ok ( "2001:db8:1:1:1:1:1:1:2" );
214 inet6_aton_fail_ok ( "2001:db8::1::2" );
215 inet6_aton_fail_ok ( "2001:ba8:0:1d4:::6950:5845" );
216 inet6_aton_fail_ok ( ":::" );
219 /** IPv6 self-test */
220 struct self_test ipv6_test __self_test = {
222 .exec = ipv6_test_exec,