CookieEncryptedUsingControllerTest.php 5.2 KB

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