OpenSslTest.php 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. <?php
  2. /**
  3. * CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
  4. * Copyright (c) Cake Software Foundation, Inc. (https://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. (https://cakefoundation.org)
  11. * @link https://cakephp.org CakePHP(tm) Project
  12. * @since 3.0.0
  13. * @license https://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. * @var OpenSsl
  25. */
  26. private $crypt;
  27. /**
  28. * Setup function.
  29. *
  30. * @return void
  31. */
  32. public function setUp()
  33. {
  34. parent::setUp();
  35. $this->skipIf(!function_exists('openssl_encrypt'), 'No openssl skipping tests');
  36. $this->crypt = new OpenSsl();
  37. }
  38. /**
  39. * testRijndael method
  40. *
  41. * @return void
  42. */
  43. public function testRijndael()
  44. {
  45. $this->expectException(\LogicException::class);
  46. $txt = 'The quick brown fox jumped over the lazy dog.';
  47. $key = 'DYhG93b0qyJfIxfs2guVoUubWwvniR2G0FgaC9mi';
  48. $this->crypt->rijndael($txt, $key, 'encrypt');
  49. }
  50. /**
  51. * Test encrypt/decrypt.
  52. *
  53. * @return void
  54. */
  55. public function testEncryptDecrypt()
  56. {
  57. $txt = 'The quick brown fox';
  58. $key = 'This key is enough bytes';
  59. $result = $this->crypt->encrypt($txt, $key);
  60. $this->assertNotEquals($txt, $result, 'Should be encrypted.');
  61. $this->assertNotEquals($result, $this->crypt->encrypt($txt, $key), 'Each result is unique.');
  62. $this->assertEquals($txt, $this->crypt->decrypt($result, $key));
  63. }
  64. /**
  65. * Test that changing the key causes decryption to fail.
  66. *
  67. * @return void
  68. */
  69. public function testDecryptKeyFailure()
  70. {
  71. $txt = 'The quick brown fox';
  72. $key = 'This key is enough bytes';
  73. $result = $this->crypt->encrypt($txt, $key);
  74. $key = 'Not the same key.';
  75. $this->assertFalse($this->crypt->decrypt($txt, $key), 'Modified key will fail.');
  76. }
  77. }