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.

storage.php 20KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536
  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 array(
  93. array('folder'),
  94. array(' folder'),
  95. array('folder '),
  96. array('folder with space'),
  97. array('spéciäl földer'),
  98. );
  99. }
  100. function loremFileProvider() {
  101. $root = \OC::$SERVERROOT . '/tests/data/';
  102. return array(
  103. // small file
  104. array($root . 'lorem.txt'),
  105. // bigger file (> 8 KB which is the standard PHP block size)
  106. array($root . 'lorem-big.txt')
  107. );
  108. }
  109. /**
  110. * test the various uses of file_get_contents and file_put_contents
  111. *
  112. * @dataProvider loremFileProvider
  113. */
  114. public function testGetPutContents($sourceFile) {
  115. $sourceText = file_get_contents($sourceFile);
  116. //fill a file with string data
  117. $this->instance->file_put_contents('/lorem.txt', $sourceText);
  118. $this->assertFalse($this->instance->is_dir('/lorem.txt'));
  119. $this->assertEquals($sourceText, $this->instance->file_get_contents('/lorem.txt'), 'data returned from file_get_contents is not equal to the source data');
  120. //empty the file
  121. $this->instance->file_put_contents('/lorem.txt', '');
  122. $this->assertEquals('', $this->instance->file_get_contents('/lorem.txt'), 'file not emptied');
  123. }
  124. /**
  125. * test various known mimetypes
  126. */
  127. public function testMimeType() {
  128. $this->assertEquals('httpd/unix-directory', $this->instance->getMimeType('/'));
  129. $this->assertEquals(false, $this->instance->getMimeType('/non/existing/file'));
  130. $textFile = \OC::$SERVERROOT . '/tests/data/lorem.txt';
  131. $this->instance->file_put_contents('/lorem.txt', file_get_contents($textFile, 'r'));
  132. $this->assertEquals('text/plain', $this->instance->getMimeType('/lorem.txt'));
  133. $pngFile = \OC::$SERVERROOT . '/tests/data/logo-wide.png';
  134. $this->instance->file_put_contents('/logo-wide.png', file_get_contents($pngFile, 'r'));
  135. $this->assertEquals('image/png', $this->instance->getMimeType('/logo-wide.png'));
  136. $svgFile = \OC::$SERVERROOT . '/tests/data/logo-wide.svg';
  137. $this->instance->file_put_contents('/logo-wide.svg', file_get_contents($svgFile, 'r'));
  138. $this->assertEquals('image/svg+xml', $this->instance->getMimeType('/logo-wide.svg'));
  139. }
  140. public function copyAndMoveProvider() {
  141. return array(
  142. array('/source.txt', '/target.txt'),
  143. array('/source.txt', '/target with space.txt'),
  144. array('/source with space.txt', '/target.txt'),
  145. array('/source with space.txt', '/target with space.txt'),
  146. array('/source.txt', '/tärgét.txt'),
  147. array('/sòurcē.txt', '/target.txt'),
  148. array('/sòurcē.txt', '/tärgét.txt'),
  149. );
  150. }
  151. public function initSourceAndTarget ($source, $target = null) {
  152. $textFile = \OC::$SERVERROOT . '/tests/data/lorem.txt';
  153. $this->instance->file_put_contents($source, file_get_contents($textFile));
  154. if ($target) {
  155. $testContents = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
  156. $this->instance->file_put_contents($target, $testContents);
  157. }
  158. }
  159. public function assertSameAsLorem ($file) {
  160. $textFile = \OC::$SERVERROOT . '/tests/data/lorem.txt';
  161. $this->assertEquals(
  162. file_get_contents($textFile),
  163. $this->instance->file_get_contents($file),
  164. 'Expected '.$file.' to be a copy of '.$textFile
  165. );
  166. }
  167. /**
  168. * @dataProvider copyAndMoveProvider
  169. */
  170. public function testCopy($source, $target) {
  171. $this->initSourceAndTarget($source);
  172. $this->instance->copy($source, $target);
  173. $this->assertTrue($this->instance->file_exists($target), $target.' was not created');
  174. $this->assertSameAsLorem($target);
  175. $this->assertTrue($this->instance->file_exists($source), $source.' was deleted');
  176. }
  177. /**
  178. * @dataProvider copyAndMoveProvider
  179. */
  180. public function testMove($source, $target) {
  181. $this->initSourceAndTarget($source);
  182. $this->instance->rename($source, $target);
  183. $this->wait();
  184. $this->assertTrue($this->instance->file_exists($target), $target.' was not created');
  185. $this->assertFalse($this->instance->file_exists($source), $source.' still exists');
  186. $this->assertSameAsLorem($target);
  187. }
  188. /**
  189. * @dataProvider copyAndMoveProvider
  190. */
  191. public function testCopyOverwrite($source, $target) {
  192. $this->initSourceAndTarget($source,$target);
  193. $this->instance->copy($source, $target);
  194. $this->assertTrue($this->instance->file_exists($target), $target.' was not created');
  195. $this->assertTrue($this->instance->file_exists($source), $source.' was deleted');
  196. $this->assertSameAsLorem($target);
  197. $this->assertSameAsLorem($source);
  198. }
  199. /**
  200. * @dataProvider copyAndMoveProvider
  201. */
  202. public function testMoveOverwrite($source, $target) {
  203. $this->initSourceAndTarget($source, $target);
  204. $this->instance->rename($source, $target);
  205. $this->assertTrue($this->instance->file_exists($target), $target.' was not created');
  206. $this->assertFalse($this->instance->file_exists($source), $source.' still exists');
  207. $this->assertSameAsLorem($target);
  208. }
  209. public function testLocal() {
  210. $textFile = \OC::$SERVERROOT . '/tests/data/lorem.txt';
  211. $this->instance->file_put_contents('/lorem.txt', file_get_contents($textFile));
  212. $localFile = $this->instance->getLocalFile('/lorem.txt');
  213. $this->assertTrue(file_exists($localFile));
  214. $this->assertEquals(file_get_contents($localFile), file_get_contents($textFile));
  215. $this->instance->mkdir('/folder');
  216. $this->instance->file_put_contents('/folder/lorem.txt', file_get_contents($textFile));
  217. $this->instance->file_put_contents('/folder/bar.txt', 'asd');
  218. $this->instance->mkdir('/folder/recursive');
  219. $this->instance->file_put_contents('/folder/recursive/file.txt', 'foo');
  220. $localFolder = $this->instance->getLocalFolder('/folder');
  221. $this->assertTrue(is_dir($localFolder));
  222. // test below require to use instance->getLocalFile because the physical storage might be different
  223. $localFile = $this->instance->getLocalFile('/folder/lorem.txt');
  224. $this->assertTrue(file_exists($localFile));
  225. $this->assertEquals(file_get_contents($localFile), file_get_contents($textFile));
  226. $localFile = $this->instance->getLocalFile('/folder/bar.txt');
  227. $this->assertTrue(file_exists($localFile));
  228. $this->assertEquals(file_get_contents($localFile), 'asd');
  229. $localFile = $this->instance->getLocalFile('/folder/recursive/file.txt');
  230. $this->assertTrue(file_exists($localFile));
  231. $this->assertEquals(file_get_contents($localFile), 'foo');
  232. }
  233. public function testStat() {
  234. $textFile = \OC::$SERVERROOT . '/tests/data/lorem.txt';
  235. $ctimeStart = time();
  236. $this->instance->file_put_contents('/lorem.txt', file_get_contents($textFile));
  237. $this->assertTrue($this->instance->isReadable('/lorem.txt'));
  238. $ctimeEnd = time();
  239. $mTime = $this->instance->filemtime('/lorem.txt');
  240. $this->assertTrue($this->instance->hasUpdated('/lorem.txt', $ctimeStart - 5));
  241. $this->assertTrue($this->instance->hasUpdated('/', $ctimeStart - 5));
  242. // check that ($ctimeStart - 5) <= $mTime <= ($ctimeEnd + 1)
  243. $this->assertGreaterThanOrEqual(($ctimeStart - 5), $mTime);
  244. $this->assertLessThanOrEqual(($ctimeEnd + 1), $mTime);
  245. $this->assertEquals(filesize($textFile), $this->instance->filesize('/lorem.txt'));
  246. $stat = $this->instance->stat('/lorem.txt');
  247. //only size and mtime are required in the result
  248. $this->assertEquals($stat['size'], $this->instance->filesize('/lorem.txt'));
  249. $this->assertEquals($stat['mtime'], $mTime);
  250. if ($this->instance->touch('/lorem.txt', 100) !== false) {
  251. $mTime = $this->instance->filemtime('/lorem.txt');
  252. $this->assertEquals($mTime, 100);
  253. }
  254. $mtimeStart = time();
  255. $this->instance->unlink('/lorem.txt');
  256. $this->assertTrue($this->instance->hasUpdated('/', $mtimeStart - 5));
  257. }
  258. public function testUnlink() {
  259. $textFile = \OC::$SERVERROOT . '/tests/data/lorem.txt';
  260. $this->instance->file_put_contents('/lorem.txt', file_get_contents($textFile));
  261. $this->assertTrue($this->instance->file_exists('/lorem.txt'));
  262. $this->assertTrue($this->instance->unlink('/lorem.txt'));
  263. $this->wait();
  264. $this->assertFalse($this->instance->file_exists('/lorem.txt'));
  265. }
  266. public function testFOpen() {
  267. $textFile = \OC::$SERVERROOT . '/tests/data/lorem.txt';
  268. $fh = @$this->instance->fopen('foo', 'r');
  269. if ($fh) {
  270. fclose($fh);
  271. }
  272. $this->assertFalse($fh);
  273. $this->assertFalse($this->instance->file_exists('foo'));
  274. $fh = $this->instance->fopen('foo', 'w');
  275. fwrite($fh, file_get_contents($textFile));
  276. fclose($fh);
  277. $this->assertTrue($this->instance->file_exists('foo'));
  278. $fh = $this->instance->fopen('foo', 'r');
  279. $content = stream_get_contents($fh);
  280. $this->assertEquals(file_get_contents($textFile), $content);
  281. }
  282. public function testTouchCreateFile() {
  283. $this->assertFalse($this->instance->file_exists('touch'));
  284. // returns true on success
  285. $this->assertTrue($this->instance->touch('touch'));
  286. $this->assertTrue($this->instance->file_exists('touch'));
  287. }
  288. public function testRecursiveRmdir() {
  289. $this->instance->mkdir('folder');
  290. $this->instance->mkdir('folder/bar');
  291. $this->wait();
  292. $this->instance->file_put_contents('folder/asd.txt', 'foobar');
  293. $this->instance->file_put_contents('folder/bar/foo.txt', 'asd');
  294. $this->assertTrue($this->instance->rmdir('folder'));
  295. $this->wait();
  296. $this->assertFalse($this->instance->file_exists('folder/asd.txt'));
  297. $this->assertFalse($this->instance->file_exists('folder/bar/foo.txt'));
  298. $this->assertFalse($this->instance->file_exists('folder/bar'));
  299. $this->assertFalse($this->instance->file_exists('folder'));
  300. }
  301. public function testRecursiveUnlink() {
  302. $this->instance->mkdir('folder');
  303. $this->instance->mkdir('folder/bar');
  304. $this->instance->file_put_contents('folder/asd.txt', 'foobar');
  305. $this->instance->file_put_contents('folder/bar/foo.txt', 'asd');
  306. $this->assertTrue($this->instance->unlink('folder'));
  307. $this->wait();
  308. $this->assertFalse($this->instance->file_exists('folder/asd.txt'));
  309. $this->assertFalse($this->instance->file_exists('folder/bar/foo.txt'));
  310. $this->assertFalse($this->instance->file_exists('folder/bar'));
  311. $this->assertFalse($this->instance->file_exists('folder'));
  312. }
  313. public function hashProvider() {
  314. return array(
  315. array('Foobar', 'md5'),
  316. array('Foobar', 'sha1'),
  317. array('Foobar', 'sha256'),
  318. );
  319. }
  320. /**
  321. * @dataProvider hashProvider
  322. */
  323. public function testHash($data, $type) {
  324. $this->instance->file_put_contents('hash.txt', $data);
  325. $this->assertEquals(hash($type, $data), $this->instance->hash($type, 'hash.txt'));
  326. $this->assertEquals(hash($type, $data, true), $this->instance->hash($type, 'hash.txt', true));
  327. }
  328. public function testHashInFileName() {
  329. $this->instance->file_put_contents('#test.txt', 'data');
  330. $this->assertEquals('data', $this->instance->file_get_contents('#test.txt'));
  331. $this->instance->mkdir('#foo');
  332. $this->instance->file_put_contents('#foo/test.txt', 'data');
  333. $this->assertEquals('data', $this->instance->file_get_contents('#foo/test.txt'));
  334. $dh = $this->instance->opendir('#foo');
  335. $content = array();
  336. while ($file = readdir($dh)) {
  337. if ($file != '.' and $file != '..') {
  338. $content[] = $file;
  339. }
  340. }
  341. $this->assertEquals(array('test.txt'), $content);
  342. }
  343. public function testCopyOverWriteFile() {
  344. $this->instance->file_put_contents('target.txt', 'foo');
  345. $this->instance->file_put_contents('source.txt', 'bar');
  346. $this->instance->copy('source.txt', 'target.txt');
  347. $this->assertEquals('bar', $this->instance->file_get_contents('target.txt'));
  348. }
  349. public function testRenameOverWriteFile() {
  350. $this->instance->file_put_contents('target.txt', 'foo');
  351. $this->instance->file_put_contents('source.txt', 'bar');
  352. $this->instance->rename('source.txt', 'target.txt');
  353. $this->assertEquals('bar', $this->instance->file_get_contents('target.txt'));
  354. $this->assertFalse($this->instance->file_exists('source.txt'));
  355. }
  356. public function testRenameDirectory() {
  357. $this->instance->mkdir('source');
  358. $this->instance->file_put_contents('source/test1.txt', 'foo');
  359. $this->instance->file_put_contents('source/test2.txt', 'qwerty');
  360. $this->instance->mkdir('source/subfolder');
  361. $this->instance->file_put_contents('source/subfolder/test.txt', 'bar');
  362. $this->instance->rename('source', 'target');
  363. $this->assertFalse($this->instance->file_exists('source'));
  364. $this->assertFalse($this->instance->file_exists('source/test1.txt'));
  365. $this->assertFalse($this->instance->file_exists('source/test2.txt'));
  366. $this->assertFalse($this->instance->file_exists('source/subfolder'));
  367. $this->assertFalse($this->instance->file_exists('source/subfolder/test.txt'));
  368. $this->assertTrue($this->instance->file_exists('target'));
  369. $this->assertTrue($this->instance->file_exists('target/test1.txt'));
  370. $this->assertTrue($this->instance->file_exists('target/test2.txt'));
  371. $this->assertTrue($this->instance->file_exists('target/subfolder'));
  372. $this->assertTrue($this->instance->file_exists('target/subfolder/test.txt'));
  373. $this->assertEquals('foo', $this->instance->file_get_contents('target/test1.txt'));
  374. $this->assertEquals('qwerty', $this->instance->file_get_contents('target/test2.txt'));
  375. $this->assertEquals('bar', $this->instance->file_get_contents('target/subfolder/test.txt'));
  376. }
  377. public function testRenameOverWriteDirectory() {
  378. $this->instance->mkdir('source');
  379. $this->instance->file_put_contents('source/test1.txt', 'foo');
  380. $this->instance->mkdir('target');
  381. $this->instance->file_put_contents('target/test1.txt', 'bar');
  382. $this->instance->file_put_contents('target/test2.txt', 'bar');
  383. $this->assertTrue($this->instance->rename('source', 'target'), 'rename must return true on success');
  384. $this->assertFalse($this->instance->file_exists('source'), 'source has not been removed');
  385. $this->assertFalse($this->instance->file_exists('source/test1.txt'), 'source/test1.txt has not been removed');
  386. $this->assertFalse($this->instance->file_exists('target/test2.txt'), 'target/test2.txt has not been removed');
  387. $this->assertEquals('foo', $this->instance->file_get_contents('target/test1.txt'), 'target/test1.txt has not been overwritten');
  388. }
  389. public function testRenameOverWriteDirectoryOverFile() {
  390. $this->instance->mkdir('source');
  391. $this->instance->file_put_contents('source/test1.txt', 'foo');
  392. $this->instance->file_put_contents('target', 'bar');
  393. $this->assertTrue($this->instance->rename('source', 'target'), 'rename must return true on success');
  394. $this->assertFalse($this->instance->file_exists('source'));
  395. $this->assertFalse($this->instance->file_exists('source/test1.txt'));
  396. $this->assertEquals('foo', $this->instance->file_get_contents('target/test1.txt'));
  397. }
  398. public function testCopyDirectory() {
  399. $this->instance->mkdir('source');
  400. $this->instance->file_put_contents('source/test1.txt', 'foo');
  401. $this->instance->file_put_contents('source/test2.txt', 'qwerty');
  402. $this->instance->mkdir('source/subfolder');
  403. $this->instance->file_put_contents('source/subfolder/test.txt', 'bar');
  404. $this->instance->copy('source', 'target');
  405. $this->assertTrue($this->instance->file_exists('source'));
  406. $this->assertTrue($this->instance->file_exists('source/test1.txt'));
  407. $this->assertTrue($this->instance->file_exists('source/test2.txt'));
  408. $this->assertTrue($this->instance->file_exists('source/subfolder'));
  409. $this->assertTrue($this->instance->file_exists('source/subfolder/test.txt'));
  410. $this->assertTrue($this->instance->file_exists('target'));
  411. $this->assertTrue($this->instance->file_exists('target/test1.txt'));
  412. $this->assertTrue($this->instance->file_exists('target/test2.txt'));
  413. $this->assertTrue($this->instance->file_exists('target/subfolder'));
  414. $this->assertTrue($this->instance->file_exists('target/subfolder/test.txt'));
  415. $this->assertEquals('foo', $this->instance->file_get_contents('target/test1.txt'));
  416. $this->assertEquals('qwerty', $this->instance->file_get_contents('target/test2.txt'));
  417. $this->assertEquals('bar', $this->instance->file_get_contents('target/subfolder/test.txt'));
  418. }
  419. public function testCopyOverWriteDirectory() {
  420. $this->instance->mkdir('source');
  421. $this->instance->file_put_contents('source/test1.txt', 'foo');
  422. $this->instance->mkdir('target');
  423. $this->instance->file_put_contents('target/test1.txt', 'bar');
  424. $this->instance->file_put_contents('target/test2.txt', 'bar');
  425. $this->instance->copy('source', 'target');
  426. $this->assertFalse($this->instance->file_exists('target/test2.txt'));
  427. $this->assertEquals('foo', $this->instance->file_get_contents('target/test1.txt'));
  428. }
  429. public function testCopyOverWriteDirectoryOverFile() {
  430. $this->instance->mkdir('source');
  431. $this->instance->file_put_contents('source/test1.txt', 'foo');
  432. $this->instance->file_put_contents('target', 'bar');
  433. $this->instance->copy('source', 'target');
  434. $this->assertEquals('foo', $this->instance->file_get_contents('target/test1.txt'));
  435. }
  436. public function testInstanceOfStorage() {
  437. $this->assertTrue($this->instance->instanceOfStorage('\OCP\Files\Storage'));
  438. $this->assertTrue($this->instance->instanceOfStorage(get_class($this->instance)));
  439. $this->assertFalse($this->instance->instanceOfStorage('\OC'));
  440. }
  441. }