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