testutil.pm 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. #***************************************************************************
  2. # _ _ ____ _
  3. # Project ___| | | | _ \| |
  4. # / __| | | | |_) | |
  5. # | (__| |_| | _ <| |___
  6. # \___|\___/|_| \_\_____|
  7. #
  8. # Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al.
  9. #
  10. # This software is licensed as described in the file COPYING, which
  11. # you should have received as part of this distribution. The terms
  12. # are also available at https://curl.se/docs/copyright.html.
  13. #
  14. # You may opt to use, copy, modify, merge, publish, distribute and/or sell
  15. # copies of the Software, and permit persons to whom the Software is
  16. # furnished to do so, under the terms of the COPYING file.
  17. #
  18. # This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
  19. # KIND, either express or implied.
  20. #
  21. # SPDX-License-Identifier: curl
  22. #
  23. ###########################################################################
  24. # This module contains miscellaneous functions needed in several parts of
  25. # the test suite.
  26. package testutil;
  27. use strict;
  28. use warnings;
  29. BEGIN {
  30. use base qw(Exporter);
  31. our @EXPORT = qw(
  32. runclient
  33. runclientoutput
  34. setlogfunc
  35. shell_quote
  36. subbase64
  37. subnewlines
  38. subsha256base64file
  39. substrippemfile
  40. );
  41. our @EXPORT_OK = qw(
  42. clearlogs
  43. logmsg
  44. );
  45. }
  46. use Digest::SHA qw(sha256);
  47. use MIME::Base64;
  48. use globalconfig qw(
  49. $torture
  50. $verbose
  51. $dev_null
  52. );
  53. my $logfunc; # optional reference to function for logging
  54. my @logmessages; # array holding logged messages
  55. #######################################################################
  56. # Log an informational message
  57. # If a log callback function was set in setlogfunc, it is called. If not,
  58. # then the log message is buffered until retrieved by clearlogs.
  59. #
  60. # logmsg must only be called by one of the runner_* entry points and functions
  61. # called by them, or else logs risk being lost, since those are the only
  62. # functions that know about and will return buffered logs.
  63. sub logmsg {
  64. if(!scalar(@_)) {
  65. return;
  66. }
  67. if(defined $logfunc) {
  68. &$logfunc(@_);
  69. return;
  70. }
  71. push @logmessages, @_;
  72. }
  73. #######################################################################
  74. # Set the function to use for logging
  75. sub setlogfunc {
  76. ($logfunc)=@_;
  77. }
  78. #######################################################################
  79. # Clear the buffered log messages after returning them
  80. sub clearlogs {
  81. my $loglines = join('', @logmessages);
  82. undef @logmessages;
  83. return $loglines;
  84. }
  85. #######################################################################
  86. sub includefile {
  87. my ($f) = @_;
  88. open(F, "<$f");
  89. my @a = <F>;
  90. close(F);
  91. return join("", @a);
  92. }
  93. sub subbase64 {
  94. my ($thing) = @_;
  95. # cut out the base64 piece
  96. while($$thing =~ s/%b64\[(.*?)\]b64%/%%B64%%/i) {
  97. my $d = $1;
  98. # encode %NN characters
  99. $d =~ s/%([0-9A-Fa-f]{2})/chr(hex($1))/eg;
  100. my $enc = encode_base64($d, "");
  101. # put the result into there
  102. $$thing =~ s/%%B64%%/$enc/;
  103. }
  104. # hex decode
  105. while($$thing =~ s/%hex\[(.*?)\]hex%/%%HEX%%/i) {
  106. # decode %NN characters
  107. my $d = $1;
  108. $d =~ s/%([0-9A-Fa-f]{2})/chr(hex($1))/eg;
  109. $$thing =~ s/%%HEX%%/$d/;
  110. }
  111. # repeat
  112. while($$thing =~ s/%repeat\[(\d+) x (.*?)\]%/%%REPEAT%%/i) {
  113. # decode %NN characters
  114. my ($d, $n) = ($2, $1);
  115. $d =~ s/%([0-9A-Fa-f]{2})/chr(hex($1))/eg;
  116. $n =~ s/%([0-9A-Fa-f]{2})/chr(hex($1))/eg;
  117. my $all = $d x $n;
  118. $$thing =~ s/%%REPEAT%%/$all/;
  119. }
  120. # include a file
  121. $$thing =~ s/%include ([^%]*)%[\n\r]+/includefile($1)/ge;
  122. }
  123. my $prevupdate; # module scope so it remembers the last value
  124. sub subnewlines {
  125. my ($force, $thing) = @_;
  126. if($force) {
  127. # enforce CRLF newline
  128. $$thing =~ s/\x0d*\x0a/\x0d\x0a/;
  129. return;
  130. }
  131. # When curl is built with Hyper, it gets all response headers delivered as
  132. # name/value pairs and curl "invents" the newlines when it saves the
  133. # headers. Therefore, curl will always save headers with CRLF newlines
  134. # when built to use Hyper. By making sure we deliver all tests using CRLF
  135. # as well, all test comparisons will survive without knowing about this
  136. # little quirk.
  137. if(($$thing =~ /^HTTP\/(1.1|1.0|2|3) [1-5][^\x0d]*\z/) ||
  138. ($$thing =~ /^(GET|POST|PUT|DELETE) \S+ HTTP\/\d+(\.\d+)?/) ||
  139. (($$thing =~ /^[a-z0-9_-]+: [^\x0d]*\z/i) &&
  140. # skip curl error messages
  141. ($$thing !~ /^curl: \(\d+\) /))) {
  142. # enforce CRLF newline
  143. $$thing =~ s/\x0d*\x0a/\x0d\x0a/;
  144. $prevupdate = 1;
  145. }
  146. else {
  147. if(($$thing =~ /^\n\z/) && $prevupdate) {
  148. # if there's a blank link after a line we update, we hope it is
  149. # the empty line following headers
  150. $$thing =~ s/\x0a/\x0d\x0a/;
  151. }
  152. $prevupdate = 0;
  153. }
  154. }
  155. #######################################################################
  156. # Run the application under test and return its return code
  157. #
  158. sub runclient {
  159. my ($cmd)=@_;
  160. my $ret = system($cmd);
  161. print "CMD ($ret): $cmd\n" if($verbose && !$torture);
  162. return $ret;
  163. # This is one way to test curl on a remote machine
  164. # my $out = system("ssh $CLIENTIP cd \'$pwd\' \\; \'$cmd\'");
  165. # sleep 2; # time to allow the NFS server to be updated
  166. # return $out;
  167. }
  168. #######################################################################
  169. # Run the application under test and return its stdout
  170. #
  171. sub runclientoutput {
  172. my ($cmd)=@_;
  173. return `$cmd 2>$dev_null`;
  174. # This is one way to test curl on a remote machine
  175. # my @out = `ssh $CLIENTIP cd \'$pwd\' \\; \'$cmd\'`;
  176. # sleep 2; # time to allow the NFS server to be updated
  177. # return @out;
  178. }
  179. #######################################################################
  180. # Quote an argument for passing safely to a Bourne shell
  181. # This does the same thing as String::ShellQuote but doesn't need a package.
  182. #
  183. sub shell_quote {
  184. my ($s)=@_;
  185. if($^O eq 'MSWin32') {
  186. $s = '"' . $s . '"';
  187. }
  188. else {
  189. if($s !~ m/^[-+=.,_\/:a-zA-Z0-9]+$/) {
  190. # string contains a "dangerous" character--quote it
  191. $s =~ s/'/'"'"'/g;
  192. $s = "'" . $s . "'";
  193. }
  194. }
  195. return $s;
  196. }
  197. sub get_sha256_base64 {
  198. my ($file_path) = @_;
  199. return encode_base64(sha256(do { local $/; open my $fh, '<:raw', $file_path or die $!; <$fh> }), "");
  200. }
  201. sub subsha256base64file {
  202. my ($thing) = @_;
  203. # SHA-256 base64
  204. while ($$thing =~ s/%sha256b64file\[(.*?)\]sha256b64file%/%%SHA256B64FILE%%/i) {
  205. my $file_path = $1;
  206. $file_path =~ s/%([0-9A-Fa-f]{2})/chr(hex($1))/eg;
  207. my $hash_b64 = get_sha256_base64($file_path);
  208. $$thing =~ s/%%SHA256B64FILE%%/$hash_b64/;
  209. }
  210. }
  211. sub get_file_content {
  212. my ($file_path) = @_;
  213. my $content = do { local $/; open my $fh, '<', $file_path or die $!; <$fh> };
  214. $content =~ s/(^|-----END .*?-----[\r\n]?)(.*?)(-----BEGIN .*?-----|$)/$1$3/gs;
  215. $content =~ s/\r\n/\n/g;
  216. chomp($content);
  217. return $content;
  218. }
  219. sub substrippemfile {
  220. my ($thing) = @_;
  221. # File content substitution
  222. while ($$thing =~ s/%strippemfile\[(.*?)\]strippemfile%/%%FILE%%/i) {
  223. my $file_path = $1;
  224. $file_path =~ s/%([0-9A-Fa-f]{2})/chr(hex($1))/eg;
  225. my $file_content = get_file_content($file_path);
  226. $$thing =~ s/%%FILE%%/$file_content/;
  227. }
  228. }
  229. 1;