/tests/Zend/Filter/Encrypt/McryptTest.php

https://github.com/Exercise/zf2 · PHP · 241 lines · 144 code · 24 blank · 73 comment · 2 complexity · 20e28d20004fc91fc161ac3ec5f7f2be MD5 · raw file

  1. <?php
  2. /**
  3. * Zend Framework
  4. *
  5. * LICENSE
  6. *
  7. * This source file is subject to the new BSD license that is bundled
  8. * with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://framework.zend.com/license/new-bsd
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@zend.com so we can send you a copy immediately.
  14. *
  15. * @category Zend
  16. * @package Zend_Filter
  17. * @subpackage UnitTests
  18. * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
  19. * @license http://framework.zend.com/license/new-bsd New BSD License
  20. * @version $Id$
  21. */
  22. namespace ZendTest\Filter\Encrypt;
  23. use Zend\Filter\Encrypt\Mcrypt as McryptEncryption;
  24. /**
  25. * @category Zend
  26. * @package Zend_Filter
  27. * @subpackage UnitTests
  28. * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
  29. * @license http://framework.zend.com/license/new-bsd New BSD License
  30. * @group Zend_Filter
  31. */
  32. class McryptTest extends \PHPUnit_Framework_TestCase
  33. {
  34. public function setUp()
  35. {
  36. if (!extension_loaded('mcrypt')) {
  37. $this->markTestSkipped('This adapter needs the mcrypt extension');
  38. }
  39. }
  40. /**
  41. * Ensures that the filter follows expected behavior
  42. *
  43. * @return void
  44. */
  45. public function testBasicMcrypt()
  46. {
  47. $filter = new McryptEncryption(array('key' => 'testkey'));
  48. $valuesExpected = array(
  49. 'STRING' => 'STRING',
  50. 'ABC1@3' => 'ABC1@3',
  51. 'A b C' => 'A B C'
  52. );
  53. $enc = $filter->getEncryption();
  54. $filter->setVector('testvect');
  55. $this->assertEquals('testkey', $enc['key']);
  56. foreach ($valuesExpected as $input => $output) {
  57. $this->assertNotEquals($output, $filter->encrypt($input));
  58. }
  59. }
  60. /**
  61. * Ensures that the vector can be set / returned
  62. *
  63. * @return void
  64. */
  65. public function testGetSetVector()
  66. {
  67. $filter = new McryptEncryption(array('key' => 'testkey'));
  68. $filter->setVector('testvect');
  69. $this->assertEquals('testvect', $filter->getVector());
  70. try {
  71. $filter->setVector('1');
  72. $this->fail();
  73. } catch (\Zend\Filter\Exception $e) {
  74. $this->assertContains('wrong size', $e->getMessage());
  75. }
  76. }
  77. /**
  78. * Ensures that the filter allows default encryption
  79. *
  80. * @return void
  81. */
  82. public function testDefaultEncryption()
  83. {
  84. $filter = new McryptEncryption(array('key' => 'testkey'));
  85. $filter->setVector('testvect');
  86. $this->assertEquals(
  87. array('key' => 'testkey',
  88. 'algorithm' => MCRYPT_BLOWFISH,
  89. 'algorithm_directory' => '',
  90. 'mode' => MCRYPT_MODE_CBC,
  91. 'mode_directory' => '',
  92. 'vector' => 'testvect',
  93. 'salt' => false),
  94. $filter->getEncryption()
  95. );
  96. }
  97. /**
  98. * Ensures that the filter allows setting options de/encryption
  99. *
  100. * @return void
  101. */
  102. public function testGetSetEncryption()
  103. {
  104. $filter = new McryptEncryption(array('key' => 'testkey'));
  105. $filter->setVector('testvect');
  106. $filter->setEncryption(
  107. array('mode' => MCRYPT_MODE_ECB,
  108. 'algorithm' => MCRYPT_3DES));
  109. $this->assertEquals(
  110. array('key' => 'testkey',
  111. 'algorithm' => MCRYPT_3DES,
  112. 'algorithm_directory' => '',
  113. 'mode' => MCRYPT_MODE_ECB,
  114. 'mode_directory' => '',
  115. 'vector' => 'testvect',
  116. 'salt' => false),
  117. $filter->getEncryption()
  118. );
  119. }
  120. /**
  121. * Ensures that the filter allows de/encryption
  122. *
  123. * @return void
  124. */
  125. public function testEncryptionWithDecryptionMcrypt()
  126. {
  127. $filter = new McryptEncryption(array('key' => 'testkey'));
  128. $filter->setVector('testvect');
  129. $output = $filter->encrypt('teststring');
  130. $this->assertNotEquals('teststring', $output);
  131. $input = $filter->decrypt($output);
  132. $this->assertEquals('teststring', trim($input));
  133. }
  134. /**
  135. * @return void
  136. */
  137. public function testConstructionWithStringKey()
  138. {
  139. $filter = new McryptEncryption('testkey');
  140. $data = $filter->getEncryption();
  141. $this->assertEquals('testkey', $data['key']);
  142. }
  143. /**
  144. * @return void
  145. */
  146. public function testConstructionWithInteger()
  147. {
  148. try {
  149. $filter = new McryptEncryption(1234);
  150. $this->fail();
  151. } catch (\Zend\Filter\Exception $e) {
  152. $this->assertContains('Invalid options argument', $e->getMessage());
  153. }
  154. }
  155. /**
  156. * @return void
  157. */
  158. public function testToString()
  159. {
  160. $filter = new McryptEncryption('testkey');
  161. $this->assertEquals('Mcrypt', $filter->toString());
  162. }
  163. /**
  164. * @return void
  165. */
  166. public function testSettingEncryptionOptions()
  167. {
  168. $filter = new McryptEncryption('testkey');
  169. $filter->setEncryption('newkey');
  170. $test = $filter->getEncryption();
  171. $this->assertEquals('newkey', $test['key']);
  172. try {
  173. $filter->setEncryption(1234);
  174. $filter->fail();
  175. } catch (\Zend\Filter\Exception $e) {
  176. $this->assertContains('Invalid options argument', $e->getMessage());
  177. }
  178. try {
  179. $filter->setEncryption(array('algorithm' => 'unknown'));
  180. $filter->fail();
  181. } catch (\Zend\Filter\Exception $e) {
  182. $this->assertContains('The algorithm', $e->getMessage());
  183. }
  184. try {
  185. $filter->setEncryption(array('mode' => 'unknown'));
  186. $filter->fail();
  187. } catch (\Zend\Filter\Exception $e) {
  188. $this->assertContains('The mode', $e->getMessage());
  189. }
  190. }
  191. /**
  192. * @return void
  193. */
  194. public function testSettingEmptyVector()
  195. {
  196. $filter = new McryptEncryption('newkey');
  197. $filter->setVector();
  198. }
  199. /**
  200. * Ensures that the filter allows de/encryption with compression
  201. *
  202. * @return void
  203. */
  204. public function testEncryptionWithDecryptionAndCompressionMcrypt()
  205. {
  206. if (!extension_loaded('bz2')) {
  207. $this->markTestSkipped('This adapter needs the bz2 extension');
  208. }
  209. $filter = new Zend_Filter_Encrypt_Mcrypt(array('key' => 'testkey'));
  210. $filter->setVector('testvect');
  211. $filter->setCompression('bz2');
  212. $output = $filter->encrypt('teststring');
  213. $this->assertNotEquals('teststring', $output);
  214. $input = $filter->decrypt($output);
  215. $this->assertEquals('teststring', trim($input));
  216. }
  217. }