From 4e53184eee563c13ddf13784abd51bd5ba22693c Mon Sep 17 00:00:00 2001 From: Dmitry Podgorny Date: Sat, 17 Jul 2021 01:23:16 +0300 Subject: [PATCH] tests/stanza: add test for xmpp_stanza_reply_error() --- Makefile.am | 4 ++-- tests/test.h | 1 + tests/test_stanza.c | 54 ++++++++++++++++++++++++++++++++++++++++++++- 3 files changed, 56 insertions(+), 3 deletions(-) diff --git a/Makefile.am b/Makefile.am index 71abf84..8c9d039 100644 --- a/Makefile.am +++ b/Makefile.am @@ -238,8 +238,8 @@ 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_SOURCES = tests/test_stanza.c tests/test.h +tests_test_stanza_CFLAGS = $(STROPHE_FLAGS) -I$(top_srcdir)/src tests_test_stanza_LDADD = $(STROPHE_LIBS) tests_test_stanza_LDFLAGS = -static diff --git a/tests/test.h b/tests/test.h index a0ad636..6166d5b 100644 --- a/tests/test.h +++ b/tests/test.h @@ -14,6 +14,7 @@ #include #include +#include #include #include "ostypes.h" diff --git a/tests/test_stanza.c b/tests/test_stanza.c index 14cf8ab..8643f2e 100644 --- a/tests/test_stanza.c +++ b/tests/test_stanza.c @@ -17,6 +17,8 @@ #include #include +#include "test.h" + #define MAGICPTR ((void *)0xfeedbeef) static unsigned long used_blocks = 0; @@ -107,7 +109,7 @@ static void test_stanza_from_string(xmpp_ctx_t *ctx) assert(stanza != NULL); ret = xmpp_stanza_to_text(stanza, &buf, &buflen); assert(ret == XMPP_EOK); - assert(strcmp(buf, str) == 0); + COMPARE(str, buf); xmpp_free(ctx, buf); xmpp_stanza_release(stanza); @@ -116,6 +118,55 @@ static void test_stanza_from_string(xmpp_ctx_t *ctx) assert(stanza == NULL); } +static void test_stanza_error(xmpp_ctx_t *ctx) +{ + xmpp_stanza_t *stanza; + xmpp_stanza_t *error; + xmpp_stanza_t *item; + char *buf; + size_t buflen; + const char *attr[10]; + int attrlen = ARRAY_SIZE(attr); + int ret; + + static const char *str = + ""; + static const char *str_error = + ""; + + stanza = xmpp_stanza_new_from_string(ctx, str); + assert(stanza != NULL); + error = + xmpp_stanza_reply_error(stanza, "cancel", "service-unavailable", NULL); + assert(error != NULL); + + assert(xmpp_stanza_get_to(error) != NULL); + COMPARE("romeo@montague.lit/home", xmpp_stanza_get_to(error)); + assert(xmpp_stanza_get_from(error) != NULL); + COMPARE("juliet@capulet.lit/chamber", xmpp_stanza_get_from(error)); + assert(xmpp_stanza_get_id(error) != NULL); + COMPARE("e2e1", xmpp_stanza_get_id(error)); + assert(xmpp_stanza_get_type(error) != NULL); + COMPARE("error", xmpp_stanza_get_type(error)); + + ret = xmpp_stanza_get_attributes(error, attr, attrlen); + /* attr contains both attribute name and value. */ + assert(ret == 8); + + item = xmpp_stanza_get_child_by_name(error, "error"); + assert(item != NULL); + + ret = xmpp_stanza_to_text(item, &buf, &buflen); + assert(ret == XMPP_EOK); + COMPARE(str_error, buf); + + xmpp_free(ctx, buf); + xmpp_stanza_release(stanza); + xmpp_stanza_release(error); +} + int main() { xmpp_ctx_t *ctx; @@ -126,6 +177,7 @@ int main() test_stanza_add_child(ctx); test_stanza_from_string(ctx); + test_stanza_error(ctx); xmpp_ctx_free(ctx); xmpp_shutdown();