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
20 FILE_LICENCE ( GPL2_OR_LATER );
28 /* Forcibly enable assertions */
34 #include <ipxe/ipv6.h>
35 #include <ipxe/test.h>
37 /** Define inline IPv6 address */
38 #define IPV6(...) { __VA_ARGS__ }
40 /** The unspecified IPv6 address */
41 static const struct in6_addr sample_unspecified = {
42 .s6_addr = IPV6 ( 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
43 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 ),
46 /** A sample link-local IPv6 address */
47 static const struct in6_addr sample_link_local = {
48 .s6_addr = IPV6 ( 0xfe, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
49 0x00, 0x00, 0x69, 0xff, 0xfe, 0x50, 0x58, 0x45 ),
52 /** A sample global IPv6 address */
53 static const struct in6_addr sample_global = {
54 .s6_addr = IPV6 ( 0x20, 0x01, 0x0b, 0xa8, 0x00, 0x00, 0x01, 0xd4,
55 0x00, 0x00, 0x00, 0x00, 0x69, 0x50, 0x58, 0x45 ),
58 /** A sample multicast IPv6 address */
59 static const struct in6_addr sample_multicast = {
60 .s6_addr = IPV6 ( 0xff, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
61 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01 ),
65 * Report an inet6_ntoa() test result
67 * @v addr IPv6 address
68 * @v text Expected textual representation
70 #define inet6_ntoa_ok( addr, text ) do { \
71 static const struct in6_addr in = { \
74 static const char expected[] = text; \
77 actual = inet6_ntoa ( &in ); \
78 DBG ( "inet6_ntoa ( %04x:%04x:%04x:%04x:%04x:%04x:%04x:%04x ) " \
79 "= %s\n", ntohs ( in.s6_addr16[0] ), \
80 ntohs ( in.s6_addr16[1] ), ntohs ( in.s6_addr16[2] ), \
81 ntohs ( in.s6_addr16[3] ), ntohs ( in.s6_addr16[4] ), \
82 ntohs ( in.s6_addr16[5] ), ntohs ( in.s6_addr16[6] ), \
83 ntohs ( in.s6_addr16[7] ), actual ); \
84 ok ( strcmp ( actual, expected ) == 0 ); \
88 * Report an inet6_aton() test result
90 * @v text Textual representation
91 * @v addr Expected IPv6 address
93 #define inet6_aton_ok( text, addr ) do { \
94 static const char string[] = text; \
95 static const struct in6_addr expected = { \
98 struct in6_addr actual; \
100 ok ( inet6_aton ( string, &actual ) == 0 ); \
101 DBG ( "inet6_aton ( \"%s\" ) = %s\n", string, \
102 inet6_ntoa ( &actual ) ); \
103 ok ( memcmp ( &actual, &expected, sizeof ( actual ) ) == 0 ); \
107 * Report an inet6_aton() failure test result
109 * @v text Textual representation
111 #define inet6_aton_fail_ok( text ) do { \
112 static const char string[] = text; \
113 struct in6_addr dummy; \
115 ok ( inet6_aton ( string, &dummy ) != 0 ); \
119 * Perform IPv6 self-tests
122 static void ipv6_test_exec ( void ) {
124 /* Address testing macros */
125 ok ( IN6_IS_ADDR_UNSPECIFIED ( &sample_unspecified ) );
126 ok ( ! IN6_IS_ADDR_UNSPECIFIED ( &sample_link_local ) );
127 ok ( ! IN6_IS_ADDR_UNSPECIFIED ( &sample_global ) );
128 ok ( ! IN6_IS_ADDR_UNSPECIFIED ( &sample_multicast ) );
129 ok ( ! IN6_IS_ADDR_MULTICAST ( &sample_unspecified ) );
130 ok ( ! IN6_IS_ADDR_MULTICAST ( &sample_link_local ) );
131 ok ( ! IN6_IS_ADDR_MULTICAST ( &sample_global ) );
132 ok ( IN6_IS_ADDR_MULTICAST ( &sample_multicast ) );
133 ok ( ! IN6_IS_ADDR_LINKLOCAL ( &sample_unspecified ) );
134 ok ( IN6_IS_ADDR_LINKLOCAL ( &sample_link_local ) );
135 ok ( ! IN6_IS_ADDR_LINKLOCAL ( &sample_global ) );
136 ok ( ! IN6_IS_ADDR_LINKLOCAL ( &sample_multicast ) );
138 /* inet6_ntoa() tests */
139 inet6_ntoa_ok ( IPV6 ( 0x20, 0x01, 0x0b, 0xa8, 0x00, 0x00, 0x01, 0xd4,
140 0x00, 0x00, 0x00, 0x00, 0x69, 0x50, 0x58, 0x45 ),
141 "2001:ba8:0:1d4::6950:5845" );
143 inet6_ntoa_ok ( IPV6 ( 0x20, 0x01, 0x0d, 0xb8, 0x00, 0x01, 0x00, 0x01,
144 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01 ),
145 "2001:db8:1:1:1:1:1:1" );
147 inet6_ntoa_ok ( IPV6 ( 0x20, 0x01, 0x0d, 0xb8, 0x00, 0x00, 0x00, 0x00,
148 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01 ),
150 /* No "::" for single zero */
151 inet6_ntoa_ok ( IPV6 ( 0x20, 0x01, 0x0d, 0xb8, 0x00, 0x00, 0x00, 0x01,
152 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01 ),
153 "2001:db8:0:1:1:1:1:1" );
154 /* Use "::" for longest run of zeros */
155 inet6_ntoa_ok ( IPV6 ( 0x20, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01,
156 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01 ),
158 /* Use "::" for leftmost equal-length run of zeros */
159 inet6_ntoa_ok ( IPV6 ( 0x20, 0x01, 0x0d, 0xb8, 0x00, 0x00, 0x00, 0x00,
160 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01 ),
161 "2001:db8::1:0:0:1" );
162 /* Trailing run of zeros */
163 inet6_ntoa_ok ( IPV6 ( 0xfe, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
164 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 ),
166 /* Leading run of zeros */
167 inet6_ntoa_ok ( IPV6 ( 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
168 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01 ),
171 inet6_ntoa_ok ( IPV6 ( 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
172 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 ),
175 inet6_ntoa_ok ( IPV6 ( 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
176 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff ),
177 "ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff" );
179 /* inet6_aton() tests */
180 inet6_aton_ok ( "2001:ba8:0:1d4::6950:5845",
181 IPV6 ( 0x20, 0x01, 0x0b, 0xa8, 0x00, 0x00, 0x01, 0xd4,
182 0x00, 0x00, 0x00, 0x00, 0x69, 0x50, 0x58, 0x45));
184 inet6_aton_ok ( "2001:db8:1:1:1:1:1:1",
185 IPV6 ( 0x20, 0x01, 0x0d, 0xb8, 0x00, 0x01, 0x00, 0x01,
186 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01));
187 /* All intervening zeros */
188 inet6_aton_ok ( "fe80::1",
189 IPV6 ( 0xfe, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
190 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01));
191 /* Trailing run of zeros */
192 inet6_aton_ok ( "fe80::",
193 IPV6 ( 0xfe, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
194 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00));
195 /* Leading run of zeros */
196 inet6_aton_ok ( "::1",
197 IPV6 ( 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
198 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01));
200 inet6_aton_ok ( "::",
201 IPV6 ( 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
202 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00));
204 /* inet6_aton() failure tests */
205 inet6_aton_fail_ok ( "20012:ba8:0:1d4::6950:5845" );
206 inet6_aton_fail_ok ( "200z:ba8:0:1d4::6950:5845" );
207 inet6_aton_fail_ok ( "2001.ba8:0:1d4::6950:5845" );
208 inet6_aton_fail_ok ( "2001:db8:1:1:1:1:1" );
209 inet6_aton_fail_ok ( "2001:db8:1:1:1:1:1:1:2" );
210 inet6_aton_fail_ok ( "2001:db8::1::2" );
211 inet6_aton_fail_ok ( "2001:ba8:0:1d4:::6950:5845" );
212 inet6_aton_fail_ok ( ":::" );
215 /** IPv6 self-test */
216 struct self_test ipv6_test __self_test = {
218 .exec = ipv6_test_exec,