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