forked from codeigniter4/CodeIgniter4
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSodiumHandlerTest.php
More file actions
154 lines (119 loc) · 5.1 KB
/
SodiumHandlerTest.php
File metadata and controls
154 lines (119 loc) · 5.1 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
<?php
declare(strict_types=1);
/**
* This file is part of CodeIgniter 4 framework.
*
* (c) CodeIgniter Foundation <[email protected]>
*
* 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;
/**
* @internal
*/
#[Group('Others')]
final class SodiumHandlerTest extends CIUnitTestCase
{
private Encryption $encryption;
private EncryptionConfig $config;
protected function setUp(): void
{
if (! extension_loaded('sodium')) {
$this->markTestSkipped('Libsodium is not available.');
}
parent::setUp();
$this->config = new EncryptionConfig();
$this->config->driver = 'Sodium';
$this->config->key = sodium_crypto_secretbox_keygen();
$this->encryption = new Encryption($this->config);
}
public function testPropertiesGetter(): void
{
$this->config->key = sodium_crypto_secretbox_keygen();
$this->config->blockSize = 256;
$encrypter = $this->encryption->initialize($this->config);
$this->assertSame($this->config->key, $encrypter->key);
$this->assertSame($this->config->blockSize, $encrypter->blockSize);
$this->assertNull($encrypter->driver);
}
public function testEmptyKeyThrowsErrorOnInitialize(): void
{
$this->expectException(EncryptionException::class);
$this->config->key = '';
$this->encryption->initialize($this->config);
}
public function testEmptyKeyThrowsErrorOnEncrypt(): void
{
$this->expectException(EncryptionException::class);
$encrypter = $this->encryption->initialize($this->config);
$encrypter->encrypt('Some message to encrypt', '');
}
public function testInvalidBlockSizeThrowsErrorOnEncrypt(): void
{
$this->expectException(EncryptionException::class);
$this->config->blockSize = -1;
$encrypter = $this->encryption->initialize($this->config);
$encrypter->encrypt('Some message.');
}
public function testHandlerCanBeReusedAfterEncryption(): void
{
$encrypter = $this->encryption->initialize($this->config);
$message = 'Some message to encrypt';
$ciphertext = $encrypter->encrypt($message);
$plaintext = $encrypter->decrypt($ciphertext);
$this->assertSame($message, $plaintext);
// Should also work for another encryption
$message2 = 'Another message';
$ciphertext2 = $encrypter->encrypt($message2);
$plaintext2 = $encrypter->decrypt($ciphertext2);
$this->assertSame($message2, $plaintext2);
}
public function testInvalidBlockSizeThrowsErrorOnDecrypt(): void
{
$this->expectException(EncryptionException::class);
$key = $this->config->key;
$encrypter = $this->encryption->initialize($this->config);
$ciphertext = $encrypter->encrypt('Some message.');
// After encrypt, the message and key are wiped from buffer.
$encrypter->decrypt($ciphertext, ['key' => $key, 'blockSize' => 0]);
}
public function testTruncatedMessageThrowsErrorOnDecrypt(): void
{
$this->expectException(EncryptionException::class);
$encrypter = $this->encryption->initialize($this->config);
$ciphertext = $encrypter->encrypt('Some message to encrypt');
$truncated = mb_substr($ciphertext, 0, 24, '8bit');
$encrypter->decrypt($truncated, ['blockSize' => 256, 'key' => sodium_crypto_secretbox_keygen()]);
}
public function testDecryptingMessages(): void
{
$key = sodium_crypto_secretbox_keygen();
$msg = 'A plaintext message for you.';
$this->config->key = $key;
$encrypter = $this->encryption->initialize($this->config);
$ciphertext = $encrypter->encrypt($msg);
$this->assertSame($msg, $encrypter->decrypt($ciphertext, $key));
$this->assertNotSame('A plain-text message for you.', $encrypter->decrypt($ciphertext, $key));
}
public function testInternalKeyNotModifiedByParams(): void
{
$originalKey = sodium_crypto_secretbox_keygen();
$this->config->key = $originalKey;
$encrypter = $this->encryption->initialize($this->config);
$this->assertSame($originalKey, $encrypter->key);
$message = 'This is a plain-text message.';
$differentKey = sodium_crypto_secretbox_keygen();
$encoded = $encrypter->encrypt($message, ['key' => $differentKey]);
$this->assertSame($originalKey, $encrypter->key);
$message2 = 'Another message.';
$encoded2 = $encrypter->encrypt($message2);
$this->assertSame($message2, $encrypter->decrypt($encoded2, ['key' => $originalKey]));
$this->assertSame($message, $encrypter->decrypt($encoded, ['key' => $differentKey]));
}
}