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