forked from absolute-quantum/DoctrineEncryptBundle
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathHaliteEncryptorTest.php
More file actions
61 lines (48 loc) · 1.93 KB
/
Copy pathHaliteEncryptorTest.php
File metadata and controls
61 lines (48 loc) · 1.93 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
<?php
namespace Ambta\DoctrineEncryptBundle\Tests\Unit\Encryptors;
use Ambta\DoctrineEncryptBundle\Encryptors\HaliteEncryptor;
use PHPUnit\Framework\TestCase;
class HaliteEncryptorTest extends TestCase
{
private const DATA = 'foobar';
public function testEncryptExtension(): void
{
if (! extension_loaded('sodium')) {
$this->markTestSkipped('This test only runs when the sodium extension is enabled.');
}
$keyfile = __DIR__.'/fixtures/halite.key';
$key = file_get_contents($keyfile);
$halite = new HaliteEncryptor($keyfile);
$encrypted = $halite->encrypt(self::DATA);
$this->assertNotSame(self::DATA, $encrypted);
$decrypted = $halite->decrypt($encrypted);
$this->assertSame(self::DATA, $decrypted);
$newkey = file_get_contents($keyfile);
$this->assertSame($key, $newkey, 'The key must not be modified');
}
public function testGenerateKey(): void
{
if (! extension_loaded('sodium')) {
$this->markTestSkipped('This test only runs when the sodium extension is enabled.');
}
$keyfile = sys_get_temp_dir().'/halite-'.md5(time());
if (file_exists($keyfile)) {
unlink($keyfile);
}
$halite = new HaliteEncryptor($keyfile);
$halite->encrypt(self::DATA);
$this->assertFileExists($keyfile);
$this->assertNotEmpty(file_get_contents($keyfile), 'A key should have been created and saved to the file');
unlink($keyfile);
}
public function testEncryptWithoutExtensionThrowsException(): void
{
if (extension_loaded('sodium')) {
$this->markTestSkipped('This only runs when the sodium extension is disabled.');
}
$keyfile = __DIR__.'/fixtures/halite.key';
$halite = new HaliteEncryptor($keyfile);
$this->expectException(\SodiumException::class);
$halite->encrypt(self::DATA);
}
}