chiark / gitweb /
a3b9f37c8cf4862b1dc87120d808cd650b9c1c98
[disorder] / lib / t-cgi.c
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
23 static void input_from(const char *s) {
24   FILE *fp = tmpfile();
25   char buffer[64];
26
27   if(fputs(s, fp) < 0 || fflush(fp) < 0)
28     fatal(errno, "writing to temporary file");
29   rewind(fp);
30   xdup2(fileno(fp), 0);
31   lseek(0, 0/*offset*/, SEEK_SET);
32   snprintf(buffer, sizeof buffer, "%zu", strlen(s));
33   setenv("CONTENT_LENGTH", buffer, 1);
34 }
35
36 static void test_cgi(void) {
37   struct dynstr d[1];
38
39   setenv("REQUEST_METHOD", "GET", 1);
40   setenv("QUERY_STRING", "foo=bar&a=b+c&c=x%7ey", 1);
41   cgi_init();
42   check_string(cgi_get("foo"), "bar");
43   check_string(cgi_get("a"), "b c");
44   check_string(cgi_get("c"), "x~y");
45
46   setenv("REQUEST_METHOD", "POST", 1);
47   setenv("CONTENT_TYPE", "application/x-www-form-urlencoded", 1);
48   unsetenv("QUERY_STRING");
49   input_from("foo=xbar&a=xb+c&c=xx%7ey");
50   cgi_init();
51   check_string(cgi_get("foo"), "xbar");
52   check_string(cgi_get("a"), "xb c");
53   check_string(cgi_get("c"), "xx~y");
54
55   /* This input string generated by Firefox 2.0.0.14 */
56   input_from("-----------------------------16128946562344073111198667379\r\n"
57              "Content-Disposition: form-data; name=\"input1\"\r\n"
58              "\r\n"
59              "normal input\r\n"
60              "-----------------------------16128946562344073111198667379\r\n"
61              "Content-Disposition: form-data; name=\"input2\"\r\n"
62              "\r\n"
63              "with pound sign: \xC2\xA3\r\n"
64              "-----------------------------16128946562344073111198667379\r\n"
65              "Content-Disposition: form-data; name=\"input3\"\r\n"
66              "\r\n"
67              "hidden input\r\n"
68              "-----------------------------16128946562344073111198667379\r\n"
69              "Content-Disposition: form-data; name=\"submit\"\r\n"
70              "\r\n"
71              "Submit Query\r\n"
72              "-----------------------------16128946562344073111198667379--\r\n");
73   setenv("CONTENT_TYPE", "multipart/form-data; boundary=---------------------------16128946562344073111198667379", 1);
74   unsetenv("QUERY_STRING");
75   cgi_init();
76   check_string(cgi_get("input1"), "normal input");
77   check_string(cgi_get("input2"), "with pound sign: \xC2\xA3");
78   check_string(cgi_get("input3"), "hidden input");
79   check_string(cgi_get("submit"), "Submit Query");
80
81   input_from("-----------------------------33919340914020259251659146591\r\n"
82              "Content-Disposition: form-data; name=\"text\"\r\n"
83              "\r\n"
84              "Text with\r\n"
85              "multiple lines\r\n"
86              "and trailing spaces \r\n"
87              "---and leading dashes\r\n"
88              "and pound sign \xC2\xA3\r\n"
89              "\r\n"
90              "-----------------------------33919340914020259251659146591\r\n"
91              "Content-Disposition: form-data; name=\"empty\"\r\n"
92              "\r\n"
93              "\r\n"
94              "-----------------------------33919340914020259251659146591\r\n"
95              "Content-Disposition: form-data; name=\"submit\"\r\n"
96              "\r\n"
97              "Submit Query\r\n"
98              "-----------------------------33919340914020259251659146591--\r\n");
99   setenv("CONTENT_TYPE", "multipart/form-data; boundary=---------------------------33919340914020259251659146591", 1);
100   cgi_init();
101   check_string(cgi_get("text"), ("Text with\r\n"
102                                  "multiple lines\r\n"
103                                  "and trailing spaces \r\n"
104                                  "---and leading dashes\r\n"
105                                  "and pound sign \xC2\xA3\r\n"
106                                  ""));
107   check_string(cgi_get("empty"), "");
108   
109   check_string(cgi_sgmlquote("foobar"), "foobar");
110   check_string(cgi_sgmlquote("<wibble>"), "&#60;wibble&#62;");
111   check_string(cgi_sgmlquote("\"&\""), "&#34;&#38;&#34;");
112   check_string(cgi_sgmlquote("\xC2\xA3"), "&#163;");
113
114   dynstr_init(d);
115   cgi_opentag(sink_dynstr(d), "element",
116               "foo", "bar",
117               "foo", "has space",
118               "foo", "has \"quotes\"",
119               (char *)NULL);
120   dynstr_terminate(d);
121   check_string(d->vec, "<element foo=bar foo=\"has space\" foo=\"has &#34;quotes&#34;\">");
122   
123   dynstr_init(d);
124   cgi_opentag(sink_dynstr(d), "element",
125               "foo", (char *)NULL,
126               (char *)NULL);
127   dynstr_terminate(d);
128   check_string(d->vec, "<element foo>");
129   
130   dynstr_init(d);
131   cgi_closetag(sink_dynstr(d), "element");
132   dynstr_terminate(d);
133   check_string(d->vec, "</element>");
134
135   check_string(cgi_makeurl("http://example.com/", (char *)NULL),
136                "http://example.com/");
137   check_string(cgi_makeurl("http://example.com/",
138                            "foo", "bar",
139                            "a", "b c",
140                            "d", "f=g+h",
141                            (char *)NULL),
142                "http://example.com/?foo=bar&a=b%20c&d=f%3dg%2bh");
143   
144 }
145
146 TEST(cgi);
147
148 /*
149 Local Variables:
150 c-basic-offset:2
151 comment-column:40
152 fill-column:79
153 indent-tabs-mode:nil
154 End:
155 */