2 # Copyright 2018 The OpenSSL Project Authors. All Rights Reserved.
4 # Licensed under the OpenSSL license (the "License"). You may not use
5 # this file except in compliance with the License. You can obtain a copy
6 # in the file LICENSE in the source distribution or at
7 # https://www.openssl.org/source/license.html
10 no strict 'refs'; # To be able to use strings as function refs
12 use OpenSSL::Test::Utils;
14 use POSIX qw(strerror);
16 # We actually have space for up to 4095 error messages,
17 # numerically speaking... but we're currently only using
18 # numbers 1 through 127.
19 # This constant should correspond to the same constant
20 # defined in crypto/err/err.c, or at least must not be
21 # assigned a greater number.
22 use constant NUM_SYS_STR_REASONS => 127;
26 # In a cross compiled situation, there are chances that our
27 # application is linked against different C libraries than
28 # perl, and may thereby get different error messages for the
30 # The safest is not to test under such circumstances.
31 plan skip_all => 'This is unsupported for cross compiled configurations'
32 if config('CROSS_COMPILE');
34 plan skip_all => 'OpenSSL is configured "no-autoerrinit" or "no-err"'
35 if disabled('autoerrinit') || disabled('err');
37 # These are POSIX error names, which Errno implements as functions
38 # (this is documented)
39 my @posix_errors = @{$Errno::EXPORT_TAGS{POSIX}};
41 plan tests => scalar @posix_errors
42 +1 # Checking that error 128 gives 'reason(128)'
43 +1 # Checking that error 0 gives the library name
46 foreach my $errname (@posix_errors) {
47 my $errnum = "Errno::$errname"->();
50 skip "Error $errname ($errnum) isn't within our range", 1
51 if $errnum > NUM_SYS_STR_REASONS;
54 # Set $! to the error number...
56 # ... and $! will give you the error string back
60 # We know that the system reasons are in OpenSSL error library 2
61 my @oerr = run(app([ qw(openssl errstr), sprintf("2%06x", $errnum) ]),
64 $oerr[0] =~ s|.*system library:||g; # The actual message is last
66 ok($oerr[0] eq $perr, "($errnum) '$oerr[0]' == '$perr'");
70 my @after = run(app([ qw(openssl errstr 2000080) ]), capture => 1);
72 $after[0] =~ s|.*system library:||g;
73 ok($after[0] eq "reason(128)", "(128) '$after[0]' == 'reason(128)'");
75 my @zero = run(app([ qw(openssl errstr 2000000) ]), capture => 1);
77 $zero[0] =~ s|.*system library:||g;
78 ok($zero[0] eq "system library", "(0) '$zero[0]' == 'system library'");