You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

helper.php 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  1. <?php
  2. /**
  3. * @author Björn Schießle <schiessle@owncloud.com>
  4. * @author Joas Schilling <nickvergessen@owncloud.com>
  5. * @author Jörn Friedrich Dreyer <jfd@butonic.de>
  6. * @author Morris Jobke <hey@morrisjobke.de>
  7. *
  8. * @copyright Copyright (c) 2015, ownCloud, Inc.
  9. * @license AGPL-3.0
  10. *
  11. * This code is free software: you can redistribute it and/or modify
  12. * it under the terms of the GNU Affero General Public License, version 3,
  13. * as published by the Free Software Foundation.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU Affero General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU Affero General Public License, version 3,
  21. * along with this program. If not, see <http://www.gnu.org/licenses/>
  22. *
  23. */
  24. namespace OCA\Files_Encryption\Tests;
  25. use OCA\Files_Encryption;
  26. use OCA\Files_Encryption\Helper;
  27. /**
  28. * Class Helper
  29. */
  30. class TestHelper extends TestCase {
  31. const TEST_ENCRYPTION_HELPER_USER1 = "test-helper-user1";
  32. const TEST_ENCRYPTION_HELPER_USER2 = "test-helper-user2";
  33. protected function setUpUsers() {
  34. // create test user
  35. self::loginHelper(self::TEST_ENCRYPTION_HELPER_USER2, true);
  36. self::loginHelper(self::TEST_ENCRYPTION_HELPER_USER1, true);
  37. }
  38. protected function cleanUpUsers() {
  39. // cleanup test user
  40. \OC_User::deleteUser(self::TEST_ENCRYPTION_HELPER_USER1);
  41. \OC_User::deleteUser(self::TEST_ENCRYPTION_HELPER_USER2);
  42. }
  43. public static function setupHooks() {
  44. // Filesystem related hooks
  45. Helper::registerFilesystemHooks();
  46. // clear and register hooks
  47. \OC_FileProxy::clearProxies();
  48. \OC_FileProxy::register(new Files_Encryption\Proxy());
  49. }
  50. public static function tearDownAfterClass() {
  51. parent::tearDownAfterClass();
  52. }
  53. /**
  54. * @medium
  55. */
  56. function testStripPartialFileExtension() {
  57. $partFilename = 'testfile.txt.part';
  58. $filename = 'testfile.txt';
  59. $this->assertTrue(Helper::isPartialFilePath($partFilename));
  60. $this->assertEquals('testfile.txt', Helper::stripPartialFileExtension($partFilename));
  61. $this->assertFalse(Helper::isPartialFilePath($filename));
  62. $this->assertEquals('testfile.txt', Helper::stripPartialFileExtension($filename));
  63. }
  64. /**
  65. * @medium
  66. */
  67. function testStripPartialFileExtensionWithTransferIdPath() {
  68. $partFilename = 'testfile.txt.ocTransferId643653835.part';
  69. $filename = 'testfile.txt';
  70. $this->assertTrue(Helper::isPartialFilePath($partFilename));
  71. $this->assertEquals('testfile.txt', Helper::stripPartialFileExtension($partFilename));
  72. $this->assertFalse(Helper::isPartialFilePath($filename));
  73. $this->assertEquals('testfile.txt', Helper::stripPartialFileExtension($filename));
  74. }
  75. /**
  76. * @dataProvider dataVersionsPathPositive
  77. */
  78. function testGetPathFromVersionPositive($path, $expected) {
  79. $result = Helper::getPathFromVersion($path);
  80. $this->assertSame($expected, $result);
  81. }
  82. function dataVersionsPathPositive() {
  83. return array(
  84. array('/user/files_versions/foo/bar/test.txt.v456756835', 'foo/bar/test.txt'),
  85. array('user/files_versions/foo/bar/test.txt.v456756835', 'foo/bar/test.txt'),
  86. array('user/files_versions//foo/bar/test.txt.v456756835', 'foo/bar/test.txt'),
  87. array('user/files_versions/test.txt.v456756835', 'test.txt'),
  88. );
  89. }
  90. /**
  91. * @dataProvider dataVersionsPathNegative
  92. * @expectedException \OCA\Files_Encryption\Exception\EncryptionException
  93. */
  94. function testGetPathFromVersionNegative($path) {
  95. Helper::getPathFromVersion($path);
  96. }
  97. function dataVersionsPathNegative() {
  98. return array(
  99. array('/user/files_versions/'),
  100. array('/user/files_versions'),
  101. );
  102. }
  103. /**
  104. * @dataProvider dataPathsCachedFilePositive
  105. */
  106. function testGetPathFromCachedFilePositive($path, $expected) {
  107. $result = Helper::getPathFromCachedFile($path);
  108. $this->assertEquals($expected, $result);
  109. }
  110. function dataPathsCachedFilePositive() {
  111. return array(
  112. array('/user/cache/transferid636483/foo/bar/test.txt', 'foo/bar/test.txt'),
  113. array('/user/cache/transferid636483//test.txt', 'test.txt'),
  114. array('user/cache/transferid636483//test.txt', 'test.txt'),
  115. );
  116. }
  117. /**
  118. * @dataProvider dataPathsCachedFileNegative
  119. * @expectedException \OCA\Files_Encryption\Exception\EncryptionException
  120. */
  121. function testGetPathFromCachedFileNegative($path) {
  122. Helper::getPathFromCachedFile($path);
  123. }
  124. function dataPathsCachedFileNegative() {
  125. return array(
  126. array('/user/cache/transferid636483/'),
  127. array('/user/cache/transferid636483'),
  128. array('/user/cache/transferid636483//'),
  129. array('/user/cache'),
  130. );
  131. }
  132. function testGetUser() {
  133. self::setUpUsers();
  134. $path1 = "/" . self::TEST_ENCRYPTION_HELPER_USER1 . "/files/foo/bar.txt";
  135. $path2 = "/" . self::TEST_ENCRYPTION_HELPER_USER1 . "/cache/foo/bar.txt";
  136. $path3 = "/" . self::TEST_ENCRYPTION_HELPER_USER2 . "/thumbnails/foo";
  137. $path4 ="/" . "/" . self::TEST_ENCRYPTION_HELPER_USER1;
  138. self::loginHelper(self::TEST_ENCRYPTION_HELPER_USER1);
  139. // if we are logged-in every path should return the currently logged-in user
  140. $this->assertEquals(self::TEST_ENCRYPTION_HELPER_USER1, Helper::getUser($path1));
  141. $this->assertEquals(self::TEST_ENCRYPTION_HELPER_USER1, Helper::getUser($path2));
  142. $this->assertEquals(self::TEST_ENCRYPTION_HELPER_USER1, Helper::getUser($path3));
  143. $this->assertEquals(self::TEST_ENCRYPTION_HELPER_USER1, Helper::getUser($path4));
  144. // now log out
  145. self::logoutHelper();
  146. // now we should only get the user from /user/files and user/cache paths
  147. $this->assertEquals(self::TEST_ENCRYPTION_HELPER_USER1, Helper::getUser($path1));
  148. $this->assertEquals(self::TEST_ENCRYPTION_HELPER_USER1, Helper::getUser($path2));
  149. try {
  150. $this->assertFalse(Helper::getUser($path3));
  151. $this->assertFalse(true, '"OCA\Files_Encryption\Exception\EncryptionException: Could not determine user expected"');
  152. } catch (Files_Encryption\Exception\EncryptionException $e) {
  153. $this->assertSame('Could not determine user', $e->getMessage());
  154. }
  155. try {
  156. $this->assertFalse(Helper::getUser($path4));
  157. $this->assertFalse(true, '"OCA\Files_Encryption\Exception\EncryptionException: Could not determine user expected"');
  158. } catch (Files_Encryption\Exception\EncryptionException $e) {
  159. $this->assertSame('Could not determine user', $e->getMessage());
  160. }
  161. // Log-in again
  162. self::loginHelper(self::TEST_ENCRYPTION_HELPER_USER1);
  163. self::cleanUpUsers();
  164. }
  165. /**
  166. * @dataProvider dataStripUserFilesPath
  167. */
  168. function testStripUserFilesPath($path, $expected) {
  169. $result = Helper::stripUserFilesPath($path);
  170. $this->assertSame($expected, $result);
  171. }
  172. function dataStripUserFilesPath() {
  173. return array(
  174. array('/user/files/foo.txt', 'foo.txt'),
  175. array('//user/files/foo.txt', 'foo.txt'),
  176. array('user//files/foo/bar.txt', 'foo/bar.txt'),
  177. array('user//files/', false),
  178. array('/user', false),
  179. array('', false),
  180. );
  181. }
  182. /**
  183. * @dataProvider dataStripUserFilesPathPositive
  184. */
  185. function testGetUserFromPathPositive($path, $expected) {
  186. self::setUpUsers();
  187. $result = Helper::getUserFromPath($path);
  188. $this->assertSame($expected, $result);
  189. self::cleanUpUsers();
  190. }
  191. function dataStripUserFilesPathPositive() {
  192. return array(
  193. array('/' . self::TEST_ENCRYPTION_HELPER_USER1 . '/files/foo.txt', self::TEST_ENCRYPTION_HELPER_USER1),
  194. array('//' . self::TEST_ENCRYPTION_HELPER_USER2 . '/files_versions/foo.txt', self::TEST_ENCRYPTION_HELPER_USER2),
  195. array('/' . self::TEST_ENCRYPTION_HELPER_USER1 . '/files_trashbin/', self::TEST_ENCRYPTION_HELPER_USER1),
  196. array(self::TEST_ENCRYPTION_HELPER_USER1 . '//cache/foo/bar.txt', self::TEST_ENCRYPTION_HELPER_USER1),
  197. );
  198. }
  199. /**
  200. * @dataProvider dataStripUserFilesPathNegative
  201. * @expectedException \OCA\Files_Encryption\Exception\EncryptionException
  202. */
  203. function testGetUserFromPathNegative($path) {
  204. Helper::getUserFromPath($path);
  205. }
  206. function dataStripUserFilesPathNegative() {
  207. return array(
  208. array('/unknown_user/files/foo.txt'),
  209. array('/' . self::TEST_ENCRYPTION_HELPER_USER2 . '/unknown_folder/foo.txt'),
  210. array('/' . self::TEST_ENCRYPTION_HELPER_USER1),
  211. array(''),
  212. );
  213. }
  214. /**
  215. * @dataProvider dataPaths
  216. */
  217. function testMkdirr($path, $expected) {
  218. self::setUpUsers();
  219. Helper::mkdirr($path, new \OC\Files\View('/' . self::TEST_ENCRYPTION_HELPER_USER1 . '/files'));
  220. // ignore the filename because we only check for the directories
  221. $dirParts = array_slice($expected, 0, -1);
  222. $expectedPath = implode('/', $dirParts);
  223. $this->assertTrue(\OC\Files\Filesystem::is_dir($expectedPath));
  224. // cleanup
  225. \OC\Files\Filesystem::unlink('/' . $expected[0]);
  226. self::cleanUpUsers();
  227. }
  228. /**
  229. * @dataProvider dataDetectFileTypePositive
  230. */
  231. function testDetectFileTypePositive($path, $expected) {
  232. $result = Helper::detectFileType($path);
  233. $this->assertSame($expected, $result);
  234. }
  235. function dataDetectFileTypePositive() {
  236. return array(
  237. array(self::TEST_ENCRYPTION_HELPER_USER1 . '/files', Files_Encryption\Util::FILE_TYPE_FILE),
  238. array(self::TEST_ENCRYPTION_HELPER_USER1 . '/files/foo/bar', Files_Encryption\Util::FILE_TYPE_FILE),
  239. array('/' . self::TEST_ENCRYPTION_HELPER_USER1 . '/files/foo/bar', Files_Encryption\Util::FILE_TYPE_FILE),
  240. array(self::TEST_ENCRYPTION_HELPER_USER1 . '/files_versions', Files_Encryption\Util::FILE_TYPE_VERSION),
  241. array('/' . self::TEST_ENCRYPTION_HELPER_USER1 . '//files_versions/foo/bar', Files_Encryption\Util::FILE_TYPE_VERSION),
  242. array('/' . self::TEST_ENCRYPTION_HELPER_USER1 . '//cache/foo/bar', Files_Encryption\Util::FILE_TYPE_CACHE),
  243. );
  244. }
  245. /**
  246. * @dataProvider dataDetectFileTypeNegative
  247. * @expectedException \OCA\Files_Encryption\Exception\EncryptionException
  248. */
  249. function testDetectFileTypeNegative($path) {
  250. Helper::detectFileType($path);
  251. }
  252. function dataDetectFileTypeNegative() {
  253. return array(
  254. array('/files'),
  255. array('/' . self::TEST_ENCRYPTION_HELPER_USER1 . '/unsuported_dir/foo/bar'),
  256. );
  257. }
  258. /**
  259. * @dataProvider dataPaths
  260. */
  261. function testSplitPath($path, $expected) {
  262. $result = Helper::splitPath($path);
  263. $this->compareArray($result, $expected);
  264. }
  265. function dataPaths() {
  266. return array(
  267. array('foo/bar/test.txt', array('', 'foo', 'bar', 'test.txt')),
  268. array('/foo/bar/test.txt', array('', 'foo', 'bar', 'test.txt')),
  269. array('/foo/bar//test.txt', array('', 'foo', 'bar', 'test.txt')),
  270. array('//foo/bar/test.txt', array('', 'foo', 'bar', 'test.txt')),
  271. array('foo', array('', 'foo')),
  272. array('/foo', array('', 'foo')),
  273. array('//foo', array('', 'foo')),
  274. );
  275. }
  276. function compareArray($result, $expected) {
  277. $this->assertSame(count($expected), count($result));
  278. foreach ($expected as $key => $value) {
  279. $this->assertArrayHasKey($key, $result);
  280. $this->assertSame($value, $result[$key]);
  281. }
  282. }
  283. }