Browse Source

[test] Add IPv4 self-tests

Signed-off-by: Michael Brown <mcb30@ipxe.org>
tags/v1.20.1
Michael Brown 9 years ago
parent
commit
b05e7e50fa
2 changed files with 148 additions and 0 deletions
  1. 147
    0
      src/tests/ipv4_test.c
  2. 1
    0
      src/tests/tests.c

+ 147
- 0
src/tests/ipv4_test.c View File

@@ -0,0 +1,147 @@
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
+
20
+FILE_LICENCE ( GPL2_OR_LATER );
21
+
22
+/** @file
23
+ *
24
+ * IPv4 tests
25
+ *
26
+ */
27
+
28
+/* Forcibly enable assertions */
29
+#undef NDEBUG
30
+
31
+#include <stdint.h>
32
+#include <string.h>
33
+#include <byteswap.h>
34
+#include <ipxe/in.h>
35
+#include <ipxe/test.h>
36
+
37
+/** Define inline IPv4 address */
38
+#define IPV4(a,b,c,d) ( ( (a) << 24 ) | ( (b) << 16 ) | ( (c) << 8 ) | (d) )
39
+
40
+/**
41
+ * Report an inet_ntoa() test result
42
+ *
43
+ * @v addr		IPv4 address
44
+ * @v text		Expected textual representation
45
+ * @v file		Test code file
46
+ * @v line		Test code line
47
+ */
48
+static void inet_ntoa_okx ( uint32_t addr, const char *text, const char *file,
49
+			    unsigned int line ) {
50
+	struct in_addr in = { .s_addr = htonl ( addr ) };
51
+	char *actual;
52
+
53
+	/* Format address */
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 );
61
+}
62
+#define inet_ntoa_ok( addr, text ) \
63
+	inet_ntoa_okx ( addr, text, __FILE__, __LINE__ )
64
+
65
+/**
66
+ * Report an inet_aton() test result
67
+ *
68
+ * @v text		Textual representation
69
+ * @v addr		Expected IPv4 address
70
+ * @v file		Test code file
71
+ * @v line		Test code line
72
+ */
73
+static void inet_aton_okx ( const char *text, uint32_t addr, const char *file,
74
+			    unsigned int line ) {
75
+	struct in_addr actual;
76
+
77
+	/* Parse address */
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 );
81
+};
82
+#define inet_aton_ok( text, addr ) \
83
+	inet_aton_okx ( text, addr, __FILE__, __LINE__ )
84
+
85
+/**
86
+ * Report an inet_aton() failure test result
87
+ *
88
+ * @v text		Textual representation
89
+ * @v file		Test code file
90
+ * @v line		Test code line
91
+ */
92
+static void inet_aton_fail_okx ( const char *text, const char *file,
93
+				 unsigned int line ) {
94
+	struct in_addr actual;
95
+
96
+	/* Attempt to parse address */
97
+	okx ( inet_aton ( text, &actual ) == 0, file, line );
98
+}
99
+#define inet_aton_fail_ok( text ) \
100
+	inet_aton_fail_okx ( text, __FILE__, __LINE__ )
101
+
102
+/**
103
+ * Perform IPv4 self-tests
104
+ *
105
+ */
106
+static void ipv4_test_exec ( void ) {
107
+
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 ) ) );
126
+
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" );
132
+
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 ) );
136
+
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
+}
142
+
143
+/** IPv4 self-test */
144
+struct self_test ipv4_test __self_test = {
145
+	.name = "ipv4",
146
+	.exec = ipv4_test_exec,
147
+};

+ 1
- 0
src/tests/tests.c View File

@@ -38,6 +38,7 @@ REQUIRE_OBJECT ( base16_test );
38 38
 REQUIRE_OBJECT ( settings_test );
39 39
 REQUIRE_OBJECT ( time_test );
40 40
 REQUIRE_OBJECT ( tcpip_test );
41
+REQUIRE_OBJECT ( ipv4_test );
41 42
 REQUIRE_OBJECT ( ipv6_test );
42 43
 REQUIRE_OBJECT ( crc32_test );
43 44
 REQUIRE_OBJECT ( md5_test );

Loading…
Cancel
Save