EncryptorTest.php 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Framework\Encryption;
  7. class EncryptorTest extends \PHPUnit\Framework\TestCase
  8. {
  9. /**
  10. * @var \Magento\Framework\Encryption\Encryptor
  11. */
  12. private $encryptor;
  13. protected function setUp()
  14. {
  15. $this->encryptor = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->create(
  16. \Magento\Framework\Encryption\Encryptor::class
  17. );
  18. }
  19. public function testEncryptDecrypt()
  20. {
  21. $this->assertEquals('', $this->encryptor->decrypt($this->encryptor->encrypt('')));
  22. $this->assertEquals('test', $this->encryptor->decrypt($this->encryptor->encrypt('test')));
  23. }
  24. /**
  25. * @param string $key
  26. * @dataProvider validEncryptionKeyDataProvider
  27. */
  28. public function testValidateKey($key)
  29. {
  30. $this->encryptor->validateKey($key);
  31. }
  32. public function validEncryptionKeyDataProvider()
  33. {
  34. return [
  35. '32 numbers' => ['12345678901234567890123456789012'],
  36. '32 characters' => ['aBcdeFghIJKLMNOPQRSTUvwxYzabcdef'],
  37. '32 special characters' => ['!@#$%^&*()_+~`:;"<>,.?/|*&^%$#@!'],
  38. '32 combination' =>['1234eFghI1234567^&*(890123456789'],
  39. ];
  40. }
  41. /**
  42. * @expectedException \Exception
  43. * @expectedExceptionMessage Encryption key must be 32 character string without any white space.
  44. *
  45. * @param string $key
  46. * @dataProvider invalidEncryptionKeyDataProvider
  47. */
  48. public function testValidateKeyInvalid($key)
  49. {
  50. $this->encryptor->validateKey($key);
  51. }
  52. public function invalidEncryptionKeyDataProvider()
  53. {
  54. return [
  55. 'empty string' => [''],
  56. 'leading space' => [' 1234567890123456789012345678901'],
  57. 'tailing space' => ['1234567890123456789012345678901 '],
  58. 'space in the middle' => ['12345678901 23456789012345678901'],
  59. 'tab in the middle' => ['12345678901 23456789012345678'],
  60. 'return in the middle' => ['12345678901
  61. 23456789012345678901'],
  62. '31 characters' => ['1234567890123456789012345678901'],
  63. '33 characters' => ['123456789012345678901234567890123'],
  64. ];
  65. }
  66. }