1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the MesCryptoBundle package. |
5
|
|
|
* |
6
|
|
|
* (c) Francesco Cartenì <http://www.multimediaexperiencestudio.it/> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Mes\Security\CryptoBundle\Tests; |
13
|
|
|
|
14
|
|
|
use Defuse\Crypto\Exception\CryptoException as BaseCryptoException; |
15
|
|
|
use Mes\Security\CryptoBundle\EncryptionWrapper; |
16
|
|
|
use Mes\Security\CryptoBundle\Exception\CryptoException; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* Class EncryptionResourceWrapperTest. |
20
|
|
|
*/ |
21
|
|
|
class EncryptionResourceWrapperTest extends \PHPUnit_Framework_TestCase |
22
|
|
|
{ |
23
|
|
|
/** |
24
|
|
|
* @var EncryptionWrapper |
25
|
|
|
*/ |
26
|
|
|
private $wrapper; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @var \PHPUnit_Framework_MockObject_MockObject |
30
|
|
|
*/ |
31
|
|
|
private $encryption; |
32
|
|
|
|
33
|
|
|
protected function setUp() |
34
|
|
|
{ |
35
|
|
|
$this->encryption = $this->getMockBuilder('Mes\Security\CryptoBundle\EncryptionInterface') |
36
|
|
|
->getMock(); |
37
|
|
|
$this->wrapper = new EncryptionWrapper($this->encryption); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
protected function tearDown() |
41
|
|
|
{ |
42
|
|
|
$this->wrapper = null; |
43
|
|
|
$this->encryption = null; |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/* =========================================== |
47
|
|
|
* |
48
|
|
|
* EncryptionInterface::EncryptResourceWithKey |
49
|
|
|
* |
50
|
|
|
* =========================================== |
51
|
|
|
*/ |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* @throws CryptoException |
55
|
|
|
*/ |
56
|
|
View Code Duplication |
public function testEncryptResourceWithKeyEncryptsHandle() |
|
|
|
|
57
|
|
|
{ |
58
|
|
|
$this->encryption->expects($this->once()) |
59
|
|
|
->method('encryptResourceWithKey'); |
60
|
|
|
|
61
|
|
|
$this->wrapper->encryptResourceWithKey(fopen('php://stdin', 'r'), fopen('php://memory', 'wb'), $this->getMockBuilder('Mes\Security\CryptoBundle\Model\KeyInterface') |
|
|
|
|
62
|
|
|
->getMock()); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* @throws CryptoException |
67
|
|
|
*/ |
68
|
|
View Code Duplication |
public function testDecryptResourceWithKeyDecryptsEncryptedHandle() |
|
|
|
|
69
|
|
|
{ |
70
|
|
|
$this->encryption->expects($this->once()) |
71
|
|
|
->method('decryptResourceWithKey'); |
72
|
|
|
|
73
|
|
|
$this->wrapper->decryptResourceWithKey(fopen('php://stdin', 'r'), fopen('php://memory', 'wb'), $this->getMockBuilder('Mes\Security\CryptoBundle\Model\KeyInterface') |
|
|
|
|
74
|
|
|
->getMock()); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* @expectedException \Mes\Security\CryptoBundle\Exception\CryptoException |
79
|
|
|
*/ |
80
|
|
View Code Duplication |
public function testEncryptResourceWithKeyThrowsException() |
|
|
|
|
81
|
|
|
{ |
82
|
|
|
try { |
83
|
|
|
$this->encryption->expects($this->once()) |
84
|
|
|
->method('encryptResourceWithKey') |
85
|
|
|
->will($this->throwException(new BaseCryptoException())); |
86
|
|
|
} catch (BaseCryptoException $e) { |
87
|
|
|
$this->throwException(new CryptoException()); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
$this->wrapper->encryptResourceWithKey(fopen('php://stdin', 'r'), fopen('php://memory', 'wb'), $this->getMockBuilder('Mes\Security\CryptoBundle\Model\KeyInterface') |
|
|
|
|
91
|
|
|
->getMock()); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* @expectedException \Mes\Security\CryptoBundle\Exception\CryptoException |
96
|
|
|
*/ |
97
|
|
View Code Duplication |
public function testDecryptResourceWithKeyThrowsException() |
|
|
|
|
98
|
|
|
{ |
99
|
|
|
try { |
100
|
|
|
$this->encryption->expects($this->once()) |
101
|
|
|
->method('decryptResourceWithKey') |
102
|
|
|
->will($this->throwException(new BaseCryptoException())); |
103
|
|
|
} catch (BaseCryptoException $e) { |
104
|
|
|
$this->throwException(new CryptoException()); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
$this->wrapper->decryptResourceWithKey(fopen('php://stdin', 'r'), fopen('php://memory', 'wb'), $this->getMockBuilder('Mes\Security\CryptoBundle\Model\KeyInterface') |
|
|
|
|
108
|
|
|
->getMock()); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/* ================================================ |
112
|
|
|
* |
113
|
|
|
* EncryptionInterface::EncryptResourceWithPassword |
114
|
|
|
* |
115
|
|
|
* ================================================ |
116
|
|
|
*/ |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* @throws CryptoException |
120
|
|
|
*/ |
121
|
|
View Code Duplication |
public function testEncryptResourceWithPasswordEncryptsHandle() |
|
|
|
|
122
|
|
|
{ |
123
|
|
|
$this->encryption->expects($this->once()) |
124
|
|
|
->method('encryptResourceWithPassword'); |
125
|
|
|
|
126
|
|
|
$this->wrapper->encryptResourceWithPassword(fopen('php://stdin', 'r'), fopen('php://memory', 'wb'), 'SecretPa$$word'); |
|
|
|
|
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* @throws CryptoException |
131
|
|
|
*/ |
132
|
|
View Code Duplication |
public function testDecryptResourceWithPasswordDecryptsEncryptedHandle() |
|
|
|
|
133
|
|
|
{ |
134
|
|
|
$this->encryption->expects($this->once()) |
135
|
|
|
->method('decryptResourceWithPassword'); |
136
|
|
|
|
137
|
|
|
$this->wrapper->decryptResourceWithPassword(fopen('php://stdin', 'r'), fopen('php://memory', 'wb'), 'SecretPa$$word'); |
|
|
|
|
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
/** |
141
|
|
|
* @expectedException \Mes\Security\CryptoBundle\Exception\CryptoException |
142
|
|
|
*/ |
143
|
|
View Code Duplication |
public function testEncryptResourceWithPasswordThrowsException() |
|
|
|
|
144
|
|
|
{ |
145
|
|
|
try { |
146
|
|
|
$this->encryption->expects($this->once()) |
147
|
|
|
->method('encryptResourceWithPassword') |
148
|
|
|
->will($this->throwException(new BaseCryptoException())); |
149
|
|
|
} catch (BaseCryptoException $e) { |
150
|
|
|
$this->throwException(new CryptoException()); |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
$this->wrapper->encryptResourceWithPassword(fopen('php://stdin', 'r'), fopen('php://memory', 'wb'), 'SecretPa$$word'); |
|
|
|
|
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
/** |
157
|
|
|
* @expectedException \Mes\Security\CryptoBundle\Exception\CryptoException |
158
|
|
|
*/ |
159
|
|
View Code Duplication |
public function testDecryptResourceWithPasswordThrowsException() |
|
|
|
|
160
|
|
|
{ |
161
|
|
|
try { |
162
|
|
|
$this->encryption->expects($this->once()) |
163
|
|
|
->method('decryptResourceWithPassword') |
164
|
|
|
->will($this->throwException(new BaseCryptoException())); |
165
|
|
|
} catch (BaseCryptoException $e) { |
166
|
|
|
$this->throwException(new CryptoException()); |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
$this->wrapper->decryptResourceWithPassword(fopen('php://stdin', 'r'), fopen('php://memory', 'wb'), 'SecretPa$$word'); |
|
|
|
|
170
|
|
|
} |
171
|
|
|
} |
172
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.