From e1ad1a02d2502f71860e17b484970f6e983629ac Mon Sep 17 00:00:00 2001 From: michael Date: Sun, 19 Aug 2012 22:08:32 +0000 Subject: [PATCH] * 2 test cases from Reinier Olislagers git-svn-id: trunk@22138 - --- .gitattributes | 2 + packages/paszlib/tests/tconend.pp | 152 +++++++++++++++++++++++++++++ packages/paszlib/tests/tczipper.pp | 144 +++++++++++++++++++++++++++ 3 files changed, 298 insertions(+) create mode 100644 packages/paszlib/tests/tconend.pp create mode 100644 packages/paszlib/tests/tczipper.pp diff --git a/.gitattributes b/.gitattributes index c6bd8f4779..e70c097453 100644 --- a/.gitattributes +++ b/.gitattributes @@ -5840,6 +5840,8 @@ packages/paszlib/src/zipper.pp svneol=native#text/plain packages/paszlib/src/ziputils.pas svneol=native#text/plain packages/paszlib/src/zstream.pp svneol=native#text/plain packages/paszlib/src/zuncompr.pas svneol=native#text/plain +packages/paszlib/tests/tconend.pp svneol=native#text/plain +packages/paszlib/tests/tczipper.pp svneol=native#text/plain packages/pcap/Makefile svneol=native#text/plain packages/pcap/Makefile.fpc svneol=native#text/plain packages/pcap/Makefile.fpc.fpcmake svneol=native#text/plain diff --git a/packages/paszlib/tests/tconend.pp b/packages/paszlib/tests/tconend.pp new file mode 100644 index 0000000000..6c0c146198 --- /dev/null +++ b/packages/paszlib/tests/tconend.pp @@ -0,0 +1,152 @@ +{ + This file is part of the Free Pascal packages. + Copyright (c) 1999-2012 by the Free Pascal development team + + Tests zip/unzip functionality provided by the FPC zipper.pp unit. + Created by Reinier Olislagers + + See the file COPYING.FPC, included in this distribution, + for details about the copyright. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. + + **********************************************************************} +program tconend; + +{$mode objfpc}{$h+} + +uses SysUtils, classes, zipper, md5; + +type + TCallBackHandler = class(TObject) + public + procedure EndOfFile(Sender:TObject; const Ratio:double); + procedure StartOfFile(Sender:TObject; const AFileName:string); + end; + + +procedure TCallBackHandler.EndOfFile(Sender : TObject; Const Ratio : Double); +begin + writeln('End of file handler hit; ratio: '+floattostr(ratio)); + if (Ratio<0) then + begin + writeln('Found compression ratio '+floattostr(Ratio)+', which should never be lower than 0.'); + halt(3); + end; +end; + +procedure TCallBackHandler.StartOfFile(Sender : TObject; Const AFileName : String); +begin + writeln('Start of file handler hit; filename: '+AFileName); + if AFileName='' then + begin + writeln('Archive filename should not be empty.'); + halt(4); + end; +end; + +var + code: cardinal; + CallBackHandler: TCallBackHandler; + CompressedFile: string; + FileContents: TStringList; + UncompressedFile1: string; + UncompressedFile1Hash: string; + UncompressedFile2: string; + UncompressedFile2Hash: string; + OurZipper: TZipper; + UnZipper: TUnZipper; +begin + code := 0; + UncompressedFile1:=SysUtils.GetTempFileName('', 'UNC'); + UncompressedFile2:=SysUtils.GetTempFileName('', 'UNC'); + CompressedFile:=SysUtils.GetTempFileName('', 'ZP'); + + FileContents:=TStringList.Create; + OurZipper:=TZipper.Create; + UnZipper:=TUnZipper.Create; + CallBackHandler:=TCallBackHandler.Create; + try + // Set up uncompressed files + FileContents.Add('This is an uncompressed file.'); + FileContents.Add('And another line.'); + FileContents.SaveToFile(UncompressedFile1); + FileContents.Clear; + FileContents.Add('Have you looked into using fpcup today?'); + FileContents.Add('It works nicely with fpc and goes well with a fruity red wine, too.'); + FileContents.SaveToFile(UncompressedFile2); + // Remember their content, so we can compare later. + UncompressedFile1Hash:=MD5Print(MD5File(UncompressedFile1, MDDefBufSize)); + UncompressedFile2Hash:=MD5Print(MD5File(UncompressedFile2, MDDefBufSize)); + + // Test zip functionality. + writeln('Beginning zip test'); + OurZipper.FileName:=CompressedFile; + // Add the files only with their filenames, we don't want to create + // subdirectories: + OurZipper.Entries.AddFileEntry(UncompressedFile1,ExtractFileName(UncompressedFile1)); + OurZipper.Entries.AddFileEntry(UncompressedFile2,ExtractFileName(UncompressedFile2)); + OurZipper.OnStartFile:=@CallBackHandler.StartOfFile; + OurZipper.OnEndFile:=@CallBackHandler.EndOfFile; + OurZipper.ZipAllFiles; + if not FileExists(CompressedFile) then + begin + writeln('Zip file was not created.'); + halt(5); + end; + writeln('Zip test done'); + + // Delete original files + DeleteFile(UncompressedFile1); + DeleteFile(UncompressedFile2); + + // Now unzip + writeln('Beginning unzip test'); + Unzipper.FileName:=CompressedFile; + Unzipper.OutputPath:=ExtractFilePath(UncompressedFile1); + UnZipper.OnStartFile:=@CallBackHandler.StartOfFile; + UnZipper.OnEndFile:=@CallBackHandler.EndOfFile; + Unzipper.Examine; + Unzipper.UnZipAllFiles; + + // Now we should have the uncompressed files again + if (not FileExists(UncompressedFile1)) or + (not FileExists(UncompressedFile2)) then + begin + writeln('Unzip failed: could not find decompressed files.'); + halt(6); + end; + + // Compare hashes + if + (UncompressedFile1Hash<>MD5Print(MD5File(UncompressedFile1, MDDefBufSize))) + or + (UncompressedFile2Hash<>MD5Print(MD5File(UncompressedFile2, MDDefBufSize))) + then + begin + writeln('Unzip failed: uncompressed files are not the same as the originals.'); + halt(7); + end; + writeln('Unzip test done'); + + if code = 0 then + writeln('Basic zip/unzip tests passed') + else + writeln('Basic zip/unzip test failed: ', code); + finally + FileContents.Free; + CallBackHandler.Free; + OurZipper.Free; + UnZipper.Free; + try + if FileExists(CompressedFile) then DeleteFile(CompressedFile); + if FileExists(UncompressedFile1) then DeleteFile(UncompressedFile1); + if FileExists(UncompressedFile2) then DeleteFile(UncompressedFile2); + finally + // Ignore errors; operating system should clean out temp files + end; + end; + Halt(code); +end. diff --git a/packages/paszlib/tests/tczipper.pp b/packages/paszlib/tests/tczipper.pp new file mode 100644 index 0000000000..487e009fab --- /dev/null +++ b/packages/paszlib/tests/tczipper.pp @@ -0,0 +1,144 @@ +program tczipper; +{ + This file is part of the Free Pascal packages. + Copyright (c) 1999-2012 by the Free Pascal development team + + Tests zip/unzip functionality provided by the FPC zipper.pp unit. + + See the file COPYING.FPC, included in this distribution, + for details about the copyright. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. + + **********************************************************************} +{$mode objfpc}{$h+} + +uses SysUtils, classes, zipper, md5; + +type + TCallBackHandler = class(TObject) + public + procedure EndOfFile(Sender:TObject; const Ratio:double); + procedure StartOfFile(Sender:TObject; const AFileName:string); + end; + + +procedure TCallBackHandler.EndOfFile(Sender : TObject; Const Ratio : Double); +begin + if (Ratio<0) then + begin + writeln('Found compression ratio '+floattostr(Ratio)+', which should never be lower than 0.'); + halt(3); + end; +end; + +procedure TCallBackHandler.StartOfFile(Sender : TObject; Const AFileName : String); +begin + if AFileName='' then + begin + writeln('Archive filename should not be empty.'); + halt(4); + end; +end; + +var + code: cardinal; + CallBackHandler: TCallBackHandler; + CompressedFile: string; + FileContents: TStringList; + UncompressedFile1: string; + UncompressedFile1Hash: string; + UncompressedFile2: string; + UncompressedFile2Hash: string; + OurZipper: TZipper; + UnZipper: TUnZipper; +begin + code := 0; + UncompressedFile1:=SysUtils.GetTempFileName('', 'UNC'); + UncompressedFile2:=SysUtils.GetTempFileName('', 'UNC'); + CompressedFile:=SysUtils.GetTempFileName('', 'ZP'); + + FileContents:=TStringList.Create; + OurZipper:=TZipper.Create; + UnZipper:=TUnZipper.Create; + CallBackHandler:=TCallBackHandler.Create; + try + // Set up uncompressed files + FileContents.Add('This is an uncompressed file.'); + FileContents.Add('And another line.'); + FileContents.SaveToFile(UncompressedFile1); + FileContents.Clear; + FileContents.Add('Have you looked into using fpcup today?'); + FileContents.Add('It works nicely with fpc and goes well with a fruity red wine, too.'); + FileContents.SaveToFile(UncompressedFile2); + // Remember their content, so we can compare later. + UncompressedFile1Hash:=MD5Print(MD5File(UncompressedFile1, MDDefBufSize)); + UncompressedFile2Hash:=MD5Print(MD5File(UncompressedFile2, MDDefBufSize)); + + // Test zip functionality. + OurZipper.FileName:=CompressedFile; + // Add the files only with their filenames, we don't want to create + // subdirectories: + OurZipper.Entries.AddFileEntry(UncompressedFile1,ExtractFileName(UncompressedFile1)); + OurZipper.Entries.AddFileEntry(UncompressedFile2,ExtractFileName(UncompressedFile2)); + OurZipper.OnStartFile:=@CallBackHandler.StartOfFile; + OurZipper.OnEndFile:=@CallBackHandler.EndOfFile; + OurZipper.ZipAllFiles; + if not FileExists(CompressedFile) then + begin + writeln('Zip file was not created.'); + halt(5); + end; + + // Delete original files + DeleteFile(UncompressedFile1); + DeleteFile(UncompressedFile2); + + // Now unzip + Unzipper.FileName:=CompressedFile; + Unzipper.OutputPath:=ExtractFilePath(UncompressedFile1); + UnZipper.OnStartFile:=@CallBackHandler.StartOfFile; + UnZipper.OnEndFile:=@CallBackHandler.EndOfFile; + Unzipper.Examine; + Unzipper.UnZipAllFiles; + + // Now we should have the uncompressed files again + if (not FileExists(UncompressedFile1)) or + (not FileExists(UncompressedFile2)) then + begin + writeln('Unzip failed: could not find decompressed files.'); + halt(6); + end; + + // Compare hashes + if + (UncompressedFile1Hash<>MD5Print(MD5File(UncompressedFile1, MDDefBufSize))) + or + (UncompressedFile2Hash<>MD5Print(MD5File(UncompressedFile2, MDDefBufSize))) + then + begin + writeln('Unzip failed: uncompressed files are not the same as the originals.'); + halt(7); + end; + + if code = 0 then + writeln('Basic zip/unzip tests passed') + else + writeln('Basic zip/unzip test failed: ', code); + finally + FileContents.Free; + CallBackHandler.Free; + OurZipper.Free; + UnZipper.Free; + try + if FileExists(CompressedFile) then DeleteFile(CompressedFile); + if FileExists(UncompressedFile1) then DeleteFile(UncompressedFile1); + if FileExists(UncompressedFile2) then DeleteFile(UncompressedFile2); + finally + // Ignore errors; operating system should clean out temp files + end; + end; + Halt(code); +end.