Passed
Push — master ( 34eaf6...149dd5 )
by Charles
02:52
created

RequestResponseTest::testv1EncryptDecrypt()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 26
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 15
dl 0
loc 26
rs 9.7666
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php declare(strict_types=1);
2
3
namespace ncryptf\Tests;
4
5
use ncryptf\Request;
6
use ncryptf\Response;
7
use ncryptf\Tests\AbstractTest;
8
9
class RequestResponseTest extends AbstractTest
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
    private $expectedCipher = '1odrjBif71zRcZidfhEzSb80rXGJGB1J3upTb+TwhpxmFjXOXjwSDw45e7p/+FW4Y0/FDuLjHfGghOG0UC7j4xmX8qIVYUdbKCB/dLn34HQ0D0NIM6N9Qj83bpS5XgK1o+luonc0WxqA3tdXTcgkd2D+cSSSotJ/s+5fqN3w5xsKc7rKb1p3MpvRzyEmdNgJCFOk8EErn0bolz9LKyPEO0A2Mnkzr19bDwsgD1DGEYlo0i9KOw06RpaZRz2J+OJ+EveIlQGDdLT8Gh+nv65TOKJqCswOly0=';
29
    private $expectedSignature = 'dcvJclMxEx7pcW/jeVm0mFHGxVksY6h0/vNkZTfVf+wftofnP+yDFdrNs5TtZ+FQ0KEOm6mm9XUMXavLaU9yDg==';
30
31
    private $expectedv2Cipher = '3iWQAm7pUZyrfwb8J8IgjAS73UTOfsjRT9/FLTo569CkMuhiesfnkGvsDcHR3o2aPL2OVTcmWOTX8AY11odrjBif71zRcZidfhEzSb80rXGJGB1J3upTb+TwhpxmFjXOXjwSDw45e7p/+FW4Y0/FDuLjHfGghOG0UC7j4xmX8qIVYUdbKCB/dLn34HQ0D0NIM6N9Qj83bpS5XgK1o+luonc0WxqA3tdXTcgkd2D+cSSSotJ/s+5fqN3w5xsKc7rKb1p3MpvRzyEmdNgJCFOk8EErn0bolz9LKyPEO0A2Mnkzr19bDwsgD1DGEYlo0i9KOw06RpaZRz2J+OJ+EveIlQGDdLT8Gh+nv65TOKJqCswOly0i42NKK/654zGtxTSOcNHPEwtFAz0A4k0hwlIFopZEsXXLyXJTMRMe6XFv43lZtJhRxsVZLGOodP7zZGU31X/sH7aH5z/sgxXazbOU7WfhUNChDpuppvV1DF2ry2lPcg4SwqYwa53inoY2+eCPP4Hkp/PKhSOEMFlWV+dlQirn6GGf5RQSsQ7ti/QCvi/BRIhb3ZHiPptZJZIbYwqIpvYu';
32
33
    private $payload = <<<JSON
34
{
35
    "foo": "bar",
36
    "test": {
37
        "true": false,
38
        "zero": 0.0,
39
        "a": 1,
40
        "b": 3.14,
41
        "nil": null,
42
        "arr": [
43
            "a", "b", "c", "d"
44
        ]
45
    }
46
}
47
JSON;
48
49
    public function testv2EncryptDecrypt()
50
    {
51
        $request = new Request(
52
            \base64_decode($this->clientKeyPair['secret']),
53
            \base64_decode($this->signatureKeyPair['secret'])
54
        );
55
56
        $cipher = $request->encrypt($this->payload, \base64_decode($this->serverKeyPair['public']), 2, \base64_decode($this->nonce));
57
58
        $this->assertEquals($this->expectedv2Cipher, \base64_encode($cipher));
59
60
        $response = new Response(
61
            \base64_decode($this->serverKeyPair['secret'])
62
        );
63
64
        $plain = $response->decrypt($cipher);
65
66
        $this->assertEquals($this->payload, $plain);
67
    }
68
69
    public function testDecryptEmptyString()
70
    {
71
        $request = new Request(
72
            \base64_decode($this->clientKeyPair['secret']),
73
            \base64_decode($this->signatureKeyPair['secret'])
74
        );
75
76
        $cipher = $request->encrypt('', \base64_decode($this->serverKeyPair['public']));
77
78
        $response = new Response(
79
            \base64_decode($this->serverKeyPair['secret'])
80
        );
81
82
        $plain = $response->decrypt($cipher);
83
84
        $this->assertEquals('', $plain);
85
    }
86
87
    public function testv1EncryptDecrypt()
88
    {
89
        $request = new Request(
90
            \base64_decode($this->clientKeyPair['secret']),
91
            \base64_decode($this->signatureKeyPair['secret'])
92
        );
93
94
        $cipher = $request->encrypt($this->payload, \base64_decode($this->serverKeyPair['public']), 1, \base64_decode($this->nonce));
95
96
        $signature = $request->sign($this->payload);
97
98
        $this->assertEquals($this->expectedCipher, \base64_encode($cipher));
99
        $this->assertEquals($this->expectedSignature, \base64_encode($signature));
100
101
        $response = new Response(
102
            \base64_decode($this->serverKeyPair['secret'])
103
        );
104
105
        $plain = $response->decrypt($cipher, \base64_decode($this->clientKeyPair['public']), \base64_decode($this->nonce));
106
107
        $this->assertEquals($this->payload, $plain);
108
109
        $this->assertTrue($response->isSignatureValid(
110
            $this->payload,
111
            $signature,
112
            \base64_decode($this->signatureKeyPair['public'])
113
        ));
114
    }
115
116
    public function testPublicKeyExtraction()
117
    {
118
        $publicKey = Response::getPublicKeyFromResponse(\base64_decode($this->expectedv2Cipher));
119
        $this->assertEquals(\base64_decode($this->clientKeyPair['public']), $publicKey);
120
    }
121
122
    public function testVersion()
123
    {
124
        $this->assertEquals(1, Response::getVersion(\base64_decode($this->expectedCipher)));
125
        $this->assertEquals(2, Response::getVersion(\base64_decode($this->expectedv2Cipher)));
126
    }
127
}
128