1 /*
2  * Copyright (C) 2015 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  * IPv4 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/in.h>
39 #include <ipxe/test.h>
40 
41 /** Define inline IPv4 address */
42 #define IPV4(a,b,c,d) \
43 	htonl ( ( (a) << 24 ) | ( (b) << 16 ) | ( (c) << 8 ) | (d) )
44 
45 /**
46  * Report an inet_ntoa() test result
47  *
48  * @v addr		IPv4 address
49  * @v text		Expected textual representation
50  * @v file		Test code file
51  * @v line		Test code line
52  */
inet_ntoa_okx(uint32_t addr,const char * text,const char * file,unsigned int line)53 static void inet_ntoa_okx ( uint32_t addr, const char *text, const char *file,
54 			    unsigned int line ) {
55 	struct in_addr in = { .s_addr = addr };
56 	char *actual;
57 
58 	/* Format address */
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 );
66 }
67 #define inet_ntoa_ok( addr, text ) \
68 	inet_ntoa_okx ( addr, text, __FILE__, __LINE__ )
69 
70 /**
71  * Report an inet_aton() test result
72  *
73  * @v text		Textual representation
74  * @v addr		Expected IPv4 address
75  * @v file		Test code file
76  * @v line		Test code line
77  */
inet_aton_okx(const char * text,uint32_t addr,const char * file,unsigned int line)78 static void inet_aton_okx ( const char *text, uint32_t addr, const char *file,
79 			    unsigned int line ) {
80 	struct in_addr actual;
81 
82 	/* Parse address */
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 );
86 };
87 #define inet_aton_ok( text, addr ) \
88 	inet_aton_okx ( text, addr, __FILE__, __LINE__ )
89 
90 /**
91  * Report an inet_aton() failure test result
92  *
93  * @v text		Textual representation
94  * @v file		Test code file
95  * @v line		Test code line
96  */
inet_aton_fail_okx(const char * text,const char * file,unsigned int line)97 static void inet_aton_fail_okx ( const char *text, const char *file,
98 				 unsigned int line ) {
99 	struct in_addr actual;
100 
101 	/* Attempt to parse address */
102 	okx ( inet_aton ( text, &actual ) == 0, file, line );
103 }
104 #define inet_aton_fail_ok( text ) \
105 	inet_aton_fail_okx ( text, __FILE__, __LINE__ )
106 
107 /**
108  * Perform IPv4 self-tests
109  *
110  */
ipv4_test_exec(void)111 static void ipv4_test_exec ( void ) {
112 
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 ) ) );
131 
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" );
137 
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 ) );
141 
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 */
148 }
149 
150 /** IPv4 self-test */
151 struct self_test ipv4_test __self_test = {
152 	.name = "ipv4",
153 	.exec = ipv4_test_exec,
154 };
155