|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* @author Björn Schießle <[email protected]> |
|
4
|
|
|
* @author Clark Tomlinson <[email protected]> |
|
5
|
|
|
* @author Morris Jobke <[email protected]> |
|
6
|
|
|
* |
|
7
|
|
|
* @copyright Copyright (c) 2015, ownCloud, Inc. |
|
8
|
|
|
* @license AGPL-3.0 |
|
9
|
|
|
* |
|
10
|
|
|
* This code is free software: you can redistribute it and/or modify |
|
11
|
|
|
* it under the terms of the GNU Affero General Public License, version 3, |
|
12
|
|
|
* as published by the Free Software Foundation. |
|
13
|
|
|
* |
|
14
|
|
|
* This program is distributed in the hope that it will be useful, |
|
15
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
16
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
17
|
|
|
* GNU Affero General Public License for more details. |
|
18
|
|
|
* |
|
19
|
|
|
* You should have received a copy of the GNU Affero General Public License, version 3, |
|
20
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/> |
|
21
|
|
|
* |
|
22
|
|
|
*/ |
|
23
|
|
|
|
|
24
|
|
|
|
|
25
|
|
|
|
|
26
|
|
|
namespace OCA\Encryption\Tests\Hooks; |
|
27
|
|
|
|
|
28
|
|
|
|
|
29
|
|
|
use OCA\Encryption\Crypto\Crypt; |
|
30
|
|
|
use OCA\Encryption\Hooks\UserHooks; |
|
31
|
|
|
use Test\TestCase; |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* Class UserHooksTest |
|
35
|
|
|
* |
|
36
|
|
|
* @group DB |
|
37
|
|
|
* @package OCA\Encryption\Tests\Hooks |
|
38
|
|
|
*/ |
|
39
|
|
|
class UserHooksTest extends TestCase { |
|
40
|
|
|
/** |
|
41
|
|
|
* @var \PHPUnit_Framework_MockObject_MockObject |
|
42
|
|
|
*/ |
|
43
|
|
|
private $utilMock; |
|
44
|
|
|
/** |
|
45
|
|
|
* @var \PHPUnit_Framework_MockObject_MockObject |
|
46
|
|
|
*/ |
|
47
|
|
|
private $recoveryMock; |
|
48
|
|
|
/** |
|
49
|
|
|
* @var \PHPUnit_Framework_MockObject_MockObject |
|
50
|
|
|
*/ |
|
51
|
|
|
private $sessionMock; |
|
52
|
|
|
/** |
|
53
|
|
|
* @var \PHPUnit_Framework_MockObject_MockObject |
|
54
|
|
|
*/ |
|
55
|
|
|
private $keyManagerMock; |
|
56
|
|
|
/** |
|
57
|
|
|
* @var \PHPUnit_Framework_MockObject_MockObject |
|
58
|
|
|
*/ |
|
59
|
|
|
private $userManagerMock; |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* @var \PHPUnit_Framework_MockObject_MockObject |
|
63
|
|
|
*/ |
|
64
|
|
|
private $userSetupMock; |
|
65
|
|
|
/** |
|
66
|
|
|
* @var \PHPUnit_Framework_MockObject_MockObject |
|
67
|
|
|
*/ |
|
68
|
|
|
private $userSessionMock; |
|
69
|
|
|
/** |
|
70
|
|
|
* @var \PHPUnit_Framework_MockObject_MockObject |
|
71
|
|
|
*/ |
|
72
|
|
|
private $cryptMock; |
|
73
|
|
|
/** |
|
74
|
|
|
* @var UserHooks |
|
75
|
|
|
*/ |
|
76
|
|
|
private $instance; |
|
77
|
|
|
|
|
78
|
|
|
private $params = ['uid' => 'testUser', 'password' => 'password']; |
|
79
|
|
|
|
|
80
|
1 |
|
public function testLogin() { |
|
81
|
1 |
|
$this->userSetupMock->expects($this->exactly(2)) |
|
82
|
1 |
|
->method('setupUser') |
|
83
|
1 |
|
->willReturnOnConsecutiveCalls(true, false); |
|
84
|
|
|
|
|
85
|
1 |
|
$this->keyManagerMock->expects($this->once()) |
|
86
|
1 |
|
->method('init') |
|
87
|
1 |
|
->with('testUser', 'password'); |
|
88
|
|
|
|
|
89
|
1 |
|
$this->assertNull($this->instance->login($this->params)); |
|
90
|
1 |
|
$this->assertFalse($this->instance->login($this->params)); |
|
91
|
1 |
|
} |
|
92
|
|
|
|
|
93
|
1 |
|
public function testLogout() { |
|
94
|
1 |
|
$this->sessionMock->expects($this->once()) |
|
95
|
1 |
|
->method('clear'); |
|
96
|
|
|
|
|
97
|
1 |
|
$this->assertNull($this->instance->logout()); |
|
98
|
1 |
|
} |
|
99
|
|
|
|
|
100
|
1 |
|
public function testPostCreateUser() { |
|
101
|
1 |
|
$this->userSetupMock->expects($this->once()) |
|
102
|
1 |
|
->method('setupUser'); |
|
103
|
|
|
|
|
104
|
1 |
|
$this->assertNull($this->instance->postCreateUser($this->params)); |
|
105
|
1 |
|
} |
|
106
|
|
|
|
|
107
|
1 |
|
public function testPostDeleteUser() { |
|
108
|
1 |
|
$this->keyManagerMock->expects($this->once()) |
|
109
|
1 |
|
->method('deletePublicKey') |
|
110
|
1 |
|
->with('testUser'); |
|
111
|
|
|
|
|
112
|
1 |
|
$this->assertNull($this->instance->postDeleteUser($this->params)); |
|
113
|
1 |
|
} |
|
114
|
|
|
|
|
115
|
|
|
/** |
|
116
|
|
|
* @dataProvider dataTestPreSetPassphrase |
|
117
|
|
|
*/ |
|
118
|
2 |
|
public function testPreSetPassphrase($canChange) { |
|
119
|
|
|
|
|
120
|
|
|
/** @var UserHooks | \PHPUnit_Framework_MockObject_MockObject $instance */ |
|
121
|
2 |
|
$instance = $this->getMockBuilder('OCA\Encryption\Hooks\UserHooks') |
|
122
|
2 |
|
->setConstructorArgs( |
|
123
|
|
|
[ |
|
124
|
2 |
|
$this->keyManagerMock, |
|
125
|
2 |
|
$this->userManagerMock, |
|
126
|
2 |
|
$this->loggerMock, |
|
127
|
2 |
|
$this->userSetupMock, |
|
128
|
2 |
|
$this->userSessionMock, |
|
129
|
2 |
|
$this->utilMock, |
|
130
|
2 |
|
$this->sessionMock, |
|
131
|
2 |
|
$this->cryptMock, |
|
132
|
2 |
|
$this->recoveryMock |
|
133
|
2 |
|
] |
|
134
|
2 |
|
) |
|
135
|
2 |
|
->setMethods(['setPassphrase']) |
|
136
|
2 |
|
->getMock(); |
|
137
|
|
|
|
|
138
|
2 |
|
$userMock = $this->getMock('OCP\IUser'); |
|
139
|
|
|
|
|
140
|
2 |
|
$this->userManagerMock->expects($this->once()) |
|
141
|
2 |
|
->method('get') |
|
142
|
2 |
|
->with($this->params['uid']) |
|
143
|
2 |
|
->willReturn($userMock); |
|
144
|
2 |
|
$userMock->expects($this->once()) |
|
145
|
2 |
|
->method('canChangePassword') |
|
146
|
2 |
|
->willReturn($canChange); |
|
147
|
|
|
|
|
148
|
2 |
|
if ($canChange) { |
|
149
|
|
|
// in this case the password will be changed in the post hook |
|
150
|
1 |
|
$instance->expects($this->never())->method('setPassphrase'); |
|
151
|
1 |
|
} else { |
|
152
|
|
|
// if user can't change the password we update the encryption |
|
153
|
|
|
// key password already in the pre hook |
|
154
|
1 |
|
$instance->expects($this->once()) |
|
155
|
1 |
|
->method('setPassphrase') |
|
156
|
1 |
|
->with($this->params); |
|
157
|
|
|
} |
|
158
|
|
|
|
|
159
|
2 |
|
$instance->preSetPassphrase($this->params); |
|
160
|
2 |
|
} |
|
161
|
|
|
|
|
162
|
|
|
public function dataTestPreSetPassphrase() { |
|
163
|
|
|
return [ |
|
164
|
|
|
[true], |
|
165
|
|
|
[false] |
|
166
|
|
|
]; |
|
167
|
|
|
} |
|
168
|
|
|
|
|
169
|
1 |
|
public function testSetPassphrase() { |
|
170
|
1 |
|
$this->sessionMock->expects($this->exactly(4)) |
|
171
|
1 |
|
->method('getPrivateKey') |
|
172
|
1 |
|
->willReturnOnConsecutiveCalls(true, false); |
|
173
|
|
|
|
|
174
|
1 |
|
$this->cryptMock->expects($this->exactly(4)) |
|
175
|
1 |
|
->method('symmetricEncryptFileContent') |
|
176
|
1 |
|
->willReturn(true); |
|
177
|
|
|
|
|
178
|
1 |
|
$this->cryptMock->expects($this->any()) |
|
179
|
1 |
|
->method('generateHeader') |
|
180
|
1 |
|
->willReturn(Crypt::HEADER_START . ':Cipher:test:' . Crypt::HEADER_END); |
|
181
|
|
|
|
|
182
|
1 |
|
$this->keyManagerMock->expects($this->exactly(4)) |
|
183
|
1 |
|
->method('setPrivateKey') |
|
184
|
1 |
|
->willReturnCallback(function ($user, $key) { |
|
185
|
1 |
|
$header = substr($key, 0, strlen(Crypt::HEADER_START)); |
|
186
|
1 |
|
$this->assertSame( |
|
187
|
1 |
|
Crypt::HEADER_START, |
|
188
|
1 |
|
$header, 'every encrypted file should start with a header'); |
|
189
|
1 |
|
}); |
|
190
|
|
|
|
|
191
|
1 |
|
$this->assertNull($this->instance->setPassphrase($this->params)); |
|
192
|
1 |
|
$this->params['recoveryPassword'] = 'password'; |
|
193
|
|
|
|
|
194
|
1 |
|
$this->recoveryMock->expects($this->exactly(3)) |
|
195
|
1 |
|
->method('isRecoveryEnabledForUser') |
|
196
|
1 |
|
->with('testUser') |
|
197
|
1 |
|
->willReturnOnConsecutiveCalls(true, false); |
|
198
|
|
|
|
|
199
|
|
|
|
|
200
|
1 |
|
$this->instance = $this->getMockBuilder('OCA\Encryption\Hooks\UserHooks') |
|
201
|
1 |
|
->setConstructorArgs( |
|
202
|
|
|
[ |
|
203
|
1 |
|
$this->keyManagerMock, |
|
204
|
1 |
|
$this->userManagerMock, |
|
205
|
1 |
|
$this->loggerMock, |
|
206
|
1 |
|
$this->userSetupMock, |
|
207
|
1 |
|
$this->userSessionMock, |
|
208
|
1 |
|
$this->utilMock, |
|
209
|
1 |
|
$this->sessionMock, |
|
210
|
1 |
|
$this->cryptMock, |
|
211
|
1 |
|
$this->recoveryMock |
|
212
|
1 |
|
] |
|
213
|
1 |
|
)->setMethods(['initMountPoints'])->getMock(); |
|
214
|
|
|
|
|
215
|
1 |
|
$this->instance->expects($this->exactly(3))->method('initMountPoints'); |
|
216
|
|
|
|
|
217
|
|
|
// Test first if statement |
|
218
|
1 |
|
$this->assertNull($this->instance->setPassphrase($this->params)); |
|
219
|
|
|
|
|
220
|
|
|
// Test Second if conditional |
|
221
|
1 |
|
$this->keyManagerMock->expects($this->exactly(2)) |
|
222
|
1 |
|
->method('userHasKeys') |
|
223
|
1 |
|
->with('testUser') |
|
224
|
1 |
|
->willReturn(true); |
|
225
|
|
|
|
|
226
|
1 |
|
$this->assertNull($this->instance->setPassphrase($this->params)); |
|
227
|
|
|
|
|
228
|
|
|
// Test third and final if condition |
|
229
|
1 |
|
$this->utilMock->expects($this->once()) |
|
230
|
1 |
|
->method('userHasFiles') |
|
231
|
1 |
|
->with('testUser') |
|
232
|
1 |
|
->willReturn(false); |
|
233
|
|
|
|
|
234
|
1 |
|
$this->cryptMock->expects($this->once()) |
|
235
|
1 |
|
->method('createKeyPair'); |
|
236
|
|
|
|
|
237
|
1 |
|
$this->keyManagerMock->expects($this->once()) |
|
238
|
1 |
|
->method('setPrivateKey'); |
|
239
|
|
|
|
|
240
|
1 |
|
$this->recoveryMock->expects($this->once()) |
|
241
|
1 |
|
->method('recoverUsersFiles') |
|
242
|
1 |
|
->with('password', 'testUser'); |
|
243
|
|
|
|
|
244
|
1 |
|
$this->assertNull($this->instance->setPassphrase($this->params)); |
|
245
|
1 |
|
} |
|
246
|
|
|
|
|
247
|
1 |
|
public function testSetPasswordNoUser() { |
|
248
|
1 |
|
$this->sessionMock->expects($this->once()) |
|
249
|
1 |
|
->method('getPrivateKey') |
|
250
|
1 |
|
->willReturn(true); |
|
251
|
|
|
|
|
252
|
1 |
|
$userSessionMock = $this->getMockBuilder('OCP\IUserSession') |
|
253
|
1 |
|
->disableOriginalConstructor() |
|
254
|
1 |
|
->getMock(); |
|
255
|
|
|
|
|
256
|
1 |
|
$userSessionMock->expects($this->any())->method('getUser')->will($this->returnValue(null)); |
|
257
|
|
|
|
|
258
|
1 |
|
$this->recoveryMock->expects($this->once()) |
|
259
|
1 |
|
->method('isRecoveryEnabledForUser') |
|
260
|
1 |
|
->with('testUser') |
|
261
|
1 |
|
->willReturn(false); |
|
262
|
|
|
|
|
263
|
1 |
|
$userHooks = $this->getMockBuilder('OCA\Encryption\Hooks\UserHooks') |
|
264
|
1 |
|
->setConstructorArgs( |
|
265
|
|
|
[ |
|
266
|
1 |
|
$this->keyManagerMock, |
|
267
|
1 |
|
$this->userManagerMock, |
|
268
|
1 |
|
$this->loggerMock, |
|
269
|
1 |
|
$this->userSetupMock, |
|
270
|
1 |
|
$userSessionMock, |
|
271
|
1 |
|
$this->utilMock, |
|
272
|
1 |
|
$this->sessionMock, |
|
273
|
1 |
|
$this->cryptMock, |
|
274
|
1 |
|
$this->recoveryMock |
|
275
|
1 |
|
] |
|
276
|
1 |
|
)->setMethods(['initMountPoints'])->getMock(); |
|
277
|
|
|
|
|
278
|
1 |
|
$this->assertNull($userHooks->setPassphrase($this->params)); |
|
279
|
1 |
|
} |
|
280
|
|
|
|
|
281
|
1 |
|
public function testPostPasswordReset() { |
|
282
|
1 |
|
$this->keyManagerMock->expects($this->once()) |
|
283
|
1 |
|
->method('replaceUserKeys') |
|
284
|
1 |
|
->with('testUser'); |
|
285
|
|
|
|
|
286
|
1 |
|
$this->userSetupMock->expects($this->once()) |
|
287
|
1 |
|
->method('setupServerSide') |
|
288
|
1 |
|
->with('testUser', 'password'); |
|
289
|
|
|
|
|
290
|
1 |
|
$this->assertNull($this->instance->postPasswordReset($this->params)); |
|
291
|
1 |
|
} |
|
292
|
|
|
|
|
293
|
9 |
|
protected function setUp() { |
|
294
|
9 |
|
parent::setUp(); |
|
295
|
9 |
|
$this->loggerMock = $this->getMock('OCP\ILogger'); |
|
296
|
9 |
|
$this->keyManagerMock = $this->getMockBuilder('OCA\Encryption\KeyManager') |
|
297
|
9 |
|
->disableOriginalConstructor() |
|
298
|
9 |
|
->getMock(); |
|
299
|
9 |
|
$this->userManagerMock = $this->getMockBuilder('OCP\IUserManager') |
|
300
|
9 |
|
->disableOriginalConstructor() |
|
301
|
9 |
|
->getMock(); |
|
302
|
9 |
|
$this->userSetupMock = $this->getMockBuilder('OCA\Encryption\Users\Setup') |
|
303
|
9 |
|
->disableOriginalConstructor() |
|
304
|
9 |
|
->getMock(); |
|
305
|
|
|
|
|
306
|
9 |
|
$this->userSessionMock = $this->getMockBuilder('OCP\IUserSession') |
|
307
|
9 |
|
->disableOriginalConstructor() |
|
308
|
9 |
|
->setMethods([ |
|
309
|
9 |
|
'isLoggedIn', |
|
310
|
9 |
|
'getUID', |
|
311
|
9 |
|
'login', |
|
312
|
9 |
|
'logout', |
|
313
|
9 |
|
'setUser', |
|
314
|
9 |
|
'getUser', |
|
315
|
|
|
'canChangePassword' |
|
316
|
9 |
|
]) |
|
317
|
9 |
|
->getMock(); |
|
318
|
|
|
|
|
319
|
9 |
|
$this->userSessionMock->expects($this->any())->method('getUID')->will($this->returnValue('testUser')); |
|
320
|
|
|
|
|
321
|
9 |
|
$this->userSessionMock->expects($this->any()) |
|
322
|
9 |
|
->method($this->anything()) |
|
323
|
9 |
|
->will($this->returnSelf()); |
|
324
|
|
|
|
|
325
|
9 |
|
$utilMock = $this->getMockBuilder('OCA\Encryption\Util') |
|
326
|
9 |
|
->disableOriginalConstructor() |
|
327
|
9 |
|
->getMock(); |
|
328
|
|
|
|
|
329
|
9 |
|
$sessionMock = $this->getMockBuilder('OCA\Encryption\Session') |
|
330
|
9 |
|
->disableOriginalConstructor() |
|
331
|
9 |
|
->getMock(); |
|
332
|
|
|
|
|
333
|
9 |
|
$this->cryptMock = $this->getMockBuilder('OCA\Encryption\Crypto\Crypt') |
|
334
|
9 |
|
->disableOriginalConstructor() |
|
335
|
9 |
|
->getMock(); |
|
336
|
9 |
|
$recoveryMock = $this->getMockBuilder('OCA\Encryption\Recovery') |
|
337
|
9 |
|
->disableOriginalConstructor() |
|
338
|
9 |
|
->getMock(); |
|
339
|
|
|
|
|
340
|
9 |
|
$this->sessionMock = $sessionMock; |
|
341
|
9 |
|
$this->recoveryMock = $recoveryMock; |
|
342
|
9 |
|
$this->utilMock = $utilMock; |
|
343
|
9 |
|
$this->instance = new UserHooks($this->keyManagerMock, |
|
344
|
9 |
|
$this->userManagerMock, |
|
345
|
9 |
|
$this->loggerMock, |
|
346
|
9 |
|
$this->userSetupMock, |
|
347
|
9 |
|
$this->userSessionMock, |
|
348
|
9 |
|
$this->utilMock, |
|
349
|
9 |
|
$this->sessionMock, |
|
350
|
9 |
|
$this->cryptMock, |
|
351
|
9 |
|
$this->recoveryMock |
|
352
|
9 |
|
); |
|
353
|
|
|
|
|
354
|
9 |
|
} |
|
355
|
|
|
|
|
356
|
|
|
} |
|
357
|
|
|
|