Completed
Push — master ( d865ae...34c801 )
by Charles
03:27
created

RequestResponseTest   A

Complexity

Total Complexity 2

Size/Duplication

Total Lines 103
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 2
eloc 70
dl 0
loc 103
c 0
b 0
f 0
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A testv2EncryptDecrypt() 0 35 1
A testv1EncryptDecrypt() 0 43 1
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 = '7VIJIG7pUZyrfwb8J8IgjAS73UTOfsjRT9/FLWFO+F9jqh7oy1Qx/cIBB7HA+Xsoiz73LoEVGRukdwQf1odrjBif71zRcZidfhEzSb80rXGJGB1J3upTb+TwhpxmFjXOXjwSDw45e7p/+FW4Y0/FDuLjHfGghOG0UC7j4xmX8qIVYUdbKCB/dLn34HQ0D0NIM6N9Qj83bpS5XgK1o+luonc0WxqA3tdXTcgkd2D+cSSSotJ/s+5fqN3w5xsKc7rKb1p3MpvRzyEmdNgJCFOk8EErn0bolz9LKyPEO0A2Mnkzr19bDwsgD1DGEYlo0i9KOw06RpaZRz2J+OJ+EveIlQGDdLT8Gh+nv65TOKJqCswOly0i42NKK/654zGtxTSOcNHPEwtFAz0A4k0hwlIFopZEsXXLyXJTMRMe6XFv43lZtJhRxsVZLGOodP7zZGU31X/sH7aH5z/sgxXazbOU7WfhUNChDpuppvV1DF2ry2lPcg41/K6NkVYeH5RWFt9WNQ6+8d/qvQ/odHD9pJq86ejU8SldDGDqAfwMmdQh9AHUJIbtxlOg9cNUWCNYX8wQ9qbz';
32
    public function testv2EncryptDecrypt()
33
    {
34
        $payload = <<<JSON
35
{
36
    "foo": "bar",
37
    "test": {
38
        "true": false,
39
        "zero": 0.0,
40
        "a": 1,
41
        "b": 3.14,
42
        "nil": null,
43
        "arr": [
44
            "a", "b", "c", "d"
45
        ]
46
    }
47
}
48
JSON;
49
50
        $request = new Request(
51
            \base64_decode($this->clientKeyPair['secret']),
52
            \base64_decode($this->serverKeyPair['public'])
53
        );
54
55
        $cipher = $request->encrypt($payload, \base64_decode($this->signatureKeyPair['secret']), \base64_decode($this->nonce));
56
57
        $this->assertEquals($this->expectedv2Cipher, \base64_encode($cipher));
58
        
59
        $response = new Response(
60
            \base64_decode($this->serverKeyPair['secret']),
61
            \base64_decode($this->clientKeyPair['public'])
62
        );
63
64
        $plain = $response->decrypt($cipher);
65
66
        $this->assertEquals($payload, $plain);
67
    }
68
69
    public function testv1EncryptDecrypt()
70
    {
71
        $payload = <<<JSON
72
{
73
    "foo": "bar",
74
    "test": {
75
        "true": false,
76
        "zero": 0.0,
77
        "a": 1,
78
        "b": 3.14,
79
        "nil": null,
80
        "arr": [
81
            "a", "b", "c", "d"
82
        ]
83
    }
84
}
85
JSON;
86
87
        $request = new Request(
88
            \base64_decode($this->clientKeyPair['secret']),
89
            \base64_decode($this->serverKeyPair['public'])
90
        );
91
92
        $cipher = $request->encrypt($payload, null, \base64_decode($this->nonce), 1);
93
94
        $signature = $request->sign($payload, \base64_decode($this->signatureKeyPair['secret']));
95
        
96
        $this->assertEquals($this->expectedCipher, \base64_encode($cipher));
97
        $this->assertEquals($this->expectedSignature, \base64_encode($signature));
98
99
        $response = new Response(
100
            \base64_decode($this->serverKeyPair['secret']),
101
            \base64_decode($this->clientKeyPair['public'])
102
        );
103
104
        $plain = $response->decrypt($cipher, \base64_decode($this->nonce));
105
106
        $this->assertEquals($payload, $plain);
107
108
        $this->assertTrue($response->isSignatureValid(
109
            $payload,
110
            $signature,
111
            \base64_decode($this->signatureKeyPair['public'])
112
        ));
113
    }
114
}
115