]> git.mxchange.org Git - friendica.git/blob - tests/src/Util/CryptoTest.php
Merge pull request #11250 from nupplaphil/bug/redis_pw
[friendica.git] / tests / src / Util / CryptoTest.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2010-2022, the Friendica project
4  *
5  * @license GNU AGPL version 3 or any later version
6  *
7  * This program is free software: you can redistribute it and/or modify
8  * it under the terms of the GNU Affero General Public License as
9  * published by the Free Software Foundation, either version 3 of the
10  * License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU Affero General Public License for more details.
16  *
17  * You should have received a copy of the GNU Affero General Public License
18  * along with this program.  If not, see <https://www.gnu.org/licenses/>.
19  *
20  * This is in the same namespace as Crypto for mocking 'rand' and 'random_init'
21  */
22
23 /// @todo Use right namespace - needs alternative way of mocking random_int()
24 namespace Friendica\Util;
25
26 use phpseclib\Crypt\RSA;
27 use phpseclib\Math\BigInteger;
28 use PHPUnit\Framework\TestCase;
29
30 class CryptoTest extends TestCase
31 {
32         public static function tearDownAfterClass(): void
33         {
34                 // Reset mocking
35                 global $phpMock;
36                 $phpMock = [];
37
38                 parent::tearDownAfterClass();
39         }
40
41         /**
42          * Replaces random_int results with given mocks
43          *
44          */
45         private function assertRandomInt($min, $max)
46         {
47                 global $phpMock;
48                 $phpMock['random_int'] = function ($mMin, $mMax) use ($min, $max) {
49                         self::assertEquals($min, $mMin);
50                         self::assertEquals($max, $mMax);
51                         return 1;
52                 };
53         }
54
55         public function testRandomDigitsRandomInt()
56         {
57                 self::assertRandomInt(0, 9);
58
59                 $test = Crypto::randomDigits(1);
60                 self::assertEquals(1, strlen($test));
61                 self::assertEquals(1, $test);
62
63                 $test = Crypto::randomDigits(8);
64                 self::assertEquals(8, strlen($test));
65                 self::assertEquals(11111111, $test);
66         }
67
68         public function dataRsa()
69         {
70                 return [
71                         'diaspora' => [
72                                 'key' => file_get_contents(__DIR__ . '/../../datasets/crypto/rsa/diaspora-public-rsa-base64'),
73                                 'expected' => file_get_contents(__DIR__ . '/../../datasets/crypto/rsa/diaspora-public-pem'),
74                         ],
75                 ];
76         }
77
78         /**
79          * @dataProvider dataRsa
80          */
81         public function testPubRsaToMe(string $key, string $expected)
82         {
83                 self::assertEquals($expected, Crypto::rsaToPem(base64_decode($key)));
84         }
85
86
87         public function dataPEM()
88         {
89                 return [
90                         'diaspora' => [
91                                 'key' => file_get_contents(__DIR__ . '/../../datasets/crypto/rsa/diaspora-public-pem'),
92                         ],
93                 ];
94         }
95
96         /**
97          * @dataProvider dataPEM
98          */
99         public function testPemToMe(string $key)
100         {
101                 Crypto::pemToMe($key, $m, $e);
102
103                 $expectedRSA = new RSA();
104                 $expectedRSA->loadKey([
105                         'e' => new BigInteger($e, 256),
106                         'n' => new BigInteger($m, 256)
107                 ]);
108
109                 self::assertEquals($expectedRSA->getPublicKey(), $key);
110         }
111
112         /**
113          * @dataProvider dataPEM
114          */
115         public function testMeToPem(string $key)
116         {
117                 Crypto::pemToMe($key, $m, $e);
118
119                 $checkKey = Crypto::meToPem($m, $e);
120
121                 self::assertEquals($key, $checkKey);
122         }
123 }
124
125 /**
126  * A workaround to replace the PHP native random_int() (>= 7.0) with a mocked function
127  *
128  * @return int
129  */
130 function random_int($min, $max)
131 {
132         global $phpMock;
133         if (isset($phpMock['random_int'])) {
134                 return call_user_func_array($phpMock['random_int'], func_get_args());
135         }
136 }