]> 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 d014d2818223be2130bcbcc6d54e8953c82c8c8e..82afc24832264f94a1f4c8e13cf3600749efc9a2 100644 (file)
 <?php
+/**
+ * @copyright Copyright (C) 2010-2023, the Friendica project
+ *
+ * @license GNU AGPL version 3 or any later version
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License as
+ * published by the Free Software Foundation, either version 3 of the
+ * License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU Affero General Public License for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public License
+ * along with this program.  If not, see <https://www.gnu.org/licenses/>.
+ *
+ * This is in the same namespace as Crypto for mocking 'rand' and 'random_init'
+ */
 
-// this is in the same namespace as Install for mocking 'rand' and 'random_init'
+/// @todo Use right namespace - needs alternative way of mocking random_int()
 namespace Friendica\Util;
 
+use phpseclib\Crypt\RSA;
+use phpseclib\Math\BigInteger;
 use PHPUnit\Framework\TestCase;
 
 class CryptoTest extends TestCase
 {
-       /**
-        * Replaces function_exists results with given mocks
-        *
-        * @param array $functions a list from function names and their result
-        */
-       private function setFunctions($functions)
+       public static function tearDownAfterClass(): void
        {
+               // Reset mocking
                global $phpMock;
-               $phpMock['function_exists'] = function($function) use ($functions) {
-                       foreach ($functions as $name => $value) {
-                               if ($function == $name) {
-                                       return $value;
-                               }
-                       }
-                       return '__phpunit_continue__';
-               };
-       }
+               $phpMock = [];
 
-       /**
-        * Replaces rand results with given mocks
-        *
-        */
-       private function assertRand($min, $max)
-       {
-               global $phpMock;
-               $phpMock['rand'] = function($mMin, $mMax) use ($min, $max) {
-                       $this->assertEquals($min, $mMin);
-                       $this->assertEquals($max, $mMax);
-                       return 1;
-               };
+               parent::tearDownAfterClass();
        }
 
        /**
-        * Replaces rand results with given mocks
+        * Replaces random_int results with given mocks
         *
         */
        private function assertRandomInt($min, $max)
        {
                global $phpMock;
-               $phpMock['random_int'] = function($mMin, $mMax) use ($min, $max) {
-                       $this->assertEquals($min, $mMin);
-                       $this->assertEquals($max, $mMax);
+               $phpMock['random_int'] = function ($mMin, $mMax) use ($min, $max) {
+                       self::assertEquals($min, $mMin);
+                       self::assertEquals($max, $mMax);
                        return 1;
                };
        }
 
-       public function testRandomDigitsRand()
+       public function testRandomDigitsRandomInt()
        {
-               $this->setFunctions(['random_int' => false]);
-               $this->assertRand(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 testRandomDigitsRandomInt()
+       public function dataRsa(): array
        {
-               $this->setFunctions(['random_int' => true]);
-               $this->assertRandomInt(0, 9);
-
-               $test = Crypto::randomDigits(1);
-               $this->assertEquals(1, strlen($test));
-               $this->assertEquals(1, $test);
-
-               $test = Crypto::randomDigits(8);
-               $this->assertEquals(8, strlen($test));
-               $this->assertEquals(11111111, $test);
+               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'),
+                       ],
+               ];
        }
-}
 
-/**
- * A workaround to replace the PHP native function_exists() with a mocked function
- *
- * @param string $function_name the Name of the function
- *
- * @return bool true or false
- */
-function function_exists($function_name)
-{
-       global $phpMock;
-       if (isset($phpMock['function_exists'])) {
-               $result = call_user_func_array($phpMock['function_exists'], func_get_args());
-               if ($result !== '__phpunit_continue__') {
-                       return $result;
-               }
+       /**
+        * @dataProvider dataRsa
+        */
+       public function testPubRsaToMe(string $key, string $expected)
+       {
+               self::assertEquals($expected, Crypto::rsaToPem(base64_decode($key)));
        }
-       return call_user_func_array('\function_exists', func_get_args());
-}
 
-/**
- * A workaround to replace the PHP native rand() (< 7.0) with a mocked function
- *
- * @return int
- */
-function rand($min, $max)
-{
-       global $phpMock;
-       if (isset($phpMock['rand'])) {
-               $result = call_user_func_array($phpMock['rand'], func_get_args());
-               return $result;
+
+       public function dataPEM()
+       {
+               return [
+                       'diaspora' => [
+                               'key' => file_get_contents(__DIR__ . '/../../datasets/crypto/rsa/diaspora-public-pem'),
+                       ],
+               ];
        }
 }
 
@@ -125,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());
        }
 }