summaryrefslogtreecommitdiff
path: root/unit
diff options
context:
space:
mode:
authorDaniel Wagner <daniel.wagner@bmw-carit.de>2012-02-01 18:51:47 +0100
committerSamuel Ortiz <sameo@linux.intel.com>2012-02-13 11:33:17 +0100
commit2e6ad171779854947b4e2917e17e59ee90a81626 (patch)
treefe2e144f793f0dd3db7230981c7571dc3f1815b8 /unit
parent37cbd6d1e4f65892bab87194479a3cf86fa181aa (diff)
downloadconnman-2e6ad171779854947b4e2917e17e59ee90a81626.tar.gz
connman-2e6ad171779854947b4e2917e17e59ee90a81626.tar.bz2
connman-2e6ad171779854947b4e2917e17e59ee90a81626.zip
test-nat: Add nat unit test
Diffstat (limited to 'unit')
-rw-r--r--unit/test-nat.c126
1 files changed, 126 insertions, 0 deletions
diff --git a/unit/test-nat.c b/unit/test-nat.c
new file mode 100644
index 00000000..a601871d
--- /dev/null
+++ b/unit/test-nat.c
@@ -0,0 +1,126 @@
+/*
+ *
+ * Connection Manager
+ *
+ * Copyright (C) 2012 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"
+
+/* #define DEBUG */
+#ifdef DEBUG
+#include <stdio.h>
+
+#define LOG(fmt, arg...) do { \
+ fprintf(stdout, "%s:%s() " fmt "\n", \
+ __FILE__, __func__ , ## arg); \
+} while (0)
+#else
+#define LOG(fmt, arg...)
+#endif
+
+struct connman_notifier *nat_notifier;
+
+struct connman_service {
+ char *dummy;
+};
+
+char *connman_service_get_interface(struct connman_service *service)
+{
+ return "eth0";
+}
+
+int connman_notifier_register(struct connman_notifier *notifier)
+{
+ nat_notifier = notifier;
+
+ return 0;
+}
+
+void connman_notifier_unregister(struct connman_notifier *notifier)
+{
+ nat_notifier = NULL;
+}
+
+static void test_nat_basic0(void)
+{
+ int err;
+
+ err = __connman_nat_enable("bridge", "192.168.2.1", 24);
+ g_assert(err == 0);
+
+ /* test that table is empty */
+ err = __connman_iptables_command("-t nat -C POSTROUTING "
+ "-s 192.168.2.1/24 -o eth0 -j MASQUERADE");
+ g_assert(err != 0);
+
+ __connman_nat_disable("bridge");
+}
+
+static void test_nat_basic1(void)
+{
+ struct connman_service *service;
+ int err;
+
+ service = g_try_new0(struct connman_service, 1);
+ g_assert(service);
+
+ nat_notifier->default_changed(service);
+
+ err = __connman_nat_enable("bridge", "192.168.2.1", 24);
+ g_assert(err == 0);
+
+ /* test that table is not empty */
+ err = __connman_iptables_command("-t nat -C POSTROUTING "
+ "-s 192.168.2.1/24 -o eth0 -j MASQUERADE");
+ g_assert(err == 0);
+
+ __connman_nat_disable("bridge");
+
+ /* test that table is empty again */
+ err = __connman_iptables_command("-t nat -C POSTROUTING "
+ "-s 192.168.2.1/24 -o eth0 -j MASQUERADE");
+ g_assert(err != 0);
+}
+
+int main(int argc, char *argv[])
+{
+ int err;
+
+ g_test_init(&argc, &argv, NULL);
+
+ __connman_log_init(argv[0], "*", FALSE);
+ __connman_iptables_init();
+ __connman_nat_init();
+
+ g_test_add_func("/basic0", test_nat_basic0);
+ g_test_add_func("/basic1", test_nat_basic1);
+
+ err = g_test_run();
+
+ __connman_nat_cleanup();
+ __connman_iptables_cleanup();
+ __connman_log_cleanup();
+
+ return err;
+}