iPXE
ipv4_test.c
Go to the documentation of this file.
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  */
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  */
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  */
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  */
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 };
#define IN_IS_CLASSA(addr)
Definition: in.h:27
__be32 in[4]
Definition: CIB_PRM.h:35
#define IN_IS_CLASSB(addr)
Definition: in.h:29
#define ntohl(value)
Definition: byteswap.h:134
Self-test infrastructure.
const char * name
Test set name.
Definition: test.h:17
A self-test set.
Definition: test.h:15
static void inet_ntoa_okx(uint32_t addr, const char *text, const char *file, unsigned int line)
Report an inet_ntoa() test result.
Definition: ipv4_test.c:53
struct self_test ipv4_test __self_test
IPv4 self-test.
Definition: ipv4_test.c:151
#define okx(success, file, line)
Report test result.
Definition: test.h:44
#define inet_aton_ok(text, addr)
Definition: ipv4_test.c:87
IP address structure.
Definition: in.h:39
int inet_aton(const char *string, struct in_addr *in)
Parse IPv4 address.
Definition: ipv4.c:631
char * inet_ntoa(struct in_addr in)
Convert IPv4 address to dotted-quad notation.
Definition: ipv4.c:658
u32 addr
Definition: sky2.h:8
#define inet_ntoa_ok(addr, text)
Definition: ipv4_test.c:67
static void inet_aton_fail_okx(const char *text, const char *file, unsigned int line)
Report an inet_aton() failure test result.
Definition: ipv4_test.c:97
unsigned int uint32_t
Definition: stdint.h:12
uint32_t s_addr
Definition: in.h:40
#define IPV4(a, b, c, d)
Define inline IPv4 address.
Definition: ipv4_test.c:42
FILE_LICENCE(GPL2_OR_LATER_OR_UBDL)
int strcmp(const char *first, const char *second)
Compare strings.
Definition: string.c:173
#define inet_aton_fail_ok(text)
Definition: ipv4_test.c:104
#define IN_IS_MULTICAST(addr)
Definition: in.h:33
static void ipv4_test_exec(void)
Perform IPv4 self-tests.
Definition: ipv4_test.c:111
#define IN_IS_CLASSC(addr)
Definition: in.h:31
#define DBG(...)
Print a debugging message.
Definition: compiler.h:498
#define ok(success)
Definition: test.h:46
String functions.
static void inet_aton_okx(const char *text, uint32_t addr, const char *file, unsigned int line)
Report an inet_aton() test result.
Definition: ipv4_test.c:78