1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/os/ossrv/compressionlibs/ziplib/test/rtest/decompresstest/decompresstest.cpp Fri Jun 15 03:10:57 2012 +0200
1.3 @@ -0,0 +1,251 @@
1.4 +// Copyright (c) 2003-2009 Nokia Corporation and/or its subsidiary(-ies).
1.5 +// All rights reserved.
1.6 +// This component and the accompanying materials are made available
1.7 +// under the terms of "Eclipse Public License v1.0"
1.8 +// which accompanies this distribution, and is available
1.9 +// at the URL "http://www.eclipse.org/legal/epl-v10.html".
1.10 +//
1.11 +// Initial Contributors:
1.12 +// Nokia Corporation - initial contribution.
1.13 +//
1.14 +// Contributors:
1.15 +//
1.16 +// Description:
1.17 +//
1.18 +
1.19 +#include <e32cmn.h>
1.20 +#include <f32file.h>
1.21 +#include <e32test.h>
1.22 +#include <ezgzip.h>
1.23 +#include <ezfilebuffer.h>
1.24 +#include <ezdecompressor.h>
1.25 +
1.26 +_LIT(KTestTitle, "Decompress Test. ");
1.27 +_LIT(KFileOutputPath, "c:\\test\\decompresstest\\");
1.28 +_LIT(KFileInputPath, "c:\\test\\decompresstest\\testfiles\\");
1.29 +
1.30 +RTest test(_L("decompresstest.exe"));
1.31 +
1.32 +/* Test macro and function */
1.33 +void Check(TInt aValue, TInt aExpected, TInt aLine)
1.34 + {
1.35 + if (aValue != aExpected)
1.36 + {
1.37 + test.Printf(_L("*** Expected error: %d, got: %d\r\n"), aExpected, aValue);
1.38 + test.operator()(EFalse, aLine);
1.39 + }
1.40 + }
1.41 +#define test2(a, b) Check(a, b, __LINE__)
1.42 +
1.43 +
1.44 +void DecompressZipL(RFile &aInputFile, RFile &aOutputFile)
1.45 + {
1.46 + CEZFileBufferManager *bufferManager = CEZFileBufferManager::NewLC(aInputFile, aOutputFile);
1.47 + CEZDecompressor *decompressor = CEZDecompressor::NewLC(*bufferManager, MAX_WBITS);
1.48 + while(decompressor->InflateL())
1.49 + {
1.50 + // Do nothing here
1.51 + }
1.52 +
1.53 + CleanupStack::PopAndDestroy(3);
1.54 + }
1.55 +
1.56 +void DecompressGZipL(RFs &aFs, RFile &aOutputFile, const TFileName &aFileInputPath, const TFileName &aFileName)
1.57 + {
1.58 + aFs.SetSessionPath(aFileInputPath);
1.59 + CEZGZipToFile *decompressor = CEZGZipToFile::NewLC(aFs, aFileName, aOutputFile);
1.60 + while(decompressor->InflateL())
1.61 + {
1.62 + // Do nothing here
1.63 + }
1.64 +
1.65 + CleanupStack::PopAndDestroy(1);
1.66 + }
1.67 +
1.68 +void TestDecompressL(RFs &aFs, const TFileName &aFileInputPath, const TFileName &aFileName)
1.69 + {
1.70 + test.Printf(_L("\nUsing file %S. "), &aFileName);
1.71 +
1.72 + // Create temporary output file and open it for writing
1.73 + TFileName outputFileName;
1.74 + RFile outputFile;
1.75 +
1.76 + aFs.MkDir(KFileOutputPath);
1.77 + outputFile.Temp(aFs, KFileOutputPath, outputFileName, EFileWrite);
1.78 + CleanupClosePushL(outputFile);
1.79 +
1.80 + // Decompress file based on whether its a gzip file or zip archive
1.81 + aFs.SetSessionPath(aFileInputPath);
1.82 + EZGZipFile gzipFile;
1.83 + if(gzipFile.IsGzipFileL(aFs, aFileName))
1.84 + {
1.85 + // Check for memory leaks
1.86 + __UHEAP_MARK;
1.87 + TRAPD(err, DecompressGZipL(aFs, outputFile, aFileInputPath, aFileName));
1.88 + __UHEAP_MARKEND;
1.89 + switch(err)
1.90 + {
1.91 + case KEZlibErrStream:
1.92 + case KEZlibErrData:
1.93 + case KEZlibErrBuf:
1.94 + case KEZlibErrVersion:
1.95 + case KEZlibErrUnexpected:
1.96 + case KEZlibErrDeflateTerminated:
1.97 + case KEZlibErrInflateTerminated:
1.98 + case KEZlibErrInflateDictionary:
1.99 + case KEZlibErrNotGZipFile:
1.100 + case KEZlibErrInvalidCompression:
1.101 + case KEZlibErrBadGZipHeader:
1.102 + case KEZlibErrBadGZipTrailer:
1.103 + case KEZlibErrBadGZipCrc:
1.104 + break;
1.105 + default:
1.106 + if(err > KErrNone || err < KErrNoSecureTime)
1.107 + {
1.108 + test.Printf(_L("FAILED! error = %d"), err);
1.109 + test2(err, KErrNone);
1.110 + }
1.111 + }
1.112 + }
1.113 + else
1.114 + {
1.115 + // Open the input file for reading
1.116 + RFile inputFile;
1.117 +
1.118 + aFs.SetSessionPath(aFileInputPath);
1.119 + User::LeaveIfError(inputFile.Open(aFs, aFileName, EFileRead));
1.120 + CleanupClosePushL(inputFile);
1.121 +
1.122 + __UHEAP_MARK;
1.123 + TRAPD(err, DecompressZipL(inputFile, outputFile));
1.124 + __UHEAP_MARKEND;
1.125 + switch(err)
1.126 + {
1.127 + case KEZlibErrStream:
1.128 + case KEZlibErrData:
1.129 + case KEZlibErrBuf:
1.130 + case KEZlibErrVersion:
1.131 + case KEZlibErrUnexpected:
1.132 + case KEZlibErrDeflateTerminated:
1.133 + case KEZlibErrInflateTerminated:
1.134 + case KEZlibErrInflateDictionary:
1.135 + break;
1.136 + default:
1.137 + if(err > KErrNone || err < KErrNoSecureTime)
1.138 + {
1.139 + test.Printf(_L("FAILED! error = %d"), err);
1.140 + test2(err, KErrNone);
1.141 + }
1.142 + }
1.143 +
1.144 + CleanupStack::PopAndDestroy(1);
1.145 + }
1.146 + CleanupStack::PopAndDestroy(1);
1.147 +
1.148 + // Delete temporary output file
1.149 + aFs.SetSessionPath(KFileOutputPath);
1.150 + aFs.Delete(outputFileName);
1.151 + }
1.152 +
1.153 +/**
1.154 +@SYMTestCaseID SYSLIB-EZLIB2-CT-4315
1.155 +@SYMTestCaseDesc To test for invalid return codes, panics and memory leaks when
1.156 + decompressing invalid Zip archive and GZip files.
1.157 +@SYMTestPriority Medium
1.158 +@SYMTestActions 1. Create an instance of CDir and get the number of files and
1.159 + directories in the test file directory.
1.160 + 2. Loop through the list of files and directories, held by CDir.
1.161 + a. If the current entry is a directory navigate to it and go
1.162 + back to step 1.
1.163 + b. If the current entry is a file, open a temporary output
1.164 + file for writing. Then check if we have a GZip or Zip archive.
1.165 + c. If we have a GZip file:
1.166 + i. Call __UHEAP_MARK
1.167 + ii. Create a CEZGZipToFile passing it the open output file
1.168 + and the name of the input file.
1.169 + iii.Call InflateL as many times as needed to decompress
1.170 + the input file.
1.171 + iv. Call __UHEAP_MARKEND
1.172 + v. Check any leave errors against a list of expected
1.173 + errors. If an unexpected leave value is returned, panic.
1.174 + d. If we have a Zip archive
1.175 + i. Open the input file for reading.
1.176 + ii. Call __UHEAP_MARK.
1.177 + iii.Create a CEZFileBufferManager passing it the open
1.178 + input and output files.
1.179 + iv. Create a CEZDecompressor passing it the
1.180 + CEZFileBufferManager and windowBits of 15.
1.181 + v. Call InflateL as many times as needed to decompress
1.182 + the input file.
1.183 + vi. Call __UHEAP_MARKEND
1.184 + vii.Check any leave errors against a list of expected
1.185 + errors. If an unexpected leave value is returned, panic.
1.186 + e. Delete the temporary output file.
1.187 +@SYMTestExpectedResults There will be no memory leaks or panics and all return codes will
1.188 + be the expected ones.
1.189 +@SYMDEF REQ8024
1.190 +*/
1.191 +void TestL(RFs &aFs, TFileName aFileInputPath)
1.192 + {
1.193 + test.Printf(_L("\nIn directory %S. "), &aFileInputPath);
1.194 + test.Next(_L(" @SYMTestCaseID:SYSLIB-EZLIB2-CT-4315 "));
1.195 +
1.196 + CDir *inputFiles;
1.197 + aFs.GetDir(aFileInputPath, KEntryAttNormal|KEntryAttDir, ESortByName | EDirsFirst, inputFiles);
1.198 + CleanupStack::PushL(inputFiles);
1.199 +
1.200 + TInt numInputFiles = inputFiles->Count();
1.201 + for(TInt i = 0; i < numInputFiles; i++)
1.202 + {
1.203 + TEntry entry = (*inputFiles)[i];
1.204 +
1.205 + // If we have a directory we need to try decompressing the files contained in it
1.206 + if(entry.IsDir())
1.207 + {
1.208 + TFileName currentDir = aFileInputPath;
1.209 + currentDir.Append(entry.iName);
1.210 + currentDir.Append(_L("\\"));
1.211 +
1.212 + TestL(aFs, currentDir);
1.213 + }
1.214 + else
1.215 + {
1.216 + TRAPD(err, TestDecompressL(aFs, aFileInputPath, entry.iName));
1.217 + test2(err, KErrNone);
1.218 + }
1.219 + }
1.220 + CleanupStack::PopAndDestroy(1);
1.221 + }
1.222 +
1.223 +void RunTestL()
1.224 + {
1.225 + RFs fs;
1.226 + User::LeaveIfError(fs.Connect());
1.227 + CleanupClosePushL(fs);
1.228 +
1.229 + TFileName filePath(KFileInputPath);
1.230 + TestL(fs, filePath);
1.231 +
1.232 + CleanupStack::PopAndDestroy(1);
1.233 + }
1.234 +
1.235 +TInt E32Main()
1.236 + {
1.237 + __UHEAP_MARK;
1.238 +
1.239 + test.Printf(_L("\n"));
1.240 + test.Title();
1.241 + test.Start(KTestTitle);
1.242 +
1.243 + CTrapCleanup* cleanup = CTrapCleanup::New();
1.244 +
1.245 + TRAPD(err, RunTestL());
1.246 + test2(err, KErrNone);
1.247 +
1.248 + test.End();
1.249 + test.Close();
1.250 + delete cleanup;
1.251 +
1.252 + __UHEAP_MARKEND;
1.253 + return KErrNone;
1.254 + }