CookieEncryptedUsingControllerTest.php 5.1 KB

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