Skip to content

Commit ed77df4

Browse files
committed
Test tempnam prefix maximum size
Ensure the string is properly truncated when its length is greater than 63 characters.
1 parent 0442c2a commit ed77df4

File tree

1 file changed

+68
-0
lines changed

1 file changed

+68
-0
lines changed
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
--TEST--
2+
Test tempnam() function: usage variations - test prefix maximum size
3+
--SKIPIF--
4+
<?php
5+
if(substr(PHP_OS, 0, 3) == "WIN")
6+
die("skip Do not run on Windows");
7+
?>
8+
--FILE--
9+
<?php
10+
/* Testing the maximum prefix size */
11+
12+
echo "*** Testing tempnam() maximum prefix size ***\n";
13+
$file_path = __DIR__."/tempnamVar9";
14+
mkdir($file_path);
15+
16+
$pre_prefix = "begin_";
17+
$post_prefix = "_end";
18+
$fixed_length = strlen($pre_prefix) + strlen($post_prefix);
19+
/* An array of prefixes */
20+
$names_arr = array(
21+
$pre_prefix . str_repeat("x", 7) . $post_prefix,
22+
$pre_prefix . str_repeat("x", 63 - $fixed_length) . $post_prefix,
23+
$pre_prefix . str_repeat("x", 64 - $fixed_length) . $post_prefix,
24+
$pre_prefix . str_repeat("x", 65 - $fixed_length) . $post_prefix,
25+
$pre_prefix . str_repeat("x", 300) . $post_prefix,
26+
);
27+
28+
for ($i=0; $i<count($names_arr); $i++) {
29+
echo "-- Iteration $i --\n";
30+
try {
31+
$file_name = tempnam("$file_path", $names_arr[$i]);
32+
} catch (Error $e) {
33+
echo $e->getMessage(), "\n";
34+
continue;
35+
}
36+
37+
$base_name = basename($file_name);
38+
echo "File name is => ";
39+
print($base_name);
40+
echo "\n";
41+
echo "File name length is => ";
42+
print(strlen($base_name));
43+
echo "\n";
44+
45+
if (file_exists($file_name)) {
46+
unlink($file_name);
47+
}
48+
}
49+
rmdir($file_path);
50+
51+
?>
52+
--EXPECTF--
53+
*** Testing tempnam() maximum prefix size ***
54+
-- Iteration 0 --
55+
File name is => begin_%rx{7}%r_end%r.{6}%r
56+
File name length is => 23
57+
-- Iteration 1 --
58+
File name is => begin_%rx{53}%r_end%r.{6}%r
59+
File name length is => 69
60+
-- Iteration 2 --
61+
File name is => begin_%rx{54}%r_en%r.{6}%r
62+
File name length is => 69
63+
-- Iteration 3 --
64+
File name is => begin_%rx{55}%r_e%r.{6}%r
65+
File name length is => 69
66+
-- Iteration 4 --
67+
File name is => begin_%rx{57}%r%r.{6}%r
68+
File name length is => 69

0 commit comments

Comments
 (0)