summaryrefslogtreecommitdiffstats
path: root/src/tests
diff options
context:
space:
mode:
authorMichael Brown2015-02-18 15:11:32 +0100
committerMichael Brown2015-02-18 15:11:32 +0100
commitb05e7e50fa3d1a1453256662c62e5446feaa4d58 (patch)
treefd5665734e95543349358eb254da84d0411b6f4f /src/tests
parent[xhci] Abort commands on timeout (diff)
downloadipxe-b05e7e50fa3d1a1453256662c62e5446feaa4d58.tar.gz
ipxe-b05e7e50fa3d1a1453256662c62e5446feaa4d58.tar.xz
ipxe-b05e7e50fa3d1a1453256662c62e5446feaa4d58.zip
[test] Add IPv4 self-tests
Signed-off-by: Michael Brown <mcb30@ipxe.org>
Diffstat (limited to 'src/tests')
-rw-r--r--src/tests/ipv4_test.c147
-rw-r--r--src/tests/tests.c1
2 files changed, 148 insertions, 0 deletions
diff --git a/src/tests/ipv4_test.c b/src/tests/ipv4_test.c
new file mode 100644
index 00000000..ba18f845
--- /dev/null
+++ b/src/tests/ipv4_test.c
@@ -0,0 +1,147 @@
+/*
+ * Copyright (C) 2015 Michael Brown <mbrown@fensystems.co.uk>.
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License as
+ * published by the Free Software Foundation; either version 2 of the
+ * License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+ * 02110-1301, USA.
+ */
+
+FILE_LICENCE ( GPL2_OR_LATER );
+
+/** @file
+ *
+ * IPv4 tests
+ *
+ */
+
+/* Forcibly enable assertions */
+#undef NDEBUG
+
+#include <stdint.h>
+#include <string.h>
+#include <byteswap.h>
+#include <ipxe/in.h>
+#include <ipxe/test.h>
+
+/** Define inline IPv4 address */
+#define IPV4(a,b,c,d) ( ( (a) << 24 ) | ( (b) << 16 ) | ( (c) << 8 ) | (d) )
+
+/**
+ * Report an inet_ntoa() test result
+ *
+ * @v addr IPv4 address
+ * @v text Expected textual representation
+ * @v file Test code file
+ * @v line Test code line
+ */
+static void inet_ntoa_okx ( uint32_t addr, const char *text, const char *file,
+ unsigned int line ) {
+ struct in_addr in = { .s_addr = htonl ( addr ) };
+ char *actual;
+
+ /* Format address */
+ actual = inet_ntoa ( in );
+ DBG ( "inet_ntoa ( %d.%d.%d.%d ) = %s\n",
+ ( ( ntohl ( addr ) >> 24 ) & 0xff ),
+ ( ( ntohl ( addr ) >> 16 ) & 0xff ),
+ ( ( ntohl ( addr ) >> 8 ) & 0xff ),
+ ( ( ntohl ( addr ) >> 0 ) & 0xff ), actual );
+ okx ( strcmp ( actual, text ) == 0, file, line );
+}
+#define inet_ntoa_ok( addr, text ) \
+ inet_ntoa_okx ( addr, text, __FILE__, __LINE__ )
+
+/**
+ * Report an inet_aton() test result
+ *
+ * @v text Textual representation
+ * @v addr Expected IPv4 address
+ * @v file Test code file
+ * @v line Test code line
+ */
+static void inet_aton_okx ( const char *text, uint32_t addr, const char *file,
+ unsigned int line ) {
+ struct in_addr actual;
+
+ /* Parse address */
+ okx ( inet_aton ( text, &actual ) != 0, file, line );
+ DBG ( "inet_aton ( \"%s\" ) = %s\n", text, inet_ntoa ( actual ) );
+ okx ( ntohl ( actual.s_addr ) == addr, file, line );
+};
+#define inet_aton_ok( text, addr ) \
+ inet_aton_okx ( text, addr, __FILE__, __LINE__ )
+
+/**
+ * Report an inet_aton() failure test result
+ *
+ * @v text Textual representation
+ * @v file Test code file
+ * @v line Test code line
+ */
+static void inet_aton_fail_okx ( const char *text, const char *file,
+ unsigned int line ) {
+ struct in_addr actual;
+
+ /* Attempt to parse address */
+ okx ( inet_aton ( text, &actual ) == 0, file, line );
+}
+#define inet_aton_fail_ok( text ) \
+ inet_aton_fail_okx ( text, __FILE__, __LINE__ )
+
+/**
+ * Perform IPv4 self-tests
+ *
+ */
+static void ipv4_test_exec ( void ) {
+
+ /* Address testing macros */
+ ok ( IN_CLASSA ( IPV4 ( 10, 0, 0, 1 ) ) );
+ ok ( ! IN_CLASSB ( IPV4 ( 10, 0, 0, 1 ) ) );
+ ok ( ! IN_CLASSC ( IPV4 ( 10, 0, 0, 1 ) ) );
+ ok ( ! IN_CLASSA ( IPV4 ( 172, 16, 0, 1 ) ) );
+ ok ( IN_CLASSB ( IPV4 ( 172, 16, 0, 1 ) ) );
+ ok ( ! IN_CLASSC ( IPV4 ( 172, 16, 0, 1 ) ) );
+ ok ( ! IN_CLASSA ( IPV4 ( 192, 168, 0, 1 ) ) );
+ ok ( ! IN_CLASSB ( IPV4 ( 192, 168, 0, 1 ) ) );
+ ok ( IN_CLASSC ( IPV4 ( 192, 168, 0, 1 ) ) );
+ ok ( ! IN_MULTICAST ( IPV4 ( 127, 0, 0, 1 ) ) );
+ ok ( ! IN_MULTICAST ( IPV4 ( 8, 8, 8, 8 ) ) );
+ ok ( ! IN_MULTICAST ( IPV4 ( 0, 0, 0, 0 ) ) );
+ ok ( ! IN_MULTICAST ( IPV4 ( 223, 0, 0, 1 ) ) );
+ ok ( ! IN_MULTICAST ( IPV4 ( 240, 0, 0, 1 ) ) );
+ ok ( IN_MULTICAST ( IPV4 ( 224, 0, 0, 1 ) ) );
+ ok ( IN_MULTICAST ( IPV4 ( 231, 89, 0, 2 ) ) );
+ ok ( IN_MULTICAST ( IPV4 ( 239, 6, 1, 17 ) ) );
+
+ /* inet_ntoa() tests */
+ inet_ntoa_ok ( IPV4 ( 127, 0, 0, 1 ), "127.0.0.1" );
+ inet_ntoa_ok ( IPV4 ( 0, 0, 0, 0 ), "0.0.0.0" );
+ inet_ntoa_ok ( IPV4 ( 255, 255, 255, 255 ), "255.255.255.255" );
+ inet_ntoa_ok ( IPV4 ( 212, 13, 204, 60 ), "212.13.204.60" );
+
+ /* inet_aton() tests */
+ inet_aton_ok ( "212.13.204.60", IPV4 ( 212, 13, 204, 60 ) );
+ inet_aton_ok ( "127.0.0.1", IPV4 ( 127, 0, 0, 1 ) );
+
+ /* inet_aton() failure tests */
+ inet_aton_fail_ok ( "256.0.0.1" ); /* Byte out of range */
+ inet_aton_fail_ok ( "212.13.204.60.1" ); /* Too long */
+ inet_aton_fail_ok ( "127.0.0" ); /* Too short */
+}
+
+/** IPv4 self-test */
+struct self_test ipv4_test __self_test = {
+ .name = "ipv4",
+ .exec = ipv4_test_exec,
+};
diff --git a/src/tests/tests.c b/src/tests/tests.c
index f9bb47e1..8255ee6b 100644
--- a/src/tests/tests.c
+++ b/src/tests/tests.c
@@ -38,6 +38,7 @@ REQUIRE_OBJECT ( base16_test );
REQUIRE_OBJECT ( settings_test );
REQUIRE_OBJECT ( time_test );
REQUIRE_OBJECT ( tcpip_test );
+REQUIRE_OBJECT ( ipv4_test );
REQUIRE_OBJECT ( ipv6_test );
REQUIRE_OBJECT ( crc32_test );
REQUIRE_OBJECT ( md5_test );