]> git.ipfire.org Git - thirdparty/strongswan.git/blob - src/libcharon/plugins/vici/suites/test_event.c
Update copyright headers after acquisition by secunet
[thirdparty/strongswan.git] / src / libcharon / plugins / vici / suites / test_event.c
1 /*
2 * Copyright (C) 2014 Martin Willi
3 *
4 * Copyright (C) secunet Security Networks AG
5 *
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>.
10 *
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
14 * for more details.
15 */
16
17 #include <test_suite.h>
18
19 #include "../vici_dispatcher.h"
20 #include "../libvici.h"
21
22 #include <unistd.h>
23
24 #ifdef WIN32
25 # define URI "tcp://127.0.0.1:6543"
26 #else /* !WIN32 */
27 # define URI "unix:///tmp/strongswan-vici-event-test"
28 #endif /* !WIN32 */
29
30 static void event_cb(void *user, char *name, vici_res_t *ev)
31 {
32 int *count = (int*)user;
33
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);
39
40 (*count)++;
41 }
42
43 START_TEST(test_event)
44 {
45 vici_dispatcher_t *dispatcher;
46 vici_conn_t *conn;
47 int count = 0;
48
49 lib->processor->set_threads(lib->processor, 8);
50
51 dispatcher = vici_dispatcher_create(URI);
52 ck_assert(dispatcher);
53
54 dispatcher->manage_event(dispatcher, "test", TRUE);
55
56 vici_init();
57 conn = vici_connect(URI);
58 ck_assert(conn);
59
60 ck_assert(vici_register(conn, "test", event_cb, &count) == 0);
61 ck_assert(vici_register(conn, "nonexistent", event_cb, &count) != 0);
62
63 dispatcher->raise_event(dispatcher, "test", 0, vici_message_create_from_args(
64 VICI_KEY_VALUE, "key1", chunk_from_str("value1"),
65 VICI_END));
66
67 while (count == 0)
68 {
69 usleep(1000);
70 }
71
72 vici_disconnect(conn);
73
74 dispatcher->manage_event(dispatcher, "test", FALSE);
75
76 lib->processor->cancel(lib->processor);
77 dispatcher->destroy(dispatcher);
78
79 vici_deinit();
80 }
81 END_TEST
82
83 #define EVENT_COUNT 500
84
85 CALLBACK(raise_cb, vici_message_t*,
86 vici_dispatcher_t *dispatcher, char *name, u_int id, vici_message_t *req)
87 {
88 u_int i;
89
90 for (i = 0; i < EVENT_COUNT; i++)
91 {
92 dispatcher->raise_event(dispatcher, "event", id,
93 vici_message_create_from_args(
94 VICI_KEY_VALUE, "counter", chunk_from_thing(i),
95 VICI_END));
96 }
97 return vici_message_create_from_args(VICI_END);
98 }
99
100 CALLBACK(raise_event_cb, void,
101 int *count, char *name, vici_res_t *ev)
102 {
103 u_int *value, len;
104
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);
111
112 ck_assert_int_eq(*count, *value);
113 (*count)++;
114 }
115
116 START_TEST(test_raise_events)
117 {
118 vici_dispatcher_t *dispatcher;
119 vici_res_t *res;
120 vici_conn_t *conn;
121 int count = 0;
122
123 lib->processor->set_threads(lib->processor, 8);
124
125 dispatcher = vici_dispatcher_create(URI);
126 ck_assert(dispatcher);
127
128 dispatcher->manage_event(dispatcher, "event", TRUE);
129 dispatcher->manage_command(dispatcher, "raise", raise_cb, dispatcher);
130
131 vici_init();
132 conn = vici_connect(URI);
133 ck_assert(conn);
134
135 ck_assert(vici_register(conn, "event", raise_event_cb, &count) == 0);
136
137 res = vici_submit(vici_begin("raise"), conn);
138
139 ck_assert_int_eq(count, EVENT_COUNT);
140 ck_assert(res);
141 vici_free_res(res);
142
143 vici_disconnect(conn);
144
145 dispatcher->manage_event(dispatcher, "event", FALSE);
146 dispatcher->manage_command(dispatcher, "raise", NULL, NULL);
147
148 lib->processor->cancel(lib->processor);
149 dispatcher->destroy(dispatcher);
150
151 vici_deinit();
152 }
153 END_TEST
154
155 START_TEST(test_stress)
156 {
157 vici_dispatcher_t *dispatcher;
158 vici_conn_t *conn;
159 int count = 0, i, total = 50;
160
161 lib->processor->set_threads(lib->processor, 8);
162
163 dispatcher = vici_dispatcher_create(URI);
164 ck_assert(dispatcher);
165
166 dispatcher->manage_event(dispatcher, "test", TRUE);
167 dispatcher->manage_event(dispatcher, "dummy", TRUE);
168
169 vici_init();
170 conn = vici_connect(URI);
171 ck_assert(conn);
172
173 vici_register(conn, "test", event_cb, &count);
174
175 for (i = 0; i < total; i++)
176 {
177 /* do some event re/deregistration in between */
178 ck_assert(vici_register(conn, "dummy", event_cb, NULL) == 0);
179
180 dispatcher->raise_event(dispatcher, "test", 0,
181 vici_message_create_from_args(
182 VICI_KEY_VALUE, "key1", chunk_from_str("value1"),
183 VICI_END));
184
185 ck_assert(vici_register(conn, "dummy", NULL, NULL) == 0);
186 }
187
188 while (count < total)
189 {
190 usleep(1000);
191 }
192
193 vici_disconnect(conn);
194
195 dispatcher->manage_event(dispatcher, "test", FALSE);
196 dispatcher->manage_event(dispatcher, "dummy", FALSE);
197
198 lib->processor->cancel(lib->processor);
199 dispatcher->destroy(dispatcher);
200
201 vici_deinit();
202 }
203 END_TEST
204
205 Suite *event_suite_create()
206 {
207 Suite *s;
208 TCase *tc;
209
210 s = suite_create("vici events");
211
212 tc = tcase_create("single");
213 tcase_add_test(tc, test_event);
214 suite_add_tcase(s, tc);
215
216 tc = tcase_create("raise events");
217 tcase_add_test(tc, test_raise_events);
218 suite_add_tcase(s, tc);
219
220 tc = tcase_create("stress");
221 tcase_add_test(tc, test_stress);
222 suite_add_tcase(s, tc);
223
224 return s;
225 }