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
20 FILE_LICENCE ( GPL2_OR_LATER
);
28 /* Forcibly enable assertions */
35 #include <ipxe/test.h>
37 /** Define inline IPv4 address */
38 #define IPV4(a,b,c,d) ( ( (a) << 24 ) | ( (b) << 16 ) | ( (c) << 8 ) | (d) )
41 * Report an inet_ntoa() test result
43 * @v addr IPv4 address
44 * @v text Expected textual representation
45 * @v file Test code file
46 * @v line Test code line
48 static void inet_ntoa_okx ( uint32_t addr
, const char *text
, const char *file
,
50 struct in_addr in
= { .s_addr
= htonl ( addr
) };
54 actual
= inet_ntoa ( in
);
55 DBG ( "inet_ntoa ( %d.%d.%d.%d ) = %s\n",
56 ( ( ntohl ( addr
) >> 24 ) & 0xff ),
57 ( ( ntohl ( addr
) >> 16 ) & 0xff ),
58 ( ( ntohl ( addr
) >> 8 ) & 0xff ),
59 ( ( ntohl ( addr
) >> 0 ) & 0xff ), actual
);
60 okx ( strcmp ( actual
, text
) == 0, file
, line
);
62 #define inet_ntoa_ok( addr, text ) \
63 inet_ntoa_okx ( addr, text, __FILE__, __LINE__ )
66 * Report an inet_aton() test result
68 * @v text Textual representation
69 * @v addr Expected IPv4 address
70 * @v file Test code file
71 * @v line Test code line
73 static void inet_aton_okx ( const char *text
, uint32_t addr
, const char *file
,
75 struct in_addr actual
;
78 okx ( inet_aton ( text
, &actual
) != 0, file
, line
);
79 DBG ( "inet_aton ( \"%s\" ) = %s\n", text
, inet_ntoa ( actual
) );
80 okx ( ntohl ( actual
.s_addr
) == addr
, file
, line
);
82 #define inet_aton_ok( text, addr ) \
83 inet_aton_okx ( text, addr, __FILE__, __LINE__ )
86 * Report an inet_aton() failure test result
88 * @v text Textual representation
89 * @v file Test code file
90 * @v line Test code line
92 static void inet_aton_fail_okx ( const char *text
, const char *file
,
94 struct in_addr actual
;
96 /* Attempt to parse address */
97 okx ( inet_aton ( text
, &actual
) == 0, file
, line
);
99 #define inet_aton_fail_ok( text ) \
100 inet_aton_fail_okx ( text, __FILE__, __LINE__ )
103 * Perform IPv4 self-tests
106 static void ipv4_test_exec ( void ) {
108 /* Address testing macros */
109 ok ( IN_CLASSA ( IPV4 ( 10, 0, 0, 1 ) ) );
110 ok ( ! IN_CLASSB ( IPV4 ( 10, 0, 0, 1 ) ) );
111 ok ( ! IN_CLASSC ( IPV4 ( 10, 0, 0, 1 ) ) );
112 ok ( ! IN_CLASSA ( IPV4 ( 172, 16, 0, 1 ) ) );
113 ok ( IN_CLASSB ( IPV4 ( 172, 16, 0, 1 ) ) );
114 ok ( ! IN_CLASSC ( IPV4 ( 172, 16, 0, 1 ) ) );
115 ok ( ! IN_CLASSA ( IPV4 ( 192, 168, 0, 1 ) ) );
116 ok ( ! IN_CLASSB ( IPV4 ( 192, 168, 0, 1 ) ) );
117 ok ( IN_CLASSC ( IPV4 ( 192, 168, 0, 1 ) ) );
118 ok ( ! IN_MULTICAST ( IPV4 ( 127, 0, 0, 1 ) ) );
119 ok ( ! IN_MULTICAST ( IPV4 ( 8, 8, 8, 8 ) ) );
120 ok ( ! IN_MULTICAST ( IPV4 ( 0, 0, 0, 0 ) ) );
121 ok ( ! IN_MULTICAST ( IPV4 ( 223, 0, 0, 1 ) ) );
122 ok ( ! IN_MULTICAST ( IPV4 ( 240, 0, 0, 1 ) ) );
123 ok ( IN_MULTICAST ( IPV4 ( 224, 0, 0, 1 ) ) );
124 ok ( IN_MULTICAST ( IPV4 ( 231, 89, 0, 2 ) ) );
125 ok ( IN_MULTICAST ( IPV4 ( 239, 6, 1, 17 ) ) );
127 /* inet_ntoa() tests */
128 inet_ntoa_ok ( IPV4 ( 127, 0, 0, 1 ), "127.0.0.1" );
129 inet_ntoa_ok ( IPV4 ( 0, 0, 0, 0 ), "0.0.0.0" );
130 inet_ntoa_ok ( IPV4 ( 255, 255, 255, 255 ), "255.255.255.255" );
131 inet_ntoa_ok ( IPV4 ( 212, 13, 204, 60 ), "212.13.204.60" );
133 /* inet_aton() tests */
134 inet_aton_ok ( "212.13.204.60", IPV4 ( 212, 13, 204, 60 ) );
135 inet_aton_ok ( "127.0.0.1", IPV4 ( 127, 0, 0, 1 ) );
137 /* inet_aton() failure tests */
138 inet_aton_fail_ok ( "256.0.0.1" ); /* Byte out of range */
139 inet_aton_fail_ok ( "212.13.204.60.1" ); /* Too long */
140 inet_aton_fail_ok ( "127.0.0" ); /* Too short */
141 inet_aton_fail_ok ( "1.2.3.a" ); /* Invalid characters */
142 inet_aton_fail_ok ( "127.0..1" ); /* Missing bytes */
145 /** IPv4 self-test */
146 struct self_test ipv4_test __self_test
= {
148 .exec
= ipv4_test_exec
,