]> git.mxchange.org Git - friendica.git/blob - tests/src/Protocol/SalmonTest.php
Merge pull request #12697 from MrPetovan/bug/deprecated
[friendica.git] / tests / src / Protocol / SalmonTest.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2010-2023, 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  */
21
22 namespace Friendica\Test\src\Protocol;
23
24 use Friendica\Protocol\Salmon;
25
26 class SalmonTest extends \PHPUnit\Framework\TestCase
27 {
28         public function dataMagic(): array
29         {
30                 return [
31                         'salmon' => [
32                                 'magic' => file_get_contents(__DIR__ . '/../../datasets/crypto/rsa/salmon-public-magic'),
33                                 'pem'   => file_get_contents(__DIR__ . '/../../datasets/crypto/rsa/salmon-public-pem'),
34                         ],
35                 ];
36         }
37
38         /**
39          * @dataProvider dataMagic
40          *
41          * @param $magic
42          * @param $pem
43          * @return void
44          * @throws \Exception
45          */
46         public function testSalmonKey($magic, $pem)
47         {
48                 $this->assertEquals($magic, Salmon::salmonKey($pem));
49         }
50
51         /**
52          * @dataProvider dataMagic
53          *
54          * @param $magic
55          * @param $pem
56          * @return void
57          */
58         public function testMagicKeyToPem($magic, $pem)
59         {
60                 $this->assertEquals($pem, Salmon::magicKeyToPem($magic));
61         }
62
63         public function dataMagicFailure(): array
64         {
65                 return [
66                         'empty string' => [
67                                 'magic' => '',
68                         ],
69                         'Missing algo' => [
70                                 'magic' => 'tvsoBZbLUvqWs-0d8C5hVQLjLCjjxyZb17Rm8_9FDqBYUigBSFDcJCzG27FM-zuddwpgJB0vDuPKQnt59kKRsw.AQAB',
71                         ],
72                         'Missing modulus' => [
73                                 'magic' => 'RSA.AQAB',
74                         ],
75                         'Missing exponent' => [
76                                 'magic' => 'RSA.tvsoBZbLUvqWs-0d8C5hVQLjLCjjxyZb17Rm8_9FDqBYUigBSFDcJCzG27FM-zuddwpgJB0vDuPKQnt59kKRsw',
77                         ],
78                         'Missing key parts' => [
79                                 'magic' => 'RSA.',
80                         ],
81                         'Too many parts' => [
82                                 'magic' => 'RSA.tvsoBZbLUvqWs-0d8C5hVQLjLCjjxyZb17Rm8_9FDqBYUigBSFDcJCzG27FM-zuddwpgJB0vDuPKQnt59kKRsw.AQAB.AQAB',
83                         ],
84                         'Wrong encoding' => [
85                                 'magic' => 'RSA.tvsoBZbLUvqWs-0d8C5hVQLjLCjjxyZb17Rm8/9FDqBYUigBSFDcJCzG27FM+zuddwpgJB0vDuPKQnt59kKRsw.AQAB',
86                         ],
87                         'Wrong algo' => [
88                                 'magic' => 'ECDSA.tvsoBZbLUvqWs-0d8C5hVQLjLCjjxyZb17Rm8_9FDqBYUigBSFDcJCzG27FM-zuddwpgJB0vDuPKQnt59kKRsw.AQAB',
89                         ],
90                 ];
91         }
92
93         /**
94          * @dataProvider dataMagicFailure
95          *
96          * @param $magic
97          * @return void
98          */
99         public function testMagicKeyToPemFailure($magic)
100         {
101                 $this->expectException(\Throwable::class);
102
103                 Salmon::magicKeyToPem($magic);
104         }
105 }