cosmopolitan/test/tool/net/redbean_test.c

291 lines
9.8 KiB
C
Raw Normal View History

/*-*- 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 2021 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/sigset.h"
#include "libc/dce.h"
#include "libc/fmt/conv.h"
#include "libc/mem/gc.internal.h"
#include "libc/runtime/runtime.h"
2021-08-14 13:17:56 +00:00
#include "libc/sock/goodsocket.internal.h"
2021-05-03 08:59:27 +00:00
#include "libc/sock/sock.h"
Prove that Makefile is fully defined The whole repository is now buildable with GNU Make Landlock sandboxing. This proves that no Makefile targets exist which touch files other than their declared prerequisites. In order to do this, we had to: 1. Stop code morphing GCC output in package.com and instead run a newly introduced FIXUPOBJ.COM command after GCC invocations. 2. Disable all the crumby Python unit tests that do things like create files in the current directory, or rename() files between folders. This ended up being a lot of tests, but most of them are still ok. 3. Introduce an .UNSANDBOXED variable to GNU Make to disable Landlock. We currently only do this for things like `make tags`. 4. This change deletes some GNU Make code that was preventing the execve() optimization from working. This means it should no longer be necessary in most cases for command invocations to be indirected through the cocmd interpreter. 5. Missing dependencies had to be declared in certain places, in cases where they couldn't be automatically determined by MKDEPS.COM 6. The libcxx header situation has finally been tamed. One of the things that makes this difficult is MKDEPS.COM only wants to consider the first 64kb of a file, in order to go fast. But libcxx likes to have #include lines buried after huge documentation. 7. An .UNVEIL variable has been introduced to GNU Make just in case we ever wish to explicitly specify additional things that need to be whitelisted which aren't strictly prerequisites. This works in a manner similar to the recently introduced .EXTRA_PREREQS feature. There's now a new build/bootstrap/make.com prebuilt binary available. It should no longer be possible to write invalid Makefile code.
2022-08-06 10:51:50 +00:00
#include "libc/sock/struct/sockaddr.h"
#include "libc/stdio/stdio.h"
#include "libc/str/str.h"
2021-05-03 08:59:27 +00:00
#include "libc/sysv/consts/af.h"
#include "libc/sysv/consts/auxv.h"
2021-05-03 08:59:27 +00:00
#include "libc/sysv/consts/inaddr.h"
#include "libc/sysv/consts/ipproto.h"
#include "libc/sysv/consts/o.h"
2021-05-03 08:59:27 +00:00
#include "libc/sysv/consts/shut.h"
#include "libc/sysv/consts/sig.h"
2021-05-03 08:59:27 +00:00
#include "libc/sysv/consts/sock.h"
2021-05-05 14:25:39 +00:00
#include "libc/sysv/consts/tcp.h"
#include "libc/testlib/testlib.h"
#include "libc/x/x.h"
2021-05-03 08:59:27 +00:00
#include "third_party/regex/regex.h"
#ifdef __x86_64__
STATIC_YOINK("zipos");
STATIC_YOINK("o/" MODE "/test/tool/net/redbean-tester.com");
2021-05-03 08:59:27 +00:00
char testlib_enable_tmp_setup_teardown_once;
int port;
2021-05-03 08:59:27 +00:00
void SetUpOnce(void) {
if (IsWindows()) return;
ssize_t n;
2021-05-03 08:21:50 +00:00
char buf[1024];
int fdin, fdout;
ASSERT_NE(-1, mkdir("bin", 0755));
ASSERT_NE(-1, (fdin = open("/zip/o/" MODE "/test/tool/net/redbean-tester.com",
O_RDONLY)));
ASSERT_NE(-1, (fdout = creat("bin/redbean-tester.com", 0755)));
for (;;) {
ASSERT_NE(-1, (n = read(fdin, buf, sizeof(buf))));
if (!n) break;
2021-05-03 08:21:50 +00:00
ASSERT_EQ(n, write(fdout, buf, n));
}
close(fdout);
close(fdin);
}
2021-05-05 14:25:39 +00:00
int Socket(void) {
2021-08-14 13:17:56 +00:00
return GoodSocket(AF_INET, SOCK_STREAM, IPPROTO_TCP, false, 0);
2021-05-05 14:25:39 +00:00
}
2021-05-03 08:59:27 +00:00
char *SendHttpRequest(const char *s) {
int fd;
char *p;
size_t n;
ssize_t rc;
struct sockaddr_in addr = {AF_INET, htons(port), {htonl(INADDR_LOOPBACK)}};
2021-05-05 14:25:39 +00:00
EXPECT_NE(-1, (fd = Socket()));
EXPECT_NE(-1, connect(fd, (struct sockaddr *)&addr, sizeof(addr)));
2021-05-03 08:59:27 +00:00
n = strlen(s);
EXPECT_EQ(n, write(fd, s, n));
shutdown(fd, SHUT_WR);
for (p = 0, n = 0;; n += rc) {
p = xrealloc(p, n + 512);
EXPECT_NE(-1, (rc = read(fd, p + n, 512)));
if (rc <= 0) break;
}
p = xrealloc(p, n + 1);
p[n] = 0;
close(fd);
return p;
}
bool Matches(const char *regex, const char *str) {
bool r;
regex_t re;
printf("%s\n", str);
EXPECT_EQ(REG_OK, regcomp(&re, regex, 0));
2021-05-03 08:59:27 +00:00
r = regexec(&re, str, 0, 0, 0) == REG_OK;
regfree(&re);
return r;
}
TEST(redbean, testOptions) {
if (IsWindows()) return;
char portbuf[16];
2021-05-03 08:59:27 +00:00
int pid, pipefds[2];
sigset_t chldmask, savemask;
sigaddset(&chldmask, SIGCHLD);
EXPECT_NE(-1, sigprocmask(SIG_BLOCK, &chldmask, &savemask));
2021-05-03 08:21:50 +00:00
ASSERT_NE(-1, pipe(pipefds));
Add SSL to redbean Your redbean can now interoperate with clients that require TLS crypto. This is accomplished using a protocol polyglot that lets us distinguish between HTTP and HTTPS regardless of the port number. Certificates will be generated automatically, if none are supplied by the user. Footprint increases by only a few hundred kb so redbean in MODY=tiny is now 1.0mb - Add lseek() polyfills for ZIP executable - Automatically polyfill /tmp/FOO paths on NT - Fix readdir() / ftw() / nftw() bugs on Windows - Introduce -B flag for slower SSL that's stronger - Remove mbedtls features Cosmopolitan doesn't need - Have base64 decoder support the uri-safe alternative - Remove Truncated HMAC because it's forbidden by the IETF - Add all the mbedtls test suites and make them go 3x faster - Support opendir() / readdir() / closedir() on ZIP executable - Use Everest for ECDHE-ECDSA because it's so good it's so good - Add tinier implementation of sha1 since it's not worth the rom - Add chi-square monte-carlo mean correlation tests for getrandom() - Source entropy on Windows from the proper interface everyone uses We're continuing to outperform NGINX and other servers on raw message throughput. Using SSL means that instead of 1,000,000 qps you can get around 300,000 qps. However redbean isn't as fast as NGINX yet at SSL handshakes, since redbean can do 2,627 per second and NGINX does 4.3k Right now, the SSL UX story works best if you give your redbean a key signing key since that can be easily generated by openssl using a one liner then redbean will do all the things that are impossibly hard to do like signing ecdsa and rsa certificates that'll work in chrome. We should integrate the let's encrypt acme protocol in the future. Live Demo: https://redbean.justine.lol/ Root Cert: https://redbean.justine.lol/redbean1.crt
2021-06-24 19:31:26 +00:00
ASSERT_NE(-1, (pid = fork()));
if (!pid) {
setpgrp();
close(0);
open("/dev/null", O_RDWR);
close(1);
2021-05-03 08:21:50 +00:00
close(pipefds[0]);
dup2(pipefds[1], 1);
sigprocmask(SIG_SETMASK, &savemask, NULL);
execv("bin/redbean-tester.com",
(char *const[]){"bin/redbean-tester.com", "-vvszXp0", "-l127.0.0.1",
__strace > 0 ? "--strace" : 0, 0});
_exit(127);
}
EXPECT_NE(-1, close(pipefds[1]));
EXPECT_NE(-1, read(pipefds[0], portbuf, sizeof(portbuf)));
port = atoi(portbuf);
2021-05-03 08:59:27 +00:00
EXPECT_TRUE(Matches("HTTP/1\\.1 200 OK\r\n"
"Accept: \\*/\\*\r\n"
"Accept-Charset: utf-8,ISO-8859-1;q=0\\.7,\\*;q=0\\.5\r\n"
"Allow: GET, HEAD, POST, PUT, DELETE, OPTIONS\r\n"
"Date: .*\r\n"
2021-05-05 14:25:39 +00:00
"Server: redbean/.*\r\n"
2021-05-03 08:59:27 +00:00
"Content-Length: 0\r\n"
"\r\n",
gc(SendHttpRequest("OPTIONS * HTTP/1.1\n\n"))));
EXPECT_EQ(0, close(pipefds[0]));
EXPECT_NE(-1, kill(pid, SIGTERM));
EXPECT_NE(-1, wait(0));
EXPECT_NE(-1, sigprocmask(SIG_SETMASK, &savemask, 0));
}
2021-05-05 14:25:39 +00:00
TEST(redbean, testPipeline) {
if (IsWindows()) return;
char portbuf[16];
int pid, pipefds[2];
sigset_t chldmask, savemask;
sigaddset(&chldmask, SIGCHLD);
EXPECT_NE(-1, sigprocmask(SIG_BLOCK, &chldmask, &savemask));
2021-05-05 14:25:39 +00:00
ASSERT_NE(-1, pipe(pipefds));
Add SSL to redbean Your redbean can now interoperate with clients that require TLS crypto. This is accomplished using a protocol polyglot that lets us distinguish between HTTP and HTTPS regardless of the port number. Certificates will be generated automatically, if none are supplied by the user. Footprint increases by only a few hundred kb so redbean in MODY=tiny is now 1.0mb - Add lseek() polyfills for ZIP executable - Automatically polyfill /tmp/FOO paths on NT - Fix readdir() / ftw() / nftw() bugs on Windows - Introduce -B flag for slower SSL that's stronger - Remove mbedtls features Cosmopolitan doesn't need - Have base64 decoder support the uri-safe alternative - Remove Truncated HMAC because it's forbidden by the IETF - Add all the mbedtls test suites and make them go 3x faster - Support opendir() / readdir() / closedir() on ZIP executable - Use Everest for ECDHE-ECDSA because it's so good it's so good - Add tinier implementation of sha1 since it's not worth the rom - Add chi-square monte-carlo mean correlation tests for getrandom() - Source entropy on Windows from the proper interface everyone uses We're continuing to outperform NGINX and other servers on raw message throughput. Using SSL means that instead of 1,000,000 qps you can get around 300,000 qps. However redbean isn't as fast as NGINX yet at SSL handshakes, since redbean can do 2,627 per second and NGINX does 4.3k Right now, the SSL UX story works best if you give your redbean a key signing key since that can be easily generated by openssl using a one liner then redbean will do all the things that are impossibly hard to do like signing ecdsa and rsa certificates that'll work in chrome. We should integrate the let's encrypt acme protocol in the future. Live Demo: https://redbean.justine.lol/ Root Cert: https://redbean.justine.lol/redbean1.crt
2021-06-24 19:31:26 +00:00
ASSERT_NE(-1, (pid = fork()));
2021-05-05 14:25:39 +00:00
if (!pid) {
setpgrp();
close(0);
open("/dev/null", O_RDWR);
2021-05-05 14:25:39 +00:00
close(pipefds[0]);
dup2(pipefds[1], 1);
sigprocmask(SIG_SETMASK, &savemask, NULL);
execv("bin/redbean-tester.com",
(char *const[]){"bin/redbean-tester.com", "-vvszXp0", "-l127.0.0.1",
__strace > 0 ? "--strace" : 0, 0});
2021-05-05 14:25:39 +00:00
_exit(127);
}
EXPECT_NE(-1, close(pipefds[1]));
EXPECT_NE(-1, read(pipefds[0], portbuf, sizeof(portbuf)));
port = atoi(portbuf);
EXPECT_TRUE(Matches("HTTP/1\\.1 200 OK\r\n"
"Accept: \\*/\\*\r\n"
"Accept-Charset: utf-8,ISO-8859-1;q=0\\.7,\\*;q=0\\.5\r\n"
"Allow: GET, HEAD, POST, PUT, DELETE, OPTIONS\r\n"
"Date: .*\r\n"
"Server: redbean/.*\r\n"
"Content-Length: 0\r\n"
"\r\n"
"HTTP/1\\.1 200 OK\r\n"
"Accept: \\*/\\*\r\n"
"Accept-Charset: utf-8,ISO-8859-1;q=0\\.7,\\*;q=0\\.5\r\n"
"Allow: GET, HEAD, POST, PUT, DELETE, OPTIONS\r\n"
"Date: .*\r\n"
"Server: redbean/.*\r\n"
"Content-Length: 0\r\n"
"\r\n",
gc(SendHttpRequest("OPTIONS * HTTP/1.1\n\n"
"OPTIONS * HTTP/1.1\n\n"))));
EXPECT_EQ(0, close(pipefds[0]));
2021-05-05 14:25:39 +00:00
EXPECT_NE(-1, kill(pid, SIGTERM));
EXPECT_NE(-1, wait(0));
EXPECT_NE(-1, sigprocmask(SIG_SETMASK, &savemask, 0));
}
TEST(redbean, testContentRange) {
if (IsWindows()) return;
char portbuf[16];
int pid, pipefds[2];
sigset_t chldmask, savemask;
sigaddset(&chldmask, SIGCHLD);
EXPECT_NE(-1, sigprocmask(SIG_BLOCK, &chldmask, &savemask));
ASSERT_NE(-1, pipe(pipefds));
ASSERT_NE(-1, (pid = fork()));
if (!pid) {
setpgrp();
close(0);
open("/dev/null", O_RDWR);
close(pipefds[0]);
dup2(pipefds[1], 1);
sigprocmask(SIG_SETMASK, &savemask, NULL);
execv("bin/redbean-tester.com",
(char *const[]){"bin/redbean-tester.com", "-vvszXp0", "-l127.0.0.1",
__strace > 0 ? "--strace" : 0, 0});
_exit(127);
}
EXPECT_NE(-1, close(pipefds[1]));
EXPECT_NE(-1, read(pipefds[0], portbuf, sizeof(portbuf)));
port = atoi(portbuf);
EXPECT_TRUE(Matches("\
HTTP/1.1 206 Partial Content\r\n\
Content-Range: bytes 18-21/52\r\n\
Content-Type: text/plain; charset=utf-8\r\n\
Vary: Accept-Encoding\r\n\
Last-Modified: .*\r\n\
Accept-Ranges: bytes\r\n\
X-Content-Type-Options: nosniff\r\n\
Date: .*\r\n\
Server: redbean/.*\r\n\
Content-Length: 4\r\n\
\r\n\
J\n\
K\n",
gc(SendHttpRequest("GET /seekable.txt HTTP/1.1\r\n"
"Range: bytes=18-21\r\n"
"\r\n"))));
EXPECT_TRUE(Matches("\
HTTP/1.1 416 Range Not Satisfiable\r\n\
Content-Range: bytes \\*/52\r\n\
Content-Type: text/plain; charset=utf-8\r\n\
Vary: Accept-Encoding\r\n\
Last-Modified: .*\r\n\
Accept-Ranges: bytes\r\n\
X-Content-Type-Options: nosniff\r\n\
Date: .*\r\n\
Server: redbean/.*\r\n\
Content-Length: 0\r\n\
\r\n",
gc(SendHttpRequest("GET /seekable.txt HTTP/1.1\r\n"
"Range: bytes=-18-21\r\n"
"\r\n"))));
EXPECT_TRUE(Matches("\
2022-11-07 14:31:30 +00:00
HTTP/1.1 206 Partial Content\r\n\
Content-Range: bytes 18-51/52\r\n\
Content-Type: text/plain; charset=utf-8\r\n\
Vary: Accept-Encoding\r\n\
Last-Modified: .*\r\n\
Accept-Ranges: bytes\r\n\
X-Content-Type-Options: nosniff\r\n\
Date: .*\r\n\
2022-11-07 14:31:30 +00:00
Server: redbean/2.2.0\r\n\
Content-Length: 34\r\n\
\r\n\
J\n\
K\n\
L\n\
M\n\
N\n\
O\n\
P\n\
Q\n\
R\n\
S\n\
T\n\
U\n\
V\n\
W\n\
X\n\
Y\n\
Z\n",
gc(SendHttpRequest("GET /seekable.txt HTTP/1.1\r\n"
"Range: bytes=18-60\r\n"
"\r\n"))));
EXPECT_EQ(0, close(pipefds[0]));
EXPECT_NE(-1, kill(pid, SIGTERM));
EXPECT_NE(-1, wait(0));
EXPECT_NE(-1, sigprocmask(SIG_SETMASK, &savemask, 0));
2021-05-05 14:25:39 +00:00
}
#endif /* __x86_64__ */