-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathOpenSSLEncryptionTest.php
151 lines (95 loc) · 4.04 KB
/
OpenSSLEncryptionTest.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
<?php
declare(strict_types=1);
namespace PHPSess\Tests;
use PHPSess\Encryption\OpenSSLEncryption;
use PHPSess\Exception\UnableToHashException;
use PHPSess\Exception\UnableToEncryptException;
use PHPSess\Exception\UnableToDecryptException;
use PHPUnit\Framework\TestCase;
final class OpenSSLEncryptionTest extends TestCase
{
public function testThrowErrorUnknownHash()
{
$encryption = new OpenSSLEncryption('appKey');
$this->expectException(UnableToHashException::class);
$encryption->setHashAlgorithm('unknown_hash_algo');
}
public function testThrowErrorUnknownEncryption()
{
$encryption = new OpenSSLEncryption('appKey');
$this->expectException(UnableToEncryptException::class);
$encryption->setEncryptionAlgorithm('unknown_encryption_algo');
}
public function testIdentifierDifferentFromSid()
{
$crypt_provider = new OpenSSLEncryption('appKey');
$session_id = 'test_id';
$identifier = $crypt_provider->makeSessionIdentifier($session_id);
$this->assertNotEquals($session_id, $identifier);
}
public function testEncryptedDataDifferentFromData()
{
$crypt_provider = new OpenSSLEncryption('appKey');
$session_id = 'test_id';
$data = 'test_data';
$encrypted_data = $crypt_provider->encryptSessionData($session_id, $data);
$this->assertNotEquals($data, $encrypted_data);
}
public function testCanDecryptEncryptedData()
{
$crypt_provider = new OpenSSLEncryption('appKey');
$session_id = 'test_id';
$data = 'test_data';
$encrypted_data = $crypt_provider->encryptSessionData($session_id, $data);
$decrypted_data = $crypt_provider->decryptSessionData($session_id, $encrypted_data);
$this->assertEquals($data, $decrypted_data);
}
public function testCantDecryptWithWrongSessionId()
{
$crypt_provider = new OpenSSLEncryption('appKey');
$data = 'test_data';
$encrypted_data = $crypt_provider->encryptSessionData('original_session_id', $data);
$this->expectException(UnableToDecryptException::class);
$crypt_provider->decryptSessionData('wrong_session_id', $encrypted_data);
}
public function testCanDecryptWithNewInstance()
{
$app_key = 'appKey';
$crypt_provider = new OpenSSLEncryption($app_key);
$session_id = 'test_id';
$data = 'test_data';
$encrypted_data = $crypt_provider->encryptSessionData($session_id, $data);
$new_crypt_provider = new OpenSSLEncryption($app_key);
$decrypted_data = $new_crypt_provider->decryptSessionData($session_id, $encrypted_data);
$this->assertEquals($data, $decrypted_data);
}
public function testCantDecryptWithWrongKey()
{
$crypt_provider = new OpenSSLEncryption('original_key');
$session_id = 'test_id';
$data = 'test_data';
$encrypted_data = $crypt_provider->encryptSessionData($session_id, $data);
$new_crypt_provider = new OpenSSLEncryption('wrong_key');
$this->expectException(UnableToDecryptException::class);
$new_crypt_provider->decryptSessionData($session_id, $encrypted_data);
}
public function testThrowExceptionWithUnparsableJson()
{
$crypt_provider = new OpenSSLEncryption('appKey');
$this->expectException(UnableToDecryptException::class);
$crypt_provider->decryptSessionData('aSessionId', '{some: unparsable: json}');
}
public function testDecryptEmptyData()
{
$crypt_provider = new OpenSSLEncryption('appKey');
$data = $crypt_provider->decryptSessionData('aSessionId', '');
$this->assertEquals('', $data);
}
public function testWrongInitVector()
{
$data = json_encode(['data' => 'test', 'initVector' => 'wrong Init-Vector']);
$crypt_provider = new OpenSSLEncryption('appKey');
$this->expectException(UnableToDecryptException::class);
$crypt_provider->decryptSessionData('aSessionId', $data);
}
}