]> git.mxchange.org Git - friendica.git/blobdiff - tests/src/Util/CryptoTest.php
Merge remote-tracking branch 'upstream/develop' into inbox-gsid
[friendica.git] / tests / src / Util / CryptoTest.php
index 51305337a94c66f3d27bd36e332e9a2a130783fe..82afc24832264f94a1f4c8e13cf3600749efc9a2 100644 (file)
@@ -1,6 +1,6 @@
 <?php
 /**
- * @copyright Copyright (C) 2020, Friendica
+ * @copyright Copyright (C) 2010-2023, the Friendica project
  *
  * @license GNU AGPL version 3 or any later version
  *
@@ -19,6 +19,8 @@
  *
  * This is in the same namespace as Crypto for mocking 'rand' and 'random_init'
  */
+
+/// @todo Use right namespace - needs alternative way of mocking random_int()
 namespace Friendica\Util;
 
 use phpseclib\Crypt\RSA;
@@ -27,6 +29,15 @@ use PHPUnit\Framework\TestCase;
 
 class CryptoTest extends TestCase
 {
+       public static function tearDownAfterClass(): void
+       {
+               // Reset mocking
+               global $phpMock;
+               $phpMock = [];
+
+               parent::tearDownAfterClass();
+       }
+
        /**
         * Replaces random_int results with given mocks
         *
@@ -35,26 +46,26 @@ class CryptoTest extends TestCase
        {
                global $phpMock;
                $phpMock['random_int'] = function ($mMin, $mMax) use ($min, $max) {
-                       $this->assertEquals($min, $mMin);
-                       $this->assertEquals($max, $mMax);
+                       self::assertEquals($min, $mMin);
+                       self::assertEquals($max, $mMax);
                        return 1;
                };
        }
 
        public function testRandomDigitsRandomInt()
        {
-               $this->assertRandomInt(0, 9);
+               self::assertRandomInt(0, 9);
 
                $test = Crypto::randomDigits(1);
-               $this->assertEquals(1, strlen($test));
-               $this->assertEquals(1, $test);
+               self::assertEquals(1, strlen($test));
+               self::assertEquals(1, $test);
 
                $test = Crypto::randomDigits(8);
-               $this->assertEquals(8, strlen($test));
-               $this->assertEquals(11111111, $test);
+               self::assertEquals(8, strlen($test));
+               self::assertEquals(11111111, $test);
        }
 
-       public function dataRsa()
+       public function dataRsa(): array
        {
                return [
                        'diaspora' => [
@@ -69,11 +80,11 @@ class CryptoTest extends TestCase
         */
        public function testPubRsaToMe(string $key, string $expected)
        {
-               $this->assertEquals($expected, Crypto::rsaToPem(base64_decode($key)));
+               self::assertEquals($expected, Crypto::rsaToPem(base64_decode($key)));
        }
 
 
-       public function datePem()
+       public function dataPEM()
        {
                return [
                        'diaspora' => [
@@ -81,22 +92,6 @@ class CryptoTest extends TestCase
                        ],
                ];
        }
-
-       /**
-        * @dataProvider datePem
-        */
-       public function testPemToMe(string $key)
-       {
-               Crypto::pemToMe($key, $m, $e);
-
-               $expectedRSA = new RSA();
-               $expectedRSA->loadKey([
-                                                                 'e' => new BigInteger($e, 256),
-                                                                 'n' => new BigInteger($m, 256)
-                                                         ]);
-
-               $this->assertEquals($expectedRSA->getPublicKey(), $key);
-       }
 }
 
 /**
@@ -108,7 +103,6 @@ function random_int($min, $max)
 {
        global $phpMock;
        if (isset($phpMock['random_int'])) {
-               $result = call_user_func_array($phpMock['random_int'], func_get_args());
-               return $result;
+               return call_user_func_array($phpMock['random_int'], func_get_args());
        }
 }