Skip tests when they will fail

GetProgramExecutableName does not work right on assimilated OpenBSD or
non-silicon XNU binaries. If we are on one of those platforms, look for
ape magic in ourselves and quit if none is found.

If `argv[0]` is not reliable for the test invocation itself (e.g.
because the test is run out of `PATH`), the test assert-fails.
This commit is contained in:
Jōshin 2023-12-31 12:01:12 -05:00
parent 673daea2c2
commit 6e25e84518
No known key found for this signature in database

View file

@ -21,6 +21,7 @@
#include "libc/dce.h"
#include "libc/limits.h"
#include "libc/runtime/runtime.h"
#include "libc/serialize.h"
#include "libc/stdio/stdio.h"
#include "libc/str/str.h"
#include "libc/sysv/consts/o.h"
@ -34,6 +35,21 @@ static char *self;
void SetUpOnce(void) {
self = GetProgramExecutableName();
testlib_enable_tmp_setup_teardown();
if (IsOpenbsd() || (IsXnu() && !IsXnuSilicon())) {
ASSERT_STRNE(self, "");
ASSERT_SYS(0, 3, open(self, O_RDONLY));
char buf[8];
ASSERT_SYS(0, 8, pread(3, buf, 8, 0));
ASSERT_SYS(0, 0, close(3));
if (READ64LE(buf) != READ64LE("MZqFpD='") &&
READ64LE(buf) != READ64LE("jartsr='") &&
READ64LE(buf) != READ64LE("APEDBG='")) {
fprintf(stderr,
"GetProgramExecutableName is unreliable on this platform for\n"
"assimilated binaries\n");
exit(0);
}
}
}
__attribute__((__constructor__)) static void Child(int argc, char *argv[]) {