Completed
Push — master ( 5132c9...a70667 )
by Francesco
03:24
created

EncryptionWrapperTest::testDecryptWithPasswordDecryptsCiphertextThrowsException()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 13
Code Lines 9

Duplication

Lines 13
Ratio 100 %

Importance

Changes 0
Metric Value
dl 13
loc 13
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 9
nc 2
nop 0
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 PHPUnit\Framework\TestCase;
20
use Symfony\Component\Filesystem\Filesystem;
21
22
/**
23
 * Class EncryptionWrapperTest.
24
 */
25
class EncryptionWrapperTest extends TestCase
26
{
27
    /**
28
     * @var \Mes\Security\CryptoBundle\EncryptionWrapper
29
     */
30
    private $wrapper;
31
32
    /**
33
     * @var \PHPUnit_Framework_MockObject_MockObject
34
     */
35
    private $encryption;
36
37
    protected function setUp()
38
    {
39
        $this->encryption = $this->getMockBuilder('Mes\Security\CryptoBundle\EncryptionInterface')
40
                                 ->getMock();
41
        $this->wrapper = new EncryptionWrapper($this->encryption);
42
    }
43
44
    protected function tearDown()
45
    {
46
        $this->wrapper = null;
47
        $this->encryption = null;
48
    }
49
50
    /**
51
     * @throws \Mes\Security\CryptoBundle\Exception\CryptoException
52
     */
53 View Code Duplication
    public function testEncryptWithKeyEncryptsPlaintext()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
54
    {
55
        $this->encryption->expects($this->once())
56
                         ->method('encryptWithKey')
57
                         ->with('The quick brown fox jumps over the lazy dog', $this->getMockBuilder('Mes\Security\CryptoBundle\Model\KeyInterface')->getMock())
58
                         ->will($this->returnValue('ThisIsACipherText'));
59
60
        $ciphertext = $this->wrapper->encryptWithKey('The quick brown fox jumps over the lazy dog', $this->getMockBuilder('Mes\Security\CryptoBundle\Model\KeyInterface')->getMock());
61
62
        $this->assertTrue(ctype_print($ciphertext), 'is printable');
63
    }
64
65
    /**
66
     * @expectedException \Mes\Security\CryptoBundle\Exception\CryptoException
67
     */
68 View Code Duplication
    public function testEncryptWithKeyEncryptsPlaintextThrowsException()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
69
    {
70
        try {
71
            $this->encryption->expects($this->once())
72
                             ->method('encryptWithKey')
73
                             ->with('The quick brown fox jumps over the lazy dog', $this->getMockBuilder('Mes\Security\CryptoBundle\Model\KeyInterface')->getMock())
74
                             ->will($this->throwException(new EnvironmentIsBrokenException()));
75
        } catch (EnvironmentIsBrokenException $e) {
76
            $this->throwException(new CryptoException());
77
        }
78
79
        $this->wrapper->encryptWithKey('The quick brown fox jumps over the lazy dog', $this->getMockBuilder('Mes\Security\CryptoBundle\Model\KeyInterface')->getMock());
80
    }
81
82
    /**
83
     * @throws \Mes\Security\CryptoBundle\Exception\CryptoException
84
     */
85 View Code Duplication
    public function testDecryptWithKeyDecryptsCiphertext()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
86
    {
87
        $this->encryption->expects($this->once())
88
                         ->method('decryptWithKey')
89
                         ->with('ThisIsACipherText', $this->getMockBuilder('Mes\Security\CryptoBundle\Model\KeyInterface')->getMock())
90
                         ->will($this->returnValue('The quick brown fox jumps over the lazy dog'));
91
92
        $decryptedText = $this->wrapper->decryptWithKey('ThisIsACipherText', $this->getMockBuilder('Mes\Security\CryptoBundle\Model\KeyInterface')->getMock());
93
94
        $this->assertSame('The quick brown fox jumps over the lazy dog', $decryptedText);
95
    }
96
97
    /**
98
     * @expectedException \Mes\Security\CryptoBundle\Exception\CryptoException
99
     */
100 View Code Duplication
    public function testDecryptWithKeyDecryptsCiphertextThrowsException()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
101
    {
102
        try {
103
            $this->encryption->expects($this->once())
104
                             ->method('decryptWithKey')
105
                             ->with('ThisIsACipherText', $this->getMockBuilder('Mes\Security\CryptoBundle\Model\KeyInterface')->getMock())
106
                             ->will($this->throwException(new BaseCryptoException()));
107
        } catch (BaseCryptoException $e) {
108
            $this->throwException(new CryptoException());
109
        }
110
111
        $this->wrapper->decryptWithKey('ThisIsACipherText', $this->getMockBuilder('Mes\Security\CryptoBundle\Model\KeyInterface')->getMock());
112
    }
113
114
    /**
115
     * @throws \Mes\Security\CryptoBundle\Exception\CryptoException
116
     */
117 View Code Duplication
    public function testEncryptWithKeyFileEncryptsFile()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
118
    {
119
        $this->encryption->expects($this->once())
120
                         ->method('encryptFileWithKey')
121
                         ->will($this->returnCallback(function ($input, $output) {
122
                             $fs = new Filesystem();
123
                             $fs->dumpFile($output, '');
124
                         }));
125
126
        $this->wrapper->encryptFileWithKey(__DIR__.'/file.txt', __DIR__.'/file.crypto', $this->getMockBuilder('Mes\Security\CryptoBundle\Model\KeyInterface')->getMock());
127
128
        $this->assertFileExists(__DIR__.'/file.crypto');
129
130
        unlink(__DIR__.'/file.crypto');
131
    }
132
133
    /**
134
     * @throws \Mes\Security\CryptoBundle\Exception\CryptoException
135
     */
136 View Code Duplication
    public function testDecryptWithKeyFileDecryptsEncryptedFile()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
137
    {
138
        $this->encryption->expects($this->once())
139
                         ->method('decryptFileWithKey')
140
                         ->will($this->returnCallback(function ($input, $output) {
141
                             $fs = new Filesystem();
142
                             $fs->dumpFile($output, 'Plain text');
143
                         }));
144
145
        $this->wrapper->decryptFileWithKey(__DIR__.'/file.crypto', __DIR__.'/file.txt', $this->getMockBuilder('Mes\Security\CryptoBundle\Model\KeyInterface')->getMock());
146
147
        $this->assertFileExists(__DIR__.'/file.txt');
148
        $this->assertContains('Plain text', file_get_contents(__DIR__.'/file.txt'));
149
150
        unlink(__DIR__.'/file.txt');
151
    }
152
153
    /**
154
     * @expectedException \Mes\Security\CryptoBundle\Exception\CryptoException
155
     */
156 View Code Duplication
    public function testEncryptWithKeyFileThrowsException()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
157
    {
158
        try {
159
            $this->encryption->expects($this->once())
160
                             ->method('encryptFileWithKey')
161
                             ->will($this->throwException(new BaseCryptoException()));
162
        } catch (BaseCryptoException $e) {
163
            $this->throwException(new CryptoException());
164
        }
165
166
        $this->wrapper->encryptFileWithKey(__DIR__.'/file.txt', __DIR__.'/file.crypto', $this->getMockBuilder('Mes\Security\CryptoBundle\Model\KeyInterface')->getMock());
167
    }
168
169
    /**
170
     * @expectedException \Mes\Security\CryptoBundle\Exception\CryptoException
171
     */
172 View Code Duplication
    public function testDecryptWithKeyFileThrowsException()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
173
    {
174
        try {
175
            $this->encryption->expects($this->once())
176
                             ->method('decryptFileWithKey')
177
                             ->will($this->throwException(new BaseCryptoException()));
178
        } catch (BaseCryptoException $e) {
179
            $this->throwException(new CryptoException());
180
        }
181
182
        $this->wrapper->decryptFileWithKey(__DIR__.'/file.crypto', __DIR__.'/file.txt', $this->getMockBuilder('Mes\Security\CryptoBundle\Model\KeyInterface')->getMock());
183
    }
184
185
    public function testEncryptWithPasswordEncryptsPlaintext()
186
    {
187
        $this->encryption->expects($this->once())
188
                         ->method('encryptWithPassword')
189
                         ->with('The quick brown fox jumps over the lazy dog', 'SuperSecretPa$$word')
190
                         ->will($this->returnValue('ThisIsACipherText'));
191
192
        $ciphertext = $this->wrapper->encryptWithPassword('The quick brown fox jumps over the lazy dog', 'SuperSecretPa$$word');
193
194
        $this->assertTrue(ctype_print($ciphertext), 'is printable');
195
    }
196
197
    /**
198
     * @expectedException \Mes\Security\CryptoBundle\Exception\CryptoException
199
     */
200 View Code Duplication
    public function testEncryptWithPasswordEncryptsPlaintextThrowsException()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
201
    {
202
        try {
203
            $this->encryption->expects($this->once())
204
                             ->method('encryptWithPassword')
205
                             ->with('The quick brown fox jumps over the lazy dog', 'SuperSecretPa$$word')
206
                             ->will($this->throwException(new EnvironmentIsBrokenException()));
207
        } catch (EnvironmentIsBrokenException $e) {
208
            $this->throwException(new CryptoException());
209
        }
210
211
        $this->wrapper->encryptWithPassword('The quick brown fox jumps over the lazy dog', 'SuperSecretPa$$word');
212
    }
213
214
    public function testDecryptWithPasswordDecryptsCiphertext()
215
    {
216
        $this->encryption->expects($this->once())
217
                         ->method('decryptWithPassword')
218
                         ->with('ThisIsACipherText', 'SuperSecretPa$$word')
219
                         ->will($this->returnValue('The quick brown fox jumps over the lazy dog'));
220
221
        $decryptedText = $this->wrapper->decryptWithPassword('ThisIsACipherText', 'SuperSecretPa$$word');
222
223
        $this->assertSame('The quick brown fox jumps over the lazy dog', $decryptedText);
224
    }
225
226
    /**
227
     * @expectedException \Mes\Security\CryptoBundle\Exception\CryptoException
228
     */
229 View Code Duplication
    public function testDecryptWithPasswordDecryptsCiphertextThrowsException()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
230
    {
231
        try {
232
            $this->encryption->expects($this->once())
233
                             ->method('decryptWithPassword')
234
                             ->with('ThisIsACipherText', 'SuperSecretPa$$word')
235
                             ->will($this->throwException(new WrongKeyOrModifiedCiphertextException()));
236
        } catch (BaseCryptoException $e) {
237
            $this->throwException(new CryptoException());
238
        }
239
240
        $this->wrapper->decryptWithPassword('ThisIsACipherText', 'SuperSecretPa$$word');
241
    }
242
243 View Code Duplication
    public function testEncryptFileWithPasswordEncryptsFile()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
244
    {
245
        $this->encryption->expects($this->once())
246
                         ->method('encryptFileWithPassword')
247
                         ->will($this->returnCallback(function ($input, $output, $password) {
0 ignored issues
show
Unused Code introduced by
The parameter $password is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
248
                             $fs = new Filesystem();
249
                             $fs->dumpFile($output, '');
250
                         }));
251
252
        $this->wrapper->encryptFileWithPassword(__DIR__.'/file.txt', __DIR__.'/file.crypto', 'SuperSecretPa$$word');
253
254
        $this->assertFileExists(__DIR__.'/file.crypto');
255
256
        unlink(__DIR__.'/file.crypto');
257
    }
258
259
    /**
260
     * @expectedException \Mes\Security\CryptoBundle\Exception\CryptoException
261
     */
262 View Code Duplication
    public function testEncryptFileWithPasswordEncryptsFileThrowsException()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
263
    {
264
        try {
265
            $this->encryption->expects($this->once())
266
                             ->method('encryptFileWithPassword')
267
                             ->will($this->throwException(new EnvironmentIsBrokenException()));
268
        } catch (EnvironmentIsBrokenException $e) {
269
            $this->throwException(new CryptoException());
270
        }
271
272
        $this->wrapper->encryptFileWithPassword(__DIR__.'/file.txt', __DIR__.'/file.crypto', 'SuperSecretPa$$word');
273
    }
274
275 View Code Duplication
    public function testDecryptFileWithPasswordDecryptsEncryptedFile()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
276
    {
277
        $this->encryption->expects($this->once())
278
                         ->method('decryptFileWithPassword')
279
                         ->will($this->returnCallback(function ($input, $output, $password) {
0 ignored issues
show
Unused Code introduced by
The parameter $password is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
280
                             $fs = new Filesystem();
281
                             $fs->dumpFile($output, 'Plain text');
282
                         }));
283
284
        $this->wrapper->decryptFileWithPassword(__DIR__.'/file.crypto', __DIR__.'/file.txt', 'SuperSecretPa$$word');
285
286
        $this->assertFileExists(__DIR__.'/file.txt');
287
        $this->assertContains('Plain text', file_get_contents(__DIR__.'/file.txt'));
288
289
        unlink(__DIR__.'/file.txt');
290
    }
291
292
    /**
293
     * @expectedException \Mes\Security\CryptoBundle\Exception\CryptoException
294
     */
295 View Code Duplication
    public function testDecryptFileWithPasswordDecryptsEncryptedFileThrowsException()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
296
    {
297
        try {
298
            $this->encryption->expects($this->once())
299
                             ->method('decryptFileWithPassword')
300
                             ->will($this->throwException(new WrongKeyOrModifiedCiphertextException()));
301
        } catch (BaseCryptoException $e) {
302
            $this->throwException(new CryptoException());
303
        }
304
305
        $this->wrapper->decryptFileWithPassword(__DIR__.'/file.crypto', __DIR__.'/file.txt', 'SuperSecretPa$$word');
306
    }
307
}
308