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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
|
<?php
/**
* Copyright (c) 2012 Sam Tuke <samtuke@owncloud.com>
* This file is licensed under the Affero General Public License version 3 or
* later.
* See the COPYING-README file.
*/
require_once realpath( dirname( __FILE__ ) . '/../../../lib/base.php' );
require_once realpath( dirname( __FILE__ ) . '/../lib/crypt.php' );
require_once realpath( dirname( __FILE__ ) . '/../lib/keymanager.php' );
require_once realpath( dirname( __FILE__ ) . '/../lib/proxy.php' );
require_once realpath( dirname( __FILE__ ) . '/../lib/stream.php' );
require_once realpath( dirname( __FILE__ ) . '/../lib/util.php' );
require_once realpath( dirname( __FILE__ ) . '/../appinfo/app.php' );
use OCA\Encryption;
/**
* Class Test_Encryption_Util
*/
class Test_Encryption_Util extends \PHPUnit_Framework_TestCase
{
public $userId;
public $encryptionDir;
public $publicKeyDir;
public $pass;
/**
* @var OC_FilesystemView
*/
public $view;
public $keyfilesPath;
public $publicKeyPath;
public $privateKeyPath;
/**
* @var \OCA\Encryption\Util
*/
public $util;
public $dataShort;
public $legacyEncryptedData;
public $legacyEncryptedDataKey;
public $lagacyKey;
function setUp() {
// reset backend
\OC_User::useBackend( 'database' );
\OC_User::setUserId( 'admin' );
$this->userId = 'admin';
$this->pass = 'admin';
// set content for encrypting / decrypting in tests
$this->dataUrl = realpath( dirname( __FILE__ ) . '/../lib/crypt.php' );
$this->dataShort = 'hats';
$this->dataLong = file_get_contents( realpath( dirname( __FILE__ ) . '/../lib/crypt.php' ) );
$this->legacyData = realpath( dirname( __FILE__ ) . '/legacy-text.txt' );
$this->legacyEncryptedData = realpath( dirname( __FILE__ ) . '/legacy-encrypted-text.txt' );
$this->legacyEncryptedDataKey = realpath( dirname( __FILE__ ) . '/encryption.key' );
$this->lagacyKey = '62829813025828180801';
$keypair = Encryption\Crypt::createKeypair();
$this->genPublicKey = $keypair['publicKey'];
$this->genPrivateKey = $keypair['privateKey'];
$this->publicKeyDir = '/' . 'public-keys';
$this->encryptionDir = '/' . $this->userId . '/' . 'files_encryption';
$this->keyfilesPath = $this->encryptionDir . '/' . 'keyfiles';
$this->publicKeyPath = $this->publicKeyDir . '/' . $this->userId . '.public.key'; // e.g. data/public-keys/admin.public.key
$this->privateKeyPath = $this->encryptionDir . '/' . $this->userId . '.private.key'; // e.g. data/admin/admin.private.key
$this->view = new \OC_FilesystemView( '/' );
$userHome = \OC_User::getHome( $this->userId );
$this->dataDir = str_replace( '/' . $this->userId, '', $userHome );
// Filesystem related hooks
\OCA\Encryption\Helper::registerFilesystemHooks();
// clear and register hooks
\OC_FileProxy::clearProxies();
\OC_FileProxy::register( new OCA\Encryption\Proxy() );
// setup filesystem
\OC_Util::tearDownFS();
\OC_User::setUserId( '' );
\OC\Files\Filesystem::tearDown();
\OC_Util::setupFS( $this->userId );
\OC_User::setUserId( $this->userId );
// login admin
$params['uid'] = $this->userId;
$params['password'] = $this->pass;
OCA\Encryption\Hooks::login( $params );
$this->util = new Encryption\Util( $this->view, $this->userId );
}
function tearDown() {
// clear and register hooks
\OC_FileProxy::clearProxies();
}
/**
* @brief test that paths set during User construction are correct
*/
function testKeyPaths() {
$util = new Encryption\Util( $this->view, $this->userId );
$this->assertEquals( $this->publicKeyDir, $util->getPath( 'publicKeyDir' ) );
$this->assertEquals( $this->encryptionDir, $util->getPath( 'encryptionDir' ) );
$this->assertEquals( $this->keyfilesPath, $util->getPath( 'keyfilesPath' ) );
$this->assertEquals( $this->publicKeyPath, $util->getPath( 'publicKeyPath' ) );
$this->assertEquals( $this->privateKeyPath, $util->getPath( 'privateKeyPath' ) );
}
/**
* @brief test setup of encryption directories
*/
function testSetupServerSide() {
$this->assertEquals( true, $this->util->setupServerSide( $this->pass ) );
}
/**
* @brief test checking whether account is ready for encryption,
*/
function testUserIsReady() {
$this->assertEquals( true, $this->util->ready() );
}
/**
* @brief test checking whether account is not ready for encryption,
*/
function testUserIsNotReady() {
$this->view->unlink( $this->publicKeyDir );
$params['uid'] = $this->userId;
$params['password'] = $this->pass;
$this->assertFalse( OCA\Encryption\Hooks::login( $params ) );
$this->view->unlink( $this->privateKeyPath );
}
/**
* @brief test checking whether account is not ready for encryption,
*/
function testIsLagacyUser() {
$userView = new \OC_FilesystemView( '/' . $this->userId );
// Disable encryption proxy to prevent recursive calls
$proxyStatus = \OC_FileProxy::$enabled;
\OC_FileProxy::$enabled = false;
$encryptionKeyContent = file_get_contents( $this->legacyEncryptedDataKey );
$userView->file_put_contents( '/encryption.key', $encryptionKeyContent );
\OC_FileProxy::$enabled = $proxyStatus;
$params['uid'] = $this->userId;
$params['password'] = $this->pass;
$util = new Encryption\Util( $this->view, $this->userId );
$util->setMigrationStatus( 0 );
$this->assertTrue( OCA\Encryption\Hooks::login( $params ) );
$this->assertEquals( $this->lagacyKey, $_SESSION['legacyKey'] );
}
function testRecoveryEnabledForUser() {
$util = new Encryption\Util( $this->view, $this->userId );
// Record the value so we can return it to it's original state later
$enabled = $util->recoveryEnabledForUser();
$this->assertTrue( $util->setRecoveryForUser( 1 ) );
$this->assertEquals( 1, $util->recoveryEnabledForUser() );
$this->assertTrue( $util->setRecoveryForUser( 0 ) );
$this->assertEquals( 0, $util->recoveryEnabledForUser() );
// Return the setting to it's previous state
$this->assertTrue( $util->setRecoveryForUser( $enabled ) );
}
function testGetUidAndFilename() {
\OC_User::setUserId( 'admin' );
$filename = 'tmp-' . time() . '.test';
// Disable encryption proxy to prevent recursive calls
$proxyStatus = \OC_FileProxy::$enabled;
\OC_FileProxy::$enabled = false;
$this->view->file_put_contents( $this->userId . '/files/' . $filename, $this->dataShort );
// Re-enable proxy - our work is done
\OC_FileProxy::$enabled = $proxyStatus;
$util = new Encryption\Util( $this->view, $this->userId );
list( $fileOwnerUid, $file ) = $util->getUidAndFilename( $filename );
$this->assertEquals( 'admin', $fileOwnerUid );
$this->assertEquals( $file, $filename );
$this->view->unlink( $this->userId . '/files/' . $filename );
}
function testIsSharedPath() {
$sharedPath = '/user1/files/Shared/test';
$path = '/user1/files/test';
$this->assertTrue( $this->util->isSharedPath( $sharedPath ) );
$this->assertFalse( $this->util->isSharedPath( $path ) );
}
function testEncryptLagacyFiles() {
// login admin
$params['uid'] = $this->userId;
$params['password'] = $this->pass;
OCA\Encryption\Hooks::login( $params );
$userView = new \OC_FilesystemView( '/' . $this->userId );
$view = new \OC_FilesystemView( '/' . $this->userId . '/files' );
// Disable encryption proxy to prevent recursive calls
$proxyStatus = \OC_FileProxy::$enabled;
\OC_FileProxy::$enabled = false;
$encryptionKeyContent = file_get_contents( $this->legacyEncryptedDataKey );
$userView->file_put_contents( '/encryption.key', $encryptionKeyContent );
$legacyEncryptedData = file_get_contents( $this->legacyEncryptedData );
$view->mkdir( '/test/' );
$view->mkdir( '/test/subtest/' );
$view->file_put_contents( '/test/subtest/legacy-encrypted-text.txt', $legacyEncryptedData );
$fileInfo = $view->getFileInfo( '/test/subtest/legacy-encrypted-text.txt' );
$fileInfo['encrypted'] = true;
$view->putFileInfo( '/test/subtest/legacy-encrypted-text.txt', $fileInfo );
\OC_FileProxy::$enabled = $proxyStatus;
$params['uid'] = $this->userId;
$params['password'] = $this->pass;
$util = new Encryption\Util( $this->view, $this->userId );
$util->setMigrationStatus( 0 );
$this->assertTrue( OCA\Encryption\Hooks::login( $params ) );
$this->assertEquals( $this->lagacyKey, $_SESSION['legacyKey'] );
$files = $util->findEncFiles( '/' . $this->userId . '/files/' );
$this->assertTrue( is_array( $files ) );
$found = false;
foreach ( $files['encrypted'] as $encryptedFile ) {
if ( $encryptedFile['name'] === 'legacy-encrypted-text.txt' ) {
$found = true;
break;
}
}
$this->assertTrue( $found );
}
}
|