1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281
|
#***************************************************************************
# _ _ ____ _
# Project ___| | | | _ \| |
# / __| | | | |_) | |
# | (__| |_| | _ <| |___
# \___|\___/|_| \_\_____|
#
# Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al.
#
# This software is licensed as described in the file COPYING, which
# you should have received as part of this distribution. The terms
# are also available at https://curl.se/docs/copyright.html.
#
# You may opt to use, copy, modify, merge, publish, distribute and/or sell
# copies of the Software, and permit persons to whom the Software is
# furnished to do so, under the terms of the COPYING file.
#
# This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
# KIND, either express or implied.
#
# SPDX-License-Identifier: curl
#
###########################################################################
# This module contains miscellaneous functions needed in several parts of
# the test suite.
package testutil;
use strict;
use warnings;
BEGIN {
use base qw(Exporter);
our @EXPORT = qw(
runclient
runclientoutput
setlogfunc
exerunner
subtextfile
subchars
subbase64
subnewlines
subsha256base64file
substrippemfile
);
our @EXPORT_OK = qw(
clearlogs
logmsg
);
}
use Digest::SHA qw(sha256);
use MIME::Base64;
use globalconfig qw(
$torture
$verbose
$dev_null
);
my $logfunc; # optional reference to function for logging
my @logmessages; # array holding logged messages
#######################################################################
# Log an informational message
# If a log callback function was set in setlogfunc, it is called. If not,
# then the log message is buffered until retrieved by clearlogs.
#
# logmsg must only be called by one of the runner_* entry points and functions
# called by them, or else logs risk being lost, since those are the only
# functions that know about and will return buffered logs.
sub logmsg {
if(!scalar(@_)) {
return;
}
if(defined $logfunc) {
&$logfunc(@_);
return;
}
push @logmessages, @_;
}
#######################################################################
# Set the function to use for logging
sub setlogfunc {
($logfunc)=@_;
}
#######################################################################
# Clear the buffered log messages after returning them
sub clearlogs {
my $loglines = join('', @logmessages);
undef @logmessages;
return $loglines;
}
#######################################################################
sub includefile {
my ($f, $text) = @_;
open(F, "<$f");
if($text) {
binmode F, ':crlf';
}
my @a = <F>;
close(F);
return join("", @a);
}
sub subtextfile {
my ($thing) = @_;
my $count = ($$thing =~ s/%includetext ([^%]*)%[\n\r]+/includefile($1, 1)/ge);
return $count > 0;
}
sub subchars {
my ($thing) = @_;
$$thing =~ s/%SP/ /g; # space
$$thing =~ s/%TAB/\t/g; # horizontal tab
$$thing =~ s/%CR/\r/g; # carriage return aka \r aka 0x0d
$$thing =~ s/%LT/</g;
$$thing =~ s/%GT/>/g;
$$thing =~ s/%AMP/&/g;
}
sub subbase64 {
my ($thing) = @_;
# cut out the base64 piece
while($$thing =~ s/%b64\[(.*?)\]b64%/%%B64%%/i) {
my $d = $1;
# encode %NN characters
$d =~ s/%([0-9A-Fa-f]{2})/chr(hex($1))/eg;
my $enc = encode_base64($d, "");
# put the result into there
$$thing =~ s/%%B64%%/$enc/;
}
# hex decode
while($$thing =~ s/%hex\[(.*?)\]hex%/%%HEX%%/i) {
# decode %NN characters
my $d = $1;
$d =~ s/%([0-9A-Fa-f]{2})/chr(hex($1))/eg;
$$thing =~ s/%%HEX%%/$d/;
}
# repeat
while($$thing =~ s/%repeat\[(\d+) x (.*?)\]%/%%REPEAT%%/i) {
# decode %NN characters
my ($d, $n) = ($2, $1);
$d =~ s/%([0-9A-Fa-f]{2})/chr(hex($1))/eg;
$n =~ s/%([0-9A-Fa-f]{2})/chr(hex($1))/eg;
my $all = $d x $n;
$$thing =~ s/%%REPEAT%%/$all/;
}
# days
while($$thing =~ s/%days\[(.*?)\]/%%DAYS%%/i) {
# convert to now + given days in epoch seconds, align to a 60 second
# boundary. Then provide two alternatives.
my $now = time();
my $d = ($1 * 24 * 3600) + $now + 30;
$d = int($d/60) * 60;
my $d2 = $d + 60;
$$thing =~ s/%%DAYS%%/%alternatives[$d,$d2]/;
}
# include a file
$$thing =~ s/%include ([^%]*)%[\n\r]+/includefile($1, 0)/ge;
}
my $prevupdate; # module scope so it remembers the last value
sub subnewlines {
my ($force, $thing) = @_;
if($force) {
# enforce CRLF newline
$$thing =~ s/\x0d*\x0a/\x0d\x0a/;
return;
}
if(($$thing =~ /^HTTP\/(1.1|1.0|2|3) ([1-5]|9)[^\x0d]*\z/) ||
($$thing =~ /^(GET|HEAD|POST|PUT|DELETE|CONNECT) \S+ HTTP\/\d+(\.\d+)?/) ||
($$thing =~ /^(SETUP|GET_PARAMETER|OPTIONS|ANNOUNCE|DESCRIBE) \S+ RTSP\/\d+(\.\d+)?/) ||
(($$thing =~ /^[a-z0-9_-]+: [^\x0d]*\z/i) &&
# skip curl error messages
($$thing !~ /^curl: \(\d+\) /))) {
# enforce CRLF newline
$$thing =~ s/\x0d*\x0a/\x0d\x0a/;
$prevupdate = 1;
}
else {
if(($$thing =~ /^\n\z/) && $prevupdate) {
# if there is a blank link after a line we update, we hope it is
# the empty line following headers
$$thing =~ s/\x0a/\x0d\x0a/;
}
$prevupdate = 0;
}
}
#######################################################################
# Run the application under test and return its return code
#
sub runclient {
my ($cmd)=@_;
my $ret = system($cmd);
print "CMD ($ret): $cmd\n" if($verbose && !$torture);
return $ret;
# This is one way to test curl on a remote machine
# my $out = system("ssh $CLIENTIP cd \'$pwd\' \\; \'$cmd\'");
# sleep 2; # time to allow the NFS server to be updated
# return $out;
}
#######################################################################
# Run the application under test and return its stdout
#
sub runclientoutput {
my ($cmd)=@_;
return `$cmd 2>$dev_null`;
# This is one way to test curl on a remote machine
# my @out = `ssh $CLIENTIP cd \'$pwd\' \\; \'$cmd\'`;
# sleep 2; # time to allow the NFS server to be updated
# return @out;
}
#######################################################################
# Return custom tool (e.g. wine or qemu) to run curl binaries.
#
sub exerunner {
if($ENV{'CURL_TEST_EXE_RUNNER'}) {
return $ENV{'CURL_TEST_EXE_RUNNER'} . ' ';
}
return '';
}
sub get_sha256_base64 {
my ($file_path) = @_;
return encode_base64(sha256(do { local $/; open my $fh, '<:raw', $file_path or die $!; <$fh> }), "");
}
sub subsha256base64file {
my ($thing) = @_;
# SHA-256 base64
while($$thing =~ s/%sha256b64file\[(.*?)\]sha256b64file%/%%SHA256B64FILE%%/i) {
my $file_path = $1;
$file_path =~ s/%([0-9A-Fa-f]{2})/chr(hex($1))/eg;
my $hash_b64 = get_sha256_base64($file_path);
$$thing =~ s/%%SHA256B64FILE%%/$hash_b64/;
}
}
sub get_file_content {
my ($file_path) = @_;
my $content = do { local $/; open my $fh, '<', $file_path or die $!; <$fh> };
$content =~ s/(^|-----END .*?-----[\r\n]?)(.*?)(-----BEGIN .*?-----|$)/$1$3/gs;
$content =~ s/\r\n/\n/g;
chomp($content);
return $content;
}
sub substrippemfile {
my ($thing) = @_;
# File content substitution
while($$thing =~ s/%strippemfile\[(.*?)\]strippemfile%/%%FILE%%/i) {
my $file_path = $1;
$file_path =~ s/%([0-9A-Fa-f]{2})/chr(hex($1))/eg;
my $file_content = get_file_content($file_path);
$$thing =~ s/%%FILE%%/$file_content/;
}
}
1;
|