os/security/cryptomgmtlibs/securitytestfw/test/autotesting/cdrive.pl
changeset 0 bde4ae8d615e
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/os/security/cryptomgmtlibs/securitytestfw/test/autotesting/cdrive.pl	Fri Jun 15 03:10:57 2012 +0200
     1.3 @@ -0,0 +1,364 @@
     1.4 +#
     1.5 +# Copyright (c) 2006-2009 Nokia Corporation and/or its subsidiary(-ies).
     1.6 +# All rights reserved.
     1.7 +# This component and the accompanying materials are made available
     1.8 +# under the terms of the License "Eclipse Public License v1.0"
     1.9 +# which accompanies this distribution, and is available
    1.10 +# at the URL "http://www.eclipse.org/legal/epl-v10.html".
    1.11 +#
    1.12 +# Initial Contributors:
    1.13 +# Nokia Corporation - initial contribution.
    1.14 +#
    1.15 +# Contributors:
    1.16 +#
    1.17 +# Description: 
    1.18 +# Created by Relja Arandjelovic
    1.19 +# relja.arandjelovic@symbian.com
    1.20 +# Used to check for occurrences of "C: , EDriveC and [<index>]='C' and report warnings about it into HTML file
    1.21 +# USAGE:
    1.22 +# cdrive.pl -exclude=exclude.txt -excludedir=/common/generic/security/caf2/ -log=logfilename.htm path1 path2 ...
    1.23 +# log is created in $ENV{EPOCROOT}epoc32\\winscw\\c\\
    1.24 +# add -mytest option if the log should be stored in the execution directory
    1.25 +# (for example, if you are not using the script during overnight builds)
    1.26 +# exclude contains items formated as as follows (new line corresponds to new line in file):
    1.27 +# filename
    1.28 +# line content
    1.29 +# comment about why the warning should be excluded (may be in several lines, but with no line with only whice characters)
    1.30 +# line with only white characters
    1.31 +# NB: paths in cdrive.pl argumetns and filename in exclude list should be absolute paths
    1.32 +#
    1.33 +
    1.34 +use strict;
    1.35 +use Getopt::Std;
    1.36 +use Getopt::Long;
    1.37 +use File::Find;
    1.38 +
    1.39 +my @ListOfFiles=();
    1.40 +my @FileIsTest=();
    1.41 +my $nTests=0;
    1.42 +my %Exclude=();
    1.43 +
    1.44 +my $exclude="";
    1.45 +my $excludedir="_no_dir_";
    1.46 +my $LogFileName="";
    1.47 +
    1.48 +
    1.49 +
    1.50 +################
    1.51 +########
    1.52 +sub TypeOfWarning($$$$){
    1.53 +	my ($FileName,$FileIsTest,$linetext,$Warning)=@_;
    1.54 +	
    1.55 +	$linetext=~ s/^\s*//; $linetext=~ s/\s*$//;
    1.56 +
    1.57 +	$$Warning="warning";
    1.58 +	
    1.59 +	if ($FileIsTest==1){
    1.60 +		$$Warning="test";
    1.61 +	} else {
    1.62 +		# check if in include list
    1.63 +	
    1.64 +		my $ExcludeLine;
    1.65 +		foreach $ExcludeLine (@{$Exclude{$FileName}}){
    1.66 +			if ($linetext eq $ExcludeLine) {
    1.67 +				$$Warning="ignoredwarning";
    1.68 +				last;
    1.69 +			}
    1.70 +		}		
    1.71 +	}
    1.72 +	
    1.73 +}
    1.74 +	
    1.75 +	
    1.76 +	
    1.77 +################
    1.78 +########
    1.79 +
    1.80 +
    1.81 +my $FirstWarning=1;
    1.82 +my $PrevFileName="_no_file_";
    1.83 +
    1.84 +sub printHTML($$$$){
    1.85 +	my ($FileName,$linenumber,$linetext,$Warning)=@_;
    1.86 +
    1.87 +	
    1.88 +	$linetext=~ s/^\s*//; $linetext=~ s/\s*$//;
    1.89 +
    1.90 +	# convert special characters to HTML format
    1.91 +	$linetext=~ s/&/&amp;/sg;
    1.92 +	$linetext=~ s/</&lt;/sg;
    1.93 +	$linetext=~ s/>/&gt;/sg;
    1.94 +	
    1.95 +
    1.96 +	if ($FileName ne $PrevFileName){
    1.97 +		print HTML "
    1.98 +<tr><td colspan=\"4\" height=\"20\">&nbsp;</td></tr>
    1.99 +	<tr><td width=\"20\"></td><td colspan=\"3\">
   1.100 +		$FileName
   1.101 +	</td></tr>	
   1.102 +";
   1.103 +	}
   1.104 +	
   1.105 +	print HTML "
   1.106 +<tr><td colspan=\"4\" height=\"10\">&nbsp;</td></tr>
   1.107 +		<tr class=\"$Warning\"><td width=\"20\"></td><td width=\"60\"></td><td colspan=\"2\">
   1.108 +			Line number: $linenumber
   1.109 +		</td></tr>
   1.110 +			<tr class=\"$Warning\"><td width=\"20\"></td><td width=\"60\"></td><td width=\"80\"></td><td>
   1.111 +$linetext
   1.112 +			</td></tr>
   1.113 +";
   1.114 +	
   1.115 +	$PrevFileName=$FileName;
   1.116 +	$FirstWarning=0;
   1.117 +}
   1.118 +
   1.119 +################
   1.120 +########
   1.121 +my $prevDir="_no_previous_dir_";
   1.122 +my $isTest=0;
   1.123 +
   1.124 +sub MakeList(){
   1.125 +	
   1.126 +
   1.127 +	if (lc($File::Find::dir) =~ /^$excludedir/) { return; }
   1.128 +	if (lc($File::Find::dir) =~ /test/i) { return; }
   1.129 +	if (lc($File::Find::dir) =~ /examples/i) { return; }
   1.130 +	if (lc($File::Find::dir) =~ /tpkcs10/i) { return; }
   1.131 +	if (lc($File::Find::dir) =~ /tocsp/i) { return; }
   1.132 +	if (lc($File::Find::dir) =~ /referencedrmagent/i) { return; }
   1.133 +	if (lc($File::Find::dir) =~ /dumpswicertstoretool/i) { return; }
   1.134 +	if (!(lc($File::Find::name) =~ /\.(cpp|cxx|c\+\+|c|h)$/)){ return; }
   1.135 +	
   1.136 +	# include in list of files to be searched only if not test code
   1.137 +	# ie distribution policy doesn't include Reference/Test
   1.138 +	if ($prevDir ne lc($File::Find::dir)){ # new dir => search for distribution.policy	
   1.139 +		$isTest=0;
   1.140 +		my $policy="".($File::Find::dir)."/distribution.policy";
   1.141 +		
   1.142 +		if (-e $policy){
   1.143 +			open (POLICY , "<$policy" );
   1.144 +			while (<POLICY>){
   1.145 +				if ($_ =~ /Reference\/Test/){
   1.146 +					$isTest=1;
   1.147 +					last;
   1.148 +				}
   1.149 +			}
   1.150 +			close (POLICY);
   1.151 +		}
   1.152 +	}
   1.153 +	
   1.154 +	push(@ListOfFiles,lc($File::Find::name));
   1.155 +	push(@FileIsTest,$isTest);	
   1.156 +	if ($isTest) { $nTests++; }
   1.157 +	
   1.158 +	$prevDir=lc($File::Find::dir);
   1.159 +}
   1.160 +
   1.161 +################
   1.162 +########
   1.163 +sub FindC($$$$){
   1.164 +	my ($FileName,$FileIsTest,$count,$countunique)=@_;
   1.165 +			
   1.166 +	open(SOURCE,"<$FileName") or die ("Could not open file: $FileName");
   1.167 +	
   1.168 +	my $prevCount=$$count;
   1.169 +	my $line; my $templine; my $linenumber=1;
   1.170 +	my $MultiLineComment=0;
   1.171 +	my $MadeChangeDueToComments;
   1.172 +	my $FirstLine=0;
   1.173 +	
   1.174 +	my $HTMLFirstWarning=1;
   1.175 +	
   1.176 +	while ($line=<SOURCE>){
   1.177 +	
   1.178 +		$templine=$line;
   1.179 +		$linenumber++;
   1.180 +		$FirstLine=0;
   1.181 +		
   1.182 +		# process comments
   1.183 +		do {
   1.184 +		
   1.185 +			$MadeChangeDueToComments=0;
   1.186 +		
   1.187 +			if ($MultiLineComment==0){
   1.188 +		
   1.189 +				# remove text in // coments 
   1.190 +				$templine=~ s/(.*?)\/\/.*$/$1/;
   1.191 +				# remove /* */ comments found in one line
   1.192 +				$templine=~ s/\/\*.*\*\///g;
   1.193 +			
   1.194 +				# if only /* then remove text after it and mark the start of comment
   1.195 +				if ($templine=~ /^(.*?)\/\*/){
   1.196 +					$templine=$1;
   1.197 +					$MultiLineComment=1;
   1.198 +					$MadeChangeDueToComments=1;
   1.199 +					$FirstLine=1;
   1.200 +				}
   1.201 +			
   1.202 +			} else {	# $MultiLineComment==1
   1.203 +		
   1.204 +				# if */ end comment
   1.205 +				if ($templine=~ /\*\/(.*)$/){
   1.206 +					$templine=$1;
   1.207 +					$MultiLineComment=0;
   1.208 +					$MadeChangeDueToComments=1;				
   1.209 +				}
   1.210 +			
   1.211 +			}
   1.212 +			
   1.213 +		} while ($MadeChangeDueToComments==1);
   1.214 +		# end of processing comments
   1.215 +		
   1.216 +		
   1.217 +		if ($MultiLineComment==1 && $FirstLine==0) { next; } # skip checking if in comment
   1.218 +	
   1.219 +	
   1.220 +		if (
   1.221 +			$templine=~ /["'][cC]:/	|| # '" # need comment for correct highlighting in codewarrior
   1.222 +			$templine=~ /EDriveC/		||
   1.223 +			$templine=~ /\[.+\]\s*=\s*'[cC]':/
   1.224 +			){
   1.225 +			
   1.226 +				my $Warning;
   1.227 +				TypeOfWarning($FileName,$FileIsTest,$line,\$Warning);
   1.228 +				printHTML($FileName,$linenumber,$line,$Warning);
   1.229 +				if ($Warning eq "warning") { $$count++; }
   1.230 +				
   1.231 +			}
   1.232 +		
   1.233 +	}
   1.234 +	
   1.235 +	close(SOURCE);
   1.236 +	
   1.237 +	if ($prevCount<$$count) { $$countunique++; }
   1.238 +}
   1.239 +
   1.240 +################
   1.241 +########
   1.242 +sub ReadExcludeList(){
   1.243 +
   1.244 +	#print "\n";
   1.245 +	
   1.246 +	
   1.247 +	if ($exclude eq ""){
   1.248 +		#print "Exclude list file not specified\nCarrying on without an exclude list\n";
   1.249 +		return;
   1.250 +	} elsif (!(-e $exclude)){
   1.251 +		#print "Exclude list file doesn't exist!\nCarrying on without an exclude list\n";
   1.252 +		return;
   1.253 +	}
   1.254 +	
   1.255 +	
   1.256 +	my $line; my $FileName; my $LineText; my $justification;
   1.257 +	
   1.258 +	open (EXCLUDE,"<$exclude");
   1.259 +	while (1){
   1.260 +	
   1.261 +		$line=<EXCLUDE> or last;
   1.262 +		$line=~ s/^(\s*)//g; $line=~ s/(\s*)$//g;
   1.263 +		$line=~ s/\\/\//g;
   1.264 +		$FileName=lc($line);
   1.265 +		
   1.266 +		$line=<EXCLUDE>; $line=~ s/^(\s*)//g; $line=~ s/(\s*)$//g;
   1.267 +		$LineText=$line;
   1.268 +		
   1.269 +		$justification=0;
   1.270 +		while($line=<EXCLUDE>){
   1.271 +			$line=~ s/^(\s*)//g; $line=~ s/(\s*)$//g;
   1.272 +			if ($line eq "") { last;}
   1.273 +			$justification=1;
   1.274 +		}
   1.275 +		
   1.276 +		if ($justification==0){
   1.277 +			#print "Not added to the excludion list since no justification found for:\n$FileName\n$LineText\n";
   1.278 +		} else {		
   1.279 +			push(@{$Exclude{$FileName}},$LineText);
   1.280 +		}
   1.281 +		
   1.282 +	}
   1.283 +	close(EXCLUDE);
   1.284 +	
   1.285 +}
   1.286 +
   1.287 +################
   1.288 +######## Main
   1.289 +
   1.290 +	
   1.291 +	my $MyTest=0;
   1.292 +	
   1.293 +	GetOptions(
   1.294 +			"exclude=s" => \$exclude ,
   1.295 +			"excludedir=s" => \$excludedir, # one dir to be excluded from scaning (along wih its subdirs)
   1.296 +			"log=s" => \$LogFileName,
   1.297 +			"mytest" => \$MyTest # inteded for my personal testing (so that path is not the one for overnight testing)
   1.298 +				);
   1.299 +	
   1.300 +	$excludedir=~ s/\\/\//g; # convert \ into / so that it matches perl's path form
   1.301 +	
   1.302 +	ReadExcludeList();
   1.303 +
   1.304 +	
   1.305 +	if ($MyTest==0){ # overnight
   1.306 +		$LogFileName = "$ENV{EPOCROOT}epoc32\\winscw\\c\\".$LogFileName;
   1.307 +	}
   1.308 +
   1.309 +			
   1.310 +	my $iArgv;
   1.311 +	for ($iArgv=0;$iArgv<@ARGV;$iArgv++){
   1.312 +		$ARGV[$iArgv]=~ s/\\/\//g; # convert \ into / so that it matches perl's path form
   1.313 +		find(\&MakeList, ($ARGV[$iArgv]) );
   1.314 +	}
   1.315 +
   1.316 +	
   1.317 +	open(HTML,">$LogFileName");
   1.318 +	print HTML "
   1.319 +<html>
   1.320 +
   1.321 +<head>
   1.322 +	<title>CDrive warnings</title>
   1.323 +	
   1.324 +	<style type=\'text/css\'>
   1.325 +.warning {
   1.326 +	background-color: #FFAAAA;
   1.327 +}
   1.328 +.ignoredwarning {
   1.329 +	background-color: #90ffff;
   1.330 +}
   1.331 +.test{
   1.332 +	background-color: #ffc060;
   1.333 +}
   1.334 +	</style>
   1.335 +</head>
   1.336 +<body>	
   1.337 +	<table border=\"0\" width=\"100%\" cellpadding=\"0\" cellspacing=\"0\">
   1.338 +";
   1.339 +	
   1.340 +
   1.341 +	my $count=0; my $countunique=0;
   1.342 +	my $iList; my $nList=@ListOfFiles;
   1.343 +	for ($iList=0;$iList<$nList;$iList++){
   1.344 +		FindC($ListOfFiles[$iList],$FileIsTest[$iList],\$count,\$countunique);
   1.345 +	}
   1.346 +
   1.347 +	
   1.348 +	print HTML "\n</table>\n";
   1.349 +
   1.350 +	my $total=$nList-$nTests;
   1.351 +	# workaround in order to be reported to the standardised system	
   1.352 +	print HTML "<br><center>$countunique tests failed out of $total\n</center>\n";	
   1.353 +	
   1.354 +	print HTML "
   1.355 +</body>
   1.356 +</html>	
   1.357 +	";
   1.358 +	
   1.359 +	#print "\n\tNumber of files:\t$nList\n";
   1.360 +	#print "\n\tNumber of warnings:\t$count\n";
   1.361 +	#print "\n\tNumber of unique warnings:\t$countunique\n";
   1.362 +	
   1.363 +	close(HTML);
   1.364 +
   1.365 +################
   1.366 +######## end-Main
   1.367 +