mirror of
https://github.com/jart/cosmopolitan.git
synced 2025-06-26 22:38:30 +00:00
Strongly link glob() into system() and popen()
This commit is contained in:
parent
4e9566cd33
commit
cafdb456ed
24 changed files with 216 additions and 59 deletions
88
test/libc/system/BUILD.mk
Normal file
88
test/libc/system/BUILD.mk
Normal file
|
@ -0,0 +1,88 @@
|
|||
#-*-mode:makefile-gmake;indent-tabs-mode:t;tab-width:8;coding:utf-8-*-┐
|
||||
#── vi: set noet ft=make ts=8 sw=8 fenc=utf-8 :vi ────────────────────┘
|
||||
|
||||
PKGS += TEST_LIBC_SYSTEM
|
||||
|
||||
TEST_LIBC_SYSTEM_FILES := $(wildcard test/libc/system/*)
|
||||
TEST_LIBC_SYSTEM_SRCS = $(filter %.c,$(TEST_LIBC_SYSTEM_FILES))
|
||||
TEST_LIBC_SYSTEM_INCS = $(filter %.inc,$(TEST_LIBC_SYSTEM_FILES))
|
||||
TEST_LIBC_SYSTEM_SRCS_TEST = $(filter %_test.c,$(TEST_LIBC_SYSTEM_SRCS))
|
||||
|
||||
TEST_LIBC_SYSTEM_OBJS = \
|
||||
$(TEST_LIBC_SYSTEM_SRCS:%.c=o/$(MODE)/%.o)
|
||||
|
||||
TEST_LIBC_SYSTEM_COMS = \
|
||||
$(TEST_LIBC_SYSTEM_SRCS:%.c=o/$(MODE)/%)
|
||||
|
||||
TEST_LIBC_SYSTEM_BINS = \
|
||||
$(TEST_LIBC_SYSTEM_COMS) \
|
||||
$(TEST_LIBC_SYSTEM_COMS:%=%.dbg)
|
||||
|
||||
TEST_LIBC_SYSTEM_TESTS = \
|
||||
$(TEST_LIBC_SYSTEM_SRCS_TEST:%.c=o/$(MODE)/%.ok)
|
||||
|
||||
TEST_LIBC_SYSTEM_CHECKS = \
|
||||
$(TEST_LIBC_SYSTEM_SRCS_TEST:%.c=o/$(MODE)/%.runs)
|
||||
|
||||
TEST_LIBC_SYSTEM_DIRECTDEPS = \
|
||||
LIBC_CALLS \
|
||||
LIBC_INTRIN \
|
||||
LIBC_LOG \
|
||||
LIBC_MEM \
|
||||
LIBC_NEXGEN32E \
|
||||
LIBC_RUNTIME \
|
||||
LIBC_STDIO \
|
||||
LIBC_STDIO \
|
||||
LIBC_SYSTEM \
|
||||
LIBC_SYSV \
|
||||
LIBC_TESTLIB \
|
||||
LIBC_THREAD \
|
||||
LIBC_X \
|
||||
THIRD_PARTY_MUSL \
|
||||
THIRD_PARTY_TR \
|
||||
|
||||
TEST_LIBC_SYSTEM_DEPS := \
|
||||
$(call uniq,$(foreach x,$(TEST_LIBC_SYSTEM_DIRECTDEPS),$($(x))))
|
||||
|
||||
o/$(MODE)/test/libc/system/system.pkg: \
|
||||
$(TEST_LIBC_SYSTEM_OBJS) \
|
||||
$(foreach x,$(TEST_LIBC_SYSTEM_DIRECTDEPS),$($(x)_A).pkg)
|
||||
|
||||
o/$(MODE)/test/libc/system/%.dbg: \
|
||||
$(TEST_LIBC_SYSTEM_DEPS) \
|
||||
o/$(MODE)/test/libc/system/%.o \
|
||||
o/$(MODE)/test/libc/system/system.pkg \
|
||||
o/$(MODE)/tool/build/echo.zip.o \
|
||||
$(LIBC_TESTMAIN) \
|
||||
$(CRT) \
|
||||
$(APE_NO_MODIFY_SELF)
|
||||
@$(APELINK)
|
||||
|
||||
o/$(MODE)/test/libc/system/popen_test.dbg: \
|
||||
$(TEST_LIBC_SYSTEM_DEPS) \
|
||||
o/$(MODE)/test/libc/system/popen_test.o \
|
||||
o/$(MODE)/test/libc/system/system.pkg \
|
||||
o/$(MODE)/tool/build/echo.zip.o \
|
||||
$(LIBC_TESTMAIN) \
|
||||
$(CRT) \
|
||||
$(APE_NO_MODIFY_SELF)
|
||||
@$(APELINK)
|
||||
|
||||
o/$(MODE)/test/libc/system/system_test.dbg: \
|
||||
$(TEST_LIBC_SYSTEM_DEPS) \
|
||||
o/$(MODE)/test/libc/system/system_test.o \
|
||||
o/$(MODE)/test/libc/system/system.pkg \
|
||||
o/$(MODE)/tool/build/echo.zip.o \
|
||||
o/$(MODE)/tool/build/cocmd.zip.o \
|
||||
o/$(MODE)/tool/build/false.zip.o \
|
||||
$(LIBC_TESTMAIN) \
|
||||
$(CRT) \
|
||||
$(APE_NO_MODIFY_SELF)
|
||||
@$(APELINK)
|
||||
|
||||
$(TEST_LIBC_SYSTEM_OBJS): test/libc/system/BUILD.mk
|
||||
|
||||
.PHONY: o/$(MODE)/test/libc/system
|
||||
o/$(MODE)/test/libc/system: \
|
||||
$(TEST_LIBC_SYSTEM_BINS) \
|
||||
$(TEST_LIBC_SYSTEM_CHECKS)
|
173
test/libc/system/popen_test.c
Normal file
173
test/libc/system/popen_test.c
Normal file
|
@ -0,0 +1,173 @@
|
|||
/*-*- mode:c;indent-tabs-mode:nil;c-basic-offset:2;tab-width:8;coding:utf-8 -*-│
|
||||
│ vi: set et ft=c ts=2 sts=2 sw=2 fenc=utf-8 :vi │
|
||||
╞══════════════════════════════════════════════════════════════════════════════╡
|
||||
│ Copyright 2022 Justine Alexandra Roberts Tunney │
|
||||
│ │
|
||||
│ 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. │
|
||||
│ │
|
||||
│ 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. │
|
||||
╚─────────────────────────────────────────────────────────────────────────────*/
|
||||
#include "libc/calls/calls.h"
|
||||
#include "libc/calls/struct/sigaction.h"
|
||||
#include "libc/dce.h"
|
||||
#include "libc/errno.h"
|
||||
#include "libc/fmt/itoa.h"
|
||||
#include "libc/intrin/kprintf.h"
|
||||
#include "libc/limits.h"
|
||||
#include "libc/log/log.h"
|
||||
#include "libc/mem/gc.h"
|
||||
#include "libc/mem/mem.h"
|
||||
#include "libc/runtime/runtime.h"
|
||||
#include "libc/stdio/rand.h"
|
||||
#include "libc/stdio/stdio.h"
|
||||
#include "libc/str/str.h"
|
||||
#include "libc/sysv/consts/f.h"
|
||||
#include "libc/sysv/consts/sig.h"
|
||||
#include "libc/testlib/testlib.h"
|
||||
#include "libc/thread/thread.h"
|
||||
#ifdef __x86_64__
|
||||
|
||||
FILE *f;
|
||||
char buf[32];
|
||||
|
||||
void SetUpOnce(void) {
|
||||
testlib_enable_tmp_setup_teardown();
|
||||
}
|
||||
|
||||
void CheckForFdLeaks(void) {
|
||||
int rc, i, l = 0, e = errno;
|
||||
for (i = 3; i < 50; ++i) {
|
||||
rc = fcntl(i, F_GETFL);
|
||||
if (rc == -1) {
|
||||
ASSERT_EQ(EBADF, errno);
|
||||
errno = e;
|
||||
} else {
|
||||
kprintf("file descriptor %d leaked!\n", i);
|
||||
++l;
|
||||
}
|
||||
}
|
||||
if (l) {
|
||||
__die();
|
||||
}
|
||||
}
|
||||
|
||||
TEST(popen, command) {
|
||||
char foo[6];
|
||||
testlib_extract("/zip/echo", "echo", 0755);
|
||||
ASSERT_NE(NULL, (f = popen("./echo hello", "r")));
|
||||
ASSERT_NE(NULL, fgets(foo, sizeof(foo), f));
|
||||
ASSERT_STREQ("hello", foo);
|
||||
ASSERT_EQ(0, pclose(f));
|
||||
CheckForFdLeaks();
|
||||
}
|
||||
|
||||
TEST(popen, semicolon) {
|
||||
ASSERT_NE(NULL, (f = popen("echo hello;echo there", "r")));
|
||||
ASSERT_STREQ("hello\n", fgets(buf, sizeof(buf), f));
|
||||
ASSERT_STREQ("there\n", fgets(buf, sizeof(buf), f));
|
||||
ASSERT_EQ(0, pclose(f));
|
||||
CheckForFdLeaks();
|
||||
}
|
||||
|
||||
TEST(popen, singleQuotes) {
|
||||
setenv("there", "a b c", true);
|
||||
ASSERT_NE(NULL, (f = popen("echo 'hello $there'; echo yo", "r")));
|
||||
ASSERT_STREQ("hello $there\n", fgets(buf, sizeof(buf), f));
|
||||
ASSERT_STREQ("yo\n", fgets(buf, sizeof(buf), f));
|
||||
ASSERT_EQ(0, pclose(f));
|
||||
CheckForFdLeaks();
|
||||
}
|
||||
|
||||
TEST(popen, doubleQuotes) {
|
||||
setenv("hello", "a b c", true);
|
||||
ASSERT_NE(NULL, (f = popen("echo \"$hello there\"", "r")));
|
||||
ASSERT_STREQ("a b c there\n", fgets(buf, sizeof(buf), f));
|
||||
ASSERT_EQ(0, pclose(f));
|
||||
CheckForFdLeaks();
|
||||
}
|
||||
|
||||
TEST(popen, quoteless) {
|
||||
setenv("there", "a b c", true);
|
||||
ASSERT_NE(NULL, (f = popen("echo hello; echo a$there; echo yo", "r")));
|
||||
ASSERT_STREQ("hello\n", fgets(buf, sizeof(buf), f));
|
||||
ASSERT_STREQ("aa b c\n", fgets(buf, sizeof(buf), f)); // mixed feelings
|
||||
ASSERT_STREQ("yo\n", fgets(buf, sizeof(buf), f));
|
||||
ASSERT_EQ(0, pclose(f));
|
||||
CheckForFdLeaks();
|
||||
}
|
||||
|
||||
TEST(popen, pipe) {
|
||||
setenv("there", "a b c", true);
|
||||
ASSERT_NE(NULL, (f = popen("echo hello | toupper", "r")));
|
||||
ASSERT_STREQ("HELLO\n", fgets(buf, sizeof(buf), f));
|
||||
ASSERT_EQ(0, pclose(f));
|
||||
CheckForFdLeaks();
|
||||
}
|
||||
|
||||
sig_atomic_t gotsig;
|
||||
|
||||
void OnSig(int sig) {
|
||||
gotsig = 1;
|
||||
}
|
||||
|
||||
TEST(popen, complicated) {
|
||||
char cmd[64];
|
||||
signal(SIGUSR1, OnSig);
|
||||
sprintf(cmd, "read a ; test \"x$a\" = xhello && kill -USR1 %d", getpid());
|
||||
ASSERT_NE(NULL, (f = popen(cmd, "w")));
|
||||
ASSERT_GE(fputs("hello", f), 0);
|
||||
ASSERT_EQ(0, pclose(f));
|
||||
ASSERT_EQ(1, gotsig);
|
||||
signal(SIGUSR1, SIG_DFL);
|
||||
CheckForFdLeaks();
|
||||
}
|
||||
|
||||
void *Worker(void *arg) {
|
||||
FILE *f;
|
||||
char buf[32];
|
||||
char *arg1, *arg2, *cmd;
|
||||
for (int i = 0; i < 8; ++i) {
|
||||
cmd = malloc(64);
|
||||
arg1 = malloc(13);
|
||||
arg2 = malloc(13);
|
||||
FormatInt32(arg1, _rand64());
|
||||
FormatInt32(arg2, _rand64());
|
||||
sprintf(cmd, "echo %s; ./echo %s", arg1, arg2);
|
||||
strcat(arg1, "\n");
|
||||
strcat(arg2, "\n");
|
||||
ASSERT_NE(NULL, (f = popen(cmd, "r")));
|
||||
EXPECT_STREQ(arg1, fgets(buf, sizeof(buf), f));
|
||||
EXPECT_STREQ(arg2, fgets(buf, sizeof(buf), f));
|
||||
if (IsWindows())
|
||||
// todo(jart): why does it flake with echild?
|
||||
pclose(f);
|
||||
else
|
||||
ASSERT_EQ(0, pclose(f));
|
||||
free(arg2);
|
||||
free(arg1);
|
||||
free(cmd);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
TEST(popen, torture) {
|
||||
int i, n = 4;
|
||||
pthread_t *t = gc(malloc(sizeof(pthread_t) * n));
|
||||
testlib_extract("/zip/echo", "echo", 0755);
|
||||
for (i = 0; i < n; ++i)
|
||||
ASSERT_EQ(0, pthread_create(t + i, 0, Worker, 0));
|
||||
for (i = 0; i < n; ++i)
|
||||
ASSERT_EQ(0, pthread_join(t[i], 0));
|
||||
CheckForFdLeaks();
|
||||
}
|
||||
|
||||
#endif /* __x86_64__ */
|
290
test/libc/system/system_test.c
Normal file
290
test/libc/system/system_test.c
Normal file
|
@ -0,0 +1,290 @@
|
|||
/*-*- mode:c;indent-tabs-mode:nil;c-basic-offset:2;tab-width:8;coding:utf-8 -*-│
|
||||
│ vi: set et ft=c ts=2 sts=2 sw=2 fenc=utf-8 :vi │
|
||||
╞══════════════════════════════════════════════════════════════════════════════╡
|
||||
│ Copyright 2022 Justine Alexandra Roberts Tunney │
|
||||
│ │
|
||||
│ 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. │
|
||||
│ │
|
||||
│ 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. │
|
||||
╚─────────────────────────────────────────────────────────────────────────────*/
|
||||
#include "libc/calls/calls.h"
|
||||
#include "libc/cosmo.h"
|
||||
#include "libc/dce.h"
|
||||
#include "libc/intrin/kprintf.h"
|
||||
#include "libc/mem/gc.h"
|
||||
#include "libc/paths.h"
|
||||
#include "libc/runtime/runtime.h"
|
||||
#include "libc/stdio/stdio.h"
|
||||
#include "libc/str/str.h"
|
||||
#include "libc/sysv/consts/o.h"
|
||||
#include "libc/sysv/consts/sig.h"
|
||||
#include "libc/testlib/ezbench.h"
|
||||
#include "libc/testlib/testlib.h"
|
||||
#include "libc/x/x.h"
|
||||
#ifdef __x86_64__
|
||||
|
||||
#define GETEXITSTATUS(x) \
|
||||
({ \
|
||||
int status_ = (x); \
|
||||
if (WIFSIGNALED(status_)) { \
|
||||
kprintf("%s:%d: %s terminated with %G\n", __FILE__, __LINE__, #x, \
|
||||
WTERMSIG(status_)); \
|
||||
exit(1); \
|
||||
} \
|
||||
WEXITSTATUS(status_); \
|
||||
})
|
||||
|
||||
__static_yoink("_tr");
|
||||
__static_yoink("glob");
|
||||
|
||||
int pipefd[2];
|
||||
int stdoutBack;
|
||||
|
||||
void SetUpOnce(void) {
|
||||
testlib_enable_tmp_setup_teardown();
|
||||
}
|
||||
|
||||
void CaptureStdout(void) {
|
||||
ASSERT_NE(-1, (stdoutBack = dup(1)));
|
||||
ASSERT_SYS(0, 0, pipe(pipefd));
|
||||
ASSERT_NE(-1, dup2(pipefd[1], 1));
|
||||
}
|
||||
|
||||
void RestoreStdout(void) {
|
||||
ASSERT_SYS(0, 1, dup2(stdoutBack, 1));
|
||||
ASSERT_SYS(0, 0, close(stdoutBack));
|
||||
ASSERT_SYS(0, 0, close(pipefd[1]));
|
||||
ASSERT_SYS(0, 0, close(pipefd[0]));
|
||||
}
|
||||
|
||||
TEST(system, haveShell) {
|
||||
ASSERT_TRUE(system(0));
|
||||
}
|
||||
|
||||
TEST(system, echo) {
|
||||
ASSERT_EQ(0, system("echo hello >\"hello there.txt\""));
|
||||
EXPECT_STREQ("hello\n", gc(xslurp("hello there.txt", 0)));
|
||||
}
|
||||
|
||||
TEST(system, exit) {
|
||||
ASSERT_EQ(123, GETEXITSTATUS(system("exit 123")));
|
||||
}
|
||||
|
||||
TEST(system, testStdoutRedirect) {
|
||||
testlib_extract("/zip/echo", "echo", 0755);
|
||||
ASSERT_EQ(0, system("./echo hello >hello.txt"));
|
||||
EXPECT_STREQ("hello\n", gc(xslurp("hello.txt", 0)));
|
||||
}
|
||||
|
||||
TEST(system, testStdoutRedirect_withSpacesInFilename) {
|
||||
testlib_extract("/zip/echo", "echo", 0755);
|
||||
ASSERT_EQ(0, system("./echo hello >\"hello there.txt\""));
|
||||
EXPECT_STREQ("hello\n", gc(xslurp("hello there.txt", 0)));
|
||||
}
|
||||
|
||||
TEST(system, testStderrRedirect_toStdout) {
|
||||
CaptureStdout();
|
||||
int stderrBack = dup(2);
|
||||
ASSERT_NE(-1, stderrBack);
|
||||
char buf[5] = {0};
|
||||
ASSERT_NE(-1, dup2(1, 2));
|
||||
bool success = false;
|
||||
if (GETEXITSTATUS(system("echo aaa 2>&1")) == 0) {
|
||||
success = read(pipefd[0], buf, 4) == (4);
|
||||
}
|
||||
ASSERT_NE(-1, dup2(stderrBack, 2));
|
||||
ASSERT_EQ(true, success);
|
||||
ASSERT_STREQ("aaa\n", buf);
|
||||
buf[0] = 0;
|
||||
buf[1] = 0;
|
||||
buf[2] = 0;
|
||||
buf[3] = 0;
|
||||
testlib_extract("/zip/echo", "echo", 0755);
|
||||
ASSERT_NE(-1, dup2(1, 2));
|
||||
success = false;
|
||||
if (GETEXITSTATUS(system("./echo aaa 2>&1")) == 0) {
|
||||
success = read(pipefd[0], buf, 4) == (4);
|
||||
}
|
||||
ASSERT_NE(-1, dup2(stderrBack, 2));
|
||||
ASSERT_EQ(true, success);
|
||||
ASSERT_STREQ("aaa\n", buf);
|
||||
RestoreStdout();
|
||||
}
|
||||
|
||||
TEST(system, and) {
|
||||
ASSERT_EQ(1, GETEXITSTATUS(system("false && false")));
|
||||
ASSERT_EQ(1, GETEXITSTATUS(system("true&& false")));
|
||||
ASSERT_EQ(1, GETEXITSTATUS(system("false &&true")));
|
||||
ASSERT_EQ(0, GETEXITSTATUS(system("true&&true")));
|
||||
}
|
||||
|
||||
TEST(system, or) {
|
||||
ASSERT_EQ(1, GETEXITSTATUS(system("false || false")));
|
||||
ASSERT_EQ(0, GETEXITSTATUS(system("true|| false")));
|
||||
ASSERT_EQ(0, GETEXITSTATUS(system("false ||true")));
|
||||
ASSERT_EQ(0, GETEXITSTATUS(system("true||true")));
|
||||
}
|
||||
|
||||
TEST(system, async1) {
|
||||
ASSERT_EQ(123, GETEXITSTATUS(system("exit 123 & "
|
||||
"wait $!")));
|
||||
}
|
||||
|
||||
TEST(system, async4) {
|
||||
ASSERT_EQ(0, GETEXITSTATUS(system("echo a >a & "
|
||||
"echo b >b & "
|
||||
"echo c >c & "
|
||||
"echo d >d & "
|
||||
"wait")));
|
||||
ASSERT_TRUE(fileexists("a"));
|
||||
ASSERT_TRUE(fileexists("b"));
|
||||
ASSERT_TRUE(fileexists("c"));
|
||||
ASSERT_TRUE(fileexists("d"));
|
||||
}
|
||||
|
||||
TEST(system, equals) {
|
||||
setenv("var", "wand", true);
|
||||
ASSERT_EQ(0, GETEXITSTATUS(system("test a = a")));
|
||||
ASSERT_EQ(1, GETEXITSTATUS(system("test a = b")));
|
||||
ASSERT_EQ(0, GETEXITSTATUS(system("test x$var = xwand")));
|
||||
ASSERT_EQ(0, GETEXITSTATUS(system("[ a = a ]")));
|
||||
ASSERT_EQ(1, GETEXITSTATUS(system("[ a = b ]")));
|
||||
}
|
||||
|
||||
TEST(system, notequals) {
|
||||
ASSERT_EQ(1, GETEXITSTATUS(system("test a != a")));
|
||||
ASSERT_EQ(0, GETEXITSTATUS(system("test a != b")));
|
||||
}
|
||||
|
||||
TEST(system, usleep) {
|
||||
ASSERT_EQ(0, GETEXITSTATUS(system("usleep & kill $!; wait")));
|
||||
}
|
||||
|
||||
TEST(system, kill) {
|
||||
int ws = system("kill -TERM $$; usleep");
|
||||
ASSERT_EQ(SIGTERM, WTERMSIG(ws));
|
||||
}
|
||||
|
||||
TEST(system, exitStatusPreservedAfterSemiColon) {
|
||||
testlib_extract("/zip/false", "false", 0755);
|
||||
ASSERT_EQ(1, GETEXITSTATUS(system("false;")));
|
||||
ASSERT_EQ(1, GETEXITSTATUS(system("false; ")));
|
||||
ASSERT_EQ(1, GETEXITSTATUS(system("./false;")));
|
||||
ASSERT_EQ(1, GETEXITSTATUS(system("./false;")));
|
||||
CaptureStdout();
|
||||
ASSERT_EQ(0, GETEXITSTATUS(system("false; echo $?")));
|
||||
char buf[9] = {0};
|
||||
ASSERT_EQ(2, read(pipefd[0], buf, 8));
|
||||
ASSERT_STREQ("1\n", buf);
|
||||
ASSERT_EQ(0, GETEXITSTATUS(system("./false; echo $?")));
|
||||
ASSERT_EQ(2, read(pipefd[0], buf, 8));
|
||||
ASSERT_STREQ("1\n", buf);
|
||||
ASSERT_EQ(0, GETEXITSTATUS(system("echo -n hi")));
|
||||
EXPECT_EQ(2, read(pipefd[0], buf, 8));
|
||||
ASSERT_STREQ("hi", buf);
|
||||
RestoreStdout();
|
||||
}
|
||||
|
||||
TEST(system, devStdout) {
|
||||
CaptureStdout();
|
||||
ASSERT_EQ(0, GETEXITSTATUS(system("echo sup >/dev/stdout")));
|
||||
char buf[16] = {0};
|
||||
ASSERT_EQ(4, read(pipefd[0], buf, 16));
|
||||
ASSERT_STREQ("sup\n", buf);
|
||||
RestoreStdout();
|
||||
}
|
||||
|
||||
TEST(system, globio) {
|
||||
char buf[9] = {0};
|
||||
CaptureStdout();
|
||||
ASSERT_SYS(0, 0, touch("a", 0644));
|
||||
ASSERT_SYS(0, 0, touch("b", 0644));
|
||||
ASSERT_EQ(0, GETEXITSTATUS(system("echo *")));
|
||||
EXPECT_EQ(4, read(pipefd[0], buf, 8));
|
||||
ASSERT_STREQ("a b\n", buf);
|
||||
RestoreStdout();
|
||||
}
|
||||
|
||||
TEST(system, allowsLoneCloseCurlyBrace) {
|
||||
CaptureStdout();
|
||||
char buf[6] = {0};
|
||||
ASSERT_EQ(0, GETEXITSTATUS(system("echo \"aaa\"}")));
|
||||
ASSERT_EQ(5, read(pipefd[0], buf, 5));
|
||||
ASSERT_STREQ("aaa}\n", buf);
|
||||
bzero(buf, 6);
|
||||
testlib_extract("/zip/echo", "echo", 0755);
|
||||
ASSERT_EQ(0, GETEXITSTATUS(system("./echo \"aaa\"}")));
|
||||
ASSERT_EQ(5, read(pipefd[0], buf, 5));
|
||||
ASSERT_STREQ("aaa}\n", buf);
|
||||
RestoreStdout();
|
||||
}
|
||||
|
||||
TEST(system, glob) {
|
||||
testlib_extract("/zip/echo", "echo", 0755);
|
||||
ASSERT_EQ(0, system("./ec* aaa"));
|
||||
ASSERT_EQ(0, system("./ec?o aaa"));
|
||||
}
|
||||
|
||||
TEST(system, env) {
|
||||
ASSERT_EQ(0, GETEXITSTATUS(system("env - a=b c=d >res")));
|
||||
ASSERT_STREQ("a=b\nc=d\n", gc(xslurp("res", 0)));
|
||||
ASSERT_EQ(0, GETEXITSTATUS(system("env -i -0 a=b c=d >res")));
|
||||
ASSERT_STREQN("a=b\0c=d\0", gc(xslurp("res", 0)), 8);
|
||||
ASSERT_EQ(0, GETEXITSTATUS(system("env -i0 a=b c=d >res")));
|
||||
ASSERT_STREQN("a=b\0c=d\0", gc(xslurp("res", 0)), 8);
|
||||
ASSERT_EQ(0, GETEXITSTATUS(system("env - a=b c=d -u a z=g >res")));
|
||||
ASSERT_STREQ("c=d\nz=g\n", gc(xslurp("res", 0)));
|
||||
ASSERT_EQ(0, GETEXITSTATUS(system("env - a=b c=d -ua z=g >res")));
|
||||
ASSERT_STREQ("c=d\nz=g\n", gc(xslurp("res", 0)));
|
||||
ASSERT_EQ(0, GETEXITSTATUS(system("env - dope='show' >res")));
|
||||
ASSERT_STREQ("dope=show\n", gc(xslurp("res", 0)));
|
||||
}
|
||||
|
||||
TEST(system, pipelineCanOutputToFile) {
|
||||
ASSERT_EQ(0, GETEXITSTATUS(system("echo hello | tr a-z A-Z >res")));
|
||||
ASSERT_STREQ("HELLO\n", gc(xslurp("res", 0)));
|
||||
testlib_extract("/zip/echo", "echo", 0755);
|
||||
ASSERT_EQ(0, GETEXITSTATUS(system("./echo hello | tr a-z A-Z >res")));
|
||||
ASSERT_STREQ("HELLO\n", gc(xslurp("res", 0)));
|
||||
}
|
||||
|
||||
TEST(system, pipelineCanOutputBackToSelf) {
|
||||
char buf[16] = {0};
|
||||
CaptureStdout();
|
||||
ASSERT_EQ(0, GETEXITSTATUS(system("echo hello | tr a-z A-Z")));
|
||||
ASSERT_SYS(0, 6, read(pipefd[0], buf, 16));
|
||||
ASSERT_STREQ("HELLO\n", buf);
|
||||
ASSERT_EQ(0, GETEXITSTATUS(system("echo hello | exec tr a-z A-Z")));
|
||||
ASSERT_SYS(0, 6, read(pipefd[0], buf, 16));
|
||||
ASSERT_STREQ("HELLO\n", buf);
|
||||
testlib_extract("/zip/echo", "echo", 0755);
|
||||
ASSERT_EQ(0, GETEXITSTATUS(system("./echo hello | tr a-z A-Z")));
|
||||
ASSERT_SYS(0, 6, read(pipefd[0], buf, 16));
|
||||
ASSERT_STREQ("HELLO\n", buf);
|
||||
ASSERT_EQ(0, GETEXITSTATUS(system("./echo hello | exec tr a-z A-Z")));
|
||||
ASSERT_SYS(0, 6, read(pipefd[0], buf, 16));
|
||||
ASSERT_STREQ("HELLO\n", buf);
|
||||
RestoreStdout();
|
||||
}
|
||||
|
||||
int system2(const char *);
|
||||
|
||||
BENCH(system, bench) {
|
||||
testlib_extract("/zip/echo", "echo", 0755);
|
||||
EZBENCH2("system cmd", donothing, system("./echo hi >/dev/null"));
|
||||
EZBENCH2("systemvpe cmd", donothing,
|
||||
systemvpe("./echo", (char *[]){"./echo", "hi", 0}, 0));
|
||||
EZBENCH2("cocmd echo", donothing, system("echo hi >/dev/null"));
|
||||
EZBENCH2("cocmd exit", donothing, system("exit"));
|
||||
}
|
||||
|
||||
#endif /* __x86_64__ */
|
Loading…
Add table
Add a link
Reference in a new issue