1
0

MultipartRequestParserTest.php 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2021, Louis Chemineau <louis@chmn.me>
  4. *
  5. * @author Louis Chemineau <louis@chmn.me>
  6. *
  7. * @license AGPL-3.0
  8. *
  9. * This code is free software: you can redistribute it and/or modify
  10. * it under the terms of the GNU Affero General Public License, version 3,
  11. * as published by the Free Software Foundation.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU Affero General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU Affero General Public License, version 3,
  19. * along with this program. If not, see <http://www.gnu.org/licenses/>
  20. *
  21. */
  22. namespace OCA\DAV\Tests\unit\DAV;
  23. use \OCA\DAV\BulkUpload\MultipartRequestParser;
  24. use Psr\Log\LoggerInterface;
  25. use Test\TestCase;
  26. class MultipartRequestParserTest extends TestCase {
  27. protected LoggerInterface $logger;
  28. protected function setUp(): void {
  29. $this->logger = $this->createMock(LoggerInterface::class);
  30. }
  31. private function getValidBodyObject() {
  32. return [
  33. [
  34. "headers" => [
  35. "Content-Length" => 7,
  36. "X-File-MD5" => "4f2377b4d911f7ec46325fe603c3af03",
  37. "X-File-Path" => "/coucou.txt"
  38. ],
  39. "content" => "Coucou\n"
  40. ]
  41. ];
  42. }
  43. private function getMultipartParser(array $parts, array $headers = [], string $boundary = "boundary_azertyuiop"): MultipartRequestParser {
  44. $request = $this->getMockBuilder('Sabre\HTTP\RequestInterface')
  45. ->disableOriginalConstructor()
  46. ->getMock();
  47. $headers = array_merge(['Content-Type' => 'multipart/related; boundary='.$boundary], $headers);
  48. $request->expects($this->any())
  49. ->method('getHeader')
  50. ->willReturnCallback(function (string $key) use (&$headers) {
  51. return $headers[$key];
  52. });
  53. $body = "";
  54. foreach ($parts as $part) {
  55. $body .= '--'.$boundary."\r\n";
  56. foreach ($part['headers'] as $headerKey => $headerPart) {
  57. $body .= $headerKey.": ".$headerPart."\r\n";
  58. }
  59. $body .= "\r\n";
  60. $body .= $part['content']."\r\n";
  61. }
  62. $body .= '--'.$boundary."--";
  63. $stream = fopen('php://temp', 'r+');
  64. fwrite($stream, $body);
  65. rewind($stream);
  66. $request->expects($this->any())
  67. ->method('getBody')
  68. ->willReturn($stream);
  69. return new MultipartRequestParser($request, $this->logger);
  70. }
  71. /**
  72. * Test validation of the request's body type
  73. */
  74. public function testBodyTypeValidation(): void {
  75. $bodyStream = "I am not a stream, but pretend to be";
  76. $request = $this->getMockBuilder('Sabre\HTTP\RequestInterface')
  77. ->disableOriginalConstructor()
  78. ->getMock();
  79. $request->expects($this->any())
  80. ->method('getBody')
  81. ->willReturn($bodyStream);
  82. $this->expectExceptionMessage('Body should be of type resource');
  83. new MultipartRequestParser($request, $this->logger);
  84. }
  85. /**
  86. * Test with valid request.
  87. * - valid boundary
  88. * - valid md5 hash
  89. * - valid content-length
  90. * - valid file content
  91. * - valid file path
  92. */
  93. public function testValidRequest(): void {
  94. $multipartParser = $this->getMultipartParser(
  95. $this->getValidBodyObject()
  96. );
  97. [$headers, $content] = $multipartParser->parseNextPart();
  98. $this->assertSame((int)$headers["content-length"], 7, "Content-Length header should be the same as provided.");
  99. $this->assertSame($headers["x-file-md5"], "4f2377b4d911f7ec46325fe603c3af03", "X-File-MD5 header should be the same as provided.");
  100. $this->assertSame($headers["x-file-path"], "/coucou.txt", "X-File-Path header should be the same as provided.");
  101. $this->assertSame($content, "Coucou\n", "Content should be the same");
  102. }
  103. /**
  104. * Test with invalid md5 hash.
  105. */
  106. public function testInvalidMd5Hash(): void {
  107. $bodyObject = $this->getValidBodyObject();
  108. $bodyObject["0"]["headers"]["X-File-MD5"] = "f2377b4d911f7ec46325fe603c3af03";
  109. $multipartParser = $this->getMultipartParser(
  110. $bodyObject
  111. );
  112. $this->expectExceptionMessage('Computed md5 hash is incorrect.');
  113. $multipartParser->parseNextPart();
  114. }
  115. /**
  116. * Test with a null md5 hash.
  117. */
  118. public function testNullMd5Hash(): void {
  119. $bodyObject = $this->getValidBodyObject();
  120. unset($bodyObject["0"]["headers"]["X-File-MD5"]);
  121. $multipartParser = $this->getMultipartParser(
  122. $bodyObject
  123. );
  124. $this->expectExceptionMessage('The X-File-MD5 header must not be null.');
  125. $multipartParser->parseNextPart();
  126. }
  127. /**
  128. * Test with a null Content-Length.
  129. */
  130. public function testNullContentLength(): void {
  131. $bodyObject = $this->getValidBodyObject();
  132. unset($bodyObject["0"]["headers"]["Content-Length"]);
  133. $multipartParser = $this->getMultipartParser(
  134. $bodyObject
  135. );
  136. $this->expectExceptionMessage('The Content-Length header must not be null.');
  137. $multipartParser->parseNextPart();
  138. }
  139. /**
  140. * Test with a lower Content-Length.
  141. */
  142. public function testLowerContentLength(): void {
  143. $bodyObject = $this->getValidBodyObject();
  144. $bodyObject["0"]["headers"]["Content-Length"] = 6;
  145. $multipartParser = $this->getMultipartParser(
  146. $bodyObject
  147. );
  148. $this->expectExceptionMessage('Computed md5 hash is incorrect.');
  149. $multipartParser->parseNextPart();
  150. }
  151. /**
  152. * Test with a higher Content-Length.
  153. */
  154. public function testHigherContentLength(): void {
  155. $bodyObject = $this->getValidBodyObject();
  156. $bodyObject["0"]["headers"]["Content-Length"] = 8;
  157. $multipartParser = $this->getMultipartParser(
  158. $bodyObject
  159. );
  160. $this->expectExceptionMessage('Computed md5 hash is incorrect.');
  161. $multipartParser->parseNextPart();
  162. }
  163. /**
  164. * Test with wrong boundary in body.
  165. */
  166. public function testWrongBoundary(): void {
  167. $bodyObject = $this->getValidBodyObject();
  168. $multipartParser = $this->getMultipartParser(
  169. $bodyObject,
  170. ['Content-Type' => 'multipart/related; boundary=boundary_poiuytreza']
  171. );
  172. $this->expectExceptionMessage('Boundary not found where it should be.');
  173. $multipartParser->parseNextPart();
  174. }
  175. /**
  176. * Test with no boundary in request headers.
  177. */
  178. public function testNoBoundaryInHeader(): void {
  179. $bodyObject = $this->getValidBodyObject();
  180. $this->expectExceptionMessage('Error while parsing boundary in Content-Type header.');
  181. $this->getMultipartParser(
  182. $bodyObject,
  183. ['Content-Type' => 'multipart/related']
  184. );
  185. }
  186. /**
  187. * Test with no boundary in the request's headers.
  188. */
  189. public function testNoBoundaryInBody(): void {
  190. $bodyObject = $this->getValidBodyObject();
  191. $multipartParser = $this->getMultipartParser(
  192. $bodyObject,
  193. ['Content-Type' => 'multipart/related; boundary=boundary_azertyuiop'],
  194. ''
  195. );
  196. $this->expectExceptionMessage('Boundary not found where it should be.');
  197. $multipartParser->parseNextPart();
  198. }
  199. /**
  200. * Test with a boundary with quotes in the request's headers.
  201. */
  202. public function testBoundaryWithQuotes(): void {
  203. $bodyObject = $this->getValidBodyObject();
  204. $multipartParser = $this->getMultipartParser(
  205. $bodyObject,
  206. ['Content-Type' => 'multipart/related; boundary="boundary_azertyuiop"'],
  207. );
  208. $multipartParser->parseNextPart();
  209. // Dummy assertion, we just want to test that the parsing works.
  210. $this->assertTrue(true);
  211. }
  212. /**
  213. * Test with a wrong Content-Type in the request's headers.
  214. */
  215. public function testWrongContentType(): void {
  216. $bodyObject = $this->getValidBodyObject();
  217. $this->expectExceptionMessage('Content-Type must be multipart/related');
  218. $this->getMultipartParser(
  219. $bodyObject,
  220. ['Content-Type' => 'multipart/form-data; boundary="boundary_azertyuiop"'],
  221. );
  222. }
  223. /**
  224. * Test with a wrong key after the content type in the request's headers.
  225. */
  226. public function testWrongKeyInContentType(): void {
  227. $bodyObject = $this->getValidBodyObject();
  228. $this->expectExceptionMessage('Boundary is invalid');
  229. $this->getMultipartParser(
  230. $bodyObject,
  231. ['Content-Type' => 'multipart/related; wrongkey="boundary_azertyuiop"'],
  232. );
  233. }
  234. /**
  235. * Test with a null Content-Type in the request's headers.
  236. */
  237. public function testNullContentType(): void {
  238. $bodyObject = $this->getValidBodyObject();
  239. $this->expectExceptionMessage('Content-Type can not be null');
  240. $this->getMultipartParser(
  241. $bodyObject,
  242. ['Content-Type' => null],
  243. );
  244. }
  245. }