LimiterTest.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2017 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\RateLimiting;
  22. use OC\Security\RateLimiting\Backend\IBackend;
  23. use OC\Security\RateLimiting\Limiter;
  24. use OCP\AppFramework\Utility\ITimeFactory;
  25. use OCP\ICacheFactory;
  26. use OCP\IRequest;
  27. use OCP\IUser;
  28. use OCP\IUserSession;
  29. use Test\TestCase;
  30. class LimiterTest extends TestCase {
  31. /** @var IUserSession|\PHPUnit_Framework_MockObject_MockObject */
  32. private $userSession;
  33. /** @var IRequest|\PHPUnit_Framework_MockObject_MockObject */
  34. private $request;
  35. /** @var ITimeFactory|\PHPUnit_Framework_MockObject_MockObject */
  36. private $timeFactory;
  37. /** @var IBackend|\PHPUnit_Framework_MockObject_MockObject */
  38. private $backend;
  39. /** @var Limiter */
  40. private $limiter;
  41. public function setUp() {
  42. parent::setUp();
  43. $this->userSession = $this->createMock(IUserSession::class);
  44. $this->request = $this->createMock(IRequest::class);
  45. $this->timeFactory = $this->createMock(ITimeFactory::class);
  46. $this->backend = $this->createMock(IBackend::class);
  47. $this->limiter = new Limiter(
  48. $this->userSession,
  49. $this->request,
  50. $this->timeFactory,
  51. $this->backend
  52. );
  53. }
  54. /**
  55. * @expectedException \OC\Security\RateLimiting\Exception\RateLimitExceededException
  56. * @expectedExceptionMessage Rate limit exceeded
  57. */
  58. public function testRegisterAnonRequestExceeded() {
  59. $this->backend
  60. ->expects($this->once())
  61. ->method('getAttempts')
  62. ->with(
  63. 'MyIdentifier',
  64. '4664f0d9c88dcb7552be47b37bb52ce35977b2e60e1ac13757cf625f31f87050a41f3da064887fa87d49fd042e4c8eb20de8f10464877d3959677ab011b73a47',
  65. 100
  66. )
  67. ->willReturn(101);
  68. $this->limiter->registerAnonRequest('MyIdentifier', 100, 100, '127.0.0.1');
  69. }
  70. public function testRegisterAnonRequestSuccess() {
  71. $this->timeFactory
  72. ->expects($this->once())
  73. ->method('getTime')
  74. ->willReturn(2000);
  75. $this->backend
  76. ->expects($this->once())
  77. ->method('getAttempts')
  78. ->with(
  79. 'MyIdentifier',
  80. '4664f0d9c88dcb7552be47b37bb52ce35977b2e60e1ac13757cf625f31f87050a41f3da064887fa87d49fd042e4c8eb20de8f10464877d3959677ab011b73a47',
  81. 100
  82. )
  83. ->willReturn(99);
  84. $this->backend
  85. ->expects($this->once())
  86. ->method('registerAttempt')
  87. ->with(
  88. 'MyIdentifier',
  89. '4664f0d9c88dcb7552be47b37bb52ce35977b2e60e1ac13757cf625f31f87050a41f3da064887fa87d49fd042e4c8eb20de8f10464877d3959677ab011b73a47',
  90. 2000
  91. );
  92. $this->limiter->registerAnonRequest('MyIdentifier', 100, 100, '127.0.0.1');
  93. }
  94. /**
  95. * @expectedException \OC\Security\RateLimiting\Exception\RateLimitExceededException
  96. * @expectedExceptionMessage Rate limit exceeded
  97. */
  98. public function testRegisterUserRequestExceeded() {
  99. /** @var IUser|\PHPUnit_Framework_MockObject_MockObject $user */
  100. $user = $this->createMock(IUser::class);
  101. $user
  102. ->expects($this->once())
  103. ->method('getUID')
  104. ->willReturn('MyUid');
  105. $this->backend
  106. ->expects($this->once())
  107. ->method('getAttempts')
  108. ->with(
  109. 'MyIdentifier',
  110. 'ddb2ec50fa973fd49ecf3d816f677c8095143e944ad10485f30fb3dac85c13a346dace4dae2d0a15af91867320957bfd38a43d9eefbb74fe6919e15119b6d805',
  111. 100
  112. )
  113. ->willReturn(101);
  114. $this->limiter->registerUserRequest('MyIdentifier', 100, 100, $user);
  115. }
  116. public function testRegisterUserRequestSuccess() {
  117. /** @var IUser|\PHPUnit_Framework_MockObject_MockObject $user */
  118. $user = $this->createMock(IUser::class);
  119. $user
  120. ->expects($this->once())
  121. ->method('getUID')
  122. ->willReturn('MyUid');
  123. $this->timeFactory
  124. ->expects($this->once())
  125. ->method('getTime')
  126. ->willReturn(2000);
  127. $this->backend
  128. ->expects($this->once())
  129. ->method('getAttempts')
  130. ->with(
  131. 'MyIdentifier',
  132. 'ddb2ec50fa973fd49ecf3d816f677c8095143e944ad10485f30fb3dac85c13a346dace4dae2d0a15af91867320957bfd38a43d9eefbb74fe6919e15119b6d805',
  133. 100
  134. )
  135. ->willReturn(99);
  136. $this->backend
  137. ->expects($this->once())
  138. ->method('registerAttempt')
  139. ->with(
  140. 'MyIdentifier',
  141. 'ddb2ec50fa973fd49ecf3d816f677c8095143e944ad10485f30fb3dac85c13a346dace4dae2d0a15af91867320957bfd38a43d9eefbb74fe6919e15119b6d805',
  142. 2000
  143. );
  144. $this->limiter->registerUserRequest('MyIdentifier', 100, 100, $user);
  145. }
  146. }