Blame view

sources/apps/files_encryption/tests/webdav.php 8.24 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
  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__ . '/util.php';
03e52840d   Kload   Init
30
31
32
33
34
  
  use OCA\Encryption;
  
  /**
   * Class Test_Encryption_Webdav
6d9380f96   Cédric Dupont   Update sources OC...
35
36
   *
   * this class provide basic webdav tests for PUT,GET and DELETE
03e52840d   Kload   Init
37
38
39
40
41
42
43
44
   */
  class Test_Encryption_Webdav extends \PHPUnit_Framework_TestCase {
  
  	const TEST_ENCRYPTION_WEBDAV_USER1 = "test-webdav-user1";
  
  	public $userId;
  	public $pass;
  	/**
6d9380f96   Cédric Dupont   Update sources OC...
45
  	 * @var \OC\Files\View
03e52840d   Kload   Init
46
47
48
49
  	 */
  	public $view;
  	public $dataShort;
  	public $stateFilesTrashbin;
6d9380f96   Cédric Dupont   Update sources OC...
50
  	private $storage;
03e52840d   Kload   Init
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
  	public static function setUpBeforeClass() {
  		// reset backend
  		\OC_User::clearBackends();
  		\OC_User::useBackend('database');
  
  		// Filesystem related hooks
  		\OCA\Encryption\Helper::registerFilesystemHooks();
  
  		// Filesystem related hooks
  		\OCA\Encryption\Helper::registerUserHooks();
  
  		// clear and register hooks
  		\OC_FileProxy::clearProxies();
  		\OC_FileProxy::register(new OCA\Encryption\Proxy());
  
  		// create test user
  		\Test_Encryption_Util::loginHelper(\Test_Encryption_Webdav::TEST_ENCRYPTION_WEBDAV_USER1, true);
6d9380f96   Cédric Dupont   Update sources OC...
68

03e52840d   Kload   Init
69
70
71
72
73
74
75
76
77
78
79
80
  	}
  
  	function setUp() {
  		// reset backend
  		\OC_User::useBackend('database');
  
  		// set user id
  		\OC_User::setUserId(\Test_Encryption_Webdav::TEST_ENCRYPTION_WEBDAV_USER1);
  		$this->userId = \Test_Encryption_Webdav::TEST_ENCRYPTION_WEBDAV_USER1;
  		$this->pass = \Test_Encryption_Webdav::TEST_ENCRYPTION_WEBDAV_USER1;
  
  		// init filesystem view
6d9380f96   Cédric Dupont   Update sources OC...
81
82
  		$this->view = new \OC\Files\View('/');
  		list($this->storage, ) = $this->view->resolvePath('/');
03e52840d   Kload   Init
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
  		// init short data
  		$this->dataShort = 'hats';
  
  		// remember files_trashbin state
  		$this->stateFilesTrashbin = OC_App::isEnabled('files_trashbin');
  
  		// we don't want to tests with app files_trashbin enabled
  		\OC_App::disable('files_trashbin');
  
  		// create test user
  		\Test_Encryption_Util::loginHelper(\Test_Encryption_Webdav::TEST_ENCRYPTION_WEBDAV_USER1);
  	}
  
  	function tearDown() {
  		// reset app files_trashbin
  		if ($this->stateFilesTrashbin) {
  			OC_App::enable('files_trashbin');
6d9380f96   Cédric Dupont   Update sources OC...
100
  		} else {
03e52840d   Kload   Init
101
102
103
104
105
106
107
108
109
110
  			OC_App::disable('files_trashbin');
  		}
  	}
  
  	public static function tearDownAfterClass() {
  		// cleanup test user
  		\OC_User::deleteUser(\Test_Encryption_Webdav::TEST_ENCRYPTION_WEBDAV_USER1);
  	}
  
  	/**
6d9380f96   Cédric Dupont   Update sources OC...
111
  	 * test webdav put random file
03e52840d   Kload   Init
112
113
114
115
  	 */
  	function testWebdavPUT() {
  
  		// generate filename
a293d369c   Kload   Update sources to...
116
  		$filename = '/tmp-' . uniqid() . '.txt';
03e52840d   Kload   Init
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
  
  		// set server vars
  		$_SERVER['REQUEST_METHOD'] = 'OPTIONS';
  
  		$_SERVER['REQUEST_METHOD'] = 'PUT';
  		$_SERVER['REQUEST_URI'] = '/remote.php/webdav' . $filename;
  		$_SERVER['HTTP_AUTHORIZATION'] = 'Basic dGVzdC13ZWJkYXYtdXNlcjE6dGVzdC13ZWJkYXYtdXNlcjE=';
  		$_SERVER['CONTENT_TYPE'] = 'application/octet-stream';
  		$_SERVER['PATH_INFO'] = '/webdav' . $filename;
  		$_SERVER['CONTENT_LENGTH'] = strlen($this->dataShort);
  
  		// handle webdav request
  		$this->handleWebdavRequest($this->dataShort);
  
  		// check if file was created
  		$this->assertTrue($this->view->file_exists('/' . $this->userId . '/files' . $filename));
  
  		// check if key-file was created
  		$this->assertTrue($this->view->file_exists(
  			'/' . $this->userId . '/files_encryption/keyfiles/' . $filename . '.key'));
  
  		// check if shareKey-file was created
  		$this->assertTrue($this->view->file_exists(
  			'/' . $this->userId . '/files_encryption/share-keys/' . $filename . '.' . $this->userId . '.shareKey'));
  
  		// disable encryption proxy to prevent recursive calls
  		$proxyStatus = \OC_FileProxy::$enabled;
  		\OC_FileProxy::$enabled = false;
  
  		// get encrypted file content
  		$encryptedContent = $this->view->file_get_contents('/' . $this->userId . '/files' . $filename);
  
  		// restore proxy state
  		\OC_FileProxy::$enabled = $proxyStatus;
  
  		// check if encrypted content is valid
  		$this->assertTrue(Encryption\Crypt::isCatfileContent($encryptedContent));
  
  		// get decrypted file contents
6d9380f96   Cédric Dupont   Update sources OC...
156
  		$decrypt = file_get_contents('crypt:///' . $this->userId . '/files' . $filename);
03e52840d   Kload   Init
157
158
159
160
161
162
163
164
165
  
  		// check if file content match with the written content
  		$this->assertEquals($this->dataShort, $decrypt);
  
  		// return filename for next test
  		return $filename;
  	}
  
  	/**
6d9380f96   Cédric Dupont   Update sources OC...
166
  	 * test webdav get random file
03e52840d   Kload   Init
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
  	 *
  	 * @depends testWebdavPUT
  	 */
  	function testWebdavGET($filename) {
  
  		// set server vars
  		$_SERVER['REQUEST_METHOD'] = 'GET';
  		$_SERVER['REQUEST_URI'] = '/remote.php/webdav' . $filename;
  		$_SERVER['HTTP_AUTHORIZATION'] = 'Basic dGVzdC13ZWJkYXYtdXNlcjE6dGVzdC13ZWJkYXYtdXNlcjE=';
  		$_SERVER['PATH_INFO'] = '/webdav' . $filename;
  
  		// handle webdav request
  		$content = $this->handleWebdavRequest();
  
  		// check if file content match with the written content
  		$this->assertEquals($this->dataShort, $content);
  
  		// return filename for next test
  		return $filename;
  	}
  
  	/**
6d9380f96   Cédric Dupont   Update sources OC...
189
  	 * test webdav delete random file
03e52840d   Kload   Init
190
191
192
193
194
195
196
197
  	 * @depends testWebdavGET
  	 */
  	function testWebdavDELETE($filename) {
  		// set server vars
  		$_SERVER['REQUEST_METHOD'] = 'DELETE';
  		$_SERVER['REQUEST_URI'] = '/remote.php/webdav' . $filename;
  		$_SERVER['HTTP_AUTHORIZATION'] = 'Basic dGVzdC13ZWJkYXYtdXNlcjE6dGVzdC13ZWJkYXYtdXNlcjE=';
  		$_SERVER['PATH_INFO'] = '/webdav' . $filename;
6d9380f96   Cédric Dupont   Update sources OC...
198
199
  		// at the beginning the file should exist
  		$this->assertTrue($this->view->file_exists('/' . $this->userId . '/files' . $filename));
03e52840d   Kload   Init
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
  		// handle webdav request
  		$content = $this->handleWebdavRequest();
  
  		// check if file was removed
  		$this->assertFalse($this->view->file_exists('/' . $this->userId . '/files' . $filename));
  
  		// check if key-file was removed
  		$this->assertFalse($this->view->file_exists(
  			'/' . $this->userId . '/files_encryption/keyfiles' . $filename . '.key'));
  
  		// check if shareKey-file was removed
  		$this->assertFalse($this->view->file_exists(
  			'/' . $this->userId . '/files_encryption/share-keys' . $filename . '.' . $this->userId . '.shareKey'));
  	}
  
  	/**
6d9380f96   Cédric Dupont   Update sources OC...
216
  	 * handle webdav request
03e52840d   Kload   Init
217
218
219
220
221
222
223
224
225
226
227
228
  	 *
  	 * @param bool $body
  	 *
  	 * @note this init procedure is copied from /apps/files/appinfo/remote.php
  	 */
  	function handleWebdavRequest($body = false) {
  		// Backends
  		$authBackend = new OC_Connector_Sabre_Auth();
  		$lockBackend = new OC_Connector_Sabre_Locks();
  		$requestBackend = new OC_Connector_Sabre_Request();
  
  		// Create ownCloud Dir
6d9380f96   Cédric Dupont   Update sources OC...
229
230
231
232
233
234
  		$root = '/' . $this->userId . '/files';
  		$view = new \OC\Files\View($root);
  		$publicDir = new OC_Connector_Sabre_Directory($view, $view->getFileInfo(''));
  		$objectTree = new \OC\Connector\Sabre\ObjectTree();
  		$mountManager = \OC\Files\Filesystem::getMountManager();
  		$objectTree->init($publicDir, $view, $mountManager);
03e52840d   Kload   Init
235
236
  
  		// Fire up server
6d9380f96   Cédric Dupont   Update sources OC...
237
  		$server = new \Sabre\DAV\Server($publicDir);
03e52840d   Kload   Init
238
239
240
241
  		$server->httpRequest = $requestBackend;
  		$server->setBaseUri('/remote.php/webdav/');
  
  		// Load plugins
6d9380f96   Cédric Dupont   Update sources OC...
242
243
244
245
  		$server->addPlugin(new \Sabre\DAV\Auth\Plugin($authBackend, 'ownCloud'));
  		$server->addPlugin(new \Sabre\DAV\Locks\Plugin($lockBackend));
  		$server->addPlugin(new \Sabre\DAV\Browser\Plugin(false)); // Show something in the Browser, but no upload
  		$server->addPlugin(new OC_Connector_Sabre_QuotaPlugin($view));
03e52840d   Kload   Init
246
  		$server->addPlugin(new OC_Connector_Sabre_MaintenancePlugin());
6d9380f96   Cédric Dupont   Update sources OC...
247
  		$server->debugExceptions = true;
03e52840d   Kload   Init
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
  
  		// And off we go!
  		if ($body) {
  			$server->httpRequest->setBody($body);
  		}
  
  		// turn on output buffering
  		ob_start();
  
  		// handle request
  		$server->exec();
  
  		// file content is written in the output buffer
  		$content = ob_get_contents();
  
  		// flush the output buffer and turn off output buffering
  		ob_end_clean();
  
  		// return captured content
  		return $content;
  	}
31b7f2792   Kload   Upgrade to ownclo...
269
  }