]> git.ipfire.org Git - thirdparty/squid.git/blob - src/tests/testHttpRequest.cc
Broken: define and use stub_libauth.cc
[thirdparty/squid.git] / src / tests / testHttpRequest.cc
1 #define SQUID_UNIT_TEST 1
2 #include "squid.h"
3
4 #include <cppunit/TestAssert.h>
5
6 #include "HttpHeader.h"
7 #include "HttpRequest.h"
8 #include "Mem.h"
9 #include "mime_header.h"
10 #include "testHttpRequest.h"
11
12 CPPUNIT_TEST_SUITE_REGISTRATION( testHttpRequest );
13
14 /** wrapper for testing HttpRequest object private and protected functions */
15 class PrivateHttpRequest : public HttpRequest
16 {
17 public:
18 bool doSanityCheckStartLine(MemBuf *b, const size_t h, Http::StatusCode *e) { return sanityCheckStartLine(b,h,e); };
19 };
20
21 /* init memory pools */
22
23 void
24 testHttpRequest::setUp()
25 {
26 Mem::Init();
27 httpHeaderInitModule();
28 }
29
30 /*
31 * Test creating an HttpRequest object from a Url and method
32 */
33 void
34 testHttpRequest::testCreateFromUrlAndMethod()
35 {
36 /* vanilla url */
37 unsigned short expected_port;
38 char * url = xstrdup("http://foo:90/bar");
39 HttpRequest *aRequest = HttpRequest::CreateFromUrlAndMethod(url, Http::METHOD_GET);
40 expected_port = 90;
41 HttpRequest *nullRequest = NULL;
42 CPPUNIT_ASSERT_EQUAL(expected_port, aRequest->port);
43 CPPUNIT_ASSERT(aRequest->method == Http::METHOD_GET);
44 CPPUNIT_ASSERT_EQUAL(String("foo"), String(aRequest->GetHost()));
45 CPPUNIT_ASSERT_EQUAL(String("/bar"), aRequest->urlpath);
46 CPPUNIT_ASSERT_EQUAL(AnyP::PROTO_HTTP, aRequest->protocol);
47 CPPUNIT_ASSERT_EQUAL(String("http://foo:90/bar"), String(url));
48 xfree(url);
49
50 /* vanilla url, different method */
51 url = xstrdup("http://foo/bar");
52 aRequest = HttpRequest::CreateFromUrlAndMethod(url, Http::METHOD_PUT);
53 expected_port = 80;
54 CPPUNIT_ASSERT_EQUAL(expected_port, aRequest->port);
55 CPPUNIT_ASSERT(aRequest->method == Http::METHOD_PUT);
56 CPPUNIT_ASSERT_EQUAL(String("foo"), String(aRequest->GetHost()));
57 CPPUNIT_ASSERT_EQUAL(String("/bar"), aRequest->urlpath);
58 CPPUNIT_ASSERT_EQUAL(AnyP::PROTO_HTTP, aRequest->protocol);
59 CPPUNIT_ASSERT_EQUAL(String("http://foo/bar"), String(url));
60 xfree(url);
61
62 /* a connect url with non-CONNECT data */
63 url = xstrdup(":foo/bar");
64 aRequest = HttpRequest::CreateFromUrlAndMethod(url, Http::METHOD_CONNECT);
65 xfree(url);
66 CPPUNIT_ASSERT_EQUAL(nullRequest, aRequest);
67
68 /* a CONNECT url with CONNECT data */
69 url = xstrdup("foo:45");
70 aRequest = HttpRequest::CreateFromUrlAndMethod(url, Http::METHOD_CONNECT);
71 expected_port = 45;
72 CPPUNIT_ASSERT_EQUAL(expected_port, aRequest->port);
73 CPPUNIT_ASSERT(aRequest->method == Http::METHOD_CONNECT);
74 CPPUNIT_ASSERT_EQUAL(String("foo"), String(aRequest->GetHost()));
75 CPPUNIT_ASSERT_EQUAL(String(""), aRequest->urlpath);
76 CPPUNIT_ASSERT_EQUAL(AnyP::PROTO_NONE, aRequest->protocol);
77 CPPUNIT_ASSERT_EQUAL(String("foo:45"), String(url));
78 xfree(url);
79 }
80
81 /*
82 * Test creating an HttpRequest object from a Url alone.
83 */
84 void
85 testHttpRequest::testCreateFromUrl()
86 {
87 /* vanilla url */
88 unsigned short expected_port;
89 char * url = xstrdup("http://foo:90/bar");
90 HttpRequest *aRequest = HttpRequest::CreateFromUrl(url);
91 expected_port = 90;
92 CPPUNIT_ASSERT_EQUAL(expected_port, aRequest->port);
93 CPPUNIT_ASSERT(aRequest->method == Http::METHOD_GET);
94 CPPUNIT_ASSERT_EQUAL(String("foo"), String(aRequest->GetHost()));
95 CPPUNIT_ASSERT_EQUAL(String("/bar"), aRequest->urlpath);
96 CPPUNIT_ASSERT_EQUAL(AnyP::PROTO_HTTP, aRequest->protocol);
97 CPPUNIT_ASSERT_EQUAL(String("http://foo:90/bar"), String(url));
98 xfree(url);
99 }
100
101 /*
102 * Test BUG: URL '2000:800:45' opens host 2000 port 800 !!
103 */
104 void
105 testHttpRequest::testIPv6HostColonBug()
106 {
107 unsigned short expected_port;
108 char * url = NULL;
109 HttpRequest *aRequest = NULL;
110
111 /* valid IPv6 address without port */
112 url = xstrdup("http://[2000:800::45]/foo");
113 aRequest = HttpRequest::CreateFromUrlAndMethod(url, Http::METHOD_GET);
114 expected_port = 80;
115 CPPUNIT_ASSERT_EQUAL(expected_port, aRequest->port);
116 CPPUNIT_ASSERT(aRequest->method == Http::METHOD_GET);
117 CPPUNIT_ASSERT_EQUAL(String("[2000:800::45]"), String(aRequest->GetHost()));
118 CPPUNIT_ASSERT_EQUAL(String("/foo"), aRequest->urlpath);
119 CPPUNIT_ASSERT_EQUAL(AnyP::PROTO_HTTP, aRequest->protocol);
120 CPPUNIT_ASSERT_EQUAL(String("http://[2000:800::45]/foo"), String(url));
121 xfree(url);
122
123 /* valid IPv6 address with port */
124 url = xstrdup("http://[2000:800::45]:90/foo");
125 aRequest = HttpRequest::CreateFromUrlAndMethod(url, Http::METHOD_GET);
126 expected_port = 90;
127 CPPUNIT_ASSERT_EQUAL(expected_port, aRequest->port);
128 CPPUNIT_ASSERT(aRequest->method == Http::METHOD_GET);
129 CPPUNIT_ASSERT_EQUAL(String("[2000:800::45]"), String(aRequest->GetHost()));
130 CPPUNIT_ASSERT_EQUAL(String("/foo"), aRequest->urlpath);
131 CPPUNIT_ASSERT_EQUAL(AnyP::PROTO_HTTP, aRequest->protocol);
132 CPPUNIT_ASSERT_EQUAL(String("http://[2000:800::45]:90/foo"), String(url));
133 xfree(url);
134
135 /* IPv6 address as invalid (bug trigger) */
136 url = xstrdup("http://2000:800::45/foo");
137 aRequest = HttpRequest::CreateFromUrlAndMethod(url, Http::METHOD_GET);
138 expected_port = 80;
139 CPPUNIT_ASSERT_EQUAL(expected_port, aRequest->port);
140 CPPUNIT_ASSERT(aRequest->method == Http::METHOD_GET);
141 CPPUNIT_ASSERT_EQUAL(String("[2000:800::45]"), String(aRequest->GetHost()));
142 CPPUNIT_ASSERT_EQUAL(String("/foo"), aRequest->urlpath);
143 CPPUNIT_ASSERT_EQUAL(AnyP::PROTO_HTTP, aRequest->protocol);
144 CPPUNIT_ASSERT_EQUAL(String("http://2000:800::45/foo"), String(url));
145 xfree(url);
146 }
147
148 void
149 testHttpRequest::testSanityCheckStartLine()
150 {
151 MemBuf input;
152 PrivateHttpRequest engine;
153 Http::StatusCode error = Http::scNone;
154 size_t hdr_len;
155 input.init();
156
157 // a valid request line
158 input.append("GET / HTTP/1.1\n\n", 16);
159 hdr_len = headersEnd(input.content(), input.contentSize());
160 CPPUNIT_ASSERT(engine.doSanityCheckStartLine(&input, hdr_len, &error) );
161 CPPUNIT_ASSERT_EQUAL(error, Http::scNone);
162 input.reset();
163 error = Http::scNone;
164
165 input.append("GET / HTTP/1.1\n\n", 18);
166 hdr_len = headersEnd(input.content(), input.contentSize());
167 CPPUNIT_ASSERT(engine.doSanityCheckStartLine(&input, hdr_len, &error) );
168 CPPUNIT_ASSERT_EQUAL(error, Http::scNone);
169 input.reset();
170 error = Http::scNone;
171
172 // strange but valid methods
173 input.append(". / HTTP/1.1\n\n", 14);
174 hdr_len = headersEnd(input.content(), input.contentSize());
175 CPPUNIT_ASSERT(engine.doSanityCheckStartLine(&input, hdr_len, &error) );
176 CPPUNIT_ASSERT_EQUAL(error, Http::scNone);
177 input.reset();
178 error = Http::scNone;
179
180 input.append("OPTIONS * HTTP/1.1\n\n", 20);
181 hdr_len = headersEnd(input.content(), input.contentSize());
182 CPPUNIT_ASSERT(engine.doSanityCheckStartLine(&input, hdr_len, &error) );
183 CPPUNIT_ASSERT_EQUAL(error, Http::scNone);
184 input.reset();
185 error = Http::scNone;
186
187 // TODO no method
188
189 // TODO binary code in method
190
191 // TODO no URL
192
193 // TODO no status (okay)
194
195 // TODO non-HTTP protocol
196
197 input.append(" \n\n", 8);
198 hdr_len = headersEnd(input.content(), input.contentSize());
199 CPPUNIT_ASSERT(!engine.doSanityCheckStartLine(&input, hdr_len, &error) );
200 CPPUNIT_ASSERT_EQUAL(error, Http::scInvalidHeader);
201 input.reset();
202 error = Http::scNone;
203 }