MemoryCacheTest.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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\Backend;
  22. use OC\Security\RateLimiting\Backend\MemoryCache;
  23. use OCP\AppFramework\Utility\ITimeFactory;
  24. use OCP\ICache;
  25. use OCP\ICacheFactory;
  26. use Test\TestCase;
  27. class MemoryCacheTest extends TestCase {
  28. /** @var ICacheFactory|\PHPUnit_Framework_MockObject_MockObject */
  29. private $cacheFactory;
  30. /** @var ITimeFactory|\PHPUnit_Framework_MockObject_MockObject */
  31. private $timeFactory;
  32. /** @var ICache|\PHPUnit_Framework_MockObject_MockObject */
  33. private $cache;
  34. /** @var MemoryCache */
  35. private $memoryCache;
  36. public function setUp() {
  37. parent::setUp();
  38. $this->cacheFactory = $this->createMock(ICacheFactory::class);
  39. $this->timeFactory = $this->createMock(ITimeFactory::class);
  40. $this->cache = $this->createMock(ICache::class);
  41. $this->cacheFactory
  42. ->expects($this->once())
  43. ->method('createDistributed')
  44. ->with('OC\Security\RateLimiting\Backend\MemoryCache')
  45. ->willReturn($this->cache);
  46. $this->memoryCache = new MemoryCache(
  47. $this->cacheFactory,
  48. $this->timeFactory
  49. );
  50. }
  51. public function testGetAttemptsWithNoAttemptsBefore() {
  52. $this->cache
  53. ->expects($this->once())
  54. ->method('get')
  55. ->with('eea460b8d756885099c7f0a4c083bf6a745069ee4a301984e726df58fd4510bffa2dac4b7fd5d835726a6753ffa8343ba31c7e902bbef78fc68c2e743667cb4b')
  56. ->willReturn(null);
  57. $this->assertSame(0, $this->memoryCache->getAttempts('Method', 'User', 123));
  58. }
  59. public function testGetAttempts() {
  60. $this->timeFactory
  61. ->expects($this->once())
  62. ->method('getTime')
  63. ->willReturn(210);
  64. $this->cache
  65. ->expects($this->once())
  66. ->method('get')
  67. ->with('eea460b8d756885099c7f0a4c083bf6a745069ee4a301984e726df58fd4510bffa2dac4b7fd5d835726a6753ffa8343ba31c7e902bbef78fc68c2e743667cb4b')
  68. ->willReturn(json_encode([
  69. '1',
  70. '2',
  71. '87',
  72. '123',
  73. '123',
  74. '124',
  75. ]));
  76. $this->assertSame(3, $this->memoryCache->getAttempts('Method', 'User', 123));
  77. }
  78. public function testRegisterAttemptWithNoAttemptsBefore() {
  79. $this->timeFactory
  80. ->expects($this->once())
  81. ->method('getTime')
  82. ->willReturn(123);
  83. $this->cache
  84. ->expects($this->once())
  85. ->method('get')
  86. ->with('eea460b8d756885099c7f0a4c083bf6a745069ee4a301984e726df58fd4510bffa2dac4b7fd5d835726a6753ffa8343ba31c7e902bbef78fc68c2e743667cb4b')
  87. ->willReturn(null);
  88. $this->cache
  89. ->expects($this->once())
  90. ->method('set')
  91. ->with(
  92. 'eea460b8d756885099c7f0a4c083bf6a745069ee4a301984e726df58fd4510bffa2dac4b7fd5d835726a6753ffa8343ba31c7e902bbef78fc68c2e743667cb4b',
  93. json_encode(['123'])
  94. );
  95. $this->memoryCache->registerAttempt('Method', 'User', 100);
  96. }
  97. public function testRegisterAttempt() {
  98. $this->timeFactory
  99. ->expects($this->once())
  100. ->method('getTime')
  101. ->willReturn(129);
  102. $this->cache
  103. ->expects($this->once())
  104. ->method('get')
  105. ->with('eea460b8d756885099c7f0a4c083bf6a745069ee4a301984e726df58fd4510bffa2dac4b7fd5d835726a6753ffa8343ba31c7e902bbef78fc68c2e743667cb4b')
  106. ->willReturn(json_encode([
  107. '1',
  108. '2',
  109. '87',
  110. '123',
  111. '123',
  112. '124',
  113. ]));
  114. $this->cache
  115. ->expects($this->once())
  116. ->method('set')
  117. ->with(
  118. 'eea460b8d756885099c7f0a4c083bf6a745069ee4a301984e726df58fd4510bffa2dac4b7fd5d835726a6753ffa8343ba31c7e902bbef78fc68c2e743667cb4b',
  119. json_encode([
  120. '87',
  121. '123',
  122. '123',
  123. '124',
  124. '129',
  125. ])
  126. );
  127. $this->memoryCache->registerAttempt('Method', 'User', 100);
  128. }
  129. }