From c80f99bc68520697421ff6dae3e90b8e3b39abcc Mon Sep 17 00:00:00 2001 From: Dmitry Podgorny Date: Sun, 4 Sep 2016 00:52:37 +0300 Subject: [PATCH] tests: add tests for string functions --- .gitignore | 1 + Makefile.am | 7 ++- tests/test_string.c | 116 ++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 123 insertions(+), 1 deletion(-) create mode 100644 tests/test_string.c diff --git a/.gitignore b/.gitignore index 5ad3569..960cd92 100644 --- a/.gitignore +++ b/.gitignore @@ -53,6 +53,7 @@ tests/test_sasl tests/test_scram tests/test_sha1 tests/test_snprintf +tests/test_string tests/test_sock m4/ libstrophe.project diff --git a/Makefile.am b/Makefile.am index 49ab604..b6753b8 100644 --- a/Makefile.am +++ b/Makefile.am @@ -77,7 +77,7 @@ examples_uuid_LDADD = $(STROPHE_LIBS) ## Tests TESTS = tests/check_parser tests/test_sha1 tests/test_md5 tests/test_rand \ tests/test_scram tests/test_ctx tests/test_base64 tests/test_jid \ - tests/test_snprintf tests/test_resolver + tests/test_snprintf tests/test_string tests/test_resolver check_PROGRAMS = $(TESTS) tests_check_parser_SOURCES = tests/check_parser.c tests/test.h @@ -120,3 +120,8 @@ tests_test_md5_CFLAGS = -I$(top_srcdir)/src tests_test_snprintf_SOURCES = tests/test_snprintf.c tests_test_snprintf_CFLAGS = -I$(top_srcdir)/src + +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 diff --git a/tests/test_string.c b/tests/test_string.c new file mode 100644 index 0000000..f7993da --- /dev/null +++ b/tests/test_string.c @@ -0,0 +1,116 @@ +/* test_string.c + * strophe XMPP client library -- tests for re-implemented string functions + * + * Copyright (C) 2016 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. + */ + +#include +#include +#include +#include + +#include "strophe.h" +#include "common.h" +#include "util.h" + +#include "test.h" /* ARRAY_SIZE */ + +static int test_strtok_r(void) +{ + const char *test = "-abc-=-def--"; + char *s1, *s2, *sub1, *sub2; + char *sp1, *sp2; + + s1 = strdup(test); + s2 = strdup(test); + assert(strcmp(s1, s2) == 0); + + sub1 = strtok_r(s1, "-", &sp1); + sub2 = xmpp_strtok_r(s2, "-", &sp2); + if (strcmp(sub1, sub2) != 0) { + printf("1st token is '%s', must be '%s'\n", sub2, sub1); + return -1; + } + sub1 = strtok_r(NULL, "-=", &sp1); + sub2 = xmpp_strtok_r(NULL, "-=", &sp2); + if (strcmp(sub1, sub2) != 0) { + printf("2nd token is '%s', must be '%s'\n", sub2, sub1); + return -1; + } + sub1 = strtok_r(NULL, "-", &sp1); + sub2 = xmpp_strtok_r(NULL, "-", &sp2); + if (sub1 != sub2) { + printf("3rd call returns %p instead of NULL\n", sub2); + return -1; + } + + free(s1); + free(s2); + + return 0; +} + +static int test_strdup_one(xmpp_ctx_t *ctx, const char *s) +{ + char *s1, *s2; + int rc; + + s1 = strdup(s); + s2 = xmpp_strdup(ctx, s); + + if (!s1 || !s2 || strcmp(s1, s2) != 0) { + rc = -1; + printf("strdup: '%s', xmpp_strdup: '%s'\n", + s1 ? s1 : "", s2 ? s2 : ""); + } + + free(s1); + if (s2) + xmpp_free(ctx, s2); + + return rc; +} + +static int test_strdup(void) +{ + xmpp_ctx_t *ctx; + int i; + int rc = 0; + + static const char *tests[] = { "", "\0", "test", "s p a c e", "\n\r" }; + + ctx = xmpp_ctx_new(NULL, NULL); + assert(ctx != NULL); + for (i = 0; i < ARRAY_SIZE(tests); ++i) { + rc = test_strdup_one(ctx, tests[i]); + if (rc != 0) + break; + } + xmpp_ctx_free(ctx); + + return rc; +} + +int main() +{ + int rc; + + printf("xmpp_strtok_r() tests... "); + rc = test_strtok_r(); + if (rc != 0) + return 1; + printf("ok\n"); + + printf("xmpp_strdup() tests... "); + rc = test_strdup(); + if (rc != 0) + return 1; + printf("ok\n"); + + return 0; +}