Add an error message test recipes for system error messages
[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 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
8
9 use strict;
10 no strict 'refs';               # To be able to use strings as function refs
11 use OpenSSL::Test;
12 use Errno qw(:POSIX);
13 use POSIX qw(strerror);
14
15 # We actually have space for up to 4095 error messages,
16 # numerically speaking...  but we're currently only using
17 # numbers 1 through 127.
18 # This constant should correspond to the same constant
19 # defined in crypto/err/err.c, or at least must not be
20 # assigned a greater number.
21 use constant NUM_SYS_STR_REASONS => 127;
22
23 setup('test_errstr');
24
25 # These are POSIX error names, which Errno implements as functions
26 # (this is documented)
27 my @posix_errors = @{$Errno::EXPORT_TAGS{POSIX}};
28
29 plan tests => scalar @posix_errors
30     +1                          # Checking that error 128 gives 'reason(128)'
31     +1                          # Checking that error 0 gives the library name
32     ;
33
34 foreach my $errname (@posix_errors) {
35     my $errnum = "Errno::$errname"->();
36
37  SKIP: {
38         skip "Error $errname ($errnum) isn't within our range", 1
39             if $errnum > NUM_SYS_STR_REASONS;
40
41         my $perr = eval {
42             # Set $! to the error number...
43             local $! = $errnum;
44             # ... and $! will give you the error string back
45             $!
46         };
47
48         # We know that the system reasons are in OpenSSL error library 2
49         my @oerr = run(app([ qw(openssl errstr), sprintf("2%06x", $errnum) ]),
50                        capture => 1);
51         $oerr[0] =~ s|\R$||;
52         $oerr[0] =~ s|.*system library:||g; # The actual message is last
53
54         ok($oerr[0] eq $perr, "($errnum) '$oerr[0]' == '$perr'");
55     }
56 }
57
58 my @after = run(app([ qw(openssl errstr 2000080) ]), capture => 1);
59 $after[0] =~ s|\R$||;
60 $after[0] =~ s|.*system library:||g;
61 ok($after[0] eq "reason(128)", "(128) '$after[0]' == 'reason(128)'");
62
63 my @zero = run(app([ qw(openssl errstr 2000000) ]), capture => 1);
64 $zero[0] =~ s|\R$||;
65 $zero[0] =~ s|.*system library:||g;
66 ok($zero[0] eq "system library", "(0) '$zero[0]' == 'system library'");