sl@0: // Copyright (c) 2003-2009 Nokia Corporation and/or its subsidiary(-ies).
sl@0: // All rights reserved.
sl@0: // This component and the accompanying materials are made available
sl@0: // under the terms of "Eclipse Public License v1.0"
sl@0: // which accompanies this distribution, and is available
sl@0: // at the URL "http://www.eclipse.org/legal/epl-v10.html".
sl@0: //
sl@0: // Initial Contributors:
sl@0: // Nokia Corporation - initial contribution.
sl@0: //
sl@0: // Contributors:
sl@0: //
sl@0: // Description:
sl@0: //
sl@0: 
sl@0: #include <e32cmn.h>
sl@0: #include <f32file.h>
sl@0: #include <e32test.h>
sl@0: #include <ezgzip.h>
sl@0: #include <ezfilebuffer.h>
sl@0: #include <ezdecompressor.h>
sl@0: 
sl@0: _LIT(KTestTitle, "Decompress Test. ");
sl@0: _LIT(KFileOutputPath, "c:\\test\\decompresstest\\");
sl@0: _LIT(KFileInputPath, "c:\\test\\decompresstest\\testfiles\\");
sl@0: 
sl@0: RTest test(_L("decompresstest.exe"));
sl@0: 
sl@0: /* Test macro and function */
sl@0: void Check(TInt aValue, TInt aExpected, TInt aLine)
sl@0: 	{
sl@0:     if (aValue != aExpected)
sl@0:     	{
sl@0:         test.Printf(_L("*** Expected error: %d, got: %d\r\n"), aExpected, aValue);
sl@0:         test.operator()(EFalse, aLine);
sl@0:         }
sl@0:     }
sl@0: #define test2(a, b) Check(a, b, __LINE__)
sl@0: 
sl@0: 
sl@0: void DecompressZipL(RFile &aInputFile, RFile &aOutputFile)
sl@0: 	{
sl@0: 	CEZFileBufferManager *bufferManager = CEZFileBufferManager::NewLC(aInputFile, aOutputFile);	
sl@0: 	CEZDecompressor *decompressor = CEZDecompressor::NewLC(*bufferManager, MAX_WBITS);
sl@0: 	while(decompressor->InflateL())
sl@0: 		{
sl@0: 		// Do nothing here
sl@0: 		}
sl@0: 	
sl@0: 	CleanupStack::PopAndDestroy(3);
sl@0: 	}
sl@0: 
sl@0: void DecompressGZipL(RFs &aFs, RFile &aOutputFile, const TFileName &aFileInputPath, const TFileName &aFileName)
sl@0: 	{
sl@0: 	aFs.SetSessionPath(aFileInputPath);
sl@0: 	CEZGZipToFile *decompressor = CEZGZipToFile::NewLC(aFs, aFileName, aOutputFile);
sl@0: 	while(decompressor->InflateL())
sl@0: 		{
sl@0: 		// Do nothing here
sl@0: 		}
sl@0: 	
sl@0: 	CleanupStack::PopAndDestroy(1);
sl@0: 	}
sl@0: 
sl@0: void TestDecompressL(RFs &aFs, const TFileName &aFileInputPath, const TFileName &aFileName)
sl@0: 	{
sl@0: 	test.Printf(_L("\nUsing file %S. "), &aFileName);
sl@0: 	
sl@0: 	// Create temporary output file and open it for writing
sl@0: 	TFileName outputFileName;
sl@0: 	RFile outputFile;
sl@0: 	
sl@0: 	aFs.MkDir(KFileOutputPath);
sl@0: 	outputFile.Temp(aFs, KFileOutputPath, outputFileName, EFileWrite);
sl@0: 	CleanupClosePushL(outputFile);
sl@0: 	
sl@0: 	// Decompress file based on whether its a gzip file or zip archive
sl@0: 	aFs.SetSessionPath(aFileInputPath);
sl@0: 	EZGZipFile gzipFile;	
sl@0: 	if(gzipFile.IsGzipFileL(aFs, aFileName))
sl@0: 		{
sl@0: 		// Check for memory leaks
sl@0: 		__UHEAP_MARK;
sl@0: 		TRAPD(err, DecompressGZipL(aFs, outputFile, aFileInputPath, aFileName));
sl@0: 		__UHEAP_MARKEND;
sl@0: 		switch(err)
sl@0: 			{
sl@0: 			case KEZlibErrStream:
sl@0: 			case KEZlibErrData:
sl@0: 			case KEZlibErrBuf:
sl@0: 			case KEZlibErrVersion:
sl@0: 			case KEZlibErrUnexpected:
sl@0: 			case KEZlibErrDeflateTerminated:
sl@0: 			case KEZlibErrInflateTerminated:
sl@0: 			case KEZlibErrInflateDictionary:
sl@0: 			case KEZlibErrNotGZipFile:
sl@0: 			case KEZlibErrInvalidCompression:
sl@0: 			case KEZlibErrBadGZipHeader:
sl@0: 			case KEZlibErrBadGZipTrailer:
sl@0: 			case KEZlibErrBadGZipCrc: 
sl@0: 				break;
sl@0: 			default: 
sl@0: 				if(err > KErrNone || err < KErrNoSecureTime)
sl@0: 					{
sl@0: 					test.Printf(_L("FAILED! error = %d"), err);
sl@0: 					test2(err, KErrNone);	
sl@0: 					}
sl@0: 			}
sl@0: 		}
sl@0: 	else
sl@0: 		{
sl@0: 		// Open the input file for reading
sl@0:         RFile inputFile;
sl@0:         
sl@0:         aFs.SetSessionPath(aFileInputPath);
sl@0:         User::LeaveIfError(inputFile.Open(aFs, aFileName, EFileRead));
sl@0:         CleanupClosePushL(inputFile);
sl@0: 		
sl@0: 		__UHEAP_MARK;
sl@0: 		TRAPD(err, DecompressZipL(inputFile, outputFile));
sl@0: 		__UHEAP_MARKEND;
sl@0: 		switch(err)
sl@0: 			{
sl@0: 			case KEZlibErrStream:
sl@0: 			case KEZlibErrData:
sl@0: 			case KEZlibErrBuf:
sl@0: 			case KEZlibErrVersion:
sl@0: 			case KEZlibErrUnexpected:
sl@0: 			case KEZlibErrDeflateTerminated:
sl@0: 			case KEZlibErrInflateTerminated:
sl@0: 			case KEZlibErrInflateDictionary: 
sl@0: 				break;
sl@0: 			default: 
sl@0: 				if(err > KErrNone || err < KErrNoSecureTime)
sl@0: 					{
sl@0: 					test.Printf(_L("FAILED! error = %d"), err);
sl@0: 					test2(err, KErrNone);
sl@0: 					}
sl@0: 			}
sl@0: 			
sl@0: 			CleanupStack::PopAndDestroy(1);
sl@0: 		}
sl@0: 	CleanupStack::PopAndDestroy(1);
sl@0: 	
sl@0: 	// Delete temporary output file
sl@0: 	aFs.SetSessionPath(KFileOutputPath);
sl@0: 	aFs.Delete(outputFileName);
sl@0: 	}
sl@0: 
sl@0: /**
sl@0: @SYMTestCaseID          SYSLIB-EZLIB2-CT-4315
sl@0: @SYMTestCaseDesc	    To test for invalid return codes, panics and memory leaks when 
sl@0:                         decompressing invalid Zip archive and GZip files.
sl@0: @SYMTestPriority 	    Medium
sl@0: @SYMTestActions  	    1.	Create an instance of CDir and get the number of files and 
sl@0:                             directories in the test file directory.
sl@0:                         2.	Loop through the list of files and directories, held by CDir. 
sl@0:                             a.	If the current entry is a directory navigate to it and go 
sl@0:                                 back to step 1.
sl@0:                             b.	If the current entry is a file, open a temporary output 
sl@0:                                 file for writing. Then check if we have a GZip or Zip archive.
sl@0:                             c.	If we have a GZip file:
sl@0:                                 i.	Call __UHEAP_MARK
sl@0:                                 ii.	Create a CEZGZipToFile passing it the open output file 
sl@0:                                     and the name of the input file.
sl@0:                                 iii.Call InflateL as many times as needed to decompress 
sl@0:                                     the input file.
sl@0:                                 iv.	Call __UHEAP_MARKEND
sl@0:                                 v.	Check any leave errors against a list of expected 
sl@0:                                     errors. If an unexpected leave value is returned, panic.
sl@0:                             d.	If we have a Zip archive
sl@0:                                 i.	Open the input file for reading.
sl@0:                                 ii.	Call __UHEAP_MARK.
sl@0:                                 iii.Create a CEZFileBufferManager passing it the open 
sl@0:                                     input and output files.
sl@0:                                 iv.	Create a CEZDecompressor passing it the 
sl@0:                                     CEZFileBufferManager and windowBits of 15.
sl@0:                                 v.	Call InflateL as many times as needed to decompress 
sl@0:                                     the input file.
sl@0:                                 vi.	Call __UHEAP_MARKEND
sl@0:                                 vii.Check any leave errors against a list of expected 
sl@0:                                     errors. If an unexpected leave value is returned, panic.
sl@0:                             e.	Delete the temporary output file.
sl@0: @SYMTestExpectedResults There will be no memory leaks or panics and all return codes will 
sl@0:                         be the expected ones.
sl@0: @SYMDEF                 REQ8024
sl@0: */
sl@0: void TestL(RFs &aFs, TFileName aFileInputPath)
sl@0: 	{
sl@0: 	test.Printf(_L("\nIn directory %S. "), &aFileInputPath);
sl@0: 	test.Next(_L(" @SYMTestCaseID:SYSLIB-EZLIB2-CT-4315 "));
sl@0: 	
sl@0: 	CDir *inputFiles;
sl@0: 	aFs.GetDir(aFileInputPath, KEntryAttNormal|KEntryAttDir, ESortByName | EDirsFirst, inputFiles);
sl@0: 	CleanupStack::PushL(inputFiles);
sl@0: 	
sl@0: 	TInt numInputFiles = inputFiles->Count();
sl@0: 	for(TInt i = 0; i < numInputFiles; i++)
sl@0: 		{
sl@0: 		TEntry entry = (*inputFiles)[i];
sl@0: 		
sl@0: 		// If we have a directory we need to try decompressing the files contained in it
sl@0: 		if(entry.IsDir())
sl@0: 			{
sl@0: 			TFileName currentDir = aFileInputPath;
sl@0: 			currentDir.Append(entry.iName);
sl@0: 			currentDir.Append(_L("\\"));
sl@0: 			
sl@0: 			TestL(aFs, currentDir);
sl@0: 			}
sl@0: 		else
sl@0: 			{
sl@0: 			TRAPD(err, TestDecompressL(aFs, aFileInputPath, entry.iName));
sl@0: 			test2(err, KErrNone);
sl@0: 			}
sl@0: 		}
sl@0: 	CleanupStack::PopAndDestroy(1);
sl@0: 	}
sl@0: 
sl@0: void RunTestL()
sl@0: 	{
sl@0: 	RFs fs;
sl@0: 	User::LeaveIfError(fs.Connect());
sl@0: 	CleanupClosePushL(fs);
sl@0: 	
sl@0: 	TFileName filePath(KFileInputPath);
sl@0: 	TestL(fs, filePath);
sl@0: 
sl@0: 	CleanupStack::PopAndDestroy(1);
sl@0: 	}
sl@0: 
sl@0: TInt E32Main()
sl@0: 	{
sl@0: 	__UHEAP_MARK;
sl@0: 
sl@0: 	test.Printf(_L("\n"));
sl@0: 	test.Title();
sl@0: 	test.Start(KTestTitle);
sl@0: 
sl@0: 	CTrapCleanup* cleanup = CTrapCleanup::New();
sl@0: 
sl@0: 	TRAPD(err, RunTestL());
sl@0: 	test2(err, KErrNone);
sl@0: 	
sl@0: 	test.End();
sl@0: 	test.Close();
sl@0: 	delete cleanup;
sl@0: 
sl@0: 	__UHEAP_MARKEND;
sl@0: 	return KErrNone;
sl@0: 	}