doc: Move Perl version baseline as the first perl coding style subsection
[dpkg.git] / scripts / t / Dpkg_Shlibs.t
blob32436741f7b978da31c342b36b70a91c47a8f639
1 #!/usr/bin/perl
3 # This program is free software; you can redistribute it and/or modify
4 # it under the terms of the GNU General Public License as published by
5 # the Free Software Foundation; either version 2 of the License, or
6 # (at your option) any later version.
8 # This program is distributed in the hope that it will be useful,
9 # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 # GNU General Public License for more details.
13 # You should have received a copy of the GNU General Public License
14 # along with this program. If not, see <https://www.gnu.org/licenses/>.
16 use strict;
17 use warnings;
19 use Test::More;
20 use Test::Dpkg qw(:needs :paths);
22 use Cwd;
23 use IPC::Cmd qw(can_run);
25 plan tests => 150;
27 $ENV{DEB_BUILD_ARCH} = 'amd64';
28 $ENV{DEB_HOST_ARCH} = 'amd64';
30 use_ok('Dpkg::Shlibs');
32 my $tmp;
33 my @tmp;
34 my %tmp;
36 my $datadir = test_get_data_path();
38 my @librarypaths;
41 # XXX: Keep as long as we support the deprecated LD_LIBRARY_PATH.
42 local $ENV{LD_LIBRARY_PATH} = '/test-env';
44 Dpkg::Shlibs::add_library_dir('/test-a');
45 @librarypaths = Dpkg::Shlibs::get_library_paths();
46 is($librarypaths[0], '/test-a', 'add_library_dir() does not get lost');
48 Dpkg::Shlibs::add_library_dir('/test-b');
49 @librarypaths = Dpkg::Shlibs::get_library_paths();
50 is_deeply([ @librarypaths[0, 1] ] , [ '/test-a', '/test-b' ],
51 'add_library_dir() prepends');
54 Dpkg::Shlibs::blank_library_paths();
56 # We want relative paths inside the ld.so.conf fragments to work, and $srcdir
57 # is usually a relative path, so let's temporarily switch directory.
58 # XXX: An alternative would be to make parse_ldso_conf relative path aware.
59 my $cwd = getcwd();
60 test_needs_srcdir_switch();
61 Dpkg::Shlibs::parse_ldso_conf('t/Dpkg_Shlibs/ld.so.conf');
62 chdir($cwd);
64 @librarypaths = Dpkg::Shlibs::get_library_paths();
65 is_deeply(\@librarypaths,
66 [ qw(/nonexistent32 /nonexistent/lib64
67 /usr/local/lib /nonexistent/lib128) ], 'parsed library paths');
69 use_ok('Dpkg::Shlibs::Objdump');
71 my $obj = Dpkg::Shlibs::Objdump::Object->new;
73 open my $objdump, '<', "$datadir/objdump.dbd-pg"
74 or die "$datadir/objdump.dbd-pg: $!";
75 $obj->parse_objdump_output($objdump);
76 close $objdump;
77 ok(!$obj->is_public_library(), 'Pg.so is not a public library');
78 ok(!$obj->is_executable(), 'Pg.so is not an executable');
80 open $objdump, '<', "$datadir/objdump.ls"
81 or die "$datadir/objdump.ls: $!";
82 $obj->reset();
83 $obj->parse_objdump_output($objdump);
84 close $objdump;
85 ok(!$obj->is_public_library(), 'ls is not a public library');
86 ok($obj->is_executable(), 'ls is an executable');
88 my $sym = $obj->get_symbol('optarg@GLIBC_2.0');
89 ok($sym, 'optarg@GLIBC_2.0 exists');
90 ok(!$sym->{defined}, 'R_*_COPY relocations are taken into account');
92 open $objdump, '<', "$datadir/objdump.space"
93 or die "$datadir/objdump.space: $!";
94 $obj->reset();
95 $obj->parse_objdump_output($objdump);
96 close $objdump;
98 # Non-regression test for #506139
99 $sym = $obj->get_symbol('singlespace');
100 ok($sym, 'version less symbol separated by a single space are correctly parsed');
102 open $objdump, '<', "$datadir/objdump.libc6-2.6"
103 or die "$datadir/objdump.libc6-2.6: $!";
104 $obj->reset();
105 $obj->parse_objdump_output($objdump);
106 close $objdump;
108 ok($obj->is_public_library(), 'libc6 is a public library');
109 ok($obj->is_executable(), 'libc6 is an executable');
111 is($obj->{SONAME}, 'libc.so.6', 'SONAME');
112 is($obj->{HASH}, '0x13d99c', 'HASH');
113 is($obj->{GNU_HASH}, '0x194', 'GNU_HASH');
114 is($obj->{format}, 'elf32-i386', 'format');
115 is_deeply($obj->{flags}, { DYNAMIC => 1, HAS_SYMS => 1, D_PAGED => 1 }, 'flags');
116 is_deeply($obj->{NEEDED}, [ 'ld-linux.so.2' ], 'NEEDED');
117 is_deeply([ $obj->get_needed_libraries ], [ 'ld-linux.so.2' ], 'NEEDED');
119 $sym = $obj->get_symbol('_sys_nerr@GLIBC_2.3');
120 is_deeply( $sym, { name => '_sys_nerr', version => 'GLIBC_2.3',
121 soname => 'libc.so.6', objid => 'libc.so.6',
122 section => '.rodata', dynamic => 1,
123 debug => '', type => 'O', weak => '',
124 local => '', global => 1, visibility => '',
125 hidden => 1, defined => 1 }, 'Symbol' );
126 $sym = $obj->get_symbol('_IO_stdin_used');
127 is_deeply( $sym, { name => '_IO_stdin_used', version => '',
128 soname => 'libc.so.6', objid => 'libc.so.6',
129 section => '*UND*', dynamic => 1,
130 debug => '', type => ' ', weak => 1,
131 local => '', global => '', visibility => '',
132 hidden => '', defined => '' }, 'Symbol 2' );
134 my @syms = $obj->get_exported_dynamic_symbols;
135 is( scalar @syms, 2231, 'defined && dynamic' );
136 @syms = $obj->get_undefined_dynamic_symbols;
137 is( scalar @syms, 9, 'undefined && dynamic' );
140 my $obj_old = Dpkg::Shlibs::Objdump::Object->new;
142 open $objdump, '<', "$datadir/objdump.libc6-2.3"
143 or die "$datadir/objdump.libc6-2.3: $!";
144 $obj_old->parse_objdump_output($objdump);
145 close $objdump;
148 use_ok('Dpkg::Shlibs::SymbolFile');
149 use_ok('Dpkg::Shlibs::Symbol');
151 my $sym_file = Dpkg::Shlibs::SymbolFile->new(file => "$datadir/symbol_file.tmp");
152 my $sym_file_dup = Dpkg::Shlibs::SymbolFile->new(file => "$datadir/symbol_file.tmp");
153 my $sym_file_old = Dpkg::Shlibs::SymbolFile->new(file => "$datadir/symbol_file.tmp");
155 $sym_file->merge_symbols($obj_old, '2.3.6.ds1-13');
156 $sym_file_old->merge_symbols($obj_old, '2.3.6.ds1-13');
158 ok( $sym_file->has_object('libc.so.6'), 'SONAME in sym file' );
160 $sym_file->merge_symbols($obj, '2.6-1');
162 ok( $sym_file->get_new_symbols($sym_file_old), 'has new symbols' );
163 ok( $sym_file_old->get_lost_symbols($sym_file), 'has lost symbols' );
165 is( $sym_file_old->lookup_symbol('__bss_start@Base', ['libc.so.6']),
166 undef, 'internal symbols are ignored' );
168 %tmp = $sym_file->lookup_symbol('_errno@GLIBC_2.0', ['libc.so.6'], 1);
169 isa_ok($tmp{symbol}, 'Dpkg::Shlibs::Symbol');
170 is_deeply(\%tmp, { symbol => Dpkg::Shlibs::Symbol->new(symbol => '_errno@GLIBC_2.0',
171 minver => '2.3.6.ds1-13', dep_id => 0,
172 deprecated => '2.6-1'), soname => 'libc.so.6' },
173 'deprecated symbol');
175 # Wildcard test
176 my $pat = $sym_file_old->create_symbol('*@GLIBC_PRIVATE 2.3.6.wildcard');
177 $sym_file_old->add_symbol($pat, 'libc.so.6');
178 $sym_file_old->merge_symbols($obj, '2.6-1');
179 $sym = $sym_file_old->lookup_symbol('__nss_services_lookup@GLIBC_PRIVATE', 'libc.so.6');
180 is_deeply($sym, Dpkg::Shlibs::Symbol->new(symbol => '__nss_services_lookup@GLIBC_PRIVATE',
181 minver => '2.3.6.wildcard', dep_id => 0, deprecated => 0,
182 tags => { symver => undef, optional => undef },
183 tagorder => [ 'symver', 'optional' ],
184 matching_pattern => $pat ), 'wildcarded symbol');
186 # Save -> Load test
187 use File::Temp;
188 use File::Basename qw(basename);
190 sub save_load_test {
191 my ($symfile, $comment, @opts) = @_;
193 my $save_file = File::Temp->new();
194 $symfile->save($save_file->filename, @opts);
195 my $dup = Dpkg::Shlibs::SymbolFile->new(file => $save_file->filename);
196 # Force sync of non-stored attributes
197 $dup->{file} = $symfile->{file};
198 $dup->{arch} = $symfile->{arch};
200 is_deeply($dup, $symfile, $comment);
201 if (-f $symfile->{file}) {
202 is(system('diff', '-u', $symfile->{file}, $save_file->filename), 0,
203 basename($symfile->{file}) . ' dumped identical');
207 save_load_test( $sym_file, 'save -> load' );
210 # Test ignoring internal symbols
211 open $objdump, '<', "$datadir/objdump.internal"
212 or die "objdump.internal: $!";
213 $obj->reset();
214 $obj->parse_objdump_output($objdump);
215 close $objdump;
217 # Do not ignore any internal symbols
218 $sym_file = Dpkg::Shlibs::SymbolFile->new(file => "$datadir/symbols.internal-filter");
219 $sym_file->merge_symbols($obj, '100.MISSING');
221 $sym = $sym_file->lookup_symbol('symbol@Base', ['libinternal.so.0']);
222 is_deeply($sym, Dpkg::Shlibs::Symbol->new(symbol => 'symbol@Base',
223 minver => '1.0', dep_id => 0, deprecated => 0),
224 'symbol unaffected w/o including internal symbols');
226 $sym = $sym_file->lookup_symbol('.gomp_critical_user_foo@Base', ['libinternal.so.0']);
227 is($sym, undef, 'gomp symbol omitted while filtering internal symbols');
229 $sym = $sym_file->lookup_symbol('__aeabi_lcmp@GCC_3.0', ['libinternal.so.0']);
230 is($sym, undef, 'known aeabi symbol omitted while filtering internal symbols');
232 $sym = $sym_file->lookup_symbol('__aeabi_unknown@GCC_4.0', ['libinternal.so.0']);
233 is($sym, undef, 'unknown aeabi symbol omitted while filtering internal symbols');
235 # Include internal symbols using the allow-internal tag.
236 $sym_file = Dpkg::Shlibs::SymbolFile->new(file => "$datadir/symbols.internal-allow");
237 $sym_file->merge_symbols($obj, '100.MISSING');
239 $sym = $sym_file->lookup_symbol('symbol@Base', ['libinternal.so.0']);
240 is_deeply($sym, Dpkg::Shlibs::Symbol->new(symbol => 'symbol@Base',
241 minver => '1.0', dep_id => 0, deprecated => 0),
242 'symbol unaffected while including internal symbols via symbol tag');
244 $sym = $sym_file->lookup_symbol('.gomp_critical_user_foo@Base', ['libinternal.so.0']);
245 is_deeply($sym, Dpkg::Shlibs::Symbol->new(symbol => '.gomp_critical_user_foo@Base',
246 minver => '2.0', dep_id => 0, deprecated => 0,
247 tags => { 'allow-internal' => undef },
248 tagorder => [ 'allow-internal' ]),
249 'internal gomp symbol included via symbol tag');
251 $sym = $sym_file->lookup_symbol('__aeabi_lcmp@GCC_3.0', ['libinternal.so.0']);
252 is_deeply($sym, Dpkg::Shlibs::Symbol->new(symbol => '__aeabi_lcmp@GCC_3.0',
253 minver => '3.0', dep_id => 0, deprecated => 0,
254 tags => { 'allow-internal' => undef },
255 tagorder => [ 'allow-internal' ]),
256 'internal known aeabi symbol included via symbol tag');
258 $sym = $sym_file->lookup_symbol('__aeabi_unknown@GCC_4.0', ['libinternal.so.0']);
259 is_deeply($sym, Dpkg::Shlibs::Symbol->new(symbol => '__aeabi_unknown@GCC_4.0',
260 minver => '4.0', dep_id => 0, deprecated => 0,
261 tags => { 'allow-internal' => undef },
262 tagorder => [ 'allow-internal' ]),
263 'internal unknown aeabi symbol omitted via symbol tag');
265 # Include internal symbols using the Allow-Internal-Symbol-Groups field
266 $sym_file = Dpkg::Shlibs::SymbolFile->new(file => "$datadir/symbols.internal-allow-groups");
267 $sym_file->merge_symbols($obj, '100.MISSING');
269 $sym = $sym_file->lookup_symbol('symbol@Base', ['libinternal.so.0']);
270 is_deeply($sym, Dpkg::Shlibs::Symbol->new(symbol => 'symbol@Base',
271 minver => '1.0', dep_id => 0, deprecated => 0),
272 'symbol unaffected w/o including internal symbols');
274 $sym = $sym_file->lookup_symbol('.gomp_critical_user_foo@Base', ['libinternal.so.0']);
275 is_deeply($sym, Dpkg::Shlibs::Symbol->new(symbol => '.gomp_critical_user_foo@Base',
276 minver => '2.0', dep_id => 0, deprecated => 0),
277 'internal gomp symbol included via library field');
279 $sym = $sym_file->lookup_symbol('__aeabi_lcmp@GCC_3.0', ['libinternal.so.0']);
280 is_deeply($sym, Dpkg::Shlibs::Symbol->new(symbol => '__aeabi_lcmp@GCC_3.0',
281 minver => '3.0', dep_id => 0, deprecated => 0),
282 'internal known aeabi symbol included via library field');
284 $sym = $sym_file->lookup_symbol('__aeabi_unknown@GCC_4.0', ['libinternal.so.0']);
285 is_deeply($sym, Dpkg::Shlibs::Symbol->new(symbol => '__aeabi_unknown@GCC_4.0',
286 minver => '4.0', dep_id => 0, deprecated => 0),
287 'internal unknown aeabi symbol included via library field');
290 # Test include mechanism of SymbolFile
291 $sym_file = Dpkg::Shlibs::SymbolFile->new(file => "$datadir/symbols.include-1");
293 $sym = $sym_file->lookup_symbol('symbol_before@Base', ['libfake.so.1']);
294 is_deeply($sym, Dpkg::Shlibs::Symbol->new(symbol => 'symbol_before@Base',
295 minver => '0.9', dep_id => 0, deprecated => 0),
296 'symbol before include not lost');
298 $sym = $sym_file->lookup_symbol('symbol_after@Base', ['libfake.so.1']);
299 is_deeply($sym, Dpkg::Shlibs::Symbol->new(symbol => 'symbol_after@Base',
300 minver => '1.1', dep_id => 0, deprecated => 0),
301 'symbol after include not lost');
303 $sym = $sym_file->lookup_symbol('symbol1_fake1@Base', ['libfake.so.1']);
304 is_deeply($sym, Dpkg::Shlibs::Symbol->new(symbol => 'symbol1_fake1@Base',
305 minver => '1.0', dep_id => 0, deprecated => 0),
306 'overrides order with #include');
308 $sym = $sym_file->lookup_symbol('symbol3_fake1@Base', ['libfake.so.1']);
309 is_deeply($sym, Dpkg::Shlibs::Symbol->new(symbol => 'symbol3_fake1@Base',
310 minver => '0', dep_id => 0, deprecated => 0),
311 'overrides order with #include');
313 is($sym_file->get_smallest_version('libfake.so.1'), '0',
314 'get_smallest_version with null version');
316 $sym = $sym_file->lookup_symbol('symbol_in_libdivert@Base', ['libdivert.so.1']);
317 is_deeply($sym, Dpkg::Shlibs::Symbol->new(symbol => 'symbol_in_libdivert@Base',
318 minver => '1.0~beta1', dep_id => 0, deprecated => 0),
319 '#include can change current object');
321 $sym_file = Dpkg::Shlibs::SymbolFile->new(file => "$datadir/symbols.include-2");
323 $sym = $sym_file->lookup_symbol('symbol1_fake2@Base', ['libfake.so.1']);
324 is_deeply($sym, Dpkg::Shlibs::Symbol->new(symbol => 'symbol1_fake2@Base',
325 minver => '1.0', dep_id => 1, deprecated => 0),
326 'overrides order with circular #include');
328 is($sym_file->get_smallest_version('libfake.so.1'), '1.0',
329 'get_smallest_version');
331 my $io_data;
332 my $io;
334 # Check dump output
335 open $io, '>', \$io_data or die "cannot open io string\n";
336 $sym_file->output($io, package => 'libfake1');
337 is($io_data,
338 'libfake.so.1 libfake1 #MINVER#
339 | libvirtualfake
340 * Build-Depends-Package: libfake-dev
341 symbol1_fake2@Base 1.0 1
342 symbol2_fake2@Base 1.0
343 symbol3_fake2@Base 1.1
344 ', "Dump of $datadir/symbols.include-2");
347 # Check parsing of objdump output on ia64 (local symbols
348 # without versions and with visibility attribute)
349 $obj = Dpkg::Shlibs::Objdump::Object->new;
351 open $objdump, '<', "$datadir/objdump.glib-ia64"
352 or die "$datadir/objdump.glib-ia64: $!";
353 $obj->parse_objdump_output($objdump);
354 close $objdump;
355 ok($obj->is_public_library(), 'glib-ia64 is a public library');
356 ok(!$obj->is_executable(), 'glib-ia64 is not an executable');
358 $sym = $obj->get_symbol('IA__g_free');
359 is_deeply( $sym, { name => 'IA__g_free', version => '',
360 soname => 'libglib-2.0.so.0', objid => 'libglib-2.0.so.0',
361 section => '.text', dynamic => 1,
362 debug => '', type => 'F', weak => '',
363 local => 1, global => '', visibility => 'hidden',
364 hidden => '', defined => 1 },
365 'symbol with visibility without version' );
367 # Check parsing of objdump output when symbol names contain spaces
368 $obj = Dpkg::Shlibs::Objdump::Object->new;
370 open $objdump, '<', "$datadir/objdump.spacesyms"
371 or die "$datadir/objdump.spacesyms: $!";
372 $obj->parse_objdump_output($objdump);
373 close $objdump;
375 sub check_spacesym {
376 my ($name, $version, $visibility) = @_;
378 $visibility //= '';
379 $sym = $obj->get_symbol($name . "@" . $version);
380 is_deeply($sym, { name => $name, version => $version,
381 soname => 'libspacesyms.so.1',
382 objid => 'libspacesyms.so.1',
383 section => '.text', dynamic => 1,
384 debug => '', type => 'F', weak => '',
385 local => '', global => 1, visibility => $visibility,
386 hidden => '', defined => 1 }, $name);
387 ok(defined $obj->{dynrelocs}{$name . "@@" . $version},
388 "dynreloc found for $name");
391 check_spacesym('symdefaultvernospacedefault', 'Base');
392 check_spacesym('symdefaultvernospaceprotected', 'Base', 'protected');
393 check_spacesym('symlongvernospacedefault', 'VERY_LONG_VERSION_1');
394 check_spacesym('symlongvernospaceprotected', 'VERY_LONG_VERSION_1', 'protected');
395 check_spacesym('symshortvernospacedefault', 'V1');
396 check_spacesym('symshortvernospaceprotected', 'V1', 'protected');
397 check_spacesym('symdefaultverSPA CEdefault', 'Base');
398 check_spacesym('symdefaultverSPA CEprotected', 'Base', 'protected');
399 check_spacesym('symlongverSPA CEdefault', 'VERY_LONG_VERSION_1');
400 check_spacesym('symlongverSPA CEprotected', 'VERY_LONG_VERSION_1', 'protected');
401 check_spacesym('symshortverSPA CEdefault', 'V1');
402 check_spacesym('symshortverSPA CEprotected', 'V1', 'protected');
404 ####### Test symbol tagging support ######
406 # Parsing/dumping
407 # Template mode
408 $sym_file = Dpkg::Shlibs::SymbolFile->new(file => "$datadir/basictags.symbols", arch => 'amd64');
409 save_load_test($sym_file, 'template save -> load', template_mode => 1);
411 # Dumping in non-template mode (amd64) (test for arch tags)
412 open $io, '>', \$io_data or die "cannot open io string\n";
413 $sym_file->output($io);
414 is($io_data,
415 'libbasictags.so.1 libbasictags1 #MINVER#
416 | libbasictags1 (>= 1.1)
417 symbol11_optional@Base 1.1 1
418 symbol21_amd64@Base 2.1
419 symbol25_64@Base 2.5
420 symbol26_little@Base 2.6
421 symbol31_randomtag@Base 3.1
422 symbol51_untagged@Base 5.1
423 ', 'template vs. non-template on amd64');
425 # Dumping in non-template mode (mips) (test for arch tags)
426 open $io, '>', \$io_data or die "cannot open io string\n";
427 $sym_file = Dpkg::Shlibs::SymbolFile->new(file => "$datadir/basictags.symbols", arch => 'mips');
428 $sym_file->output($io);
429 is($io_data,
430 'libbasictags.so.1 libbasictags1 #MINVER#
431 | libbasictags1 (>= 1.1)
432 symbol11_optional@Base 1.1 1
433 symbol23_mips@Base 2.3
434 symbol24_32@Base 2.4
435 symbol27_big@Base 2.7
436 symbol31_randomtag@Base 3.1
437 symbol42_mips_and_optional@Base 4.2
438 symbol51_untagged@Base 5.1
439 ', 'template vs. non-template on mips');
441 # Dumping in non-template mode (i386) (test for arch tags)
442 open $io, '>', \$io_data or die "cannot open io string\n";
443 $sym_file = Dpkg::Shlibs::SymbolFile->new(file => "$datadir/basictags.symbols", arch => 'i386');
444 $sym_file_dup = Dpkg::Shlibs::SymbolFile->new(file => "$datadir/basictags.symbols", arch => 'i386');
445 $sym_file->output($io);
446 is($io_data,
447 'libbasictags.so.1 libbasictags1 #MINVER#
448 | libbasictags1 (>= 1.1)
449 symbol11_optional@Base 1.1 1
450 symbol22_i386@Base 2.2
451 symbol24_32@Base 2.4
452 symbol26_little@Base 2.6
453 symbol28_little_32@Base 2.8
454 symbol31_randomtag@Base 3.1
455 symbol41_i386_and_optional@Base 4.1
456 symbol51_untagged@Base 5.1
457 ', 'template vs. non-template on i386');
459 ok (defined $sym_file->{objects}{'libbasictags.so.1'}{syms}{'symbol21_amd64@Base'},
460 'syms keys are symbol names without quotes');
462 # Preload objdumps
463 my $tags_obj_i386 = Dpkg::Shlibs::Objdump::Object->new();
464 open $objdump, '<', "$datadir/objdump.basictags-i386"
465 or die "$datadir/objdump.basictags-i386: $!";
466 $tags_obj_i386->parse_objdump_output($objdump);
467 close $objdump;
468 $sym_file->merge_symbols($tags_obj_i386, '100.MISSING');
469 is_deeply($sym_file, $sym_file_dup, 'is objdump.basictags-i386 and basictags.symbols in sync');
471 my $tags_obj_amd64 = Dpkg::Shlibs::Objdump::Object->new();
472 open $objdump, '<', "$datadir/objdump.basictags-amd64"
473 or die "$datadir/objdump.basictags-amd64: $!";
474 $tags_obj_amd64->parse_objdump_output($objdump);
475 close $objdump;
477 # Merge/get_{new,lost} tests for optional tag:
478 # - disappeared
479 my $symbol11 = $tags_obj_i386->get_symbol('symbol11_optional@Base');
480 delete $tags_obj_i386->{dynsyms}{'symbol11_optional@Base'};
481 $sym_file->merge_symbols($tags_obj_i386, '100.MISSING');
483 $sym = $sym_file->lookup_symbol('symbol11_optional@Base', ['libbasictags.so.1'], 1);
484 is_deeply($sym, Dpkg::Shlibs::Symbol->new(symbol => 'symbol11_optional@Base',
485 symbol_templ => 'symbol11_optional@Base',
486 minver => '1.1', dep_id => 1, deprecated => '100.MISSING',
487 tags => { optional => undef }, tagorder => [ 'optional' ]),
488 'disappered optional symbol gets deprecated');
490 $sym_file->merge_symbols($tags_obj_i386, '101.MISSING');
491 $sym = $sym_file->lookup_symbol('symbol11_optional@Base', ['libbasictags.so.1'], 1);
492 is_deeply($sym, Dpkg::Shlibs::Symbol->new(symbol => 'symbol11_optional@Base',
493 symbol_templ => 'symbol11_optional@Base',
494 minver => '1.1', dep_id => 1, deprecated => '101.MISSING',
495 tags => { optional => undef }, tagorder => [ 'optional' ]),
496 'deprecated text of MISSING optional symbol gets rebumped each merge');
498 is( scalar($sym_file->get_lost_symbols($sym_file_dup)), 0, 'missing optional symbol is not LOST');
500 # - reappeared (undeprecate, minver should be 1.1, not 100.MISSED)
501 $tags_obj_i386->add_dynamic_symbol($symbol11);
502 $sym_file->merge_symbols($tags_obj_i386, '100.MISSING');
503 $sym = $sym_file->lookup_symbol('symbol11_optional@Base', ['libbasictags.so.1']);
504 is_deeply($sym, Dpkg::Shlibs::Symbol->new(symbol => 'symbol11_optional@Base',
505 symbol_templ => 'symbol11_optional@Base',
506 minver => '1.1', dep_id => 1, deprecated => 0,
507 tags => { optional => undef }, tagorder => [ 'optional' ]),
508 'reappeared optional symbol gets undeprecated + minver');
509 is( scalar($sym_file->get_lost_symbols($sym_file_dup) +
510 $sym_file->get_new_symbols($sym_file_dup)), 0, 'reappeared optional symbol: neither NEW nor LOST');
512 # Merge/get_{new,lost} tests for arch tag:
513 # - arch specific appears on wrong arch: 'arch' tag should be removed
514 my $symbol21 = $tags_obj_amd64->get_symbol('symbol21_amd64@Base');
515 $tags_obj_i386->add_dynamic_symbol($symbol21);
516 $sym_file->merge_symbols($tags_obj_i386, '100.MISSING');
517 $sym = $sym_file->lookup_symbol('symbol21_amd64@Base', ['libbasictags.so.1']);
518 is_deeply($sym, Dpkg::Shlibs::Symbol->new(symbol => 'symbol21_amd64@Base',
519 symbol_templ => 'symbol21_amd64@Base', symbol_quoted => "'",
520 minver => '2.1', dep_id => 0, deprecated => 0),
521 'symbol appears on foreign arch, arch tag should be removed');
522 @tmp = map { $_->{symbol}->get_symbolname() } $sym_file->get_new_symbols($sym_file_dup);
523 is_deeply( \@tmp, [ 'symbol21_amd64@Base' ], 'symbol from foreign arch is NEW');
524 is( $sym->get_symbolspec(1), ' symbol21_amd64@Base 2.1', 'no tags => no quotes in the symbol name' );
526 # - arch specific symbol disappears
527 delete $tags_obj_i386->{dynsyms}{'symbol22_i386@Base'};
528 delete $tags_obj_i386->{dynsyms}{'symbol24_32@Base'};
529 delete $tags_obj_i386->{dynsyms}{'symbol26_little@Base'};
530 delete $tags_obj_i386->{dynsyms}{'symbol28_little_32@Base'};
531 delete $tags_obj_i386->{dynsyms}{'symbol41_i386_and_optional@Base'};
532 $sym_file->merge_symbols($tags_obj_i386, '100.MISSING');
534 $sym = $sym_file->lookup_symbol('symbol22_i386@Base', ['libbasictags.so.1'], 1);
535 is_deeply($sym, Dpkg::Shlibs::Symbol->new(symbol => 'symbol22_i386@Base',
536 symbol_templ => 'symbol22_i386@Base',
537 minver => '2.2', dep_id => 0, deprecated => '100.MISSING',
538 tags => { arch => '!amd64 !ia64 !mips' },
539 tagorder => [ 'arch' ]),
540 'disappeared arch specific symbol gets deprecated');
541 $sym = $sym_file->lookup_symbol('symbol24_32@Base', ['libbasictags.so.1'], 1);
542 is_deeply($sym, Dpkg::Shlibs::Symbol->new(symbol => 'symbol24_32@Base',
543 symbol_templ => 'symbol24_32@Base',
544 minver => '2.4', dep_id => 0, deprecated => '100.MISSING',
545 tags => { 'arch-bits' => '32' },
546 tagorder => [ 'arch-bits' ]),
547 'disappeared arch bits specific symbol gets deprecated');
548 $sym = $sym_file->lookup_symbol('symbol26_little@Base', ['libbasictags.so.1'], 1);
549 is_deeply($sym, Dpkg::Shlibs::Symbol->new(symbol => 'symbol26_little@Base',
550 symbol_templ => 'symbol26_little@Base',
551 minver => '2.6', dep_id => 0, deprecated => '100.MISSING',
552 tags => { 'arch-endian' => 'little' },
553 tagorder => [ 'arch-endian' ]),
554 'disappeared arch endian specific symbol gets deprecated');
555 $sym = $sym_file->lookup_symbol('symbol28_little_32@Base', ['libbasictags.so.1'], 1);
556 is_deeply($sym, Dpkg::Shlibs::Symbol->new(symbol => 'symbol28_little_32@Base',
557 symbol_templ => 'symbol28_little_32@Base',
558 minver => '2.8', dep_id => 0, deprecated => '100.MISSING',
559 tags => { 'arch-bits' => '32', 'arch-endian' => 'little' },
560 tagorder => [ 'arch-bits', 'arch-endian' ]),
561 'disappeared arch bits and endian specific symbol gets deprecated');
562 $sym = $sym_file->lookup_symbol('symbol41_i386_and_optional@Base', ['libbasictags.so.1'], 1);
563 is_deeply($sym, Dpkg::Shlibs::Symbol->new(symbol => 'symbol41_i386_and_optional@Base',
564 symbol_templ => 'symbol41_i386_and_optional@Base',
565 symbol_quoted => '"',
566 minver => '4.1', dep_id => 0, deprecated => '100.MISSING',
567 tags => { arch => 'i386', optional => 'reason' },
568 tagorder => [ 'arch', 'optional' ]),
569 'disappeared optional arch specific symbol gets deprecated');
570 @tmp = sort map { $_->{symbol}->get_symbolname() } $sym_file->get_lost_symbols($sym_file_dup);
571 is_deeply(\@tmp, [ 'symbol22_i386@Base', 'symbol24_32@Base',
572 'symbol26_little@Base', 'symbol28_little_32@Base' ],
573 "missing arch specific is LOST, but optional arch specific isn't");
575 # Tests for tagged #includes
576 $sym_file = Dpkg::Shlibs::SymbolFile->new(file => "$datadir/symbols.include-3", arch => 'i386');
577 $sym = $sym_file->lookup_symbol('symbol2_fake1@Base', ['libbasictags.so.2']);
578 is_deeply($sym, Dpkg::Shlibs::Symbol->new(symbol => 'symbol2_fake1@Base',
579 minver => '1.0',
580 tags => { optional => undef, 'random tag' => 'random value' },
581 tagorder => [ 'optional', 'random tag' ] ),
582 'symbols from #included file inherits tags');
583 $sym = $sym_file->lookup_symbol('symbol41_i386_and_optional@Base', ['libbasictags.so.1']);
584 is_deeply($sym, Dpkg::Shlibs::Symbol->new(symbol => 'symbol41_i386_and_optional@Base',
585 symbol_templ => 'symbol41_i386_and_optional@Base',
586 symbol_quoted => '"',
587 minver => '4.1',
588 tags => { optional => 'reason', t => 'v', arch => 'i386' },
589 tagorder => [ 'optional', 't', 'arch' ]),
590 'symbols in #included file can override tag values');
591 $sym = $sym_file->lookup_symbol('symbol51_untagged@Base', ['libbasictags.so.1']);
592 is_deeply($sym, Dpkg::Shlibs::Symbol->new(symbol => 'symbol51_untagged@Base',
593 minver => '5.1',
594 tags => { optional => 'from parent', t => 'v' },
595 tagorder => [ 'optional', 't' ]),
596 'symbols are properly cloned when #including');
598 # Test Symbol::clone()
599 $sym = Dpkg::Shlibs::Symbol->new(symbol => 'foobar', testfield => 1, teststruct => { foo => 1 });
600 $tmp = $sym->clone();
601 $tmp->{teststruct}{foo} = 3;
602 $tmp->{testfield} = 3;
603 is ( $sym->{teststruct}{foo}, 1, 'original field "foo" not changed' );
604 is ( $sym->{testfield}, 1, 'original field "testfield" not changed' );
606 ############ Test symbol patterns ###########
608 SKIP: {
610 skip 'c++filt not available', 41 if not can_run('c++filt');
612 sub load_patterns_obj {
613 $obj = Dpkg::Shlibs::Objdump::Object->new();
614 open $objdump, '<', "$datadir/objdump.patterns"
615 or die "$datadir/objdump.patterns: $!";
616 $obj->parse_objdump_output($objdump);
617 close $objdump;
618 return $obj;
621 sub load_patterns_symbols {
622 $sym_file = Dpkg::Shlibs::SymbolFile->new(file => "$datadir/patterns.symbols");
623 return $sym_file;
626 load_patterns_obj();
627 $sym_file_dup = load_patterns_symbols();
628 load_patterns_symbols();
630 save_load_test($sym_file, 'save -> load test of patterns template', template_mode => 1);
632 isnt( $sym_file->get_patterns('libpatterns.so.1') , 0,
633 'patterns.symbols has patterns');
635 $sym_file->merge_symbols($obj, '100.MISSING');
637 @tmp = map { $_->get_symbolname() } $sym_file->get_lost_symbols($sym_file_dup);
638 is_deeply(\@tmp, [], 'no LOST symbols if all patterns matched.');
639 @tmp = map { $_->get_symbolname() } $sym_file->get_new_symbols($sym_file_dup);
640 is_deeply(\@tmp, [], 'no NEW symbols if all patterns matched.');
642 # Pattern resolution order: aliases (c++, symver), generic
643 $sym = $sym_file->lookup_symbol('SYMVER_1@SYMVER_1', 'libpatterns.so.1');
644 is($sym->{minver}, '1', 'specific SYMVER_1 symbol');
646 $sym = $sym_file->lookup_symbol('_ZN3NSB6Symver14symver_method1Ev@SYMVER_1', 'libpatterns.so.1');
647 is($sym->{minver}, '1.method1', 'specific symbol preferred over pattern');
649 $sym = $sym_file->lookup_symbol('_ZN3NSB6Symver14symver_method2Ev@SYMVER_1', 'libpatterns.so.1');
650 is($sym->{minver}, '1.method2', 'c++ alias pattern preferred over generic pattern');
651 is($sym->get_pattern()->get_symbolname(), 'NSB::Symver::symver_method2()@SYMVER_1',
652 'c++ alias pattern preferred over generic pattern, on demangled name');
654 $sym = $sym_file->lookup_symbol('_ZN3NSB6SymverD1Ev@SYMVER_1', 'libpatterns.so.1');
655 is ( $sym->{minver}, '1.generic', 'generic (c++ & symver) pattern covers the rest (destructor)' );
656 ok($sym->get_pattern()->equals($sym_file->create_symbol('(c++|symver)SYMVER_1 1.generic')),
657 'generic (c++ & symver) pattern covers the rest (destructor), compared');
659 # Test old style wildcard support
660 load_patterns_symbols();
661 $sym = $sym_file->create_symbol('*@SYMVEROPT_2 2');
662 ok($sym->is_optional(), 'Old style wildcard is optional');
663 is($sym->get_alias_type(), 'symver', 'old style wildcard is a symver pattern');
664 is($sym->get_symbolname(), 'SYMVEROPT_2', 'wildcard pattern got renamed');
666 $pat = $sym_file->lookup_pattern('(symver|optional)SYMVEROPT_2', 'libpatterns.so.1');
667 $sym->{symbol_templ} = $pat->{symbol_templ};
668 is_deeply($pat, $sym, 'old style wildcard is the same as (symver|optional)');
670 # Get rid of all SymverOptional symbols
671 foreach my $tmp (keys %{$obj->{dynsyms}}) {
672 delete $obj->{dynsyms}{$tmp} if ($tmp =~ /SymverOptional/);
674 $sym_file->merge_symbols($obj, '100.MISSING');
675 is_deeply ( [ map { $_->get_symbolname() } $pat->get_pattern_matches() ],
676 [], 'old style wildcard matches nothing.');
677 is($pat->{deprecated}, '100.MISSING', 'old style wildcard gets deprecated.');
678 @tmp = map { $_->{symbol}->get_symbolname() } $sym_file->get_lost_symbols($sym_file_dup);
679 is_deeply(\@tmp, [], 'but old style wildcard is not LOST.');
681 # 'Internal' pattern covers all internal symbols
682 load_patterns_obj();
683 @tmp = grep { $_->get_symbolname() =~ /Internal/ } $sym_file->get_symbols('libpatterns.so.1');
684 $sym = $sym_file->create_symbol('(regex|c++)^_Z(T[ISV])?N3NSA6ClassA8Internal.*@Base$ 1.internal');
685 $pat = $sym_file->lookup_pattern($sym, 'libpatterns.so.1');
686 is_deeply ([ sort $pat->get_pattern_matches() ], [ sort @tmp ],
687 'Pattern covers all internal symbols');
688 is($tmp[0]->{minver}, '1.internal', 'internal pattern covers first symbol');
690 # Lookup private pattern
691 my @private_symnames = sort qw(
692 _ZN3NSA6ClassA7Private11privmethod1Ei@Base
693 _ZN3NSA6ClassA7Private11privmethod2Ei@Base
694 _ZN3NSA6ClassA7PrivateC1Ev@Base
695 _ZN3NSA6ClassA7PrivateC2Ev@Base
696 _ZN3NSA6ClassA7PrivateD0Ev@Base
697 _ZN3NSA6ClassA7PrivateD1Ev@Base
698 _ZN3NSA6ClassA7PrivateD2Ev@Base
699 _ZTIN3NSA6ClassA7PrivateE@Base
700 _ZTSN3NSA6ClassA7PrivateE@Base
701 _ZTVN3NSA6ClassA7PrivateE@Base
703 $sym = $sym_file->create_symbol('(c++|regex|optional)NSA::ClassA::Private(::.*)?@Base 1');
704 $pat = $sym_file->lookup_pattern($sym, 'libpatterns.so.1');
705 isnt( $pat, undef, 'pattern for private class has been found' );
706 is_deeply( [ sort map { $_->get_symbolname() } $pat->get_pattern_matches() ],
707 \@private_symnames, 'private pattern matched expected symbols');
708 ok( ($pat->get_pattern_matches())[0]->is_optional(),
709 'private symbol is optional like its pattern');
710 ok( $sym_file->lookup_symbol(($pat->get_pattern_matches())[0], 'libpatterns.so.1'),
711 'lookup_symbol() finds symbols matched by pattern (after merge)');
713 # Get rid of a private symbol, it should not be lost
714 delete $obj->{dynsyms}{$private_symnames[0]};
715 load_patterns_symbols();
716 $sym_file->merge_symbols($obj, '100.MISSING');
718 $pat = $sym_file->lookup_pattern($sym, 'libpatterns.so.1');
719 @tmp = map { $_->{symbol}->get_symbolname() } $sym_file->get_lost_symbols($sym_file_dup);
720 is_deeply(\@tmp, [], 'no LOST symbols when got rid of patterned optional symbol.');
721 ok(!$pat->{deprecated}, 'there are still matches, pattern is not deprecated.');
723 # Get rid of all private symbols, the pattern should be deprecated.
724 foreach my $tmp (@private_symnames) {
725 delete $obj->{dynsyms}{$tmp};
727 load_patterns_symbols();
728 $sym_file->merge_symbols($obj, '100.MISSING');
730 $pat = $sym_file->lookup_pattern($sym, 'libpatterns.so.1', 1);
731 @tmp = $sym_file->get_lost_symbols($sym_file_dup);
732 is_deeply( \@tmp, [ ],
733 'All private symbols gone, but pattern is not LOST because it is optional.');
734 is( $pat->{deprecated}, '100.MISSING',
735 'All private symbols gone - pattern deprecated.');
737 # Internal symbols. All covered by the pattern?
738 @tmp = grep { $_->get_symbolname() =~ /Internal/ } values %{$sym_file->{objects}{'libpatterns.so.1'}{syms}};
739 $sym = $sym_file->create_symbol('(regex|c++)^_Z(T[ISV])?N3NSA6ClassA8Internal.*@Base$ 1.internal');
740 $pat = $sym_file->lookup_pattern($sym, 'libpatterns.so.1');
741 is_deeply ([ sort $pat->get_pattern_matches() ], [ sort @tmp ],
742 'Pattern covers all internal symbols');
743 is($tmp[0]->{minver}, '1.internal', 'internal pattern covers first symbol');
745 # Delete matches of the non-optional pattern
746 $sym = $sym_file->create_symbol('(c++)"non-virtual thunk to NSB::ClassD::generate_vt(char const*) const@Base" 1');
747 $pat = $sym_file->lookup_pattern($sym, 'libpatterns.so.1');
748 isnt( $pat, undef, 'lookup_pattern() finds alias-based pattern' );
750 is(scalar($pat->get_pattern_matches()), 2, 'two matches for the generate_vt pattern');
751 foreach my $tmp ($pat->get_pattern_matches()) {
752 delete $obj->{dynsyms}{$tmp->get_symbolname()};
754 load_patterns_symbols();
755 $sym_file->merge_symbols($obj, '100.MISSING');
757 $pat = $sym_file->lookup_pattern($sym, 'libpatterns.so.1', 1);
758 @tmp = map { scalar $sym_file->lookup_pattern($_->{symbol}, 'libpatterns.so.1', 1) }
759 $sym_file->get_lost_symbols($sym_file_dup);
760 is_deeply(\@tmp, [ $pat ], 'No matches - generate_vt() pattern is LOST.');
761 is( $pat->{deprecated}, '100.MISSING',
762 'No matches - generate_vt() pattern is deprecated.');
764 # Pattern undeprecation when matches are discovered
765 load_patterns_obj();
766 load_patterns_symbols();
768 $pat = $sym_file_dup->lookup_pattern($sym, 'libpatterns.so.1');
769 $pat->{deprecated} = '0.1-1';
770 $pat = $sym_file->lookup_pattern($sym, 'libpatterns.so.1');
771 $pat->{deprecated} = '0.1-1';
773 $sym_file->merge_symbols($obj, '100.FOUND');
774 ok( ! $pat->{deprecated},
775 'Previously deprecated pattern with matches got undeprecated');
776 is( $pat->{minver}, '100.FOUND',
777 'Previously deprecated pattern with matches got minver bumped');
778 @tmp = map { $_->{symbol}->get_symbolspec(1) } $sym_file->get_new_symbols($sym_file_dup);
779 is_deeply( \@tmp, [ $pat->get_symbolspec(1) ],
780 'Previously deprecated pattern with matches is NEW. Matches themselves are not NEW.');
781 foreach my $sym ($pat->get_pattern_matches()) {
782 ok(!$sym->{deprecated}, $sym->get_symbolname() . ': not deprecated');
783 is($sym->{minver}, '100.FOUND', $sym->get_symbolname() . ': version bumped');