vici: Add a libvici low-level client library
[strongswan.git] / src / libcharon / plugins / vici / suites / test_event.c
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 #define URI "unix:///tmp/strongswan-vici-event-test"
24
25 static void event_cb(void *user, char *name, vici_res_t *ev)
26 {
27 int *count = (int*)user;
28
29 ck_assert_str_eq(name, "test");
30 ck_assert(vici_parse(ev) == VICI_PARSE_KEY_VALUE);
31 ck_assert_str_eq(vici_parse_name(ev), "key1");
32 ck_assert_str_eq(vici_parse_value_str(ev), "value1");
33 ck_assert(vici_parse(ev) == VICI_PARSE_END);
34
35 (*count)++;
36 }
37
38 START_TEST(test_event)
39 {
40 vici_dispatcher_t *dispatcher;
41 vici_conn_t *conn;
42 int count = 0;
43
44 lib->processor->set_threads(lib->processor, 8);
45
46 dispatcher = vici_dispatcher_create(URI);
47 ck_assert(dispatcher);
48
49 dispatcher->manage_event(dispatcher, "test", TRUE);
50
51 vici_init();
52 conn = vici_connect(URI);
53 ck_assert(conn);
54
55 ck_assert(vici_register(conn, "test", event_cb, &count) == 0);
56 ck_assert(vici_register(conn, "nonexistent", event_cb, &count) != 0);
57
58 dispatcher->raise_event(dispatcher, "test", vici_message_create_from_args(
59 VICI_KEY_VALUE, "key1", chunk_from_str("value1"),
60 VICI_END));
61
62 while (count == 0)
63 {
64 usleep(1000);
65 }
66
67 vici_disconnect(conn);
68
69 dispatcher->manage_event(dispatcher, "test", FALSE);
70
71 lib->processor->cancel(lib->processor);
72 dispatcher->destroy(dispatcher);
73
74 vici_deinit();
75 }
76 END_TEST
77
78 START_TEST(test_stress)
79 {
80 vici_dispatcher_t *dispatcher;
81 vici_conn_t *conn;
82 int count = 0, i, total = 50;
83
84 lib->processor->set_threads(lib->processor, 8);
85
86 dispatcher = vici_dispatcher_create(URI);
87 ck_assert(dispatcher);
88
89 dispatcher->manage_event(dispatcher, "test", TRUE);
90 dispatcher->manage_event(dispatcher, "dummy", TRUE);
91
92 vici_init();
93 conn = vici_connect(URI);
94 ck_assert(conn);
95
96 vici_register(conn, "test", event_cb, &count);
97
98 for (i = 0; i < total; i++)
99 {
100 /* do some event re/deregistration in between */
101 ck_assert(vici_register(conn, "dummy", event_cb, NULL) == 0);
102
103 dispatcher->raise_event(dispatcher, "test",
104 vici_message_create_from_args(
105 VICI_KEY_VALUE, "key1", chunk_from_str("value1"),
106 VICI_END));
107
108 ck_assert(vici_register(conn, "dummy", NULL, NULL) == 0);
109 }
110
111 while (count < total)
112 {
113 usleep(1000);
114 }
115
116 vici_disconnect(conn);
117
118 dispatcher->manage_event(dispatcher, "test", FALSE);
119 dispatcher->manage_event(dispatcher, "dummy", FALSE);
120
121 lib->processor->cancel(lib->processor);
122 dispatcher->destroy(dispatcher);
123
124 vici_deinit();
125 }
126 END_TEST
127
128 Suite *event_suite_create()
129 {
130 Suite *s;
131 TCase *tc;
132
133 s = suite_create("vici events");
134
135 tc = tcase_create("single");
136 tcase_add_test(tc, test_event);
137 suite_add_tcase(s, tc);
138
139 tc = tcase_create("stress");
140 tcase_add_test(tc, test_stress);
141 suite_add_tcase(s, tc);
142
143 return s;
144 }