ksgi

Форк
0
/
test-debug-write-long.c 
181 строка · 3.6 Кб
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_ERR
20
# include <err.h>
21
#endif
22
#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

34
static char log[33] = "/tmp/test-debug-write.XXXXXXXXXX";
35

36
static 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

53
static int
54
parent(CURL *curl)
55
{
56

57
	curl_easy_setopt(curl, CURLOPT_URL, 
58
		"http://localhost:17123/");
59
	return curl_easy_perform(curl) == CURLE_OK;
60
}
61

62
static int
63
child(void)
64
{
65
	struct kreq	 r;
66
	const char 	*page = "index";
67
	enum kcgi_err	 er;
68

69
	if (!kutil_openlog(log))
70
		return 0;
71

72
	er = khttp_parsex
73
		(&r, ksuffixmap, kmimetypes, KMIME__MAX, 
74
		 NULL, 0, &page, 1, KMIME_TEXT_HTML,
75
		 0, NULL, NULL, KREQ_DEBUG_WRITE, NULL);
76
	if (er != KCGI_OK)
77
		return 0;
78

79
	khttp_head(&r, kresps[KRESP_STATUS], 
80
		"%s", khttps[KHTTP_200]);
81
	khttp_head(&r, kresps[KRESP_CONTENT_TYPE], 
82
		"%s", kmimetypes[KMIME_TEXT_HTML]);
83
	khttp_body(&r);
84
	khttp_puts(&r, data);
85
	khttp_free(&r);
86
	return 1;
87
}
88

89
int
90
main(int argc, char *argv[])
91
{
92
	int		 fd = -1, rc = 1;
93
	FILE		*f = NULL;
94
	char		*line = NULL;
95
	const char	*cp;
96
	size_t		 linesize = 0, lineno = 0;
97
	ssize_t		 linelen;
98
	struct log_line	 log_line;
99

100
	if ((fd = mkstemp(log)) == -1)
101
		err(1, "%s", log);
102

103
	if (!regress_cgi(parent, child))
104
		goto out;
105

106
	if ((f = fdopen(fd, "r")) == NULL) {
107
		warn("%s", log);
108
		goto out;
109
	}
110

111
	for (;;) {
112
		if ((linelen = getline(&line, &linesize, f)) == -1)
113
			break;
114
		if (!log_line_parse(line, &log_line))
115
			goto out;
116
		if (strcmp(log_line.level, "INFO"))
117
			continue;
118

119
		/* Extract after pid-tx. */
120

121
		if ((cp = strchr(log_line.umsg, ':')) == NULL)
122
			goto out;
123
		cp++;
124
		while (*cp == ' ')
125
			cp++;
126

127
		/* Examine line-by-line. */
128

129
		switch (lineno++) {
130
		case 0:
131
			if (strcmp(cp, "Status: 200 OK\\r\n"))
132
				goto out;
133
			break;
134
		case 1:
135
			if (strcmp(cp, "Content-Type: text/html\\r\n"))
136
				goto out;
137
			break;
138
		case 2:
139
			if (strcmp(cp, "\\r\n"))
140
				goto out;
141
			break;
142
		case 3:
143
			if (strcmp(cp, "foo="
144
			    "012345678901234567890123456789"
145
			    "012345678901234567890123456789"
146
			    "0123456789012345...\n"))
147
				goto out;
148
			break;
149
		case 4:
150
			if (strcmp(cp, "6789"
151
			    "012345678901234567890123456789"
152
			    "012345678901234567890123456789"
153
			    "0123456789012345...\n"))
154
				goto out;
155
			break;
156
		case 5:
157
			if (strcmp(cp, "6\n"))
158
				goto out;
159
			break;
160
		case 6:
161
			/* Ignore. */
162
			break;
163
		default:
164
			break;
165
		}
166
	}
167

168
	if (ferror(f) || lineno != 7)
169
		goto out;
170

171
	rc = 0;
172
out:
173
	if (f != NULL)
174
		fclose(f);
175
	else if (fd != -1)
176
		close(fd);
177

178
	free(line);
179
	unlink(log);
180
	return rc;
181
}
182

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.