|
|
|
|
@ -7,12 +7,14 @@ |
|
|
|
|
namespace OCA\DAV\Tests\unit\DAV; |
|
|
|
|
|
|
|
|
|
use OCA\DAV\BulkUpload\MultipartRequestParser; |
|
|
|
|
use PHPUnit\Framework\MockObject\MockObject; |
|
|
|
|
use Psr\Log\LoggerInterface; |
|
|
|
|
use Sabre\HTTP\RequestInterface; |
|
|
|
|
use Test\TestCase; |
|
|
|
|
|
|
|
|
|
class MultipartRequestParserTest extends TestCase { |
|
|
|
|
|
|
|
|
|
protected LoggerInterface $logger; |
|
|
|
|
protected LoggerInterface&MockObject $logger; |
|
|
|
|
|
|
|
|
|
protected function setUp(): void { |
|
|
|
|
$this->logger = $this->createMock(LoggerInterface::class); |
|
|
|
|
@ -24,6 +26,7 @@ class MultipartRequestParserTest extends TestCase { |
|
|
|
|
'headers' => [ |
|
|
|
|
'Content-Length' => 7, |
|
|
|
|
'X-File-MD5' => '4f2377b4d911f7ec46325fe603c3af03', |
|
|
|
|
'OC-Checksum' => 'md5:4f2377b4d911f7ec46325fe603c3af03', |
|
|
|
|
'X-File-Path' => '/coucou.txt' |
|
|
|
|
], |
|
|
|
|
'content' => "Coucou\n" |
|
|
|
|
@ -32,7 +35,8 @@ class MultipartRequestParserTest extends TestCase { |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
private function getMultipartParser(array $parts, array $headers = [], string $boundary = 'boundary_azertyuiop'): MultipartRequestParser { |
|
|
|
|
$request = $this->getMockBuilder('Sabre\HTTP\RequestInterface') |
|
|
|
|
/** @var RequestInterface&MockObject $request */ |
|
|
|
|
$request = $this->getMockBuilder(RequestInterface::class) |
|
|
|
|
->disableOriginalConstructor() |
|
|
|
|
->getMock(); |
|
|
|
|
|
|
|
|
|
@ -74,7 +78,8 @@ class MultipartRequestParserTest extends TestCase { |
|
|
|
|
*/ |
|
|
|
|
public function testBodyTypeValidation(): void { |
|
|
|
|
$bodyStream = 'I am not a stream, but pretend to be'; |
|
|
|
|
$request = $this->getMockBuilder('Sabre\HTTP\RequestInterface') |
|
|
|
|
/** @var RequestInterface&MockObject $request */ |
|
|
|
|
$request = $this->getMockBuilder(RequestInterface::class) |
|
|
|
|
->disableOriginalConstructor() |
|
|
|
|
->getMock(); |
|
|
|
|
$request->expects($this->any()) |
|
|
|
|
@ -88,15 +93,39 @@ class MultipartRequestParserTest extends TestCase { |
|
|
|
|
/** |
|
|
|
|
* Test with valid request. |
|
|
|
|
* - valid boundary |
|
|
|
|
* - valid md5 hash |
|
|
|
|
* - valid hash |
|
|
|
|
* - valid content-length |
|
|
|
|
* - valid file content |
|
|
|
|
* - valid file path |
|
|
|
|
*/ |
|
|
|
|
public function testValidRequest(): void { |
|
|
|
|
$multipartParser = $this->getMultipartParser( |
|
|
|
|
$this->getValidBodyObject() |
|
|
|
|
); |
|
|
|
|
$bodyObject = $this->getValidBodyObject(); |
|
|
|
|
unset($bodyObject['0']['headers']['X-File-MD5']); |
|
|
|
|
|
|
|
|
|
$multipartParser = $this->getMultipartParser($bodyObject); |
|
|
|
|
|
|
|
|
|
[$headers, $content] = $multipartParser->parseNextPart(); |
|
|
|
|
|
|
|
|
|
$this->assertSame((int)$headers['content-length'], 7, 'Content-Length header should be the same as provided.'); |
|
|
|
|
$this->assertSame($headers['oc-checksum'], 'md5:4f2377b4d911f7ec46325fe603c3af03', 'OC-Checksum header should be the same as provided.'); |
|
|
|
|
$this->assertSame($headers['x-file-path'], '/coucou.txt', 'X-File-Path header should be the same as provided.'); |
|
|
|
|
|
|
|
|
|
$this->assertSame($content, "Coucou\n", 'Content should be the same'); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* Test with valid request. |
|
|
|
|
* - valid boundary |
|
|
|
|
* - valid md5 hash |
|
|
|
|
* - valid content-length |
|
|
|
|
* - valid file content |
|
|
|
|
* - valid file path |
|
|
|
|
*/ |
|
|
|
|
public function testValidRequestWithMd5(): void { |
|
|
|
|
$bodyObject = $this->getValidBodyObject(); |
|
|
|
|
unset($bodyObject['0']['headers']['OC-Checksum']); |
|
|
|
|
|
|
|
|
|
$multipartParser = $this->getMultipartParser($bodyObject); |
|
|
|
|
|
|
|
|
|
[$headers, $content] = $multipartParser->parseNextPart(); |
|
|
|
|
|
|
|
|
|
@ -107,31 +136,48 @@ class MultipartRequestParserTest extends TestCase { |
|
|
|
|
$this->assertSame($content, "Coucou\n", 'Content should be the same'); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* Test with invalid hash. |
|
|
|
|
*/ |
|
|
|
|
public function testInvalidHash(): void { |
|
|
|
|
$bodyObject = $this->getValidBodyObject(); |
|
|
|
|
$bodyObject['0']['headers']['OC-Checksum'] = 'md5:f2377b4d911f7ec46325fe603c3af03'; |
|
|
|
|
unset($bodyObject['0']['headers']['X-File-MD5']); |
|
|
|
|
$multipartParser = $this->getMultipartParser( |
|
|
|
|
$bodyObject |
|
|
|
|
); |
|
|
|
|
|
|
|
|
|
$this->expectExceptionMessage('Computed md5 hash is incorrect (4f2377b4d911f7ec46325fe603c3af03).'); |
|
|
|
|
$multipartParser->parseNextPart(); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* Test with invalid md5 hash. |
|
|
|
|
*/ |
|
|
|
|
public function testInvalidMd5Hash(): void { |
|
|
|
|
$bodyObject = $this->getValidBodyObject(); |
|
|
|
|
unset($bodyObject['0']['headers']['OC-Checksum']); |
|
|
|
|
$bodyObject['0']['headers']['X-File-MD5'] = 'f2377b4d911f7ec46325fe603c3af03'; |
|
|
|
|
$multipartParser = $this->getMultipartParser( |
|
|
|
|
$bodyObject |
|
|
|
|
); |
|
|
|
|
|
|
|
|
|
$this->expectExceptionMessage('Computed md5 hash is incorrect.'); |
|
|
|
|
$this->expectExceptionMessage('Computed md5 hash is incorrect (4f2377b4d911f7ec46325fe603c3af03).'); |
|
|
|
|
$multipartParser->parseNextPart(); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* Test with a null md5 hash. |
|
|
|
|
* Test with a null hash headers. |
|
|
|
|
*/ |
|
|
|
|
public function testNullMd5Hash(): void { |
|
|
|
|
public function testNullHash(): void { |
|
|
|
|
$bodyObject = $this->getValidBodyObject(); |
|
|
|
|
unset($bodyObject['0']['headers']['OC-Checksum']); |
|
|
|
|
unset($bodyObject['0']['headers']['X-File-MD5']); |
|
|
|
|
$multipartParser = $this->getMultipartParser( |
|
|
|
|
$bodyObject |
|
|
|
|
); |
|
|
|
|
|
|
|
|
|
$this->expectExceptionMessage('The X-File-MD5 header must not be null.'); |
|
|
|
|
$this->expectExceptionMessage('The hash headers must not be null.'); |
|
|
|
|
$multipartParser->parseNextPart(); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@ -159,7 +205,7 @@ class MultipartRequestParserTest extends TestCase { |
|
|
|
|
$bodyObject |
|
|
|
|
); |
|
|
|
|
|
|
|
|
|
$this->expectExceptionMessage('Computed md5 hash is incorrect.'); |
|
|
|
|
$this->expectExceptionMessage('Computed md5 hash is incorrect (41060d3ddfdf63e68fc2bf196f652ee9).'); |
|
|
|
|
$multipartParser->parseNextPart(); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@ -173,7 +219,7 @@ class MultipartRequestParserTest extends TestCase { |
|
|
|
|
$bodyObject |
|
|
|
|
); |
|
|
|
|
|
|
|
|
|
$this->expectExceptionMessage('Computed md5 hash is incorrect.'); |
|
|
|
|
$this->expectExceptionMessage('Computed md5 hash is incorrect (0161002bbee6a744f18741b8a914e413).'); |
|
|
|
|
$multipartParser->parseNextPart(); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|