|
1
|
|
|
<?php
|
|
2
|
|
|
/**
|
|
3
|
|
|
* @author Semih Serhat Karakaya <[email protected]>
|
|
4
|
|
|
*
|
|
5
|
|
|
* @copyright Copyright (c) 2017, ownCloud GmbH
|
|
6
|
|
|
* @license AGPL-3.0
|
|
7
|
|
|
*
|
|
8
|
|
|
* This code is free software: you can redistribute it and/or modify
|
|
9
|
|
|
* it under the terms of the GNU Affero General Public License, version 3,
|
|
10
|
|
|
* as published by the Free Software Foundation.
|
|
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, version 3,
|
|
18
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>
|
|
19
|
|
|
*
|
|
20
|
|
|
*/
|
|
21
|
|
|
|
|
22
|
|
|
|
|
23
|
|
|
namespace OCA\Security\Tests;
|
|
24
|
|
|
|
|
25
|
|
|
|
|
26
|
|
|
use OCA\Security\Db\DbService;
|
|
27
|
|
|
use OCA\Security\Throttle;
|
|
28
|
|
|
use OC\User\LoginException;
|
|
29
|
|
|
use OCA\Security\SecurityConfig;
|
|
30
|
|
|
use OCP\AppFramework\Utility\ITimeFactory;
|
|
31
|
|
|
use OCP\IL10N;
|
|
32
|
|
|
use Test\TestCase;
|
|
33
|
|
|
|
|
34
|
|
|
class ThrottleTest extends TestCase {
|
|
35
|
|
|
|
|
36
|
|
|
/** @var Throttle */
|
|
37
|
|
|
private $throttle;
|
|
38
|
|
|
/**
|
|
39
|
|
|
* @var \PHPUnit_Framework_MockObject_MockObject | DbService
|
|
40
|
|
|
*/
|
|
41
|
|
|
private $dbServiceMock;
|
|
42
|
|
|
/**
|
|
43
|
|
|
* @var \PHPUnit_Framework_MockObject_MockObject | SecurityConfig
|
|
44
|
|
|
*/
|
|
45
|
|
|
private $configMock;
|
|
46
|
|
|
/**
|
|
47
|
|
|
* @var \PHPUnit_Framework_MockObject_MockObject | IL10N
|
|
48
|
|
|
*/
|
|
49
|
|
|
private $lMock;
|
|
50
|
|
|
/**
|
|
51
|
|
|
* @var \PHPUnit_Framework_MockObject_MockObject | ITimeFactory
|
|
52
|
|
|
*/
|
|
53
|
|
|
private $timeFactoryMock;
|
|
54
|
|
|
|
|
55
|
|
|
public function setUp() {
|
|
56
|
|
|
parent::setUp();
|
|
57
|
|
|
|
|
58
|
|
|
$this->dbServiceMock = $this->getMockBuilder('OCA\Security\Db\DbService')
|
|
59
|
|
|
->disableOriginalConstructor()
|
|
60
|
|
|
->getMock();
|
|
61
|
|
|
$this->lMock = $this->getMockBuilder('OCP\IL10N')
|
|
62
|
|
|
->disableOriginalConstructor()
|
|
63
|
|
|
->getMock();
|
|
64
|
|
|
$this->timeFactoryMock = $this->getMockBuilder('OCP\AppFramework\Utility\ITimeFactory')
|
|
65
|
|
|
->disableOriginalConstructor()
|
|
66
|
|
|
->getMock();
|
|
67
|
|
|
$this->configMock = $this->getMockBuilder('OCA\Security\SecurityConfig')
|
|
68
|
|
|
->disableOriginalConstructor()
|
|
69
|
|
|
->getMock();
|
|
70
|
|
|
|
|
71
|
|
|
$this->throttle = new Throttle($this->dbServiceMock, $this->configMock, $this->lMock, $this->timeFactoryMock);
|
|
72
|
|
|
}
|
|
73
|
|
|
|
|
74
|
|
|
public function testAddFailedLoginAttempt() {
|
|
75
|
|
|
$this->dbServiceMock->expects($this->once())->method('addFailedLoginAttempt')
|
|
76
|
|
|
->with('test', '192.168.1.1');
|
|
77
|
|
|
|
|
78
|
|
|
$this->throttle->addFailedLoginAttempt('test', '192.168.1.1');
|
|
79
|
|
|
}
|
|
80
|
|
|
|
|
81
|
|
|
/**
|
|
82
|
|
|
* @dataProvider bruteForceTestData
|
|
83
|
|
|
*/
|
|
84
|
|
|
public function testApplyBruteForcePolicy($lastAttempt, $attemptCount, $banPeriod, $failTolerance, $time) {
|
|
85
|
|
|
$this->dbServiceMock->expects($this->once())
|
|
86
|
|
|
->method('getLastFailedLoginAttemptTimeForIp')
|
|
87
|
|
|
->with('192.168.1.1')
|
|
88
|
|
|
->will($this->returnValue($lastAttempt));
|
|
89
|
|
|
$this->dbServiceMock->expects($this->once())
|
|
90
|
|
|
->method('getSuspiciousActivityCountForIp')
|
|
91
|
|
|
->with('192.168.1.1')
|
|
92
|
|
|
->will($this->returnValue($attemptCount));
|
|
93
|
|
|
$this->configMock->expects($this->once())
|
|
94
|
|
|
->method('getBruteForceProtectionBanPeriod')
|
|
95
|
|
|
->will($this->returnValue($banPeriod));
|
|
96
|
|
|
$this->configMock->expects($this->once())
|
|
97
|
|
|
->method('getBruteForceProtectionFailTolerance')
|
|
98
|
|
|
->will($this->returnValue($failTolerance));
|
|
99
|
|
|
$this->timeFactoryMock->expects($this->once())
|
|
100
|
|
|
->method('getTime')
|
|
101
|
|
|
->will($this->returnValue($time));
|
|
102
|
|
|
$this->expectException('OC\User\LoginException');
|
|
103
|
|
|
$this->throttle->applyBruteForcePolicy('192.168.1.1');
|
|
104
|
|
|
}
|
|
105
|
|
|
public function bruteForceTestData() {
|
|
106
|
|
|
return [
|
|
107
|
|
|
[5, 5, 10, 4, 14],
|
|
108
|
|
|
[0, 3, 300, 2, 250]
|
|
109
|
|
|
];
|
|
110
|
|
|
}
|
|
111
|
|
|
public function testClearSuspiciousAttemptsForIp() {
|
|
112
|
|
|
$this->dbServiceMock->expects($this->once())->method('deleteSuspiciousAttemptsForIp')
|
|
113
|
|
|
->with('192.168.1.1');
|
|
114
|
|
|
|
|
115
|
|
|
$this->throttle->clearSuspiciousAttemptsForIp('192.168.1.1');
|
|
116
|
|
|
}
|
|
117
|
|
|
}
|
|
118
|
|
|
|