Skip to content

Commit af59806

Browse files
committed
Add integration test for redbean
1 parent 01e6b3a commit af59806

File tree

5 files changed

+106
-41
lines changed

5 files changed

+106
-41
lines changed

libc/fmt/pflink.h

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -18,20 +18,20 @@
1818
* format strings are constexprs that only contain directives.
1919
*/
2020

21-
#define PFLINK(FMT) \
22-
({ \
23-
if (___PFLINK(FMT, strpbrk, "faAeEgG")) STATIC_YOINK("__fmt_dtoa"); \
24-
if (___PFLINK(FMT, strpbrk, "cmrqs")) { \
25-
if (___PFLINK(FMT, strchr, '#')) STATIC_YOINK("kCp437"); \
26-
if (___PFLINK(FMT, strstr, "%m")) STATIC_YOINK("strerror"); \
27-
if (!IsTiny() && (___PFLINK(FMT, strstr, "%*") || \
28-
___PFLINK(FMT, strpbrk, "0123456789"))) { \
29-
STATIC_YOINK("strnwidth"); \
30-
STATIC_YOINK("strnwidth16"); \
31-
STATIC_YOINK("wcsnwidth"); \
32-
} \
33-
} \
34-
FMT; \
21+
#define PFLINK(FMT) \
22+
({ \
23+
if (___PFLINK(FMT, strpbrk, "faAeg")) STATIC_YOINK("__fmt_dtoa"); \
24+
if (___PFLINK(FMT, strpbrk, "cmrqs")) { \
25+
if (___PFLINK(FMT, strchr, '#')) STATIC_YOINK("kCp437"); \
26+
if (___PFLINK(FMT, strstr, "%m")) STATIC_YOINK("strerror"); \
27+
if (!IsTiny() && (___PFLINK(FMT, strstr, "%*") || \
28+
___PFLINK(FMT, strpbrk, "0123456789"))) { \
29+
STATIC_YOINK("strnwidth"); \
30+
STATIC_YOINK("strnwidth16"); \
31+
STATIC_YOINK("wcsnwidth"); \
32+
} \
33+
} \
34+
FMT; \
3535
})
3636

3737
#define SFLINK(FMT) \

libc/testlib/testlib.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ COSMOPOLITAN_C_START_
7474
* and if the test succeeds it'll be removed along with any contents.
7575
*/
7676
extern char testlib_enable_tmp_setup_teardown;
77+
extern char testlib_enable_tmp_setup_teardown_once;
7778

7879
/**
7980
* User-defined test setup function.
@@ -82,6 +83,7 @@ extern char testlib_enable_tmp_setup_teardown;
8283
* defined by the linkage.
8384
*/
8485
void SetUp(void);
86+
void SetUpOnce(void);
8587

8688
/**
8789
* User-defined test cleanup function.
@@ -90,6 +92,7 @@ void SetUp(void);
9092
* defined by the linkage.
9193
*/
9294
void TearDown(void);
95+
void TearDownOnce(void);
9396

9497
/*───────────────────────────────────────────────────────────────────────────│─╗
9598
│ cosmopolitan § testing library » assert or die ─╬─│┼

libc/testlib/testrunner.c

Lines changed: 23 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,10 @@
2929
#include "libc/testlib/testlib.h"
3030
#include "libc/x/x.h"
3131

32+
static int x;
33+
static char cwd[PATH_MAX];
34+
static char tmp[PATH_MAX];
35+
3236
void testlib_finish(void) {
3337
if (g_testlib_failed) {
3438
fprintf(stderr, "%u / %u %s\n", g_testlib_failed, g_testlib_ran,
@@ -42,6 +46,18 @@ wontreturn void testlib_abort(void) {
4246
unreachable;
4347
}
4448

49+
static void SetupTmpDir(void) {
50+
snprintf(tmp, sizeof(tmp), "o/tmp/%s.%d.%d", program_invocation_short_name,
51+
getpid(), x++);
52+
CHECK_NE(-1, makedirs(tmp, 0755), "tmp=%s", tmp);
53+
CHECK_NE(-1, chdir(tmp), "tmp=%s", tmp);
54+
}
55+
56+
static void TearDownTmpDir(void) {
57+
CHECK_NE(-1, chdir(cwd));
58+
CHECK_NE(-1, rmrf(tmp));
59+
}
60+
4561
/**
4662
* Runs all test case functions in sorted order.
4763
*/
@@ -62,18 +78,12 @@ testonly void testlib_runtestcases(testfn_t *start, testfn_t *end,
6278
*
6379
* @see ape/ape.lds
6480
*/
65-
int x;
66-
char cwd[PATH_MAX];
67-
char tmp[PATH_MAX];
6881
const testfn_t *fn;
82+
CHECK_NOTNULL(getcwd(cwd, sizeof(cwd)));
83+
if (weaken(testlib_enable_tmp_setup_teardown_once)) SetupTmpDir();
84+
if (weaken(SetUpOnce)) weaken(SetUpOnce)();
6985
for (x = 0, fn = start; fn != end; ++fn) {
70-
if (weaken(testlib_enable_tmp_setup_teardown)) {
71-
CHECK_NOTNULL(getcwd(cwd, sizeof(cwd)));
72-
snprintf(tmp, sizeof(tmp), "o/tmp/%s.%d.%d",
73-
program_invocation_short_name, getpid(), x++);
74-
CHECK_NE(-1, makedirs(tmp, 0755), "tmp=%s", tmp);
75-
CHECK_NE(-1, chdir(tmp), "tmp=%s", tmp);
76-
}
86+
if (weaken(testlib_enable_tmp_setup_teardown)) SetupTmpDir();
7787
if (weaken(SetUp)) weaken(SetUp)();
7888
errno = 0;
7989
SetLastError(0);
@@ -83,9 +93,8 @@ testonly void testlib_runtestcases(testfn_t *start, testfn_t *end,
8393
(*fn)();
8494
sys_getpid();
8595
if (weaken(TearDown)) weaken(TearDown)();
86-
if (weaken(testlib_enable_tmp_setup_teardown)) {
87-
CHECK_NE(-1, chdir(cwd));
88-
CHECK_NE(-1, rmrf(tmp));
89-
}
96+
if (weaken(testlib_enable_tmp_setup_teardown)) TearDownTmpDir();
9097
}
98+
if (weaken(TearDownOnce)) weaken(TearDownOnce)();
99+
if (weaken(testlib_enable_tmp_setup_teardown_once)) TearDownTmpDir();
91100
}

test/tool/net/redbean_test.c

Lines changed: 56 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,20 +19,30 @@
1919
#include "libc/calls/calls.h"
2020
#include "libc/calls/sigbits.h"
2121
#include "libc/fmt/conv.h"
22+
#include "libc/log/check.h"
2223
#include "libc/runtime/gc.internal.h"
2324
#include "libc/runtime/runtime.h"
25+
#include "libc/sock/sock.h"
2426
#include "libc/stdio/stdio.h"
27+
#include "libc/sysv/consts/af.h"
2528
#include "libc/sysv/consts/auxv.h"
29+
#include "libc/sysv/consts/inaddr.h"
30+
#include "libc/sysv/consts/ipproto.h"
2631
#include "libc/sysv/consts/o.h"
32+
#include "libc/sysv/consts/shut.h"
2733
#include "libc/sysv/consts/sig.h"
34+
#include "libc/sysv/consts/sock.h"
2835
#include "libc/testlib/testlib.h"
2936
#include "libc/x/x.h"
37+
#include "third_party/regex/regex.h"
3038

3139
STATIC_YOINK("zip_uri_support");
3240
STATIC_YOINK("o/" MODE "/tool/net/redbean.com");
33-
char testlib_enable_tmp_setup_teardown;
41+
char testlib_enable_tmp_setup_teardown_once;
42+
int port;
3443

35-
void SetUp(void) {
44+
void SetUpOnce(void) {
45+
if (IsWindows()) return;
3646
ssize_t n;
3747
char buf[1024];
3848
int fdin, fdout;
@@ -48,9 +58,41 @@ void SetUp(void) {
4858
close(fdin);
4959
}
5060

51-
TEST(redbean, test) {
61+
char *SendHttpRequest(const char *s) {
62+
int fd;
63+
char *p;
64+
size_t n;
65+
ssize_t rc;
66+
struct sockaddr_in addr = {AF_INET, htons(port), {htonl(INADDR_LOOPBACK)}};
67+
EXPECT_NE(-1, (fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)));
68+
EXPECT_NE(-1, connect(fd, &addr, sizeof(addr)));
69+
n = strlen(s);
70+
EXPECT_EQ(n, write(fd, s, n));
71+
shutdown(fd, SHUT_WR);
72+
for (p = 0, n = 0;; n += rc) {
73+
p = xrealloc(p, n + 512);
74+
EXPECT_NE(-1, (rc = read(fd, p + n, 512)));
75+
if (rc <= 0) break;
76+
}
77+
p = xrealloc(p, n + 1);
78+
p[n] = 0;
79+
close(fd);
80+
return p;
81+
}
82+
83+
bool Matches(const char *regex, const char *str) {
84+
bool r;
85+
regex_t re;
86+
CHECK_EQ(REG_OK, regcomp(&re, regex, 0));
87+
r = regexec(&re, str, 0, 0, 0) == REG_OK;
88+
regfree(&re);
89+
return r;
90+
}
91+
92+
TEST(redbean, testOptions) {
93+
if (IsWindows()) return;
5294
char portbuf[16];
53-
int pid, port, pipefds[2];
95+
int pid, pipefds[2];
5496
sigset_t chldmask, savemask;
5597
sigaddset(&chldmask, SIGCHLD);
5698
sigprocmask(SIG_BLOCK, &chldmask, &savemask);
@@ -61,14 +103,21 @@ TEST(redbean, test) {
61103
dup2(pipefds[1], 1);
62104
sigprocmask(SIG_SETMASK, &savemask, NULL);
63105
execv("bin/redbean.com",
64-
(char *const[]){"bin/redbean.com", "-zp0", "-l127.0.0.1", 0});
106+
(char *const[]){"bin/redbean.com", "-szp0", "-l127.0.0.1", 0});
65107
_exit(127);
66108
}
67109
EXPECT_NE(-1, close(pipefds[1]));
68110
EXPECT_NE(-1, read(pipefds[0], portbuf, sizeof(portbuf)));
69111
port = atoi(portbuf);
70-
printf("port %d\n", port);
71-
fflush(stdout);
112+
EXPECT_TRUE(Matches("HTTP/1\\.1 200 OK\r\n"
113+
"Accept: \\*/\\*\r\n"
114+
"Accept-Charset: utf-8,ISO-8859-1;q=0\\.7,\\*;q=0\\.5\r\n"
115+
"Allow: GET, HEAD, POST, PUT, DELETE, OPTIONS\r\n"
116+
"Date: .*\r\n"
117+
"Server: redbean/0\\.4\r\n"
118+
"Content-Length: 0\r\n"
119+
"\r\n",
120+
gc(SendHttpRequest("OPTIONS * HTTP/1.1\n\n"))));
72121
EXPECT_NE(-1, kill(pid, SIGTERM));
73122
EXPECT_NE(-1, wait(0));
74123
}

test/tool/net/test.mk

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,17 +29,21 @@ TEST_TOOL_NET_CHECKS = \
2929

3030
TEST_TOOL_NET_DIRECTDEPS = \
3131
LIBC_CALLS \
32-
LIBC_STUBS \
3332
LIBC_FMT \
34-
LIBC_STDIO \
3533
LIBC_INTRIN \
36-
LIBC_NEXGEN32E \
37-
LIBC_SYSV \
34+
LIBC_LOG \
3835
LIBC_MEM \
36+
LIBC_NEXGEN32E \
3937
LIBC_RUNTIME \
40-
LIBC_X \
38+
LIBC_SOCK \
39+
LIBC_STDIO \
40+
LIBC_STR \
41+
LIBC_STUBS \
42+
LIBC_SYSV \
4143
LIBC_TESTLIB \
42-
LIBC_ZIPOS
44+
LIBC_X \
45+
LIBC_ZIPOS \
46+
THIRD_PARTY_REGEX
4347

4448
TEST_TOOL_NET_DEPS := \
4549
$(call uniq,$(foreach x,$(TEST_TOOL_NET_DIRECTDEPS),$($(x))))

0 commit comments

Comments
 (0)