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