1e1051a39Sopenharmony_ci#! /usr/bin/env perl
2e1051a39Sopenharmony_ci# Copyright 2016-2021 The OpenSSL Project Authors. All Rights Reserved.
3e1051a39Sopenharmony_ci#
4e1051a39Sopenharmony_ci# Licensed under the Apache License 2.0 (the "License").  You may not use
5e1051a39Sopenharmony_ci# this file except in compliance with the License.  You can obtain a copy
6e1051a39Sopenharmony_ci# in the file LICENSE in the source distribution or at
7e1051a39Sopenharmony_ci# https://www.openssl.org/source/license.html
8e1051a39Sopenharmony_ci
9e1051a39Sopenharmony_ciuse OpenSSL::Test qw/:DEFAULT srctop_dir bldtop_dir/;
10e1051a39Sopenharmony_ciuse OpenSSL::Test::Utils;
11e1051a39Sopenharmony_ci
12e1051a39Sopenharmony_ci#Load configdata.pm
13e1051a39Sopenharmony_ci
14e1051a39Sopenharmony_ciBEGIN {
15e1051a39Sopenharmony_ci    setup("test_shlibload");
16e1051a39Sopenharmony_ci}
17e1051a39Sopenharmony_ciuse lib srctop_dir('Configurations');
18e1051a39Sopenharmony_ciuse lib bldtop_dir('.');
19e1051a39Sopenharmony_ciuse platform;
20e1051a39Sopenharmony_ci
21e1051a39Sopenharmony_ciplan skip_all => "Test only supported in a shared build" if disabled("shared");
22e1051a39Sopenharmony_ciplan skip_all => "Test is disabled on AIX" if config('target') =~ m|^aix|;
23e1051a39Sopenharmony_ciplan skip_all => "Test is disabled on NonStop" if config('target') =~ m|^nonstop|;
24e1051a39Sopenharmony_ciplan skip_all => "Test only supported in a dso build" if disabled("dso");
25e1051a39Sopenharmony_ciplan skip_all => "Test is disabled in an address sanitizer build" unless disabled("asan");
26e1051a39Sopenharmony_ci
27e1051a39Sopenharmony_ciplan tests => 10;
28e1051a39Sopenharmony_ci
29e1051a39Sopenharmony_cimy $libcrypto = platform->sharedlib('libcrypto');
30e1051a39Sopenharmony_cimy $libssl = platform->sharedlib('libssl');
31e1051a39Sopenharmony_cimy $atexit_outfile;
32e1051a39Sopenharmony_ci
33e1051a39Sopenharmony_ci$atexit_outfile = 'atexit-cryptofirst.txt';
34e1051a39Sopenharmony_ci1 while unlink $atexit_outfile;
35e1051a39Sopenharmony_ciok(run(test(["shlibloadtest", "-crypto_first", $libcrypto, $libssl, $atexit_outfile])),
36e1051a39Sopenharmony_ci   "running shlibloadtest -crypto_first $atexit_outfile");
37e1051a39Sopenharmony_ciok(check_atexit($atexit_outfile));
38e1051a39Sopenharmony_ci
39e1051a39Sopenharmony_ci$atexit_outfile = 'atexit-sslfirst.txt';
40e1051a39Sopenharmony_ci1 while unlink $atexit_outfile;
41e1051a39Sopenharmony_ciok(run(test(["shlibloadtest", "-ssl_first", $libcrypto, $libssl, $atexit_outfile])),
42e1051a39Sopenharmony_ci   "running shlibloadtest -ssl_first $atexit_outfile");
43e1051a39Sopenharmony_ciok(check_atexit($atexit_outfile));
44e1051a39Sopenharmony_ci
45e1051a39Sopenharmony_ci$atexit_outfile = 'atexit-justcrypto.txt';
46e1051a39Sopenharmony_ci1 while unlink $atexit_outfile;
47e1051a39Sopenharmony_ciok(run(test(["shlibloadtest", "-just_crypto", $libcrypto, $libssl, $atexit_outfile])),
48e1051a39Sopenharmony_ci   "running shlibloadtest -just_crypto $atexit_outfile");
49e1051a39Sopenharmony_ciok(check_atexit($atexit_outfile));
50e1051a39Sopenharmony_ci
51e1051a39Sopenharmony_ci$atexit_outfile = 'atexit-dsoref.txt';
52e1051a39Sopenharmony_ci1 while unlink $atexit_outfile;
53e1051a39Sopenharmony_ciok(run(test(["shlibloadtest", "-dso_ref", $libcrypto, $libssl, $atexit_outfile])),
54e1051a39Sopenharmony_ci   "running shlibloadtest -dso_ref $atexit_outfile");
55e1051a39Sopenharmony_ciok(check_atexit($atexit_outfile));
56e1051a39Sopenharmony_ci
57e1051a39Sopenharmony_ci$atexit_outfile = 'atexit-noatexit.txt';
58e1051a39Sopenharmony_ci1 while unlink $atexit_outfile;
59e1051a39Sopenharmony_ciok(run(test(["shlibloadtest", "-no_atexit", $libcrypto, $libssl, $atexit_outfile])),
60e1051a39Sopenharmony_ci   "running shlibloadtest -no_atexit $atexit_outfile");
61e1051a39Sopenharmony_ciok(!check_atexit($atexit_outfile));
62e1051a39Sopenharmony_ci
63e1051a39Sopenharmony_cisub check_atexit {
64e1051a39Sopenharmony_ci    my $filename = shift;
65e1051a39Sopenharmony_ci
66e1051a39Sopenharmony_ci    open my $fh, '<', $filename;
67e1051a39Sopenharmony_ci    return 0 unless defined $fh;
68e1051a39Sopenharmony_ci
69e1051a39Sopenharmony_ci    my $data = <$fh>;
70e1051a39Sopenharmony_ci
71e1051a39Sopenharmony_ci    return 1 if (defined $data && $data =~ m/atexit\(\) run/);
72e1051a39Sopenharmony_ci
73e1051a39Sopenharmony_ci    return 0;
74e1051a39Sopenharmony_ci}
75