diff options
author | Lukas Reschke <lukas@owncloud.com> | 2015-09-24 16:35:47 +0300 |
---|---|---|
committer | Lukas Reschke <lukas@owncloud.com> | 2015-09-24 16:35:47 +0300 |
commit | 6e9f11c70ff6624742b3d390204805cfb7673e9e (patch) | |
tree | 5ee42f4cc8b878af33d2d3a6478bbdb4da7b2d42 /tests | |
parent | e6421754b61a4491329d0458b37311d5d9b73ea0 (diff) | |
parent | f67d0c2d78b4f28347727ca5384948bdb4e88f55 (diff) |
Merge pull request #361 from owncloud/secure-mimetypes
Secure mimetypes
Diffstat (limited to 'tests')
-rw-r--r-- | tests/unit/GalleryUnitTest.php | 4 | ||||
-rw-r--r-- | tests/unit/controller/ConfigApiControllerTest.php | 1 | ||||
-rw-r--r-- | tests/unit/controller/ConfigControllerTest.php | 3 | ||||
-rw-r--r-- | tests/unit/controller/ConfigPublicControllerTest.php | 1 | ||||
-rw-r--r-- | tests/unit/controller/FilesControllerTest.php | 32 | ||||
-rw-r--r-- | tests/unit/controller/PreviewApiControllerTest.php | 51 | ||||
-rw-r--r-- | tests/unit/controller/PreviewControllerTest.php | 68 | ||||
-rw-r--r-- | tests/unit/controller/PreviewPublicControllerTest.php | 1 | ||||
-rw-r--r-- | tests/unit/service/ConfigServiceTest.php | 206 | ||||
-rw-r--r-- | tests/unit/service/PreviewServiceTest.php | 92 |
10 files changed, 339 insertions, 120 deletions
diff --git a/tests/unit/GalleryUnitTest.php b/tests/unit/GalleryUnitTest.php index b506e3f5..44b75913 100644 --- a/tests/unit/GalleryUnitTest.php +++ b/tests/unit/GalleryUnitTest.php @@ -13,6 +13,8 @@ namespace Test; use OCP\ILogger; +use OCP\Files\File; +use OCP\Files\Folder; use OCA\Gallery\Environment\Environment; use OCA\Gallery\Service\ServiceException; @@ -52,7 +54,7 @@ abstract class GalleryUnitTest extends \Test\TestCase { * * @param object $mockedObject * @param int $fileId - * @param \PHPUnit_Framework_MockObject_MockObject $answer + * @param File|Folder $answer */ protected function mockGetResourceFromId($mockedObject, $fileId, $answer) { $mockedObject->expects($this->once()) diff --git a/tests/unit/controller/ConfigApiControllerTest.php b/tests/unit/controller/ConfigApiControllerTest.php index 35c65ab8..c6221c14 100644 --- a/tests/unit/controller/ConfigApiControllerTest.php +++ b/tests/unit/controller/ConfigApiControllerTest.php @@ -27,7 +27,6 @@ class ConfigApiControllerTest extends ConfigControllerTest { $this->appName, $this->request, $this->configService, - $this->previewService, $this->logger ); } diff --git a/tests/unit/controller/ConfigControllerTest.php b/tests/unit/controller/ConfigControllerTest.php index 0d5932e6..95ecbbd9 100644 --- a/tests/unit/controller/ConfigControllerTest.php +++ b/tests/unit/controller/ConfigControllerTest.php @@ -77,7 +77,6 @@ class ConfigControllerTest extends \Test\TestCase { $this->appName, $this->request, $this->configService, - $this->previewService, $this->logger ); } @@ -202,7 +201,7 @@ class ConfigControllerTest extends \Test\TestCase { * @param $mimeTypes */ private function mockSupportedMediaTypes($slideshow, $nativeSvgSupport, $mimeTypes) { - $this->previewService->expects($this->any()) + $this->configService->expects($this->any()) ->method('getSupportedMediaTypes') ->with( $this->equalTo($slideshow), diff --git a/tests/unit/controller/ConfigPublicControllerTest.php b/tests/unit/controller/ConfigPublicControllerTest.php index dfd6e271..ea1e4e24 100644 --- a/tests/unit/controller/ConfigPublicControllerTest.php +++ b/tests/unit/controller/ConfigPublicControllerTest.php @@ -27,7 +27,6 @@ class ConfigPublicControllerTest extends ConfigControllerTest { $this->appName, $this->request, $this->configService, - $this->previewService, $this->logger ); } diff --git a/tests/unit/controller/FilesControllerTest.php b/tests/unit/controller/FilesControllerTest.php index 00b45734..a9e45860 100644 --- a/tests/unit/controller/FilesControllerTest.php +++ b/tests/unit/controller/FilesControllerTest.php @@ -15,6 +15,7 @@ namespace OCA\Gallery\Controller; use OCA\Gallery\Service\ServiceException; use OCP\IRequest; use OCP\IURLGenerator; +use OCP\Files\File; use OCP\ILogger; use OCP\AppFramework\IAppContainer; @@ -105,18 +106,35 @@ class FilesControllerTest extends \Test\GalleryUnitTest { ); } - public function testDownload() { - $fileId = 1234; - $filename = null; + /** + * @return array + */ + public function providesTestDownloadData() { + return [ + [1234, $this->mockJpgFile(1234), 'image/jpeg'], + [4567, $this->mockSvgFile(4567), 'text/plain'] + ]; + } - $download = $this->mockGetDownload($fileId, $filename); + /** + * @dataProvider providesTestDownloadData + * + * @param int $fileId + * @param File $file + * @param string $expectedMimeType + * + * @internal param string $type + */ + public function testDownload($fileId, $file, $expectedMimeType) { + $filename = null; + $download = $this->mockGetDownload($fileId, $file, $filename); /** @type ImageResponse $response */ $response = $this->controller->download($fileId, $filename); $this->assertEquals(Http::STATUS_OK, $response->getStatus()); $this->assertEquals( - $download['mimetype'] . '; charset=utf-8', $response->getHeaders()['Content-type'] + $expectedMimeType . '; charset=utf-8', $response->getHeaders()['Content-type'] ); $this->assertEquals($download['preview'], $response->render()); } @@ -262,9 +280,7 @@ class FilesControllerTest extends \Test\GalleryUnitTest { * * @return array */ - private function mockGetDownload($fileId, $filename) { - $file = $this->mockFile($fileId); - + private function mockGetDownload($fileId, $file, $filename) { $this->mockGetResourceFromId($this->downloadService, $fileId, $file); $download = $this->mockDownloadData($file, $filename); diff --git a/tests/unit/controller/PreviewApiControllerTest.php b/tests/unit/controller/PreviewApiControllerTest.php index 1029fa2f..914889d8 100644 --- a/tests/unit/controller/PreviewApiControllerTest.php +++ b/tests/unit/controller/PreviewApiControllerTest.php @@ -14,6 +14,12 @@ namespace OCA\Gallery\Controller; require_once __DIR__ . '/PreviewControllerTest.php'; +use OCP\Files\File; + +use OCP\AppFramework\Http; + +use OCA\Gallery\Http\ImageResponse; + /** * Class PreviewApiControllerTest * @@ -21,12 +27,16 @@ require_once __DIR__ . '/PreviewControllerTest.php'; */ class PreviewApiControllerTest extends PreviewControllerTest { + /** @var PreviewApiController */ + protected $controller; + public function setUp() { parent::setUp(); $this->controller = new PreviewApiController( $this->appName, $this->request, $this->urlGenerator, + $this->configService, $this->thumbnailService, $this->previewService, $this->downloadService, @@ -35,4 +45,45 @@ class PreviewApiControllerTest extends PreviewControllerTest { ); } + /** + * @return array + */ + public function providesTestDownloadData() { + return [ + [1234, $this->mockSvgFile(1234), true, 'image/svg+xml'], + [4567, $this->mockSvgFile(4567), false, 'text/plain'] + ]; + } + + /** + * @dataProvider providesTestDownloadData + * + * @param int $fileId + * @param File $file + * @param string $nativeSvg + * @param string $expectedMimeType + * + * @internal param string $type + */ + public function testGetPreviewOfSvg($fileId, $file, $nativeSvg, $expectedMimeType) { + $width = 1024; + $height = 768; + + /** @type File $file */ + $preview = $this->mockGetData( + $fileId, $file, $width, $height, $keepAspect = true, $animatedPreview = true, + $base64Encode = false, $previewRequired = false + ); + $preview['name'] = $file->getName(); + + /** @type ImageResponse $response */ + $response = $this->controller->getPreview($fileId, $width, $height, $nativeSvg); + + $this->assertEquals(Http::STATUS_OK, $response->getStatus()); + + $this->assertEquals( + $expectedMimeType . '; charset=utf-8', $response->getHeaders()['Content-type'] + ); + } + } diff --git a/tests/unit/controller/PreviewControllerTest.php b/tests/unit/controller/PreviewControllerTest.php index e3b0caca..d8f9f44b 100644 --- a/tests/unit/controller/PreviewControllerTest.php +++ b/tests/unit/controller/PreviewControllerTest.php @@ -23,6 +23,7 @@ use OCP\AppFramework\Http\JSONResponse; use OCA\Gallery\AppInfo\Application; use OCA\Gallery\Http\ImageResponse; +use OCA\Gallery\Service\ConfigService; use OCA\Gallery\Service\ThumbnailService; use OCA\Gallery\Service\PreviewService; use OCA\Gallery\Service\DownloadService; @@ -47,6 +48,8 @@ class PreviewControllerTest extends \Test\GalleryUnitTest { protected $controller; /** @var IURLGenerator */ protected $urlGenerator; + /** @var ConfigService */ + protected $configService; /** @var ThumbnailService */ protected $thumbnailService; /** @var PreviewService */ @@ -75,6 +78,9 @@ class PreviewControllerTest extends \Test\GalleryUnitTest { $this->urlGenerator = $this->getMockBuilder('\OCP\IURLGenerator') ->disableOriginalConstructor() ->getMock(); + $this->configService = $this->getMockBuilder('\OCA\Gallery\Service\ConfigService') + ->disableOriginalConstructor() + ->getMock(); $this->thumbnailService = $this->getMockBuilder('\OCA\Gallery\Service\ThumbnailService') ->disableOriginalConstructor() ->getMock(); @@ -94,6 +100,7 @@ class PreviewControllerTest extends \Test\GalleryUnitTest { $this->appName, $this->request, $this->urlGenerator, + $this->configService, $this->thumbnailService, $this->previewService, $this->downloadService, @@ -110,6 +117,9 @@ class PreviewControllerTest extends \Test\GalleryUnitTest { $scale = 2.5; $thumbnailId = 1234; + $file = $this->mockJpgFile($thumbnailId); + $this->mockGetResourceFromId($this->previewService, $thumbnailId, $file); + $this->controller->getThumbnails($thumbnailId, $square, $scale); } @@ -147,10 +157,11 @@ class PreviewControllerTest extends \Test\GalleryUnitTest { $base64Encode ]; $this->mockGetThumbnailSpecs($square, $scale, $thumbnailSpecs); - - list($file, $mockedPreview) = + /** @type File $file */ + $file = $this->mockJpgFile($thumbnailId); + $mockedPreview = $this->mockGetData( - $thumbnailId, $width, $height, $aspect, $animatedPreview, $base64Encode + $thumbnailId, $file, $width, $height, $aspect, $animatedPreview, $base64Encode ); $this->mockPreviewValidator($square, $base64Encode, $mockedPreview['preview']); @@ -221,7 +232,8 @@ class PreviewControllerTest extends \Test\GalleryUnitTest { $height = 768; /** @type File $file */ - list($file, $preview) = $this->mockGetData($fileId, $width, $height); + $file = $this->mockJpgFile($fileId); + $preview = $this->mockGetData($fileId, $file, $width, $height); $preview['name'] = $file->getName(); /** @type ImageResponse $response */ @@ -272,26 +284,34 @@ class PreviewControllerTest extends \Test\GalleryUnitTest { * Mocks Preview->getData * * @param int $fileId the ID of the file of which we need a large preview of + * @param File $file * @param int $width * @param int $height * @param bool $keepAspect * @param bool $animatedPreview * @param bool $base64Encode + * @param bool $previewRequired * * @return array */ - private function mockGetData( - $fileId, $width, $height, $keepAspect = true, $animatedPreview = true, $base64Encode = false + protected function mockGetData( + $fileId, $file, $width, $height, $keepAspect = true, $animatedPreview = true, + $base64Encode = false, $previewRequired = true ) { - $file = $this->mockJpgFile($fileId); $this->mockGetResourceFromId($this->previewService, $fileId, $file); - $this->mockIsPreviewRequired($file, $animatedPreview, true); - $previewData = $this->mockPreviewData($file); + $this->mockIsPreviewRequired($file, $animatedPreview, $previewRequired); + $previewData = $this->mockPreviewData($file, $previewRequired); - $this->mockCreatePreview($file, $width, $height, $keepAspect, $base64Encode, $previewData); + if ($previewRequired) { + $this->mockCreatePreview( + $file, $width, $height, $keepAspect, $base64Encode, $previewData + ); + } else { + $this->mockDownloadFile($file, $base64Encode, $previewData); + } - return [$file, $previewData]; + return $previewData; } /** @@ -389,14 +409,17 @@ class PreviewControllerTest extends \Test\GalleryUnitTest { } /** - * @param object|\PHPUnit_Framework_MockObject_MockObject $file + * @param File $file + * @param bool $previewRequired * - * @return array<string,mixed> + * @return array <string,mixed> */ - private function mockPreviewData($file) { + private function mockPreviewData($file, $previewRequired) { + $mimeType = $previewRequired ? 'image/png' : $file->getMimeType(); + $preview = [ 'preview' => $file->getContent(), // Not a real preview, but it's not important - 'mimetype' => 'image/png', //Most previews are PNGs + 'mimetype' => $mimeType, ]; return $preview; @@ -463,6 +486,21 @@ class PreviewControllerTest extends \Test\GalleryUnitTest { } /** + * @param $file + * @param $base64Encode + * @param $preview + */ + private function mockDownloadFile($file, $base64Encode, $preview) { + $this->downloadService->expects($this->once()) + ->method('downloadFile') + ->with( + $this->equalTo($file), + $this->equalTo($base64Encode) + ) + ->willReturn($preview); + } + + /** * @param $event * @param $data * @param $message diff --git a/tests/unit/controller/PreviewPublicControllerTest.php b/tests/unit/controller/PreviewPublicControllerTest.php index 17dbd514..70a2ed3b 100644 --- a/tests/unit/controller/PreviewPublicControllerTest.php +++ b/tests/unit/controller/PreviewPublicControllerTest.php @@ -27,6 +27,7 @@ class PreviewPublicControllerTest extends PreviewControllerTest { $this->appName, $this->request, $this->urlGenerator, + $this->configService, $this->thumbnailService, $this->previewService, $this->downloadService, diff --git a/tests/unit/service/ConfigServiceTest.php b/tests/unit/service/ConfigServiceTest.php index 1f8d9850..db772413 100644 --- a/tests/unit/service/ConfigServiceTest.php +++ b/tests/unit/service/ConfigServiceTest.php @@ -15,6 +15,8 @@ namespace OCA\Gallery\Service; use OCA\Gallery\Config\ConfigParser; use OCA\Gallery\Config\ConfigException; +use OCA\Gallery\Preview\Preview; + /** * Class ConfigServiceTest * @@ -26,6 +28,8 @@ class ConfigServiceTest extends \Test\GalleryUnitTest { protected $service; /** @var ConfigParser */ protected $configParser; + /** @var Preview */ + protected $previewManager; /** * Test set up @@ -36,14 +40,193 @@ class ConfigServiceTest extends \Test\GalleryUnitTest { $this->configParser = $this->getMockBuilder('\OCA\Gallery\Config\ConfigParser') ->disableOriginalConstructor() ->getMock(); + $this->previewManager = $this->getMockBuilder('\OCA\Gallery\Preview\Preview') + ->disableOriginalConstructor() + ->getMock(); $this->service = new ConfigService ( $this->appName, $this->environment, $this->configParser, + $this->previewManager, $this->logger ); } + public function providesGetSupportedMediaTypesData() { + $baseMimeTypes = [ + 'image/jpeg', + ]; + + $slideshowMimes = array_merge( + $baseMimeTypes, + [ + 'application/font-sfnt', + 'application/x-font', + ] + ); + + $baseMimeTypesWithSvg = array_merge( + $baseMimeTypes, + [ + 'image/svg+xml', + ] + ); + + $slideshowMimesWithSvg = array_merge( + $slideshowMimes, + [ + 'image/svg+xml', + ] + ); + + return [ + [$baseMimeTypes, false, false, $baseMimeTypes], + [$baseMimeTypes, false, true, $baseMimeTypesWithSvg], + [$baseMimeTypes, true, true, $slideshowMimesWithSvg], + [$baseMimeTypes, true, false, $slideshowMimes], + ]; + } + + /** + * @dataProvider providesGetSupportedMediaTypesData + * + * @param $baseMimeTypes + * @param $extraMediaTypes + * @param $nativeSvgSupport + * @param $expectedResult + */ + public function testGetSupportedMediaTypes( + $baseMimeTypes, $extraMediaTypes, $nativeSvgSupport, $expectedResult + ) { + + $this->assertSame( + $baseMimeTypes, self::invokePrivate($this->service, 'baseMimeTypes', [$baseMimeTypes]) + ); + + $this->mockIsMimeSupported($nativeSvgSupport); + + $response = $this->service->getSupportedMediaTypes($extraMediaTypes, $nativeSvgSupport); + + $this->assertSame($expectedResult, $response); + } + + public function testGetSupportedMediaTypesWithBrokenPreviewSystem() { + // We only support 1 media type: GIF + self::invokePrivate($this->service, 'baseMimeTypes', [['image/gif']]); + + // Unfortunately, the GIF preview is broken + $this->mockIsMimeSupportedWithBrokenSystem('image/gif'); + + $response = $this->service->getSupportedMediaTypes(false, false); + + // 1-1 = 0 + $this->assertEmpty($response); + } + + public function providesValidateMimeTypeData() { + return [ + ['image/png'], + ['image/jpeg'], + ['image/gif'], + ['application/postscript'], + ['application/x-font'] + ]; + } + + /** + * @dataProvider providesValidateMimeTypeData + * + * @param $mimeType + * + */ + public function testValidateMimeType($mimeType) { + $supportedMimeTypes = [ + 'image/png', + 'image/jpeg', + 'image/gif', + 'application/postscript', + 'application/x-font' + ]; + + $this->assertSame( + $supportedMimeTypes, + self::invokePrivate($this->service, 'baseMimeTypes', [$supportedMimeTypes]) + ); + $this->mockIsMimeSupported($nativeSvgSupport = true); + + $this->service->validateMimeType($mimeType); + } + + public function providesValidateMimeTypeWithForbiddenMimeData() { + return [ + ['text/plain'], + ['application/javascript'], + ['application/json'], + ['text/markdown'], + ['application/yaml'], + ['application/xml'], + ]; + } + + /** + * @dataProvider providesValidateMimeTypeWithForbiddenMimeData + * + * @param $mimeType + * + * @expectedException \OCA\Gallery\Service\ForbiddenServiceException + */ + public function testValidateMimeTypeWithForbiddenMime($mimeType) { + $supportedMimeTypes = [ + 'image/png', + 'image/jpeg', + 'image/gif', + 'image/x-xbitmap', + 'image/bmp', + 'application/postscript', + 'application/x-font' + ]; + + $this->assertSame( + $supportedMimeTypes, + self::invokePrivate($this->service, 'baseMimeTypes', [$supportedMimeTypes]) + ); + $this->mockIsMimeSupported($nativeSvgSupport = true); + + $this->service->validateMimeType($mimeType); + } + + public function providesAddSvgSupportData() { + $supportedMimes = [ + 'image/png', + 'image/jpeg', + 'image/gif' + ]; + + $supportedMimesWithSvg = array_merge($supportedMimes, ['image/svg+xml']); + + return [ + [$supportedMimes, true, $supportedMimesWithSvg], + [$supportedMimes, false, $supportedMimes], + [$supportedMimesWithSvg, true, $supportedMimesWithSvg], + [$supportedMimesWithSvg, false, $supportedMimesWithSvg], + ]; + } + + /** + * @dataProvider providesAddSvgSupportData + * + * @param array $supportedMimes + * @param bool $nativeSvgSupport + * @param array $expectedResult + */ + public function testAddSvgSupport($supportedMimes, $nativeSvgSupport, $expectedResult) { + $response = self::invokePrivate( + $this->service, 'addSvgSupport', [$supportedMimes, $nativeSvgSupport] + ); + + $this->assertSame($expectedResult, $response); + } + public function testBuildFolderConfigWithBrokenSetup() { $nodeId = 65965; $files = []; @@ -116,6 +299,29 @@ class ConfigServiceTest extends \Test\GalleryUnitTest { $this->assertSame($modifiedAlbumConfig, $response); } + private function mockIsMimeSupported($mimeSupported) { + $map = [ + ['image/png', true], + ['image/jpeg', true], + ['application/postscript', true], + ['application/font-sfnt', true], + ['application/x-font', true], + ['image/svg+xml', $mimeSupported], + ['image/gif', $mimeSupported] + ]; + $this->previewManager->method('isMimeSupported') + ->will( + $this->returnValueMap($map) + ); + } + + private function mockIsMimeSupportedWithBrokenSystem($mimeType) { + $this->previewManager->expects($this->once()) + ->method('isMimeSupported') + ->with($mimeType) + ->willThrowException(new \Exception('Boom')); + } + private function mockGetFolderConfigWithBrokenSetup( $folder, $configName, $config, $configItems, $level, $exception ) { diff --git a/tests/unit/service/PreviewServiceTest.php b/tests/unit/service/PreviewServiceTest.php index c9c28152..fa1c1e31 100644 --- a/tests/unit/service/PreviewServiceTest.php +++ b/tests/unit/service/PreviewServiceTest.php @@ -48,64 +48,6 @@ class PreviewServiceTest extends \Test\GalleryUnitTest { ); } - public function providesGetSupportedMediaTypesData() { - $baseMimeTypes = [ - 'image/jpeg', - ]; - - $slideshowMimes = array_merge( - $baseMimeTypes, - [ - 'application/font-sfnt', - 'application/x-font', - ] - ); - - $baseMimeTypesWithSvg = array_merge( - $baseMimeTypes, - [ - 'image/svg+xml', - ] - ); - - $slideshowMimesWithSvg = array_merge( - $slideshowMimes, - [ - 'image/svg+xml', - ] - ); - - return [ - [$baseMimeTypes, false, false, $baseMimeTypes], - [$baseMimeTypes, false, true, $baseMimeTypesWithSvg], - [$baseMimeTypes, true, true, $slideshowMimesWithSvg], - [$baseMimeTypes, true, false, $slideshowMimes], - ]; - } - - /** - * @dataProvider providesGetSupportedMediaTypesData - * - * @param $baseMimeTypes - * @param $extraMediaTypes - * @param $nativeSvgSupport - * @param $expectedResult - */ - public function testGetSupportedMediaTypes( - $baseMimeTypes, $extraMediaTypes, $nativeSvgSupport, $expectedResult - ) { - - $this->assertSame( - $baseMimeTypes, self::invokePrivate($this->service, 'baseMimeTypes', [$baseMimeTypes]) - ); - - $this->mockIsMimeSupported($nativeSvgSupport); - - $response = $this->service->getSupportedMediaTypes($extraMediaTypes, $nativeSvgSupport); - - $this->assertSame($expectedResult, $response); - } - public function providesIsPreviewRequiredData() { return [ [true], @@ -238,40 +180,6 @@ class PreviewServiceTest extends \Test\GalleryUnitTest { $this->service->previewValidator($square, $base64Encode); } - - public function providesAddSvgSupportData() { - $supportedMimes = [ - 'image/png', - 'image/jpeg', - 'image/gif' - ]; - - $supportedMimesWithSvg = array_merge($supportedMimes, ['image/svg+xml']); - - return [ - [$supportedMimes, true, $supportedMimesWithSvg], - [$supportedMimes, false, $supportedMimes], - [$supportedMimesWithSvg, true, $supportedMimesWithSvg], - [$supportedMimesWithSvg, false, $supportedMimesWithSvg], - ]; - } - - /** - * @dataProvider providesAddSvgSupportData - * - * @param array $supportedMimes - * @param bool $nativeSvgSupport - * @param array $expectedResult - */ - public function testAddSvgSupport($supportedMimes, $nativeSvgSupport, $expectedResult) { - $response = self::invokePrivate( - $this->service, 'addSvgSupport', [$supportedMimes, $nativeSvgSupport] - ); - - $this->assertSame($expectedResult, $response); - } - - private function mockIsMimeSupported($mimeSupported) { $map = [ ['image/jpeg', true], |