1--TEST--
2Test readlink bc behaviour on Windows
3--DESCRIPTION--
4Checks readlink is backward-compatible with PHP-7.3 and below
5--SKIPIF--
6<?php
7if (substr(PHP_OS, 0, 3) != 'WIN') {
8    die('skip windows only test');
9}
10exec('fltmc', $output, $exitCode);
11if ($exitCode !== 0) {
12    die('skip administrator privileges required');
13}
14?>
15--FILE--
16<?php
17$tmpDir = __DIR__ . '\\mnt';
18mkdir($tmpDir);
19
20// mounted volume
21$volume = trim(exec('mountvol C: /L'));
22exec(sprintf('mountvol "%s" %s', $tmpDir, $volume));
23var_dump(readlink($tmpDir));
24exec(sprintf('mountvol "%s" /D', $tmpDir));
25
26mkdir($tmpDir . '\\test\\directory', 0777, true);
27chdir($tmpDir . '\\test');
28
29// junction to a volume (same as a mounted volume)
30$link = $tmpDir . '\\test\\volume_junction';
31exec(sprintf('mklink /J "%s" %s', $link, $volume));
32var_dump(readlink($link));
33rmdir($link);
34
35// junction to a directory
36$link = $tmpDir . '\\test\\directory_junction';
37$target = $tmpDir . '\\test\\directory';
38exec(sprintf('mklink /J "%s" "%s"', $link, $target));
39var_dump(readlink($link));
40rmdir($link);
41
42// symlink to a directory (absolute and relative)
43$link = $tmpDir . '\\test\\directory_symlink';
44$target = $tmpDir . '\\test\\directory';
45exec(sprintf('mklink /D "%s" "%s"', $link, $target));
46var_dump(readlink($link));
47rmdir($link);
48exec(sprintf('mklink /D "%s" directory', $link));
49var_dump(readlink($link));
50rmdir($link);
51
52// create a file to link to
53$filename = $tmpDir . '\\test\\directory\\a.php';
54$fh = fopen($filename, 'w');
55fclose($fh);
56
57// symlink to a file (absolute and relative)
58$link = $tmpDir . '\\test\\file_symlink';
59exec(sprintf('mklink "%s" "%s"', $link, $filename));
60var_dump(readlink($link));
61unlink($link);
62exec(sprintf('mklink "%s" directory\\a.php', $link));
63var_dump(readlink($link));
64unlink($link);
65
66// unexpected behaviour
67echo "\n*** Unexpected behaviour when not a reparse point\n";
68var_dump(readlink($tmpDir . '\\test\\directory'));
69var_dump(readlink($filename));
70
71unlink($filename);
72
73chdir(__DIR__);
74rmdir($tmpDir . '\\test\\directory');
75rmdir($tmpDir . '\\test');
76rmdir($tmpDir);
77?>
78--EXPECTF--
79string(3) "C:\"
80string(3) "C:\"
81string(%d) "%s\mnt\test\directory"
82string(%d) "%s\mnt\test\directory"
83string(%d) "%s\mnt\test\directory"
84string(%d) "%s\mnt\test\directory\a.php"
85string(%d) "%s\mnt\test\directory\a.php"
86
87*** Unexpected behaviour when not a reparse point
88string(%d) "%s\mnt\test\directory"
89string(%d) "%s\mnt\test\directory\a.php"
90