file : all the management of the files, with integrated filesystem checks, and virtual chroot support
git-svn-id: https://svn.fournier38.fr/svn/ProgSVN/trunk@2694 bf3deb0d-5f1a-0410-827f-c0cc1f45334c
This commit is contained in:
441
Tests/fileTest.php
Normal file
441
Tests/fileTest.php
Normal file
@@ -0,0 +1,441 @@
|
||||
<?php
|
||||
/** DomFramework
|
||||
@package domframework
|
||||
@author Dominique Fournier <dominique@fournier38.fr> */
|
||||
|
||||
/** Test the domframework file part */
|
||||
class test_file extends PHPUnit_Framework_TestCase
|
||||
{
|
||||
public function testinit ()
|
||||
{
|
||||
exec ("rm -rf /tmp/testDFFileDir");
|
||||
}
|
||||
|
||||
public function testRealPath01 ()
|
||||
{
|
||||
$file = new file ();
|
||||
$res = $file->realpath ("/tmp");
|
||||
$this->assertSame($res, "/tmp");
|
||||
}
|
||||
|
||||
public function testRealPath02 ()
|
||||
{
|
||||
$file = new file ();
|
||||
$res = $file->realpath ("////tmp");
|
||||
$this->assertSame($res, "/tmp");
|
||||
}
|
||||
|
||||
public function testRealPath03 ()
|
||||
{
|
||||
$file = new file ();
|
||||
$res = $file->realpath ("////tmp////");
|
||||
$this->assertSame($res, "/tmp");
|
||||
}
|
||||
|
||||
public function testRealPath04 ()
|
||||
{
|
||||
$file = new file ();
|
||||
$res = $file->realpath (".////tmp////");
|
||||
$this->assertSame($res, "./tmp");
|
||||
}
|
||||
|
||||
public function testRealPath05 ()
|
||||
{
|
||||
$file = new file ();
|
||||
$res = $file->realpath ("../../../../../tmp/file");
|
||||
$this->assertSame($res, "/tmp/file");
|
||||
}
|
||||
|
||||
public function testRealPath06 ()
|
||||
{
|
||||
$file = new file ();
|
||||
$res = $file->realpath ("../../../../../tmp/file/../../../..");
|
||||
$this->assertSame($res, "/");
|
||||
}
|
||||
|
||||
public function testRealPath07 ()
|
||||
{
|
||||
$file = new file ();
|
||||
$res = $file->realpath (".././././../tmp/file/../././..");
|
||||
$this->assertSame($res, "/");
|
||||
}
|
||||
|
||||
public function testRealPath11 ()
|
||||
{
|
||||
$file = new file ();
|
||||
$file->chroot ("/tmp");
|
||||
$res = $file->realpath ("/tmp2");
|
||||
$this->assertSame($res, "/tmp/tmp2");
|
||||
}
|
||||
|
||||
public function testRealPath12 ()
|
||||
{
|
||||
$file = new file ();
|
||||
$file->chroot ("/tmp");
|
||||
$res = $file->realpath ("////tmp2");
|
||||
$this->assertSame($res, "/tmp/tmp2");
|
||||
}
|
||||
|
||||
public function testRealPath13 ()
|
||||
{
|
||||
$file = new file ();
|
||||
$file->chroot ("/tmp");
|
||||
$res = $file->realpath ("////tmp2////");
|
||||
$this->assertSame($res, "/tmp/tmp2");
|
||||
}
|
||||
|
||||
public function testRealPath14 ()
|
||||
{
|
||||
$file = new file ();
|
||||
$file->chroot ("/tmp");
|
||||
$res = $file->realpath (".////tmp2////");
|
||||
$this->assertSame($res, "/tmp/tmp2");
|
||||
}
|
||||
|
||||
public function testRealPath15 ()
|
||||
{
|
||||
$file = new file ();
|
||||
$file->chroot ("/tmp");
|
||||
$res = $file->realpath ("../../../../../tmp2/file");
|
||||
$this->assertSame($res, "/tmp/tmp2/file");
|
||||
}
|
||||
|
||||
public function testRealPath16 ()
|
||||
{
|
||||
$file = new file ();
|
||||
$file->chroot ("/tmp");
|
||||
$res = $file->realpath ("../../../../../tmp2/file/../../../..");
|
||||
$this->assertSame($res, "/tmp");
|
||||
}
|
||||
|
||||
public function testRealPath17 ()
|
||||
{
|
||||
$file = new file ();
|
||||
$file->chroot ("/tmp");
|
||||
$res = $file->realpath (".././././../tmp2/file/../././..");
|
||||
$this->assertSame($res, "/tmp");
|
||||
}
|
||||
|
||||
|
||||
public function testGetcwd1 ()
|
||||
{
|
||||
$file = new file ();
|
||||
$res = $file->getcwd ();
|
||||
$this->assertSame($res, ".");
|
||||
}
|
||||
|
||||
public function testGetcwd2 ()
|
||||
{
|
||||
$file = new file ();
|
||||
$file->chdir ("/tmp");
|
||||
$res = $file->getcwd ();
|
||||
$this->assertSame($res, "/tmp");
|
||||
}
|
||||
|
||||
public function testGetcwd3 ()
|
||||
{
|
||||
$this->setExpectedException ("Exception", "Path '/NON EXISTS' not found");
|
||||
$file = new file ();
|
||||
$file->chdir ("/NON EXISTS");
|
||||
$res = $file->getcwd ();
|
||||
}
|
||||
public function testGetcwd4 ()
|
||||
{
|
||||
$file = new file ();
|
||||
$file->chdir ("/../../../tmp");
|
||||
$res = $file->getcwd ();
|
||||
$this->assertSame($res, "/tmp");
|
||||
}
|
||||
|
||||
public function testGetcwd5 ()
|
||||
{
|
||||
$file = new file ();
|
||||
$file->chdir ("../../../../../../tmp");
|
||||
$res = $file->getcwd ();
|
||||
$this->assertSame($res, "/tmp");
|
||||
}
|
||||
|
||||
public function testGetcwd6 ()
|
||||
{
|
||||
$this->setExpectedException ("Exception", "Path './tmp' not found");
|
||||
$file = new file ();
|
||||
$file->chdir ("./tmp");
|
||||
$res = $file->getcwd ();
|
||||
}
|
||||
|
||||
public function testGetcwd7 ()
|
||||
{
|
||||
$this->setExpectedException ("Exception",
|
||||
"Path '/tmp/NON EXISTS' not found");
|
||||
$file = new file ();
|
||||
$file->chdir ("/tmp/NON EXISTS");
|
||||
$res = $file->getcwd ();
|
||||
}
|
||||
|
||||
public function testGetcwd8 ()
|
||||
{
|
||||
$this->setExpectedException ("Exception",
|
||||
"Parent Path '/NON EXISTS/' not found");
|
||||
$file = new file ();
|
||||
$file->chdir ("/NON EXISTS/tmp");
|
||||
$res = $file->getcwd ();
|
||||
}
|
||||
|
||||
public function testchroot1 ()
|
||||
{
|
||||
$this->setExpectedException ("Exception");
|
||||
$file = new file ();
|
||||
$res = $file->chroot ("non exists");
|
||||
}
|
||||
|
||||
public function testchroot2 ()
|
||||
{
|
||||
$file = new file ();
|
||||
$res = $file->chroot ("/tmp");
|
||||
$this->assertSame($res, true);
|
||||
}
|
||||
|
||||
// Without chroot
|
||||
public function testMkdir1 ()
|
||||
{
|
||||
$file = new file ();
|
||||
$res = $file->mkdir ("/tmp/testmkdir1-".time());
|
||||
$this->assertSame($res, true);
|
||||
}
|
||||
|
||||
// With chroot in tmp
|
||||
public function testMkdir2 ()
|
||||
{
|
||||
$file = new file ();
|
||||
$res = $file->chroot ("/tmp");
|
||||
$res = $file->mkdir ("/testmkdir2-".time());
|
||||
$this->assertSame($res, true);
|
||||
}
|
||||
|
||||
// Without chroot and relative
|
||||
public function testMkdir3 ()
|
||||
{
|
||||
$file = new file ();
|
||||
$res = $file->mkdir ("/tmp/testmkdir3-".time());
|
||||
$this->assertSame($res, true);
|
||||
}
|
||||
|
||||
// With chroot in tmp and in relative
|
||||
public function testMkdir4 ()
|
||||
{
|
||||
$file = new file ();
|
||||
$res = $file->chroot ("/tmp");
|
||||
$res = $file->mkdir ("testmkdir4-".time());
|
||||
$this->assertSame($res, true);
|
||||
}
|
||||
|
||||
// With chroot in tmp
|
||||
public function testChdir1 ()
|
||||
{
|
||||
@rmdir ("/tmp/testDFFileDir");
|
||||
$file = new file ();
|
||||
$file->chroot ("/tmp");
|
||||
$file->mkdir ("/testDFFileDir");
|
||||
$file->chdir ("/testDFFileDir");
|
||||
$res = $file->getcwd ();
|
||||
$this->assertSame($res, "/testDFFileDir");
|
||||
}
|
||||
|
||||
// With chroot in tmp
|
||||
public function testChdir2 ()
|
||||
{
|
||||
@rmdir ("/tmp/testDFFileDir");
|
||||
$file = new file ();
|
||||
$file->chroot ("/tmp");
|
||||
$file->mkdir ("/testDFFileDir");
|
||||
$file->chroot ("/");
|
||||
$file->chdir ("/testDFFileDir");
|
||||
$res = $file->getcwd ();
|
||||
$this->assertSame($res, "/testDFFileDir");
|
||||
}
|
||||
|
||||
// With chroot in tmp
|
||||
public function testChdir3 ()
|
||||
{
|
||||
$this->setExpectedException ("Exception",
|
||||
"Path '/tmp/testDFFileDir/NON EXISTS' not found");
|
||||
@rmdir ("/tmp/testDFFileDir");
|
||||
$file = new file ();
|
||||
$file->chroot ("/tmp");
|
||||
$file->mkdir ("/testDFFileDir");
|
||||
$file->chroot ("/testDFFileDir");
|
||||
$file->chdir ("/NON EXISTS");
|
||||
}
|
||||
|
||||
// With chroot in tmp
|
||||
public function testChdir4 ()
|
||||
{
|
||||
$this->setExpectedException ("Exception",
|
||||
"Parent Path '/tmp/tmp/' not found");
|
||||
@rmdir ("/tmp/testDFFileDir");
|
||||
$file = new file ();
|
||||
$file->chroot ("/tmp");
|
||||
$file->mkdir ("/testDFFileDir");
|
||||
$file->chroot ("..");
|
||||
$file->chdir ("/tmp/testDFFileDir");
|
||||
}
|
||||
|
||||
public function testFile_get_contents1 ()
|
||||
{
|
||||
$this->setExpectedException ("Exception",
|
||||
"File '/tmp/testDFFileDir' is not a file");
|
||||
@rmdir ("/tmp/testDFFileDir");
|
||||
$file = new file ();
|
||||
$file->chroot ("/tmp");
|
||||
$file->mkdir ("/testDFFileDir");
|
||||
$res = $file->file_get_contents ("/testDFFileDir");
|
||||
}
|
||||
|
||||
public function testFile_get_contents2 ()
|
||||
{
|
||||
@rmdir ("/tmp/testDFFileDir");
|
||||
$file = new file ();
|
||||
$file->chroot ("/tmp");
|
||||
$file->mkdir ("/testDFFileDir");
|
||||
$file->touch ("/testDFFileDir/toto");
|
||||
$res = $file->file_get_contents ("/testDFFileDir/toto");
|
||||
$this->assertSame($res, "");
|
||||
}
|
||||
|
||||
public function testFile_put_contents1 ()
|
||||
{
|
||||
$file = new file ();
|
||||
$file->chroot ("/tmp");
|
||||
$file->file_put_contents ("/testDFFileDir/toto", "content");
|
||||
$res = $file->file_get_contents ("/testDFFileDir/toto");
|
||||
$this->assertSame($res, "content");
|
||||
}
|
||||
|
||||
public function testFile_put_contents2 ()
|
||||
{
|
||||
$this->setExpectedException ("Exception",
|
||||
"File '/tmp/testDFFileDir' is not a file");
|
||||
$file = new file ();
|
||||
$file->file_put_contents ("/tmp/testDFFileDir", "content");
|
||||
}
|
||||
|
||||
public function testFile_put_contents3 ()
|
||||
{
|
||||
$file = new file ();
|
||||
$file->chroot ("/tmp");
|
||||
$res = $file->file_put_contents ("/testDFFileDir/toto", "content");
|
||||
$this->assertSame($res, 7);
|
||||
}
|
||||
|
||||
public function testscandir1 ()
|
||||
{
|
||||
$file = new file ();
|
||||
$res = $file->scandir ("/tmp/testDFFileDir");
|
||||
$this->assertSame ($res, array ("toto"));
|
||||
}
|
||||
|
||||
public function testscandir2 ()
|
||||
{
|
||||
$file = new file ();
|
||||
$file->chroot ("/tmp");
|
||||
$res = $file->scandir ("/testDFFileDir");
|
||||
$this->assertSame ($res, array ("toto"));
|
||||
}
|
||||
|
||||
public function testscandir3 ()
|
||||
{
|
||||
$file = new file ();
|
||||
$file->chroot ("/tmp");
|
||||
$res = $file->scandir ("/testDFFileDir/");
|
||||
$this->assertSame ($res, array ("toto"));
|
||||
}
|
||||
|
||||
public function testrmdir1 ()
|
||||
{
|
||||
// Directory not empty and NOT recursive : return false
|
||||
$file = new file ();
|
||||
$res = $file->rmdir ("/tmp/testDFFileDir");
|
||||
$this->assertSame ($res, false);
|
||||
}
|
||||
|
||||
public function testrmdir2 ()
|
||||
{
|
||||
// Directory not empty and NOT recursive : return false
|
||||
$file = new file ();
|
||||
$file->chroot ("/tmp");
|
||||
$res = $file->rmdir ("/testDFFileDir");
|
||||
$this->assertSame ($res, false);
|
||||
}
|
||||
|
||||
public function testrmdir3 ()
|
||||
{
|
||||
// Directory not empty and recursive : return true
|
||||
$file = new file ();
|
||||
$file->chroot ("/tmp");
|
||||
$res = $file->rmdir ("/testDFFileDir", true);
|
||||
$this->assertSame ($res, true);
|
||||
}
|
||||
|
||||
public function testUnlink1 ()
|
||||
{
|
||||
@rmdir ("/tmp/testDFFileDir");
|
||||
$file = new file ();
|
||||
$file->chroot ("/tmp");
|
||||
$file->mkdir ("/testDFFileDir");
|
||||
$file->touch ("/testDFFileDir/toto");
|
||||
$res = $file->unlink ("/testDFFileDir/toto");
|
||||
$this->assertSame ($res, true);
|
||||
}
|
||||
|
||||
public function testIsDir1 ()
|
||||
{
|
||||
$file = new file ();
|
||||
$file->chroot ("/tmp");
|
||||
$res = $file->is_dir ("//testDFFileDir");
|
||||
$this->assertSame ($res, true);
|
||||
}
|
||||
|
||||
public function testIsDir2 ()
|
||||
{
|
||||
$file = new file ();
|
||||
$file->chroot ("/tmp");
|
||||
$file->touch ("/testDFFileDir/toto");
|
||||
$res = $file->is_dir ("//testDFFileDir/toto");
|
||||
$this->assertSame ($res, false);
|
||||
}
|
||||
|
||||
public function testIsFile1 ()
|
||||
{
|
||||
$file = new file ();
|
||||
$file->chroot ("/tmp");
|
||||
$file->touch ("/testDFFileDir/toto");
|
||||
$res = $file->is_file ("/testDFFileDir/toto");
|
||||
$this->assertSame ($res, true);
|
||||
}
|
||||
|
||||
public function testIsFile2 ()
|
||||
{
|
||||
$file = new file ();
|
||||
$file->chroot ("/tmp");
|
||||
$file->touch ("/testDFFileDir/toto");
|
||||
$res = $file->is_file ("//testDFFileDir");
|
||||
$this->assertSame ($res, false);
|
||||
}
|
||||
|
||||
public function testMkdir5 ()
|
||||
{
|
||||
$file = new file ();
|
||||
$file->chroot ("/tmp");
|
||||
// The parent doesn't exists and not recursive mode : exception
|
||||
$this->expectException ();
|
||||
$res = $file->mkdir ("/testDFFileDir/tptp/titi/poo");
|
||||
}
|
||||
|
||||
public function testMkdir6 ()
|
||||
{
|
||||
$file = new file ();
|
||||
$file->chroot ("/tmp");
|
||||
$res = $file->mkdir ("/testDFFileDir/tptp/titi/poo", 0777, true);
|
||||
$this->assertSame ($res, true);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user