]> git.ipfire.org Git - thirdparty/openssl.git/blob - test/recipes/01-test_symbol_presence.t
e979c18f46f60e066f30bc861497d37bfb2504c6
[thirdparty/openssl.git] / test / recipes / 01-test_symbol_presence.t
1 #! /usr/bin/env perl
2 # -*- mode: Perl -*-
3 # Copyright 2016-2020 The OpenSSL Project Authors. All Rights Reserved.
4 #
5 # Licensed under the Apache License 2.0 (the "License"). You may not use
6 # this file except in compliance with the License. You can obtain a copy
7 # in the file LICENSE in the source distribution or at
8 # https://www.openssl.org/source/license.html
9
10 use strict;
11 use File::Spec::Functions qw(devnull);
12 use OpenSSL::Test qw(:DEFAULT srctop_file srctop_dir bldtop_dir bldtop_file);
13 use OpenSSL::Test::Utils;
14
15 BEGIN {
16 setup("test_symbol_presence");
17 }
18
19 use lib srctop_dir('Configurations');
20 use lib bldtop_dir('.');
21 use platform;
22
23 plan skip_all => "Test is disabled on NonStop" if config('target') =~ m|^nonstop|;
24 plan skip_all => "Only useful when building shared libraries"
25 if disabled("shared");
26
27 my @libnames = ("crypto", "ssl");
28 my $testcount = scalar @libnames;
29
30 plan tests => $testcount * 2;
31
32 note
33 "NOTE: developer test! It's possible that it won't run on your\n",
34 "platform, and that's perfectly fine. This is mainly for developers\n",
35 "on Unix to check that our shared libraries are consistent with the\n",
36 "ordinals (util/*.num in the source tree), something that should be\n",
37 "good enough a check for the other platforms as well.\n";
38
39 foreach my $libname (@libnames) {
40 SKIP:
41 {
42 my $shlibpath = bldtop_file(platform->sharedlib("lib$libname"));
43 *OSTDERR = *STDERR;
44 *OSTDOUT = *STDOUT;
45 open STDERR, ">", devnull();
46 open STDOUT, ">", devnull();
47 my @nm_lines = map { s|\R$||; $_ } `nm -Pg $shlibpath 2> /dev/null`;
48 close STDERR;
49 close STDOUT;
50 *STDERR = *OSTDERR;
51 *STDOUT = *OSTDOUT;
52 skip "Can't run 'nm -Pg $shlibpath' => $?... ignoring", 2
53 unless $? == 0;
54
55 my $bldtop = bldtop_dir();
56 my @def_lines;
57 indir $bldtop => sub {
58 my $mkdefpath = srctop_file("util", "mkdef.pl");
59 my $libnumpath = srctop_file("util", "lib$libname.num");
60 @def_lines = map { s|\R$||; $_ } `$^X $mkdefpath --ordinals $libnumpath --name $libname --OS linux 2> /dev/null`;
61 ok($? == 0, "running 'cd $bldtop; $^X $mkdefpath --ordinals $libnumpath --name $libname --OS linux' => $?");
62 }, create => 0, cleanup => 0;
63
64 note "Number of lines in \@nm_lines before massaging: ", scalar @nm_lines;
65 note "Number of lines in \@def_lines before massaging: ", scalar @def_lines;
66
67 # Massage the nm output to only contain defined symbols
68 @nm_lines = sort map { s| .*||; $_ } grep(m|.* [BCDST] .*|, @nm_lines);
69
70 # Massage the mkdef.pl output to only contain global symbols
71 # The output we got is in Unix .map format, which has a global
72 # and a local section. We're only interested in the global
73 # section.
74 my $in_global = 0;
75 @def_lines =
76 sort
77 map { s|;||; s|\s+||g; $_ }
78 grep { $in_global = 1 if m|global:|;
79 $in_global = 0 if m|local:|;
80 $in_global = 0 if m|\}|;
81 $in_global && m|;|; } @def_lines;
82
83 note "Number of lines in \@nm_lines after massaging: ", scalar @nm_lines;
84 note "Number of lines in \@def_lines after massaging: ", scalar @def_lines;
85
86 # Maintain lists of symbols that are missing in the shared library,
87 # or that are extra.
88 my @missing = ();
89 my @extra = ();
90
91 while (scalar @nm_lines || scalar @def_lines) {
92 my $nm_first = $nm_lines[0];
93 my $def_first = $def_lines[0];
94
95 if (!defined($nm_first)) {
96 push @missing, shift @def_lines;
97 } elsif (!defined($def_first)) {
98 push @extra, shift @nm_lines;
99 } elsif ($nm_first gt $def_first) {
100 push @missing, shift @def_lines;
101 } elsif ($nm_first lt $def_first) {
102 push @extra, shift @nm_lines;
103 } else {
104 shift @def_lines;
105 shift @nm_lines;
106 }
107 }
108
109 if (scalar @missing) {
110 note "The following symbols are missing in lib$libname.so:";
111 foreach (@missing) {
112 note " $_";
113 }
114 }
115 if (scalar @extra) {
116 note "The following symbols are extra in lib$libname.so:";
117 foreach (@extra) {
118 note " $_";
119 }
120 }
121 ok(scalar @missing == 0,
122 "check that there are no missing symbols in lib$libname.so");
123 }
124 }