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