aboutsummaryrefslogtreecommitdiffstats
path: root/apps/encryption/tests/Crypto/CryptTest.php
diff options
context:
space:
mode:
Diffstat (limited to 'apps/encryption/tests/Crypto/CryptTest.php')
-rw-r--r--apps/encryption/tests/Crypto/CryptTest.php163
1 files changed, 66 insertions, 97 deletions
diff --git a/apps/encryption/tests/Crypto/CryptTest.php b/apps/encryption/tests/Crypto/CryptTest.php
index 738dfe0a2a0..1355e2c855d 100644
--- a/apps/encryption/tests/Crypto/CryptTest.php
+++ b/apps/encryption/tests/Crypto/CryptTest.php
@@ -1,5 +1,7 @@
<?php
+declare(strict_types=1);
+
/**
* SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors
* SPDX-FileCopyrightText: 2016 ownCloud, Inc.
@@ -8,27 +10,21 @@
namespace OCA\Encryption\Tests\Crypto;
use OCA\Encryption\Crypto\Crypt;
+use OCP\Encryption\Exceptions\GenericEncryptionException;
use OCP\IConfig;
use OCP\IL10N;
use OCP\IUserSession;
+use PHPUnit\Framework\MockObject\MockObject;
use Psr\Log\LoggerInterface;
use Test\TestCase;
class CryptTest extends TestCase {
- /** @var LoggerInterface|\PHPUnit\Framework\MockObject\MockObject */
- private $logger;
-
- /** @var \OCP\IUserSession|\PHPUnit\Framework\MockObject\MockObject */
- private $userSession;
-
- /** @var \OCP\IConfig|\PHPUnit\Framework\MockObject\MockObject */
- private $config;
-
- /** @var \OCP\IL10N|\PHPUnit\Framework\MockObject\MockObject */
- private $l;
+ protected LoggerInterface&MockObject $logger;
+ protected IUserSession&MockObject $userSession;
+ protected IConfig&MockObject $config;
+ protected IL10N&MockObject $l;
- /** @var Crypt */
- private $crypt;
+ protected Crypt $crypt;
protected function setUp(): void {
parent::setUp();
@@ -37,8 +33,7 @@ class CryptTest extends TestCase {
->disableOriginalConstructor()
->getMock();
$this->logger->expects($this->any())
- ->method('warning')
- ->willReturn(true);
+ ->method('warning');
$this->userSession = $this->getMockBuilder(IUserSession::class)
->disableOriginalConstructor()
->getMock();
@@ -53,7 +48,7 @@ class CryptTest extends TestCase {
/**
* test getOpenSSLConfig without any additional parameters
*/
- public function testGetOpenSSLConfigBasic() {
+ public function testGetOpenSSLConfigBasic(): void {
$this->config->expects($this->once())
->method('getSystemValue')
->with($this->equalTo('openssl'), $this->equalTo([]))
@@ -68,7 +63,7 @@ class CryptTest extends TestCase {
/**
* test getOpenSSLConfig with additional parameters defined in config.php
*/
- public function testGetOpenSSLConfig() {
+ public function testGetOpenSSLConfig(): void {
$this->config->expects($this->once())
->method('getSystemValue')
->with($this->equalTo('openssl'), $this->equalTo([]))
@@ -85,10 +80,9 @@ class CryptTest extends TestCase {
/**
* test generateHeader with valid key formats
- *
- * @dataProvider dataTestGenerateHeader
*/
- public function testGenerateHeader($keyFormat, $expected) {
+ #[\PHPUnit\Framework\Attributes\DataProvider('dataTestGenerateHeader')]
+ public function testGenerateHeader($keyFormat, $expected): void {
$this->config->expects($this->once())
->method('getSystemValueString')
->with($this->equalTo('cipher'), $this->equalTo('AES-256-CTR'))
@@ -107,16 +101,13 @@ class CryptTest extends TestCase {
* test generateHeader with invalid key format
*
*/
- public function testGenerateHeaderInvalid() {
+ public function testGenerateHeaderInvalid(): void {
$this->expectException(\InvalidArgumentException::class);
$this->crypt->generateHeader('unknown');
}
- /**
- * @return array
- */
- public function dataTestGenerateHeader() {
+ public static function dataTestGenerateHeader(): array {
return [
[null, 'HBEGIN:cipher:AES-128-CFB:keyFormat:hash2:encoding:binary:HEND'],
['password', 'HBEGIN:cipher:AES-128-CFB:keyFormat:password:encoding:binary:HEND'],
@@ -124,11 +115,11 @@ class CryptTest extends TestCase {
];
}
- public function testGetCipherWithInvalidCipher() {
+ public function testGetCipherWithInvalidCipher(): void {
$this->config->expects($this->once())
- ->method('getSystemValueString')
- ->with($this->equalTo('cipher'), $this->equalTo('AES-256-CTR'))
- ->willReturn('Not-Existing-Cipher');
+ ->method('getSystemValueString')
+ ->with($this->equalTo('cipher'), $this->equalTo('AES-256-CTR'))
+ ->willReturn('Not-Existing-Cipher');
$this->logger
->expects($this->once())
->method('warning')
@@ -138,11 +129,11 @@ class CryptTest extends TestCase {
}
/**
- * @dataProvider dataProviderGetCipher
* @param string $configValue
* @param string $expected
*/
- public function testGetCipher($configValue, $expected) {
+ #[\PHPUnit\Framework\Attributes\DataProvider('dataProviderGetCipher')]
+ public function testGetCipher($configValue, $expected): void {
$this->config->expects($this->once())
->method('getSystemValueString')
->with($this->equalTo('cipher'), $this->equalTo('AES-256-CTR'))
@@ -155,10 +146,8 @@ class CryptTest extends TestCase {
/**
* data provider for testGetCipher
- *
- * @return array
*/
- public function dataProviderGetCipher() {
+ public static function dataProviderGetCipher(): array {
return [
['AES-128-CFB', 'AES-128-CFB'],
['AES-256-CFB', 'AES-256-CFB'],
@@ -172,7 +161,7 @@ class CryptTest extends TestCase {
/**
* test concatIV()
*/
- public function testConcatIV() {
+ public function testConcatIV(): void {
$result = self::invokePrivate(
$this->crypt,
'concatIV',
@@ -183,10 +172,8 @@ class CryptTest extends TestCase {
);
}
- /**
- * @dataProvider dataTestSplitMetaData
- */
- public function testSplitMetaData($data, $expected) {
+ #[\PHPUnit\Framework\Attributes\DataProvider('dataTestSplitMetaData')]
+ public function testSplitMetaData($data, $expected): void {
$this->config->method('getSystemValueBool')
->with('encryption_skip_signature_check', false)
->willReturn(true);
@@ -201,7 +188,7 @@ class CryptTest extends TestCase {
$this->assertSame($expected['signature'], $result['signature']);
}
- public function dataTestSplitMetaData() {
+ public static function dataTestSplitMetaData(): array {
return [
['encryptedContent00iv001234567890123456xx',
['encrypted' => 'encryptedContent', 'iv' => '1234567890123456', 'signature' => false]],
@@ -210,10 +197,8 @@ class CryptTest extends TestCase {
];
}
- /**
- * @dataProvider dataTestHasSignature
- */
- public function testHasSignature($data, $expected) {
+ #[\PHPUnit\Framework\Attributes\DataProvider('dataTestHasSignature')]
+ public function testHasSignature($data, $expected): void {
$this->config->method('getSystemValueBool')
->with('encryption_skip_signature_check', false)
->willReturn(true);
@@ -222,24 +207,22 @@ class CryptTest extends TestCase {
);
}
- public function dataTestHasSignature() {
+ public static function dataTestHasSignature(): array {
return [
['encryptedContent00iv001234567890123456xx', false],
['encryptedContent00iv00123456789012345600sig00e1992521e437f6915f9173b190a512cfc38a00ac24502db44e0ba10c2bb0cc86xxx', true]
];
}
- /**
- * @dataProvider dataTestHasSignatureFail
- */
- public function testHasSignatureFail($cipher) {
- $this->expectException(\OCP\Encryption\Exceptions\GenericEncryptionException::class);
+ #[\PHPUnit\Framework\Attributes\DataProvider('dataTestHasSignatureFail')]
+ public function testHasSignatureFail($cipher): void {
+ $this->expectException(GenericEncryptionException::class);
$data = 'encryptedContent00iv001234567890123456xx';
$this->invokePrivate($this->crypt, 'hasSignature', [$data, $cipher]);
}
- public function dataTestHasSignatureFail() {
+ public static function dataTestHasSignatureFail(): array {
return [
['AES-256-CTR'],
['aes-256-ctr'],
@@ -251,7 +234,7 @@ class CryptTest extends TestCase {
/**
* test addPadding()
*/
- public function testAddPadding() {
+ public function testAddPadding(): void {
$result = self::invokePrivate($this->crypt, 'addPadding', ['data']);
$this->assertSame('dataxxx', $result);
}
@@ -259,21 +242,19 @@ class CryptTest extends TestCase {
/**
* test removePadding()
*
- * @dataProvider dataProviderRemovePadding
* @param $data
* @param $expected
*/
- public function testRemovePadding($data, $expected) {
+ #[\PHPUnit\Framework\Attributes\DataProvider('dataProviderRemovePadding')]
+ public function testRemovePadding($data, $expected): void {
$result = self::invokePrivate($this->crypt, 'removePadding', [$data]);
$this->assertSame($expected, $result);
}
/**
* data provider for testRemovePadding
- *
- * @return array
*/
- public function dataProviderRemovePadding() {
+ public static function dataProviderRemovePadding(): array {
return [
['dataxx', 'data'],
['data', false]
@@ -283,7 +264,7 @@ class CryptTest extends TestCase {
/**
* test parseHeader()
*/
- public function testParseHeader() {
+ public function testParseHeader(): void {
$header = 'HBEGIN:foo:bar:cipher:AES-256-CFB:encoding:binary:HEND';
$result = self::invokePrivate($this->crypt, 'parseHeader', [$header]);
@@ -328,7 +309,7 @@ class CryptTest extends TestCase {
*
* @depends testEncrypt
*/
- public function testDecrypt($data) {
+ public function testDecrypt($data): void {
$result = self::invokePrivate(
$this->crypt,
'decrypt',
@@ -339,10 +320,9 @@ class CryptTest extends TestCase {
/**
* test return values of valid ciphers
- *
- * @dataProvider dataTestGetKeySize
*/
- public function testGetKeySize($cipher, $expected) {
+ #[\PHPUnit\Framework\Attributes\DataProvider('dataTestGetKeySize')]
+ public function testGetKeySize($cipher, $expected): void {
$result = $this->invokePrivate($this->crypt, 'getKeySize', [$cipher]);
$this->assertSame($expected, $result);
}
@@ -351,16 +331,13 @@ class CryptTest extends TestCase {
* test exception if cipher is unknown
*
*/
- public function testGetKeySizeFailure() {
+ public function testGetKeySizeFailure(): void {
$this->expectException(\InvalidArgumentException::class);
$this->invokePrivate($this->crypt, 'getKeySize', ['foo']);
}
- /**
- * @return array
- */
- public function dataTestGetKeySize() {
+ public static function dataTestGetKeySize(): array {
return [
['AES-256-CFB', 32],
['AES-128-CFB', 16],
@@ -369,33 +346,28 @@ class CryptTest extends TestCase {
];
}
- /**
- * @dataProvider dataTestDecryptPrivateKey
- */
- public function testDecryptPrivateKey($header, $privateKey, $expectedCipher, $isValidKey, $expected) {
+ #[\PHPUnit\Framework\Attributes\DataProvider('dataTestDecryptPrivateKey')]
+ public function testDecryptPrivateKey($header, $privateKey, $expectedCipher, $isValidKey, $expected): void {
$this->config->method('getSystemValueBool')
- ->withConsecutive(['encryption.legacy_format_support', false],
- ['encryption.use_legacy_base64_encoding', false])
- ->willReturnOnConsecutiveCalls(true, false);
+ ->willReturnMap([
+ ['encryption.legacy_format_support', false, true],
+ ['encryption.use_legacy_base64_encoding', false, false],
+ ]);
- /** @var \OCA\Encryption\Crypto\Crypt | \PHPUnit\Framework\MockObject\MockObject $crypt */
+ /** @var Crypt|\PHPUnit\Framework\MockObject\MockObject $crypt */
$crypt = $this->getMockBuilder(Crypt::class)
- ->setConstructorArgs(
- [
- $this->logger,
- $this->userSession,
- $this->config,
- $this->l
- ]
- )
- ->setMethods(
- [
- 'parseHeader',
- 'generatePasswordHash',
- 'symmetricDecryptFileContent',
- 'isValidPrivateKey'
- ]
- )
+ ->setConstructorArgs([
+ $this->logger,
+ $this->userSession,
+ $this->config,
+ $this->l
+ ])
+ ->onlyMethods([
+ 'parseHeader',
+ 'generatePasswordHash',
+ 'symmetricDecryptFileContent',
+ 'isValidPrivateKey'
+ ])
->getMock();
$crypt->expects($this->once())->method('parseHeader')->willReturn($header);
@@ -416,10 +388,7 @@ class CryptTest extends TestCase {
$this->assertSame($expected, $result);
}
- /**
- * @return array
- */
- public function dataTestDecryptPrivateKey() {
+ public static function dataTestDecryptPrivateKey(): array {
return [
[['cipher' => 'AES-128-CFB', 'keyFormat' => 'password'], 'HBEGIN:HENDprivateKey', 'AES-128-CFB', true, 'key'],
[['cipher' => 'AES-256-CFB', 'keyFormat' => 'password'], 'HBEGIN:HENDprivateKey', 'AES-256-CFB', true, 'key'],
@@ -430,7 +399,7 @@ class CryptTest extends TestCase {
];
}
- public function testIsValidPrivateKey() {
+ public function testIsValidPrivateKey(): void {
$res = openssl_pkey_new();
openssl_pkey_export($res, $privateKey);
@@ -445,7 +414,7 @@ class CryptTest extends TestCase {
);
}
- public function testMultiKeyEncrypt() {
+ public function testMultiKeyEncrypt(): void {
$res = openssl_pkey_new();
openssl_pkey_export($res, $privateKey);
$publicKeyPem = openssl_pkey_get_details($res)['key'];