OpenSslTest.php 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  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. * @return void
  32. */
  33. public function setUp()
  34. {
  35. parent::setUp();
  36. $this->skipIf(!function_exists('openssl_encrypt'), 'No openssl skipping tests');
  37. $this->crypt = new OpenSsl();
  38. }
  39. /**
  40. * Test encrypt/decrypt.
  41. *
  42. * @return void
  43. */
  44. public function testEncryptDecrypt()
  45. {
  46. $txt = 'The quick brown fox';
  47. $key = 'This key is enough bytes';
  48. $result = $this->crypt->encrypt($txt, $key);
  49. $this->assertNotEquals($txt, $result, 'Should be encrypted.');
  50. $this->assertNotEquals($result, $this->crypt->encrypt($txt, $key), 'Each result is unique.');
  51. $this->assertEquals($txt, $this->crypt->decrypt($result, $key));
  52. }
  53. /**
  54. * Test that changing the key causes decryption to fail.
  55. *
  56. * @return void
  57. */
  58. public function testDecryptKeyFailure()
  59. {
  60. $txt = 'The quick brown fox';
  61. $key = 'This key is enough bytes';
  62. $result = $this->crypt->encrypt($txt, $key);
  63. $key = 'Not the same key.';
  64. $this->assertNull($this->crypt->decrypt($txt, $key), 'Modified key will fail.');
  65. }
  66. }