CsrfTokenManagerTest.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. <?php
  2. /**
  3. * @author Lukas Reschke <lukas@owncloud.com>
  4. *
  5. * @copyright Copyright (c) 2016, ownCloud, Inc.
  6. * @license AGPL-3.0
  7. *
  8. * This code is free software: you can redistribute it and/or modify
  9. * it under the terms of the GNU Affero General Public License, version 3,
  10. * as published by the Free Software Foundation.
  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, version 3,
  18. * along with this program. If not, see <http://www.gnu.org/licenses/>
  19. *
  20. */
  21. class CsrfTokenManagerTest extends \Test\TestCase {
  22. /** @var \OC\Security\CSRF\CsrfTokenManager */
  23. private $csrfTokenManager;
  24. /** @var \OC\Security\CSRF\CsrfTokenGenerator */
  25. private $tokenGenerator;
  26. /** @var \OC\Security\CSRF\TokenStorage\SessionStorage */
  27. private $storageInterface;
  28. public function setUp() {
  29. parent::setUp();
  30. $this->tokenGenerator = $this->getMockBuilder('\OC\Security\CSRF\CsrfTokenGenerator')
  31. ->disableOriginalConstructor()->getMock();
  32. $this->storageInterface = $this->getMockBuilder('\OC\Security\CSRF\TokenStorage\SessionStorage')
  33. ->disableOriginalConstructor()->getMock();
  34. $this->csrfTokenManager = new \OC\Security\CSRF\CsrfTokenManager(
  35. $this->tokenGenerator,
  36. $this->storageInterface
  37. );
  38. }
  39. public function testGetTokenWithExistingToken() {
  40. $this->storageInterface
  41. ->expects($this->once())
  42. ->method('hasToken')
  43. ->willReturn(true);
  44. $this->storageInterface
  45. ->expects($this->once())
  46. ->method('getToken')
  47. ->willReturn('MyExistingToken');
  48. $expected = new \OC\Security\CSRF\CsrfToken('MyExistingToken');
  49. $this->assertEquals($expected, $this->csrfTokenManager->getToken());
  50. }
  51. public function testGetTokenWithoutExistingToken() {
  52. $this->storageInterface
  53. ->expects($this->once())
  54. ->method('hasToken')
  55. ->willReturn(false);
  56. $this->tokenGenerator
  57. ->expects($this->once())
  58. ->method('generateToken')
  59. ->willReturn('MyNewToken');
  60. $this->storageInterface
  61. ->expects($this->once())
  62. ->method('setToken')
  63. ->with('MyNewToken');
  64. $expected = new \OC\Security\CSRF\CsrfToken('MyNewToken');
  65. $this->assertEquals($expected, $this->csrfTokenManager->getToken());
  66. }
  67. public function testRefreshToken() {
  68. $this->tokenGenerator
  69. ->expects($this->once())
  70. ->method('generateToken')
  71. ->willReturn('MyNewToken');
  72. $this->storageInterface
  73. ->expects($this->once())
  74. ->method('setToken')
  75. ->with('MyNewToken');
  76. $expected = new \OC\Security\CSRF\CsrfToken('MyNewToken');
  77. $this->assertEquals($expected, $this->csrfTokenManager->refreshToken());
  78. }
  79. public function testRemoveToken() {
  80. $this->storageInterface
  81. ->expects($this->once())
  82. ->method('removeToken');
  83. $this->csrfTokenManager->removeToken();
  84. }
  85. public function testIsTokenValidWithoutToken() {
  86. $this->storageInterface
  87. ->expects($this->once())
  88. ->method('hasToken')
  89. ->willReturn(false);
  90. $token = new \OC\Security\CSRF\CsrfToken('Token');
  91. $this->assertSame(false, $this->csrfTokenManager->isTokenValid($token));
  92. }
  93. public function testIsTokenValidWithWrongToken() {
  94. $this->storageInterface
  95. ->expects($this->once())
  96. ->method('hasToken')
  97. ->willReturn(true);
  98. $token = new \OC\Security\CSRF\CsrfToken('Token');
  99. $this->storageInterface
  100. ->expects($this->once())
  101. ->method('getToken')
  102. ->willReturn('MyToken');
  103. $this->assertSame(false, $this->csrfTokenManager->isTokenValid($token));
  104. }
  105. public function testIsTokenValidWithValidToken() {
  106. $this->storageInterface
  107. ->expects($this->once())
  108. ->method('hasToken')
  109. ->willReturn(true);
  110. $token = new \OC\Security\CSRF\CsrfToken('XlQhHjgWCgBXAEI0Khl+IQEiCXN2LUcDHAQTQAc1HQs=:qgkUlg8l3m8WnkOG4XM9Az33pAt1vSVMx4hcJFsxdqc=');
  111. $this->storageInterface
  112. ->expects($this->once())
  113. ->method('getToken')
  114. ->willReturn('/3JKTq2ldmzcDr1f5zDJ7Wt0lEgqqfKF');
  115. $this->assertSame(true, $this->csrfTokenManager->isTokenValid($token));
  116. }
  117. }