sl@0: // Copyright (c) 2005-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: sl@0: #include sl@0: #include sl@0: sl@0: LOCAL_D RTest test(_L("ezdefect.exe")); sl@0: LOCAL_D RFs TheFs; sl@0: LOCAL_D CTrapCleanup* TheTrapCleanup = NULL; sl@0: sl@0: #if !defined(__WINS__) sl@0: _LIT(KPath, "Z:\\test\\zip\\"); sl@0: #else sl@0: _LIT(KPath, "C:\\test\\zip\\"); sl@0: #endif sl@0: sl@0: sl@0: sl@0: void ExtractFileL(const CZipFileMember* aMember, CZipFile* aZip) sl@0: { sl@0: HBufC* name = aMember->Name()->AllocLC(); sl@0: for (TInt i = 0; iLength(); i++) sl@0: { sl@0: if ((*name)[i] == '/') sl@0: { sl@0: name->Des()[i] = '\\'; sl@0: } sl@0: } sl@0: sl@0: RZipFileMemberReaderStream* fileStream; sl@0: User::LeaveIfError(aZip->GetInputStreamL(aMember, fileStream)); sl@0: CleanupStack::PushL(fileStream); sl@0: sl@0: TUint32 size = aMember->UncompressedSize(); sl@0: HBufC8* bytes = HBufC8::New(size); sl@0: CleanupStack::PushL(bytes); sl@0: TPtr8 ptr = bytes->Des(); sl@0: User::LeaveIfError(fileStream->Read(ptr,size)); sl@0: CleanupStack::PopAndDestroy(3,name); // bytes, fileStream and name sl@0: } sl@0: sl@0: // Extract all members from the zip. sl@0: // These files are non compressed, i.e. EStored. sl@0: void ExtractMembersL(CZipFile* aZip) sl@0: { sl@0: test.Next(_L("DEF057916: Getting Corrupt file error when reading wav-file from Zip - PART 2")); sl@0: sl@0: CZipFileMemberIterator* members = aZip->GetMembersL(); sl@0: CleanupStack::PushL(members); sl@0: sl@0: CZipFileMember* member = members->NextL(); sl@0: while (member) sl@0: { sl@0: CleanupStack::PushL(member); sl@0: ExtractFileL(member, aZip); sl@0: CleanupStack::PopAndDestroy(member); sl@0: member = members->NextL(); sl@0: } sl@0: sl@0: CleanupStack::PopAndDestroy(members); sl@0: } sl@0: sl@0: // Extract the specified files from the zip. sl@0: // These files are non compressed, i.e. EStored. sl@0: void DEF057916L() sl@0: { sl@0: test.Next(_L("DEF057916: Getting Corrupt file error when reading wav-file from Zip - PART 1")); sl@0: sl@0: User::LeaveIfError(TheFs.Connect()); //Connect to file session sl@0: User::LeaveIfError(TheFs.SetSessionPath(KPath)); //Set Session Path to direcrt containing test zip files sl@0: CleanupClosePushL(TheFs); sl@0: sl@0: const TUint KDataChunk = 2048; sl@0: _LIT(tmpFileName, "\\Test\\Zip\\compression_estored.zip"); sl@0: _LIT(testFile1, "zip\\test.wav"); sl@0: _LIT(testFile2, "zip\\rfc2459.zip"); sl@0: _LIT(testFile3, "zip\\META-INF\\MANIFEST.MF"); sl@0: sl@0: CZipFile* iZipFile = CZipFile::NewL(TheFs, tmpFileName); sl@0: CleanupStack::PushL(iZipFile); sl@0: sl@0: TInt err = KErrNone; sl@0: sl@0: CZipFileMember* zipMember1 = iZipFile->CaseSensitiveOrCaseInsensitiveMemberL(testFile1); sl@0: if(zipMember1) sl@0: { sl@0: CleanupStack::PushL(zipMember1); sl@0: sl@0: RZipFileMemberReaderStream* iZipStream; sl@0: iZipFile->GetInputStreamL(zipMember1, iZipStream); sl@0: CleanupStack::PushL(iZipStream); sl@0: sl@0: HBufC8* data = HBufC8::NewLC(KDataChunk); sl@0: TPtr8 ptr = data->Des(); sl@0: err = KErrNone; sl@0: sl@0: err = iZipStream->Read(ptr,KDataChunk); sl@0: test (err == KErrNone); sl@0: sl@0: err = iZipStream->Read(ptr,KDataChunk); sl@0: test (err == KErrEof); sl@0: sl@0: CleanupStack::PopAndDestroy(data); sl@0: CleanupStack::PopAndDestroy(iZipStream); sl@0: CleanupStack::PopAndDestroy(zipMember1); sl@0: } sl@0: sl@0: sl@0: CZipFileMember* zipMember2 = iZipFile->CaseSensitiveOrCaseInsensitiveMemberL(testFile2); sl@0: if(zipMember2) sl@0: { sl@0: CleanupStack::PushL(zipMember2); sl@0: sl@0: RZipFileMemberReaderStream* iZipStream; sl@0: iZipFile->GetInputStreamL(zipMember2, iZipStream); sl@0: CleanupStack::PushL(iZipStream); sl@0: sl@0: HBufC8* data = HBufC8::NewLC(KDataChunk*65); sl@0: TPtr8 ptr = data->Des(); sl@0: err = KErrNone; sl@0: sl@0: err = iZipStream->Read(ptr,KDataChunk*65); sl@0: test (err == KErrNone); // file is very very large sl@0: sl@0: err = iZipStream->Read(ptr,KDataChunk); sl@0: test (err == KErrEof); sl@0: sl@0: CleanupStack::PopAndDestroy(data); sl@0: CleanupStack::PopAndDestroy(iZipStream); sl@0: CleanupStack::PopAndDestroy(zipMember2); sl@0: } sl@0: sl@0: sl@0: CZipFileMember* zipMember3 = iZipFile->CaseSensitiveOrCaseInsensitiveMemberL(testFile3); sl@0: if(zipMember3) sl@0: { sl@0: CleanupStack::PushL(zipMember3); sl@0: sl@0: RZipFileMemberReaderStream* iZipStream; sl@0: iZipFile->GetInputStreamL(zipMember3, iZipStream); sl@0: CleanupStack::PushL(iZipStream); sl@0: sl@0: HBufC8* data = HBufC8::NewLC(KDataChunk); sl@0: TPtr8 ptr = data->Des(); sl@0: err = KErrNone; sl@0: sl@0: err = iZipStream->Read(ptr,KDataChunk); sl@0: test (err == KErrNone); sl@0: sl@0: err = iZipStream->Read(ptr,KDataChunk); sl@0: test (err == KErrEof); sl@0: sl@0: CleanupStack::PopAndDestroy(data); sl@0: CleanupStack::PopAndDestroy(iZipStream); sl@0: CleanupStack::PopAndDestroy(zipMember3); sl@0: } sl@0: sl@0: ExtractMembersL (iZipFile); sl@0: sl@0: CleanupStack::PopAndDestroy(iZipFile); sl@0: CleanupStack::PopAndDestroy(&TheFs); sl@0: } sl@0: sl@0: /** sl@0: @SYMTestCaseID SYSLIB-EZLIB-CT-3464 sl@0: @SYMTestCaseDesc RZipFileMemberReaderStream::Read returns incorrect error code under OOM sl@0: @SYMTestPriority High sl@0: @SYMTestActions Tries to read zipfile stream with heap allocation set to fail on all following sl@0: allocations. Check result returned from Read(TDes16&, TInt) and Read(TDes8&, TInt) sl@0: is KErrNoMemory. Note that zip file contents must be compressed, otherwise no sl@0: allocations are performed and Read() function returns KErrNone. sl@0: @SYMTestExpectedResults Read() function should return KErrNoMemory sl@0: @SYMDEF DEF103961 sl@0: */ sl@0: void DEF103961L() sl@0: { sl@0: test.Next(_L(" @SYMTestCaseID:SYSLIB-EZLIB-CT-3464 DEF103961: RZipFileMemberReaderStream::Read returns incorrect error code under OOM ")); sl@0: sl@0: User::LeaveIfError(TheFs.Connect()); //Connect to file session sl@0: User::LeaveIfError(TheFs.SetSessionPath(KPath)); //Set Session Path to directory containing test zip files sl@0: CleanupClosePushL(TheFs); sl@0: sl@0: const TUint KDataChunk = 512; sl@0: _LIT(testFile1, "zip_archive2_with_comments.zip"); sl@0: _LIT(internalFile, "doorslam.wav"); sl@0: sl@0: // open zip file sl@0: CZipFile* zipFile = CZipFile::NewL(TheFs, testFile1); sl@0: CleanupStack::PushL(zipFile); sl@0: sl@0: // get input stream for a file contained in the zip archive sl@0: CZipFileMember* zipMember = zipFile->CaseSensitiveOrCaseInsensitiveMemberL(internalFile); sl@0: test(zipMember!=NULL); sl@0: CleanupStack::PushL(zipMember); sl@0: RZipFileMemberReaderStream* zipStream; sl@0: zipFile->GetInputStreamL(zipMember, zipStream); sl@0: CleanupStack::PushL(zipStream); sl@0: sl@0: // some descriptors to hold stream data... sl@0: HBufC8* data8 = HBufC8::NewLC(KDataChunk); sl@0: TPtr8 ptr8 = data8->Des(); sl@0: HBufC16* data16 = HBufC16::NewLC(KDataChunk); sl@0: TPtr16 ptr16 = data16->Des(); sl@0: sl@0: // do memory tests for RZipFileMemberReaderStream::Read() and its overloaded brother sl@0: __UHEAP_SETFAIL(RHeap::EDeterministic,1); sl@0: TInt error = zipStream->Read(ptr8, KDataChunk); sl@0: test (error == KErrNoMemory); sl@0: error = zipStream->Read(ptr16, KDataChunk); sl@0: test (error == KErrNoMemory); sl@0: __UHEAP_SETFAIL(RHeap::ENone,0); sl@0: sl@0: CleanupStack::PopAndDestroy(data16); sl@0: CleanupStack::PopAndDestroy(data8); sl@0: CleanupStack::PopAndDestroy(zipStream); sl@0: CleanupStack::PopAndDestroy(zipMember); sl@0: CleanupStack::PopAndDestroy(zipFile); sl@0: CleanupStack::PopAndDestroy(&TheFs); sl@0: } sl@0: sl@0: /** sl@0: @SYMTestCaseID SYSLIB-EZLIB-CT-3475 sl@0: @SYMTestCaseDesc RZipFileMemberReaderStream constructor ignores errors under OOM sl@0: @SYMTestPriority High sl@0: @SYMTestActions Construct RZipFileMemberReaderStream under OOM conditions. Check the Leave code. sl@0: @SYMTestExpectedResults Should Leave with KErrNoMemory sl@0: @SYMDEF DEF105995 sl@0: */ sl@0: void DEF105995L() sl@0: { sl@0: test.Next(_L(" @SYMTestCaseID:SYSLIB-EZLIB-CT-3475 DEF105995: RZipFileMemberReaderStream constructor not checking OOM conditions ")); sl@0: sl@0: User::LeaveIfError(TheFs.Connect()); //Connect to file session sl@0: User::LeaveIfError(TheFs.SetSessionPath(KPath)); //Set Session Path to directory containing test zip files sl@0: CleanupClosePushL(TheFs); sl@0: sl@0: _LIT(testFile1, "zip_archive2_with_comments.zip"); sl@0: _LIT(internalFile, "doorslam.wav"); sl@0: sl@0: // open zip file sl@0: CZipFile* zipFile = CZipFile::NewL(TheFs, testFile1); sl@0: CleanupStack::PushL(zipFile); sl@0: sl@0: // get input stream for a file contained in the zip archive sl@0: CZipFileMember* zipMember = zipFile->CaseSensitiveOrCaseInsensitiveMemberL(internalFile); sl@0: test(zipMember!=NULL); sl@0: CleanupStack::PushL(zipMember); sl@0: sl@0: __UHEAP_SETFAIL(RHeap::EDeterministic,2); // so memory allocation in inflateInit2() fails sl@0: sl@0: RZipFileMemberReaderStream* zipStream; sl@0: TRAPD(err, zipFile->GetInputStreamL(zipMember, zipStream)); sl@0: test(err == KErrNoMemory); sl@0: sl@0: __UHEAP_SETFAIL(RHeap::ENone,0); sl@0: sl@0: CleanupStack::PopAndDestroy(zipMember); sl@0: CleanupStack::PopAndDestroy(zipFile); sl@0: CleanupStack::PopAndDestroy(&TheFs); sl@0: } sl@0: sl@0: void RunTestL() sl@0: { sl@0: DEF057916L(); // Getting Corrupt file error when reading wav-file from Zip sl@0: #if defined(_DEBUG) // OOM tests are only run on Debug builds sl@0: DEF103961L(); // RZipFileMemberReaderStream::Read returns incorrect error code under OOM sl@0: DEF105995L(); // inflateInit2 is not checked for errors in RZipFileMemberReaderStream constructor sl@0: #endif sl@0: } sl@0: sl@0: GLDEF_C TInt E32Main() sl@0: { sl@0: __UHEAP_MARK; sl@0: sl@0: test.Printf(_L("\n")); sl@0: test.Title(); sl@0: test.Start( _L("EZlib defect Tests.") ); sl@0: sl@0: TheTrapCleanup=CTrapCleanup::New(); sl@0: sl@0: TRAPD(err,RunTestL()); sl@0: test (err==KErrNone); sl@0: sl@0: test.End(); sl@0: test.Close(); sl@0: delete TheTrapCleanup; sl@0: sl@0: __UHEAP_MARKEND; sl@0: return KErrNone; sl@0: }