Utils.pm 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. # Copyright 2016-2024 The OpenSSL Project Authors. All Rights Reserved.
  2. #
  3. # Licensed under the Apache License 2.0 (the "License"). You may not use
  4. # this file except in compliance with the License. You can obtain a copy
  5. # in the file LICENSE in the source distribution or at
  6. # https://www.openssl.org/source/license.html
  7. package OpenSSL::Test::Utils;
  8. use strict;
  9. use warnings;
  10. use Exporter;
  11. use vars qw($VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS);
  12. $VERSION = "0.1";
  13. @ISA = qw(Exporter);
  14. @EXPORT = qw(alldisabled anydisabled disabled config available_protocols
  15. have_IPv4 have_IPv6);
  16. =head1 NAME
  17. OpenSSL::Test::Utils - test utility functions
  18. =head1 SYNOPSIS
  19. use OpenSSL::Test::Utils;
  20. my @tls = available_protocols("tls");
  21. my @dtls = available_protocols("dtls");
  22. alldisabled("dh", "dsa");
  23. anydisabled("dh", "dsa");
  24. config("fips");
  25. have_IPv4();
  26. have_IPv6();
  27. =head1 DESCRIPTION
  28. This module provides utility functions for the testing framework.
  29. =cut
  30. use OpenSSL::Test qw/:DEFAULT bldtop_file/;
  31. =over 4
  32. =item B<available_protocols STRING>
  33. Returns a list of strings for all the available SSL/TLS versions if
  34. STRING is "tls", or for all the available DTLS versions if STRING is
  35. "dtls". Otherwise, it returns the empty list. The strings in the
  36. returned list can be used with B<alldisabled> and B<anydisabled>.
  37. =item B<alldisabled ARRAY>
  38. =item B<anydisabled ARRAY>
  39. In an array context returns an array with each element set to 1 if the
  40. corresponding feature is disabled and 0 otherwise.
  41. In a scalar context, alldisabled returns 1 if all of the features in
  42. ARRAY are disabled, while anydisabled returns 1 if any of them are
  43. disabled.
  44. =item B<config STRING>
  45. Returns an item from the %config hash in \$TOP/configdata.pm.
  46. =item B<have_IPv4>
  47. =item B<have_IPv6>
  48. Return true if IPv4 / IPv6 is possible to use on the current system.
  49. Additionally, B<have_IPv6> also checks how OpenSSL was configured,
  50. i.e. if IPv6 was explicitly disabled with -DOPENSSL_USE_IPv6=0.
  51. =back
  52. =cut
  53. our %available_protocols;
  54. our %disabled;
  55. our %config;
  56. our %target;
  57. my $configdata_loaded = 0;
  58. sub load_configdata {
  59. # We eval it so it doesn't run at compile time of this file.
  60. # The latter would have bldtop_file() complain that setup() hasn't
  61. # been run yet.
  62. my $configdata = bldtop_file("configdata.pm");
  63. eval { require $configdata;
  64. %available_protocols = %configdata::available_protocols;
  65. %disabled = %configdata::disabled;
  66. %config = %configdata::config;
  67. %target = %configdata::target;
  68. };
  69. $configdata_loaded = 1;
  70. }
  71. # args
  72. # list of 1s and 0s, coming from check_disabled()
  73. sub anyof {
  74. my $x = 0;
  75. foreach (@_) { $x += $_ }
  76. return $x > 0;
  77. }
  78. # args
  79. # list of 1s and 0s, coming from check_disabled()
  80. sub allof {
  81. my $x = 1;
  82. foreach (@_) { $x *= $_ }
  83. return $x > 0;
  84. }
  85. # args
  86. # list of strings, all of them should be names of features
  87. # that can be disabled.
  88. # returns a list of 1s (if the corresponding feature is disabled)
  89. # and 0s (if it isn't)
  90. sub check_disabled {
  91. return map { exists $disabled{lc $_} ? 1 : 0 } @_;
  92. }
  93. # Exported functions #################################################
  94. # args:
  95. # list of features to check
  96. sub anydisabled {
  97. load_configdata() unless $configdata_loaded;
  98. my @ret = check_disabled(@_);
  99. return @ret if wantarray;
  100. return anyof(@ret);
  101. }
  102. # args:
  103. # list of features to check
  104. sub alldisabled {
  105. load_configdata() unless $configdata_loaded;
  106. my @ret = check_disabled(@_);
  107. return @ret if wantarray;
  108. return allof(@ret);
  109. }
  110. # !!! Kept for backward compatibility
  111. # args:
  112. # single string
  113. sub disabled {
  114. anydisabled(@_);
  115. }
  116. sub available_protocols {
  117. load_configdata() unless $configdata_loaded;
  118. my $protocol_class = shift;
  119. if (exists $available_protocols{lc $protocol_class}) {
  120. return @{$available_protocols{lc $protocol_class}}
  121. }
  122. return ();
  123. }
  124. sub config {
  125. load_configdata() unless $configdata_loaded;
  126. return $config{$_[0]};
  127. }
  128. # IPv4 / IPv6 checker
  129. my $have_IPv4 = -1;
  130. my $have_IPv6 = -1;
  131. my $IP_factory;
  132. sub check_IP {
  133. my $listenaddress = shift;
  134. eval {
  135. require IO::Socket::IP;
  136. my $s = IO::Socket::IP->new(
  137. LocalAddr => $listenaddress,
  138. LocalPort => 0,
  139. Listen=>1,
  140. );
  141. $s or die "\n";
  142. $s->close();
  143. };
  144. if ($@ eq "") {
  145. return 1;
  146. }
  147. eval {
  148. require IO::Socket::INET6;
  149. my $s = IO::Socket::INET6->new(
  150. LocalAddr => $listenaddress,
  151. LocalPort => 0,
  152. Listen=>1,
  153. );
  154. $s or die "\n";
  155. $s->close();
  156. };
  157. if ($@ eq "") {
  158. return 1;
  159. }
  160. eval {
  161. require IO::Socket::INET;
  162. my $s = IO::Socket::INET->new(
  163. LocalAddr => $listenaddress,
  164. LocalPort => 0,
  165. Listen=>1,
  166. );
  167. $s or die "\n";
  168. $s->close();
  169. };
  170. if ($@ eq "") {
  171. return 1;
  172. }
  173. return 0;
  174. }
  175. sub have_IPv4 {
  176. if ($have_IPv4 < 0) {
  177. $have_IPv4 = check_IP("127.0.0.1");
  178. }
  179. return $have_IPv4;
  180. }
  181. sub have_IPv6 {
  182. if ($have_IPv6 < 0) {
  183. load_configdata() unless $configdata_loaded;
  184. # If OpenSSL is configured with IPv6 explicitly disabled, no IPv6
  185. # related tests should be performed. In other words, pretend IPv6
  186. # isn't present.
  187. $have_IPv6 = 0
  188. if grep { $_ eq 'OPENSSL_USE_IPV6=0' } @{$config{CPPDEFINES}};
  189. # Similarly, if a config target has explicitly disabled IPv6, no
  190. # IPv6 related tests should be performed.
  191. $have_IPv6 = 0
  192. if grep { $_ eq 'OPENSSL_USE_IPV6=0' } @{$target{defines}};
  193. }
  194. if ($have_IPv6 < 0) {
  195. $have_IPv6 = check_IP("::1");
  196. }
  197. return $have_IPv6;
  198. }
  199. =head1 SEE ALSO
  200. L<OpenSSL::Test>
  201. =head1 AUTHORS
  202. Stephen Henson E<lt>[email protected]<gt> and
  203. Richard Levitte E<lt>[email protected]<gt>
  204. =cut
  205. 1;