storage.php 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538
  1. <?php
  2. /**
  3. * ownCloud
  4. *
  5. * @author Robin Appelman
  6. * @copyright 2012 Robin Appelman icewind@owncloud.com
  7. *
  8. * This library is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE
  10. * License as published by the Free Software Foundation; either
  11. * version 3 of the License, or any later version.
  12. *
  13. * This library is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU AFFERO GENERAL PUBLIC LICENSE for more details.
  17. *
  18. * You should have received a copy of the GNU Affero General Public
  19. * License along with this library. If not, see <http://www.gnu.org/licenses/>.
  20. *
  21. */
  22. namespace Test\Files\Storage;
  23. abstract class Storage extends \Test\TestCase {
  24. /**
  25. * @var \OC\Files\Storage\Storage instance
  26. */
  27. protected $instance;
  28. protected $waitDelay = 0;
  29. /**
  30. * Sleep for the number of seconds specified in the
  31. * $waitDelay attribute
  32. */
  33. protected function wait() {
  34. if ($this->waitDelay > 0) {
  35. sleep($this->waitDelay);
  36. }
  37. }
  38. /**
  39. * the root folder of the storage should always exist, be readable and be recognized as a directory
  40. */
  41. public function testRoot() {
  42. $this->assertTrue($this->instance->file_exists('/'), 'Root folder does not exist');
  43. $this->assertTrue($this->instance->isReadable('/'), 'Root folder is not readable');
  44. $this->assertTrue($this->instance->is_dir('/'), 'Root folder is not a directory');
  45. $this->assertFalse($this->instance->is_file('/'), 'Root folder is a file');
  46. $this->assertEquals('dir', $this->instance->filetype('/'));
  47. //without this, any further testing would be useless, not an actual requirement for filestorage though
  48. $this->assertTrue($this->instance->isUpdatable('/'), 'Root folder is not writable');
  49. }
  50. /**
  51. * Check that the test() function works
  52. */
  53. public function testTestFunction() {
  54. $this->assertTrue($this->instance->test());
  55. }
  56. /**
  57. * @dataProvider directoryProvider
  58. */
  59. public function testDirectories($directory) {
  60. $this->assertFalse($this->instance->file_exists('/' . $directory));
  61. $this->assertTrue($this->instance->mkdir('/' . $directory));
  62. $this->assertTrue($this->instance->file_exists('/' . $directory));
  63. $this->assertTrue($this->instance->is_dir('/' . $directory));
  64. $this->assertFalse($this->instance->is_file('/' . $directory));
  65. $this->assertEquals('dir', $this->instance->filetype('/' . $directory));
  66. $this->assertEquals(0, $this->instance->filesize('/' . $directory));
  67. $this->assertTrue($this->instance->isReadable('/' . $directory));
  68. $this->assertTrue($this->instance->isUpdatable('/' . $directory));
  69. $dh = $this->instance->opendir('/');
  70. $content = array();
  71. while ($file = readdir($dh)) {
  72. if ($file != '.' and $file != '..') {
  73. $content[] = $file;
  74. }
  75. }
  76. $this->assertEquals(array($directory), $content);
  77. $this->assertFalse($this->instance->mkdir('/' . $directory)); //cant create existing folders
  78. $this->assertTrue($this->instance->rmdir('/' . $directory));
  79. $this->wait();
  80. $this->assertFalse($this->instance->file_exists('/' . $directory));
  81. $this->assertFalse($this->instance->rmdir('/' . $directory)); //cant remove non existing folders
  82. $dh = $this->instance->opendir('/');
  83. $content = array();
  84. while ($file = readdir($dh)) {
  85. if ($file != '.' and $file != '..') {
  86. $content[] = $file;
  87. }
  88. }
  89. $this->assertEquals(array(), $content);
  90. }
  91. public function directoryProvider() {
  92. return [
  93. ['folder'],
  94. [' folder'],
  95. ['folder '],
  96. ['folder with space'],
  97. ['spéciäl földer'],
  98. ['test single\'quote'],
  99. ];
  100. }
  101. function loremFileProvider() {
  102. $root = \OC::$SERVERROOT . '/tests/data/';
  103. return array(
  104. // small file
  105. array($root . 'lorem.txt'),
  106. // bigger file (> 8 KB which is the standard PHP block size)
  107. array($root . 'lorem-big.txt')
  108. );
  109. }
  110. /**
  111. * test the various uses of file_get_contents and file_put_contents
  112. *
  113. * @dataProvider loremFileProvider
  114. */
  115. public function testGetPutContents($sourceFile) {
  116. $sourceText = file_get_contents($sourceFile);
  117. //fill a file with string data
  118. $this->instance->file_put_contents('/lorem.txt', $sourceText);
  119. $this->assertFalse($this->instance->is_dir('/lorem.txt'));
  120. $this->assertEquals($sourceText, $this->instance->file_get_contents('/lorem.txt'), 'data returned from file_get_contents is not equal to the source data');
  121. //empty the file
  122. $this->instance->file_put_contents('/lorem.txt', '');
  123. $this->assertEquals('', $this->instance->file_get_contents('/lorem.txt'), 'file not emptied');
  124. }
  125. /**
  126. * test various known mimetypes
  127. */
  128. public function testMimeType() {
  129. $this->assertEquals('httpd/unix-directory', $this->instance->getMimeType('/'));
  130. $this->assertEquals(false, $this->instance->getMimeType('/non/existing/file'));
  131. $textFile = \OC::$SERVERROOT . '/tests/data/lorem.txt';
  132. $this->instance->file_put_contents('/lorem.txt', file_get_contents($textFile, 'r'));
  133. $this->assertEquals('text/plain', $this->instance->getMimeType('/lorem.txt'));
  134. $pngFile = \OC::$SERVERROOT . '/tests/data/logo-wide.png';
  135. $this->instance->file_put_contents('/logo-wide.png', file_get_contents($pngFile, 'r'));
  136. $this->assertEquals('image/png', $this->instance->getMimeType('/logo-wide.png'));
  137. $svgFile = \OC::$SERVERROOT . '/tests/data/logo-wide.svg';
  138. $this->instance->file_put_contents('/logo-wide.svg', file_get_contents($svgFile, 'r'));
  139. $this->assertEquals('image/svg+xml', $this->instance->getMimeType('/logo-wide.svg'));
  140. }
  141. public function copyAndMoveProvider() {
  142. return [
  143. ['/source.txt', '/target.txt'],
  144. ['/source.txt', '/target with space.txt'],
  145. ['/source with space.txt', '/target.txt'],
  146. ['/source with space.txt', '/target with space.txt'],
  147. ['/source.txt', '/tärgét.txt'],
  148. ['/sòurcē.txt', '/target.txt'],
  149. ['/sòurcē.txt', '/tärgét.txt'],
  150. ['/single \' quote.txt', '/tar\'get.txt'],
  151. ];
  152. }
  153. public function initSourceAndTarget ($source, $target = null) {
  154. $textFile = \OC::$SERVERROOT . '/tests/data/lorem.txt';
  155. $this->instance->file_put_contents($source, file_get_contents($textFile));
  156. if ($target) {
  157. $testContents = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
  158. $this->instance->file_put_contents($target, $testContents);
  159. }
  160. }
  161. public function assertSameAsLorem ($file) {
  162. $textFile = \OC::$SERVERROOT . '/tests/data/lorem.txt';
  163. $this->assertEquals(
  164. file_get_contents($textFile),
  165. $this->instance->file_get_contents($file),
  166. 'Expected '.$file.' to be a copy of '.$textFile
  167. );
  168. }
  169. /**
  170. * @dataProvider copyAndMoveProvider
  171. */
  172. public function testCopy($source, $target) {
  173. $this->initSourceAndTarget($source);
  174. $this->instance->copy($source, $target);
  175. $this->assertTrue($this->instance->file_exists($target), $target.' was not created');
  176. $this->assertSameAsLorem($target);
  177. $this->assertTrue($this->instance->file_exists($source), $source.' was deleted');
  178. }
  179. /**
  180. * @dataProvider copyAndMoveProvider
  181. */
  182. public function testMove($source, $target) {
  183. $this->initSourceAndTarget($source);
  184. $this->instance->rename($source, $target);
  185. $this->wait();
  186. $this->assertTrue($this->instance->file_exists($target), $target.' was not created');
  187. $this->assertFalse($this->instance->file_exists($source), $source.' still exists');
  188. $this->assertSameAsLorem($target);
  189. }
  190. /**
  191. * @dataProvider copyAndMoveProvider
  192. */
  193. public function testCopyOverwrite($source, $target) {
  194. $this->initSourceAndTarget($source,$target);
  195. $this->instance->copy($source, $target);
  196. $this->assertTrue($this->instance->file_exists($target), $target.' was not created');
  197. $this->assertTrue($this->instance->file_exists($source), $source.' was deleted');
  198. $this->assertSameAsLorem($target);
  199. $this->assertSameAsLorem($source);
  200. }
  201. /**
  202. * @dataProvider copyAndMoveProvider
  203. */
  204. public function testMoveOverwrite($source, $target) {
  205. $this->initSourceAndTarget($source, $target);
  206. $this->instance->rename($source, $target);
  207. $this->assertTrue($this->instance->file_exists($target), $target.' was not created');
  208. $this->assertFalse($this->instance->file_exists($source), $source.' still exists');
  209. $this->assertSameAsLorem($target);
  210. }
  211. public function testLocal() {
  212. $textFile = \OC::$SERVERROOT . '/tests/data/lorem.txt';
  213. $this->instance->file_put_contents('/lorem.txt', file_get_contents($textFile));
  214. $localFile = $this->instance->getLocalFile('/lorem.txt');
  215. $this->assertTrue(file_exists($localFile));
  216. $this->assertEquals(file_get_contents($textFile), file_get_contents($localFile));
  217. $this->instance->mkdir('/folder');
  218. $this->instance->file_put_contents('/folder/lorem.txt', file_get_contents($textFile));
  219. $this->instance->file_put_contents('/folder/bar.txt', 'asd');
  220. $this->instance->mkdir('/folder/recursive');
  221. $this->instance->file_put_contents('/folder/recursive/file.txt', 'foo');
  222. $localFolder = $this->instance->getLocalFolder('/folder');
  223. $this->assertTrue(is_dir($localFolder));
  224. // test below require to use instance->getLocalFile because the physical storage might be different
  225. $localFile = $this->instance->getLocalFile('/folder/lorem.txt');
  226. $this->assertTrue(file_exists($localFile));
  227. $this->assertEquals(file_get_contents($localFile), file_get_contents($textFile));
  228. $localFile = $this->instance->getLocalFile('/folder/bar.txt');
  229. $this->assertTrue(file_exists($localFile));
  230. $this->assertEquals(file_get_contents($localFile), 'asd');
  231. $localFile = $this->instance->getLocalFile('/folder/recursive/file.txt');
  232. $this->assertTrue(file_exists($localFile));
  233. $this->assertEquals(file_get_contents($localFile), 'foo');
  234. }
  235. public function testStat() {
  236. $textFile = \OC::$SERVERROOT . '/tests/data/lorem.txt';
  237. $ctimeStart = time();
  238. $this->instance->file_put_contents('/lorem.txt', file_get_contents($textFile));
  239. $this->assertTrue($this->instance->isReadable('/lorem.txt'));
  240. $ctimeEnd = time();
  241. $mTime = $this->instance->filemtime('/lorem.txt');
  242. $this->assertTrue($this->instance->hasUpdated('/lorem.txt', $ctimeStart - 5));
  243. $this->assertTrue($this->instance->hasUpdated('/', $ctimeStart - 5));
  244. // check that ($ctimeStart - 5) <= $mTime <= ($ctimeEnd + 1)
  245. $this->assertGreaterThanOrEqual(($ctimeStart - 5), $mTime);
  246. $this->assertLessThanOrEqual(($ctimeEnd + 1), $mTime);
  247. $this->assertEquals(filesize($textFile), $this->instance->filesize('/lorem.txt'));
  248. $stat = $this->instance->stat('/lorem.txt');
  249. //only size and mtime are required in the result
  250. $this->assertEquals($stat['size'], $this->instance->filesize('/lorem.txt'));
  251. $this->assertEquals($stat['mtime'], $mTime);
  252. if ($this->instance->touch('/lorem.txt', 100) !== false) {
  253. $mTime = $this->instance->filemtime('/lorem.txt');
  254. $this->assertEquals($mTime, 100);
  255. }
  256. $mtimeStart = time();
  257. $this->instance->unlink('/lorem.txt');
  258. $this->assertTrue($this->instance->hasUpdated('/', $mtimeStart - 5));
  259. }
  260. public function testUnlink() {
  261. $textFile = \OC::$SERVERROOT . '/tests/data/lorem.txt';
  262. $this->instance->file_put_contents('/lorem.txt', file_get_contents($textFile));
  263. $this->assertTrue($this->instance->file_exists('/lorem.txt'));
  264. $this->assertTrue($this->instance->unlink('/lorem.txt'));
  265. $this->wait();
  266. $this->assertFalse($this->instance->file_exists('/lorem.txt'));
  267. }
  268. public function testFOpen() {
  269. $textFile = \OC::$SERVERROOT . '/tests/data/lorem.txt';
  270. $fh = @$this->instance->fopen('foo', 'r');
  271. if ($fh) {
  272. fclose($fh);
  273. }
  274. $this->assertFalse($fh);
  275. $this->assertFalse($this->instance->file_exists('foo'));
  276. $fh = $this->instance->fopen('foo', 'w');
  277. fwrite($fh, file_get_contents($textFile));
  278. fclose($fh);
  279. $this->assertTrue($this->instance->file_exists('foo'));
  280. $fh = $this->instance->fopen('foo', 'r');
  281. $content = stream_get_contents($fh);
  282. $this->assertEquals(file_get_contents($textFile), $content);
  283. }
  284. public function testTouchCreateFile() {
  285. $this->assertFalse($this->instance->file_exists('touch'));
  286. // returns true on success
  287. $this->assertTrue($this->instance->touch('touch'));
  288. $this->assertTrue($this->instance->file_exists('touch'));
  289. }
  290. public function testRecursiveRmdir() {
  291. $this->instance->mkdir('folder');
  292. $this->instance->mkdir('folder/bar');
  293. $this->wait();
  294. $this->instance->file_put_contents('folder/asd.txt', 'foobar');
  295. $this->instance->file_put_contents('folder/bar/foo.txt', 'asd');
  296. $this->assertTrue($this->instance->rmdir('folder'));
  297. $this->wait();
  298. $this->assertFalse($this->instance->file_exists('folder/asd.txt'));
  299. $this->assertFalse($this->instance->file_exists('folder/bar/foo.txt'));
  300. $this->assertFalse($this->instance->file_exists('folder/bar'));
  301. $this->assertFalse($this->instance->file_exists('folder'));
  302. }
  303. public function testRecursiveUnlink() {
  304. $this->instance->mkdir('folder');
  305. $this->instance->mkdir('folder/bar');
  306. $this->instance->file_put_contents('folder/asd.txt', 'foobar');
  307. $this->instance->file_put_contents('folder/bar/foo.txt', 'asd');
  308. $this->assertTrue($this->instance->unlink('folder'));
  309. $this->wait();
  310. $this->assertFalse($this->instance->file_exists('folder/asd.txt'));
  311. $this->assertFalse($this->instance->file_exists('folder/bar/foo.txt'));
  312. $this->assertFalse($this->instance->file_exists('folder/bar'));
  313. $this->assertFalse($this->instance->file_exists('folder'));
  314. }
  315. public function hashProvider() {
  316. return array(
  317. array('Foobar', 'md5'),
  318. array('Foobar', 'sha1'),
  319. array('Foobar', 'sha256'),
  320. );
  321. }
  322. /**
  323. * @dataProvider hashProvider
  324. */
  325. public function testHash($data, $type) {
  326. $this->instance->file_put_contents('hash.txt', $data);
  327. $this->assertEquals(hash($type, $data), $this->instance->hash($type, 'hash.txt'));
  328. $this->assertEquals(hash($type, $data, true), $this->instance->hash($type, 'hash.txt', true));
  329. }
  330. public function testHashInFileName() {
  331. $this->instance->file_put_contents('#test.txt', 'data');
  332. $this->assertEquals('data', $this->instance->file_get_contents('#test.txt'));
  333. $this->instance->mkdir('#foo');
  334. $this->instance->file_put_contents('#foo/test.txt', 'data');
  335. $this->assertEquals('data', $this->instance->file_get_contents('#foo/test.txt'));
  336. $dh = $this->instance->opendir('#foo');
  337. $content = array();
  338. while ($file = readdir($dh)) {
  339. if ($file != '.' and $file != '..') {
  340. $content[] = $file;
  341. }
  342. }
  343. $this->assertEquals(array('test.txt'), $content);
  344. }
  345. public function testCopyOverWriteFile() {
  346. $this->instance->file_put_contents('target.txt', 'foo');
  347. $this->instance->file_put_contents('source.txt', 'bar');
  348. $this->instance->copy('source.txt', 'target.txt');
  349. $this->assertEquals('bar', $this->instance->file_get_contents('target.txt'));
  350. }
  351. public function testRenameOverWriteFile() {
  352. $this->instance->file_put_contents('target.txt', 'foo');
  353. $this->instance->file_put_contents('source.txt', 'bar');
  354. $this->instance->rename('source.txt', 'target.txt');
  355. $this->assertEquals('bar', $this->instance->file_get_contents('target.txt'));
  356. $this->assertFalse($this->instance->file_exists('source.txt'));
  357. }
  358. public function testRenameDirectory() {
  359. $this->instance->mkdir('source');
  360. $this->instance->file_put_contents('source/test1.txt', 'foo');
  361. $this->instance->file_put_contents('source/test2.txt', 'qwerty');
  362. $this->instance->mkdir('source/subfolder');
  363. $this->instance->file_put_contents('source/subfolder/test.txt', 'bar');
  364. $this->instance->rename('source', 'target');
  365. $this->assertFalse($this->instance->file_exists('source'));
  366. $this->assertFalse($this->instance->file_exists('source/test1.txt'));
  367. $this->assertFalse($this->instance->file_exists('source/test2.txt'));
  368. $this->assertFalse($this->instance->file_exists('source/subfolder'));
  369. $this->assertFalse($this->instance->file_exists('source/subfolder/test.txt'));
  370. $this->assertTrue($this->instance->file_exists('target'));
  371. $this->assertTrue($this->instance->file_exists('target/test1.txt'));
  372. $this->assertTrue($this->instance->file_exists('target/test2.txt'));
  373. $this->assertTrue($this->instance->file_exists('target/subfolder'));
  374. $this->assertTrue($this->instance->file_exists('target/subfolder/test.txt'));
  375. $this->assertEquals('foo', $this->instance->file_get_contents('target/test1.txt'));
  376. $this->assertEquals('qwerty', $this->instance->file_get_contents('target/test2.txt'));
  377. $this->assertEquals('bar', $this->instance->file_get_contents('target/subfolder/test.txt'));
  378. }
  379. public function testRenameOverWriteDirectory() {
  380. $this->instance->mkdir('source');
  381. $this->instance->file_put_contents('source/test1.txt', 'foo');
  382. $this->instance->mkdir('target');
  383. $this->instance->file_put_contents('target/test1.txt', 'bar');
  384. $this->instance->file_put_contents('target/test2.txt', 'bar');
  385. $this->assertTrue($this->instance->rename('source', 'target'), 'rename must return true on success');
  386. $this->assertFalse($this->instance->file_exists('source'), 'source has not been removed');
  387. $this->assertFalse($this->instance->file_exists('source/test1.txt'), 'source/test1.txt has not been removed');
  388. $this->assertFalse($this->instance->file_exists('target/test2.txt'), 'target/test2.txt has not been removed');
  389. $this->assertEquals('foo', $this->instance->file_get_contents('target/test1.txt'), 'target/test1.txt has not been overwritten');
  390. }
  391. public function testRenameOverWriteDirectoryOverFile() {
  392. $this->instance->mkdir('source');
  393. $this->instance->file_put_contents('source/test1.txt', 'foo');
  394. $this->instance->file_put_contents('target', 'bar');
  395. $this->assertTrue($this->instance->rename('source', 'target'), 'rename must return true on success');
  396. $this->assertFalse($this->instance->file_exists('source'));
  397. $this->assertFalse($this->instance->file_exists('source/test1.txt'));
  398. $this->assertEquals('foo', $this->instance->file_get_contents('target/test1.txt'));
  399. }
  400. public function testCopyDirectory() {
  401. $this->instance->mkdir('source');
  402. $this->instance->file_put_contents('source/test1.txt', 'foo');
  403. $this->instance->file_put_contents('source/test2.txt', 'qwerty');
  404. $this->instance->mkdir('source/subfolder');
  405. $this->instance->file_put_contents('source/subfolder/test.txt', 'bar');
  406. $this->instance->copy('source', 'target');
  407. $this->assertTrue($this->instance->file_exists('source'));
  408. $this->assertTrue($this->instance->file_exists('source/test1.txt'));
  409. $this->assertTrue($this->instance->file_exists('source/test2.txt'));
  410. $this->assertTrue($this->instance->file_exists('source/subfolder'));
  411. $this->assertTrue($this->instance->file_exists('source/subfolder/test.txt'));
  412. $this->assertTrue($this->instance->file_exists('target'));
  413. $this->assertTrue($this->instance->file_exists('target/test1.txt'));
  414. $this->assertTrue($this->instance->file_exists('target/test2.txt'));
  415. $this->assertTrue($this->instance->file_exists('target/subfolder'));
  416. $this->assertTrue($this->instance->file_exists('target/subfolder/test.txt'));
  417. $this->assertEquals('foo', $this->instance->file_get_contents('target/test1.txt'));
  418. $this->assertEquals('qwerty', $this->instance->file_get_contents('target/test2.txt'));
  419. $this->assertEquals('bar', $this->instance->file_get_contents('target/subfolder/test.txt'));
  420. }
  421. public function testCopyOverWriteDirectory() {
  422. $this->instance->mkdir('source');
  423. $this->instance->file_put_contents('source/test1.txt', 'foo');
  424. $this->instance->mkdir('target');
  425. $this->instance->file_put_contents('target/test1.txt', 'bar');
  426. $this->instance->file_put_contents('target/test2.txt', 'bar');
  427. $this->instance->copy('source', 'target');
  428. $this->assertFalse($this->instance->file_exists('target/test2.txt'));
  429. $this->assertEquals('foo', $this->instance->file_get_contents('target/test1.txt'));
  430. }
  431. public function testCopyOverWriteDirectoryOverFile() {
  432. $this->instance->mkdir('source');
  433. $this->instance->file_put_contents('source/test1.txt', 'foo');
  434. $this->instance->file_put_contents('target', 'bar');
  435. $this->instance->copy('source', 'target');
  436. $this->assertEquals('foo', $this->instance->file_get_contents('target/test1.txt'));
  437. }
  438. public function testInstanceOfStorage() {
  439. $this->assertTrue($this->instance->instanceOfStorage('\OCP\Files\Storage'));
  440. $this->assertTrue($this->instance->instanceOfStorage(get_class($this->instance)));
  441. $this->assertFalse($this->instance->instanceOfStorage('\OC'));
  442. }
  443. }