forked from codeigniter4/CodeIgniter4
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEncryptionTest.php
More file actions
184 lines (149 loc) · 5.36 KB
/
Copy pathEncryptionTest.php
File metadata and controls
184 lines (149 loc) · 5.36 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
/**
* 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;
use CodeIgniter\Encryption\Exceptions\EncryptionException;
use CodeIgniter\Test\CIUnitTestCase;
use Config\Encryption as EncryptionConfig;
use Config\Services;
/**
* @internal
*
* @group Others
*/
final class EncryptionTest extends CIUnitTestCase
{
private Encryption $encryption;
protected function setUp(): void
{
$this->encryption = new Encryption();
}
/**
* __construct test
*
* Covers behavior with config encryption key set or not
*/
public function testConstructor()
{
// Assume no configuration from set_up()
$this->assertEmpty($this->encryption->key);
// Try with an empty value
$config = new EncryptionConfig();
$this->encryption = new Encryption($config);
$this->assertEmpty($this->encryption->key);
// try a different key
$ikm = 'Secret stuff';
$config->key = $ikm;
$this->encryption = new Encryption($config);
$this->assertSame($ikm, $this->encryption->key);
}
/**
* Covers behavior with invalid parameters
*/
public function testBadDriver()
{
$this->expectException(EncryptionException::class);
// ask for a bad driver
$config = new EncryptionConfig();
$config->driver = 'Bogus';
$config->key = 'anything';
$this->encryption->initialize($config);
}
/**
* Covers behavior with invalid parameters
*/
public function testMissingDriver()
{
$this->expectException(EncryptionException::class);
// ask for a bad driver
$config = new EncryptionConfig();
$config->driver = '';
$config->key = 'anything';
$this->encryption->initialize($config);
}
public function testKeyCreation()
{
$this->assertNotEmpty($this->encryption->createKey());
$this->assertSame(32, strlen($this->encryption->createKey()));
$this->assertSame(16, strlen($this->encryption->createKey(16)));
}
public function testServiceSuccess()
{
$config = new EncryptionConfig();
$config->driver = 'OpenSSL';
$config->key = 'anything';
$encrypter = Services::encrypter($config);
$this->assertInstanceOf(EncrypterInterface::class, $encrypter);
}
public function testServiceFailure()
{
$this->expectException(EncryptionException::class);
// ask for a bad driver
$config = new EncryptionConfig();
$config->driver = 'Kazoo';
$config->key = 'anything';
Services::encrypter($config);
}
public function testServiceWithoutKey()
{
$this->expectException(EncryptionException::class);
Services::encrypter();
}
public function testServiceShared()
{
$config = new EncryptionConfig();
$config->driver = 'OpenSSL';
$config->key = 'anything';
$encrypter = Services::encrypter($config, true);
$config->key = 'Abracadabra';
$encrypter = Services::encrypter($config, true);
$this->assertSame('anything', $encrypter->key);
}
public function testMagicIssetTrue()
{
$this->assertTrue(isset($this->encryption->digest));
}
public function testMagicIssetFalse()
{
$this->assertFalse(isset($this->encryption->bogus));
}
public function testMagicGet()
{
$this->assertSame('SHA512', $this->encryption->digest);
}
public function testMagicGetMissing()
{
$this->assertNull($this->encryption->bogus);
}
public function testDecryptEncryptedDataByCI3()
{
$config = new EncryptionConfig();
$config->driver = 'OpenSSL';
$config->key = hex2bin('64c70b0b8d45b80b9eba60b8b3c8a34d0193223d20fea46f8644b848bf7ce67f');
$config->rawData = false;
$config->encryptKeyInfo = 'encryption';
$config->authKeyInfo = 'authentication';
$encrypter = Services::encrypter($config, false);
$encrypted = 'f5eeb3f056b2dc5e8119b4a5f5ba793d724b9ca2d1ca23ab89bc72e51863f8da233a83ccb48d5daf3d6905d61f357877aaad32c8bc7a7c5e48f3268d2ba362b9UTw2A7U4CB9vb+6izrDzJHAdz1hAutIt2Ex2C2FqamJAXc8Z8RQor9UvaWy2';
$decrypted = $encrypter->decrypt($encrypted);
$expected = 'This is a plain-text message.';
$this->assertSame($expected, $decrypted);
}
public function testDecryptEncryptedDataByCI42()
{
$config = new EncryptionConfig();
$config->key = hex2bin('64c70b0b8d45b80b9eba60b8b3c8a34d0193223d20fea46f8644b848bf7ce67f');
$encrypter = Services::encrypter($config, false);
// Encrypted message by CI v4.2.0.
$encrypted = base64_decode('UB9PC3QfQIoLY5+/GU8BUQnfhEcCml6i4Sve6k0f8r6Id6IzlbkvMhfWf5E2lBH5+OTWuv5MUoTBQWv9Pd46ua07QsqS6/vHaW3rCg6cpLM/8d2IZE/VO+uXeaU6XHO5mJ8ehGKg96JITvKjxA==', true);
$decrypted = $encrypter->decrypt($encrypted);
$expected = 'This is a plain-text message.';
$this->assertSame($expected, $decrypted);
}
}