]>
Commit | Line | Data |
---|---|---|
4e0938ef | 1 | /* |
77b1029d | 2 | * Copyright (C) 1996-2020 The Squid Software Foundation and contributors |
4e0938ef AJ |
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 | ||
f7f3304a | 9 | #include "squid.h" |
0f9db2d6 AJ |
10 | #include <cppunit/TestAssert.h> |
11 | ||
ec6f82c1 | 12 | #include "HttpHeader.h" |
0f9db2d6 | 13 | #include "HttpReply.h" |
b6149797 | 14 | #include "mime_header.h" |
4d5904f7 | 15 | #include "SquidConfig.h" |
602d9612 | 16 | #include "testHttpReply.h" |
7f861c77 AJ |
17 | #include "unitTestMain.h" |
18 | ||
0f9db2d6 AJ |
19 | CPPUNIT_TEST_SUITE_REGISTRATION( testHttpReply ); |
20 | ||
4d5904f7 | 21 | class SquidConfig Config; |
0f9db2d6 AJ |
22 | |
23 | /* stub functions to link successfully */ | |
24 | ||
0f9db2d6 AJ |
25 | #include "MemObject.h" |
26 | int64_t | |
27 | MemObject::endOffset() const | |
28 | { | |
29 | return 0; | |
30 | } | |
31 | ||
0f9db2d6 AJ |
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; | |
955394ce | 46 | Http::StatusCode error = Http::scNone; |
0f9db2d6 AJ |
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()); | |
84ae6223 | 53 | CPPUNIT_ASSERT( 1 && engine.sanityCheckStartLine(input.content(), hdr_len, &error) ); |
955394ce | 54 | CPPUNIT_ASSERT_EQUAL(error, Http::scNone); |
0f9db2d6 | 55 | input.reset(); |
955394ce | 56 | error = Http::scNone; |
0f9db2d6 AJ |
57 | |
58 | input.append("HTTP/1.1 200 Okay \n\n", 28); | |
59 | hdr_len = headersEnd(input.content(),input.contentSize()); | |
84ae6223 | 60 | CPPUNIT_ASSERT( 2 && engine.sanityCheckStartLine(input.content(), hdr_len, &error) ); |
955394ce | 61 | CPPUNIT_ASSERT_EQUAL(error, Http::scNone); |
0f9db2d6 | 62 | input.reset(); |
955394ce | 63 | error = Http::scNone; |
0f9db2d6 AJ |
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()); | |
84ae6223 | 69 | CPPUNIT_ASSERT( 3 && !engine.sanityCheckStartLine(input.content(), hdr_len, &error) ); |
955394ce | 70 | CPPUNIT_ASSERT_EQUAL(error, Http::scInvalidHeader); |
0f9db2d6 | 71 | input.reset(); |
955394ce | 72 | error = Http::scNone; |
0f9db2d6 AJ |
73 | |
74 | input.append("HTTP/1.1 2000 Okay \n\n", 29); | |
75 | hdr_len = headersEnd(input.content(),input.contentSize()); | |
84ae6223 | 76 | CPPUNIT_ASSERT( 4 && engine.sanityCheckStartLine(input.content(), hdr_len, &error) ); |
955394ce | 77 | CPPUNIT_ASSERT_EQUAL(error, Http::scNone); |
0f9db2d6 | 78 | input.reset(); |
955394ce | 79 | error = Http::scNone; |
0f9db2d6 AJ |
80 | #endif |
81 | ||
e77d7ef0 | 82 | // valid ICY protocol status line |
d82a234d | 83 | input.append("ICY 200 Okay\n\n", 14); |
e77d7ef0 | 84 | hdr_len = headersEnd(input.content(),input.contentSize()); |
84ae6223 | 85 | CPPUNIT_ASSERT( engine.sanityCheckStartLine(input.content(), hdr_len, &error) ); |
955394ce | 86 | CPPUNIT_ASSERT_EQUAL(error, Http::scNone); |
e77d7ef0 | 87 | input.reset(); |
955394ce | 88 | error = Http::scNone; |
0f741291 AJ |
89 | /* NP: the engine saves details about the protocol. even when being reset :( */ |
90 | engine.protoPrefix="HTTP/"; | |
91 | engine.reset(); | |
e77d7ef0 | 92 | |
0f9db2d6 AJ |
93 | // empty status line |
94 | input.append("\n\n", 2); | |
95 | hdr_len = headersEnd(input.content(),input.contentSize()); | |
84ae6223 | 96 | CPPUNIT_ASSERT( 5 && !engine.sanityCheckStartLine(input.content(), hdr_len, &error) ); |
955394ce | 97 | CPPUNIT_ASSERT_EQUAL(error, Http::scInvalidHeader); |
0f9db2d6 | 98 | input.reset(); |
955394ce | 99 | error = Http::scNone; |
0f9db2d6 AJ |
100 | |
101 | input.append(" \n\n", 8); | |
102 | hdr_len = headersEnd(input.content(),input.contentSize()); | |
84ae6223 | 103 | CPPUNIT_ASSERT( 6 && !engine.sanityCheckStartLine(input.content(), hdr_len, &error) ); |
955394ce | 104 | CPPUNIT_ASSERT_EQUAL(error, Http::scInvalidHeader); |
0f9db2d6 | 105 | input.reset(); |
955394ce | 106 | error = Http::scNone; |
0f9db2d6 AJ |
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()); | |
84ae6223 | 111 | CPPUNIT_ASSERT(engine.sanityCheckStartLine(input.content(), hdr_len, &error) ); |
955394ce | 112 | CPPUNIT_ASSERT_EQUAL(error, Http::scNone); |
0f9db2d6 | 113 | input.reset(); |
955394ce | 114 | error = Http::scNone; |
0f9db2d6 AJ |
115 | |
116 | input.append("HTTP/1.1 200 \n\n", 15); /* real case seen */ | |
117 | hdr_len = headersEnd(input.content(),input.contentSize()); | |
84ae6223 | 118 | CPPUNIT_ASSERT(engine.sanityCheckStartLine(input.content(), hdr_len, &error) ); |
955394ce | 119 | CPPUNIT_ASSERT_EQUAL(error, Http::scNone); |
0f9db2d6 | 120 | input.reset(); |
955394ce | 121 | error = Http::scNone; |
0f9db2d6 | 122 | |
6934ca51 AJ |
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()); | |
84ae6223 | 126 | CPPUNIT_ASSERT(!engine.sanityCheckStartLine(input.content(), hdr_len, &error) ); |
955394ce | 127 | CPPUNIT_ASSERT_EQUAL(error, Http::scNone); |
6934ca51 | 128 | input.reset(); |
955394ce | 129 | error = Http::scNone; |
6934ca51 AJ |
130 | |
131 | input.append("HTTP/", 5); | |
132 | hdr_len = headersEnd(input.content(),input.contentSize()); | |
84ae6223 | 133 | CPPUNIT_ASSERT(!engine.sanityCheckStartLine(input.content(), hdr_len, &error) ); |
955394ce | 134 | CPPUNIT_ASSERT_EQUAL(error, Http::scNone); |
6934ca51 | 135 | input.reset(); |
955394ce | 136 | error = Http::scNone; |
6934ca51 AJ |
137 | |
138 | input.append("HTTP/1", 6); | |
139 | hdr_len = headersEnd(input.content(),input.contentSize()); | |
84ae6223 | 140 | CPPUNIT_ASSERT(!engine.sanityCheckStartLine(input.content(), hdr_len, &error) ); |
955394ce | 141 | CPPUNIT_ASSERT_EQUAL(error, Http::scNone); |
6934ca51 | 142 | input.reset(); |
955394ce | 143 | error = Http::scNone; |
6934ca51 AJ |
144 | |
145 | input.append("HTTP/1.1", 8); | |
146 | hdr_len = headersEnd(input.content(),input.contentSize()); | |
84ae6223 | 147 | CPPUNIT_ASSERT(!engine.sanityCheckStartLine(input.content(), hdr_len, &error) ); |
955394ce | 148 | CPPUNIT_ASSERT_EQUAL(error, Http::scNone); |
6934ca51 | 149 | input.reset(); |
955394ce | 150 | error = Http::scNone; |
6934ca51 AJ |
151 | |
152 | input.append("HTTP/1.1 ", 9); /* real case seen */ | |
153 | hdr_len = headersEnd(input.content(),input.contentSize()); | |
84ae6223 | 154 | CPPUNIT_ASSERT(!engine.sanityCheckStartLine(input.content(), hdr_len, &error) ); |
955394ce | 155 | CPPUNIT_ASSERT_EQUAL(error, Http::scNone); |
6934ca51 | 156 | input.reset(); |
955394ce | 157 | error = Http::scNone; |
6934ca51 AJ |
158 | |
159 | input.append("HTTP/1.1 20", 14); | |
160 | hdr_len = headersEnd(input.content(),input.contentSize()); | |
84ae6223 | 161 | CPPUNIT_ASSERT(!engine.sanityCheckStartLine(input.content(), hdr_len, &error) ); |
955394ce | 162 | CPPUNIT_ASSERT_EQUAL(error, Http::scNone); |
6934ca51 | 163 | input.reset(); |
955394ce | 164 | error = Http::scNone; |
0f9db2d6 AJ |
165 | |
166 | // status line with no status | |
167 | input.append("HTTP/1.1 \n\n", 11); | |
168 | hdr_len = headersEnd(input.content(),input.contentSize()); | |
84ae6223 | 169 | CPPUNIT_ASSERT(!engine.sanityCheckStartLine(input.content(), hdr_len, &error) ); |
955394ce | 170 | CPPUNIT_ASSERT_EQUAL(error, Http::scInvalidHeader); |
0f9db2d6 | 171 | input.reset(); |
955394ce | 172 | error = Http::scNone; |
0f9db2d6 AJ |
173 | |
174 | input.append("HTTP/1.1 \n\n", 15); | |
175 | hdr_len = headersEnd(input.content(),input.contentSize()); | |
84ae6223 | 176 | CPPUNIT_ASSERT(!engine.sanityCheckStartLine(input.content(), hdr_len, &error) ); |
955394ce | 177 | CPPUNIT_ASSERT_EQUAL(error, Http::scInvalidHeader); |
0f9db2d6 | 178 | input.reset(); |
955394ce | 179 | error = Http::scNone; |
0f9db2d6 AJ |
180 | |
181 | input.append("HTTP/1.1 Okay\n\n", 16); /* real case seen */ | |
182 | hdr_len = headersEnd(input.content(),input.contentSize()); | |
84ae6223 | 183 | CPPUNIT_ASSERT(!engine.sanityCheckStartLine(input.content(), hdr_len, &error) ); |
955394ce | 184 | CPPUNIT_ASSERT_EQUAL(error, Http::scInvalidHeader); |
0f9db2d6 | 185 | input.reset(); |
955394ce | 186 | error = Http::scNone; |
0f9db2d6 AJ |
187 | |
188 | // status line with nul-byte | |
467d3e2d | 189 | input.append("HTTP/1.1" "\0" "200 Okay\n\n", 19); /* real case seen */ |
0f9db2d6 | 190 | hdr_len = headersEnd(input.content(),input.contentSize()); |
84ae6223 | 191 | CPPUNIT_ASSERT(!engine.sanityCheckStartLine(input.content(), hdr_len, &error) ); |
955394ce | 192 | CPPUNIT_ASSERT_EQUAL(error, Http::scInvalidHeader); |
0f9db2d6 | 193 | input.reset(); |
955394ce | 194 | error = Http::scNone; |
0f9db2d6 AJ |
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()); | |
84ae6223 | 199 | CPPUNIT_ASSERT(!engine.sanityCheckStartLine(input.content(), hdr_len, &error) ); |
955394ce | 200 | CPPUNIT_ASSERT_EQUAL(error, Http::scInvalidHeader); |
0f9db2d6 | 201 | input.reset(); |
955394ce | 202 | error = Http::scNone; |
0f9db2d6 | 203 | } |
f53969cc | 204 |