Test Failed
Push — master ( e2b526...9772b8 )
by Francesco
03:25
created

Tests/EncryptionResourceTest.php (5 issues)

Labels
Severity

Upgrade to new PHP Analysis Engine

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
It seems like you code against a concrete implementation and not the interface Mes\Security\CryptoBundle\EncryptionInterface as the method encryptResourceWithKey() does only exist in the following implementations of said interface: Mes\Security\CryptoBundle\Encryption.

Let’s take a look at an example:

interface User
{
    /** @return string */
    public function getPassword();
}

class MyUser implements User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
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
It seems like you code against a concrete implementation and not the interface Mes\Security\CryptoBundle\EncryptionInterface as the method decryptResourceWithKey() does only exist in the following implementations of said interface: Mes\Security\CryptoBundle\Encryption.

Let’s take a look at an example:

interface User
{
    /** @return string */
    public function getPassword();
}

class MyUser implements User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
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
It seems like you code against a concrete implementation and not the interface Mes\Security\CryptoBundle\EncryptionInterface as the method encryptResourceWithKey() does only exist in the following implementations of said interface: Mes\Security\CryptoBundle\Encryption.

Let’s take a look at an example:

interface User
{
    /** @return string */
    public function getPassword();
}

class MyUser implements User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
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
It seems like you code against a concrete implementation and not the interface Mes\Security\CryptoBundle\EncryptionInterface as the method encryptResourceWithPassword() does only exist in the following implementations of said interface: Mes\Security\CryptoBundle\Encryption.

Let’s take a look at an example:

interface User
{
    /** @return string */
    public function getPassword();
}

class MyUser implements User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
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
It seems like you code against a concrete implementation and not the interface Mes\Security\CryptoBundle\EncryptionInterface as the method decryptResourceWithPassword() does only exist in the following implementations of said interface: Mes\Security\CryptoBundle\Encryption.

Let’s take a look at an example:

interface User
{
    /** @return string */
    public function getPassword();
}

class MyUser implements User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
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