1<?php
2
3/* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
4
5/**
6 * General test cases 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 GeneralTestCase
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-2013 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 */
46require_once 'TestCase.php';
47
48/**
49 * General tests for Crypt_GPG.
50 *
51 * @category  Encryption
52 * @package   Crypt_GPG
53 * @author    Michael Gauthier <mike@silverorange.com>
54 * @copyright 2008-2013 silverorange
55 * @license   http://www.gnu.org/copyleft/lesser.html LGPL License 2.1
56 * @link      http://pear.php.net/package/Crypt_GPG
57 */
58class GeneralTest extends Crypt_GPG_TestCase
59{
60    public function testPublicKeyringFileException()
61    {
62        $this->expectException('Crypt_GPG_FileException');
63
64        $publicKeyringFile = $this->getTempFilename('pubring.gpg');
65        new Crypt_GPG(
66            array(
67                'publicKeyring' => $publicKeyringFile
68            )
69        );
70    }
71
72    public function testPrivateKeyringFileException()
73    {
74        $this->expectException('Crypt_GPG_FileException');
75
76        $privateKeyringFile = $this->getTempFilename('secring.gpg');
77        new Crypt_GPG(
78            array(
79                'privateKeyring' => $privateKeyringFile
80            )
81        );
82    }
83
84    public function testTrustDatabaseFileException()
85    {
86        $this->expectException('Crypt_GPG_FileException');
87
88        $trustDbFile = $this->getTempFilename('secring.gpg');
89        new Crypt_GPG(
90            array(
91                'trustDb' => $trustDbFile
92            )
93        );
94    }
95
96    public function testHomedirFileException_NoCreate()
97    {
98        $this->expectException('Crypt_GPG_FileException');
99        $this->expectExceptionMessage('cannot be created');
100
101        if (posix_getuid() === 0) {
102            $this->markTestSkipped('Root can write to any homedir.');
103        }
104
105        $nonCreatableDirectory = '//.gnupg';
106        new Crypt_GPG(array('homedir' => $nonCreatableDirectory));
107    }
108
109    public function testHomedirFileException_NoExecute()
110    {
111        $this->expectException('Crypt_GPG_FileException');
112        $this->expectExceptionMessage('is not enterable');
113
114        if (posix_getuid() === 0) {
115            $this->markTestSkipped('Root can do what it wants to any homedir.');
116        }
117
118        $nonExecutableDirectory = $this->getTempFilename('home-no-execute');
119        mkdir($nonExecutableDirectory);
120        chmod($nonExecutableDirectory, 0600); // rw- --- ---
121
122        new Crypt_GPG(array('homedir' => $nonExecutableDirectory));
123    }
124
125    public function testHomedirFileException_NoWrite()
126    {
127        $this->expectException('Crypt_GPG_FileException');
128        $this->expectExceptionMessage('is not writable');
129
130        if (posix_getuid() === 0) {
131            $this->markTestSkipped('Root can write to any homedir.');
132        }
133
134        $nonWriteableDirectory = $this->getTempFilename('home-no-write');
135        mkdir($nonWriteableDirectory);
136        chmod($nonWriteableDirectory, 0500); // r-x --- ---
137
138        new Crypt_GPG(array('homedir' => $nonWriteableDirectory));
139    }
140
141    public function testBinaryPEARException()
142    {
143        $this->expectException('PEAR_Exception');
144
145        new Crypt_GPG(array('binary' => './non-existent-binary'));
146    }
147
148    public function testGPGBinaryPEARException()
149    {
150        $this->expectException('PEAR_Exception');
151
152        new Crypt_GPG(array('gpgBinary' => './non-existent-binary'));
153    }
154
155    public function testSetEngine()
156    {
157        $engine = new Crypt_GPG_Engine($this->getOptions());
158        $gpg = new Crypt_GPG();
159        $gpg->setEngine($engine);
160
161        $this->assertSame($this->getPropertyValue('Crypt_GPG', $gpg, 'engine'), $engine);
162    }
163
164    /**
165     * @group fluent
166     */
167    public function testFluentInterface()
168    {
169        $returnedGpg = $this->gpg->setEngine(
170            new Crypt_GPG_Engine($this->getOptions())
171        );
172        $this->assertEquals(
173            $this->gpg,
174            $returnedGpg,
175            'Failed asserting fluent interface works for setEngine() method.'
176        );
177
178        $returnedGpg = $this->gpg->addDecryptKey(
179            '8D2299D9C5C211128B32BBB0C097D9EC94C06363',
180            'test1'
181        );
182        $this->assertEquals(
183            $this->gpg,
184            $returnedGpg,
185            'Failed asserting fluent interface works for addDecryptKey() ' .
186            'method.'
187        );
188
189        $returnedGpg = $this->gpg->addEncryptKey(
190            '8D2299D9C5C211128B32BBB0C097D9EC94C06363'
191        );
192        $this->assertEquals(
193            $this->gpg,
194            $returnedGpg,
195            'Failed asserting fluent interface works for addEncryptKey() ' .
196            'method.'
197        );
198
199        $returnedGpg = $this->gpg->addSignKey(
200            '8D2299D9C5C211128B32BBB0C097D9EC94C06363',
201            'test1'
202        );
203        $this->assertEquals(
204            $this->gpg,
205            $returnedGpg,
206            'Failed asserting fluent interface works for addSignKey() ' .
207            'method.'
208        );
209
210        $returnedGpg = $this->gpg->clearDecryptKeys();
211        $this->assertEquals(
212            $this->gpg,
213            $returnedGpg,
214            'Failed asserting fluent interface works for clearDecryptKeys() ' .
215            'method.'
216        );
217
218        $returnedGpg = $this->gpg->clearEncryptKeys();
219        $this->assertEquals(
220            $this->gpg,
221            $returnedGpg,
222            'Failed asserting fluent interface works for clearEncryptKeys() ' .
223            'method.'
224        );
225
226        $returnedGpg = $this->gpg->clearSignKeys();
227        $this->assertEquals(
228            $this->gpg,
229            $returnedGpg,
230            'Failed asserting fluent interface works for clearSignKeys() ' .
231            'method.'
232        );
233    }
234}
235