These changes are the raw update to qemu-2.6.
[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  * 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.
22  */
23
24 FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
25
26 /** @file
27  *
28  * IPv6 tests
29  *
30  */
31
32 /* Forcibly enable assertions */
33 #undef NDEBUG
34
35 #include <stdint.h>
36 #include <string.h>
37 #include <byteswap.h>
38 #include <ipxe/ipv6.h>
39 #include <ipxe/test.h>
40
41 /** Define inline IPv6 address */
42 #define IPV6(...) { __VA_ARGS__ }
43
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 ),
48 };
49
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 ),
54 };
55
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 ),
60 };
61
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 ),
66 };
67
68 /**
69  * Report an inet6_ntoa() test result
70  *
71  * @v addr              IPv6 address
72  * @v text              Expected textual representation
73  */
74 #define inet6_ntoa_ok( addr, text ) do {                                \
75         static const struct in6_addr in = {                             \
76                 .s6_addr = addr,                                        \
77         };                                                              \
78         static const char expected[] = text;                            \
79         char *actual;                                                   \
80                                                                         \
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 );                        \
89         } while ( 0 )
90
91 /**
92  * Report an inet6_aton() test result
93  *
94  * @v text              Textual representation
95  * @v addr              Expected IPv6 address
96  */
97 #define inet6_aton_ok( text, addr ) do {                                \
98         static const char string[] = text;                              \
99         static const struct in6_addr expected = {                       \
100                 .s6_addr = addr,                                        \
101         };                                                              \
102         struct in6_addr actual;                                         \
103                                                                         \
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 );   \
108         } while ( 0 )
109
110 /**
111  * Report an inet6_aton() failure test result
112  *
113  * @v text              Textual representation
114  */
115 #define inet6_aton_fail_ok( text ) do {                                 \
116         static const char string[] = text;                              \
117         struct in6_addr dummy;                                          \
118                                                                         \
119         ok ( inet6_aton ( string, &dummy ) != 0 );                      \
120         } while ( 0 )
121
122 /**
123  * Perform IPv6 self-tests
124  *
125  */
126 static void ipv6_test_exec ( void ) {
127
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 ) );
141
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" );
146         /* No zeros */
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" );
150         /* Run of zeros */
151         inet6_ntoa_ok ( IPV6 ( 0x20, 0x01, 0x0d, 0xb8, 0x00, 0x00, 0x00, 0x00,
152                                0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01 ),
153                         "2001:db8::1" );
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 ),
161                         "2001:0:0:1::1" );
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 ),
169                         "fe80::" );
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 ),
173                         "::1" );
174         /* All zeros */
175         inet6_ntoa_ok ( IPV6 ( 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
176                                0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 ),
177                         "::" );
178         /* Maximum length */
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" );
182
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));
187         /* No zeros */
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));
203         /* All zeros */
204         inet6_aton_ok ( "::",
205                         IPV6 ( 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
206                                0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00));
207
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 ( ":::" );
217 }
218
219 /** IPv6 self-test */
220 struct self_test ipv6_test __self_test = {
221         .name = "ipv6",
222         .exec = ipv6_test_exec,
223 };