1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/os/ossrv/compressionlibs/ziplib/test/rtest/ziptest/ziptest.cpp Fri Jun 15 03:10:57 2012 +0200
1.3 @@ -0,0 +1,422 @@
1.4 +// Copyright (c) 2003-2009 Nokia Corporation and/or its subsidiary(-ies).
1.5 +// All rights reserved.
1.6 +// This component and the accompanying materials are made available
1.7 +// under the terms of "Eclipse Public License v1.0"
1.8 +// which accompanies this distribution, and is available
1.9 +// at the URL "http://www.eclipse.org/legal/epl-v10.html".
1.10 +//
1.11 +// Initial Contributors:
1.12 +// Nokia Corporation - initial contribution.
1.13 +//
1.14 +// Contributors:
1.15 +//
1.16 +// Description:
1.17 +//
1.18 +
1.19 +#include "zipfile.h"
1.20 +
1.21 +#include <e32cons.h>
1.22 +#include <s32file.h>
1.23 +#include <s32strm.h>
1.24 +#include <utf.h>
1.25 +#include <e32test.h>
1.26 +
1.27 +/*
1.28 + class CZipTest - defines text UI
1.29 +*/
1.30 +static RTest test(_L("ZIPTEST"));
1.31 +
1.32 +class CZipTest : public CBase
1.33 + {
1.34 +public:
1.35 + // execute UI until exit command processed
1.36 + void ExecuteL();
1.37 +private:
1.38 + // destructor
1.39 + ~CZipTest();
1.40 + // other functions
1.41 + void TrapDoTest();
1.42 + void DoTestL();
1.43 + void ExtractFileL(RFs&, const CZipFileMember*, CZipFile*, const TFileName*, RFileWriteStream&);
1.44 + void ExpandMember();
1.45 + void TheZipTestL(RFs&, TFileName*, RFileWriteStream&);
1.46 +
1.47 +private:
1.48 + CConsoleBase* iConsole;
1.49 + RFile iZipOut;
1.50 + TBool iNoFiles;
1.51 + };
1.52 +
1.53 +_LIT(KNoFilesFound,"No appropriate files located in this folder.\r\n");
1.54 +_LIT(KOpeningFile,"Opening file ");
1.55 +_LIT(KDots,"...");
1.56 +_LIT(KOutputFileName,"C:\\test\\zip\\extracts\\zip_out.txt");
1.57 +_LIT(KErrFileNotOpened, "File could not be opened.");
1.58 +_LIT(KOk, "Ok");
1.59 +_LIT(KZipName,"Name: ");
1.60 +_LIT(KZipCRC32,"CRC32: ");
1.61 +_LIT(KZipCSize,"Compressed Size: ");
1.62 +_LIT(KZipUCSize,"Uncompressed Size: ");
1.63 +_LIT(KOpenedSuccessfully, "%S opened sucessfully.\n");
1.64 +_LIT(KLineSpace, "\r\n");
1.65 +
1.66 +_LIT(KExtractZipPath, "C:\\test\\zip\\extracts\\");
1.67 +
1.68 +#if !defined(__WINS__)
1.69 +_LIT(KPath, "Z:\\test\\zip\\");
1.70 +#else
1.71 +_LIT(KPath, "C:\\test\\zip\\");
1.72 +#endif
1.73 +
1.74 +void CZipTest::ExecuteL()
1.75 + {
1.76 + //text constant declarations
1.77 + _LIT(KTxtZipTest,"Zip Test");
1.78 +
1.79 + iConsole=Console::NewL(KTxtZipTest,
1.80 + TSize(KConsFullScreen,KConsFullScreen));
1.81 + DoTestL();
1.82 + }
1.83 +
1.84 +CZipTest::~CZipTest()
1.85 + {
1.86 + delete iConsole;
1.87 + }
1.88 +void CZipTest::TrapDoTest()
1.89 + {
1.90 + TInt q=10;
1.91 +
1.92 + while(q<2000)
1.93 + {
1.94 + __UHEAP_SETFAIL(RHeap::EDeterministic, q);
1.95 + __UHEAP_MARK;
1.96 + TRAPD(err, DoTestL());
1.97 + test(err==KErrNone);
1.98 + __UHEAP_MARKEND;
1.99 + q++;
1.100 + }
1.101 + }
1.102 +
1.103 +void CZipTest::DoTestL()
1.104 + {
1.105 +
1.106 + TFileName* FileName;
1.107 + RFs Fs;
1.108 +
1.109 + iNoFiles=ETrue;
1.110 +
1.111 + User::LeaveIfError(Fs.Connect()); //Connect to file session
1.112 + CleanupClosePushL(Fs);
1.113 + User::LeaveIfError(Fs.SetSessionPath(KPath)); //Set Session Path to direcrt containing test zip files
1.114 +
1.115 + Fs.MkDirAll(KOutputFileName);
1.116 + iZipOut.Replace(Fs, KOutputFileName, EFileShareAny|EFileWrite);
1.117 + RFileWriteStream stream(iZipOut);
1.118 + CleanupClosePushL(stream);
1.119 + CDir* fileList;
1.120 +
1.121 + Fs.GetDir(KPath, KEntryAttNormal, ESortByName, fileList);
1.122 + CleanupStack::PushL(fileList);
1.123 + TInt count = fileList->Count();
1.124 + for (TInt y=0; y<count; y++)
1.125 + {
1.126 +
1.127 + iNoFiles=EFalse;
1.128 +
1.129 + TBuf<256> dirFileName((*fileList)[y].iName);
1.130 +
1.131 + FileName= &dirFileName;
1.132 + stream.WriteL(KLineSpace);
1.133 + stream.WriteL(KOpeningFile);
1.134 + stream.WriteL(*FileName);
1.135 + stream.WriteL(KDots);
1.136 + TheZipTestL(Fs, FileName, stream);
1.137 + /* for(TInt t=1;t<50;t++)
1.138 + {
1.139 + __UHEAP_SETFAIL(RHeap::EDeterministic, t);
1.140 + __UHEAP_MARK;
1.141 +
1.142 + TRAPD(err,TheZipTestL(Fs, FileName, stream));
1.143 +
1.144 + if (err!=KErrNoMemory) break;
1.145 + __UHEAP_MARKEND;
1.146 + }
1.147 +*/
1.148 + }
1.149 + if (iNoFiles)
1.150 + {
1.151 + iConsole->Printf(KNoFilesFound);
1.152 + stream.WriteL(KNoFilesFound);
1.153 + }
1.154 + CleanupStack::PopAndDestroy(3);
1.155 +/* CleanupStack::Pop();
1.156 + Fs.Close();
1.157 +*/ }
1.158 +
1.159 +#if defined(__GCC32__)
1.160 +typedef wchar_t __TText;
1.161 +#elif defined(__VC32__)
1.162 +typedef TUint16 __TText;
1.163 +#elif defined(__CW32__)
1.164 +typedef TUint16 __TText;
1.165 +#elif !defined(__TText_defined)
1.166 +#error no typedef for __TText
1.167 +#endif
1.168 +const __TText* KShortData1252 = L"\x20AC\u00FC\u00E9";
1.169 +const __TText* KShortData850 = L"\u00C7\u00FC\u00E9";
1.170 +const __TText* KLongData1252 = L"\u00E7\u00F8d\u00EB\u00DE\u00E3g\u00EA1252";
1.171 +const __TText* KLongData850 = L"\u00E7\u00F6d\u00EBp\u00E4g\u00EA850";
1.172 +const __TText* KShortWin2K1252 = L"CP1252Win2kShort.zip"; // created on win2k with winzip
1.173 +const __TText* KShortWinXP1252 = L"CP1252WinXPShort.zip"; // created on XP with winzip
1.174 +const __TText* KShortWin2K850 = L"CP850Win2kShort.zip";
1.175 +const __TText* KShortWinXPMS850= L"CP850WinXPMSShort.zip";// created on XP with Compressed Folders
1.176 +const __TText* KShortWinXP850 = L"CP850WinXPShort.zip";
1.177 +const __TText* KLongWin2K = L"CPMixWin2kLong.zip";
1.178 +const __TText* KLongWinXP = L"CPMixWinXPLong.zip";
1.179 +const __TText* KLongWinXPMS = L"CPMixWinXPMSLong.zip";
1.180 +struct TUnzipResultsArray
1.181 + {
1.182 + const __TText* iZipFileName;
1.183 + TInt iZipFileMemberIndex;
1.184 + const __TText* iZipFileMemberExpectedName;
1.185 + };
1.186 +const TUnzipResultsArray KUnzipResults[] =
1.187 + {
1.188 + {KShortWin2K1252, 1, KShortData1252},
1.189 + {KShortWinXP1252, 1, KShortData1252},
1.190 + {KShortWin2K850, 1, KShortData850},
1.191 + {KShortWinXPMS850,1, KShortData850},
1.192 + {KShortWinXP850, 1, KShortData850},
1.193 + {KLongWin2K, 1, KLongData1252},
1.194 + {KLongWin2K, 2, KLongData850},
1.195 + {KLongWinXP, 1, KLongData850},
1.196 + {KLongWinXP, 2, KLongData1252},
1.197 + {KLongWinXPMS, 1, KLongData850},
1.198 + {KLongWinXPMS, 2, KLongData1252}
1.199 + };
1.200 +void TestEntry(const TDesC16& aFileName, TInt aIndex, const TDesC16& aEntryName)
1.201 +// Test for correct decoding of filenames containing extended characters (DEF083102)
1.202 +// Cycle through data defined above looking for matching filenames and data
1.203 + {
1.204 + for (TInt i = 0; i != sizeof(KUnzipResults)/sizeof(KUnzipResults[0]); ++i)
1.205 + {
1.206 + TPtrC filename(reinterpret_cast<const TText16*>(KUnzipResults[i].iZipFileName));
1.207 + if(aFileName==filename && aIndex==KUnzipResults[i].iZipFileMemberIndex)
1.208 + {
1.209 + TPtrC entryname(reinterpret_cast<const TText16*>(KUnzipResults[i].iZipFileMemberExpectedName));
1.210 + test.Next(_L("DEF083102"));
1.211 + test(aEntryName==entryname);
1.212 + }
1.213 + }
1.214 + }
1.215 +
1.216 +void CZipTest::TheZipTestL(RFs& aFs,
1.217 + TFileName* aFileName,
1.218 + RFileWriteStream& aStream)
1.219 + {
1.220 + CZipFile* ZipFile=0;
1.221 + CZipFileMember* Member=0;
1.222 + CZipFileMemberIterator* FileMembers;
1.223 +
1.224 + iConsole->Printf(_L("Test file: %S\r\n"), aFileName);
1.225 + TRAPD(err, ZipFile = CZipFile::NewL(aFs,*aFileName));
1.226 +
1.227 + if(err==KErrNoMemory)
1.228 + User::Leave(err); // leave here if error is no memory
1.229 +
1.230 + if (err!=KErrNone)
1.231 + {
1.232 + aStream.WriteL(KErrFileNotOpened);
1.233 + aStream.WriteL(KLineSpace);
1.234 +
1.235 + if (err == CZipArchive::KZipArchiveError)
1.236 + aStream.WriteL(_L("Error: Zip Archive Error.\r\n"));
1.237 + else if (err == CZipArchive::KCentralDirectoryTrailerNotFound)
1.238 + aStream.WriteL(_L("Error: Central Directory Trailer Not Found.\r\n"));
1.239 + else if (err == CZipArchive::KCentralDirectoryTrailerInvalid)
1.240 + aStream.WriteL(_L("Error: KCentral Directory Trailer Invalid.\r\n"));
1.241 + else if (err == CZipArchive::KCompressionMethodNotSupported)
1.242 + aStream.WriteL(_L("Error: Compression Method Not Supported.\r\n"));
1.243 + else if (err == CZipArchive::KMultiDiskArchivesNotSupported)
1.244 + aStream.WriteL(_L("Error: Multi Disk Archives Not Supported.\r\n"));
1.245 + else if (err == CZipArchive::KMemberNotFound)
1.246 + aStream.WriteL(_L("Error: Member Not Found.\r\n"));
1.247 + else if (err == CZipArchive::KZipArchiveMinError)
1.248 + aStream.WriteL(_L("Error: Zip Archive Min Error.\r\n"));
1.249 +
1.250 + User::Leave(err);
1.251 + }
1.252 + else
1.253 + {
1.254 + CleanupStack::PushL(ZipFile);
1.255 + aStream.WriteL(KOk);
1.256 + aStream.WriteL(KLineSpace);
1.257 + aStream.WriteL(KLineSpace);
1.258 + iConsole->Printf(KOpenedSuccessfully, aFileName);
1.259 +
1.260 + iNoFiles=EFalse;
1.261 +
1.262 + FileMembers = ZipFile->GetMembersL();
1.263 + CleanupStack::PushL(FileMembers);
1.264 + Member = FileMembers->NextL();
1.265 + TInt index=1;// have to provide this - the one in the iterator is private
1.266 + while (Member != NULL)
1.267 + {
1.268 + CleanupStack::PushL(Member);
1.269 + TBuf<16> CRC32;
1.270 + CRC32.NumUC((TUint)Member->CRC32(), EHex);
1.271 + TBuf<16> UnComp;
1.272 + UnComp.NumUC((TUint)Member->UncompressedSize());
1.273 + TBuf<16> Comp;
1.274 + Comp.NumUC((TUint)Member->CompressedSize());
1.275 + aStream.WriteL(KZipName);
1.276 + aStream.WriteL(*Member->Name());
1.277 + aStream.WriteL(KLineSpace);
1.278 + aStream.WriteL(KZipCRC32);
1.279 + aStream.WriteL(CRC32);
1.280 + aStream.WriteL(KLineSpace);
1.281 + aStream.WriteL(KZipCSize);
1.282 + aStream.WriteL(Comp);
1.283 + aStream.WriteL(KLineSpace);
1.284 + aStream.WriteL(KZipUCSize);
1.285 + aStream.WriteL(UnComp);
1.286 + aStream.WriteL(KLineSpace);
1.287 + aStream.CommitL();
1.288 + ExtractFileL(aFs, Member, ZipFile, aFileName, aStream);
1.289 + TestEntry(*aFileName,index++,*Member->Name());
1.290 + aFs.SetSessionPath(KPath);
1.291 + CleanupStack::PopAndDestroy(Member);
1.292 + Member = FileMembers->NextL();
1.293 + }
1.294 + CleanupStack::PopAndDestroy(FileMembers);
1.295 + CleanupStack::PopAndDestroy(ZipFile);
1.296 + }
1.297 +}
1.298 +
1.299 +void CZipTest::ExtractFileL(RFs& aFs,
1.300 + const CZipFileMember* aMember,
1.301 + CZipFile* aZipFile,
1.302 + const TFileName* aFileName,
1.303 + RFileWriteStream& aStream)
1.304 + {
1.305 + TInt loop=0;
1.306 + HBufC* name = aMember->Name()->AllocLC();
1.307 + while (loop<name->Length())
1.308 + {
1.309 + if ((*name)[loop] == '/')
1.310 + {
1.311 + name->Des()[loop] = '\\';
1.312 + }
1.313 + loop++;
1.314 + }
1.315 +
1.316 + TFileName fn;
1.317 + fn.Append(KExtractZipPath);
1.318 + fn.Append(*aFileName);
1.319 + fn.Append('\\');
1.320 + fn.Append(*name);
1.321 +//
1.322 + RFile expandedMember;
1.323 + CleanupClosePushL(expandedMember);
1.324 +//
1.325 + aFs.SetSessionPath(KExtractZipPath);
1.326 + aFs.MkDirAll(fn);
1.327 + TInt retVal = expandedMember.Replace(aFs,fn, EFileShareAny|EFileWrite);
1.328 + if(retVal != KErrNone)
1.329 + {
1.330 + //Could be a directory name which we are planning to write to.
1.331 + CleanupStack::PopAndDestroy(2,name); //expanded member, name
1.332 + expandedMember.Close();
1.333 + return;
1.334 + }
1.335 +
1.336 +//
1.337 + RZipFileMemberReaderStream* fileStream;
1.338 + TInt error = aZipFile->GetInputStreamL(aMember, fileStream);
1.339 + if (error != KErrNone)
1.340 + {
1.341 + _LIT(KCompressionNotSupported, "Error: Compression Method Not Supported");
1.342 + aStream.WriteL(KCompressionNotSupported);
1.343 + aStream.WriteL(KLineSpace);
1.344 + CleanupStack::PopAndDestroy(2,name); //expanded member and name
1.345 + aFs.Delete(fn);
1.346 + return;
1.347 + }
1.348 + CleanupStack::PushL(fileStream);
1.349 +//
1.350 + TUint32 size = aMember->UncompressedSize();
1.351 + HBufC8* bytes = HBufC8::New(size);
1.352 + CleanupStack::PushL(bytes);
1.353 + TPtr8 ptr = bytes->Des();
1.354 + User::LeaveIfError(fileStream->Read(ptr,size));
1.355 + User::LeaveIfError(expandedMember.Write(ptr));
1.356 + CleanupStack::PopAndDestroy(4,name); // bytes, fileStream, expanded member and name
1.357 + expandedMember.Close();
1.358 + }
1.359 +
1.360 +static void executeUiL()
1.361 + {
1.362 + CZipTest* ziptest=new (ELeave) CZipTest;
1.363 + CleanupStack::PushL(ziptest);
1.364 + ziptest->ExecuteL();
1.365 + CleanupStack::PopAndDestroy();
1.366 + }
1.367 +/**
1.368 +@SYMTestCaseID SYSLIB-EZLIB2-UT-4299
1.369 +@SYMTestCaseDesc Test CZipFile functionality like open, close, size of the file.
1.370 +@SYMTestPriority High
1.371 +@SYMTestActions Creates a new CZipFile object using the supplied file server session and a valid file handle using CZipFile::NewL()
1.372 + Open zip file using CZipFile::OpenL()
1.373 + Check the size of the zip file using CZipFile::Size()
1.374 + Close zip file using CZipFile::Close()
1.375 +@SYMTestExpectedResults The test succeeds with no errors i.e. KErrNone
1.376 +@SYMDEF REQ8024
1.377 +*/
1.378 +static void TestCZipFileMethodsL()
1.379 + {
1.380 + test.Next(_L(" @SYMTestCaseID:SYSLIB-EZLIB2-UT-4299 "));
1.381 + RFs rFs;
1.382 +
1.383 + User::LeaveIfError(rFs.Connect()); //Connect to file session
1.384 + CleanupClosePushL(rFs);
1.385 + User::LeaveIfError(rFs.SetSessionPath(KPath)); //Set Session Path to direcrt containing test zip files
1.386 +
1.387 + TFileName fileName(_L("rfc2459.zip"));
1.388 +
1.389 + test.Printf(_L("Test file: %S\r\n"), &fileName);
1.390 +
1.391 + CZipFile* ZipFile = 0;
1.392 + TRAPD(err, ZipFile = CZipFile::NewL(rFs, fileName));
1.393 +
1.394 + CleanupStack::PushL(ZipFile);
1.395 +
1.396 + TRAP(err, ZipFile->OpenL());
1.397 + test(err == KErrNone);
1.398 +
1.399 + TInt fileSize;
1.400 + TRAP(err, ZipFile->Size(fileSize));
1.401 + test(err == KErrNone && fileSize > 0);
1.402 +
1.403 + TRAP(err, ZipFile->Close());
1.404 + test(err == KErrNone);
1.405 +
1.406 + CleanupStack::PopAndDestroy(2);// rFs, ZipFile
1.407 + }
1.408 +
1.409 +GLDEF_C TInt E32Main() // main function called by E32
1.410 + {
1.411 + test.Title();
1.412 + __UHEAP_MARK; // mark heap state
1.413 + test.Start(_L("Test ZIP"));
1.414 + CTrapCleanup* cleanup=CTrapCleanup::New(); // get clean-up stack
1.415 + TRAPD(error, executeUiL()); // do most stuff under cleanup stack
1.416 + test(error==KErrNone);
1.417 + test.Next(_L("Test CZipFile functionality like open, close, size of the file"));
1.418 + TRAP(error, TestCZipFileMethodsL());
1.419 + test.End();
1.420 + delete cleanup; // destroy clean-up stack
1.421 + __UHEAP_MARKEND; // check no memory leak
1.422 + test.Close();
1.423 + return 0; // and return
1.424 + }
1.425 +