Add qemu 2.4.0
[kvmfornfv.git] / qemu / roms / ipxe / src / tests / ipv6_test.c
1 /*
2  * Copyright (C) 2013 Michael Brown <mbrown@fensystems.co.uk>.
3  *
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.
8  *
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.
13  *
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
17  * 02110-1301, USA.
18  */
19
20 FILE_LICENCE ( GPL2_OR_LATER );
21
22 /** @file
23  *
24  * IPv6 tests
25  *
26  */
27
28 /* Forcibly enable assertions */
29 #undef NDEBUG
30
31 #include <stdint.h>
32 #include <string.h>
33 #include <byteswap.h>
34 #include <ipxe/ipv6.h>
35 #include <ipxe/test.h>
36
37 /** Define inline IPv6 address */
38 #define IPV6(...) { __VA_ARGS__ }
39
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 ),
44 };
45
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 ),
50 };
51
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 ),
56 };
57
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 ),
62 };
63
64 /**
65  * Report an inet6_ntoa() test result
66  *
67  * @v addr              IPv6 address
68  * @v text              Expected textual representation
69  */
70 #define inet6_ntoa_ok( addr, text ) do {                                \
71         static const struct in6_addr in = {                             \
72                 .s6_addr = addr,                                        \
73         };                                                              \
74         static const char expected[] = text;                            \
75         char *actual;                                                   \
76                                                                         \
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 );                        \
85         } while ( 0 )
86
87 /**
88  * Report an inet6_aton() test result
89  *
90  * @v text              Textual representation
91  * @v addr              Expected IPv6 address
92  */
93 #define inet6_aton_ok( text, addr ) do {                                \
94         static const char string[] = text;                              \
95         static const struct in6_addr expected = {                       \
96                 .s6_addr = addr,                                        \
97         };                                                              \
98         struct in6_addr actual;                                         \
99                                                                         \
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 );   \
104         } while ( 0 )
105
106 /**
107  * Report an inet6_aton() failure test result
108  *
109  * @v text              Textual representation
110  */
111 #define inet6_aton_fail_ok( text ) do {                                 \
112         static const char string[] = text;                              \
113         struct in6_addr dummy;                                          \
114                                                                         \
115         ok ( inet6_aton ( string, &dummy ) != 0 );                      \
116         } while ( 0 )
117
118 /**
119  * Perform IPv6 self-tests
120  *
121  */
122 static void ipv6_test_exec ( void ) {
123
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 ) );
137
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" );
142         /* No zeros */
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" );
146         /* Run of zeros */
147         inet6_ntoa_ok ( IPV6 ( 0x20, 0x01, 0x0d, 0xb8, 0x00, 0x00, 0x00, 0x00,
148                                0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01 ),
149                         "2001:db8::1" );
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 ),
157                         "2001:0:0:1::1" );
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 ),
165                         "fe80::" );
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 ),
169                         "::1" );
170         /* All zeros */
171         inet6_ntoa_ok ( IPV6 ( 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
172                                0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 ),
173                         "::" );
174         /* Maximum length */
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" );
178
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));
183         /* No zeros */
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));
199         /* All zeros */
200         inet6_aton_ok ( "::",
201                         IPV6 ( 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
202                                0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00));
203
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 ( ":::" );
213 }
214
215 /** IPv6 self-test */
216 struct self_test ipv6_test __self_test = {
217         .name = "ipv6",
218         .exec = ipv6_test_exec,
219 };