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