compressionlibs/ziplib/test/rtest/decompresstest/decompresstest.cpp
changeset 0 e4d67989cc36
equal deleted inserted replaced
-1:000000000000 0:e4d67989cc36
       
     1 // Copyright (c) 2003-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 #include <e32cmn.h>
       
    17 #include <f32file.h>
       
    18 #include <e32test.h>
       
    19 #include <ezgzip.h>
       
    20 #include <ezfilebuffer.h>
       
    21 #include <ezdecompressor.h>
       
    22 
       
    23 _LIT(KTestTitle, "Decompress Test. ");
       
    24 _LIT(KFileOutputPath, "c:\\test\\decompresstest\\");
       
    25 _LIT(KFileInputPath, "c:\\test\\decompresstest\\testfiles\\");
       
    26 
       
    27 RTest test(_L("decompresstest.exe"));
       
    28 
       
    29 /* Test macro and function */
       
    30 void Check(TInt aValue, TInt aExpected, TInt aLine)
       
    31 	{
       
    32     if (aValue != aExpected)
       
    33     	{
       
    34         test.Printf(_L("*** Expected error: %d, got: %d\r\n"), aExpected, aValue);
       
    35         test.operator()(EFalse, aLine);
       
    36         }
       
    37     }
       
    38 #define test2(a, b) Check(a, b, __LINE__)
       
    39 
       
    40 
       
    41 void DecompressZipL(RFile &aInputFile, RFile &aOutputFile)
       
    42 	{
       
    43 	CEZFileBufferManager *bufferManager = CEZFileBufferManager::NewLC(aInputFile, aOutputFile);	
       
    44 	CEZDecompressor *decompressor = CEZDecompressor::NewLC(*bufferManager, MAX_WBITS);
       
    45 	while(decompressor->InflateL())
       
    46 		{
       
    47 		// Do nothing here
       
    48 		}
       
    49 	
       
    50 	CleanupStack::PopAndDestroy(3);
       
    51 	}
       
    52 
       
    53 void DecompressGZipL(RFs &aFs, RFile &aOutputFile, const TFileName &aFileInputPath, const TFileName &aFileName)
       
    54 	{
       
    55 	aFs.SetSessionPath(aFileInputPath);
       
    56 	CEZGZipToFile *decompressor = CEZGZipToFile::NewLC(aFs, aFileName, aOutputFile);
       
    57 	while(decompressor->InflateL())
       
    58 		{
       
    59 		// Do nothing here
       
    60 		}
       
    61 	
       
    62 	CleanupStack::PopAndDestroy(1);
       
    63 	}
       
    64 
       
    65 void TestDecompressL(RFs &aFs, const TFileName &aFileInputPath, const TFileName &aFileName)
       
    66 	{
       
    67 	test.Printf(_L("\nUsing file %S. "), &aFileName);
       
    68 	
       
    69 	// Create temporary output file and open it for writing
       
    70 	TFileName outputFileName;
       
    71 	RFile outputFile;
       
    72 	
       
    73 	aFs.MkDir(KFileOutputPath);
       
    74 	outputFile.Temp(aFs, KFileOutputPath, outputFileName, EFileWrite);
       
    75 	CleanupClosePushL(outputFile);
       
    76 	
       
    77 	// Decompress file based on whether its a gzip file or zip archive
       
    78 	aFs.SetSessionPath(aFileInputPath);
       
    79 	EZGZipFile gzipFile;	
       
    80 	if(gzipFile.IsGzipFileL(aFs, aFileName))
       
    81 		{
       
    82 		// Check for memory leaks
       
    83 		__UHEAP_MARK;
       
    84 		TRAPD(err, DecompressGZipL(aFs, outputFile, aFileInputPath, aFileName));
       
    85 		__UHEAP_MARKEND;
       
    86 		switch(err)
       
    87 			{
       
    88 			case KEZlibErrStream:
       
    89 			case KEZlibErrData:
       
    90 			case KEZlibErrBuf:
       
    91 			case KEZlibErrVersion:
       
    92 			case KEZlibErrUnexpected:
       
    93 			case KEZlibErrDeflateTerminated:
       
    94 			case KEZlibErrInflateTerminated:
       
    95 			case KEZlibErrInflateDictionary:
       
    96 			case KEZlibErrNotGZipFile:
       
    97 			case KEZlibErrInvalidCompression:
       
    98 			case KEZlibErrBadGZipHeader:
       
    99 			case KEZlibErrBadGZipTrailer:
       
   100 			case KEZlibErrBadGZipCrc: 
       
   101 				break;
       
   102 			default: 
       
   103 				if(err > KErrNone || err < KErrNoSecureTime)
       
   104 					{
       
   105 					test.Printf(_L("FAILED! error = %d"), err);
       
   106 					test2(err, KErrNone);	
       
   107 					}
       
   108 			}
       
   109 		}
       
   110 	else
       
   111 		{
       
   112 		// Open the input file for reading
       
   113         RFile inputFile;
       
   114         
       
   115         aFs.SetSessionPath(aFileInputPath);
       
   116         User::LeaveIfError(inputFile.Open(aFs, aFileName, EFileRead));
       
   117         CleanupClosePushL(inputFile);
       
   118 		
       
   119 		__UHEAP_MARK;
       
   120 		TRAPD(err, DecompressZipL(inputFile, outputFile));
       
   121 		__UHEAP_MARKEND;
       
   122 		switch(err)
       
   123 			{
       
   124 			case KEZlibErrStream:
       
   125 			case KEZlibErrData:
       
   126 			case KEZlibErrBuf:
       
   127 			case KEZlibErrVersion:
       
   128 			case KEZlibErrUnexpected:
       
   129 			case KEZlibErrDeflateTerminated:
       
   130 			case KEZlibErrInflateTerminated:
       
   131 			case KEZlibErrInflateDictionary: 
       
   132 				break;
       
   133 			default: 
       
   134 				if(err > KErrNone || err < KErrNoSecureTime)
       
   135 					{
       
   136 					test.Printf(_L("FAILED! error = %d"), err);
       
   137 					test2(err, KErrNone);
       
   138 					}
       
   139 			}
       
   140 			
       
   141 			CleanupStack::PopAndDestroy(1);
       
   142 		}
       
   143 	CleanupStack::PopAndDestroy(1);
       
   144 	
       
   145 	// Delete temporary output file
       
   146 	aFs.SetSessionPath(KFileOutputPath);
       
   147 	aFs.Delete(outputFileName);
       
   148 	}
       
   149 
       
   150 /**
       
   151 @SYMTestCaseID          SYSLIB-EZLIB2-CT-4315
       
   152 @SYMTestCaseDesc	    To test for invalid return codes, panics and memory leaks when 
       
   153                         decompressing invalid Zip archive and GZip files.
       
   154 @SYMTestPriority 	    Medium
       
   155 @SYMTestActions  	    1.	Create an instance of CDir and get the number of files and 
       
   156                             directories in the test file directory.
       
   157                         2.	Loop through the list of files and directories, held by CDir. 
       
   158                             a.	If the current entry is a directory navigate to it and go 
       
   159                                 back to step 1.
       
   160                             b.	If the current entry is a file, open a temporary output 
       
   161                                 file for writing. Then check if we have a GZip or Zip archive.
       
   162                             c.	If we have a GZip file:
       
   163                                 i.	Call __UHEAP_MARK
       
   164                                 ii.	Create a CEZGZipToFile passing it the open output file 
       
   165                                     and the name of the input file.
       
   166                                 iii.Call InflateL as many times as needed to decompress 
       
   167                                     the input file.
       
   168                                 iv.	Call __UHEAP_MARKEND
       
   169                                 v.	Check any leave errors against a list of expected 
       
   170                                     errors. If an unexpected leave value is returned, panic.
       
   171                             d.	If we have a Zip archive
       
   172                                 i.	Open the input file for reading.
       
   173                                 ii.	Call __UHEAP_MARK.
       
   174                                 iii.Create a CEZFileBufferManager passing it the open 
       
   175                                     input and output files.
       
   176                                 iv.	Create a CEZDecompressor passing it the 
       
   177                                     CEZFileBufferManager and windowBits of 15.
       
   178                                 v.	Call InflateL as many times as needed to decompress 
       
   179                                     the input file.
       
   180                                 vi.	Call __UHEAP_MARKEND
       
   181                                 vii.Check any leave errors against a list of expected 
       
   182                                     errors. If an unexpected leave value is returned, panic.
       
   183                             e.	Delete the temporary output file.
       
   184 @SYMTestExpectedResults There will be no memory leaks or panics and all return codes will 
       
   185                         be the expected ones.
       
   186 @SYMDEF                 REQ8024
       
   187 */
       
   188 void TestL(RFs &aFs, TFileName aFileInputPath)
       
   189 	{
       
   190 	test.Printf(_L("\nIn directory %S. "), &aFileInputPath);
       
   191 	test.Next(_L(" @SYMTestCaseID:SYSLIB-EZLIB2-CT-4315 "));
       
   192 	
       
   193 	CDir *inputFiles;
       
   194 	aFs.GetDir(aFileInputPath, KEntryAttNormal|KEntryAttDir, ESortByName | EDirsFirst, inputFiles);
       
   195 	CleanupStack::PushL(inputFiles);
       
   196 	
       
   197 	TInt numInputFiles = inputFiles->Count();
       
   198 	for(TInt i = 0; i < numInputFiles; i++)
       
   199 		{
       
   200 		TEntry entry = (*inputFiles)[i];
       
   201 		
       
   202 		// If we have a directory we need to try decompressing the files contained in it
       
   203 		if(entry.IsDir())
       
   204 			{
       
   205 			TFileName currentDir = aFileInputPath;
       
   206 			currentDir.Append(entry.iName);
       
   207 			currentDir.Append(_L("\\"));
       
   208 			
       
   209 			TestL(aFs, currentDir);
       
   210 			}
       
   211 		else
       
   212 			{
       
   213 			TRAPD(err, TestDecompressL(aFs, aFileInputPath, entry.iName));
       
   214 			test2(err, KErrNone);
       
   215 			}
       
   216 		}
       
   217 	CleanupStack::PopAndDestroy(1);
       
   218 	}
       
   219 
       
   220 void RunTestL()
       
   221 	{
       
   222 	RFs fs;
       
   223 	User::LeaveIfError(fs.Connect());
       
   224 	CleanupClosePushL(fs);
       
   225 	
       
   226 	TFileName filePath(KFileInputPath);
       
   227 	TestL(fs, filePath);
       
   228 
       
   229 	CleanupStack::PopAndDestroy(1);
       
   230 	}
       
   231 
       
   232 TInt E32Main()
       
   233 	{
       
   234 	__UHEAP_MARK;
       
   235 
       
   236 	test.Printf(_L("\n"));
       
   237 	test.Title();
       
   238 	test.Start(KTestTitle);
       
   239 
       
   240 	CTrapCleanup* cleanup = CTrapCleanup::New();
       
   241 
       
   242 	TRAPD(err, RunTestL());
       
   243 	test2(err, KErrNone);
       
   244 	
       
   245 	test.End();
       
   246 	test.Close();
       
   247 	delete cleanup;
       
   248 
       
   249 	__UHEAP_MARKEND;
       
   250 	return KErrNone;
       
   251 	}