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