From f1871a7d9fea10f0460a1457d45c2999b7390806 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dag-Erling=20Sm=C3=B8rgrav?= Date: Thu, 21 Oct 2021 15:47:06 +0200 Subject: [PATCH] Add unit tests for openpam_straddch(3). --- t/.gitignore | 7 +- t/Makefile.am | 1 + t/t_openpam_straddch.c | 184 +++++++++++++++++++++++++++++++++++++++++ 3 files changed, 187 insertions(+), 5 deletions(-) create mode 100644 t/t_openpam_straddch.c diff --git a/t/.gitignore b/t/.gitignore index 79f308f..052fc9e 100644 --- a/t/.gitignore +++ b/t/.gitignore @@ -1,10 +1,7 @@ +/t_*.trs /t_openpam_ctype -/t_openpam_ctype.trs /t_openpam_dispatch -/t_openpam_dispatch.trs /t_openpam_readlinev -/t_openpam_readlinev.trs /t_openpam_readword -/t_openpam_readword.trs +/t_openpam_straddch /t_pam_env -/t_pam_env.trs diff --git a/t/Makefile.am b/t/Makefile.am index 9b0bcdf..93133a2 100644 --- a/t/Makefile.am +++ b/t/Makefile.am @@ -12,6 +12,7 @@ TESTS += t_openpam_ctype TESTS += t_openpam_dispatch TESTS += t_openpam_readword TESTS += t_openpam_readlinev +TESTS += t_openpam_straddch TESTS += t_pam_env check_PROGRAMS = $(TESTS) diff --git a/t/t_openpam_straddch.c b/t/t_openpam_straddch.c new file mode 100644 index 0000000..03cdba7 --- /dev/null +++ b/t/t_openpam_straddch.c @@ -0,0 +1,184 @@ +/*- + * Copyright (c) 2021 Dag-Erling Smørgrav + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. The name of the author may not be used to endorse or promote + * products derived from this software without specific prior written + * permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +#ifdef HAVE_CONFIG_H +# include "config.h" +#endif + +#include + +#include +#include +#include + +#include + +#include +#include "openpam_impl.h" + +static int +t_straddch_empty(char **desc CRYB_UNUSED, void *arg CRYB_UNUSED) +{ + char *str; + size_t size, len; + int ret; + + str = NULL; + size = len = SIZE_MAX; + ret = t_is_zero_i(openpam_straddch(&str, &size, &len, '\0')); + ret &= t_is_not_null(str); + ret &= t_is_not_zero_sz(size); + ret &= t_is_zero_sz(len); + free(str); + return ret; +} + +static int +t_straddch_alloc_fail(char **desc CRYB_UNUSED, void *arg CRYB_UNUSED) +{ + char *str; + size_t size, len; + int ret; + + str = NULL; + size = len = SIZE_MAX; + errno = 0; + t_malloc_fail = 1; + ret = t_compare_i(-1, openpam_straddch(&str, &size, &len, '\0')); + t_malloc_fail = 0; + ret &= t_compare_i(ENOMEM, errno); + ret &= t_is_null(str); + ret &= t_compare_sz(SIZE_MAX, size); + ret &= t_compare_sz(SIZE_MAX, len); + free(str); + return ret; +} + +static int +t_straddch_realloc_fail(char **desc CRYB_UNUSED, void *arg CRYB_UNUSED) +{ + char *str, *_str; + size_t size, _size, len, _len; + int i, ret; + + // start with an empty string + str = NULL; + size = len = SIZE_MAX; + ret = t_is_zero_i(openpam_straddch(&str, &size, &len, '\0')); + ret &= t_is_not_null(str); + ret &= t_is_not_zero_sz(size); + ret &= t_is_zero_sz(len); + if (!ret) + goto end; + // repeatedly append to it until allocation fails + errno = 0; + _str = str; + _size = size; + _len = len; + t_malloc_fail = 1; + for (i = 0; i < 4096; i++) { + if ((ret = openpam_straddch(&str, &size, &len, 'x')) != 0) + break; + _size = size; + _len = len; + } + t_malloc_fail = 0; + ret = t_compare_i(-1, ret); + ret &= t_compare_i(ENOMEM, errno); + ret &= t_compare_ptr(_str, str); + ret &= t_compare_sz(_size, size); + ret &= t_compare_sz(_len, len); +end: + free(str); + return ret; +} + +static int +t_straddch_realloc_ok(char **desc CRYB_UNUSED, void *arg CRYB_UNUSED) +{ + char *str, *_str; + size_t size, _size, len, _len; + int i, ret; + + // start with an empty string + str = NULL; + size = len = SIZE_MAX; + ret = t_is_zero_i(openpam_straddch(&str, &size, &len, '\0')); + ret &= t_is_not_null(str); + ret &= t_is_not_zero_sz(size); + ret &= t_is_zero_sz(len); + if (!ret) + goto end; + // repeatedly append to it until size changes + _str = str; + _size = size; + _len = len; + for (i = ' '; i <= '~'; i++) { // assume ascii + if ((ret = openpam_straddch(&str, &size, &len, i)) != 0) + break; + if (size != _size) + break; + if (len != _len + 1) + break; + _len = len; + } + ret = t_is_zero_i(ret); + if (!ret) + goto end; + ret &= t_compare_sz(_len + 1, len); + ret &= t_compare_sz(_size * 2, size); + ret &= t_compare_i(i, str[_len]); + ret &= t_is_zero_i(str[len]); +end: + free(str); + return ret; +} + + +/*************************************************************************** + * Boilerplate + */ + +static int +t_prepare(int argc CRYB_UNUSED, char *argv[] CRYB_UNUSED) +{ + + t_add_test(t_straddch_empty, NULL, "empty string"); + t_add_test(t_straddch_alloc_fail, NULL, "allocation failure"); + t_add_test(t_straddch_realloc_fail, NULL, "reallocation failure"); + t_add_test(t_straddch_realloc_ok, NULL, "reallocation success"); + return (0); +} + +int +main(int argc, char *argv[]) +{ + + t_main(t_prepare, NULL, argc, argv); +}