From 63deb7770557c9f88b30c03801ce6c6c1f6593f4 Mon Sep 17 00:00:00 2001 From: "Todd C. Miller" Date: Sun, 15 Jan 2017 19:07:59 -0700 Subject: [PATCH] Add regress for vsyslog replacement. --- MANIFEST | 1 + configure | 1 + configure.ac | 1 + lib/util/Makefile.in | 13 ++- lib/util/regress/vsyslog/vsyslog_test.c | 133 ++++++++++++++++++++++++ 5 files changed, 148 insertions(+), 1 deletion(-) create mode 100644 lib/util/regress/vsyslog/vsyslog_test.c diff --git a/MANIFEST b/MANIFEST index ea8f458b1..057f67b85 100644 --- a/MANIFEST +++ b/MANIFEST @@ -153,6 +153,7 @@ lib/util/regress/sudo_parseln/test5.out.ok lib/util/regress/sudo_parseln/test6.in lib/util/regress/sudo_parseln/test6.out.ok lib/util/regress/tailq/hltq_test.c +lib/util/regress/vsyslog/vsyslog_test.c lib/util/secure_path.c lib/util/setgroups.c lib/util/sha2.c diff --git a/configure b/configure index 9461e557e..fbc0594ab 100755 --- a/configure +++ b/configure @@ -20551,6 +20551,7 @@ esac " done + COMPAT_TEST_PROGS="${COMPAT_TEST_PROGS}${COMPAT_TEST_PROGS+ }vsyslog_test" fi done diff --git a/configure.ac b/configure.ac index 99e8b6502..82dd6b557 100644 --- a/configure.ac +++ b/configure.ac @@ -2690,6 +2690,7 @@ fi AC_CHECK_FUNCS([vsyslog], [], [ AC_LIBOBJ(vsyslog) SUDO_APPEND_COMPAT_EXP(sudo_vsyslog) + COMPAT_TEST_PROGS="${COMPAT_TEST_PROGS}${COMPAT_TEST_PROGS+ }vsyslog_test" ]) dnl dnl Function checks for sudo_noexec diff --git a/lib/util/Makefile.in b/lib/util/Makefile.in index 3eecc2410..425946bb3 100644 --- a/lib/util/Makefile.in +++ b/lib/util/Makefile.in @@ -1,5 +1,5 @@ # -# Copyright (c) 2011-2015 Todd C. Miller +# Copyright (c) 2011-2017 Todd C. Miller # # Permission to use, copy, modify, and distribute this software for any # purpose with or without fee is hereby granted, provided that the above @@ -134,6 +134,8 @@ STRSPLIT_TEST_OBJS = strsplit_test.lo PARSE_GIDS_TEST_OBJS = parse_gids_test.lo +VSYSLOG_TEST_OBJS = vsyslog_test.lo + all: libsudo_util.la Makefile: $(srcdir)/Makefile.in @@ -210,6 +212,9 @@ parse_gids_test: $(PARSE_GIDS_TEST_OBJS) libsudo_util.la strsplit_test: $(STRSPLIT_TEST_OBJS) libsudo_util.la $(LIBTOOL) $(LTFLAGS) --mode=link $(CC) -o $@ $(STRSPLIT_TEST_OBJS) libsudo_util.la $(ASAN_LDFLAGS) $(PIE_LDFLAGS) $(SSP_LDFLAGS) $(TEST_LDFLAGS) $(TEST_LIBS) +vsyslog_test: $(VSYSLOG_TEST_OBJS) libsudo_util.la + $(LIBTOOL) $(LTFLAGS) --mode=link $(CC) -o $@ $(VSYSLOG_TEST_OBJS) libsudo_util.la $(ASAN_LDFLAGS) $(PIE_LDFLAGS) $(SSP_LDFLAGS) $(TEST_LDFLAGS) $(TEST_LIBS) + pre-install: install: install-dirs @@ -273,6 +278,7 @@ check: $(TEST_PROGS) rm -f ./progname_test2; ln -s ./progname_test ./progname_test2; \ ./progname_test2 || rval=`expr $$rval + $$?`; \ rm -f ./progname_test2; \ + ./vsyslog_test || rval=`expr $$rval + $$?`; \ build_dir=`pwd`; \ cd $(srcdir); \ for dir in sudo_conf sudo_parseln; do \ @@ -587,3 +593,8 @@ utimens.lo: $(srcdir)/utimens.c $(incdir)/compat/stdbool.h \ $(LIBTOOL) $(LTFLAGS) --mode=compile $(CC) -c -o $@ $(CPPFLAGS) $(CFLAGS) $(ASAN_CFLAGS) $(PIE_CFLAGS) $(SSP_CFLAGS) $(srcdir)/utimens.c vsyslog.lo: $(srcdir)/vsyslog.c $(incdir)/sudo_compat.h $(top_builddir)/config.h $(LIBTOOL) $(LTFLAGS) --mode=compile $(CC) -c -o $@ $(CPPFLAGS) $(CFLAGS) $(ASAN_CFLAGS) $(PIE_CFLAGS) $(SSP_CFLAGS) $(srcdir)/vsyslog.c +vsyslog_test.lo: $(srcdir)/regress/vsyslog/vsyslog_test.c \ + $(incdir)/compat/stdbool.h $(incdir)/sudo_compat.h \ + $(incdir)/sudo_fatal.h $(incdir)/sudo_util.h \ + $(top_builddir)/config.h + $(LIBTOOL) $(LTFLAGS) --mode=compile $(CC) -c -o $@ $(CPPFLAGS) $(CFLAGS) $(ASAN_CFLAGS) $(PIE_CFLAGS) $(SSP_CFLAGS) $(srcdir)/regress/vsyslog/vsyslog_test.c diff --git a/lib/util/regress/vsyslog/vsyslog_test.c b/lib/util/regress/vsyslog/vsyslog_test.c new file mode 100644 index 000000000..a25e072c2 --- /dev/null +++ b/lib/util/regress/vsyslog/vsyslog_test.c @@ -0,0 +1,133 @@ +/* + * Copyright (c) 2017 Todd C. Miller + * + * Permission to use, copy, modify, and distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice appear in all copies. + * + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + */ + +#include + +#include +#include +#include +#include +#ifdef HAVE_STRING_H +# include +#endif /* HAVE_STRING_H */ +#ifdef HAVE_STRINGS_H +# include +#endif /* HAVE_STRINGS_H */ +#ifdef HAVE_STDBOOL_H +# include +#else +# include "compat/stdbool.h" +#endif +#include + +#include "sudo_compat.h" +#include "sudo_fatal.h" +#include "sudo_util.h" + +__dso_public int main(int argc, char *argv[]); + +/* + * Test that sudo_vsyslog() works as expected. + */ +static char *expected_result; +static int errors; + +/* + * Dummy version of syslog to verify the message + */ +void +syslog(int priority, const char *fmt, ...) +{ + va_list ap; + const char *msg; + + if (strcmp(fmt, "%s") != 0) + sudo_fatalx_nodebug("Expected syslog format \"%%s\", got \"%s\"", fmt); + + va_start(ap, fmt); + msg = va_arg(ap, char *); + if (strcmp(msg, expected_result) != 0) { + sudo_warnx_nodebug("Expected \"%s\", got \"%s\"", expected_result, msg); + errors++; + } + va_end(ap); +} + +static void +test_vsyslog(int priority, const char *fmt, ...) +{ + va_list ap; + + va_start(ap, fmt); + sudo_vsyslog(priority, fmt, ap); + va_end(ap); +} + +int +main(int argc, char *argv[]) +{ + int ntests = 0; + char buf1[1024 * 16], buf2[1024 * 16]; + initprogname(argc > 0 ? argv[0] : "vsyslog_test"); + + /* Test small buffer. */ + expected_result = "sudo: millert : TTY=ttypa ; PWD=/etc/mail ; USER=root ; TSID=000AB0 ; COMMAND=/usr/sbin/newaliases"; + test_vsyslog(0, + "%s: %s : TTY=%s ; PWD=%s ; USER=%s ; TSID=%s ; COMMAND=%s", + "sudo", "millert", "ttypa", "/etc/mail", "root", "000AB0", + "/usr/sbin/newaliases"); + ntests++; + + /* Test small buffer w/ errno. */ + snprintf(buf1, sizeof(buf1), + "unable to open %s: %s", "/var/log/sudo-io/seq", strerror(ENOENT)); + expected_result = buf1; + errno = ENOENT; + test_vsyslog(0, "unable to open %s: %m", "/var/log/sudo-io/seq"); + ntests++; + + /* Test large buffer > 8192 bytes. */ + memset(buf1, 'a', 8192); + buf1[8192] = '\0'; + expected_result = buf1; + test_vsyslog(0, "%s", buf1); + ntests++; + + /* Test large buffer w/ errno > 8192 bytes. */ + memset(buf1, 'b', 8184); + buf1[8184] = '\0'; + snprintf(buf2, sizeof(buf2), "%s: %s", buf1, strerror(EINVAL)); + expected_result = buf2; + errno = EINVAL; + test_vsyslog(0, "%s: %m", buf1); + ntests++; + + /* Test large buffer w/ errno > 8192 bytes. */ + memset(buf1, 'b', 8184); + buf1[8184] = '\0'; + snprintf(buf2, sizeof(buf2), "%s: %s", buf1, strerror(EINVAL)); + expected_result = buf2; + errno = EINVAL; + strlcat(buf1, ": %m", sizeof(buf1)); + test_vsyslog(0, buf1); + ntests++; + + if (ntests != 0) { + printf("%s: %d tests run, %d errors, %d%% success rate\n", + getprogname(), ntests, errors, (ntests - errors) * 100 / ntests); + } + exit(errors); +} -- 2.40.0