]> git.ipfire.org Git - thirdparty/squid.git/blob - src/tests/testHttpReply.cc
Merged from trunk
[thirdparty/squid.git] / src / tests / testHttpReply.cc
1 #include "config.h"
2 #include <cppunit/TestAssert.h>
3
4 #include "testHttpReply.h"
5 #include "HttpReply.h"
6 #include "Mem.h"
7
8 /* to avoid libsquid.la and its comm stuff */
9 #include "TextException.cc"
10
11 CPPUNIT_TEST_SUITE_REGISTRATION( testHttpReply );
12
13 struct SquidConfig Config;
14
15 /* stub functions to link successfully */
16
17 #include "Store.h"
18 void
19 StoreEntry::timestampsSet()
20 {
21 fatal("StoreEntry::timestampsSet. Not implemented.");
22 }
23
24 void
25 StoreEntry::setPublicKey()
26 {
27 fatal("StoreEntry::setPulicKey. Not implemented.");
28 }
29
30 #include "MemObject.h"
31 int64_t
32 MemObject::endOffset() const
33 {
34 return 0;
35 }
36
37 #include "ConfigParser.h"
38 void
39 ConfigParser::destruct()
40 {
41 // CALLED as shutdown no-op
42 // fatal("ConfigParser::destruct. Not implemented.");
43 }
44
45 void
46 eventAdd(const char *name, EVH * func, void *arg, double when, int, bool cbdata)
47 {
48 // CALLED as setUp no-op
49 // fatal("eventAdd. Not implemented.");
50 }
51
52 /* end */
53
54 void
55 testHttpReply::setUp()
56 {
57 Mem::Init();
58 httpHeaderInitModule();
59 }
60
61 void
62 testHttpReply::testSanityCheckFirstLine()
63 {
64 MemBuf input;
65 HttpReply engine;
66 http_status error = HTTP_STATUS_NONE;
67 size_t hdr_len;
68 input.init();
69
70 // a valid status line
71 input.append("HTTP/1.1 200 Okay\n\n", 19);
72 hdr_len = headersEnd(input.content(),input.contentSize());
73 CPPUNIT_ASSERT( 1 && engine.sanityCheckStartLine(&input, hdr_len, &error) );
74 CPPUNIT_ASSERT_EQUAL(error, HTTP_STATUS_NONE);
75 input.reset();
76 error = HTTP_STATUS_NONE;
77
78 input.append("HTTP/1.1 200 Okay \n\n", 28);
79 hdr_len = headersEnd(input.content(),input.contentSize());
80 CPPUNIT_ASSERT( 2 && engine.sanityCheckStartLine(&input, hdr_len, &error) );
81 CPPUNIT_ASSERT_EQUAL(error, HTTP_STATUS_NONE);
82 input.reset();
83 error = HTTP_STATUS_NONE;
84
85 #if TODO // these cases are only checked after parse...
86 // invalid status line
87 input.append("HTTP/1.1 999 Okay\n\n", 19);
88 hdr_len = headersEnd(input.content(),input.contentSize());
89 CPPUNIT_ASSERT( 3 && !engine.sanityCheckStartLine(&input, hdr_len, &error) );
90 CPPUNIT_ASSERT_EQUAL(error, HTTP_INVALID_HEADER);
91 input.reset();
92 error = HTTP_STATUS_NONE;
93
94 input.append("HTTP/1.1 2000 Okay \n\n", 29);
95 hdr_len = headersEnd(input.content(),input.contentSize());
96 CPPUNIT_ASSERT( 4 && engine.sanityCheckStartLine(&input, hdr_len, &error) );
97 CPPUNIT_ASSERT_EQUAL(error, HTTP_STATUS_NONE);
98 input.reset();
99 error = HTTP_STATUS_NONE;
100 #endif
101
102 // empty status line
103 input.append("\n\n", 2);
104 hdr_len = headersEnd(input.content(),input.contentSize());
105 CPPUNIT_ASSERT( 5 && !engine.sanityCheckStartLine(&input, hdr_len, &error) );
106 CPPUNIT_ASSERT_EQUAL(error, HTTP_INVALID_HEADER);
107 input.reset();
108 error = HTTP_STATUS_NONE;
109
110 input.append(" \n\n", 8);
111 hdr_len = headersEnd(input.content(),input.contentSize());
112 CPPUNIT_ASSERT( 6 && !engine.sanityCheckStartLine(&input, hdr_len, &error) );
113 CPPUNIT_ASSERT_EQUAL(error, HTTP_INVALID_HEADER);
114 input.reset();
115 error = HTTP_STATUS_NONE;
116
117 // status line with no message
118 input.append("HTTP/1.1 200\n\n", 14); /* real case seen */
119 hdr_len = headersEnd(input.content(),input.contentSize());
120 CPPUNIT_ASSERT(engine.sanityCheckStartLine(&input, hdr_len, &error) );
121 CPPUNIT_ASSERT_EQUAL(error, HTTP_STATUS_NONE);
122 input.reset();
123 error = HTTP_STATUS_NONE;
124
125 input.append("HTTP/1.1 200 \n\n", 15); /* real case seen */
126 hdr_len = headersEnd(input.content(),input.contentSize());
127 CPPUNIT_ASSERT(engine.sanityCheckStartLine(&input, hdr_len, &error) );
128 CPPUNIT_ASSERT_EQUAL(error, HTTP_STATUS_NONE);
129 input.reset();
130 error = HTTP_STATUS_NONE;
131
132 // incomplete (short) status lines... not sane (yet), but no error either.
133 input.append("H", 1);
134 hdr_len = headersEnd(input.content(),input.contentSize());
135 CPPUNIT_ASSERT(!engine.sanityCheckStartLine(&input, hdr_len, &error) );
136 CPPUNIT_ASSERT_EQUAL(error, HTTP_STATUS_NONE);
137 input.reset();
138 error = HTTP_STATUS_NONE;
139
140 input.append("HTTP/", 5);
141 hdr_len = headersEnd(input.content(),input.contentSize());
142 CPPUNIT_ASSERT(!engine.sanityCheckStartLine(&input, hdr_len, &error) );
143 CPPUNIT_ASSERT_EQUAL(error, HTTP_STATUS_NONE);
144 input.reset();
145 error = HTTP_STATUS_NONE;
146
147 input.append("HTTP/1", 6);
148 hdr_len = headersEnd(input.content(),input.contentSize());
149 CPPUNIT_ASSERT(!engine.sanityCheckStartLine(&input, hdr_len, &error) );
150 CPPUNIT_ASSERT_EQUAL(error, HTTP_STATUS_NONE);
151 input.reset();
152 error = HTTP_STATUS_NONE;
153
154 input.append("HTTP/1.1", 8);
155 hdr_len = headersEnd(input.content(),input.contentSize());
156 CPPUNIT_ASSERT(!engine.sanityCheckStartLine(&input, hdr_len, &error) );
157 CPPUNIT_ASSERT_EQUAL(error, HTTP_STATUS_NONE);
158 input.reset();
159 error = HTTP_STATUS_NONE;
160
161 input.append("HTTP/1.1 ", 9); /* real case seen */
162 hdr_len = headersEnd(input.content(),input.contentSize());
163 CPPUNIT_ASSERT(engine.sanityCheckStartLine(&input, hdr_len, &error) );
164 CPPUNIT_ASSERT_EQUAL(error, HTTP_STATUS_NONE);
165 input.reset();
166 error = HTTP_STATUS_NONE;
167
168 input.append("HTTP/1.1 20", 14);
169 hdr_len = headersEnd(input.content(),input.contentSize());
170 CPPUNIT_ASSERT(engine.sanityCheckStartLine(&input, hdr_len, &error) );
171 CPPUNIT_ASSERT_EQUAL(error, HTTP_STATUS_NONE);
172 input.reset();
173 error = HTTP_STATUS_NONE;
174
175 // status line with no status
176 input.append("HTTP/1.1 \n\n", 11);
177 hdr_len = headersEnd(input.content(),input.contentSize());
178 CPPUNIT_ASSERT(!engine.sanityCheckStartLine(&input, hdr_len, &error) );
179 CPPUNIT_ASSERT_EQUAL(error, HTTP_INVALID_HEADER);
180 input.reset();
181 error = HTTP_STATUS_NONE;
182
183 input.append("HTTP/1.1 \n\n", 15);
184 hdr_len = headersEnd(input.content(),input.contentSize());
185 CPPUNIT_ASSERT(!engine.sanityCheckStartLine(&input, hdr_len, &error) );
186 CPPUNIT_ASSERT_EQUAL(error, HTTP_INVALID_HEADER);
187 input.reset();
188 error = HTTP_STATUS_NONE;
189
190 input.append("HTTP/1.1 Okay\n\n", 16); /* real case seen */
191 hdr_len = headersEnd(input.content(),input.contentSize());
192 CPPUNIT_ASSERT(!engine.sanityCheckStartLine(&input, hdr_len, &error) );
193 CPPUNIT_ASSERT_EQUAL(error, HTTP_INVALID_HEADER);
194 input.reset();
195 error = HTTP_STATUS_NONE;
196
197 // status line with nul-byte
198 input.append("HTTP/1.1\0200 Okay\n\n", 19); /* real case seen */
199 hdr_len = headersEnd(input.content(),input.contentSize());
200 CPPUNIT_ASSERT(!engine.sanityCheckStartLine(&input, hdr_len, &error) );
201 CPPUNIT_ASSERT_EQUAL(error, HTTP_INVALID_HEADER);
202 input.reset();
203 error = HTTP_STATUS_NONE;
204
205 // status line with negative status
206 input.append("HTTP/1.1 -000\n\n", 15); /* real case seen */
207 hdr_len = headersEnd(input.content(),input.contentSize());
208 CPPUNIT_ASSERT(!engine.sanityCheckStartLine(&input, hdr_len, &error) );
209 CPPUNIT_ASSERT_EQUAL(error, HTTP_INVALID_HEADER);
210 input.reset();
211 error = HTTP_STATUS_NONE;
212
213 // status line with non-HTTP protocol
214 input.append("ICY/1.1 200 Okay\n\n", 18); /* real case seen */
215 hdr_len = headersEnd(input.content(),input.contentSize());
216 /* NP: for nw ICY is handled as a pass-thru */
217 /* Squid-3 will ignore it (and mangle the headers as per HTTP). */
218 CPPUNIT_ASSERT(!engine.sanityCheckStartLine(&input, hdr_len, &error) );
219 CPPUNIT_ASSERT_EQUAL(error, HTTP_INVALID_HEADER);
220 input.reset();
221 error = HTTP_STATUS_NONE;
222
223 }