Add a minimal regress test for the http client code.
authorclaudio <claudio@openbsd.org>
Fri, 9 Apr 2021 10:14:36 +0000 (10:14 +0000)
committerclaudio <claudio@openbsd.org>
Fri, 9 Apr 2021 10:14:36 +0000 (10:14 +0000)
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.

regress/usr.sbin/rpki-client/libressl/Makefile
regress/usr.sbin/rpki-client/libressl/test-http.sum [new file with mode: 0644]
regress/usr.sbin/rpki-client/test-http.c [new file with mode: 0644]

index 38c2a35..67060e5 100644 (file)
@@ -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 <bsd.regress.mk>
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 (file)
index 0000000..0d07085
--- /dev/null
@@ -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 (file)
index 0000000..b9cb26b
--- /dev/null
@@ -0,0 +1,126 @@
+#include <sys/queue.h>
+#include <sys/socket.h>
+#include <err.h>
+#include <errno.h>
+#include <fcntl.h>
+#include <limits.h>
+#include <stdarg.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+
+#include <imsg.h>
+
+#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;
+}