comparison vendor/pear/crypt_gpg/tests/EncryptTest.php @ 0:1e000243b222

vanilla 1.3.3 distro, I hope
author Charlie Root
date Thu, 04 Jan 2018 15:50:29 -0500
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:1e000243b222
1 <?php
2
3 /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
4
5 /**
6 * Encryption tests for the Crypt_GPG package.
7 *
8 * These tests require the PHPUnit 3.6 or greater package to be installed.
9 * PHPUnit is installable using PEAR. See the
10 * {@link http://www.phpunit.de/manual/3.6/en/installation.html manual}
11 * for detailed installation instructions.
12 *
13 * To run these tests, use:
14 * <code>
15 * $ phpunit EncryptTestCase
16 * </code>
17 *
18 * LICENSE:
19 *
20 * This library is free software; you can redistribute it and/or modify
21 * it under the terms of the GNU Lesser General Public License as
22 * published by the Free Software Foundation; either version 2.1 of the
23 * License, or (at your option) any later version.
24 *
25 * This library is distributed in the hope that it will be useful,
26 * but WITHOUT ANY WARRANTY; without even the implied warranty of
27 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
28 * Lesser General Public License for more details.
29 *
30 * You should have received a copy of the GNU Lesser General Public
31 * License along with this library; if not, see
32 * <http://www.gnu.org/licenses/>
33 *
34 * @category Encryption
35 * @package Crypt_GPG
36 * @author Michael Gauthier <mike@silverorange.com>
37 * @copyright 2005-2008 silverorange
38 * @license http://www.gnu.org/copyleft/lesser.html LGPL License 2.1
39 * @version CVS: $Id$
40 * @link http://pear.php.net/package/Crypt_GPG
41 */
42
43 /**
44 * Base test case.
45 */
46 require_once 'TestCase.php';
47
48 /**
49 * Tests encryption abilities of Crypt_GPG.
50 *
51 * @category Encryption
52 * @package Crypt_GPG
53 * @author Michael Gauthier <mike@silverorange.com>
54 * @copyright 2005-2008 silverorange
55 * @license http://www.gnu.org/copyleft/lesser.html LGPL License 2.1
56 * @link http://pear.php.net/package/Crypt_GPG
57 */
58 class EncryptTestCase extends Crypt_GPG_TestCase
59 {
60 public function testHasEncryptKeys()
61 {
62 $this->assertFalse($this->gpg->hasEncryptKeys());
63 $this->gpg->addEncryptKey('no-passphrase@example.com');
64 $this->assertTrue($this->gpg->hasEncryptKeys());
65 }
66
67 // {{{ testEncrypt()
68
69 /**
70 * @group string
71 */
72 public function testEncrypt()
73 {
74 $data = 'Hello, Alice! Goodbye, Bob!';
75 $keyId = 'first-keypair@example.com';
76 $passphrase = 'test1';
77
78 $this->gpg->addEncryptKey($keyId);
79 $encryptedData = $this->gpg->encrypt($data);
80
81 $this->gpg->addDecryptKey($keyId, $passphrase);
82 $decryptedData = $this->gpg->decrypt($encryptedData);
83
84 $this->assertEquals($data, $decryptedData);
85 }
86
87 // }}}
88 // {{{ testEncryptDual()
89
90 /**
91 * @group string
92 */
93 public function testEncryptDual()
94 {
95 $data = 'Hello, Alice! Goodbye, Bob!';
96
97 $this->gpg->addEncryptKey('first-keypair@example.com');
98 $this->gpg->addEncryptKey('second-keypair@example.com');
99 $encryptedData = $this->gpg->encrypt($data);
100
101 // decrypt with first key
102 $this->gpg->addDecryptKey('first-keypair@example.com', 'test1');
103 $decryptedData = $this->gpg->decrypt($encryptedData);
104 $this->assertEquals($data, $decryptedData);
105 $this->gpg->clearDecryptKeys();
106
107 // decrypt with second key
108 $this->gpg->addDecryptKey('second-keypair@example.com', 'test2');
109 $decryptedData = $this->gpg->decrypt($encryptedData);
110 $this->gpg->clearDecryptKeys();
111 $this->assertEquals($data, $decryptedData);
112 }
113
114 // }}}
115 // {{{ testEncryptKeyNotFoundException_invalid()
116
117 /**
118 * @expectedException Crypt_GPG_KeyNotFoundException
119 *
120 * @group string
121 */
122 public function testEncryptNotFoundException_invalid()
123 {
124 $data = 'Hello, Alice! Goodbye, Bob!';
125 $this->gpg->addEncryptKey('non-existent-key@example.com');
126 $this->gpg->encrypt($data);
127 }
128
129 // }}}
130 // {{{ testEncryptKeyNotFoundException_none()
131
132 /**
133 * @expectedException Crypt_GPG_KeyNotFoundException
134 *
135 * @group string
136 */
137 public function testEncryptNotFoundException_none()
138 {
139 $data = 'Hello, Alice! Goodbye, Bob!';
140 $this->gpg->encrypt($data);
141 }
142
143 // }}}
144 // {{{ testEncryptEmpty()
145
146 /**
147 * @group string
148 */
149 public function testEncryptEmpty()
150 {
151 $data = '';
152
153 $this->gpg->addEncryptKey('first-keypair@example.com');
154 $encryptedData = $this->gpg->encrypt($data);
155
156 $this->gpg->addDecryptKey('first-keypair@example.com', 'test1');
157 $decryptedData = $this->gpg->decrypt($encryptedData);
158
159 $this->assertEquals($data, $decryptedData);
160 }
161
162 // }}}
163
164 // file
165 // {{{ testEncryptFile()
166
167 /**
168 * @group file
169 */
170 public function testEncryptFile()
171 {
172 $expectedMd5Sum = 'f96267d87551ee09bfcac16921e351c1';
173 $originalFilename = $this->getDataFilename('testFileMedium.plain');
174 $encryptedFilename = $this->getTempFilename('testEncryptFile.asc');
175 $decryptedFilename = $this->getTempFilename('testEncryptFile.plain');
176
177 $this->gpg->addEncryptKey('first-keypair@example.com');
178 $this->gpg->encryptFile($originalFilename, $encryptedFilename);
179
180 $this->gpg->addDecryptKey('first-keypair@example.com', 'test1');
181 $this->gpg->decryptFile($encryptedFilename, $decryptedFilename);
182
183 $md5Sum = $this->getMd5Sum($decryptedFilename);
184 $this->assertEquals($expectedMd5Sum, $md5Sum);
185 }
186
187 // }}}
188 // {{{ testEncryptFileDual()
189
190 /**
191 * @group file
192 */
193 public function testEncryptFileDual()
194 {
195 $expectedMd5Sum = 'f96267d87551ee09bfcac16921e351c1';
196 $originalFilename = $this->getDataFilename('testFileMedium.plain');
197 $encryptedFilename = $this->getTempFilename('testEncryptFile.asc');
198 $decryptedFilename = $this->getTempFilename('testEncryptFile.plain');
199
200 $this->gpg->addEncryptKey('first-keypair@example.com');
201 $this->gpg->addEncryptKey('second-keypair@example.com');
202 $this->gpg->encryptFile($originalFilename, $encryptedFilename);
203
204 // decrypt with first key
205 $this->gpg->addDecryptKey('first-keypair@example.com', 'test1');
206 $this->gpg->decryptFile($encryptedFilename, $decryptedFilename);
207 $this->gpg->clearDecryptKeys();
208 $md5Sum = $this->getMd5Sum($decryptedFilename);
209 $this->assertEquals($expectedMd5Sum, $md5Sum);
210
211 // decrypt with second key
212 $this->gpg->addDecryptKey('second-keypair@example.com', 'test2');
213 $this->gpg->decryptFile($encryptedFilename, $decryptedFilename);
214 $this->gpg->clearDecryptKeys();
215 $md5Sum = $this->getMd5Sum($decryptedFilename);
216 $this->assertEquals($expectedMd5Sum, $md5Sum);
217 }
218
219 // }}}
220 // {{{ testEncryptFileToString()
221
222 /**
223 * @group file
224 */
225 public function testEncryptFileToString()
226 {
227 $expectedData = 'Hello, Alice! Goodbye, Bob!';
228 $originalFilename = $this->getDataFilename('testFileSmall.plain');
229
230 $this->gpg->addEncryptKey('first-keypair@example.com');
231 $encryptedData = $this->gpg->encryptFile($originalFilename);
232
233 $this->gpg->addDecryptKey('first-keypair@example.com', 'test1');
234 $decryptedData = $this->gpg->decrypt($encryptedData);
235
236 $this->assertEquals($expectedData, $decryptedData);
237 }
238
239 // }}}
240 // {{{ testEncryptFileFileException_input()
241
242 /**
243 * @group file
244 *
245 * @expectedException Crypt_GPG_FileException
246 */
247 public function testEncryptFileFileException_input()
248 {
249 // input file does not exist
250 $filename =
251 $this->getDataFilename('testEncryptFileFileException_input.plain');
252
253 $this->gpg->addEncryptKey('first-keypair@example.com');
254 $this->gpg->encryptFile($filename);
255 }
256
257 // }}}
258 // {{{ testEncryptFileFileException_output()
259
260 /**
261 * @group file
262 *
263 * @expectedException Crypt_GPG_FileException
264 */
265 public function testEncryptFileFileException_output()
266 {
267 // output file does not exist
268 $inputFilename = $this->getDataFilename('testFileMedium.plain');
269 $outputFilename = './non-existent' .
270 '/testEncryptFileFileException_output.asc';
271
272 $this->gpg->addEncryptKey('first-keypair@example.com');
273 $this->gpg->encryptFile($inputFilename, $outputFilename);
274 }
275
276 // }}}
277 // {{{ testEncryptFileEmpty()
278
279 /**
280 * @group file
281 */
282 public function testEncryptFileEmpty()
283 {
284 $filename = $this->getDataFilename('testFileEmpty.plain');
285
286 $this->gpg->addEncryptKey('first-keypair@example.com');
287 $encryptedData = $this->gpg->encryptFile($filename);
288
289 $this->gpg->addDecryptKey('first-keypair@example.com', 'test1');
290 $decryptedData = $this->gpg->decrypt($encryptedData);
291
292 $this->assertEquals('', $decryptedData);
293 }
294
295 // }}}
296 }
297
298 ?>