CookieEncryptedUsingControllerTest.php 5.4 KB

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