Nan Zhou | 77665bd | 2022-10-12 20:28:58 +0000 | [diff] [blame] | 1 | #include "http_request.hpp" |
Nan Zhou | ca0cb4c | 2022-06-21 17:47:09 +0000 | [diff] [blame] | 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> |
Ed Tanous | af4edf6 | 2020-07-21 08:46:25 -0700 | [diff] [blame] | 5 | |
Ed Tanous | f0b59af | 2024-03-20 13:38:04 -0700 | [diff] [blame^] | 6 | #include <iterator> |
Nan Zhou | d5c80ad | 2022-07-11 01:16:31 +0000 | [diff] [blame] | 7 | #include <string_view> |
Nan Zhou | ca0cb4c | 2022-06-21 17:47:09 +0000 | [diff] [blame] | 8 | #include <system_error> |
Nan Zhou | d5c80ad | 2022-07-11 01:16:31 +0000 | [diff] [blame] | 9 | #include <vector> |
Ed Tanous | af4edf6 | 2020-07-21 08:46:25 -0700 | [diff] [blame] | 10 | |
Nan Zhou | d5c80ad | 2022-07-11 01:16:31 +0000 | [diff] [blame] | 11 | #include <gtest/gtest.h> // IWYU pragma: keep |
| 12 | |
| 13 | // IWYU pragma: no_include <gtest/gtest-message.h> |
| 14 | // IWYU pragma: no_include <gtest/gtest-test-part.h> |
| 15 | // IWYU pragma: no_include "gtest/gtest_pred_impl.h" |
| 16 | // IWYU pragma: no_include <boost/beast/http/impl/fields.hpp> |
| 17 | // IWYU pragma: no_include <boost/intrusive/detail/list_iterator.hpp> |
| 18 | // IWYU pragma: no_include <boost/intrusive/detail/tree_iterator.hpp> |
Ed Tanous | af4edf6 | 2020-07-21 08:46:25 -0700 | [diff] [blame] | 19 | |
Nan Zhou | e52d156 | 2022-06-21 17:47:15 +0000 | [diff] [blame] | 20 | namespace |
| 21 | { |
| 22 | using ::testing::Test; |
| 23 | |
| 24 | class MultipartTest : public Test |
Ed Tanous | af4edf6 | 2020-07-21 08:46:25 -0700 | [diff] [blame] | 25 | { |
| 26 | public: |
Ed Tanous | af4edf6 | 2020-07-21 08:46:25 -0700 | [diff] [blame] | 27 | MultipartParser parser; |
| 28 | std::error_code ec; |
| 29 | }; |
| 30 | |
| 31 | TEST_F(MultipartTest, TestGoodMultipartParser) |
| 32 | { |
Ed Tanous | 6fb96ce | 2024-01-28 21:30:06 -0800 | [diff] [blame] | 33 | std::string_view body = |
| 34 | "-----------------------------d74496d66958873e\r\n" |
| 35 | "Content-Disposition: form-data; name=\"Test1\"\r\n\r\n" |
| 36 | "111111111111111111111111112222222222222222222222222222222\r\n" |
| 37 | "-----------------------------d74496d66958873e\r\n" |
| 38 | "Content-Disposition: form-data; name=\"Test2\"\r\n\r\n" |
| 39 | "{\r\n-----------------------------d74496d66958873e123456\r\n" |
| 40 | "-----------------------------d74496d66958873e\r\n" |
| 41 | "Content-Disposition: form-data; name=\"Test3\"\r\n\r\n" |
| 42 | "{\r\n--------d74496d6695887}\r\n" |
| 43 | "-----------------------------d74496d66958873e--\r\n"; |
Ed Tanous | af4edf6 | 2020-07-21 08:46:25 -0700 | [diff] [blame] | 44 | |
Ed Tanous | 6fb96ce | 2024-01-28 21:30:06 -0800 | [diff] [blame] | 45 | crow::Request reqIn(body, ec); |
Ed Tanous | af4edf6 | 2020-07-21 08:46:25 -0700 | [diff] [blame] | 46 | |
Ed Tanous | 6fb96ce | 2024-01-28 21:30:06 -0800 | [diff] [blame] | 47 | reqIn.addHeader("Content-Type", |
| 48 | "multipart/form-data; " |
| 49 | "boundary=---------------------------d74496d66958873e"); |
| 50 | |
Ed Tanous | af4edf6 | 2020-07-21 08:46:25 -0700 | [diff] [blame] | 51 | ParserError rc = parser.parse(reqIn); |
| 52 | ASSERT_EQ(rc, ParserError::PARSER_SUCCESS); |
| 53 | |
Nan Zhou | cc60619 | 2022-06-21 17:47:19 +0000 | [diff] [blame] | 54 | EXPECT_EQ(parser.boundary, |
Ed Tanous | af4edf6 | 2020-07-21 08:46:25 -0700 | [diff] [blame] | 55 | "\r\n-----------------------------d74496d66958873e"); |
Nan Zhou | cc60619 | 2022-06-21 17:47:19 +0000 | [diff] [blame] | 56 | EXPECT_EQ(parser.mime_fields.size(), 3); |
Ed Tanous | af4edf6 | 2020-07-21 08:46:25 -0700 | [diff] [blame] | 57 | |
Nan Zhou | cc60619 | 2022-06-21 17:47:19 +0000 | [diff] [blame] | 58 | EXPECT_EQ(parser.mime_fields[0].fields.at("Content-Disposition"), |
Ed Tanous | af4edf6 | 2020-07-21 08:46:25 -0700 | [diff] [blame] | 59 | "form-data; name=\"Test1\""); |
Nan Zhou | cc60619 | 2022-06-21 17:47:19 +0000 | [diff] [blame] | 60 | EXPECT_EQ(parser.mime_fields[0].content, |
Ed Tanous | af4edf6 | 2020-07-21 08:46:25 -0700 | [diff] [blame] | 61 | "111111111111111111111111112222222222222222222222222222222"); |
| 62 | |
Nan Zhou | cc60619 | 2022-06-21 17:47:19 +0000 | [diff] [blame] | 63 | EXPECT_EQ(parser.mime_fields[1].fields.at("Content-Disposition"), |
Ed Tanous | af4edf6 | 2020-07-21 08:46:25 -0700 | [diff] [blame] | 64 | "form-data; name=\"Test2\""); |
Nan Zhou | cc60619 | 2022-06-21 17:47:19 +0000 | [diff] [blame] | 65 | EXPECT_EQ(parser.mime_fields[1].content, |
Ed Tanous | af4edf6 | 2020-07-21 08:46:25 -0700 | [diff] [blame] | 66 | "{\r\n-----------------------------d74496d66958873e123456"); |
Nan Zhou | cc60619 | 2022-06-21 17:47:19 +0000 | [diff] [blame] | 67 | EXPECT_EQ(parser.mime_fields[2].fields.at("Content-Disposition"), |
Ed Tanous | af4edf6 | 2020-07-21 08:46:25 -0700 | [diff] [blame] | 68 | "form-data; name=\"Test3\""); |
Nan Zhou | cc60619 | 2022-06-21 17:47:19 +0000 | [diff] [blame] | 69 | EXPECT_EQ(parser.mime_fields[2].content, "{\r\n--------d74496d6695887}"); |
Ed Tanous | af4edf6 | 2020-07-21 08:46:25 -0700 | [diff] [blame] | 70 | } |
| 71 | |
| 72 | TEST_F(MultipartTest, TestBadMultipartParser1) |
| 73 | { |
Ed Tanous | 6fb96ce | 2024-01-28 21:30:06 -0800 | [diff] [blame] | 74 | std::string_view body = |
| 75 | "-----------------------------d74496d66958873e\r\n" |
| 76 | "Content-Disposition: form-data; name=\"Test1\"\r\n\r\n" |
| 77 | "1234567890\r\n" |
| 78 | "-----------------------------d74496d66958873e\r-\r\n"; |
Ed Tanous | af4edf6 | 2020-07-21 08:46:25 -0700 | [diff] [blame] | 79 | |
Ed Tanous | 6fb96ce | 2024-01-28 21:30:06 -0800 | [diff] [blame] | 80 | crow::Request reqIn(body, ec); |
Ed Tanous | af4edf6 | 2020-07-21 08:46:25 -0700 | [diff] [blame] | 81 | |
Ed Tanous | 6fb96ce | 2024-01-28 21:30:06 -0800 | [diff] [blame] | 82 | reqIn.addHeader("Content-Type", |
| 83 | "multipart/form-data; " |
| 84 | "boundary=---------------------------d74496d66958873e"); |
| 85 | |
Ed Tanous | af4edf6 | 2020-07-21 08:46:25 -0700 | [diff] [blame] | 86 | ParserError rc = parser.parse(reqIn); |
Ed Tanous | af4edf6 | 2020-07-21 08:46:25 -0700 | [diff] [blame] | 87 | |
Krzysztof Grobelny | 18e3f7f | 2022-08-24 09:24:33 +0200 | [diff] [blame] | 88 | EXPECT_EQ(rc, ParserError::ERROR_UNEXPECTED_END_OF_INPUT); |
Ed Tanous | af4edf6 | 2020-07-21 08:46:25 -0700 | [diff] [blame] | 89 | } |
| 90 | |
| 91 | TEST_F(MultipartTest, TestBadMultipartParser2) |
| 92 | { |
Ed Tanous | 6fb96ce | 2024-01-28 21:30:06 -0800 | [diff] [blame] | 93 | std::string_view body = |
| 94 | "-----------------------------d74496d66958873e\r\n" |
| 95 | "Content-Disposition: form-data; name=\"Test1\"\r\n\r\n" |
| 96 | "abcd\r\n" |
| 97 | "-----------------------------d74496d66958873e-\r\n"; |
| 98 | crow::Request reqIn(body, ec); |
Ed Tanous | af4edf6 | 2020-07-21 08:46:25 -0700 | [diff] [blame] | 99 | |
Ed Tanous | 6fb96ce | 2024-01-28 21:30:06 -0800 | [diff] [blame] | 100 | reqIn.addHeader("Content-Type", |
| 101 | "multipart/form-data; " |
| 102 | "boundary=---------------------------d74496d66958873e"); |
Ed Tanous | af4edf6 | 2020-07-21 08:46:25 -0700 | [diff] [blame] | 103 | |
Ed Tanous | af4edf6 | 2020-07-21 08:46:25 -0700 | [diff] [blame] | 104 | ParserError rc = parser.parse(reqIn); |
Ed Tanous | af4edf6 | 2020-07-21 08:46:25 -0700 | [diff] [blame] | 105 | |
Krzysztof Grobelny | 18e3f7f | 2022-08-24 09:24:33 +0200 | [diff] [blame] | 106 | EXPECT_EQ(rc, ParserError::ERROR_UNEXPECTED_END_OF_INPUT); |
Ed Tanous | af4edf6 | 2020-07-21 08:46:25 -0700 | [diff] [blame] | 107 | } |
| 108 | |
| 109 | TEST_F(MultipartTest, TestErrorBoundaryFormat) |
| 110 | { |
Ed Tanous | 6fb96ce | 2024-01-28 21:30:06 -0800 | [diff] [blame] | 111 | std::string_view body = |
| 112 | "-----------------------------d74496d66958873e\r\n" |
| 113 | "Content-Disposition: form-data; name=\"Test1\"\r\n\r\n" |
| 114 | "{\"Key1\": 11223333333333333333333333333333333333333333}\r\n" |
| 115 | "-----------------------------d74496d66958873e\r\n" |
| 116 | "Content-Disposition: form-data; name=\"Test2\"\r\n\r\n" |
| 117 | "123456\r\n" |
| 118 | "-----------------------------d74496d66958873e--\r\n"; |
Ed Tanous | af4edf6 | 2020-07-21 08:46:25 -0700 | [diff] [blame] | 119 | |
Ed Tanous | 6fb96ce | 2024-01-28 21:30:06 -0800 | [diff] [blame] | 120 | crow::Request reqIn(body, ec); |
Ed Tanous | af4edf6 | 2020-07-21 08:46:25 -0700 | [diff] [blame] | 121 | |
Ed Tanous | 6fb96ce | 2024-01-28 21:30:06 -0800 | [diff] [blame] | 122 | reqIn.addHeader("Content-Type", |
| 123 | "multipart/form-data; " |
| 124 | "boundary+=-----------------------------d74496d66958873e"); |
| 125 | |
Nan Zhou | cc60619 | 2022-06-21 17:47:19 +0000 | [diff] [blame] | 126 | EXPECT_EQ(parser.parse(reqIn), ParserError::ERROR_BOUNDARY_FORMAT); |
Ed Tanous | af4edf6 | 2020-07-21 08:46:25 -0700 | [diff] [blame] | 127 | } |
| 128 | |
| 129 | TEST_F(MultipartTest, TestErrorBoundaryCR) |
| 130 | { |
Ed Tanous | 6fb96ce | 2024-01-28 21:30:06 -0800 | [diff] [blame] | 131 | std::string_view body = |
| 132 | "-----------------------------d74496d66958873e" |
| 133 | "Content-Disposition: form-data; name=\"Test1\"\r\n\r" |
| 134 | "{\"Key1\": 112233}\r\n" |
| 135 | "-----------------------------d74496d66958873e\r\n" |
| 136 | "Content-Disposition: form-data; name=\"Test2\"\r\n\r\n" |
| 137 | "123456\r\n" |
| 138 | "-----------------------------d74496d66958873e--\r\n"; |
| 139 | crow::Request reqIn(body, ec); |
Ed Tanous | af4edf6 | 2020-07-21 08:46:25 -0700 | [diff] [blame] | 140 | |
Ed Tanous | 6fb96ce | 2024-01-28 21:30:06 -0800 | [diff] [blame] | 141 | reqIn.addHeader("Content-Type", |
| 142 | "multipart/form-data; " |
| 143 | "boundary=---------------------------d74496d66958873e"); |
Ed Tanous | af4edf6 | 2020-07-21 08:46:25 -0700 | [diff] [blame] | 144 | |
Nan Zhou | cc60619 | 2022-06-21 17:47:19 +0000 | [diff] [blame] | 145 | EXPECT_EQ(parser.parse(reqIn), ParserError::ERROR_BOUNDARY_CR); |
Ed Tanous | af4edf6 | 2020-07-21 08:46:25 -0700 | [diff] [blame] | 146 | } |
| 147 | |
| 148 | TEST_F(MultipartTest, TestErrorBoundaryLF) |
| 149 | { |
Ed Tanous | 6fb96ce | 2024-01-28 21:30:06 -0800 | [diff] [blame] | 150 | std::string_view body = |
| 151 | "-----------------------------d74496d66958873e\r" |
| 152 | "Content-Disposition: form-data; name=\"Test1\"\r\n\r\n" |
| 153 | "{\"Key1\": 112233}\r\n" |
| 154 | "-----------------------------d74496d66958873e\r\n" |
| 155 | "Content-Disposition: form-data; name=\"Test2\"\r\n\r\n" |
| 156 | "123456\r\n" |
| 157 | "-----------------------------d74496d66958873e--\r\n"; |
Ed Tanous | af4edf6 | 2020-07-21 08:46:25 -0700 | [diff] [blame] | 158 | |
Ed Tanous | 6fb96ce | 2024-01-28 21:30:06 -0800 | [diff] [blame] | 159 | crow::Request reqIn(body, ec); |
Ed Tanous | af4edf6 | 2020-07-21 08:46:25 -0700 | [diff] [blame] | 160 | |
Ed Tanous | 6fb96ce | 2024-01-28 21:30:06 -0800 | [diff] [blame] | 161 | reqIn.addHeader("Content-Type", |
| 162 | "multipart/form-data; " |
| 163 | "boundary=---------------------------d74496d66958873e"); |
| 164 | |
Nan Zhou | cc60619 | 2022-06-21 17:47:19 +0000 | [diff] [blame] | 165 | EXPECT_EQ(parser.parse(reqIn), ParserError::ERROR_BOUNDARY_LF); |
Ed Tanous | af4edf6 | 2020-07-21 08:46:25 -0700 | [diff] [blame] | 166 | } |
| 167 | |
| 168 | TEST_F(MultipartTest, TestErrorBoundaryData) |
| 169 | { |
Ed Tanous | 6fb96ce | 2024-01-28 21:30:06 -0800 | [diff] [blame] | 170 | std::string_view body = |
| 171 | "-----------------------------d74496d66958873e\r\n" |
| 172 | "Content-Disposition: form-data; name=\"Test1\"\r\n\r\n" |
| 173 | "{\"Key1\": 112233}\r\n" |
| 174 | "-----------------------------d74496d66958873e\r\n" |
| 175 | "Content-Disposition: form-data; name=\"Test2\"\r\n\r\n" |
| 176 | "123456\r\n" |
| 177 | "-----------------------------d74496d66958873e--\r\n"; |
Ed Tanous | af4edf6 | 2020-07-21 08:46:25 -0700 | [diff] [blame] | 178 | |
Ed Tanous | 6fb96ce | 2024-01-28 21:30:06 -0800 | [diff] [blame] | 179 | crow::Request reqIn(body, ec); |
Ed Tanous | af4edf6 | 2020-07-21 08:46:25 -0700 | [diff] [blame] | 180 | |
Ed Tanous | 6fb96ce | 2024-01-28 21:30:06 -0800 | [diff] [blame] | 181 | reqIn.addHeader("Content-Type", |
| 182 | "multipart/form-data; " |
| 183 | "boundary=---------------------------d7449sd6d66958873e"); |
| 184 | |
Nan Zhou | cc60619 | 2022-06-21 17:47:19 +0000 | [diff] [blame] | 185 | EXPECT_EQ(parser.parse(reqIn), ParserError::ERROR_BOUNDARY_DATA); |
Ed Tanous | af4edf6 | 2020-07-21 08:46:25 -0700 | [diff] [blame] | 186 | } |
| 187 | |
| 188 | TEST_F(MultipartTest, TestErrorEmptyHeader) |
| 189 | { |
Ed Tanous | 6fb96ce | 2024-01-28 21:30:06 -0800 | [diff] [blame] | 190 | std::string_view body = |
| 191 | "-----------------------------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 | crow::Request reqIn(body, ec); |
Ed Tanous | af4edf6 | 2020-07-21 08:46:25 -0700 | [diff] [blame] | 199 | |
Ed Tanous | 6fb96ce | 2024-01-28 21:30:06 -0800 | [diff] [blame] | 200 | reqIn.addHeader("Content-Type", |
| 201 | "multipart/form-data; " |
| 202 | "boundary=---------------------------d74496d66958873e"); |
Ed Tanous | af4edf6 | 2020-07-21 08:46:25 -0700 | [diff] [blame] | 203 | |
Nan Zhou | cc60619 | 2022-06-21 17:47:19 +0000 | [diff] [blame] | 204 | EXPECT_EQ(parser.parse(reqIn), ParserError::ERROR_EMPTY_HEADER); |
Ed Tanous | af4edf6 | 2020-07-21 08:46:25 -0700 | [diff] [blame] | 205 | } |
| 206 | |
| 207 | TEST_F(MultipartTest, TestErrorHeaderName) |
| 208 | { |
Ed Tanous | 6fb96ce | 2024-01-28 21:30:06 -0800 | [diff] [blame] | 209 | std::string_view body = |
| 210 | "-----------------------------d74496d66958873e\r\n" |
| 211 | "Content-!!Disposition: form-data; name=\"Test1\"\r\n" |
| 212 | "{\"Key1\": 112233}\r\n" |
| 213 | "-----------------------------d74496d66958873e\r\n" |
| 214 | "Content-Disposition: form-data; name=\"Test2\"\r\n\r\n" |
| 215 | "123456\r\n" |
| 216 | "-----------------------------d74496d66958873e--\r\n"; |
| 217 | crow::Request reqIn(body, ec); |
Ed Tanous | af4edf6 | 2020-07-21 08:46:25 -0700 | [diff] [blame] | 218 | |
Ed Tanous | 6fb96ce | 2024-01-28 21:30:06 -0800 | [diff] [blame] | 219 | reqIn.addHeader("Content-Type", |
| 220 | "multipart/form-data; " |
| 221 | "boundary=---------------------------d74496d66958873e"); |
Ed Tanous | af4edf6 | 2020-07-21 08:46:25 -0700 | [diff] [blame] | 222 | |
Nan Zhou | cc60619 | 2022-06-21 17:47:19 +0000 | [diff] [blame] | 223 | EXPECT_EQ(parser.parse(reqIn), ParserError::ERROR_HEADER_NAME); |
Ed Tanous | af4edf6 | 2020-07-21 08:46:25 -0700 | [diff] [blame] | 224 | } |
| 225 | |
| 226 | TEST_F(MultipartTest, TestErrorHeaderValue) |
| 227 | { |
Ed Tanous | 6fb96ce | 2024-01-28 21:30:06 -0800 | [diff] [blame] | 228 | std::string_view body = |
| 229 | "-----------------------------d74496d66958873e\r\n" |
| 230 | "Content-Disposition: form-data; name=\"Test1\"\r" |
| 231 | "{\"Key1\": 112233}\r\n" |
| 232 | "-----------------------------d74496d66958873e\r\n" |
| 233 | "Content-Disposition: form-data; name=\"Test2\"\r\n\r\n" |
| 234 | "123456\r\n" |
| 235 | "-----------------------------d74496d66958873e--\r\n"; |
Ed Tanous | af4edf6 | 2020-07-21 08:46:25 -0700 | [diff] [blame] | 236 | |
Ed Tanous | 6fb96ce | 2024-01-28 21:30:06 -0800 | [diff] [blame] | 237 | crow::Request reqIn(body, ec); |
Ed Tanous | af4edf6 | 2020-07-21 08:46:25 -0700 | [diff] [blame] | 238 | |
Ed Tanous | 6fb96ce | 2024-01-28 21:30:06 -0800 | [diff] [blame] | 239 | reqIn.addHeader("Content-Type", |
| 240 | "multipart/form-data; " |
| 241 | "boundary=---------------------------d74496d66958873e"); |
| 242 | |
Nan Zhou | cc60619 | 2022-06-21 17:47:19 +0000 | [diff] [blame] | 243 | EXPECT_EQ(parser.parse(reqIn), ParserError::ERROR_HEADER_VALUE); |
Ed Tanous | af4edf6 | 2020-07-21 08:46:25 -0700 | [diff] [blame] | 244 | } |
| 245 | |
| 246 | TEST_F(MultipartTest, TestErrorHeaderEnding) |
| 247 | { |
Ed Tanous | 6fb96ce | 2024-01-28 21:30:06 -0800 | [diff] [blame] | 248 | std::string_view body = |
| 249 | "-----------------------------d74496d66958873e\r\n" |
| 250 | "Content-Disposition: form-data; name=\"Test1\"\r\n\r" |
| 251 | "{\"Key1\": 112233}\r\n" |
| 252 | "-----------------------------d74496d66958873e\r\n" |
| 253 | "Content-Disposition: form-data; name=\"Test2\"\r\n\r\n" |
| 254 | "123456\r\n" |
| 255 | "-----------------------------d74496d66958873e--\r\n"; |
Ed Tanous | af4edf6 | 2020-07-21 08:46:25 -0700 | [diff] [blame] | 256 | |
Ed Tanous | 6fb96ce | 2024-01-28 21:30:06 -0800 | [diff] [blame] | 257 | crow::Request reqIn(body, ec); |
Ed Tanous | af4edf6 | 2020-07-21 08:46:25 -0700 | [diff] [blame] | 258 | |
Ed Tanous | 6fb96ce | 2024-01-28 21:30:06 -0800 | [diff] [blame] | 259 | reqIn.addHeader("Content-Type", |
| 260 | "multipart/form-data; " |
| 261 | "boundary=---------------------------d74496d66958873e"); |
| 262 | |
Nan Zhou | cc60619 | 2022-06-21 17:47:19 +0000 | [diff] [blame] | 263 | EXPECT_EQ(parser.parse(reqIn), ParserError::ERROR_HEADER_ENDING); |
Ed Tanous | af4edf6 | 2020-07-21 08:46:25 -0700 | [diff] [blame] | 264 | } |
Krzysztof Grobelny | 18e3f7f | 2022-08-24 09:24:33 +0200 | [diff] [blame] | 265 | |
| 266 | TEST_F(MultipartTest, TestGoodMultipartParserMultipleHeaders) |
| 267 | { |
Ed Tanous | 6fb96ce | 2024-01-28 21:30:06 -0800 | [diff] [blame] | 268 | std::string_view body = "-----------------------------d74496d66958873e\r\n" |
| 269 | "Content-Disposition: form-data; name=\"Test1\"\r\n" |
| 270 | "Other-Header: value=\"v1\"\r\n" |
| 271 | "\r\n" |
| 272 | "Data1\r\n" |
| 273 | "-----------------------------d74496d66958873e--"; |
Krzysztof Grobelny | 18e3f7f | 2022-08-24 09:24:33 +0200 | [diff] [blame] | 274 | |
Ed Tanous | 6fb96ce | 2024-01-28 21:30:06 -0800 | [diff] [blame] | 275 | crow::Request reqIn(body, ec); |
Krzysztof Grobelny | 18e3f7f | 2022-08-24 09:24:33 +0200 | [diff] [blame] | 276 | |
Ed Tanous | 6fb96ce | 2024-01-28 21:30:06 -0800 | [diff] [blame] | 277 | reqIn.addHeader("Content-Type", |
| 278 | "multipart/form-data; " |
| 279 | "boundary=---------------------------d74496d66958873e"); |
| 280 | |
Krzysztof Grobelny | 18e3f7f | 2022-08-24 09:24:33 +0200 | [diff] [blame] | 281 | ParserError rc = parser.parse(reqIn); |
| 282 | ASSERT_EQ(rc, ParserError::PARSER_SUCCESS); |
| 283 | |
| 284 | EXPECT_EQ(parser.boundary, |
| 285 | "\r\n-----------------------------d74496d66958873e"); |
| 286 | ASSERT_EQ(parser.mime_fields.size(), 1); |
| 287 | |
| 288 | EXPECT_EQ(parser.mime_fields[0].fields.at("Content-Disposition"), |
| 289 | "form-data; name=\"Test1\""); |
| 290 | EXPECT_EQ(parser.mime_fields[0].fields.at("Other-Header"), "value=\"v1\""); |
| 291 | EXPECT_EQ(parser.mime_fields[0].content, "Data1"); |
| 292 | } |
| 293 | |
| 294 | TEST_F(MultipartTest, TestErrorHeaderWithoutColon) |
| 295 | { |
Ed Tanous | 6fb96ce | 2024-01-28 21:30:06 -0800 | [diff] [blame] | 296 | std::string_view body = "----end\r\n" |
| 297 | "abc\r\n" |
| 298 | "\r\n" |
| 299 | "Data1\r\n" |
| 300 | "----end--\r\n"; |
| 301 | crow::Request reqIn(body, ec); |
Krzysztof Grobelny | 18e3f7f | 2022-08-24 09:24:33 +0200 | [diff] [blame] | 302 | |
Ed Tanous | 6fb96ce | 2024-01-28 21:30:06 -0800 | [diff] [blame] | 303 | reqIn.addHeader("Content-Type", "multipart/form-data; " |
| 304 | "boundary=--end"); |
Krzysztof Grobelny | 18e3f7f | 2022-08-24 09:24:33 +0200 | [diff] [blame] | 305 | |
Krzysztof Grobelny | 18e3f7f | 2022-08-24 09:24:33 +0200 | [diff] [blame] | 306 | EXPECT_EQ(parser.parse(reqIn), ParserError::ERROR_UNEXPECTED_END_OF_HEADER); |
| 307 | } |
| 308 | |
| 309 | TEST_F(MultipartTest, TestUnknownHeaderIsCorrectlyParsed) |
| 310 | { |
Ed Tanous | 6fb96ce | 2024-01-28 21:30:06 -0800 | [diff] [blame] | 311 | std::string_view body = |
Krzysztof Grobelny | 18e3f7f | 2022-08-24 09:24:33 +0200 | [diff] [blame] | 312 | "----end\r\n" |
| 313 | "t-DiPpcccc:cccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccgcccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccaaaaaa\r\n" |
| 314 | "\r\n" |
| 315 | "Data1\r\n" |
| 316 | "----end--\r\n"; |
| 317 | |
Ed Tanous | 6fb96ce | 2024-01-28 21:30:06 -0800 | [diff] [blame] | 318 | crow::Request reqIn(body, ec); |
| 319 | |
| 320 | reqIn.addHeader("Content-Type", "multipart/form-data; " |
| 321 | "boundary=--end"); |
Krzysztof Grobelny | 18e3f7f | 2022-08-24 09:24:33 +0200 | [diff] [blame] | 322 | ParserError rc = parser.parse(reqIn); |
| 323 | |
| 324 | ASSERT_EQ(rc, ParserError::PARSER_SUCCESS); |
| 325 | |
| 326 | EXPECT_EQ(parser.boundary, "\r\n----end"); |
| 327 | ASSERT_EQ(parser.mime_fields.size(), 1); |
| 328 | |
| 329 | EXPECT_EQ( |
| 330 | parser.mime_fields[0].fields.at("t-DiPpcccc"), |
| 331 | "cccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccgcccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccaaaaaa"); |
| 332 | EXPECT_EQ(parser.mime_fields[0].content, "Data1"); |
| 333 | } |
| 334 | |
| 335 | TEST_F(MultipartTest, TestErrorMissingSeparatorBetweenMimeFieldsAndData) |
| 336 | { |
Ed Tanous | 6fb96ce | 2024-01-28 21:30:06 -0800 | [diff] [blame] | 337 | std::string_view body = |
Krzysztof Grobelny | 18e3f7f | 2022-08-24 09:24:33 +0200 | [diff] [blame] | 338 | "-----------------------------d74496d66958873e\r\n" |
| 339 | "t-DiPpcccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccgcccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccaaaaaa\r\n" |
| 340 | "Data1" |
| 341 | "-----------------------------d74496d66958873e--"; |
| 342 | |
Ed Tanous | 6fb96ce | 2024-01-28 21:30:06 -0800 | [diff] [blame] | 343 | crow::Request reqIn(body, ec); |
| 344 | |
| 345 | reqIn.addHeader( |
| 346 | "Content-Type", |
| 347 | "multipart/form-data; boundary=---------------------------d74496d66958873e"); |
| 348 | |
Krzysztof Grobelny | 18e3f7f | 2022-08-24 09:24:33 +0200 | [diff] [blame] | 349 | ParserError rc = parser.parse(reqIn); |
| 350 | |
| 351 | EXPECT_EQ(rc, ParserError::ERROR_UNEXPECTED_END_OF_HEADER); |
| 352 | } |
| 353 | |
| 354 | TEST_F(MultipartTest, TestDataWithoutMimeFields) |
| 355 | { |
Ed Tanous | 6fb96ce | 2024-01-28 21:30:06 -0800 | [diff] [blame] | 356 | std::string_view body = "-----------------------------d74496d66958873e\r\n" |
| 357 | "\r\n" |
| 358 | "Data1\r\n" |
| 359 | "-----------------------------d74496d66958873e--"; |
| 360 | |
| 361 | crow::Request reqIn(body, ec); |
| 362 | |
| 363 | reqIn.addHeader( |
Krzysztof Grobelny | 18e3f7f | 2022-08-24 09:24:33 +0200 | [diff] [blame] | 364 | "Content-Type", |
| 365 | "multipart/form-data; boundary=---------------------------d74496d66958873e"); |
| 366 | |
Krzysztof Grobelny | 18e3f7f | 2022-08-24 09:24:33 +0200 | [diff] [blame] | 367 | ParserError rc = parser.parse(reqIn); |
| 368 | |
| 369 | ASSERT_EQ(rc, ParserError::PARSER_SUCCESS); |
| 370 | |
| 371 | EXPECT_EQ(parser.boundary, |
| 372 | "\r\n-----------------------------d74496d66958873e"); |
| 373 | ASSERT_EQ(parser.mime_fields.size(), 1); |
| 374 | |
| 375 | EXPECT_EQ(std::distance(parser.mime_fields[0].fields.begin(), |
| 376 | parser.mime_fields[0].fields.end()), |
| 377 | 0); |
| 378 | EXPECT_EQ(parser.mime_fields[0].content, "Data1"); |
| 379 | } |
| 380 | |
| 381 | TEST_F(MultipartTest, TestErrorMissingFinalBoundry) |
| 382 | { |
Ed Tanous | 6fb96ce | 2024-01-28 21:30:06 -0800 | [diff] [blame] | 383 | std::string_view body = |
Krzysztof Grobelny | 18e3f7f | 2022-08-24 09:24:33 +0200 | [diff] [blame] | 384 | "----XX\r\n" |
| 385 | "Content-Disposition: form-data; name=\"Test2\"\r\n\r\n" |
| 386 | "t-DiPpccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccAAAAAAAAAAAAAAABCDz\r\n" |
| 387 | "\335\r\n\r\n"; |
| 388 | |
Ed Tanous | 6fb96ce | 2024-01-28 21:30:06 -0800 | [diff] [blame] | 389 | crow::Request reqIn(body, ec); |
| 390 | |
| 391 | reqIn.addHeader("Content-Type", "multipart/form-data; boundary=--XX"); |
| 392 | |
Krzysztof Grobelny | 18e3f7f | 2022-08-24 09:24:33 +0200 | [diff] [blame] | 393 | ParserError rc = parser.parse(reqIn); |
| 394 | |
| 395 | EXPECT_EQ(rc, ParserError::ERROR_UNEXPECTED_END_OF_INPUT); |
| 396 | } |
| 397 | |
| 398 | TEST_F(MultipartTest, TestIgnoreDataAfterFinalBoundary) |
| 399 | { |
Ed Tanous | 6fb96ce | 2024-01-28 21:30:06 -0800 | [diff] [blame] | 400 | std::string_view body = |
| 401 | "----XX\r\n" |
| 402 | "Content-Disposition: form-data; name=\"Test1\"\r\n\r\n" |
| 403 | "Data1\r\n" |
| 404 | "----XX--\r\n" |
| 405 | "Content-Disposition: form-data; name=\"Test2\"\r\n\r\n" |
| 406 | "Data2\r\n" |
| 407 | "----XX--\r\n"; |
Krzysztof Grobelny | 18e3f7f | 2022-08-24 09:24:33 +0200 | [diff] [blame] | 408 | |
Ed Tanous | 6fb96ce | 2024-01-28 21:30:06 -0800 | [diff] [blame] | 409 | crow::Request reqIn(body, ec); |
Krzysztof Grobelny | 18e3f7f | 2022-08-24 09:24:33 +0200 | [diff] [blame] | 410 | |
Ed Tanous | 6fb96ce | 2024-01-28 21:30:06 -0800 | [diff] [blame] | 411 | reqIn.addHeader("Content-Type", "multipart/form-data; boundary=--XX"); |
| 412 | |
Krzysztof Grobelny | 18e3f7f | 2022-08-24 09:24:33 +0200 | [diff] [blame] | 413 | ParserError rc = parser.parse(reqIn); |
| 414 | |
| 415 | ASSERT_EQ(rc, ParserError::PARSER_SUCCESS); |
| 416 | |
| 417 | EXPECT_EQ(parser.boundary, "\r\n----XX"); |
| 418 | EXPECT_EQ(parser.mime_fields.size(), 1); |
| 419 | |
| 420 | EXPECT_EQ(parser.mime_fields[0].fields.at("Content-Disposition"), |
| 421 | "form-data; name=\"Test1\""); |
| 422 | EXPECT_EQ(parser.mime_fields[0].content, "Data1"); |
| 423 | } |
| 424 | |
| 425 | TEST_F(MultipartTest, TestFinalBoundaryIsCorrectlyRecognized) |
| 426 | { |
Ed Tanous | 6fb96ce | 2024-01-28 21:30:06 -0800 | [diff] [blame] | 427 | std::string_view body = |
| 428 | "----XX\r\n" |
| 429 | "Content-Disposition: form-data; name=\"Test1\"\r\n\r\n" |
| 430 | "Data1\r\n" |
| 431 | "----XX-abc-\r\n" |
| 432 | "StillData1\r\n" |
| 433 | "----XX--\r\n"; |
Krzysztof Grobelny | 18e3f7f | 2022-08-24 09:24:33 +0200 | [diff] [blame] | 434 | |
Ed Tanous | 6fb96ce | 2024-01-28 21:30:06 -0800 | [diff] [blame] | 435 | crow::Request reqIn(body, ec); |
Krzysztof Grobelny | 18e3f7f | 2022-08-24 09:24:33 +0200 | [diff] [blame] | 436 | |
Ed Tanous | 6fb96ce | 2024-01-28 21:30:06 -0800 | [diff] [blame] | 437 | reqIn.addHeader("Content-Type", "multipart/form-data; boundary=--XX"); |
| 438 | |
Krzysztof Grobelny | 18e3f7f | 2022-08-24 09:24:33 +0200 | [diff] [blame] | 439 | ParserError rc = parser.parse(reqIn); |
| 440 | |
| 441 | ASSERT_EQ(rc, ParserError::PARSER_SUCCESS); |
| 442 | |
| 443 | EXPECT_EQ(parser.boundary, "\r\n----XX"); |
| 444 | EXPECT_EQ(parser.mime_fields.size(), 1); |
| 445 | |
| 446 | EXPECT_EQ(parser.mime_fields[0].fields.at("Content-Disposition"), |
| 447 | "form-data; name=\"Test1\""); |
| 448 | EXPECT_EQ(parser.mime_fields[0].content, "Data1\r\n" |
| 449 | "----XX-abc-\r\n" |
| 450 | "StillData1"); |
| 451 | } |
| 452 | |
| 453 | } // namespace |