-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCryptoTest.php
More file actions
104 lines (83 loc) · 2.91 KB
/
CryptoTest.php
File metadata and controls
104 lines (83 loc) · 2.91 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
<?php // vim:ts=3:sts=3:sw=3:et:
/**
* Test class for MindFrame2_Crypto
*
* PHP Version 5
*
* @category PHP
* @package MindFrame2
* @author Bryan C. Geraghty <bryan@ravensight.org>
* @copyright 2005-2011 Bryan C. Geraghty
* @license http://www.gnu.org/licenses/lgpl-3.0.txt GNU LGPL
* @link https://github.com/archwisp/MindFrame2
*/
/**
* Test class for MindFrame2_Crypto
*
* @category PHP
* @package MindFrame2
* @author Bryan C. Geraghty <bryan@ravensight.org>
* @license http://www.gnu.org/licenses/lgpl-3.0.txt GNU LGPL
* @link https://github.com/archwisp/MindFrame2
*/
class MindFrame2_CryptoTest extends PHPUnit_Framework_TestCase
{
private $_instance;
private $_encoded_iv = '6R3yY9RWoIvsE4fR43nAUUtr12hJmssJJ/vnSh1T8e4=';
public function setUp()
{
$this->_instance = new MindFrame2_Crypto(
MindFrame2_Crypto::RIJNDAEL_256,
MindFrame2_Crypto::MODE_CBC);
}
public function testDecrypt()
{
$plaintext = $this->_instance->decrypt(
base64_decode('k8xz1hVUYUAe9qg2gebMgViyHOEji2q1KUXwUnwHPwk='),
'Easy Key', base64_decode($this->_encoded_iv));
$padded = $this->_instance->padWithNulls('FooBar ');
$this->assertEquals($padded, $plaintext);
$trimmed = $this->_instance->trimNulls($plaintext);
$this->assertEquals('FooBar ', $trimmed);
}
public function testEncrypt()
{
$ciphertext = $this->_instance->encrypt(
'FooBar ', 'Easy Key', base64_decode($this->_encoded_iv));
$this->assertEquals('k8xz1hVUYUAe9qg2gebMgViyHOEji2q1KUXwUnwHPwk=', base64_encode($ciphertext));
}
public function testEncryptInvalidIvLength()
{
$this->setExpectedException('Exception');
$ciphertext = $this->_instance->encrypt(
'FooBar ', 'Easy Key', base64_decode('Short IV'));
$this->assertEquals('k8xz1hVUYUAe9qg2gebMgViyHOEji2q1KUXwUnwHPwk=', base64_encode($ciphertext));
}
public function testGenerateIvLength()
{
$block_size = $this->_instance->getBlockSize();
$iv = $this->_instance->generateIv();
$this->assertEquals($block_size, strlen($iv));
$second_iv = $this->_instance->generateIv();
$this->assertEquals($block_size, strlen($iv));
$this->assertNotEquals($iv, $second_iv);
}
public function testGetBlockSize()
{
$this->assertEquals(32, $this->_instance->getBlockSize());
}
public function testGetKeySize()
{
$this->assertEquals(32, $this->_instance->getKeySize());
}
public function testPkcs7Padding()
{
$block_size = $this->_instance->getBlockSize();
$plaintext = "Foobar \x0";
$padded = $this->_instance->padWithPkcs7($plaintext);
$this->assertNotEquals($plaintext, $padded);
$this->assertEquals($block_size, strlen($padded));
$this->assertEquals($plaintext,
$this->_instance->trimPkcs7($padded));
}
}