Blame view

sources/apps/files_encryption/tests/trashbin.php 10.4 KB
03e52840d   Kload   Init
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
  <?php
  /**
   * ownCloud
   *
   * @author Florin Peter
   * @copyright 2013 Florin Peter <owncloud@florin-peter.de>
   *
   * This library is free software; you can redistribute it and/or
   * modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE
   * License as published by the Free Software Foundation; either
   * version 3 of the License, or any later version.
   *
   * This library is distributed in the hope that it will be useful,
   * but WITHOUT ANY WARRANTY; without even the implied warranty of
   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   * GNU AFFERO GENERAL PUBLIC LICENSE for more details.
   *
   * You should have received a copy of the GNU Affero General Public
   * License along with this library.  If not, see <http://www.gnu.org/licenses/>.
   *
   */
31b7f2792   Kload   Upgrade to ownclo...
22
23
24
25
26
27
28
29
30
  require_once __DIR__ . '/../../../lib/base.php';
  require_once __DIR__ . '/../lib/crypt.php';
  require_once __DIR__ . '/../lib/keymanager.php';
  require_once __DIR__ . '/../lib/proxy.php';
  require_once __DIR__ . '/../lib/stream.php';
  require_once __DIR__ . '/../lib/util.php';
  require_once __DIR__ . '/../appinfo/app.php';
  require_once __DIR__ . '/../../files_trashbin/appinfo/app.php';
  require_once __DIR__ . '/util.php';
03e52840d   Kload   Init
31
32
33
34
35
  
  use OCA\Encryption;
  
  /**
   * Class Test_Encryption_Trashbin
6d9380f96   Cédric Dupont   Update sources OC...
36
   * this class provide basic trashbin app tests
03e52840d   Kload   Init
37
38
39
40
41
42
43
44
   */
  class Test_Encryption_Trashbin extends \PHPUnit_Framework_TestCase {
  
  	const TEST_ENCRYPTION_TRASHBIN_USER1 = "test-trashbin-user1";
  
  	public $userId;
  	public $pass;
  	/**
6d9380f96   Cédric Dupont   Update sources OC...
45
  	 * @var \OC\Files\View
03e52840d   Kload   Init
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
  	 */
  	public $view;
  	public $dataShort;
  	public $stateFilesTrashbin;
  	public $folder1;
  	public $subfolder;
  	public $subsubfolder;
  
  	public static function setUpBeforeClass() {
  		// reset backend
  		\OC_User::clearBackends();
  		\OC_User::useBackend('database');
  
  		\OC_Hook::clear('OC_Filesystem');
  		\OC_Hook::clear('OC_User');
  
  		// trashbin hooks
  		\OCA\Files_Trashbin\Trashbin::registerHooks();
  
  		// Filesystem related hooks
  		\OCA\Encryption\Helper::registerFilesystemHooks();
  
  		// clear and register hooks
  		\OC_FileProxy::clearProxies();
  		\OC_FileProxy::register(new OCA\Encryption\Proxy());
  
  		// create test user
  		\Test_Encryption_Util::loginHelper(\Test_Encryption_Trashbin::TEST_ENCRYPTION_TRASHBIN_USER1, true);
  	}
  
  	function setUp() {
  		// set user id
  		\OC_User::setUserId(\Test_Encryption_Trashbin::TEST_ENCRYPTION_TRASHBIN_USER1);
  		$this->userId = \Test_Encryption_Trashbin::TEST_ENCRYPTION_TRASHBIN_USER1;
  		$this->pass = \Test_Encryption_Trashbin::TEST_ENCRYPTION_TRASHBIN_USER1;
  
  		// init filesystem view
6d9380f96   Cédric Dupont   Update sources OC...
83
  		$this->view = new \OC\Files\View('/');
03e52840d   Kload   Init
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
  
  		// init short data
  		$this->dataShort = 'hats';
  
  		$this->folder1 = '/folder1';
  		$this->subfolder = '/subfolder1';
  		$this->subsubfolder = '/subsubfolder1';
  
  		// remember files_trashbin state
  		$this->stateFilesTrashbin = OC_App::isEnabled('files_trashbin');
  
  		// we want to tests with app files_trashbin enabled
  		\OC_App::enable('files_trashbin');
  	}
  
  	function tearDown() {
  		// reset app files_trashbin
  		if ($this->stateFilesTrashbin) {
  			OC_App::enable('files_trashbin');
  		}
  		else {
  			OC_App::disable('files_trashbin');
  		}
  	}
  
  	public static function tearDownAfterClass() {
  		// cleanup test user
  		\OC_User::deleteUser(\Test_Encryption_Trashbin::TEST_ENCRYPTION_TRASHBIN_USER1);
  	}
  
  	/**
31b7f2792   Kload   Upgrade to ownclo...
115
  	 * @medium
6d9380f96   Cédric Dupont   Update sources OC...
116
  	 * test delete file
03e52840d   Kload   Init
117
118
119
120
  	 */
  	function testDeleteFile() {
  
  		// generate filename
a293d369c   Kload   Update sources to...
121
  		$filename = 'tmp-' . uniqid() . '.txt';
03e52840d   Kload   Init
122
123
  
  		// save file with content
31b7f2792   Kload   Upgrade to ownclo...
124
  		$cryptedFile = file_put_contents('crypt:///' .\Test_Encryption_Trashbin::TEST_ENCRYPTION_TRASHBIN_USER1. '/files/'. $filename, $this->dataShort);
03e52840d   Kload   Init
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
  
  		// test that data was successfully written
  		$this->assertTrue(is_int($cryptedFile));
  
  		// check if key for admin exists
  		$this->assertTrue($this->view->file_exists(
  			'/' . \Test_Encryption_Trashbin::TEST_ENCRYPTION_TRASHBIN_USER1 . '/files_encryption/keyfiles/' . $filename
  			. '.key'));
  
  		// check if share key for admin exists
  		$this->assertTrue($this->view->file_exists(
  			'/' . \Test_Encryption_Trashbin::TEST_ENCRYPTION_TRASHBIN_USER1 . '/files_encryption/share-keys/'
  			. $filename . '.' . \Test_Encryption_Trashbin::TEST_ENCRYPTION_TRASHBIN_USER1 . '.shareKey'));
  
  		// delete file
  		\OC\FIles\Filesystem::unlink($filename);
  
  		// check if file not exists
  		$this->assertFalse($this->view->file_exists(
  			'/' . \Test_Encryption_Trashbin::TEST_ENCRYPTION_TRASHBIN_USER1 . '/files/' . $filename));
  
  		// check if key for admin not exists
  		$this->assertFalse($this->view->file_exists(
  			'/' . \Test_Encryption_Trashbin::TEST_ENCRYPTION_TRASHBIN_USER1 . '/files_encryption/keyfiles/' . $filename
  			. '.key'));
  
  		// check if share key for admin not exists
  		$this->assertFalse($this->view->file_exists(
  			'/' . \Test_Encryption_Trashbin::TEST_ENCRYPTION_TRASHBIN_USER1 . '/files_encryption/share-keys/'
  			. $filename . '.' . \Test_Encryption_Trashbin::TEST_ENCRYPTION_TRASHBIN_USER1 . '.shareKey'));
  
  		// get files
  		$trashFiles = $this->view->getDirectoryContent(
  			'/' . \Test_Encryption_Trashbin::TEST_ENCRYPTION_TRASHBIN_USER1 . '/files_trashbin/files/');
  
  		$trashFileSuffix = null;
  		// find created file with timestamp
  		foreach ($trashFiles as $file) {
  			if (strncmp($file['path'], $filename, strlen($filename))) {
  				$path_parts = pathinfo($file['name']);
  				$trashFileSuffix = $path_parts['extension'];
  			}
  		}
  
  		// check if we found the file we created
  		$this->assertNotNull($trashFileSuffix);
  
  		// check if key for admin not exists
  		$this->assertTrue($this->view->file_exists(
  			'/' . \Test_Encryption_Trashbin::TEST_ENCRYPTION_TRASHBIN_USER1 . '/files_trashbin/keyfiles/' . $filename
  			. '.key.' . $trashFileSuffix));
  
  		// check if share key for admin not exists
  		$this->assertTrue($this->view->file_exists(
  			'/' . \Test_Encryption_Trashbin::TEST_ENCRYPTION_TRASHBIN_USER1 . '/files_trashbin/share-keys/' . $filename
  			. '.' . \Test_Encryption_Trashbin::TEST_ENCRYPTION_TRASHBIN_USER1 . '.shareKey.' . $trashFileSuffix));
  
  		// return filename for next test
  		return $filename . '.' . $trashFileSuffix;
  	}
  
  	/**
31b7f2792   Kload   Upgrade to ownclo...
187
  	 * @medium
6d9380f96   Cédric Dupont   Update sources OC...
188
  	 * test restore file
03e52840d   Kload   Init
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
  	 *
  	 * @depends testDeleteFile
  	 */
  	function testRestoreFile($filename) {
  
  		// prepare file information
  		$path_parts = pathinfo($filename);
  		$trashFileSuffix = $path_parts['extension'];
  		$timestamp = str_replace('d', '', $trashFileSuffix);
  		$fileNameWithoutSuffix = str_replace('.' . $trashFileSuffix, '', $filename);
  
  		// restore file
  		$this->assertTrue(\OCA\Files_Trashbin\Trashbin::restore($filename, $fileNameWithoutSuffix, $timestamp));
  
  		// check if file exists
  		$this->assertTrue($this->view->file_exists(
  			'/' . \Test_Encryption_Trashbin::TEST_ENCRYPTION_TRASHBIN_USER1 . '/files/' . $fileNameWithoutSuffix));
  
  		// check if key for admin exists
  		$this->assertTrue($this->view->file_exists(
  			'/' . \Test_Encryption_Trashbin::TEST_ENCRYPTION_TRASHBIN_USER1 . '/files_encryption/keyfiles/'
  			. $fileNameWithoutSuffix . '.key'));
  
  		// check if share key for admin exists
  		$this->assertTrue($this->view->file_exists(
  			'/' . \Test_Encryption_Trashbin::TEST_ENCRYPTION_TRASHBIN_USER1 . '/files_encryption/share-keys/'
  			. $fileNameWithoutSuffix . '.' . \Test_Encryption_Trashbin::TEST_ENCRYPTION_TRASHBIN_USER1 . '.shareKey'));
  	}
  
  	/**
31b7f2792   Kload   Upgrade to ownclo...
219
  	 * @medium
6d9380f96   Cédric Dupont   Update sources OC...
220
  	 * test delete file forever
03e52840d   Kload   Init
221
222
223
224
  	 */
  	function testPermanentDeleteFile() {
  
  		// generate filename
a293d369c   Kload   Update sources to...
225
  		$filename = 'tmp-' . uniqid() . '.txt';
03e52840d   Kload   Init
226
227
  
  		// save file with content
31b7f2792   Kload   Upgrade to ownclo...
228
  		$cryptedFile = file_put_contents('crypt:///' .$this->userId. '/files/' . $filename, $this->dataShort);
03e52840d   Kload   Init
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
279
280
281
282
283
  
  		// test that data was successfully written
  		$this->assertTrue(is_int($cryptedFile));
  
  		// check if key for admin exists
  		$this->assertTrue($this->view->file_exists(
  			'/' . \Test_Encryption_Trashbin::TEST_ENCRYPTION_TRASHBIN_USER1 . '/files_encryption/keyfiles/' . $filename
  			. '.key'));
  
  		// check if share key for admin exists
  		$this->assertTrue($this->view->file_exists(
  			'/' . \Test_Encryption_Trashbin::TEST_ENCRYPTION_TRASHBIN_USER1 . '/files_encryption/share-keys/'
  			. $filename . '.' . \Test_Encryption_Trashbin::TEST_ENCRYPTION_TRASHBIN_USER1 . '.shareKey'));
  
  		// delete file
  		\OC\FIles\Filesystem::unlink($filename);
  
  		// check if file not exists
  		$this->assertFalse($this->view->file_exists(
  			'/' . \Test_Encryption_Trashbin::TEST_ENCRYPTION_TRASHBIN_USER1 . '/files/' . $filename));
  
  		// check if key for admin not exists
  		$this->assertFalse($this->view->file_exists(
  			'/' . \Test_Encryption_Trashbin::TEST_ENCRYPTION_TRASHBIN_USER1 . '/files_encryption/keyfiles/' . $filename
  			. '.key'));
  
  		// check if share key for admin not exists
  		$this->assertFalse($this->view->file_exists(
  			'/' . \Test_Encryption_Trashbin::TEST_ENCRYPTION_TRASHBIN_USER1 . '/files_encryption/share-keys/'
  			. $filename . '.' . \Test_Encryption_Trashbin::TEST_ENCRYPTION_TRASHBIN_USER1 . '.shareKey'));
  
  		// find created file with timestamp
  		$query = \OC_DB::prepare('SELECT `timestamp`,`type` FROM `*PREFIX*files_trash`'
  								 . ' WHERE `id`=?');
  		$result = $query->execute(array($filename))->fetchRow();
  
  		$this->assertTrue(is_array($result));
  
  		// build suffix
  		$trashFileSuffix = 'd' . $result['timestamp'];
  
  		// check if key for admin exists
  		$this->assertTrue($this->view->file_exists(
  			'/' . \Test_Encryption_Trashbin::TEST_ENCRYPTION_TRASHBIN_USER1 . '/files_trashbin/keyfiles/' . $filename
  			. '.key.' . $trashFileSuffix));
  
  		// check if share key for admin exists
  		$this->assertTrue($this->view->file_exists(
  			'/' . \Test_Encryption_Trashbin::TEST_ENCRYPTION_TRASHBIN_USER1 . '/files_trashbin/share-keys/' . $filename
  			. '.' . \Test_Encryption_Trashbin::TEST_ENCRYPTION_TRASHBIN_USER1 . '.shareKey.' . $trashFileSuffix));
  
  		// get timestamp from file
  		$timestamp = str_replace('d', '', $trashFileSuffix);
  
  		// delete file forever
6d9380f96   Cédric Dupont   Update sources OC...
284
  		$this->assertGreaterThan(0, \OCA\Files_Trashbin\Trashbin::delete($filename, $this->userId, $timestamp));
03e52840d   Kload   Init
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
  
  		// check if key for admin not exists
  		$this->assertFalse($this->view->file_exists(
  			'/' . \Test_Encryption_Trashbin::TEST_ENCRYPTION_TRASHBIN_USER1 . '/files_trashbin/files/' . $filename . '.'
  			. $trashFileSuffix));
  
  		// check if key for admin not exists
  		$this->assertFalse($this->view->file_exists(
  			'/' . \Test_Encryption_Trashbin::TEST_ENCRYPTION_TRASHBIN_USER1 . '/files_trashbin/keyfiles/' . $filename
  			. '.key.' . $trashFileSuffix));
  
  		// check if share key for admin not exists
  		$this->assertFalse($this->view->file_exists(
  			'/' . \Test_Encryption_Trashbin::TEST_ENCRYPTION_TRASHBIN_USER1 . '/files_trashbin/share-keys/' . $filename
  			. '.' . \Test_Encryption_Trashbin::TEST_ENCRYPTION_TRASHBIN_USER1 . '.shareKey.' . $trashFileSuffix));
  	}
31b7f2792   Kload   Upgrade to ownclo...
301
  }