EncryptTestBase.php 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. <?php
  2. /**
  3. * Test Base for Encryption Tests. Encryption tests need to extend this Class.
  4. * Checks simple encode and decode function and Provides Keys
  5. *
  6. * @group ko7
  7. * @group ko7.encrypt
  8. *
  9. * @package KO7/Encrypt
  10. * @category Test
  11. *
  12. * @copyright (c) 2007-2012 Kohana Team
  13. * @copyright (c) 2016-2018 Koseven Team
  14. * @license https://koseven.dev/LICENSE
  15. */
  16. abstract class EncryptTestBase extends Unittest_TestCase {
  17. /**
  18. * 128 bit encryption key
  19. * @var string
  20. */
  21. const KEY16 = '0123456789012345';
  22. /**
  23. * 256 bit encryption key
  24. * @var string
  25. */
  26. const KEY32 = '01234567890123456789012345678901';
  27. /**
  28. * Should be called from every test
  29. *
  30. * @return void
  31. */
  32. public function setUp(): void
  33. {
  34. parent::setUp();
  35. // clear instances
  36. Encrypt::$instances = [];
  37. }
  38. /**
  39. * Checks if encoded string is same after decoding
  40. * WARNING: This could also pass if encryption is not
  41. * implemented correctly! Please ensure you do KAT tests!
  42. *
  43. * @dataProvider provider_encode_and_decode
  44. *
  45. * @param string $plaintext
  46. *
  47. * @return void
  48. * @throws KO7_Exception
  49. */
  50. public function test_encode_and_decode(string $plaintext)
  51. {
  52. $instance = Encrypt::instance();
  53. $this->assertEquals($plaintext, $instance->decode($instance->encode($plaintext)));
  54. }
  55. /**
  56. * Test Encrypt class initialization
  57. *
  58. * @return void
  59. * @throws KO7_Exception
  60. */
  61. public function test_initialization()
  62. {
  63. $this->assertCount(0, Encrypt::$instances);
  64. Encrypt::instance();
  65. Encrypt::instance(Encrypt::$default);
  66. $this->assertCount(1, Encrypt::$instances);
  67. $this->assertArrayHasKey('default', Encrypt::$instances);
  68. $this->set_config(KO7::$config->load('encrypt')->default, 'secondary');
  69. Encrypt::instance('secondary');
  70. $this->assertCount(2, Encrypt::$instances);
  71. $this->assertArrayHasKey('secondary', Encrypt::$instances);
  72. }
  73. /**
  74. * Overwrites Configuration Values
  75. *
  76. * @param array $config
  77. * @param string|null $name
  78. *
  79. * @return void
  80. * @throws KO7_Exception
  81. */
  82. public function set_config(array $config, string $name = NULL)
  83. {
  84. if ($name === NULL) {
  85. $name = Encrypt::$default;
  86. }
  87. KO7::$config->load('encrypt')->set($name, $config);
  88. }
  89. /**
  90. * Data source for encode_and_decode test
  91. *
  92. * @return array
  93. */
  94. public function provider_encode_and_decode(): array
  95. {
  96. return [
  97. [
  98. 'abcdefghijklm',
  99. ],
  100. [
  101. '777888000',
  102. ],
  103. [
  104. 'verylongTEXTwithSTUFFandnumbers0123456789',
  105. ],
  106. ];
  107. }
  108. }