Use dbus_free for deallocation, when allocated with dbus_malloc0
[platform/upstream/connman.git] / unit / test-ippool.c
old mode 100644 (file)
new mode 100755 (executable)
index f45c5b9..a6cae65
@@ -2,7 +2,7 @@
  *
  *  Connection Manager
  *
- *  Copyright (C) 2012  BWM CarIT GmbH. All rights reserved.
+ *  Copyright (C) 2012-2014  BMW Car IT GmbH.
  *
  *  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
@@ -47,13 +47,13 @@ static void test_case_1(void)
        __connman_ippool_init();
 
        pool = __connman_ippool_create(23, 1, 500, NULL, NULL);
-       g_assert(pool == NULL);
+       g_assert(!pool);
 
        for (i = 0; i < 100000; i++) {
                pool = __connman_ippool_create(23, 1, 20, NULL, NULL);
                g_assert(pool);
 
-               __connman_ippool_unref(pool);
+               __connman_ippool_free(pool);
        }
 
        __connman_ippool_cleanup();
@@ -92,7 +92,7 @@ static void test_case_2(void)
                        "\tgateway %s broadcast %s mask %s", start_ip, end_ip,
                        gateway, broadcast, subnet_mask);
 
-               __connman_ippool_unref(pool);
+               __connman_ippool_free(pool);
        }
 
        __connman_ippool_cleanup();
@@ -131,7 +131,7 @@ static void test_case_3(void)
 
        while (TRUE) {
                pool = __connman_ippool_create(23, 1, 100, NULL, NULL);
-               if (pool == NULL)
+               if (!pool)
                        break;
                i += 1;
 
@@ -154,10 +154,10 @@ static void test_case_3(void)
 
        LOG("Number of blocks %d", i);
 
-       for (it = list; it != NULL; it = it->next) {
+       for (it = list; it; it = it->next) {
                pool = it->data;
 
-               __connman_ippool_unref(pool);
+               __connman_ippool_free(pool);
        }
 
        g_slist_free(list);
@@ -219,7 +219,7 @@ static void test_case_4(void)
 
        g_assert(flag == 1);
 
-       __connman_ippool_unref(pool);
+       __connman_ippool_free(pool);
 
        flag = 0;
 
@@ -246,7 +246,187 @@ static void test_case_4(void)
 
        g_assert(flag == 1);
 
-       __connman_ippool_unref(pool);
+       __connman_ippool_free(pool);
+
+       __connman_ippool_cleanup();
+}
+
+static void test_case_5(void)
+{
+       struct connman_ippool *pool1, *pool2;
+       const char *gateway;
+       const char *broadcast;
+       const char *subnet_mask;
+       const char *start_ip;
+       const char *end_ip;
+       int flag;
+
+       __connman_ippool_init();
+
+       /* Test the IP range collision */
+
+       flag = 0;
+       start_ip = "192.168.1.2";
+       __connman_ippool_newaddr(25, start_ip, 24);
+       g_assert(flag == 0);
+
+       /* pool should return 192.168.0.1 now */
+       pool1 = __connman_ippool_create(26, 1, 100, collision_cb, &flag);
+       g_assert(pool1);
+
+       gateway = __connman_ippool_get_gateway(pool1);
+       broadcast = __connman_ippool_get_broadcast(pool1);
+       subnet_mask = __connman_ippool_get_subnet_mask(pool1);
+       start_ip = __connman_ippool_get_start_ip(pool1);
+       end_ip = __connman_ippool_get_end_ip(pool1);
+
+       g_assert(gateway);
+       g_assert(broadcast);
+       g_assert(subnet_mask);
+       g_assert(start_ip);
+       g_assert(end_ip);
+
+       g_assert_cmpstr(gateway, ==, "192.168.0.1");
+       g_assert_cmpstr(broadcast, ==, "192.168.0.255");
+       g_assert_cmpstr(subnet_mask, ==, "255.255.255.0");
+       g_assert_cmpstr(start_ip, ==, "192.168.0.1");
+       g_assert_cmpstr(end_ip, ==, "192.168.0.101");
+
+       LOG("\n\tIP range %s --> %s\n"
+               "\tgateway %s broadcast %s mask %s", start_ip, end_ip,
+               gateway, broadcast, subnet_mask);
+
+       /*
+        * Now create the pool again, we should not get collision
+        * with existing allocated address.
+        */
+
+       /* pool should return 192.168.2.1 now */
+       flag = 0;
+       pool2 = __connman_ippool_create(23, 1, 100, collision_cb, &flag);
+       g_assert(pool2);
+
+       gateway = __connman_ippool_get_gateway(pool2);
+       broadcast = __connman_ippool_get_broadcast(pool2);
+       subnet_mask = __connman_ippool_get_subnet_mask(pool2);
+       start_ip = __connman_ippool_get_start_ip(pool2);
+       end_ip = __connman_ippool_get_end_ip(pool2);
+
+       g_assert(gateway);
+       g_assert(broadcast);
+       g_assert(subnet_mask);
+       g_assert(start_ip);
+       g_assert(end_ip);
+
+       g_assert_cmpstr(gateway, ==, "192.168.2.1");
+       g_assert_cmpstr(broadcast, ==, "192.168.2.255");
+       g_assert_cmpstr(subnet_mask, ==, "255.255.255.0");
+       g_assert_cmpstr(start_ip, ==, "192.168.2.1");
+       g_assert_cmpstr(end_ip, ==, "192.168.2.101");
+
+       LOG("\n\tIP range %s --> %s\n"
+               "\tgateway %s broadcast %s mask %s", start_ip, end_ip,
+               gateway, broadcast, subnet_mask);
+
+       g_assert(flag == 0);
+
+       __connman_ippool_free(pool1);
+       __connman_ippool_free(pool2);
+
+       __connman_ippool_cleanup();
+}
+
+static void test_case_6(void)
+{
+       struct connman_ippool *pool1, *pool2;
+       const char *gateway;
+       const char *broadcast;
+       const char *subnet_mask;
+       const char *start_ip;
+       const char *end_ip;
+       int flag;
+
+       __connman_ippool_init();
+
+       /* Test the IP range collision */
+
+       flag = 0;
+       start_ip = "192.168.1.2";
+       __connman_ippool_newaddr(25, start_ip, 24);
+       g_assert(flag == 0);
+
+       flag = 0;
+       start_ip = "192.168.0.2";
+       __connman_ippool_newaddr(25, start_ip, 24);
+       g_assert(flag == 0);
+
+       /* pool should return 192.168.2.1 now */
+       pool1 = __connman_ippool_create(26, 1, 100, collision_cb, &flag);
+       g_assert(pool1);
+
+       gateway = __connman_ippool_get_gateway(pool1);
+       broadcast = __connman_ippool_get_broadcast(pool1);
+       subnet_mask = __connman_ippool_get_subnet_mask(pool1);
+       start_ip = __connman_ippool_get_start_ip(pool1);
+       end_ip = __connman_ippool_get_end_ip(pool1);
+
+       g_assert(gateway);
+       g_assert(broadcast);
+       g_assert(subnet_mask);
+       g_assert(start_ip);
+       g_assert(end_ip);
+
+       g_assert_cmpstr(gateway, ==, "192.168.2.1");
+       g_assert_cmpstr(broadcast, ==, "192.168.2.255");
+       g_assert_cmpstr(subnet_mask, ==, "255.255.255.0");
+       g_assert_cmpstr(start_ip, ==, "192.168.2.1");
+       g_assert_cmpstr(end_ip, ==, "192.168.2.101");
+
+       LOG("\n\tIP range %s --> %s\n"
+               "\tgateway %s broadcast %s mask %s", start_ip, end_ip,
+               gateway, broadcast, subnet_mask);
+
+       /*
+        * Now create the pool again, we should not get collision
+        * with existing allocated address.
+        */
+
+       /* pool should return 192.168.3.1 now */
+       flag = 0;
+       pool2 = __connman_ippool_create(23, 1, 100, collision_cb, &flag);
+       g_assert(pool2);
+
+       gateway = __connman_ippool_get_gateway(pool2);
+       broadcast = __connman_ippool_get_broadcast(pool2);
+       subnet_mask = __connman_ippool_get_subnet_mask(pool2);
+       start_ip = __connman_ippool_get_start_ip(pool2);
+       end_ip = __connman_ippool_get_end_ip(pool2);
+
+       g_assert(gateway);
+       g_assert(broadcast);
+       g_assert(subnet_mask);
+       g_assert(start_ip);
+       g_assert(end_ip);
+
+       g_assert_cmpstr(gateway, ==, "192.168.3.1");
+       g_assert_cmpstr(broadcast, ==, "192.168.3.255");
+       g_assert_cmpstr(subnet_mask, ==, "255.255.255.0");
+       g_assert_cmpstr(start_ip, ==, "192.168.3.1");
+       g_assert_cmpstr(end_ip, ==, "192.168.3.101");
+
+       LOG("\n\tIP range %s --> %s\n"
+               "\tgateway %s broadcast %s mask %s", start_ip, end_ip,
+               gateway, broadcast, subnet_mask);
+
+       g_assert(flag == 0);
+
+       flag = 0;
+       start_ip = "192.168.3.2";
+       __connman_ippool_newaddr(25, start_ip, 24);
+       g_assert(flag == 1);
+
+       __connman_ippool_free(pool1);
+       __connman_ippool_free(pool2);
 
        __connman_ippool_cleanup();
 }
@@ -259,6 +439,8 @@ int main(int argc, char *argv[])
        g_test_add_func("/ippool/Test case 2", test_case_2);
        g_test_add_func("/ippool/Test case 3", test_case_3);
        g_test_add_func("/ippool/Test case 4", test_case_4);
+       g_test_add_func("/ippool/Test case 5", test_case_5);
+       g_test_add_func("/ippool/Test case 6", test_case_6);
 
        return g_test_run();
 }