1--TEST--
2Test tempnam() function: usage variations - obscure prefixes
3--SKIPIF--
4<?php
5if(substr(PHP_OS, 0, 3) != "WIN")
6    die("skip run only on Windows");
7?>
8--CONFLICTS--
9obscure_filename
10--FILE--
11<?php
12/* Passing invalid/non-existing args for $prefix */
13
14echo "*** Testing tempnam() with obscure prefixes ***\n";
15$file_path = __DIR__."/tempnamVar3";
16if (!mkdir($file_path)) {
17    echo "Failed, cannot create temp dir $filepath\n";
18    exit(1);
19}
20
21$file_path = realpath($file_path);
22
23/* An array of prefixes */
24$names_arr = array(
25    /* Valid args (casting)*/
26    -1,
27    TRUE,
28    FALSE,
29    "",
30    " ",
31    "\0",
32    /* Invalid args */
33    array(),
34
35    /* Valid args*/
36    /* prefix with path separator of a non existing directory*/
37    "/no/such/file/dir",
38    "php/php"
39);
40
41$res_arr = array(
42    /* Invalid args */
43    true,
44    true,
45    true,
46    true,
47    true,
48    true,
49    false,
50
51    /* prefix with path separator of a non existing directory*/
52    true,
53    true
54);
55
56for( $i=0; $i<count($names_arr); $i++ ) {
57    echo "-- Iteration $i --\n";
58    try {
59        $file_name = tempnam($file_path, $names_arr[$i]);
60    } catch (Error $e) {
61        echo $e->getMessage(), "\n";
62        continue;
63    }
64
65    /* creating the files in existing dir */
66    if (file_exists($file_name) && !$res_arr[$i]) {
67        echo "Failed\n";
68    }
69    if ($res_arr[$i]) {
70        $file_dir = dirname($file_name);
71        if (realpath($file_dir) == $file_path || realpath($file_dir . "\\") == $file_path) {
72            echo "OK\n";
73        } else {
74            echo "Failed, not created in the correct directory " . realpath($file_dir) . ' vs ' . $file_path ."\n";
75        }
76
77        if (!is_writable($file_name)) {
78            printf("%o\n", fileperms($file_name) );
79
80        }
81    } else {
82        echo "OK\n";
83    }
84    @unlink($file_name);
85}
86
87rmdir($file_path);
88?>
89--EXPECTF--
90*** Testing tempnam() with obscure prefixes ***
91-- Iteration 0 --
92OK
93-- Iteration 1 --
94OK
95-- Iteration 2 --
96OK
97-- Iteration 3 --
98OK
99-- Iteration 4 --
100
101Notice: tempnam(): file created in the system's temporary directory in %stempnam_variation3-win32.php on line %d
102Failed, not created in the correct directory %s vs %s
1030
104-- Iteration 5 --
105tempnam(): Argument #2 ($prefix) must not contain any null bytes
106-- Iteration 6 --
107tempnam(): Argument #2 ($prefix) must be of type string, array given
108-- Iteration 7 --
109OK
110-- Iteration 8 --
111OK
112