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