Passed
Push — master ( 34c6af...d5f109 )
by Charles
02:18
created

RequestResponseTest::testVersion()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 2
dl 0
loc 4
rs 10
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 testv1EncryptDecrypt()
70
    {
71
        $request = new Request(
72
            \base64_decode($this->clientKeyPair['secret']),
73
            \base64_decode($this->signatureKeyPair['secret'])
74
        );
75
76
        $cipher = $request->encrypt($this->payload, \base64_decode($this->serverKeyPair['public']), 1, \base64_decode($this->nonce));
77
78
        $signature = $request->sign($this->payload);
79
        
80
        $this->assertEquals($this->expectedCipher, \base64_encode($cipher));
81
        $this->assertEquals($this->expectedSignature, \base64_encode($signature));
82
83
        $response = new Response(
84
            \base64_decode($this->serverKeyPair['secret'])
85
        );
86
87
        $plain = $response->decrypt($cipher, \base64_decode($this->clientKeyPair['public']), \base64_decode($this->nonce));
88
89
        $this->assertEquals($this->payload, $plain);
90
91
        $this->assertTrue($response->isSignatureValid(
92
            $this->payload,
93
            $signature,
94
            \base64_decode($this->signatureKeyPair['public'])
95
        ));
96
    }
97
98
    public function testPublicKeyExtraction()
99
    {
100
        $publicKey = Response::getPublicKeyFromResponse(\base64_decode($this->expectedv2Cipher));
101
        $this->assertEquals(\base64_decode($this->clientKeyPair['public']), $publicKey);
102
    }
103
104
    public function testVersion()
105
    {
106
        $this->assertEquals(1, Response::getVersion(\base64_decode($this->expectedCipher)));
107
        $this->assertEquals(2, Response::getVersion(\base64_decode($this->expectedv2Cipher)));
108
    }
109
}
110