-
Notifications
You must be signed in to change notification settings - Fork 106
/
Copy pathUtilTest.php
164 lines (155 loc) · 5.29 KB
/
UtilTest.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
<?php
declare(strict_types=1);
namespace ParagonIE\Paseto\Tests;
use ParagonIE\ConstantTime\{
Base64UrlSafe,
Hex
};
use ParagonIE\Paseto\Exception\EncodingException;
use ParagonIE\Paseto\Util;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\TestCase;
use TypeError;
/**
* Class UtilTest
* @package ParagonIE\Paseto\Tests
*/
class UtilTest extends TestCase
{
/**
* @throws EncodingException
*/
public function testDepth()
{
$this->assertSame(1, Util::calculateJsonDepth('"abc"'));
$this->assertSame(2, Util::calculateJsonDepth('{"abc":"def"}'));
$this->assertSame(3, Util::calculateJsonDepth('{"abc":{"abc":"def"}}'));
$this->assertSame(4, Util::calculateJsonDepth('{"abc":{"abc":{"abc":"def"}}}'));
$this->assertSame(2, Util::calculateJsonDepth('{"abc":"{\"test\":\"foo\"}"}'));
$this->assertSame(3, Util::calculateJsonDepth('{"abc":"{\"test\":\"foo\"}","def":[{"abc":"def"}]}'));
$this->assertSame(3, Util::calculateJsonDepth('{"abc":"{\"test\":\"foo\"}" ,"def":[{"abc":"def"}]}'));
$depth = random_int(1024, 8192);
$this->assertSame(
$depth + 1,
Util::calculateJsonDepth(
'{"a":' .
str_repeat('[', $depth) .
'1, 2, 3' .
str_repeat(']', $depth) .
'}'
)
);
}
/**
* @throws EncodingException
*/
public function testInvalid()
{
$this->expectException(EncodingException::class);
Util::calculateJsonDepth('{"a":[}');
}
/**
* @covers Util::preAuthEncode()
* @throws TypeError
*/
public function testPreAuthEncode()
{
$this->assertSame(
'0000000000000000',
Hex::encode(Util::preAuthEncode(...[])),
'Empty array'
);
$this->assertSame(
'0000000000000000',
Hex::encode(Util::preAuthEncode()),
'Empty array'
);
$this->assertSame(
'01000000000000000000000000000000',
Hex::encode(Util::preAuthEncode(...[''])),
'Array of empty string'
);
$this->assertSame(
'01000000000000000000000000000000',
Hex::encode(Util::preAuthEncode('')),
'Array of empty string'
);
$this->assertSame(
'020000000000000000000000000000000000000000000000',
Hex::encode(Util::preAuthEncode(...['', ''])),
'Array of empty strings'
);
$this->assertSame(
'020000000000000000000000000000000000000000000000',
Hex::encode(Util::preAuthEncode('', '')),
'Array of empty strings'
);
$this->assertSame(
'0100000000000000070000000000000050617261676f6e',
Hex::encode(Util::preAuthEncode(...['Paragon'])),
'Array of non-empty string'
);
$this->assertSame(
'0100000000000000070000000000000050617261676f6e',
Hex::encode(Util::preAuthEncode('Paragon')),
'Array of non-empty string'
);
$this->assertSame(
'0200000000000000070000000000000050617261676f6e0a00000000000000496e6974696174697665',
Hex::encode(Util::preAuthEncode(...['Paragon', 'Initiative'])),
'Array of two non-empty strings'
);
$this->assertSame(
'0200000000000000070000000000000050617261676f6e0a00000000000000496e6974696174697665',
Hex::encode(Util::preAuthEncode('Paragon', 'Initiative')),
'Array of two non-empty strings'
);
$this->assertSame(
'0100000000000000190000000000000050617261676f6e0a00000000000000496e6974696174697665',
Hex::encode(Util::preAuthEncode(...[
'Paragon' . chr(10) . str_repeat("\0", 7) . 'Initiative'
])),
'Ensure that faked padding results in different prefixes'
);
$this->assertSame(
'0100000000000000190000000000000050617261676f6e0a00000000000000496e6974696174697665',
Hex::encode(Util::preAuthEncode(
'Paragon' . chr(10) . str_repeat("\0", 7) . 'Initiative'
)),
'Ensure that faked padding results in different prefixes'
);
}
public static function newlineProvider(): array
{
return [
["abc\ndef", 'abcdef'],
["abc\r\ndef", 'abcdef'],
["abc \r\ndef", 'abc def'],
["\n", ''],
["\r\n", ''],
];
}
#[DataProvider('newlineProvider')]
public function testStripNewliens(string $input, string $output): void
{
$this->assertSame($output, Util::stripNewlines($input));
}
/**
* @covers Util::validateAndRemoveFooter()
*/
public function testValidateAndRemoveFooter()
{
$token = Base64UrlSafe::encode(random_bytes(30));
$footer = random_bytes(10);
$combined = $token . '.' . Base64UrlSafe::encodeUnpadded($footer);
$this->assertSame(
$token,
Util::validateAndRemoveFooter($combined, $footer)
);
try {
Util::validateAndRemoveFooter($combined, 'wrong');
$this->fail('Invalid footer was accepted');
} catch (\Exception $ex) {
}
}
}