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