Utils.pm 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. # Copyright 2016 The OpenSSL Project Authors. All Rights Reserved.
  2. #
  3. # Licensed under the OpenSSL license (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. =back
  50. =cut
  51. our %available_protocols;
  52. our %disabled;
  53. our %config;
  54. my $configdata_loaded = 0;
  55. sub load_configdata {
  56. # We eval it so it doesn't run at compile time of this file.
  57. # The latter would have bldtop_file() complain that setup() hasn't
  58. # been run yet.
  59. my $configdata = bldtop_file("configdata.pm");
  60. eval { require $configdata;
  61. %available_protocols = %configdata::available_protocols;
  62. %disabled = %configdata::disabled;
  63. %config = %configdata::config;
  64. };
  65. $configdata_loaded = 1;
  66. }
  67. # args
  68. # list of 1s and 0s, coming from check_disabled()
  69. sub anyof {
  70. my $x = 0;
  71. foreach (@_) { $x += $_ }
  72. return $x > 0;
  73. }
  74. # args
  75. # list of 1s and 0s, coming from check_disabled()
  76. sub allof {
  77. my $x = 1;
  78. foreach (@_) { $x *= $_ }
  79. return $x > 0;
  80. }
  81. # args
  82. # list of strings, all of them should be names of features
  83. # that can be disabled.
  84. # returns a list of 1s (if the corresponding feature is disabled)
  85. # and 0s (if it isn't)
  86. sub check_disabled {
  87. return map { exists $disabled{lc $_} ? 1 : 0 } @_;
  88. }
  89. # Exported functions #################################################
  90. # args:
  91. # list of features to check
  92. sub anydisabled {
  93. load_configdata() unless $configdata_loaded;
  94. my @ret = check_disabled(@_);
  95. return @ret if wantarray;
  96. return anyof(@ret);
  97. }
  98. # args:
  99. # list of features to check
  100. sub alldisabled {
  101. load_configdata() unless $configdata_loaded;
  102. my @ret = check_disabled(@_);
  103. return @ret if wantarray;
  104. return allof(@ret);
  105. }
  106. # !!! Kept for backward compatibility
  107. # args:
  108. # single string
  109. sub disabled {
  110. anydisabled(@_);
  111. }
  112. sub available_protocols {
  113. load_configdata() unless $configdata_loaded;
  114. my $protocol_class = shift;
  115. if (exists $available_protocols{lc $protocol_class}) {
  116. return @{$available_protocols{lc $protocol_class}}
  117. }
  118. return ();
  119. }
  120. sub config {
  121. load_configdata() unless $configdata_loaded;
  122. return $config{$_[0]};
  123. }
  124. # IPv4 / IPv6 checker
  125. my $have_IPv4 = -1;
  126. my $have_IPv6 = -1;
  127. my $IP_factory;
  128. sub check_IP {
  129. my $listenaddress = shift;
  130. eval {
  131. require IO::Socket::IP;
  132. my $s = IO::Socket::IP->new(
  133. LocalAddr => $listenaddress,
  134. LocalPort => 0,
  135. Listen=>1,
  136. );
  137. $s or die "\n";
  138. $s->close();
  139. };
  140. if ($@ eq "") {
  141. return 1;
  142. }
  143. eval {
  144. require IO::Socket::INET6;
  145. my $s = IO::Socket::INET6->new(
  146. LocalAddr => $listenaddress,
  147. LocalPort => 0,
  148. Listen=>1,
  149. );
  150. $s or die "\n";
  151. $s->close();
  152. };
  153. if ($@ eq "") {
  154. return 1;
  155. }
  156. eval {
  157. require IO::Socket::INET;
  158. my $s = IO::Socket::INET->new(
  159. LocalAddr => $listenaddress,
  160. LocalPort => 0,
  161. Listen=>1,
  162. );
  163. $s or die "\n";
  164. $s->close();
  165. };
  166. if ($@ eq "") {
  167. return 1;
  168. }
  169. return 0;
  170. }
  171. sub have_IPv4 {
  172. if ($have_IPv4 < 0) {
  173. $have_IPv4 = check_IP("127.0.0.1");
  174. }
  175. return $have_IPv4;
  176. }
  177. sub have_IPv6 {
  178. if ($have_IPv6 < 0) {
  179. $have_IPv6 = check_IP("::1");
  180. }
  181. return $have_IPv6;
  182. }
  183. =head1 SEE ALSO
  184. L<OpenSSL::Test>
  185. =head1 AUTHORS
  186. Stephen Henson E<lt>steve@openssl.orgE<gt> and
  187. Richard Levitte E<lt>levitte@openssl.orgE<gt>
  188. =cut
  189. 1;