blob: 35e05e202fb93a4784385e014909e4a5b4460570 [file] [log] [blame]
Ed Tanousaf4edf62020-07-21 08:46:25 -07001#include <http_utility.hpp>
2#include <multipart_parser.hpp>
3
4#include <map>
5
6#include "gmock/gmock.h"
7
8class MultipartTest : public ::testing::Test
9{
10 public:
11 boost::beast::http::request<boost::beast::http::string_body> req{};
12 MultipartParser parser;
13 std::error_code ec;
14};
15
16TEST_F(MultipartTest, TestGoodMultipartParser)
17{
18 req.set("Content-Type",
19 "multipart/form-data; "
20 "boundary=---------------------------d74496d66958873e");
21
22 req.body() = "-----------------------------d74496d66958873e\r\n"
23 "Content-Disposition: form-data; name=\"Test1\"\r\n\r\n"
24 "111111111111111111111111112222222222222222222222222222222\r\n"
25 "-----------------------------d74496d66958873e\r\n"
26 "Content-Disposition: form-data; name=\"Test2\"\r\n\r\n"
27 "{\r\n-----------------------------d74496d66958873e123456\r\n"
28 "-----------------------------d74496d66958873e\r\n"
29 "Content-Disposition: form-data; name=\"Test3\"\r\n\r\n"
30 "{\r\n--------d74496d6695887}\r\n"
31 "-----------------------------d74496d66958873e--\r\n";
32
33 crow::Request reqIn(req, ec);
34 ParserError rc = parser.parse(reqIn);
35 ASSERT_EQ(rc, ParserError::PARSER_SUCCESS);
36
37 ASSERT_EQ(parser.boundary,
38 "\r\n-----------------------------d74496d66958873e");
39 ASSERT_EQ(parser.mime_fields.size(), 3);
40
41 ASSERT_EQ(parser.mime_fields[0].fields.at("Content-Disposition"),
42 "form-data; name=\"Test1\"");
43 ASSERT_EQ(parser.mime_fields[0].content,
44 "111111111111111111111111112222222222222222222222222222222");
45
46 ASSERT_EQ(parser.mime_fields[1].fields.at("Content-Disposition"),
47 "form-data; name=\"Test2\"");
48 ASSERT_EQ(parser.mime_fields[1].content,
49 "{\r\n-----------------------------d74496d66958873e123456");
50 ASSERT_EQ(parser.mime_fields[2].fields.at("Content-Disposition"),
51 "form-data; name=\"Test3\"");
52 ASSERT_EQ(parser.mime_fields[2].content, "{\r\n--------d74496d6695887}");
53}
54
55TEST_F(MultipartTest, TestBadMultipartParser1)
56{
57 req.set("Content-Type",
58 "multipart/form-data; "
59 "boundary=---------------------------d74496d66958873e");
60
61 req.body() = "-----------------------------d74496d66958873e\r\n"
62 "Content-Disposition: form-data; name=\"Test1\"\r\n\r\n"
63 "1234567890\r\n"
64 "-----------------------------d74496d66958873e\r-\r\n";
65
66 crow::Request reqIn(req, ec);
67 ParserError rc = parser.parse(reqIn);
68 ASSERT_EQ(rc, ParserError::PARSER_SUCCESS);
69
70 ASSERT_EQ(parser.boundary,
71 "\r\n-----------------------------d74496d66958873e");
72 ASSERT_EQ(parser.mime_fields.size(), 1);
73}
74
75TEST_F(MultipartTest, TestBadMultipartParser2)
76{
77 req.set("Content-Type",
78 "multipart/form-data; "
79 "boundary=---------------------------d74496d66958873e");
80
81 req.body() = "-----------------------------d74496d66958873e\r\n"
82 "Content-Disposition: form-data; name=\"Test1\"\r\n\r\n"
83 "abcd\r\n"
84 "-----------------------------d74496d66958873e-\r\n";
85
86 crow::Request reqIn(req, ec);
87 ParserError rc = parser.parse(reqIn);
88 ASSERT_EQ(rc, ParserError::PARSER_SUCCESS);
89
90 ASSERT_EQ(parser.boundary,
91 "\r\n-----------------------------d74496d66958873e");
92 ASSERT_EQ(parser.mime_fields.size(), 1);
93}
94
95TEST_F(MultipartTest, TestErrorBoundaryFormat)
96{
97 req.set("Content-Type",
98 "multipart/form-data; "
99 "boundary+=-----------------------------d74496d66958873e");
100
101 req.body() = "-----------------------------d74496d66958873e\r\n"
102 "Content-Disposition: form-data; name=\"Test1\"\r\n\r\n"
103 "{\"Key1\": 11223333333333333333333333333333333333333333}\r\n"
104 "-----------------------------d74496d66958873e\r\n"
105 "Content-Disposition: form-data; name=\"Test2\"\r\n\r\n"
106 "123456\r\n"
107 "-----------------------------d74496d66958873e--\r\n";
108
109 crow::Request reqIn(req, ec);
110 ASSERT_EQ(parser.parse(reqIn), ParserError::ERROR_BOUNDARY_FORMAT);
111}
112
113TEST_F(MultipartTest, TestErrorBoundaryCR)
114{
115 req.set("Content-Type",
116 "multipart/form-data; "
117 "boundary=---------------------------d74496d66958873e");
118
119 req.body() = "-----------------------------d74496d66958873e"
120 "Content-Disposition: form-data; name=\"Test1\"\r\n\r"
121 "{\"Key1\": 112233}\r\n"
122 "-----------------------------d74496d66958873e\r\n"
123 "Content-Disposition: form-data; name=\"Test2\"\r\n\r\n"
124 "123456\r\n"
125 "-----------------------------d74496d66958873e--\r\n";
126
127 crow::Request reqIn(req, ec);
128 ASSERT_EQ(parser.parse(reqIn), ParserError::ERROR_BOUNDARY_CR);
129}
130
131TEST_F(MultipartTest, TestErrorBoundaryLF)
132{
133 req.set("Content-Type",
134 "multipart/form-data; "
135 "boundary=---------------------------d74496d66958873e");
136
137 req.body() = "-----------------------------d74496d66958873e\r"
138 "Content-Disposition: form-data; name=\"Test1\"\r\n\r\n"
139 "{\"Key1\": 112233}\r\n"
140 "-----------------------------d74496d66958873e\r\n"
141 "Content-Disposition: form-data; name=\"Test2\"\r\n\r\n"
142 "123456\r\n"
143 "-----------------------------d74496d66958873e--\r\n";
144
145 crow::Request reqIn(req, ec);
146 ASSERT_EQ(parser.parse(reqIn), ParserError::ERROR_BOUNDARY_LF);
147}
148
149TEST_F(MultipartTest, TestErrorBoundaryData)
150{
151 req.set("Content-Type",
152 "multipart/form-data; "
153 "boundary=---------------------------d7449sd6d66958873e");
154
155 req.body() = "-----------------------------d74496d66958873e\r\n"
156 "Content-Disposition: form-data; name=\"Test1\"\r\n\r\n"
157 "{\"Key1\": 112233}\r\n"
158 "-----------------------------d74496d66958873e\r\n"
159 "Content-Disposition: form-data; name=\"Test2\"\r\n\r\n"
160 "123456\r\n"
161 "-----------------------------d74496d66958873e--\r\n";
162
163 crow::Request reqIn(req, ec);
164 ASSERT_EQ(parser.parse(reqIn), ParserError::ERROR_BOUNDARY_DATA);
165}
166
167TEST_F(MultipartTest, TestErrorEmptyHeader)
168{
169 req.set("Content-Type",
170 "multipart/form-data; "
171 "boundary=---------------------------d74496d66958873e");
172
173 req.body() = "-----------------------------d74496d66958873e\r\n"
174 ": form-data; name=\"Test1\"\r\n"
175 "{\"Key1\": 112233}\r\n"
176 "-----------------------------d74496d66958873e\r\n"
177 "Content-Disposition: form-data; name=\"Test2\"\r\n"
178 "123456\r\n"
179 "-----------------------------d74496d66958873e--\r\n";
180
181 crow::Request reqIn(req, ec);
182 ASSERT_EQ(parser.parse(reqIn), ParserError::ERROR_EMPTY_HEADER);
183}
184
185TEST_F(MultipartTest, TestErrorHeaderName)
186{
187 req.set("Content-Type",
188 "multipart/form-data; "
189 "boundary=---------------------------d74496d66958873e");
190
191 req.body() = "-----------------------------d74496d66958873e\r\n"
192 "Content-!!Disposition: form-data; name=\"Test1\"\r\n"
193 "{\"Key1\": 112233}\r\n"
194 "-----------------------------d74496d66958873e\r\n"
195 "Content-Disposition: form-data; name=\"Test2\"\r\n\r\n"
196 "123456\r\n"
197 "-----------------------------d74496d66958873e--\r\n";
198
199 crow::Request reqIn(req, ec);
200 ASSERT_EQ(parser.parse(reqIn), ParserError::ERROR_HEADER_NAME);
201}
202
203TEST_F(MultipartTest, TestErrorHeaderValue)
204{
205 req.set("Content-Type",
206 "multipart/form-data; "
207 "boundary=---------------------------d74496d66958873e");
208
209 req.body() = "-----------------------------d74496d66958873e\r\n"
210 "Content-Disposition: form-data; name=\"Test1\"\r"
211 "{\"Key1\": 112233}\r\n"
212 "-----------------------------d74496d66958873e\r\n"
213 "Content-Disposition: form-data; name=\"Test2\"\r\n\r\n"
214 "123456\r\n"
215 "-----------------------------d74496d66958873e--\r\n";
216
217 crow::Request reqIn(req, ec);
218 ASSERT_EQ(parser.parse(reqIn), ParserError::ERROR_HEADER_VALUE);
219}
220
221TEST_F(MultipartTest, TestErrorHeaderEnding)
222{
223 req.set("Content-Type",
224 "multipart/form-data; "
225 "boundary=---------------------------d74496d66958873e");
226
227 req.body() = "-----------------------------d74496d66958873e\r\n"
228 "Content-Disposition: form-data; name=\"Test1\"\r\n\r"
229 "{\"Key1\": 112233}\r\n"
230 "-----------------------------d74496d66958873e\r\n"
231 "Content-Disposition: form-data; name=\"Test2\"\r\n\r\n"
232 "123456\r\n"
233 "-----------------------------d74496d66958873e--\r\n";
234
235 crow::Request reqIn(req, ec);
236 ASSERT_EQ(parser.parse(reqIn), ParserError::ERROR_HEADER_ENDING);
237}