Skip to content

Commit cf3a772

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 cf3a772

File tree

1 file changed

+76
-0
lines changed

1 file changed

+76
-0
lines changed
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
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+
foreach($names_arr as $i=>$prefix) {
29+
echo "-- Iteration $i --\n";
30+
try {
31+
$file_name = tempnam("$file_path", $prefix);
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+
--CLEAN--
53+
<?php
54+
$file_path = __DIR__."/tempnamVar9";
55+
if (file_exists($file_path)) {
56+
array_map('unlink', glob($file_path . "/*"));
57+
rmdir($file_path);
58+
}
59+
?>
60+
--EXPECTF--
61+
*** Testing tempnam() maximum prefix size ***
62+
-- Iteration 0 --
63+
File name is => begin_%rx{7}%r_end%r.{6}%r
64+
File name length is => 23
65+
-- Iteration 1 --
66+
File name is => begin_%rx{53}%r_end%r.{6}%r
67+
File name length is => 69
68+
-- Iteration 2 --
69+
File name is => begin_%rx{54}%r_en%r.{6}%r
70+
File name length is => 69
71+
-- Iteration 3 --
72+
File name is => begin_%rx{55}%r_e%r.{6}%r
73+
File name length is => 69
74+
-- Iteration 4 --
75+
File name is => begin_%rx{57}%r%r.{6}%r
76+
File name length is => 69

0 commit comments

Comments
 (0)