2 * Copyright (C) 2015 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 */
39 #include <ipxe/test.h>
41 /** Define inline IPv4 address */
42 #define IPV4(a,b,c,d) \
43 htonl ( ( (a) << 24 ) | ( (b) << 16 ) | ( (c) << 8 ) | (d) )
46 * Report an inet_ntoa() test result
48 * @v addr IPv4 address
49 * @v text Expected textual representation
50 * @v file Test code file
51 * @v line Test code line
53 static void inet_ntoa_okx ( uint32_t addr, const char *text, const char *file,
55 struct in_addr in = { .s_addr = addr };
59 actual = inet_ntoa ( in );
60 DBG ( "inet_ntoa ( %d.%d.%d.%d ) = %s\n",
61 ( ( ntohl ( addr ) >> 24 ) & 0xff ),
62 ( ( ntohl ( addr ) >> 16 ) & 0xff ),
63 ( ( ntohl ( addr ) >> 8 ) & 0xff ),
64 ( ( ntohl ( addr ) >> 0 ) & 0xff ), actual );
65 okx ( strcmp ( actual, text ) == 0, file, line );
67 #define inet_ntoa_ok( addr, text ) \
68 inet_ntoa_okx ( addr, text, __FILE__, __LINE__ )
71 * Report an inet_aton() test result
73 * @v text Textual representation
74 * @v addr Expected IPv4 address
75 * @v file Test code file
76 * @v line Test code line
78 static void inet_aton_okx ( const char *text, uint32_t addr, const char *file,
80 struct in_addr actual;
83 okx ( inet_aton ( text, &actual ) != 0, file, line );
84 DBG ( "inet_aton ( \"%s\" ) = %s\n", text, inet_ntoa ( actual ) );
85 okx ( actual.s_addr == addr, file, line );
87 #define inet_aton_ok( text, addr ) \
88 inet_aton_okx ( text, addr, __FILE__, __LINE__ )
91 * Report an inet_aton() failure test result
93 * @v text Textual representation
94 * @v file Test code file
95 * @v line Test code line
97 static void inet_aton_fail_okx ( const char *text, const char *file,
99 struct in_addr actual;
101 /* Attempt to parse address */
102 okx ( inet_aton ( text, &actual ) == 0, file, line );
104 #define inet_aton_fail_ok( text ) \
105 inet_aton_fail_okx ( text, __FILE__, __LINE__ )
108 * Perform IPv4 self-tests
111 static void ipv4_test_exec ( void ) {
113 /* Address testing macros */
114 ok ( IN_IS_CLASSA ( IPV4 ( 10, 0, 0, 1 ) ) );
115 ok ( ! IN_IS_CLASSB ( IPV4 ( 10, 0, 0, 1 ) ) );
116 ok ( ! IN_IS_CLASSC ( IPV4 ( 10, 0, 0, 1 ) ) );
117 ok ( ! IN_IS_CLASSA ( IPV4 ( 172, 16, 0, 1 ) ) );
118 ok ( IN_IS_CLASSB ( IPV4 ( 172, 16, 0, 1 ) ) );
119 ok ( ! IN_IS_CLASSC ( IPV4 ( 172, 16, 0, 1 ) ) );
120 ok ( ! IN_IS_CLASSA ( IPV4 ( 192, 168, 0, 1 ) ) );
121 ok ( ! IN_IS_CLASSB ( IPV4 ( 192, 168, 0, 1 ) ) );
122 ok ( IN_IS_CLASSC ( IPV4 ( 192, 168, 0, 1 ) ) );
123 ok ( ! IN_IS_MULTICAST ( IPV4 ( 127, 0, 0, 1 ) ) );
124 ok ( ! IN_IS_MULTICAST ( IPV4 ( 8, 8, 8, 8 ) ) );
125 ok ( ! IN_IS_MULTICAST ( IPV4 ( 0, 0, 0, 0 ) ) );
126 ok ( ! IN_IS_MULTICAST ( IPV4 ( 223, 0, 0, 1 ) ) );
127 ok ( ! IN_IS_MULTICAST ( IPV4 ( 240, 0, 0, 1 ) ) );
128 ok ( IN_IS_MULTICAST ( IPV4 ( 224, 0, 0, 1 ) ) );
129 ok ( IN_IS_MULTICAST ( IPV4 ( 231, 89, 0, 2 ) ) );
130 ok ( IN_IS_MULTICAST ( IPV4 ( 239, 6, 1, 17 ) ) );
132 /* inet_ntoa() tests */
133 inet_ntoa_ok ( IPV4 ( 127, 0, 0, 1 ), "127.0.0.1" );
134 inet_ntoa_ok ( IPV4 ( 0, 0, 0, 0 ), "0.0.0.0" );
135 inet_ntoa_ok ( IPV4 ( 255, 255, 255, 255 ), "255.255.255.255" );
136 inet_ntoa_ok ( IPV4 ( 212, 13, 204, 60 ), "212.13.204.60" );
138 /* inet_aton() tests */
139 inet_aton_ok ( "212.13.204.60", IPV4 ( 212, 13, 204, 60 ) );
140 inet_aton_ok ( "127.0.0.1", IPV4 ( 127, 0, 0, 1 ) );
142 /* inet_aton() failure tests */
143 inet_aton_fail_ok ( "256.0.0.1" ); /* Byte out of range */
144 inet_aton_fail_ok ( "212.13.204.60.1" ); /* Too long */
145 inet_aton_fail_ok ( "127.0.0" ); /* Too short */
146 inet_aton_fail_ok ( "1.2.3.a" ); /* Invalid characters */
147 inet_aton_fail_ok ( "127.0..1" ); /* Missing bytes */
150 /** IPv4 self-test */
151 struct self_test ipv4_test __self_test = {
153 .exec = ipv4_test_exec,