]> granicus.if.org Git - php/commitdiff
- new tests for readdir() function
authorJosie Messa <jmessa@php.net>
Tue, 4 Mar 2008 17:09:24 +0000 (17:09 +0000)
committerJosie Messa <jmessa@php.net>
Tue, 4 Mar 2008 17:09:24 +0000 (17:09 +0000)
ext/standard/tests/dir/readdir_basic.phpt [new file with mode: 0644]
ext/standard/tests/dir/readdir_error.phpt [new file with mode: 0644]
ext/standard/tests/dir/readdir_variation1.phpt [new file with mode: 0644]
ext/standard/tests/dir/readdir_variation2.phpt [new file with mode: 0644]
ext/standard/tests/dir/readdir_variation3.phpt [new file with mode: 0644]
ext/standard/tests/dir/readdir_variation4.phpt [new file with mode: 0644]
ext/standard/tests/dir/readdir_variation5.phpt [new file with mode: 0644]
ext/standard/tests/dir/readdir_variation6.phpt [new file with mode: 0644]

diff --git a/ext/standard/tests/dir/readdir_basic.phpt b/ext/standard/tests/dir/readdir_basic.phpt
new file mode 100644 (file)
index 0000000..7afdc42
--- /dev/null
@@ -0,0 +1,63 @@
+--TEST--
+Test readdir() function : basic functionality 
+--FILE--
+<?php
+/* Prototype  : string readdir([resource $dir_handle])
+ * Description: Read directory entry from dir_handle 
+ * Source code: ext/standard/dir.C
+ */
+
+/*
+ * Test basic functionality of readdir()
+ */
+
+echo "*** Testing readdir() : basic functionality ***\n";
+
+// include the file.inc for Function: function create_files()
+chdir(dirname(__FILE__));
+include(dirname(__FILE__)."/../file/file.inc");
+
+$path = dirname(__FILE__) . '/readdir_basic';
+mkdir($path);
+create_files($path, 3);
+
+echo "\n-- Call readdir() with \$path argument --\n";
+var_dump($dh = opendir($path));
+while( FALSE !== ($file = readdir($dh)) ) {
+       var_dump($file);
+}
+
+echo "\n-- Call readdir() without \$path argument --\n";
+var_dump($dh = opendir($path));
+while( FALSE !== ( $file = readdir() ) ) {
+       var_dump($file);
+}
+
+delete_files($path, 3);
+closedir($dh);
+?>
+===DONE===
+--CLEAN--
+<?php
+$path = dirname(__FILE__) . '/readdir_basic';
+rmdir($path);
+?>
+--EXPECTF--
+*** Testing readdir() : basic functionality ***
+
+-- Call readdir() with $path argument --
+resource(%d) of type (stream)
+string(1) "."
+string(2) ".."
+string(9) "file1.tmp"
+string(9) "file2.tmp"
+string(9) "file3.tmp"
+
+-- Call readdir() without $path argument --
+resource(%d) of type (stream)
+string(1) "."
+string(2) ".."
+string(9) "file1.tmp"
+string(9) "file2.tmp"
+string(9) "file3.tmp"
+===DONE===
diff --git a/ext/standard/tests/dir/readdir_error.phpt b/ext/standard/tests/dir/readdir_error.phpt
new file mode 100644 (file)
index 0000000..4b4011a
--- /dev/null
@@ -0,0 +1,43 @@
+--TEST--
+Test readdir() function : error conditions - Incorrect number of args
+--FILE--
+<?php
+/* Prototype  : string readdir([resource $dir_handle])
+ * Description: Read directory entry from dir_handle 
+ * Source code: ext/standard/dir.c
+ */
+
+/*
+ * Pass incorrect number of arguments to readdir() to test behaviour
+ */
+
+echo "*** Testing readdir() : error conditions ***\n";
+
+
+//Test readdir with one more than the expected number of arguments
+echo "\n-- Testing readdir() function with more than expected no. of arguments --\n";
+
+$path = dirname(__FILE__) . "/readdir_error";
+mkdir($path);
+$dir_handle = opendir($path);
+$extra_arg = 10;
+
+var_dump( readdir($dir_handle, $extra_arg) );
+
+// close the handle so can remove dir in CLEAN section
+closedir($dir_handle);
+?>
+===DONE===
+--CLEAN--
+<?php
+$path = dirname(__FILE__) . "/readdir_error";
+rmdir($path);
+?> 
+--EXPECTF--
+*** Testing readdir() : error conditions ***
+
+-- Testing readdir() function with more than expected no. of arguments --
+
+Warning: Wrong parameter count for readdir() in %s on line %d
+NULL
+===DONE===
diff --git a/ext/standard/tests/dir/readdir_variation1.phpt b/ext/standard/tests/dir/readdir_variation1.phpt
new file mode 100644 (file)
index 0000000..b9cd85b
--- /dev/null
@@ -0,0 +1,210 @@
+--TEST--
+Test readdir() function : usage variations - different data types as $dir_handle arg
+--FILE--
+<?php
+/* Prototype  : string readdir([resource $dir_handle])
+ * Description: Read directory entry from dir_handle 
+ * Source code: ext/standard/dir.c
+ */
+
+/*
+ * Pass different data types as $dir_handle argument to readdir() to test behaviour
+ */
+
+echo "*** Testing readdir() : usage variations ***\n";
+
+//get an unset variable
+$unset_var = 10;
+unset ($unset_var);
+
+// get a class
+class classA
+{
+       public function __toString() {
+               return "Class A object";
+       }
+}
+
+// heredoc string
+$heredoc = <<<EOT
+hello world
+EOT;
+
+// unexpected values to be passed to $dir_handle argument
+$inputs = array(
+
+       // int data
+/*1*/  0,
+       1,
+       12345,
+       -2345,
+
+       // float data
+/*5*/  10.5,
+       -10.5,
+       12.3456789000e10,
+       12.3456789000E-10,
+       .5,
+
+       // null data
+/*10*/ NULL,
+       null,
+
+       // boolean data
+/*12*/ true,
+       false,
+       TRUE,
+       FALSE,
+       
+       // empty data
+/*16*/ "",
+       '',
+       array(),
+
+       // string data
+/*19*/ "string",
+       'string',
+       $heredoc,
+       
+       // object data
+/*22*/ new classA(),
+
+       // undefined data
+/*23*/ @$undefined_var,
+
+       // unset data
+/*24*/ @$unset_var,
+);
+
+// loop through each element of $inputs to check the behavior of readdir()
+$iterator = 1;
+foreach($inputs as $input) {
+       echo "\n-- Iteration $iterator --\n";
+       var_dump( readdir($input) );
+       $iterator++;
+};
+?>
+===DONE===
+--EXPECTF--
+*** Testing readdir() : usage variations ***
+
+-- Iteration 1 --
+
+Warning: readdir(): supplied argument is not a valid Directory resource in %s on line %d
+bool(false)
+
+-- Iteration 2 --
+
+Warning: readdir(): supplied argument is not a valid Directory resource in %s on line %d
+bool(false)
+
+-- Iteration 3 --
+
+Warning: readdir(): supplied argument is not a valid Directory resource in %s on line %d
+bool(false)
+
+-- Iteration 4 --
+
+Warning: readdir(): supplied argument is not a valid Directory resource in %s on line %d
+bool(false)
+
+-- Iteration 5 --
+
+Warning: readdir(): supplied argument is not a valid Directory resource in %s on line %d
+bool(false)
+
+-- Iteration 6 --
+
+Warning: readdir(): supplied argument is not a valid Directory resource in %s on line %d
+bool(false)
+
+-- Iteration 7 --
+
+Warning: readdir(): supplied argument is not a valid Directory resource in %s on line %d
+bool(false)
+
+-- Iteration 8 --
+
+Warning: readdir(): supplied argument is not a valid Directory resource in %s on line %d
+bool(false)
+
+-- Iteration 9 --
+
+Warning: readdir(): supplied argument is not a valid Directory resource in %s on line %d
+bool(false)
+
+-- Iteration 10 --
+
+Warning: readdir(): supplied argument is not a valid Directory resource in %s on line %d
+bool(false)
+
+-- Iteration 11 --
+
+Warning: readdir(): supplied argument is not a valid Directory resource in %s on line %d
+bool(false)
+
+-- Iteration 12 --
+
+Warning: readdir(): supplied argument is not a valid Directory resource in %s on line %d
+bool(false)
+
+-- Iteration 13 --
+
+Warning: readdir(): supplied argument is not a valid Directory resource in %s on line %d
+bool(false)
+
+-- Iteration 14 --
+
+Warning: readdir(): supplied argument is not a valid Directory resource in %s on line %d
+bool(false)
+
+-- Iteration 15 --
+
+Warning: readdir(): supplied argument is not a valid Directory resource in %s on line %d
+bool(false)
+
+-- Iteration 16 --
+
+Warning: readdir(): supplied argument is not a valid Directory resource in %s on line %d
+bool(false)
+
+-- Iteration 17 --
+
+Warning: readdir(): supplied argument is not a valid Directory resource in %s on line %d
+bool(false)
+
+-- Iteration 18 --
+
+Warning: readdir(): supplied argument is not a valid Directory resource in %s on line %d
+bool(false)
+
+-- Iteration 19 --
+
+Warning: readdir(): supplied argument is not a valid Directory resource in %s on line %d
+bool(false)
+
+-- Iteration 20 --
+
+Warning: readdir(): supplied argument is not a valid Directory resource in %s on line %d
+bool(false)
+
+-- Iteration 21 --
+
+Warning: readdir(): supplied argument is not a valid Directory resource in %s on line %d
+bool(false)
+
+-- Iteration 22 --
+
+Warning: readdir(): supplied argument is not a valid Directory resource in %s on line %d
+bool(false)
+
+-- Iteration 23 --
+
+Warning: readdir(): supplied argument is not a valid Directory resource in %s on line %d
+bool(false)
+
+-- Iteration 24 --
+
+Warning: readdir(): supplied argument is not a valid Directory resource in %s on line %d
+bool(false)
+===DONE===
diff --git a/ext/standard/tests/dir/readdir_variation2.phpt b/ext/standard/tests/dir/readdir_variation2.phpt
new file mode 100644 (file)
index 0000000..6809ac2
--- /dev/null
@@ -0,0 +1,39 @@
+--TEST--
+Test readdir() function : usage variations - empty directories
+--FILE--
+<?php
+/* Prototype  : string readdir([resource $dir_handle])
+ * Description: Read directory entry from dir_handle 
+ * Source code: ext/standard/dir.c
+ */
+
+/*
+ * Pass readdir() a directory handle pointing to an empty directory to test behaviour
+ */
+
+echo "*** Testing readdir() : usage variations ***\n";
+
+$path = dirname(__FILE__) . '/readdir_variation2';
+mkdir($path);
+$dir_handle = opendir($path);
+
+echo "\n-- Pass an empty directory to readdir() --\n";
+while(FALSE !== ($file = readdir($dir_handle))){
+       var_dump($file);
+}
+
+closedir($dir_handle);
+?>
+===DONE===
+--CLEAN--
+<?php
+$path = dirname(__FILE__) . '/readdir_variation2';
+rmdir($path);
+?>
+--EXPECTF--
+*** Testing readdir() : usage variations ***
+
+-- Pass an empty directory to readdir() --
+string(1) "."
+string(2) ".."
+===DONE===
diff --git a/ext/standard/tests/dir/readdir_variation3.phpt b/ext/standard/tests/dir/readdir_variation3.phpt
new file mode 100644 (file)
index 0000000..4e73e74
--- /dev/null
@@ -0,0 +1,68 @@
+--TEST--
+Test readdir() function : usage variations - sub-directories 
+--FILE--
+<?php
+/* Prototype  : string readdir([resource $dir_handle])
+ * Description: Read directory entry from dir_handle 
+ * Source code: ext/standard/dir.c
+ */
+
+/*
+ * Pass a directory handle pointing to a directory that has a sub-directory
+ * to test behaviour of readdir()
+ */
+
+echo "*** Testing readdir() : usage variations ***\n";
+
+// include the file.inc for Function: function create_files()
+chdir(dirname(__FILE__));
+include(dirname(__FILE__)."/../file/file.inc");
+
+$path_top = dirname(__FILE__) . '/readdir_variation3';
+$path_sub = $path_top . '/sub_folder';
+mkdir($path_top);
+mkdir($path_sub);
+
+create_files($path_top, 2);
+create_files($path_sub, 2);
+
+$dir_handle = opendir($path_top);
+while(FALSE !== ($file = readdir($dir_handle))) {
+       
+       // different OS order files differently so will
+       // store file names into an array so can use sorted in expected output
+       $contents[] = $file;
+}
+
+// more important to check that all contents are present than order they are returned in
+sort($contents);
+var_dump($contents);
+
+delete_files($path_top, 2);
+delete_files($path_sub, 2);
+
+closedir($dir_handle);
+?>
+===DONE===
+--CLEAN--
+<?php
+$path_top = dirname(__FILE__) . '/readdir_variation3';
+$path_sub = $path_top . '/sub_folder';
+rmdir($path_sub);
+rmdir($path_top);
+?>
+--EXPECTF--
+*** Testing readdir() : usage variations ***
+array(5) {
+  [0]=>
+  string(1) "."
+  [1]=>
+  string(2) ".."
+  [2]=>
+  string(9) "file1.tmp"
+  [3]=>
+  string(9) "file2.tmp"
+  [4]=>
+  string(10) "sub_folder"
+}
+===DONE===
diff --git a/ext/standard/tests/dir/readdir_variation4.phpt b/ext/standard/tests/dir/readdir_variation4.phpt
new file mode 100644 (file)
index 0000000..4c22de8
--- /dev/null
@@ -0,0 +1,178 @@
+--TEST--
+Test readdir() function : usage variations - different file names
+--FILE--
+<?php
+/* Prototype  : string readdir([resource $dir_handle])
+ * Description: Read directory entry from dir_handle 
+ * Source code: ext/standard/dir.c
+ */
+
+/*
+ * Pass a directory handle pointing to a directory that contains 
+ * files with different file names to test how readdir() reads them
+ */
+
+echo "*** Testing readdir() : usage variations ***\n";
+
+$dir_path = dirname(__FILE__) . "/readdir_variation4/";
+mkdir($dir_path);
+
+// heredoc string
+$heredoc = <<<EOT
+hd_file
+EOT;
+
+$inputs = array(
+
+       // int data
+/*1*/  0,
+       1,
+       12345,
+       -2345,
+
+       // float data
+/*5*/  10.5,
+       -10.5,
+       12.3456789000e10,
+       12.3456789000E-10,
+       .5,
+       
+       // empty data
+/*10*/ "",
+       array(),
+
+       // string data
+/*12*/ "double_file",
+       'single_file',
+       $heredoc,
+);
+
+$iterator = 1;
+foreach($inputs as $key => $input) {
+       echo "\n-- Iteration $iterator --\n";
+       $handle = "fp{$iterator}";
+       var_dump( $$handle = fopen($dir_path . $input . '.tmp', 'w') );
+       var_dump( fwrite($$handle, $key));
+       fclose($$handle);
+       $iterator++;
+};
+
+echo "\n-- Call to readdir() --\n";
+$dir_handle = opendir($dir_path);
+while(FALSE !== ($file = readdir($dir_handle))){
+       
+       // different OS order files differently so will
+       // store file names into an array so can use sorted in expected output
+       $contents[] = $file;
+       
+       // remove files while going through directory
+       @unlink($dir_path . $file);
+}
+
+// more important to check that all contents are present than order they are returned in
+sort($contents);
+var_dump($contents);
+
+closedir($dir_handle);
+?>
+===DONE===
+--CLEAN--
+<?php
+$dir_path = dirname(__FILE__) . "/readdir_variation4/";
+rmdir($dir_path);
+?>
+--EXPECTF--
+*** Testing readdir() : usage variations ***
+
+-- Iteration 1 --
+resource(%d) of type (stream)
+int(1)
+
+-- Iteration 2 --
+resource(%d) of type (stream)
+int(1)
+
+-- Iteration 3 --
+resource(%d) of type (stream)
+int(1)
+
+-- Iteration 4 --
+resource(%d) of type (stream)
+int(1)
+
+-- Iteration 5 --
+resource(%d) of type (stream)
+int(1)
+
+-- Iteration 6 --
+resource(%d) of type (stream)
+int(1)
+
+-- Iteration 7 --
+resource(%d) of type (stream)
+int(1)
+
+-- Iteration 8 --
+resource(%d) of type (stream)
+int(1)
+
+-- Iteration 9 --
+resource(%d) of type (stream)
+int(1)
+
+-- Iteration 10 --
+resource(%d) of type (stream)
+int(1)
+
+-- Iteration 11 --
+resource(%d) of type (stream)
+int(2)
+
+-- Iteration 12 --
+resource(%d) of type (stream)
+int(2)
+
+-- Iteration 13 --
+resource(%d) of type (stream)
+int(2)
+
+-- Iteration 14 --
+resource(%d) of type (stream)
+int(2)
+
+-- Call to readdir() --
+array(16) {
+  [0]=>
+  string(9) "-10.5.tmp"
+  [1]=>
+  string(9) "-2345.tmp"
+  [2]=>
+  string(1) "."
+  [3]=>
+  string(2) ".."
+  [4]=>
+  string(4) ".tmp"
+  [5]=>
+  string(7) "0.5.tmp"
+  [6]=>
+  string(5) "0.tmp"
+  [7]=>
+  string(17) "1.23456789E-9.tmp"
+  [8]=>
+  string(5) "1.tmp"
+  [9]=>
+  string(8) "10.5.tmp"
+  [10]=>
+  string(9) "12345.tmp"
+  [11]=>
+  string(16) "123456789000.tmp"
+  [12]=>
+  string(9) "Array.tmp"
+  [13]=>
+  string(15) "double_file.tmp"
+  [14]=>
+  string(11) "hd_file.tmp"
+  [15]=>
+  string(15) "single_file.tmp"
+}
+===DONE===
diff --git a/ext/standard/tests/dir/readdir_variation5.phpt b/ext/standard/tests/dir/readdir_variation5.phpt
new file mode 100644 (file)
index 0000000..8c12f13
--- /dev/null
@@ -0,0 +1,144 @@
+--TEST--
+Test readdir() function : usage variations - different permissions
+--SKIPIF--
+<?php
+if( substr(PHP_OS, 0, 3) == 'WIN') {
+  die('skip Not for Windows');
+}
+// Skip if being run by root (files are always readable, writeable and executable)
+$filename = dirname(__FILE__)."/readdir_root_check.tmp";
+$fp = fopen($filename, 'w');
+fclose($fp);
+if(fileowner($filename) == 0) {
+        unlink ($filename);
+        die('skip...cannot be run as root\n');
+}
+unlink($filename);
+?>
+--FILE--
+<?php
+/* Prototype  : string readdir([resource $dir_handle])
+ * Description: Read directory entry from dir_handle 
+ * Source code: ext/standard/dir.c
+ */
+
+/*
+ * Open a directory with different premissions then try to read it
+ * to test behaviour of readdir()
+ */
+
+echo "*** Testing readdir() : usage variations ***\n";
+
+// create the temporary directory
+$dir_path = dirname(__FILE__) . "/readdir_variation5";
+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.
+$iterator = 1;
+foreach($permission_values as $perm) {
+       echo "\n-- Iteration $iterator --\n";
+       
+       if (is_dir($dir_path)) {
+               chmod ($dir_path, 0777); // change dir permission to allow all operation
+               rmdir ($dir_path);
+       }
+       mkdir($dir_path);
+
+       // change the dir permisson to test dir on it
+       var_dump( chmod($dir_path, $perm) );
+       var_dump($dh = opendir($dir_path));
+
+       echo "-- Calling readdir() --\n";
+       var_dump(readdir($dh));
+
+       closedir($dh);
+       $iterator++;
+}
+?>
+===DONE===
+--CLEAN--
+<?php
+$dir_path = dirname(__FILE__) . "/readdir_variation5";
+rmdir($dir_path);
+?>
+--EXPECTF--
+*** Testing readdir() : usage variations ***
+
+-- Iteration 1 --
+bool(true)
+resource(%d) of type (stream)
+-- Calling readdir() --
+string(%d) "%s"
+
+-- Iteration 2 --
+bool(true)
+resource(%d) of type (stream)
+-- Calling readdir() --
+string(%d) "%s"
+
+-- Iteration 3 --
+bool(true)
+resource(%d) of type (stream)
+-- Calling readdir() --
+string(%d) "%s"
+
+-- Iteration 4 --
+bool(true)
+resource(%d) of type (stream)
+-- Calling readdir() --
+string(%d) "%s"
+
+-- Iteration 5 --
+bool(true)
+resource(%d) of type (stream)
+-- Calling readdir() --
+string(%d) "%s"
+
+-- Iteration 6 --
+bool(true)
+resource(%d) of type (stream)
+-- Calling readdir() --
+string(%d) "%s"
+
+-- Iteration 7 --
+bool(true)
+resource(%d) of type (stream)
+-- Calling readdir() --
+string(%d) "%s"
+
+-- Iteration 8 --
+bool(true)
+resource(%d) of type (stream)
+-- Calling readdir() --
+string(%d) "%s"
+
+-- Iteration 9 --
+bool(true)
+resource(%d) of type (stream)
+-- Calling readdir() --
+string(%d) "%s"
+
+-- Iteration 10 --
+bool(true)
+resource(%d) of type (stream)
+-- Calling readdir() --
+string(%d) "%s"
+===DONE===
diff --git a/ext/standard/tests/dir/readdir_variation6.phpt b/ext/standard/tests/dir/readdir_variation6.phpt
new file mode 100644 (file)
index 0000000..2c76650
--- /dev/null
@@ -0,0 +1,70 @@
+--TEST--
+Test readdir() function : usage variations - operate on previously opened directory
+--FILE--
+<?php
+/* Prototype  : string readdir([resource $dir_handle])
+ * Description: Read directory entry from dir_handle 
+ * Source code: ext/standard/dir.c
+ */
+
+/*
+ * Open two directory handles on the same directory and pass both
+ * to readdir() to test behaviour
+ */
+
+echo "*** Testing readdir() : usage variations ***\n";
+
+// include the file.inc for Function: function create_files()
+include( dirname(__FILE__)."/../file/file.inc");
+
+// create the temporary directory
+$dir_path = dirname(__FILE__) . "/readdir_variation6";
+mkdir($dir_path);
+
+// create files within the temporary directory
+create_files($dir_path, 3, "alphanumeric", 0755, 1, "w", "readdir_variation6");
+
+// open the directory
+$dir_handle1 = opendir($dir_path);
+
+// open the same directory again without closing it
+opendir($dir_path);
+
+echo "\n-- Reading Directory Contents with Previous Handle --\n";
+while (FALSE !== ($file = readdir($dir_handle1))) {
+       var_dump($file);
+}
+
+echo "\n-- Reading Directory Contents with Current Handle (no arguments supplied) --\n";
+while (FALSE !== ($file = readdir())) {
+       var_dump($file);
+}
+
+// delete temporary files
+delete_files($dir_path, 3, "readdir_variation6");
+closedir($dir_handle1);
+closedir();
+?>
+===DONE===
+--CLEAN--
+<?php
+$dir_path = dirname(__FILE__) . "/readdir_variation6";
+rmdir($dir_path);
+?>
+--EXPECTF--
+*** Testing readdir() : usage variations ***
+
+-- Reading Directory Contents with Previous Handle --
+string(1) "."
+string(2) ".."
+string(23) "readdir_variation61.tmp"
+string(23) "readdir_variation62.tmp"
+string(23) "readdir_variation63.tmp"
+
+-- Reading Directory Contents with Current Handle (no arguments supplied) --
+string(1) "."
+string(2) ".."
+string(23) "readdir_variation61.tmp"
+string(23) "readdir_variation62.tmp"
+string(23) "readdir_variation63.tmp"
+===DONE===