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