iPXE
ipv4_test.c
Go to the documentation of this file.
00001 /*
00002  * Copyright (C) 2015 Michael Brown <mbrown@fensystems.co.uk>.
00003  *
00004  * This program is free software; you can redistribute it and/or
00005  * modify it under the terms of the GNU General Public License as
00006  * published by the Free Software Foundation; either version 2 of the
00007  * License, or (at your option) any later version.
00008  *
00009  * This program is distributed in the hope that it will be useful, but
00010  * WITHOUT ANY WARRANTY; without even the implied warranty of
00011  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00012  * General Public License for more details.
00013  *
00014  * You should have received a copy of the GNU General Public License
00015  * along with this program; if not, write to the Free Software
00016  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
00017  * 02110-1301, USA.
00018  *
00019  * You can also choose to distribute this program under the terms of
00020  * the Unmodified Binary Distribution Licence (as given in the file
00021  * COPYING.UBDL), provided that you have satisfied its requirements.
00022  */
00023 
00024 FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
00025 
00026 /** @file
00027  *
00028  * IPv4 tests
00029  *
00030  */
00031 
00032 /* Forcibly enable assertions */
00033 #undef NDEBUG
00034 
00035 #include <stdint.h>
00036 #include <string.h>
00037 #include <byteswap.h>
00038 #include <ipxe/in.h>
00039 #include <ipxe/test.h>
00040 
00041 /** Define inline IPv4 address */
00042 #define IPV4(a,b,c,d) \
00043         htonl ( ( (a) << 24 ) | ( (b) << 16 ) | ( (c) << 8 ) | (d) )
00044 
00045 /**
00046  * Report an inet_ntoa() test result
00047  *
00048  * @v addr              IPv4 address
00049  * @v text              Expected textual representation
00050  * @v file              Test code file
00051  * @v line              Test code line
00052  */
00053 static void inet_ntoa_okx ( uint32_t addr, const char *text, const char *file,
00054                             unsigned int line ) {
00055         struct in_addr in = { .s_addr = addr };
00056         char *actual;
00057 
00058         /* Format address */
00059         actual = inet_ntoa ( in );
00060         DBG ( "inet_ntoa ( %d.%d.%d.%d ) = %s\n",
00061               ( ( ntohl ( addr ) >> 24 ) & 0xff ),
00062               ( ( ntohl ( addr ) >> 16 ) & 0xff ),
00063               ( ( ntohl ( addr ) >> 8 ) & 0xff ),
00064               ( ( ntohl ( addr ) >> 0 ) & 0xff ), actual );
00065         okx ( strcmp ( actual, text ) == 0, file, line );
00066 }
00067 #define inet_ntoa_ok( addr, text ) \
00068         inet_ntoa_okx ( addr, text, __FILE__, __LINE__ )
00069 
00070 /**
00071  * Report an inet_aton() test result
00072  *
00073  * @v text              Textual representation
00074  * @v addr              Expected IPv4 address
00075  * @v file              Test code file
00076  * @v line              Test code line
00077  */
00078 static void inet_aton_okx ( const char *text, uint32_t addr, const char *file,
00079                             unsigned int line ) {
00080         struct in_addr actual;
00081 
00082         /* Parse address */
00083         okx ( inet_aton ( text, &actual ) != 0, file, line );
00084         DBG ( "inet_aton ( \"%s\" ) = %s\n", text, inet_ntoa ( actual ) );
00085         okx ( actual.s_addr == addr, file, line );
00086 };
00087 #define inet_aton_ok( text, addr ) \
00088         inet_aton_okx ( text, addr, __FILE__, __LINE__ )
00089 
00090 /**
00091  * Report an inet_aton() failure test result
00092  *
00093  * @v text              Textual representation
00094  * @v file              Test code file
00095  * @v line              Test code line
00096  */
00097 static void inet_aton_fail_okx ( const char *text, const char *file,
00098                                  unsigned int line ) {
00099         struct in_addr actual;
00100 
00101         /* Attempt to parse address */
00102         okx ( inet_aton ( text, &actual ) == 0, file, line );
00103 }
00104 #define inet_aton_fail_ok( text ) \
00105         inet_aton_fail_okx ( text, __FILE__, __LINE__ )
00106 
00107 /**
00108  * Perform IPv4 self-tests
00109  *
00110  */
00111 static void ipv4_test_exec ( void ) {
00112 
00113         /* Address testing macros */
00114         ok (   IN_IS_CLASSA ( IPV4 ( 10, 0, 0, 1 ) ) );
00115         ok ( ! IN_IS_CLASSB ( IPV4 ( 10, 0, 0, 1 ) ) );
00116         ok ( ! IN_IS_CLASSC ( IPV4 ( 10, 0, 0, 1 ) ) );
00117         ok ( ! IN_IS_CLASSA ( IPV4 ( 172, 16, 0, 1 ) ) );
00118         ok (   IN_IS_CLASSB ( IPV4 ( 172, 16, 0, 1 ) ) );
00119         ok ( ! IN_IS_CLASSC ( IPV4 ( 172, 16, 0, 1 ) ) );
00120         ok ( ! IN_IS_CLASSA ( IPV4 ( 192, 168, 0, 1 ) ) );
00121         ok ( ! IN_IS_CLASSB ( IPV4 ( 192, 168, 0, 1 ) ) );
00122         ok (   IN_IS_CLASSC ( IPV4 ( 192, 168, 0, 1 ) ) );
00123         ok ( ! IN_IS_MULTICAST ( IPV4 ( 127, 0, 0, 1 ) ) );
00124         ok ( ! IN_IS_MULTICAST ( IPV4 ( 8, 8, 8, 8 ) ) );
00125         ok ( ! IN_IS_MULTICAST ( IPV4 ( 0, 0, 0, 0 ) ) );
00126         ok ( ! IN_IS_MULTICAST ( IPV4 ( 223, 0, 0, 1 ) ) );
00127         ok ( ! IN_IS_MULTICAST ( IPV4 ( 240, 0, 0, 1 ) ) );
00128         ok (   IN_IS_MULTICAST ( IPV4 ( 224, 0, 0, 1 ) ) );
00129         ok (   IN_IS_MULTICAST ( IPV4 ( 231, 89, 0, 2 ) ) );
00130         ok (   IN_IS_MULTICAST ( IPV4 ( 239, 6, 1, 17 ) ) );
00131 
00132         /* inet_ntoa() tests */
00133         inet_ntoa_ok ( IPV4 ( 127, 0, 0, 1 ), "127.0.0.1" );
00134         inet_ntoa_ok ( IPV4 ( 0, 0, 0, 0 ), "0.0.0.0" );
00135         inet_ntoa_ok ( IPV4 ( 255, 255, 255, 255 ), "255.255.255.255" );
00136         inet_ntoa_ok ( IPV4 ( 212, 13, 204, 60 ), "212.13.204.60" );
00137 
00138         /* inet_aton() tests */
00139         inet_aton_ok ( "212.13.204.60", IPV4 ( 212, 13, 204, 60 ) );
00140         inet_aton_ok ( "127.0.0.1", IPV4 ( 127, 0, 0, 1 ) );
00141 
00142         /* inet_aton() failure tests */
00143         inet_aton_fail_ok ( "256.0.0.1" ); /* Byte out of range */
00144         inet_aton_fail_ok ( "212.13.204.60.1" ); /* Too long */
00145         inet_aton_fail_ok ( "127.0.0" ); /* Too short */
00146         inet_aton_fail_ok ( "1.2.3.a" ); /* Invalid characters */
00147         inet_aton_fail_ok ( "127.0..1" ); /* Missing bytes */
00148 }
00149 
00150 /** IPv4 self-test */
00151 struct self_test ipv4_test __self_test = {
00152         .name = "ipv4",
00153         .exec = ipv4_test_exec,
00154 };