test-ippool: Add unit test for ippool
authorDaniel Wagner <daniel.wagner@bmw-carit.de>
Thu, 26 Jan 2012 15:03:32 +0000 (16:03 +0100)
committerDaniel Wagner <daniel.wagner@bmw-carit.de>
Wed, 1 Feb 2012 09:13:19 +0000 (10:13 +0100)
Makefile.am
unit/test-ippool.c [new file with mode: 0644]

index 4bb85cf..49f10fc 100644 (file)
@@ -145,7 +145,7 @@ noinst_PROGRAMS += tools/wispr 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 \
-                       tools/alg-test unit/test-session
+                       tools/alg-test unit/test-session unit/test-ippool
 
 tools_wispr_SOURCES = $(gweb_sources) tools/wispr.c
 tools_wispr_LDADD = @GLIB_LIBS@ @GNUTLS_LIBS@ -lresolv
@@ -188,6 +188,11 @@ unit_test_session_SOURCES = $(gdbus_sources) src/log.c src/dbus.c \
                unit/session-api.c unit/test-connman.h
 unit_test_session_LDADD = @GLIB_LIBS@ @DBUS_LIBS@ -ldl
 unit_objects += $(unit_test_session_OBJECTS)
+
+unit_test_ippool_SOURCES = $(gdbus_sources) src/log.c src/dbus.c \
+                src/ippool.c unit/test-ippool.c
+unit_test_ippool_LDADD = @GLIB_LIBS@ @DBUS_LIBS@ -ldl
+unit_objects += $(unit_test_ippool_OBJECTS)
 endif
 
 test_scripts = test/get-state test/list-services \
diff --git a/unit/test-ippool.c b/unit/test-ippool.c
new file mode 100644 (file)
index 0000000..8cbcf64
--- /dev/null
@@ -0,0 +1,154 @@
+/*
+ *
+ *  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
+
+static void test_ippool_basic0(void)
+{
+       struct connman_ippool *pool;
+       const char *gateway;
+       const char *broadcast;
+       const char *subnet_mask;
+       const char *start_ip;
+       const char *end_ip;
+       int i;
+
+       /* Test the IP range */
+
+       pool = __connman_ippool_create(1, 500);
+       g_assert(pool == NULL);
+
+       for (i = 1; i < 254; i++) {
+               pool = __connman_ippool_create(1, i);
+               g_assert(pool);
+
+               gateway = __connman_ippool_get_gateway(pool);
+               broadcast = __connman_ippool_get_broadcast(pool);
+               subnet_mask = __connman_ippool_get_subnet_mask(pool);
+               start_ip = __connman_ippool_get_start_ip(pool);
+               end_ip = __connman_ippool_get_end_ip(pool);
+
+               g_assert(gateway);
+               g_assert(broadcast);
+               g_assert(subnet_mask);
+               g_assert(start_ip);
+               g_assert(end_ip);
+
+               LOG("\n\tIP range %s --> %s\n"
+                       "\tgateway %s broadcast %s mask %s", start_ip, end_ip,
+                       gateway, broadcast, subnet_mask);
+
+               __connman_ippool_unref(pool);
+       }
+}
+
+static void test_ippool_basic1(void)
+{
+       struct connman_ippool *pool;
+       const char *gateway;
+       const char *broadcast;
+       const char *subnet_mask;
+       const char *start_ip;
+       const char *end_ip;
+       GSList *list = NULL, *it;
+       int i = 0;
+
+       /* Allocate all possible pools */
+
+       /*
+        *                                             Number of addresses
+        * 24-bit block         10.0.0.0    – 10.255.255.255    16,777,216
+        * 20-bit block         172.16.0.0  – 172.31.255.255     1,048,576
+        * 16-bit block         192.168.0.0 – 192.168.255.255       65,536
+        *
+        * Total                                                17,891,328
+        *
+        * Total numbers of 256 blocks:                             69,888
+        */
+
+       while (TRUE) {
+               pool = __connman_ippool_create(1, 100);
+               if (pool == NULL)
+                       break;
+               i += 1;
+               g_assert(i < 69888);
+
+               list = g_slist_prepend(list, pool);
+
+               gateway = __connman_ippool_get_gateway(pool);
+               broadcast = __connman_ippool_get_broadcast(pool);
+               subnet_mask = __connman_ippool_get_subnet_mask(pool);
+               start_ip = __connman_ippool_get_start_ip(pool);
+               end_ip = __connman_ippool_get_end_ip(pool);
+
+               g_assert(gateway);
+               g_assert(broadcast);
+               g_assert(subnet_mask);
+               g_assert(start_ip);
+               g_assert(end_ip);
+       }
+
+       LOG("Number of blocks %d", i);
+
+       for (it = list; it != NULL; it = it->next) {
+               pool = it->data;
+
+               __connman_ippool_unref(pool);
+       }
+
+       g_slist_free(list);
+}
+
+int main(int argc, char *argv[])
+{
+       int err;
+
+       g_test_init(&argc, &argv, NULL);
+
+       __connman_ippool_init();
+
+       g_test_add_func("/basic0", test_ippool_basic0);
+       g_test_add_func("/basic1", test_ippool_basic1);
+
+       err = g_test_run();
+
+       __connman_ippool_cleanup();
+
+       return err;
+}