MemoryCacheBackendTest.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * SPDX-FileCopyrightText: 2017 Nextcloud GmbH and Nextcloud contributors
  5. * SPDX-License-Identifier: AGPL-3.0-or-later
  6. */
  7. namespace Test\Security\RateLimiting\Backend;
  8. use OC\Security\RateLimiting\Backend\MemoryCacheBackend;
  9. use OCP\AppFramework\Utility\ITimeFactory;
  10. use OCP\ICache;
  11. use OCP\ICacheFactory;
  12. use OCP\IConfig;
  13. use Test\TestCase;
  14. class MemoryCacheBackendTest extends TestCase {
  15. /** @var IConfig|\PHPUnit\Framework\MockObject\MockObject */
  16. private $config;
  17. /** @var ICacheFactory|\PHPUnit\Framework\MockObject\MockObject */
  18. private $cacheFactory;
  19. /** @var ITimeFactory|\PHPUnit\Framework\MockObject\MockObject */
  20. private $timeFactory;
  21. /** @var ICache|\PHPUnit\Framework\MockObject\MockObject */
  22. private $cache;
  23. /** @var MemoryCacheBackend */
  24. private $memoryCache;
  25. protected function setUp(): void {
  26. parent::setUp();
  27. $this->config = $this->createMock(IConfig::class);
  28. $this->cacheFactory = $this->createMock(ICacheFactory::class);
  29. $this->timeFactory = $this->createMock(ITimeFactory::class);
  30. $this->cache = $this->createMock(ICache::class);
  31. $this->cacheFactory
  32. ->expects($this->once())
  33. ->method('createDistributed')
  34. ->with('OC\Security\RateLimiting\Backend\MemoryCacheBackend')
  35. ->willReturn($this->cache);
  36. $this->config->method('getSystemValueBool')
  37. ->with('ratelimit.protection.enabled')
  38. ->willReturn(true);
  39. $this->memoryCache = new MemoryCacheBackend(
  40. $this->config,
  41. $this->cacheFactory,
  42. $this->timeFactory
  43. );
  44. }
  45. public function testGetAttemptsWithNoAttemptsBefore() {
  46. $this->cache
  47. ->expects($this->once())
  48. ->method('get')
  49. ->with('eea460b8d756885099c7f0a4c083bf6a745069ee4a301984e726df58fd4510bffa2dac4b7fd5d835726a6753ffa8343ba31c7e902bbef78fc68c2e743667cb4b')
  50. ->willReturn(null);
  51. $this->assertSame(0, $this->memoryCache->getAttempts('Method', 'User'));
  52. }
  53. public function testGetAttempts() {
  54. $this->timeFactory
  55. ->expects($this->once())
  56. ->method('getTime')
  57. ->willReturn(210);
  58. $this->cache
  59. ->expects($this->once())
  60. ->method('get')
  61. ->with('eea460b8d756885099c7f0a4c083bf6a745069ee4a301984e726df58fd4510bffa2dac4b7fd5d835726a6753ffa8343ba31c7e902bbef78fc68c2e743667cb4b')
  62. ->willReturn(json_encode([
  63. '1',
  64. '2',
  65. '87',
  66. '223',
  67. '223',
  68. '224',
  69. ]));
  70. $this->assertSame(3, $this->memoryCache->getAttempts('Method', 'User'));
  71. }
  72. public function testRegisterAttemptWithNoAttemptsBefore() {
  73. $this->timeFactory
  74. ->expects($this->once())
  75. ->method('getTime')
  76. ->willReturn(123);
  77. $this->cache
  78. ->expects($this->once())
  79. ->method('get')
  80. ->with('eea460b8d756885099c7f0a4c083bf6a745069ee4a301984e726df58fd4510bffa2dac4b7fd5d835726a6753ffa8343ba31c7e902bbef78fc68c2e743667cb4b')
  81. ->willReturn(null);
  82. $this->cache
  83. ->expects($this->once())
  84. ->method('set')
  85. ->with(
  86. 'eea460b8d756885099c7f0a4c083bf6a745069ee4a301984e726df58fd4510bffa2dac4b7fd5d835726a6753ffa8343ba31c7e902bbef78fc68c2e743667cb4b',
  87. json_encode(['223'])
  88. );
  89. $this->memoryCache->registerAttempt('Method', 'User', 100);
  90. }
  91. public function testRegisterAttempt() {
  92. $this->timeFactory
  93. ->expects($this->once())
  94. ->method('getTime')
  95. ->willReturn(86);
  96. $this->cache
  97. ->expects($this->once())
  98. ->method('get')
  99. ->with('eea460b8d756885099c7f0a4c083bf6a745069ee4a301984e726df58fd4510bffa2dac4b7fd5d835726a6753ffa8343ba31c7e902bbef78fc68c2e743667cb4b')
  100. ->willReturn(json_encode([
  101. '1',
  102. '2',
  103. '87',
  104. '123',
  105. '123',
  106. '124',
  107. ]));
  108. $this->cache
  109. ->expects($this->once())
  110. ->method('set')
  111. ->with(
  112. 'eea460b8d756885099c7f0a4c083bf6a745069ee4a301984e726df58fd4510bffa2dac4b7fd5d835726a6753ffa8343ba31c7e902bbef78fc68c2e743667cb4b',
  113. json_encode([
  114. '87',
  115. '123',
  116. '123',
  117. '124',
  118. '186',
  119. ])
  120. );
  121. $this->memoryCache->registerAttempt('Method', 'User', 100);
  122. }
  123. }