os/ossrv/lowlevellibsandfws/apputils/multipartparser/test/tef/smoketest/src/mppsmoketestlib.cpp
author sl
Tue, 10 Jun 2014 14:32:02 +0200
changeset 1 260cb5ec6c19
permissions -rw-r--r--
Update contrib.
     1 // Copyright (c) 2008-2009 Nokia Corporation and/or its subsidiary(-ies).
     2 // All rights reserved.
     3 // This component and the accompanying materials are made available
     4 // under the terms of "Eclipse Public License v1.0"
     5 // which accompanies this distribution, and is available
     6 // at the URL "http://www.eclipse.org/legal/epl-v10.html".
     7 //
     8 // Initial Contributors:
     9 // Nokia Corporation - initial contribution.
    10 //
    11 // Contributors:
    12 //
    13 // Description:
    14 //
    15 
    16 
    17 #include "mppsmoketestlib.h"
    18 #include <flogger.h>
    19 //#include "xmlengtesterdef.h"
    20 //#include <utils/xmlengxestd.h>
    21 
    22 /**
    23  * Utility fcn to compare two files ( skip or not skip white space ).
    24  * @param aSrcFile full filename of a file you want to check/compare.
    25  * @param aVerificationFile fill filename of a correct output file, aSrcFile will be compared to this to verify it's validity.
    26  * @param aSkipWhiteSpace do not include whitespace when comparing the two files.
    27  * @return Symbian OS error code.
    28  */
    29 TInt CMultipartTestContainer::CompareFilesL( TPtrC aSrcFile, TPtrC aVerificationFile,
    30 																				 TBool aSkipWhiteSpace)
    31 {
    32     _LIT(KSourceFileError,"Source file error.");
    33     _LIT(KPatternFileError,"Pattern file error.");
    34     _LIT(KComparePassed,"Files compare test PASSED.");
    35     _LIT(KCompareFailed,"Files compare test FAILED.");
    36     
    37     TInt nResult = KErrNone;
    38     
    39     TBool skipWhite = FALSE;
    40     TBool foundRes = FALSE;
    41     TBool foundRef = FALSE;
    42     
    43     RFs fs;
    44 	User::LeaveIfError(fs.Connect());
    45 	CleanupClosePushL(fs);
    46     
    47     RFile srcFile;
    48     RFile referenceFile;
    49 
    50     TFileName srcFileName;
    51 	TFileName referenceFileName;
    52     
    53   TPtrC SrcFile = aSrcFile;
    54   TPtrC ReferenceFile = aVerificationFile;
    55 	if (aSkipWhiteSpace)
    56 	 {
    57 		 skipWhite = TRUE;
    58 	 }	
    59     
    60     if ( nResult == KErrNone )
    61 	{
    62 		srcFileName.Copy(SrcFile);
    63 		referenceFileName.Copy(ReferenceFile);
    64 
    65 	    if(srcFile.Open(fs, srcFileName, EFileStream|EFileRead) == KErrNone)
    66 		{
    67 			// Does reference file exist.
    68 			if(referenceFile.Open(fs, referenceFileName, EFileStream|EFileRead) == KErrNone)
    69 		    {
    70 		        // Integer variables for compare to length of files (result and reference).
    71 		        TInt resSize;
    72 		        TInt refSize;
    73 
    74 		        srcFile.Size(resSize);
    75 		        referenceFile.Size(refSize);
    76 
    77 	            // Next compare one letter at the time, but only if files have same length.
    78 	            if(skipWhite)
    79 		        {
    80 			        TBuf8<1> resBuf;
    81 			        TBuf8<1> refBuf;
    82 			        nResult = KErrNone;
    83 			        TInt j = 0;
    84 			        TInt i = 0;
    85 			        
    86 			        //for(TInt i = 0; i < Size; i++)
    87 			        while (TRUE)
    88 				    {
    89 				    	foundRes = FALSE;
    90 				    	foundRef = FALSE;
    91 				        // Read result file
    92 				        while(i < (resSize + 1))
    93 				        {
    94 				        	i++;
    95 				        	srcFile.Read(resBuf);
    96 				        	resBuf.Trim();	
    97 				        	if ( resBuf.Length() > 0)
    98 				        	{
    99 				        		foundRes = TRUE;
   100 				        		break;
   101 				        	}
   102 				        }
   103 						
   104 						// Read reference file
   105 				        while(j < (refSize + 1))
   106 				        {
   107 				        	j++;
   108 				        	referenceFile.Read(refBuf);
   109 				        	refBuf.Trim();
   110 				        	if ( refBuf.Length() > 0)
   111 				        	{
   112 				        		foundRef = TRUE;
   113 				        		break;
   114 				        	}
   115 				        }
   116 				        
   117 				        // Compare single letter at the time.
   118 				        if( ( i < resSize ) && ( j < refSize ) && (resBuf[0] != refBuf[0]) )
   119 				        {
   120 					        nResult = KErrGeneral;
   121 					        break;
   122 					    }
   123 					    if( (i == (resSize + 1)) && (j < refSize) && foundRef)
   124 				    	{
   125 					    	nResult = KErrGeneral;
   126 					        break;
   127 				    	}
   128 					    if( (i < resSize) && (j == (refSize + 1)) && foundRes)
   129 				    	{
   130 					    	nResult = KErrGeneral;
   131 					        break;
   132 				    	}
   133 				    	if ((i > resSize) && (j > refSize))
   134 				    		break;
   135 				    }
   136 			    }
   137 		        else
   138 			    {
   139 			        if (resSize != refSize)
   140 			       		nResult = KErrGeneral;
   141 			        else
   142 		        	{
   143 				        TBuf8<1> resBuf;
   144 			        	TBuf8<1> refBuf;
   145 			        	nResult = KErrNone;
   146 			        	for(TInt i = 0; i < resSize; i++)
   147 			        	{
   148 				        	// Read result file
   149 				        	srcFile.Read(resBuf);
   150 
   151 					        // Read reference file
   152 					        referenceFile.Read(refBuf);
   153 
   154 					        // Compare single letter at the time.
   155 
   156 					        if(resBuf[0] != refBuf[0])
   157 					        {
   158 						        nResult = KErrGeneral;
   159 					    	    break;
   160 				        	}
   161 			        	}
   162 		        	}
   163 			    }
   164 	            referenceFile.Close();
   165 	            srcFile.Close();
   166 			}
   167 			else
   168 			{
   169 				nResult = KErrGeneral;
   170 				INFO_PRINTF1(KPatternFileError);
   171 			}
   172 			srcFile.Close();
   173 		}
   174 		else
   175 		{
   176 			nResult = KErrGeneral;
   177 			INFO_PRINTF1(KSourceFileError);
   178 		}
   179 			
   180 	}
   181 	
   182 	CleanupStack::PopAndDestroy(&fs);
   183 	
   184 	if ( nResult == KErrNone)
   185 		{
   186 		INFO_PRINTF1(KComparePassed);
   187 		}
   188 	else
   189 		{
   190 		INFO_PRINTF1(KCompareFailed);
   191 		}
   192 
   193 	return nResult;
   194     }
   195 
   196 CMultipartTestContainer::CMultipartTestContainer(CTestExecuteLogger& aLogger)
   197  : iLog(aLogger)
   198     {
   199     }
   200 
   201 /**
   202  * @param aLogger TEF logger from CTestStep.
   203  */
   204 void CMultipartTestContainer::ConstructL()
   205 	{
   206 	}
   207 
   208 /**
   209  * @param aLogger TEF logger from CTestStep.
   210  */
   211 CMultipartTestContainer* CMultipartTestContainer::NewLC(CTestExecuteLogger& aLogger)
   212 	{
   213 	CMultipartTestContainer* self = new (ELeave) CMultipartTestContainer(aLogger);
   214     
   215 	CleanupStack::PushL( self );
   216 	self->ConstructL();
   217 
   218 	return self;
   219     
   220 	}
   221     
   222 /**
   223  * @param aLogger TEF logger from CTestStep.
   224  */
   225 CMultipartTestContainer* CMultipartTestContainer::NewL(CTestExecuteLogger &aLogger)
   226 	{
   227 	CMultipartTestContainer* self = NewLC(aLogger );
   228 	CleanupStack::Pop( self );
   229 
   230 	return self;
   231     
   232 	}
   233     
   234 CMultipartTestContainer::~CMultipartTestContainer()
   235 	{ 
   236 	}
   237 
   238 /**
   239  * Read a file from the hard drive.
   240  * 
   241  * @param aFileName full filename to read in.
   242  * @return contents of the file.
   243  */ 
   244 HBufC8* CMultipartTestContainer::ReadFileLC(const TDesC& aFileName)
   245     {
   246     RFs rfs;
   247     RFile file;
   248     User::LeaveIfError(rfs.Connect());
   249     CleanupClosePushL(rfs);
   250     User::LeaveIfError(file.Open(rfs, aFileName, EFileRead));
   251     CleanupClosePushL(file);
   252     TInt size;
   253     User::LeaveIfError(file.Size(size));
   254     HBufC8* buf = HBufC8::NewLC(size);
   255     TPtr8 bufPtr(buf->Des());
   256     User::LeaveIfError(file.Read(bufPtr));
   257     CleanupStack::Pop(); // buf
   258     CleanupStack::PopAndDestroy(2); // file, rfs
   259     CleanupStack::PushL(buf);
   260     return buf;
   261     }
   262 
   263 /**
   264  * Write the file to the hard drive.
   265  * 
   266  * @param aFileName 
   267  * @param aBodyPartArray
   268  */ 
   269 void CMultipartTestContainer::WriteFileL( const TDesC& aFileName,
   270                                            RPointerArray<CBodyPart>& aBodyPartArray )
   271     {
   272     RFs fs;
   273     fs.Connect();
   274     CleanupClosePushL(fs);
   275     RFile output;
   276     TInt err = output.Open(fs, aFileName, EFileWrite);
   277     if (err == KErrNotFound)
   278     	{
   279 			User::LeaveIfError(output.Create(fs, aFileName, EFileWrite));
   280 			}		
   281     
   282     // output file
   283     TInt size = aBodyPartArray.Count();
   284     TInt i;
   285 		_LIT8(KEol, "\r\n");
   286     for (i = 0; i < size; i++)
   287         {
   288 
   289         CBodyPart* bodyPart = aBodyPartArray[i];
   290 
   291         if( bodyPart->Url().Ptr() )
   292             {
   293             output.Write(_L8("***************Ur"));
   294             output.Write(KEol);
   295 			RBuf8 narrow;
   296 			err = narrow.Create(bodyPart->Url().Length()*2);
   297 			if (err != KErrNone)
   298 				{
   299 				INFO_PRINTF1(_L("Error printing Url to output file; continueing..."));
   300 				}						
   301 			narrow.Copy(bodyPart->Url());
   302             output.Write(narrow.Left(narrow.Length()));
   303             output.Write(KEol);
   304             }
   305         if( bodyPart->ContentID().Ptr() )
   306             {
   307             output.Write(_L8("***************ContentID"));
   308             output.Write(KEol);
   309             output.Write(bodyPart->ContentID() );
   310             output.Write(KEol);
   311             }
   312         if( bodyPart->Charset().Ptr() )
   313             {
   314             output.Write(_L8("***************Charset"));
   315             output.Write(KEol);
   316             output.Write( bodyPart->Charset() );
   317             output.Write(KEol);
   318             }
   319         if( bodyPart->ContentType().Ptr() )
   320             {
   321             output.Write(_L8("***************ContentType"));
   322             output.Write(KEol);
   323             output.Write( bodyPart->ContentType() );
   324             output.Write(KEol);
   325             }
   326         if( bodyPart->Headers().Ptr() )
   327             {
   328             output.Write(_L8("***************Headers"));
   329             output.Write(KEol);
   330             output.Write(bodyPart->Headers() );
   331             output.Write(KEol);
   332             }
   333         if( bodyPart->Body().Ptr() )
   334             {
   335             output.Write(_L8("***************Body"));
   336             output.Write(KEol);
   337             output.Write(bodyPart->Body() );
   338             output.Write(KEol);
   339             }
   340         output.Write(_L8("=========================================part ends"));
   341         output.Write(KEol);
   342 
   343         } // end of loop
   344     
   345     output.Close();
   346     CleanupStack::PopAndDestroy(1, &fs);
   347     fs.Close();
   348 		}
   349 		
   350 
   351 /**
   352  * Write the data to a file.
   353  * 
   354  * @param aFileName  filename to write the data to.
   355  * @param aData data to write.
   356  */
   357 void CMultipartTestContainer::WriteToDiskL( const TDesC& aFileName,
   358                                             const TDesC8& aData )
   359     {
   360     RFs fsSession;
   361     RFile file;
   362     TInt status( KErrNone );
   363     User::LeaveIfError( fsSession.Connect() );
   364     status = file.Replace( fsSession, aFileName, EFileWrite|EFileShareAny);    
   365     if( status == KErrNone )
   366         {
   367         file.Write( aData );
   368         }
   369     file.Close();
   370     }
   371 
   372 
   373 /**
   374  * Parse a file with base64 encoding.
   375  * 
   376  * @param aFile filename to be read in.
   377  * @param aUrl  ???  //DEBUG
   378  * @param aOutput name out output file.
   379  */ 
   380 void CMultipartTestContainer::ParseFileL( const TDesC& aFile,
   381                                           const TDesC& aUrl,
   382                                           const TDesC& aOutput )
   383     {
   384     // body
   385     HBufC8* data = ReadFileLC( aFile );
   386     // content type
   387     _LIT8(KMultipartMixed, "multipart/mixed");
   388     // boundary
   389     _LIT8(KBoundary1, "KBoundary");
   390     // body part array
   391     RPointerArray<CBodyPart> bodyPartArray;  
   392 
   393     // parse
   394 		TInt err;
   395     if( aUrl.Ptr() )
   396         {
   397         TRAP( err, MultipartParser::ParseL( *data, KMultipartMixed, KBoundary1, aUrl, bodyPartArray ) );
   398         }
   399     else
   400         {
   401         TRAP( err, MultipartParser::ParseL( *data, KMultipartMixed, KBoundary1, aFile, bodyPartArray ) );
   402         }
   403 	if (err != KErrNone)
   404 		{
   405 		INFO_PRINTF1(_L("Error parsing file."));
   406 		}			
   407 
   408     // output
   409     WriteFileL( aOutput, bodyPartArray);
   410 
   411     // clean up memory
   412     CleanupStack::PopAndDestroy(); // data
   413     // clean up body part array and its content
   414     TInt size = bodyPartArray.Count();
   415     TInt i;
   416     for (i = 0; i < size; i++)
   417         {
   418         delete bodyPartArray[i];
   419         }
   420     bodyPartArray.Reset();
   421     }