mirror of
https://github.com/jart/cosmopolitan.git
synced 2025-10-11 10:19:45 +00:00
Initial import
This commit is contained in:
commit
c91b3c5006
14915 changed files with 590219 additions and 0 deletions
77
test/libc/calls/hefty/commandv_test.c
Normal file
77
test/libc/calls/hefty/commandv_test.c
Normal file
|
@ -0,0 +1,77 @@
|
|||
/*-*- 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 │
|
||||
│ │
|
||||
│ This program is free software; you can redistribute it and/or modify │
|
||||
│ it under the terms of the GNU General Public License as published by │
|
||||
│ the Free Software Foundation; version 2 of the License. │
|
||||
│ │
|
||||
│ This program is distributed in the hope that it will be useful, but │
|
||||
│ WITHOUT ANY WARRANTY; without even the implied warranty of │
|
||||
│ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU │
|
||||
│ General Public License for more details. │
|
||||
│ │
|
||||
│ You should have received a copy of the GNU General Public License │
|
||||
│ along with this program; if not, write to the Free Software │
|
||||
│ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA │
|
||||
│ 02110-1301 USA │
|
||||
╚─────────────────────────────────────────────────────────────────────────────*/
|
||||
#include "libc/calls/calls.h"
|
||||
#include "libc/dce.h"
|
||||
#include "libc/mem/mem.h"
|
||||
#include "libc/runtime/gc.h"
|
||||
#include "libc/runtime/internal.h"
|
||||
#include "libc/runtime/runtime.h"
|
||||
#include "libc/stdio/stdio.h"
|
||||
#include "libc/testlib/testlib.h"
|
||||
#include "libc/x/x.h"
|
||||
|
||||
uint64_t i;
|
||||
const char *oldpath, *bindir, *homedir, *binsh, *sh;
|
||||
|
||||
TEST(commandv_00, todo) { /* TODO(jart): Improve this on Windows. */
|
||||
if (IsWindows()) exit(0);
|
||||
}
|
||||
|
||||
TEST(commandv_001, setupFiles) {
|
||||
mkdir("o", 0755);
|
||||
mkdir("o/tmp", 0755);
|
||||
oldpath = strdup(getenv("PATH"));
|
||||
homedir = xasprintf("o/tmp/home.%d", getpid());
|
||||
bindir = xasprintf("o/tmp/bin.%d", getpid());
|
||||
binsh = xasprintf("%s/sh.com", bindir);
|
||||
ASSERT_NE(-1, mkdir(homedir, 0755));
|
||||
ASSERT_NE(-1, mkdir(bindir, 0755));
|
||||
ASSERT_NE(-1, touch(binsh, 0755));
|
||||
ASSERT_NE(-1, setenv("PATH", bindir, true));
|
||||
}
|
||||
|
||||
TEST(commandv_010, testSlashes_wontSearchPath_butChecksAccess) {
|
||||
sh = defer(unlink, gc(xasprintf("%s/sh.com", homedir)));
|
||||
EXPECT_NE(-1, touch(sh, 0755));
|
||||
i = g_syscount;
|
||||
EXPECT_STREQ(sh, commandv(sh));
|
||||
if (!IsWindows()) EXPECT_EQ(i + 1 /* access() */, g_syscount);
|
||||
}
|
||||
|
||||
TEST(commandv_010, testNoSlashes_searchesPath_withMemoization) {
|
||||
if (IsTiny()) return;
|
||||
i = g_syscount;
|
||||
EXPECT_STREQ(binsh, commandv("sh.com"));
|
||||
if (!IsWindows()) EXPECT_GT(g_syscount, i);
|
||||
i = g_syscount;
|
||||
EXPECT_STREQ(binsh, commandv("sh.com"));
|
||||
if (!IsWindows()) EXPECT_EQ(g_syscount, i);
|
||||
}
|
||||
|
||||
TEST(commandv_999, teardown) {
|
||||
setenv("PATH", oldpath, true);
|
||||
unlink(binsh);
|
||||
rmdir(bindir);
|
||||
rmdir(homedir);
|
||||
free(bindir);
|
||||
free(binsh);
|
||||
free(homedir);
|
||||
free(oldpath);
|
||||
}
|
54
test/libc/calls/hefty/dirstream_test.c
Normal file
54
test/libc/calls/hefty/dirstream_test.c
Normal file
|
@ -0,0 +1,54 @@
|
|||
/*-*- 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 │
|
||||
│ │
|
||||
│ This program is free software; you can redistribute it and/or modify │
|
||||
│ it under the terms of the GNU General Public License as published by │
|
||||
│ the Free Software Foundation; version 2 of the License. │
|
||||
│ │
|
||||
│ This program is distributed in the hope that it will be useful, but │
|
||||
│ WITHOUT ANY WARRANTY; without even the implied warranty of │
|
||||
│ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU │
|
||||
│ General Public License for more details. │
|
||||
│ │
|
||||
│ You should have received a copy of the GNU General Public License │
|
||||
│ along with this program; if not, write to the Free Software │
|
||||
│ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA │
|
||||
│ 02110-1301 USA │
|
||||
╚─────────────────────────────────────────────────────────────────────────────*/
|
||||
#include "libc/calls/calls.h"
|
||||
#include "libc/calls/struct/dirent.h"
|
||||
#include "libc/rand/rand.h"
|
||||
#include "libc/runtime/gc.h"
|
||||
#include "libc/runtime/runtime.h"
|
||||
#include "libc/str/str.h"
|
||||
#include "libc/testlib/testlib.h"
|
||||
#include "libc/x/x.h"
|
||||
|
||||
TEST(dirstream, test) {
|
||||
DIR *dir;
|
||||
struct dirent *ent;
|
||||
char *dpath, *file1, *file2;
|
||||
dpath = gc(xasprintf("%s%s%lu", kTmpPath, "dirstream", rand32()));
|
||||
file1 = gc(xasprintf("%s/%s", dpath, "foo"));
|
||||
file2 = gc(xasprintf("%s/%s", dpath, "bar"));
|
||||
EXPECT_NE(-1, mkdir(dpath, 0755));
|
||||
EXPECT_NE(-1, touch(file1, 0644));
|
||||
EXPECT_NE(-1, touch(file2, 0644));
|
||||
|
||||
EXPECT_TRUE(NULL != (dir = opendir(dpath)));
|
||||
bool hasfoo = false;
|
||||
bool hasbar = false;
|
||||
while ((ent = readdir(dir))) {
|
||||
if (strcmp(ent->d_name, "foo")) hasfoo = true;
|
||||
if (strcmp(ent->d_name, "bar")) hasbar = true;
|
||||
}
|
||||
EXPECT_TRUE(hasfoo);
|
||||
EXPECT_TRUE(hasbar);
|
||||
EXPECT_NE(-1, closedir(dir));
|
||||
|
||||
EXPECT_NE(-1, unlink(file2));
|
||||
EXPECT_NE(-1, unlink(file1));
|
||||
EXPECT_NE(-1, rmdir(dpath));
|
||||
}
|
77
test/libc/calls/hefty/mkntcmdline_test.c
Normal file
77
test/libc/calls/hefty/mkntcmdline_test.c
Normal file
|
@ -0,0 +1,77 @@
|
|||
/*-*- 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 │
|
||||
│ │
|
||||
│ This program is free software; you can redistribute it and/or modify │
|
||||
│ it under the terms of the GNU General Public License as published by │
|
||||
│ the Free Software Foundation; version 2 of the License. │
|
||||
│ │
|
||||
│ This program is distributed in the hope that it will be useful, but │
|
||||
│ WITHOUT ANY WARRANTY; without even the implied warranty of │
|
||||
│ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU │
|
||||
│ General Public License for more details. │
|
||||
│ │
|
||||
│ You should have received a copy of the GNU General Public License │
|
||||
│ along with this program; if not, write to the Free Software │
|
||||
│ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA │
|
||||
│ 02110-1301 USA │
|
||||
╚─────────────────────────────────────────────────────────────────────────────*/
|
||||
#include "libc/calls/hefty/ntspawn.h"
|
||||
#include "libc/errno.h"
|
||||
#include "libc/mem/mem.h"
|
||||
#include "libc/runtime/gc.h"
|
||||
#include "libc/str/str.h"
|
||||
#include "libc/testlib/testlib.h"
|
||||
|
||||
TEST(mkntcmdline, emptyArgvList_isntValidCommandLine) {
|
||||
char *argv[] = {NULL};
|
||||
EXPECT_EQ(NULL, gc(mkntcmdline(argv)));
|
||||
EXPECT_EQ(EINVAL, errno);
|
||||
}
|
||||
|
||||
TEST(mkntcmdline, emptyArgs_isEmptyString) {
|
||||
char *argv[] = {"", NULL};
|
||||
EXPECT_STREQ(u"", gc(mkntcmdline(argv)));
|
||||
}
|
||||
|
||||
TEST(mkntcmdline, ignoranceIsBliss) {
|
||||
char *argv[] = {"echo", "hello", "world", NULL};
|
||||
EXPECT_STREQ(u"echo hello world", gc(mkntcmdline(argv)));
|
||||
}
|
||||
|
||||
TEST(mkntcmdline, spaceInArgument_getQuotesWrappedAround) {
|
||||
char *argv[] = {"echo", "hello there", "world", NULL};
|
||||
EXPECT_STREQ(u"echo \"hello there\" world", gc(mkntcmdline(argv)));
|
||||
}
|
||||
|
||||
TEST(mkntcmdline, justQuote) {
|
||||
char *argv[] = {"\"", NULL};
|
||||
EXPECT_STREQ(u"\"\\\"\"", gc(mkntcmdline(argv)));
|
||||
}
|
||||
|
||||
TEST(mkntcmdline, justSlash) {
|
||||
char *argv[] = {"\\", NULL};
|
||||
EXPECT_STREQ(u"\\", gc(mkntcmdline(argv)));
|
||||
}
|
||||
|
||||
TEST(mkntcmdline, justSlashQuote) {
|
||||
char *argv[] = {"\\\"", NULL};
|
||||
EXPECT_STREQ(u"\"\\\\\\\"\"" /* "\\\"" */, gc(mkntcmdline(argv)));
|
||||
}
|
||||
|
||||
TEST(mkntcmdline, basicQuoting) {
|
||||
char *argv[] = {"a\"b c", "d", NULL};
|
||||
EXPECT_STREQ(u"\"a\\\"b c\" d" /* "a\"b c" d */, gc(mkntcmdline(argv)));
|
||||
}
|
||||
|
||||
TEST(mkntcmdline, testUnicode) {
|
||||
char *argv1[] = {
|
||||
gc(strdup("(╯°□°)╯")),
|
||||
gc(strdup("要依法治国是赞美那些谁是公义的和惩罚恶人。 - 韩非")),
|
||||
NULL,
|
||||
};
|
||||
EXPECT_STREQ(
|
||||
u"(╯°□°)╯ \"要依法治国是赞美那些谁是公义的和惩罚恶人。 - 韩非\"",
|
||||
gc(mkntcmdline(memcpy(gc(malloc(sizeof(argv1))), argv1, sizeof(argv1)))));
|
||||
}
|
39
test/libc/calls/hefty/mkntenvblock_test.c
Normal file
39
test/libc/calls/hefty/mkntenvblock_test.c
Normal file
|
@ -0,0 +1,39 @@
|
|||
/*-*- 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 │
|
||||
│ │
|
||||
│ This program is free software; you can redistribute it and/or modify │
|
||||
│ it under the terms of the GNU General Public License as published by │
|
||||
│ the Free Software Foundation; version 2 of the License. │
|
||||
│ │
|
||||
│ This program is distributed in the hope that it will be useful, but │
|
||||
│ WITHOUT ANY WARRANTY; without even the implied warranty of │
|
||||
│ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU │
|
||||
│ General Public License for more details. │
|
||||
│ │
|
||||
│ You should have received a copy of the GNU General Public License │
|
||||
│ along with this program; if not, write to the Free Software │
|
||||
│ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA │
|
||||
│ 02110-1301 USA │
|
||||
╚─────────────────────────────────────────────────────────────────────────────*/
|
||||
#include "libc/runtime/gc.h"
|
||||
#include "libc/calls/hefty/ntspawn.h"
|
||||
#include "libc/testlib/testlib.h"
|
||||
|
||||
TEST(mkntenvblock, emptyList_onlyOutputsDoubleNulStringTerminator) {
|
||||
char *envp[] = {NULL};
|
||||
ASSERT_BINEQ(u" ", gc(mkntenvblock(envp)));
|
||||
}
|
||||
|
||||
TEST(mkntenvblock, envp_becomesSortedDoubleNulTerminatedUtf16String) {
|
||||
char *envp[] = {"u=b", "c=d", "韩=非", "uh=d", "hduc=d", NULL};
|
||||
ASSERT_BINEQ(
|
||||
u"c = d "
|
||||
u"h d u c = d "
|
||||
u"u = b "
|
||||
u"u h = d "
|
||||
u"Θù= ^ù "
|
||||
u" ",
|
||||
gc(mkntenvblock(envp)));
|
||||
}
|
33
test/libc/calls/hefty/sortenvp_test.c
Normal file
33
test/libc/calls/hefty/sortenvp_test.c
Normal file
|
@ -0,0 +1,33 @@
|
|||
/*-*- 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 │
|
||||
│ │
|
||||
│ This program is free software; you can redistribute it and/or modify │
|
||||
│ it under the terms of the GNU General Public License as published by │
|
||||
│ the Free Software Foundation; version 2 of the License. │
|
||||
│ │
|
||||
│ This program is distributed in the hope that it will be useful, but │
|
||||
│ WITHOUT ANY WARRANTY; without even the implied warranty of │
|
||||
│ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU │
|
||||
│ General Public License for more details. │
|
||||
│ │
|
||||
│ You should have received a copy of the GNU General Public License │
|
||||
│ along with this program; if not, write to the Free Software │
|
||||
│ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA │
|
||||
│ 02110-1301 USA │
|
||||
╚─────────────────────────────────────────────────────────────────────────────*/
|
||||
#include "libc/runtime/gc.h"
|
||||
#include "libc/calls/hefty/ntspawn.h"
|
||||
#include "libc/testlib/testlib.h"
|
||||
|
||||
TEST(sortenvp, test) {
|
||||
char *envp[] = {"u=b", "c=d", "韩=非", "uh=d", "hduc=d", NULL};
|
||||
char **sortedenvp = gc(sortenvp(envp));
|
||||
EXPECT_STREQ("c=d", sortedenvp[0]);
|
||||
EXPECT_STREQ("hduc=d", sortedenvp[1]);
|
||||
EXPECT_STREQ("u=b", sortedenvp[2]);
|
||||
EXPECT_STREQ("uh=d", sortedenvp[3]);
|
||||
EXPECT_STREQ("韩=非", sortedenvp[4]);
|
||||
EXPECT_EQ(NULL, sortedenvp[5]);
|
||||
}
|
74
test/libc/calls/hefty/spawnve_test.c
Normal file
74
test/libc/calls/hefty/spawnve_test.c
Normal file
|
@ -0,0 +1,74 @@
|
|||
/*-*- 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 │
|
||||
│ │
|
||||
│ This program is free software; you can redistribute it and/or modify │
|
||||
│ it under the terms of the GNU General Public License as published by │
|
||||
│ the Free Software Foundation; version 2 of the License. │
|
||||
│ │
|
||||
│ This program is distributed in the hope that it will be useful, but │
|
||||
│ WITHOUT ANY WARRANTY; without even the implied warranty of │
|
||||
│ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU │
|
||||
│ General Public License for more details. │
|
||||
│ │
|
||||
│ You should have received a copy of the GNU General Public License │
|
||||
│ along with this program; if not, write to the Free Software │
|
||||
│ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA │
|
||||
│ 02110-1301 USA │
|
||||
╚─────────────────────────────────────────────────────────────────────────────*/
|
||||
#include "libc/calls/calls.h"
|
||||
#include "libc/calls/hefty/spawn.h"
|
||||
#include "libc/dce.h"
|
||||
#include "libc/errno.h"
|
||||
#include "libc/runtime/runtime.h"
|
||||
#include "libc/str/str.h"
|
||||
#include "libc/sysv/consts/auxv.h"
|
||||
#include "libc/sysv/consts/exit.h"
|
||||
#include "libc/sysv/consts/fileno.h"
|
||||
#include "libc/testlib/testlib.h"
|
||||
|
||||
#define CMD (IsWindows() ? "cmd" : "sh")
|
||||
#define ARG (IsWindows() ? "/c" : "-c")
|
||||
#define COD (IsWindows() ? "echo %BOOP%" : "echo $BOOP")
|
||||
#define OUT (IsWindows() ? u"hello♪◙" : u"hello◙")
|
||||
|
||||
TEST(spawnve, testIpc) {
|
||||
ssize_t got;
|
||||
int pid, wstatus;
|
||||
char buf[16] = {0};
|
||||
int tubes[3] = {STDIN_FILENO, -1, STDERR_FILENO};
|
||||
errno = 0;
|
||||
setenv("BOOP", "hello", true);
|
||||
ASSERT_EQ(0, errno);
|
||||
ASSERT_NE(-1, (pid = spawnlp(0, tubes, CMD, CMD, ARG, COD, NULL)));
|
||||
ASSERT_EQ(0, errno);
|
||||
ASSERT_NE(-1, (got = read(tubes[1], buf, sizeof(buf))));
|
||||
ASSERT_EQ(0, errno);
|
||||
ASSERT_NE(-1, waitpid(pid, &wstatus, 0));
|
||||
ASSERT_EQ(0, errno);
|
||||
EXPECT_EQ(0, WEXITSTATUS(wstatus));
|
||||
EXPECT_BINEQ(OUT, buf);
|
||||
}
|
||||
|
||||
TEST(spawnve, testExit) {
|
||||
int pid, wstatus;
|
||||
ASSERT_NE(-1, (pid = spawnlp(0, NULL, CMD, CMD, ARG, "exit 42", NULL)));
|
||||
ASSERT_NE(-1, waitpid(pid, &wstatus, 0));
|
||||
EXPECT_EQ(42, WEXITSTATUS(wstatus));
|
||||
}
|
||||
|
||||
TEST(spawnve, testSpawnSelf) {
|
||||
int pid, wstatus;
|
||||
ASSERT_NE(-1,
|
||||
(pid = spawnlp(0, NULL, (const char *)getauxval(AT_EXECFN),
|
||||
program_invocation_name, "--testSpawnSelf", NULL)));
|
||||
ASSERT_NE(-1, waitpid(pid, &wstatus, 0));
|
||||
EXPECT_EQ(123, WEXITSTATUS(wstatus));
|
||||
}
|
||||
static textstartup void onspawnself() {
|
||||
if (g_argc > 1 && strcmp(g_argv[1], "--testSpawnSelf") == 0) {
|
||||
_exit(123);
|
||||
}
|
||||
}
|
||||
const void *const onspawnself_ctor[] initarray = {onspawnself};
|
Loading…
Add table
Add a link
Reference in a new issue