-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathXorEncryptorTest.php
More file actions
162 lines (138 loc) · 5.49 KB
/
XorEncryptorTest.php
File metadata and controls
162 lines (138 loc) · 5.49 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
<?php declare(strict_types=1);
namespace Tests\FileEncryptor;
use PHPUnit\Framework\TestCase;
/**
* 测试 php-file-encryptor\FileEncryptor\XorEncryptor
*
* @author fdipzone
*/
final class XorEncryptorTest extends TestCase
{
// 定义用例用到的测试文件
private static $source_file = '/tmp/source_file.txt';
private static $encrypt_file = '/tmp/encrypt_file.txt';
private static $decrypt_file = '/tmp/decrypt_file.txt';
// 初始化测试文件
public static function setUpBeforeClass():void
{
file_put_contents(self::$source_file, 'abcdef');
}
// 删除测试文件
public static function tearDownAfterClass():void
{
if(file_exists(self::$source_file))
{
unlink(self::$source_file);
}
if(file_exists(self::$encrypt_file))
{
unlink(self::$encrypt_file);
}
if(file_exists(self::$decrypt_file))
{
unlink(self::$decrypt_file);
}
}
/**
* @covers \FileEncryptor\XorEncryptor::__construct
*/
public function testConstruct()
{
$encrypt_key = '123456';
$xor_encryptor = new \FileEncryptor\XorEncryptor($encrypt_key);
$this->assertEquals('FileEncryptor\XorEncryptor', get_class($xor_encryptor));
}
/**
* @covers \FileEncryptor\XorEncryptor::__construct
*/
public function testConstructException()
{
$this->expectException(\Exception::class);
$this->expectExceptionMessage('xor encryptor: encrypt key is empty');
new \FileEncryptor\XorEncryptor('');
}
/**
* @covers \FileEncryptor\XorEncryptor::encrypt
* @covers \FileEncryptor\XorEncryptor::decrypt
*/
public function testEncryptAndDecrypt()
{
$encrypt_key = '123456';
$xor_encryptor = new \FileEncryptor\XorEncryptor($encrypt_key);
// encrypt
$ret = $xor_encryptor->encrypt(self::$source_file, self::$encrypt_file);
$this->assertTrue($ret);
$this->assertSame(strlen(file_get_contents(self::$source_file)), strlen(file_get_contents(self::$encrypt_file)));
// decrypt
$ret = $xor_encryptor->decrypt(self::$encrypt_file, self::$decrypt_file);
$this->assertTrue($ret);
$this->assertSame(strlen(file_get_contents(self::$encrypt_file)), strlen(file_get_contents(self::$decrypt_file)));
$this->assertEquals(file_get_contents(self::$source_file), file_get_contents(self::$decrypt_file));
}
/**
* @covers \FileEncryptor\XorEncryptor::encrypt
*/
public function testEncryptParameterSourceFileException()
{
$this->expectException(\Exception::class);
$this->expectExceptionMessage('xor encryptor: source file not exists');
$encrypt_key = '123456';
$xor_encryptor = new \FileEncryptor\XorEncryptor($encrypt_key);
$xor_encryptor->encrypt('/tmp/not_exists_file.txt', self::$encrypt_file);
}
/**
* @covers \FileEncryptor\XorEncryptor::encrypt
*/
public function testEncryptParameterEncryptFileException()
{
$this->expectException(\Exception::class);
$this->expectExceptionMessage('xor encryptor: encrypt file is empty');
$encrypt_key = '123456';
$xor_encryptor = new \FileEncryptor\XorEncryptor($encrypt_key);
$xor_encryptor->encrypt(self::$source_file, '');
}
/**
* @covers \FileEncryptor\XorEncryptor::decrypt
*/
public function testDecryptParameterEncryptFileException()
{
$this->expectException(\Exception::class);
$this->expectExceptionMessage('xor encryptor: encrypt file not exists');
$encrypt_key = '123456';
$xor_encryptor = new \FileEncryptor\XorEncryptor($encrypt_key);
$xor_encryptor->decrypt('/tmp/not_exists_file.txt', self::$decrypt_file);
}
/**
* @covers \FileEncryptor\XorEncryptor::decrypt
*/
public function testDecryptParameterDecryptFileException()
{
$this->expectException(\Exception::class);
$this->expectExceptionMessage('xor encryptor: decrypt file is empty');
$encrypt_key = '123456';
$xor_encryptor = new \FileEncryptor\XorEncryptor($encrypt_key);
$xor_encryptor->decrypt(self::$encrypt_file, '');
}
/**
* @covers \FileEncryptor\XorEncryptor::xorEncrypt
*/
public function testXorEncrypt()
{
$encrypt_key = '123456';
$xor_encryptor = new \FileEncryptor\XorEncryptor($encrypt_key);
$encrypt_file = sprintf('/tmp/ut-%s-%s/encrypt_file.txt', md5(__CLASS__), date('YmdHis'));
$decrypt_file = sprintf('/tmp/ut-%s-%s/decrypt_file.txt', md5(__CLASS__), date('YmdHis'));
// encrypt
$ret = \Tests\Utils\PHPUnitExtension::callMethod($xor_encryptor, 'xorEncrypt', [self::$source_file, $encrypt_file]);
$this->assertTrue($ret);
$this->assertSame(strlen(file_get_contents(self::$source_file)), strlen(file_get_contents($encrypt_file)));
// decrypt
$ret = \Tests\Utils\PHPUnitExtension::callMethod($xor_encryptor, 'xorEncrypt', [$encrypt_file, $decrypt_file]);
$this->assertTrue($ret);
$this->assertSame(strlen(file_get_contents($encrypt_file)), strlen(file_get_contents($decrypt_file)));
$this->assertEquals(file_get_contents(self::$source_file), file_get_contents($decrypt_file));
// exception
$ret = \Tests\Utils\PHPUnitExtension::callMethod($xor_encryptor, 'xorEncrypt', ['/tmp/not_exists_file.txt', $decrypt_file]);
$this->assertFalse($ret);
}
}