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