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