-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathFuzzBase64.php
More file actions
71 lines (59 loc) · 1.98 KB
/
Copy pathFuzzBase64.php
File metadata and controls
71 lines (59 loc) · 1.98 KB
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
<?php
declare(strict_types=1);
namespace FediE2EE\PKDServer\Fuzzing;
use ParagonIE\ConstantTime\{
Base64,
Base64UrlSafe
};
use PhpFuzzer\Config;
use RangeException;
use RuntimeException;
use SodiumException;
use TypeError;
/** @var Config $config */
require_once dirname(__DIR__) . '/vendor/autoload.php';
$config->setTarget(function (string $input): void {
// Test Base64UrlSafe::decodeNoPadding
try {
$decoded = Base64UrlSafe::decodeNoPadding($input);
// Verify round-trip
$reencoded = Base64UrlSafe::encodeUnpadded($decoded);
$redecoded = Base64UrlSafe::decodeNoPadding($reencoded);
if ($decoded !== $redecoded) {
throw new RuntimeException('Round-trip mismatch');
}
} catch (RangeException|TypeError) {
// Expected for invalid base64
}
// Test Base64UrlSafe::decode (with padding)
try {
$decoded = Base64UrlSafe::decode($input);
// Verify encode produces valid output
$reencoded = Base64UrlSafe::encode($decoded);
Base64UrlSafe::decode($reencoded);
} catch (RangeException|TypeError) {
// Expected for invalid base64
}
// Test standard Base64 operations
try {
$decoded = Base64::decode($input);
Base64::encode($decoded);
} catch (RangeException|TypeError) {
// Expected for invalid base64
}
// Test encoding arbitrary binary data (should never fail)
$encoded = Base64UrlSafe::encodeUnpadded($input);
$decoded = Base64UrlSafe::decodeNoPadding($encoded);
if ($input !== $decoded) {
throw new RuntimeException('Encode/decode round-trip failed');
}
// Test with hex conversion (common pattern in codebase)
try {
$hex = bin2hex($input);
$binary = sodium_hex2bin($hex);
$base64 = Base64UrlSafe::encodeUnpadded($binary);
Base64UrlSafe::decodeNoPadding($base64);
} catch (SodiumException|RangeException|TypeError) {
// Expected for edge cases
}
});