ui_accounts.c builds the account list and a settings form from the adapter schema: text, secret, int, bool, choice, info and button. Secrets are collected into a separate JSON object and never shown; a modifyVerify callback keeps the masked text while the field stores the real value (this Motif has no XmNechoChar). Add/Delete create or remove accounts through dc_model_remove_account, Save validates required fields and numbers and updates the account label. Wired to File/Accounts... with a demo IRC schema. Standalone build clean; dialog opens on Xvfb, password masked, Save shows 'saved (demo)'; test_conv and ASan/UBSan green. make mcde-host could not run: another agent's release build holds build/.build.lock (pid 175328).
191 lines
5.6 KiB
C
191 lines
5.6 KiB
C
/*
|
|
* test_conv.c -- unit tests for the account/conversation model
|
|
*
|
|
* 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 void
|
|
test_accounts(void)
|
|
{
|
|
DcModel *model = dc_model_new();
|
|
DcAccount *account;
|
|
DcConv *conv;
|
|
DcConv *again;
|
|
|
|
CHECK(model != NULL);
|
|
CHECK(dc_model_account_count(model) == 0);
|
|
account = dc_model_add_account(model, "libera", "irc", "Libera");
|
|
CHECK(account != NULL);
|
|
CHECK(dc_model_account_count(model) == 1);
|
|
CHECK(dc_model_account_at(model, 0) == account);
|
|
CHECK(dc_model_account_at(model, 1) == NULL);
|
|
CHECK(strcmp(dc_account_id(account), "libera") == 0);
|
|
CHECK(strcmp(dc_account_protocol(account), "irc") == 0);
|
|
CHECK(strcmp(dc_account_label(account), "Libera") == 0);
|
|
CHECK(dc_account_conv_count(account) == 0);
|
|
|
|
/* idempotent: same pointer, label updated, no duplicate */
|
|
CHECK(dc_model_add_account(model, "libera", "irc", "Libera 2") ==
|
|
account);
|
|
CHECK(dc_model_account_count(model) == 1);
|
|
CHECK(strcmp(dc_account_label(account), "Libera 2") == 0);
|
|
|
|
conv = dc_model_add_conv(model, "libera", "#cde", DC_CONV_CHANNEL,
|
|
"#cde");
|
|
CHECK(conv != NULL);
|
|
CHECK(dc_account_conv_count(account) == 1);
|
|
CHECK(dc_account_conv_at(account, 0) == conv);
|
|
CHECK(dc_model_find_conv(model, "libera", "#cde") == conv);
|
|
CHECK(strcmp(dc_conv_id(conv), "#cde") == 0);
|
|
CHECK(dc_conv_kind(conv) == DC_CONV_CHANNEL);
|
|
CHECK(strcmp(dc_conv_title(conv), "#cde") == 0);
|
|
CHECK(dc_conv_topic(conv) == NULL);
|
|
CHECK(dc_conv_presence(conv) == DC_PRESENCE_OFFLINE);
|
|
dc_conv_set_presence(conv, DC_PRESENCE_ONLINE);
|
|
CHECK(dc_conv_presence(conv) == DC_PRESENCE_ONLINE);
|
|
|
|
again = dc_model_add_conv(model, "libera", "#cde", DC_CONV_CHANNEL,
|
|
"#cde -- topic");
|
|
CHECK(again == conv);
|
|
CHECK(dc_account_conv_count(account) == 1);
|
|
CHECK(strcmp(dc_conv_title(conv), "#cde -- topic") == 0);
|
|
dc_conv_set_topic(conv, "the topic");
|
|
CHECK(strcmp(dc_conv_topic(conv), "the topic") == 0);
|
|
|
|
CHECK(dc_model_find_account(model, "nope") == NULL);
|
|
CHECK(dc_model_find_conv(model, "nope", "#cde") == NULL);
|
|
CHECK(dc_model_find_conv(model, "libera", "nope") == NULL);
|
|
CHECK(dc_model_add_account(model, "", "irc", "") == NULL);
|
|
CHECK(dc_model_add_conv(model, "nope", "#x", DC_CONV_CHANNEL, "#x") ==
|
|
NULL);
|
|
CHECK(dc_model_add_conv(model, "libera", "", DC_CONV_DIRECT, "") ==
|
|
NULL);
|
|
CHECK(dc_model_remove_account(model, "nope") == -1);
|
|
CHECK(dc_model_remove_account(model, "libera") == 0);
|
|
CHECK(dc_model_account_count(model) == 0);
|
|
CHECK(dc_model_find_account(model, "libera") == NULL);
|
|
CHECK(dc_model_find_conv(model, "libera", "#cde") == NULL);
|
|
dc_model_free(model);
|
|
}
|
|
|
|
static void
|
|
test_messages(void)
|
|
{
|
|
DcModel *model = dc_model_new();
|
|
DcConv *conv;
|
|
const DcMessage *msg;
|
|
|
|
CHECK(model != NULL);
|
|
CHECK(dc_model_add_account(model, "a", "irc", "A") != NULL);
|
|
conv = dc_model_add_conv(model, "a", "bob", DC_CONV_DIRECT, "bob");
|
|
CHECK(conv != NULL);
|
|
CHECK(dc_conv_message_count(conv) == 0);
|
|
CHECK(dc_conv_message_at(conv, 0) == NULL);
|
|
|
|
CHECK(dc_conv_add_message(conv, "alice", "hello", 100, 0) == 0);
|
|
CHECK(dc_conv_add_message(conv, "bob", "world", 101, 0) == 0);
|
|
CHECK(dc_conv_add_message(conv, "me", "hi", 102, DTCHAT_MSG_SELF) == 0);
|
|
CHECK(dc_conv_message_count(conv) == 3);
|
|
CHECK(dc_conv_unread(conv) == 2);
|
|
|
|
msg = dc_conv_message_at(conv, 0);
|
|
CHECK(msg != NULL);
|
|
if (msg != NULL) {
|
|
CHECK(strcmp(msg->from, "alice") == 0);
|
|
CHECK(strcmp(msg->text, "hello") == 0);
|
|
CHECK(msg->ts == 100);
|
|
CHECK(msg->flags == 0);
|
|
}
|
|
msg = dc_conv_message_at(conv, 2);
|
|
CHECK(msg != NULL);
|
|
if (msg != NULL)
|
|
CHECK(msg->flags & DTCHAT_MSG_SELF);
|
|
CHECK(dc_conv_message_at(conv, 3) == NULL);
|
|
|
|
dc_conv_mark_read(conv);
|
|
CHECK(dc_conv_unread(conv) == 0);
|
|
CHECK(dc_conv_add_message(conv, "x", "joined", 103,
|
|
DTCHAT_MSG_SYSTEM) == 0);
|
|
CHECK(dc_conv_unread(conv) == 0);
|
|
CHECK(dc_conv_add_message(conv, "bob", "waves", 104,
|
|
DTCHAT_MSG_ACTION) == 0);
|
|
CHECK(dc_conv_unread(conv) == 1);
|
|
dc_model_free(model);
|
|
}
|
|
|
|
static void
|
|
test_ring(void)
|
|
{
|
|
DcModel *model = dc_model_new();
|
|
DcConv *conv;
|
|
char text[32];
|
|
size_t i;
|
|
const DcMessage *msg;
|
|
|
|
CHECK(model != NULL);
|
|
CHECK(dc_model_add_account(model, "a", "irc", "A") != NULL);
|
|
conv = dc_model_add_conv(model, "a", "#ring", DC_CONV_CHANNEL, "#ring");
|
|
CHECK(conv != NULL);
|
|
for (i = 0; i < 2500; i++) {
|
|
snprintf(text, sizeof(text), "m%zu", i);
|
|
CHECK(dc_conv_add_message(conv, "u", text, (long long)i, 0) == 0);
|
|
}
|
|
CHECK(dc_conv_message_count(conv) == 2000);
|
|
CHECK(dc_conv_unread(conv) == 2500);
|
|
msg = dc_conv_message_at(conv, 0);
|
|
CHECK(msg != NULL);
|
|
if (msg != NULL) {
|
|
CHECK(strcmp(msg->text, "m500") == 0);
|
|
CHECK(msg->ts == 500);
|
|
}
|
|
msg = dc_conv_message_at(conv, 1999);
|
|
CHECK(msg != NULL);
|
|
if (msg != NULL)
|
|
CHECK(strcmp(msg->text, "m2499") == 0);
|
|
CHECK(dc_conv_message_at(conv, 2000) == NULL);
|
|
|
|
dc_conv_mark_read(conv);
|
|
CHECK(dc_conv_add_message(conv, "me", "self", 2500,
|
|
DTCHAT_MSG_SELF) == 0);
|
|
CHECK(dc_conv_message_count(conv) == 2000);
|
|
CHECK(dc_conv_unread(conv) == 0);
|
|
msg = dc_conv_message_at(conv, 0);
|
|
CHECK(msg != NULL);
|
|
if (msg != NULL)
|
|
CHECK(strcmp(msg->text, "m501") == 0);
|
|
msg = dc_conv_message_at(conv, 1999);
|
|
CHECK(msg != NULL);
|
|
if (msg != NULL)
|
|
CHECK(strcmp(msg->text, "self") == 0);
|
|
dc_model_free(model);
|
|
}
|
|
|
|
int
|
|
main(void)
|
|
{
|
|
test_accounts();
|
|
test_messages();
|
|
test_ring();
|
|
if (fails != 0) {
|
|
fprintf(stderr, "%d test(s) failed\n", fails);
|
|
return 1;
|
|
}
|
|
printf("test_conv: all ok\n");
|
|
return 0;
|
|
}
|