-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCryptoUtilsTest.php
More file actions
130 lines (118 loc) · 3.49 KB
/
CryptoUtilsTest.php
File metadata and controls
130 lines (118 loc) · 3.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
<?php declare(strict_types=1);
namespace Tests\Csrf;
use PHPUnit\Framework\TestCase;
/**
* 测试 php-csrf\Csrf\CryptoUtils
*
* @author fdipzone
*/
final class CryptoUtilsTest extends TestCase
{
/**
* @covers \Csrf\CryptoUtils::encrypt
* @covers \Csrf\CryptoUtils::decrypt
*/
public function testEncryptAndDecrypt()
{
$source_string = 'I am programmer';
$secret = 'abc123';
// 加密
$encrypt_string = \Csrf\CryptoUtils::encrypt($source_string, $secret);
$this->assertTrue($encrypt_string!='');
// 解密
$decrypt_string = \Csrf\CryptoUtils::decrypt($encrypt_string, $secret);
$this->assertEquals($source_string, $decrypt_string);
}
/**
* @covers \Csrf\CryptoUtils::decrypt
*/
public function testDecryptEncryptStringError()
{
$secret = 'abc123';
$encrypt_string = 'error';
$decrypt_string = \Csrf\CryptoUtils::decrypt($encrypt_string, $secret);
$this->assertEquals('', $decrypt_string);
}
/**
* @covers \Csrf\CryptoUtils::decrypt
*/
public function testDecryptFail()
{
$secret = 'abc123';
$encrypt_string = 'abc::::456';
$decrypt_string = \Csrf\CryptoUtils::decrypt($encrypt_string, $secret);
$this->assertEquals('', $decrypt_string);
}
/**
* @covers \Csrf\CryptoUtils::encrypt
* @dataProvider encryptExceptionCases
*/
public function testEncryptException($func, $exception_message)
{
$this->expectException(\Csrf\Exception\CryptoException::class);
$this->expectExceptionMessage($exception_message);
$func();
}
// 测试 encrypt 异常
public function encryptExceptionCases()
{
// 异常情况
$exception_cases = array(
array(
function()
{
$source_string = '';
$secret = 'abc123';
\Csrf\CryptoUtils::encrypt($source_string, $secret);
},
'source string is empty'
),
array(
function()
{
$source_string = 'I am programmer';
$secret = '';
\Csrf\CryptoUtils::encrypt($source_string, $secret);
},
'secret is empty'
),
);
return $exception_cases;
}
/**
* @covers \Csrf\CryptoUtils::decrypt
* @dataProvider decryptExceptionCases
*/
public function testDecryptException($func, $exception_message)
{
$this->expectException(\Csrf\Exception\CryptoException::class);
$this->expectExceptionMessage($exception_message);
$func();
}
// 测试 decrypt 异常
public function decryptExceptionCases()
{
// 异常情况
$exception_cases = array(
array(
function()
{
$encrypt_string = '';
$secret = 'abc123';
\Csrf\CryptoUtils::decrypt($encrypt_string, $secret);
},
'encrypt string is empty'
),
array(
function()
{
$encrypt_string = 'encrypt string';
$secret = '';
\Csrf\CryptoUtils::decrypt($encrypt_string, $secret);
},
'secret is empty'
),
);
return $exception_cases;
}
}