Annotation of embedaddon/strongswan/src/libcharon/plugins/vici/suites/test_request.c, revision 1.1

1.1     ! misho       1: /*
        !             2:  * Copyright (C) 2014 Martin Willi
        !             3:  * Copyright (C) 2014 revosec AG
        !             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 "../vici_dispatcher.h"
        !            19: #include "../libvici.h"
        !            20: 
        !            21: #include <unistd.h>
        !            22: 
        !            23: #ifdef WIN32
        !            24: # define URI "tcp://127.0.0.1:6543"
        !            25: #else /* !WIN32 */
        !            26: # define URI "unix:///tmp/strongswan-vici-request-test"
        !            27: #endif /* !WIN32 */
        !            28: 
        !            29: static void encode_section(vici_req_t *req)
        !            30: {
        !            31:        vici_begin_section(req, "section1");
        !            32:        vici_add_key_valuef(req, "key1", "value%u", 1);
        !            33:        vici_add_key_value(req, "key2", "value2", strlen("value2"));
        !            34:        vici_end_section(req);
        !            35: }
        !            36: 
        !            37: static void decode_section(vici_res_t *res)
        !            38: {
        !            39:        char *str;
        !            40:        int len;
        !            41: 
        !            42:        ck_assert(vici_parse(res) == VICI_PARSE_BEGIN_SECTION);
        !            43:        ck_assert_str_eq(vici_parse_name(res), "section1");
        !            44:        ck_assert(vici_parse(res) == VICI_PARSE_KEY_VALUE);
        !            45:        ck_assert_str_eq(vici_parse_name(res), "key1");
        !            46:        ck_assert_str_eq(vici_parse_value_str(res), "value1");
        !            47:        ck_assert(vici_parse(res) == VICI_PARSE_KEY_VALUE);
        !            48:        ck_assert_str_eq(vici_parse_name(res), "key2");
        !            49:        str = vici_parse_value(res, &len);
        !            50:        ck_assert(chunk_equals(chunk_from_str("value2"), chunk_create(str, len)));
        !            51:        ck_assert(vici_parse(res) == VICI_PARSE_END_SECTION);
        !            52:        ck_assert(vici_parse(res) == VICI_PARSE_END);
        !            53: }
        !            54: 
        !            55: static void encode_list(vici_req_t *req)
        !            56: {
        !            57:        vici_begin_list(req, "list1");
        !            58:        vici_add_list_item(req, "item1", strlen("item1"));
        !            59:        vici_add_list_itemf(req, "item%u", 2);
        !            60:        vici_end_list(req);
        !            61: }
        !            62: 
        !            63: static void decode_list(vici_res_t *res)
        !            64: {
        !            65:        char *str;
        !            66:        int len;
        !            67: 
        !            68:        ck_assert(vici_parse(res) == VICI_PARSE_BEGIN_LIST);
        !            69:        ck_assert_str_eq(vici_parse_name(res), "list1");
        !            70:        ck_assert(vici_parse(res) == VICI_PARSE_LIST_ITEM);
        !            71:        ck_assert_str_eq(vici_parse_value_str(res), "item1");
        !            72:        ck_assert(vici_parse(res) == VICI_PARSE_LIST_ITEM);
        !            73:        str = vici_parse_value(res, &len);
        !            74:        ck_assert(chunk_equals(chunk_from_str("item2"), chunk_create(str, len)));
        !            75:        ck_assert(vici_parse(res) == VICI_PARSE_END_LIST);
        !            76:        ck_assert(vici_parse(res) == VICI_PARSE_END);
        !            77: }
        !            78: 
        !            79: static struct {
        !            80:        void (*encode)(vici_req_t* req);
        !            81:        void (*decode)(vici_res_t* res);
        !            82: } echo_tests[] = {
        !            83:        { encode_section, decode_section },
        !            84:        { encode_list, decode_list },
        !            85: };
        !            86: 
        !            87: static vici_message_t* echo_cb(void *user, char *name,
        !            88:                                                           u_int id, vici_message_t *request)
        !            89: {
        !            90:        ck_assert_str_eq(name, "echo");
        !            91:        ck_assert_int_eq((uintptr_t)user, 1);
        !            92: 
        !            93:        return vici_message_create_from_enumerator(request->create_enumerator(request));
        !            94: }
        !            95: 
        !            96: START_TEST(test_echo)
        !            97: {
        !            98:        vici_dispatcher_t *dispatcher;
        !            99:        vici_conn_t *conn;
        !           100:        vici_req_t *req;
        !           101:        vici_res_t *res;
        !           102: 
        !           103:        lib->processor->set_threads(lib->processor, 8);
        !           104: 
        !           105:        dispatcher = vici_dispatcher_create(URI);
        !           106:        ck_assert(dispatcher);
        !           107: 
        !           108:        dispatcher->manage_command(dispatcher, "echo", echo_cb, (void*)(uintptr_t)1);
        !           109: 
        !           110:        vici_init();
        !           111:        conn = vici_connect(URI);
        !           112:        ck_assert(conn);
        !           113: 
        !           114:        req = vici_begin("echo");
        !           115:        echo_tests[_i].encode(req);
        !           116:        res = vici_submit(req, conn);
        !           117:        ck_assert(res);
        !           118:        echo_tests[_i].decode(res);
        !           119:        vici_free_res(res);
        !           120: 
        !           121:        vici_disconnect(conn);
        !           122: 
        !           123:        dispatcher->manage_command(dispatcher, "echo", NULL, NULL);
        !           124: 
        !           125:        lib->processor->cancel(lib->processor);
        !           126:        dispatcher->destroy(dispatcher);
        !           127: 
        !           128:        vici_deinit();
        !           129: }
        !           130: END_TEST
        !           131: 
        !           132: START_TEST(test_missing)
        !           133: {
        !           134:        vici_dispatcher_t *dispatcher;
        !           135:        vici_conn_t *conn;
        !           136:        vici_req_t *req;
        !           137:        vici_res_t *res;
        !           138: 
        !           139:        lib->processor->set_threads(lib->processor, 8);
        !           140: 
        !           141:        dispatcher = vici_dispatcher_create(URI);
        !           142:        ck_assert(dispatcher);
        !           143: 
        !           144:        vici_init();
        !           145:        conn = vici_connect(URI);
        !           146:        ck_assert(conn);
        !           147: 
        !           148:        req = vici_begin("nonexistent");
        !           149:        encode_section(req);
        !           150:        res = vici_submit(req, conn);
        !           151:        ck_assert(res == NULL);
        !           152: 
        !           153:        vici_disconnect(conn);
        !           154: 
        !           155:        dispatcher->manage_command(dispatcher, "echo", NULL, NULL);
        !           156: 
        !           157:        lib->processor->cancel(lib->processor);
        !           158:        dispatcher->destroy(dispatcher);
        !           159: 
        !           160:        vici_deinit();
        !           161: }
        !           162: END_TEST
        !           163: 
        !           164: static void event_cb(void *user, char *name, vici_res_t *ev)
        !           165: {
        !           166:        int *events = (int*)user;
        !           167: 
        !           168:        (*events)++;
        !           169: }
        !           170: 
        !           171: START_TEST(test_stress)
        !           172: {
        !           173:        vici_dispatcher_t *dispatcher;
        !           174:        vici_conn_t *conn;
        !           175:        vici_req_t *req;
        !           176:        vici_res_t *res;
        !           177:        int i, total = 50, events = 0;
        !           178: 
        !           179:        lib->processor->set_threads(lib->processor, 8);
        !           180: 
        !           181:        dispatcher = vici_dispatcher_create(URI);
        !           182:        ck_assert(dispatcher);
        !           183: 
        !           184:        dispatcher->manage_command(dispatcher, "echo", echo_cb, (void*)(uintptr_t)1);
        !           185:        dispatcher->manage_event(dispatcher, "dummy", TRUE);
        !           186: 
        !           187:        vici_init();
        !           188:        conn = vici_connect(URI);
        !           189:        ck_assert(conn);
        !           190: 
        !           191:        for (i = 0; i < total; i++)
        !           192:        {
        !           193:                /* do some event management in between */
        !           194:                ck_assert(vici_register(conn, "dummy", event_cb, &events) == 0);
        !           195:                dispatcher->raise_event(dispatcher, "dummy", 0,
        !           196:                        vici_message_create_from_args(
        !           197:                                 VICI_KEY_VALUE, "key1", chunk_from_str("value1"),
        !           198:                                VICI_END));
        !           199: 
        !           200:                req = vici_begin("echo");
        !           201:                encode_section(req);
        !           202:                res = vici_submit(req, conn);
        !           203:                ck_assert(res);
        !           204:                decode_section(res);
        !           205:                vici_free_res(res);
        !           206: 
        !           207:                ck_assert(vici_register(conn, "dummy", NULL, NULL) == 0);
        !           208:        }
        !           209: 
        !           210:        while (events < total)
        !           211:        {
        !           212:                usleep(1000);
        !           213:        }
        !           214: 
        !           215:        vici_disconnect(conn);
        !           216: 
        !           217:        dispatcher->manage_command(dispatcher, "echo", NULL, NULL);
        !           218:        dispatcher->manage_event(dispatcher, "dummy", FALSE);
        !           219: 
        !           220:        lib->processor->cancel(lib->processor);
        !           221:        dispatcher->destroy(dispatcher);
        !           222: 
        !           223:        vici_deinit();
        !           224: }
        !           225: END_TEST
        !           226: 
        !           227: Suite *request_suite_create()
        !           228: {
        !           229:        Suite *s;
        !           230:        TCase *tc;
        !           231: 
        !           232:        s = suite_create("vici request");
        !           233: 
        !           234:        tc = tcase_create("echo");
        !           235:        tcase_add_loop_test(tc, test_echo, 0, countof(echo_tests));
        !           236:        suite_add_tcase(s, tc);
        !           237: 
        !           238:        tc = tcase_create("missing");
        !           239:        tcase_add_test(tc, test_missing);
        !           240:        suite_add_tcase(s, tc);
        !           241: 
        !           242:        tc = tcase_create("stress");
        !           243:        tcase_add_test(tc, test_stress);
        !           244:        suite_add_tcase(s, tc);
        !           245: 
        !           246:        return s;
        !           247: }

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