ksgi
1/* $Id$ */
2/*
3* Copyright (c) 2020 Kristaps Dzonsons <kristaps@bsd.lv>
4*
5* Permission to use, copy, modify, and distribute this software for any
6* purpose with or without fee is hereby granted, provided that the above
7* copyright notice and this permission notice appear in all copies.
8*
9* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16*/
17#include "../config.h"18
19#if HAVE_ERR20# include <err.h>21#endif22#include <stdarg.h>23#include <stdint.h>24#include <stdlib.h>25#include <string.h>26#include <unistd.h>27
28#include <curl/curl.h>29
30#include "../kcgi.h"31#include "regress.h"32
33static size_t34bufcb(void *contents, size_t sz, size_t nm, void *dat)35{
36struct kcgi_buf *buf = dat;37
38if (kcgi_buf_write(contents, nm * sz, buf) != KCGI_OK)39return 0;40return nm * sz;41}
42
43static int44parent(CURL *curl)45{
46struct kcgi_buf buf;47int rc;48unsigned char want[] = {49'a', 'b', 'c',50'-', '1', '2',51'a', 'b', 'c',520, 10,53/* We'll lose bits, but that's the point. */54(unsigned char)256, (unsigned char)257,550, 1, 256};57
58memset(&buf, 0, sizeof(struct kcgi_buf));59
60curl_easy_setopt(curl, CURLOPT_URL,61"http://localhost:17123/index");62curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, bufcb);63curl_easy_setopt(curl, CURLOPT_WRITEDATA, &buf);64if (curl_easy_perform(curl) != CURLE_OK)65return 0;66
67/* Check same string and NUL terminated. */68
69rc = buf.sz == sizeof(want) &&70memcmp(buf.buf, want, buf.sz) == 0 &&71buf.buf[buf.sz] == '\0';72
73free(buf.buf);74return rc;75}
76
77static int78child(void)79{
80struct kreq r;81const char *page[] = { "index" };82char bbuf[3] = { 0, 1, 2 };83int rc = 0;84
85if (khttp_parse(&r, NULL, 0, page, 1, 0) != KCGI_OK) {86warnx("khttp_parse");87return 0;88} else if (r.page != 0)89goto out;90
91/* Setup. */92
93if (khttp_head(&r, kresps[KRESP_STATUS],94"%s", khttps[KHTTP_200]) != KCGI_OK) {95warnx("khttp_head");96goto out;97} else if (khttp_body(&r) != KCGI_OK) {98warnx("khttp_body");99goto out;100}101
102/* Tests. */103
104if (khttp_puts(&r, "abc") != KCGI_OK) {105warnx("khttp_puts");106goto out;107} else if (khttp_puts(&r, NULL) != KCGI_OK) {108warnx("khttp_puts");109goto out;110} else if (khttp_puts(&r, "") != KCGI_OK) {111warnx("khttp_puts");112goto out;113} else if (khttp_printf(&r, "%d", -12) != KCGI_OK) {114warnx("khttp_printf");115goto out;116} else if (khttp_printf(&r, NULL) != KCGI_OK) {117warnx("khttp_printf");118goto out;119} else if (khttp_printf(&r, "%s", "abc") != KCGI_OK) {120warnx("khttp_printf");121goto out;122} else if (khttp_putc(&r, 0) != KCGI_OK) {123warnx("khttp_putc");124goto out;125} else if (khttp_putc(&r, 10) != KCGI_OK) {126warnx("khttp_putc");127goto out;128} else if (khttp_putc(&r, 256) != KCGI_OK) {129warnx("khttp_putc");130goto out;131} else if (khttp_putc(&r, 257) != KCGI_OK) {132warnx("khttp_putc");133goto out;134} else if (khttp_write(&r, bbuf, sizeof(bbuf)) != KCGI_OK) {135warnx("khttp_putc");136goto out;137} else if (khttp_write(&r, NULL, sizeof(bbuf)) != KCGI_OK) {138warnx("khttp_putc");139goto out;140} else if (khttp_write(&r, bbuf, 0) != KCGI_OK) {141warnx("khttp_putc");142goto out;143}144
145rc = 1;146out:147khttp_free(&r);148return rc;149}
150
151int
152main(int argc, char *argv[])153{
154
155return regress_cgi(parent, child) ?156EXIT_SUCCESS : EXIT_FAILURE;157}
158