|
1
|
|
|
<?php
|
|
2
|
|
|
/**
|
|
3
|
|
|
* @copyright Copyright (c) 2017 Semih Serhat Karakaya <[email protected]>
|
|
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 OCA\Security\Tests;
|
|
22
|
|
|
|
|
23
|
|
|
use OCA\Security\SecurityConfig;
|
|
24
|
|
|
use OCP\IConfig;
|
|
25
|
|
|
use Test\TestCase;
|
|
26
|
|
|
class SecurityConfigTest extends TestCase {
|
|
27
|
|
|
/** @var IConfig|\PHPUnit_Framework_MockObject_MockObject */
|
|
28
|
|
|
private $config;
|
|
29
|
|
|
/** @var SecurityConfig */
|
|
30
|
|
|
private $securityConfig;
|
|
31
|
|
|
public function setUp() {
|
|
32
|
|
|
parent::setUp();
|
|
33
|
|
|
$this->config = $this->createMock(IConfig::class);
|
|
34
|
|
|
$this->securityConfig = new SecurityConfig($this->config);
|
|
35
|
|
|
}
|
|
36
|
|
|
|
|
37
|
|
|
/**
|
|
38
|
|
|
* @param array $mockedMethods
|
|
39
|
|
|
* @return SecurityConfig | \PHPUnit_Framework_MockObject_MockObject
|
|
40
|
|
|
*/
|
|
41
|
|
|
private function getMockInstance($mockedMethods = []) {
|
|
42
|
|
|
$passwordValidator = $this->getMockBuilder('OCA\Security\SecurityConfig')
|
|
43
|
|
|
->setConstructorArgs([$this->config])
|
|
44
|
|
|
->setMethods($mockedMethods)->getMock();
|
|
45
|
|
|
return $passwordValidator;
|
|
46
|
|
|
}
|
|
47
|
|
|
|
|
48
|
|
|
public function testGetAllSecurityConfigs() {
|
|
49
|
|
|
$instance = $this->getMockInstance(
|
|
50
|
|
|
[
|
|
51
|
|
|
'getBruteForceProtectionFailTolerance',
|
|
52
|
|
|
'getBruteForceProtectionTimeThreshold',
|
|
53
|
|
|
'getBruteForceProtectionBanPeriod',
|
|
54
|
|
|
'getMinPasswordLength',
|
|
55
|
|
|
'getIsUpperLowerCaseEnforced',
|
|
56
|
|
|
'getIsNumericCharactersEnforced',
|
|
57
|
|
|
'getIsSpecialCharactersEnforced'
|
|
58
|
|
|
]
|
|
59
|
|
|
);
|
|
60
|
|
|
$instance->expects($this->once())->method('getBruteForceProtectionFailTolerance');
|
|
|
|
|
|
|
61
|
|
|
$instance->expects($this->once())->method('getBruteForceProtectionTimeThreshold');
|
|
|
|
|
|
|
62
|
|
|
$instance->expects($this->once())->method('getBruteForceProtectionBanPeriod');
|
|
|
|
|
|
|
63
|
|
|
$instance->expects($this->once())->method('getMinPasswordLength');
|
|
|
|
|
|
|
64
|
|
|
$instance->expects($this->once())->method('getIsUpperLowerCaseEnforced');
|
|
|
|
|
|
|
65
|
|
|
$instance->expects($this->once())->method('getIsNumericCharactersEnforced');
|
|
|
|
|
|
|
66
|
|
|
$instance->expects($this->once())->method('getIsNumericCharactersEnforced');
|
|
|
|
|
|
|
67
|
|
|
$instance->getAllSecurityConfigs();
|
|
68
|
|
|
}
|
|
69
|
|
|
/**
|
|
70
|
|
|
* @dataProvider numericConfTestData
|
|
71
|
|
|
* @param string $appConfigValue
|
|
72
|
|
|
* @param int $expected
|
|
73
|
|
|
*/
|
|
74
|
|
View Code Duplication |
public function testGetBruteForceProtectionFailTolerance($appConfigValue, $expected) {
|
|
|
|
|
|
|
75
|
|
|
$this->config->expects($this->once())->method('getAppValue')
|
|
76
|
|
|
->with('security', 'brute_force_protection_fail_tolerance', '3')
|
|
77
|
|
|
->willReturn($appConfigValue);
|
|
78
|
|
|
$this->assertSame($expected,
|
|
79
|
|
|
$this->securityConfig->getBruteForceProtectionFailTolerance()
|
|
80
|
|
|
);
|
|
81
|
|
|
}
|
|
82
|
|
|
/**
|
|
83
|
|
|
* @dataProvider numericConfTestData
|
|
84
|
|
|
* @param string $appConfigValue
|
|
85
|
|
|
* @param int $expected
|
|
86
|
|
|
*/
|
|
87
|
|
View Code Duplication |
public function testGetBruteForceProtectionTimeThreshold($appConfigValue, $expected) {
|
|
|
|
|
|
|
88
|
|
|
$this->config->expects($this->once())->method('getAppValue')
|
|
89
|
|
|
->with('security', 'brute_force_protection_time_threshold', '600')
|
|
90
|
|
|
->willReturn($appConfigValue);
|
|
91
|
|
|
$this->assertSame($expected,
|
|
92
|
|
|
$this->securityConfig->getBruteForceProtectionTimeThreshold()
|
|
93
|
|
|
);
|
|
94
|
|
|
}
|
|
95
|
|
|
/**
|
|
96
|
|
|
* @dataProvider numericConfTestData
|
|
97
|
|
|
* @param string $appConfigValue
|
|
98
|
|
|
* @param int $expected
|
|
99
|
|
|
*/
|
|
100
|
|
View Code Duplication |
public function testGetBruteForceProtectionBanPeriod($appConfigValue, $expected) {
|
|
|
|
|
|
|
101
|
|
|
$this->config->expects($this->once())->method('getAppValue')
|
|
102
|
|
|
->with('security', 'brute_force_protection_ban_period', '300')
|
|
103
|
|
|
->willReturn($appConfigValue);
|
|
104
|
|
|
$this->assertSame($expected,
|
|
105
|
|
|
$this->securityConfig->getBruteForceProtectionBanPeriod()
|
|
106
|
|
|
);
|
|
107
|
|
|
}
|
|
108
|
|
|
/**
|
|
109
|
|
|
* @dataProvider minPassTestData
|
|
110
|
|
|
* @param string $appConfigValue
|
|
111
|
|
|
* @param int $expected
|
|
112
|
|
|
*/
|
|
113
|
|
View Code Duplication |
public function testGetMinPasswordLength($appConfigValue, $expected) {
|
|
|
|
|
|
|
114
|
|
|
$this->config->expects($this->once())->method('getAppValue')
|
|
115
|
|
|
->with('security', 'min_password_length', '8')
|
|
116
|
|
|
->willReturn($appConfigValue);
|
|
117
|
|
|
$this->assertSame($expected,
|
|
118
|
|
|
$this->securityConfig->getMinPasswordLength()
|
|
119
|
|
|
);
|
|
120
|
|
|
}
|
|
121
|
|
|
/**
|
|
122
|
|
|
* @dataProvider configTestData
|
|
123
|
|
|
* @param string $appConfigValue
|
|
124
|
|
|
* @param bool $expected
|
|
125
|
|
|
*/
|
|
126
|
|
View Code Duplication |
public function testGetIsUpperLowerCaseEnforced($appConfigValue, $expected) {
|
|
|
|
|
|
|
127
|
|
|
$this->config->expects($this->once())->method('getAppValue')
|
|
128
|
|
|
->with('security', 'enforce_upper_lower_case', '0')
|
|
129
|
|
|
->willReturn($appConfigValue);
|
|
130
|
|
|
$this->assertSame($expected,
|
|
131
|
|
|
$this->securityConfig->getIsUpperLowerCaseEnforced()
|
|
132
|
|
|
);
|
|
133
|
|
|
}
|
|
134
|
|
|
/**
|
|
135
|
|
|
* @dataProvider configTestData
|
|
136
|
|
|
* @param string $appConfigValue
|
|
137
|
|
|
* @param bool $expected
|
|
138
|
|
|
*/
|
|
139
|
|
View Code Duplication |
public function testGetIsNumericCharactersEnforced($appConfigValue, $expected) {
|
|
|
|
|
|
|
140
|
|
|
$this->config->expects($this->once())->method('getAppValue')
|
|
141
|
|
|
->with('security', 'enforce_numeric_characters', '0')
|
|
142
|
|
|
->willReturn($appConfigValue);
|
|
143
|
|
|
$this->assertSame($expected,
|
|
144
|
|
|
$this->securityConfig->getIsNumericCharactersEnforced()
|
|
145
|
|
|
);
|
|
146
|
|
|
}
|
|
147
|
|
|
/**
|
|
148
|
|
|
* @dataProvider configTestData
|
|
149
|
|
|
* @param string $appConfigValue
|
|
150
|
|
|
* @param bool $expected
|
|
151
|
|
|
*/
|
|
152
|
|
View Code Duplication |
public function testGetIsSpecialCharactersEnforced($appConfigValue, $expected) {
|
|
|
|
|
|
|
153
|
|
|
$this->config->expects($this->once())->method('getAppValue')
|
|
154
|
|
|
->with('security', 'enforce_special_characters', '0')
|
|
155
|
|
|
->willReturn($appConfigValue);
|
|
156
|
|
|
$this->assertSame($expected,
|
|
157
|
|
|
$this->securityConfig->getIsSpecialCharactersEnforced()
|
|
158
|
|
|
);
|
|
159
|
|
|
}
|
|
160
|
|
|
/**
|
|
161
|
|
|
* @dataProvider numericConfTestData
|
|
162
|
|
|
* @param string $expected
|
|
163
|
|
|
* @param int $setValue
|
|
164
|
|
|
*/
|
|
165
|
|
|
public function testSetBruteForceProtectionFailTolerance($expected, $setValue) {
|
|
166
|
|
|
$this->config->expects($this->once())->method('setAppValue')
|
|
167
|
|
|
->with('security', 'brute_force_protection_fail_tolerance', $expected);
|
|
168
|
|
|
$this->securityConfig->setBruteForceProtectionFailTolerance($setValue);
|
|
169
|
|
|
}
|
|
170
|
|
|
/**
|
|
171
|
|
|
* @dataProvider numericConfTestData
|
|
172
|
|
|
* @param string $expected
|
|
173
|
|
|
* @param int $setValue
|
|
174
|
|
|
*/
|
|
175
|
|
|
public function testSetBruteForceProtectionTimeThreshold($expected, $setValue) {
|
|
176
|
|
|
$this->config->expects($this->once())->method('setAppValue')
|
|
177
|
|
|
->with('security', 'brute_force_protection_time_threshold', $expected);
|
|
178
|
|
|
$this->securityConfig->setBruteForceProtectionTimeThreshold($setValue);
|
|
179
|
|
|
}
|
|
180
|
|
|
/**
|
|
181
|
|
|
* @dataProvider numericConfTestData
|
|
182
|
|
|
* @param string $expected
|
|
183
|
|
|
* @param int $setValue
|
|
184
|
|
|
*/
|
|
185
|
|
|
public function testSetBruteForceProtectionBanPeriod($expected, $setValue) {
|
|
186
|
|
|
$this->config->expects($this->once())->method('setAppValue')
|
|
187
|
|
|
->with('security', 'brute_force_protection_ban_period', $expected);
|
|
188
|
|
|
$this->securityConfig->setBruteForceProtectionBanPeriod($setValue);
|
|
189
|
|
|
}
|
|
190
|
|
|
/**
|
|
191
|
|
|
* @dataProvider minPassTestData
|
|
192
|
|
|
* @param string $expected
|
|
193
|
|
|
* @param int $setValue
|
|
194
|
|
|
*/
|
|
195
|
|
|
public function testSetMinPasswordLength($expected, $setValue) {
|
|
196
|
|
|
$this->config->expects($this->once())->method('setAppValue')
|
|
197
|
|
|
->with('security', 'min_password_length', $expected);
|
|
198
|
|
|
$this->securityConfig->setMinPasswordLength($setValue);
|
|
199
|
|
|
}
|
|
200
|
|
|
/**
|
|
201
|
|
|
* @dataProvider configTestData
|
|
202
|
|
|
* @param string $expected
|
|
203
|
|
|
* @param bool $setValue
|
|
204
|
|
|
*/
|
|
205
|
|
|
public function testSetIsUpperLowerCaseEnforced($expected, $setValue) {
|
|
206
|
|
|
$this->config->expects($this->once())->method('setAppValue')
|
|
207
|
|
|
->with('security', 'enforce_upper_lower_case', $expected);
|
|
208
|
|
|
$this->securityConfig->setIsUpperLowerCaseEnforced($setValue);
|
|
209
|
|
|
}
|
|
210
|
|
|
/**
|
|
211
|
|
|
* @dataProvider configTestData
|
|
212
|
|
|
* @param string $expected
|
|
213
|
|
|
* @param bool $setValue
|
|
214
|
|
|
*/
|
|
215
|
|
|
public function testSetIsNumericCharactersEnforced($expected, $setValue) {
|
|
216
|
|
|
$this->config->expects($this->once())->method('setAppValue')
|
|
217
|
|
|
->with('security', 'enforce_numeric_characters', $expected);
|
|
218
|
|
|
$this->securityConfig->setIsNumericCharactersEnforced($setValue);
|
|
219
|
|
|
}
|
|
220
|
|
|
/**
|
|
221
|
|
|
* @dataProvider configTestData
|
|
222
|
|
|
* @param string $expected
|
|
223
|
|
|
* @param bool $setValue
|
|
224
|
|
|
*/
|
|
225
|
|
|
public function testSetIsSpecialCharactersEnforced($expected, $setValue) {
|
|
226
|
|
|
$this->config->expects($this->once())->method('setAppValue')
|
|
227
|
|
|
->with('security', 'enforce_special_characters', $expected);
|
|
228
|
|
|
$this->securityConfig->setIsSpecialCharactersEnforced($setValue);
|
|
229
|
|
|
}
|
|
230
|
|
|
public function configTestData() {
|
|
231
|
|
|
return [
|
|
232
|
|
|
['1', true],
|
|
233
|
|
|
['0', false],
|
|
234
|
|
|
];
|
|
235
|
|
|
}
|
|
236
|
|
|
public function minPassTestData() {
|
|
237
|
|
|
return [
|
|
238
|
|
|
['8', 8],
|
|
239
|
|
|
['16', 16],
|
|
240
|
|
|
['255', 255]
|
|
241
|
|
|
];
|
|
242
|
|
|
}
|
|
243
|
|
|
public function numericConfTestData() {
|
|
244
|
|
|
return [
|
|
245
|
|
|
['42', 42],
|
|
246
|
|
|
['300', 300],
|
|
247
|
|
|
['1000', 1000]
|
|
248
|
|
|
];
|
|
249
|
|
|
}
|
|
250
|
|
|
|
|
251
|
|
|
} |
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.
This is most likely a typographical error or the method has been renamed.