02-test_errstr.t 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. #! /usr/bin/env perl
  2. # Copyright 2018-2019 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. 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(strerror);
  14. # We actually have space for up to 4095 error messages,
  15. # numerically speaking... but we're currently only using
  16. # numbers 1 through 127.
  17. # This constant should correspond to the same constant
  18. # defined in crypto/err/err.c, or at least must not be
  19. # assigned a greater number.
  20. use constant NUM_SYS_STR_REASONS => 127;
  21. setup('test_errstr');
  22. # In a cross compiled situation, there are chances that our
  23. # application is linked against different C libraries than
  24. # perl, and may thereby get different error messages for the
  25. # same error.
  26. # The safest is not to test under such circumstances.
  27. plan skip_all => 'This is unsupported for cross compiled configurations'
  28. if config('CROSS_COMPILE');
  29. # The same can be said when compiling OpenSSL with mingw configuration
  30. # on Windows when built with msys perl. Similar problems are also observed
  31. # in MSVC builds, depending on the perl implementation used.
  32. plan skip_all => 'This is unsupported on MSYS/MinGW or MSWin32'
  33. if $^O eq 'msys' or $^O eq 'MSWin32';
  34. plan skip_all => 'OpenSSL is configured "no-autoerrinit" or "no-err"'
  35. if disabled('autoerrinit') || disabled('err');
  36. # These are POSIX error names, which Errno implements as functions
  37. # (this is documented)
  38. my @posix_errors = @{$Errno::EXPORT_TAGS{POSIX}};
  39. if ($^O eq 'MSWin32') {
  40. # On Windows, these errors have been observed to not always be loaded by
  41. # apps/openssl, while they are in perl, which causes a difference that we
  42. # consider a false alarm. So we skip checking these errors.
  43. # Because we can't know exactly what symbols exist in a perticular perl
  44. # version, we resort to discovering them directly in the Errno package
  45. # symbol table.
  46. my @error_skiplist = qw(
  47. ENETDOWN
  48. ENETUNREACH
  49. ENETRESET
  50. ECONNABORTED
  51. EISCONN
  52. ENOTCONN
  53. ESHUTDOWN
  54. ETOOMANYREFS
  55. ETIMEDOUT
  56. EHOSTDOWN
  57. EHOSTUNREACH
  58. EALREADY
  59. EINPROGRESS
  60. ESTALE
  61. EUCLEAN
  62. ENOTNAM
  63. ENAVAIL
  64. ENOMEDIUM
  65. ENOKEY
  66. );
  67. @posix_errors =
  68. grep {
  69. my $x = $_;
  70. ! grep {
  71. exists $Errno::{$_} && $x == $Errno::{$_}
  72. } @error_skiplist
  73. } @posix_errors;
  74. }
  75. plan tests => scalar @posix_errors
  76. +1 # Checking that error 128 gives 'reason(128)'
  77. +1 # Checking that error 0 gives the library name
  78. ;
  79. foreach my $errname (@posix_errors) {
  80. my $errnum = "Errno::$errname"->();
  81. SKIP: {
  82. skip "Error $errname ($errnum) isn't within our range", 1
  83. if $errnum > NUM_SYS_STR_REASONS;
  84. my $perr = eval {
  85. # Set $! to the error number...
  86. local $! = $errnum;
  87. # ... and $! will give you the error string back
  88. $!
  89. };
  90. # We know that the system reasons are in OpenSSL error library 2
  91. my @oerr = run(app([ qw(openssl errstr), sprintf("2%06x", $errnum) ]),
  92. capture => 1);
  93. $oerr[0] =~ s|\R$||;
  94. $oerr[0] =~ s|.*system library:||g; # The actual message is last
  95. ok($oerr[0] eq $perr, "($errnum) '$oerr[0]' == '$perr'");
  96. }
  97. }
  98. my @after = run(app([ qw(openssl errstr 2000080) ]), capture => 1);
  99. $after[0] =~ s|\R$||;
  100. $after[0] =~ s|.*system library:||g;
  101. ok($after[0] eq "reason(128)", "(128) '$after[0]' == 'reason(128)'");
  102. my @zero = run(app([ qw(openssl errstr 2000000) ]), capture => 1);
  103. $zero[0] =~ s|\R$||;
  104. $zero[0] =~ s|.*system library:||g;
  105. ok($zero[0] eq "system library", "(0) '$zero[0]' == 'system library'");