cosmopolitan/libc/testlib/testrunner.c

246 lines
8.4 KiB
C
Raw Normal View History

2020-06-15 14:18:57 +00:00
/*-*- mode:c;indent-tabs-mode:nil;c-basic-offset:2;tab-width:8;coding:utf-8 -*-│
vi: set net ft=c ts=2 sts=2 sw=2 fenc=utf-8 :vi
Copyright 2020 Justine Alexandra Roberts Tunney
2020-12-28 01:18:44 +00:00
Permission to use, copy, modify, and/or 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.
2020-06-15 14:18:57 +00:00
2020-12-28 01:18:44 +00:00
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.
2020-06-15 14:18:57 +00:00
*/
#include "libc/assert.h"
2022-08-11 19:13:18 +00:00
#include "libc/intrin/atomic.h"
#include "libc/intrin/weaken.h"
#include "libc/calls/calls.h"
2022-07-18 09:11:06 +00:00
#include "libc/calls/state.internal.h"
#include "libc/calls/strace.internal.h"
Make numerous improvements - Python static hello world now 1.8mb - Python static fully loaded now 10mb - Python HTTPS client now uses MbedTLS - Python REPL now completes import stmts - Increase stack size for Python for now - Begin synthesizing posixpath and ntpath - Restore Python \N{UNICODE NAME} support - Restore Python NFKD symbol normalization - Add optimized code path for Intel SHA-NI - Get more Python unit tests passing faster - Get Python help() pagination working on NT - Python hashlib now supports MbedTLS PBKDF2 - Make memcpy/memmove/memcmp/bcmp/etc. faster - Add Mersenne Twister and Vigna to LIBC_RAND - Provide privileged __printf() for error code - Fix zipos opendir() so that it reports ENOTDIR - Add basic chmod() implementation for Windows NT - Add Cosmo's best functions to Python cosmo module - Pin function trace indent depth to that of caller - Show memory diagram on invalid access in MODE=dbg - Differentiate stack overflow on crash in MODE=dbg - Add stb_truetype and tools for analyzing font files - Upgrade to UNICODE 13 and reduce its binary footprint - COMPILE.COM now logs resource usage of build commands - Start implementing basic poll() support on bare metal - Set getauxval(AT_EXECFN) to GetModuleFileName() on NT - Add descriptions to strerror() in non-TINY build modes - Add COUNTBRANCH() macro to help with micro-optimizations - Make error / backtrace / asan / memory code more unbreakable - Add fast perfect C implementation of μ-Law and a-Law audio codecs - Make strtol() functions consistent with other libc implementations - Improve Linenoise implementation (see also github.com/jart/bestline) - COMPILE.COM now suppresses stdout/stderr of successful build commands
2021-09-28 05:58:51 +00:00
#include "libc/calls/struct/sigaction.h"
#include "libc/calls/struct/sigset.h"
2022-05-23 22:06:11 +00:00
#include "libc/calls/syscall-sysv.internal.h"
#include "libc/dce.h"
2020-06-15 14:18:57 +00:00
#include "libc/errno.h"
#include "libc/fmt/fmt.h"
#include "libc/fmt/itoa.h"
#include "libc/intrin/kprintf.h"
#include "libc/intrin/spinlock.h"
#include "libc/log/check.h"
#include "libc/log/internal.h"
#include "libc/macros.internal.h"
2020-06-15 14:18:57 +00:00
#include "libc/nt/process.h"
#include "libc/runtime/internal.h"
#include "libc/runtime/runtime.h"
#include "libc/runtime/symbols.internal.h"
#include "libc/sock/sock.h"
2020-06-15 14:18:57 +00:00
#include "libc/stdio/stdio.h"
#include "libc/str/str.h"
#include "libc/sysv/consts/poll.h"
#include "libc/sysv/consts/sa.h"
Make numerous improvements - Python static hello world now 1.8mb - Python static fully loaded now 10mb - Python HTTPS client now uses MbedTLS - Python REPL now completes import stmts - Increase stack size for Python for now - Begin synthesizing posixpath and ntpath - Restore Python \N{UNICODE NAME} support - Restore Python NFKD symbol normalization - Add optimized code path for Intel SHA-NI - Get more Python unit tests passing faster - Get Python help() pagination working on NT - Python hashlib now supports MbedTLS PBKDF2 - Make memcpy/memmove/memcmp/bcmp/etc. faster - Add Mersenne Twister and Vigna to LIBC_RAND - Provide privileged __printf() for error code - Fix zipos opendir() so that it reports ENOTDIR - Add basic chmod() implementation for Windows NT - Add Cosmo's best functions to Python cosmo module - Pin function trace indent depth to that of caller - Show memory diagram on invalid access in MODE=dbg - Differentiate stack overflow on crash in MODE=dbg - Add stb_truetype and tools for analyzing font files - Upgrade to UNICODE 13 and reduce its binary footprint - COMPILE.COM now logs resource usage of build commands - Start implementing basic poll() support on bare metal - Set getauxval(AT_EXECFN) to GetModuleFileName() on NT - Add descriptions to strerror() in non-TINY build modes - Add COUNTBRANCH() macro to help with micro-optimizations - Make error / backtrace / asan / memory code more unbreakable - Add fast perfect C implementation of μ-Law and a-Law audio codecs - Make strtol() functions consistent with other libc implementations - Improve Linenoise implementation (see also github.com/jart/bestline) - COMPILE.COM now suppresses stdout/stderr of successful build commands
2021-09-28 05:58:51 +00:00
#include "libc/sysv/consts/sig.h"
#include "libc/sysv/consts/w.h"
2020-06-15 14:18:57 +00:00
#include "libc/testlib/testlib.h"
#include "libc/x/x.h"
2020-06-15 14:18:57 +00:00
2021-05-03 08:59:27 +00:00
static int x;
char g_testlib_olddir[PATH_MAX];
char g_testlib_tmpdir[PATH_MAX];
struct sigaction wanthandlers[31];
static char testlib_error_lock;
2021-05-03 08:59:27 +00:00
2020-06-15 14:18:57 +00:00
void testlib_finish(void) {
if (g_testlib_failed) {
fprintf(stderr, "%u / %u %s\n", g_testlib_failed, g_testlib_ran,
"tests failed");
}
}
void testlib_error_enter(const char *file, const char *func) {
atomic_fetch_sub_explicit(&__ftrace, 1, memory_order_relaxed);
atomic_fetch_sub_explicit(&__strace, 1, memory_order_relaxed);
2022-07-18 09:11:06 +00:00
if (!__vforked) _spinlock(&testlib_error_lock);
if (!IsWindows()) sys_getpid(); /* make strace easier to read */
if (!IsWindows()) sys_getpid();
if (g_testlib_shoulddebugbreak) {
DebugBreak();
}
testlib_showerror_file = file;
testlib_showerror_func = func;
}
void testlib_error_leave(void) {
atomic_fetch_add_explicit(&__ftrace, 1, memory_order_relaxed);
atomic_fetch_add_explicit(&__strace, 1, memory_order_relaxed);
_spunlock(&testlib_error_lock);
}
wontreturn void testlib_abort(void) {
2020-06-15 14:18:57 +00:00
testlib_finish();
__restorewintty();
_Exit(MIN(255, g_testlib_failed));
2020-06-15 14:18:57 +00:00
}
2021-05-03 08:59:27 +00:00
static void SetupTmpDir(void) {
Make numerous improvements - Python static hello world now 1.8mb - Python static fully loaded now 10mb - Python HTTPS client now uses MbedTLS - Python REPL now completes import stmts - Increase stack size for Python for now - Begin synthesizing posixpath and ntpath - Restore Python \N{UNICODE NAME} support - Restore Python NFKD symbol normalization - Add optimized code path for Intel SHA-NI - Get more Python unit tests passing faster - Get Python help() pagination working on NT - Python hashlib now supports MbedTLS PBKDF2 - Make memcpy/memmove/memcmp/bcmp/etc. faster - Add Mersenne Twister and Vigna to LIBC_RAND - Provide privileged __printf() for error code - Fix zipos opendir() so that it reports ENOTDIR - Add basic chmod() implementation for Windows NT - Add Cosmo's best functions to Python cosmo module - Pin function trace indent depth to that of caller - Show memory diagram on invalid access in MODE=dbg - Differentiate stack overflow on crash in MODE=dbg - Add stb_truetype and tools for analyzing font files - Upgrade to UNICODE 13 and reduce its binary footprint - COMPILE.COM now logs resource usage of build commands - Start implementing basic poll() support on bare metal - Set getauxval(AT_EXECFN) to GetModuleFileName() on NT - Add descriptions to strerror() in non-TINY build modes - Add COUNTBRANCH() macro to help with micro-optimizations - Make error / backtrace / asan / memory code more unbreakable - Add fast perfect C implementation of μ-Law and a-Law audio codecs - Make strtol() functions consistent with other libc implementations - Improve Linenoise implementation (see also github.com/jart/bestline) - COMPILE.COM now suppresses stdout/stderr of successful build commands
2021-09-28 05:58:51 +00:00
char *p = g_testlib_tmpdir;
p = stpcpy(p, "o/tmp/");
p = stpcpy(p, program_invocation_short_name), *p++ = '.';
p = FormatInt64(p, getpid()), *p++ = '.';
p = FormatInt64(p, x++);
Make numerous improvements - Python static hello world now 1.8mb - Python static fully loaded now 10mb - Python HTTPS client now uses MbedTLS - Python REPL now completes import stmts - Increase stack size for Python for now - Begin synthesizing posixpath and ntpath - Restore Python \N{UNICODE NAME} support - Restore Python NFKD symbol normalization - Add optimized code path for Intel SHA-NI - Get more Python unit tests passing faster - Get Python help() pagination working on NT - Python hashlib now supports MbedTLS PBKDF2 - Make memcpy/memmove/memcmp/bcmp/etc. faster - Add Mersenne Twister and Vigna to LIBC_RAND - Provide privileged __printf() for error code - Fix zipos opendir() so that it reports ENOTDIR - Add basic chmod() implementation for Windows NT - Add Cosmo's best functions to Python cosmo module - Pin function trace indent depth to that of caller - Show memory diagram on invalid access in MODE=dbg - Differentiate stack overflow on crash in MODE=dbg - Add stb_truetype and tools for analyzing font files - Upgrade to UNICODE 13 and reduce its binary footprint - COMPILE.COM now logs resource usage of build commands - Start implementing basic poll() support on bare metal - Set getauxval(AT_EXECFN) to GetModuleFileName() on NT - Add descriptions to strerror() in non-TINY build modes - Add COUNTBRANCH() macro to help with micro-optimizations - Make error / backtrace / asan / memory code more unbreakable - Add fast perfect C implementation of μ-Law and a-Law audio codecs - Make strtol() functions consistent with other libc implementations - Improve Linenoise implementation (see also github.com/jart/bestline) - COMPILE.COM now suppresses stdout/stderr of successful build commands
2021-09-28 05:58:51 +00:00
p[0] = '\0';
CHECK_NE(-1, makedirs(g_testlib_tmpdir, 0755), "%s", g_testlib_tmpdir);
CHECK_NOTNULL(realpath(g_testlib_tmpdir, g_testlib_tmpdir), "%`'s",
g_testlib_tmpdir);
CHECK_NE(-1, chdir(g_testlib_tmpdir), "%s", g_testlib_tmpdir);
2021-05-03 08:59:27 +00:00
}
static void TearDownTmpDir(void) {
CHECK_NE(-1, chdir(g_testlib_olddir));
CHECK_NE(-1, rmrf(g_testlib_tmpdir), "%s", g_testlib_tmpdir);
}
static void DoNothing(int sig) {
// function intentionally empty
}
static void CopySignalHandlers(void) {
#if 0
int i;
for (i = 0; i < ARRAYLEN(wanthandlers); ++i) {
if (i + 1 == SIGKILL || i + 1 == SIGSTOP) continue;
CHECK_EQ(0, sigaction(i + 1, 0, wanthandlers + i), "sig=%d", i + 1);
}
#endif
}
static void CheckSignalHandler(int sig) {
#if 0
int i;
struct sigaction sa = {0};
assert(0 <= sig - 1 && sig - 1 < ARRAYLEN(wanthandlers));
CHECK_EQ(0, sigaction(sig, 0, &sa));
CHECK_EQ(0, memcmp(wanthandlers + sig - 1, &sa, sizeof(sa)),
"signal handler for %s was %p/%#x/%#x:%x "
"but should have been restored to %p/%#x/%#x:%x",
strsignal(sig), sa.sa_handler, sa.sa_flags, sa.sa_mask.__bits[0],
sa.sa_mask.__bits[1], wanthandlers[sig - 1].sa_handler,
wanthandlers[sig - 1].sa_flags,
wanthandlers[sig - 1].sa_mask.__bits[0],
wanthandlers[sig - 1].sa_mask.__bits[1]);
#endif
}
static void CheckForSignalHandlers(void) {
#if 0
CheckSignalHandler(SIGINT);
CheckSignalHandler(SIGQUIT);
CheckSignalHandler(SIGCHLD);
CheckSignalHandler(SIGFPE);
CheckSignalHandler(SIGILL);
CheckSignalHandler(SIGSEGV);
CheckSignalHandler(SIGTRAP);
CheckSignalHandler(SIGABRT);
CheckSignalHandler(SIGBUS);
CheckSignalHandler(SIGSYS);
CheckSignalHandler(SIGWINCH);
#endif
}
static void CheckForFileDescriptors(void) {
#if 0
// TODO: race condition on fd cleanup :'(
int i;
struct pollfd pfds[16];
if (!weaken(open) && !weaken(socket)) return;
for (i = 0; i < ARRAYLEN(pfds); ++i) {
pfds[i].fd = i + 3;
pfds[i].events = POLLIN;
}
if (poll(pfds, ARRAYLEN(pfds), 0) > 0) {
for (i = 0; i < ARRAYLEN(pfds); ++i) {
if (pfds[i].revents & POLLNVAL) continue;
++g_testlib_failed;
fprintf(stderr, "error: test failed to close() fd %d\n", pfds[i].fd);
}
}
#endif
}
static void CheckForZombies(void) {
#if 0
int e, pid;
sigset_t ss, oldss;
struct sigaction oldsa;
struct sigaction ignore = {.sa_handler = DoNothing};
if (!weaken(fork)) return;
for (;;) {
if ((pid = wait(0)) == -1) {
CHECK_EQ(ECHILD, errno);
break;
} else {
++g_testlib_failed;
fprintf(stderr, "error: test failed to reap zombies %d\n", pid);
}
}
#endif
2021-05-03 08:59:27 +00:00
}
2020-06-15 14:18:57 +00:00
/**
* Runs all test case functions in sorted order.
*/
testonly void testlib_runtestcases(testfn_t *start, testfn_t *end,
testfn_t warmup) {
2020-12-26 10:09:07 +00:00
/*
2020-06-15 14:18:57 +00:00
* getpid() calls are inserted to help visually see tests in traces
* which can be performed on Linux, FreeBSD, OpenBSD, and XNU:
*
* strace -f o/default/test.com |& less
* truss o/default/test.com |& less
* ktrace -f trace o/default/test.com </dev/null; kdump -f trace | less
* dtruss o/default/test.com |& less
*
* Test cases are iterable via a decentralized section. Your TEST()
* macro inserts .testcase.SUITENAME sections into the binary which
* the linker sorts into an array.
*
* @see ape/ape.lds
*/
const testfn_t *fn;
CopySignalHandlers();
if (weaken(testlib_enable_tmp_setup_teardown) ||
weaken(testlib_enable_tmp_setup_teardown_once)) {
CHECK_NOTNULL(getcwd(g_testlib_olddir, sizeof(g_testlib_olddir)));
}
if (weaken(testlib_enable_tmp_setup_teardown_once)) {
SetupTmpDir();
}
2021-05-03 08:59:27 +00:00
if (weaken(SetUpOnce)) weaken(SetUpOnce)();
for (x = 0, fn = start; fn != end; ++fn) {
2021-05-03 08:59:27 +00:00
if (weaken(testlib_enable_tmp_setup_teardown)) SetupTmpDir();
2020-06-15 14:18:57 +00:00
if (weaken(SetUp)) weaken(SetUp)();
errno = 0;
SetLastError(0);
if (!IsWindows()) sys_getpid();
2020-06-15 14:18:57 +00:00
if (warmup) warmup();
testlib_clearxmmregisters();
2020-06-15 14:18:57 +00:00
(*fn)();
if (!IsWindows()) sys_getpid();
2020-06-15 14:18:57 +00:00
if (weaken(TearDown)) weaken(TearDown)();
2021-05-03 08:59:27 +00:00
if (weaken(testlib_enable_tmp_setup_teardown)) TearDownTmpDir();
CheckForFileDescriptors();
CheckForSignalHandlers();
CheckForZombies();
2020-06-15 14:18:57 +00:00
}
if (weaken(TearDownOnce)) {
weaken(TearDownOnce)();
}
if (weaken(testlib_enable_tmp_setup_teardown_once)) {
TearDownTmpDir();
}
2020-06-15 14:18:57 +00:00
}