ssl/tsrc/crypto_test/src/crypto_testBlocks.cpp
changeset 0 e4d67989cc36
child 9 fa985af77159
equal deleted inserted replaced
-1:000000000000 0:e4d67989cc36
       
     1 /*
       
     2 * Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies). All rights reserved.
       
     3 
       
     4 * Redistribution and use in source and binary forms, with or without 
       
     5 * modification, are permitted provided that the following conditions are met:
       
     6 
       
     7 * Redistributions of source code must retain the above copyright notice, this 
       
     8 * list of conditions and the following disclaimer.
       
     9 * Redistributions in binary form must reproduce the above copyright notice, 
       
    10 * this list of conditions and the following disclaimer in the documentation 
       
    11 * and/or other materials provided with the distribution.
       
    12 * Neither the name of Nokia Corporation nor the names of its contributors 
       
    13 * may be used to endorse or promote products derived from this software 
       
    14 * without specific prior written permission.
       
    15 
       
    16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 
       
    17 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 
       
    18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 
       
    19 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE 
       
    20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 
       
    21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 
       
    22 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 
       
    23 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 
       
    24 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 
       
    25 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
       
    26 *
       
    27 * Description:  ?Description
       
    28 *
       
    29 */
       
    30 
       
    31 
       
    32 
       
    33 // INCLUDE FILES
       
    34 #include <e32svr.h>
       
    35 #include <StifParser.h>
       
    36 #include <Stiftestinterface.h>
       
    37 #include "crypto_test.h"
       
    38 #include "tcrypto_test.h"
       
    39 #include <stdio.h>
       
    40 #include <string.h>
       
    41 #include  <stdlib.h>
       
    42 #include <errno.h>
       
    43 // EXTERNAL DATA STRUCTURES
       
    44 //extern  ?external_data;
       
    45 
       
    46 // EXTERNAL FUNCTION PROTOTYPES  
       
    47 //extern ?external_function( ?arg_type,?arg_type );
       
    48 
       
    49 // CONSTANTS
       
    50 //const ?type ?constant_var = ?constant;
       
    51 
       
    52 // MACROS
       
    53 //#define ?macro ?macro_def
       
    54 
       
    55 // LOCAL CONSTANTS AND MACROS
       
    56 //const ?type ?constant_var = ?constant;
       
    57 //#define ?macro_name ?macro_def
       
    58 
       
    59 // MODULE DATA STRUCTURES
       
    60 //enum ?declaration
       
    61 //typedef ?declaration
       
    62 
       
    63 // LOCAL FUNCTION PROTOTYPES
       
    64 //?type ?function_name( ?arg_type, ?arg_type );
       
    65 
       
    66 // FORWARD DECLARATIONS
       
    67 //class ?FORWARD_CLASSNAME;
       
    68 
       
    69 // ============================= LOCAL FUNCTIONS ===============================
       
    70 
       
    71 // -----------------------------------------------------------------------------
       
    72 // ?function_name ?description.
       
    73 // ?description
       
    74 // Returns: ?value_1: ?description
       
    75 //          ?value_n: ?description_line1
       
    76 //                    ?description_line2
       
    77 // -----------------------------------------------------------------------------
       
    78 //
       
    79 /*
       
    80 ?type ?function_name(
       
    81     ?arg_type arg,  // ?description
       
    82     ?arg_type arg)  // ?description
       
    83     {
       
    84 
       
    85     ?code  // ?comment
       
    86 
       
    87     // ?comment
       
    88     ?code
       
    89     }
       
    90 */
       
    91 
       
    92 // ============================ MEMBER FUNCTIONS ===============================
       
    93 
       
    94 // -----------------------------------------------------------------------------
       
    95 // Ccrypto_test::Delete
       
    96 // Delete here all resources allocated and opened from test methods. 
       
    97 // Called from destructor. 
       
    98 // -----------------------------------------------------------------------------
       
    99 //
       
   100 void Ccrypto_test::Delete() 
       
   101     {
       
   102 
       
   103     }
       
   104 
       
   105 // -----------------------------------------------------------------------------
       
   106 // Ccrypto_test::RunMethodL
       
   107 // Run specified method. Contains also table of test mothods and their names.
       
   108 // -----------------------------------------------------------------------------
       
   109 //
       
   110 TInt Ccrypto_test::RunMethodL( 
       
   111     CStifItemParser& aItem ) 
       
   112     {
       
   113 
       
   114     static TStifFunctionInfo const KFunctions[] =
       
   115         {  
       
   116         // Copy this line for every implemented function.
       
   117         // First string is the function name used in TestScripter script file.
       
   118         // Second is the actual implementation member function. 
       
   119         ENTRY( "BNTest", Ccrypto_test::BnTest ),
       
   120         ENTRY( "DESTest", Ccrypto_test::DesTest ),
       
   121         ENTRY( "DHTest", Ccrypto_test::DhTest ),
       
   122         ENTRY( "DSATest", Ccrypto_test::DsaTest ),
       
   123         ENTRY( "ENGINETest", Ccrypto_test::EngineTest ),
       
   124         ENTRY( "EVPTest", Ccrypto_test::EvpTest ),
       
   125         ENTRY( "EXPTest", Ccrypto_test::ExpTest ),
       
   126         ENTRY( "HMACTest", Ccrypto_test::HmacTest ),
       
   127         ENTRY( "MD2Test", Ccrypto_test::Md2Test ),
       
   128         ENTRY( "MD5Test", Ccrypto_test::Md5Test ),
       
   129         ENTRY( "RANDTest", Ccrypto_test::RandTest ),
       
   130         ENTRY( "RC2Test", Ccrypto_test::Rc2Test ),
       
   131         ENTRY( "RC4Test", Ccrypto_test::Rc4Test ),
       
   132         ENTRY( "RSATest", Ccrypto_test::RsaTest ),
       
   133         ENTRY( "SHATest", Ccrypto_test::ShaTest ),
       
   134         ENTRY( "SHA1Test", Ccrypto_test::Sha1Test ),
       
   135 
       
   136 
       
   137         };
       
   138 
       
   139     const TInt count = sizeof( KFunctions ) / 
       
   140                         sizeof( TStifFunctionInfo );
       
   141 
       
   142     return RunInternalL( KFunctions, count, aItem );
       
   143 
       
   144     }
       
   145     
       
   146 #ifdef __cplusplus
       
   147 extern "C"
       
   148 {
       
   149 #endif	
       
   150     
       
   151 FILE *fp_stdout=NULL;
       
   152 FILE *fp_stderr=NULL;
       
   153 
       
   154 #ifdef __cplusplus
       
   155 }
       
   156 #endif
       
   157 int cryptotest_init(char *mod)
       
   158 {
       
   159     char str[200];
       
   160     int len=0;
       
   161     if(!mod)
       
   162     return 1;
       
   163     len=strlen(LOG_STDOUT);
       
   164     strcpy(str,LOG_STDOUT);
       
   165     strcat(str,"_");
       
   166     strcat(str,mod);
       
   167     len+=strlen(mod);
       
   168     str[len+1]='\0';
       
   169 	fp_stdout = fopen(str,"a+b");
       
   170 	if(!fp_stdout)
       
   171 	return 1;
       
   172 	fp_stderr=fp_stdout;
       
   173 	return 0;
       
   174 }
       
   175 
       
   176 void cryptotest_deinit(void)
       
   177 {
       
   178     fclose(fp_stdout);
       
   179     fp_stderr=NULL;
       
   180     fp_stdout=NULL;
       
   181 }
       
   182 char ** MakeArgs(CStifItemParser& aItem,int * argc)
       
   183 {
       
   184 	char *ini_cmd[256];
       
   185 	char **cmd_line;
       
   186 	   
       
   187 	TInt len=0;
       
   188 	TPtrC string;
       
   189     TBuf8<50> buf1;
       
   190 	char* argument;
       
   191 	int cnt=0;
       
   192 	int i;
       
   193 	
       
   194 	//parse through the parameters of cfg to find the number and strings of cmd line
       
   195 		
       
   196 	while(aItem.GetNextString(string)!=-1)
       
   197 	{
       
   198 		buf1.Copy(string);
       
   199 		argument=(char *)buf1.Ptr();
       
   200 		len=buf1.Length();
       
   201 		argument[len]='\0';
       
   202 		
       
   203 		ini_cmd[cnt]=(char *)malloc(sizeof(char)*len+1);
       
   204 		if(ini_cmd[cnt]==NULL)
       
   205 		{
       
   206 			for(i=0;i<cnt;i++)
       
   207 			{
       
   208 				if(ini_cmd[i])
       
   209 			  free(ini_cmd[i]);
       
   210 		  }
       
   211 			return NULL;
       
   212 		}	
       
   213 		strcpy(ini_cmd[cnt],(const char *)argument);
       
   214 		
       
   215 		cnt++;
       
   216 	}
       
   217 	
       
   218 	//allocate memory for the command line ragged array
       
   219 	
       
   220 	cmd_line=(char **)malloc(cnt*sizeof(char *));
       
   221 	if(cmd_line==NULL)
       
   222 	{
       
   223 		for(i=0;i<cnt;i++)
       
   224   	   {
       
   225 	  	if(ini_cmd[i])
       
   226 		  free(ini_cmd[i]);
       
   227 	   }
       
   228     return NULL;
       
   229 	}	
       
   230 	
       
   231 	//initialize the array
       
   232 	
       
   233 	for(i=0;i<cnt;i++) cmd_line[i]=ini_cmd[i];
       
   234 	
       
   235 	//initialize argc
       
   236 	
       
   237 	*argc=cnt;
       
   238 	
       
   239 	return cmd_line;
       
   240 	
       
   241 }
       
   242 //-----------------------------------------------------------------------------
       
   243 //function function for destroying argv
       
   244 //-----------------------------------------------------------------------------
       
   245 void DeleteArgs(char ** cmd_line,int argc)
       
   246 {
       
   247 	int i;
       
   248 	for(i=0;i<argc;i++) free(cmd_line[i]);
       
   249 	
       
   250 	free(cmd_line);
       
   251 }
       
   252 
       
   253 
       
   254 // -----------------------------------------------------------------------------
       
   255 // Ccrypto_test::ExampleL
       
   256 // Example test method function.
       
   257 // (other items were commented in a header).
       
   258 // -----------------------------------------------------------------------------
       
   259 //
       
   260 
       
   261 
       
   262 TInt Ccrypto_test::BnTest( CStifItemParser&/* aItem*/ )
       
   263     {
       
   264     TInt ret=1;
       
   265     
       
   266     
       
   267     if(!cryptotest_init("bn"))
       
   268     {
       
   269      ret = bn_main(0,NULL);
       
   270      cryptotest_deinit();
       
   271 
       
   272     }
       
   273     if(ret==1&&errno==ENOMEM)
       
   274     {
       
   275     	return KErrNoMemory;
       
   276     }
       
   277     return ret;
       
   278 
       
   279     }
       
   280 
       
   281 
       
   282 
       
   283 TInt Ccrypto_test::DesTest( CStifItemParser&/* aItem*/ )
       
   284     {
       
   285     TInt ret=1;
       
   286     
       
   287     
       
   288     if(!cryptotest_init("des"))
       
   289     {
       
   290     ret = des_main(0,NULL);
       
   291     cryptotest_deinit();
       
   292 	
       
   293     }
       
   294     if(ret==1&&errno==ENOMEM)
       
   295     {
       
   296     	return KErrNoMemory;
       
   297     }
       
   298     return ret;
       
   299 
       
   300     return ret;
       
   301 
       
   302     }
       
   303     
       
   304     
       
   305 TInt Ccrypto_test::DhTest( CStifItemParser&/* aItem*/ )
       
   306     {
       
   307     TInt ret=1;
       
   308     
       
   309       
       
   310     if(!cryptotest_init("dh"))
       
   311     {
       
   312     ret = dh_main(0,NULL);
       
   313     cryptotest_deinit();
       
   314     }
       
   315     if(ret==1&&errno==ENOMEM)
       
   316     {
       
   317     	return KErrNoMemory;
       
   318     }
       
   319     return ret;
       
   320 
       
   321     }    
       
   322     
       
   323 TInt Ccrypto_test::DsaTest( CStifItemParser&/* aItem*/ )
       
   324     {
       
   325     TInt ret=1;
       
   326     if(!cryptotest_init("dsa"))
       
   327     {
       
   328     ret = dsa_main(0,NULL);
       
   329     cryptotest_deinit();
       
   330     
       
   331     }
       
   332     if(ret==1&&errno==ENOMEM)
       
   333     {
       
   334     	return KErrNoMemory;
       
   335     }
       
   336     return ret;
       
   337     
       
   338 
       
   339     }      
       
   340     
       
   341     
       
   342 TInt Ccrypto_test::EngineTest( CStifItemParser&/* aItem*/ )
       
   343     {
       
   344     TInt ret=1;
       
   345     
       
   346     if(!cryptotest_init("eng"))
       
   347     {
       
   348     ret = engine_main(0,NULL);
       
   349     cryptotest_deinit();
       
   350     	
       
   351     }
       
   352     if(ret==1&&errno==ENOMEM)
       
   353     {
       
   354     	return KErrNoMemory;
       
   355     }
       
   356     return ret;
       
   357 
       
   358     }      
       
   359     
       
   360 TInt Ccrypto_test::EvpTest( CStifItemParser& aItem )
       
   361     {
       
   362     TInt ret=1;
       
   363     int argc=0;
       
   364     char** argv=NULL;
       
   365 
       
   366     argv = MakeArgs(aItem,&argc);
       
   367     if(!argv)
       
   368     {
       
   369       return KErrNoMemory;    
       
   370     }	
       
   371     if(!cryptotest_init("evp"))
       
   372     {
       
   373     ret = evp_main(argc,argv);
       
   374     cryptotest_deinit();
       
   375     
       
   376     }
       
   377     DeleteArgs(argv,argc);
       
   378     if(ret==1&&errno==ENOMEM)
       
   379     {
       
   380     	return KErrNoMemory;
       
   381     }
       
   382     return ret;
       
   383     }     
       
   384     
       
   385     
       
   386     
       
   387 TInt Ccrypto_test::ExpTest( CStifItemParser& /*aItem */)
       
   388     {
       
   389     TInt ret=1;
       
   390     
       
   391     if(!cryptotest_init("exp"))
       
   392     {
       
   393     ret = exp_main(0,NULL);
       
   394     cryptotest_deinit();
       
   395    
       
   396     }
       
   397    if(ret==1&&errno==ENOMEM)
       
   398     {
       
   399     	return KErrNoMemory;
       
   400     }
       
   401     return ret;
       
   402 
       
   403     }          
       
   404 TInt Ccrypto_test::HmacTest( CStifItemParser& /*aItem */)
       
   405     {
       
   406     TInt ret=1;
       
   407     
       
   408     if(!cryptotest_init("hmac"))
       
   409     {
       
   410     ret = hmac_main(0,NULL);
       
   411     cryptotest_deinit();
       
   412     	
       
   413     }
       
   414     if(ret==1&&errno==ENOMEM)
       
   415     {
       
   416     	return KErrNoMemory;
       
   417     }
       
   418     return ret;
       
   419 
       
   420     }     
       
   421 
       
   422              
       
   423 TInt Ccrypto_test::Md2Test( CStifItemParser& /*aItem */)
       
   424     {
       
   425     TInt ret=1;
       
   426     
       
   427     if(!cryptotest_init("md2"))
       
   428     {
       
   429     ret = md2_main(0,NULL);
       
   430     cryptotest_deinit();
       
   431     	
       
   432     }
       
   433     if(ret==1&&errno==ENOMEM)
       
   434     {
       
   435     	return KErrNoMemory;
       
   436     }
       
   437     return ret;
       
   438 
       
   439     }   
       
   440 TInt Ccrypto_test::Md5Test( CStifItemParser& /*aItem */)
       
   441     {
       
   442     TInt ret=1;
       
   443 
       
   444     if(!cryptotest_init("md5"))
       
   445     {
       
   446     ret = md5_main(0,NULL);
       
   447     cryptotest_deinit();
       
   448     	
       
   449     }
       
   450     if(ret==1&&errno==ENOMEM)
       
   451     {
       
   452     	return KErrNoMemory;
       
   453     }
       
   454     return ret;
       
   455 
       
   456     }   
       
   457 TInt Ccrypto_test::RandTest( CStifItemParser& /*aItem */)
       
   458     {
       
   459     TInt ret=1;
       
   460 
       
   461     if(!cryptotest_init("rand"))
       
   462     {
       
   463     ret = rand_main(0,NULL);
       
   464     cryptotest_deinit();
       
   465     	
       
   466     }
       
   467     if(ret==1&&errno==ENOMEM)
       
   468     {
       
   469     	return KErrNoMemory;
       
   470     }
       
   471     return ret;
       
   472 
       
   473     }      
       
   474     
       
   475     
       
   476 TInt Ccrypto_test::Rc2Test( CStifItemParser& /*aItem */)
       
   477     {
       
   478     TInt ret=1;
       
   479 
       
   480     if(!cryptotest_init("rc2"))
       
   481     {
       
   482     ret = rc2_main(0,NULL);
       
   483     cryptotest_deinit();
       
   484     	
       
   485     }
       
   486 
       
   487     if(ret==1&&errno==ENOMEM)
       
   488     {
       
   489     	return KErrNoMemory;
       
   490     }
       
   491     return ret;
       
   492 
       
   493     }      
       
   494     
       
   495 TInt Ccrypto_test::Rc4Test( CStifItemParser& /*aItem */)
       
   496     {
       
   497     TInt ret=1;
       
   498 
       
   499     if(!cryptotest_init("rc4"))
       
   500     {
       
   501     ret = rc4_main(0,NULL);
       
   502     cryptotest_deinit();
       
   503     	
       
   504     }
       
   505 
       
   506     if(ret==1&&errno==ENOMEM)
       
   507     {
       
   508     	return KErrNoMemory;
       
   509     }
       
   510     return ret;
       
   511 
       
   512     }     
       
   513 
       
   514 TInt Ccrypto_test::RsaTest( CStifItemParser& /*aItem */)
       
   515     {
       
   516     TInt ret=1;
       
   517 
       
   518     if(!cryptotest_init("rsa"))
       
   519     {
       
   520     ret = rsa_main(0,NULL);
       
   521     cryptotest_deinit();
       
   522     	
       
   523     }
       
   524 
       
   525     if(ret==1&&errno==ENOMEM)
       
   526     {
       
   527     	return KErrNoMemory;
       
   528     }
       
   529     return ret;
       
   530 
       
   531     }      
       
   532     
       
   533 TInt Ccrypto_test::ShaTest( CStifItemParser& /*aItem */)
       
   534     {
       
   535     TInt ret=1;
       
   536 
       
   537     if(!cryptotest_init("sha"))
       
   538     {
       
   539     ret = sha_main(0,NULL);
       
   540     cryptotest_deinit();
       
   541     	
       
   542     }
       
   543 
       
   544     if(ret==1&&errno==ENOMEM)
       
   545     {
       
   546     	return KErrNoMemory;
       
   547     }
       
   548     return ret;
       
   549 
       
   550     }     
       
   551 
       
   552 TInt Ccrypto_test::Sha1Test( CStifItemParser& /*aItem */)
       
   553     {
       
   554     TInt ret=1;
       
   555 
       
   556     if(!cryptotest_init("sha1"))
       
   557     {
       
   558     ret = sha1_main(0,NULL);
       
   559     cryptotest_deinit();
       
   560     	
       
   561     }
       
   562 
       
   563     if(ret==1&&errno==ENOMEM)
       
   564     {
       
   565     	return KErrNoMemory;
       
   566     }
       
   567     return ret;
       
   568 
       
   569     }                                         
       
   570     
       
   571     
       
   572     
       
   573                                        
       
   574 // -----------------------------------------------------------------------------
       
   575 // Ccrypto_test::?member_function
       
   576 // ?implementation_description
       
   577 // (other items were commented in a header).
       
   578 // -----------------------------------------------------------------------------
       
   579 //
       
   580 /*
       
   581 TInt Ccrypto_test::?member_function(
       
   582    CItemParser& aItem )
       
   583    {
       
   584 
       
   585    ?code
       
   586 
       
   587    }
       
   588 */
       
   589 
       
   590 // ========================== OTHER EXPORTED FUNCTIONS =========================
       
   591 // None
       
   592 
       
   593 //  End of File