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