MemoryCacheBackendTest.php 5.2 KB

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