capture-libs.pl 11.4 KB
Newer Older
Simon McVittie's avatar
Simon McVittie committed
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
#!/usr/bin/perl

# Copyright © 2017 Collabora Ltd
#
# This file is part of libcapsule.
#
# libcapsule is free software: you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as
# published by the Free Software Foundation; either version 2.1 of the
# License, or (at your option) any later version.
#
# libcapsule 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 Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with libcapsule.  If not, see <http://www.gnu.org/licenses/>.

use autodie;
use warnings;
use strict;

use Cwd qw(abs_path);
use File::Temp qw();
use IPC::Run qw(run);
use Test::More;

use FindBin;
use lib $FindBin::Bin;

use CapsuleTest;

skip_all_unless_bwrap;

36
my $LIBDIR = qr{/(?:usr/)?lib(?:32|64|x32)?(?:/\w+-linux-gnu\w*)?(?:/i686)?(?:/cmov)?};
Simon McVittie's avatar
Simon McVittie committed
37
38
39
40
41
42
43
44
45

my $test_tempdir = File::Temp->newdir();
diag "Working directory: $test_tempdir";
my $host = "${test_tempdir}/host";
mkdir($host);
my $libdir = "${test_tempdir}/libdir";

ok(! system('rm', '-fr', $libdir));
mkdir($libdir);
46
run_ok([$CAPSULE_CAPTURE_LIBS_TOOL, 'soname:libc.so.6'], '>&2',
Simon McVittie's avatar
Simon McVittie committed
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
    init => sub { chdir $libdir or die $!; });
ok(-e "$libdir/libc.so.6", 'libc.so.6 was captured');
like(readlink "$libdir/libc.so.6", qr{^$LIBDIR/libc\.so\.6$},
    '$libdir/libc.so.6 is a symlink to the real libc.so.6');
my $libc = abs_path("$libdir/libc.so.6");
ok(-e "$libdir/libBrokenLocale.so.1", 'related libraries were captured');
ok(-e "$libdir/libm.so.6", 'related libraries were captured');
ok(-e "$libdir/libpthread.so.0", 'related libraries were captured');
ok(-e "$libdir/libmemusage.so", 'related libraries were captured');
like(readlink "$libdir/libm.so.6", qr{^$LIBDIR/libm\.so\.6$},
    'libm is a symlink to the right place');
my @libc_family;
{
    opendir(my $dir_iter, $libdir);
    @libc_family = sort grep { $_ ne '.' and $_ ne '..' } readdir $dir_iter;
    closedir $dir_iter;
    foreach my $symlink (@libc_family) {
        diag "- $symlink -> ".readlink("$libdir/$symlink");
    }
}

ok(! system('rm', '-fr', $libdir));
mkdir($libdir);
run_ok([$CAPSULE_CAPTURE_LIBS_TOOL, '--link-target=/run/host',
71
        "--dest=$libdir", 'soname-match:libc.so.6'], '>&2');
Simon McVittie's avatar
Simon McVittie committed
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
{
    opendir(my $dir_iter, $libdir);
    my @links = sort grep { $_ ne '.' and $_ ne '..' } readdir $dir_iter;
    foreach my $symlink (@links) {
        diag "- $symlink -> ".readlink("$libdir/$symlink");
    }
    closedir $dir_iter;
    is_deeply(\@links, \@libc_family,
        'the same libraries are captured when using --link-target');
}
like(readlink "$libdir/libc.so.6", qr{^/run/host\Q$libc\E$},
     '$libdir/libc.so.6 is a symlink to /run/host + realpath of libc.so.6');

ok(! system('rm', '-fr', $libdir));
mkdir($libdir);
run_ok([qw(bwrap --ro-bind / / --ro-bind /), $host,
        '--bind', $libdir, $libdir,
        qw(--dev-bind /dev /dev),
        $CAPSULE_CAPTURE_LIBS_TOOL,
        "--dest=$libdir",
92
        "--provider=$host", 'soname:libc.so.6'], '>&2');
Simon McVittie's avatar
Simon McVittie committed
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
{
    opendir(my $dir_iter, $libdir);
    my @links = sort grep { $_ ne '.' and $_ ne '..' } readdir $dir_iter;
    foreach my $symlink (@links) {
        diag "- $symlink -> ".readlink("$libdir/$symlink");
    }
    closedir $dir_iter;
    is_deeply(\@links, \@libc_family,
        'the same libraries are captured when using $host');
}
like(readlink "$libdir/libc.so.6", qr{^\Q$host\E\Q$libc\E$},
     '$libdir/libc.so.6 is a symlink to $host + realpath of libc.so.6');

ok(! system('rm', '-fr', $libdir));
mkdir($libdir);
run_ok([qw(bwrap --ro-bind / / --ro-bind /), $host,
        '--bind', $libdir, $libdir,
        qw(--dev-bind /dev /dev),
        $CAPSULE_CAPTURE_LIBS_TOOL, '--link-target=/run/host',
        "--dest=$libdir",
113
        "--provider=$host", 'soname:libc.so.6'], '>&2');
Simon McVittie's avatar
Simon McVittie committed
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
{
    opendir(my $dir_iter, $libdir);
    my @links = sort grep { $_ ne '.' and $_ ne '..' } readdir $dir_iter;
    foreach my $symlink (@links) {
        diag "- $symlink -> ".readlink("$libdir/$symlink");
    }
    closedir $dir_iter;
    is_deeply(\@links, \@libc_family,
        'the same libraries are captured when using $host and --link-target');
}
like(readlink "$libdir/libc.so.6", qr{^/run/host\Q$libc\E$},
     '$libdir/libc.so.6 is a symlink to /run/host + realpath of libc.so.6');

ok(! system('rm', '-fr', $libdir));
mkdir($libdir);
run_ok([qw(bwrap --ro-bind / / --ro-bind /), $host,
        '--bind', $libdir, $libdir,
        qw(--dev-bind /dev /dev),
        $CAPSULE_CAPTURE_LIBS_TOOL, '--link-target=/',
133
134
        "--dest=$libdir", "--provider=$host",
        'soname-match:libjp*g.so.6*'], '>&2');
Simon McVittie's avatar
Simon McVittie committed
135
136
137
138
139
140
141
142
143
144
145
{
    opendir(my $dir_iter, $libdir);
    foreach my $symlink (readdir $dir_iter) {
        next if $symlink eq '.' || $symlink eq '..';
        diag "- $symlink -> ".readlink("$libdir/$symlink");
    }
    closedir $dir_iter;
}
like(readlink "$libdir/libjpeg.so.62", qr{^$LIBDIR/libjpeg\.so\.62(?:[0-9.]+)$},
     '$libdir/libjpeg.so.62 is a symlink to /run/host + realpath of libjpeg-6b');

146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
ok(! system('rm', '-fr', $libdir));
mkdir($libdir);
my $stderr;
my $result = run_verbose([qw(bwrap --ro-bind / / --ro-bind /), $host,
                          '--bind', $libdir, $libdir,
                          qw(--dev-bind /dev /dev),
                          $CAPSULE_CAPTURE_LIBS_TOOL, '--link-target=/',
                          "--dest=$libdir", "--provider=$host",
                          'soname-match:this*library*does?not?exist'],
                         '2>', \$stderr, '>&2');
ok(! $result, 'a non-matching wildcard should fail');
like($stderr, qr{"this\*library\*does\?not\?exist"});

ok(! system('rm', '-fr', $libdir));
mkdir($libdir);
run_ok([qw(bwrap --ro-bind / / --ro-bind /), $host,
        '--bind', $libdir, $libdir,
        qw(--dev-bind /dev /dev),
        $CAPSULE_CAPTURE_LIBS_TOOL, '--link-target=/',
        "--dest=$libdir", "--provider=$host",
        'if-exists:soname-match:this*library*does?not?exist']);

168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
# libgobject-2.0.so.0 depends on libglib-2.0.so.0 so normally,
# capturing GObject captures GLib
ok(! system('rm', '-fr', $libdir));
mkdir($libdir);
run_ok([qw(bwrap --ro-bind / / --ro-bind /), $host,
        '--bind', $libdir, $libdir,
        qw(--dev-bind /dev /dev),
        $CAPSULE_CAPTURE_LIBS_TOOL, '--link-target=/',
        "--dest=$libdir", "--provider=$host",
        'soname:libgobject-2.0.so.0']);
{
    opendir(my $dir_iter, $libdir);
    foreach my $symlink (readdir $dir_iter) {
        next if $symlink eq '.' || $symlink eq '..';
        diag "- $symlink -> ".readlink("$libdir/$symlink");
    }
    closedir $dir_iter;
}
like(readlink "$libdir/libgobject-2.0.so.0",
     qr{^$LIBDIR/libgobject-2\.0\.so\.0(?:[0-9.]+)$},
     '$libdir/libgobject-2.0.so.0 is captured');
like(readlink "$libdir/libglib-2.0.so.0",
     qr{^$LIBDIR/libglib-2\.0\.so\.0(?:[0-9.]+)$},
     '$libdir/libglib-2.0.so.0 is captured as a dependency');

# only-dependencies: captures the dependency, GLib but not the
# dependent library, GObject
ok(! system('rm', '-fr', $libdir));
mkdir($libdir);
run_ok([qw(bwrap --ro-bind / / --ro-bind /), $host,
        '--bind', $libdir, $libdir,
        qw(--dev-bind /dev /dev),
        $CAPSULE_CAPTURE_LIBS_TOOL, '--link-target=/',
        "--dest=$libdir", "--provider=$host",
        'only-dependencies:soname:libgobject-2.0.so.0']);
{
    opendir(my $dir_iter, $libdir);
    foreach my $symlink (readdir $dir_iter) {
        next if $symlink eq '.' || $symlink eq '..';
        diag "- $symlink -> ".readlink("$libdir/$symlink");
    }
    closedir $dir_iter;
}
ok(! -e "$libdir/libgobject-2.0.so.0",
   'only-dependencies: does not capture the library itself');
like(readlink "$libdir/libglib-2.0.so.0",
     qr{^$LIBDIR/libglib-2\.0\.so\.0(?:[0-9.]+)$},
     '$libdir/libglib-2.0.so.0 is captured as a dependency');

# no-dependencies: captures the dependent library, GObject but not the
# dependency, GLib (not amazingly useful)
ok(! system('rm', '-fr', $libdir));
mkdir($libdir);
run_ok([qw(bwrap --ro-bind / / --ro-bind /), $host,
        '--bind', $libdir, $libdir,
        qw(--dev-bind /dev /dev),
        $CAPSULE_CAPTURE_LIBS_TOOL, '--link-target=/',
        "--dest=$libdir", "--provider=$host",
        'no-dependencies:soname:libgobject-2.0.so.0']);
{
    opendir(my $dir_iter, $libdir);
    foreach my $symlink (readdir $dir_iter) {
        next if $symlink eq '.' || $symlink eq '..';
        diag "- $symlink -> ".readlink("$libdir/$symlink");
    }
    closedir $dir_iter;
}
like(readlink "$libdir/libgobject-2.0.so.0",
     qr{^$LIBDIR/libgobject-2\.0\.so\.0(?:[0-9.]+)$},
     '$libdir/libgobject-2.0.so.0 is captured');
ok(! -e "$libdir/libglib-2.0.so.0",
   'no-dependencies: does not capture libglib-2.0.so.0');

241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
# --no-glibc doesn't capture glibc even if other dependencies are
# captured
ok(! system('rm', '-fr', $libdir));
mkdir($libdir);
run_ok([qw(bwrap --ro-bind / / --ro-bind /), $host,
        '--bind', $libdir, $libdir,
        qw(--dev-bind /dev /dev),
        $CAPSULE_CAPTURE_LIBS_TOOL, '--link-target=/',
        "--dest=$libdir", "--provider=$host", "--no-glibc",
        'soname:libgobject-2.0.so.0']);
{
    opendir(my $dir_iter, $libdir);
    foreach my $symlink (readdir $dir_iter) {
        next if $symlink eq '.' || $symlink eq '..';
        diag "- $symlink -> ".readlink("$libdir/$symlink");
    }
    closedir $dir_iter;
}
like(readlink "$libdir/libgobject-2.0.so.0",
     qr{^$LIBDIR/libgobject-2\.0\.so\.0(?:[0-9.]+)$},
     '$libdir/libgobject-2.0.so.0 is captured');
like(readlink "$libdir/libglib-2.0.so.0",
     qr{^$LIBDIR/libglib-2\.0\.so\.0(?:[0-9.]+)$},
     '$libdir/libglib-2.0.so.0 is captured');
ok(! -e "$libdir/libc.so.6",
   '--no-glibc does not capture libc.so.6');

268
269
270
# only-dependencies:no-dependencies: (either way round) is completely useless
ok(! system('rm', '-fr', $libdir));
mkdir($libdir);
271
272
273
274
275
276
277
$result = run_verbose([qw(bwrap --ro-bind / / --ro-bind /), $host,
                       '--bind', $libdir, $libdir,
                       qw(--dev-bind /dev /dev),
                       $CAPSULE_CAPTURE_LIBS_TOOL, '--link-target=/',
                       "--dest=$libdir", "--provider=$host",
                       'only-dependencies:no-dependencies:libglib-2.0.so.0'],
                       '>&2');
278
279
280
ok(! $result, 'only-dependencies:no-dependencies: is useless');
ok(! system('rm', '-fr', $libdir));
mkdir($libdir);
281
282
283
284
285
286
287
$result = run_verbose([qw(bwrap --ro-bind / / --ro-bind /), $host,
                       '--bind', $libdir, $libdir,
                       qw(--dev-bind /dev /dev),
                       $CAPSULE_CAPTURE_LIBS_TOOL, '--link-target=/',
                       "--dest=$libdir", "--provider=$host",
                       'no-dependencies:only-dependencies:libglib-2.0.so.0'],
                       '>&2');
288
289
ok(! $result, 'no-dependencies:only-dependencies: is useless');

Simon McVittie's avatar
Simon McVittie committed
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
SKIP: {
    skip "not on Linux? Good luck!", 1 unless $^O eq 'linux';
    my $stdout;
    my $ld_so;

    run_ok([$CAPSULE_CAPTURE_LIBS_TOOL, '--print-ld.so'], '>', \$ld_so);
    chomp $ld_so;
    diag "ld.so is $ld_so";

    run_ok([$CAPSULE_CAPTURE_LIBS_TOOL, '--resolve-ld.so=/'], '>', \$stdout);

    is($stdout, abs_path($ld_so)."\n",
        '--resolve-ld.so=/ should print '.abs_path($ld_so));
    run_ok([qw(bwrap --ro-bind / / --ro-bind /), $host,
            $CAPSULE_CAPTURE_LIBS_TOOL, "--resolve-ld.so=$host"],
        '>', \$stdout);
    is($stdout, abs_path($ld_so)."\n",
        "--resolve-ld.so=$host should print ".abs_path($ld_so));
};

done_testing;

# vim:set sw=4 sts=4 et: