ksgi

Форк
0
/
test-write.c 
157 строк · 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 <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

33
static size_t
34
bufcb(void *contents, size_t sz, size_t nm, void *dat)
35
{
36
	struct kcgi_buf	*buf = dat;
37

38
	if (kcgi_buf_write(contents, nm * sz, buf) != KCGI_OK)
39
		return 0;
40
	return nm * sz;
41
}
42

43
static int
44
parent(CURL *curl)
45
{
46
	struct kcgi_buf	 buf;
47
	int		 rc;
48
	unsigned char	 want[] = {
49
		'a', 'b', 'c',
50
		'-', '1', '2',
51
		'a', 'b', 'c',
52
		0, 10, 
53
		/* We'll lose bits, but that's the point. */
54
		(unsigned char)256, (unsigned char)257,
55
		0, 1, 2
56
	};
57

58
	memset(&buf, 0, sizeof(struct kcgi_buf));
59

60
	curl_easy_setopt(curl, CURLOPT_URL, 
61
		"http://localhost:17123/index");
62
	curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, bufcb);
63
	curl_easy_setopt(curl, CURLOPT_WRITEDATA, &buf);
64
	if (curl_easy_perform(curl) != CURLE_OK)
65
		return 0;
66

67
	/* Check same string and NUL terminated. */
68

69
	rc = buf.sz == sizeof(want) &&
70
   	     memcmp(buf.buf, want, buf.sz) == 0 &&
71
	     buf.buf[buf.sz] == '\0';
72

73
	free(buf.buf);
74
	return rc;
75
}
76

77
static int
78
child(void)
79
{
80
	struct kreq	 r;
81
	const char 	*page[] = { "index" };
82
	char		 bbuf[3] = { 0, 1, 2 };
83
	int		 rc = 0;
84

85
	if (khttp_parse(&r, NULL, 0, page, 1, 0) != KCGI_OK) {
86
		warnx("khttp_parse");
87
		return 0;
88
	} else if (r.page != 0)
89
		goto out;
90

91
	/* Setup. */
92

93
	if (khttp_head(&r, kresps[KRESP_STATUS],
94
	    "%s", khttps[KHTTP_200]) != KCGI_OK) {
95
		warnx("khttp_head");
96
		goto out;
97
	} else if (khttp_body(&r) != KCGI_OK) {
98
		warnx("khttp_body");
99
		goto out;
100
	}
101

102
	/* Tests. */
103

104
	if (khttp_puts(&r, "abc") != KCGI_OK) {
105
		warnx("khttp_puts");
106
		goto out;
107
	} else if (khttp_puts(&r, NULL) != KCGI_OK) {
108
		warnx("khttp_puts");
109
		goto out;
110
	} else if (khttp_puts(&r, "") != KCGI_OK) {
111
		warnx("khttp_puts");
112
		goto out;
113
	} else if (khttp_printf(&r, "%d", -12) != KCGI_OK) {
114
		warnx("khttp_printf");
115
		goto out;
116
	} else if (khttp_printf(&r, NULL) != KCGI_OK) {
117
		warnx("khttp_printf");
118
		goto out;
119
	} else if (khttp_printf(&r, "%s", "abc") != KCGI_OK) {
120
		warnx("khttp_printf");
121
		goto out;
122
	} else if (khttp_putc(&r, 0) != KCGI_OK) {
123
		warnx("khttp_putc");
124
		goto out;
125
	} else if (khttp_putc(&r, 10) != KCGI_OK) {
126
		warnx("khttp_putc");
127
		goto out;
128
	} else if (khttp_putc(&r, 256) != KCGI_OK) {
129
		warnx("khttp_putc");
130
		goto out;
131
	} else if (khttp_putc(&r, 257) != KCGI_OK) {
132
		warnx("khttp_putc");
133
		goto out;
134
	} else if (khttp_write(&r, bbuf, sizeof(bbuf)) != KCGI_OK) {
135
		warnx("khttp_putc");
136
		goto out;
137
	} else if (khttp_write(&r, NULL, sizeof(bbuf)) != KCGI_OK) {
138
		warnx("khttp_putc");
139
		goto out;
140
	} else if (khttp_write(&r, bbuf, 0) != KCGI_OK) {
141
		warnx("khttp_putc");
142
		goto out;
143
	}
144

145
	rc = 1;
146
out:
147
	khttp_free(&r);
148
	return rc;
149
}
150

151
int
152
main(int argc, char *argv[])
153
{
154

155
	return regress_cgi(parent, child) ?
156
		EXIT_SUCCESS : EXIT_FAILURE;
157
}
158

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

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

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

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