From 0e6b8d0af9ff588526889de1bf944a1c2c11b69a Mon Sep 17 00:00:00 2001 From: Simon Arlott Date: Sun, 30 Jul 2017 17:05:26 +0100 Subject: [PATCH] tests: add rb_snprintf_append, rb_snprintf_try_append tests --- .gitignore | 4 +- tests/Makefile.am | 4 + tests/TESTS | 2 + tests/rb_snprintf_append1.c | 184 ++++++++++++++++++++++++++++++++ tests/rb_snprintf_try_append1.c | 184 ++++++++++++++++++++++++++++++++ 5 files changed, 377 insertions(+), 1 deletion(-) create mode 100644 tests/rb_snprintf_append1.c create mode 100644 tests/rb_snprintf_try_append1.c diff --git a/.gitignore b/.gitignore index 9cec2f06..d1d39a8f 100644 --- a/.gitignore +++ b/.gitignore @@ -58,7 +58,9 @@ ssld/ssld wsockd/wsockd tests/msgbuf_parse1 tests/msgbuf_unparse1 -tests/rb_linebuf_put +tests/rb_linebuf_put1 +tests/rb_snprintf_append1 +tests/rb_snprintf_try_append1 tests/substitution1 tests/runtests testsuite/ircd.pid.* diff --git a/tests/Makefile.am b/tests/Makefile.am index e5a3785b..c57b876a 100644 --- a/tests/Makefile.am +++ b/tests/Makefile.am @@ -2,6 +2,8 @@ check_PROGRAMS = runtests \ msgbuf_parse1 \ msgbuf_unparse1 \ rb_linebuf_put1 \ + rb_snprintf_append1 \ + rb_snprintf_try_append1 \ substitution1 AM_CFLAGS=$(WARNFLAGS) AM_CPPFLAGS = $(DEFAULT_INCLUDES) -I../librb/include -I.. @@ -20,6 +22,8 @@ tap_libtap_a_SOURCES = tap/basic.c tap/basic.h \ msgbuf_parse1_SOURCES = msgbuf_parse1.c msgbuf_unparse1_SOURCES = msgbuf_unparse1.c rb_linebuf_put1_SOURCES = rb_linebuf_put1.c +rb_snprintf_append1_SOURCES = rb_snprintf_append1.c +rb_snprintf_try_append1_SOURCES = rb_snprintf_try_append1.c substitution1_SOURCES = substitution1.c check-local: $(check_PROGRAMS) diff --git a/tests/TESTS b/tests/TESTS index defae925..e5849bef 100644 --- a/tests/TESTS +++ b/tests/TESTS @@ -1,4 +1,6 @@ msgbuf_parse1 msgbuf_unparse1 rb_linebuf_put1 +rb_snprintf_append1 +rb_snprintf_try_append1 substitution1 diff --git a/tests/rb_snprintf_append1.c b/tests/rb_snprintf_append1.c new file mode 100644 index 00000000..2ecd29bf --- /dev/null +++ b/tests/rb_snprintf_append1.c @@ -0,0 +1,184 @@ +/* + * rb_snprintf_append1.c: Test rb_snprintf_append under various conditions + * Copyright 2017 Simon Arlott + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 + * USA + */ +#include +#include +#include +#include +#include "tap/basic.h" + +#include "stdinc.h" +#include "ircd_defs.h" +#include "client.h" +#include "rb_lib.h" + +#define MSG "%s:%d (%s)", __FILE__, __LINE__, __FUNCTION__ + +struct Client me; + +static void from_empty1(void) +{ + char output[2048] = { 0 }; + int len; + + len = rb_snprintf_append(output, sizeof(output), "%s", "test"); + is_int(4, len, MSG); + is_string("test", output, MSG); +} + +static void basic_append1(void) +{ + char output[2048] = { 0 }; + int len; + + strcat(output, "test"); + + len = rb_snprintf_append(output, sizeof(output), "%s", "TESTING"); + is_int(11, len, MSG); + is_string("testTESTING", output, MSG); +} + +static void append_empty1(void) +{ + char output[2048] = { 0 }; + int len; + + strcat(output, "test"); + + len = rb_snprintf_append(output, sizeof(output), "%s", ""); + is_int(4, len, MSG); + is_string("test", output, MSG); +} + +static void exact_fit_empty1(void) +{ + char output[5] = { 0 }; + int len; + + strcat(output, "test"); + + len = rb_snprintf_append(output, sizeof(output), "%s", ""); + is_int(4, len, MSG); + is_string("test", output, MSG); +} + +static void exact_fit_basic_append1(void) +{ + char output[12] = { 0 }; + int len; + + strcat(output, "test"); + + len = rb_snprintf_append(output, sizeof(output), "%s", "TESTING"); + is_int(11, len, MSG); + is_string("testTESTING", output, MSG); +} + +static void too_long_basic_append1(void) +{ + char output[11] = { 0 }; + int len; + + strcat(output, "test"); + + len = rb_snprintf_append(output, sizeof(output), "%s", "TESTING"); + is_int(11, len, MSG); + is_string("testTESTIN", output, MSG); +} + +static void exact_fit_from_empty1(void) +{ + char output[5] = { 0 }; + int len; + + len = rb_snprintf_append(output, sizeof(output), "%s", "test"); + is_int(4, len, MSG); + is_string("test", output, MSG); +} + +static void too_long_from_empty1(void) +{ + char output[4] = { 0 }; + int len; + + len = rb_snprintf_append(output, sizeof(output), "%s", "test"); + is_int(4, len, MSG); + is_string("tes", output, MSG); +} + +static void truncate_existing1(void) +{ + char output[2048] = { 0 }; + int len; + + strcat(output, "testing"); + + len = rb_snprintf_append(output, 5, "%s", ""); + is_int(4, len, MSG); + is_string("test", output, MSG); +} + +static void truncate_existing2(void) +{ + char output[2048] = { 0 }; + int len; + + strcat(output, "testing"); + + len = rb_snprintf_append(output, 5, "%s", "TEST"); + is_int(4, len, MSG); + is_string("test", output, MSG); +} + +static void no_buffer(void) +{ + int len; + + len = rb_snprintf_append(NULL, 0, "%s", "test"); + is_int(-1, len, MSG); +} + +int main(int argc, char *argv[]) +{ + memset(&me, 0, sizeof(me)); + strcpy(me.name, "me.name."); + + rb_lib_init(NULL, NULL, NULL, 0, 1024, DNODE_HEAP_SIZE, FD_HEAP_SIZE); + rb_linebuf_init(LINEBUF_HEAP_SIZE); + + plan_lazy(); + + from_empty1(); + basic_append1(); + append_empty1(); + + exact_fit_from_empty1(); + too_long_from_empty1(); + + exact_fit_basic_append1(); + too_long_basic_append1(); + + exact_fit_empty1(); + + truncate_existing1(); + truncate_existing2(); + no_buffer(); + + return 0; +} diff --git a/tests/rb_snprintf_try_append1.c b/tests/rb_snprintf_try_append1.c new file mode 100644 index 00000000..52647e68 --- /dev/null +++ b/tests/rb_snprintf_try_append1.c @@ -0,0 +1,184 @@ +/* + * rb_snprintf_try_append1.c: Test rb_snprintf_try_append under various conditions + * Copyright 2017 Simon Arlott + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 + * USA + */ +#include +#include +#include +#include +#include "tap/basic.h" + +#include "stdinc.h" +#include "ircd_defs.h" +#include "client.h" +#include "rb_lib.h" + +#define MSG "%s:%d (%s)", __FILE__, __LINE__, __FUNCTION__ + +struct Client me; + +static void from_empty1(void) +{ + char output[2048] = { 0 }; + int len; + + len = rb_snprintf_try_append(output, sizeof(output), "%s", "test"); + is_int(4, len, MSG); + is_string("test", output, MSG); +} + +static void basic_append1(void) +{ + char output[2048] = { 0 }; + int len; + + strcat(output, "test"); + + len = rb_snprintf_try_append(output, sizeof(output), "%s", "TESTING"); + is_int(11, len, MSG); + is_string("testTESTING", output, MSG); +} + +static void append_empty1(void) +{ + char output[2048] = { 0 }; + int len; + + strcat(output, "test"); + + len = rb_snprintf_try_append(output, sizeof(output), "%s", ""); + is_int(4, len, MSG); + is_string("test", output, MSG); +} + +static void exact_fit_empty1(void) +{ + char output[5] = { 0 }; + int len; + + strcat(output, "test"); + + len = rb_snprintf_try_append(output, sizeof(output), "%s", ""); + is_int(4, len, MSG); + is_string("test", output, MSG); +} + +static void exact_fit_basic_append1(void) +{ + char output[12] = { 0 }; + int len; + + strcat(output, "test"); + + len = rb_snprintf_try_append(output, sizeof(output), "%s", "TESTING"); + is_int(11, len, MSG); + is_string("testTESTING", output, MSG); +} + +static void too_long_basic_append1(void) +{ + char output[11] = { 0 }; + int len; + + strcat(output, "test"); + + len = rb_snprintf_try_append(output, sizeof(output), "%s", "TESTING"); + is_int(-1, len, MSG); + is_string("test", output, MSG); +} + +static void exact_fit_from_empty1(void) +{ + char output[5] = { 0 }; + int len; + + len = rb_snprintf_try_append(output, sizeof(output), "%s", "test"); + is_int(4, len, MSG); + is_string("test", output, MSG); +} + +static void too_long_from_empty1(void) +{ + char output[4] = { 0 }; + int len; + + len = rb_snprintf_try_append(output, sizeof(output), "%s", "test"); + is_int(-1, len, MSG); + is_string("", output, MSG); +} + +static void truncate_existing1(void) +{ + char output[2048] = { 0 }; + int len; + + strcat(output, "testing"); + + len = rb_snprintf_try_append(output, 5, "%s", ""); + is_int(-1, len, MSG); + is_string("test", output, MSG); +} + +static void truncate_existing2(void) +{ + char output[2048] = { 0 }; + int len; + + strcat(output, "testing"); + + len = rb_snprintf_try_append(output, 5, "%s", "TEST"); + is_int(-1, len, MSG); + is_string("test", output, MSG); +} + +static void no_buffer(void) +{ + int len; + + len = rb_snprintf_try_append(NULL, 0, "%s", "test"); + is_int(-1, len, MSG); +} + +int main(int argc, char *argv[]) +{ + memset(&me, 0, sizeof(me)); + strcpy(me.name, "me.name."); + + rb_lib_init(NULL, NULL, NULL, 0, 1024, DNODE_HEAP_SIZE, FD_HEAP_SIZE); + rb_linebuf_init(LINEBUF_HEAP_SIZE); + + plan_lazy(); + + from_empty1(); + basic_append1(); + append_empty1(); + + exact_fit_from_empty1(); + too_long_from_empty1(); + + exact_fit_basic_append1(); + too_long_basic_append1(); + + exact_fit_empty1(); + + truncate_existing1(); + truncate_existing2(); + no_buffer(); + + return 0; +}