-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Base32Test.php
302 lines (266 loc) · 10.7 KB
/
Base32Test.php
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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
<?php
declare(strict_types=1);
namespace Bakame\Aide\Base32;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\Attributes\Test;
use PHPUnit\Framework\TestCase;
use ValueError;
use function base64_decode;
use const PHP_BASE32_ASCII;
use const PHP_BASE32_HEX;
/**
* @see https://opensource.apple.com/source/tcl/tcl-87/tcl_ext/tcllib/tcllib/modules/base32/base32hex.testsuite.auto.html
* @see https://opensource.apple.com/source/tcl/tcl-87/tcl_ext/tcllib/tcllib/modules/base32/base32.testsuite.auto.html
*/
final class Base32Test extends TestCase
{
#[DataProvider('base32encodeAsciiDataProvider')]
#[Test]
public function it_will_base32_encode_on_ascii_mode(string $decoded, string $encoded): void
{
self::assertSame($encoded, base32_encode($decoded));
}
#[DataProvider('base32encodeHexDataProvider')]
#[Test]
public function it_will_base32_encode_on_hex_mode(string $decoded, string $encoded): void
{
self::assertSame($encoded, base32_encode($decoded, PHP_BASE32_HEX));
}
#[DataProvider('base32decodeAsciiDataProvider')]
#[Test]
public function it_will_base32_decode_on_ascii_mode(string $decoded, string $encoded): void
{
self::assertSame($decoded, base32_decode($encoded));
}
#[DataProvider('base32decodeHexDataProvider')]
#[Test]
public function it_will_base32_decode_on_hex_mode(string $decoded, string $encoded): void
{
self::assertSame($decoded, base32_decode($encoded, PHP_BASE32_HEX));
}
#[DataProvider('backAndForthDataProvider')]
#[Test]
public function it_will_base32_encode_and_decode(string $string): void
{
self::assertSame($string, base32_decode(base32_encode($string)));
self::assertSame($string, base32_decode(base32_encode($string, PHP_BASE32_HEX), PHP_BASE32_HEX));
}
#[Test]
public function it_will_base32_decode_multiline_data(): void
{
$base32 = <<<BASE
89GMSPRL
D4yyyyyy
BASE;
self::assertSame('Bangui', base32_decode($base32, PHP_BASE32_HEX, 'y', true));
}
#[Test]
public function it_wiil_distinguish_alphabet_on_character_casing_on_strict_mode(): void
{
$alphabet = strtolower(PHP_BASE32_ASCII);
$expected = 'bangui';
self::assertSame($expected, base32_decode(encoded: base32_encode($expected, $alphabet)));
self::assertFalse(base32_decode(encoded: base32_encode($expected, $alphabet), strict: true));
}
#[DataProvider('invalidDecodingSequence')]
#[Test]
public function it_will_return_false_from_invalid_encoded_string_with_base32_decode_function(
string $sequence,
string $message,
string $alphabet,
string $padding
): void {
try {
self::assertFalse(base32_decode($sequence, $alphabet, $padding, true)); /* @phpstan-ignore-line */
} catch (ValueError $exception) {
self::assertSame($message, $exception->getMessage());
}
}
/**
* @return array<string, array{0:string|false, 1:string}>
*/
public static function base32encodeAsciiDataProvider(): array
{
return [
'RFC Vector 1' => ['f', 'MY======'],
'RFC Vector 2' => ['fo', 'MZXQ===='],
'RFC Vector 3' => ['foo', 'MZXW6==='],
'RFC Vector 4' => ['foob', 'MZXW6YQ='],
'RFC Vector 5' => ['fooba', 'MZXW6YTB'],
'RFC Vector 6' => ['foobar', 'MZXW6YTBOI======'],
'Old Vector 1' => [' ', 'EA======'],
'Old Vector 2' => [' ', 'EAQA===='],
'Old Vector 3' => [' ', 'EAQCA==='],
'Old Vector 4' => [' ', 'EAQCAIA='],
'Old Vector 5' => [' ', 'EAQCAIBA'],
'Old Vector 6' => [' ', 'EAQCAIBAEA======'],
'Empty String' => ['', ''],
'Random Integers' => [base64_decode('HgxBl1kJ4souh+ELRIHm/x8yTc/cgjDmiCNyJR/NJfs=', true), 'DYGEDF2ZBHRMULUH4EFUJAPG74PTETOP3SBDBZUIENZCKH6NEX5Q===='],
'Partial zero edge case' => ['8', 'HA======'],
];
}
/**
* @return array<string, array{0:string|false, 1:string}>
*/
public static function base32decodeAsciiDataProvider(): array
{
return [
...self::base32encodeAsciiDataProvider(),
'All Invalid Characters' => ['', '8908908908908908'],
'empty Characters' => ['', ' '],
];
}
/**
* @return array<string, array{0: string|false, 1: string}>
**/
public static function base32encodeHexDataProvider(): array
{
return [
'RFC Vector 1' => ['f', 'CO======'],
'RFC Vector 2' => ['fo', 'CPNG===='],
'RFC Vector 3' => ['foo', 'CPNMU==='],
'RFC Vector 4' => ['foob', 'CPNMUOG='],
'RFC Vector 5' => ['fooba', 'CPNMUOJ1'],
'RFC Vector 6' => ['foobar', 'CPNMUOJ1E8======'],
'Old Vector 1' => [' ', '40======'],
'Old Vector 2' => [' ', '40G0===='],
'Old Vector 3' => [' ', '40G20==='],
'Old Vector 4' => [' ', '40G2080='],
'Old Vector 5' => [' ', '40G20810'],
'Old Vector 6' => [' ', '40G2081040======'],
'Empty String' => ['', ''],
'Random Integers' => [base64_decode('HgxBl1kJ4souh+ELRIHm/x8yTc/cgjDmiCNyJR/NJfs=', true), '3O6435QP17HCKBK7S45K90F6VSFJ4JEFRI131PK84DP2A7UD4NTG===='],
];
}
/**
* @return array<string, array{0: string|false, 1: string}>
*/
public static function base32decodeHexDataProvider(): array
{
return [
...self::base32encodeHexDataProvider(),
'All Invalid Characters' => ['', 'WXYXWXYZWXYZWXYZ'],
'empty Characters' => ['', ' '],
];
}
/**
* Back and forth encoding must return the same result.
*
* @return array<string, array<string>>
*/
public static function backAndForthDataProvider(): array
{
return [
'Empty String' => [''],
'Ten' => ['10'],
'Test130' => ['test130'],
'test' => ['test'],
'Eight' => ['8'],
'Zero' => ['0'],
'Equals' => ['='],
'Foobar' => ['foobar'],
];
}
/**
* @return iterable<string, array{sequence: string, message: string, alphabet: string, padding:string}>
*/
public static function invalidDecodingSequence(): iterable
{
yield 'characters outside of base32 extended hex alphabet' => [
'sequence' => 'MZXQ====',
'message' => 'The encoded string contains characters outside of the base32 alphabet.',
'alphabet' => PHP_BASE32_HEX,
'padding' => '=',
];
yield 'characters outside of base32 us ascii alphabet' => [
'sequence' => '90890808',
'message' => 'The encoded string contains characters outside of the base32 alphabet.',
'alphabet' => PHP_BASE32_ASCII,
'padding' => '=',
];
yield 'characters not upper-cased' => [
'sequence' => 'MzxQ====',
'message' => 'The encoded string contains lower-cased characters which is forbidden on strict mode.',
'alphabet' => PHP_BASE32_ASCII,
'padding' => '=',
];
yield 'padding character in the middle of the sequence' => [
'sequence' => 'A=ACA===',
'message' => 'A padding character is contained in the middle of the encoded string.',
'alphabet' => PHP_BASE32_ASCII,
'padding' => '=',
];
yield 'invalid padding length' => [
'sequence' => 'A=======',
'message' => 'The encoded string contains an invalid padding length.',
'alphabet' => PHP_BASE32_ASCII,
'padding' => '=',
];
yield 'invalid encoded string length' => [
'sequence' => 'A',
'message' => 'The encoded string length is not a multiple of 8.',
'alphabet' => PHP_BASE32_HEX,
'padding' => '=',
];
yield 'invalid alphabet length' => [
'sequence' => 'A',
'message' => 'The alphabet must be a 32 bytes long string.',
'alphabet' => '1234567890asdfghjklzxcvbnm',
'padding' => '=',
];
yield 'the padding character is contained within the alphabet' => [
'sequence' => 'A',
'message' => 'The alphabet can not contain a reserved character.',
'alphabet' => str_replace('A', '*', PHP_BASE32_ASCII),
'padding' => '*',
];
yield 'the padding character is contained within the alphabet is case insensitive' => [
'sequence' => 'A',
'message' => 'The alphabet can not contain a reserved character.',
'alphabet' => str_replace('A', '*', PHP_BASE32_ASCII),
'padding' => 'a',
];
yield 'the padding character is different than one byte' => [
'sequence' => 'A',
'message' => 'The padding character must be a non-reserved single byte character.',
'alphabet' => PHP_BASE32_ASCII,
'padding' => 'yo',
];
yield 'the padding character can not contain "\r"' => [
'sequence' => 'A',
'message' => 'The padding character must be a non-reserved single byte character.',
'alphabet' => PHP_BASE32_ASCII,
'padding' => "\r",
];
yield 'the padding character can not contain "\n"' => [
'sequence' => 'A',
'message' => 'The padding character must be a non-reserved single byte character.',
'alphabet' => PHP_BASE32_ASCII,
'padding' => "\n",
];
yield 'the padding character can not contain "\t"' => [
'sequence' => 'A',
'message' => 'The padding character must be a non-reserved single byte character.',
'alphabet' => PHP_BASE32_ASCII,
'padding' => "\t",
];
yield 'the alphabet can not contain "\r"' => [
'sequence' => 'A',
'message' => 'The alphabet can not contain a reserved character.',
'alphabet' => substr(PHP_BASE32_ASCII, 0, -1)."\r",
'padding' => '=',
];
yield 'the alphabet can not contain "\n"' => [
'sequence' => 'A',
'message' => 'The alphabet can not contain a reserved character.',
'alphabet' => substr(PHP_BASE32_HEX, 0, -1)."\n",
'padding' => '=',
];
yield 'the alphabet can not contain "\t"' => [
'sequence' => 'A',
'message' => 'The alphabet can not contain a reserved character.',
'alphabet' => substr(PHP_BASE32_HEX, 0, -1)."\t",
'padding' => '=',
];
}
}