summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDaniel Wagner <daniel.wagner@bmw-carit.de>2013-02-12 10:19:36 +0100
committerPatrik Flykt <patrik.flykt@linux.intel.com>2013-02-12 12:26:34 +0200
commite2e73d11d463dd443832f6f96633fb8afddf1bf5 (patch)
tree2b7631fad95d3be0c4d6e39957721ce99dc790bc
parent1dcb7cd93631d04efa72afcf9ebf6f9768db59e8 (diff)
downloadconnman-e2e73d11d463dd443832f6f96633fb8afddf1bf5.tar.gz
connman-e2e73d11d463dd443832f6f96633fb8afddf1bf5.tar.bz2
connman-e2e73d11d463dd443832f6f96633fb8afddf1bf5.zip
test-iptables: Add unit test for iptables
-rw-r--r--.gitignore1
-rw-r--r--Makefile.am8
-rw-r--r--unit/test-iptables.c122
3 files changed, 130 insertions, 1 deletions
diff --git a/.gitignore b/.gitignore
index 9b4b30d9..cb7d5e12 100644
--- a/.gitignore
+++ b/.gitignore
@@ -55,6 +55,7 @@ tools/private-network-test
unit/test-session
unit/test-ippool
unit/test-nat
+unit/test-iptables
doc/*.bak
doc/*.stamp
diff --git a/Makefile.am b/Makefile.am
index 26081d83..15d59da7 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -249,7 +249,8 @@ noinst_PROGRAMS += tools/supplicant-test \
tools/dbus-test tools/polkit-test \
tools/iptables-test tools/tap-test tools/wpad-test \
tools/stats-tool tools/private-network-test \
- unit/test-session unit/test-ippool unit/test-nat
+ unit/test-session unit/test-ippool unit/test-nat \
+ unit/test-iptables
tools_supplicant_test_SOURCES = $(gdbus_sources) tools/supplicant-test.c \
tools/supplicant-dbus.h tools/supplicant-dbus.c \
@@ -293,6 +294,11 @@ unit_test_ippool_SOURCES = $(gdbus_sources) src/log.c src/dbus.c \
unit_test_ippool_LDADD = @GLIB_LIBS@ @DBUS_LIBS@ -ldl
unit_objects += $(unit_test_ippool_OBJECTS)
+unit_test_iptables_SOURCES = $(gdbus_sources) src/log.c \
+ src/iptables.c unit/test-iptables.c
+unit_test_iptables_LDADD = @GLIB_LIBS@ @DBUS_LIBS@ @XTABLES_LIBS@ -ldl
+unit_objects += $(unit_test_iptables_OBJECTS)
+
unit_test_nat_SOURCES = $(gdbus_sources) src/log.c src/dbus.c \
src/iptables.c src/nat.c unit/test-nat.c
unit_test_nat_LDADD = @GLIB_LIBS@ @DBUS_LIBS@ @XTABLES_LIBS@ -ldl
diff --git a/unit/test-iptables.c b/unit/test-iptables.c
new file mode 100644
index 00000000..f11ed551
--- /dev/null
+++ b/unit/test-iptables.c
@@ -0,0 +1,122 @@
+/*
+ *
+ * Connection Manager
+ *
+ * Copyright (C) 2013 BWM CarIT GmbH. All rights reserved.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 2 as
+ * published by the Free Software Foundation.
+ *
+ * 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 St, Fifth Floor, Boston, MA 02110-1301 USA
+ *
+ */
+
+#ifdef HAVE_CONFIG_H
+#include <config.h>
+#endif
+
+#include <glib.h>
+
+#include "../src/connman.h"
+
+static void test_iptables_basic0(void)
+{
+ int err;
+
+ err = __connman_iptables_command("-t filter -A INPUT "
+ "-m mark --mark 1 -j LOG");
+ g_assert(err == 0);
+
+ err = __connman_iptables_commit("filter");
+ g_assert(err == 0);
+
+ err = __connman_iptables_command("-t filter -D INPUT "
+ "-m mark --mark 1 -j LOG");
+ g_assert(err == 0);
+
+ err = __connman_iptables_commit("filter");
+ g_assert(err == 0);
+}
+
+static void test_iptables_basic1(void)
+{
+ int err;
+
+ /* Test if we can do NAT stuff */
+
+ err = __connman_iptables_command("-t nat -A POSTROUTING "
+ "-s 10.10.1.0/24 -o eth0 -j MASQUERADE");
+
+ err = __connman_iptables_commit("nat");
+ g_assert(err == 0);
+
+ err = __connman_iptables_command("-t nat -D POSTROUTING "
+ "-s 10.10.1.0/24 -o eth0 -j MASQUERADE");
+
+ err = __connman_iptables_commit("nat");
+ g_assert(err == 0);
+}
+
+static void test_iptables_basic2(void)
+{
+ int err;
+
+ /* Test if the right rule is removed */
+
+ err = __connman_iptables_command("-t filter -A INPUT "
+ "-m mark --mark 1 -j LOG");
+ g_assert(err == 0);
+
+ err = __connman_iptables_commit("filter");
+ g_assert(err == 0);
+
+ err = __connman_iptables_command("-t filter -A INPUT "
+ "-m mark --mark 2 -j LOG");
+ g_assert(err == 0);
+
+ err = __connman_iptables_commit("filter");
+ g_assert(err == 0);
+
+ err = __connman_iptables_command("-t filter -D INPUT "
+ "-m mark --mark 2 -j LOG");
+ g_assert(err == 0);
+
+ err = __connman_iptables_commit("filter");
+ g_assert(err == 0);
+
+ err = __connman_iptables_command("-t filter -D INPUT "
+ "-m mark --mark 1 -j LOG");
+ g_assert(err == 0);
+
+ err = __connman_iptables_commit("filter");
+ g_assert(err == 0);
+}
+
+int main(int argc, char *argv[])
+{
+ int err;
+
+ g_test_init(&argc, &argv, NULL);
+
+ __connman_log_init(argv[0], "*", FALSE, FALSE,
+ "Unit Tests Connection Manager", VERSION);
+ __connman_iptables_init();
+
+ g_test_add_func("/iptables/basic0", test_iptables_basic0);
+ g_test_add_func("/iptables/basic1", test_iptables_basic1);
+ g_test_add_func("/iptables/basic2", test_iptables_basic2);
+
+ err = g_test_run();
+
+ __connman_iptables_cleanup();
+
+ return err;
+}