CookieEncryptedUsingControllerTest.php 5.3 KB

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