test/recipes/02-err_errstr: skip errors that may not be loaded on Windows
[openssl.git] / test / recipes / 02-test_errstr.t
1 #! /usr/bin/env perl
2 # Copyright 2018 The OpenSSL Project Authors. All Rights Reserved.
3 #
4 # Licensed under the Apache License 2.0 (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
8
9 use strict;
10 no strict 'refs';               # To be able to use strings as function refs
11 use OpenSSL::Test;
12 use OpenSSL::Test::Utils;
13 use Errno qw(:POSIX);
14 use POSIX qw(strerror);
15
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;
23
24 setup('test_errstr');
25
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
29 # same error.
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');
33
34 plan skip_all => 'OpenSSL is configured "no-autoerrinit" or "no-err"'
35     if disabled('autoerrinit') || disabled('err');
36
37 # These are POSIX error names, which Errno implements as functions
38 # (this is documented)
39 my @posix_errors = @{$Errno::EXPORT_TAGS{POSIX}};
40
41 if ($^O eq 'MSWin32') {
42     # On Windows, these errors have been observed to not always be loaded by
43     # apps/openssl, while they are in perl, which causes a difference that we
44     # consider a false alarm.  So we skip checking these errors.
45     # Because we can't know exactly what symbols exist in a perticular perl
46     # version, we resort to discovering them directly in the Errno package
47     # symbol table.
48     my @error_skiplist = qw(
49         ENETDOWN
50         ENETUNREACH
51         ENETRESET
52         ECONNABORTED
53         EISCONN
54         ENOTCONN
55         ESHUTDOWN
56         ETOOMANYREFS
57         ETIMEDOUT
58         EHOSTDOWN
59         EHOSTUNREACH
60         EALREADY
61         EINPROGRESS
62         ESTALE
63         EUCLEAN
64         ENOTNAM
65         ENAVAIL
66         ENOMEDIUM
67         ENOKEY
68     );
69     @posix_errors =
70         grep {
71             my $x = $_;
72             ! grep {
73                 exists $Errno::{$_} && $x == $Errno::{$_}
74             } @error_skiplist
75         } @posix_errors;
76 }
77
78 plan tests => scalar @posix_errors
79     +1                          # Checking that error 128 gives 'reason(128)'
80     +1                          # Checking that error 0 gives the library name
81     ;
82
83 foreach my $errname (@posix_errors) {
84     my $errnum = "Errno::$errname"->();
85
86  SKIP: {
87         skip "Error $errname ($errnum) isn't within our range", 1
88             if $errnum > NUM_SYS_STR_REASONS;
89
90         my $perr = eval {
91             # Set $! to the error number...
92             local $! = $errnum;
93             # ... and $! will give you the error string back
94             $!
95         };
96
97         # We know that the system reasons are in OpenSSL error library 2
98         my @oerr = run(app([ qw(openssl errstr), sprintf("2%06x", $errnum) ]),
99                        capture => 1);
100         $oerr[0] =~ s|\R$||;
101         $oerr[0] =~ s|.*system library:||g; # The actual message is last
102
103         ok($oerr[0] eq $perr, "($errnum) '$oerr[0]' == '$perr'");
104     }
105 }
106
107 my @after = run(app([ qw(openssl errstr 2000080) ]), capture => 1);
108 $after[0] =~ s|\R$||;
109 $after[0] =~ s|.*system library:||g;
110 ok($after[0] eq "reason(128)", "(128) '$after[0]' == 'reason(128)'");
111
112 my @zero = run(app([ qw(openssl errstr 2000000) ]), capture => 1);
113 $zero[0] =~ s|\R$||;
114 $zero[0] =~ s|.*system library:||g;
115 ok($zero[0] eq "system library", "(0) '$zero[0]' == 'system library'");