Passed
Push — master ( 2e741c...2e2d05 )
by Charles
07:03
created

RequestResponseTest::testEncryptDecrypt()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 39
Code Lines 28

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 28
nc 1
nop 0
dl 0
loc 39
rs 9.472
c 0
b 0
f 0
1
<?php declare(strict_types=1);
2
3
namespace ncryptf\Tests;
4
5
use ncryptf\Request;
6
use ncryptf\Response;
7
use PHPUnit\Framework\TestCase;
8
9
class RequestResponseTest extends TestCase
10
{
11
    private $clientKeyPair = [
12
        'secret' => 'bvV/vnfB43spmprI8aBK/Fd8xxSBlx7EhuxfxxTVI2o=',
13
        'public' => 'Ojnr0KQy6GJ6x+eQa+wNwdHejZo8vY5VNyZY5NfwBjU='
14
    ];
15
16
    private $serverKeyPair = [
17
        'secret' => 'gH1+ileX1W5fMeOWue8HxdREnK04u72ybxCQgivWoZ4=',
18
        'public' => 'YU74X2OqHujLVDH9wgEHscD5eyiLPvcugRUZG6R3BB8='
19
    ];
20
21
    private $signatureKeyPair = [
22
        'secret' => '9wdUWlSW2ZQB6ImeUZ5rVqcW+mgQncN1Cr5D2YvFdvEi42NKK/654zGtxTSOcNHPEwtFAz0A4k0hwlIFopZEsQ==',
23
        'public' => 'IuNjSiv+ueMxrcU0jnDRzxMLRQM9AOJNIcJSBaKWRLE='
24
    ];
25
    
26
    private $nonce = 'bulRnKt/BvwnwiCMBLvdRM5+yNFP38Ut';
27
28
    public function testEncryptDecrypt()
29
    {
30
        $payload = <<<JSON
31
{
32
    "foo": "bar",
33
    "test": {
34
        "true": false,
35
        "a": 1,
36
        "b": 3.14,
37
        "nil": null,
38
        "arr": [
39
            "a", "b", "c", "d"
40
        ]
41
    }
42
}
43
JSON;
44
45
        $request = new Request(
46
            \base64_decode($this->clientKeyPair['secret']),
47
            \base64_decode($this->serverKeyPair['public'])
48
        );
49
50
        $cipher = $request->encrypt($payload, \base64_decode($this->nonce));
51
52
        $signature = $request->sign($payload, \base64_decode($this->signatureKeyPair['secret']));
53
54
        $response = new Response(
55
            \base64_decode($this->serverKeyPair['secret']),
56
            \base64_decode($this->clientKeyPair['public'])
57
        );
58
59
        $plain = $response->decrypt($cipher, \base64_decode($this->nonce));
60
61
        $this->assertEquals($payload, $plain);
62
63
        $this->assertTrue($response->isSignatureValid(
64
            $payload,
65
            $signature,
66
            \base64_decode($this->signatureKeyPair['public'])
67
        ));
68
    }
69
}
70