Files
mcde/lib/libDtChat/test_irc.c
T
lotherk 8665cab421 dtchat: IRC messages, splitting and the send bucket (T3.5)
Incoming PRIVMSG/NOTICE become text/notice messages, CTCP ACTION becomes an action message and direct messages open a direct conversation. dc_irc_split_next() splits at UTF-8 boundaries, prefers spaces and always breaks at newlines; the adapter budgets 512 bytes per PRIVMSG line. Sends go through a 128-line queue with a 4-message burst and 2 messages/s, pumped from the poll timeout. test_irc covers the splitter (ASCII, multibyte, over-wide character, newlines, empty); the fake-server test passes 19/19 with three chunks rejoining exactly, every line <= 512 bytes and a 0.50s max gap under flood. make mcde-host still blocked by pid 1207058.
2026-11-16 19:18:28 +00:00

269 lines
7.0 KiB
C

/*
* test_irc.c -- tests for the IRC line parser (T3.2)
*
* License: CNCL 1.0 (Could Not Care Less), see LICENSE at the top of the tree.
*/
#include "dc_internal.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
static int fails;
#define CHECK(cond) do {\
if (!(cond)) {\
fprintf(stderr, "FAIL %s:%d: %s\n", __FILE__, __LINE__,\
#cond);\
fails++;\
}\
} while (0)
static int
parse(const char *line, DcIrcMessage *msg)
{
char err[128];
return dc_irc_parse(line, strlen(line), msg, err, sizeof(err));
}
static void
test_privmsg(void)
{
DcIrcMessage msg;
CHECK(parse("PRIVMSG #cde :hello world", &msg) == 0);
CHECK(msg.tags == NULL);
CHECK(msg.prefix == NULL);
CHECK(strcmp(msg.command, "PRIVMSG") == 0);
CHECK(msg.nparams == 1);
CHECK(strcmp(msg.params[0], "#cde") == 0);
CHECK(msg.trailing != NULL);
CHECK(strcmp(msg.trailing, "hello world") == 0);
dc_irc_message_free(&msg);
CHECK(msg.command == NULL);
}
static void
test_prefix_and_tags(void)
{
DcIrcMessage msg;
CHECK(parse("@time=2026-11-16T19:00:00.000Z;msgid=abc "
":alice!a@example.org PRIVMSG #cde :hi there", &msg) == 0);
CHECK(msg.tags != NULL);
CHECK(strcmp(msg.tags,
"time=2026-11-16T19:00:00.000Z;msgid=abc") == 0);
CHECK(msg.prefix != NULL);
CHECK(strcmp(msg.prefix, "alice!a@example.org") == 0);
CHECK(strcmp(msg.command, "PRIVMSG") == 0);
CHECK(msg.nparams == 1);
CHECK(strcmp(msg.params[0], "#cde") == 0);
CHECK(strcmp(msg.trailing, "hi there") == 0);
dc_irc_message_free(&msg);
}
static void
test_numerics(void)
{
DcIrcMessage msg;
CHECK(parse(":srv 001 tester :Welcome to the network", &msg) == 0);
CHECK(strcmp(msg.prefix, "srv") == 0);
CHECK(strcmp(msg.command, "001") == 0);
CHECK(msg.nparams == 1);
CHECK(strcmp(msg.params[0], "tester") == 0);
CHECK(strcmp(msg.trailing, "Welcome to the network") == 0);
dc_irc_message_free(&msg);
CHECK(parse(":srv 005 tester CHANTYPES=# PREFIX=(ov)@+ :are supported",
&msg) == 0);
CHECK(strcmp(msg.command, "005") == 0);
CHECK(msg.nparams == 3);
CHECK(strcmp(msg.params[2], "PREFIX=(ov)@+") == 0);
CHECK(strcmp(msg.trailing, "are supported") == 0);
dc_irc_message_free(&msg);
}
static void
test_ping_mode_names(void)
{
DcIrcMessage msg;
CHECK(parse("PING :abc", &msg) == 0);
CHECK(strcmp(msg.command, "PING") == 0);
CHECK(msg.nparams == 0);
CHECK(strcmp(msg.trailing, "abc") == 0);
dc_irc_message_free(&msg);
CHECK(parse("MODE #cde +o alice", &msg) == 0);
CHECK(strcmp(msg.command, "MODE") == 0);
CHECK(msg.nparams == 3);
CHECK(strcmp(msg.params[0], "#cde") == 0);
CHECK(strcmp(msg.params[1], "+o") == 0);
CHECK(strcmp(msg.params[2], "alice") == 0);
CHECK(msg.trailing == NULL);
dc_irc_message_free(&msg);
CHECK(parse(":srv 353 tester = #cde :alice bob carol", &msg) == 0);
CHECK(strcmp(msg.command, "353") == 0);
CHECK(msg.nparams == 3);
CHECK(strcmp(msg.trailing, "alice bob carol") == 0);
dc_irc_message_free(&msg);
CHECK(parse("CAP * LS :multi-prefix sasl", &msg) == 0);
CHECK(msg.nparams == 2);
CHECK(strcmp(msg.trailing, "multi-prefix sasl") == 0);
dc_irc_message_free(&msg);
CHECK(parse("PRIVMSG #cde :", &msg) == 0);
CHECK(msg.trailing != NULL);
CHECK(strcmp(msg.trailing, "") == 0);
dc_irc_message_free(&msg);
/* several spaces between tokens are tolerated */
CHECK(parse("PRIVMSG #cde :hi", &msg) == 0);
CHECK(strcmp(msg.command, "PRIVMSG") == 0);
CHECK(msg.nparams == 1);
CHECK(strcmp(msg.trailing, "hi") == 0);
dc_irc_message_free(&msg);
}
static void
test_errors(void)
{
DcIrcMessage msg;
char err[128];
char *big;
CHECK(dc_irc_parse("", 0, &msg, err, sizeof(err)) == -1);
CHECK(msg.command == NULL);
CHECK(strstr(err, "command") != NULL);
CHECK(dc_irc_parse(" ", 3, &msg, err, sizeof(err)) == -1);
CHECK(dc_irc_parse("PRIVMSG", 7, &msg, NULL, 0) == 0);
dc_irc_message_free(&msg);
CHECK(dc_irc_parse("A\0B", 3, &msg, err, sizeof(err)) == -1);
CHECK(strstr(err, "NUL") != NULL);
CHECK(dc_irc_parse(":only-prefix", 12, &msg, err, sizeof(err)) == -1);
CHECK(dc_irc_parse("@tag-only", 9, &msg, err, sizeof(err)) == -1);
CHECK(dc_irc_parse("PRIVMSG 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16",
strlen("PRIVMSG 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16"),
&msg, err, sizeof(err)) == -1);
CHECK(strstr(err, "15") != NULL);
/* 1 MiB of rubbish: either a single huge command or a clean error,
* but no crash and no leak */
big = malloc(DC_IRC_MAX_LINE + 1);
CHECK(big != NULL);
if (big != NULL) {
memset(big, 'A', DC_IRC_MAX_LINE);
big[DC_IRC_MAX_LINE] = '\0';
if (dc_irc_parse(big, DC_IRC_MAX_LINE, &msg, err,
sizeof(err)) == 0) {
CHECK(strlen(msg.command) == DC_IRC_MAX_LINE);
dc_irc_message_free(&msg);
}
CHECK(dc_irc_parse(big, DC_IRC_MAX_LINE + 1, &msg, err,
sizeof(err)) == -1);
CHECK(strstr(err, "long") != NULL);
free(big);
}
/* a long line with real structure: 4000 params is over the limit */
{
size_t len = 20000;
char *line = malloc(len + 1);
CHECK(line != NULL);
if (line != NULL) {
size_t i;
memcpy(line, "PRIVMSG", 7);
for (i = 7; i < len; i += 2) {
line[i] = ' ';
line[i + 1] = 'x';
}
line[len] = '\0';
CHECK(dc_irc_parse(line, len, &msg, err,
sizeof(err)) == -1);
CHECK(strstr(err, "15") != NULL);
free(line);
}
}
}
static void
test_split(void)
{
char out[256];
char joined[4096];
size_t off, n;
const char *text = "the quick brown fox jumps over the lazy dog";
off = 0;
joined[0] = '\0';
while ((n = dc_irc_split_next(text, &off, 10, out, sizeof(out))) > 0) {
CHECK(n <= 10);
CHECK(dc_text_valid_utf8(out));
if (joined[0] != '\0')
strcat(joined, " ");
strcat(joined, out);
}
CHECK(strcmp(joined, text) == 0);
/* multibyte characters are never cut */
{
int chunks = 0;
off = 0;
while ((n = dc_irc_split_next("\u00e4\u00e4 \u00e4\u00e4 \u00e4\u00e4",
&off, 5, out, sizeof(out))) > 0) {
CHECK(dc_text_valid_utf8(out));
CHECK(n <= 5);
chunks++;
}
CHECK(chunks == 3);
}
/* one character wider than the budget still makes progress */
off = 0;
n = dc_irc_split_next("\u00e4\u00e4", &off, 1, out, sizeof(out));
CHECK(n == 2);
CHECK(strcmp(out, "\u00e4") == 0);
n = dc_irc_split_next("\u00e4\u00e4", &off, 1, out, sizeof(out));
CHECK(n == 2);
n = dc_irc_split_next("\u00e4\u00e4", &off, 1, out, sizeof(out));
CHECK(n == 0);
/* newlines separate messages */
off = 0;
CHECK(dc_irc_split_next("one\ntwo", &off, 100, out, sizeof(out)) == 3);
CHECK(strcmp(out, "one") == 0);
CHECK(dc_irc_split_next("one\ntwo", &off, 100, out, sizeof(out)) == 3);
CHECK(strcmp(out, "two") == 0);
CHECK(dc_irc_split_next("one\ntwo", &off, 100, out, sizeof(out)) == 0);
/* empty and spaces only */
off = 0;
CHECK(dc_irc_split_next(" ", &off, 10, out, sizeof(out)) == 0);
off = 0;
CHECK(dc_irc_split_next("", &off, 10, out, sizeof(out)) == 0);
}
int
main(void)
{
test_privmsg();
test_prefix_and_tags();
test_numerics();
test_ping_mode_names();
test_errors();
test_split();
if (fails != 0) {
fprintf(stderr, "%d test(s) failed\n", fails);
return 1;
}
printf("test_irc: all ok\n");
return 0;
}