Add some tests for execve()

This commit is contained in:
Justine Tunney 2022-10-02 14:58:14 -07:00
parent 2526a9b8c7
commit daca5499b9
No known key found for this signature in database
GPG key ID: BE714B4575D6E328
6 changed files with 81 additions and 14 deletions

View file

@ -0,0 +1,60 @@
/*-*- 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 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/fmt/conv.h"
#include "libc/fmt/itoa.h"
#include "libc/runtime/runtime.h"
#include "libc/str/str.h"
#include "libc/testlib/subprocess.h"
#include "libc/testlib/testlib.h"
#define N 127
char *GenBuf(char buf[8], int x) {
int i;
bzero(buf, 8);
for (i = 0; i < 7; ++i) {
buf[i] = x & 127; // nt doesn't respect invalid unicode?
x >>= 1;
}
return buf;
}
__attribute__((__constructor__)) static void init(void) {
char buf[8];
if (__argc == 4 && !strcmp(__argv[1], "-")) {
ASSERT_STREQ(GenBuf(buf, atoi(__argv[2])), __argv[3]);
exit(0);
}
}
TEST(execve, testArgPassing) {
int i;
char ibuf[12], buf[8];
for (i = 0; i < N; ++i) {
FormatInt32(ibuf, i);
GenBuf(buf, i);
SPAWN(vfork);
execve(GetProgramExecutableName(),
(char *const[]){GetProgramExecutableName(), "-", ibuf, buf, 0},
(char *const[]){0});
notpossible;
EXITS(0);
}
}

View file

@ -100,6 +100,6 @@ TEST(mkntcmdline, fixAsBestAsWeCanForNow2) {
TEST(mkntcmdline, testWut) {
char *argv[] = {"redbean.com", "--strace", NULL};
EXPECT_NE(-1, mkntcmdline(cmdline, "C:\\Users\\jart\\redbean.com", argv));
EXPECT_STREQ(u"C:\\Users\\jart\\redbean.com --strace", cmdline);
EXPECT_NE(-1, mkntcmdline(cmdline, "C:\\Users\\jart\\𝑟𝑒𝑑𝑏𝑒𝑎𝑛.com", argv));
EXPECT_STREQ(u"C:\\Users\\jart\\𝑟𝑒𝑑𝑏𝑒𝑎𝑛.com --strace", cmdline);
}

View file

@ -175,11 +175,11 @@ TEST(GetDosArgv, cmdToil) {
size_t size = ARG_MAX / 2;
char *buf = malloc(size * sizeof(char));
char **argv = malloc(max * sizeof(char *));
EXPECT_EQ(3, GetDosArgv(u"cmd.exe /C \"echo hi >\"\"\"foo bar.txt\"\"\"\"",
EXPECT_EQ(3, GetDosArgv(u"cmd.exe /C \"echo hi >\"\"\"𝑓𝑜𝑜 bar.txt\"\"\"\"",
buf, size, argv, max));
EXPECT_STREQ("cmd.exe", argv[0]);
EXPECT_STREQ("/C", argv[1]);
EXPECT_STREQ("echo hi >\"foo bar.txt\"", argv[2]);
EXPECT_STREQ("echo hi >\"𝑓𝑜𝑜 bar.txt\"", argv[2]);
EXPECT_EQ(NULL, argv[3]);
free(argv);
free(buf);

View file

@ -16,13 +16,14 @@
TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
PERFORMANCE OF THIS SOFTWARE.
*/
#include "libc/errno.h"
#include "libc/intrin/bits.h"
#include "libc/log/check.h"
#include "libc/math.h"
#include "libc/nexgen32e/x86feature.h"
#include "libc/runtime/runtime.h"
#include "libc/stdio/lcg.internal.h"
#include "libc/stdio/rand.h"
#include "libc/runtime/runtime.h"
#include "libc/stdio/stdio.h"
#include "libc/sysv/consts/grnd.h"
#include "libc/testlib/ezbench.h"
@ -241,3 +242,7 @@ TEST(getrandom, sanityTest) {
}
}
}
TEST(getrandom, badflags_einval) {
ASSERT_SYS(EINVAL, -1, getrandom(0, 0, -1));
}