1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/os/ossrv/compressionlibs/ziplib/test/rtest/ezdefect/ezdefect.cpp Fri Jun 15 03:10:57 2012 +0200
1.3 @@ -0,0 +1,303 @@
1.4 +// Copyright (c) 2005-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 +
1.20 +#include <e32test.h>
1.21 +#include <zipfile.h>
1.22 +
1.23 +LOCAL_D RTest test(_L("ezdefect.exe"));
1.24 +LOCAL_D RFs TheFs;
1.25 +LOCAL_D CTrapCleanup* TheTrapCleanup = NULL;
1.26 +
1.27 +#if !defined(__WINS__)
1.28 +_LIT(KPath, "Z:\\test\\zip\\");
1.29 +#else
1.30 +_LIT(KPath, "C:\\test\\zip\\");
1.31 +#endif
1.32 +
1.33 +
1.34 +
1.35 +void ExtractFileL(const CZipFileMember* aMember, CZipFile* aZip)
1.36 + {
1.37 + HBufC* name = aMember->Name()->AllocLC();
1.38 + for (TInt i = 0; i<name->Length(); i++)
1.39 + {
1.40 + if ((*name)[i] == '/')
1.41 + {
1.42 + name->Des()[i] = '\\';
1.43 + }
1.44 + }
1.45 +
1.46 + RZipFileMemberReaderStream* fileStream;
1.47 + User::LeaveIfError(aZip->GetInputStreamL(aMember, fileStream));
1.48 + CleanupStack::PushL(fileStream);
1.49 +
1.50 + TUint32 size = aMember->UncompressedSize();
1.51 + HBufC8* bytes = HBufC8::New(size);
1.52 + CleanupStack::PushL(bytes);
1.53 + TPtr8 ptr = bytes->Des();
1.54 + User::LeaveIfError(fileStream->Read(ptr,size));
1.55 + CleanupStack::PopAndDestroy(3,name); // bytes, fileStream and name
1.56 + }
1.57 +
1.58 +// Extract all members from the zip.
1.59 +// These files are non compressed, i.e. EStored.
1.60 +void ExtractMembersL(CZipFile* aZip)
1.61 + {
1.62 + test.Next(_L("DEF057916: Getting Corrupt file error when reading wav-file from Zip - PART 2"));
1.63 +
1.64 + CZipFileMemberIterator* members = aZip->GetMembersL();
1.65 + CleanupStack::PushL(members);
1.66 +
1.67 + CZipFileMember* member = members->NextL();
1.68 + while (member)
1.69 + {
1.70 + CleanupStack::PushL(member);
1.71 + ExtractFileL(member, aZip);
1.72 + CleanupStack::PopAndDestroy(member);
1.73 + member = members->NextL();
1.74 + }
1.75 +
1.76 + CleanupStack::PopAndDestroy(members);
1.77 + }
1.78 +
1.79 +// Extract the specified files from the zip.
1.80 +// These files are non compressed, i.e. EStored.
1.81 +void DEF057916L()
1.82 + {
1.83 + test.Next(_L("DEF057916: Getting Corrupt file error when reading wav-file from Zip - PART 1"));
1.84 +
1.85 + User::LeaveIfError(TheFs.Connect()); //Connect to file session
1.86 + User::LeaveIfError(TheFs.SetSessionPath(KPath)); //Set Session Path to direcrt containing test zip files
1.87 + CleanupClosePushL(TheFs);
1.88 +
1.89 + const TUint KDataChunk = 2048;
1.90 + _LIT(tmpFileName, "\\Test\\Zip\\compression_estored.zip");
1.91 + _LIT(testFile1, "zip\\test.wav");
1.92 + _LIT(testFile2, "zip\\rfc2459.zip");
1.93 + _LIT(testFile3, "zip\\META-INF\\MANIFEST.MF");
1.94 +
1.95 + CZipFile* iZipFile = CZipFile::NewL(TheFs, tmpFileName);
1.96 + CleanupStack::PushL(iZipFile);
1.97 +
1.98 + TInt err = KErrNone;
1.99 +
1.100 + CZipFileMember* zipMember1 = iZipFile->CaseSensitiveOrCaseInsensitiveMemberL(testFile1);
1.101 + if(zipMember1)
1.102 + {
1.103 + CleanupStack::PushL(zipMember1);
1.104 +
1.105 + RZipFileMemberReaderStream* iZipStream;
1.106 + iZipFile->GetInputStreamL(zipMember1, iZipStream);
1.107 + CleanupStack::PushL(iZipStream);
1.108 +
1.109 + HBufC8* data = HBufC8::NewLC(KDataChunk);
1.110 + TPtr8 ptr = data->Des();
1.111 + err = KErrNone;
1.112 +
1.113 + err = iZipStream->Read(ptr,KDataChunk);
1.114 + test (err == KErrNone);
1.115 +
1.116 + err = iZipStream->Read(ptr,KDataChunk);
1.117 + test (err == KErrEof);
1.118 +
1.119 + CleanupStack::PopAndDestroy(data);
1.120 + CleanupStack::PopAndDestroy(iZipStream);
1.121 + CleanupStack::PopAndDestroy(zipMember1);
1.122 + }
1.123 +
1.124 +
1.125 + CZipFileMember* zipMember2 = iZipFile->CaseSensitiveOrCaseInsensitiveMemberL(testFile2);
1.126 + if(zipMember2)
1.127 + {
1.128 + CleanupStack::PushL(zipMember2);
1.129 +
1.130 + RZipFileMemberReaderStream* iZipStream;
1.131 + iZipFile->GetInputStreamL(zipMember2, iZipStream);
1.132 + CleanupStack::PushL(iZipStream);
1.133 +
1.134 + HBufC8* data = HBufC8::NewLC(KDataChunk*65);
1.135 + TPtr8 ptr = data->Des();
1.136 + err = KErrNone;
1.137 +
1.138 + err = iZipStream->Read(ptr,KDataChunk*65);
1.139 + test (err == KErrNone); // file is very very large
1.140 +
1.141 + err = iZipStream->Read(ptr,KDataChunk);
1.142 + test (err == KErrEof);
1.143 +
1.144 + CleanupStack::PopAndDestroy(data);
1.145 + CleanupStack::PopAndDestroy(iZipStream);
1.146 + CleanupStack::PopAndDestroy(zipMember2);
1.147 + }
1.148 +
1.149 +
1.150 + CZipFileMember* zipMember3 = iZipFile->CaseSensitiveOrCaseInsensitiveMemberL(testFile3);
1.151 + if(zipMember3)
1.152 + {
1.153 + CleanupStack::PushL(zipMember3);
1.154 +
1.155 + RZipFileMemberReaderStream* iZipStream;
1.156 + iZipFile->GetInputStreamL(zipMember3, iZipStream);
1.157 + CleanupStack::PushL(iZipStream);
1.158 +
1.159 + HBufC8* data = HBufC8::NewLC(KDataChunk);
1.160 + TPtr8 ptr = data->Des();
1.161 + err = KErrNone;
1.162 +
1.163 + err = iZipStream->Read(ptr,KDataChunk);
1.164 + test (err == KErrNone);
1.165 +
1.166 + err = iZipStream->Read(ptr,KDataChunk);
1.167 + test (err == KErrEof);
1.168 +
1.169 + CleanupStack::PopAndDestroy(data);
1.170 + CleanupStack::PopAndDestroy(iZipStream);
1.171 + CleanupStack::PopAndDestroy(zipMember3);
1.172 + }
1.173 +
1.174 + ExtractMembersL (iZipFile);
1.175 +
1.176 + CleanupStack::PopAndDestroy(iZipFile);
1.177 + CleanupStack::PopAndDestroy(&TheFs);
1.178 + }
1.179 +
1.180 +/**
1.181 +@SYMTestCaseID SYSLIB-EZLIB-CT-3464
1.182 +@SYMTestCaseDesc RZipFileMemberReaderStream::Read returns incorrect error code under OOM
1.183 +@SYMTestPriority High
1.184 +@SYMTestActions Tries to read zipfile stream with heap allocation set to fail on all following
1.185 + allocations. Check result returned from Read(TDes16&, TInt) and Read(TDes8&, TInt)
1.186 + is KErrNoMemory. Note that zip file contents must be compressed, otherwise no
1.187 + allocations are performed and Read() function returns KErrNone.
1.188 +@SYMTestExpectedResults Read() function should return KErrNoMemory
1.189 +@SYMDEF DEF103961
1.190 +*/
1.191 +void DEF103961L()
1.192 + {
1.193 + test.Next(_L(" @SYMTestCaseID:SYSLIB-EZLIB-CT-3464 DEF103961: RZipFileMemberReaderStream::Read returns incorrect error code under OOM "));
1.194 +
1.195 + User::LeaveIfError(TheFs.Connect()); //Connect to file session
1.196 + User::LeaveIfError(TheFs.SetSessionPath(KPath)); //Set Session Path to directory containing test zip files
1.197 + CleanupClosePushL(TheFs);
1.198 +
1.199 + const TUint KDataChunk = 512;
1.200 + _LIT(testFile1, "zip_archive2_with_comments.zip");
1.201 + _LIT(internalFile, "doorslam.wav");
1.202 +
1.203 + // open zip file
1.204 + CZipFile* zipFile = CZipFile::NewL(TheFs, testFile1);
1.205 + CleanupStack::PushL(zipFile);
1.206 +
1.207 + // get input stream for a file contained in the zip archive
1.208 + CZipFileMember* zipMember = zipFile->CaseSensitiveOrCaseInsensitiveMemberL(internalFile);
1.209 + test(zipMember!=NULL);
1.210 + CleanupStack::PushL(zipMember);
1.211 + RZipFileMemberReaderStream* zipStream;
1.212 + zipFile->GetInputStreamL(zipMember, zipStream);
1.213 + CleanupStack::PushL(zipStream);
1.214 +
1.215 + // some descriptors to hold stream data...
1.216 + HBufC8* data8 = HBufC8::NewLC(KDataChunk);
1.217 + TPtr8 ptr8 = data8->Des();
1.218 + HBufC16* data16 = HBufC16::NewLC(KDataChunk);
1.219 + TPtr16 ptr16 = data16->Des();
1.220 +
1.221 + // do memory tests for RZipFileMemberReaderStream::Read() and its overloaded brother
1.222 + __UHEAP_SETFAIL(RHeap::EDeterministic,1);
1.223 + TInt error = zipStream->Read(ptr8, KDataChunk);
1.224 + test (error == KErrNoMemory);
1.225 + error = zipStream->Read(ptr16, KDataChunk);
1.226 + test (error == KErrNoMemory);
1.227 + __UHEAP_SETFAIL(RHeap::ENone,0);
1.228 +
1.229 + CleanupStack::PopAndDestroy(data16);
1.230 + CleanupStack::PopAndDestroy(data8);
1.231 + CleanupStack::PopAndDestroy(zipStream);
1.232 + CleanupStack::PopAndDestroy(zipMember);
1.233 + CleanupStack::PopAndDestroy(zipFile);
1.234 + CleanupStack::PopAndDestroy(&TheFs);
1.235 + }
1.236 +
1.237 +/**
1.238 +@SYMTestCaseID SYSLIB-EZLIB-CT-3475
1.239 +@SYMTestCaseDesc RZipFileMemberReaderStream constructor ignores errors under OOM
1.240 +@SYMTestPriority High
1.241 +@SYMTestActions Construct RZipFileMemberReaderStream under OOM conditions. Check the Leave code.
1.242 +@SYMTestExpectedResults Should Leave with KErrNoMemory
1.243 +@SYMDEF DEF105995
1.244 +*/
1.245 +void DEF105995L()
1.246 + {
1.247 + test.Next(_L(" @SYMTestCaseID:SYSLIB-EZLIB-CT-3475 DEF105995: RZipFileMemberReaderStream constructor not checking OOM conditions "));
1.248 +
1.249 + User::LeaveIfError(TheFs.Connect()); //Connect to file session
1.250 + User::LeaveIfError(TheFs.SetSessionPath(KPath)); //Set Session Path to directory containing test zip files
1.251 + CleanupClosePushL(TheFs);
1.252 +
1.253 + _LIT(testFile1, "zip_archive2_with_comments.zip");
1.254 + _LIT(internalFile, "doorslam.wav");
1.255 +
1.256 + // open zip file
1.257 + CZipFile* zipFile = CZipFile::NewL(TheFs, testFile1);
1.258 + CleanupStack::PushL(zipFile);
1.259 +
1.260 + // get input stream for a file contained in the zip archive
1.261 + CZipFileMember* zipMember = zipFile->CaseSensitiveOrCaseInsensitiveMemberL(internalFile);
1.262 + test(zipMember!=NULL);
1.263 + CleanupStack::PushL(zipMember);
1.264 +
1.265 + __UHEAP_SETFAIL(RHeap::EDeterministic,2); // so memory allocation in inflateInit2() fails
1.266 +
1.267 + RZipFileMemberReaderStream* zipStream;
1.268 + TRAPD(err, zipFile->GetInputStreamL(zipMember, zipStream));
1.269 + test(err == KErrNoMemory);
1.270 +
1.271 + __UHEAP_SETFAIL(RHeap::ENone,0);
1.272 +
1.273 + CleanupStack::PopAndDestroy(zipMember);
1.274 + CleanupStack::PopAndDestroy(zipFile);
1.275 + CleanupStack::PopAndDestroy(&TheFs);
1.276 + }
1.277 +
1.278 +void RunTestL()
1.279 + {
1.280 + DEF057916L(); // Getting Corrupt file error when reading wav-file from Zip
1.281 + #if defined(_DEBUG) // OOM tests are only run on Debug builds
1.282 + DEF103961L(); // RZipFileMemberReaderStream::Read returns incorrect error code under OOM
1.283 + DEF105995L(); // inflateInit2 is not checked for errors in RZipFileMemberReaderStream constructor
1.284 + #endif
1.285 + }
1.286 +
1.287 +GLDEF_C TInt E32Main()
1.288 + {
1.289 + __UHEAP_MARK;
1.290 +
1.291 + test.Printf(_L("\n"));
1.292 + test.Title();
1.293 + test.Start( _L("EZlib defect Tests.") );
1.294 +
1.295 + TheTrapCleanup=CTrapCleanup::New();
1.296 +
1.297 + TRAPD(err,RunTestL());
1.298 + test (err==KErrNone);
1.299 +
1.300 + test.End();
1.301 + test.Close();
1.302 + delete TheTrapCleanup;
1.303 +
1.304 + __UHEAP_MARKEND;
1.305 + return KErrNone;
1.306 + }