summaryrefslogtreecommitdiffstats
path: root/scripts/t/dpkg_realpath.t
blob: a57202d3845ee0853b454e9e64b826ad608f08ea (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
#!/usr/bin/perl
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <https://www.gnu.org/licenses/>.

use strict;
use warnings;

use Test::More;
use Test::Dpkg qw(:paths);

use Cwd;
use File::Spec::Functions qw(rel2abs);
use File::Path qw(make_path);

use Dpkg::IPC;

plan tests => 48;

my $srcdir = rel2abs($ENV{srcdir} || '.');
my $tmpdir = rel2abs(test_get_temp_path());

# Delete variables that can affect the tests.
delete $ENV{DPKG_ROOT};

sub gen_file
{
    my ($pathname) = @_;

    open my $fh, '>', $pathname or BAIL_OUT("cannot create file $pathname");
    close $fh;
}

sub gen_hier
{
    my $curdir = getcwd();

    chdir $tmpdir;

    make_path('aa/bb/cc');
    make_path('zz/yy/xx');
    make_path('usr/bin');

    gen_file('aa/bb/cc/file');
    symlink('aa/bb/cc/file', 'zz/yy/xx/symlink-rel');
    symlink('/aa/bb/cc/file', 'zz/yy/xx/symlink-abs');
    gen_file('usr/bin/a-shell');
    symlink('/usr/bin/a-shell', 'usr/bin/sh');

    chdir $curdir;
}

sub test_realpath
{
    my ($pathname, $realpath, $root) = @_;
    my ($stderr, $stdout);
    $root //= q{};

    spawn(
        exec => [ $ENV{SHELL}, "$srcdir/dpkg-realpath.sh", $pathname ],
        env => {
            DPKG_ROOT => $root,
            DPKG_DATADIR => rel2abs($srcdir),
        },
        error_to_string => \$stderr,
        to_string => \$stdout,
        wait_child => 1,
        nocheck => 1,
    );

    ok($? == 0, "dpkg-realpath $pathname succeeded");
    diag($stderr) unless $? == 0;

    chomp $stdout;

    is($stdout, $realpath,
       "resolved realpath for $pathname matches $realpath with root='$root'");
}

gen_hier();

# Relative paths
my $curdir = getcwd();
chdir $tmpdir;

test_realpath('aa/bb/cc', "$tmpdir/aa/bb/cc");
test_realpath('zz/yy/xx', "$tmpdir/zz/yy/xx");
test_realpath('usr/bin', "$tmpdir/usr/bin");
test_realpath('aa/bb/cc/file', "$tmpdir/aa/bb/cc/file");
test_realpath('zz/yy/xx/symlink-rel', "$tmpdir/zz/yy/xx/aa/bb/cc/file");
test_realpath('zz/yy/xx/symlink-abs', '/aa/bb/cc/file');
test_realpath('usr/bin/a-shell', "$tmpdir/usr/bin/a-shell");
test_realpath('usr/bin/sh', '/usr/bin/a-shell');

chdir $curdir;

# Absolute paths
test_realpath("$tmpdir/aa/bb/cc", "$tmpdir/aa/bb/cc");
test_realpath("$tmpdir/zz/yy/xx", "$tmpdir/zz/yy/xx");
test_realpath("$tmpdir/usr/bin", "$tmpdir/usr/bin");
test_realpath("$tmpdir/aa/bb/cc/file", "$tmpdir/aa/bb/cc/file");
test_realpath("$tmpdir/zz/yy/xx/symlink-rel", "$tmpdir/zz/yy/xx/aa/bb/cc/file");
test_realpath("$tmpdir/zz/yy/xx/symlink-abs", '/aa/bb/cc/file');
test_realpath("$tmpdir/usr/bin/a-shell", "$tmpdir/usr/bin/a-shell");
test_realpath("$tmpdir/usr/bin/sh", '/usr/bin/a-shell');

# Chrooted paths
test_realpath('/aa/bb/cc', '/aa/bb/cc', $tmpdir);
test_realpath('/zz/yy/xx', '/zz/yy/xx', $tmpdir);
test_realpath('/usr/bin', '/usr/bin', $tmpdir);
test_realpath('/aa/bb/cc/file', '/aa/bb/cc/file', $tmpdir);
test_realpath('/zz/yy/xx/symlink-rel', '/zz/yy/xx/aa/bb/cc/file', $tmpdir);
test_realpath('/zz/yy/xx/symlink-abs', '/aa/bb/cc/file', $tmpdir);
test_realpath('/usr/bin/a-shell', '/usr/bin/a-shell', $tmpdir);
test_realpath('/usr/bin/sh', '/usr/bin/a-shell', $tmpdir);

1;