OpenSslTest.php 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. <?php
  2. /**
  3. * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
  4. * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  5. *
  6. * Licensed under The MIT License
  7. * For full copyright and license information, please see the LICENSE.txt
  8. * Redistributions of files must retain the above copyright notice.
  9. *
  10. * @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  11. * @link http://cakephp.org CakePHP(tm) Project
  12. * @since 3.0.0
  13. * @license http://www.opensource.org/licenses/mit-license.php MIT License
  14. */
  15. namespace Cake\Test\TestCase\Utility\Crypto;
  16. use Cake\TestSuite\TestCase;
  17. use Cake\Utility\Crypto\OpenSsl;
  18. /**
  19. * Openssl engine tests.
  20. */
  21. class OpenSslTest extends TestCase
  22. {
  23. /**
  24. * Setup function.
  25. *
  26. * @return void
  27. */
  28. public function setUp()
  29. {
  30. parent::setUp();
  31. $this->skipIf(!function_exists('openssl_encrypt'), 'No openssl skipping tests');
  32. $this->crypt = new OpenSsl();
  33. }
  34. /**
  35. * testRijndael method
  36. *
  37. * @expectedException \LogicException
  38. * @return void
  39. */
  40. public function testRijndael()
  41. {
  42. $txt = 'The quick brown fox jumped over the lazy dog.';
  43. $key = 'DYhG93b0qyJfIxfs2guVoUubWwvniR2G0FgaC9mi';
  44. $this->crypt->rijndael($txt, $key, 'encrypt');
  45. }
  46. /**
  47. * Test encrypt/decrypt.
  48. *
  49. * @return void
  50. */
  51. public function testEncryptDecrypt()
  52. {
  53. $txt = 'The quick brown fox';
  54. $key = 'This key is enough bytes';
  55. $result = $this->crypt->encrypt($txt, $key);
  56. $this->assertNotEquals($txt, $result, 'Should be encrypted.');
  57. $this->assertNotEquals($result, $this->crypt->encrypt($txt, $key), 'Each result is unique.');
  58. $this->assertEquals($txt, $this->crypt->decrypt($result, $key));
  59. }
  60. /**
  61. * Test that changing the key causes decryption to fail.
  62. *
  63. * @return void
  64. */
  65. public function testDecryptKeyFailure()
  66. {
  67. $txt = 'The quick brown fox';
  68. $key = 'This key is enough bytes';
  69. $result = $this->crypt->encrypt($txt, $key);
  70. $key = 'Not the same key.';
  71. $this->assertFalse($this->crypt->decrypt($txt, $key), 'Modified key will fail.');
  72. }
  73. }