OpenSslTest.php 1.9 KB

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