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 Defuse\Crypto\Exception\EnvironmentIsBrokenException; |
16
|
|
|
use Defuse\Crypto\Exception\WrongKeyOrModifiedCiphertextException; |
17
|
|
|
use Mes\Security\CryptoBundle\EncryptionWrapper; |
18
|
|
|
use Mes\Security\CryptoBundle\Exception\CryptoException; |
19
|
|
|
use Symfony\Component\Filesystem\Filesystem; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Class EncryptionWrapperTest. |
23
|
|
|
*/ |
24
|
|
|
class EncryptionWrapperTest extends \PHPUnit_Framework_TestCase |
25
|
|
|
{ |
26
|
|
|
/** |
27
|
|
|
* @var EncryptionWrapper |
28
|
|
|
*/ |
29
|
|
|
private $wrapper; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @var \PHPUnit_Framework_MockObject_MockObject |
33
|
|
|
*/ |
34
|
|
|
private $encryption; |
35
|
|
|
|
36
|
|
|
protected function setUp() |
37
|
|
|
{ |
38
|
|
|
$this->encryption = $this->getMockBuilder('Mes\Security\CryptoBundle\EncryptionInterface') |
39
|
|
|
->getMock(); |
40
|
|
|
$this->wrapper = new EncryptionWrapper($this->encryption); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
protected function tearDown() |
44
|
|
|
{ |
45
|
|
|
$this->wrapper = null; |
46
|
|
|
$this->encryption = null; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/* =================================== |
50
|
|
|
* |
51
|
|
|
* EncryptionInterface::EncryptWithKey |
52
|
|
|
* |
53
|
|
|
* =================================== |
54
|
|
|
*/ |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* @throws CryptoException |
58
|
|
|
*/ |
59
|
|
View Code Duplication |
public function testEncryptWithKeyEncryptsPlaintext() |
60
|
|
|
{ |
61
|
|
|
$this->encryption->expects($this->once()) |
62
|
|
|
->method('encryptWithKey') |
63
|
|
|
->with('The quick brown fox jumps over the lazy dog', $this->getMockBuilder('Mes\Security\CryptoBundle\Model\KeyInterface') |
64
|
|
|
->getMock()) |
65
|
|
|
->will($this->returnValue('ThisIsACipherText')); |
66
|
|
|
|
67
|
|
|
$ciphertext = $this->wrapper->encryptWithKey('The quick brown fox jumps over the lazy dog', $this->getMockBuilder('Mes\Security\CryptoBundle\Model\KeyInterface') |
68
|
|
|
->getMock()); |
69
|
|
|
|
70
|
|
|
$this->assertTrue(ctype_print($ciphertext), 'is printable'); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* @expectedException \Mes\Security\CryptoBundle\Exception\CryptoException |
75
|
|
|
*/ |
76
|
|
View Code Duplication |
public function testEncryptWithKeyEncryptsPlaintextThrowsException() |
77
|
|
|
{ |
78
|
|
|
try { |
79
|
|
|
$this->encryption->expects($this->once()) |
80
|
|
|
->method('encryptWithKey') |
81
|
|
|
->with('The quick brown fox jumps over the lazy dog', $this->getMockBuilder('Mes\Security\CryptoBundle\Model\KeyInterface') |
82
|
|
|
->getMock()) |
83
|
|
|
->will($this->throwException(new EnvironmentIsBrokenException())); |
84
|
|
|
} catch (EnvironmentIsBrokenException $e) { |
85
|
|
|
$this->throwException(new CryptoException()); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
$this->wrapper->encryptWithKey('The quick brown fox jumps over the lazy dog', $this->getMockBuilder('Mes\Security\CryptoBundle\Model\KeyInterface') |
89
|
|
|
->getMock()); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* @throws CryptoException |
94
|
|
|
*/ |
95
|
|
View Code Duplication |
public function testDecryptWithKeyDecryptsCiphertext() |
96
|
|
|
{ |
97
|
|
|
$this->encryption->expects($this->once()) |
98
|
|
|
->method('decryptWithKey') |
99
|
|
|
->with('ThisIsACipherText', $this->getMockBuilder('Mes\Security\CryptoBundle\Model\KeyInterface') |
100
|
|
|
->getMock()) |
101
|
|
|
->will($this->returnValue('The quick brown fox jumps over the lazy dog')); |
102
|
|
|
|
103
|
|
|
$decryptedText = $this->wrapper->decryptWithKey('ThisIsACipherText', $this->getMockBuilder('Mes\Security\CryptoBundle\Model\KeyInterface') |
104
|
|
|
->getMock()); |
105
|
|
|
|
106
|
|
|
$this->assertSame('The quick brown fox jumps over the lazy dog', $decryptedText); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* @expectedException \Mes\Security\CryptoBundle\Exception\CryptoException |
111
|
|
|
*/ |
112
|
|
View Code Duplication |
public function testDecryptWithKeyDecryptsCiphertextThrowsException() |
113
|
|
|
{ |
114
|
|
|
try { |
115
|
|
|
$this->encryption->expects($this->once()) |
116
|
|
|
->method('decryptWithKey') |
117
|
|
|
->with('ThisIsACipherText', $this->getMockBuilder('Mes\Security\CryptoBundle\Model\KeyInterface') |
118
|
|
|
->getMock()) |
119
|
|
|
->will($this->throwException(new BaseCryptoException())); |
120
|
|
|
} catch (BaseCryptoException $e) { |
121
|
|
|
$this->throwException(new CryptoException()); |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
$this->wrapper->decryptWithKey('ThisIsACipherText', $this->getMockBuilder('Mes\Security\CryptoBundle\Model\KeyInterface') |
125
|
|
|
->getMock()); |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/* ======================================= |
129
|
|
|
* |
130
|
|
|
* EncryptionInterface::EncryptFileWithKey |
131
|
|
|
* |
132
|
|
|
* ======================================= |
133
|
|
|
*/ |
134
|
|
|
|
135
|
|
|
/** |
136
|
|
|
* @throws CryptoException |
137
|
|
|
*/ |
138
|
|
View Code Duplication |
public function testEncryptFileWithKeyEncryptsFile() |
139
|
|
|
{ |
140
|
|
|
$this->encryption->expects($this->once()) |
141
|
|
|
->method('encryptFileWithKey') |
142
|
|
|
->will($this->returnCallback(function ($input, $output) { |
143
|
|
|
$fs = new Filesystem(); |
144
|
|
|
$fs->dumpFile($output, ''); |
145
|
|
|
})); |
146
|
|
|
|
147
|
|
|
$this->wrapper->encryptFileWithKey(__DIR__.'/file.txt', __DIR__.'/file.crypto', $this->getMockBuilder('Mes\Security\CryptoBundle\Model\KeyInterface') |
148
|
|
|
->getMock()); |
149
|
|
|
|
150
|
|
|
$this->assertFileExists(__DIR__.'/file.crypto'); |
151
|
|
|
|
152
|
|
|
unlink(__DIR__.'/file.crypto'); |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
/** |
156
|
|
|
* @throws CryptoException |
157
|
|
|
*/ |
158
|
|
View Code Duplication |
public function testDecryptFileWithKeyDecryptsEncryptedFile() |
159
|
|
|
{ |
160
|
|
|
$this->encryption->expects($this->once()) |
161
|
|
|
->method('decryptFileWithKey') |
162
|
|
|
->will($this->returnCallback(function ($input, $output) { |
163
|
|
|
$fs = new Filesystem(); |
164
|
|
|
$fs->dumpFile($output, 'Plain text'); |
165
|
|
|
})); |
166
|
|
|
|
167
|
|
|
$this->wrapper->decryptFileWithKey(__DIR__.'/file.crypto', __DIR__.'/file.txt', $this->getMockBuilder('Mes\Security\CryptoBundle\Model\KeyInterface') |
168
|
|
|
->getMock()); |
169
|
|
|
|
170
|
|
|
$this->assertFileExists(__DIR__.'/file.txt'); |
171
|
|
|
$this->assertContains('Plain text', file_get_contents(__DIR__.'/file.txt')); |
172
|
|
|
|
173
|
|
|
unlink(__DIR__.'/file.txt'); |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
/** |
177
|
|
|
* @expectedException \Mes\Security\CryptoBundle\Exception\CryptoException |
178
|
|
|
*/ |
179
|
|
View Code Duplication |
public function testEncryptFileWithKeyThrowsException() |
180
|
|
|
{ |
181
|
|
|
try { |
182
|
|
|
$this->encryption->expects($this->once()) |
183
|
|
|
->method('encryptFileWithKey') |
184
|
|
|
->will($this->throwException(new BaseCryptoException())); |
185
|
|
|
} catch (BaseCryptoException $e) { |
186
|
|
|
$this->throwException(new CryptoException()); |
187
|
|
|
} |
188
|
|
|
|
189
|
|
|
$this->wrapper->encryptFileWithKey(__DIR__.'/file.txt', __DIR__.'/file.crypto', $this->getMockBuilder('Mes\Security\CryptoBundle\Model\KeyInterface') |
190
|
|
|
->getMock()); |
191
|
|
|
} |
192
|
|
|
|
193
|
|
|
/** |
194
|
|
|
* @expectedException \Mes\Security\CryptoBundle\Exception\CryptoException |
195
|
|
|
*/ |
196
|
|
View Code Duplication |
public function testDecryptFileWithKeyThrowsException() |
197
|
|
|
{ |
198
|
|
|
try { |
199
|
|
|
$this->encryption->expects($this->once()) |
200
|
|
|
->method('decryptFileWithKey') |
201
|
|
|
->will($this->throwException(new BaseCryptoException())); |
202
|
|
|
} catch (BaseCryptoException $e) { |
203
|
|
|
$this->throwException(new CryptoException()); |
204
|
|
|
} |
205
|
|
|
|
206
|
|
|
$this->wrapper->decryptFileWithKey(__DIR__.'/file.crypto', __DIR__.'/file.txt', $this->getMockBuilder('Mes\Security\CryptoBundle\Model\KeyInterface') |
207
|
|
|
->getMock()); |
208
|
|
|
} |
209
|
|
|
|
210
|
|
|
/* ======================================== |
211
|
|
|
* |
212
|
|
|
* EncryptionInterface::EncryptWithPassword |
213
|
|
|
* |
214
|
|
|
* ======================================== |
215
|
|
|
*/ |
216
|
|
|
|
217
|
|
|
public function testEncryptWithPasswordEncryptsPlaintext() |
218
|
|
|
{ |
219
|
|
|
$this->encryption->expects($this->once()) |
220
|
|
|
->method('encryptWithPassword') |
221
|
|
|
->with('The quick brown fox jumps over the lazy dog', 'SuperSecretPa$$word') |
222
|
|
|
->will($this->returnValue('ThisIsACipherText')); |
223
|
|
|
|
224
|
|
|
$ciphertext = $this->wrapper->encryptWithPassword('The quick brown fox jumps over the lazy dog', 'SuperSecretPa$$word'); |
225
|
|
|
|
226
|
|
|
$this->assertTrue(ctype_print($ciphertext), 'is printable'); |
227
|
|
|
} |
228
|
|
|
|
229
|
|
|
/** |
230
|
|
|
* @expectedException \Mes\Security\CryptoBundle\Exception\CryptoException |
231
|
|
|
*/ |
232
|
|
View Code Duplication |
public function testEncryptWithPasswordEncryptsPlaintextThrowsException() |
233
|
|
|
{ |
234
|
|
|
try { |
235
|
|
|
$this->encryption->expects($this->once()) |
236
|
|
|
->method('encryptWithPassword') |
237
|
|
|
->with('The quick brown fox jumps over the lazy dog', 'SuperSecretPa$$word') |
238
|
|
|
->will($this->throwException(new EnvironmentIsBrokenException())); |
239
|
|
|
} catch (EnvironmentIsBrokenException $e) { |
240
|
|
|
$this->throwException(new CryptoException()); |
241
|
|
|
} |
242
|
|
|
|
243
|
|
|
$this->wrapper->encryptWithPassword('The quick brown fox jumps over the lazy dog', 'SuperSecretPa$$word'); |
244
|
|
|
} |
245
|
|
|
|
246
|
|
|
public function testDecryptWithPasswordDecryptsCiphertext() |
247
|
|
|
{ |
248
|
|
|
$this->encryption->expects($this->once()) |
249
|
|
|
->method('decryptWithPassword') |
250
|
|
|
->with('ThisIsACipherText', 'SuperSecretPa$$word') |
251
|
|
|
->will($this->returnValue('The quick brown fox jumps over the lazy dog')); |
252
|
|
|
|
253
|
|
|
$decryptedText = $this->wrapper->decryptWithPassword('ThisIsACipherText', 'SuperSecretPa$$word'); |
254
|
|
|
|
255
|
|
|
$this->assertSame('The quick brown fox jumps over the lazy dog', $decryptedText); |
256
|
|
|
} |
257
|
|
|
|
258
|
|
|
/** |
259
|
|
|
* @expectedException \Mes\Security\CryptoBundle\Exception\CryptoException |
260
|
|
|
*/ |
261
|
|
View Code Duplication |
public function testDecryptWithPasswordDecryptsCiphertextThrowsException() |
262
|
|
|
{ |
263
|
|
|
try { |
264
|
|
|
$this->encryption->expects($this->once()) |
265
|
|
|
->method('decryptWithPassword') |
266
|
|
|
->with('ThisIsACipherText', 'SuperSecretPa$$word') |
267
|
|
|
->will($this->throwException(new WrongKeyOrModifiedCiphertextException())); |
268
|
|
|
} catch (BaseCryptoException $e) { |
269
|
|
|
$this->throwException(new CryptoException()); |
270
|
|
|
} |
271
|
|
|
|
272
|
|
|
$this->wrapper->decryptWithPassword('ThisIsACipherText', 'SuperSecretPa$$word'); |
273
|
|
|
} |
274
|
|
|
|
275
|
|
|
/* ============================================ |
276
|
|
|
* |
277
|
|
|
* EncryptionInterface::EncryptFileWithPassword |
278
|
|
|
* |
279
|
|
|
* ============================================ |
280
|
|
|
*/ |
281
|
|
|
|
282
|
|
View Code Duplication |
public function testEncryptFileWithPasswordEncryptsFile() |
283
|
|
|
{ |
284
|
|
|
$this->encryption->expects($this->once()) |
285
|
|
|
->method('encryptFileWithPassword') |
286
|
|
|
->will($this->returnCallback(function ($input, $output, $password) { |
|
|
|
|
287
|
|
|
$fs = new Filesystem(); |
288
|
|
|
$fs->dumpFile($output, ''); |
289
|
|
|
})); |
290
|
|
|
|
291
|
|
|
$this->wrapper->encryptFileWithPassword(__DIR__.'/file.txt', __DIR__.'/file.crypto', 'SuperSecretPa$$word'); |
292
|
|
|
|
293
|
|
|
$this->assertFileExists(__DIR__.'/file.crypto'); |
294
|
|
|
|
295
|
|
|
unlink(__DIR__.'/file.crypto'); |
296
|
|
|
} |
297
|
|
|
|
298
|
|
|
/** |
299
|
|
|
* @expectedException \Mes\Security\CryptoBundle\Exception\CryptoException |
300
|
|
|
*/ |
301
|
|
View Code Duplication |
public function testEncryptFileWithPasswordEncryptsFileThrowsException() |
302
|
|
|
{ |
303
|
|
|
try { |
304
|
|
|
$this->encryption->expects($this->once()) |
305
|
|
|
->method('encryptFileWithPassword') |
306
|
|
|
->will($this->throwException(new EnvironmentIsBrokenException())); |
307
|
|
|
} catch (EnvironmentIsBrokenException $e) { |
308
|
|
|
$this->throwException(new CryptoException()); |
309
|
|
|
} |
310
|
|
|
|
311
|
|
|
$this->wrapper->encryptFileWithPassword(__DIR__.'/file.txt', __DIR__.'/file.crypto', 'SuperSecretPa$$word'); |
312
|
|
|
} |
313
|
|
|
|
314
|
|
View Code Duplication |
public function testDecryptFileWithPasswordDecryptsEncryptedFile() |
315
|
|
|
{ |
316
|
|
|
$this->encryption->expects($this->once()) |
317
|
|
|
->method('decryptFileWithPassword') |
318
|
|
|
->will($this->returnCallback(function ($input, $output, $password) { |
|
|
|
|
319
|
|
|
$fs = new Filesystem(); |
320
|
|
|
$fs->dumpFile($output, 'Plain text'); |
321
|
|
|
})); |
322
|
|
|
|
323
|
|
|
$this->wrapper->decryptFileWithPassword(__DIR__.'/file.crypto', __DIR__.'/file.txt', 'SuperSecretPa$$word'); |
324
|
|
|
|
325
|
|
|
$this->assertFileExists(__DIR__.'/file.txt'); |
326
|
|
|
$this->assertContains('Plain text', file_get_contents(__DIR__.'/file.txt')); |
327
|
|
|
|
328
|
|
|
unlink(__DIR__.'/file.txt'); |
329
|
|
|
} |
330
|
|
|
|
331
|
|
|
/** |
332
|
|
|
* @expectedException \Mes\Security\CryptoBundle\Exception\CryptoException |
333
|
|
|
*/ |
334
|
|
View Code Duplication |
public function testDecryptFileWithPasswordDecryptsEncryptedFileThrowsException() |
335
|
|
|
{ |
336
|
|
|
try { |
337
|
|
|
$this->encryption->expects($this->once()) |
338
|
|
|
->method('decryptFileWithPassword') |
339
|
|
|
->will($this->throwException(new WrongKeyOrModifiedCiphertextException())); |
340
|
|
|
} catch (BaseCryptoException $e) { |
341
|
|
|
$this->throwException(new CryptoException()); |
342
|
|
|
} |
343
|
|
|
|
344
|
|
|
$this->wrapper->decryptFileWithPassword(__DIR__.'/file.crypto', __DIR__.'/file.txt', 'SuperSecretPa$$word'); |
345
|
|
|
} |
346
|
|
|
} |
347
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.