]> git.mxchange.org Git - friendica.git/blob - tests/src/Util/CryptoTest.php
Merge pull request #8261 from MrPetovan/task/8251-use-about-for-pdesc
[friendica.git] / tests / src / Util / CryptoTest.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2020, Friendica
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 namespace Friendica\Util;
23
24 use PHPUnit\Framework\TestCase;
25
26 class CryptoTest extends TestCase
27 {
28         /**
29          * Replaces random_int results with given mocks
30          *
31          */
32         private function assertRandomInt($min, $max)
33         {
34                 global $phpMock;
35                 $phpMock['random_int'] = function($mMin, $mMax) use ($min, $max) {
36                         $this->assertEquals($min, $mMin);
37                         $this->assertEquals($max, $mMax);
38                         return 1;
39                 };
40         }
41
42         public function testRandomDigitsRandomInt()
43         {
44                 $this->assertRandomInt(0, 9);
45
46                 $test = Crypto::randomDigits(1);
47                 $this->assertEquals(1, strlen($test));
48                 $this->assertEquals(1, $test);
49
50                 $test = Crypto::randomDigits(8);
51                 $this->assertEquals(8, strlen($test));
52                 $this->assertEquals(11111111, $test);
53         }
54 }
55
56 /**
57  * A workaround to replace the PHP native random_int() (>= 7.0) with a mocked function
58  *
59  * @return int
60  */
61 function random_int($min, $max)
62 {
63         global $phpMock;
64         if (isset($phpMock['random_int'])) {
65                 $result = call_user_func_array($phpMock['random_int'], func_get_args());
66                 return $result;
67         }
68 }