xref: /PHP-8.4/ext/phar/tests/bug54289.phpt (revision 74859783)
1--TEST--
2Bug #54289 Phar::extractTo() does not accept specific directories to be extracted
3--EXTENSIONS--
4phar
5--INI--
6phar.readonly = 0
7--FILE--
8<?php
9// put our test fixtures into a far
10$base  = __DIR__.DIRECTORY_SEPARATOR.'bug54289'.DIRECTORY_SEPARATOR;
11$inDir = $base.'in';
12$phar  = $base.'test.phar';
13$pharA = new Phar($phar);
14$pharA->buildFromDirectory($inDir);
15
16// we should be able to pull out a directory that's there, but none that share
17// the same prefix
18$outDir = $base.'out';
19$pharB = new Phar($phar);
20$pharB->extractTo($outDir, 'dirA/', true);
21var_dump(file_exists($outDir.DIRECTORY_SEPARATOR.'dirA'.DIRECTORY_SEPARATOR.'fileA'));
22var_dump(file_exists($outDir.DIRECTORY_SEPARATOR.'dirA'.DIRECTORY_SEPARATOR.'fileB'));
23var_dump(is_dir($outDir.DIRECTORY_SEPARATOR.'dirAB'));
24
25// should also not be able to pull out non-existent ones
26try {
27  $pharB->extractTo($outDir, 'dirX/', true);
28  echo 'failed to throw expected exception';
29} catch (PharException $ex) {
30}
31
32// should also not be able to pull out /, because paths are not "rooted" that way
33try {
34  $pharB->extractTo($outDir, '/', true);
35  echo 'failed to throw expected exception';
36} catch (PharException $ex) {
37}
38
39// should be able to do by array, too
40$pharB = new Phar($phar);
41$pharB->extractTo($outDir, [ 'dirA/', 'dirAB/' ], true);
42
43// but not an array with a bad member in it
44try {
45  $pharB = new Phar($phar);
46  $pharB->extractTo($outDir, [ 'dirA/', 'dirX/' ], true);
47  echo 'failed to throw expected exception';
48} catch (PharException $ex) {
49}
50
51echo 'done';
52?>
53--CLEAN--
54<?php
55$base   = __DIR__.DIRECTORY_SEPARATOR.'bug54289'.DIRECTORY_SEPARATOR;
56$phar   = $base.'test.phar';
57$outDir = $base.'out';
58unlink($phar);
59$iter = new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator(
60  $outDir, \FilesystemIterator::SKIP_DOTS|\FilesystemIterator::UNIX_PATHS),
61  \RecursiveIteratorIterator::CHILD_FIRST);
62foreach ($iter as $value) {
63    $value->isFile() ? unlink($value) : rmdir($value);
64}
65?>
66--EXPECT--
67bool(true)
68bool(true)
69bool(false)
70done
71