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

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: /*******************************************************************************
1.1.1.2 ! misho     770:  * path_first/last_separator
        !           771:  */
        !           772: 
        !           773: static struct {
        !           774:        char *path;
        !           775:        int len;
        !           776:        int first;
        !           777:        int last;
        !           778: } separator_data[] = {
        !           779:        {NULL, -1, -1, -1},
        !           780:        {"",   -1, -1, -1},
        !           781:        {".",  -1, -1, -1},
        !           782:        {"..", -1, -1, -1},
        !           783: #ifdef WIN32
        !           784:        {"C:\\",         -1, 2, 2},
        !           785:        {"C:/",          -1, 2, 2},
        !           786:        {"X:\\\\",       -1, 2, 3},
        !           787:        {"d:\\f",        -1, 2, 2},
        !           788:        {"d:\\f",         2, -1, -1},
        !           789:        {"C:\\foo\\",    -1, 2, 6},
        !           790:        {"foo\\bar",     -1, 3, 3},
        !           791:        {"foo\\\\bar",   -1, 3, 4},
        !           792:        {"\\foo\\bar",   -1, 0, 4},
        !           793:        {"\\\\foo\\bar", -1, 0, 5},
        !           794:        {"\\\\foo\\bar",  4, 0, 1},
        !           795:        {"foo\\bar/baz", -1, 3, 7},
        !           796: #endif /* WIN32 */
        !           797:        {"/",         -1, 0, 0},
        !           798:        {"//",        -1, 0, 1},
        !           799:        {"foo",       -1, -1, -1},
        !           800:        {"f/",        -1, 1, 1},
        !           801:        {"foo/",      -1, 3, 3},
        !           802:        {"foo/",       2, -1, -1},
        !           803:        {"foo//",     -1, 3, 4},
        !           804:        {"/foo",      -1, 0, 0},
        !           805:        {"/foo/",     -1, 0, 4},
        !           806:        {"/foo/",      3, 0, 0},
        !           807:        {"//foo/",    -1, 0, 5},
        !           808:        {"foo/bar",   -1, 3, 3},
        !           809:        {"foo/bar",    1, -1, -1},
        !           810:        {"foo/bar",    2, -1, -1},
        !           811:        {"foo/bar",    3, -1, -1},
        !           812:        {"foo/bar",    4, 3, 3},
        !           813:        {"foo/bar",    5, 3, 3},
        !           814:        {"foo//bar",  -1, 3, 4},
        !           815:        {"/foo/bar",  -1, 0, 4},
        !           816:        {"/foo/bar/", -1, 0, 8},
        !           817:        {"/foo/bar/",  0, -1, -1},
        !           818:        {"/foo/bar/",  1, 0, 0},
        !           819:        {"/foo/bar/",  2, 0, 0},
        !           820:        {"/foo/bar/",  3, 0, 0},
        !           821:        {"/foo/bar/",  4, 0, 0},
        !           822:        {"/foo/bar/",  5, 0, 4},
        !           823:        {"/foo/bar/",  7, 0, 4},
        !           824:        {"/foo/bar/",  8, 0, 4},
        !           825:        {"/foo/bar/",  9, 0, 8},
        !           826: };
        !           827: 
        !           828: START_TEST(test_path_first_separator)
        !           829: {
        !           830:        char *pos;
        !           831: 
        !           832:        pos = path_first_separator(separator_data[_i].path, separator_data[_i].len);
        !           833:        if (separator_data[_i].first >= 0)
        !           834:        {
        !           835:                ck_assert_int_eq(pos-separator_data[_i].path, separator_data[_i].first);
        !           836:        }
        !           837:        else
        !           838:        {
        !           839:                ck_assert(!pos);
        !           840:        }
        !           841: }
        !           842: END_TEST
        !           843: 
        !           844: START_TEST(test_path_last_separator)
        !           845: {
        !           846:        char *pos;
        !           847: 
        !           848:        pos = path_last_separator(separator_data[_i].path, separator_data[_i].len);
        !           849:        if (separator_data[_i].last >= 0)
        !           850:        {
        !           851:                ck_assert_int_eq(pos-separator_data[_i].path, separator_data[_i].last);
        !           852:        }
        !           853:        else
        !           854:        {
        !           855:                ck_assert(!pos);
        !           856:        }
        !           857: }
        !           858: END_TEST
        !           859: 
        !           860: /*******************************************************************************
1.1       misho     861:  * path_dirname/basename/absolute
                    862:  */
                    863: 
                    864: static struct {
                    865:        char *path;
                    866:        char *dir;
                    867:        char *base;
                    868:        bool absolute;
                    869: } path_data[] = {
                    870:        {NULL, ".", ".", FALSE},
                    871:        {"", ".", ".", FALSE},
                    872:        {".", ".", ".", FALSE},
                    873:        {"..", ".", "..", FALSE},
                    874: #ifdef WIN32
                    875:        {"C:\\", "C:", "C:", TRUE},
1.1.1.2 ! misho     876:        {"C:/", "C:", "C:", TRUE},
1.1       misho     877:        {"X:\\\\", "X:", "X:", TRUE},
                    878:        {"foo", ".", "foo", FALSE},
                    879:        {"f\\", ".", "f", FALSE},
                    880:        {"foo\\", ".", "foo", FALSE},
                    881:        {"foo\\\\", ".", "foo", FALSE},
                    882:        {"d:\\f", "d:", "f", TRUE},
                    883:        {"C:\\f\\", "C:", "f", TRUE},
1.1.1.2 ! misho     884:        {"C:\\f\\", "C:", "f", TRUE},
1.1       misho     885:        {"C:\\foo", "C:", "foo", TRUE},
                    886:        {"C:\\foo\\", "C:", "foo", TRUE},
1.1.1.2 ! misho     887:        {"C:\\foo/", "C:", "foo", TRUE},
1.1       misho     888:        {"foo\\bar", "foo", "bar", FALSE},
                    889:        {"foo\\\\bar", "foo", "bar", FALSE},
                    890:        {"C:\\foo\\bar", "C:\\foo", "bar", TRUE},
                    891:        {"C:\\foo\\bar\\", "C:\\foo", "bar", TRUE},
                    892:        {"C:\\foo\\bar\\baz", "C:\\foo\\bar", "baz", TRUE},
1.1.1.2 ! misho     893:        {"C:\\foo/bar\\baz", "C:\\foo/bar", "baz", TRUE},
        !           894:        {"C:/foo/bar/baz", "C:/foo/bar", "baz", TRUE},
        !           895:        {"\\foo\\bar", "\\foo", "bar", TRUE},
1.1       misho     896:        {"\\\\foo\\bar", "\\\\foo", "bar", TRUE},
1.1.1.2 ! misho     897: #endif /* WIN32 */
1.1       misho     898:        {"/", "/", "/", TRUE},
                    899:        {"//", "/", "/", TRUE},
                    900:        {"foo", ".", "foo", FALSE},
                    901:        {"f/", ".", "f", FALSE},
                    902:        {"foo/", ".", "foo", FALSE},
                    903:        {"foo//", ".", "foo", FALSE},
                    904:        {"/f", "/", "f", TRUE},
                    905:        {"/f/", "/", "f", TRUE},
                    906:        {"/foo", "/", "foo", TRUE},
                    907:        {"/foo/", "/", "foo", TRUE},
                    908:        {"//foo/", "/", "foo", TRUE},
                    909:        {"foo/bar", "foo", "bar", FALSE},
                    910:        {"foo//bar", "foo", "bar", FALSE},
                    911:        {"/foo/bar", "/foo", "bar", TRUE},
                    912:        {"/foo/bar/", "/foo", "bar", TRUE},
                    913:        {"/foo/bar/baz", "/foo/bar", "baz", TRUE},
                    914: };
                    915: 
                    916: START_TEST(test_path_dirname)
                    917: {
                    918:        char *dir;
                    919: 
                    920:        dir = path_dirname(path_data[_i].path);
                    921:        ck_assert_str_eq(path_data[_i].dir, dir);
                    922:        free(dir);
                    923: }
                    924: END_TEST
                    925: 
                    926: START_TEST(test_path_basename)
                    927: {
                    928:        char *base;
                    929: 
                    930:        base = path_basename(path_data[_i].path);
                    931:        ck_assert_str_eq(path_data[_i].base, base);
                    932:        free(base);
                    933: }
                    934: END_TEST
                    935: 
                    936: START_TEST(test_path_absolute)
                    937: {
                    938:        ck_assert(path_data[_i].absolute == path_absolute(path_data[_i].path));
                    939: }
                    940: END_TEST
                    941: 
                    942: /*******************************************************************************
                    943:  * time_printf_hook
                    944:  */
                    945: 
                    946: static struct {
                    947:        time_t in;
                    948:        bool utc;
                    949:        char *out;
                    950: } time_data[] = {
                    951:        {UNDEFINED_TIME, FALSE, "--- -- --:--:-- ----"},
                    952:        {UNDEFINED_TIME, TRUE , "--- -- --:--:-- UTC ----"},
                    953:        {1, FALSE, "Jan 01 01:00:01 1970"},
                    954:        {1, TRUE , "Jan 01 00:00:01 UTC 1970"},
                    955:        {1341150196, FALSE, "Jul 01 15:43:16 2012"},
                    956:        {1341150196, TRUE , "Jul 01 13:43:16 UTC 2012"},
                    957: };
                    958: 
                    959: START_TEST(test_time_printf_hook)
                    960: {
                    961:        char buf[32];
                    962:        int len;
                    963: 
                    964:        len = snprintf(buf, sizeof(buf), "%T", &time_data[_i].in, time_data[_i].utc);
                    965:        ck_assert(len >= 0 && len < sizeof(buf));
                    966:        ck_assert_str_eq(buf, time_data[_i].out);
                    967: }
                    968: END_TEST
                    969: 
                    970: /*******************************************************************************
                    971:  * time_delta_printf_hook
                    972:  */
                    973: 
                    974: static struct {
                    975:        time_t a;
                    976:        time_t b;
                    977:        char *out;
                    978: } time_delta_data[] = {
                    979:        {0, 0, "0 seconds"},
                    980:        {0, 1, "1 second"},
                    981:        {0, -1, "1 second"},
                    982:        {1, 0, "1 second"},
                    983:        {0, 2, "2 seconds"},
                    984:        {2, 0, "2 seconds"},
                    985:        {0, 60, "60 seconds"},
                    986:        {0, 120, "120 seconds"},
                    987:        {0, 121, "2 minutes"},
                    988:        {0, 3600, "60 minutes"},
                    989:        {0, 7200, "120 minutes"},
                    990:        {0, 7201, "2 hours"},
                    991:        {0, 86400, "24 hours"},
                    992:        {0, 172800, "48 hours"},
                    993:        {0, 172801, "2 days"},
                    994:        {172801, 86400, "24 hours"},
                    995: };
                    996: 
                    997: START_TEST(test_time_delta_printf_hook)
                    998: {
                    999:        char buf[16];
                   1000:        int len;
                   1001: 
                   1002:        len = snprintf(buf, sizeof(buf), "%V", &time_delta_data[_i].a, &time_delta_data[_i].b);
                   1003:        ck_assert(len >= 0 && len < sizeof(buf));
                   1004:        ck_assert_str_eq(buf, time_delta_data[_i].out);
                   1005: }
                   1006: END_TEST
                   1007: 
                   1008: /*******************************************************************************
                   1009:  * mark_from_string
                   1010:  */
                   1011: 
                   1012: static struct {
                   1013:        char *s;
                   1014:        bool ok;
                   1015:        mark_op_t ops;
                   1016:        mark_t m;
                   1017: } mark_data[] = {
                   1018:        {NULL,                  FALSE,  MARK_OP_NONE, { 0 }},
                   1019:        {"",                    TRUE,   MARK_OP_NONE, { 0, 0xffffffff }},
                   1020:        {"/",                   TRUE,   MARK_OP_NONE, { 0, 0 }},
                   1021:        {"42",                  TRUE,   MARK_OP_NONE, { 42, 0xffffffff }},
                   1022:        {"0x42",                TRUE,   MARK_OP_NONE, { 0x42, 0xffffffff }},
                   1023:        {"x",                   FALSE,  MARK_OP_NONE, { 0 }},
                   1024:        {"42/",                 TRUE,   MARK_OP_NONE, { 0, 0 }},
                   1025:        {"42/0",                TRUE,   MARK_OP_NONE, { 0, 0 }},
                   1026:        {"42/x",                FALSE,  MARK_OP_NONE, { 0 }},
                   1027:        {"42/42",               TRUE,   MARK_OP_NONE, { 42, 42 }},
                   1028:        {"42/0xff",             TRUE,   MARK_OP_NONE, { 42, 0xff }},
                   1029:        {"0x42/0xff",   TRUE,   MARK_OP_NONE, { 0x42, 0xff }},
                   1030:        {"/0xff",               TRUE,   MARK_OP_NONE, { 0, 0xff }},
                   1031:        {"/x",                  FALSE,  MARK_OP_NONE, { 0 }},
                   1032:        {"x/x",                 FALSE,  MARK_OP_NONE, { 0 }},
                   1033:        {"0xfffffff0/0x0000ffff",       TRUE,   MARK_OP_UNIQUE,
                   1034:                { 0x0000fff0, 0x0000ffff }},
                   1035:        {"%unique",                                     TRUE,   MARK_OP_UNIQUE,
                   1036:                { MARK_UNIQUE, 0xffffffff }},
                   1037:        {"%unique/",                            TRUE,   MARK_OP_UNIQUE,
                   1038:                { MARK_UNIQUE, 0 }},
                   1039:        {"%unique",                                     FALSE,  MARK_OP_NONE,
                   1040:                { 0, 0 }},
                   1041:        {"%unique/0x0000ffff",          TRUE,   MARK_OP_UNIQUE,
                   1042:                { MARK_UNIQUE, 0x0000ffff }},
                   1043:        {"%unique/0xffffffff",          TRUE,   MARK_OP_UNIQUE,
                   1044:                { MARK_UNIQUE, 0xffffffff }},
                   1045:        {"%unique0xffffffffff",         FALSE,  MARK_OP_UNIQUE,
                   1046:                { 0, 0 }},
                   1047:        {"0xffffffff/0x0000ffff",       TRUE,   MARK_OP_UNIQUE,
                   1048:                { MARK_UNIQUE, 0x0000ffff }},
                   1049:        {"0xffffffff/0xffffffff",       TRUE,   MARK_OP_UNIQUE,
                   1050:                { MARK_UNIQUE, 0xffffffff }},
                   1051:        {"%unique-dir",                         TRUE,   MARK_OP_UNIQUE,
                   1052:                { MARK_UNIQUE_DIR, 0xffffffff }},
                   1053:        {"%unique-dir/",                        TRUE,   MARK_OP_UNIQUE,
                   1054:                { MARK_UNIQUE_DIR, 0 }},
                   1055:        {"%unique-dir",                         FALSE,  MARK_OP_NONE,
                   1056:                { 0, 0 }},
                   1057:        {"%unique-dir/0x0000ffff",      TRUE,   MARK_OP_UNIQUE,
                   1058:                { MARK_UNIQUE_DIR, 0x0000ffff }},
                   1059:        {"%unique-dir/0xffffffff",      TRUE,   MARK_OP_UNIQUE,
                   1060:                { MARK_UNIQUE_DIR, 0xffffffff }},
                   1061:        {"%unique-dir0xffffffff",       FALSE,  MARK_OP_UNIQUE,
                   1062:                { 0, 0 }},
                   1063:        {"0xfffffffe/0x0000ffff",       TRUE,   MARK_OP_UNIQUE,
                   1064:                { MARK_UNIQUE_DIR, 0x0000ffff }},
                   1065:        {"0xfffffffe/0xffffffff",       TRUE,   MARK_OP_UNIQUE,
                   1066:                { MARK_UNIQUE_DIR, 0xffffffff }},
                   1067:        {"%unique-/0xffffffff",         FALSE,  MARK_OP_UNIQUE,
                   1068:                { 0, 0 }},
                   1069:        {"%unique-foo/0xffffffff",      FALSE,  MARK_OP_UNIQUE,
                   1070:                { 0, 0 }},
                   1071:        {"%same",                                       TRUE,   MARK_OP_SAME,
                   1072:                { MARK_SAME, 0xffffffff }},
                   1073:        {"%same/0x0000ffff",            TRUE,   MARK_OP_SAME,
                   1074:                { MARK_SAME, 0x0000ffff }},
                   1075:        {"%%same",                                      FALSE,  MARK_OP_NONE,
                   1076:                { 0, 0 }},
                   1077: };
                   1078: 
                   1079: START_TEST(test_mark_from_string)
                   1080: {
                   1081:        mark_t mark;
                   1082: 
                   1083:        if (mark_from_string(mark_data[_i].s, mark_data[_i].ops, &mark))
                   1084:        {
                   1085:                ck_assert_int_eq(mark.value, mark_data[_i].m.value);
                   1086:                ck_assert_int_eq(mark.mask, mark_data[_i].m.mask);
                   1087:        }
                   1088:        else
                   1089:        {
                   1090:                ck_assert(!mark_data[_i].ok);
                   1091:        }
                   1092: }
                   1093: END_TEST
                   1094: 
                   1095: /*******************************************************************************
                   1096:  * if_id_from_string
                   1097:  */
                   1098: 
                   1099: static struct {
                   1100:        char *s;
                   1101:        bool ok;
                   1102:        uint32_t i;
                   1103: } if_id_data[] = {
                   1104:        {NULL,                  FALSE,  0 },
                   1105:        {"",                    TRUE,   0 },
                   1106:        {"/",                   FALSE,  0 },
                   1107:        {"42",                  TRUE,   42 },
                   1108:        {"0x42",                TRUE,   0x42 },
                   1109:        {"x",                   FALSE,  0 },
                   1110:        {"42/",                 FALSE,  0 },
                   1111:        {"42/0",                FALSE,  0 },
                   1112:        {"%unique",             TRUE,   IF_ID_UNIQUE },
                   1113:        {"%unique/",    FALSE,  0},
                   1114:        {"%unique0xffffffffff", FALSE,  0},
                   1115:        {"0xffffffff",  TRUE,   IF_ID_UNIQUE},
                   1116:        {"%unique-dir", TRUE,   IF_ID_UNIQUE_DIR},
                   1117:        {"%unique-dir/",FALSE,  0},
                   1118:        {"0xfffffffe",  TRUE,   IF_ID_UNIQUE_DIR},
                   1119:        {"%unique-",    FALSE,  0},
                   1120:        {"%unique-foo", FALSE,  0},
                   1121: };
                   1122: 
                   1123: START_TEST(test_if_id_from_string)
                   1124: {
                   1125:        uint32_t if_id;
                   1126: 
                   1127:        if (if_id_from_string(if_id_data[_i].s, &if_id))
                   1128:        {
                   1129:                ck_assert_int_eq(if_id, if_id_data[_i].i);
                   1130:        }
                   1131:        else
                   1132:        {
                   1133:                ck_assert(!if_id_data[_i].ok);
                   1134:        }
                   1135: }
                   1136: END_TEST
                   1137: 
                   1138: /*******************************************************************************
                   1139:  * allocate_unique_if_ids
                   1140:  */
                   1141: 
                   1142: static struct {
                   1143:        uint32_t in;
                   1144:        uint32_t out;
                   1145:        uint32_t exp_in;
                   1146:        uint32_t exp_out;
                   1147: } unique_if_id_data[] = {
                   1148:        {0,     0, 0, 0 },
                   1149:        {42, 42, 42, 42 },
                   1150:        {42, 1337, 42, 1337 },
                   1151:        /* each call increases the internal counter by 1 or 2*/
                   1152:        {IF_ID_UNIQUE, 42, 1, 42 },
                   1153:        {42, IF_ID_UNIQUE, 42, 2 },
                   1154:        {IF_ID_UNIQUE_DIR, 42, 3, 42 },
                   1155:        {42, IF_ID_UNIQUE_DIR, 42, 4 },
                   1156:        {IF_ID_UNIQUE, IF_ID_UNIQUE, 5, 5 },
                   1157:        {IF_ID_UNIQUE_DIR, IF_ID_UNIQUE, 6, 7 },
                   1158:        {IF_ID_UNIQUE, IF_ID_UNIQUE_DIR, 8, 9 },
                   1159:        {IF_ID_UNIQUE_DIR, IF_ID_UNIQUE_DIR, 10, 11 },
                   1160: };
                   1161: 
                   1162: START_TEST(test_allocate_unique_if_ids)
                   1163: {
                   1164:        uint32_t if_id_in = unique_if_id_data[_i].in,
                   1165:                         if_id_out = unique_if_id_data[_i].out;
                   1166: 
                   1167:        allocate_unique_if_ids(&if_id_in, &if_id_out);
                   1168:        ck_assert_int_eq(if_id_in, unique_if_id_data[_i].exp_in);
                   1169:        ck_assert_int_eq(if_id_out, unique_if_id_data[_i].exp_out);
                   1170: }
                   1171: END_TEST
                   1172: 
                   1173: /*******************************************************************************
                   1174:  * signature_schemes_for_key
                   1175:  */
                   1176: 
                   1177: static struct {
                   1178:        key_type_t type;
                   1179:        int size;
                   1180:        signature_scheme_t expected[7];
                   1181: } scheme_data[] = {
                   1182:        {KEY_RSA,   1024, { SIGN_RSA_EMSA_PSS, SIGN_RSA_EMSA_PSS,
                   1183:                                                SIGN_RSA_EMSA_PSS, SIGN_RSA_EMSA_PKCS1_SHA2_256,
                   1184:                                                SIGN_RSA_EMSA_PKCS1_SHA2_384, SIGN_RSA_EMSA_PKCS1_SHA2_512,
                   1185:                                                SIGN_UNKNOWN }},
                   1186:        {KEY_RSA,   2048, { SIGN_RSA_EMSA_PSS, SIGN_RSA_EMSA_PSS,
                   1187:                                                SIGN_RSA_EMSA_PSS, SIGN_RSA_EMSA_PKCS1_SHA2_256,
                   1188:                                                SIGN_RSA_EMSA_PKCS1_SHA2_384, SIGN_RSA_EMSA_PKCS1_SHA2_512,
                   1189:                                                SIGN_UNKNOWN }},
                   1190:        {KEY_RSA,   4096, { SIGN_RSA_EMSA_PSS, SIGN_RSA_EMSA_PSS,
                   1191:                                                SIGN_RSA_EMSA_PKCS1_SHA2_384, SIGN_RSA_EMSA_PKCS1_SHA2_512,
                   1192:                                                SIGN_UNKNOWN }},
                   1193:        {KEY_RSA,   8192, { SIGN_RSA_EMSA_PSS, SIGN_RSA_EMSA_PKCS1_SHA2_512, SIGN_UNKNOWN }},
                   1194:        {KEY_ECDSA,  256, { SIGN_ECDSA_WITH_SHA256_DER, SIGN_ECDSA_WITH_SHA384_DER,
                   1195:                                                SIGN_ECDSA_WITH_SHA512_DER, SIGN_UNKNOWN }},
                   1196:        {KEY_ECDSA,  384, { SIGN_ECDSA_WITH_SHA384_DER, SIGN_ECDSA_WITH_SHA512_DER,
                   1197:                                                SIGN_UNKNOWN }},
                   1198:        {KEY_ECDSA,  512, { SIGN_ECDSA_WITH_SHA512_DER, SIGN_UNKNOWN }},
                   1199:        {KEY_BLISS,  128, { SIGN_BLISS_WITH_SHA2_256, SIGN_BLISS_WITH_SHA2_384,
                   1200:                                                SIGN_BLISS_WITH_SHA2_512, SIGN_UNKNOWN }},
                   1201:        {KEY_BLISS,  192, { SIGN_BLISS_WITH_SHA2_384, SIGN_BLISS_WITH_SHA2_512,
                   1202:                                                SIGN_UNKNOWN }},
                   1203:        {KEY_BLISS,  256, { SIGN_BLISS_WITH_SHA2_512, SIGN_UNKNOWN }},
                   1204: };
                   1205: 
                   1206: START_TEST(test_signature_schemes_for_key)
                   1207: {
                   1208:        enumerator_t  *enumerator;
                   1209:        signature_params_t *params;
                   1210:        int i;
                   1211: 
                   1212:        enumerator = signature_schemes_for_key(scheme_data[_i].type, scheme_data[_i].size);
                   1213:        for (i = 0; scheme_data[_i].expected[i] != SIGN_UNKNOWN; i++)
                   1214:        {
                   1215:                ck_assert(enumerator->enumerate(enumerator, &params));
                   1216:                ck_assert_int_eq(scheme_data[_i].expected[i], params->scheme);
                   1217:        }
                   1218:        ck_assert(!enumerator->enumerate(enumerator, &params));
                   1219:        enumerator->destroy(enumerator);
                   1220: }
                   1221: END_TEST
                   1222: 
                   1223: Suite *utils_suite_create()
                   1224: {
                   1225:        Suite *s;
                   1226:        TCase *tc;
                   1227: 
                   1228:        /* force a timezone to match non-UTC conversions */
                   1229: #ifdef WIN32
                   1230:        _putenv("TZ=GST-1GDT");
                   1231: #else
                   1232:        setenv("TZ", "Europe/Zurich", 1);
                   1233: #endif
                   1234:        tzset();
                   1235: 
                   1236:        s = suite_create("utils");
                   1237: 
                   1238:        tc = tcase_create("objects");
                   1239:        tcase_add_test(tc, test_objects);
                   1240:        suite_add_tcase(s, tc);
                   1241: 
                   1242:        tc = tcase_create("return functions");
                   1243:        tcase_add_test(tc, test_return_functions);
                   1244:        suite_add_tcase(s, tc);
                   1245: 
                   1246:        tc = tcase_create("timeval_add_ms");
                   1247:        tcase_add_test(tc, test_timeval_add_ms);
                   1248:        suite_add_tcase(s, tc);
                   1249: 
                   1250:        tc = tcase_create("timespan_from_string");
                   1251:        tcase_add_loop_test(tc, test_timespan_from_string, 0, countof(ts_data));
                   1252:        suite_add_tcase(s, tc);
                   1253: 
                   1254:        tc = tcase_create("htoun,untoh");
                   1255:        tcase_add_test(tc, test_htoun);
                   1256:        tcase_add_test(tc, test_untoh);
                   1257:        suite_add_tcase(s, tc);
                   1258: 
                   1259:        tc = tcase_create("round");
                   1260:        tcase_add_test(tc, test_round);
                   1261:        suite_add_tcase(s, tc);
                   1262: 
                   1263:        tc = tcase_create("string helper");
                   1264:        tcase_add_loop_test(tc, test_streq, 0, countof(streq_data));
                   1265:        tcase_add_loop_test(tc, test_strneq, 0, countof(strneq_data));
                   1266:        tcase_add_loop_test(tc, test_strpfx, 0, countof(strpfx_data));
                   1267:        suite_add_tcase(s, tc);
                   1268: 
                   1269:        tc = tcase_create("malloc_align");
                   1270:        tcase_add_test(tc, test_malloc_align);
                   1271:        suite_add_tcase(s, tc);
                   1272: 
                   1273:        tc = tcase_create("memxor");
                   1274:        tcase_add_test(tc, test_memxor);
                   1275:        tcase_add_test(tc, test_memxor_aligned);
                   1276:        suite_add_tcase(s, tc);
                   1277: 
                   1278:        tc = tcase_create("memeq");
                   1279:        tcase_add_loop_test(tc, test_memeq, 0, countof(memeq_data));
                   1280:        tcase_add_loop_test(tc, test_memeq_const, 0, countof(memeq_data));
                   1281:        suite_add_tcase(s, tc);
                   1282: 
                   1283:        tc = tcase_create("memstr");
                   1284:        tcase_add_loop_test(tc, test_memstr, 0, countof(memstr_data));
                   1285:        suite_add_tcase(s, tc);
                   1286: 
                   1287:        tc = tcase_create("memwipe");
                   1288:        tcase_add_test(tc, test_memwipe_null);
                   1289:        tcase_add_test(tc, test_memwipe_stack);
                   1290:        tcase_add_test(tc, test_memwipe_heap);
                   1291:        suite_add_tcase(s, tc);
                   1292: 
                   1293:        tc = tcase_create("utils_memrchr");
                   1294:        tcase_add_loop_test(tc, test_utils_memrchr, 0, countof(memrchr_data));
                   1295:        suite_add_tcase(s, tc);
                   1296: 
                   1297:        tc = tcase_create("translate");
                   1298:        tcase_add_loop_test(tc, test_translate, 0, countof(translate_data));
                   1299:        suite_add_tcase(s, tc);
                   1300: 
                   1301:        tc = tcase_create("strreplace");
                   1302:        tcase_add_loop_test(tc, test_strreplace, 0, countof(strreplace_data));
                   1303:        suite_add_tcase(s, tc);
                   1304: 
1.1.1.2 ! misho    1305:        tc = tcase_create("path_first/last_separator");
        !          1306:        tcase_add_loop_test(tc, test_path_first_separator, 0, countof(separator_data));
        !          1307:        tcase_add_loop_test(tc, test_path_last_separator, 0, countof(separator_data));
        !          1308:        suite_add_tcase(s, tc);
        !          1309: 
1.1       misho    1310:        tc = tcase_create("path_dirname");
                   1311:        tcase_add_loop_test(tc, test_path_dirname, 0, countof(path_data));
                   1312:        suite_add_tcase(s, tc);
                   1313: 
                   1314:        tc = tcase_create("path_basename");
                   1315:        tcase_add_loop_test(tc, test_path_basename, 0, countof(path_data));
                   1316:        suite_add_tcase(s, tc);
                   1317: 
                   1318:        tc = tcase_create("path_absolute");
                   1319:        tcase_add_loop_test(tc, test_path_absolute, 0, countof(path_data));
                   1320:        suite_add_tcase(s, tc);
                   1321: 
                   1322:        tc = tcase_create("printf_hooks");
                   1323:        tcase_add_loop_test(tc, test_time_printf_hook, 0, countof(time_data));
                   1324:        tcase_add_loop_test(tc, test_time_delta_printf_hook, 0, countof(time_delta_data));
                   1325:        suite_add_tcase(s, tc);
                   1326: 
                   1327:        tc = tcase_create("mark_from_string");
                   1328:        tcase_add_loop_test(tc, test_mark_from_string, 0, countof(mark_data));
                   1329:        suite_add_tcase(s, tc);
                   1330: 
                   1331:        tc = tcase_create("if_id_from_string");
                   1332:        tcase_add_loop_test(tc, test_if_id_from_string, 0, countof(if_id_data));
                   1333:        suite_add_tcase(s, tc);
                   1334: 
                   1335:        tc = tcase_create("allocate_unique_if_ids");
                   1336:        tcase_add_loop_test(tc, test_allocate_unique_if_ids, 0, countof(unique_if_id_data));
                   1337:        suite_add_tcase(s, tc);
                   1338: 
                   1339:        tc = tcase_create("signature_schemes_for_key");
                   1340:        tcase_add_loop_test(tc, test_signature_schemes_for_key, 0, countof(scheme_data));
                   1341:        suite_add_tcase(s, tc);
                   1342: 
                   1343:        return s;
                   1344: }

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