Annotation of embedaddon/strongswan/src/libcharon/tests/suites/test_mem_pool.c, revision 1.1.1.1

1.1       misho       1: /*
                      2:  * Copyright (C) 2014 Tobias Brunner
                      3:  * HSR Hochschule fuer Technik Rapperswil
                      4:  *
                      5:  * This program is free software; you can redistribute it and/or modify it
                      6:  * under the terms of the GNU General Public License as published by the
                      7:  * Free Software Foundation; either version 2 of the License, or (at your
                      8:  * option) any later version.  See <http://www.fsf.org/copyleft/gpl.txt>.
                      9:  *
                     10:  * This program is distributed in the hope that it will be useful, but
                     11:  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
                     12:  * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
                     13:  * for more details.
                     14:  */
                     15: 
                     16: #include "test_suite.h"
                     17: 
                     18: #include <attributes/mem_pool.h>
                     19: 
                     20: static void assert_host(char *expected, host_t *host)
                     21: {
                     22:        if (!expected)
                     23:        {
                     24:                ck_assert_msg(!host, "not expecting IP != %+H", host);
                     25:        }
                     26:        else
                     27:        {
                     28:                host_t *verifier;
                     29:                verifier = host_create_from_string(expected, 0);
                     30:                ck_assert_msg(host, "expected IP %+H != NULL", verifier);
                     31:                ck_assert_msg(verifier->ip_equals(verifier, host), "expected IP %+H != "
                     32:                                          "%+H", verifier, host);;
                     33:                verifier->destroy(verifier);
                     34:        }
                     35: }
                     36: 
                     37: static void assert_acquire(mem_pool_t *pool, char *requested, char *expected,
                     38:                                                   mem_pool_op_t operation)
                     39: {
                     40:        identification_t *id;
                     41:        host_t *req, *acquired;
                     42: 
                     43:        id = identification_create_from_string("tester");
                     44:        req = host_create_from_string(requested, 0);
                     45: 
                     46:        acquired = pool->acquire_address(pool, id, req, operation, NULL);
                     47:        assert_host(expected, acquired);
                     48:        DESTROY_IF(acquired);
                     49: 
                     50:        req->destroy(req);
                     51:        id->destroy(id);
                     52: }
                     53: 
                     54: static void assert_acquires_new(mem_pool_t *pool, char *pattern, int first)
                     55: {
                     56:        char expected[16];
                     57:        int i;
                     58: 
                     59:        for (i = 0; i < pool->get_size(pool); i++)
                     60:        {
                     61:                snprintf(expected, sizeof(expected), pattern, first + i);
                     62:                assert_acquire(pool, "0.0.0.0", expected, MEM_POOL_NEW);
                     63:                ck_assert_int_eq(i + 1, pool->get_online(pool));
                     64:        }
                     65:        assert_acquire(pool, "0.0.0.0", NULL, MEM_POOL_NEW);
                     66: }
                     67: 
                     68: START_TEST(test_config)
                     69: {
                     70:        mem_pool_t *pool;
                     71: 
                     72:        pool = mem_pool_create("test", NULL, 0);
                     73:        ck_assert_int_eq(0, pool->get_size(pool));
                     74:        assert_acquire(pool, "192.168.0.1", "192.168.0.1", MEM_POOL_NEW);
                     75:        assert_acquire(pool, "10.0.1.1", "10.0.1.1", MEM_POOL_NEW);
                     76:        assert_acquire(pool, "0.0.0.0", "0.0.0.0", MEM_POOL_NEW);
                     77:        assert_acquire(pool, "255.255.255.255", "255.255.255.255", MEM_POOL_NEW);
                     78:        ck_assert_int_eq(0, pool->get_online(pool));
                     79:        pool->destroy(pool);
                     80: }
                     81: END_TEST
                     82: 
                     83: START_TEST(test_cidr)
                     84: {
                     85:        mem_pool_t *pool;
                     86:        host_t *base;
                     87: 
                     88:        base = host_create_from_string("192.168.0.0", 0);
                     89: 
                     90:        pool = mem_pool_create("test", base, 32);
                     91:        ck_assert_int_eq(1, pool->get_size(pool));
                     92:        assert_acquires_new(pool, "192.168.0.%d", 0);
                     93:        pool->destroy(pool);
                     94: 
                     95:        pool = mem_pool_create("test", base, 31);
                     96:        ck_assert_int_eq(2, pool->get_size(pool));
                     97:        assert_acquires_new(pool, "192.168.0.%d", 0);
                     98:        pool->destroy(pool);
                     99: 
                    100:        pool = mem_pool_create("test", base, 30);
                    101:        ck_assert_int_eq(2, pool->get_size(pool));
                    102:        assert_acquires_new(pool, "192.168.0.%d", 1);
                    103:        pool->destroy(pool);
                    104: 
                    105:        pool = mem_pool_create("test", base, 29);
                    106:        ck_assert_int_eq(6, pool->get_size(pool));
                    107:        assert_acquires_new(pool, "192.168.0.%d", 1);
                    108:        pool->destroy(pool);
                    109: 
                    110:        pool = mem_pool_create("test", base, 24);
                    111:        ck_assert_int_eq(254, pool->get_size(pool));
                    112:        assert_acquires_new(pool, "192.168.0.%d", 1);
                    113:        pool->destroy(pool);
                    114: 
                    115:        base->destroy(base);
                    116: }
                    117: END_TEST
                    118: 
                    119: START_TEST(test_cidr_offset)
                    120: {
                    121:        mem_pool_t *pool;
                    122:        host_t *base;
                    123: 
                    124:        base = host_create_from_string("192.168.0.1", 0);
                    125:        pool = mem_pool_create("test", base, 31);
                    126:        ck_assert_int_eq(1, pool->get_size(pool));
                    127:        assert_acquires_new(pool, "192.168.0.%d", 1);
                    128:        pool->destroy(pool);
                    129: 
                    130:        pool = mem_pool_create("test", base, 30);
                    131:        ck_assert_int_eq(2, pool->get_size(pool));
                    132:        assert_acquires_new(pool, "192.168.0.%d", 1);
                    133:        pool->destroy(pool);
                    134:        base->destroy(base);
                    135: 
                    136:        base = host_create_from_string("192.168.0.2", 0);
                    137:        pool = mem_pool_create("test", base, 30);
                    138:        ck_assert_int_eq(1, pool->get_size(pool));
                    139:        assert_acquires_new(pool, "192.168.0.%d", 2);
                    140:        pool->destroy(pool);
                    141: 
                    142:        pool = mem_pool_create("test", base, 24);
                    143:        ck_assert_int_eq(253, pool->get_size(pool));
                    144:        assert_acquires_new(pool, "192.168.0.%d", 2);
                    145:        pool->destroy(pool);
                    146:        base->destroy(base);
                    147: 
                    148:        base = host_create_from_string("192.168.0.254", 0);
                    149:        pool = mem_pool_create("test", base, 24);
                    150:        ck_assert_int_eq(1, pool->get_size(pool));
                    151:        assert_acquires_new(pool, "192.168.0.%d", 254);
                    152:        pool->destroy(pool);
                    153:        base->destroy(base);
                    154: 
                    155:        /* due to size == 0 we get the requested IP back */
                    156:        base = host_create_from_string("192.168.0.255", 0);
                    157:        pool = mem_pool_create("test", base, 24);
                    158:        ck_assert_int_eq(0, pool->get_size(pool));
                    159:        assert_acquire(pool, "192.168.0.1", "192.168.0.1", MEM_POOL_NEW);
                    160:        pool->destroy(pool);
                    161: 
                    162:        base->destroy(base);
                    163: }
                    164: END_TEST
                    165: 
                    166: START_TEST(test_range)
                    167: {
                    168:        mem_pool_t *pool;
                    169:        host_t *from, *to;
                    170: 
                    171:        from = host_create_from_string("192.168.0.0", 0);
                    172:        to = host_create_from_string("192.168.0.0", 0);
                    173:        pool = mem_pool_create_range("test", from, to);
                    174:        ck_assert_int_eq(1, pool->get_size(pool));
                    175:        assert_acquires_new(pool, "192.168.0.%d", 0);
                    176:        pool->destroy(pool);
                    177: 
                    178:        to->destroy(to);
                    179:        to = host_create_from_string("192.168.0.1", 0);
                    180:        pool = mem_pool_create_range("test", from, to);
                    181:        ck_assert_int_eq(2, pool->get_size(pool));
                    182:        assert_acquires_new(pool, "192.168.0.%d", 0);
                    183:        pool->destroy(pool);
                    184: 
                    185:        from->destroy(from);
                    186:        from = host_create_from_string("192.168.0.10", 0);
                    187:        pool = mem_pool_create_range("test", from, to);
                    188:        ck_assert(!pool);
                    189: 
                    190:        to->destroy(to);
                    191:        to = host_create_from_string("192.168.0.20", 0);
                    192:        pool = mem_pool_create_range("test", from, to);
                    193:        ck_assert_int_eq(11, pool->get_size(pool));
                    194:        assert_acquires_new(pool, "192.168.0.%d", 10);
                    195:        pool->destroy(pool);
                    196: 
                    197:        from->destroy(from);
                    198:        from = host_create_from_string("fec::1", 0);
                    199:        to->destroy(to);
                    200:        to = host_create_from_string("fed::1", 0);
                    201:        pool = mem_pool_create_range("test", from, to);
                    202:        ck_assert(!pool);
                    203: 
                    204:        from->destroy(from);
                    205:        to->destroy(to);
                    206: }
                    207: END_TEST
                    208: 
                    209: Suite *mem_pool_suite_create()
                    210: {
                    211:        Suite *s;
                    212:        TCase *tc;
                    213: 
                    214:        s = suite_create("mem_pool");
                    215: 
                    216:        tc = tcase_create("%config-like pool");
                    217:        tcase_add_test(tc, test_config);
                    218:        suite_add_tcase(s, tc);
                    219: 
                    220:        tc = tcase_create("cidr constructor");
                    221:        tcase_add_test(tc, test_cidr);
                    222:        tcase_add_test(tc, test_cidr_offset);
                    223:        suite_add_tcase(s, tc);
                    224: 
                    225:        tc = tcase_create("range constructor");
                    226:        tcase_add_test(tc, test_range);
                    227:        suite_add_tcase(s, tc);
                    228: 
                    229:        return s;
                    230: }

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>