Storage.php 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682
  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. use OC\Files\Cache\Watcher;
  24. use OCP\Files\Storage\IWriteStreamStorage;
  25. abstract class Storage extends \Test\TestCase {
  26. /**
  27. * @var \OC\Files\Storage\Storage instance
  28. */
  29. protected $instance;
  30. protected $waitDelay = 0;
  31. /**
  32. * Sleep for the number of seconds specified in the
  33. * $waitDelay attribute
  34. */
  35. protected function wait() {
  36. if ($this->waitDelay > 0) {
  37. sleep($this->waitDelay);
  38. }
  39. }
  40. /**
  41. * the root folder of the storage should always exist, be readable and be recognized as a directory
  42. */
  43. public function testRoot() {
  44. $this->assertTrue($this->instance->file_exists('/'), 'Root folder does not exist');
  45. $this->assertTrue($this->instance->isReadable('/'), 'Root folder is not readable');
  46. $this->assertTrue($this->instance->is_dir('/'), 'Root folder is not a directory');
  47. $this->assertFalse($this->instance->is_file('/'), 'Root folder is a file');
  48. $this->assertEquals('dir', $this->instance->filetype('/'));
  49. //without this, any further testing would be useless, not an actual requirement for filestorage though
  50. $this->assertTrue($this->instance->isUpdatable('/'), 'Root folder is not writable');
  51. }
  52. /**
  53. * Check that the test() function works
  54. */
  55. public function testTestFunction() {
  56. $this->assertTrue($this->instance->test());
  57. }
  58. /**
  59. * @dataProvider directoryProvider
  60. */
  61. public function testDirectories($directory) {
  62. $this->assertFalse($this->instance->file_exists('/' . $directory));
  63. $this->assertTrue($this->instance->mkdir('/' . $directory));
  64. $this->assertTrue($this->instance->file_exists('/' . $directory));
  65. $this->assertTrue($this->instance->is_dir('/' . $directory));
  66. $this->assertFalse($this->instance->is_file('/' . $directory));
  67. $this->assertEquals('dir', $this->instance->filetype('/' . $directory));
  68. $this->assertEquals(0, $this->instance->filesize('/' . $directory));
  69. $this->assertTrue($this->instance->isReadable('/' . $directory));
  70. $this->assertTrue($this->instance->isUpdatable('/' . $directory));
  71. $dh = $this->instance->opendir('/');
  72. $content = [];
  73. while ($file = readdir($dh)) {
  74. if ($file != '.' and $file != '..') {
  75. $content[] = $file;
  76. }
  77. }
  78. $this->assertEquals([$directory], $content);
  79. $content = iterator_to_array($this->instance->getDirectoryContent('/'));
  80. $this->assertCount(1, $content);
  81. $dirEntry = $content[0];
  82. unset($dirEntry['scan_permissions']);
  83. unset($dirEntry['etag']);
  84. $this->assertLessThanOrEqual(1, abs($dirEntry['mtime'] - $this->instance->filemtime($directory)));
  85. unset($dirEntry['mtime']);
  86. unset($dirEntry['storage_mtime']);
  87. $this->assertEquals([
  88. 'name' => $directory,
  89. 'mimetype' => $this->instance->getMimeType($directory),
  90. 'size' => -1,
  91. 'permissions' => $this->instance->getPermissions($directory),
  92. ], $dirEntry);
  93. $this->assertFalse($this->instance->mkdir('/' . $directory)); //can't create existing folders
  94. $this->assertTrue($this->instance->rmdir('/' . $directory));
  95. $this->wait();
  96. $this->assertFalse($this->instance->file_exists('/' . $directory));
  97. $this->assertFalse($this->instance->rmdir('/' . $directory)); //can't remove non existing folders
  98. $dh = $this->instance->opendir('/');
  99. $content = [];
  100. while ($file = readdir($dh)) {
  101. if ($file != '.' and $file != '..') {
  102. $content[] = $file;
  103. }
  104. }
  105. $this->assertEquals([], $content);
  106. }
  107. public function fileNameProvider() {
  108. return [
  109. ['file.txt'],
  110. [' file.txt'],
  111. ['folder .txt'],
  112. ['file with space.txt'],
  113. ['spéciäl fäile'],
  114. ['test single\'quote.txt'],
  115. ];
  116. }
  117. public function directoryProvider() {
  118. return [
  119. ['folder'],
  120. [' folder'],
  121. ['folder '],
  122. ['folder with space'],
  123. ['spéciäl földer'],
  124. ['test single\'quote'],
  125. ];
  126. }
  127. public function loremFileProvider() {
  128. $root = \OC::$SERVERROOT . '/tests/data/';
  129. return [
  130. // small file
  131. [$root . 'lorem.txt'],
  132. // bigger file (> 8 KB which is the standard PHP block size)
  133. [$root . 'lorem-big.txt']
  134. ];
  135. }
  136. /**
  137. * test the various uses of file_get_contents and file_put_contents
  138. *
  139. * @dataProvider loremFileProvider
  140. */
  141. public function testGetPutContents($sourceFile) {
  142. $sourceText = file_get_contents($sourceFile);
  143. //fill a file with string data
  144. $this->instance->file_put_contents('/lorem.txt', $sourceText);
  145. $this->assertFalse($this->instance->is_dir('/lorem.txt'));
  146. $this->assertEquals($sourceText, $this->instance->file_get_contents('/lorem.txt'), 'data returned from file_get_contents is not equal to the source data');
  147. //empty the file
  148. $this->instance->file_put_contents('/lorem.txt', '');
  149. $this->assertEquals('', $this->instance->file_get_contents('/lorem.txt'), 'file not emptied');
  150. }
  151. /**
  152. * test various known mimetypes
  153. */
  154. public function testMimeType() {
  155. $this->assertEquals('httpd/unix-directory', $this->instance->getMimeType('/'));
  156. $this->assertEquals(false, $this->instance->getMimeType('/non/existing/file'));
  157. $textFile = \OC::$SERVERROOT . '/tests/data/lorem.txt';
  158. $this->instance->file_put_contents('/lorem.txt', file_get_contents($textFile, 'r'));
  159. $this->assertEquals('text/plain', $this->instance->getMimeType('/lorem.txt'));
  160. $pngFile = \OC::$SERVERROOT . '/tests/data/desktopapp.png';
  161. $this->instance->file_put_contents('/desktopapp.png', file_get_contents($pngFile, 'r'));
  162. $this->assertEquals('image/png', $this->instance->getMimeType('/desktopapp.png'));
  163. $svgFile = \OC::$SERVERROOT . '/tests/data/desktopapp.svg';
  164. $this->instance->file_put_contents('/desktopapp.svg', file_get_contents($svgFile, 'r'));
  165. $this->assertEquals('image/svg+xml', $this->instance->getMimeType('/desktopapp.svg'));
  166. }
  167. public function copyAndMoveProvider() {
  168. return [
  169. ['/source.txt', '/target.txt'],
  170. ['/source.txt', '/target with space.txt'],
  171. ['/source with space.txt', '/target.txt'],
  172. ['/source with space.txt', '/target with space.txt'],
  173. ['/source.txt', '/tärgét.txt'],
  174. ['/sòurcē.txt', '/target.txt'],
  175. ['/sòurcē.txt', '/tärgét.txt'],
  176. ['/single \' quote.txt', '/tar\'get.txt'],
  177. ];
  178. }
  179. public function initSourceAndTarget($source, $target = null) {
  180. $textFile = \OC::$SERVERROOT . '/tests/data/lorem.txt';
  181. $this->instance->file_put_contents($source, file_get_contents($textFile));
  182. if ($target) {
  183. $testContents = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
  184. $this->instance->file_put_contents($target, $testContents);
  185. }
  186. }
  187. public function assertSameAsLorem($file) {
  188. $textFile = \OC::$SERVERROOT . '/tests/data/lorem.txt';
  189. $this->assertEquals(
  190. file_get_contents($textFile),
  191. $this->instance->file_get_contents($file),
  192. 'Expected ' . $file . ' to be a copy of ' . $textFile
  193. );
  194. }
  195. /**
  196. * @dataProvider copyAndMoveProvider
  197. */
  198. public function testCopy($source, $target) {
  199. $this->initSourceAndTarget($source);
  200. $this->instance->copy($source, $target);
  201. $this->assertTrue($this->instance->file_exists($target), $target . ' was not created');
  202. $this->assertSameAsLorem($target);
  203. $this->assertTrue($this->instance->file_exists($source), $source . ' was deleted');
  204. }
  205. /**
  206. * @dataProvider copyAndMoveProvider
  207. */
  208. public function testMove($source, $target) {
  209. $this->initSourceAndTarget($source);
  210. $this->instance->rename($source, $target);
  211. $this->wait();
  212. $this->assertTrue($this->instance->file_exists($target), $target . ' was not created');
  213. $this->assertFalse($this->instance->file_exists($source), $source . ' still exists');
  214. $this->assertSameAsLorem($target);
  215. }
  216. /**
  217. * @dataProvider copyAndMoveProvider
  218. */
  219. public function testCopyOverwrite($source, $target) {
  220. $this->initSourceAndTarget($source, $target);
  221. $this->instance->copy($source, $target);
  222. $this->assertTrue($this->instance->file_exists($target), $target . ' was not created');
  223. $this->assertTrue($this->instance->file_exists($source), $source . ' was deleted');
  224. $this->assertSameAsLorem($target);
  225. $this->assertSameAsLorem($source);
  226. }
  227. /**
  228. * @dataProvider copyAndMoveProvider
  229. */
  230. public function testMoveOverwrite($source, $target) {
  231. $this->initSourceAndTarget($source, $target);
  232. $this->instance->rename($source, $target);
  233. $this->assertTrue($this->instance->file_exists($target), $target . ' was not created');
  234. $this->assertFalse($this->instance->file_exists($source), $source . ' still exists');
  235. $this->assertSameAsLorem($target);
  236. }
  237. public function testLocal() {
  238. $textFile = \OC::$SERVERROOT . '/tests/data/lorem.txt';
  239. $this->instance->file_put_contents('/lorem.txt', file_get_contents($textFile));
  240. $localFile = $this->instance->getLocalFile('/lorem.txt');
  241. $this->assertTrue(file_exists($localFile));
  242. $this->assertEquals(file_get_contents($textFile), file_get_contents($localFile));
  243. $this->instance->mkdir('/folder');
  244. $this->instance->file_put_contents('/folder/lorem.txt', file_get_contents($textFile));
  245. $this->instance->file_put_contents('/folder/bar.txt', 'asd');
  246. $this->instance->mkdir('/folder/recursive');
  247. $this->instance->file_put_contents('/folder/recursive/file.txt', 'foo');
  248. // test below require to use instance->getLocalFile because the physical storage might be different
  249. $localFile = $this->instance->getLocalFile('/folder/lorem.txt');
  250. $this->assertTrue(file_exists($localFile));
  251. $this->assertEquals(file_get_contents($localFile), file_get_contents($textFile));
  252. $localFile = $this->instance->getLocalFile('/folder/bar.txt');
  253. $this->assertTrue(file_exists($localFile));
  254. $this->assertEquals(file_get_contents($localFile), 'asd');
  255. $localFile = $this->instance->getLocalFile('/folder/recursive/file.txt');
  256. $this->assertTrue(file_exists($localFile));
  257. $this->assertEquals(file_get_contents($localFile), 'foo');
  258. }
  259. public function testStat() {
  260. $textFile = \OC::$SERVERROOT . '/tests/data/lorem.txt';
  261. $ctimeStart = time();
  262. $this->instance->file_put_contents('/lorem.txt', file_get_contents($textFile));
  263. $this->assertTrue($this->instance->isReadable('/lorem.txt'));
  264. $ctimeEnd = time();
  265. $mTime = $this->instance->filemtime('/lorem.txt');
  266. $this->assertTrue($this->instance->hasUpdated('/lorem.txt', $ctimeStart - 5));
  267. $this->assertTrue($this->instance->hasUpdated('/', $ctimeStart - 5));
  268. // check that ($ctimeStart - 5) <= $mTime <= ($ctimeEnd + 1)
  269. $this->assertGreaterThanOrEqual(($ctimeStart - 5), $mTime);
  270. $this->assertLessThanOrEqual(($ctimeEnd + 1), $mTime);
  271. $this->assertEquals(filesize($textFile), $this->instance->filesize('/lorem.txt'));
  272. $stat = $this->instance->stat('/lorem.txt');
  273. //only size and mtime are required in the result
  274. $this->assertEquals($stat['size'], $this->instance->filesize('/lorem.txt'));
  275. $this->assertEquals($stat['mtime'], $mTime);
  276. if ($this->instance->touch('/lorem.txt', 100) !== false) {
  277. $mTime = $this->instance->filemtime('/lorem.txt');
  278. $this->assertEquals($mTime, 100);
  279. }
  280. $mtimeStart = time();
  281. $this->instance->unlink('/lorem.txt');
  282. $this->assertTrue($this->instance->hasUpdated('/', $mtimeStart - 5));
  283. }
  284. /**
  285. * Test whether checkUpdate properly returns false when there was
  286. * no change.
  287. */
  288. public function testCheckUpdate() {
  289. if ($this->instance instanceof \OC\Files\Storage\Wrapper\Wrapper) {
  290. $this->markTestSkipped('Cannot test update check on wrappers');
  291. }
  292. $textFile = \OC::$SERVERROOT . '/tests/data/lorem.txt';
  293. $watcher = $this->instance->getWatcher();
  294. $watcher->setPolicy(Watcher::CHECK_ALWAYS);
  295. $this->instance->file_put_contents('/lorem.txt', file_get_contents($textFile));
  296. $this->assertTrue($watcher->checkUpdate('/lorem.txt'), 'Update detected');
  297. $this->assertFalse($watcher->checkUpdate('/lorem.txt'), 'No update');
  298. }
  299. public function testUnlink() {
  300. $textFile = \OC::$SERVERROOT . '/tests/data/lorem.txt';
  301. $this->instance->file_put_contents('/lorem.txt', file_get_contents($textFile));
  302. $this->assertTrue($this->instance->file_exists('/lorem.txt'));
  303. $this->assertTrue($this->instance->unlink('/lorem.txt'));
  304. $this->wait();
  305. $this->assertFalse($this->instance->file_exists('/lorem.txt'));
  306. }
  307. /**
  308. * @dataProvider fileNameProvider
  309. */
  310. public function testFOpen($fileName) {
  311. $textFile = \OC::$SERVERROOT . '/tests/data/lorem.txt';
  312. $fh = @$this->instance->fopen($fileName, 'r');
  313. if ($fh) {
  314. fclose($fh);
  315. }
  316. $this->assertFalse($fh);
  317. $this->assertFalse($this->instance->file_exists($fileName));
  318. $fh = $this->instance->fopen($fileName, 'w');
  319. fwrite($fh, file_get_contents($textFile));
  320. fclose($fh);
  321. $this->assertTrue($this->instance->file_exists($fileName));
  322. $fh = $this->instance->fopen($fileName, 'r');
  323. $content = stream_get_contents($fh);
  324. $this->assertEquals(file_get_contents($textFile), $content);
  325. }
  326. public function testTouchCreateFile() {
  327. $this->assertFalse($this->instance->file_exists('touch'));
  328. // returns true on success
  329. $this->assertTrue($this->instance->touch('touch'));
  330. $this->assertTrue($this->instance->file_exists('touch'));
  331. }
  332. public function testRecursiveRmdir() {
  333. $this->instance->mkdir('folder');
  334. $this->instance->mkdir('folder/bar');
  335. $this->wait();
  336. $this->instance->file_put_contents('folder/asd.txt', 'foobar');
  337. $this->instance->file_put_contents('folder/bar/foo.txt', 'asd');
  338. $this->assertTrue($this->instance->rmdir('folder'));
  339. $this->wait();
  340. $this->assertFalse($this->instance->file_exists('folder/asd.txt'));
  341. $this->assertFalse($this->instance->file_exists('folder/bar/foo.txt'));
  342. $this->assertFalse($this->instance->file_exists('folder/bar'));
  343. $this->assertFalse($this->instance->file_exists('folder'));
  344. }
  345. public function testRmdirEmptyFolder() {
  346. $this->assertTrue($this->instance->mkdir('empty'));
  347. $this->wait();
  348. $this->assertTrue($this->instance->rmdir('empty'));
  349. $this->assertFalse($this->instance->file_exists('empty'));
  350. }
  351. public function testRecursiveUnlink() {
  352. $this->instance->mkdir('folder');
  353. $this->instance->mkdir('folder/bar');
  354. $this->instance->file_put_contents('folder/asd.txt', 'foobar');
  355. $this->instance->file_put_contents('folder/bar/foo.txt', 'asd');
  356. $this->assertTrue($this->instance->unlink('folder'));
  357. $this->wait();
  358. $this->assertFalse($this->instance->file_exists('folder/asd.txt'));
  359. $this->assertFalse($this->instance->file_exists('folder/bar/foo.txt'));
  360. $this->assertFalse($this->instance->file_exists('folder/bar'));
  361. $this->assertFalse($this->instance->file_exists('folder'));
  362. }
  363. public function hashProvider() {
  364. return [
  365. ['Foobar', 'md5'],
  366. ['Foobar', 'sha1'],
  367. ['Foobar', 'sha256'],
  368. ];
  369. }
  370. /**
  371. * @dataProvider hashProvider
  372. */
  373. public function testHash($data, $type) {
  374. $this->instance->file_put_contents('hash.txt', $data);
  375. $this->assertEquals(hash($type, $data), $this->instance->hash($type, 'hash.txt'));
  376. $this->assertEquals(hash($type, $data, true), $this->instance->hash($type, 'hash.txt', true));
  377. }
  378. public function testHashInFileName() {
  379. $this->instance->file_put_contents('#test.txt', 'data');
  380. $this->assertEquals('data', $this->instance->file_get_contents('#test.txt'));
  381. $this->instance->mkdir('#foo');
  382. $this->instance->file_put_contents('#foo/test.txt', 'data');
  383. $this->assertEquals('data', $this->instance->file_get_contents('#foo/test.txt'));
  384. $dh = $this->instance->opendir('#foo');
  385. $content = [];
  386. while ($file = readdir($dh)) {
  387. if ($file != '.' and $file != '..') {
  388. $content[] = $file;
  389. }
  390. }
  391. $this->assertEquals(['test.txt'], $content);
  392. }
  393. public function testCopyOverWriteFile() {
  394. $this->instance->file_put_contents('target.txt', 'foo');
  395. $this->instance->file_put_contents('source.txt', 'bar');
  396. $this->instance->copy('source.txt', 'target.txt');
  397. $this->assertEquals('bar', $this->instance->file_get_contents('target.txt'));
  398. }
  399. public function testRenameOverWriteFile() {
  400. $this->instance->file_put_contents('target.txt', 'foo');
  401. $this->instance->file_put_contents('source.txt', 'bar');
  402. $this->instance->rename('source.txt', 'target.txt');
  403. $this->assertEquals('bar', $this->instance->file_get_contents('target.txt'));
  404. $this->assertFalse($this->instance->file_exists('source.txt'));
  405. }
  406. public function testRenameDirectory() {
  407. $this->instance->mkdir('source');
  408. $this->instance->file_put_contents('source/test1.txt', 'foo');
  409. $this->instance->file_put_contents('source/test2.txt', 'qwerty');
  410. $this->instance->mkdir('source/subfolder');
  411. $this->instance->file_put_contents('source/subfolder/test.txt', 'bar');
  412. $this->instance->rename('source', 'target');
  413. $this->assertFalse($this->instance->file_exists('source'));
  414. $this->assertFalse($this->instance->file_exists('source/test1.txt'));
  415. $this->assertFalse($this->instance->file_exists('source/test2.txt'));
  416. $this->assertFalse($this->instance->file_exists('source/subfolder'));
  417. $this->assertFalse($this->instance->file_exists('source/subfolder/test.txt'));
  418. $this->assertTrue($this->instance->file_exists('target'));
  419. $this->assertTrue($this->instance->file_exists('target/test1.txt'));
  420. $this->assertTrue($this->instance->file_exists('target/test2.txt'));
  421. $this->assertTrue($this->instance->file_exists('target/subfolder'));
  422. $this->assertTrue($this->instance->file_exists('target/subfolder/test.txt'));
  423. $contents = iterator_to_array($this->instance->getDirectoryContent(''));
  424. $this->assertCount(1, $contents);
  425. $this->assertEquals('foo', $this->instance->file_get_contents('target/test1.txt'));
  426. $this->assertEquals('qwerty', $this->instance->file_get_contents('target/test2.txt'));
  427. $this->assertEquals('bar', $this->instance->file_get_contents('target/subfolder/test.txt'));
  428. }
  429. public function testRenameOverWriteDirectory() {
  430. $this->instance->mkdir('source');
  431. $this->instance->file_put_contents('source/test1.txt', 'foo');
  432. $this->instance->mkdir('target');
  433. $this->instance->file_put_contents('target/test1.txt', 'bar');
  434. $this->instance->file_put_contents('target/test2.txt', 'bar');
  435. $this->assertTrue($this->instance->rename('source', 'target'), 'rename must return true on success');
  436. $this->assertFalse($this->instance->file_exists('source'), 'source has not been removed');
  437. $this->assertFalse($this->instance->file_exists('source/test1.txt'), 'source/test1.txt has not been removed');
  438. $this->assertFalse($this->instance->file_exists('target/test2.txt'), 'target/test2.txt has not been removed');
  439. $this->assertEquals('foo', $this->instance->file_get_contents('target/test1.txt'), 'target/test1.txt has not been overwritten');
  440. }
  441. public function testRenameOverWriteDirectoryOverFile() {
  442. $this->instance->mkdir('source');
  443. $this->instance->file_put_contents('source/test1.txt', 'foo');
  444. $this->instance->file_put_contents('target', 'bar');
  445. $this->assertTrue($this->instance->rename('source', 'target'), 'rename must return true on success');
  446. $this->assertFalse($this->instance->file_exists('source'));
  447. $this->assertFalse($this->instance->file_exists('source/test1.txt'));
  448. $this->assertEquals('foo', $this->instance->file_get_contents('target/test1.txt'));
  449. }
  450. public function testCopyDirectory() {
  451. $this->instance->mkdir('source');
  452. $this->instance->file_put_contents('source/test1.txt', 'foo');
  453. $this->instance->file_put_contents('source/test2.txt', 'qwerty');
  454. $this->instance->mkdir('source/subfolder');
  455. $this->instance->file_put_contents('source/subfolder/test.txt', 'bar');
  456. $this->instance->copy('source', 'target');
  457. $this->assertTrue($this->instance->file_exists('source'));
  458. $this->assertTrue($this->instance->file_exists('source/test1.txt'));
  459. $this->assertTrue($this->instance->file_exists('source/test2.txt'));
  460. $this->assertTrue($this->instance->file_exists('source/subfolder'));
  461. $this->assertTrue($this->instance->file_exists('source/subfolder/test.txt'));
  462. $this->assertTrue($this->instance->file_exists('target'));
  463. $this->assertTrue($this->instance->file_exists('target/test1.txt'));
  464. $this->assertTrue($this->instance->file_exists('target/test2.txt'));
  465. $this->assertTrue($this->instance->file_exists('target/subfolder'));
  466. $this->assertTrue($this->instance->file_exists('target/subfolder/test.txt'));
  467. $this->assertEquals('foo', $this->instance->file_get_contents('target/test1.txt'));
  468. $this->assertEquals('qwerty', $this->instance->file_get_contents('target/test2.txt'));
  469. $this->assertEquals('bar', $this->instance->file_get_contents('target/subfolder/test.txt'));
  470. }
  471. public function testCopyOverWriteDirectory() {
  472. $this->instance->mkdir('source');
  473. $this->instance->file_put_contents('source/test1.txt', 'foo');
  474. $this->instance->mkdir('target');
  475. $this->instance->file_put_contents('target/test1.txt', 'bar');
  476. $this->instance->file_put_contents('target/test2.txt', 'bar');
  477. $this->instance->copy('source', 'target');
  478. $this->assertFalse($this->instance->file_exists('target/test2.txt'));
  479. $this->assertEquals('foo', $this->instance->file_get_contents('target/test1.txt'));
  480. }
  481. public function testCopyOverWriteDirectoryOverFile() {
  482. $this->instance->mkdir('source');
  483. $this->instance->file_put_contents('source/test1.txt', 'foo');
  484. $this->instance->file_put_contents('target', 'bar');
  485. $this->instance->copy('source', 'target');
  486. $this->assertEquals('foo', $this->instance->file_get_contents('target/test1.txt'));
  487. }
  488. public function testInstanceOfStorage() {
  489. $this->assertTrue($this->instance->instanceOfStorage('\OCP\Files\Storage'));
  490. $this->assertTrue($this->instance->instanceOfStorage(get_class($this->instance)));
  491. $this->assertFalse($this->instance->instanceOfStorage('\OC'));
  492. }
  493. /**
  494. * @dataProvider copyAndMoveProvider
  495. */
  496. public function testCopyFromSameStorage($source, $target) {
  497. $this->initSourceAndTarget($source);
  498. $this->instance->copyFromStorage($this->instance, $source, $target);
  499. $this->assertTrue($this->instance->file_exists($target), $target . ' was not created');
  500. $this->assertSameAsLorem($target);
  501. $this->assertTrue($this->instance->file_exists($source), $source . ' was deleted');
  502. }
  503. public function testIsCreatable() {
  504. $this->instance->mkdir('source');
  505. $this->assertTrue($this->instance->isCreatable('source'));
  506. }
  507. public function testIsReadable() {
  508. $this->instance->mkdir('source');
  509. $this->assertTrue($this->instance->isReadable('source'));
  510. }
  511. public function testIsUpdatable() {
  512. $this->instance->mkdir('source');
  513. $this->assertTrue($this->instance->isUpdatable('source'));
  514. }
  515. public function testIsDeletable() {
  516. $this->instance->mkdir('source');
  517. $this->assertTrue($this->instance->isDeletable('source'));
  518. }
  519. public function testIsShareable() {
  520. $this->instance->mkdir('source');
  521. $this->assertTrue($this->instance->isSharable('source'));
  522. }
  523. public function testStatAfterWrite() {
  524. $this->instance->file_put_contents('foo.txt', 'bar');
  525. $stat = $this->instance->stat('foo.txt');
  526. $this->assertEquals(3, $stat['size']);
  527. $fh = $this->instance->fopen('foo.txt', 'w');
  528. fwrite($fh, 'qwerty');
  529. fclose($fh);
  530. $stat = $this->instance->stat('foo.txt');
  531. $this->assertEquals(6, $stat['size']);
  532. }
  533. public function testPartFile() {
  534. $this->instance->file_put_contents('bar.txt.part', 'bar');
  535. $this->instance->rename('bar.txt.part', 'bar.txt');
  536. $this->assertEquals('bar', $this->instance->file_get_contents('bar.txt'));
  537. }
  538. public function testWriteStream() {
  539. $textFile = \OC::$SERVERROOT . '/tests/data/lorem.txt';
  540. if (!$this->instance->instanceOfStorage(IWriteStreamStorage::class)) {
  541. $this->markTestSkipped('Not a WriteSteamStorage');
  542. }
  543. /** @var IWriteStreamStorage $storage */
  544. $storage = $this->instance;
  545. $source = fopen($textFile, 'r');
  546. $storage->writeStream('test.txt', $source);
  547. $this->assertTrue($storage->file_exists('test.txt'));
  548. $this->assertStringEqualsFile($textFile, $storage->file_get_contents('test.txt'));
  549. $this->assertEquals('resource (closed)', gettype($source));
  550. }
  551. public function testFseekSize() {
  552. $textFile = \OC::$SERVERROOT . '/tests/data/lorem.txt';
  553. $this->instance->file_put_contents('bar.txt', file_get_contents($textFile));
  554. $size = $this->instance->filesize('bar.txt');
  555. $this->assertEquals(filesize($textFile), $size);
  556. $fh = $this->instance->fopen('bar.txt', 'r');
  557. fseek($fh, 0, SEEK_END);
  558. $pos = ftell($fh);
  559. $this->assertEquals($size, $pos);
  560. }
  561. }