Add test for disabled HTML failure and make simple HTML test more interesting.

This commit is contained in:
kristapsdz committed 2020-03-26 10:10:26 +00:00
1 parent 672da95138
commit fc1db72f16
3 files changed
+142 -4

No files matched your search

+1
View File
@@ -179,6 +179,7 @@ REGRESS = regress/test-abort-validator \
regress/test-header \
regress/test-header-bad \
regress/test-header-builtin \
regress/test-html-disabled \
regress/test-html-simple \
regress/test-httpdate \
regress/test-invalidate \
+114
View File
@@ -0,0 +1,114 @@
/* $Id$ */
/*
* Copyright (c) 2020 Kristaps Dzonsons <kristaps@bsd.lv>
*
* Permission to use, copy, modify, and 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 "../config.h"
#if HAVE_ERR
# include <err.h>
#endif
#include <stdarg.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <curl/curl.h>
#include "../kcgi.h"
#include "../kcgihtml.h"
#include "regress.h"
static size_t
bufcb(void *contents, size_t sz, size_t nm, void *dat)
{
struct kcgi_buf *buf = dat;
if (kcgi_buf_write(contents, nm * sz, buf) != KCGI_OK)
return 0;
return nm * sz;
}
static int
parent(CURL *curl)
{
struct kcgi_buf buf;
size_t sz;
memset(&buf, 0, sizeof(struct kcgi_buf));
curl_easy_setopt(curl, CURLOPT_URL,
"http://localhost:17123/index.html");
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, bufcb);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &buf);
if (curl_easy_perform(curl) != CURLE_OK) {
warnx("curl_easy_perform");
return 0;
}
if ((sz = buf.sz) > 0)
warnx("unexpected content");
free(buf.buf);
return sz == 0;
}
static int
child(void)
{
struct kreq r;
struct khtmlreq req;
const char *page[] = { "index" };
if (khttp_parse(&r, NULL, 0, page, 1, 0) != KCGI_OK) {
warnx("khttp_parse");
return 0;
} else if (r.page != 0 || r.mime != KMIME_TEXT_HTML)
goto out;
/* Setup. */
if (khttp_head(&r, kresps[KRESP_STATUS],
"%s", khttps[KHTTP_200]) != KCGI_OK) {
warnx("khttp_head");
goto out;
} else if (khttp_head(&r, kresps[KRESP_CONTENT_TYPE],
"%s", kmimetypes[r.mime]) != KCGI_OK) {
warnx("khttp_head");
goto out;
} else if (khttp_body(&r) != KCGI_OK) {
warnx("khttp_body");
goto out;
}
/*
* This will cause khtml_open() to abort.
* The result: zero-length content.
*/
kcgi_writer_disable(&r);
if (khtml_open(&req, &r, 0) == KCGI_OK)
warnx("khtml_open should not complete");
out:
khttp_free(&r);
return 0;
}
int
main(int argc, char *argv[])
{
return regress_cgi(parent, child) ?
EXIT_SUCCESS : EXIT_FAILURE;
}
+27 -4
View File
@@ -31,6 +31,11 @@
#include "../kcgihtml.h"
#include "regress.h"
#define EXPECT \
"<!DOCTYPE html><html><body>" \
"Hi&#x2014;5 minutes til midnight!&#x1f601;" \
"</body></html>"
static size_t
bufcb(void *contents, size_t sz, size_t nm, void *dat)
{
@@ -57,8 +62,8 @@ parent(CURL *curl)
warnx("curl_easy_perform");
return 0;
}
if (!(rc = strcmp(buf.buf, "<!DOCTYPE html>") == 0))
warnx("content test");
if (!(rc = strcmp(buf.buf, EXPECT) == 0))
warnx("content test: %s", buf.buf);
free(buf.buf);
return rc;
}
@@ -95,10 +100,28 @@ child(void)
if (khtml_open(&req, &r, 0) != KCGI_OK) {
warnx("khtml_open");
goto out;
} else if (khtml_elem(&req, KELEM_DOCTYPE) != KCGI_OK) {
}
kcgi_writer_disable(&r);
if (khtml_elem(&req, KELEM_DOCTYPE) != KCGI_OK ||
khtml_elem(&req, KELEM_HTML) != KCGI_OK ||
khtml_elem(&req, KELEM_BODY) != KCGI_OK) {
warnx("khtml_elem");
goto out;
} else if (khtml_close(&req) != KCGI_OK) {
}
if (khtml_puts(&req, "Hi") != KCGI_OK ||
khtml_entity(&req, KENTITY_mdash) != KCGI_OK ||
khtml_int(&req, 5) != KCGI_OK ||
khtml_write(" minutes xxx", 9, &req) != KCGI_OK ||
khtml_printf(&req, "til %s!", "midnight") != KCGI_OK ||
khtml_ncr(&req, 0x1F601) != KCGI_OK) {
warnx("khtml_puts");
goto out;
}
if (khtml_close(&req) != KCGI_OK) {
warnx("khtml_close");
goto out;
}