|
1
|
|
|
<?php |
|
2
|
|
|
declare(strict_types=1); |
|
3
|
|
|
|
|
4
|
|
|
namespace SKien\Test\PNServer; |
|
5
|
|
|
|
|
6
|
|
|
use PHPUnit\Framework\TestCase; |
|
7
|
|
|
use SKien\PNServer\PNEncryption; |
|
8
|
|
|
|
|
9
|
|
|
/** |
|
10
|
|
|
* @author Stefanius <[email protected]> |
|
11
|
|
|
* @copyright MIT License - see the LICENSE file for details |
|
12
|
|
|
*/ |
|
13
|
|
|
class PNEncryptionTest extends TestCase |
|
14
|
|
|
{ |
|
15
|
|
|
use TestHelperTrait; |
|
16
|
|
|
|
|
17
|
|
|
const VALID_P256H = "BEQrfuNX-ZrXPf0Mm-IdVMO1LMpu5N3ifgcyeUD2nYwuUhRUDmn_wVOM3eQyYux5vW2B8-TyTYco4-bFKKR02IA"; |
|
18
|
|
|
const VALID_AUTH = "jOfywakW_srfHhMF-NiZ3Q"; |
|
19
|
|
|
const VALID_ENCODING = "aesgcm"; |
|
20
|
|
|
|
|
21
|
|
|
public function test_encrypt1() : void |
|
22
|
|
|
{ |
|
23
|
|
|
$enc = new PNEncryption(self::VALID_P256H, self::VALID_AUTH); |
|
24
|
|
|
|
|
25
|
|
|
$result = $enc->encrypt(''); |
|
26
|
|
|
$this->assertTrue(is_string($result)); |
|
27
|
|
|
$this->assertEquals('', $result); |
|
28
|
|
|
|
|
29
|
|
|
$result = $enc->encrypt('my payload'); |
|
30
|
|
|
$this->assertTrue(is_string($result)); |
|
31
|
|
|
$this->assertTrue(strlen($result) > 0); |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
|
|
public function test_encrypt2() : void |
|
35
|
|
|
{ |
|
36
|
|
|
$enc = new PNEncryption(self::VALID_P256H, self::VALID_AUTH, "aes128gcm"); |
|
37
|
|
|
$result = $enc->encrypt('my payload'); |
|
38
|
|
|
$this->assertTrue(is_string($result)); |
|
39
|
|
|
$this->assertNotEmpty($result); |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
public function test_encryptError1() : void |
|
43
|
|
|
{ |
|
44
|
|
|
$sub = json_decode($this->loadSubscription('invalid_subscription.json')); |
|
45
|
|
|
$enc = new PNEncryption($sub->keys->p256dh, $sub->keys->auth); |
|
46
|
|
|
$this->assertFalse($enc->encrypt('my payload')); |
|
47
|
|
|
$this->assertNotEmpty($enc->getError()); |
|
48
|
|
|
$this->assertEquals("Invalid client public key length!", $enc->getError()); |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
public function test_encryptError2() : void |
|
52
|
|
|
{ |
|
53
|
|
|
$enc = new PNEncryption(self::VALID_P256H, self::VALID_AUTH, 'invalidencoding'); |
|
54
|
|
|
$this->assertFalse($enc->encrypt('my payload')); |
|
55
|
|
|
$this->assertNotEmpty($enc->getError()); |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
public function test_getHeaders1() : void |
|
59
|
|
|
{ |
|
60
|
|
|
$enc = new PNEncryption(self::VALID_P256H, self::VALID_AUTH); |
|
61
|
|
|
|
|
62
|
|
|
$enc->encrypt(''); |
|
63
|
|
|
$headers = $enc->getHeaders(); |
|
64
|
|
|
$this->assertTrue(is_array($headers)); |
|
65
|
|
|
$this->assertEquals(0, count($headers)); |
|
66
|
|
|
|
|
67
|
|
|
$enc->encrypt('my payload'); |
|
68
|
|
|
$headers = $enc->getHeaders(); |
|
69
|
|
|
$this->assertTrue(is_array($headers)); |
|
70
|
|
|
$this->assertArrayHasKey('Content-Type', $headers); |
|
71
|
|
|
$this->assertEquals('application/octet-stream', $headers['Content-Type']); |
|
72
|
|
|
$this->assertArrayHasKey('Content-Encoding', $headers); |
|
73
|
|
|
$this->assertEquals("aesgcm", $headers['Content-Encoding']); |
|
74
|
|
|
$this->assertArrayHasKey('Encryption', $headers); |
|
75
|
|
|
$this->assertTrue(strlen($headers['Encryption']) > 0); |
|
76
|
|
|
$this->assertArrayHasKey('Crypto-Key', $headers); |
|
77
|
|
|
$this->assertTrue(strlen($headers['Crypto-Key']) > 0); |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
public function test_getHeaders2() : void |
|
81
|
|
|
{ |
|
82
|
|
|
$enc = new PNEncryption(self::VALID_P256H, self::VALID_AUTH); |
|
83
|
|
|
$enc->encrypt('my payload'); |
|
84
|
|
|
// with existing header to merge with |
|
85
|
|
|
$headers = $enc->getHeaders(['Crypto-Key' => 'testkey']); |
|
86
|
|
|
$this->assertEquals(';testkey', substr($headers['Crypto-Key'], -8)); |
|
87
|
|
|
} |
|
88
|
|
|
} |
|
89
|
|
|
|