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 "../kcgihtml.h"32#include "regress.h"33
34#define EXPECT \35"<!DOCTYPE html><html><body>foo</body></html>"36
37static size_t38bufcb(void *contents, size_t sz, size_t nm, void *dat)39{
40struct kcgi_buf *buf = dat;41
42if (kcgi_buf_write(contents, nm * sz, buf) != KCGI_OK)43return 0;44return nm * sz;45}
46
47static int48parent(CURL *curl)49{
50struct kcgi_buf buf;51int rc;52
53memset(&buf, 0, sizeof(struct kcgi_buf));54
55curl_easy_setopt(curl, CURLOPT_URL,56"http://localhost:17123/index.html");57curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, bufcb);58curl_easy_setopt(curl, CURLOPT_WRITEDATA, &buf);59if (curl_easy_perform(curl) != CURLE_OK) {60warnx("curl_easy_perform");61return 0;62}63if (!(rc = strcmp(buf.buf, EXPECT) == 0))64warnx("content test failure: %s", buf.buf);65free(buf.buf);66return rc;67}
68
69static int70child(void)71{
72struct kreq r;73struct khtmlreq req;74const char *page[] = { "index" };75int rc = 0;76size_t pos;77
78if (khttp_parse(&r, NULL, 0, page, 1, 0) != KCGI_OK) {79warnx("khttp_parse");80return 0;81} else if (r.page != 0 || r.mime != KMIME_TEXT_HTML)82goto out;83
84/* Setup. */85
86if (khttp_head(&r, kresps[KRESP_STATUS],87"%s", khttps[KHTTP_200]) != KCGI_OK) {88warnx("khttp_head");89goto out;90} else if (khttp_head(&r, kresps[KRESP_CONTENT_TYPE],91"%s", kmimetypes[r.mime]) != KCGI_OK) {92warnx("khttp_head");93goto out;94} else if (khttp_body(&r) != KCGI_OK) {95warnx("khttp_body");96goto out;97}98
99if (khtml_open(&req, &r, 0) != KCGI_OK) {100warnx("khtml_open");101goto out;102}103
104kcgi_writer_disable(&r);105
106if (khtml_closeelem(&req, 0) != KCGI_OK ||107khtml_closeelem(&req, 100) != KCGI_OK) {108warnx("khtml_closeelem");109goto out;110}111
112if (khtml_closeto(&req, 0) != KCGI_OK ||113khtml_closeto(&req, 100) != KCGI_OK) {114warnx("khtml_closeto");115goto out;116}117
118if (khtml_elem(&req, KELEM_DOCTYPE) != KCGI_OK ||119khtml_elem(&req, KELEM_HTML) != KCGI_OK ||120khtml_elem(&req, KELEM_BODY) != KCGI_OK) {121warnx("khtml_elem");122goto out;123}124pos = khtml_elemat(&req);125if (khtml_closeto(&req, pos) != KCGI_OK) {126warnx("khtml_closeto");127goto out;128} else if (pos != khtml_elemat(&req)) {129warnx("should not have closed scopes");130goto out;131}132
133if (khtml_puts(&req, "foo") != KCGI_OK) {134warnx("khtml_puts");135goto out;136} else if (pos != khtml_elemat(&req)) {137warnx("should not have closed scopes");138goto out;139}140
141if (khtml_close(&req) != KCGI_OK) {142warnx("khtml_close");143goto out;144}145
146rc = 1;147out:148khttp_free(&r);149return rc;150}
151
152int
153main(int argc, char *argv[])154{
155
156return regress_cgi(parent, child) ?157EXIT_SUCCESS : EXIT_FAILURE;158}
159