From 5409c015184b17206fe0d6af28098345e1821590 Mon Sep 17 00:00:00 2001 From: nakst <> Date: Sun, 16 Jan 2022 11:33:45 +0000 Subject: [PATCH] add test for posix subsystem --- desktop/api_tests.cpp | 85 ++++++++++++++++++++++++++++++++++++ desktop/api_tests.ini | 1 + util/automation/run_tests.sh | 3 ++ 3 files changed, 89 insertions(+) diff --git a/desktop/api_tests.cpp b/desktop/api_tests.cpp index 3932414..f03ba53 100644 --- a/desktop/api_tests.cpp +++ b/desktop/api_tests.cpp @@ -1117,6 +1117,90 @@ bool PipeTests() { ////////////////////////////////////////////////////////////// +#include + +#define _exit(x) EsPOSIXSystemCall(SYS_exit_group, (intptr_t) x, 0, 0, 0, 0, 0) +#define close(x) EsPOSIXSystemCall(SYS_close, (intptr_t) x, 0, 0, 0, 0, 0) +#define dup2(x, y) EsPOSIXSystemCall(SYS_dup2, (intptr_t) x, (intptr_t) y, 0, 0, 0, 0) +#define execve(x, y, z) EsPOSIXSystemCall(SYS_execve, (intptr_t) x, (intptr_t) y, (intptr_t) z, 0, 0, 0) +#define exit(x) EsPOSIXSystemCall(SYS_exit_group, (intptr_t) x, 0, 0, 0, 0, 0) +#define pipe(x) EsPOSIXSystemCall(SYS_pipe, (intptr_t) x, 0, 0, 0, 0, 0) +#define read(x, y, z) EsPOSIXSystemCall(SYS_read, (intptr_t) x, (intptr_t) y, (intptr_t) z, 0, 0, 0) +#define rename(x, y) EsPOSIXSystemCall(SYS_rename, (intptr_t) x, (intptr_t) y, 0, 0, 0, 0) +#define truncate(x, y) EsPOSIXSystemCall(SYS_truncate, (intptr_t) x, (intptr_t) y, 0, 0, 0, 0) +#define unlink(x) EsPOSIXSystemCall(SYS_unlink, (intptr_t) x, 0, 0, 0, 0, 0) +#define vfork() EsPOSIXSystemCall(SYS_vfork, 0, 0, 0, 0, 0, 0) +#define wait4(x, y, z, w) EsPOSIXSystemCall(SYS_wait4, (intptr_t) x, (intptr_t) y, (intptr_t) z, (intptr_t) w, 0, 0) + +bool POSIXSubsystemTest() { + const char *executeEnvironment[] = { + "PATH=/Applications/POSIX/bin", + "TMPDIR=/Applications/POSIX/tmp", + NULL, + }; + + const char *executable = "/Applications/POSIX/bin/busybox"; + + const char *argv[] = { + (char *) "busybox", + (char *) "echo", + (char *) "hello", + NULL, + }; + + int _argc; + char **_argv; + EsPOSIXInitialise(&_argc, &_argv); + + int stdoutPipe[2]; + pipe(stdoutPipe); + + long pid = vfork(); + + if (pid == 0) { + dup2(stdoutPipe[1], 1); + dup2(stdoutPipe[1], 2); + close(stdoutPipe[1]); + execve(executable, argv, executeEnvironment); + EsPrint("Could not execve.\n"); + return false; + } else if (pid > 0) { + close(stdoutPipe[1]); + char readData[10]; + int readPosition = 0; + + while (true) { + intptr_t bytesRead = read(stdoutPipe[0], readData + readPosition, sizeof(readData) - readPosition); + + if (bytesRead <= 0) { + break; + } else { + readPosition += bytesRead; + } + } + + if (readPosition != 6 || EsMemoryCompare(readData, "hello\n", 6)) { + EsPrint("Incorrect output: '%s'.\n", readPosition, readData); + return false; + } + + int status; + wait4(-1, &status, 0, NULL); + + if (status) { + EsPrint("status = %d.\n", status); + return false; + } + } else { + EsPrint("Could not vfork.\n"); + return false; + } + + return true; +} + +////////////////////////////////////////////////////////////// + #endif const Test tests[] = { @@ -1132,6 +1216,7 @@ const Test tests[] = { TEST(RangeSetTests, 60), TEST(UTF8Tests, 60), TEST(PipeTests, 60), + TEST(POSIXSubsystemTest, 60), }; #ifndef API_TESTS_FOR_RUNNER diff --git a/desktop/api_tests.ini b/desktop/api_tests.ini index 851cebf..e7690e8 100644 --- a/desktop/api_tests.ini +++ b/desktop/api_tests.ini @@ -4,6 +4,7 @@ background_service=1 permission_shutdown=1 permission_manage_processes=1 permission_all_files=1 +permission_posix_subsystem=1 [build] source=desktop/api_tests.cpp diff --git a/util/automation/run_tests.sh b/util/automation/run_tests.sh index 914355a..5236262 100755 --- a/util/automation/run_tests.sh +++ b/util/automation/run_tests.sh @@ -7,10 +7,13 @@ set -eux cd essence mkdir -p bin echo "accepted_license=1" >> bin/build_config.ini +echo "Flag.ENABLE_POSIX_SUBSYSTEM=1" >> bin/config.ini echo "desktop/api_tests.ini" >> bin/extra_applications.ini echo `git log | head -n 1 | cut -b 8-14` > bin/commit.txt ./start.sh get-source prefix https://github.com/nakst/build-gcc/releases/download/gcc-11.1.0/gcc-x86_64-essence.tar.xz ./start.sh setup-pre-built-toolchain +./start.sh build +./start.sh build-port busybox ./start.sh run-tests util/x11/build.sh apps/samples/hello.c rm -rf cross .git bin/cache bin/freetype bin/harfbuzz bin/musl root/Applications/POSIX/lib bin/drive