]> granicus.if.org Git - php/commitdiff
new testcases for dir() function
authorRaghubansh Kumar <kraghuba@php.net>
Mon, 24 Dec 2007 05:21:08 +0000 (05:21 +0000)
committerRaghubansh Kumar <kraghuba@php.net>
Mon, 24 Dec 2007 05:21:08 +0000 (05:21 +0000)
ext/standard/tests/dir/dir_basic.phpt [new file with mode: 0644]
ext/standard/tests/dir/dir_error.phpt [new file with mode: 0644]
ext/standard/tests/dir/dir_variation1.phpt [new file with mode: 0644]
ext/standard/tests/dir/dir_variation2.phpt [new file with mode: 0644]
ext/standard/tests/dir/dir_variation3.phpt [new file with mode: 0644]
ext/standard/tests/dir/dir_variation4.phpt [new file with mode: 0644]
ext/standard/tests/dir/dir_variation5.phpt [new file with mode: 0644]
ext/standard/tests/dir/dir_variation6.phpt [new file with mode: 0644]
ext/standard/tests/dir/dir_variation7.phpt [new file with mode: 0644]
ext/standard/tests/dir/dir_variation8.phpt [new file with mode: 0644]
ext/standard/tests/dir/dir_variation9.phpt [new file with mode: 0644]

diff --git a/ext/standard/tests/dir/dir_basic.phpt b/ext/standard/tests/dir/dir_basic.phpt
new file mode 100644 (file)
index 0000000..dba49db
--- /dev/null
@@ -0,0 +1,86 @@
+--TEST--
+Test dir() function : basic functionality
+--FILE--
+<?php
+/* 
+ * Prototype  : object dir(string $directory[, resource $context])
+ * Description: Directory class with properties, handle and class and methods read, rewind and close
+ * Source code: ext/standard/dir.c
+ */
+
+echo "*** Testing dir() : basic functionality ***\n";
+
+// include the file.inc for Function: function create_files()
+include(dirname(__FILE__)."/../file/file.inc");
+
+// create the temporary directory
+$file_path = dirname(__FILE__);
+$dir_path = $file_path."/dir_basic";
+@mkdir($dir_path);
+
+// create files within the temporary directory
+create_files($dir_path, 3, "alphanumeric", 0755, 1, "w", "dir_basic");
+
+echo "Get Directory instance:\n";
+$d = dir($dir_path);
+var_dump( $d );
+
+echo "\nRead and rewind:\n";
+var_dump( $d->read() );
+var_dump( $d->read() );
+var_dump( $d->rewind() );
+
+echo "\nTest using handle directly:\n";
+var_dump( readdir($d->handle) );
+var_dump( readdir($d->handle) );
+
+echo "\nClose directory:\n";
+var_dump( $d->close() );
+var_dump( $d );
+
+echo "\nTest read after closing the dir:";
+var_dump( $d->read() );
+
+// delete temp files
+delete_files($dir_path, 3, "dir_basic", 1, ".tmp");
+echo "Done";
+?>
+--CLEAN--
+<?php
+$file_path = dirname(__FILE__);
+$dir_path = $file_path."/dir_basic";
+
+rmdir($dir_path);
+?>
+--EXPECTF--
+*** Testing dir() : basic functionality ***
+Get Directory instance:
+object(Directory)#%d (2) {
+  ["path"]=>
+  string(%d) "%s/dir_basic"
+  ["handle"]=>
+  resource(%d) of type (stream)
+}
+
+Read and rewind:
+string(%d) "%s"
+string(%d) "%s"
+NULL
+
+Test using handle directly:
+string(%d) "%s"
+string(%d) "%s"
+
+Close directory:
+NULL
+object(Directory)#%d (2) {
+  ["path"]=>
+  string(%d) "%s/dir_basic"
+  ["handle"]=>
+  resource(%d) of type (Unknown)
+}
+
+Test read after closing the dir:
+Warning: Directory::read(): %d is not a valid Directory resource in %s on line %d
+bool(false)
+Done
\ No newline at end of file
diff --git a/ext/standard/tests/dir/dir_error.phpt b/ext/standard/tests/dir/dir_error.phpt
new file mode 100644 (file)
index 0000000..f2ef25c
--- /dev/null
@@ -0,0 +1,34 @@
+--TEST--
+Test dir() function : error conditions 
+--FILE--
+<?php
+/* 
+ * Prototype  : object dir(string $directory[, resource $context])
+ * Description: Directory class with properties, handle and class and methods read, rewind and close
+ * Source code: ext/standard/dir.c
+ */
+
+echo "*** Testing dir() : error conditions ***\n";
+
+// Zero arguments
+echo "\n-- Testing dir() function with zero arguments --";
+var_dump( dir() );
+
+// With one more than expected number of arguments
+echo "\n-- Testing dir() function with one more than expected number of arguments --";
+$extra_arg = 10;
+var_dump( dir(getcwd(), "stream", $extra_arg) );
+
+echo "Done";
+?>
+--EXPECTF--
+*** Testing dir() : error conditions ***
+
+-- Testing dir() function with zero arguments --
+Warning: dir() expects at least 1 parameter, 0 given in %s on line %d
+NULL
+
+-- Testing dir() function with one more than expected number of arguments --
+Warning: dir() expects at most 2 parameters, 3 given in %s on line %d
+NULL
+Done
diff --git a/ext/standard/tests/dir/dir_variation1.phpt b/ext/standard/tests/dir/dir_variation1.phpt
new file mode 100644 (file)
index 0000000..ad912c0
--- /dev/null
@@ -0,0 +1,160 @@
+--TEST--
+Test dir() function : usage variations - unexpected value for 'dir' argument
+--FILE--
+<?php
+/* 
+ * Prototype  : object dir(string $directory[, resource $context])
+ * Description: Directory class with properties, handle and class and methods read, rewind and close
+ * Source code: ext/standard/dir.c
+ */
+
+/*
+ * Passing non string values to 'directory' argument of dir() and see
+ * that the function outputs proper warning messages wherever expected.
+ */
+
+echo "*** Testing dir() : unexpected values for \$directory argument ***\n";
+
+// get an unset variable
+$unset_var = 10;
+unset($unset_var);
+
+class A
+{
+  public $var;
+  public function init() {
+    $this->var = 10;
+  }
+}
+
+// get a resource variable
+$fp = fopen(__FILE__, "r"); // get a file handle 
+$dfp = opendir( dirname(__FILE__) ); // get a dir handle
+
+// unexpected values to be passed to $directory argument
+$unexpected_values = array (
+
+       // array data
+/*1*/  array(),
+       array(0),
+       array(1),
+       array(1, 2),
+       array('color' => 'red', 'item' => 'pen'),
+
+       // null data
+/*6*/  NULL,
+       null,
+
+       // boolean data
+/*8*/  true,
+       false,
+       TRUE,
+       FALSE,
+
+       // empty data
+/*12*/ "",
+       '',
+
+       // undefined data
+/*14*/ @$undefined_var,
+
+       // unset data
+/*15*/ @$unset_var,
+
+       // resource variable(dir and file handle)
+/*16*/ $fp,
+       $dfp,
+
+       // object data
+/*18*/ new A()
+);
+
+// loop through various elements of $unexpected_values to check the behavior of dir()
+$iterator = 1;
+foreach( $unexpected_values as $unexpected_value ) {
+  echo "\n-- Iteration $iterator --\n";
+  var_dump( dir($unexpected_value) );
+  $iterator++;
+}
+
+fclose($fp);
+closedir($dfp);
+echo "Done";
+?>
+--EXPECTF--
+*** Testing dir() : unexpected values for $directory argument ***
+
+-- Iteration 1 --
+
+Warning: dir() expects parameter 1 to be string, array given in %s on line %d
+NULL
+
+-- Iteration 2 --
+
+Warning: dir() expects parameter 1 to be string, array given in %s on line %d
+NULL
+
+-- Iteration 3 --
+
+Warning: dir() expects parameter 1 to be string, array given in %s on line %d
+NULL
+
+-- Iteration 4 --
+
+Warning: dir() expects parameter 1 to be string, array given in %s on line %d
+NULL
+
+-- Iteration 5 --
+
+Warning: dir() expects parameter 1 to be string, array given in %s on line %d
+NULL
+
+-- Iteration 6 --
+bool(false)
+
+-- Iteration 7 --
+bool(false)
+
+-- Iteration 8 --
+
+Warning: dir(1): failed to open dir: %s in %s on line %d
+bool(false)
+
+-- Iteration 9 --
+bool(false)
+
+-- Iteration 10 --
+
+Warning: dir(1): failed to open dir: %s in %s on line %d
+bool(false)
+
+-- Iteration 11 --
+bool(false)
+
+-- Iteration 12 --
+bool(false)
+
+-- Iteration 13 --
+bool(false)
+
+-- Iteration 14 --
+bool(false)
+
+-- Iteration 15 --
+bool(false)
+
+-- Iteration 16 --
+
+Warning: dir() expects parameter 1 to be string, resource given in %s on line %d
+NULL
+
+-- Iteration 17 --
+
+Warning: dir() expects parameter 1 to be string, resource given in %s on line %d
+NULL
+
+-- Iteration 18 --
+
+Warning: dir() expects parameter 1 to be string, object given in %s on line %d
+NULL
+Done
\ No newline at end of file
diff --git a/ext/standard/tests/dir/dir_variation2.phpt b/ext/standard/tests/dir/dir_variation2.phpt
new file mode 100644 (file)
index 0000000..ee42900
--- /dev/null
@@ -0,0 +1,223 @@
+--TEST--
+Test dir() function : usage variations - unexpected value for 'context' argument
+--FILE--
+<?php
+/* 
+ * Prototype  : object dir(string $directory[, resource $context])
+ * Description: Directory class with properties, handle and class and methods read, rewind and close
+ * Source code: ext/standard/dir.c
+ */
+
+/*
+ * Passing non resource values to 'context' argument of dir() and see
+ * that the function outputs proper warning messages wherever expected.
+ */
+
+echo "*** Testing dir() : unexpected values for \$context argument ***\n";
+
+// create the temporary directory
+$file_path = dirname(__FILE__);
+$directory = $file_path."/dir_variation2";
+@mkdir($directory);
+
+// get an unset variable
+$unset_var = stream_context_create();
+unset($unset_var);
+
+class classA
+{
+  public $var;
+  public function init() {
+    $this->var = 10;
+  }
+}
+
+// heredoc string
+$heredoc = <<<EOT
+hello world
+EOT;
+
+// unexpected values to be passed to $directory argument
+$unexpected_values = array (
+       // int data
+/*1*/  0,
+       1,
+       12345,
+       -2345,
+
+       // float data
+/*5*/  10.5,
+       -10.5,
+       12.3456789000e10,
+       12.3456789000E-10,
+       .5,
+
+       // array data
+/*10*/ array(),
+       array(0),
+       array(1),
+       array(1, 2),
+       array('color' => 'red', 'item' => 'pen'),
+
+
+       // null data
+/*15*/ NULL,
+       null,
+
+       // boolean data
+/*17*/ true,
+       false,
+       TRUE,
+       FALSE,
+
+       // empty data
+/*21*/ "",
+       '',
+
+       // string data
+/*23*/ "string",
+       'string',
+       $heredoc,
+
+       // object data
+/*26*/ new classA(),
+
+       // undefined data
+/*27*/ @$undefined_var,
+
+       // unset data
+/*28*/ @$unset_var
+);
+
+// loop through various elements of $unexpected_values to check the behavior of dir()
+$iterator = 1;
+foreach( $unexpected_values as $unexpected_value ) {
+  echo "\n-- Iteration $iterator --";
+  var_dump( dir($directory, $unexpected_value) );
+  $iterator++;
+}
+
+echo "Done";
+?>
+--CLEAN--
+<?php
+$file_path = dirname(__FILE__);
+$directory = $file_path."/dir_variation2";
+
+rmdir($directory);
+?>
+--EXPECTF--
+*** Testing dir() : unexpected values for $context argument ***
+
+-- Iteration 1 --
+Warning: dir() expects parameter 2 to be resource, integer given in %s on line %d
+NULL
+
+-- Iteration 2 --
+Warning: dir() expects parameter 2 to be resource, integer given in %s on line %d
+NULL
+
+-- Iteration 3 --
+Warning: dir() expects parameter 2 to be resource, integer given in %s on line %d
+NULL
+
+-- Iteration 4 --
+Warning: dir() expects parameter 2 to be resource, integer given in %s on line %d
+NULL
+
+-- Iteration 5 --
+Warning: dir() expects parameter 2 to be resource, double given in %s on line %d
+NULL
+
+-- Iteration 6 --
+Warning: dir() expects parameter 2 to be resource, double given in %s on line %d
+NULL
+
+-- Iteration 7 --
+Warning: dir() expects parameter 2 to be resource, double given in %s on line %d
+NULL
+
+-- Iteration 8 --
+Warning: dir() expects parameter 2 to be resource, double given in %s on line %d
+NULL
+
+-- Iteration 9 --
+Warning: dir() expects parameter 2 to be resource, double given in %s on line %d
+NULL
+
+-- Iteration 10 --
+Warning: dir() expects parameter 2 to be resource, array given in %s on line %d
+NULL
+
+-- Iteration 11 --
+Warning: dir() expects parameter 2 to be resource, array given in %s on line %d
+NULL
+
+-- Iteration 12 --
+Warning: dir() expects parameter 2 to be resource, array given in %s on line %d
+NULL
+
+-- Iteration 13 --
+Warning: dir() expects parameter 2 to be resource, array given in %s on line %d
+NULL
+
+-- Iteration 14 --
+Warning: dir() expects parameter 2 to be resource, array given in %s on line %d
+NULL
+
+-- Iteration 15 --
+Warning: dir() expects parameter 2 to be resource, null given in %s on line %d
+NULL
+
+-- Iteration 16 --
+Warning: dir() expects parameter 2 to be resource, null given in %s on line %d
+NULL
+
+-- Iteration 17 --
+Warning: dir() expects parameter 2 to be resource, boolean given in %s on line %d
+NULL
+
+-- Iteration 18 --
+Warning: dir() expects parameter 2 to be resource, boolean given in %s on line %d
+NULL
+
+-- Iteration 19 --
+Warning: dir() expects parameter 2 to be resource, boolean given in %s on line %d
+NULL
+
+-- Iteration 20 --
+Warning: dir() expects parameter 2 to be resource, boolean given in %s on line %d
+NULL
+
+-- Iteration 21 --
+Warning: dir() expects parameter 2 to be resource, string given in %s on line %d
+NULL
+
+-- Iteration 22 --
+Warning: dir() expects parameter 2 to be resource, string given in %s on line %d
+NULL
+
+-- Iteration 23 --
+Warning: dir() expects parameter 2 to be resource, string given in %s on line %d
+NULL
+
+-- Iteration 24 --
+Warning: dir() expects parameter 2 to be resource, string given in %s on line %d
+NULL
+
+-- Iteration 25 --
+Warning: dir() expects parameter 2 to be resource, string given in %s on line %d
+NULL
+
+-- Iteration 26 --
+Warning: dir() expects parameter 2 to be resource, object given in %s on line %d
+NULL
+
+-- Iteration 27 --
+Warning: dir() expects parameter 2 to be resource, null given in %s on line %d
+NULL
+
+-- Iteration 28 --
+Warning: dir() expects parameter 2 to be resource, null given in %s on line %d
+NULL
+Done
\ No newline at end of file
diff --git a/ext/standard/tests/dir/dir_variation3.phpt b/ext/standard/tests/dir/dir_variation3.phpt
new file mode 100644 (file)
index 0000000..a3badc7
--- /dev/null
@@ -0,0 +1,195 @@
+--TEST--
+Test dir() function : usage variations - different directory permissions
+--SKIPIF--
+<?php
+if( substr(PHP_OS, 0, 3) == 'WIN') {
+  die('skip Not for Windows');
+}
+?>
+--FILE--
+<?php
+/* 
+ * Prototype  : object dir(string $directory[, resource $context])
+ * Description: Directory class with properties, handle and class and methods read, rewind and close
+ * Source code: ext/standard/dir.c
+ */
+
+/*
+ * Providing various permissions to the directory to be opened and checking
+ * to see if dir() function opens the directory successfully.
+ */
+
+echo "*** Testing dir() : different directory permissions ***";
+
+// create the temporary directory
+$file_path = dirname(__FILE__);
+$dir_path = $file_path."/dir_variation3";
+@mkdir($dir_path);
+
+/* different values for directory permissions */
+$permission_values = array(
+/*1*/  0477,  // owner has read only, other and group has rwx
+       0677,  // owner has rw only, other and group has rwx
+
+/*3*/  0444,  // all have read only
+       0666,  // all have rw only
+
+/*5*/  0400,  // owner has read only, group and others have no permission
+       0600,   // owner has rw only, group and others have no permission
+
+/*7*/  0470,  // owner has read only, group has rwx & others have no permission
+       0407,  // owner has read only, other has rwx & group has no permission
+
+/*9*/  0670,  // owner has rw only, group has rwx & others have no permission
+/*10*/ 0607   // owner has rw only, group has no permission and others have rwx
+);
+
+// Open directory with different permission values, read and close, expected: none of them to succeed.
+for($count = 0; $count < count($permission_values); $count++) {
+  echo "\n-- Iteration ".($count + 1)." --\n";
+
+  // try to remove the dir if exists  & create 
+  $file_path = dirname(__FILE__);
+  $dir_path = $file_path."/dir_variation3";
+  @chmod ($dir_path, 0777); // change dir permission to allow all operation
+  @rmdir ($dir_path);  // try n delete the dir
+
+  // create the dir now
+  @mkdir($dir_path);
+
+  // change the dir permisson to test dir on it
+  var_dump( chmod($dir_path, $permission_values[$count]) );
+
+  // try to get dir handle
+  $d = dir($dir_path);
+  var_dump($d);   // dump the handle 
+
+  // try read directory, expected : false
+  echo "-- reading contents --\n";
+  var_dump($d->read());
+
+  // close directory
+  $d->close();
+}
+
+echo "Done";
+?>
+--CLEAN--
+<?php
+// deleting temporary directory
+$file_path = dirname(__FILE__);
+$dir_path = $file_path."/dir_variation3";
+rmdir($dir_path);
+?>
+--EXPECTF--
+*** Testing dir() : different directory permissions ***
+-- Iteration 1 --
+bool(true)
+object(Directory)#%d (2) {
+  ["path"]=>
+  string(%d) "%s/dir_variation3"
+  ["handle"]=>
+  resource(%d) of type (stream)
+}
+-- reading contents --
+string(%d) "%s"
+
+-- Iteration 2 --
+bool(true)
+object(Directory)#%d (2) {
+  ["path"]=>
+  string(%d) "%s/dir_variation3"
+  ["handle"]=>
+  resource(%d) of type (stream)
+}
+-- reading contents --
+string(%d) "%s"
+
+-- Iteration 3 --
+bool(true)
+object(Directory)#%d (2) {
+  ["path"]=>
+  string(%d) "%s/dir_variation3"
+  ["handle"]=>
+  resource(%d) of type (stream)
+}
+-- reading contents --
+string(%d) "%s"
+
+-- Iteration 4 --
+bool(true)
+object(Directory)#%d (2) {
+  ["path"]=>
+  string(%d) "%s/dir_variation3"
+  ["handle"]=>
+  resource(%d) of type (stream)
+}
+-- reading contents --
+string(%d) "%s"
+
+-- Iteration 5 --
+bool(true)
+object(Directory)#%d (2) {
+  ["path"]=>
+  string(%d) "%s/dir_variation3"
+  ["handle"]=>
+  resource(%d) of type (stream)
+}
+-- reading contents --
+string(%d) "%s"
+
+-- Iteration 6 --
+bool(true)
+object(Directory)#%d (2) {
+  ["path"]=>
+  string(%d) "%s/dir_variation3"
+  ["handle"]=>
+  resource(%d) of type (stream)
+}
+-- reading contents --
+string(%d) "%s"
+
+-- Iteration 7 --
+bool(true)
+object(Directory)#%d (2) {
+  ["path"]=>
+  string(%d) "%s/dir_variation3"
+  ["handle"]=>
+  resource(%d) of type (stream)
+}
+-- reading contents --
+string(%d) "%s"
+
+-- Iteration 8 --
+bool(true)
+object(Directory)#%d (2) {
+  ["path"]=>
+  string(%d) "%s/dir_variation3"
+  ["handle"]=>
+  resource(%d) of type (stream)
+}
+-- reading contents --
+string(%d) "%s"
+
+-- Iteration 9 --
+bool(true)
+object(Directory)#%d (2) {
+  ["path"]=>
+  string(%d) "%s/dir_variation3"
+  ["handle"]=>
+  resource(%d) of type (stream)
+}
+-- reading contents --
+string(%d) "%s"
+
+-- Iteration 10 --
+bool(true)
+object(Directory)#%d (2) {
+  ["path"]=>
+  string(%d) "%s/dir_variation3"
+  ["handle"]=>
+  resource(%d) of type (stream)
+}
+-- reading contents --
+string(%d) "%s"
+Done
diff --git a/ext/standard/tests/dir/dir_variation4.phpt b/ext/standard/tests/dir/dir_variation4.phpt
new file mode 100644 (file)
index 0000000..62b10b2
--- /dev/null
@@ -0,0 +1,72 @@
+--TEST--
+Test dir() function : usage variations - operate on previously opened directory
+--FILE--
+<?php
+/* 
+ * Prototype  : object dir(string $directory[, resource $context])
+ * Description: Directory class with properties, handle and class and methods read, rewind and close
+ * Source code: ext/standard/dir.c
+ */
+
+/*
+ * Testing the behavior of dir() function by trying to open a
+ * directory which is already open.
+ */
+
+echo "*** Testing dir() : operate on previously opened directory ***\n";
+
+// include the file.inc for Function: function create_files()
+include( dirname(__FILE__)."/../file/file.inc");
+
+// create the temporary directory
+$file_path = dirname(__FILE__);
+$dir_path = $file_path."/dir_variation4";
+@mkdir($dir_path);
+
+// create files within the temporary directory
+create_files($dir_path, 3, "alphanumeric", 0755, 1, "w", "dir_variation4");
+
+// open the directory
+$d = dir($dir_path);
+var_dump( $d );
+
+// open the same directory again without closing it
+$e = dir($dir_path);
+var_dump( $e );
+
+echo "-- reading directory contents with previous handle --\n";
+var_dump( $d->read() ); // with previous handle
+
+echo "-- reading directory contents with current handle --\n";
+var_dump( $e->read() ); // with current handle
+
+// delete temporary files
+delete_files($dir_path, 3, "dir_variation4");
+echo "Done";
+?>
+--CLEAN--
+<?php
+$file_path = dirname(__FILE__);
+$dir_path = $file_path."/dir_variation4";
+
+rmdir($dir_path);
+?>
+--EXPECTF--
+*** Testing dir() : operate on previously opened directory ***
+object(Directory)#%d (2) {
+  ["path"]=>
+  string(%d) "%s/dir_variation4"
+  ["handle"]=>
+  resource(%d) of type (stream)
+}
+object(Directory)#%d (2) {
+  ["path"]=>
+  string(%d) "%s/dir_variation4"
+  ["handle"]=>
+  resource(%d) of type (stream)
+}
+-- reading directory contents with previous handle --
+string(%d) "%s"
+-- reading directory contents with current handle --
+string(%d) "%s"
+Done
diff --git a/ext/standard/tests/dir/dir_variation5.phpt b/ext/standard/tests/dir/dir_variation5.phpt
new file mode 100644 (file)
index 0000000..2399c63
--- /dev/null
@@ -0,0 +1,29 @@
+--TEST--
+Test dir() function : usage variations - open a file instead of directory
+--FILE--
+<?php
+/* 
+ * Prototype  : object dir(string $directory[, resource $context])
+ * Description: Directory class with properties, handle and class and methods read, rewind and close
+ * Source code: ext/standard/dir.c
+ */
+
+/*
+ * Passing a file as argument to dir() function instead of a directory 
+ * and checking if proper warning message is generated.
+ */
+
+echo "*** Testing dir() : open a file instead of a directory ***\n";
+
+// open the file instead of directory
+$d = dir(__FILE__);
+var_dump( $d );
+
+echo "Done";
+?>
+--EXPECTF--
+*** Testing dir() : open a file instead of a directory ***
+
+Warning: dir(%s): failed to open dir: %s in %s on line %d
+bool(false)
+Done
diff --git a/ext/standard/tests/dir/dir_variation6.phpt b/ext/standard/tests/dir/dir_variation6.phpt
new file mode 100644 (file)
index 0000000..e42057e
--- /dev/null
@@ -0,0 +1,51 @@
+--TEST--
+Test dir() function : usage variations - non-existent directory
+--FILE--
+<?php
+/* 
+ * Prototype  : object dir(string $directory[, resource $context])
+ * Description: Directory class with properties, handle and class and methods read, rewind and close
+ * Source code: ext/standard/dir.c
+ */
+
+/*
+ * Passing a non-existent directory as argument to dir() function
+ * and checking to see if proper warning message is output.
+ */
+echo "*** Testing dir() : open a non-existent directory ***\n";
+
+// create the temporary directory
+$file_path = dirname(__FILE__);
+$dir_path = $file_path."/dir_variation6";
+@mkdir($dir_path);
+
+// open existent directory
+$d = dir($dir_path);
+$d->close(); //close the dir
+
+// remove directory and try to open the same(non-existent) directory again
+rmdir($dir_path);
+clearstatcache();
+
+echo "-- opening previously removed directory --\n";
+var_dump( dir($dir_path) );
+
+// point to a non-existent directory
+$non_existent_dir = $file_path."/non_existent_dir";
+echo "-- opening non-existent directory --\n";
+$d = dir($non_existent_dir);
+var_dump( $d );
+
+echo "Done";
+?>
+--EXPECTF--
+*** Testing dir() : open a non-existent directory ***
+-- opening previously removed directory --
+
+Warning: dir(%s): failed to open dir: %s in %s on line %d
+bool(false)
+-- opening non-existent directory --
+
+Warning: dir(%s): failed to open dir: %s in %s on line %d
+bool(false)
+Done
diff --git a/ext/standard/tests/dir/dir_variation7.phpt b/ext/standard/tests/dir/dir_variation7.phpt
new file mode 100644 (file)
index 0000000..baf9a79
--- /dev/null
@@ -0,0 +1,85 @@
+--TEST--
+Test dir() function : usage variations - directories with restricted permissions
+--SKIPIF--
+<?php
+if( substr(PHP_OS, 0, 3) == 'WIN') {
+  die('skip Not for Windows');
+}
+?>
+--FILE--
+<?php
+/* 
+ * Prototype  : object dir(string $directory[, resource $context])
+ * Description: Directory class with properties, handle and class and methods read, rewind and close
+ * Source code: ext/standard/dir.c
+ */
+
+/* 
+ * remove the execute permission from the parent dir and test dir() on child dir
+ *   1) remove write & execute permission from the 1st parent and test dir()
+ *   2) remove execute permission from 2nd parent and test dir()
+ */
+
+echo "*** Testing dir() : remove execute permission from the parent dir ***\n";
+
+/* create the temporary directory :
+  dir_variation7  ( parent )
+    |-> sub_dir    ( sub parent )
+         |-> child_dir  ( child dir)
+*/
+$file_path = dirname(__FILE__);
+$parent_dir_path = $file_path."/dir_variation7";
+@mkdir($parent_dir_path);
+chmod($parent_dir_path, 0777);
+
+// create sub_dir
+$sub_dir_path = $parent_dir_path."/sub_dir";
+@mkdir($sub_dir_path);
+chmod($sub_dir_path, 0777);
+
+//create sub_sub_dir
+$child_dir_path = $sub_dir_path."/child_dir";
+@mkdir($child_dir_path);
+
+// remove the write and execute permisson from sub parent
+chmod($sub_dir_path, 0444);
+echo "-- After restricting 1st level parent directory --\n";
+$d = dir($child_dir_path); // try to open, expected failure
+var_dump( $d ); // dump it
+
+// remove the execute permisson from parent dir, allowing all permission for sub dir
+chmod($sub_dir_path, 0777); // all permisson to sub dir
+chmod($parent_dir_path, 0666); // restricting parent directory
+echo "-- After restricting parent directory --\n";
+$d = dir($child_dir_path); // try to open, expected failure
+var_dump( $d ); // dump it
+
+echo "Done";
+?>
+--CLEAN--
+<?php
+$file_path = dirname(__FILE__);
+$parent_dir_path = $file_path."/dir_variation7";
+$sub_dir_path = $parent_dir_path."/sub_dir";
+$child_dir_path = $sub_dir_path."/child_dir";
+
+// changing permissions for each temporary directory to delete them
+chmod($parent_dir_path, 0777);
+chmod($sub_dir_path, 0777);
+chmod($child_dir_path, 0777);
+
+rmdir($child_dir_path);
+rmdir($sub_dir_path);
+rmdir($parent_dir_path);
+?>
+--EXPECTF--
+*** Testing dir() : remove execute permission from the parent dir ***
+-- After restricting 1st level parent directory --
+
+Warning: dir(%s/dir_variation7/sub_dir/child_dir): failed to open dir: Permission denied in %s on line %d
+bool(false)
+-- After restricting parent directory --
+
+Warning: dir(%s/dir_variation7/sub_dir/child_dir): failed to open dir: Permission denied in %s on line %d
+bool(false)
+Done
\ No newline at end of file
diff --git a/ext/standard/tests/dir/dir_variation8.phpt b/ext/standard/tests/dir/dir_variation8.phpt
new file mode 100644 (file)
index 0000000..4121750
--- /dev/null
@@ -0,0 +1,63 @@
+--TEST--
+Test dir() function : usage variations - checking with wildcard characters
+--FILE--
+<?php
+/* 
+ * Prototype  : object dir(string $directory[, resource $context])
+ * Description: Directory class with properties, handle and class and methods read, rewind and close
+ * Source code: ext/standard/dir.c
+ */
+
+/* 
+ * Create more than one temporary directory & subdirectory and check if dir() function can open 
+ * those directories when wildcard characters are used to refer to them.
+ */
+
+echo "*** Testing dir() : checking with wildcard characters ***\n";
+
+// create the temporary directories
+$file_path = dirname(__FILE__);
+$dir_path = $file_path."/dir_variation81";
+$sub_dir_path = $dir_path."/sub_dir1";
+
+@mkdir($dir_path1);
+@mkdir($sub_dir_path);
+
+/* with different wildcard characters */
+
+echo "-- wildcard = '*' --\n"; 
+var_dump( dir($file_path."/dir_var*") );
+var_dump( dir($file_path."/*") );
+
+echo "-- wildcard = '?' --\n";
+var_dump( dir($dir_path."/sub_dir?") );
+var_dump( dir($dir_path."/sub?dir1") );
+
+echo "Done";
+?>
+--CLEAN--
+<?php
+$file_path = dirname(__FILE__);
+$dir_path = $file_path."/dir_variation81";
+$sub_dir_path = $dir_path."/sub_dir1";
+
+rmdir($dir_path1);
+rmdir($dir_path2);
+?>
+--EXPECTF--
+*** Testing dir() : checking with wildcard characters ***
+-- wildcard = '*' --
+
+Warning: dir(%s/dir_var*): failed to open dir: %s in %s on line %d
+bool(false)
+
+Warning: dir(%s/*): failed to open dir: %s in %s on line %d
+bool(false)
+-- wildcard = '?' --
+
+Warning: dir(%s/dir_variation81/sub_dir?): failed to open dir: %s in %s on line %d
+bool(false)
+
+Warning: dir(%s/dir_variation81/sub?dir1): failed to open dir: %s in %s on line %d
+bool(false)
+Done
diff --git a/ext/standard/tests/dir/dir_variation9.phpt b/ext/standard/tests/dir/dir_variation9.phpt
new file mode 100644 (file)
index 0000000..22f3d5b
--- /dev/null
@@ -0,0 +1,111 @@
+--TEST--
+Test dir() function : usage variations - relative valid and invalid paths
+--FILE--
+<?php
+/* 
+ * Prototype  : object dir(string $directory[, resource $context])
+ * Description: Directory class with properties, handle and class and methods read, rewind and close
+ * Source code: ext/standard/dir.c
+ */
+
+/* 
+ * Checking the behavior of dir() function by passing directories which
+ * have valid and invalid relative path.
+ */
+
+echo "*** Testing dir() : checking with valid and invalid paths ***\n";
+
+/* create the temporary directories */
+
+$file_path = dirname(__FILE__);
+
+// directory dir_variation91 with one sub-directory sub_dir11 and sub-sub-directory sub_dir111
+$dir_path1 = $file_path."/dir_variation91";
+$sub_dir11 = $dir_path1."/sub_dir11";
+$sub_dir111 = $sub_dir11."/sub_dir111";
+
+// directory dir_variation92 with one sub-directory sub_dir21
+$dir_path2 = $file_path."/dir_variation92";
+$sub_dir21 = $dir_path2."/sub_dir21";
+
+@mkdir($dir_path1);
+@mkdir($dir_path2);
+@mkdir($sub_dir11);
+@mkdir($sub_dir111);
+@mkdir($sub_dir21);
+
+// open the directory with valid paths
+echo "\n-- With valid paths --\n";
+var_dump( dir("$dir_path1/sub_dir11/sub_dir111/..") );
+var_dump( dir("$dir_path2/sub_dir21/../../dir_variation91") );
+var_dump( dir("$dir_path2/sub_dir21/../../dir_variation91/sub_dir11/..") );
+var_dump( dir("$dir_path1/sub_dir11/sub_dir111/../../../dir_variation92/sub_dir21/..") );
+
+// open the directory with invalid path
+echo "\n-- With invalid paths --\n";
+var_dump( dir("$dir_path1/sub_dir12/sub_dir111/..") );
+var_dump( dir("$dir_path2/sub_dir21/../dir_variation91") );
+var_dump( dir("$dir_path2/sub_dir21/../../dir_variation91/sub_dir12/..") );
+var_dump( dir("$dir_path1/sub_dir11/sub_dir111/../../dir_variation92/sub_dir21/..") );
+
+echo "Done";
+?>
+--CLEAN--
+<?php
+$file_path = dirname(__FILE__);
+
+$dir_path1 = $file_path."/dir_variation91";
+$sub_dir11 = $dir_path1."/sub_dir11";
+$sub_dir111 = $sub_dir11."/sub_dir111";
+$dir_path2 = $file_path."/dir_variation92";
+$sub_dir21 = $dir_path2."/sub_dir21";
+
+rmdir($sub_dir21);
+rmdir($sub_dir111);
+rmdir($sub_dir11);
+rmdir($dir_path1);
+rmdir($dir_path2);
+?>
+--EXPECTF--
+*** Testing dir() : checking with valid and invalid paths ***
+
+-- With valid paths --
+object(Directory)#%d (2) {
+  ["path"]=>
+  string(%d) "%s/dir_variation91/sub_dir11/sub_dir111/.."
+  ["handle"]=>
+  resource(%d) of type (stream)
+}
+object(Directory)#%d (2) {
+  ["path"]=>
+  string(%d) "%s/dir_variation92/sub_dir21/../../dir_variation91"
+  ["handle"]=>
+  resource(%d) of type (stream)
+}
+object(Directory)#%d (2) {
+  ["path"]=>
+  string(%d) "%s/dir_variation92/sub_dir21/../../dir_variation91/sub_dir11/.."
+  ["handle"]=>
+  resource(%d) of type (stream)
+}
+object(Directory)#%d (2) {
+  ["path"]=>
+  string(%d) "%s/dir_variation91/sub_dir11/sub_dir111/../../../dir_variation92/sub_dir21/.."
+  ["handle"]=>
+  resource(%d) of type (stream)
+}
+
+-- With invalid paths --
+
+Warning: dir(%s/dir_variation91/sub_dir12/sub_dir111/..): failed to open dir: %s in %s on line %d
+bool(false)
+
+Warning: dir(%s/dir_variation92/sub_dir21/../dir_variation91): failed to open dir: %s in %s on line %d
+bool(false)
+
+Warning: dir(%s/dir_variation92/sub_dir21/../../dir_variation91/sub_dir12/..): failed to open dir: %s in %s on line %d
+bool(false)
+
+Warning: dir(%s/dir_variation91/sub_dir11/sub_dir111/../../dir_variation92/sub_dir21/..): failed to open dir: %s in %s on line %d
+bool(false)
+Done