testHttpReply.cc
Go to the documentation of this file.
1/*
2 * Copyright (C) 1996-2022 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
20
22
23/* stub functions to link successfully */
24
25#include "MemObject.h"
26int64_t
28{
29 return 0;
30}
31
32/* end */
33
34void
36{
37 Mem::Init();
39}
40
41void
43{
44 MemBuf input;
45 HttpReply engine;
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();
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();
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();
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();
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();
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();
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();
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();
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();
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();
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();
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();
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();
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();
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();
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();
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();
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();
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();
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();
203}
204
void httpHeaderInitModule(void)
Definition: HttpHeader.cc:119
void error(char *format,...)
virtual bool sanityCheckStartLine(const char *buf, const size_t hdr_len, Http::StatusCode *error)
Definition: HttpReply.cc:399
virtual void reset()
Definition: HttpReply.cc:57
String protoPrefix
Definition: HttpReply.h:60
Definition: MemBuf.h:24
virtual void append(const char *c, int sz)
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
the representation of the configuration. POD.
Definition: SquidConfig.h:77
void testSanityCheckFirstLine()
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
class SquidConfig Config
CPPUNIT_TEST_SUITE_REGISTRATION(testHttpReply)

 

Introduction

Documentation

Support

Miscellaneous

Web Site Translations

Mirrors