comparison tests/test-plugin-logger.c @ 995:0d71bfa6c97a

tests: add plugin tests
author David Demelier <markand@malikania.fr>
date Thu, 11 Feb 2021 17:39:22 +0100
parents
children 3ea3361f0fc7
comparison
equal deleted inserted replaced
994:56114ae85868 995:0d71bfa6c97a
1 /*
2 * test-plugin-logger.c -- test logger plugin
3 *
4 * Copyright (c) 2013-2021 David Demelier <markand@malikania.fr>
5 *
6 * Permission to use, copy, modify, and/or distribute this software for any
7 * purpose with or without fee is hereby granted, provided that the above
8 * copyright notice and this permission notice appear in all copies.
9 *
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 */
18
19 #include <err.h>
20
21 #define GREATEST_USE_ABBREVS 0
22 #include <greatest.h>
23
24 #include <irccd/compat.h>
25 #include <irccd/js-plugin.h>
26 #include <irccd/log.h>
27 #include <irccd/plugin.h>
28 #include <irccd/server.h>
29
30 static struct irc_server *server;
31 static struct irc_plugin *plugin;
32
33 static void
34 setup(void *udata)
35 {
36 (void)udata;
37
38 remove(BINARY "/log");
39
40 server = irc_server_new("test", "t", "t", "t", "127.0.0.1", 6667);
41 plugin = js_plugin_open("logger", CMAKE_SOURCE_DIR "/plugins/logger/logger.js");
42
43 if (!plugin)
44 errx(1, "could not load plugin");
45
46 irc_log_to_console();
47 irc_server_incref(server);
48 irc_plugin_set_template(plugin, "join", "join=#{server}:#{channel}:#{origin}:#{nickname}");
49 irc_plugin_set_template(plugin, "kick", "kick=#{server}:#{channel}:#{origin}:#{nickname}:#{target}:#{reason}");
50 irc_plugin_set_template(plugin, "me", "me=#{server}:#{channel}:#{origin}:#{nickname}:#{message}");
51 irc_plugin_set_template(plugin, "message", "message=#{server}:#{channel}:#{origin}:#{nickname}:#{message}");
52 irc_plugin_set_template(plugin, "mode", "mode=#{server}:#{origin}:#{channel}:#{mode}:#{limit}:#{user}:#{mask}");
53 irc_plugin_set_template(plugin, "notice", "notice=#{server}:#{origin}:#{channel}:#{message}");
54 irc_plugin_set_template(plugin, "part", "part=#{server}:#{channel}:#{origin}:#{nickname}:#{reason}");
55 irc_plugin_set_template(plugin, "query", "query=#{server}:#{origin}:#{nickname}:#{message}");
56 irc_plugin_set_template(plugin, "topic", "topic=#{server}:#{channel}:#{origin}:#{nickname}:#{topic}");
57 irc_plugin_set_option(plugin, "file", BINARY "/log");
58 irc_plugin_load(plugin);
59
60 /* Fake server connected to send data. */
61 server->state = IRC_SERVER_STATE_CONNECTED;
62 }
63
64 static void
65 teardown(void *udata)
66 {
67 (void)udata;
68
69 irc_plugin_finish(plugin);
70 irc_server_decref(server);
71 }
72
73 static const char *
74 last(void)
75 {
76 static char buf[1024];
77 FILE *fp;
78
79 buf[0] = '\0';
80
81 if (!(fp = fopen(BINARY "/log", "r")))
82 err(1, "fopen");
83 if (!(fgets(buf, sizeof (buf), fp)))
84 err(1, "fgets");
85
86 fclose(fp);
87
88 buf[strcspn(buf, "\r\n")] = '\0';
89
90 return buf;
91 }
92
93 GREATEST_TEST
94 basics_join(void)
95 {
96 irc_plugin_handle(plugin, &(const struct irc_event) {
97 .type = IRC_EVENT_JOIN,
98 .server = server,
99 .join = {
100 .origin = "jean!jean@localhost",
101 .channel = "#staff"
102 }
103 });
104
105 GREATEST_ASSERT_STR_EQ("join=test:#staff:jean!jean@localhost:jean", last());
106 GREATEST_PASS();
107 }
108
109 GREATEST_TEST
110 basics_kick(void)
111 {
112 irc_plugin_handle(plugin, &(const struct irc_event) {
113 .type = IRC_EVENT_KICK,
114 .server = server,
115 .kick = {
116 .origin = "jean!jean@localhost",
117 .channel = "#staff",
118 .target = "badboy",
119 .reason = "please do not flood"
120 }
121 });
122
123 GREATEST_ASSERT_STR_EQ("kick=test:#staff:jean!jean@localhost:jean:badboy:please do not flood", last());
124 GREATEST_PASS();
125 }
126
127 GREATEST_TEST
128 basics_me(void)
129 {
130 irc_plugin_handle(plugin, &(const struct irc_event) {
131 .type = IRC_EVENT_ME,
132 .server = server,
133 .message = {
134 .origin = "jean!jean@localhost",
135 .channel = "#staff",
136 .message = "is drinking water"
137 }
138 });
139
140 GREATEST_ASSERT_STR_EQ("me=test:#staff:jean!jean@localhost:jean:is drinking water", last());
141 GREATEST_PASS();
142 }
143
144 GREATEST_TEST
145 basics_message(void)
146 {
147 irc_plugin_handle(plugin, &(const struct irc_event) {
148 .type = IRC_EVENT_MESSAGE,
149 .server = server,
150 .message = {
151 .origin = "jean!jean@localhost",
152 .channel = "#staff",
153 .message = "hello guys"
154 }
155 });
156
157 GREATEST_ASSERT_STR_EQ("message=test:#staff:jean!jean@localhost:jean:hello guys", last());
158 GREATEST_PASS();
159 }
160
161 GREATEST_TEST
162 basics_mode(void)
163 {
164 irc_plugin_handle(plugin, &(const struct irc_event) {
165 .type = IRC_EVENT_MODE,
166 .server = server,
167 .mode = {
168 .origin = "jean!jean@localhost",
169 .channel = "chris",
170 .mode = "+i",
171 .limit = "l",
172 .user = "u",
173 .mask = "m"
174 }
175 });
176
177 GREATEST_ASSERT_STR_EQ("mode=test:jean!jean@localhost:chris:+i:l:u:m", last());
178 GREATEST_PASS();
179 }
180
181 GREATEST_TEST
182 basics_notice(void)
183 {
184 irc_plugin_handle(plugin, &(const struct irc_event) {
185 .type = IRC_EVENT_NOTICE,
186 .server = server,
187 .notice = {
188 .origin = "jean!jean@localhost",
189 .channel = "chris",
190 .notice = "tu veux voir mon chat ?"
191 }
192 });
193
194 GREATEST_ASSERT_STR_EQ("notice=test:jean!jean@localhost:chris:tu veux voir mon chat ?", last());
195 GREATEST_PASS();
196 }
197
198 GREATEST_TEST
199 basics_part(void)
200 {
201 irc_plugin_handle(plugin, &(const struct irc_event) {
202 .type = IRC_EVENT_PART,
203 .server = server,
204 .part = {
205 .origin = "jean!jean@localhost",
206 .channel = "#staff",
207 .reason = "too noisy here"
208 }
209 });
210
211 GREATEST_ASSERT_STR_EQ("part=test:#staff:jean!jean@localhost:jean:too noisy here", last());
212 GREATEST_PASS();
213 }
214
215 GREATEST_TEST
216 basics_topic(void)
217 {
218 irc_plugin_handle(plugin, &(const struct irc_event) {
219 .type = IRC_EVENT_TOPIC,
220 .server = server,
221 .topic = {
222 .origin = "jean!jean@localhost",
223 .channel = "#staff",
224 .topic = "oh yeah yeaaaaaaaah"
225 }
226 });
227
228 GREATEST_ASSERT_STR_EQ("topic=test:#staff:jean!jean@localhost:jean:oh yeah yeaaaaaaaah", last());
229 GREATEST_PASS();
230 }
231
232 GREATEST_TEST
233 basics_case_insensitive(void)
234 {
235 irc_plugin_handle(plugin, &(const struct irc_event) {
236 .type = IRC_EVENT_MESSAGE,
237 .server = server,
238 .message = {
239 .origin = "jean!jean@localhost",
240 .channel = "#STAFF",
241 .message = "hello guys"
242 }
243 });
244
245 GREATEST_ASSERT_STR_EQ("message=test:#staff:jean!jean@localhost:jean:hello guys", last());
246 GREATEST_PASS();
247 }
248
249 GREATEST_SUITE(suite_basics)
250 {
251 GREATEST_SET_SETUP_CB(setup, NULL);
252 GREATEST_SET_TEARDOWN_CB(teardown, NULL);
253 GREATEST_RUN_TEST(basics_join);
254 GREATEST_RUN_TEST(basics_kick);
255 GREATEST_RUN_TEST(basics_me);
256 GREATEST_RUN_TEST(basics_message);
257 GREATEST_RUN_TEST(basics_mode);
258 GREATEST_RUN_TEST(basics_notice);
259 GREATEST_RUN_TEST(basics_part);
260 GREATEST_RUN_TEST(basics_topic);
261 GREATEST_RUN_TEST(basics_case_insensitive);
262 }
263
264 GREATEST_MAIN_DEFS();
265
266 int
267 main(int argc, char **argv)
268 {
269 GREATEST_MAIN_BEGIN();
270 GREATEST_RUN_SUITE(suite_basics);
271 GREATEST_MAIN_END();
272
273 return 0;
274 }