MemoryCacheBackendTest.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * @copyright Copyright (c) 2017 Lukas Reschke <lukas@statuscode.ch>
  5. *
  6. * @license GNU AGPL version 3 or any later version
  7. *
  8. * This program is free software: you can redistribute it and/or modify
  9. * it under the terms of the GNU Affero General Public License as
  10. * published by the Free Software Foundation, either version 3 of the
  11. * License, or (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU Affero General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU Affero General Public License
  19. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  20. *
  21. */
  22. namespace Test\Security\RateLimiting\Backend;
  23. use OC\Security\RateLimiting\Backend\MemoryCacheBackend;
  24. use OCP\AppFramework\Utility\ITimeFactory;
  25. use OCP\ICache;
  26. use OCP\ICacheFactory;
  27. use OCP\IConfig;
  28. use Test\TestCase;
  29. class MemoryCacheBackendTest extends TestCase {
  30. /** @var IConfig|\PHPUnit\Framework\MockObject\MockObject */
  31. private $config;
  32. /** @var ICacheFactory|\PHPUnit\Framework\MockObject\MockObject */
  33. private $cacheFactory;
  34. /** @var ITimeFactory|\PHPUnit\Framework\MockObject\MockObject */
  35. private $timeFactory;
  36. /** @var ICache|\PHPUnit\Framework\MockObject\MockObject */
  37. private $cache;
  38. /** @var MemoryCacheBackend */
  39. private $memoryCache;
  40. protected function setUp(): void {
  41. parent::setUp();
  42. $this->config = $this->createMock(IConfig::class);
  43. $this->cacheFactory = $this->createMock(ICacheFactory::class);
  44. $this->timeFactory = $this->createMock(ITimeFactory::class);
  45. $this->cache = $this->createMock(ICache::class);
  46. $this->cacheFactory
  47. ->expects($this->once())
  48. ->method('createDistributed')
  49. ->with('OC\Security\RateLimiting\Backend\MemoryCacheBackend')
  50. ->willReturn($this->cache);
  51. $this->config->method('getSystemValueBool')
  52. ->with('ratelimit.protection.enabled')
  53. ->willReturn(true);
  54. $this->memoryCache = new MemoryCacheBackend(
  55. $this->config,
  56. $this->cacheFactory,
  57. $this->timeFactory
  58. );
  59. }
  60. public function testGetAttemptsWithNoAttemptsBefore() {
  61. $this->cache
  62. ->expects($this->once())
  63. ->method('get')
  64. ->with('eea460b8d756885099c7f0a4c083bf6a745069ee4a301984e726df58fd4510bffa2dac4b7fd5d835726a6753ffa8343ba31c7e902bbef78fc68c2e743667cb4b')
  65. ->willReturn(null);
  66. $this->assertSame(0, $this->memoryCache->getAttempts('Method', 'User'));
  67. }
  68. public function testGetAttempts() {
  69. $this->timeFactory
  70. ->expects($this->once())
  71. ->method('getTime')
  72. ->willReturn(210);
  73. $this->cache
  74. ->expects($this->once())
  75. ->method('get')
  76. ->with('eea460b8d756885099c7f0a4c083bf6a745069ee4a301984e726df58fd4510bffa2dac4b7fd5d835726a6753ffa8343ba31c7e902bbef78fc68c2e743667cb4b')
  77. ->willReturn(json_encode([
  78. '1',
  79. '2',
  80. '87',
  81. '223',
  82. '223',
  83. '224',
  84. ]));
  85. $this->assertSame(3, $this->memoryCache->getAttempts('Method', 'User'));
  86. }
  87. public function testRegisterAttemptWithNoAttemptsBefore() {
  88. $this->timeFactory
  89. ->expects($this->once())
  90. ->method('getTime')
  91. ->willReturn(123);
  92. $this->cache
  93. ->expects($this->once())
  94. ->method('get')
  95. ->with('eea460b8d756885099c7f0a4c083bf6a745069ee4a301984e726df58fd4510bffa2dac4b7fd5d835726a6753ffa8343ba31c7e902bbef78fc68c2e743667cb4b')
  96. ->willReturn(null);
  97. $this->cache
  98. ->expects($this->once())
  99. ->method('set')
  100. ->with(
  101. 'eea460b8d756885099c7f0a4c083bf6a745069ee4a301984e726df58fd4510bffa2dac4b7fd5d835726a6753ffa8343ba31c7e902bbef78fc68c2e743667cb4b',
  102. json_encode(['223'])
  103. );
  104. $this->memoryCache->registerAttempt('Method', 'User', 100);
  105. }
  106. public function testRegisterAttempt() {
  107. $this->timeFactory
  108. ->expects($this->once())
  109. ->method('getTime')
  110. ->willReturn(86);
  111. $this->cache
  112. ->expects($this->once())
  113. ->method('get')
  114. ->with('eea460b8d756885099c7f0a4c083bf6a745069ee4a301984e726df58fd4510bffa2dac4b7fd5d835726a6753ffa8343ba31c7e902bbef78fc68c2e743667cb4b')
  115. ->willReturn(json_encode([
  116. '1',
  117. '2',
  118. '87',
  119. '123',
  120. '123',
  121. '124',
  122. ]));
  123. $this->cache
  124. ->expects($this->once())
  125. ->method('set')
  126. ->with(
  127. 'eea460b8d756885099c7f0a4c083bf6a745069ee4a301984e726df58fd4510bffa2dac4b7fd5d835726a6753ffa8343ba31c7e902bbef78fc68c2e743667cb4b',
  128. json_encode([
  129. '87',
  130. '123',
  131. '123',
  132. '124',
  133. '186',
  134. ])
  135. );
  136. $this->memoryCache->registerAttempt('Method', 'User', 100);
  137. }
  138. }