Merge branch 'improve-tests' into add-static-code-analysis

This commit is contained in:
Art4 2024-11-10 00:20:29 +00:00
commit 221dad7663
7 changed files with 57 additions and 84 deletions

View file

@ -157,6 +157,7 @@
},
"scripts": {
"test": "phpunit",
"test:unit": "phpunit -c tests/phpunit.xml --testsuite unit",
"phpstan": "phpstan analyze --memory-limit 1024M --configuration .phpstan.neon",
"lint": "find . -name \\*.php -not -path './vendor/*' -not -path './view/asset/*' -print0 | xargs -0 -n1 php -l",
"docker:translate": "docker run --rm -v $PWD:/data -w /data friendicaci/transifex bin/run_xgettext.sh",

View file

@ -306,13 +306,6 @@ class Crypto
*/
public static function randomDigits($digits)
{
$rn = '';
// generating cryptographically secure pseudo-random integers
for ($i = 0; $i < $digits; $i++) {
$rn .= random_int(0, 9);
}
return (int) $rn;
return random_int(0, 10 ** $digits - 1);
}
}

View file

@ -0,0 +1,45 @@
<?php
// Copyright (C) 2010-2024, the Friendica project
// SPDX-FileCopyrightText: 2010-2024 the Friendica project
//
// SPDX-License-Identifier: AGPL-3.0-or-later
namespace Friendica\Test\Unit\Util;
use Friendica\Util\Crypto;
use phpmock\phpunit\PHPMock;
use PHPUnit\Framework\TestCase;
class CryptoTest extends TestCase
{
use PHPMock;
public function testRandomDigitsRandomInt()
{
$random_int = $this->getFunctionMock('Friendica\Util', 'random_int');
$random_int->expects($this->any())->willReturnCallback(function ($min, $max) {
return 12345678;
});
self::assertSame(12345678, Crypto::randomDigits(8));
}
public function testDiasporaPubRsaToMe()
{
$key = 'LS0tLS1CRUdJTiBSU0EgUFVCTElDIEtFWS0tLS0tDQpNSUdKQW9HQkFORjVLTmJzN2k3aTByNVFZckNpRExEZ09pU1BWbmgvdlFnMXpnSk9VZVRheWVETk5yZTR6T1RVDQpSVDcyZGlLQ294OGpYOE5paElJTFJtcUtTOWxVYVNzd21QcVNFenVpdE5xeEhnQy8xS2ZuaXM1Qm96NnRwUUxjDQpsZDMwQjJSMWZIVWdFTHZWd0JkV29pRDhSRUt1dFNuRVBGd1RwVmV6aVlWYWtNY25pclRWQWdNQkFBRT0NCi0tLS0tRU5EIFJTQSBQVUJMSUMgS0VZLS0tLS0';
// TODO PHPUnit 10: Replace with assertStringEqualsStringIgnoringLineEndings()
self::assertSame(
str_replace("\n", "\r\n", <<< TXT
-----BEGIN PUBLIC KEY-----
MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDReSjW7O4u4tK+UGKwogyw4Dok
j1Z4f70INc4CTlHk2sngzTa3uMzk1EU+9nYigqMfI1/DYoSCC0ZqikvZVGkrMJj6
khM7orTasR4Av9Sn54rOQaM+raUC3JXd9AdkdXx1IBC71cAXVqIg/ERCrrUpxDxc
E6VXs4mFWpDHJ4q01QIDAQAB
-----END PUBLIC KEY-----
TXT),
Crypto::rsaToPem(base64_decode($key))
);
}
}

View file

@ -1,6 +0,0 @@
-----BEGIN PUBLIC KEY-----
MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDReSjW7O4u4tK+UGKwogyw4Dok
j1Z4f70INc4CTlHk2sngzTa3uMzk1EU+9nYigqMfI1/DYoSCC0ZqikvZVGkrMJj6
khM7orTasR4Av9Sn54rOQaM+raUC3JXd9AdkdXx1IBC71cAXVqIg/ERCrrUpxDxc
E6VXs4mFWpDHJ4q01QIDAQAB
-----END PUBLIC KEY-----

View file

@ -1 +0,0 @@
LS0tLS1CRUdJTiBSU0EgUFVCTElDIEtFWS0tLS0tDQpNSUdKQW9HQkFORjVLTmJzN2k3aTByNVFZckNpRExEZ09pU1BWbmgvdlFnMXpnSk9VZVRheWVETk5yZTR6T1RVDQpSVDcyZGlLQ294OGpYOE5paElJTFJtcUtTOWxVYVNzd21QcVNFenVpdE5xeEhnQy8xS2ZuaXM1Qm96NnRwUUxjDQpsZDMwQjJSMWZIVWdFTHZWd0JkV29pRDhSRUt1dFNuRVBGd1RwVmV6aVlWYWtNY25pclRWQWdNQkFBRT0NCi0tLS0tRU5EIFJTQSBQVUJMSUMgS0VZLS0tLS0

View file

@ -7,10 +7,16 @@
timeoutForMediumTests="900"
timeoutForLargeTests="900"
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.3/phpunit.xsd">
<testsuites>
<testsuite name="friendica">
<directory suffix=".php">functional/</directory>
<directory suffix=".php">src/</directory>
<directory suffix=".php">Unit/</directory>
</testsuite>
<testsuite name="unit">
<directory suffix=".php">Unit/</directory>
</testsuite>
</testsuites>
<!-- Filters for Code Coverage -->
<coverage>
<include>

View file

@ -1,65 +0,0 @@
<?php
// Copyright (C) 2010-2024, the Friendica project
// SPDX-FileCopyrightText: 2010-2024 the Friendica project
//
// SPDX-License-Identifier: AGPL-3.0-or-later
namespace Friendica\Test\src\Util;
use Friendica\Util\Crypto;
use phpmock\phpunit\PHPMock;
use PHPUnit\Framework\TestCase;
class CryptoTest extends TestCase
{
use PHPMock;
public static function tearDownAfterClass(): void
{
// Reset mocking
global $phpMock;
$phpMock = [];
parent::tearDownAfterClass();
}
public function testRandomDigitsRandomInt()
{
$random_int = $this->getFunctionMock('Friendica\Util', 'random_int');
$random_int->expects($this->any())->willReturnCallback(function($min, $max) {
return 1;
});
self::assertSame(1, Crypto::randomDigits(1));
self::assertSame(11111111, Crypto::randomDigits(8));
}
public function dataRsa(): array
{
return [
'diaspora' => [
'key' => file_get_contents(__DIR__ . '/../../datasets/crypto/rsa/diaspora-public-rsa-base64'),
'expected' => file_get_contents(__DIR__ . '/../../datasets/crypto/rsa/diaspora-public-pem'),
],
];
}
/**
* @dataProvider dataRsa
*/
public function testPubRsaToMe(string $key, string $expected)
{
self::assertSame($expected, Crypto::rsaToPem(base64_decode($key)));
}
public function dataPEM()
{
return [
'diaspora' => [
'key' => file_get_contents(__DIR__ . '/../../datasets/crypto/rsa/diaspora-public-pem'),
],
];
}
}