Passed
Branch master (c73d10)
by Stefan
02:51 queued 55s
created

PNEncryptionTest::test_encrypt2()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 1
eloc 4
nc 1
nop 0
dl 0
loc 6
rs 10
c 1
b 0
f 1
1
<?php
2
declare(strict_types = 1);
3
4
namespace SKien\Test\PNServer;
5
6
use PHPUnit\Framework\TestCase;
0 ignored issues
show
Bug introduced by
The type PHPUnit\Framework\TestCase was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
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);
0 ignored issues
show
Bug introduced by
It seems like $result can also be of type false; however, parameter $string of strlen() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

31
        $this->assertTrue(strlen(/** @scrutinizer ignore-type */ $result) > 0);
Loading history...
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