These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
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\IOException; |
||
15 | use Mes\Security\CryptoBundle\Encryption; |
||
16 | use Mes\Security\CryptoBundle\EncryptionInterface; |
||
17 | use Mes\Security\CryptoBundle\KeyGenerator\KeyGenerator; |
||
18 | use Mes\Security\CryptoBundle\KeyGenerator\KeyGeneratorInterface; |
||
19 | use Mes\Security\CryptoBundle\Model\KeyInterface; |
||
20 | |||
21 | /** |
||
22 | * Class EncryptionResourceTest. |
||
23 | */ |
||
24 | class EncryptionResourceTest extends \PHPUnit_Framework_TestCase |
||
25 | { |
||
26 | /** |
||
27 | * @var EncryptionInterface |
||
28 | */ |
||
29 | private $encryption; |
||
30 | |||
31 | /** |
||
32 | * @var KeyGeneratorInterface |
||
33 | */ |
||
34 | private $generator; |
||
35 | |||
36 | private $plainContentResourceFile; |
||
37 | private $plainContentResourceHandle; |
||
38 | |||
39 | private $cipherContentResourceFile; |
||
40 | private $cipherContentResourceHandle; |
||
41 | |||
42 | protected function setUp() |
||
43 | { |
||
44 | $this->encryption = new Encryption(); |
||
45 | $this->generator = new KeyGenerator(); |
||
46 | $this->cipherContentResourceFile = __DIR__.'/cipherContentResourceFile.crypto'; |
||
47 | } |
||
48 | |||
49 | protected function tearDown() |
||
50 | { |
||
51 | $this->encryption = null; |
||
52 | $this->generator = null; |
||
53 | } |
||
54 | |||
55 | /* =========================================== |
||
56 | * |
||
57 | * EncryptionInterface::EncryptResourceWithKey |
||
58 | * |
||
59 | * =========================================== |
||
60 | */ |
||
61 | |||
62 | /** |
||
63 | * @return array |
||
64 | */ |
||
65 | public function testEncryptResourceWithKeyEncryptsAnHandle() |
||
66 | { |
||
67 | $this->createPlainContentFile(); |
||
68 | |||
69 | /** @var KeyInterface $key */ |
||
70 | $key = $this->generator->generate('ThisIsASuperSecret'); |
||
71 | |||
72 | $this->plainContentResourceHandle = $this->openHandle($this->plainContentResourceFile, 'rb'); |
||
73 | $this->cipherContentResourceHandle = $this->openHandle($this->cipherContentResourceFile, 'wb'); |
||
74 | |||
75 | $this->encryption->encryptResourceWithKey($this->plainContentResourceHandle, $this->cipherContentResourceHandle, $key); |
||
0 ignored issues
–
show
|
|||
76 | |||
77 | $this->assertFileExists($this->cipherContentResourceFile, sprintf('File %s not exists', $this->cipherContentResourceFile)); |
||
78 | $this->assertGreaterThan(0, (new \SplFileInfo($this->cipherContentResourceFile))->getSize()); |
||
79 | |||
80 | $this->closeHandle($this->plainContentResourceHandle); |
||
81 | $this->closeHandle($this->cipherContentResourceHandle); |
||
82 | |||
83 | $hash = md5_file($this->plainContentResourceFile); |
||
84 | unlink($this->plainContentResourceFile); |
||
85 | |||
86 | return array( |
||
87 | 'key' => $key->getEncoded(), |
||
88 | 'secret' => $key->getSecret(), |
||
89 | 'hash' => $hash, |
||
90 | ); |
||
91 | } |
||
92 | |||
93 | /** |
||
94 | * @depends testEncryptResourceWithKeyEncryptsAnHandle |
||
95 | * |
||
96 | * @param $args |
||
97 | */ |
||
98 | public function testDecryptResourceWithKeyDecryptsAnHandle($args) |
||
99 | { |
||
100 | $this->createPlainContentFile(false); |
||
101 | |||
102 | /** @var KeyInterface $key */ |
||
103 | $key = $this->generator->generateFromAscii($args['key'], $args['secret']); |
||
104 | |||
105 | $this->cipherContentResourceHandle = fopen($this->cipherContentResourceFile, 'rb'); |
||
106 | $this->plainContentResourceHandle = fopen($this->plainContentResourceFile, 'wb'); |
||
107 | |||
108 | $this->encryption->decryptResourceWithKey($this->cipherContentResourceHandle, $this->plainContentResourceHandle, $key); |
||
0 ignored issues
–
show
The method
decryptResourceWithKey() does not seem to exist on object<Mes\Security\Cryp...le\EncryptionInterface> .
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces. This is most likely a typographical error or the method has been renamed.
Loading history...
|
|||
109 | |||
110 | $this->assertCount(5, file($this->plainContentResourceFile)); |
||
111 | $this->assertSame($args['hash'], md5_file($this->plainContentResourceFile), 'Original file mismatches the result of encrypt and decrypt'); |
||
112 | $this->assertGreaterThan(0, (new \SplFileInfo($this->cipherContentResourceFile))->getSize()); |
||
113 | |||
114 | $this->closeHandle($this->plainContentResourceHandle); |
||
115 | $this->closeHandle($this->cipherContentResourceHandle); |
||
116 | } |
||
117 | |||
118 | /** |
||
119 | * @expectedException \Defuse\Crypto\Exception\IOException |
||
120 | */ |
||
121 | public function testEncryptResourceThrowsException() |
||
122 | { |
||
123 | $this->createPlainContentFile(); |
||
124 | $this->plainContentResourceHandle = fopen($this->plainContentResourceFile, 'rb'); |
||
125 | |||
126 | try { |
||
127 | $this->encryption->encryptResourceWithKey($this->plainContentResourceHandle, $this->cipherContentResourceFile, $this->generator->generate()); |
||
0 ignored issues
–
show
The method
encryptResourceWithKey() does not seem to exist on object<Mes\Security\Cryp...le\EncryptionInterface> .
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces. This is most likely a typographical error or the method has been renamed.
Loading history...
|
|||
128 | } catch (IOException $e) { |
||
129 | $this->closeHandle($this->plainContentResourceHandle); |
||
130 | throw $e; |
||
131 | } |
||
132 | } |
||
133 | |||
134 | public function testEncryptResourceWithPasswordEncryptsAnHandle() |
||
135 | { |
||
136 | $this->createPlainContentFile(); |
||
137 | |||
138 | $this->plainContentResourceHandle = $this->openHandle($this->plainContentResourceFile, 'rb'); |
||
139 | $this->cipherContentResourceHandle = $this->openHandle($this->cipherContentResourceFile, 'wb'); |
||
140 | |||
141 | $this->encryption->encryptResourceWithPassword($this->plainContentResourceHandle, $this->cipherContentResourceHandle, 'SuperPa$$word'); |
||
0 ignored issues
–
show
The method
encryptResourceWithPassword() does not seem to exist on object<Mes\Security\Cryp...le\EncryptionInterface> .
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces. This is most likely a typographical error or the method has been renamed.
Loading history...
|
|||
142 | |||
143 | $this->assertFileExists($this->cipherContentResourceFile, sprintf('File %s not exists', $this->cipherContentResourceFile)); |
||
144 | $this->assertGreaterThan(0, (new \SplFileInfo($this->cipherContentResourceFile))->getSize()); |
||
145 | |||
146 | $this->closeHandle($this->plainContentResourceHandle); |
||
147 | $this->closeHandle($this->cipherContentResourceHandle); |
||
148 | |||
149 | $hash = md5_file($this->plainContentResourceFile); |
||
150 | unlink($this->plainContentResourceFile); |
||
151 | |||
152 | return array( |
||
153 | 'hash' => $hash, |
||
154 | ); |
||
155 | } |
||
156 | |||
157 | /** |
||
158 | * @depends testEncryptResourceWithKeyEncryptsAnHandle |
||
159 | * |
||
160 | * @param $args |
||
161 | */ |
||
162 | public function testDecryptResourceWithPasswordDecryptsAnHandle($args) |
||
163 | { |
||
164 | $this->createPlainContentFile(false); |
||
165 | |||
166 | $this->cipherContentResourceHandle = fopen($this->cipherContentResourceFile, 'rb'); |
||
167 | $this->plainContentResourceHandle = fopen($this->plainContentResourceFile, 'wb'); |
||
168 | |||
169 | $this->encryption->decryptResourceWithPassword($this->cipherContentResourceHandle, $this->plainContentResourceHandle, 'SuperPa$$word'); |
||
0 ignored issues
–
show
The method
decryptResourceWithPassword() does not seem to exist on object<Mes\Security\Cryp...le\EncryptionInterface> .
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces. This is most likely a typographical error or the method has been renamed.
Loading history...
|
|||
170 | |||
171 | $this->assertCount(5, file($this->plainContentResourceFile)); |
||
172 | $this->assertSame($args['hash'], md5_file($this->plainContentResourceFile), 'Original file mismatches the result of encrypt and decrypt'); |
||
173 | $this->assertGreaterThan(0, (new \SplFileInfo($this->cipherContentResourceFile))->getSize()); |
||
174 | |||
175 | $this->closeHandle($this->plainContentResourceHandle); |
||
176 | $this->closeHandle($this->cipherContentResourceHandle); |
||
177 | |||
178 | unlink($this->plainContentResourceFile); |
||
179 | unlink($this->cipherContentResourceFile); |
||
180 | } |
||
181 | |||
182 | private function createPlainContentFile($pushContent = true) |
||
183 | { |
||
184 | $this->plainContentResourceFile = __DIR__.'/plainContentResourceFile.crypto'; |
||
185 | if (!file_exists($this->plainContentResourceFile)) { |
||
186 | if ($pushContent) { |
||
187 | file_put_contents($this->plainContentResourceFile, <<<'EOT' |
||
188 | Line 1 |
||
189 | Line 2 |
||
190 | Line 3 |
||
191 | Line 4 |
||
192 | Line 5 |
||
193 | EOT |
||
194 | ); |
||
195 | } else { |
||
196 | touch($this->plainContentResourceFile); |
||
197 | } |
||
198 | } |
||
199 | } |
||
200 | |||
201 | /** |
||
202 | * @param $filename |
||
203 | * @param $mode |
||
204 | * |
||
205 | * @return resource |
||
206 | */ |
||
207 | private function openHandle($filename, $mode) |
||
208 | { |
||
209 | return fopen($filename, $mode); |
||
210 | } |
||
211 | |||
212 | /** |
||
213 | * @param resource $handle |
||
214 | */ |
||
215 | private function closeHandle($handle) |
||
216 | { |
||
217 | fclose($handle); |
||
218 | } |
||
219 | } |
||
220 |
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.
This is most likely a typographical error or the method has been renamed.