]> git.ipfire.org Git - thirdparty/squid.git/blob - src/tests/testHttpReply.cc
Merged from trunk
[thirdparty/squid.git] / src / tests / testHttpReply.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 #include <cppunit/TestAssert.h>
11
12 #include "HttpHeader.h"
13 #include "HttpReply.h"
14 #include "mime_header.h"
15 #include "SquidConfig.h"
16 #include "testHttpReply.h"
17
18 CPPUNIT_TEST_SUITE_REGISTRATION( testHttpReply );
19
20 class SquidConfig Config;
21
22 /* stub functions to link successfully */
23
24 #include "MemObject.h"
25 int64_t
26 MemObject::endOffset() const
27 {
28 return 0;
29 }
30
31 /* end */
32
33 void
34 testHttpReply::setUp()
35 {
36 Mem::Init();
37 httpHeaderInitModule();
38 }
39
40 void
41 testHttpReply::testSanityCheckFirstLine()
42 {
43 MemBuf input;
44 HttpReply engine;
45 Http::StatusCode error = Http::scNone;
46 size_t hdr_len;
47 input.init();
48
49 // a valid status line
50 input.append("HTTP/1.1 200 Okay\n\n", 19);
51 hdr_len = headersEnd(input.content(),input.contentSize());
52 CPPUNIT_ASSERT( 1 && engine.sanityCheckStartLine(&input, hdr_len, &error) );
53 CPPUNIT_ASSERT_EQUAL(error, Http::scNone);
54 input.reset();
55 error = Http::scNone;
56
57 input.append("HTTP/1.1 200 Okay \n\n", 28);
58 hdr_len = headersEnd(input.content(),input.contentSize());
59 CPPUNIT_ASSERT( 2 && engine.sanityCheckStartLine(&input, hdr_len, &error) );
60 CPPUNIT_ASSERT_EQUAL(error, Http::scNone);
61 input.reset();
62 error = Http::scNone;
63
64 #if TODO // these cases are only checked after parse...
65 // invalid status line
66 input.append("HTTP/1.1 999 Okay\n\n", 19);
67 hdr_len = headersEnd(input.content(),input.contentSize());
68 CPPUNIT_ASSERT( 3 && !engine.sanityCheckStartLine(&input, hdr_len, &error) );
69 CPPUNIT_ASSERT_EQUAL(error, Http::scInvalidHeader);
70 input.reset();
71 error = Http::scNone;
72
73 input.append("HTTP/1.1 2000 Okay \n\n", 29);
74 hdr_len = headersEnd(input.content(),input.contentSize());
75 CPPUNIT_ASSERT( 4 && engine.sanityCheckStartLine(&input, hdr_len, &error) );
76 CPPUNIT_ASSERT_EQUAL(error, Http::scNone);
77 input.reset();
78 error = Http::scNone;
79 #endif
80
81 // valid ICY protocol status line
82 input.append("ICY 200 Okay\n\n", 14);
83 hdr_len = headersEnd(input.content(),input.contentSize());
84 CPPUNIT_ASSERT( engine.sanityCheckStartLine(&input, hdr_len, &error) );
85 CPPUNIT_ASSERT_EQUAL(error, Http::scNone);
86 input.reset();
87 error = Http::scNone;
88 /* NP: the engine saves details about the protocol. even when being reset :( */
89 engine.protoPrefix="HTTP/";
90 engine.reset();
91
92 // empty status line
93 input.append("\n\n", 2);
94 hdr_len = headersEnd(input.content(),input.contentSize());
95 CPPUNIT_ASSERT( 5 && !engine.sanityCheckStartLine(&input, hdr_len, &error) );
96 CPPUNIT_ASSERT_EQUAL(error, Http::scInvalidHeader);
97 input.reset();
98 error = Http::scNone;
99
100 input.append(" \n\n", 8);
101 hdr_len = headersEnd(input.content(),input.contentSize());
102 CPPUNIT_ASSERT( 6 && !engine.sanityCheckStartLine(&input, hdr_len, &error) );
103 CPPUNIT_ASSERT_EQUAL(error, Http::scInvalidHeader);
104 input.reset();
105 error = Http::scNone;
106
107 // status line with no message
108 input.append("HTTP/1.1 200\n\n", 14); /* real case seen */
109 hdr_len = headersEnd(input.content(),input.contentSize());
110 CPPUNIT_ASSERT(engine.sanityCheckStartLine(&input, hdr_len, &error) );
111 CPPUNIT_ASSERT_EQUAL(error, Http::scNone);
112 input.reset();
113 error = Http::scNone;
114
115 input.append("HTTP/1.1 200 \n\n", 15); /* real case seen */
116 hdr_len = headersEnd(input.content(),input.contentSize());
117 CPPUNIT_ASSERT(engine.sanityCheckStartLine(&input, hdr_len, &error) );
118 CPPUNIT_ASSERT_EQUAL(error, Http::scNone);
119 input.reset();
120 error = Http::scNone;
121
122 // incomplete (short) status lines... not sane (yet), but no error either.
123 input.append("H", 1);
124 hdr_len = headersEnd(input.content(),input.contentSize());
125 CPPUNIT_ASSERT(!engine.sanityCheckStartLine(&input, hdr_len, &error) );
126 CPPUNIT_ASSERT_EQUAL(error, Http::scNone);
127 input.reset();
128 error = Http::scNone;
129
130 input.append("HTTP/", 5);
131 hdr_len = headersEnd(input.content(),input.contentSize());
132 CPPUNIT_ASSERT(!engine.sanityCheckStartLine(&input, hdr_len, &error) );
133 CPPUNIT_ASSERT_EQUAL(error, Http::scNone);
134 input.reset();
135 error = Http::scNone;
136
137 input.append("HTTP/1", 6);
138 hdr_len = headersEnd(input.content(),input.contentSize());
139 CPPUNIT_ASSERT(!engine.sanityCheckStartLine(&input, hdr_len, &error) );
140 CPPUNIT_ASSERT_EQUAL(error, Http::scNone);
141 input.reset();
142 error = Http::scNone;
143
144 input.append("HTTP/1.1", 8);
145 hdr_len = headersEnd(input.content(),input.contentSize());
146 CPPUNIT_ASSERT(!engine.sanityCheckStartLine(&input, hdr_len, &error) );
147 CPPUNIT_ASSERT_EQUAL(error, Http::scNone);
148 input.reset();
149 error = Http::scNone;
150
151 input.append("HTTP/1.1 ", 9); /* real case seen */
152 hdr_len = headersEnd(input.content(),input.contentSize());
153 CPPUNIT_ASSERT(engine.sanityCheckStartLine(&input, hdr_len, &error) );
154 CPPUNIT_ASSERT_EQUAL(error, Http::scNone);
155 input.reset();
156 error = Http::scNone;
157
158 input.append("HTTP/1.1 20", 14);
159 hdr_len = headersEnd(input.content(),input.contentSize());
160 CPPUNIT_ASSERT(engine.sanityCheckStartLine(&input, hdr_len, &error) );
161 CPPUNIT_ASSERT_EQUAL(error, Http::scNone);
162 input.reset();
163 error = Http::scNone;
164
165 // status line with no status
166 input.append("HTTP/1.1 \n\n", 11);
167 hdr_len = headersEnd(input.content(),input.contentSize());
168 CPPUNIT_ASSERT(!engine.sanityCheckStartLine(&input, hdr_len, &error) );
169 CPPUNIT_ASSERT_EQUAL(error, Http::scInvalidHeader);
170 input.reset();
171 error = Http::scNone;
172
173 input.append("HTTP/1.1 \n\n", 15);
174 hdr_len = headersEnd(input.content(),input.contentSize());
175 CPPUNIT_ASSERT(!engine.sanityCheckStartLine(&input, hdr_len, &error) );
176 CPPUNIT_ASSERT_EQUAL(error, Http::scInvalidHeader);
177 input.reset();
178 error = Http::scNone;
179
180 input.append("HTTP/1.1 Okay\n\n", 16); /* real case seen */
181 hdr_len = headersEnd(input.content(),input.contentSize());
182 CPPUNIT_ASSERT(!engine.sanityCheckStartLine(&input, hdr_len, &error) );
183 CPPUNIT_ASSERT_EQUAL(error, Http::scInvalidHeader);
184 input.reset();
185 error = Http::scNone;
186
187 // status line with nul-byte
188 input.append("HTTP/1.1" "\0" "200 Okay\n\n", 19); /* real case seen */
189 hdr_len = headersEnd(input.content(),input.contentSize());
190 CPPUNIT_ASSERT(!engine.sanityCheckStartLine(&input, hdr_len, &error) );
191 CPPUNIT_ASSERT_EQUAL(error, Http::scInvalidHeader);
192 input.reset();
193 error = Http::scNone;
194
195 // status line with negative status
196 input.append("HTTP/1.1 -000\n\n", 15); /* real case seen */
197 hdr_len = headersEnd(input.content(),input.contentSize());
198 CPPUNIT_ASSERT(!engine.sanityCheckStartLine(&input, hdr_len, &error) );
199 CPPUNIT_ASSERT_EQUAL(error, Http::scInvalidHeader);
200 input.reset();
201 error = Http::scNone;
202 }
203