File: GeneralTest.php

package info (click to toggle)
php-crypt-gpg 1.6.4-2%2Bdeb11u1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 2,184 kB
  • sloc: php: 9,171; xml: 201; makefile: 9
file content (276 lines) | stat: -rw-r--r-- 7,816 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
<?php

/* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */

/**
 * General test cases for the Crypt_GPG package.
 *
 * These tests require the PHPUnit 3.6 or greater package to be installed.
 * PHPUnit is installable using PEAR. See the
 * {@link http://www.phpunit.de/manual/3.6/en/installation.html manual}
 * for detailed installation instructions.
 *
 * To run these tests, use:
 * <code>
 * $ phpunit GeneralTest
 * </code>
 *
 * LICENSE:
 *
 * This library is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; either version 2.1 of the
 * License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, see
 * <http://www.gnu.org/licenses/>
 *
 * @category  Encryption
 * @package   Crypt_GPG
 * @author    Michael Gauthier <mike@silverorange.com>
 * @copyright 2005-2013 silverorange
 * @license   http://www.gnu.org/copyleft/lesser.html LGPL License 2.1
 * @version   CVS: $Id$
 * @link      http://pear.php.net/package/Crypt_GPG
 */

/**
 * Base test case.
 */
require_once 'TestCase.php';

/**
 * General tests for Crypt_GPG.
 *
 * @category  Encryption
 * @package   Crypt_GPG
 * @author    Michael Gauthier <mike@silverorange.com>
 * @copyright 2008-2013 silverorange
 * @license   http://www.gnu.org/copyleft/lesser.html LGPL License 2.1
 * @link      http://pear.php.net/package/Crypt_GPG
 */
class GeneralTest extends Crypt_GPG_TestCase
{
    // {{{ testPublicKeyringFileException()

    public function testPublicKeyringFileException()
    {
        $this->expectException('Crypt_GPG_FileException');
        $publicKeyringFile = $this->getTempFilename('pubring.gpg');
        new Crypt_GPG(
            array(
                'publicKeyring' => $publicKeyringFile
            )
        );
    }

    // }}}
    // {{{ testPrivateKeyringFileException()

    public function testPrivateKeyringFileException()
    {
        $this->expectException('Crypt_GPG_FileException');
        $privateKeyringFile = $this->getTempFilename('secring.gpg');
        new Crypt_GPG(
            array(
                'privateKeyring' => $privateKeyringFile
            )
        );
    }

    // }}}
    // {{{ testTrustDatabaseFileException()

    public function testTrustDatabaseFileException()
    {
        $this->expectException('Crypt_GPG_FileException');
        $trustDbFile = $this->getTempFilename('secring.gpg');
        new Crypt_GPG(
            array(
                'trustDb' => $trustDbFile
            )
        );
    }

    // }}}
    // {{{ testHomedirFileException_NoCreate()

    public function testHomedirFileException_NoCreate()
    {
        $this->expectException('Crypt_GPG_FileException');
        $this->expectExceptionMessage('cannot be created');
        if (posix_getuid() === 0) {
            $this->markTestSkipped('Root can write to any homedir.');
        }

        $nonCreatableDirectory = '//.gnupg';
        new Crypt_GPG(array('homedir' => $nonCreatableDirectory));
    }

    // }}}
    // {{{ testHomedirFileException_NoExecute()

    public function testHomedirFileException_NoExecute()
    {
        $this->expectException('Crypt_GPG_FileException');
        $this->expectExceptionMessage('is not enterable');
        if (posix_getuid() === 0) {
            $this->markTestSkipped('Root can do what it wants to any homedir.');
        }

        $nonExecutableDirectory = $this->getTempFilename('home-no-execute');
        mkdir($nonExecutableDirectory);
        chmod($nonExecutableDirectory, 0600); // rw- --- ---

        new Crypt_GPG(array('homedir' => $nonExecutableDirectory));
    }

    // }}}
    // {{{ testHomedirFileException_NoWrite()

    public function testHomedirFileException_NoWrite()
    {
        $this->expectException('Crypt_GPG_FileException');
        $this->expectExceptionMessage('is not writable');
        if (posix_getuid() === 0) {
            $this->markTestSkipped('Root can write to any homedir.');
        }

        $nonWriteableDirectory = $this->getTempFilename('home-no-write');
        mkdir($nonWriteableDirectory);
        chmod($nonWriteableDirectory, 0500); // r-x --- ---

        new Crypt_GPG(array('homedir' => $nonWriteableDirectory));
    }

    // }}}
    // {{{ testBinaryPEARException()

    public function testBinaryPEARException()
    {
        $this->expectException('PEAR_Exception');
        new Crypt_GPG(array('binary' => './non-existent-binary'));
    }

    // }}}
    // {{{ testGPGBinaryPEARException()

    public function testGPGBinaryPEARException()
    {
        $this->expectException('PEAR_Exception');
        new Crypt_GPG(array('gpgBinary' => './non-existent-binary'));
    }

    // }}}
    // {{{ testSetEngine()

    /**
     * @group write
     */
    public function testSetEngine()
    {
        $engine = new Crypt_GPG_Engine($this->getOptions());
        $gpg = new Crypt_GPG();
        $gpg->setEngine($engine);

        $homedirConstraint = $this->attribute(
            $this->attributeEqualTo(
                '_homedir',
                __DIR__ . '/' . self::HOMEDIR
            ),
            'engine'
        );

        $this->assertThat(
            $gpg,
            $homedirConstraint,
            'Engine was not set properly.'
        );
    }

    // }}}

    // fluent interface
    // {{{ testFluentInterface

    /**
     * @group fluent
     */
    public function testFluentInterface()
    {
        $returnedGpg = $this->gpg->setEngine(
            new Crypt_GPG_Engine($this->getOptions())
        );
        $this->assertEquals(
            $this->gpg,
            $returnedGpg,
            'Failed asserting fluent interface works for setEngine() method.'
        );

        $returnedGpg = $this->gpg->addDecryptKey(
            '8D2299D9C5C211128B32BBB0C097D9EC94C06363',
            'test1'
        );
        $this->assertEquals(
            $this->gpg,
            $returnedGpg,
            'Failed asserting fluent interface works for addDecryptKey() ' .
            'method.'
        );

        $returnedGpg = $this->gpg->addEncryptKey(
            '8D2299D9C5C211128B32BBB0C097D9EC94C06363'
        );
        $this->assertEquals(
            $this->gpg,
            $returnedGpg,
            'Failed asserting fluent interface works for addEncryptKey() ' .
            'method.'
        );

        $returnedGpg = $this->gpg->addSignKey(
            '8D2299D9C5C211128B32BBB0C097D9EC94C06363',
            'test1'
        );
        $this->assertEquals(
            $this->gpg,
            $returnedGpg,
            'Failed asserting fluent interface works for addSignKey() ' .
            'method.'
        );

        $returnedGpg = $this->gpg->clearDecryptKeys();
        $this->assertEquals(
            $this->gpg,
            $returnedGpg,
            'Failed asserting fluent interface works for clearDecryptKeys() ' .
            'method.'
        );

        $returnedGpg = $this->gpg->clearEncryptKeys();
        $this->assertEquals(
            $this->gpg,
            $returnedGpg,
            'Failed asserting fluent interface works for clearEncryptKeys() ' .
            'method.'
        );

        $returnedGpg = $this->gpg->clearSignKeys();
        $this->assertEquals(
            $this->gpg,
            $returnedGpg,
            'Failed asserting fluent interface works for clearSignKeys() ' .
            'method.'
        );
    }

    // }}}
}

?>