PageRenderTime 59ms CodeModel.GetById 31ms RepoModel.GetById 0ms app.codeStats 0ms

/tests/PHPFrame/Utils/FilesystemTest.php

https://github.com/chrismcband/PHPFrame
PHP | 701 lines | 513 code | 136 blank | 52 comment | 22 complexity | 52925106807f40c9b02bb91fb01c090e MD5 | raw file
Possible License(s): BSD-3-Clause, LGPL-2.1
  1. <?php
  2. // Include framework if not inculded yet
  3. require_once preg_replace("/tests\/.*/", "src/PHPFrame.php", __FILE__);
  4. class PHPFrame_FilesystemTest extends PHPUnit_Framework_TestCase
  5. {
  6. private $_sys_tmp_dir;
  7. public function setUp()
  8. {
  9. $this->_sys_tmp_dir = PHPFrame_Filesystem::getSystemTempDir();
  10. }
  11. public function tearDown()
  12. {
  13. //...
  14. }
  15. public function test_cpFileToDir()
  16. {
  17. $dir = $this->_sys_tmp_dir.DS."test-dir";
  18. $file = $this->_sys_tmp_dir.DS."file1.txt";
  19. if (is_dir($dir)) {
  20. PHPFrame_Filesystem::rm($dir, true);
  21. }
  22. if (is_file($file)) {
  23. unlink($file);
  24. }
  25. if (!is_dir($dir)) mkdir($dir);
  26. touch($file);
  27. $this->assertTrue(is_dir($dir));
  28. $this->assertTrue(is_file($file));
  29. PHPFrame_Filesystem::cp($file, $dir);
  30. $this->assertTrue(is_file($dir.DS."file1.txt"));
  31. unlink($dir.DS."file1.txt");
  32. rmdir($dir);
  33. unlink($file);
  34. }
  35. public function test_cpFileToFile()
  36. {
  37. $source = $this->_sys_tmp_dir.DS."file1.txt";
  38. $dest = $this->_sys_tmp_dir.DS."file2.txt";
  39. touch($source);
  40. $this->assertTrue(is_file($source));
  41. PHPFrame_Filesystem::cp($source, $dest);
  42. $this->assertTrue(is_file($dest));
  43. unlink($source);
  44. unlink($dest);
  45. }
  46. public function test_cpDirIntoExisitingDir()
  47. {
  48. $source = $this->_sys_tmp_dir.DS."test-dir";
  49. $subdir = $source.DS."subdir";
  50. $dest = $this->_sys_tmp_dir.DS."test-dir2";
  51. if (!is_dir($source)) mkdir($source);
  52. if (!is_dir($subdir)) mkdir($subdir);
  53. if (!is_dir($dest)) mkdir($dest);
  54. $file1 = $source.DS."file1.txt";
  55. $file2 = $source.DS."file2.txt";
  56. $file3 = $source.DS."file3.txt";
  57. $file4 = $subdir.DS."file4.txt";
  58. $file5 = $subdir.DS."file5.txt";
  59. touch($file1);
  60. touch($file2);
  61. touch($file3);
  62. touch($file4);
  63. touch($file5);
  64. $this->assertTrue(is_dir($source));
  65. $this->assertTrue(is_dir($subdir));
  66. $this->assertTrue(is_dir($dest));
  67. $this->assertTrue(is_file($file1));
  68. $this->assertTrue(is_file($file2));
  69. $this->assertTrue(is_file($file3));
  70. $this->assertTrue(is_file($file4));
  71. $this->assertTrue(is_file($file5));
  72. PHPFrame_Filesystem::cp($source, $dest, true);
  73. $this->assertTrue(is_dir($dest.DS."test-dir".DS."subdir"));
  74. $this->assertTrue(is_file($dest.DS."test-dir".DS."file1.txt"));
  75. $this->assertTrue(is_file($dest.DS."test-dir".DS."file2.txt"));
  76. $this->assertTrue(is_file($dest.DS."test-dir".DS."file3.txt"));
  77. $this->assertTrue(is_file($dest.DS."test-dir".DS."subdir".DS."file4.txt"));
  78. $this->assertTrue(is_file($dest.DS."test-dir".DS."subdir".DS."file5.txt"));
  79. // Cleanup
  80. PHPFrame_Filesystem::rm($source, true);
  81. PHPFrame_Filesystem::rm($dest, true);
  82. }
  83. public function test_cpDirContentsToDir()
  84. {
  85. $source = $this->_sys_tmp_dir.DS."test-dir";
  86. $subdir = $source.DS."subdir";
  87. $dest = $this->_sys_tmp_dir.DS."test-dir2";
  88. if (!is_dir($source)) mkdir($source);
  89. if (!is_dir($subdir)) mkdir($subdir);
  90. if (!is_dir($dest)) mkdir($dest);
  91. $file1 = $source.DS."file1.txt";
  92. $file2 = $source.DS."file2.txt";
  93. $file3 = $source.DS."file3.txt";
  94. $file4 = $subdir.DS."file4.txt";
  95. $file5 = $subdir.DS."file5.txt";
  96. touch($file1);
  97. touch($file2);
  98. touch($file3);
  99. touch($file4);
  100. touch($file5);
  101. $this->assertTrue(is_dir($source));
  102. $this->assertTrue(is_dir($subdir));
  103. $this->assertTrue(is_dir($dest));
  104. $this->assertTrue(is_file($file1));
  105. $this->assertTrue(is_file($file2));
  106. $this->assertTrue(is_file($file3));
  107. $this->assertTrue(is_file($file4));
  108. $this->assertTrue(is_file($file5));
  109. PHPFrame_Filesystem::cp($source.DS, $dest, true);
  110. $this->assertTrue(is_dir($dest.DS."subdir"));
  111. $this->assertTrue(is_file($dest.DS."file1.txt"));
  112. $this->assertTrue(is_file($dest.DS."file2.txt"));
  113. $this->assertTrue(is_file($dest.DS."file3.txt"));
  114. $this->assertTrue(is_file($dest.DS."subdir".DS."file4.txt"));
  115. $this->assertTrue(is_file($dest.DS."subdir".DS."file5.txt"));
  116. // Cleanup
  117. PHPFrame_Filesystem::rm($source, true);
  118. PHPFrame_Filesystem::rm($dest, true);
  119. }
  120. public function test_cpDirToNewDir()
  121. {
  122. }
  123. public function test_cpSourceNotReadableFailure()
  124. {
  125. $source = "/i/do/not/exist";
  126. $dest = $this->_sys_tmp_dir.DS."test-dir";
  127. $this->setExpectedException("RuntimeException");
  128. PHPFrame_Filesystem::cp($source, $dest, true);
  129. }
  130. public function test_cpDestinationNotWriteableFailure()
  131. {
  132. $source = $this->_sys_tmp_dir;
  133. $dest = "/i/do/not/exist";
  134. $this->setExpectedException("RuntimeException");
  135. PHPFrame_Filesystem::cp($source, $dest, true);
  136. }
  137. public function test_cpDirectoryNonRecursiveFailure()
  138. {
  139. $source = $this->_sys_tmp_dir;
  140. $dest = $this->_sys_tmp_dir.DS."test-dir";
  141. $this->setExpectedException("LogicException");
  142. PHPFrame_Filesystem::cp($source, $dest);
  143. }
  144. public function test_rm()
  145. {
  146. $file = $this->_sys_tmp_dir.DS."file1.txt";
  147. touch($file);
  148. $this->assertTrue(is_file($file));
  149. PHPFrame_Filesystem::rm($file);
  150. $this->assertFalse(is_file($file));
  151. }
  152. public function test_rmRecursive()
  153. {
  154. $test_dir = $this->_sys_tmp_dir.DS."test-dir";
  155. $file1 = $test_dir.DS."file1.txt";
  156. $file2 = $test_dir.DS."file2.txt";
  157. $file3 = $test_dir.DS."file3.txt";
  158. $subdir = $test_dir.DS."subdir";
  159. $file4 = $subdir.DS."file4.txt";
  160. $file5 = $subdir.DS."file5.txt";
  161. if (!is_dir($test_dir)) mkdir($test_dir);
  162. touch($file1);
  163. touch($file2);
  164. touch($file3);
  165. if (!is_dir($subdir)) mkdir($subdir);
  166. touch($file4);
  167. touch($file5);
  168. $this->assertTrue(is_dir($test_dir));
  169. $this->assertTrue(is_file($file1));
  170. $this->assertTrue(is_file($file2));
  171. $this->assertTrue(is_file($file3));
  172. $this->assertTrue(is_dir($subdir));
  173. $this->assertTrue(is_file($file4));
  174. $this->assertTrue(is_file($file5));
  175. PHPFrame_Filesystem::rm($test_dir, true);
  176. $this->assertFalse(is_dir($test_dir));
  177. $this->assertFalse(is_file($file1));
  178. $this->assertFalse(is_file($file2));
  179. $this->assertFalse(is_file($file3));
  180. $this->assertFalse(is_dir($subdir));
  181. $this->assertFalse(is_file($file4));
  182. $this->assertFalse(is_file($file5));
  183. }
  184. public function test_ensureWritableDir()
  185. {
  186. $dir = $this->_sys_tmp_dir.DS."test-dir";
  187. $subdir = $dir.DS."subdir";
  188. PHPFrame_Filesystem::ensureWritableDir($subdir);
  189. $this->assertTrue(is_dir($subdir));
  190. $this->assertTrue(is_writable($subdir));
  191. PHPFrame_Filesystem::rm($dir, true);
  192. }
  193. public function test_isEmptyDir()
  194. {
  195. $dir = $this->_sys_tmp_dir.DS."test-dir";
  196. if (is_dir($dir)) PHPFrame_Filesystem::rm($dir, true);
  197. mkdir($dir);
  198. $this->assertTrue(PHPFrame_Filesystem::isEmptyDir($dir));
  199. touch($dir.DS."file1.txt");
  200. touch($dir.DS."file2.txt");
  201. $this->assertFalse(PHPFrame_Filesystem::isEmptyDir($dir));
  202. PHPFrame_Filesystem::rm($dir, true);
  203. }
  204. public function test_upload()
  205. {
  206. $tmp_file = $this->_sys_tmp_dir.DS."uploadedfile";
  207. if (is_file($tmp_file)) {
  208. unlink($tmp_file);
  209. }
  210. touch($tmp_file);
  211. file_put_contents($tmp_file, "Lorem ipsum...");
  212. $this->assertTrue(is_file($tmp_file));
  213. $file_array = array(
  214. "name" => "MyUploadedFile.txt",
  215. "type" => "text/plain",
  216. "tmp_name" => $tmp_file,
  217. "error" => UPLOAD_ERR_OK,
  218. "size" => filesize($tmp_file)
  219. );
  220. PHPFrame_Filesystem::testMode(true);
  221. $array = PHPFrame_Filesystem::upload($file_array, $this->_sys_tmp_dir);
  222. PHPFrame_Filesystem::testMode(false);
  223. $this->assertType("array", $array);
  224. $this->assertArrayHasKey("finfo", $array);
  225. $this->assertArrayHasKey("mimetype", $array);
  226. $this->assertType("SplFileInfo", $array["finfo"]);
  227. $this->assertEquals("text/plain", $array["mimetype"]);
  228. unlink($array["finfo"]->getRealPath());
  229. unlink($tmp_file);
  230. }
  231. public function test_uploadFileArrayFailure()
  232. {
  233. $tmp_file = $this->_sys_tmp_dir.DS."uploadedfile";
  234. $file_array = array(
  235. "name" => "MyUploadedFile.txt",
  236. "type" => "text/plain",
  237. "tmp_name" => $tmp_file,
  238. "error" => UPLOAD_ERR_OK
  239. );
  240. $caught = false;
  241. try {
  242. PHPFrame_Filesystem::upload($file_array, $this->_sys_tmp_dir);
  243. } catch (InvalidArgumentException $e) {
  244. $this->assertRegExp("/file_array/", $e->getMessage());
  245. $caught = true;
  246. }
  247. $this->assertTrue($caught);
  248. }
  249. public function test_uploadUploadErrorFailure()
  250. {
  251. $tmp_file = $this->_sys_tmp_dir.DS."uploadedfile";
  252. $errors = array(
  253. UPLOAD_ERR_INI_SIZE => "PHP upload maximum file size exceeded!",
  254. UPLOAD_ERR_FORM_SIZE => "PHP maximum post size exceeded!",
  255. UPLOAD_ERR_PARTIAL => "Partial upload!",
  256. UPLOAD_ERR_NO_FILE => "No file submitted for upload!",
  257. UPLOAD_ERR_NO_TMP_DIR => "Missing temporary folder!",
  258. UPLOAD_ERR_CANT_WRITE => "Failed to write file to disk!",
  259. UPLOAD_ERR_EXTENSION => "A PHP extension stopped the file upload!"
  260. );
  261. $caught = 0;
  262. foreach ($errors as $code=>$msg) {
  263. $file_array = array(
  264. "name" => "MyUploadedFile.txt",
  265. "type" => "text/plain",
  266. "tmp_name" => $tmp_file,
  267. "error" => $code,
  268. "size" => 0
  269. );
  270. try {
  271. PHPFrame_Filesystem::upload($file_array, $this->_sys_tmp_dir);
  272. } catch (RuntimeException $e) {
  273. $this->assertEquals($msg, $e->getMessage());
  274. $caught++;
  275. }
  276. }
  277. $this->assertEquals(count($errors), $caught);
  278. }
  279. public function test_uploadMimeTypeFailure()
  280. {
  281. $tmp_file = $this->_sys_tmp_dir.DS."uploadedfile";
  282. if (is_file($tmp_file)) {
  283. unlink($tmp_file);
  284. }
  285. touch($tmp_file);
  286. file_put_contents($tmp_file, "Lorem ipsum...");
  287. $this->assertTrue(is_file($tmp_file));
  288. $file_array = array(
  289. "name" => "MyUploadedFile.txt",
  290. "type" => "text/plain",
  291. "tmp_name" => $tmp_file,
  292. "error" => UPLOAD_ERR_OK,
  293. "size" => filesize($tmp_file)
  294. );
  295. $caught = false;
  296. try {
  297. PHPFrame_Filesystem::upload($file_array, $this->_sys_tmp_dir, "image/png");
  298. } catch (RuntimeException $e) {
  299. $this->assertRegExp("/File type not valid/", $e->getMessage());
  300. $caught = true;
  301. }
  302. $this->assertTrue($caught);
  303. unlink($tmp_file);
  304. }
  305. public function test_uploadMaliciousScriptFailure()
  306. {
  307. $tmp_file = $this->_sys_tmp_dir.DS."uploadedfile";
  308. if (is_file($tmp_file)) {
  309. unlink($tmp_file);
  310. }
  311. touch($tmp_file);
  312. file_put_contents($tmp_file, "Lorem ipsum...");
  313. $this->assertTrue(is_file($tmp_file));
  314. $file_array = array(
  315. "name" => "MyUploadedFile.jpg.php",
  316. "type" => "image/jpg",
  317. "tmp_name" => $tmp_file,
  318. "error" => UPLOAD_ERR_OK,
  319. "size" => filesize($tmp_file)
  320. );
  321. $caught = false;
  322. try {
  323. PHPFrame_Filesystem::upload($file_array, $this->_sys_tmp_dir);
  324. } catch (RuntimeException $e) {
  325. $this->assertRegExp("/file attack/", $e->getMessage());
  326. $caught = true;
  327. }
  328. $this->assertTrue($caught);
  329. unlink($tmp_file);
  330. }
  331. public function test_uploadMaxFileSizeFailure()
  332. {
  333. $tmp_file = $this->_sys_tmp_dir.DS."uploadedfile";
  334. if (is_file($tmp_file)) {
  335. unlink($tmp_file);
  336. }
  337. touch($tmp_file);
  338. file_put_contents($tmp_file, "Lorem ipsum...");
  339. $this->assertTrue(is_file($tmp_file));
  340. $file_array = array(
  341. "name" => "MyUploadedFile.txt",
  342. "type" => "text/plain",
  343. "tmp_name" => $tmp_file,
  344. "error" => UPLOAD_ERR_OK,
  345. "size" => (2*1024*1024)
  346. );
  347. $caught = false;
  348. try {
  349. PHPFrame_Filesystem::upload($file_array, $this->_sys_tmp_dir, "*", 1);
  350. } catch (RuntimeException $e) {
  351. $this->assertRegExp("/Maximum file size exceeded/", $e->getMessage());
  352. $caught = true;
  353. }
  354. $this->assertTrue($caught);
  355. unlink($tmp_file);
  356. }
  357. public function test_uploadDestinationDirDoesntExistsFailure()
  358. {
  359. $tmp_file = $this->_sys_tmp_dir.DS."uploadedfile";
  360. if (is_file($tmp_file)) {
  361. unlink($tmp_file);
  362. }
  363. touch($tmp_file);
  364. file_put_contents($tmp_file, "Lorem ipsum...");
  365. $this->assertTrue(is_file($tmp_file));
  366. $file_array = array(
  367. "name" => "MyUploadedFile.txt",
  368. "type" => "text/plain",
  369. "tmp_name" => $tmp_file,
  370. "error" => UPLOAD_ERR_OK,
  371. "size" => filesize($tmp_file)
  372. );
  373. $ghost_dir = $this->_sys_tmp_dir.DS."ghostdir";
  374. if (is_dir($ghost_dir)) {
  375. PHPFrame_Filesystem::rm($ghost_dir, true);
  376. }
  377. $caught = false;
  378. try {
  379. PHPFrame_Filesystem::testMode(true);
  380. PHPFrame_Filesystem::upload($file_array, $ghost_dir);
  381. } catch (InvalidArgumentException $e) {
  382. $this->assertRegExp("/Destination directory/", $e->getMessage());
  383. $caught = true;
  384. }
  385. $this->assertTrue($caught);
  386. unlink($tmp_file);
  387. }
  388. public function test_uploadFileAttackFailure()
  389. {
  390. $tmp_file = $this->_sys_tmp_dir.DS."uploadedfile";
  391. if (is_file($tmp_file)) {
  392. unlink($tmp_file);
  393. }
  394. touch($tmp_file);
  395. file_put_contents($tmp_file, "Lorem ipsum...");
  396. $this->assertTrue(is_file($tmp_file));
  397. $file_array = array(
  398. "name" => "MyUploadedFile.txt",
  399. "type" => "text/plain",
  400. "tmp_name" => $tmp_file,
  401. "error" => UPLOAD_ERR_OK,
  402. "size" => filesize($tmp_file)
  403. );
  404. $caught = false;
  405. try {
  406. PHPFrame_Filesystem::testMode(false);
  407. PHPFrame_Filesystem::upload($file_array, $this->_sys_tmp_dir);
  408. } catch (RuntimeException $e) {
  409. $caught = true;
  410. }
  411. $this->assertTrue($caught);
  412. unlink($tmp_file);
  413. }
  414. // public function test_uploadMoveFailure()
  415. // {
  416. // $tmp_file = $this->_sys_tmp_dir.DS."uploadedfile";
  417. //
  418. // if (is_file($tmp_file)) {
  419. // unlink($tmp_file);
  420. // }
  421. //
  422. // touch($tmp_file);
  423. // file_put_contents($tmp_file, "Lorem ipsum...");
  424. //
  425. // $this->assertTrue(is_file($tmp_file));
  426. //
  427. // $file_array = array(
  428. // "name" => "MyUploadedFile.txt",
  429. // "type" => "text/plain",
  430. // "tmp_name" => $tmp_file,
  431. // "error" => UPLOAD_ERR_OK,
  432. // "size" => filesize($tmp_file)
  433. // );
  434. //
  435. // $ghost_dir = $this->_sys_tmp_dir.DS."ghostdir";
  436. // if (is_dir($ghost_dir)) {
  437. // rmdir($ghost_dir);
  438. // }
  439. //
  440. // mkdir($ghost_dir, 0555);
  441. //
  442. // $this->assertTrue(is_dir($ghost_dir));
  443. // $this->assertFalse(is_writable($ghost_dir));
  444. //
  445. // $caught = false;
  446. // PHPFrame_Filesystem::testMode(true);
  447. //
  448. // try {
  449. // PHPFrame_Filesystem::upload($file_array, $ghost_dir);
  450. // } catch (RuntimeException $e) {
  451. // $this->assertRegExp("/Could not move file/", $e->getMessage());
  452. // $caught = true;
  453. // }
  454. //
  455. // $this->assertTrue($caught);
  456. // PHPFrame_Filesystem::testMode(false);
  457. //
  458. // unlink($tmp_file);
  459. // chmod($ghost_dir, 0777);
  460. // rmdir($ghost_dir);
  461. // }
  462. public function test_filterFilename()
  463. {
  464. $data = array(
  465. "foo" => "foo",
  466. "foo%*" => "foo",
  467. "some weird file n^me @" => "some weird file nme "
  468. );
  469. foreach ($data as $key=>$value) {
  470. $this->assertEquals($value, PHPFrame_Filesystem::filterFilename($key, true));
  471. }
  472. }
  473. public function test_filterFilenameFailure()
  474. {
  475. $data = array(".", "/tmp", ".ssh", "~/Desktop");
  476. $caught = 0;
  477. foreach ($data as $value) {
  478. try {
  479. PHPFrame_Filesystem::filterFilename($value);
  480. } catch (InvalidArgumentException $e) {
  481. $this->assertEquals("Invalid file or directory name.", $e->getMessage());
  482. $caught++;
  483. }
  484. }
  485. $this->assertEquals(count($data), $caught);
  486. }
  487. public function test_getMimeType()
  488. {
  489. $tmp_file = $this->_sys_tmp_dir.DS."uploadedfile";
  490. if (is_file($tmp_file)) {
  491. unlink($tmp_file);
  492. }
  493. touch($tmp_file);
  494. file_put_contents($tmp_file, "Lorem ipsum...");
  495. $this->assertTrue(is_file($tmp_file));
  496. $mime = PHPFrame_Filesystem::getMimeType($tmp_file);
  497. $this->assertEquals("text/plain", $mime);
  498. unlink($tmp_file);
  499. }
  500. public function test_getMimeTypeFileDoesntExistFailure()
  501. {
  502. $tmp_file = $this->_sys_tmp_dir.DS."uploadedfile";
  503. if (is_file($tmp_file)) {
  504. unlink($tmp_file);
  505. }
  506. $this->assertTrue(!is_file($tmp_file));
  507. try {
  508. $mime = PHPFrame_Filesystem::getMimeType($tmp_file);
  509. } catch (RuntimeException $e) {
  510. $this->assertRegExp("/File '.*' doesn't exist\./", $e->getMessage());
  511. }
  512. }
  513. public function test_getSystemTempDir()
  514. {
  515. $this->assertTrue(is_dir(PHPFrame_Filesystem::getSystemTempDir()));
  516. $this->assertTrue(is_writable(PHPFrame_Filesystem::getSystemTempDir()));
  517. }
  518. public function test_getUserHomeDir()
  519. {
  520. if (array_key_exists("HOME", $_SERVER)) {
  521. $this->assertEquals($_SERVER["HOME"], PHPFrame_Filesystem::getUserHomeDir());
  522. }
  523. }
  524. public function test_getWindowsUserHomeDir()
  525. {
  526. if (array_key_exists("HOME", $_SERVER)) {
  527. $_SERVER["HOMEPATH"] = $_SERVER["HOME"];
  528. unset($_SERVER["HOME"]);
  529. $this->assertEquals($_SERVER["HOMEPATH"], PHPFrame_Filesystem::getUserHomeDir());
  530. $_SERVER["HOME"] = $_SERVER["HOMEPATH"];
  531. unset($_SERVER["HOMEPATH"]);
  532. }
  533. }
  534. public function test_getUserHomeDirFailure()
  535. {
  536. if (array_key_exists("HOME", $_SERVER)) {
  537. $home_bk = $_SERVER["HOME"];
  538. unset($_SERVER["HOME"]);
  539. $caught = false;
  540. try {
  541. PHPFrame_Filesystem::getUserHomeDir();
  542. } catch (RuntimeException $e) {
  543. $caught = true;
  544. $_SERVER["HOME"] = $home_bk;
  545. }
  546. $this->assertTrue($caught);
  547. }
  548. }
  549. public function test_bytes2Human()
  550. {
  551. $this->assertEquals("10 bytes", PHPFrame_Filesystem::bytes2human(10));
  552. $this->assertEquals("1KB", PHPFrame_Filesystem::bytes2human(1024));
  553. $this->assertEquals("13.2KB", PHPFrame_Filesystem::bytes2human(13.2*1024));
  554. $this->assertEquals("3.23MB", PHPFrame_Filesystem::bytes2human(3.23*1024*1024));
  555. }
  556. }