Annotation of embedaddon/strongswan/src/libcharon/plugins/vici/suites/test_event.c, revision 1.1.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-event-test"
                     27: #endif /* !WIN32 */
                     28: 
                     29: static void event_cb(void *user, char *name, vici_res_t *ev)
                     30: {
                     31:        int *count = (int*)user;
                     32: 
                     33:        ck_assert_str_eq(name, "test");
                     34:        ck_assert(vici_parse(ev) == VICI_PARSE_KEY_VALUE);
                     35:        ck_assert_str_eq(vici_parse_name(ev), "key1");
                     36:        ck_assert_str_eq(vici_parse_value_str(ev), "value1");
                     37:        ck_assert(vici_parse(ev) == VICI_PARSE_END);
                     38: 
                     39:        (*count)++;
                     40: }
                     41: 
                     42: START_TEST(test_event)
                     43: {
                     44:        vici_dispatcher_t *dispatcher;
                     45:        vici_conn_t *conn;
                     46:        int count = 0;
                     47: 
                     48:        lib->processor->set_threads(lib->processor, 8);
                     49: 
                     50:        dispatcher = vici_dispatcher_create(URI);
                     51:        ck_assert(dispatcher);
                     52: 
                     53:        dispatcher->manage_event(dispatcher, "test", TRUE);
                     54: 
                     55:        vici_init();
                     56:        conn = vici_connect(URI);
                     57:        ck_assert(conn);
                     58: 
                     59:        ck_assert(vici_register(conn, "test", event_cb, &count) == 0);
                     60:        ck_assert(vici_register(conn, "nonexistent", event_cb, &count) != 0);
                     61: 
                     62:        dispatcher->raise_event(dispatcher, "test", 0, vici_message_create_from_args(
                     63:                 VICI_KEY_VALUE, "key1", chunk_from_str("value1"),
                     64:                VICI_END));
                     65: 
                     66:        while (count == 0)
                     67:        {
                     68:                usleep(1000);
                     69:        }
                     70: 
                     71:        vici_disconnect(conn);
                     72: 
                     73:        dispatcher->manage_event(dispatcher, "test", FALSE);
                     74: 
                     75:        lib->processor->cancel(lib->processor);
                     76:        dispatcher->destroy(dispatcher);
                     77: 
                     78:        vici_deinit();
                     79: }
                     80: END_TEST
                     81: 
                     82: #define EVENT_COUNT 500
                     83: 
                     84: CALLBACK(raise_cb,  vici_message_t*,
                     85:        vici_dispatcher_t *dispatcher, char *name, u_int id, vici_message_t *req)
                     86: {
                     87:        u_int i;
                     88: 
                     89:        for (i = 0; i < EVENT_COUNT; i++)
                     90:        {
                     91:                dispatcher->raise_event(dispatcher, "event", id,
                     92:                        vici_message_create_from_args(
                     93:                                 VICI_KEY_VALUE, "counter", chunk_from_thing(i),
                     94:                                VICI_END));
                     95:        }
                     96:        return vici_message_create_from_args(VICI_END);
                     97: }
                     98: 
                     99: CALLBACK(raise_event_cb, void,
                    100:        int *count, char *name, vici_res_t *ev)
                    101: {
                    102:        u_int *value, len;
                    103: 
                    104:        ck_assert_str_eq(name, "event");
                    105:        ck_assert(vici_parse(ev) == VICI_PARSE_KEY_VALUE);
                    106:        ck_assert_str_eq(vici_parse_name(ev), "counter");
                    107:        value = vici_parse_value(ev, &len);
                    108:        ck_assert_int_eq(len, sizeof(*value));
                    109:        ck_assert(vici_parse(ev) == VICI_PARSE_END);
                    110: 
                    111:        ck_assert_int_eq(*count, *value);
                    112:        (*count)++;
                    113: }
                    114: 
                    115: START_TEST(test_raise_events)
                    116: {
                    117:        vici_dispatcher_t *dispatcher;
                    118:        vici_res_t *res;
                    119:        vici_conn_t *conn;
                    120:        int count = 0;
                    121: 
                    122:        lib->processor->set_threads(lib->processor, 8);
                    123: 
                    124:        dispatcher = vici_dispatcher_create(URI);
                    125:        ck_assert(dispatcher);
                    126: 
                    127:        dispatcher->manage_event(dispatcher, "event", TRUE);
                    128:        dispatcher->manage_command(dispatcher, "raise", raise_cb, dispatcher);
                    129: 
                    130:        vici_init();
                    131:        conn = vici_connect(URI);
                    132:        ck_assert(conn);
                    133: 
                    134:        ck_assert(vici_register(conn, "event", raise_event_cb, &count) == 0);
                    135: 
                    136:        res = vici_submit(vici_begin("raise"), conn);
                    137: 
                    138:        ck_assert_int_eq(count, EVENT_COUNT);
                    139:        ck_assert(res);
                    140:        vici_free_res(res);
                    141: 
                    142:        vici_disconnect(conn);
                    143: 
                    144:        dispatcher->manage_event(dispatcher, "event", FALSE);
                    145:        dispatcher->manage_command(dispatcher, "raise", NULL, NULL);
                    146: 
                    147:        lib->processor->cancel(lib->processor);
                    148:        dispatcher->destroy(dispatcher);
                    149: 
                    150:        vici_deinit();
                    151: }
                    152: END_TEST
                    153: 
                    154: START_TEST(test_stress)
                    155: {
                    156:        vici_dispatcher_t *dispatcher;
                    157:        vici_conn_t *conn;
                    158:        int count = 0, i, total = 50;
                    159: 
                    160:        lib->processor->set_threads(lib->processor, 8);
                    161: 
                    162:        dispatcher = vici_dispatcher_create(URI);
                    163:        ck_assert(dispatcher);
                    164: 
                    165:        dispatcher->manage_event(dispatcher, "test", TRUE);
                    166:        dispatcher->manage_event(dispatcher, "dummy", TRUE);
                    167: 
                    168:        vici_init();
                    169:        conn = vici_connect(URI);
                    170:        ck_assert(conn);
                    171: 
                    172:        vici_register(conn, "test", event_cb, &count);
                    173: 
                    174:        for (i = 0; i < total; i++)
                    175:        {
                    176:                /* do some event re/deregistration in between */
                    177:                ck_assert(vici_register(conn, "dummy", event_cb, NULL) == 0);
                    178: 
                    179:                dispatcher->raise_event(dispatcher, "test", 0,
                    180:                        vici_message_create_from_args(
                    181:                                 VICI_KEY_VALUE, "key1", chunk_from_str("value1"),
                    182:                                VICI_END));
                    183: 
                    184:                ck_assert(vici_register(conn, "dummy", NULL, NULL) == 0);
                    185:        }
                    186: 
                    187:        while (count < total)
                    188:        {
                    189:                usleep(1000);
                    190:        }
                    191: 
                    192:        vici_disconnect(conn);
                    193: 
                    194:        dispatcher->manage_event(dispatcher, "test", FALSE);
                    195:        dispatcher->manage_event(dispatcher, "dummy", FALSE);
                    196: 
                    197:        lib->processor->cancel(lib->processor);
                    198:        dispatcher->destroy(dispatcher);
                    199: 
                    200:        vici_deinit();
                    201: }
                    202: END_TEST
                    203: 
                    204: Suite *event_suite_create()
                    205: {
                    206:        Suite *s;
                    207:        TCase *tc;
                    208: 
                    209:        s = suite_create("vici events");
                    210: 
                    211:        tc = tcase_create("single");
                    212:        tcase_add_test(tc, test_event);
                    213:        suite_add_tcase(s, tc);
                    214: 
                    215:        tc = tcase_create("raise events");
                    216:        tcase_add_test(tc, test_raise_events);
                    217:        suite_add_tcase(s, tc);
                    218: 
                    219:        tc = tcase_create("stress");
                    220:        tcase_add_test(tc, test_stress);
                    221:        suite_add_tcase(s, tc);
                    222: 
                    223:        return s;
                    224: }

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