diff --git a/.gitignore b/.gitignore index 2312939..75642bc 100644 --- a/.gitignore +++ b/.gitignore @@ -64,6 +64,7 @@ tests/test_rand tests/test_resolver tests/test_sasl tests/test_scram +tests/test_send_queue tests/test_sha1 tests/test_sha256 tests/test_sha512 diff --git a/Makefile.am b/Makefile.am index c4f2eb4..c57dd38 100644 --- a/Makefile.am +++ b/Makefile.am @@ -178,6 +178,7 @@ TESTS = \ tests/test_base64 \ tests/test_hash \ tests/test_jid \ + tests/test_send_queue \ tests/test_snprintf \ tests/test_string \ tests/test_stanza \ @@ -255,6 +256,11 @@ tests_test_sha512_CFLAGS = -I$(top_srcdir)/src tests_test_md5_SOURCES = tests/test_md5.c tests/test.c src/md5.c tests_test_md5_CFLAGS = -I$(top_srcdir)/src +tests_test_send_queue_SOURCES = tests/test_send_queue.c +tests_test_send_queue_CFLAGS = -I$(top_srcdir)/src +tests_test_send_queue_LDADD = $(STROPHE_LIBS) +tests_test_send_queue_LDFLAGS = -static + tests_test_snprintf_SOURCES = tests/test_snprintf.c tests_test_snprintf_CFLAGS = -I$(top_srcdir)/src diff --git a/tests/test.h b/tests/test.h index cd883a2..509975a 100644 --- a/tests/test.h +++ b/tests/test.h @@ -61,6 +61,19 @@ } \ } while (0) +#define ENSURE_EQ(v1, v2) \ + do { \ + int __v1 = v1; \ + int __v2 = v2; \ + if (__v1 != __v2) { \ + printf("Error: %s\n" \ + "Expected: %d\n" \ + "Got: %d\n", \ + #v1 " != " #v2, __v2, __v1); \ + exit(1); \ + } \ + } while (0) + void test_hex_to_bin(const char *hex, uint8_t *bin, size_t *bin_len); const char *test_bin_to_hex(const uint8_t *bin, size_t len); diff --git a/tests/test_send_queue.c b/tests/test_send_queue.c new file mode 100644 index 0000000..af10b86 --- /dev/null +++ b/tests/test_send_queue.c @@ -0,0 +1,86 @@ +/* test_send_queue.c +** libstrophe XMPP client library -- test routines for the send queue +** +** Copyright (C) 2021 Steffen Jaeckel +** +** This software is provided AS-IS with no warranty, either express +** or implied. +** +** This program is dual licensed under the MIT and GPLv3 licenses. +*/ + +#include +#include +#include + +#include "strophe.h" +#include "common.h" + +#include "test.h" + +int main() +{ + xmpp_ctx_t *ctx; + xmpp_conn_t *conn; + xmpp_log_t *log; + xmpp_conn_state_t state; + char *ret; + + unsigned int n; + + xmpp_initialize(); + log = xmpp_get_default_logger(XMPP_LEVEL_DEBUG); + ctx = xmpp_ctx_new(NULL, log); + conn = xmpp_conn_new(ctx); + + ENSURE_EQ(xmpp_conn_send_queue_len(conn), 0); + + state = conn->state; + conn->state = XMPP_STATE_CONNECTED; + + xmpp_send_raw(conn, "foo", 3); + ENSURE_EQ(xmpp_conn_send_queue_len(conn), 1); + + xmpp_send_raw(conn, "bar", 3); + ENSURE_EQ(xmpp_conn_send_queue_len(conn), 2); + + xmpp_send_raw(conn, "baz", 3); + ENSURE_EQ(xmpp_conn_send_queue_len(conn), 3); + + xmpp_send_raw(conn, "baan", 4); + ENSURE_EQ(xmpp_conn_send_queue_len(conn), 4); + + conn->send_queue_head->written = 1; + ENSURE_EQ(xmpp_conn_send_queue_len(conn), 3); + + ret = xmpp_conn_send_queue_drop_element(conn, XMPP_QUEUE_OLDEST); + COMPARE("bar", ret); + xmpp_free(ctx, ret); + ENSURE_EQ(xmpp_conn_send_queue_len(conn), 2); + + conn->send_queue_head->written = 0; + ENSURE_EQ(xmpp_conn_send_queue_len(conn), 3); + + ret = xmpp_conn_send_queue_drop_element(conn, XMPP_QUEUE_OLDEST); + COMPARE("foo", ret); + xmpp_free(ctx, ret); + ENSURE_EQ(xmpp_conn_send_queue_len(conn), 2); + + ret = xmpp_conn_send_queue_drop_element(conn, XMPP_QUEUE_YOUNGEST); + COMPARE("baan", ret); + xmpp_free(ctx, ret); + ENSURE_EQ(xmpp_conn_send_queue_len(conn), 1); + + ret = xmpp_conn_send_queue_drop_element(conn, XMPP_QUEUE_YOUNGEST); + COMPARE("baz", ret); + xmpp_free(ctx, ret); + ENSURE_EQ(xmpp_conn_send_queue_len(conn), 0); + + conn->state = state; + + xmpp_conn_release(conn); + xmpp_ctx_free(ctx); + xmpp_shutdown(); + + return 0; +}