| Conditions | 4 |
| Paths | 64 |
| Total Lines | 69 |
| Code Lines | 45 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 2 | ||
| Bugs | 0 | Features | 2 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | <?php namespace Wubbajack\Tests; |
||
| 126 | public function testDecrypt() |
||
| 127 | { |
||
| 128 | $encryptedFile = $this->fileCrypt->encrypt($this->test_file, $this->test_encrypted_file); |
||
| 129 | |||
| 130 | $this->fileCrypt->decrypt($encryptedFile, $this->test_decrypted_file); |
||
| 131 | |||
| 132 | // Test if the decrypted file exists |
||
| 133 | $this->assertTrue(file_exists($this->test_decrypted_file)); |
||
| 134 | |||
| 135 | // Test if the checksum equals the original file |
||
| 136 | $this->assertEquals($encryptedFile->getChecksum(), sha1_file($this->test_decrypted_file)); |
||
| 137 | |||
| 138 | // Test if the decrypted file contains the same content as the original |
||
| 139 | $this->assertEquals(file_get_contents($this->test_file), file_get_contents($this->test_decrypted_file)); |
||
| 140 | |||
| 141 | // Test decryption of encrypted file with incorrect wrong IV |
||
| 142 | try { |
||
| 143 | $invalidEncryptedFile = EncryptedFile::create( |
||
| 144 | '2394qsf3-f9', |
||
| 145 | $encryptedFile->getChecksum(), |
||
| 146 | $encryptedFile->getPadding(), |
||
| 147 | $encryptedFile->getFile()->getRealPath() |
||
| 148 | ); |
||
| 149 | $this->fileCrypt->decrypt($invalidEncryptedFile, $this->test_decrypted_file); |
||
| 150 | $this->fail('No exception was thrown on decrypting with an incorrect IV'); |
||
| 151 | } catch (\Exception $e) { |
||
| 152 | $this->assertInstanceOf( |
||
| 153 | DecryptException::class, |
||
| 154 | $e, |
||
| 155 | 'Expected an instance of DecryptException containing a message about IV, got '. get_class($e) |
||
| 156 | ); |
||
| 157 | } |
||
| 158 | |||
| 159 | // Test decryption of encrypted file with incorrect checksum |
||
| 160 | try { |
||
| 161 | $invalidEncryptedFile = EncryptedFile::create( |
||
| 162 | $encryptedFile->getIV(), |
||
| 163 | bin2hex(openssl_random_pseudo_bytes(16)), |
||
| 164 | $encryptedFile->getPadding(), |
||
| 165 | $encryptedFile->getFile()->getRealPath() |
||
| 166 | ); |
||
| 167 | $this->fileCrypt->decrypt($invalidEncryptedFile, $this->test_decrypted_file); |
||
| 168 | $this->fail('No exception was thrown on decrypting with unmatching checksums'); |
||
| 169 | } catch (\Exception $e) { |
||
| 170 | $this->assertInstanceOf( |
||
| 171 | DecryptException::class, |
||
| 172 | $e, |
||
| 173 | 'Expected an instance of DecryptException with a message about the checksum, got '. get_class($e) |
||
| 174 | ); |
||
| 175 | } |
||
| 176 | |||
| 177 | // Test decryption of encrypted file with twice the amount of padding |
||
| 178 | try { |
||
| 179 | $invalidEncryptedFile = EncryptedFile::create( |
||
| 180 | $encryptedFile->getIV(), |
||
| 181 | $encryptedFile->getChecksum(), |
||
| 182 | $encryptedFile->getPadding() * 2, |
||
| 183 | $encryptedFile->getFile()->getRealPath() |
||
| 184 | ); |
||
| 185 | $this->fileCrypt->decrypt($invalidEncryptedFile, $this->test_decrypted_file); |
||
| 186 | $this->fail('No exception was thrown on decrypting with too much padding'); |
||
| 187 | } catch (\Exception $e) { |
||
| 188 | $this->assertInstanceOf( |
||
| 189 | DecryptException::class, |
||
| 190 | $e, |
||
| 191 | 'Expected an instance of DecryptException with a message about the checksum, got '. get_class($e) |
||
| 192 | ); |
||
| 193 | } |
||
| 194 | } |
||
| 195 | |||
| 250 |