testutil.pm 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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 miscellanous 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. subbase64
  35. subnewlines
  36. );
  37. }
  38. use MIME::Base64;
  39. use globalconfig qw(
  40. $torture
  41. $verbose
  42. );
  43. sub subbase64 {
  44. my ($thing) = @_;
  45. # cut out the base64 piece
  46. if($$thing =~ s/%b64\[(.*)\]b64%/%%B64%%/i) {
  47. my $d = $1;
  48. # encode %NN characters
  49. $d =~ s/%([0-9A-Fa-f]{2})/chr(hex($1))/eg;
  50. my $enc = encode_base64($d, "");
  51. # put the result into there
  52. $$thing =~ s/%%B64%%/$enc/;
  53. }
  54. # hex decode
  55. if($$thing =~ s/%hex\[(.*)\]hex%/%%HEX%%/i) {
  56. # decode %NN characters
  57. my $d = $1;
  58. $d =~ s/%([0-9A-Fa-f]{2})/chr(hex($1))/eg;
  59. $$thing =~ s/%%HEX%%/$d/;
  60. }
  61. if($$thing =~ s/%repeat\[(\d+) x (.*)\]%/%%REPEAT%%/i) {
  62. # decode %NN characters
  63. my ($d, $n) = ($2, $1);
  64. $d =~ s/%([0-9A-Fa-f]{2})/chr(hex($1))/eg;
  65. my $all = $d x $n;
  66. $$thing =~ s/%%REPEAT%%/$all/;
  67. }
  68. }
  69. my $prevupdate; # module scope so it remembers the last value
  70. sub subnewlines {
  71. my ($force, $thing) = @_;
  72. if($force) {
  73. # enforce CRLF newline
  74. $$thing =~ s/\x0d*\x0a/\x0d\x0a/;
  75. return;
  76. }
  77. # When curl is built with Hyper, it gets all response headers delivered as
  78. # name/value pairs and curl "invents" the newlines when it saves the
  79. # headers. Therefore, curl will always save headers with CRLF newlines
  80. # when built to use Hyper. By making sure we deliver all tests using CRLF
  81. # as well, all test comparisons will survive without knowing about this
  82. # little quirk.
  83. if(($$thing =~ /^HTTP\/(1.1|1.0|2|3) [1-5][^\x0d]*\z/) ||
  84. ($$thing =~ /^(GET|POST|PUT|DELETE) \S+ HTTP\/\d+(\.\d+)?/) ||
  85. (($$thing =~ /^[a-z0-9_-]+: [^\x0d]*\z/i) &&
  86. # skip curl error messages
  87. ($$thing !~ /^curl: \(\d+\) /))) {
  88. # enforce CRLF newline
  89. $$thing =~ s/\x0d*\x0a/\x0d\x0a/;
  90. $prevupdate = 1;
  91. }
  92. else {
  93. if(($$thing =~ /^\n\z/) && $prevupdate) {
  94. # if there's a blank link after a line we update, we hope it is
  95. # the empty line following headers
  96. $$thing =~ s/\x0a/\x0d\x0a/;
  97. }
  98. $prevupdate = 0;
  99. }
  100. }
  101. #######################################################################
  102. # Run the application under test and return its return code
  103. #
  104. sub runclient {
  105. my ($cmd)=@_;
  106. my $ret = system($cmd);
  107. print "CMD ($ret): $cmd\n" if($verbose && !$torture);
  108. return $ret;
  109. # This is one way to test curl on a remote machine
  110. # my $out = system("ssh $CLIENTIP cd \'$pwd\' \\; \'$cmd\'");
  111. # sleep 2; # time to allow the NFS server to be updated
  112. # return $out;
  113. }
  114. #######################################################################
  115. # Run the application under test and return its stdout
  116. #
  117. sub runclientoutput {
  118. my ($cmd)=@_;
  119. return `$cmd 2>/dev/null`;
  120. # This is one way to test curl on a remote machine
  121. # my @out = `ssh $CLIENTIP cd \'$pwd\' \\; \'$cmd\'`;
  122. # sleep 2; # time to allow the NFS server to be updated
  123. # return @out;
  124. }
  125. 1;