chiark / gitweb /
Disobedience memory/widget debugging stuff has thoroughly rotted, so
[disorder] / libtests / t-cgi.c
CommitLineData
5b708e0c
RK
1/*
2 * This file is part of DisOrder.
3 * Copyright (C) 2008 Richard Kettlewell
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
18 * USA
19 */
20#include "test.h"
21#include "cgi.h"
22
23static void input_from(const char *s) {
24 FILE *fp = tmpfile();
25 char buffer[64];
26
9faa7a88
RK
27 if(fputs(s, fp) < 0
28 || fputs("wibble wibble\r\nspong", fp) < 0 /* ensure CONTENT_LENGTH
29 * honored */
30 || fflush(fp) < 0)
5b708e0c
RK
31 fatal(errno, "writing to temporary file");
32 rewind(fp);
33 xdup2(fileno(fp), 0);
34 lseek(0, 0/*offset*/, SEEK_SET);
35 snprintf(buffer, sizeof buffer, "%zu", strlen(s));
36 setenv("CONTENT_LENGTH", buffer, 1);
37}
38
39static void test_cgi(void) {
40 struct dynstr d[1];
41
42 setenv("REQUEST_METHOD", "GET", 1);
43 setenv("QUERY_STRING", "foo=bar&a=b+c&c=x%7ey", 1);
44 cgi_init();
45 check_string(cgi_get("foo"), "bar");
46 check_string(cgi_get("a"), "b c");
47 check_string(cgi_get("c"), "x~y");
48
49 setenv("REQUEST_METHOD", "POST", 1);
d16be832 50 setenv("CONTENT_TYPE", "application/x-www-form-urlencoded", 1);
5b708e0c
RK
51 unsetenv("QUERY_STRING");
52 input_from("foo=xbar&a=xb+c&c=xx%7ey");
53 cgi_init();
54 check_string(cgi_get("foo"), "xbar");
55 check_string(cgi_get("a"), "xb c");
56 check_string(cgi_get("c"), "xx~y");
57
d16be832
RK
58 /* This input string generated by Firefox 2.0.0.14 */
59 input_from("-----------------------------16128946562344073111198667379\r\n"
60 "Content-Disposition: form-data; name=\"input1\"\r\n"
61 "\r\n"
62 "normal input\r\n"
63 "-----------------------------16128946562344073111198667379\r\n"
64 "Content-Disposition: form-data; name=\"input2\"\r\n"
65 "\r\n"
66 "with pound sign: \xC2\xA3\r\n"
67 "-----------------------------16128946562344073111198667379\r\n"
68 "Content-Disposition: form-data; name=\"input3\"\r\n"
69 "\r\n"
70 "hidden input\r\n"
71 "-----------------------------16128946562344073111198667379\r\n"
72 "Content-Disposition: form-data; name=\"submit\"\r\n"
73 "\r\n"
74 "Submit Query\r\n"
75 "-----------------------------16128946562344073111198667379--\r\n");
76 setenv("CONTENT_TYPE", "multipart/form-data; boundary=---------------------------16128946562344073111198667379", 1);
77 unsetenv("QUERY_STRING");
78 cgi_init();
79 check_string(cgi_get("input1"), "normal input");
80 check_string(cgi_get("input2"), "with pound sign: \xC2\xA3");
81 check_string(cgi_get("input3"), "hidden input");
82 check_string(cgi_get("submit"), "Submit Query");
5b708e0c 83
d16be832
RK
84 input_from("-----------------------------33919340914020259251659146591\r\n"
85 "Content-Disposition: form-data; name=\"text\"\r\n"
86 "\r\n"
87 "Text with\r\n"
88 "multiple lines\r\n"
89 "and trailing spaces \r\n"
90 "---and leading dashes\r\n"
91 "and pound sign \xC2\xA3\r\n"
92 "\r\n"
93 "-----------------------------33919340914020259251659146591\r\n"
94 "Content-Disposition: form-data; name=\"empty\"\r\n"
95 "\r\n"
96 "\r\n"
97 "-----------------------------33919340914020259251659146591\r\n"
98 "Content-Disposition: form-data; name=\"submit\"\r\n"
99 "\r\n"
100 "Submit Query\r\n"
101 "-----------------------------33919340914020259251659146591--\r\n");
102 setenv("CONTENT_TYPE", "multipart/form-data; boundary=---------------------------33919340914020259251659146591", 1);
103 cgi_init();
104 check_string(cgi_get("text"), ("Text with\r\n"
105 "multiple lines\r\n"
106 "and trailing spaces \r\n"
107 "---and leading dashes\r\n"
108 "and pound sign \xC2\xA3\r\n"
109 ""));
110 check_string(cgi_get("empty"), "");
111
5b708e0c
RK
112 check_string(cgi_sgmlquote("foobar"), "foobar");
113 check_string(cgi_sgmlquote("<wibble>"), "&#60;wibble&#62;");
114 check_string(cgi_sgmlquote("\"&\""), "&#34;&#38;&#34;");
115 check_string(cgi_sgmlquote("\xC2\xA3"), "&#163;");
116
117 dynstr_init(d);
118 cgi_opentag(sink_dynstr(d), "element",
119 "foo", "bar",
120 "foo", "has space",
121 "foo", "has \"quotes\"",
122 (char *)NULL);
123 dynstr_terminate(d);
124 check_string(d->vec, "<element foo=bar foo=\"has space\" foo=\"has &#34;quotes&#34;\">");
125
126 dynstr_init(d);
127 cgi_opentag(sink_dynstr(d), "element",
128 "foo", (char *)NULL,
129 (char *)NULL);
130 dynstr_terminate(d);
131 check_string(d->vec, "<element foo>");
132
133 dynstr_init(d);
134 cgi_closetag(sink_dynstr(d), "element");
135 dynstr_terminate(d);
136 check_string(d->vec, "</element>");
137
138 check_string(cgi_makeurl("http://example.com/", (char *)NULL),
139 "http://example.com/");
140 check_string(cgi_makeurl("http://example.com/",
141 "foo", "bar",
142 "a", "b c",
143 "d", "f=g+h",
144 (char *)NULL),
145 "http://example.com/?foo=bar&a=b%20c&d=f%3dg%2bh");
146
147}
148
149TEST(cgi);
150
151/*
152Local Variables:
153c-basic-offset:2
154comment-column:40
155fill-column:79
156indent-tabs-mode:nil
157End:
158*/