diff --git a/.gitignore b/.gitignore index abceee2..a3ab96c 100644 --- a/.gitignore +++ b/.gitignore @@ -54,8 +54,9 @@ tests/test_sasl tests/test_scram tests/test_sha1 tests/test_snprintf -tests/test_string tests/test_sock +tests/test_stanza +tests/test_string m4/ libstrophe.project libs/ diff --git a/Makefile.am b/Makefile.am index 54b7f6d..a435f55 100644 --- a/Makefile.am +++ b/Makefile.am @@ -139,6 +139,7 @@ TESTS = \ tests/test_jid \ tests/test_snprintf \ tests/test_string \ + tests/test_stanza \ tests/test_resolver check_PROGRAMS = $(TESTS) @@ -193,3 +194,8 @@ tests_test_string_SOURCES = tests/test_string.c tests/test.h tests_test_string_CFLAGS = $(STROPHE_FLAGS) -I$(top_srcdir)/src tests_test_string_LDADD = $(STROPHE_LIBS) tests_test_string_LDFLAGS = -static + +tests_test_stanza_SOURCES = tests/test_stanza.c +tests_test_stanza_CFLAGS = $(STROPHE_FLAGS) +tests_test_stanza_LDADD = $(STROPHE_LIBS) +tests_test_stanza_LDFLAGS = -static diff --git a/tests/test_stanza.c b/tests/test_stanza.c new file mode 100644 index 0000000..8b522de --- /dev/null +++ b/tests/test_stanza.c @@ -0,0 +1,96 @@ +/* test_stanza.c + * libstrophe XMPP client library -- test routines for stanza functions + * + * Copyright (C) 2020 Dmitry Podgorny + * + * This software is provided AS-IS with no warranty, either express + * or implied. + * + * This program is dual licensed under the MIT and GPLv3 licenses. + */ + +/* gcc -o test_stanza -I./src tests/test_stanza.c -lstrophe */ + +#include + +#include +#include + +#define MAGICPTR ((void *)0xfeedbeef) +static unsigned long used_blocks = 0; + +static void *stanza_alloc(size_t size, void *userdata) +{ + assert(userdata == MAGICPTR); + + ++used_blocks; + return malloc(size); +} + +static void stanza_free(void *ptr, void *userdata) +{ + assert(userdata == MAGICPTR); + + --used_blocks; + free(ptr); +} + +static void *stanza_realloc(void *ptr, size_t size, void *userdata) +{ + assert(userdata == MAGICPTR); + + return realloc(ptr, size); +} + +static const xmpp_mem_t stanza_mem = { + .alloc = &stanza_alloc, + .free = &stanza_free, + .realloc = &stanza_realloc, + .userdata = MAGICPTR, +}; + +static void test_stanza_add_child(xmpp_ctx_t *ctx) +{ + xmpp_stanza_t *stanza; + xmpp_stanza_t *child; + unsigned long baseline = used_blocks; + + /* xmpp_stanza_add_child */ + + stanza = xmpp_stanza_new(ctx); + child = xmpp_stanza_new(ctx); + assert(stanza != NULL); + assert(child != NULL); + xmpp_stanza_add_child(stanza, child); + xmpp_stanza_release(stanza); + assert(used_blocks > baseline); + xmpp_stanza_release(child); + assert(used_blocks == baseline); + + /* xmpp_stanza_add_child_ex */ + + stanza = xmpp_stanza_new(ctx); + child = xmpp_stanza_new(ctx); + assert(stanza != NULL); + assert(child != NULL); + xmpp_stanza_add_child_ex(stanza, child, 0); + xmpp_stanza_release(stanza); + assert(used_blocks == baseline); +} + +int main() +{ + xmpp_ctx_t *ctx; + + xmpp_initialize(); + ctx = xmpp_ctx_new(&stanza_mem, NULL); + assert(ctx != NULL); + + test_stanza_add_child(ctx); + + xmpp_ctx_free(ctx); + xmpp_shutdown(); + + /* All allocated blocks must be freed. */ + assert(used_blocks == 0); +}