From f867a12aa9cecccadbe99933e50538e220bff29d Mon Sep 17 00:00:00 2001 From: Nicolas Williams Date: Fri, 31 Mar 2017 16:55:26 -0500 Subject: [PATCH] Test rk_get*auxval() --- lib/roken/Makefile.am | 5 +- lib/roken/test-auxval.c | 118 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 122 insertions(+), 1 deletion(-) create mode 100644 lib/roken/test-auxval.c diff --git a/lib/roken/Makefile.am b/lib/roken/Makefile.am index 1a1593949..a717da16b 100644 --- a/lib/roken/Makefile.am +++ b/lib/roken/Makefile.am @@ -20,7 +20,7 @@ if HAVE_DBHEADER AM_CPPFLAGS += -I$(DBHEADER) endif -noinst_PROGRAMS = snprintf-test resolve-test rkpty test-detach +noinst_PROGRAMS = snprintf-test resolve-test rkpty test-detach test-auxval CHECK_LOCAL = snprintf-test resolve-test rkpty make-roken @@ -30,6 +30,7 @@ check_PROGRAMS = \ getifaddrs-test \ getxxyyy-test \ hex-test \ + test-auxval \ test-readenv \ resolve-test \ parse_bytes-test \ @@ -52,6 +53,7 @@ parse_reply_test_SOURCES = parse_reply-test.c resolve.c parse_reply_test_CFLAGS = -DTEST_RESOLVE test_readenv_SOURCES = test-readenv.c test-mem.c +test_auxval_SOURCES = test-auxval.c test_detach_SOURCES = test-detach.c @@ -261,6 +263,7 @@ EXTRA_DIST = \ stdint.hin \ syslogc.c \ syslog.hin \ + test-auxval.c \ test-mem.h \ test-mini_inetd.c \ win32_alloc.c \ diff --git a/lib/roken/test-auxval.c b/lib/roken/test-auxval.c new file mode 100644 index 000000000..4a8b74231 --- /dev/null +++ b/lib/roken/test-auxval.c @@ -0,0 +1,118 @@ +/* + * Copyright (c) 1999 - 2004 Kungliga Tekniska Högskolan + * (Royal Institute of Technology, Stockholm, Sweden). + * 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. Neither the name of the Institute nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE 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 INSTITUTE 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. + */ + +#include + +#include +#include +#include +#include +#include +#include +#include +#include "roken.h" + +int +main() +{ + unsigned long max_t = 0; + unsigned long a[2]; + unsigned long v; + ssize_t bytes; + int fd; + + if ((fd = open("/proc/self/auxv", O_RDONLY)) == -1) + return 0; + + do { + bytes = read(fd, a, sizeof(a)); + if (bytes != sizeof(a)) { + if (bytes == -1) + err(1, "Error reading from /proc/self/auxv"); + if (bytes == 0) + warnx("Did not see terminator in /proc/self/auxv"); + else + warnx("Partial entry in /proc/self/auxv or test interrupted"); + (void) close(fd); + return 1; + } + if (a[0] > max_t) + max_t = a[0]; + if (a[0] == 0) { + if (a[1] != 0) + warnx("AT_NULL with non-zero value %lu?!", a[1]); + continue; + } + + errno = EACCES; + + if ((v = rk_getauxval(a[0])) != a[1]) + errx(1, "rk_getauxval(%lu) should have been %lu, was %lu", + a[0], a[1], v); + if (errno != EACCES) + errx(1, "rk_getauxval(%lu) did not preserve errno", a[0]); + + if ((v = rk_getprocauxval(a[0])) != a[1]) + errx(1, "rk_getauxval(%lu) should have been %lu, was %lu", + a[0], a[1], v); + if (errno != EACCES) + errx(1, "rk_getprocauxval(%lu) did not preserve errno", a[0]); + + printf("auxv type %lu -> %lu\n", a[0], a[1]); + } while (a[0] != 0 || a[1] != 0); + + (void) close(fd); + if (max_t == 0) { + warnx("No entries in /proc/self/auxv or it is not available on this " + "system or this program is linked statically; cannot test " + "rk_getauxval()"); + return 0; + } + + errno = EACCES; + if ((v = rk_getauxval(max_t + 1)) != 0) + errx(1, "rk_getauxval((max_type_seen = %lu) + 1) should have been " + "0, was %lu", max_t, v); + if (errno != ENOENT) + errx(1, "rk_getauxval((max_type_seen = %lu) + 1) did not set " + "errno = ENOENT!", max_t); + + errno = EACCES; + if ((v = rk_getprocauxval(max_t + 1)) != 0) + errx(1, "rk_getprocauxval((max_type_seen = %lu) + 1) should have been " + "0, was %lu", max_t, v); + if (errno != ENOENT) + errx(1, "rk_getprocauxval((max_type_seen = %lu) + 1) did not set " + "errno = ENOENT!", max_t); + return 0; +}