Nan Zhou | ca0cb4c | 2022-06-21 17:47:09 +0000 | [diff] [blame] | 1 | #include "http/http_request.hpp" |
| 2 | #include "multipart_parser.hpp" |
| 3 | |
Nan Zhou | d5c80ad | 2022-07-11 01:16:31 +0000 | [diff] [blame] | 4 | #include <boost/beast/http/fields.hpp> |
Nan Zhou | ca0cb4c | 2022-06-21 17:47:09 +0000 | [diff] [blame] | 5 | #include <boost/beast/http/message.hpp> |
| 6 | #include <boost/beast/http/string_body.hpp> |
Ed Tanous | af4edf6 | 2020-07-21 08:46:25 -0700 | [diff] [blame] | 7 | |
Nan Zhou | d5c80ad | 2022-07-11 01:16:31 +0000 | [diff] [blame] | 8 | #include <memory> |
| 9 | #include <string_view> |
Nan Zhou | ca0cb4c | 2022-06-21 17:47:09 +0000 | [diff] [blame] | 10 | #include <system_error> |
Nan Zhou | d5c80ad | 2022-07-11 01:16:31 +0000 | [diff] [blame] | 11 | #include <vector> |
Ed Tanous | af4edf6 | 2020-07-21 08:46:25 -0700 | [diff] [blame] | 12 | |
Nan Zhou | d5c80ad | 2022-07-11 01:16:31 +0000 | [diff] [blame] | 13 | #include <gtest/gtest.h> // IWYU pragma: keep |
| 14 | |
| 15 | // IWYU pragma: no_include <gtest/gtest-message.h> |
| 16 | // IWYU pragma: no_include <gtest/gtest-test-part.h> |
| 17 | // IWYU pragma: no_include "gtest/gtest_pred_impl.h" |
| 18 | // IWYU pragma: no_include <boost/beast/http/impl/fields.hpp> |
| 19 | // IWYU pragma: no_include <boost/intrusive/detail/list_iterator.hpp> |
| 20 | // IWYU pragma: no_include <boost/intrusive/detail/tree_iterator.hpp> |
Ed Tanous | af4edf6 | 2020-07-21 08:46:25 -0700 | [diff] [blame] | 21 | |
Nan Zhou | e52d156 | 2022-06-21 17:47:15 +0000 | [diff] [blame] | 22 | namespace |
| 23 | { |
| 24 | using ::testing::Test; |
| 25 | |
| 26 | class MultipartTest : public Test |
Ed Tanous | af4edf6 | 2020-07-21 08:46:25 -0700 | [diff] [blame] | 27 | { |
| 28 | public: |
| 29 | boost::beast::http::request<boost::beast::http::string_body> req{}; |
| 30 | MultipartParser parser; |
| 31 | std::error_code ec; |
| 32 | }; |
| 33 | |
| 34 | TEST_F(MultipartTest, TestGoodMultipartParser) |
| 35 | { |
| 36 | req.set("Content-Type", |
| 37 | "multipart/form-data; " |
| 38 | "boundary=---------------------------d74496d66958873e"); |
| 39 | |
| 40 | req.body() = "-----------------------------d74496d66958873e\r\n" |
| 41 | "Content-Disposition: form-data; name=\"Test1\"\r\n\r\n" |
| 42 | "111111111111111111111111112222222222222222222222222222222\r\n" |
| 43 | "-----------------------------d74496d66958873e\r\n" |
| 44 | "Content-Disposition: form-data; name=\"Test2\"\r\n\r\n" |
| 45 | "{\r\n-----------------------------d74496d66958873e123456\r\n" |
| 46 | "-----------------------------d74496d66958873e\r\n" |
| 47 | "Content-Disposition: form-data; name=\"Test3\"\r\n\r\n" |
| 48 | "{\r\n--------d74496d6695887}\r\n" |
| 49 | "-----------------------------d74496d66958873e--\r\n"; |
| 50 | |
| 51 | crow::Request reqIn(req, ec); |
| 52 | ParserError rc = parser.parse(reqIn); |
| 53 | ASSERT_EQ(rc, ParserError::PARSER_SUCCESS); |
| 54 | |
Nan Zhou | cc60619 | 2022-06-21 17:47:19 +0000 | [diff] [blame] | 55 | EXPECT_EQ(parser.boundary, |
Ed Tanous | af4edf6 | 2020-07-21 08:46:25 -0700 | [diff] [blame] | 56 | "\r\n-----------------------------d74496d66958873e"); |
Nan Zhou | cc60619 | 2022-06-21 17:47:19 +0000 | [diff] [blame] | 57 | EXPECT_EQ(parser.mime_fields.size(), 3); |
Ed Tanous | af4edf6 | 2020-07-21 08:46:25 -0700 | [diff] [blame] | 58 | |
Nan Zhou | cc60619 | 2022-06-21 17:47:19 +0000 | [diff] [blame] | 59 | EXPECT_EQ(parser.mime_fields[0].fields.at("Content-Disposition"), |
Ed Tanous | af4edf6 | 2020-07-21 08:46:25 -0700 | [diff] [blame] | 60 | "form-data; name=\"Test1\""); |
Nan Zhou | cc60619 | 2022-06-21 17:47:19 +0000 | [diff] [blame] | 61 | EXPECT_EQ(parser.mime_fields[0].content, |
Ed Tanous | af4edf6 | 2020-07-21 08:46:25 -0700 | [diff] [blame] | 62 | "111111111111111111111111112222222222222222222222222222222"); |
| 63 | |
Nan Zhou | cc60619 | 2022-06-21 17:47:19 +0000 | [diff] [blame] | 64 | EXPECT_EQ(parser.mime_fields[1].fields.at("Content-Disposition"), |
Ed Tanous | af4edf6 | 2020-07-21 08:46:25 -0700 | [diff] [blame] | 65 | "form-data; name=\"Test2\""); |
Nan Zhou | cc60619 | 2022-06-21 17:47:19 +0000 | [diff] [blame] | 66 | EXPECT_EQ(parser.mime_fields[1].content, |
Ed Tanous | af4edf6 | 2020-07-21 08:46:25 -0700 | [diff] [blame] | 67 | "{\r\n-----------------------------d74496d66958873e123456"); |
Nan Zhou | cc60619 | 2022-06-21 17:47:19 +0000 | [diff] [blame] | 68 | EXPECT_EQ(parser.mime_fields[2].fields.at("Content-Disposition"), |
Ed Tanous | af4edf6 | 2020-07-21 08:46:25 -0700 | [diff] [blame] | 69 | "form-data; name=\"Test3\""); |
Nan Zhou | cc60619 | 2022-06-21 17:47:19 +0000 | [diff] [blame] | 70 | EXPECT_EQ(parser.mime_fields[2].content, "{\r\n--------d74496d6695887}"); |
Ed Tanous | af4edf6 | 2020-07-21 08:46:25 -0700 | [diff] [blame] | 71 | } |
| 72 | |
| 73 | TEST_F(MultipartTest, TestBadMultipartParser1) |
| 74 | { |
| 75 | req.set("Content-Type", |
| 76 | "multipart/form-data; " |
| 77 | "boundary=---------------------------d74496d66958873e"); |
| 78 | |
| 79 | req.body() = "-----------------------------d74496d66958873e\r\n" |
| 80 | "Content-Disposition: form-data; name=\"Test1\"\r\n\r\n" |
| 81 | "1234567890\r\n" |
| 82 | "-----------------------------d74496d66958873e\r-\r\n"; |
| 83 | |
| 84 | crow::Request reqIn(req, ec); |
| 85 | ParserError rc = parser.parse(reqIn); |
| 86 | ASSERT_EQ(rc, ParserError::PARSER_SUCCESS); |
| 87 | |
Nan Zhou | cc60619 | 2022-06-21 17:47:19 +0000 | [diff] [blame] | 88 | EXPECT_EQ(parser.boundary, |
Ed Tanous | af4edf6 | 2020-07-21 08:46:25 -0700 | [diff] [blame] | 89 | "\r\n-----------------------------d74496d66958873e"); |
Nan Zhou | cc60619 | 2022-06-21 17:47:19 +0000 | [diff] [blame] | 90 | EXPECT_EQ(parser.mime_fields.size(), 1); |
Ed Tanous | af4edf6 | 2020-07-21 08:46:25 -0700 | [diff] [blame] | 91 | } |
| 92 | |
| 93 | TEST_F(MultipartTest, TestBadMultipartParser2) |
| 94 | { |
| 95 | req.set("Content-Type", |
| 96 | "multipart/form-data; " |
| 97 | "boundary=---------------------------d74496d66958873e"); |
| 98 | |
| 99 | req.body() = "-----------------------------d74496d66958873e\r\n" |
| 100 | "Content-Disposition: form-data; name=\"Test1\"\r\n\r\n" |
| 101 | "abcd\r\n" |
| 102 | "-----------------------------d74496d66958873e-\r\n"; |
| 103 | |
| 104 | crow::Request reqIn(req, ec); |
| 105 | ParserError rc = parser.parse(reqIn); |
| 106 | ASSERT_EQ(rc, ParserError::PARSER_SUCCESS); |
| 107 | |
Nan Zhou | cc60619 | 2022-06-21 17:47:19 +0000 | [diff] [blame] | 108 | EXPECT_EQ(parser.boundary, |
Ed Tanous | af4edf6 | 2020-07-21 08:46:25 -0700 | [diff] [blame] | 109 | "\r\n-----------------------------d74496d66958873e"); |
Nan Zhou | cc60619 | 2022-06-21 17:47:19 +0000 | [diff] [blame] | 110 | EXPECT_EQ(parser.mime_fields.size(), 1); |
Ed Tanous | af4edf6 | 2020-07-21 08:46:25 -0700 | [diff] [blame] | 111 | } |
| 112 | |
| 113 | TEST_F(MultipartTest, TestErrorBoundaryFormat) |
| 114 | { |
| 115 | req.set("Content-Type", |
| 116 | "multipart/form-data; " |
| 117 | "boundary+=-----------------------------d74496d66958873e"); |
| 118 | |
| 119 | req.body() = "-----------------------------d74496d66958873e\r\n" |
| 120 | "Content-Disposition: form-data; name=\"Test1\"\r\n\r\n" |
| 121 | "{\"Key1\": 11223333333333333333333333333333333333333333}\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); |
Nan Zhou | cc60619 | 2022-06-21 17:47:19 +0000 | [diff] [blame] | 128 | EXPECT_EQ(parser.parse(reqIn), ParserError::ERROR_BOUNDARY_FORMAT); |
Ed Tanous | af4edf6 | 2020-07-21 08:46:25 -0700 | [diff] [blame] | 129 | } |
| 130 | |
| 131 | TEST_F(MultipartTest, TestErrorBoundaryCR) |
| 132 | { |
| 133 | req.set("Content-Type", |
| 134 | "multipart/form-data; " |
| 135 | "boundary=---------------------------d74496d66958873e"); |
| 136 | |
| 137 | req.body() = "-----------------------------d74496d66958873e" |
| 138 | "Content-Disposition: form-data; name=\"Test1\"\r\n\r" |
| 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); |
Nan Zhou | cc60619 | 2022-06-21 17:47:19 +0000 | [diff] [blame] | 146 | EXPECT_EQ(parser.parse(reqIn), ParserError::ERROR_BOUNDARY_CR); |
Ed Tanous | af4edf6 | 2020-07-21 08:46:25 -0700 | [diff] [blame] | 147 | } |
| 148 | |
| 149 | TEST_F(MultipartTest, TestErrorBoundaryLF) |
| 150 | { |
| 151 | req.set("Content-Type", |
| 152 | "multipart/form-data; " |
| 153 | "boundary=---------------------------d74496d66958873e"); |
| 154 | |
| 155 | req.body() = "-----------------------------d74496d66958873e\r" |
| 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); |
Nan Zhou | cc60619 | 2022-06-21 17:47:19 +0000 | [diff] [blame] | 164 | EXPECT_EQ(parser.parse(reqIn), ParserError::ERROR_BOUNDARY_LF); |
Ed Tanous | af4edf6 | 2020-07-21 08:46:25 -0700 | [diff] [blame] | 165 | } |
| 166 | |
| 167 | TEST_F(MultipartTest, TestErrorBoundaryData) |
| 168 | { |
| 169 | req.set("Content-Type", |
| 170 | "multipart/form-data; " |
| 171 | "boundary=---------------------------d7449sd6d66958873e"); |
| 172 | |
| 173 | req.body() = "-----------------------------d74496d66958873e\r\n" |
| 174 | "Content-Disposition: form-data; name=\"Test1\"\r\n\r\n" |
| 175 | "{\"Key1\": 112233}\r\n" |
| 176 | "-----------------------------d74496d66958873e\r\n" |
| 177 | "Content-Disposition: form-data; name=\"Test2\"\r\n\r\n" |
| 178 | "123456\r\n" |
| 179 | "-----------------------------d74496d66958873e--\r\n"; |
| 180 | |
| 181 | crow::Request reqIn(req, ec); |
Nan Zhou | cc60619 | 2022-06-21 17:47:19 +0000 | [diff] [blame] | 182 | EXPECT_EQ(parser.parse(reqIn), ParserError::ERROR_BOUNDARY_DATA); |
Ed Tanous | af4edf6 | 2020-07-21 08:46:25 -0700 | [diff] [blame] | 183 | } |
| 184 | |
| 185 | TEST_F(MultipartTest, TestErrorEmptyHeader) |
| 186 | { |
| 187 | req.set("Content-Type", |
| 188 | "multipart/form-data; " |
| 189 | "boundary=---------------------------d74496d66958873e"); |
| 190 | |
| 191 | req.body() = "-----------------------------d74496d66958873e\r\n" |
| 192 | ": form-data; name=\"Test1\"\r\n" |
| 193 | "{\"Key1\": 112233}\r\n" |
| 194 | "-----------------------------d74496d66958873e\r\n" |
| 195 | "Content-Disposition: form-data; name=\"Test2\"\r\n" |
| 196 | "123456\r\n" |
| 197 | "-----------------------------d74496d66958873e--\r\n"; |
| 198 | |
| 199 | crow::Request reqIn(req, ec); |
Nan Zhou | cc60619 | 2022-06-21 17:47:19 +0000 | [diff] [blame] | 200 | EXPECT_EQ(parser.parse(reqIn), ParserError::ERROR_EMPTY_HEADER); |
Ed Tanous | af4edf6 | 2020-07-21 08:46:25 -0700 | [diff] [blame] | 201 | } |
| 202 | |
| 203 | TEST_F(MultipartTest, TestErrorHeaderName) |
| 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\n" |
| 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); |
Nan Zhou | cc60619 | 2022-06-21 17:47:19 +0000 | [diff] [blame] | 218 | EXPECT_EQ(parser.parse(reqIn), ParserError::ERROR_HEADER_NAME); |
Ed Tanous | af4edf6 | 2020-07-21 08:46:25 -0700 | [diff] [blame] | 219 | } |
| 220 | |
| 221 | TEST_F(MultipartTest, TestErrorHeaderValue) |
| 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" |
| 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); |
Nan Zhou | cc60619 | 2022-06-21 17:47:19 +0000 | [diff] [blame] | 236 | EXPECT_EQ(parser.parse(reqIn), ParserError::ERROR_HEADER_VALUE); |
Ed Tanous | af4edf6 | 2020-07-21 08:46:25 -0700 | [diff] [blame] | 237 | } |
| 238 | |
| 239 | TEST_F(MultipartTest, TestErrorHeaderEnding) |
| 240 | { |
| 241 | req.set("Content-Type", |
| 242 | "multipart/form-data; " |
| 243 | "boundary=---------------------------d74496d66958873e"); |
| 244 | |
| 245 | req.body() = "-----------------------------d74496d66958873e\r\n" |
| 246 | "Content-Disposition: form-data; name=\"Test1\"\r\n\r" |
| 247 | "{\"Key1\": 112233}\r\n" |
| 248 | "-----------------------------d74496d66958873e\r\n" |
| 249 | "Content-Disposition: form-data; name=\"Test2\"\r\n\r\n" |
| 250 | "123456\r\n" |
| 251 | "-----------------------------d74496d66958873e--\r\n"; |
| 252 | |
| 253 | crow::Request reqIn(req, ec); |
Nan Zhou | cc60619 | 2022-06-21 17:47:19 +0000 | [diff] [blame] | 254 | EXPECT_EQ(parser.parse(reqIn), ParserError::ERROR_HEADER_ENDING); |
Ed Tanous | af4edf6 | 2020-07-21 08:46:25 -0700 | [diff] [blame] | 255 | } |
Nan Zhou | e52d156 | 2022-06-21 17:47:15 +0000 | [diff] [blame] | 256 | } // namespace |