1--TEST-- 2Test touch() function : variation: various valid and invalid paths 3--CREDITS-- 4Dave Kelsey <d_kelsey@uk.ibm.com> 5--SKIPIF-- 6<?php 7if (substr(PHP_OS, 0, 3) != 'WIN') { 8 die('skip.. only for Windows'); 9} 10?> 11--FILE-- 12<?php 13$workDir = "touchVar5.tmp"; 14$subDirOrFile = "aSubDirOrFile"; 15$cwd = __DIR__; 16chdir($cwd); 17if (!mkdir($cwd . '/' . $workDir)) die("cannot create directory $workDir"); 18 19$paths = array( 20 // relative 21 $workDir.'/'.$subDirOrFile, 22 './'.$workDir.'/'.$subDirOrFile, 23 $workDir.'/../'.$workDir.'/'.$subDirOrFile, 24 25 // relative bad path (note p8 msgs differ) 26 $workDir.'/../BADDIR/'.$subDirOrFile, 27 'BADDIR/'.$subDirOrFile, 28 29 //absolute 30 $cwd.'/'.$workDir.'/'.$subDirOrFile, 31 $cwd.'/./'.$workDir.'/'.$subDirOrFile, 32 $cwd.'/'.$workDir.'/../'.$workDir.'/'.$subDirOrFile, 33 34 //absolute bad path (note p8 msgs differ) 35 $cwd.'/BADDIR/'.$subDirOrFile, 36 37 //trailing separators 38 $workDir.'/'.$subDirOrFile.'/', 39 $cwd.'/'.$workDir.'/'.$subDirOrFile.'/', 40 41 // multiple separators 42 $workDir.'//'.$subDirOrFile, 43 $cwd.'//'.$workDir.'//'.$subDirOrFile, 44 45 ); 46 47echo "*** Testing touch() : variation ***\n"; 48 49echo "\n*** testing nonexisting paths ***\n"; 50test_nonexisting($paths); 51 52echo "\n*** testing existing files ***\n"; 53test_existing($paths, false); 54 55echo "\n*** testing existing directories ***\n"; 56test_existing($paths, true); 57 58 59rmdir($workDir); 60 61 62 63function test_nonexisting($paths) { 64 foreach($paths as $path) { 65 echo "--- testing $path ---\n"; 66 67 if (is_dir($path) || is_file($path)) { 68 echo "FAILED: $path - exists\n"; 69 } 70 else { 71 $res = touch($path); 72 if ($res === true) { 73 // something was created 74 if (file_exists($path)) { 75 // something found 76 if (is_dir($path)) { 77 echo "FAILED: $path - unexpected directory\n"; 78 } 79 else { 80 echo "PASSED: $path - created\n"; 81 unlink($path); 82 } 83 } 84 else { 85 // nothing found 86 echo "FAILED: $path - touch returned true, nothing there\n"; 87 } 88 } 89 else { 90 // nothing created 91 if (file_exists($path)) { 92 //something found 93 echo "FAILED: $path - touch returned false, something there\n"; 94 if (is_dir($path)) { 95 rmdir($path); 96 } 97 else { 98 unlink($path); 99 } 100 } 101 } 102 } 103 } 104} 105 106function test_existing($paths, $are_dirs) { 107 foreach($paths as $path) { 108 if ($are_dirs) { 109 $res = @mkdir($path); 110 if ($res == true) { 111 test_path($path); 112 rmdir($path); 113 } 114 } 115 else { 116 $h = @fopen($path,"w"); 117 if ($h !== false) { 118 fclose($h); 119 test_path($path); 120 unlink($path); 121 } 122 } 123 } 124} 125 126 127function test_path($path) { 128 echo "--- testing $path ---\n"; 129 $org_atime = get_atime($path); 130 clearstatcache(); 131 $res = touch($path,0,0); 132 $next_atime = get_atime($path); 133 if ($next_atime == $org_atime) { 134 echo "FAILED: $path - access time not changed\n"; 135 } 136 else { 137 echo "PASSED: $path - touched\n"; 138 } 139} 140 141function get_atime($path) { 142 $temp = stat($path); 143 return $temp['atime']; 144} 145 146 147?> 148--EXPECTF-- 149*** Testing touch() : variation *** 150 151*** testing nonexisting paths *** 152--- testing touchVar5.tmp/aSubDirOrFile --- 153PASSED: touchVar5.tmp/aSubDirOrFile - created 154--- testing ./touchVar5.tmp/aSubDirOrFile --- 155PASSED: ./touchVar5.tmp/aSubDirOrFile - created 156--- testing touchVar5.tmp/../touchVar5.tmp/aSubDirOrFile --- 157PASSED: touchVar5.tmp/../touchVar5.tmp/aSubDirOrFile - created 158--- testing touchVar5.tmp/../BADDIR/aSubDirOrFile --- 159 160Warning: touch(): Unable to create file touchVar5.tmp/../BADDIR/aSubDirOrFile because %s in %s on line %d 161--- testing BADDIR/aSubDirOrFile --- 162 163Warning: touch(): Unable to create file BADDIR/aSubDirOrFile because %s in %s on line %d 164--- testing %s/touchVar5.tmp/aSubDirOrFile --- 165PASSED: %s/touchVar5.tmp/aSubDirOrFile - created 166--- testing %s/./touchVar5.tmp/aSubDirOrFile --- 167PASSED: %s/./touchVar5.tmp/aSubDirOrFile - created 168--- testing %s/touchVar5.tmp/../touchVar5.tmp/aSubDirOrFile --- 169PASSED: %s/touchVar5.tmp/../touchVar5.tmp/aSubDirOrFile - created 170--- testing %s/BADDIR/aSubDirOrFile --- 171 172Warning: touch(): Unable to create file %s/BADDIR/aSubDirOrFile because %s in %s on line %d 173--- testing touchVar5.tmp/aSubDirOrFile/ --- 174 175Warning: touch(): Unable to create file touchVar5.tmp/aSubDirOrFile/ because %s in %s on line %d 176--- testing %s/touchVar5.tmp/aSubDirOrFile/ --- 177 178Warning: touch(): Unable to create file %s/touchVar5.tmp/aSubDirOrFile/ because %s in %s on line %d 179--- testing touchVar5.tmp//aSubDirOrFile --- 180PASSED: touchVar5.tmp//aSubDirOrFile - created 181--- testing %s//touchVar5.tmp//aSubDirOrFile --- 182PASSED: %s//touchVar5.tmp//aSubDirOrFile - created 183 184*** testing existing files *** 185--- testing touchVar5.tmp/aSubDirOrFile --- 186PASSED: touchVar5.tmp/aSubDirOrFile - touched 187--- testing ./touchVar5.tmp/aSubDirOrFile --- 188PASSED: ./touchVar5.tmp/aSubDirOrFile - touched 189--- testing touchVar5.tmp/../touchVar5.tmp/aSubDirOrFile --- 190PASSED: touchVar5.tmp/../touchVar5.tmp/aSubDirOrFile - touched 191--- testing %s/touchVar5.tmp/aSubDirOrFile --- 192PASSED: %s/touchVar5.tmp/aSubDirOrFile - touched 193--- testing %s/./touchVar5.tmp/aSubDirOrFile --- 194PASSED: %s/./touchVar5.tmp/aSubDirOrFile - touched 195--- testing %s/touchVar5.tmp/../touchVar5.tmp/aSubDirOrFile --- 196PASSED: %s/touchVar5.tmp/../touchVar5.tmp/aSubDirOrFile - touched 197--- testing touchVar5.tmp//aSubDirOrFile --- 198PASSED: touchVar5.tmp//aSubDirOrFile - touched 199--- testing %s//touchVar5.tmp//aSubDirOrFile --- 200PASSED: %s//touchVar5.tmp//aSubDirOrFile - touched 201 202*** testing existing directories *** 203--- testing touchVar5.tmp/aSubDirOrFile --- 204PASSED: touchVar5.tmp/aSubDirOrFile - touched 205--- testing ./touchVar5.tmp/aSubDirOrFile --- 206PASSED: ./touchVar5.tmp/aSubDirOrFile - touched 207--- testing touchVar5.tmp/../touchVar5.tmp/aSubDirOrFile --- 208PASSED: touchVar5.tmp/../touchVar5.tmp/aSubDirOrFile - touched 209--- testing %s/touchVar5.tmp/aSubDirOrFile --- 210PASSED: %s/touchVar5.tmp/aSubDirOrFile - touched 211--- testing %s/./touchVar5.tmp/aSubDirOrFile --- 212PASSED: %s/./touchVar5.tmp/aSubDirOrFile - touched 213--- testing %s/touchVar5.tmp/../touchVar5.tmp/aSubDirOrFile --- 214PASSED: %s/touchVar5.tmp/../touchVar5.tmp/aSubDirOrFile - touched 215--- testing touchVar5.tmp/aSubDirOrFile/ --- 216PASSED: touchVar5.tmp/aSubDirOrFile/ - touched 217--- testing %s/touchVar5.tmp/aSubDirOrFile/ --- 218PASSED: %s/touchVar5.tmp/aSubDirOrFile/ - touched 219--- testing touchVar5.tmp//aSubDirOrFile --- 220PASSED: touchVar5.tmp//aSubDirOrFile - touched 221--- testing %s//touchVar5.tmp//aSubDirOrFile --- 222PASSED: %s//touchVar5.tmp//aSubDirOrFile - touched 223