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