PageRenderTime 48ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/tests/ZendTest/Crypt/FileCipherTest.php

http://github.com/zendframework/zf2
PHP | 264 lines | 191 code | 42 blank | 31 comment | 3 complexity | e20323aba5704d0fdee9abb1358b3d8c MD5 | raw file
Possible License(s): BSD-3-Clause
  1. <?php
  2. /**
  3. * Zend Framework (http://framework.zend.com/)
  4. *
  5. * @link http://github.com/zendframework/zf2 for the canonical source repository
  6. * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
  7. * @license http://framework.zend.com/license/new-bsd New BSD License
  8. */
  9. namespace ZendTest\Crypt;
  10. use Zend\Crypt\FileCipher;
  11. use Zend\Crypt\Symmetric\Mcrypt;
  12. use Zend\Crypt\Symmetric\Exception;
  13. use Zend\Crypt\Hmac;
  14. use Zend\Math\Rand;
  15. /**
  16. * @group Zend_Crypt
  17. */
  18. class FileCipherTest extends \PHPUnit_Framework_TestCase
  19. {
  20. /**
  21. * @var fileCipher
  22. */
  23. protected $fileCipher;
  24. /**
  25. * @var string
  26. */
  27. protected $fileIn;
  28. /**
  29. * @var string
  30. */
  31. protected $fileOut;
  32. public function setUp()
  33. {
  34. try {
  35. $this->fileCipher = new FileCipher();
  36. } catch (Exception\RuntimeException $e) {
  37. $this->markTestSkipped('Mcrypt is not installed, I cannot execute the FileCipherTest');
  38. }
  39. }
  40. public function tearDown()
  41. {
  42. if (file_exists($this->fileIn)) {
  43. unlink($this->fileIn);
  44. }
  45. if (file_exists($this->fileOut)) {
  46. unlink($this->fileOut);
  47. }
  48. }
  49. public function testBufferConstant()
  50. {
  51. // The buffer size must be always the same to be able to decrypt
  52. $this->assertEquals(1048576, FileCipher::BUFFER_SIZE);
  53. }
  54. public function testSetCipher()
  55. {
  56. $cipher = new Mcrypt(array(
  57. 'algo' => 'blowfish'
  58. ));
  59. $this->fileCipher->setCipher($cipher);
  60. $this->assertInstanceOf('Zend\Crypt\Symmetric\SymmetricInterface', $this->fileCipher->getCipher());
  61. $this->assertEquals($cipher, $this->fileCipher->getCipher());
  62. }
  63. public function testSetKeyIteration()
  64. {
  65. $this->fileCipher->setKeyIteration(5000);
  66. $this->assertEquals(5000, $this->fileCipher->getKeyIteration());
  67. }
  68. public function testSetKey()
  69. {
  70. $this->fileCipher->setKey('test');
  71. $this->assertEquals('test', $this->fileCipher->getKey());
  72. }
  73. public function testSetEmptyKey()
  74. {
  75. $this->setExpectedException('Zend\Crypt\Exception\InvalidArgumentException',
  76. 'The key cannot be empty');
  77. $this->fileCipher->setKey('');
  78. }
  79. public function testSetCipherAlgorithm()
  80. {
  81. $this->fileCipher->setCipherAlgorithm('blowfish');
  82. $this->assertEquals('blowfish', $this->fileCipher->getCipherAlgorithm());
  83. }
  84. public function testSetCipherAlgorithmFail()
  85. {
  86. $this->setExpectedException('Zend\Crypt\Symmetric\Exception\InvalidArgumentException',
  87. 'The algorithm unknown is not supported by Zend\Crypt\Symmetric\Mcrypt');
  88. $this->fileCipher->setCipherAlgorithm('unknown');
  89. }
  90. public function testGetCipherSupportedAlgorithms()
  91. {
  92. $this->assertInternalType('array', $this->fileCipher->getCipherSupportedAlgorithms());
  93. }
  94. public function testSetHashAlgorithm()
  95. {
  96. $this->fileCipher->setHashAlgorithm('sha1');
  97. $this->assertEquals('sha1', $this->fileCipher->getHashAlgorithm());
  98. }
  99. public function testSetWrongHashAlgorithm()
  100. {
  101. $this->setExpectedException('Zend\Crypt\Exception\InvalidArgumentException',
  102. 'The specified hash algorithm \'unknown\' is not supported by Zend\Crypt\Hash');
  103. $this->fileCipher->setHashAlgorithm('unknown');
  104. }
  105. public function testSetPbkdf2HashAlgorithm()
  106. {
  107. $this->fileCipher->setPbkdf2HashAlgorithm('sha1');
  108. $this->assertEquals('sha1', $this->fileCipher->getPbkdf2HashAlgorithm());
  109. }
  110. public function testSetWrongPbkdf2HashAlgorithm()
  111. {
  112. $this->setExpectedException('Zend\Crypt\Exception\InvalidArgumentException',
  113. 'The specified hash algorithm \'unknown\' is not supported by Zend\Crypt\Hash');
  114. $this->fileCipher->setPbkdf2HashAlgorithm('unknown');
  115. }
  116. public function testEncrypDecryptFile()
  117. {
  118. $this->fileCipher->setKey('test');
  119. // Test 5 files with a random size between 1 Kb and 5 Mb
  120. for ($i=1; $i <= 5; $i++) {
  121. $fileIn = $this->generateTmpFile(Rand::getInteger(1024, 1048576 * 5), Rand::getBytes(1));
  122. $fileOut = $fileIn . '.enc';
  123. // encrypt without compression
  124. $this->assertTrue($this->fileCipher->encrypt($fileIn, $fileOut, false));
  125. $paddingSize = $this->fileCipher->getCipher()->getBlockSize();
  126. $this->assertEquals(filesize($fileOut),
  127. filesize($fileIn) +
  128. $this->fileCipher->getCipher()->getSaltSize() +
  129. Hmac::getOutputSize($this->fileCipher->getHashAlgorithm()) +
  130. $paddingSize - filesize($fileIn) % $paddingSize);
  131. $decryptFile = $fileOut . '.dec';
  132. // decrypt
  133. $this->assertTrue($this->fileCipher->decrypt($fileOut, $decryptFile));
  134. $this->assertEquals(filesize($fileIn), filesize($decryptFile));
  135. $this->assertEquals(file_get_contents($fileIn), file_get_contents($decryptFile));
  136. unlink($fileIn);
  137. unlink($fileOut);
  138. unlink($decryptFile);
  139. }
  140. }
  141. public function testDecryptFileNoValidAuthenticate()
  142. {
  143. $this->fileIn = $this->generateTmpFile(1048576, Rand::getBytes(1));
  144. $this->fileOut = $this->fileIn . '.enc';
  145. $this->fileCipher->setKey('test');
  146. $this->assertTrue($this->fileCipher->encrypt($this->fileIn, $this->fileOut, false));
  147. $fileOut2 = $this->fileIn . '.dec';
  148. $this->assertTrue($this->fileCipher->decrypt($this->fileOut, $fileOut2, false));
  149. unlink($fileOut2);
  150. // Tampering of the encrypted file
  151. $ciphertext = file_get_contents($this->fileOut);
  152. $ciphertext[0] = chr((ord($ciphertext[0]) + 1) % 256);
  153. file_put_contents($this->fileOut, $ciphertext);
  154. $this->assertFalse($this->fileCipher->decrypt($this->fileOut, $fileOut2, false));
  155. $this->assertFileNotExists($fileOut2);
  156. }
  157. public function testEncryptFileWithNoKey()
  158. {
  159. $this->fileIn = $this->generateTmpFile(1048576, Rand::getBytes(1));
  160. $this->fileOut = $this->fileIn . '.enc';
  161. $this->setExpectedException('Zend\Crypt\Exception\InvalidArgumentException',
  162. 'No key specified for encryption');
  163. $this->fileCipher->encrypt($this->fileIn, $this->fileOut);
  164. }
  165. public function testDecryptFileWithNoKey()
  166. {
  167. $this->fileIn = $this->generateTmpFile(1048576, Rand::getBytes(1));
  168. $this->fileOut = $this->fileIn . '.enc';
  169. $this->setExpectedException('Zend\Crypt\Exception\InvalidArgumentException',
  170. 'No key specified for decryption');
  171. $this->fileCipher->decrypt($this->fileIn, $this->fileOut);
  172. }
  173. public function testEncryptFileInvalidInputFile()
  174. {
  175. $randomFile = uniqid('Invalid_File');
  176. $this->setExpectedException('Zend\Crypt\Exception\InvalidArgumentException',
  177. "I cannot open the $randomFile file");
  178. $this->fileCipher->setKey('test');
  179. $this->fileCipher->encrypt($randomFile, '');
  180. }
  181. public function testDecryptFileInvalidInputFile()
  182. {
  183. $randomFile = uniqid('Invalid_File');
  184. $this->setExpectedException('Zend\Crypt\Exception\InvalidArgumentException',
  185. "I cannot open the $randomFile file");
  186. $this->fileCipher->setKey('test');
  187. $this->fileCipher->decrypt($randomFile, '');
  188. }
  189. public function testEncryptFileInvalidOutputFile()
  190. {
  191. $this->fileIn = $this->generateTmpFile(1024);
  192. $this->fileOut = $this->generateTmpFile(1024);
  193. $this->setExpectedException('Zend\Crypt\Exception\InvalidArgumentException',
  194. "The file {$this->fileOut} already exists");
  195. $this->fileCipher->setKey('test');
  196. $this->fileCipher->encrypt($this->fileIn, $this->fileOut);
  197. }
  198. public function testDecryptFileInvalidOutputFile()
  199. {
  200. $this->fileIn = $this->generateTmpFile(1024);
  201. $this->fileOut = $this->generateTmpFile(1024);
  202. $this->setExpectedException('Zend\Crypt\Exception\InvalidArgumentException',
  203. "The file {$this->fileOut} already exists");
  204. $this->fileCipher->setKey('test');
  205. $this->fileCipher->decrypt($this->fileIn, $this->fileOut);
  206. }
  207. /**
  208. * Generate a temporary file with a selected size
  209. *
  210. * @param string $size
  211. * @param string $content
  212. * @return string
  213. */
  214. protected function generateTmpFile($size, $content = 'A')
  215. {
  216. $fileName = sys_get_temp_dir() . '/' . uniqid('ZF2_FileCipher_test');
  217. $num = $size / strlen($content) + 1;
  218. $content = str_repeat('A', $size / strlen($content) + 1);
  219. file_put_contents($fileName, substr($content, 0, $size));
  220. return $fileName;
  221. }
  222. }