ContentSecurityPolicyNonceManagerTest.php 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016 Lukas Reschke <lukas@statuscode.ch>
  4. *
  5. * @license GNU AGPL version 3 or any later version
  6. *
  7. * This program is free software: you can redistribute it and/or modify
  8. * it under the terms of the GNU Affero General Public License as
  9. * published by the Free Software Foundation, either version 3 of the
  10. * License, or (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU Affero General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Affero General Public License
  18. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  19. *
  20. */
  21. namespace Test\Security\CSP;
  22. use OC\AppFramework\Http\Request;
  23. use OC\Security\CSP\ContentSecurityPolicyNonceManager;
  24. use OC\Security\CSRF\CsrfToken;
  25. use OC\Security\CSRF\CsrfTokenManager;
  26. use Test\TestCase;
  27. class ContentSecurityPolicyNonceManagerTest extends TestCase {
  28. /** @var CsrfTokenManager */
  29. private $csrfTokenManager;
  30. /** @var Request */
  31. private $request;
  32. /** @var ContentSecurityPolicyNonceManager */
  33. private $nonceManager;
  34. public function setUp() {
  35. $this->csrfTokenManager = $this->createMock(CsrfTokenManager::class);
  36. $this->request = $this->createMock(Request::class);
  37. $this->nonceManager = new ContentSecurityPolicyNonceManager(
  38. $this->csrfTokenManager,
  39. $this->request
  40. );
  41. }
  42. public function testGetNonce() {
  43. $token = $this->createMock(CsrfToken::class);
  44. $token
  45. ->expects($this->once())
  46. ->method('getEncryptedValue')
  47. ->willReturn('MyToken');
  48. $this->csrfTokenManager
  49. ->expects($this->once())
  50. ->method('getToken')
  51. ->willReturn($token);
  52. $this->assertSame('TXlUb2tlbg==', $this->nonceManager->getNonce());
  53. $this->assertSame('TXlUb2tlbg==', $this->nonceManager->getNonce());
  54. }
  55. public function testGetNonceServerVar() {
  56. $token = 'SERVERNONCE';
  57. $this->request
  58. ->method('__isset')
  59. ->with('server')
  60. ->willReturn(true);
  61. $this->request
  62. ->method('__get')
  63. ->with('server')
  64. ->willReturn(['CSP_NONCE' => $token]);
  65. $this->assertSame($token, $this->nonceManager->getNonce());
  66. $this->assertSame($token, $this->nonceManager->getNonce());
  67. }
  68. }