diff options
Diffstat (limited to 'tests/lib/IntegrityCheck/CheckerTest.php')
-rw-r--r-- | tests/lib/IntegrityCheck/CheckerTest.php | 234 |
1 files changed, 95 insertions, 139 deletions
diff --git a/tests/lib/IntegrityCheck/CheckerTest.php b/tests/lib/IntegrityCheck/CheckerTest.php index 5858a01203f..a8a2596a3d8 100644 --- a/tests/lib/IntegrityCheck/CheckerTest.php +++ b/tests/lib/IntegrityCheck/CheckerTest.php @@ -1,4 +1,5 @@ <?php + /** * SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors * SPDX-FileCopyrightText: 2016 ownCloud, Inc. @@ -8,6 +9,7 @@ namespace Test\IntegrityCheck; use OC\Core\Command\Maintenance\Mimetype\GenerateMimetypeFileBuilder; +use OC\Files\Type\Detection; use OC\IntegrityCheck\Checker; use OC\IntegrityCheck\Helpers\AppLocator; use OC\IntegrityCheck\Helpers\EnvironmentHelper; @@ -54,7 +56,7 @@ class CheckerTest extends TestCase { $this->appConfig = $this->createMock(IAppConfig::class); $this->cacheFactory = $this->createMock(ICacheFactory::class); $this->appManager = $this->createMock(IAppManager::class); - $this->mimeTypeDetector = $this->createMock(\OC\Files\Type\Detection::class); + $this->mimeTypeDetector = $this->createMock(Detection::class); $this->config->method('getAppValue') ->willReturnArgument(2); @@ -111,7 +113,7 @@ class CheckerTest extends TestCase { $this->fileAccessHelper ->expects($this->once()) ->method('file_put_contents') - ->will($this->throwException(new \Exception('Exception message'))); + ->willThrowException(new \Exception('Exception message')); $keyBundle = file_get_contents(__DIR__ . '/../../data/integritycheck/SomeApp.crt'); $rsaPrivateKey = file_get_contents(__DIR__ . '/../../data/integritycheck/SomeApp.key'); @@ -200,14 +202,10 @@ class CheckerTest extends TestCase { $this->fileAccessHelper ->expects($this->exactly(2)) ->method('file_get_contents') - ->withConsecutive( - [\OC::$SERVERROOT . '/tests/data/integritycheck/app//appinfo/signature.json'], - ['/resources/codesigning/root.crt'], - ) - ->willReturnOnConsecutiveCalls( - $signatureDataFile, - file_get_contents(__DIR__ . '/../../data/integritycheck/root.crt') - ); + ->willReturnMap([ + [\OC::$SERVERROOT . '/tests/data/integritycheck/app//appinfo/signature.json', $signatureDataFile], + ['/resources/codesigning/root.crt', file_get_contents(__DIR__ . '/../../data/integritycheck/root.crt')], + ]); $this->assertSame([], $this->checker->verifyAppSignature('SomeApp')); } @@ -239,14 +237,10 @@ class CheckerTest extends TestCase { $this->fileAccessHelper ->expects($this->exactly(2)) ->method('file_get_contents') - ->withConsecutive( - [\OC::$SERVERROOT . '/tests/data/integritycheck/app//appinfo/signature.json'], - ['/resources/codesigning/root.crt'], - ) - ->willReturnOnConsecutiveCalls( - $signatureDataFile, - file_get_contents(__DIR__ . '/../../data/integritycheck/root.crt') - ); + ->willReturnMap([ + [\OC::$SERVERROOT . '/tests/data/integritycheck/app//appinfo/signature.json', $signatureDataFile], + ['/resources/codesigning/root.crt', file_get_contents(__DIR__ . '/../../data/integritycheck/root.crt')], + ]); $expected = [ 'EXCEPTION' => [ @@ -284,14 +278,10 @@ class CheckerTest extends TestCase { $this->fileAccessHelper ->expects($this->exactly(2)) ->method('file_get_contents') - ->withConsecutive( - [\OC::$SERVERROOT . '/tests/data/integritycheck/appWithInvalidData//appinfo/signature.json'], - ['/resources/codesigning/root.crt'], - ) - ->willReturnOnConsecutiveCalls( - $signatureDataFile, - file_get_contents(__DIR__ . '/../../data/integritycheck/root.crt') - ); + ->willReturnMap([ + [\OC::$SERVERROOT . '/tests/data/integritycheck/appWithInvalidData//appinfo/signature.json', $signatureDataFile], + ['/resources/codesigning/root.crt', file_get_contents(__DIR__ . '/../../data/integritycheck/root.crt')], + ]); $expected = [ @@ -344,15 +334,10 @@ class CheckerTest extends TestCase { $this->fileAccessHelper ->expects($this->exactly(2)) ->method('file_get_contents') - ->withConsecutive( - [\OC::$SERVERROOT . '/tests/data/integritycheck/appWithInvalidData//appinfo/signature.json'], - ['/resources/codesigning/root.crt'], - ) - ->willReturnOnConsecutiveCalls( - $signatureDataFile, - file_get_contents(__DIR__ . '/../../data/integritycheck/root.crt') - ); - + ->willReturnMap([ + [\OC::$SERVERROOT . '/tests/data/integritycheck/appWithInvalidData//appinfo/signature.json', $signatureDataFile], + ['/resources/codesigning/root.crt', file_get_contents(__DIR__ . '/../../data/integritycheck/root.crt')], + ]); $expected = [ 'INVALID_HASH' => [ @@ -405,13 +390,10 @@ class CheckerTest extends TestCase { $this->fileAccessHelper ->expects($this->exactly(2)) ->method('file_get_contents') - ->withConsecutive( - [\OC::$SERVERROOT . '/tests/data/integritycheck/appWithInvalidData//appinfo/signature.json'], - ['/resources/codesigning/root.crt'], - )->willReturnOnConsecutiveCalls( - $signatureDataFile, - file_get_contents(__DIR__ . '/../../data/integritycheck/root.crt') - ); + ->willReturnMap([ + [\OC::$SERVERROOT . '/tests/data/integritycheck/appWithInvalidData//appinfo/signature.json', $signatureDataFile], + ['/resources/codesigning/root.crt', file_get_contents(__DIR__ . '/../../data/integritycheck/root.crt')], + ]); $expected = [ 'EXCEPTION' => [ @@ -449,13 +431,10 @@ class CheckerTest extends TestCase { $this->fileAccessHelper ->expects($this->exactly(2)) ->method('file_get_contents') - ->withConsecutive( - [\OC::$SERVERROOT . '/tests/data/integritycheck/app//appinfo/signature.json'], - ['/resources/codesigning/root.crt'], - )->willReturnOnConsecutiveCalls( - $signatureDataFile, - file_get_contents(__DIR__ . '/../../data/integritycheck/root.crt') - ); + ->willReturnMap([ + [\OC::$SERVERROOT . '/tests/data/integritycheck/app//appinfo/signature.json', $signatureDataFile], + ['/resources/codesigning/root.crt', file_get_contents(__DIR__ . '/../../data/integritycheck/root.crt')], + ]); $this->assertSame([], $this->checker->verifyAppSignature('SomeApp')); } @@ -468,7 +447,7 @@ class CheckerTest extends TestCase { $this->fileAccessHelper ->expects($this->once()) ->method('assertDirectoryExists') - ->will($this->throwException(new \Exception('Exception message'))); + ->willThrowException(new \Exception('Exception message')); $this->fileAccessHelper ->expects($this->once()) ->method('is_writable') @@ -492,7 +471,7 @@ class CheckerTest extends TestCase { $this->fileAccessHelper ->expects($this->once()) ->method('assertDirectoryExists') - ->will($this->throwException(new \Exception('Exception message'))); + ->willThrowException(new \Exception('Exception message')); $this->fileAccessHelper ->expects($this->once()) ->method('is_writable') @@ -689,13 +668,10 @@ class CheckerTest extends TestCase { $this->fileAccessHelper ->expects($this->exactly(2)) ->method('file_get_contents') - ->withConsecutive( - [\OC::$SERVERROOT . '/tests/data/integritycheck/app//core/signature.json'], - [\OC::$SERVERROOT . '/tests/data/integritycheck/app//resources/codesigning/root.crt'], - )->willReturnOnConsecutiveCalls( - $signatureDataFile, - file_get_contents(__DIR__ . '/../../data/integritycheck/root.crt') - ); + ->willReturnMap([ + [\OC::$SERVERROOT . '/tests/data/integritycheck/app//core/signature.json', $signatureDataFile], + [\OC::$SERVERROOT . '/tests/data/integritycheck/app//resources/codesigning/root.crt', file_get_contents(__DIR__ . '/../../data/integritycheck/root.crt')], + ]); $this->assertSame([], $this->checker->verifyCoreSignature()); } @@ -726,14 +702,10 @@ class CheckerTest extends TestCase { $this->fileAccessHelper ->expects($this->exactly(2)) ->method('file_get_contents') - ->withConsecutive( - [\OC::$SERVERROOT . '/tests/data/integritycheck/htaccessWithValidModifiedContent/core/signature.json'], - [\OC::$SERVERROOT . '/tests/data/integritycheck/htaccessWithValidModifiedContent/resources/codesigning/root.crt'], - ) - ->willReturnOnConsecutiveCalls( - $signatureDataFile, - file_get_contents(__DIR__ . '/../../data/integritycheck/root.crt') - ); + ->willReturnMap([ + [\OC::$SERVERROOT . '/tests/data/integritycheck/htaccessWithValidModifiedContent/core/signature.json', $signatureDataFile], + [\OC::$SERVERROOT . '/tests/data/integritycheck/htaccessWithValidModifiedContent/resources/codesigning/root.crt', file_get_contents(__DIR__ . '/../../data/integritycheck/root.crt')], + ]); $this->assertSame([], $this->checker->verifyCoreSignature()); } @@ -743,7 +715,9 @@ class CheckerTest extends TestCase { */ public function testVerifyCoreSignatureWithModifiedMimetypelistSignatureData(): void { $shippedMimetypeAliases = (array)json_decode(file_get_contents(\OC::$SERVERROOT . '/resources/config/mimetypealiases.dist.json')); + $shippedMimetypeNames = (array)json_decode(file_get_contents(\OC::$SERVERROOT . '/resources/config/mimetypenames.dist.json')); $allAliases = array_merge($shippedMimetypeAliases, ['my-custom/mimetype' => 'custom']); + $allMimetypeNames = array_merge($shippedMimetypeNames, ['my-custom/mimetype' => 'Custom Document']); $this->mimeTypeDetector ->method('getOnlyDefaultAliases') @@ -753,9 +727,14 @@ class CheckerTest extends TestCase { ->method('getAllAliases') ->willReturn($allAliases); + $this->mimeTypeDetector + ->method('getAllNamings') + ->willReturn($allMimetypeNames); + $oldMimetypeList = new GenerateMimetypeFileBuilder(); $all = $this->mimeTypeDetector->getAllAliases(); - $newFile = $oldMimetypeList->generateFile($all); + $namings = $this->mimeTypeDetector->getAllNamings(); + $newFile = $oldMimetypeList->generateFile($all, $namings); // When updating the mimetype list the test assets need to be updated as well // 1. Update core/js/mimetypelist.js with the new generated js by running the test with the next line uncommented: @@ -816,13 +795,10 @@ class CheckerTest extends TestCase { $this->fileAccessHelper ->expects($this->exactly(2)) ->method('file_get_contents') - ->withConsecutive( - [\OC::$SERVERROOT . '/tests/data/integritycheck/app//core/signature.json'], - [\OC::$SERVERROOT . '/tests/data/integritycheck/app//resources/codesigning/root.crt'], - )->willReturnOnConsecutiveCalls( - $signatureDataFile, - file_get_contents(__DIR__ . '/../../data/integritycheck/root.crt') - ); + ->willReturnMap([ + [\OC::$SERVERROOT . '/tests/data/integritycheck/app//core/signature.json', $signatureDataFile], + [\OC::$SERVERROOT . '/tests/data/integritycheck/app//resources/codesigning/root.crt', file_get_contents(__DIR__ . '/../../data/integritycheck/root.crt')], + ]); $this->assertSame([], $this->checker->verifyCoreSignature()); } @@ -853,13 +829,10 @@ class CheckerTest extends TestCase { $this->fileAccessHelper ->expects($this->exactly(2)) ->method('file_get_contents') - ->withConsecutive( - [\OC::$SERVERROOT . '/tests/data/integritycheck/appWithInvalidData//core/signature.json'], - [\OC::$SERVERROOT . '/tests/data/integritycheck/appWithInvalidData//resources/codesigning/root.crt'], - )->willReturnOnConsecutiveCalls( - $signatureDataFile, - file_get_contents(__DIR__ . '/../../data/integritycheck/root.crt') - ); + ->willReturnMap([ + [\OC::$SERVERROOT . '/tests/data/integritycheck/appWithInvalidData//core/signature.json', $signatureDataFile], + [\OC::$SERVERROOT . '/tests/data/integritycheck/appWithInvalidData//resources/codesigning/root.crt', file_get_contents(__DIR__ . '/../../data/integritycheck/root.crt')], + ]); $expected = [ 'EXCEPTION' => [ @@ -896,13 +869,10 @@ class CheckerTest extends TestCase { $this->fileAccessHelper ->expects($this->exactly(2)) ->method('file_get_contents') - ->withConsecutive( - [\OC::$SERVERROOT . '/tests/data/integritycheck/appWithInvalidData//core/signature.json'], - [\OC::$SERVERROOT . '/tests/data/integritycheck/appWithInvalidData//resources/codesigning/root.crt'], - )->willReturnOnConsecutiveCalls( - $signatureDataFile, - file_get_contents(__DIR__ . '/../../data/integritycheck/root.crt') - ); + ->willReturnMap([ + [\OC::$SERVERROOT . '/tests/data/integritycheck/appWithInvalidData//core/signature.json', $signatureDataFile], + [\OC::$SERVERROOT . '/tests/data/integritycheck/appWithInvalidData//resources/codesigning/root.crt', file_get_contents(__DIR__ . '/../../data/integritycheck/root.crt')], + ]); $expected = [ 'INVALID_HASH' => [ @@ -954,13 +924,10 @@ class CheckerTest extends TestCase { $this->fileAccessHelper ->expects($this->exactly(2)) ->method('file_get_contents') - ->withConsecutive( - [\OC::$SERVERROOT . '/tests/data/integritycheck/app//core/signature.json'], - [\OC::$SERVERROOT . '/tests/data/integritycheck/app//resources/codesigning/root.crt'], - )->willReturnOnConsecutiveCalls( - $signatureDataFile, - file_get_contents(__DIR__ . '/../../data/integritycheck/root.crt') - ); + ->willReturnMap([ + [\OC::$SERVERROOT . '/tests/data/integritycheck/app//core/signature.json', $signatureDataFile], + [\OC::$SERVERROOT . '/tests/data/integritycheck/app//resources/codesigning/root.crt', file_get_contents(__DIR__ . '/../../data/integritycheck/root.crt')], + ]); $expected = [ 'EXCEPTION' => [ @@ -997,13 +964,10 @@ class CheckerTest extends TestCase { $this->fileAccessHelper ->expects($this->exactly(2)) ->method('file_get_contents') - ->withConsecutive( - [\OC::$SERVERROOT . '/tests/data/integritycheck/app//core/signature.json'], - [\OC::$SERVERROOT . '/tests/data/integritycheck/app//resources/codesigning/root.crt'], - )->willReturnOnConsecutiveCalls( - $signatureDataFile, - file_get_contents(__DIR__ . '/../../data/integritycheck/root.crt') - ); + ->willReturnMap([ + [\OC::$SERVERROOT . '/tests/data/integritycheck/app//core/signature.json', $signatureDataFile], + [\OC::$SERVERROOT . '/tests/data/integritycheck/app//resources/codesigning/root.crt', file_get_contents(__DIR__ . '/../../data/integritycheck/root.crt')], + ]); $expected = [ 'EXCEPTION' => [ @@ -1027,7 +991,7 @@ class CheckerTest extends TestCase { $this->appManager, $this->mimeTypeDetector, ]) - ->setMethods([ + ->onlyMethods([ 'verifyCoreSignature', 'verifyAppSignature', ]) @@ -1048,45 +1012,40 @@ class CheckerTest extends TestCase { $this->appManager ->expects($this->exactly(4)) ->method('isShipped') - ->withConsecutive( - ['files'], - ['calendar'], - ['contacts'], - ['dav'], - )->willReturnOnConsecutiveCalls( - true, - false, - false, - true, - ); + ->willReturnMap([ + ['files', true], + ['calendar', false], + ['contacts', false], + ['dav', true], + ]); + + $calls = [ + 'files', + 'calendar', + 'dav', + ]; $this->checker ->expects($this->exactly(3)) ->method('verifyAppSignature') - ->withConsecutive( - ['files'], - ['calendar'], - ['dav'], - ); + ->willReturnCallback(function ($app) use (&$calls) { + $expected = array_shift($calls); + $this->assertSame($expected, $app); + return []; + }); $this->appLocator ->expects($this->exactly(2)) ->method('getAppPath') - ->withConsecutive( - ['calendar'], - ['contacts'], - )->willReturnOnConsecutiveCalls( - '/apps/calendar', - '/apps/contacts', - ); + ->willReturnMap([ + ['calendar', '/apps/calendar'], + ['contacts', '/apps/contacts'], + ]); $this->fileAccessHelper ->expects($this->exactly(2)) ->method('file_exists') - ->withConsecutive( - ['/apps/calendar/appinfo/signature.json'], - ['/apps/contacts/appinfo/signature.json'], - )->willReturnOnConsecutiveCalls( - true, - false, - ); + ->willReturnMap([ + ['/apps/calendar/appinfo/signature.json', true], + ['/apps/contacts/appinfo/signature.json', false], + ]); $this->appConfig ->expects($this->once()) ->method('deleteKey') @@ -1110,10 +1069,7 @@ class CheckerTest extends TestCase { $this->assertSame($expected, $this->checker->verifyAppSignature('SomeApp')); } - /** - * @return array - */ - public function channelDataProvider() { + public static function channelDataProvider(): array { return [ ['stable', true], ['git', false], @@ -1123,8 +1079,8 @@ class CheckerTest extends TestCase { /** * @param string $channel * @param bool $isCodeSigningEnforced - * @dataProvider channelDataProvider */ + #[\PHPUnit\Framework\Attributes\DataProvider('channelDataProvider')] public function testIsCodeCheckEnforced($channel, $isCodeSigningEnforced): void { $this->serverVersion ->expects($this->once()) @@ -1141,8 +1097,8 @@ class CheckerTest extends TestCase { /** * @param string $channel - * @dataProvider channelDataProvider */ + #[\PHPUnit\Framework\Attributes\DataProvider('channelDataProvider')] public function testIsCodeCheckEnforcedWithDisabledConfigSwitch($channel): void { $this->serverVersion ->expects($this->once()) |