secureswitools/swisistools/test/tscrtool/tscrtool.pl
changeset 0 ba25891c3a9e
equal deleted inserted replaced
-1:000000000000 0:ba25891c3a9e
       
     1 #
       
     2 # Copyright (c) 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 # testscrtool.pl
       
    16 # This perl file is used to test the scrtool used for generating database.
       
    17 # @internalComponent
       
    18 #
       
    19 
       
    20 use strict;
       
    21 use warnings;
       
    22 use File::Path;
       
    23 use File::Copy;
       
    24 use Cwd;
       
    25 
       
    26 my $LOGFD;
       
    27 
       
    28 #
       
    29 # Error Messages
       
    30 #
       
    31 my $KErrFileOpen 		= "Error opening file";
       
    32 
       
    33 #
       
    34 # Error Codes
       
    35 #
       
    36 # General errors
       
    37 #
       
    38 my %ErrorCode;
       
    39 $ErrorCode{"EUnintializedCode"} = -1;
       
    40 $ErrorCode{"ENone"} = 0;
       
    41 $ErrorCode{"EEnvNotSpecified"} = 1;
       
    42 
       
    43 # options exception
       
    44 $ErrorCode{"EInvalidArgument"} = 10;
       
    45 $ErrorCode{"EMandatoryOption"} = 11;
       
    46 $ErrorCode{"EMutuallyExclusive"} = 12;
       
    47 $ErrorCode{"EFileNotPresent"} = 13;
       
    48 $ErrorCode{"EFileExists"} = 14;
       
    49 
       
    50 # xml exceptions
       
    51 $ErrorCode{"EWarning"}=20;
       
    52 $ErrorCode{"EError"} = 21;
       
    53 $ErrorCode{"EFatalError"} = 22;
       
    54 $ErrorCode{"EParseError"} = 23;
       
    55 $ErrorCode{"EResetError"} = 24;
       
    56 
       
    57 # database exceptions
       
    58 $ErrorCode{"ELibraryLoadError"}=30;
       
    59 $ErrorCode{"ESqlArgumentError"} = 31;
       
    60 $ErrorCode{"ESqlNotFoundError"} = 32;
       
    61 $ErrorCode{"ESqlCorrupt"} = 33;
       
    62 
       
    63 
       
    64 my $KEndl 			= "\n";
       
    65 my $KSpace			= " ";
       
    66 my $KHeader 		= "scrtool Test Suite";
       
    67 
       
    68 my $KOption 		= "option";
       
    69 my $KResult 		= "result";
       
    70 my $KExpectedResult	= "Expected result";
       
    71 my $KDbExists		= "checkdbexists";
       
    72 my $KOutcome 		= "Outcome";
       
    73 my $KPass		= "PASS";
       
    74 my $KFail		= "FAIL";
       
    75 my $KAppPath		= "$ENV{'EPOCROOT'}epoc32\\tools\\";
       
    76 my $KAppName 		= $KAppPath."scrtool.exe";
       
    77 
       
    78 my $KPath		= "$ENV{'EPOCROOT'}epoc32\\winscw\\c\\tswi\\tscrtool\\";
       
    79 my $KInputPath	= $KPath."data\\";
       
    80 
       
    81 my $numTotal = 0;
       
    82 my $numPassed = 0;
       
    83 my $numFailed = 0; 
       
    84 
       
    85 my @id;
       
    86 my @dbpath = 0;
       
    87 my @option;
       
    88 my @result;
       
    89 
       
    90 
       
    91 ################## functions ##################
       
    92 
       
    93 
       
    94 sub main()
       
    95 	{
       
    96 	InitLogFile();
       
    97 	# Display header
       
    98 	LogString($KHeader);
       
    99 	LogString($KEndl);
       
   100 	TestScrTool();
       
   101 
       
   102 	my $run = "Run: $numTotal\n";
       
   103 	my $passed = "Passed: $numPassed\n";
       
   104 	my $summary = "$numFailed tests failed out of $numTotal\n";
       
   105 	LogString($run);
       
   106 	LogString($passed);
       
   107 	LogString($summary);
       
   108 	}
       
   109 
       
   110 sub InitLogFile()
       
   111 	{
       
   112 	my $KLogPath = "$ENV{'EPOCROOT'}epoc32\\winscw\\c\\";
       
   113 	if(! -e $KLogPath )
       
   114 	{
       
   115 	mkdir($KLogPath);
       
   116 	}
       
   117 	my $KLogFileName	= $KLogPath."tscrtool.txt";
       
   118 	# Open Log file in append mode
       
   119 	unless(open LOGFD, ">>$KLogFileName")
       
   120 		{
       
   121 		print STDOUT $KErrFileOpen.$KSpace.$KLogFileName.$KEndl;
       
   122 		exit;
       
   123 		}
       
   124 	}
       
   125 
       
   126 sub TestScrTool()
       
   127 	{
       
   128 	my $KScriptFile = $KPath."tscrtool.ini"; 
       
   129 	unless(open FD, "<$KScriptFile" )
       
   130 		{
       
   131 		LogString($KErrFileOpen.$KSpace.$KScriptFile);
       
   132 		LogString($KEndl);
       
   133         return;
       
   134 		}
       
   135 
       
   136 	GetInput();
       
   137 	close FD;
       
   138 
       
   139 	my $i = 0;
       
   140 	my $id;
       
   141 	my %Input;
       
   142 
       
   143 	while($id[$i])
       
   144 	{
       
   145         $Input{$id[$i]}{$KOption} = $option[$i];
       
   146         $Input{$id[$i]}{$KResult} = $result[$i];
       
   147 		$Input{$id[$i]}{$KDbExists} = $dbpath[$i];
       
   148 		$i = $i + 1;
       
   149 	}
       
   150 	
       
   151 	my $completecommand;
       
   152 	my $testresult;
       
   153 	my $expected;
       
   154 
       
   155     for $id ( sort keys % Input )
       
   156 		{
       
   157 		$numTotal++;
       
   158 		$completecommand = $KAppName.$Input{$id}{$KOption};
       
   159 		$testresult = system($completecommand);
       
   160  		$testresult = $testresult >> 8;
       
   161 		$testresult = $testresult;
       
   162 		LogString($KEndl."Test ID = $id $KEndl");
       
   163        	LogString("$KOption = $Input{$id}{$KOption} $KEndl");
       
   164 		LogString("Complete command: ".$completecommand.$KEndl);
       
   165        	
       
   166 		$expected = $Input{$id}{$KResult};
       
   167 		my $expectedErrorCode = $ErrorCode{$expected};
       
   168 
       
   169 		LogString("$KExpectedResult = ".$expectedErrorCode." ($Input{$id}{$KResult})".$KEndl);
       
   170                 
       
   171 		LogString("Actual Result:".$testresult.$KEndl);
       
   172 
       
   173 		LogString("$KOutcome = ");
       
   174         
       
   175 		if($testresult == $expectedErrorCode)
       
   176         	{
       
   177 			if($Input{$id}{$KDbExists} )
       
   178 				{
       
   179 				my $db = $Input{$id}{$KDbExists};
       
   180 				if( -e "$db")
       
   181 					{
       
   182 					LogString($KPass);
       
   183 					$numPassed++;
       
   184 					}
       
   185 				else
       
   186 					{
       
   187 					LogString($KFail);
       
   188 					$numFailed++;
       
   189 					}
       
   190 				}
       
   191 			else
       
   192 				{
       
   193 				LogString($KPass);
       
   194 				$numPassed++;
       
   195 				}
       
   196 			}
       
   197         else
       
   198         	{
       
   199            LogString($KFail);
       
   200 		   $numFailed++;
       
   201         	}
       
   202 		LogString($KEndl);
       
   203 		}
       
   204 
       
   205 
       
   206 =comment until cut;
       
   207     for $id ( keys %Input  )
       
   208 		{
       
   209         	print "---------------------", $KEndl;
       
   210 		    print "Test ID = $id", $KEndl;
       
   211             print "$KOption = $Input{$id}{$KOption}", $KEndl;
       
   212             print "$KResult = $Input{$id}{$KResult}", $KEndl;
       
   213 		}
       
   214 	print $KEndl, "END OF FILE", $KEndl;
       
   215 =cut
       
   216 
       
   217 }
       
   218 
       
   219 # Takes a string as it's argument and replaces every instance of %EPOCROOT% in the argument with the relevant environment variable's value.
       
   220 sub ReplaceEpocDir
       
   221 	{
       
   222 	my $string_args = $_[0];
       
   223 	my $path_prefix = $ENV{'EPOCROOT'};
       
   224 	
       
   225 	while ()
       
   226 		{
       
   227 		last if !($string_args =~ s/%EPOCROOT%/$path_prefix/);
       
   228 		}
       
   229 	return $string_args;
       
   230 	}
       
   231 
       
   232 sub GetInput()
       
   233 	{
       
   234 	# File descriptor passed by the callee
       
   235 		
       
   236 	my $i = 0;
       
   237 	my $startPos;
       
   238 	my $endPos;
       
   239 
       
   240 	while(<FD>)
       
   241 		{
       
   242 		# Replaces all instances of %EPOCROOT% in $_.
       
   243 		$_ = ReplaceEpocDir($_);
       
   244 		if ($_ !~/^[\t ]*#/)
       
   245 			{
       
   246 			if($_ =~/^\[.*\]/)
       
   247 				{
       
   248                 $startPos = rindex $_, "[";
       
   249                 $startPos = $startPos + 1;
       
   250                 $endPos = rindex $_, "]";
       
   251                 $id[$i] = substr $_, $startPos, $endPos - $startPos;
       
   252 				}
       
   253 			
       
   254 			if($_ =~/^$KDbExists/)
       
   255 				{
       
   256 				$startPos = rindex $_, "=";
       
   257                 $startPos = $startPos + 1;
       
   258                 $dbpath[$i] = substr $_, $startPos;
       
   259 				$dbpath[$i]=~s/\s//g;
       
   260 				chomp($dbpath[$i]);
       
   261 				}
       
   262 
       
   263 			if($_ =~/^$KOption/)
       
   264 				{
       
   265 				$startPos = rindex $_, "=";
       
   266                 $startPos = $startPos + 1;
       
   267                 $option[$i] = substr $_, $startPos;
       
   268 				chomp($option[$i]);
       
   269 				}
       
   270 			
       
   271 			if($_ =~/^$KResult/)
       
   272 				{
       
   273 				$startPos = rindex $_, "=";
       
   274                 if($startPos eq -1)
       
   275                     {
       
   276                     die "Invalid result type";
       
   277                     }
       
   278 				$startPos = $startPos + 1 ;
       
   279                 $result[$i] = substr $_, $startPos;
       
   280 				chomp($result[$i]);
       
   281     			$i++;
       
   282 				}
       
   283 
       
   284 			} # end of if
       
   285 		} # end of while
       
   286 	} # end of GetInput()
       
   287 
       
   288 
       
   289 sub CreateFile()
       
   290 	{
       
   291 	
       
   292 	my $filename = $_[0];
       
   293 	unless(open FILEFD, ">$filename")
       
   294 		{
       
   295 		LogString($KErrFileOpen.$KSpace.$filename);
       
   296 		LogString($KEndl);
       
   297 		die;
       
   298 		}
       
   299 	
       
   300 	close FILEFD;	
       
   301 	}
       
   302 
       
   303 
       
   304 
       
   305 
       
   306 sub LogString()
       
   307 	{
       
   308 	my @strBuffer = @_;
       
   309 	print LOGFD @strBuffer;
       
   310 	print STDOUT @strBuffer;
       
   311 	}
       
   312 
       
   313 main();