testHttpReply.cc
Go to the documentation of this file.
1/*
2 * Copyright (C) 1996-2023 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 "compat/cppunit.h"
13#include "HttpHeader.h"
14#include "HttpReply.h"
15#include "mime_header.h"
16#include "SquidConfig.h"
17#include "unitTestMain.h"
18
19class TestHttpReply : public CPPUNIT_NS::TestFixture
20{
24
25protected:
27};
28
30
32
33/* stub functions to link successfully */
34
35#include "MemObject.h"
36int64_t
38{
39 return 0;
40}
41
42/* end */
43
45class MyTestProgram: public TestProgram
46{
47public:
48 /* TestProgram API */
49 void startup() override;
50};
51
52void
54{
55 Mem::Init();
57}
58
59void
61{
62 MemBuf input;
63 HttpReply engine;
65 size_t hdr_len;
66 input.init();
67
68 // a valid status line
69 input.append("HTTP/1.1 200 Okay\n\n", 19);
70 hdr_len = headersEnd(input.content(),input.contentSize());
71 CPPUNIT_ASSERT( 1 && engine.sanityCheckStartLine(input.content(), hdr_len, &error) );
72 CPPUNIT_ASSERT_EQUAL(error, Http::scNone);
73 input.reset();
75
76 input.append("HTTP/1.1 200 Okay \n\n", 28);
77 hdr_len = headersEnd(input.content(),input.contentSize());
78 CPPUNIT_ASSERT( 2 && engine.sanityCheckStartLine(input.content(), hdr_len, &error) );
79 CPPUNIT_ASSERT_EQUAL(error, Http::scNone);
80 input.reset();
82
83#if TODO // these cases are only checked after parse...
84 // invalid status line
85 input.append("HTTP/1.1 999 Okay\n\n", 19);
86 hdr_len = headersEnd(input.content(),input.contentSize());
87 CPPUNIT_ASSERT( 3 && !engine.sanityCheckStartLine(input.content(), hdr_len, &error) );
88 CPPUNIT_ASSERT_EQUAL(error, Http::scInvalidHeader);
89 input.reset();
91
92 input.append("HTTP/1.1 2000 Okay \n\n", 29);
93 hdr_len = headersEnd(input.content(),input.contentSize());
94 CPPUNIT_ASSERT( 4 && engine.sanityCheckStartLine(input.content(), hdr_len, &error) );
95 CPPUNIT_ASSERT_EQUAL(error, Http::scNone);
96 input.reset();
98#endif
99
100 // valid ICY protocol status line
101 input.append("ICY 200 Okay\n\n", 14);
102 hdr_len = headersEnd(input.content(),input.contentSize());
103 CPPUNIT_ASSERT( engine.sanityCheckStartLine(input.content(), hdr_len, &error) );
104 CPPUNIT_ASSERT_EQUAL(error, Http::scNone);
105 input.reset();
107 /* NP: the engine saves details about the protocol. even when being reset :( */
108 engine.protoPrefix="HTTP/";
109 engine.reset();
110
111 // empty status line
112 input.append("\n\n", 2);
113 hdr_len = headersEnd(input.content(),input.contentSize());
114 CPPUNIT_ASSERT( 5 && !engine.sanityCheckStartLine(input.content(), hdr_len, &error) );
115 CPPUNIT_ASSERT_EQUAL(error, Http::scInvalidHeader);
116 input.reset();
118
119 input.append(" \n\n", 8);
120 hdr_len = headersEnd(input.content(),input.contentSize());
121 CPPUNIT_ASSERT( 6 && !engine.sanityCheckStartLine(input.content(), hdr_len, &error) );
122 CPPUNIT_ASSERT_EQUAL(error, Http::scInvalidHeader);
123 input.reset();
125
126 // status line with no message
127 input.append("HTTP/1.1 200\n\n", 14); /* real case seen */
128 hdr_len = headersEnd(input.content(),input.contentSize());
129 CPPUNIT_ASSERT(engine.sanityCheckStartLine(input.content(), hdr_len, &error) );
130 CPPUNIT_ASSERT_EQUAL(error, Http::scNone);
131 input.reset();
133
134 input.append("HTTP/1.1 200 \n\n", 15); /* real case seen */
135 hdr_len = headersEnd(input.content(),input.contentSize());
136 CPPUNIT_ASSERT(engine.sanityCheckStartLine(input.content(), hdr_len, &error) );
137 CPPUNIT_ASSERT_EQUAL(error, Http::scNone);
138 input.reset();
140
141 // incomplete (short) status lines... not sane (yet), but no error either.
142 input.append("H", 1);
143 hdr_len = headersEnd(input.content(),input.contentSize());
144 CPPUNIT_ASSERT(!engine.sanityCheckStartLine(input.content(), hdr_len, &error) );
145 CPPUNIT_ASSERT_EQUAL(error, Http::scNone);
146 input.reset();
148
149 input.append("HTTP/", 5);
150 hdr_len = headersEnd(input.content(),input.contentSize());
151 CPPUNIT_ASSERT(!engine.sanityCheckStartLine(input.content(), hdr_len, &error) );
152 CPPUNIT_ASSERT_EQUAL(error, Http::scNone);
153 input.reset();
155
156 input.append("HTTP/1", 6);
157 hdr_len = headersEnd(input.content(),input.contentSize());
158 CPPUNIT_ASSERT(!engine.sanityCheckStartLine(input.content(), hdr_len, &error) );
159 CPPUNIT_ASSERT_EQUAL(error, Http::scNone);
160 input.reset();
162
163 input.append("HTTP/1.1", 8);
164 hdr_len = headersEnd(input.content(),input.contentSize());
165 CPPUNIT_ASSERT(!engine.sanityCheckStartLine(input.content(), hdr_len, &error) );
166 CPPUNIT_ASSERT_EQUAL(error, Http::scNone);
167 input.reset();
169
170 input.append("HTTP/1.1 ", 9); /* real case seen */
171 hdr_len = headersEnd(input.content(),input.contentSize());
172 CPPUNIT_ASSERT(!engine.sanityCheckStartLine(input.content(), hdr_len, &error) );
173 CPPUNIT_ASSERT_EQUAL(error, Http::scNone);
174 input.reset();
176
177 input.append("HTTP/1.1 20", 14);
178 hdr_len = headersEnd(input.content(),input.contentSize());
179 CPPUNIT_ASSERT(!engine.sanityCheckStartLine(input.content(), hdr_len, &error) );
180 CPPUNIT_ASSERT_EQUAL(error, Http::scNone);
181 input.reset();
183
184 // status line with no status
185 input.append("HTTP/1.1 \n\n", 11);
186 hdr_len = headersEnd(input.content(),input.contentSize());
187 CPPUNIT_ASSERT(!engine.sanityCheckStartLine(input.content(), hdr_len, &error) );
188 CPPUNIT_ASSERT_EQUAL(error, Http::scInvalidHeader);
189 input.reset();
191
192 input.append("HTTP/1.1 \n\n", 15);
193 hdr_len = headersEnd(input.content(),input.contentSize());
194 CPPUNIT_ASSERT(!engine.sanityCheckStartLine(input.content(), hdr_len, &error) );
195 CPPUNIT_ASSERT_EQUAL(error, Http::scInvalidHeader);
196 input.reset();
198
199 input.append("HTTP/1.1 Okay\n\n", 16); /* real case seen */
200 hdr_len = headersEnd(input.content(),input.contentSize());
201 CPPUNIT_ASSERT(!engine.sanityCheckStartLine(input.content(), hdr_len, &error) );
202 CPPUNIT_ASSERT_EQUAL(error, Http::scInvalidHeader);
203 input.reset();
205
206 // status line with nul-byte
207 input.append("HTTP/1.1" "\0" "200 Okay\n\n", 19); /* real case seen */
208 hdr_len = headersEnd(input.content(),input.contentSize());
209 CPPUNIT_ASSERT(!engine.sanityCheckStartLine(input.content(), hdr_len, &error) );
210 CPPUNIT_ASSERT_EQUAL(error, Http::scInvalidHeader);
211 input.reset();
213
214 // status line with negative status
215 input.append("HTTP/1.1 -000\n\n", 15); /* real case seen */
216 hdr_len = headersEnd(input.content(),input.contentSize());
217 CPPUNIT_ASSERT(!engine.sanityCheckStartLine(input.content(), hdr_len, &error) );
218 CPPUNIT_ASSERT_EQUAL(error, Http::scInvalidHeader);
219 input.reset();
221}
222
223int
224main(int argc, char *argv[])
225{
226 return MyTestProgram().run(argc, argv);
227}
228
void httpHeaderInitModule(void)
Definition: HttpHeader.cc:121
void error(char *format,...)
bool sanityCheckStartLine(const char *buf, const size_t hdr_len, Http::StatusCode *error) override
Definition: HttpReply.cc:402
void reset() override
Definition: HttpReply.cc:59
String protoPrefix
Definition: HttpReply.h:60
Definition: MemBuf.h:24
void append(const char *c, int sz) override
Definition: MemBuf.cc:209
void init(mb_size_t szInit, mb_size_t szMax)
Definition: MemBuf.cc:93
char * content()
start of the added data
Definition: MemBuf.h:41
mb_size_t contentSize() const
available data size
Definition: MemBuf.h:47
void reset()
Definition: MemBuf.cc:129
int64_t endOffset() const
Definition: MemObject.cc:214
customizes our test setup
void startup() override
the representation of the configuration. POD.
Definition: SquidConfig.h:79
void testSanityCheckFirstLine()
CPPUNIT_TEST_SUITE(TestHttpReply)
CPPUNIT_TEST(testSanityCheckFirstLine)
implements test program's main() function while enabling customization
Definition: unitTestMain.h:26
int run(int argc, char *argv[])
Definition: unitTestMain.h:44
size_t headersEnd(const char *mime, size_t l, bool &containsObsFold)
Definition: mime_header.cc:17
StatusCode
Definition: StatusCode.h:20
@ scInvalidHeader
Definition: StatusCode.h:86
@ scNone
Definition: StatusCode.h:21
void Init()
Definition: old_api.cc:425
int main(int argc, char *argv[])
class SquidConfig Config
CPPUNIT_TEST_SUITE_REGISTRATION(TestHttpReply)

 

Introduction

Documentation

Support

Miscellaneous

Web Site Translations

Mirrors