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 <stdio.h>25#include <stdlib.h>26#include <string.h>27#include <unistd.h>28
29#include <curl/curl.h>30
31#include "../kcgi.h"32#include "regress.h"33
34static char log[33] = "/tmp/test-debug-write.XXXXXXXXXX";35
36static const char *data = "foo=0123456789"37"0123456789"38"0123456789"39"0123456789"40"0123456789"41"0123456789"42"0123456789"43"0123456789"44"0123456789"45"0123456789"46"0123456789"47"0123456789"48"0123456789"49"0123456789"50"0123456789"51"0123456";52
53static int54parent(CURL *curl)55{
56
57curl_easy_setopt(curl, CURLOPT_URL,58"http://localhost:17123/");59return curl_easy_perform(curl) == CURLE_OK;60}
61
62static int63child(void)64{
65struct kreq r;66const char *page = "index";67enum kcgi_err er;68
69if (!kutil_openlog(log))70return 0;71
72er = khttp_parsex73(&r, ksuffixmap, kmimetypes, KMIME__MAX,74NULL, 0, &page, 1, KMIME_TEXT_HTML,750, NULL, NULL, KREQ_DEBUG_WRITE, NULL);76if (er != KCGI_OK)77return 0;78
79khttp_head(&r, kresps[KRESP_STATUS],80"%s", khttps[KHTTP_200]);81khttp_head(&r, kresps[KRESP_CONTENT_TYPE],82"%s", kmimetypes[KMIME_TEXT_HTML]);83khttp_body(&r);84khttp_puts(&r, data);85khttp_free(&r);86return 1;87}
88
89int
90main(int argc, char *argv[])91{
92int fd = -1, rc = 1;93FILE *f = NULL;94char *line = NULL;95const char *cp;96size_t linesize = 0, lineno = 0;97ssize_t linelen;98struct log_line log_line;99
100if ((fd = mkstemp(log)) == -1)101err(1, "%s", log);102
103if (!regress_cgi(parent, child))104goto out;105
106if ((f = fdopen(fd, "r")) == NULL) {107warn("%s", log);108goto out;109}110
111for (;;) {112if ((linelen = getline(&line, &linesize, f)) == -1)113break;114if (!log_line_parse(line, &log_line))115goto out;116if (strcmp(log_line.level, "INFO"))117continue;118
119/* Extract after pid-tx. */120
121if ((cp = strchr(log_line.umsg, ':')) == NULL)122goto out;123cp++;124while (*cp == ' ')125cp++;126
127/* Examine line-by-line. */128
129switch (lineno++) {130case 0:131if (strcmp(cp, "Status: 200 OK\\r\n"))132goto out;133break;134case 1:135if (strcmp(cp, "Content-Type: text/html\\r\n"))136goto out;137break;138case 2:139if (strcmp(cp, "\\r\n"))140goto out;141break;142case 3:143if (strcmp(cp, "foo="144"012345678901234567890123456789"145"012345678901234567890123456789"146"0123456789012345...\n"))147goto out;148break;149case 4:150if (strcmp(cp, "6789"151"012345678901234567890123456789"152"012345678901234567890123456789"153"0123456789012345...\n"))154goto out;155break;156case 5:157if (strcmp(cp, "6\n"))158goto out;159break;160case 6:161/* Ignore. */162break;163default:164break;165}166}167
168if (ferror(f) || lineno != 7)169goto out;170
171rc = 0;172out:173if (f != NULL)174fclose(f);175else if (fd != -1)176close(fd);177
178free(line);179unlink(log);180return rc;181}
182