Repository navigation
Expand file tree
/
Copy pathOpenSSLHandlerTest.php
More file actions
184 lines (151 loc) · 5.89 KB
/
Copy pathOpenSSLHandlerTest.php
File metadata and controls
184 lines (151 loc) · 5.89 KB
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
<?php
declare(strict_types=1);
/**
* This file is part of CodeIgniter 4 framework.
*
* (c) CodeIgniter Foundation <admin@codeigniter.com>
*
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*/
namespace CodeIgniter\Encryption\Handlers;
use CodeIgniter\Encryption\Encryption;
use CodeIgniter\Encryption\Exceptions\EncryptionException;
use CodeIgniter\Test\CIUnitTestCase;
use Config\Encryption as EncryptionConfig;
use PHPUnit\Framework\Attributes\Group;
use Tests\Support\Encryption\WithoutEncryptionEnvTrait;
/**
* @internal
*/
#[Group('Others')]
final class OpenSSLHandlerTest extends CIUnitTestCase
{
use WithoutEncryptionEnvTrait;
private Encryption $encryption;
public static function setUpBeforeClass(): void
{
parent::setUpBeforeClass();
self::setUpBeforeClassWithoutEncryptionEnv();
}
public static function tearDownAfterClass(): void
{
parent::tearDownAfterClass();
self::tearDownAfterClassWithoutEncryptionEnv();
}
protected function setUp(): void
{
if (! extension_loaded('openssl')) {
$this->markTestSkipped('OpenSSL is not available.');
}
$this->encryption = new Encryption();
}
/**
* Sanity test
*/
public function testSanity(): void
{
$params = new EncryptionConfig();
$params->driver = 'OpenSSL';
$params->key = 'Something other than an empty string';
$encrypter = $this->encryption->initialize($params);
$this->assertInstanceOf(OpenSSLHandler::class, $encrypter);
$this->assertSame('AES-256-CTR', $encrypter->cipher);
$this->assertSame('Something other than an empty string', $encrypter->key);
}
/**
* initialize(), encrypt(), decrypt() test
*
* Testing the three methods separately is not realistic as they are
* designed to work together.
*/
public function testSimple(): void
{
$params = new EncryptionConfig();
$params->driver = 'OpenSSL';
$params->key = '\xd0\xc9\x08\xc4\xde\x52\x12\x6e\xf8\xcc\xdb\x03\xea\xa0\x3a\x5c';
// Default state (AES-256/Rijndael-256 in CTR mode)
$encrypter = $this->encryption->initialize($params);
$this->assertInstanceOf(OpenSSLHandler::class, $encrypter);
// Was the key properly set?
$this->assertSame($params->key, $encrypter->key);
// simple encrypt/decrypt, default parameters
$message1 = 'This is a plain-text message.';
$this->assertSame($message1, $encrypter->decrypt($encrypter->encrypt($message1)));
$message2 = 'This is a different plain-text message.';
$this->assertSame($message2, $encrypter->decrypt($encrypter->encrypt($message2)));
$this->assertNotSame($message2, $encrypter->decrypt($encrypter->encrypt($message1)));
}
/**
* Starter key needed
*/
public function testWithoutKey(): void
{
$this->expectException(EncryptionException::class);
$encrypter = new OpenSSLHandler();
$message1 = 'This is a plain-text message.';
$encrypter->encrypt($message1, ['key' => '']);
}
public function testWithKeyString(): void
{
$key = 'abracadabra';
$encrypter = new OpenSSLHandler();
$message1 = 'This is a plain-text message.';
$encoded = $encrypter->encrypt($message1, $key);
$this->assertSame($message1, $encrypter->decrypt($encoded, $key));
}
/**
* Authentication will fail decrypting with the wrong key
*/
public function testWithWrongKeyString(): void
{
$this->expectException(EncryptionException::class);
$key1 = 'abracadabra';
$encrypter = new OpenSSLHandler();
$message1 = 'This is a plain-text message.';
$encoded = $encrypter->encrypt($message1, $key1);
$this->assertNotSame($message1, $encoded);
$key2 = 'Holy cow, batman!';
$this->assertNotSame($message1, $encrypter->decrypt($encoded, $key2));
}
public function testWithKeyArray(): void
{
$key = 'abracadabra';
$encrypter = new OpenSSLHandler();
$message1 = 'This is a plain-text message.';
$encoded = $encrypter->encrypt($message1, ['key' => $key]);
$this->assertSame($message1, $encrypter->decrypt($encoded, ['key' => $key]));
}
/**
* Authentication will fail decrypting with the wrong key
*/
public function testWithWrongKeyArray(): void
{
$this->expectException(EncryptionException::class);
$key1 = 'abracadabra';
$encrypter = new OpenSSLHandler();
$message1 = 'This is a plain-text message.';
$encoded = $encrypter->encrypt($message1, ['key' => $key1]);
$this->assertNotSame($message1, $encoded);
$key2 = 'Holy cow, batman!';
$this->assertNotSame($message1, $encrypter->decrypt($encoded, ['key' => $key2]));
}
public function testInternalKeyNotModifiedByParams(): void
{
$params = new EncryptionConfig();
$params->driver = 'OpenSSL';
$params->key = 'original-key-value';
$encrypter = $this->encryption->initialize($params);
$this->assertInstanceOf(OpenSSLHandler::class, $encrypter);
$key = $encrypter->key;
$this->assertSame('original-key-value', $key);
$message = 'This is a plain-text message.';
$differentKey = 'temporary-param-key';
$encoded = $encrypter->encrypt($message, ['key' => $differentKey]);
$this->assertSame('original-key-value', $encrypter->key);
$message2 = 'Another message.';
$encoded2 = $encrypter->encrypt($message2);
$this->assertSame($message2, $encrypter->decrypt($encoded2));
$this->assertSame($message, $encrypter->decrypt($encoded, ['key' => $differentKey]));
}
}