os/ossrv/compressionlibs/ziplib/test/rtest/ziptest/ziptest.cpp
author sl
Tue, 10 Jun 2014 14:32:02 +0200
changeset 1 260cb5ec6c19
permissions -rw-r--r--
Update contrib.
sl@0
     1
// Copyright (c) 2003-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
#include "zipfile.h"
sl@0
    17
sl@0
    18
#include <e32cons.h>
sl@0
    19
#include <s32file.h>
sl@0
    20
#include <s32strm.h> 
sl@0
    21
#include <utf.h>
sl@0
    22
#include <e32test.h>
sl@0
    23
sl@0
    24
/*
sl@0
    25
	class CZipTest - defines text UI
sl@0
    26
*/
sl@0
    27
static RTest test(_L("ZIPTEST"));
sl@0
    28
sl@0
    29
class CZipTest : public CBase
sl@0
    30
	{
sl@0
    31
public:
sl@0
    32
	// execute UI until exit command processed
sl@0
    33
	void ExecuteL();
sl@0
    34
private:
sl@0
    35
	// destructor
sl@0
    36
	~CZipTest();
sl@0
    37
	// other functions
sl@0
    38
	void TrapDoTest();
sl@0
    39
	void DoTestL();
sl@0
    40
	void ExtractFileL(RFs&,  const CZipFileMember*, CZipFile*, const TFileName*, RFileWriteStream&);
sl@0
    41
	void ExpandMember();
sl@0
    42
	void TheZipTestL(RFs&, TFileName*, RFileWriteStream&);
sl@0
    43
	
sl@0
    44
private:
sl@0
    45
	CConsoleBase*	iConsole;
sl@0
    46
	RFile			iZipOut;
sl@0
    47
	TBool			iNoFiles;
sl@0
    48
	};
sl@0
    49
sl@0
    50
_LIT(KNoFilesFound,"No appropriate files located in this folder.\r\n");
sl@0
    51
_LIT(KOpeningFile,"Opening file ");
sl@0
    52
_LIT(KDots,"...");
sl@0
    53
_LIT(KOutputFileName,"C:\\test\\zip\\extracts\\zip_out.txt");
sl@0
    54
_LIT(KErrFileNotOpened, "File could not be opened.");
sl@0
    55
_LIT(KOk, "Ok");
sl@0
    56
_LIT(KZipName,"Name:		");
sl@0
    57
_LIT(KZipCRC32,"CRC32:		");
sl@0
    58
_LIT(KZipCSize,"Compressed Size:	");
sl@0
    59
_LIT(KZipUCSize,"Uncompressed Size:	");
sl@0
    60
_LIT(KOpenedSuccessfully, "%S opened sucessfully.\n");
sl@0
    61
_LIT(KLineSpace, "\r\n");
sl@0
    62
sl@0
    63
_LIT(KExtractZipPath, "C:\\test\\zip\\extracts\\");
sl@0
    64
sl@0
    65
#if !defined(__WINS__)
sl@0
    66
_LIT(KPath, "Z:\\test\\zip\\");
sl@0
    67
#else
sl@0
    68
_LIT(KPath, "C:\\test\\zip\\");
sl@0
    69
#endif
sl@0
    70
sl@0
    71
void CZipTest::ExecuteL()
sl@0
    72
	{
sl@0
    73
	//text constant declarations
sl@0
    74
	_LIT(KTxtZipTest,"Zip Test");
sl@0
    75
		
sl@0
    76
	iConsole=Console::NewL(KTxtZipTest,
sl@0
    77
		TSize(KConsFullScreen,KConsFullScreen));
sl@0
    78
	DoTestL();
sl@0
    79
	}
sl@0
    80
sl@0
    81
CZipTest::~CZipTest()
sl@0
    82
	{
sl@0
    83
	delete iConsole;
sl@0
    84
	}
sl@0
    85
void CZipTest::TrapDoTest()
sl@0
    86
	{
sl@0
    87
	TInt q=10;
sl@0
    88
sl@0
    89
	while(q<2000)
sl@0
    90
		{
sl@0
    91
		__UHEAP_SETFAIL(RHeap::EDeterministic, q);
sl@0
    92
		__UHEAP_MARK;
sl@0
    93
		TRAPD(err, DoTestL());
sl@0
    94
		test(err==KErrNone);
sl@0
    95
		__UHEAP_MARKEND;
sl@0
    96
		q++;
sl@0
    97
		}
sl@0
    98
	}
sl@0
    99
sl@0
   100
void CZipTest::DoTestL()
sl@0
   101
	{
sl@0
   102
	
sl@0
   103
	TFileName*				FileName;
sl@0
   104
	RFs						Fs;
sl@0
   105
	
sl@0
   106
	iNoFiles=ETrue;
sl@0
   107
	
sl@0
   108
	User::LeaveIfError(Fs.Connect()); //Connect to file session
sl@0
   109
	CleanupClosePushL(Fs);
sl@0
   110
	User::LeaveIfError(Fs.SetSessionPath(KPath)); //Set Session Path to direcrt containing test zip files
sl@0
   111
	
sl@0
   112
	Fs.MkDirAll(KOutputFileName);
sl@0
   113
	iZipOut.Replace(Fs, KOutputFileName, EFileShareAny|EFileWrite);
sl@0
   114
	RFileWriteStream stream(iZipOut);
sl@0
   115
	CleanupClosePushL(stream);
sl@0
   116
	CDir* fileList;
sl@0
   117
	
sl@0
   118
	Fs.GetDir(KPath, KEntryAttNormal, ESortByName, fileList);
sl@0
   119
	CleanupStack::PushL(fileList);
sl@0
   120
	TInt count = fileList->Count(); 
sl@0
   121
	for (TInt y=0; y<count; y++)
sl@0
   122
		{
sl@0
   123
sl@0
   124
		iNoFiles=EFalse;
sl@0
   125
		
sl@0
   126
		TBuf<256> dirFileName((*fileList)[y].iName);
sl@0
   127
sl@0
   128
		FileName= &dirFileName;
sl@0
   129
		stream.WriteL(KLineSpace);
sl@0
   130
		stream.WriteL(KOpeningFile);
sl@0
   131
		stream.WriteL(*FileName);
sl@0
   132
		stream.WriteL(KDots);
sl@0
   133
		TheZipTestL(Fs, FileName, stream);
sl@0
   134
	/*	for(TInt t=1;t<50;t++)
sl@0
   135
		{
sl@0
   136
		__UHEAP_SETFAIL(RHeap::EDeterministic, t);
sl@0
   137
		__UHEAP_MARK;
sl@0
   138
sl@0
   139
		TRAPD(err,TheZipTestL(Fs, FileName, stream));
sl@0
   140
		
sl@0
   141
		if (err!=KErrNoMemory) break;
sl@0
   142
		__UHEAP_MARKEND;
sl@0
   143
		}
sl@0
   144
*/
sl@0
   145
	}
sl@0
   146
	if (iNoFiles) 
sl@0
   147
		{
sl@0
   148
		iConsole->Printf(KNoFilesFound);
sl@0
   149
		stream.WriteL(KNoFilesFound);
sl@0
   150
		}
sl@0
   151
	CleanupStack::PopAndDestroy(3);
sl@0
   152
/*	CleanupStack::Pop();
sl@0
   153
	Fs.Close();
sl@0
   154
*/	}
sl@0
   155
sl@0
   156
#if defined(__GCC32__)
sl@0
   157
typedef wchar_t __TText;
sl@0
   158
#elif defined(__VC32__)
sl@0
   159
typedef TUint16 __TText;
sl@0
   160
#elif defined(__CW32__)
sl@0
   161
typedef TUint16 __TText;
sl@0
   162
#elif !defined(__TText_defined)
sl@0
   163
#error  no typedef for __TText
sl@0
   164
#endif
sl@0
   165
const __TText* KShortData1252  = L"\x20AC\u00FC\u00E9";
sl@0
   166
const __TText* KShortData850   = L"\u00C7\u00FC\u00E9";
sl@0
   167
const __TText* KLongData1252   = L"\u00E7\u00F8d\u00EB\u00DE\u00E3g\u00EA1252";
sl@0
   168
const __TText* KLongData850    = L"\u00E7\u00F6d\u00EBp\u00E4g\u00EA850";
sl@0
   169
const __TText* KShortWin2K1252 = L"CP1252Win2kShort.zip"; // created on win2k with winzip
sl@0
   170
const __TText* KShortWinXP1252 = L"CP1252WinXPShort.zip"; // created on XP with winzip
sl@0
   171
const __TText* KShortWin2K850  = L"CP850Win2kShort.zip";
sl@0
   172
const __TText* KShortWinXPMS850= L"CP850WinXPMSShort.zip";// created on XP with Compressed Folders
sl@0
   173
const __TText* KShortWinXP850  = L"CP850WinXPShort.zip";
sl@0
   174
const __TText* KLongWin2K      = L"CPMixWin2kLong.zip";
sl@0
   175
const __TText* KLongWinXP      = L"CPMixWinXPLong.zip";
sl@0
   176
const __TText* KLongWinXPMS    = L"CPMixWinXPMSLong.zip";
sl@0
   177
struct TUnzipResultsArray
sl@0
   178
	{
sl@0
   179
	const __TText*  iZipFileName;
sl@0
   180
	TInt iZipFileMemberIndex;
sl@0
   181
	const __TText*  iZipFileMemberExpectedName;
sl@0
   182
	};
sl@0
   183
const TUnzipResultsArray KUnzipResults[] =
sl@0
   184
	{
sl@0
   185
	{KShortWin2K1252, 1, KShortData1252},
sl@0
   186
	{KShortWinXP1252, 1, KShortData1252},
sl@0
   187
	{KShortWin2K850,  1, KShortData850},
sl@0
   188
	{KShortWinXPMS850,1, KShortData850},
sl@0
   189
	{KShortWinXP850,  1, KShortData850},
sl@0
   190
	{KLongWin2K,      1, KLongData1252},
sl@0
   191
	{KLongWin2K,      2, KLongData850},
sl@0
   192
	{KLongWinXP,      1, KLongData850},
sl@0
   193
	{KLongWinXP,      2, KLongData1252},
sl@0
   194
	{KLongWinXPMS,    1, KLongData850},
sl@0
   195
	{KLongWinXPMS,    2, KLongData1252}
sl@0
   196
	};
sl@0
   197
void TestEntry(const TDesC16& aFileName, TInt aIndex, const TDesC16& aEntryName)
sl@0
   198
// Test for correct decoding of filenames containing extended characters (DEF083102)	
sl@0
   199
// Cycle through data defined above looking for matching filenames and data
sl@0
   200
	{	
sl@0
   201
 	for (TInt i = 0; i != sizeof(KUnzipResults)/sizeof(KUnzipResults[0]); ++i)
sl@0
   202
 		{
sl@0
   203
 		TPtrC filename(reinterpret_cast<const TText16*>(KUnzipResults[i].iZipFileName));
sl@0
   204
 		if(aFileName==filename && aIndex==KUnzipResults[i].iZipFileMemberIndex)
sl@0
   205
 			{
sl@0
   206
	 		TPtrC entryname(reinterpret_cast<const TText16*>(KUnzipResults[i].iZipFileMemberExpectedName));
sl@0
   207
 			test.Next(_L("DEF083102"));
sl@0
   208
 			test(aEntryName==entryname);
sl@0
   209
 			}
sl@0
   210
 		}
sl@0
   211
	}
sl@0
   212
	
sl@0
   213
void CZipTest::TheZipTestL(RFs& aFs, 
sl@0
   214
						  TFileName* aFileName, 
sl@0
   215
						  RFileWriteStream& aStream)
sl@0
   216
	{
sl@0
   217
	CZipFile*				ZipFile=0;
sl@0
   218
	CZipFileMember*			Member=0;
sl@0
   219
	CZipFileMemberIterator*	FileMembers;
sl@0
   220
sl@0
   221
	iConsole->Printf(_L("Test file: %S\r\n"), aFileName);
sl@0
   222
	TRAPD(err, ZipFile = CZipFile::NewL(aFs,*aFileName));
sl@0
   223
	
sl@0
   224
	if(err==KErrNoMemory) 
sl@0
   225
		User::Leave(err); // leave here if error is no memory
sl@0
   226
	
sl@0
   227
	if (err!=KErrNone)
sl@0
   228
		{
sl@0
   229
		aStream.WriteL(KErrFileNotOpened);
sl@0
   230
		aStream.WriteL(KLineSpace);
sl@0
   231
	
sl@0
   232
		if (err == CZipArchive::KZipArchiveError)
sl@0
   233
			aStream.WriteL(_L("Error: Zip Archive Error.\r\n"));
sl@0
   234
		else if (err == CZipArchive::KCentralDirectoryTrailerNotFound)
sl@0
   235
			aStream.WriteL(_L("Error: Central Directory Trailer Not Found.\r\n"));
sl@0
   236
		else if (err == CZipArchive::KCentralDirectoryTrailerInvalid)
sl@0
   237
			aStream.WriteL(_L("Error: KCentral Directory Trailer Invalid.\r\n"));
sl@0
   238
		else if (err == CZipArchive::KCompressionMethodNotSupported)
sl@0
   239
			aStream.WriteL(_L("Error: Compression Method Not Supported.\r\n"));
sl@0
   240
		else if (err == CZipArchive::KMultiDiskArchivesNotSupported)
sl@0
   241
			aStream.WriteL(_L("Error: Multi Disk Archives Not Supported.\r\n"));
sl@0
   242
		else if (err == CZipArchive::KMemberNotFound)
sl@0
   243
			aStream.WriteL(_L("Error: Member Not Found.\r\n"));
sl@0
   244
		else if (err == CZipArchive::KZipArchiveMinError)
sl@0
   245
			aStream.WriteL(_L("Error: Zip Archive Min Error.\r\n"));
sl@0
   246
sl@0
   247
		User::Leave(err);
sl@0
   248
		}
sl@0
   249
	else
sl@0
   250
		{
sl@0
   251
		CleanupStack::PushL(ZipFile);
sl@0
   252
		aStream.WriteL(KOk);
sl@0
   253
		aStream.WriteL(KLineSpace);
sl@0
   254
			aStream.WriteL(KLineSpace);
sl@0
   255
		iConsole->Printf(KOpenedSuccessfully, aFileName);
sl@0
   256
	
sl@0
   257
		iNoFiles=EFalse;
sl@0
   258
			
sl@0
   259
		FileMembers = ZipFile->GetMembersL();
sl@0
   260
		CleanupStack::PushL(FileMembers);
sl@0
   261
		Member = FileMembers->NextL();
sl@0
   262
		TInt index=1;// have to provide this - the one in the iterator is private
sl@0
   263
		while (Member != NULL)
sl@0
   264
			{
sl@0
   265
			CleanupStack::PushL(Member);
sl@0
   266
			TBuf<16> CRC32;
sl@0
   267
			CRC32.NumUC((TUint)Member->CRC32(), EHex);
sl@0
   268
			TBuf<16> UnComp;
sl@0
   269
			UnComp.NumUC((TUint)Member->UncompressedSize());
sl@0
   270
			TBuf<16> Comp;
sl@0
   271
			Comp.NumUC((TUint)Member->CompressedSize());
sl@0
   272
			aStream.WriteL(KZipName);
sl@0
   273
			aStream.WriteL(*Member->Name());
sl@0
   274
			aStream.WriteL(KLineSpace);
sl@0
   275
			aStream.WriteL(KZipCRC32);
sl@0
   276
			aStream.WriteL(CRC32);
sl@0
   277
			aStream.WriteL(KLineSpace);
sl@0
   278
			aStream.WriteL(KZipCSize);
sl@0
   279
			aStream.WriteL(Comp);
sl@0
   280
			aStream.WriteL(KLineSpace);
sl@0
   281
			aStream.WriteL(KZipUCSize);
sl@0
   282
			aStream.WriteL(UnComp);
sl@0
   283
			aStream.WriteL(KLineSpace);
sl@0
   284
			aStream.CommitL();
sl@0
   285
			ExtractFileL(aFs, Member, ZipFile, aFileName, aStream);
sl@0
   286
			TestEntry(*aFileName,index++,*Member->Name());
sl@0
   287
			aFs.SetSessionPath(KPath);
sl@0
   288
			CleanupStack::PopAndDestroy(Member); 
sl@0
   289
			Member = FileMembers->NextL();
sl@0
   290
			}
sl@0
   291
		CleanupStack::PopAndDestroy(FileMembers); 
sl@0
   292
		CleanupStack::PopAndDestroy(ZipFile);
sl@0
   293
		}
sl@0
   294
}
sl@0
   295
sl@0
   296
void CZipTest::ExtractFileL(RFs& aFs, 
sl@0
   297
							const CZipFileMember* aMember, 
sl@0
   298
							CZipFile* aZipFile, 
sl@0
   299
							const TFileName* aFileName,
sl@0
   300
							RFileWriteStream& aStream)
sl@0
   301
	{
sl@0
   302
	TInt loop=0;
sl@0
   303
	HBufC* name = aMember->Name()->AllocLC();
sl@0
   304
	while (loop<name->Length())
sl@0
   305
		{
sl@0
   306
		if ((*name)[loop] == '/')
sl@0
   307
			{
sl@0
   308
			name->Des()[loop] = '\\';
sl@0
   309
			}
sl@0
   310
		loop++;
sl@0
   311
		}
sl@0
   312
	
sl@0
   313
	TFileName fn;
sl@0
   314
	fn.Append(KExtractZipPath);
sl@0
   315
	fn.Append(*aFileName);
sl@0
   316
	fn.Append('\\');
sl@0
   317
	fn.Append(*name);
sl@0
   318
//	
sl@0
   319
	RFile expandedMember;
sl@0
   320
	CleanupClosePushL(expandedMember);
sl@0
   321
//
sl@0
   322
	aFs.SetSessionPath(KExtractZipPath);
sl@0
   323
	aFs.MkDirAll(fn);
sl@0
   324
	TInt retVal = expandedMember.Replace(aFs,fn, EFileShareAny|EFileWrite);
sl@0
   325
	if(retVal != KErrNone)
sl@0
   326
	{
sl@0
   327
	//Could be a directory name which we are planning to write to.
sl@0
   328
	CleanupStack::PopAndDestroy(2,name); //expanded member, name
sl@0
   329
	expandedMember.Close();
sl@0
   330
	return;
sl@0
   331
	}
sl@0
   332
sl@0
   333
//
sl@0
   334
	RZipFileMemberReaderStream* fileStream;
sl@0
   335
	TInt error = aZipFile->GetInputStreamL(aMember, fileStream);
sl@0
   336
	if (error != KErrNone)
sl@0
   337
		{
sl@0
   338
		_LIT(KCompressionNotSupported, "Error: Compression Method Not Supported");
sl@0
   339
		aStream.WriteL(KCompressionNotSupported);
sl@0
   340
		aStream.WriteL(KLineSpace);
sl@0
   341
		CleanupStack::PopAndDestroy(2,name); //expanded member and name
sl@0
   342
		aFs.Delete(fn);
sl@0
   343
		return;
sl@0
   344
		}
sl@0
   345
	CleanupStack::PushL(fileStream);
sl@0
   346
//
sl@0
   347
	TUint32 size = aMember->UncompressedSize();
sl@0
   348
	HBufC8* bytes = HBufC8::New(size);
sl@0
   349
	CleanupStack::PushL(bytes);
sl@0
   350
	TPtr8 ptr = bytes->Des();
sl@0
   351
	User::LeaveIfError(fileStream->Read(ptr,size));
sl@0
   352
	User::LeaveIfError(expandedMember.Write(ptr));
sl@0
   353
	CleanupStack::PopAndDestroy(4,name); // bytes, fileStream, expanded member and name
sl@0
   354
	expandedMember.Close();	
sl@0
   355
	}
sl@0
   356
	
sl@0
   357
static void executeUiL()
sl@0
   358
	{
sl@0
   359
	CZipTest* ziptest=new (ELeave) CZipTest;
sl@0
   360
	CleanupStack::PushL(ziptest);
sl@0
   361
	ziptest->ExecuteL();
sl@0
   362
	CleanupStack::PopAndDestroy();
sl@0
   363
	}
sl@0
   364
/**
sl@0
   365
@SYMTestCaseID          SYSLIB-EZLIB2-UT-4299
sl@0
   366
@SYMTestCaseDesc	    Test CZipFile functionality like open, close, size of the file.
sl@0
   367
@SYMTestPriority 	    High
sl@0
   368
@SYMTestActions  	    Creates a new CZipFile object using the supplied file server session and a valid file handle using CZipFile::NewL() 
sl@0
   369
						Open zip file using CZipFile::OpenL()
sl@0
   370
						Check the size of the zip file using CZipFile::Size()
sl@0
   371
						Close zip file using CZipFile::Close()
sl@0
   372
@SYMTestExpectedResults The test succeeds with no errors i.e. KErrNone 
sl@0
   373
@SYMDEF                 REQ8024
sl@0
   374
*/
sl@0
   375
static void TestCZipFileMethodsL()
sl@0
   376
	{
sl@0
   377
	test.Next(_L(" @SYMTestCaseID:SYSLIB-EZLIB2-UT-4299 "));
sl@0
   378
	RFs	rFs;
sl@0
   379
	
sl@0
   380
	User::LeaveIfError(rFs.Connect()); //Connect to file session
sl@0
   381
	CleanupClosePushL(rFs);
sl@0
   382
	User::LeaveIfError(rFs.SetSessionPath(KPath)); //Set Session Path to direcrt containing test zip files
sl@0
   383
		
sl@0
   384
	TFileName fileName(_L("rfc2459.zip"));
sl@0
   385
sl@0
   386
	test.Printf(_L("Test file: %S\r\n"), &fileName);
sl@0
   387
sl@0
   388
	CZipFile* ZipFile = 0;
sl@0
   389
	TRAPD(err, ZipFile = CZipFile::NewL(rFs, fileName));
sl@0
   390
	
sl@0
   391
	CleanupStack::PushL(ZipFile);
sl@0
   392
	
sl@0
   393
	TRAP(err, ZipFile->OpenL());
sl@0
   394
	test(err == KErrNone);
sl@0
   395
	
sl@0
   396
	TInt fileSize;
sl@0
   397
	TRAP(err, ZipFile->Size(fileSize));
sl@0
   398
	test(err == KErrNone && fileSize > 0);
sl@0
   399
	
sl@0
   400
	TRAP(err, ZipFile->Close());
sl@0
   401
	test(err == KErrNone);
sl@0
   402
sl@0
   403
	CleanupStack::PopAndDestroy(2);// rFs, ZipFile
sl@0
   404
	}
sl@0
   405
sl@0
   406
GLDEF_C TInt E32Main() // main function called by E32
sl@0
   407
    {
sl@0
   408
	test.Title();
sl@0
   409
	__UHEAP_MARK; // mark heap state
sl@0
   410
	test.Start(_L("Test ZIP"));
sl@0
   411
	CTrapCleanup* cleanup=CTrapCleanup::New(); // get clean-up stack
sl@0
   412
	TRAPD(error, executeUiL()); // do most stuff under cleanup stack
sl@0
   413
	test(error==KErrNone);
sl@0
   414
	test.Next(_L("Test CZipFile functionality like open, close, size of the file"));
sl@0
   415
	TRAP(error, TestCZipFileMethodsL());
sl@0
   416
	test.End();
sl@0
   417
	delete cleanup; // destroy clean-up stack
sl@0
   418
	__UHEAP_MARKEND; // check no memory leak
sl@0
   419
	test.Close();
sl@0
   420
	return 0; // and return
sl@0
   421
    }
sl@0
   422