testutil.pm 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  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. if(($$thing =~ /^HTTP\/(1.1|1.0|2|3) [1-5][^\x0d]*\z/) ||
  132. ($$thing =~ /^(GET|POST|PUT|DELETE) \S+ HTTP\/\d+(\.\d+)?/) ||
  133. (($$thing =~ /^[a-z0-9_-]+: [^\x0d]*\z/i) &&
  134. # skip curl error messages
  135. ($$thing !~ /^curl: \(\d+\) /))) {
  136. # enforce CRLF newline
  137. $$thing =~ s/\x0d*\x0a/\x0d\x0a/;
  138. $prevupdate = 1;
  139. }
  140. else {
  141. if(($$thing =~ /^\n\z/) && $prevupdate) {
  142. # if there's a blank link after a line we update, we hope it is
  143. # the empty line following headers
  144. $$thing =~ s/\x0a/\x0d\x0a/;
  145. }
  146. $prevupdate = 0;
  147. }
  148. }
  149. #######################################################################
  150. # Run the application under test and return its return code
  151. #
  152. sub runclient {
  153. my ($cmd)=@_;
  154. my $ret = system($cmd);
  155. print "CMD ($ret): $cmd\n" if($verbose && !$torture);
  156. return $ret;
  157. # This is one way to test curl on a remote machine
  158. # my $out = system("ssh $CLIENTIP cd \'$pwd\' \\; \'$cmd\'");
  159. # sleep 2; # time to allow the NFS server to be updated
  160. # return $out;
  161. }
  162. #######################################################################
  163. # Run the application under test and return its stdout
  164. #
  165. sub runclientoutput {
  166. my ($cmd)=@_;
  167. return `$cmd 2>$dev_null`;
  168. # This is one way to test curl on a remote machine
  169. # my @out = `ssh $CLIENTIP cd \'$pwd\' \\; \'$cmd\'`;
  170. # sleep 2; # time to allow the NFS server to be updated
  171. # return @out;
  172. }
  173. #######################################################################
  174. # Quote an argument for passing safely to a Bourne shell
  175. # This does the same thing as String::ShellQuote but doesn't need a package.
  176. #
  177. sub shell_quote {
  178. my ($s)=@_;
  179. if($^O eq 'MSWin32') {
  180. $s = '"' . $s . '"';
  181. }
  182. else {
  183. if($s !~ m/^[-+=.,_\/:a-zA-Z0-9]+$/) {
  184. # string contains a "dangerous" character--quote it
  185. $s =~ s/'/'"'"'/g;
  186. $s = "'" . $s . "'";
  187. }
  188. }
  189. return $s;
  190. }
  191. sub get_sha256_base64 {
  192. my ($file_path) = @_;
  193. return encode_base64(sha256(do { local $/; open my $fh, '<:raw', $file_path or die $!; <$fh> }), "");
  194. }
  195. sub subsha256base64file {
  196. my ($thing) = @_;
  197. # SHA-256 base64
  198. while ($$thing =~ s/%sha256b64file\[(.*?)\]sha256b64file%/%%SHA256B64FILE%%/i) {
  199. my $file_path = $1;
  200. $file_path =~ s/%([0-9A-Fa-f]{2})/chr(hex($1))/eg;
  201. my $hash_b64 = get_sha256_base64($file_path);
  202. $$thing =~ s/%%SHA256B64FILE%%/$hash_b64/;
  203. }
  204. }
  205. sub get_file_content {
  206. my ($file_path) = @_;
  207. my $content = do { local $/; open my $fh, '<', $file_path or die $!; <$fh> };
  208. $content =~ s/(^|-----END .*?-----[\r\n]?)(.*?)(-----BEGIN .*?-----|$)/$1$3/gs;
  209. $content =~ s/\r\n/\n/g;
  210. chomp($content);
  211. return $content;
  212. }
  213. sub substrippemfile {
  214. my ($thing) = @_;
  215. # File content substitution
  216. while ($$thing =~ s/%strippemfile\[(.*?)\]strippemfile%/%%FILE%%/i) {
  217. my $file_path = $1;
  218. $file_path =~ s/%([0-9A-Fa-f]{2})/chr(hex($1))/eg;
  219. my $file_content = get_file_content($file_path);
  220. $$thing =~ s/%%FILE%%/$file_content/;
  221. }
  222. }
  223. 1;