-
Notifications
You must be signed in to change notification settings - Fork 305
/
Copy pathVAPIDTest.php
91 lines (84 loc) · 3.74 KB
/
VAPIDTest.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
<?php declare(strict_types=1);
/*
* This file is part of the WebPush library.
*
* (c) Louis Lagrange <lagrange.louis@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
use Minishlink\WebPush\Utils;
use Minishlink\WebPush\VAPID;
use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\Attributes\DataProvider;
#[CoversClass(VAPID::class)]
final class VAPIDTest extends PHPUnit\Framework\TestCase
{
public static function vapidProvider(): array
{
return [
[
'http://push.com',
[
'subject' => 'http://test.com',
'publicKey' => 'BA6jvk34k6YjElHQ6S0oZwmrsqHdCNajxcod6KJnI77Dagikfb--O_kYXcR2eflRz6l3PcI2r8fPCH3BElLQHDk',
'privateKey' => '-3CdhFOqjzixgAbUSa0Zv9zi-dwDVmWO7672aBxSFPQ',
],
"aesgcm",
1475452165,
'WebPush eyJ0eXAiOiJKV1QiLCJhbGciOiJFUzI1NiJ9.eyJhdWQiOiJodHRwOi8vcHVzaC5jb20iLCJleHAiOjE0NzU0NTIxNjUsInN1YiI6Imh0dHA6Ly90ZXN0LmNvbSJ9.4F3ZKjeru4P9XM20rHPNvGBcr9zxhz8_ViyNfe11_xcuy7A9y7KfEPt6yuNikyW7eT9zYYD5mQZubDGa-5H2cA',
'p256ecdsa=BA6jvk34k6YjElHQ6S0oZwmrsqHdCNajxcod6KJnI77Dagikfb--O_kYXcR2eflRz6l3PcI2r8fPCH3BElLQHDk',
], [
'http://push.com',
[
'subject' => 'http://test.com',
'publicKey' => 'BA6jvk34k6YjElHQ6S0oZwmrsqHdCNajxcod6KJnI77Dagikfb--O_kYXcR2eflRz6l3PcI2r8fPCH3BElLQHDk',
'privateKey' => '-3CdhFOqjzixgAbUSa0Zv9zi-dwDVmWO7672aBxSFPQ',
],
"aes128gcm",
1475452165,
'vapid t=eyJ0eXAiOiJKV1QiLCJhbGciOiJFUzI1NiJ9.eyJhdWQiOiJodHRwOi8vcHVzaC5jb20iLCJleHAiOjE0NzU0NTIxNjUsInN1YiI6Imh0dHA6Ly90ZXN0LmNvbSJ9.4F3ZKjeru4P9XM20rHPNvGBcr9zxhz8_ViyNfe11_xcuy7A9y7KfEPt6yuNikyW7eT9zYYD5mQZubDGa-5H2cA, k=BA6jvk34k6YjElHQ6S0oZwmrsqHdCNajxcod6KJnI77Dagikfb--O_kYXcR2eflRz6l3PcI2r8fPCH3BElLQHDk',
null,
],
];
}
/**
* @throws ErrorException
*/
#[dataProvider('vapidProvider')]
public function testGetVapidHeaders(string $audience, array $vapid, string $contentEncoding, int $expiration, string $expectedAuthorization, ?string $expectedCryptoKey): void
{
$vapid = VAPID::validate($vapid);
$headers = VAPID::getVapidHeaders(
$audience,
$vapid['subject'],
$vapid['publicKey'],
$vapid['privateKey'],
$contentEncoding,
$expiration
);
$this->assertArrayHasKey('Authorization', $headers);
$this->assertEquals(Utils::safeStrlen($expectedAuthorization), Utils::safeStrlen($headers['Authorization']));
$this->assertEquals($this->explodeAuthorization($expectedAuthorization), $this->explodeAuthorization($headers['Authorization']));
if ($expectedCryptoKey) {
$this->assertArrayHasKey('Crypto-Key', $headers);
$this->assertEquals($expectedCryptoKey, $headers['Crypto-Key']);
} else {
$this->assertArrayNotHasKey('Crypto-Key', $headers);
}
}
private function explodeAuthorization(string $auth): array
{
$auth = explode('.', $auth);
array_pop($auth); // delete the signature which changes each time
return $auth;
}
public function testCreateVapidKeys(): void
{
$keys = VAPID::createVapidKeys();
$this->assertArrayHasKey('publicKey', $keys);
$this->assertArrayHasKey('privateKey', $keys);
$this->assertGreaterThanOrEqual(86, strlen($keys['publicKey']));
$this->assertGreaterThanOrEqual(42, strlen($keys['privateKey']));
}
}