From 378cc922c429524b872e83c7b3842eb86bc4b770 Mon Sep 17 00:00:00 2001 From: Lukas Reschke Date: Mon, 6 Sep 2021 17:31:36 +0200 Subject: Adjust logic to store period instead of current timestamp Signed-off-by: Lukas Reschke --- .../Backend/MemoryCacheBackendTest.php | 149 +++++++++++++++++++++ .../RateLimiting/Backend/MemoryCacheTest.php | 149 --------------------- tests/lib/Security/RateLimiting/LimiterTest.php | 13 -- 3 files changed, 149 insertions(+), 162 deletions(-) create mode 100644 tests/lib/Security/RateLimiting/Backend/MemoryCacheBackendTest.php delete mode 100644 tests/lib/Security/RateLimiting/Backend/MemoryCacheTest.php (limited to 'tests/lib/Security') diff --git a/tests/lib/Security/RateLimiting/Backend/MemoryCacheBackendTest.php b/tests/lib/Security/RateLimiting/Backend/MemoryCacheBackendTest.php new file mode 100644 index 00000000000..8bbf88e8a8e --- /dev/null +++ b/tests/lib/Security/RateLimiting/Backend/MemoryCacheBackendTest.php @@ -0,0 +1,149 @@ + + * + * @license GNU AGPL version 3 or any later version + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + * + */ + +namespace Test\Security\RateLimiting\Backend; + +use OC\Security\RateLimiting\Backend\MemoryCacheBackend; +use OCP\AppFramework\Utility\ITimeFactory; +use OCP\ICache; +use OCP\ICacheFactory; +use Test\TestCase; + +class MemoryCacheBackendTest extends TestCase { + /** @var ICacheFactory|\PHPUnit\Framework\MockObject\MockObject */ + private $cacheFactory; + /** @var ITimeFactory|\PHPUnit\Framework\MockObject\MockObject */ + private $timeFactory; + /** @var ICache|\PHPUnit\Framework\MockObject\MockObject */ + private $cache; + /** @var MemoryCacheBackend */ + private $memoryCache; + + protected function setUp(): void { + parent::setUp(); + + $this->cacheFactory = $this->createMock(ICacheFactory::class); + $this->timeFactory = $this->createMock(ITimeFactory::class); + $this->cache = $this->createMock(ICache::class); + + $this->cacheFactory + ->expects($this->once()) + ->method('createDistributed') + ->with('OC\Security\RateLimiting\Backend\MemoryCacheBackend') + ->willReturn($this->cache); + + $this->memoryCache = new MemoryCacheBackend( + $this->cacheFactory, + $this->timeFactory + ); + } + + public function testGetAttemptsWithNoAttemptsBefore() { + $this->cache + ->expects($this->once()) + ->method('get') + ->with('eea460b8d756885099c7f0a4c083bf6a745069ee4a301984e726df58fd4510bffa2dac4b7fd5d835726a6753ffa8343ba31c7e902bbef78fc68c2e743667cb4b') + ->willReturn(null); + + $this->assertSame(0, $this->memoryCache->getAttempts('Method', 'User')); + } + + public function testGetAttempts() { + $this->timeFactory + ->expects($this->once()) + ->method('getTime') + ->willReturn(210); + $this->cache + ->expects($this->once()) + ->method('get') + ->with('eea460b8d756885099c7f0a4c083bf6a745069ee4a301984e726df58fd4510bffa2dac4b7fd5d835726a6753ffa8343ba31c7e902bbef78fc68c2e743667cb4b') + ->willReturn(json_encode([ + '1', + '2', + '87', + '123', + '123', + '124', + ])); + + $this->assertSame(3, $this->memoryCache->getAttempts('Method', 'User')); + } + + public function testRegisterAttemptWithNoAttemptsBefore() { + $this->timeFactory + ->expects($this->once()) + ->method('getTime') + ->willReturn(123); + + $this->cache + ->expects($this->once()) + ->method('get') + ->with('eea460b8d756885099c7f0a4c083bf6a745069ee4a301984e726df58fd4510bffa2dac4b7fd5d835726a6753ffa8343ba31c7e902bbef78fc68c2e743667cb4b') + ->willReturn(null); + $this->cache + ->expects($this->once()) + ->method('set') + ->with( + 'eea460b8d756885099c7f0a4c083bf6a745069ee4a301984e726df58fd4510bffa2dac4b7fd5d835726a6753ffa8343ba31c7e902bbef78fc68c2e743667cb4b', + json_encode(['123']) + ); + + $this->memoryCache->registerAttempt('Method', 'User', 100); + } + + public function testRegisterAttempt() { + $this->timeFactory + ->expects($this->once()) + ->method('getTime') + ->willReturn(129); + + $this->cache + ->expects($this->once()) + ->method('get') + ->with('eea460b8d756885099c7f0a4c083bf6a745069ee4a301984e726df58fd4510bffa2dac4b7fd5d835726a6753ffa8343ba31c7e902bbef78fc68c2e743667cb4b') + ->willReturn(json_encode([ + '1', + '2', + '87', + '123', + '123', + '124', + ])); + $this->cache + ->expects($this->once()) + ->method('set') + ->with( + 'eea460b8d756885099c7f0a4c083bf6a745069ee4a301984e726df58fd4510bffa2dac4b7fd5d835726a6753ffa8343ba31c7e902bbef78fc68c2e743667cb4b', + json_encode([ + '87', + '123', + '123', + '124', + '129', + ]) + ); + + $this->memoryCache->registerAttempt('Method', 'User', 100); + } +} diff --git a/tests/lib/Security/RateLimiting/Backend/MemoryCacheTest.php b/tests/lib/Security/RateLimiting/Backend/MemoryCacheTest.php deleted file mode 100644 index ff58bd5c09e..00000000000 --- a/tests/lib/Security/RateLimiting/Backend/MemoryCacheTest.php +++ /dev/null @@ -1,149 +0,0 @@ - - * - * @license GNU AGPL version 3 or any later version - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as - * published by the Free Software Foundation, either version 3 of the - * License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with this program. If not, see . - * - */ - -namespace Test\Security\RateLimiting\Backend; - -use OC\Security\RateLimiting\Backend\MemoryCache; -use OCP\AppFramework\Utility\ITimeFactory; -use OCP\ICache; -use OCP\ICacheFactory; -use Test\TestCase; - -class MemoryCacheTest extends TestCase { - /** @var ICacheFactory|\PHPUnit\Framework\MockObject\MockObject */ - private $cacheFactory; - /** @var ITimeFactory|\PHPUnit\Framework\MockObject\MockObject */ - private $timeFactory; - /** @var ICache|\PHPUnit\Framework\MockObject\MockObject */ - private $cache; - /** @var MemoryCache */ - private $memoryCache; - - protected function setUp(): void { - parent::setUp(); - - $this->cacheFactory = $this->createMock(ICacheFactory::class); - $this->timeFactory = $this->createMock(ITimeFactory::class); - $this->cache = $this->createMock(ICache::class); - - $this->cacheFactory - ->expects($this->once()) - ->method('createDistributed') - ->with('OC\Security\RateLimiting\Backend\MemoryCache') - ->willReturn($this->cache); - - $this->memoryCache = new MemoryCache( - $this->cacheFactory, - $this->timeFactory - ); - } - - public function testGetAttemptsWithNoAttemptsBefore() { - $this->cache - ->expects($this->once()) - ->method('get') - ->with('eea460b8d756885099c7f0a4c083bf6a745069ee4a301984e726df58fd4510bffa2dac4b7fd5d835726a6753ffa8343ba31c7e902bbef78fc68c2e743667cb4b') - ->willReturn(null); - - $this->assertSame(0, $this->memoryCache->getAttempts('Method', 'User', 123)); - } - - public function testGetAttempts() { - $this->timeFactory - ->expects($this->once()) - ->method('getTime') - ->willReturn(210); - $this->cache - ->expects($this->once()) - ->method('get') - ->with('eea460b8d756885099c7f0a4c083bf6a745069ee4a301984e726df58fd4510bffa2dac4b7fd5d835726a6753ffa8343ba31c7e902bbef78fc68c2e743667cb4b') - ->willReturn(json_encode([ - '1', - '2', - '87', - '123', - '123', - '124', - ])); - - $this->assertSame(3, $this->memoryCache->getAttempts('Method', 'User', 123)); - } - - public function testRegisterAttemptWithNoAttemptsBefore() { - $this->timeFactory - ->expects($this->once()) - ->method('getTime') - ->willReturn(123); - - $this->cache - ->expects($this->once()) - ->method('get') - ->with('eea460b8d756885099c7f0a4c083bf6a745069ee4a301984e726df58fd4510bffa2dac4b7fd5d835726a6753ffa8343ba31c7e902bbef78fc68c2e743667cb4b') - ->willReturn(null); - $this->cache - ->expects($this->once()) - ->method('set') - ->with( - 'eea460b8d756885099c7f0a4c083bf6a745069ee4a301984e726df58fd4510bffa2dac4b7fd5d835726a6753ffa8343ba31c7e902bbef78fc68c2e743667cb4b', - json_encode(['123']) - ); - - $this->memoryCache->registerAttempt('Method', 'User', 100); - } - - public function testRegisterAttempt() { - $this->timeFactory - ->expects($this->once()) - ->method('getTime') - ->willReturn(129); - - $this->cache - ->expects($this->once()) - ->method('get') - ->with('eea460b8d756885099c7f0a4c083bf6a745069ee4a301984e726df58fd4510bffa2dac4b7fd5d835726a6753ffa8343ba31c7e902bbef78fc68c2e743667cb4b') - ->willReturn(json_encode([ - '1', - '2', - '87', - '123', - '123', - '124', - ])); - $this->cache - ->expects($this->once()) - ->method('set') - ->with( - 'eea460b8d756885099c7f0a4c083bf6a745069ee4a301984e726df58fd4510bffa2dac4b7fd5d835726a6753ffa8343ba31c7e902bbef78fc68c2e743667cb4b', - json_encode([ - '87', - '123', - '123', - '124', - '129', - ]) - ); - - $this->memoryCache->registerAttempt('Method', 'User', 100); - } -} diff --git a/tests/lib/Security/RateLimiting/LimiterTest.php b/tests/lib/Security/RateLimiting/LimiterTest.php index 8b3509a4790..31c877cbda3 100644 --- a/tests/lib/Security/RateLimiting/LimiterTest.php +++ b/tests/lib/Security/RateLimiting/LimiterTest.php @@ -26,13 +26,10 @@ namespace Test\Security\RateLimiting; use OC\Security\RateLimiting\Backend\IBackend; use OC\Security\RateLimiting\Limiter; -use OCP\AppFramework\Utility\ITimeFactory; use OCP\IUser; use Test\TestCase; class LimiterTest extends TestCase { - /** @var ITimeFactory|\PHPUnit\Framework\MockObject\MockObject */ - private $timeFactory; /** @var IBackend|\PHPUnit\Framework\MockObject\MockObject */ private $backend; /** @var Limiter */ @@ -41,11 +38,9 @@ class LimiterTest extends TestCase { protected function setUp(): void { parent::setUp(); - $this->timeFactory = $this->createMock(ITimeFactory::class); $this->backend = $this->createMock(IBackend::class); $this->limiter = new Limiter( - $this->timeFactory, $this->backend ); } @@ -69,10 +64,6 @@ class LimiterTest extends TestCase { } public function testRegisterAnonRequestSuccess() { - $this->timeFactory - ->expects($this->once()) - ->method('getTime') - ->willReturn(2000); $this->backend ->expects($this->once()) ->method('getAttempts') @@ -126,10 +117,6 @@ class LimiterTest extends TestCase { ->method('getUID') ->willReturn('MyUid'); - $this->timeFactory - ->expects($this->once()) - ->method('getTime') - ->willReturn(2000); $this->backend ->expects($this->once()) ->method('getAttempts') -- cgit v1.2.3