]>
git.ipfire.org Git - thirdparty/strongswan.git/blob - src/libcharon/plugins/vici/suites/test_event.c
2 * Copyright (C) 2014 Martin Willi
4 * Copyright (C) secunet Security Networks AG
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the
8 * Free Software Foundation; either version 2 of the License, or (at your
9 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
13 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
17 #include <test_suite.h>
19 #include "../vici_dispatcher.h"
20 #include "../libvici.h"
25 # define URI "tcp://127.0.0.1:6543"
27 # define URI "unix:///tmp/strongswan-vici-event-test"
30 static void event_cb(void *user
, char *name
, vici_res_t
*ev
)
32 int *count
= (int*)user
;
34 ck_assert_str_eq(name
, "test");
35 ck_assert(vici_parse(ev
) == VICI_PARSE_KEY_VALUE
);
36 ck_assert_str_eq(vici_parse_name(ev
), "key1");
37 ck_assert_str_eq(vici_parse_value_str(ev
), "value1");
38 ck_assert(vici_parse(ev
) == VICI_PARSE_END
);
43 START_TEST(test_event
)
45 vici_dispatcher_t
*dispatcher
;
49 lib
->processor
->set_threads(lib
->processor
, 8);
51 dispatcher
= vici_dispatcher_create(URI
);
52 ck_assert(dispatcher
);
54 dispatcher
->manage_event(dispatcher
, "test", TRUE
);
57 conn
= vici_connect(URI
);
60 ck_assert(vici_register(conn
, "test", event_cb
, &count
) == 0);
61 ck_assert(vici_register(conn
, "nonexistent", event_cb
, &count
) != 0);
63 dispatcher
->raise_event(dispatcher
, "test", 0, vici_message_create_from_args(
64 VICI_KEY_VALUE
, "key1", chunk_from_str("value1"),
72 vici_disconnect(conn
);
74 dispatcher
->manage_event(dispatcher
, "test", FALSE
);
76 lib
->processor
->cancel(lib
->processor
);
77 dispatcher
->destroy(dispatcher
);
83 #define EVENT_COUNT 500
85 CALLBACK(raise_cb
, vici_message_t
*,
86 vici_dispatcher_t
*dispatcher
, char *name
, u_int id
, vici_message_t
*req
)
90 for (i
= 0; i
< EVENT_COUNT
; i
++)
92 dispatcher
->raise_event(dispatcher
, "event", id
,
93 vici_message_create_from_args(
94 VICI_KEY_VALUE
, "counter", chunk_from_thing(i
),
97 return vici_message_create_from_args(VICI_END
);
100 CALLBACK(raise_event_cb
, void,
101 int *count
, char *name
, vici_res_t
*ev
)
105 ck_assert_str_eq(name
, "event");
106 ck_assert(vici_parse(ev
) == VICI_PARSE_KEY_VALUE
);
107 ck_assert_str_eq(vici_parse_name(ev
), "counter");
108 value
= vici_parse_value(ev
, &len
);
109 ck_assert_int_eq(len
, sizeof(*value
));
110 ck_assert(vici_parse(ev
) == VICI_PARSE_END
);
112 ck_assert_int_eq(*count
, *value
);
116 START_TEST(test_raise_events
)
118 vici_dispatcher_t
*dispatcher
;
123 lib
->processor
->set_threads(lib
->processor
, 8);
125 dispatcher
= vici_dispatcher_create(URI
);
126 ck_assert(dispatcher
);
128 dispatcher
->manage_event(dispatcher
, "event", TRUE
);
129 dispatcher
->manage_command(dispatcher
, "raise", raise_cb
, dispatcher
);
132 conn
= vici_connect(URI
);
135 ck_assert(vici_register(conn
, "event", raise_event_cb
, &count
) == 0);
137 res
= vici_submit(vici_begin("raise"), conn
);
139 ck_assert_int_eq(count
, EVENT_COUNT
);
143 vici_disconnect(conn
);
145 dispatcher
->manage_event(dispatcher
, "event", FALSE
);
146 dispatcher
->manage_command(dispatcher
, "raise", NULL
, NULL
);
148 lib
->processor
->cancel(lib
->processor
);
149 dispatcher
->destroy(dispatcher
);
155 START_TEST(test_stress
)
157 vici_dispatcher_t
*dispatcher
;
159 int count
= 0, i
, total
= 50;
161 lib
->processor
->set_threads(lib
->processor
, 8);
163 dispatcher
= vici_dispatcher_create(URI
);
164 ck_assert(dispatcher
);
166 dispatcher
->manage_event(dispatcher
, "test", TRUE
);
167 dispatcher
->manage_event(dispatcher
, "dummy", TRUE
);
170 conn
= vici_connect(URI
);
173 vici_register(conn
, "test", event_cb
, &count
);
175 for (i
= 0; i
< total
; i
++)
177 /* do some event re/deregistration in between */
178 ck_assert(vici_register(conn
, "dummy", event_cb
, NULL
) == 0);
180 dispatcher
->raise_event(dispatcher
, "test", 0,
181 vici_message_create_from_args(
182 VICI_KEY_VALUE
, "key1", chunk_from_str("value1"),
185 ck_assert(vici_register(conn
, "dummy", NULL
, NULL
) == 0);
188 while (count
< total
)
193 vici_disconnect(conn
);
195 dispatcher
->manage_event(dispatcher
, "test", FALSE
);
196 dispatcher
->manage_event(dispatcher
, "dummy", FALSE
);
198 lib
->processor
->cancel(lib
->processor
);
199 dispatcher
->destroy(dispatcher
);
205 Suite
*event_suite_create()
210 s
= suite_create("vici events");
212 tc
= tcase_create("single");
213 tcase_add_test(tc
, test_event
);
214 suite_add_tcase(s
, tc
);
216 tc
= tcase_create("raise events");
217 tcase_add_test(tc
, test_raise_events
);
218 suite_add_tcase(s
, tc
);
220 tc
= tcase_create("stress");
221 tcase_add_test(tc
, test_stress
);
222 suite_add_tcase(s
, tc
);