02-test_errstr.t 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. #! /usr/bin/env perl
  2. # Copyright 2018-2020 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. use strict;
  9. no strict 'refs'; # To be able to use strings as function refs
  10. use OpenSSL::Test;
  11. use OpenSSL::Test::Utils;
  12. use Errno qw(:POSIX);
  13. use POSIX qw(:limits_h strerror);
  14. use Data::Dumper;
  15. setup('test_errstr');
  16. # In a cross compiled situation, there are chances that our
  17. # application is linked against different C libraries than
  18. # perl, and may thereby get different error messages for the
  19. # same error.
  20. # The safest is not to test under such circumstances.
  21. plan skip_all => 'This is unsupported for cross compiled configurations'
  22. if config('CROSS_COMPILE');
  23. # The same can be said when compiling OpenSSL with mingw configuration
  24. # on Windows when built with msys perl. Similar problems are also observed
  25. # in MSVC builds, depending on the perl implementation used.
  26. plan skip_all => 'This is unsupported on MSYS/MinGW or MSWin32'
  27. if $^O eq 'msys' or $^O eq 'MSWin32';
  28. plan skip_all => 'OpenSSL is configured "no-autoerrinit" or "no-err"'
  29. if disabled('autoerrinit') || disabled('err');
  30. # OpenSSL constants found in <openssl/err.h>
  31. use constant ERR_SYSTEM_FLAG => INT_MAX + 1;
  32. use constant ERR_LIB_OFFSET => 23; # Offset of the "library" errcode section
  33. # OpenSSL "library" numbers
  34. use constant ERR_LIB_NONE => 1;
  35. # We use Errno::EXPORT_OK as a list of known errno values on the current
  36. # system. libcrypto's ERR should either use the same string as perl, or if
  37. # it was outside the range that ERR looks at, ERR gives the reason string
  38. # "reason(nnn)", where nnn is the errno number.
  39. plan tests => scalar @Errno::EXPORT_OK
  40. +1 # Checking that error 128 gives 'reason(128)'
  41. +1 # Checking that error 0 gives the library name
  42. +1; # Check trailing whitespace is removed.
  43. # Test::More:ok() has a sub prototype, which means we need to use the '&ok'
  44. # syntax to force it to accept a list as a series of arguments.
  45. foreach my $errname (@Errno::EXPORT_OK) {
  46. # The error names are perl constants, which are implemented as functions
  47. # returning the numeric value of that name.
  48. my $errcode = "Errno::$errname"->();
  49. SKIP: {
  50. # On most systems, there is no E macro for errcode zero in <errno.h>,
  51. # which means that it seldom comes up here. However, reports indicate
  52. # that some platforms do have an E macro for errcode zero.
  53. # With perl, errcode zero is a bit special. Perl consistently gives
  54. # the empty string for that one, while the C strerror() may give back
  55. # something else. The easiest way to deal with that possible mismatch
  56. # is to skip this errcode.
  57. skip "perl error strings and ssystem error strings for errcode 0 differ", 1
  58. if $errcode == 0;
  59. &ok(match_syserr_reason($errcode));
  60. }
  61. }
  62. # OpenSSL library 1 is the "unknown" library
  63. &ok(match_opensslerr_reason(ERR_LIB_NONE << ERR_LIB_OFFSET | 256,
  64. "reason(256)"));
  65. # Reason code 0 of any library gives the library name as reason
  66. &ok(match_opensslerr_reason(ERR_LIB_NONE << ERR_LIB_OFFSET | 0,
  67. "unknown library"));
  68. &ok(match_any("Trailing whitespace \n\t", "?", ( "Trailing whitespace" )));
  69. exit 0;
  70. # For an error string "error:xxxxxxxx:lib:func:reason", this returns
  71. # the following array:
  72. #
  73. # ( "xxxxxxxx", "lib", "func", "reason" )
  74. sub split_error {
  75. # Limit to 5 items, in case the reason contains a colon
  76. my @erritems = split /:/, $_[0], 5;
  77. # Remove the first item, which is always "error"
  78. shift @erritems;
  79. return @erritems;
  80. }
  81. # Compares the first argument as string to each of the arguments 3 and on,
  82. # and returns an array of two elements:
  83. # 0: True if the first argument matched any of the others, otherwise false
  84. # 1: A string describing the test
  85. # The returned array can be used as the arguments to Test::More::ok()
  86. sub match_any {
  87. my $first = shift;
  88. my $desc = shift;
  89. my @strings = @_;
  90. # ignore trailing whitespace
  91. $first =~ s/\s+$//;
  92. if (scalar @strings > 1) {
  93. $desc = "match '$first' ($desc) with one of ( '"
  94. . join("', '", @strings) . "' )";
  95. } else {
  96. $desc = "match '$first' ($desc) with '$strings[0]'";
  97. }
  98. return ( scalar( grep { $first eq $_ } @strings ) > 0,
  99. $desc );
  100. }
  101. sub match_opensslerr_reason {
  102. my $errcode = shift;
  103. my @strings = @_;
  104. my $errcode_hex = sprintf "%x", $errcode;
  105. my $reason =
  106. ( run(app([ qw(openssl errstr), $errcode_hex ]), capture => 1) )[0];
  107. $reason =~ s|\R$||;
  108. $reason = ( split_error($reason) )[3];
  109. return match_any($reason, $errcode, @strings);
  110. }
  111. sub match_syserr_reason {
  112. my $errcode = shift;
  113. my @strings = ();
  114. # The POSIX reason string
  115. push @strings, eval {
  116. # Set $! to the error number...
  117. local $! = $errcode;
  118. # ... and $! will give you the error string back
  119. $!
  120. };
  121. # The OpenSSL fallback string
  122. push @strings, "reason($errcode)";
  123. return match_opensslerr_reason(ERR_SYSTEM_FLAG | $errcode, @strings);
  124. }