os/security/cryptomgmtlibs/securitytestfw/test/autotesting/cdrive.pl
author sl
Tue, 10 Jun 2014 14:32:02 +0200
changeset 1 260cb5ec6c19
permissions -rw-r--r--
Update contrib.
     1 #
     2 # Copyright (c) 2006-2009 Nokia Corporation and/or its subsidiary(-ies).
     3 # All rights reserved.
     4 # This component and the accompanying materials are made available
     5 # under the terms of the License "Eclipse Public License v1.0"
     6 # which accompanies this distribution, and is available
     7 # at the URL "http://www.eclipse.org/legal/epl-v10.html".
     8 #
     9 # Initial Contributors:
    10 # Nokia Corporation - initial contribution.
    11 #
    12 # Contributors:
    13 #
    14 # Description: 
    15 # Created by Relja Arandjelovic
    16 # relja.arandjelovic@symbian.com
    17 # Used to check for occurrences of "C: , EDriveC and [<index>]='C' and report warnings about it into HTML file
    18 # USAGE:
    19 # cdrive.pl -exclude=exclude.txt -excludedir=/common/generic/security/caf2/ -log=logfilename.htm path1 path2 ...
    20 # log is created in $ENV{EPOCROOT}epoc32\\winscw\\c\\
    21 # add -mytest option if the log should be stored in the execution directory
    22 # (for example, if you are not using the script during overnight builds)
    23 # exclude contains items formated as as follows (new line corresponds to new line in file):
    24 # filename
    25 # line content
    26 # comment about why the warning should be excluded (may be in several lines, but with no line with only whice characters)
    27 # line with only white characters
    28 # NB: paths in cdrive.pl argumetns and filename in exclude list should be absolute paths
    29 #
    30 
    31 use strict;
    32 use Getopt::Std;
    33 use Getopt::Long;
    34 use File::Find;
    35 
    36 my @ListOfFiles=();
    37 my @FileIsTest=();
    38 my $nTests=0;
    39 my %Exclude=();
    40 
    41 my $exclude="";
    42 my $excludedir="_no_dir_";
    43 my $LogFileName="";
    44 
    45 
    46 
    47 ################
    48 ########
    49 sub TypeOfWarning($$$$){
    50 	my ($FileName,$FileIsTest,$linetext,$Warning)=@_;
    51 	
    52 	$linetext=~ s/^\s*//; $linetext=~ s/\s*$//;
    53 
    54 	$$Warning="warning";
    55 	
    56 	if ($FileIsTest==1){
    57 		$$Warning="test";
    58 	} else {
    59 		# check if in include list
    60 	
    61 		my $ExcludeLine;
    62 		foreach $ExcludeLine (@{$Exclude{$FileName}}){
    63 			if ($linetext eq $ExcludeLine) {
    64 				$$Warning="ignoredwarning";
    65 				last;
    66 			}
    67 		}		
    68 	}
    69 	
    70 }
    71 	
    72 	
    73 	
    74 ################
    75 ########
    76 
    77 
    78 my $FirstWarning=1;
    79 my $PrevFileName="_no_file_";
    80 
    81 sub printHTML($$$$){
    82 	my ($FileName,$linenumber,$linetext,$Warning)=@_;
    83 
    84 	
    85 	$linetext=~ s/^\s*//; $linetext=~ s/\s*$//;
    86 
    87 	# convert special characters to HTML format
    88 	$linetext=~ s/&/&amp;/sg;
    89 	$linetext=~ s/</&lt;/sg;
    90 	$linetext=~ s/>/&gt;/sg;
    91 	
    92 
    93 	if ($FileName ne $PrevFileName){
    94 		print HTML "
    95 <tr><td colspan=\"4\" height=\"20\">&nbsp;</td></tr>
    96 	<tr><td width=\"20\"></td><td colspan=\"3\">
    97 		$FileName
    98 	</td></tr>	
    99 ";
   100 	}
   101 	
   102 	print HTML "
   103 <tr><td colspan=\"4\" height=\"10\">&nbsp;</td></tr>
   104 		<tr class=\"$Warning\"><td width=\"20\"></td><td width=\"60\"></td><td colspan=\"2\">
   105 			Line number: $linenumber
   106 		</td></tr>
   107 			<tr class=\"$Warning\"><td width=\"20\"></td><td width=\"60\"></td><td width=\"80\"></td><td>
   108 $linetext
   109 			</td></tr>
   110 ";
   111 	
   112 	$PrevFileName=$FileName;
   113 	$FirstWarning=0;
   114 }
   115 
   116 ################
   117 ########
   118 my $prevDir="_no_previous_dir_";
   119 my $isTest=0;
   120 
   121 sub MakeList(){
   122 	
   123 
   124 	if (lc($File::Find::dir) =~ /^$excludedir/) { return; }
   125 	if (lc($File::Find::dir) =~ /test/i) { return; }
   126 	if (lc($File::Find::dir) =~ /examples/i) { return; }
   127 	if (lc($File::Find::dir) =~ /tpkcs10/i) { return; }
   128 	if (lc($File::Find::dir) =~ /tocsp/i) { return; }
   129 	if (lc($File::Find::dir) =~ /referencedrmagent/i) { return; }
   130 	if (lc($File::Find::dir) =~ /dumpswicertstoretool/i) { return; }
   131 	if (!(lc($File::Find::name) =~ /\.(cpp|cxx|c\+\+|c|h)$/)){ return; }
   132 	
   133 	# include in list of files to be searched only if not test code
   134 	# ie distribution policy doesn't include Reference/Test
   135 	if ($prevDir ne lc($File::Find::dir)){ # new dir => search for distribution.policy	
   136 		$isTest=0;
   137 		my $policy="".($File::Find::dir)."/distribution.policy";
   138 		
   139 		if (-e $policy){
   140 			open (POLICY , "<$policy" );
   141 			while (<POLICY>){
   142 				if ($_ =~ /Reference\/Test/){
   143 					$isTest=1;
   144 					last;
   145 				}
   146 			}
   147 			close (POLICY);
   148 		}
   149 	}
   150 	
   151 	push(@ListOfFiles,lc($File::Find::name));
   152 	push(@FileIsTest,$isTest);	
   153 	if ($isTest) { $nTests++; }
   154 	
   155 	$prevDir=lc($File::Find::dir);
   156 }
   157 
   158 ################
   159 ########
   160 sub FindC($$$$){
   161 	my ($FileName,$FileIsTest,$count,$countunique)=@_;
   162 			
   163 	open(SOURCE,"<$FileName") or die ("Could not open file: $FileName");
   164 	
   165 	my $prevCount=$$count;
   166 	my $line; my $templine; my $linenumber=1;
   167 	my $MultiLineComment=0;
   168 	my $MadeChangeDueToComments;
   169 	my $FirstLine=0;
   170 	
   171 	my $HTMLFirstWarning=1;
   172 	
   173 	while ($line=<SOURCE>){
   174 	
   175 		$templine=$line;
   176 		$linenumber++;
   177 		$FirstLine=0;
   178 		
   179 		# process comments
   180 		do {
   181 		
   182 			$MadeChangeDueToComments=0;
   183 		
   184 			if ($MultiLineComment==0){
   185 		
   186 				# remove text in // coments 
   187 				$templine=~ s/(.*?)\/\/.*$/$1/;
   188 				# remove /* */ comments found in one line
   189 				$templine=~ s/\/\*.*\*\///g;
   190 			
   191 				# if only /* then remove text after it and mark the start of comment
   192 				if ($templine=~ /^(.*?)\/\*/){
   193 					$templine=$1;
   194 					$MultiLineComment=1;
   195 					$MadeChangeDueToComments=1;
   196 					$FirstLine=1;
   197 				}
   198 			
   199 			} else {	# $MultiLineComment==1
   200 		
   201 				# if */ end comment
   202 				if ($templine=~ /\*\/(.*)$/){
   203 					$templine=$1;
   204 					$MultiLineComment=0;
   205 					$MadeChangeDueToComments=1;				
   206 				}
   207 			
   208 			}
   209 			
   210 		} while ($MadeChangeDueToComments==1);
   211 		# end of processing comments
   212 		
   213 		
   214 		if ($MultiLineComment==1 && $FirstLine==0) { next; } # skip checking if in comment
   215 	
   216 	
   217 		if (
   218 			$templine=~ /["'][cC]:/	|| # '" # need comment for correct highlighting in codewarrior
   219 			$templine=~ /EDriveC/		||
   220 			$templine=~ /\[.+\]\s*=\s*'[cC]':/
   221 			){
   222 			
   223 				my $Warning;
   224 				TypeOfWarning($FileName,$FileIsTest,$line,\$Warning);
   225 				printHTML($FileName,$linenumber,$line,$Warning);
   226 				if ($Warning eq "warning") { $$count++; }
   227 				
   228 			}
   229 		
   230 	}
   231 	
   232 	close(SOURCE);
   233 	
   234 	if ($prevCount<$$count) { $$countunique++; }
   235 }
   236 
   237 ################
   238 ########
   239 sub ReadExcludeList(){
   240 
   241 	#print "\n";
   242 	
   243 	
   244 	if ($exclude eq ""){
   245 		#print "Exclude list file not specified\nCarrying on without an exclude list\n";
   246 		return;
   247 	} elsif (!(-e $exclude)){
   248 		#print "Exclude list file doesn't exist!\nCarrying on without an exclude list\n";
   249 		return;
   250 	}
   251 	
   252 	
   253 	my $line; my $FileName; my $LineText; my $justification;
   254 	
   255 	open (EXCLUDE,"<$exclude");
   256 	while (1){
   257 	
   258 		$line=<EXCLUDE> or last;
   259 		$line=~ s/^(\s*)//g; $line=~ s/(\s*)$//g;
   260 		$line=~ s/\\/\//g;
   261 		$FileName=lc($line);
   262 		
   263 		$line=<EXCLUDE>; $line=~ s/^(\s*)//g; $line=~ s/(\s*)$//g;
   264 		$LineText=$line;
   265 		
   266 		$justification=0;
   267 		while($line=<EXCLUDE>){
   268 			$line=~ s/^(\s*)//g; $line=~ s/(\s*)$//g;
   269 			if ($line eq "") { last;}
   270 			$justification=1;
   271 		}
   272 		
   273 		if ($justification==0){
   274 			#print "Not added to the excludion list since no justification found for:\n$FileName\n$LineText\n";
   275 		} else {		
   276 			push(@{$Exclude{$FileName}},$LineText);
   277 		}
   278 		
   279 	}
   280 	close(EXCLUDE);
   281 	
   282 }
   283 
   284 ################
   285 ######## Main
   286 
   287 	
   288 	my $MyTest=0;
   289 	
   290 	GetOptions(
   291 			"exclude=s" => \$exclude ,
   292 			"excludedir=s" => \$excludedir, # one dir to be excluded from scaning (along wih its subdirs)
   293 			"log=s" => \$LogFileName,
   294 			"mytest" => \$MyTest # inteded for my personal testing (so that path is not the one for overnight testing)
   295 				);
   296 	
   297 	$excludedir=~ s/\\/\//g; # convert \ into / so that it matches perl's path form
   298 	
   299 	ReadExcludeList();
   300 
   301 	
   302 	if ($MyTest==0){ # overnight
   303 		$LogFileName = "$ENV{EPOCROOT}epoc32\\winscw\\c\\".$LogFileName;
   304 	}
   305 
   306 			
   307 	my $iArgv;
   308 	for ($iArgv=0;$iArgv<@ARGV;$iArgv++){
   309 		$ARGV[$iArgv]=~ s/\\/\//g; # convert \ into / so that it matches perl's path form
   310 		find(\&MakeList, ($ARGV[$iArgv]) );
   311 	}
   312 
   313 	
   314 	open(HTML,">$LogFileName");
   315 	print HTML "
   316 <html>
   317 
   318 <head>
   319 	<title>CDrive warnings</title>
   320 	
   321 	<style type=\'text/css\'>
   322 .warning {
   323 	background-color: #FFAAAA;
   324 }
   325 .ignoredwarning {
   326 	background-color: #90ffff;
   327 }
   328 .test{
   329 	background-color: #ffc060;
   330 }
   331 	</style>
   332 </head>
   333 <body>	
   334 	<table border=\"0\" width=\"100%\" cellpadding=\"0\" cellspacing=\"0\">
   335 ";
   336 	
   337 
   338 	my $count=0; my $countunique=0;
   339 	my $iList; my $nList=@ListOfFiles;
   340 	for ($iList=0;$iList<$nList;$iList++){
   341 		FindC($ListOfFiles[$iList],$FileIsTest[$iList],\$count,\$countunique);
   342 	}
   343 
   344 	
   345 	print HTML "\n</table>\n";
   346 
   347 	my $total=$nList-$nTests;
   348 	# workaround in order to be reported to the standardised system	
   349 	print HTML "<br><center>$countunique tests failed out of $total\n</center>\n";	
   350 	
   351 	print HTML "
   352 </body>
   353 </html>	
   354 	";
   355 	
   356 	#print "\n\tNumber of files:\t$nList\n";
   357 	#print "\n\tNumber of warnings:\t$count\n";
   358 	#print "\n\tNumber of unique warnings:\t$countunique\n";
   359 	
   360 	close(HTML);
   361 
   362 ################
   363 ######## end-Main
   364