os/ossrv/compressionlibs/ziplib/test/rtest/ezdefect/ezdefect.cpp
author sl@SLION-WIN7.fritz.box
Fri, 15 Jun 2012 03:10:57 +0200
changeset 0 bde4ae8d615e
permissions -rw-r--r--
First public contribution.
     1 // Copyright (c) 2005-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 
    17 #include <e32test.h>
    18 #include <zipfile.h>
    19 
    20 LOCAL_D RTest				test(_L("ezdefect.exe"));
    21 LOCAL_D RFs					TheFs;
    22 LOCAL_D CTrapCleanup* 		TheTrapCleanup 		= NULL;
    23 
    24 #if !defined(__WINS__)
    25 _LIT(KPath, "Z:\\test\\zip\\");
    26 #else
    27 _LIT(KPath, "C:\\test\\zip\\");
    28 #endif
    29 
    30 
    31 
    32 void ExtractFileL(const CZipFileMember* aMember, CZipFile* aZip)
    33 	{
    34 	HBufC* name = aMember->Name()->AllocLC();
    35 	for (TInt i = 0; i<name->Length(); i++)
    36 		{
    37 		if ((*name)[i] == '/')
    38 			{
    39 			name->Des()[i] = '\\';
    40 			}
    41 		}
    42 	
    43 	RZipFileMemberReaderStream* fileStream;
    44 	User::LeaveIfError(aZip->GetInputStreamL(aMember, fileStream));
    45 	CleanupStack::PushL(fileStream);
    46 
    47 	TUint32 size = aMember->UncompressedSize();
    48 	HBufC8* bytes = HBufC8::New(size);
    49 	CleanupStack::PushL(bytes);
    50 	TPtr8 ptr = bytes->Des();
    51 	User::LeaveIfError(fileStream->Read(ptr,size));	
    52 	CleanupStack::PopAndDestroy(3,name); // bytes, fileStream and name
    53 	}
    54 		
    55 // Extract all members from the zip.
    56 // These files are non compressed, i.e. EStored.
    57 void ExtractMembersL(CZipFile* aZip)
    58 	{
    59 	test.Next(_L("DEF057916: Getting Corrupt file error when reading wav-file from Zip - PART 2"));
    60 
    61 	CZipFileMemberIterator*	members = aZip->GetMembersL();
    62 	CleanupStack::PushL(members);
    63 	
    64 	CZipFileMember* member = members->NextL();
    65 	while (member)
    66 		{
    67 		CleanupStack::PushL(member);
    68 		ExtractFileL(member, aZip);
    69 		CleanupStack::PopAndDestroy(member); 
    70 		member = members->NextL();
    71 		}
    72 
    73 	CleanupStack::PopAndDestroy(members); 
    74 	}
    75 
    76 // Extract the specified files from the zip.
    77 // These files are non compressed, i.e. EStored.
    78 void DEF057916L()
    79     {
    80 	test.Next(_L("DEF057916: Getting Corrupt file error when reading wav-file from Zip - PART 1"));
    81 
    82 	User::LeaveIfError(TheFs.Connect()); //Connect to file session
    83 	User::LeaveIfError(TheFs.SetSessionPath(KPath)); //Set Session Path to direcrt containing test zip files
    84 	CleanupClosePushL(TheFs);
    85 
    86 	const TUint KDataChunk = 2048;
    87 	_LIT(tmpFileName, "\\Test\\Zip\\compression_estored.zip");
    88 	_LIT(testFile1, "zip\\test.wav");
    89 	_LIT(testFile2, "zip\\rfc2459.zip");
    90 	_LIT(testFile3, "zip\\META-INF\\MANIFEST.MF");
    91 	
    92 	CZipFile* iZipFile = CZipFile::NewL(TheFs, tmpFileName);
    93 	CleanupStack::PushL(iZipFile);
    94 
    95 	TInt err = KErrNone;
    96 
    97 	CZipFileMember* zipMember1 = iZipFile->CaseSensitiveOrCaseInsensitiveMemberL(testFile1);
    98 	if(zipMember1)
    99 		{
   100 		CleanupStack::PushL(zipMember1);
   101 
   102 		RZipFileMemberReaderStream* iZipStream;
   103 		iZipFile->GetInputStreamL(zipMember1, iZipStream);
   104 		CleanupStack::PushL(iZipStream);
   105 
   106 		HBufC8* data = HBufC8::NewLC(KDataChunk);
   107 		TPtr8 ptr = data->Des();
   108 		err = KErrNone;
   109 		
   110 		err = iZipStream->Read(ptr,KDataChunk);
   111 		test (err == KErrNone);
   112 		
   113 		err = iZipStream->Read(ptr,KDataChunk);
   114 		test (err == KErrEof);
   115 		
   116 		CleanupStack::PopAndDestroy(data);
   117 		CleanupStack::PopAndDestroy(iZipStream);
   118 		CleanupStack::PopAndDestroy(zipMember1);  
   119 		}  
   120 		
   121 		
   122 	CZipFileMember* zipMember2 = iZipFile->CaseSensitiveOrCaseInsensitiveMemberL(testFile2);
   123 	if(zipMember2)
   124 		{
   125 		CleanupStack::PushL(zipMember2);
   126 
   127 		RZipFileMemberReaderStream* iZipStream;
   128 		iZipFile->GetInputStreamL(zipMember2, iZipStream);
   129 		CleanupStack::PushL(iZipStream);
   130 
   131 		HBufC8* data = HBufC8::NewLC(KDataChunk*65);
   132 		TPtr8 ptr = data->Des();
   133 		err = KErrNone;
   134 		
   135 		err = iZipStream->Read(ptr,KDataChunk*65);
   136 		test (err == KErrNone); // file is very very large
   137 		
   138 		err = iZipStream->Read(ptr,KDataChunk);
   139 		test (err == KErrEof);
   140 
   141 		CleanupStack::PopAndDestroy(data);
   142 		CleanupStack::PopAndDestroy(iZipStream);
   143 		CleanupStack::PopAndDestroy(zipMember2);  
   144 		}  
   145 		
   146 	
   147 	CZipFileMember* zipMember3 = iZipFile->CaseSensitiveOrCaseInsensitiveMemberL(testFile3);
   148 	if(zipMember3)
   149 		{
   150 		CleanupStack::PushL(zipMember3);
   151 
   152 		RZipFileMemberReaderStream* iZipStream;
   153 		iZipFile->GetInputStreamL(zipMember3, iZipStream);
   154 		CleanupStack::PushL(iZipStream);
   155 
   156 		HBufC8* data = HBufC8::NewLC(KDataChunk);
   157 		TPtr8 ptr = data->Des();
   158 		err = KErrNone;
   159 		
   160 		err = iZipStream->Read(ptr,KDataChunk);
   161 		test (err == KErrNone);
   162 		
   163 		err = iZipStream->Read(ptr,KDataChunk);
   164 		test (err == KErrEof);
   165 
   166 		CleanupStack::PopAndDestroy(data);
   167 		CleanupStack::PopAndDestroy(iZipStream);
   168 		CleanupStack::PopAndDestroy(zipMember3);  
   169 		}  		
   170 		
   171 	ExtractMembersL (iZipFile);
   172 	
   173 	CleanupStack::PopAndDestroy(iZipFile);
   174 	CleanupStack::PopAndDestroy(&TheFs);
   175 	}
   176 
   177 /**
   178 @SYMTestCaseID       	SYSLIB-EZLIB-CT-3464
   179 @SYMTestCaseDesc     	RZipFileMemberReaderStream::Read returns incorrect error code under OOM
   180 @SYMTestPriority     	High
   181 @SYMTestActions      	Tries to read zipfile stream with heap allocation set to fail on all following 
   182 						allocations.  Check result returned from Read(TDes16&, TInt) and Read(TDes8&, TInt) 
   183 						is KErrNoMemory.  Note that zip file contents must be compressed, otherwise no 
   184 						allocations are performed and Read() function returns KErrNone.
   185 @SYMTestExpectedResults Read() function should return KErrNoMemory
   186 @SYMDEF                 DEF103961
   187 */
   188 void DEF103961L()
   189 	{
   190 	test.Next(_L(" @SYMTestCaseID:SYSLIB-EZLIB-CT-3464 DEF103961: RZipFileMemberReaderStream::Read returns incorrect error code under OOM "));
   191 
   192 	User::LeaveIfError(TheFs.Connect()); //Connect to file session
   193 	User::LeaveIfError(TheFs.SetSessionPath(KPath)); //Set Session Path to directory containing test zip files
   194 	CleanupClosePushL(TheFs);
   195 
   196 	const TUint KDataChunk = 512;
   197 	_LIT(testFile1, "zip_archive2_with_comments.zip");
   198 	_LIT(internalFile, "doorslam.wav");
   199 	
   200 	// open zip file
   201 	CZipFile* zipFile = CZipFile::NewL(TheFs, testFile1);
   202 	CleanupStack::PushL(zipFile);
   203 
   204 	// get input stream for a file contained in the zip archive
   205 	CZipFileMember* zipMember = zipFile->CaseSensitiveOrCaseInsensitiveMemberL(internalFile);
   206 	test(zipMember!=NULL);
   207 	CleanupStack::PushL(zipMember);
   208 	RZipFileMemberReaderStream* zipStream;
   209 	zipFile->GetInputStreamL(zipMember, zipStream);
   210 	CleanupStack::PushL(zipStream);
   211 	
   212 	// some descriptors to hold stream data...
   213 	HBufC8* data8 = HBufC8::NewLC(KDataChunk);
   214 	TPtr8 ptr8 = data8->Des();
   215 	HBufC16* data16 = HBufC16::NewLC(KDataChunk);
   216 	TPtr16 ptr16 = data16->Des();
   217 
   218 	// do memory tests for RZipFileMemberReaderStream::Read() and its overloaded brother
   219 	__UHEAP_SETFAIL(RHeap::EDeterministic,1);
   220 	TInt error = zipStream->Read(ptr8, KDataChunk);
   221 	test (error == KErrNoMemory);
   222 	error = zipStream->Read(ptr16, KDataChunk);
   223 	test (error == KErrNoMemory);
   224 	__UHEAP_SETFAIL(RHeap::ENone,0);
   225 	
   226 	CleanupStack::PopAndDestroy(data16);
   227 	CleanupStack::PopAndDestroy(data8);
   228 	CleanupStack::PopAndDestroy(zipStream);
   229 	CleanupStack::PopAndDestroy(zipMember);
   230 	CleanupStack::PopAndDestroy(zipFile);
   231 	CleanupStack::PopAndDestroy(&TheFs);
   232 	}	
   233 
   234 /**
   235 @SYMTestCaseID		SYSLIB-EZLIB-CT-3475
   236 @SYMTestCaseDesc 	RZipFileMemberReaderStream constructor ignores errors under OOM
   237 @SYMTestPriority    High
   238 @SYMTestActions     Construct RZipFileMemberReaderStream under OOM conditions.  Check the Leave code.
   239 @SYMTestExpectedResults	Should Leave with KErrNoMemory
   240 @SYMDEF              	DEF105995
   241 */
   242 void DEF105995L()
   243 	{
   244 	test.Next(_L(" @SYMTestCaseID:SYSLIB-EZLIB-CT-3475 DEF105995: RZipFileMemberReaderStream constructor not checking OOM conditions "));
   245 	
   246 	User::LeaveIfError(TheFs.Connect()); //Connect to file session
   247 	User::LeaveIfError(TheFs.SetSessionPath(KPath)); //Set Session Path to directory containing test zip files
   248 	CleanupClosePushL(TheFs);
   249 
   250 	_LIT(testFile1, "zip_archive2_with_comments.zip");
   251 	_LIT(internalFile, "doorslam.wav");
   252 	
   253 	// open zip file
   254 	CZipFile* zipFile = CZipFile::NewL(TheFs, testFile1);
   255 	CleanupStack::PushL(zipFile);
   256 
   257 	// get input stream for a file contained in the zip archive
   258 	CZipFileMember* zipMember = zipFile->CaseSensitiveOrCaseInsensitiveMemberL(internalFile);
   259 	test(zipMember!=NULL);
   260 	CleanupStack::PushL(zipMember);
   261 	
   262 	__UHEAP_SETFAIL(RHeap::EDeterministic,2); // so memory allocation in inflateInit2() fails
   263 
   264 	RZipFileMemberReaderStream* zipStream;
   265 	TRAPD(err, zipFile->GetInputStreamL(zipMember, zipStream));
   266 	test(err == KErrNoMemory);	
   267 
   268 	__UHEAP_SETFAIL(RHeap::ENone,0);
   269 	
   270 	CleanupStack::PopAndDestroy(zipMember);
   271 	CleanupStack::PopAndDestroy(zipFile);
   272 	CleanupStack::PopAndDestroy(&TheFs);
   273 	}
   274 				
   275 void RunTestL()
   276 	{
   277 	DEF057916L(); // Getting Corrupt file error when reading wav-file from Zip
   278 	#if defined(_DEBUG)	// OOM tests are only run on Debug builds
   279 	DEF103961L(); // RZipFileMemberReaderStream::Read returns incorrect error code under OOM
   280 	DEF105995L(); // inflateInit2 is not checked for errors in RZipFileMemberReaderStream constructor
   281 	#endif
   282 	}	
   283 
   284 GLDEF_C TInt E32Main()
   285 	{
   286 	__UHEAP_MARK;
   287 
   288 	test.Printf(_L("\n"));
   289 	test.Title();
   290 	test.Start( _L("EZlib defect Tests.") );
   291 
   292 	TheTrapCleanup=CTrapCleanup::New();
   293 
   294 	TRAPD(err,RunTestL());
   295 	test (err==KErrNone);
   296 
   297 	test.End();
   298 	test.Close();
   299 	delete TheTrapCleanup;
   300 
   301 	__UHEAP_MARKEND;
   302 	return KErrNone;
   303 	}