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

tests: add plugin tests
author David Demelier <markand@malikania.fr>
date Thu, 11 Feb 2021 17:39:22 +0100
parents
children a35537c50f09
comparison
equal deleted inserted replaced
994:56114ae85868 995:0d71bfa6c97a
1 /*
2 * test-plugin-ask.c -- test ask 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/plugin.h>
27 #include <irccd/server.h>
28
29 static struct irc_server *server;
30 static struct irc_plugin *plugin;
31
32 static void
33 setup(void *udata)
34 {
35 (void)udata;
36
37 server = irc_server_new("test", "t", "t", "t", "127.0.0.1", 6667);
38 plugin = js_plugin_open("test", CMAKE_SOURCE_DIR "/plugins/ask/ask.js");
39
40 if (!plugin)
41 errx(1, "could not load plugin");
42
43 irc_server_incref(server);
44 irc_plugin_set_option(plugin, "file", SOURCE "/data/answers.conf");
45 irc_plugin_load(plugin);
46
47 /* Fake server connected to send data. */
48 server->state = IRC_SERVER_STATE_CONNECTED;
49 }
50
51 static void
52 teardown(void *udata)
53 {
54 (void)udata;
55
56 irc_plugin_finish(plugin);
57 irc_server_decref(server);
58 }
59
60 GREATEST_TEST
61 basics_simple(void)
62 {
63 int no = 0, yes = 0;
64
65 /*
66 * Invoke the plugin 1000 times, it will be very unlucky to not have
67 * both answers in that amount of tries.
68 */
69 for (int i = 0; i < 1000; ++i) {
70 irc_plugin_handle(plugin, &(const struct irc_event) {
71 .type = IRC_EVENT_COMMAND,
72 .server = server,
73 .message = {
74 .message = "",
75 .origin = "jean",
76 .channel = "#test"
77 }
78 });
79
80 if (strcmp(server->conn.out, "PRIVMSG #test :jean, NO\r\n") == 0)
81 yes = 1;
82 else if (strcmp(server->conn.out, "PRIVMSG #test :jean, YES\r\n") == 0)
83 no = 1;
84
85 memset(server->conn.out, 0, sizeof (server->conn.out));
86 }
87
88 GREATEST_ASSERT(no);
89 GREATEST_ASSERT(yes);
90
91 GREATEST_PASS();
92 }
93
94 GREATEST_SUITE(suite_basics)
95 {
96 GREATEST_SET_SETUP_CB(setup, NULL);
97 GREATEST_SET_TEARDOWN_CB(teardown, NULL);
98 GREATEST_RUN_TEST(basics_simple);
99 }
100
101 GREATEST_MAIN_DEFS();
102
103 int
104 main(int argc, char **argv)
105 {
106 GREATEST_MAIN_BEGIN();
107 GREATEST_RUN_SUITE(suite_basics);
108 GREATEST_MAIN_END();
109
110 return 0;
111 }