#!/usr/bin/env perl #*************************************************************************** # _ _ ____ _ # Project ___| | | | _ \| | # / __| | | | |_) | | # | (__| |_| | _ <| |___ # \___|\___/|_| \_\_____| # # Copyright (C) Daniel Stenberg, , 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 # ########################################################################### use strict; use warnings; # If the build is done out-of-source-tree, specify the build root dir as an # argument to this script. Necessary to find the generated buildprotos.h # header file. my $root = ".."; if($ARGV[0]) { $root = $ARGV[0]; } # return all known test cases sub alltests { my @a; if(open(M, "<", "./data/Makefile.am")) { while() { if(/^ test(\d+)/) { push @a, $1; # just the number } } close(M); } else { print STDERR "cannot open data/Makefile.am\n"; } return @a; } sub libtests { my @a; if(open(M, "<", "./libtest/Makefile.inc")) { while() { if(/^ *lib(\d+)\.c/) { push @a, $1; # just the number } } close(M); } else { print STDERR "cannot open Makefile.inc\n"; } return @a; } sub tunittests { my @a; if(open(M, "<", "./tunit/Makefile.inc")) { while() { if(/^ *tool(\d+)\.c/) { push @a, $1; # just the number } } close(M); } else { print STDERR "cannot open Makefile.inc\n"; } return @a; } sub unittests { my @a; if(open(M, "<", "./unit/Makefile.inc")) { while() { if(/^ *unit(\d+)\.c/) { push @a, $1; # just the number } } close(M); } else { print STDERR "cannot open Makefile.inc\n"; } return @a; } # provided a test number, return the keywords it uses sub keywords { my ($num) = @_; if(open(my $fh, "<", "./data/test$num")) { local $/; my $c = <$fh>; close($fh); # Match and extract the block if ($c =~ /\n(.*?)<\/keywords>/s) { return split(/\n/, $1); } } else { print STDERR "cannot open test$num\n"; } return undef; } # provided a test number, return the it uses sub tool { my ($num) = @_; if(open(my $fh, "<", "./data/test$num")) { local $/; my $c = <$fh>; close($fh); # Match and extract the block if ($c =~ /\n(.*?)<\/tool>/s) { return split(/\n/, $1); } } else { print STDERR "cannot open test$num\n"; } return undef; } # return all the function names that are prototyped here, as a hash sub unitprotos { my ($num) = @_; my %protos; if(open(my $fh, "<", "$root/lib/unitprotos.h")) { while(<$fh>) { if(/^UNITTEST .*[* ]([a-z0-9_]+)\(/i) { $protos{$1} = 1; } } close($fh); } else { print STDERR "cannot open unitprotos.h\n"; } return %protos; } # check if this unit test actually uses any internal symbols sub checkunit { my ($num, $protref) = @_; my $private = 0; if(open(my $fh, "<", "./unit/unit$num.c")) { while(<$fh>) { # check all function calls while(/([a-z0-9_]+)\(/cig) { my $f = $1; #print "Check ($num) $f\n"; if($f =~ /^(Curl_|curlx_)/) { $private = 1; last; } elsif($$protref{$f}) { # it uses prototyped one $private = 1; last; } } if(/struct (Curl_|connectdata)/) { $private = 1; last; } elsif(/\bCURLPROTO_(WS|WSS|MQTTS|SOCKS)\b/) { $private = 1; last; } } close($fh); } else { print STDERR "cannot open unit$num.c\n"; } if(!$private) { print "Unit test $num uses no private functions or structs\n"; } return !$private; } sub checkxml { my ($num) = @_; my $bad = 0; if(open(my $fh, "<", "./data/test$num")) { my $has_xml = 0; my %tag_count; while(<$fh>) { chomp; ++$tag_count{$1} if /^<([a-z0-9]+)( .+)?>$/; if(/^<\?xml/) { $has_xml = 1; } if(/^(# Server-side|# Client-side|# Verify data after the test has been \"shot\"|# tool is what to use instead of 'curl')/) { print "data/test$num has superfluous comment: '$_'\n"; $bad = 1; } } close($fh); if(!$has_xml) { print "data/test$num lacks tag\n"; $bad = 1; } for my $stat (sort { $tag_count{$a} <=> $tag_count{$b} || $a cmp $b } keys %tag_count) { my $max = ($stat =~ /^(dns|file[0-9]*|postcheck)/) ? 2 : 1; if($tag_count{$stat} > $max) { print "data/test$num has too many '<$stat>' tags ($tag_count{$stat} vs. $max maximum)\n"; $bad = 1; } } } else { print STDERR "cannot open test$num\n"; $bad = 1; } return $bad; } my $error; my @l; print "- Tests with source code in libtests/ have 'libtest' as \n"; @l = libtests(); for my $t (@l) { my $lt = 0; my @k = keywords($t); for my $w (@k) { if($w eq "libtest") { $lt = 1; } } if(!$lt) { print "Test $t in data/test$t lacks the 'libtest' keyword\n"; $error++; } } print "- Tests with source code in tunit/ have 'tunittest' as \n"; @l = tunittests(); for my $t (@l) { my $lt = 0; my @k = keywords($t); for my $w (@k) { if($w eq "tunittest") { $lt = 1; } elsif($w eq "unittest") { print "Test $t in data/test$t wrongly has the 'unittest' keyword\n"; } } if(!$lt) { print "Test $t in data/test$t lacks the 'tunittest' keyword\n"; $error++; } } print "- Tests with source code in unit/ have 'unittest' as \n"; @l = unittests(); for my $t (@l) { my $lt = 0; my @k = keywords($t); for my $w (@k) { if($w eq "unittest") { $lt = 1; } elsif($w eq "tunittest") { print "Test $t in data/test$t wrongly has the 'tunittest' keyword\n"; } } if(!$lt) { print "Test $t in data/test$t lacks the 'unittest' keyword\n"; $error++; } } my @a = alltests(); print "- Tests that set lib* in have 'libtest' as \n"; for my $t (@a) { my @k = tool($t); for my $w (@k) { next if(!$w); if($w =~ /lib%TESTNUMBER/) { print "test $t invokes default tool\n"; $error++; } elsif($w =~ /lib(\d+)/) { my $num = $1; if($num == $t) { print "test $t invokes default tool\n"; $error++; } my $lt = 0; # get the keywords for test $num my @k = keywords($t); for my $w (@k) { if($w eq "libtest") { $lt = 1; } } if(!$lt) { print "Test $t in data/test$t lacks the 'libtest' keyword\n"; $error++; } } } } print "- Tests that set tool* in have 'tunittest' as \n"; for my $t (@a) { my @k = tool($t); for my $w (@k) { next if(!$w); if($w =~ /tool%TESTNUMBER/) { print "test $t invokes default tool\n"; $error++; } elsif($w =~ /tool(\d+)/) { my $num = $1; if($num == $t) { print "test $t invokes default tool\n"; $error++; } my $lt = 0; # get the keywords for test $num my @k = keywords($num); for my $w (@k) { if($w eq "tunittest") { $lt = 1; } } if(!$lt) { print "Test $t in data/test$t lacks the 'tunittest' keyword\n"; $error++; } } } } print "- Tests that set unit* in have 'unittest' as \n"; for my $t (@a) { my @k = tool($t); for my $w (@k) { next if(!$w); if($w =~ /unit%TESTNUMBER/) { print "test $t invokes default tool\n"; $error++; } elsif($w =~ /unit(\d+)/) { my $num = $1; if($num == $t) { print "test $t invokes default tool\n"; $error++; } my $lt = 0; # get the keywords for test $num my @k = keywords($num); for my $w (@k) { if($w eq "unittest") { $lt = 1; } } if(!$lt) { print "Test $t in data/test$t lacks the 'unittest' keyword\n"; $error++; } } } } print "- Unit tests actually use private symbols\n"; @l = unittests(); my %protos = unitprotos(); for my $t (@l) { $error += checkunit($t, \%protos); } print "- All tests have tag on first line\n"; for my $t (@a) { $error += checkxml($t); } exit $error;