]> git.ipfire.org Git - people/ms/network.git/commitdiff
test: Add module tests for ip_network_is_subset_of
authorMichael Tremer <michael.tremer@ipfire.org>
Wed, 31 May 2017 22:11:40 +0000 (23:11 +0100)
committerMichael Tremer <michael.tremer@ipfire.org>
Wed, 31 May 2017 22:11:40 +0000 (23:11 +0100)
Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
Makefile.am
test/functions/ip/ip_network_is_subnet_of [new file with mode: 0755]
test/test-functions [new file with mode: 0644]

index c9a7155729954107f14fd3783f82f50c4d589b5a..3856d7db68df2fbc03bb9c1ed629ebcf317db318 100644 (file)
@@ -448,6 +448,7 @@ TESTS = \
        test/functions/ip/ip_get_prefix \
        test/functions/ip/ip_is_network \
        test/functions/ip/ip_is_valid \
+       test/functions/ip/ip_network_is_subnet_of \
        test/functions/ip/ip_prefix_is_valid \
        test/functions/ip/ip_protocol_is_supported \
        test/functions/ip/ip_split_prefix
diff --git a/test/functions/ip/ip_network_is_subnet_of b/test/functions/ip/ip_network_is_subnet_of
new file mode 100755 (executable)
index 0000000..d285483
--- /dev/null
@@ -0,0 +1,44 @@
+#!/bin/bash
+
+. ${functions}
+
+# Load test functions
+. ${testdir}/test-functions
+
+############################################################
+# Run a number of valid function calls                     #
+############################################################
+
+# IPv6
+test_expect_ok ip_network_is_subset_of 2001:db8:abcd::/32 2001:db8::/32
+test_expect_ok ip_network_is_subset_of 2001:db8:abcd::/33 2001:db8::/32
+test_expect_ok ip_network_is_subset_of 2001:db8:abcd::/36 2001:db8::/32
+test_expect_ok ip_network_is_subset_of 2001:db8:abcd::/48 2001:db8::/32
+test_expect_ok ip_network_is_subset_of 2001:db8:abcd::/52 2001:db8::/32
+test_expect_ok ip_network_is_subset_of 2001:db8:abcd::/64 2001:db8::/32
+
+# IPv4
+test_expect_ok ip_network_is_subset_of 1.1.1.1 1.0.0.0/8
+test_expect_ok ip_network_is_subset_of 2.2.2.2 2.0.0.0/8
+
+############################################################
+# Run a number of invalid function calls                   #
+############################################################
+
+# IPv6
+test_expect_error ip_network_is_subset_of 2001:db8::/31 2001:db8::/32
+test_expect_error ip_network_is_subset_of ::1/128 2001:db8::/32
+
+# IPv4
+test_expect_error ip_network_is_subset_of 1.1.1.1 1.0.0.0/16
+test_expect_error ip_network_is_subset_of 1.1.1.1 1.0.0.0/24
+
+# Protocols cannot be mixed
+test_expect_error ip_network_is_subset_of ::1 127.0.0.0/8
+test_expect_error ip_network_is_subset_of 127.0.0.0/8 ::1
+
+# Some garbage inputs
+test_expect_error ip_network_is_subset_of 127.0.0.0/8 a.b.c.d/e
+test_expect_error ip_network_is_subset_of a.b.c.d/e 127.0.0.0/8
+
+exit ${TEST_FAILED}
diff --git a/test/test-functions b/test/test-functions
new file mode 100644 (file)
index 0000000..21ffeca
--- /dev/null
@@ -0,0 +1,48 @@
+#!/bin/bash
+###############################################################################
+#                                                                             #
+# IPFire.org - A linux based firewall                                         #
+# Copyright (C) 2017  IPFire Network Development Team                         #
+#                                                                             #
+# 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 3 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, see <http://www.gnu.org/licenses/>.       #
+#                                                                             #
+###############################################################################
+
+TEST_FAILED=${EXIT_OK}
+
+test_expect_ok() {
+       local cmd=$@
+
+       if ${cmd}; then
+               echo "OK: ${cmd} exited successfully"
+       else
+               echo "ERROR: ${cmd} exited with failure"
+               TEST_FAILED=${EXIT_ERROR}
+       fi
+
+       return ${TEST_FAILED}
+}
+
+test_expect_error() {
+       local cmd=$@
+
+       if ! ${cmd}; then
+               echo "OK: ${cmd} exited with error"
+       else
+               echo "ERROR: ${cmd} exited successfully"
+               TEST_FAILED=${EXIT_ERROR}
+       fi
+
+       return ${TEST_FAILED}
+}