Annotation of embedaddon/strongswan/src/libstrongswan/tests/suites/test_utils.c, revision 1.1

1.1     ! misho       1: /*
        !             2:  * Copyright (C) 2013-2015 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 <library.h>
        !            19: #include <utils/utils.h>
        !            20: #include <ipsec/ipsec_types.h>
        !            21: #include <credentials/keys/public_key.h>
        !            22: 
        !            23: #include <time.h>
        !            24: 
        !            25: /*******************************************************************************
        !            26:  * object storage on lib
        !            27:  */
        !            28: 
        !            29: START_TEST(test_objects)
        !            30: {
        !            31:        char *k1 = "key1", *k2 = "key2";
        !            32:        char *v1 = "val1", *val;
        !            33: 
        !            34:        ck_assert(lib->get(lib, k1) == NULL);
        !            35: 
        !            36:        ck_assert(lib->set(lib, k1, v1));
        !            37:        ck_assert(!lib->set(lib, k1, v1));
        !            38: 
        !            39:        val = lib->get(lib, k1);
        !            40:        ck_assert(val != NULL);
        !            41:        ck_assert(streq(val, v1));
        !            42: 
        !            43:        ck_assert(lib->set(lib, k1, NULL));
        !            44:        ck_assert(!lib->set(lib, k2, NULL));
        !            45: 
        !            46:        ck_assert(lib->get(lib, k1) == NULL);
        !            47: }
        !            48: END_TEST
        !            49: 
        !            50: /*******************************************************************************
        !            51:  * test return_... functions
        !            52:  */
        !            53: 
        !            54: START_TEST(test_return_functions)
        !            55: {
        !            56:        ck_assert(return_null() == NULL);
        !            57:        ck_assert(return_null("asdf", 5, NULL, 1, "qwer") == NULL);
        !            58: 
        !            59:        ck_assert(return_true() == TRUE);
        !            60:        ck_assert(return_true("asdf", 5, NULL, 1, "qwer") == TRUE);
        !            61: 
        !            62:        ck_assert(return_false() == FALSE);
        !            63:        ck_assert(return_false("asdf", 5, NULL, 1, "qwer") == FALSE);
        !            64: 
        !            65:        ck_assert(return_failed() == FAILED);
        !            66:        ck_assert(return_failed("asdf", 5, NULL, 1, "qwer") == FAILED);
        !            67: 
        !            68:        ck_assert(return_success() == SUCCESS);
        !            69:        ck_assert(return_success("asdf", 5, NULL, 1, "qwer") == SUCCESS);
        !            70: 
        !            71:        /* just make sure this works */
        !            72:        nop();
        !            73:        nop("asdf", 5, NULL, 1, "qwer");
        !            74: }
        !            75: END_TEST
        !            76: 
        !            77: /*******************************************************************************
        !            78:  * timeval_add_ms
        !            79:  */
        !            80: 
        !            81: START_TEST(test_timeval_add_ms)
        !            82: {
        !            83:        timeval_t tv;
        !            84: 
        !            85:        tv.tv_sec = 0;
        !            86:        tv.tv_usec = 0;
        !            87:        timeval_add_ms(&tv, 0);
        !            88:        ck_assert_int_eq(tv.tv_sec, 0);
        !            89:        ck_assert_int_eq(tv.tv_usec, 0);
        !            90: 
        !            91:        timeval_add_ms(&tv, 1);
        !            92:        ck_assert_int_eq(tv.tv_sec, 0);
        !            93:        ck_assert_int_eq(tv.tv_usec, 1000);
        !            94: 
        !            95:        timeval_add_ms(&tv, 0);
        !            96:        ck_assert_int_eq(tv.tv_sec, 0);
        !            97:        ck_assert_int_eq(tv.tv_usec, 1000);
        !            98: 
        !            99:        timeval_add_ms(&tv, 999);
        !           100:        ck_assert_int_eq(tv.tv_sec, 1);
        !           101:        ck_assert_int_eq(tv.tv_usec, 0);
        !           102: 
        !           103:        timeval_add_ms(&tv, 0);
        !           104:        ck_assert_int_eq(tv.tv_sec, 1);
        !           105:        ck_assert_int_eq(tv.tv_usec, 0);
        !           106: 
        !           107:        timeval_add_ms(&tv, 1000);
        !           108:        ck_assert_int_eq(tv.tv_sec, 2);
        !           109:        ck_assert_int_eq(tv.tv_usec, 0);
        !           110: 
        !           111:        timeval_add_ms(&tv, 1500);
        !           112:        ck_assert_int_eq(tv.tv_sec, 3);
        !           113:        ck_assert_int_eq(tv.tv_usec, 500000);
        !           114: }
        !           115: END_TEST
        !           116: 
        !           117: /*******************************************************************************
        !           118:  * timespan_from_string
        !           119:  */
        !           120: 
        !           121: static struct {
        !           122:        char *s;
        !           123:        char *u;
        !           124:        bool v;
        !           125:        time_t t;
        !           126: } ts_data[] = {
        !           127:        {NULL,  NULL,   FALSE,  0},
        !           128:        {"",    NULL,   FALSE,  0},
        !           129:        {"a",   NULL,   FALSE,  0},
        !           130:        {"0",   NULL,   TRUE,   0},
        !           131:        {"5",   NULL,   TRUE,   5},
        !           132:        {"5s",  NULL,   TRUE,   5},
        !           133:        {"5m",  NULL,   TRUE,   300},
        !           134:        {"5ms", NULL,   TRUE,   300},
        !           135:        {"5h",  NULL,   TRUE,   18000},
        !           136:        {"5d",  NULL,   TRUE,   432000},
        !           137:        {"5x",  NULL,   FALSE,  0},
        !           138:        {"5",   "",             TRUE,   5},
        !           139:        {"5",   "m",    TRUE,   300},
        !           140:        {"5",   "ms",   TRUE,   300},
        !           141:        {"5",   "x",    FALSE,  0},
        !           142:        {"5x",  "m",    FALSE,  0},
        !           143:        {"18446744073709551616",        NULL,   FALSE,  0},
        !           144: };
        !           145: 
        !           146: START_TEST(test_timespan_from_string)
        !           147: {
        !           148:        time_t val = 42;
        !           149: 
        !           150:        ck_assert(timespan_from_string(ts_data[_i].s, ts_data[_i].u,
        !           151:                                                                   NULL) == ts_data[_i].v);
        !           152:        ck_assert(timespan_from_string(ts_data[_i].s, ts_data[_i].u,
        !           153:                                                                   &val) == ts_data[_i].v);
        !           154:        if (ts_data[_i].v)
        !           155:        {
        !           156:                ck_assert_int_eq(val, ts_data[_i].t);
        !           157:        }
        !           158:        else
        !           159:        {
        !           160:                ck_assert_int_eq(val, 42);
        !           161:        }
        !           162: }
        !           163: END_TEST
        !           164: 
        !           165: /*******************************************************************************
        !           166:  * htoun/untoh
        !           167:  */
        !           168: 
        !           169: START_TEST(test_htoun)
        !           170: {
        !           171:        chunk_t net64, expected;
        !           172:        uint16_t host16 = 513;
        !           173:        uint32_t net16 = 0, host32 = 67305985;
        !           174:        uint64_t net32 = 0, host64 = 578437695752307201ULL;
        !           175: 
        !           176:        net64 = chunk_alloca(16);
        !           177:        memset(net64.ptr, 0, net64.len);
        !           178: 
        !           179:        expected = chunk_from_chars(0x00, 0x02, 0x01, 0x00);
        !           180:        htoun16((char*)&net16 + 1, host16);
        !           181:        ck_assert(chunk_equals(expected, chunk_from_thing(net16)));
        !           182: 
        !           183:        expected = chunk_from_chars(0x00, 0x00, 0x04, 0x03, 0x02, 0x01, 0x00, 0x00);
        !           184:        htoun32((uint16_t*)&net32 + 1, host32);
        !           185:        ck_assert(chunk_equals(expected, chunk_from_thing(net32)));
        !           186: 
        !           187:        expected = chunk_from_chars(0x00, 0x00, 0x00, 0x00,
        !           188:                                                                0x08, 0x07, 0x06, 0x05,
        !           189:                                                                0x04, 0x03, 0x02, 0x01,
        !           190:                                                                0x00, 0x00, 0x00, 0x00);
        !           191:        htoun64((uint32_t*)net64.ptr + 1, host64);
        !           192:        ck_assert(chunk_equals(expected, net64));
        !           193: }
        !           194: END_TEST
        !           195: 
        !           196: START_TEST(test_untoh)
        !           197: {
        !           198:        chunk_t net;
        !           199:        uint16_t host16;
        !           200:        uint32_t host32;
        !           201:        uint64_t host64;
        !           202: 
        !           203:        net = chunk_from_chars(0x00, 0x02, 0x01, 0x00);
        !           204:        host16 = untoh16(net.ptr + 1);
        !           205:        ck_assert(host16 == 513);
        !           206: 
        !           207:        net = chunk_from_chars(0x00, 0x00, 0x04, 0x03, 0x02, 0x01, 0x00, 0x00);
        !           208:        host32 = untoh32(net.ptr + 2);
        !           209:        ck_assert(host32 == 67305985);
        !           210: 
        !           211:        net = chunk_from_chars(0x00, 0x00, 0x00, 0x00, 0x08, 0x07, 0x06, 0x05,
        !           212:                                                   0x04, 0x03, 0x02, 0x01, 0x00, 0x00, 0x00, 0x00);
        !           213:        host64 = untoh64(net.ptr + 4);
        !           214:        ck_assert(host64 == 578437695752307201ULL);
        !           215: }
        !           216: END_TEST
        !           217: 
        !           218: /*******************************************************************************
        !           219:  * pad_len/round_up/down
        !           220:  */
        !           221: 
        !           222: START_TEST(test_round)
        !           223: {
        !           224:        ck_assert_int_eq(pad_len(0, 4), 0);
        !           225:        ck_assert_int_eq(pad_len(1, 4), 3);
        !           226:        ck_assert_int_eq(pad_len(2, 4), 2);
        !           227:        ck_assert_int_eq(pad_len(3, 4), 1);
        !           228:        ck_assert_int_eq(pad_len(4, 4), 0);
        !           229:        ck_assert_int_eq(pad_len(5, 4), 3);
        !           230: 
        !           231:        ck_assert_int_eq(round_up(0, 4), 0);
        !           232:        ck_assert_int_eq(round_up(1, 4), 4);
        !           233:        ck_assert_int_eq(round_up(2, 4), 4);
        !           234:        ck_assert_int_eq(round_up(3, 4), 4);
        !           235:        ck_assert_int_eq(round_up(4, 4), 4);
        !           236:        ck_assert_int_eq(round_up(5, 4), 8);
        !           237: 
        !           238:        ck_assert_int_eq(round_down(0, 4), 0);
        !           239:        ck_assert_int_eq(round_down(1, 4), 0);
        !           240:        ck_assert_int_eq(round_down(2, 4), 0);
        !           241:        ck_assert_int_eq(round_down(3, 4), 0);
        !           242:        ck_assert_int_eq(round_down(4, 4), 4);
        !           243:        ck_assert_int_eq(round_down(5, 4), 4);
        !           244: }
        !           245: END_TEST
        !           246: 
        !           247: /*******************************************************************************
        !           248:  * streq
        !           249:  */
        !           250: 
        !           251: static struct {
        !           252:        char *a;
        !           253:        char *b;
        !           254:        bool eq;
        !           255:        bool case_eq;
        !           256: } streq_data[] = {
        !           257:        {NULL, NULL, TRUE, TRUE},
        !           258:        {NULL, "", FALSE, FALSE},
        !           259:        {"", NULL, FALSE, FALSE},
        !           260:        {"abc", "", FALSE, FALSE},
        !           261:        {"abc", "abc", TRUE, TRUE},
        !           262:        {"abc", "ABC", FALSE, TRUE},
        !           263: };
        !           264: 
        !           265: START_TEST(test_streq)
        !           266: {
        !           267:        bool eq;
        !           268: 
        !           269:        ck_assert(streq(streq_data[_i].a, streq_data[_i].a));
        !           270:        ck_assert(streq(streq_data[_i].b, streq_data[_i].b));
        !           271:        eq = streq(streq_data[_i].a, streq_data[_i].b);
        !           272:        ck_assert(eq == streq_data[_i].eq);
        !           273: 
        !           274:        ck_assert(strcaseeq(streq_data[_i].a, streq_data[_i].a));
        !           275:        ck_assert(strcaseeq(streq_data[_i].b, streq_data[_i].b));
        !           276:        eq = strcaseeq(streq_data[_i].a, streq_data[_i].b);
        !           277:        ck_assert(eq == streq_data[_i].case_eq);
        !           278: }
        !           279: END_TEST
        !           280: 
        !           281: /*******************************************************************************
        !           282:  * strneq
        !           283:  */
        !           284: 
        !           285: static struct {
        !           286:        char *a;
        !           287:        char *b;
        !           288:        size_t n;
        !           289:        bool eq;
        !           290:        bool case_eq;
        !           291: } strneq_data[] = {
        !           292:        {NULL, NULL, 0, TRUE, TRUE},
        !           293:        {NULL, NULL, 10, TRUE, TRUE},
        !           294:        {NULL, "", 0, FALSE, FALSE},
        !           295:        {"", NULL, 0, FALSE, FALSE},
        !           296:        {"abc", "", 0, TRUE, TRUE},
        !           297:        {"abc", "", 1, FALSE, FALSE},
        !           298:        {"abc", "ab", 1, TRUE, TRUE},
        !           299:        {"abc", "ab", 2, TRUE, TRUE},
        !           300:        {"abc", "ab", 3, FALSE, FALSE},
        !           301:        {"abc", "abc", 3, TRUE, TRUE},
        !           302:        {"abc", "abc", 4, TRUE, TRUE},
        !           303:        {"abc", "abC", 2, TRUE, TRUE},
        !           304:        {"abc", "abC", 3, FALSE, TRUE},
        !           305: };
        !           306: 
        !           307: START_TEST(test_strneq)
        !           308: {
        !           309:        bool eq;
        !           310: 
        !           311:        ck_assert(strneq(strneq_data[_i].a, strneq_data[_i].a, strneq_data[_i].n));
        !           312:        ck_assert(strneq(strneq_data[_i].b, strneq_data[_i].b, strneq_data[_i].n));
        !           313:        eq = strneq(strneq_data[_i].a, strneq_data[_i].b, strneq_data[_i].n);
        !           314:        ck_assert(eq == strneq_data[_i].eq);
        !           315: 
        !           316:        ck_assert(strncaseeq(strneq_data[_i].a, strneq_data[_i].a,  strneq_data[_i].n));
        !           317:        ck_assert(strncaseeq(strneq_data[_i].b, strneq_data[_i].b,  strneq_data[_i].n));
        !           318:        eq = strncaseeq(strneq_data[_i].a, strneq_data[_i].b,  strneq_data[_i].n);
        !           319:        ck_assert(eq == strneq_data[_i].case_eq);
        !           320: }
        !           321: END_TEST
        !           322: 
        !           323: /*******************************************************************************
        !           324:  * strpfx
        !           325:  */
        !           326: 
        !           327: static struct {
        !           328:        char *str;
        !           329:        char *pfx;
        !           330:        bool prefix;
        !           331:        bool case_prefix;
        !           332: } strpfx_data[] = {
        !           333:        {"", "", TRUE, TRUE},
        !           334:        {"abc", "", TRUE, TRUE},
        !           335:        {"abc", "a", TRUE, TRUE},
        !           336:        {"abc", "ab", TRUE, TRUE},
        !           337:        {"abc", "abc", TRUE, TRUE},
        !           338:        {"abc", "abcd", FALSE, FALSE},
        !           339:        {"abc", "AB", FALSE, TRUE},
        !           340:        {"ABC", "ab", FALSE, TRUE},
        !           341:        {" abc", "abc", FALSE, FALSE},
        !           342: };
        !           343: 
        !           344: START_TEST(test_strpfx)
        !           345: {
        !           346:        bool prefix;
        !           347: 
        !           348:        prefix = strpfx(strpfx_data[_i].str, strpfx_data[_i].pfx);
        !           349:        ck_assert(prefix == strpfx_data[_i].prefix);
        !           350:        prefix = strcasepfx(strpfx_data[_i].str, strpfx_data[_i].pfx);
        !           351:        ck_assert(prefix == strpfx_data[_i].case_prefix);
        !           352: }
        !           353: END_TEST
        !           354: 
        !           355: /*******************************************************************************
        !           356:  * malloc_align/free_align
        !           357:  */
        !           358: 
        !           359: START_TEST(test_malloc_align)
        !           360: {
        !           361:        void *ptr[128][256];
        !           362:        int size, align;
        !           363: 
        !           364:        for (size = 0; size < countof(ptr); size++)
        !           365:        {
        !           366:                for (align = 0; align < countof(ptr[0]); align++)
        !           367:                {
        !           368:                        ptr[size][align] = malloc_align(size, align);
        !           369:                        if (align)
        !           370:                        {
        !           371:                                ck_assert((uintptr_t)ptr[size][align] % align == 0);
        !           372:                        }
        !           373:                        if (size)
        !           374:                        {
        !           375:                                ck_assert(ptr[size][align]);
        !           376:                                memset(ptr[size][align], 0xEF, size);
        !           377:                        }
        !           378:                }
        !           379:        }
        !           380:        for (size = 0; size < countof(ptr); size++)
        !           381:        {
        !           382:                for (align = 0; align < countof(ptr[0]); align++)
        !           383:                {
        !           384:                        free_align(ptr[size][align]);
        !           385:                }
        !           386:        }
        !           387: }
        !           388: END_TEST
        !           389: 
        !           390: /*******************************************************************************
        !           391:  * memxor
        !           392:  */
        !           393: 
        !           394: static void do_memxor(chunk_t a, chunk_t b, chunk_t exp)
        !           395: {
        !           396:        chunk_t dst;
        !           397: 
        !           398:        dst = chunk_clonea(a);
        !           399:        dst.len = b.len;
        !           400:        memxor(dst.ptr, b.ptr, b.len);
        !           401:        ck_assert(chunk_equals(dst, exp));
        !           402: }
        !           403: 
        !           404: START_TEST(test_memxor)
        !           405: {
        !           406:        chunk_t a, b, dst;
        !           407:        int i;
        !           408: 
        !           409:        a = chunk_alloca(64);
        !           410:        memset(a.ptr, 0, a.len);
        !           411:        b = chunk_alloca(64);
        !           412:        for (i = 0; i < 64; i++)
        !           413:        {
        !           414:                b.ptr[i] = i;
        !           415:                b.len = i;
        !           416:                do_memxor(a, b, b);
        !           417:        }
        !           418:        b.len = 64;
        !           419:        do_memxor(a, b, b);
        !           420: 
        !           421:        dst = chunk_clonea(a);
        !           422:        memxor(dst.ptr, b.ptr, b.len);
        !           423:        ck_assert(chunk_equals(dst, b));
        !           424: 
        !           425:        memxor(dst.ptr, b.ptr, 0);
        !           426:        memxor(dst.ptr, b.ptr, 1);
        !           427:        memxor(dst.ptr + 1, b.ptr + 1, 1);
        !           428:        memxor(dst.ptr + 2, b.ptr + 2, b.len - 2);
        !           429:        ck_assert(chunk_equals(dst, a));
        !           430: }
        !           431: END_TEST
        !           432: 
        !           433: START_TEST(test_memxor_aligned)
        !           434: {
        !           435:        uint64_t a = 0, b = 0;
        !           436:        chunk_t ca, cb;
        !           437:        int i;
        !           438: 
        !           439:        ca = chunk_from_thing(a);
        !           440:        cb = chunk_from_thing(b);
        !           441: 
        !           442:        for (i = 0; i < 8; i++)
        !           443:        {
        !           444:                cb.ptr[i] = i + 1;
        !           445:        }
        !           446: 
        !           447:        /* 64-bit aligned */
        !           448:        memxor(ca.ptr, cb.ptr, 8);
        !           449:        ck_assert(a == b);
        !           450:        /* 32-bit aligned source */
        !           451:        a = 0;
        !           452:        memxor(ca.ptr, cb.ptr + 4, 4);
        !           453:        ck_assert(chunk_equals(ca, chunk_from_chars(0x05, 0x06, 0x07, 0x08,
        !           454:                                                                                                0x00, 0x00, 0x00, 0x00)));
        !           455:        /* 16-bit aligned source */
        !           456:        a = 0;
        !           457:        memxor(ca.ptr, cb.ptr + 2, 6);
        !           458:        ck_assert(chunk_equals(ca, chunk_from_chars(0x03, 0x04, 0x05, 0x06,
        !           459:                                                                                                0x07, 0x08, 0x00, 0x00)));
        !           460:        /* 8-bit aligned source */
        !           461:        a = 0;
        !           462:        memxor(ca.ptr, cb.ptr + 1, 7);
        !           463:        ck_assert(chunk_equals(ca, chunk_from_chars(0x02, 0x03, 0x04, 0x05,
        !           464:                                                                                                0x06, 0x07, 0x08, 0x00)));
        !           465: }
        !           466: END_TEST
        !           467: 
        !           468: /*******************************************************************************
        !           469:  * memeq/const
        !           470:  */
        !           471: 
        !           472: static struct {
        !           473:        char *a;
        !           474:        char *b;
        !           475:        size_t n;
        !           476:        bool res;
        !           477: } memeq_data[] = {
        !           478:        {NULL, NULL, 0, TRUE},
        !           479:        {"a", "b", 0, TRUE},
        !           480:        {"", "", 1, TRUE},
        !           481:        {"abcdefgh", "abcdefgh", 8, TRUE},
        !           482:        {"a", "b", 1, FALSE},
        !           483:        {"A", "a", 1, FALSE},
        !           484:        {"\0a", "\0b", 2, FALSE},
        !           485:        {"abc", "abd", 3, FALSE},
        !           486:        {"abc", "dbd", 3, FALSE},
        !           487:        {"abcdefgh", "abcdffgh", 8, FALSE},
        !           488:        {"abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz",
        !           489:         "abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz", 52, TRUE},
        !           490:        {"abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz",
        !           491:         "abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyy", 52, FALSE},
        !           492:        {"bbcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz",
        !           493:         "abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz", 52, FALSE},
        !           494: };
        !           495: 
        !           496: START_TEST(test_memeq)
        !           497: {
        !           498:        ck_assert(memeq(memeq_data[_i].a, memeq_data[_i].b,
        !           499:                                        memeq_data[_i].n) == memeq_data[_i].res);
        !           500: }
        !           501: END_TEST
        !           502: 
        !           503: START_TEST(test_memeq_const)
        !           504: {
        !           505:        ck_assert(memeq_const(memeq_data[_i].a, memeq_data[_i].b,
        !           506:                                                  memeq_data[_i].n) == memeq_data[_i].res);
        !           507: }
        !           508: END_TEST
        !           509: 
        !           510: /*******************************************************************************
        !           511:  * memstr
        !           512:  */
        !           513: 
        !           514: static struct {
        !           515:        char *haystack;
        !           516:        char *needle;
        !           517:        size_t n;
        !           518:        int offset;
        !           519: } memstr_data[] = {
        !           520:        {NULL, NULL, 0, -1},
        !           521:        {NULL, NULL, 3, -1},
        !           522:        {NULL, "abc", 0, -1},
        !           523:        {NULL, "abc", 3, -1},
        !           524:        {"", "", 0, -1},
        !           525:        {"abc", NULL, 3, -1},
        !           526:        {"abc", "", 3, -1},
        !           527:        {"abc", "abc", 3, 0},
        !           528:        {" abc", "abc", 4, 1},
        !           529:        {" abc", "abc", 3, -1},
        !           530:        {"abcabc", "abc", 6, 0},
        !           531:        {" abc ", "abc", 5, 1},
        !           532: };
        !           533: 
        !           534: START_TEST(test_memstr)
        !           535: {
        !           536:        char *ret;
        !           537: 
        !           538:        ret = memstr(memstr_data[_i].haystack, memstr_data[_i].needle, memstr_data[_i].n);
        !           539:        if (memstr_data[_i].offset >= 0)
        !           540:        {
        !           541:                ck_assert(ret == memstr_data[_i].haystack + memstr_data[_i].offset);
        !           542:        }
        !           543:        else
        !           544:        {
        !           545:                ck_assert(ret == NULL);
        !           546:        }
        !           547: }
        !           548: END_TEST
        !           549: 
        !           550: /*******************************************************************************
        !           551:  * memwipe
        !           552:  */
        !           553: 
        !           554: START_TEST(test_memwipe_null)
        !           555: {
        !           556:        memwipe(NULL, 16);
        !           557: }
        !           558: END_TEST
        !           559: 
        !           560: static inline bool test_wiped_memory(char *buf, size_t len)
        !           561: {
        !           562:        int i;
        !           563: 
        !           564:        /* comparing two bytes at once reduces the chances of conflicts if memory
        !           565:         * got overwritten already */
        !           566:        for (i = 0; i < len; i += 2)
        !           567:        {
        !           568:                if (buf[i] != 0 && buf[i] == i &&
        !           569:                        buf[i+1] != 0 && buf[i+1] == i+1)
        !           570:                {
        !           571:                        return FALSE;
        !           572:                }
        !           573:        }
        !           574:        return TRUE;
        !           575: }
        !           576: 
        !           577: START_TEST(test_memwipe_stack)
        !           578: {
        !           579:        char buf[64];
        !           580:        int i;
        !           581: 
        !           582:        for (i = 0; i < sizeof(buf); i++)
        !           583:        {
        !           584:                buf[i] = i;
        !           585:        }
        !           586:        memwipe(buf, sizeof(buf));
        !           587:        ck_assert(test_wiped_memory(buf, sizeof(buf)));
        !           588: }
        !           589: END_TEST
        !           590: 
        !           591: START_TEST(test_memwipe_heap)
        !           592: {
        !           593:        size_t len = 64;
        !           594:        char *buf = malloc(len);
        !           595:        int i;
        !           596: 
        !           597:        for (i = 0; i < len; i++)
        !           598:        {
        !           599:                buf[i] = i;
        !           600:        }
        !           601:        memwipe(buf, len);
        !           602:        ck_assert(test_wiped_memory(buf, len));
        !           603:        free(buf);
        !           604: }
        !           605: END_TEST
        !           606: 
        !           607: /*******************************************************************************
        !           608:  * utils_memrchr
        !           609:  */
        !           610: 
        !           611: static struct {
        !           612:        char *s;
        !           613:        int c;
        !           614:        size_t n;
        !           615:        int offset;
        !           616: } memrchr_data[] = {
        !           617:        {NULL, 'f', 0, -1},
        !           618:        {NULL, 'f', 3, -1},
        !           619:        {"", 'f', 0, -1},
        !           620:        {"", '\0', 1, 0},
        !           621:        {"foo", '\0', 3, -1},
        !           622:        {"foo", '\0', 4, 3},
        !           623:        {"foo", 'f', 3, 0},
        !           624:        {"foo", 'o', 3, 2},
        !           625:        {"foo", 'o', 2, 1},
        !           626:        {"foo", 'o', 1, -1},
        !           627:        {"foo", 'o', 0, -1},
        !           628:        {"foo", 'x', 3, -1},
        !           629: };
        !           630: 
        !           631: START_TEST(test_utils_memrchr)
        !           632: {
        !           633:        void *ret;
        !           634: 
        !           635:        ret = utils_memrchr(memrchr_data[_i].s, memrchr_data[_i].c, memrchr_data[_i].n);
        !           636:        if (memrchr_data[_i].offset >= 0)
        !           637:        {
        !           638:                ck_assert(ret == memrchr_data[_i].s + memrchr_data[_i].offset);
        !           639:        }
        !           640:        else
        !           641:        {
        !           642:                ck_assert(ret == NULL);
        !           643:        }
        !           644: }
        !           645: END_TEST
        !           646: 
        !           647: /*******************************************************************************
        !           648:  * translate
        !           649:  */
        !           650: 
        !           651: static struct {
        !           652:        char *in;
        !           653:        char *from;
        !           654:        char *to;
        !           655:        char *out;
        !           656: } translate_data[] = {
        !           657:        {NULL, "", "", NULL},
        !           658:        {"abc", "", "", "abc"},
        !           659:        {"abc", "", "x", "abc"},
        !           660:        {"abc", "x", "", "abc"},
        !           661:        {"abc", "abc", "xyz", "xyz"},
        !           662:        {"aabbcc", "abc", "xyz", "xxyyzz"},
        !           663:        {"abbaccb", "abc", "xyz", "xyyxzzy"},
        !           664:        {"abxyzc", "abc", "xyz", "xyxyzz"},
        !           665:        {"abcdef", "abc", "xyz", "xyzdef"},
        !           666:        {"aaa", "abc", "xyz", "xxx"},
        !           667:        {"abc", "aaa", "xyz", "xbc"},
        !           668:        {"abc", "abc", "xxx", "xxx"},
        !           669: };
        !           670: 
        !           671: START_TEST(test_translate)
        !           672: {
        !           673:        char *str, *ret;
        !           674: 
        !           675:        str = strdupnull(translate_data[_i].in);
        !           676:        ret = translate(str, translate_data[_i].from, translate_data[_i].to);
        !           677:        ck_assert(ret == str);
        !           678:        if (ret != translate_data[_i].out)
        !           679:        {
        !           680:                ck_assert_str_eq(str, translate_data[_i].out);
        !           681:        }
        !           682:        free(str);
        !           683: }
        !           684: END_TEST
        !           685: 
        !           686: /*******************************************************************************
        !           687:  * strreplace
        !           688:  */
        !           689: 
        !           690: static struct {
        !           691:        char *in;
        !           692:        char *out;
        !           693:        char *search;
        !           694:        char *replace;
        !           695:        bool allocated;
        !           696: } strreplace_data[] = {
        !           697:        /* invalid arguments */
        !           698:        {NULL, NULL, NULL, NULL, FALSE},
        !           699:        {"", "", NULL, NULL, FALSE},
        !           700:        {"", "", "", NULL, FALSE},
        !           701:        {"", "", NULL, "", FALSE},
        !           702:        {"", "", "", "", FALSE},
        !           703:        {"", "", "", "asdf", FALSE},
        !           704:        {"", "", "asdf", "", FALSE},
        !           705:        {"asdf", "asdf", NULL, NULL, FALSE},
        !           706:        {"asdf", "asdf", "", NULL, FALSE},
        !           707:        {"asdf", "asdf", NULL, "", FALSE},
        !           708:        {"asdf", "asdf", "", "", FALSE},
        !           709:        {"asdf", "asdf", "", "asdf", FALSE},
        !           710:        {"asdf", "asdf", "asdf", NULL, FALSE},
        !           711:        {"qwer", "qwer", "", "asdf", FALSE},
        !           712:        /* replacement shorter */
        !           713:        {"asdf", "", "asdf", "", TRUE},
        !           714:        {"asdfasdf", "", "asdf", "", TRUE},
        !           715:        {"asasdfdf", "asdf", "asdf", "", TRUE},
        !           716:        {"asdf", "df", "as", "", TRUE},
        !           717:        {"asdf", "as", "df", "", TRUE},
        !           718:        {"qwer", "qwer", "asdf", "", FALSE},
        !           719:        /* replacement same length */
        !           720:        {"a", "b", "a", "b", TRUE},
        !           721:        {"aaa", "bbb", "a", "b", TRUE},
        !           722:        {"aaa", "bbb", "aaa", "bbb", TRUE},
        !           723:        {"asdf", "asdf", "asdf", "asdf", TRUE},
        !           724:        {"qwer", "qwer", "asdf", "asdf", FALSE},
        !           725:        /* replacement longer */
        !           726:        {"asdf", "asdf", "", "asdf", FALSE},
        !           727:        {"asdf", "asdfasdf", "asdf", "asdfasdf", TRUE},
        !           728:        {"asdf", "asdfsdf", "a", "asdf", TRUE},
        !           729:        {"asdf", "asdasdf", "f", "asdf", TRUE},
        !           730:        {"aaa", "asdfasdfasdf", "a", "asdf", TRUE},
        !           731:        {"qwer", "qwer", "asdf", "asdfasdf", FALSE},
        !           732:        /* real examples */
        !           733:        {"http://x.org/no/spaces", "http://x.org/no/spaces", " ", "%20", FALSE},
        !           734:        {"http://x.org/end ", "http://x.org/end%20", " ", "%20", TRUE},
        !           735:        {" http://x.org/start", "%20http://x.org/start", " ", "%20", TRUE},
        !           736:        {" http://x.org/both ", "%20http://x.org/both%20", " ", "%20", TRUE},
        !           737:        {"http://x.org/ /slash", "http://x.org/%20/slash", " ", "%20", TRUE},
        !           738:        {"http://x.org/   /three", "http://x.org/%20%20%20/three", " ", "%20", TRUE},
        !           739:        {"http://x.org/      ", "http://x.org/%20%20%20%20%20%20", " ", "%20", TRUE},
        !           740:        {"http://x.org/%20/encoded", "http://x.org/%20/encoded", " ", "%20", FALSE},
        !           741: };
        !           742: 
        !           743: START_TEST(test_strreplace)
        !           744: {
        !           745:        char *ret;
        !           746: 
        !           747:        ret = strreplace(strreplace_data[_i].in, strreplace_data[_i].search,
        !           748:                                         strreplace_data[_i].replace);
        !           749:        if (ret && strreplace_data[_i].out)
        !           750:        {
        !           751:                ck_assert_str_eq(ret, strreplace_data[_i].out);
        !           752:        }
        !           753:        else
        !           754:        {
        !           755:                ck_assert(ret == strreplace_data[_i].out);
        !           756:        }
        !           757:        if (strreplace_data[_i].allocated)
        !           758:        {
        !           759:                ck_assert(ret != strreplace_data[_i].in);
        !           760:                free(ret);
        !           761:        }
        !           762:        else
        !           763:        {
        !           764:                ck_assert(ret == strreplace_data[_i].in);
        !           765:        }
        !           766: }
        !           767: END_TEST
        !           768: 
        !           769: /*******************************************************************************
        !           770:  * path_dirname/basename/absolute
        !           771:  */
        !           772: 
        !           773: static struct {
        !           774:        char *path;
        !           775:        char *dir;
        !           776:        char *base;
        !           777:        bool absolute;
        !           778: } path_data[] = {
        !           779:        {NULL, ".", ".", FALSE},
        !           780:        {"", ".", ".", FALSE},
        !           781:        {".", ".", ".", FALSE},
        !           782:        {"..", ".", "..", FALSE},
        !           783: #ifdef WIN32
        !           784:        {"C:\\", "C:", "C:", TRUE},
        !           785:        {"X:\\\\", "X:", "X:", TRUE},
        !           786:        {"foo", ".", "foo", FALSE},
        !           787:        {"f\\", ".", "f", FALSE},
        !           788:        {"foo\\", ".", "foo", FALSE},
        !           789:        {"foo\\\\", ".", "foo", FALSE},
        !           790:        {"d:\\f", "d:", "f", TRUE},
        !           791:        {"C:\\f\\", "C:", "f", TRUE},
        !           792:        {"C:\\foo", "C:", "foo", TRUE},
        !           793:        {"C:\\foo\\", "C:", "foo", TRUE},
        !           794:        {"foo\\bar", "foo", "bar", FALSE},
        !           795:        {"foo\\\\bar", "foo", "bar", FALSE},
        !           796:        {"C:\\foo\\bar", "C:\\foo", "bar", TRUE},
        !           797:        {"C:\\foo\\bar\\", "C:\\foo", "bar", TRUE},
        !           798:        {"C:\\foo\\bar\\baz", "C:\\foo\\bar", "baz", TRUE},
        !           799:        {"\\foo\\bar", "\\foo", "bar", FALSE},
        !           800:        {"\\\\foo\\bar", "\\\\foo", "bar", TRUE},
        !           801: #else /* !WIN32 */
        !           802:        {"/", "/", "/", TRUE},
        !           803:        {"//", "/", "/", TRUE},
        !           804:        {"foo", ".", "foo", FALSE},
        !           805:        {"f/", ".", "f", FALSE},
        !           806:        {"foo/", ".", "foo", FALSE},
        !           807:        {"foo//", ".", "foo", FALSE},
        !           808:        {"/f", "/", "f", TRUE},
        !           809:        {"/f/", "/", "f", TRUE},
        !           810:        {"/foo", "/", "foo", TRUE},
        !           811:        {"/foo/", "/", "foo", TRUE},
        !           812:        {"//foo/", "/", "foo", TRUE},
        !           813:        {"foo/bar", "foo", "bar", FALSE},
        !           814:        {"foo//bar", "foo", "bar", FALSE},
        !           815:        {"/foo/bar", "/foo", "bar", TRUE},
        !           816:        {"/foo/bar/", "/foo", "bar", TRUE},
        !           817:        {"/foo/bar/baz", "/foo/bar", "baz", TRUE},
        !           818: #endif
        !           819: };
        !           820: 
        !           821: START_TEST(test_path_dirname)
        !           822: {
        !           823:        char *dir;
        !           824: 
        !           825:        dir = path_dirname(path_data[_i].path);
        !           826:        ck_assert_str_eq(path_data[_i].dir, dir);
        !           827:        free(dir);
        !           828: }
        !           829: END_TEST
        !           830: 
        !           831: START_TEST(test_path_basename)
        !           832: {
        !           833:        char *base;
        !           834: 
        !           835:        base = path_basename(path_data[_i].path);
        !           836:        ck_assert_str_eq(path_data[_i].base, base);
        !           837:        free(base);
        !           838: }
        !           839: END_TEST
        !           840: 
        !           841: START_TEST(test_path_absolute)
        !           842: {
        !           843:        ck_assert(path_data[_i].absolute == path_absolute(path_data[_i].path));
        !           844: }
        !           845: END_TEST
        !           846: 
        !           847: /*******************************************************************************
        !           848:  * time_printf_hook
        !           849:  */
        !           850: 
        !           851: static struct {
        !           852:        time_t in;
        !           853:        bool utc;
        !           854:        char *out;
        !           855: } time_data[] = {
        !           856:        {UNDEFINED_TIME, FALSE, "--- -- --:--:-- ----"},
        !           857:        {UNDEFINED_TIME, TRUE , "--- -- --:--:-- UTC ----"},
        !           858:        {1, FALSE, "Jan 01 01:00:01 1970"},
        !           859:        {1, TRUE , "Jan 01 00:00:01 UTC 1970"},
        !           860:        {1341150196, FALSE, "Jul 01 15:43:16 2012"},
        !           861:        {1341150196, TRUE , "Jul 01 13:43:16 UTC 2012"},
        !           862: };
        !           863: 
        !           864: START_TEST(test_time_printf_hook)
        !           865: {
        !           866:        char buf[32];
        !           867:        int len;
        !           868: 
        !           869:        len = snprintf(buf, sizeof(buf), "%T", &time_data[_i].in, time_data[_i].utc);
        !           870:        ck_assert(len >= 0 && len < sizeof(buf));
        !           871:        ck_assert_str_eq(buf, time_data[_i].out);
        !           872: }
        !           873: END_TEST
        !           874: 
        !           875: /*******************************************************************************
        !           876:  * time_delta_printf_hook
        !           877:  */
        !           878: 
        !           879: static struct {
        !           880:        time_t a;
        !           881:        time_t b;
        !           882:        char *out;
        !           883: } time_delta_data[] = {
        !           884:        {0, 0, "0 seconds"},
        !           885:        {0, 1, "1 second"},
        !           886:        {0, -1, "1 second"},
        !           887:        {1, 0, "1 second"},
        !           888:        {0, 2, "2 seconds"},
        !           889:        {2, 0, "2 seconds"},
        !           890:        {0, 60, "60 seconds"},
        !           891:        {0, 120, "120 seconds"},
        !           892:        {0, 121, "2 minutes"},
        !           893:        {0, 3600, "60 minutes"},
        !           894:        {0, 7200, "120 minutes"},
        !           895:        {0, 7201, "2 hours"},
        !           896:        {0, 86400, "24 hours"},
        !           897:        {0, 172800, "48 hours"},
        !           898:        {0, 172801, "2 days"},
        !           899:        {172801, 86400, "24 hours"},
        !           900: };
        !           901: 
        !           902: START_TEST(test_time_delta_printf_hook)
        !           903: {
        !           904:        char buf[16];
        !           905:        int len;
        !           906: 
        !           907:        len = snprintf(buf, sizeof(buf), "%V", &time_delta_data[_i].a, &time_delta_data[_i].b);
        !           908:        ck_assert(len >= 0 && len < sizeof(buf));
        !           909:        ck_assert_str_eq(buf, time_delta_data[_i].out);
        !           910: }
        !           911: END_TEST
        !           912: 
        !           913: /*******************************************************************************
        !           914:  * mark_from_string
        !           915:  */
        !           916: 
        !           917: static struct {
        !           918:        char *s;
        !           919:        bool ok;
        !           920:        mark_op_t ops;
        !           921:        mark_t m;
        !           922: } mark_data[] = {
        !           923:        {NULL,                  FALSE,  MARK_OP_NONE, { 0 }},
        !           924:        {"",                    TRUE,   MARK_OP_NONE, { 0, 0xffffffff }},
        !           925:        {"/",                   TRUE,   MARK_OP_NONE, { 0, 0 }},
        !           926:        {"42",                  TRUE,   MARK_OP_NONE, { 42, 0xffffffff }},
        !           927:        {"0x42",                TRUE,   MARK_OP_NONE, { 0x42, 0xffffffff }},
        !           928:        {"x",                   FALSE,  MARK_OP_NONE, { 0 }},
        !           929:        {"42/",                 TRUE,   MARK_OP_NONE, { 0, 0 }},
        !           930:        {"42/0",                TRUE,   MARK_OP_NONE, { 0, 0 }},
        !           931:        {"42/x",                FALSE,  MARK_OP_NONE, { 0 }},
        !           932:        {"42/42",               TRUE,   MARK_OP_NONE, { 42, 42 }},
        !           933:        {"42/0xff",             TRUE,   MARK_OP_NONE, { 42, 0xff }},
        !           934:        {"0x42/0xff",   TRUE,   MARK_OP_NONE, { 0x42, 0xff }},
        !           935:        {"/0xff",               TRUE,   MARK_OP_NONE, { 0, 0xff }},
        !           936:        {"/x",                  FALSE,  MARK_OP_NONE, { 0 }},
        !           937:        {"x/x",                 FALSE,  MARK_OP_NONE, { 0 }},
        !           938:        {"0xfffffff0/0x0000ffff",       TRUE,   MARK_OP_UNIQUE,
        !           939:                { 0x0000fff0, 0x0000ffff }},
        !           940:        {"%unique",                                     TRUE,   MARK_OP_UNIQUE,
        !           941:                { MARK_UNIQUE, 0xffffffff }},
        !           942:        {"%unique/",                            TRUE,   MARK_OP_UNIQUE,
        !           943:                { MARK_UNIQUE, 0 }},
        !           944:        {"%unique",                                     FALSE,  MARK_OP_NONE,
        !           945:                { 0, 0 }},
        !           946:        {"%unique/0x0000ffff",          TRUE,   MARK_OP_UNIQUE,
        !           947:                { MARK_UNIQUE, 0x0000ffff }},
        !           948:        {"%unique/0xffffffff",          TRUE,   MARK_OP_UNIQUE,
        !           949:                { MARK_UNIQUE, 0xffffffff }},
        !           950:        {"%unique0xffffffffff",         FALSE,  MARK_OP_UNIQUE,
        !           951:                { 0, 0 }},
        !           952:        {"0xffffffff/0x0000ffff",       TRUE,   MARK_OP_UNIQUE,
        !           953:                { MARK_UNIQUE, 0x0000ffff }},
        !           954:        {"0xffffffff/0xffffffff",       TRUE,   MARK_OP_UNIQUE,
        !           955:                { MARK_UNIQUE, 0xffffffff }},
        !           956:        {"%unique-dir",                         TRUE,   MARK_OP_UNIQUE,
        !           957:                { MARK_UNIQUE_DIR, 0xffffffff }},
        !           958:        {"%unique-dir/",                        TRUE,   MARK_OP_UNIQUE,
        !           959:                { MARK_UNIQUE_DIR, 0 }},
        !           960:        {"%unique-dir",                         FALSE,  MARK_OP_NONE,
        !           961:                { 0, 0 }},
        !           962:        {"%unique-dir/0x0000ffff",      TRUE,   MARK_OP_UNIQUE,
        !           963:                { MARK_UNIQUE_DIR, 0x0000ffff }},
        !           964:        {"%unique-dir/0xffffffff",      TRUE,   MARK_OP_UNIQUE,
        !           965:                { MARK_UNIQUE_DIR, 0xffffffff }},
        !           966:        {"%unique-dir0xffffffff",       FALSE,  MARK_OP_UNIQUE,
        !           967:                { 0, 0 }},
        !           968:        {"0xfffffffe/0x0000ffff",       TRUE,   MARK_OP_UNIQUE,
        !           969:                { MARK_UNIQUE_DIR, 0x0000ffff }},
        !           970:        {"0xfffffffe/0xffffffff",       TRUE,   MARK_OP_UNIQUE,
        !           971:                { MARK_UNIQUE_DIR, 0xffffffff }},
        !           972:        {"%unique-/0xffffffff",         FALSE,  MARK_OP_UNIQUE,
        !           973:                { 0, 0 }},
        !           974:        {"%unique-foo/0xffffffff",      FALSE,  MARK_OP_UNIQUE,
        !           975:                { 0, 0 }},
        !           976:        {"%same",                                       TRUE,   MARK_OP_SAME,
        !           977:                { MARK_SAME, 0xffffffff }},
        !           978:        {"%same/0x0000ffff",            TRUE,   MARK_OP_SAME,
        !           979:                { MARK_SAME, 0x0000ffff }},
        !           980:        {"%%same",                                      FALSE,  MARK_OP_NONE,
        !           981:                { 0, 0 }},
        !           982: };
        !           983: 
        !           984: START_TEST(test_mark_from_string)
        !           985: {
        !           986:        mark_t mark;
        !           987: 
        !           988:        if (mark_from_string(mark_data[_i].s, mark_data[_i].ops, &mark))
        !           989:        {
        !           990:                ck_assert_int_eq(mark.value, mark_data[_i].m.value);
        !           991:                ck_assert_int_eq(mark.mask, mark_data[_i].m.mask);
        !           992:        }
        !           993:        else
        !           994:        {
        !           995:                ck_assert(!mark_data[_i].ok);
        !           996:        }
        !           997: }
        !           998: END_TEST
        !           999: 
        !          1000: /*******************************************************************************
        !          1001:  * if_id_from_string
        !          1002:  */
        !          1003: 
        !          1004: static struct {
        !          1005:        char *s;
        !          1006:        bool ok;
        !          1007:        uint32_t i;
        !          1008: } if_id_data[] = {
        !          1009:        {NULL,                  FALSE,  0 },
        !          1010:        {"",                    TRUE,   0 },
        !          1011:        {"/",                   FALSE,  0 },
        !          1012:        {"42",                  TRUE,   42 },
        !          1013:        {"0x42",                TRUE,   0x42 },
        !          1014:        {"x",                   FALSE,  0 },
        !          1015:        {"42/",                 FALSE,  0 },
        !          1016:        {"42/0",                FALSE,  0 },
        !          1017:        {"%unique",             TRUE,   IF_ID_UNIQUE },
        !          1018:        {"%unique/",    FALSE,  0},
        !          1019:        {"%unique0xffffffffff", FALSE,  0},
        !          1020:        {"0xffffffff",  TRUE,   IF_ID_UNIQUE},
        !          1021:        {"%unique-dir", TRUE,   IF_ID_UNIQUE_DIR},
        !          1022:        {"%unique-dir/",FALSE,  0},
        !          1023:        {"0xfffffffe",  TRUE,   IF_ID_UNIQUE_DIR},
        !          1024:        {"%unique-",    FALSE,  0},
        !          1025:        {"%unique-foo", FALSE,  0},
        !          1026: };
        !          1027: 
        !          1028: START_TEST(test_if_id_from_string)
        !          1029: {
        !          1030:        uint32_t if_id;
        !          1031: 
        !          1032:        if (if_id_from_string(if_id_data[_i].s, &if_id))
        !          1033:        {
        !          1034:                ck_assert_int_eq(if_id, if_id_data[_i].i);
        !          1035:        }
        !          1036:        else
        !          1037:        {
        !          1038:                ck_assert(!if_id_data[_i].ok);
        !          1039:        }
        !          1040: }
        !          1041: END_TEST
        !          1042: 
        !          1043: /*******************************************************************************
        !          1044:  * allocate_unique_if_ids
        !          1045:  */
        !          1046: 
        !          1047: static struct {
        !          1048:        uint32_t in;
        !          1049:        uint32_t out;
        !          1050:        uint32_t exp_in;
        !          1051:        uint32_t exp_out;
        !          1052: } unique_if_id_data[] = {
        !          1053:        {0,     0, 0, 0 },
        !          1054:        {42, 42, 42, 42 },
        !          1055:        {42, 1337, 42, 1337 },
        !          1056:        /* each call increases the internal counter by 1 or 2*/
        !          1057:        {IF_ID_UNIQUE, 42, 1, 42 },
        !          1058:        {42, IF_ID_UNIQUE, 42, 2 },
        !          1059:        {IF_ID_UNIQUE_DIR, 42, 3, 42 },
        !          1060:        {42, IF_ID_UNIQUE_DIR, 42, 4 },
        !          1061:        {IF_ID_UNIQUE, IF_ID_UNIQUE, 5, 5 },
        !          1062:        {IF_ID_UNIQUE_DIR, IF_ID_UNIQUE, 6, 7 },
        !          1063:        {IF_ID_UNIQUE, IF_ID_UNIQUE_DIR, 8, 9 },
        !          1064:        {IF_ID_UNIQUE_DIR, IF_ID_UNIQUE_DIR, 10, 11 },
        !          1065: };
        !          1066: 
        !          1067: START_TEST(test_allocate_unique_if_ids)
        !          1068: {
        !          1069:        uint32_t if_id_in = unique_if_id_data[_i].in,
        !          1070:                         if_id_out = unique_if_id_data[_i].out;
        !          1071: 
        !          1072:        allocate_unique_if_ids(&if_id_in, &if_id_out);
        !          1073:        ck_assert_int_eq(if_id_in, unique_if_id_data[_i].exp_in);
        !          1074:        ck_assert_int_eq(if_id_out, unique_if_id_data[_i].exp_out);
        !          1075: }
        !          1076: END_TEST
        !          1077: 
        !          1078: /*******************************************************************************
        !          1079:  * signature_schemes_for_key
        !          1080:  */
        !          1081: 
        !          1082: static struct {
        !          1083:        key_type_t type;
        !          1084:        int size;
        !          1085:        signature_scheme_t expected[7];
        !          1086: } scheme_data[] = {
        !          1087:        {KEY_RSA,   1024, { SIGN_RSA_EMSA_PSS, SIGN_RSA_EMSA_PSS,
        !          1088:                                                SIGN_RSA_EMSA_PSS, SIGN_RSA_EMSA_PKCS1_SHA2_256,
        !          1089:                                                SIGN_RSA_EMSA_PKCS1_SHA2_384, SIGN_RSA_EMSA_PKCS1_SHA2_512,
        !          1090:                                                SIGN_UNKNOWN }},
        !          1091:        {KEY_RSA,   2048, { SIGN_RSA_EMSA_PSS, SIGN_RSA_EMSA_PSS,
        !          1092:                                                SIGN_RSA_EMSA_PSS, SIGN_RSA_EMSA_PKCS1_SHA2_256,
        !          1093:                                                SIGN_RSA_EMSA_PKCS1_SHA2_384, SIGN_RSA_EMSA_PKCS1_SHA2_512,
        !          1094:                                                SIGN_UNKNOWN }},
        !          1095:        {KEY_RSA,   4096, { SIGN_RSA_EMSA_PSS, SIGN_RSA_EMSA_PSS,
        !          1096:                                                SIGN_RSA_EMSA_PKCS1_SHA2_384, SIGN_RSA_EMSA_PKCS1_SHA2_512,
        !          1097:                                                SIGN_UNKNOWN }},
        !          1098:        {KEY_RSA,   8192, { SIGN_RSA_EMSA_PSS, SIGN_RSA_EMSA_PKCS1_SHA2_512, SIGN_UNKNOWN }},
        !          1099:        {KEY_ECDSA,  256, { SIGN_ECDSA_WITH_SHA256_DER, SIGN_ECDSA_WITH_SHA384_DER,
        !          1100:                                                SIGN_ECDSA_WITH_SHA512_DER, SIGN_UNKNOWN }},
        !          1101:        {KEY_ECDSA,  384, { SIGN_ECDSA_WITH_SHA384_DER, SIGN_ECDSA_WITH_SHA512_DER,
        !          1102:                                                SIGN_UNKNOWN }},
        !          1103:        {KEY_ECDSA,  512, { SIGN_ECDSA_WITH_SHA512_DER, SIGN_UNKNOWN }},
        !          1104:        {KEY_BLISS,  128, { SIGN_BLISS_WITH_SHA2_256, SIGN_BLISS_WITH_SHA2_384,
        !          1105:                                                SIGN_BLISS_WITH_SHA2_512, SIGN_UNKNOWN }},
        !          1106:        {KEY_BLISS,  192, { SIGN_BLISS_WITH_SHA2_384, SIGN_BLISS_WITH_SHA2_512,
        !          1107:                                                SIGN_UNKNOWN }},
        !          1108:        {KEY_BLISS,  256, { SIGN_BLISS_WITH_SHA2_512, SIGN_UNKNOWN }},
        !          1109: };
        !          1110: 
        !          1111: START_TEST(test_signature_schemes_for_key)
        !          1112: {
        !          1113:        enumerator_t  *enumerator;
        !          1114:        signature_params_t *params;
        !          1115:        int i;
        !          1116: 
        !          1117:        enumerator = signature_schemes_for_key(scheme_data[_i].type, scheme_data[_i].size);
        !          1118:        for (i = 0; scheme_data[_i].expected[i] != SIGN_UNKNOWN; i++)
        !          1119:        {
        !          1120:                ck_assert(enumerator->enumerate(enumerator, &params));
        !          1121:                ck_assert_int_eq(scheme_data[_i].expected[i], params->scheme);
        !          1122:        }
        !          1123:        ck_assert(!enumerator->enumerate(enumerator, &params));
        !          1124:        enumerator->destroy(enumerator);
        !          1125: }
        !          1126: END_TEST
        !          1127: 
        !          1128: Suite *utils_suite_create()
        !          1129: {
        !          1130:        Suite *s;
        !          1131:        TCase *tc;
        !          1132: 
        !          1133:        /* force a timezone to match non-UTC conversions */
        !          1134: #ifdef WIN32
        !          1135:        _putenv("TZ=GST-1GDT");
        !          1136: #else
        !          1137:        setenv("TZ", "Europe/Zurich", 1);
        !          1138: #endif
        !          1139:        tzset();
        !          1140: 
        !          1141:        s = suite_create("utils");
        !          1142: 
        !          1143:        tc = tcase_create("objects");
        !          1144:        tcase_add_test(tc, test_objects);
        !          1145:        suite_add_tcase(s, tc);
        !          1146: 
        !          1147:        tc = tcase_create("return functions");
        !          1148:        tcase_add_test(tc, test_return_functions);
        !          1149:        suite_add_tcase(s, tc);
        !          1150: 
        !          1151:        tc = tcase_create("timeval_add_ms");
        !          1152:        tcase_add_test(tc, test_timeval_add_ms);
        !          1153:        suite_add_tcase(s, tc);
        !          1154: 
        !          1155:        tc = tcase_create("timespan_from_string");
        !          1156:        tcase_add_loop_test(tc, test_timespan_from_string, 0, countof(ts_data));
        !          1157:        suite_add_tcase(s, tc);
        !          1158: 
        !          1159:        tc = tcase_create("htoun,untoh");
        !          1160:        tcase_add_test(tc, test_htoun);
        !          1161:        tcase_add_test(tc, test_untoh);
        !          1162:        suite_add_tcase(s, tc);
        !          1163: 
        !          1164:        tc = tcase_create("round");
        !          1165:        tcase_add_test(tc, test_round);
        !          1166:        suite_add_tcase(s, tc);
        !          1167: 
        !          1168:        tc = tcase_create("string helper");
        !          1169:        tcase_add_loop_test(tc, test_streq, 0, countof(streq_data));
        !          1170:        tcase_add_loop_test(tc, test_strneq, 0, countof(strneq_data));
        !          1171:        tcase_add_loop_test(tc, test_strpfx, 0, countof(strpfx_data));
        !          1172:        suite_add_tcase(s, tc);
        !          1173: 
        !          1174:        tc = tcase_create("malloc_align");
        !          1175:        tcase_add_test(tc, test_malloc_align);
        !          1176:        suite_add_tcase(s, tc);
        !          1177: 
        !          1178:        tc = tcase_create("memxor");
        !          1179:        tcase_add_test(tc, test_memxor);
        !          1180:        tcase_add_test(tc, test_memxor_aligned);
        !          1181:        suite_add_tcase(s, tc);
        !          1182: 
        !          1183:        tc = tcase_create("memeq");
        !          1184:        tcase_add_loop_test(tc, test_memeq, 0, countof(memeq_data));
        !          1185:        tcase_add_loop_test(tc, test_memeq_const, 0, countof(memeq_data));
        !          1186:        suite_add_tcase(s, tc);
        !          1187: 
        !          1188:        tc = tcase_create("memstr");
        !          1189:        tcase_add_loop_test(tc, test_memstr, 0, countof(memstr_data));
        !          1190:        suite_add_tcase(s, tc);
        !          1191: 
        !          1192:        tc = tcase_create("memwipe");
        !          1193:        tcase_add_test(tc, test_memwipe_null);
        !          1194:        tcase_add_test(tc, test_memwipe_stack);
        !          1195:        tcase_add_test(tc, test_memwipe_heap);
        !          1196:        suite_add_tcase(s, tc);
        !          1197: 
        !          1198:        tc = tcase_create("utils_memrchr");
        !          1199:        tcase_add_loop_test(tc, test_utils_memrchr, 0, countof(memrchr_data));
        !          1200:        suite_add_tcase(s, tc);
        !          1201: 
        !          1202:        tc = tcase_create("translate");
        !          1203:        tcase_add_loop_test(tc, test_translate, 0, countof(translate_data));
        !          1204:        suite_add_tcase(s, tc);
        !          1205: 
        !          1206:        tc = tcase_create("strreplace");
        !          1207:        tcase_add_loop_test(tc, test_strreplace, 0, countof(strreplace_data));
        !          1208:        suite_add_tcase(s, tc);
        !          1209: 
        !          1210:        tc = tcase_create("path_dirname");
        !          1211:        tcase_add_loop_test(tc, test_path_dirname, 0, countof(path_data));
        !          1212:        suite_add_tcase(s, tc);
        !          1213: 
        !          1214:        tc = tcase_create("path_basename");
        !          1215:        tcase_add_loop_test(tc, test_path_basename, 0, countof(path_data));
        !          1216:        suite_add_tcase(s, tc);
        !          1217: 
        !          1218:        tc = tcase_create("path_absolute");
        !          1219:        tcase_add_loop_test(tc, test_path_absolute, 0, countof(path_data));
        !          1220:        suite_add_tcase(s, tc);
        !          1221: 
        !          1222:        tc = tcase_create("printf_hooks");
        !          1223:        tcase_add_loop_test(tc, test_time_printf_hook, 0, countof(time_data));
        !          1224:        tcase_add_loop_test(tc, test_time_delta_printf_hook, 0, countof(time_delta_data));
        !          1225:        suite_add_tcase(s, tc);
        !          1226: 
        !          1227:        tc = tcase_create("mark_from_string");
        !          1228:        tcase_add_loop_test(tc, test_mark_from_string, 0, countof(mark_data));
        !          1229:        suite_add_tcase(s, tc);
        !          1230: 
        !          1231:        tc = tcase_create("if_id_from_string");
        !          1232:        tcase_add_loop_test(tc, test_if_id_from_string, 0, countof(if_id_data));
        !          1233:        suite_add_tcase(s, tc);
        !          1234: 
        !          1235:        tc = tcase_create("allocate_unique_if_ids");
        !          1236:        tcase_add_loop_test(tc, test_allocate_unique_if_ids, 0, countof(unique_if_id_data));
        !          1237:        suite_add_tcase(s, tc);
        !          1238: 
        !          1239:        tc = tcase_create("signature_schemes_for_key");
        !          1240:        tcase_add_loop_test(tc, test_signature_schemes_for_key, 0, countof(scheme_data));
        !          1241:        suite_add_tcase(s, tc);
        !          1242: 
        !          1243:        return s;
        !          1244: }

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