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