1
0

MultipartRequestParserTest.php 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  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 Test\TestCase;
  24. use \OCA\DAV\BulkUpload\MultipartRequestParser;
  25. class MultipartRequestParserTest extends TestCase {
  26. private function getValidBodyObject() {
  27. return [
  28. [
  29. "headers" => [
  30. "Content-Length" => 7,
  31. "X-File-MD5" => "4f2377b4d911f7ec46325fe603c3af03",
  32. "X-File-Path" => "/coucou.txt"
  33. ],
  34. "content" => "Coucou\n"
  35. ]
  36. ];
  37. }
  38. private function getMultipartParser(array $parts, array $headers = [], string $boundary = "boundary_azertyuiop"): MultipartRequestParser {
  39. $request = $this->getMockBuilder('Sabre\HTTP\RequestInterface')
  40. ->disableOriginalConstructor()
  41. ->getMock();
  42. $headers = array_merge(['Content-Type' => 'multipart/related; boundary='.$boundary], $headers);
  43. $request->expects($this->any())
  44. ->method('getHeader')
  45. ->willReturnCallback(function (string $key) use (&$headers) {
  46. return $headers[$key];
  47. });
  48. $body = "";
  49. foreach ($parts as $part) {
  50. $body .= '--'.$boundary."\r\n";
  51. foreach ($part['headers'] as $headerKey => $headerPart) {
  52. $body .= $headerKey.": ".$headerPart."\r\n";
  53. }
  54. $body .= "\r\n";
  55. $body .= $part['content']."\r\n";
  56. }
  57. $body .= '--'.$boundary."--";
  58. $stream = fopen('php://temp','r+');
  59. fwrite($stream, $body);
  60. rewind($stream);
  61. $request->expects($this->any())
  62. ->method('getBody')
  63. ->willReturn($stream);
  64. return new MultipartRequestParser($request);
  65. }
  66. /**
  67. * Test validation of the request's body type
  68. */
  69. public function testBodyTypeValidation(): void {
  70. $bodyStream = "I am not a stream, but pretend to be";
  71. $request = $this->getMockBuilder('Sabre\HTTP\RequestInterface')
  72. ->disableOriginalConstructor()
  73. ->getMock();
  74. $request->expects($this->any())
  75. ->method('getBody')
  76. ->willReturn($bodyStream);
  77. $this->expectExceptionMessage('Body should be of type resource');
  78. new MultipartRequestParser($request);
  79. }
  80. /**
  81. * Test with valid request.
  82. * - valid boundary
  83. * - valid md5 hash
  84. * - valid content-length
  85. * - valid file content
  86. * - valid file path
  87. */
  88. public function testValidRequest(): void {
  89. $multipartParser = $this->getMultipartParser(
  90. $this->getValidBodyObject()
  91. );
  92. [$headers, $content] = $multipartParser->parseNextPart();
  93. $this->assertSame((int)$headers["content-length"], 7, "Content-Length header should be the same as provided.");
  94. $this->assertSame($headers["x-file-md5"], "4f2377b4d911f7ec46325fe603c3af03", "X-File-MD5 header should be the same as provided.");
  95. $this->assertSame($headers["x-file-path"], "/coucou.txt", "X-File-Path header should be the same as provided.");
  96. $this->assertSame($content, "Coucou\n", "Content should be the same");
  97. }
  98. /**
  99. * Test with invalid md5 hash.
  100. */
  101. public function testInvalidMd5Hash(): void {
  102. $bodyObject = $this->getValidBodyObject();
  103. $bodyObject["0"]["headers"]["X-File-MD5"] = "f2377b4d911f7ec46325fe603c3af03";
  104. $multipartParser = $this->getMultipartParser(
  105. $bodyObject
  106. );
  107. $this->expectExceptionMessage('Computed md5 hash is incorrect.');
  108. $multipartParser->parseNextPart();
  109. }
  110. /**
  111. * Test with a null md5 hash.
  112. */
  113. public function testNullMd5Hash(): void {
  114. $bodyObject = $this->getValidBodyObject();
  115. unset($bodyObject["0"]["headers"]["X-File-MD5"]);
  116. $multipartParser = $this->getMultipartParser(
  117. $bodyObject
  118. );
  119. $this->expectExceptionMessage('The X-File-MD5 header must not be null.');
  120. $multipartParser->parseNextPart();
  121. }
  122. /**
  123. * Test with a null Content-Length.
  124. */
  125. public function testNullContentLength(): void {
  126. $bodyObject = $this->getValidBodyObject();
  127. unset($bodyObject["0"]["headers"]["Content-Length"]);
  128. $multipartParser = $this->getMultipartParser(
  129. $bodyObject
  130. );
  131. $this->expectExceptionMessage('The Content-Length header must not be null.');
  132. $multipartParser->parseNextPart();
  133. }
  134. /**
  135. * Test with a lower Content-Length.
  136. */
  137. public function testLowerContentLength(): void {
  138. $bodyObject = $this->getValidBodyObject();
  139. $bodyObject["0"]["headers"]["Content-Length"] = 6;
  140. $multipartParser = $this->getMultipartParser(
  141. $bodyObject
  142. );
  143. $this->expectExceptionMessage('Computed md5 hash is incorrect.');
  144. $multipartParser->parseNextPart();
  145. }
  146. /**
  147. * Test with a higher Content-Length.
  148. */
  149. public function testHigherContentLength(): void {
  150. $bodyObject = $this->getValidBodyObject();
  151. $bodyObject["0"]["headers"]["Content-Length"] = 8;
  152. $multipartParser = $this->getMultipartParser(
  153. $bodyObject
  154. );
  155. $this->expectExceptionMessage('Computed md5 hash is incorrect.');
  156. $multipartParser->parseNextPart();
  157. }
  158. /**
  159. * Test with wrong boundary in body.
  160. */
  161. public function testWrongBoundary(): void {
  162. $bodyObject = $this->getValidBodyObject();
  163. $multipartParser = $this->getMultipartParser(
  164. $bodyObject,
  165. ['Content-Type' => 'multipart/related; boundary=boundary_poiuytreza']
  166. );
  167. $this->expectExceptionMessage('Boundary not found where it should be.');
  168. $multipartParser->parseNextPart();
  169. }
  170. /**
  171. * Test with no boundary in request headers.
  172. */
  173. public function testNoBoundaryInHeader(): void {
  174. $bodyObject = $this->getValidBodyObject();
  175. $this->expectExceptionMessage('Error while parsing boundary in Content-Type header.');
  176. $this->getMultipartParser(
  177. $bodyObject,
  178. ['Content-Type' => 'multipart/related']
  179. );
  180. }
  181. /**
  182. * Test with no boundary in the request's headers.
  183. */
  184. public function testNoBoundaryInBody(): void {
  185. $bodyObject = $this->getValidBodyObject();
  186. $multipartParser = $this->getMultipartParser(
  187. $bodyObject,
  188. ['Content-Type' => 'multipart/related; boundary=boundary_azertyuiop'],
  189. ''
  190. );
  191. $this->expectExceptionMessage('Boundary not found where it should be.');
  192. $multipartParser->parseNextPart();
  193. }
  194. /**
  195. * Test with a boundary with quotes in the request's headers.
  196. */
  197. public function testBoundaryWithQuotes(): void {
  198. $bodyObject = $this->getValidBodyObject();
  199. $multipartParser = $this->getMultipartParser(
  200. $bodyObject,
  201. ['Content-Type' => 'multipart/related; boundary="boundary_azertyuiop"'],
  202. );
  203. $multipartParser->parseNextPart();
  204. // Dummy assertion, we just want to test that the parsing works.
  205. $this->assertTrue(true);
  206. }
  207. /**
  208. * Test with a wrong Content-Type in the request's headers.
  209. */
  210. public function testWrongContentType(): void {
  211. $bodyObject = $this->getValidBodyObject();
  212. $this->expectExceptionMessage('Content-Type must be multipart/related');
  213. $this->getMultipartParser(
  214. $bodyObject,
  215. ['Content-Type' => 'multipart/form-data; boundary="boundary_azertyuiop"'],
  216. );
  217. }
  218. /**
  219. * Test with a wrong key after the content type in the request's headers.
  220. */
  221. public function testWrongKeyInContentType(): void {
  222. $bodyObject = $this->getValidBodyObject();
  223. $this->expectExceptionMessage('Boundary is invalid');
  224. $this->getMultipartParser(
  225. $bodyObject,
  226. ['Content-Type' => 'multipart/related; wrongkey="boundary_azertyuiop"'],
  227. );
  228. }
  229. /**
  230. * Test with a null Content-Type in the request's headers.
  231. */
  232. public function testNullContentType(): void {
  233. $bodyObject = $this->getValidBodyObject();
  234. $this->expectExceptionMessage('Content-Type can not be null');
  235. $this->getMultipartParser(
  236. $bodyObject,
  237. ['Content-Type' => null],
  238. );
  239. }
  240. }