CookieEncryptedUsingControllerTest.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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 Project
  12. * @since 3.1.6
  13. * @license https://opensource.org/licenses/mit-license.php MIT License
  14. */
  15. namespace Cake\Test\TestCase\Controller;
  16. use Cake\Routing\Router;
  17. use Cake\TestSuite\IntegrationTestCase;
  18. use Cake\Utility\Security;
  19. /**
  20. * CookieEncryptedUsingControllerTest class
  21. */
  22. class CookieEncryptedUsingControllerTest extends IntegrationTestCase
  23. {
  24. /**
  25. * reset environment.
  26. *
  27. * @return void
  28. */
  29. public function setUp()
  30. {
  31. parent::setUp();
  32. static::setAppNamespace();
  33. Security::setSalt('abcdabcdabcdabcdabcdabcdabcdabcdabcd');
  34. Router::connect('/:controller/:action/*', [], ['routeClass' => 'InflectedRoute']);
  35. $this->useHttpServer(true);
  36. }
  37. /**
  38. * Can encrypt/decrypt the cookie value.
  39. */
  40. public function testCanEncryptAndDecryptWithAes()
  41. {
  42. $this->cookieEncrypted('NameOfCookie', 'Value of Cookie', 'aes');
  43. $this->get('/cookie_component_test/view/');
  44. $this->assertStringStartsWith('Q2FrZQ==.', $this->viewVariable('ValueFromRequest'), 'Encrypted');
  45. $this->assertEquals('Value of Cookie', $this->viewVariable('ValueFromCookieComponent'), 'Decrypted');
  46. }
  47. /**
  48. * Can encrypt/decrypt the cookie value by default.
  49. */
  50. public function testCanEncryptAndDecryptCookieValue()
  51. {
  52. $this->cookieEncrypted('NameOfCookie', 'Value of Cookie');
  53. $this->get('/cookie_component_test/view/');
  54. $this->assertStringStartsWith('Q2FrZQ==.', $this->viewVariable('ValueFromRequest'), 'Encrypted');
  55. $this->assertEquals('Value of Cookie', $this->viewVariable('ValueFromCookieComponent'), 'Decrypted');
  56. }
  57. /**
  58. * Can encrypt/decrypt even if the cookie value are array.
  59. */
  60. public function testCanEncryptAndDecryptEvenIfCookieValueIsArray()
  61. {
  62. $this->cookieEncrypted('NameOfCookie', ['Value1 of Cookie', 'Value2 of Cookie']);
  63. $this->get('/cookie_component_test/view/');
  64. $this->assertStringStartsWith('Q2FrZQ==.', $this->viewVariable('ValueFromRequest'), 'Encrypted');
  65. $this->assertEquals(
  66. ['Value1 of Cookie', 'Value2 of Cookie'],
  67. $this->viewVariable('ValueFromCookieComponent'),
  68. 'Decrypted'
  69. );
  70. }
  71. /**
  72. * Can specify the encryption key.
  73. */
  74. public function testCanSpecifyEncryptionKey()
  75. {
  76. $key = 'another salt xxxxxxxxxxxxxxxxxxx';
  77. $this->cookieEncrypted('NameOfCookie', 'Value of Cookie', 'aes', $key);
  78. $this->get('/cookie_component_test/view/' . urlencode($key));
  79. $this->assertStringStartsWith('Q2FrZQ==.', $this->viewVariable('ValueFromRequest'), 'Encrypted');
  80. $this->assertEquals('Value of Cookie', $this->viewVariable('ValueFromCookieComponent'), 'Decrypted');
  81. }
  82. /**
  83. * Can be used in Security::setSalt() as the encryption key.
  84. */
  85. public function testCanBeUsedSecuritySaltAsEncryptionKey()
  86. {
  87. $key = 'another salt xxxxxxxxxxxxxxxxxxx';
  88. Security::setSalt($key);
  89. $this->cookieEncrypted('NameOfCookie', 'Value of Cookie', 'aes');
  90. $this->get('/cookie_component_test/view/' . urlencode($key));
  91. $this->assertStringStartsWith('Q2FrZQ==.', $this->viewVariable('ValueFromRequest'), 'Encrypted');
  92. $this->assertEquals('Value of Cookie', $this->viewVariable('ValueFromCookieComponent'), 'Decrypted');
  93. }
  94. /**
  95. * Can AssertCookie even if the value is encrypted by
  96. * the CookieComponent.
  97. */
  98. public function testCanAssertCookieEncrypted()
  99. {
  100. $this->get('/cookie_component_test/set_cookie');
  101. $this->assertCookieEncrypted('abc', 'NameOfCookie');
  102. }
  103. /**
  104. * Can AssertCookie even if encrypted with the aes.
  105. */
  106. public function testCanAssertCookieEncryptedWithAes()
  107. {
  108. $this->get('/cookie_component_test/set_cookie');
  109. $this->assertCookieEncrypted('abc', 'NameOfCookie', 'aes');
  110. }
  111. /**
  112. * Can AssertCookie even if encrypted with the another
  113. * encrypted key.
  114. */
  115. public function testCanAssertCookieEncryptedWithAnotherEncryptionKey()
  116. {
  117. $key = 'another salt xxxxxxxxxxxxxxxxxxx';
  118. Security::setSalt($key);
  119. $this->get('/cookie_component_test/set_cookie');
  120. $this->assertCookieEncrypted('abc', 'NameOfCookie', 'aes', $key);
  121. }
  122. /**
  123. * Can AssertCookie even if encrypted with the aes when using PSR7 server.
  124. */
  125. public function testCanAssertCookieEncryptedWithAesWhenUsingPsr7()
  126. {
  127. $this->useHttpServer(true);
  128. $this->get('/cookie_component_test/set_cookie');
  129. $this->assertCookieEncrypted('abc', 'NameOfCookie', 'aes');
  130. }
  131. }