From: claudio Date: Fri, 9 Apr 2021 10:14:36 +0000 (+0000) Subject: Add a minimal regress test for the http client code. X-Git-Url: http://artulab.com/gitweb/?a=commitdiff_plain;h=e3a286de3baa362b1033ca4dd1d9c9634f34f428;p=openbsd Add a minimal regress test for the http client code. This currently uses some external website to do redirect test and to check both regular and chunked downloads. Only for libressl because you can't mix openssl 1.1 and libtls on OpenBSD. --- diff --git a/regress/usr.sbin/rpki-client/libressl/Makefile b/regress/usr.sbin/rpki-client/libressl/Makefile index 38c2a357fea..67060e5eddc 100644 --- a/regress/usr.sbin/rpki-client/libressl/Makefile +++ b/regress/usr.sbin/rpki-client/libressl/Makefile @@ -1,3 +1,16 @@ +PROGS += test-http + +SRCS_test-http= test-http.c http.c io.c +LDADD_test-http+= -ltls -lssl -lcrypto -lutil +DPADD_test-http+= ${LIBTLS} ${LIBSSL} ${LIBCRYPTO} ${LIBUTIL} + +run-regress-test-http: test-http + ./test-http https://jigsaw.w3.org/HTTP/TE/foo.txt foo.txt + ./test-http https://jigsaw.w3.org/HTTP/ChunkedScript chunk.out + ./test-http https://jigsaw.w3.org/HTTP/300/307.html redir.out + sha256 -c ${.CURDIR}/test-http.sum + + .PATH: ${.CURDIR}/.. .include diff --git a/regress/usr.sbin/rpki-client/libressl/test-http.sum b/regress/usr.sbin/rpki-client/libressl/test-http.sum new file mode 100644 index 00000000000..0d070856e88 --- /dev/null +++ b/regress/usr.sbin/rpki-client/libressl/test-http.sum @@ -0,0 +1,3 @@ +SHA256 (foo.txt) = ae00c14e295363c2920a546e6d4ef87726c751a2a0dbc1ee1a61f33aaffbc918 +SHA256 (chunk.out) = e11db73067049905effd4c0ac3c0e81be12d479eefbe3738d96ef749223f64d0 +SHA256 (redir.out) = ed6d07d6dad8a6b1a85bb3210dfe16fbd48bd025fdc04d89cd6d2942c5655550 diff --git a/regress/usr.sbin/rpki-client/test-http.c b/regress/usr.sbin/rpki-client/test-http.c new file mode 100644 index 00000000000..b9cb26bcf59 --- /dev/null +++ b/regress/usr.sbin/rpki-client/test-http.c @@ -0,0 +1,126 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include + +#include "extern.h" + +static struct msgbuf httpq; + +void +logx(const char *fmt, ...) +{ + va_list ap; + + va_start(ap, fmt); + vwarnx(fmt, ap); + va_end(ap); +} + +static void +http_request(size_t id, const char *uri, const char *last_mod, int fd) +{ + struct ibuf *b; + + if ((b = ibuf_dynamic(256, UINT_MAX)) == NULL) + err(1, NULL); + io_simple_buffer(b, &id, sizeof(id)); + io_str_buffer(b, uri); + io_str_buffer(b, last_mod); + /* pass file as fd */ + b->fd = fd; + ibuf_close(&httpq, b); +} + +static const char * +http_result(enum http_result res) +{ + switch (res) { + case HTTP_OK: + return "OK"; + case HTTP_NOT_MOD: + return "not modified"; + case HTTP_FAILED: + return "failed"; + default: + errx(1, "unknown http result: %d", res); + } +} + +static void +http_response(int fd) +{ + size_t id; + enum http_result res; + char *lastmod; + + io_simple_read(fd, &id, sizeof(id)); + io_simple_read(fd, &res, sizeof(res)); + io_str_read(fd, &lastmod); + + printf("transfer %s", http_result(res)); + if (lastmod) + printf(", last-modified: %s" , lastmod); + printf("\n"); +} + +int +main(int argc, char **argv) +{ + pid_t httppid; + int fd[2], outfd, http; + int fl = SOCK_STREAM | SOCK_CLOEXEC; + char *uri, *file, *mod; + size_t req = 0; + + if (argc != 3 && argc != 4) { + fprintf(stderr, "usage: test-http uri file [last-modified]\n"); + return 1; + } + uri = argv[1]; + file = argv[2]; + mod = argv[3]; + + if (socketpair(AF_UNIX, fl, 0, fd) == -1) + err(1, "socketpair"); + + if ((httppid = fork()) == -1) + err(1, "fork"); + + if (httppid == 0) { + close(fd[1]); + + if (pledge("stdio rpath inet dns recvfd", NULL) == -1) + err(1, "pledge"); + + proc_http(NULL, fd[0]); + errx(1, "http process returned"); + } + + close(fd[0]); + http = fd[1]; + msgbuf_init(&httpq); + httpq.fd = http; + + if ((outfd = open(file, O_WRONLY|O_CREAT|O_TRUNC, 0666)) == -1) + err(1, "open %s", file); + + http_request(req++, uri, mod, outfd); + switch (msgbuf_write(&httpq)) { + case 0: + errx(1, "write: connection closed"); + case -1: + err(1, "write"); + } + http_response(http); + + return 0; +}