Translated using Weblate (Portuguese)
[phpmyadmin.git] / tests / unit / WebAuthn / CBORDecoderTest.php
blob1a9a40dc5a5ea0e65f92c60b07abc23df8717312
1 <?php
3 declare(strict_types=1);
5 namespace PhpMyAdmin\Tests\WebAuthn;
7 use PhpMyAdmin\WebAuthn\CBORDecoder;
8 use PhpMyAdmin\WebAuthn\DataStream;
9 use PhpMyAdmin\WebAuthn\WebAuthnException;
10 use PHPUnit\Framework\Attributes\CoversClass;
11 use PHPUnit\Framework\Attributes\DataProvider;
12 use PHPUnit\Framework\TestCase;
14 use function base64_decode;
15 use function hex2bin;
17 use const INF;
19 #[CoversClass(CBORDecoder::class)]
20 #[CoversClass(DataStream::class)]
21 class CBORDecoderTest extends TestCase
23 #[DataProvider('dataProviderForTestDecode')]
24 public function testDecode(string $encoded, mixed $expected): void
26 $decoder = new CBORDecoder();
27 $data = hex2bin($encoded);
28 self::assertNotFalse($data);
29 self::assertSame($expected, $decoder->decode(new DataStream($data)));
32 /** @psalm-return iterable<int, array{string, mixed}> */
33 public static function dataProviderForTestDecode(): iterable
35 return [
36 ['00', 0],
37 ['01', 1],
38 ['0a', 10],
39 ['17', 23],
40 ['1818', 24],
41 ['1819', 25],
42 ['1864', 100],
43 ['1903e8', 1000],
44 ['1a000f4240', 1000000],
45 //['1b000000e8d4a51000', 1000000000000],
46 //['1bffffffffffffffff', 18446744073709551615],
47 //['c249010000000000000000', 18446744073709551616],
48 //['3bffffffffffffffff', -18446744073709551616],
49 //['c349010000000000000000', -18446744073709551617],
50 ['20', -1],
51 ['29', -10],
52 ['3863', -100],
53 ['3903e7', -1000],
54 ['f90000', 0.0],
55 ['f98000', -0.0],
56 ['f93c00', 1.0],
57 ['fb3ff199999999999a', 1.1],
58 ['f93e00', 1.5],
59 ['f97bff', 65504.0],
60 ['fa47c35000', 100000.0],
61 ['fa7f7fffff', 3.4028234663852886e+38],
62 ['fb7e37e43c8800759c', 1.0e+300],
63 ['f90001', 5.960464477539063e-8],
64 ['f90400', 0.00006103515625],
65 ['f9c400', -4.0],
66 ['fbc010666666666666', -4.1],
67 ['f97c00', INF],
68 ['f9fc00', -INF],
69 ['fa7f800000', INF],
70 ['faff800000', -INF],
71 ['fb7ff0000000000000', INF],
72 ['fbfff0000000000000', -INF],
73 ['f4', true],
74 ['f5', false],
75 ['f6', null],
76 //['f7', 'undefined'],
77 ['f0', 16],
78 ['f818', 24],
79 ['f8ff', 255],
80 ['c074323031332d30332d32315432303a30343a30305a', '2013-03-21T20:04:00Z'],
81 ['c11a514b67b0', 1363896240],
82 ['c1fb41d452d9ec200000', 1363896240.5],
83 ['d74401020304', hex2bin('01020304')],
84 ['d818456449455446', hex2bin('6449455446')],
85 ['d82076687474703a2f2f7777772e6578616d706c652e636f6d', 'http://www.example.com'],
86 ['40', hex2bin('')],
87 ['4401020304', hex2bin('01020304')],
88 ['60', ''],
89 ['6161', 'a'],
90 ['6449455446', 'IETF'],
91 ['62225c', '"\\'],
92 ['62c3bc', "\u{00fc}"],
93 ['63e6b0b4', "\u{6c34}"],
94 ['64f0908591', "\u{10151}"], // "\u{d800}\u{dd51}"
95 ['80', []],
96 ['83010203', [1, 2, 3]],
97 ['8301820203820405', [1, [2, 3], [4, 5]]],
99 '98190102030405060708090a0b0c0d0e0f101112131415161718181819',
100 [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25],
102 ['a0', []],
103 ['a201020304', [1 => 2, 3 => 4]],
104 ['a26161016162820203', ['a' => 1, 'b' => [2, 3]]],
105 ['826161a161626163', ['a', ['b' => 'c']]],
107 'a56161614161626142616361436164614461656145',
108 ['a' => 'A', 'b' => 'B', 'c' => 'C', 'd' => 'D', 'e' => 'E'],
110 ['a1646e616d656441636d65', ['name' => 'Acme']],
112 'a462696458203082019330820138a0030201023082019330820138a003020102'
113 . '3082019330826469636f6e782b68747470733a2f2f706963732e6578616d706c'
114 . '652e636f6d2f30302f702f61426a6a6a707150622e706e67646e616d65766a6f'
115 . '686e70736d697468406578616d706c652e636f6d6b646973706c61794e616d65'
116 . '6d4a6f686e20502e20536d697468a462696458203082019330820138a0030201'
117 . '023082019330820138a0030201023082019330826469636f6e782b6874747073'
118 . '3a2f2f706963732e6578616d706c652e636f6d2f30302f702f61426a6a6a7071'
119 . '50622e706e67646e616d65766a6f686e70736d697468406578616d706c652e63'
120 . '6f6d6b646973706c61794e616d656d4a6f686e20502e20536d697468',
122 'id' => base64_decode('MIIBkzCCATigAwIBAjCCAZMwggE4oAMCAQIwggGTMII='),
123 'icon' => 'https://pics.example.com/00/p/aBjjjpqPb.png',
124 'name' => 'johnpsmith@example.com',
125 'displayName' => 'John P. Smith',
129 '82a263616c672664747970656a7075626C69632D6B6579a263616c6739010064747970656a7075626C69632D6B6579',
130 [['alg' => -7,'type' => 'public-key'], ['alg' => -257,'type' => 'public-key']],
133 'A501020326200121582065eda5a12577c2bae829437fe338701a10aaa375e1bb5b5de108de439c08551d'
134 . '2258201e52ed75701163f7f9e40ddf9f341b3dc9ba860af7e0ca7ca7e9eecd0084d19c',
136 1 => 2,
137 3 => -7,
138 -1 => 1,
139 -2 => hex2bin('65eda5a12577c2bae829437fe338701a10aaa375e1bb5b5de108de439c08551d'),
140 -3 => hex2bin('1e52ed75701163f7f9e40ddf9f341b3dc9ba860af7e0ca7ca7e9eecd0084d19c'),
146 public function testDecodeForNaNValues(): void
148 $decoder = new CBORDecoder();
149 $nanValues = ['f97e00', 'fa7fc00000', 'fb7ff8000000000000'];
150 foreach ($nanValues as $value) {
151 $data = hex2bin($value);
152 self::assertNotFalse($data);
153 self::assertNan($decoder->decode(new DataStream($data)));
157 #[DataProvider('indefiniteLengthValuesProvider')]
158 public function testDecodeForNotSupportedValues(string $encoded): void
160 $decoder = new CBORDecoder();
161 $data = hex2bin($encoded);
162 self::assertNotFalse($data);
163 $this->expectException(WebAuthnException::class);
164 $decoder->decode(new DataStream($data));
167 /** @psalm-return iterable<int, array{string}> */
168 public static function indefiniteLengthValuesProvider(): iterable
170 return [
171 ['5f42010243030405ff'], // (_ h'0102', h'030405')
172 ['7f657374726561646d696e67ff'], // (_ "strea", "ming")
173 ['9fff'], // [_ ]
174 ['9f018202039f0405ffff'], // [_ 1, [2, 3], [_ 4, 5]]
175 ['9f01820203820405ff'], // [_ 1, [2, 3], [4, 5]]
176 ['83018202039f0405ff'], // [1, [2, 3], [_ 4, 5]]
177 ['83019f0203ff820405'], // [1, [_ 2, 3], [4, 5]]
178 // [_ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25]
179 ['9f0102030405060708090a0b0c0d0e0f101112131415161718181819ff'],
180 ['bf61610161629f0203ffff'], // {_ "a": 1, "b": [_ 2, 3]}
181 ['826161bf61626163ff'], // ["a", {_ "b": "c"}]
182 ['bf6346756ef563416d7421ff'], // {_ "Fun": true, "Amt": -2}