📄 test.cpp
字号:
#include <windows.h>
#include <tchar.h>
#include <stdio.h>
#include "../../zip.h"
#include "../../unzip.h"
// This program runs a bunch of test cases on zip/unzip.
// Most of the test cases come from bugs that had been reported
// in an earlier version of the ziputils, to be sure that they're fixed.
bool tsame(const FILETIME t0, const FILETIME t1)
{ if (t0.dwHighDateTime!=t1.dwHighDateTime) return false;
if ( (t0.dwLowDateTime>>28) != (t1.dwLowDateTime>>28)) return false;
return true;
// we allow some flexibility in the lower bits. That's because zip's don't store times with as much precision.
}
bool fsame(const TCHAR *fn0, const TCHAR *fn1)
{ HANDLE hf0=CreateFile(fn0,GENERIC_READ,FILE_SHARE_READ,0,OPEN_EXISTING,0,0);
HANDLE hf1=CreateFile(fn1,GENERIC_READ,FILE_SHARE_READ,0,OPEN_EXISTING,0,0);
if (hf0==INVALID_HANDLE_VALUE || hf1==INVALID_HANDLE_VALUE)
{ if (hf0!=INVALID_HANDLE_VALUE) CloseHandle(hf0);
if (hf1!=INVALID_HANDLE_VALUE) CloseHandle(hf1);
return false;
}
DWORD size0=GetFileSize(hf0,0), size1=GetFileSize(hf1,0);
if (size0!=size1) {CloseHandle(hf0); CloseHandle(hf1); return false;}
DWORD size=size0;
//
char *buf[2]; buf[0]=new char[16384]; buf[1]=new char[16384];
DWORD done=0;
while (done<size)
{ DWORD left=size-done; if (left>16384) left=16384;
DWORD red; ReadFile(hf0,buf[0],left,&red,0); ReadFile(hf1,buf[1],left,&red,0);
if (memcmp(buf[0],buf[1],left)!=0) break;
done+=left;
}
delete[] buf[0]; delete[] buf[1];
CloseHandle(hf0); CloseHandle(hf1);
return (done==size);
}
void SaveResource(const TCHAR *res, const TCHAR *fn)
{ HINSTANCE hInstance = GetModuleHandle(0);
HRSRC hrsrc=FindResource(hInstance,res,RT_RCDATA);
HANDLE hglob = LoadResource(hInstance,hrsrc);
void *buf=LockResource(hglob);
unsigned int len=SizeofResource(hInstance,hrsrc);
HANDLE hf = CreateFile(fn,GENERIC_WRITE,0,0,CREATE_ALWAYS,FILE_ATTRIBUTE_NORMAL,0);
DWORD writ; WriteFile(hf,buf,len,&writ,0);
CloseHandle(hf);
}
bool any_errors=false; bool p_abort=false;
void msg(const TCHAR *s)
{ if (s[0]=='*') any_errors=true;
#ifdef UNDER_CE
int res=IDOK;
if (s[0]=='*') res=MessageBox(0,s,_T("Zip error"), MB_ICONERROR|MB_OKCANCEL);
else if (s[0]=='.') MessageBeep(0);
else MessageBox(0,s,_T("Zip test"),MB_OKCANCEL);
if (res==IDCANCEL) p_abort=true;
#else
_tprintf(_T("%s\n"),s);
#endif
}
void main()
{ HZIP hz; HANDLE hf; DWORD writ; ZRESULT zr; ZIPENTRY ze; TCHAR m[1024];
bool fast=false;
#ifdef UNDER_CE
fast=true;
#endif
#ifdef __CODEGUARD__
fast=true;
#endif
msg(_T("Zip-utils tests. Files will be left in \"\\z\""));
CreateDirectory(_T("\\z"),0);
SaveResource(MAKEINTRESOURCE(1),_T("\\z\\extra.zip"));
SaveResource(MAKEINTRESOURCE(2),_T("\\z\\ce2ce.jpg"));
SaveResource(MAKEINTRESOURCE(3),_T("\\z\\ce2ce.txt"));
// fixed bug: OpenZip errors and returns0 when you try to open a zip with no files in it
msg(_T("empty - testing whether it fails to open empty zipfiles"));
hz=CreateZip(_T("\\z\\empty.zip"),0); if (hz==0) msg(_T("* Failed to create empty.zip"));
zr=CloseZip(hz); if (zr!=ZR_OK) msg(_T("* Failed to close empty.zip"));
if (p_abort) return;
hz=OpenZip(_T("\\z\\empty.zip"),0); if (hz==0) msg(_T("* Failed to open empty.zip"));
zr=GetZipItem(hz,-1,&ze); if (zr!=ZR_OK) msg(_T("* Failed to get empty.zip index"));
zr=CloseZip(hz); if (zr!=ZR_OK) msg(_T("* Failed to close empty.zip"));
if (p_abort) return;
// fixed bug: IsZipHandle should return false for a NULL handle.
msg(_T("IsZipHandle - testing whether 0 is considered a handle"));
bool b = IsZipHandleZ(0) || IsZipHandleU(0);
if (b) msg(_T("IsZipHandle failed to deny handlehood of NULL"));
if (p_abort) return;
// fixed bug: if one file is bigger then the following must be smaller than 64k -- diff. between release and debug mode
// fixed bug: test0=71k, test1=152.2k, test2=145b, test3=120k, here test3 returns ZR_WRITE
msg(_T("sizes - testing whether large-then-small files work okay"));
{ char *c=new char[200*1024]; for (int i=0; i<200*1024; i++) c[i]=(char)(rand()%255);
hf=CreateFile(_T("\\z\\sizes-71k.dat"),GENERIC_WRITE,0,0,CREATE_ALWAYS,FILE_ATTRIBUTE_NORMAL,0); WriteFile(hf,c,71*1024,&writ,0); CloseHandle(hf);
hf=CreateFile(_T("\\z\\sizes-152_2k.dat"),GENERIC_WRITE,0,0,CREATE_ALWAYS,FILE_ATTRIBUTE_NORMAL,0); WriteFile(hf,c,152*1024+1024/5,&writ,0); CloseHandle(hf);
hf=CreateFile(_T("\\z\\sizes-145b.dat"),GENERIC_WRITE,0,0,CREATE_ALWAYS,FILE_ATTRIBUTE_NORMAL,0); WriteFile(hf,c,145,&writ,0); CloseHandle(hf);
hf=CreateFile(_T("\\z\\sizes-120k.dat"),GENERIC_WRITE,0,0,CREATE_ALWAYS,FILE_ATTRIBUTE_NORMAL,0); WriteFile(hf,c,120*1024,&writ,0); CloseHandle(hf);
delete[] c;
}
//
hz=CreateZip(_T("\\z\\sizes.zip"),0); if (hz==0) msg(_T("* Failed to create sizes.zip"));
zr=ZipAdd(hz,_T("sizes-71k.out.dat"),_T("\\z\\sizes-71k.dat")); if (zr!=ZR_OK) msg(_T("* Failed to add 71k"));
zr=ZipAdd(hz,_T("sizes-152_2k.out.dat"),_T("\\z\\sizes-152_2k.dat")); if (zr!=ZR_OK) msg(_T("* Failed to add 152_2k"));
zr=ZipAdd(hz,_T("sizes-145b.out.dat"),_T("\\z\\sizes-145b.dat")); if (zr!=ZR_OK) msg(_T("* Failed to add 145b"));
zr=ZipAdd(hz,_T("sizes-120k.out.dat"),_T("\\z\\sizes-120k.dat")); if (zr!=ZR_OK) msg(_T("* Failed to add 120k"));
zr=CloseZip(hz); if (zr!=ZR_OK) msg(_T("* Failed to close sizes.zip"));
if (p_abort) return;
hz=OpenZip(_T("\\z\\sizes.zip"),0); if (hz==0) msg(_T("* Failed to open sizes.zip"));
zr=UnzipItem(hz,0,_T("\\z\\sizes-71k.out.dat")); if (zr!=ZR_OK) msg(_T("* Failed to unzip 71k"));
if (!fsame(_T("\\z\\sizes-71k.dat"),_T("\\z\\sizes-71k.out.dat"))) msg(_T("* Failed to unzip all of 71k"));
zr=UnzipItem(hz,1,_T("\\z\\sizes-152_2k.out.dat")); if (zr!=ZR_OK || !fsame(_T("\\z\\sizes-152_2k.dat"),_T("\\z\\sizes-152_2k.out.dat"))) msg(_T("* Failed to unzip 152_2k"));
zr=UnzipItem(hz,2,_T("\\z\\sizes-145b.out.dat")); if (zr!=ZR_OK || !fsame(_T("\\z\\sizes-145b.dat"),_T("\\z\\sizes-145b.out.dat"))) msg(_T("* Failed to unzip 145b"));
zr=UnzipItem(hz,3,_T("\\z\\sizes-120k.out.dat")); if (zr!=ZR_OK || !fsame(_T("\\z\\sizes-120k.dat"),_T("\\z\\sizes-120k.out.dat"))) msg(_T("* Failed to unzip 120k"));
zr=CloseZip(hz); if (zr!=ZR_OK) msg(_T("* Failed to close sizes.zip"));
if (p_abort) return;
// fixed bug: if file-size is multiple of internal bufzip, 16k, then last buffer is not saved. Due to unzReadCurrentFile. When buf full and EOF is true at same time, EOF is returned. Could have extra parameter in unzReadCurrentFile.
// fixed bug: Otherwise unzipping into memory never works! (always fails, returns 0). if (err==Z_STREAM_END) return (iRead==0)?UNSZ_EOF:iRead; --> if (err==Z_STREAM_END) return (iRead==len)?UNZ_EOF:iRead.
msg(_T("unztomem - testing whether ZR_OK is returned upon successfull unzip to memory"));
{ char *c=new char[10240]; for (int i=0; i<10240; i++) c[i]=(char)(rand()%255);
hf=CreateFile(_T("\\z\\unztomem-10k24.dat"),GENERIC_WRITE,0,0,CREATE_ALWAYS,FILE_ATTRIBUTE_NORMAL,0); WriteFile(hf,c,10240,&writ,0); CloseHandle(hf);
hf=CreateFile(_T("\\z\\unztomem-10k00.dat"),GENERIC_WRITE,0,0,CREATE_ALWAYS,FILE_ATTRIBUTE_NORMAL,0); WriteFile(hf,c,10000,&writ,0); CloseHandle(hf);
delete[] c;
}
hz=CreateZip(_T("\\z\\unztomem.zip"),0); if (hz==0) msg(_T("* Failed to create unztomem.zip"));
zr=ZipAdd(hz,_T("unztomem-10k24.dat"),_T("\\z\\unztomem-10k24.dat")); if (zr!=ZR_OK) msg(_T("* Failed to add unztomem-10k24.dat"));
zr=ZipAdd(hz,_T("unztomem-10k00.dat"),_T("\\z\\unztomem-10k00.dat")); if (zr!=ZR_OK) msg(_T("* Failed to add unztomem-10k00.dat"));
zr=CloseZip(hz); if (zr!=ZR_OK) msg(_T("* Failed to close unztomem.zip"));
if (p_abort) return;
// test unzipping a round multiple into smaller buffer
{ hz=OpenZip(_T("\\z\\unztomem.zip"),0); if (hz==0) msg(_T("* Failed to open unztomem.zip"));
GetZipItem(hz,0,&ze); char buf[1024]; zr=ZR_MORE; long totsize=0;
while (zr==ZR_MORE)
{ zr=UnzipItem(hz,0,buf,1024); if (zr!=ZR_MORE && zr!=ZR_OK) msg(_T("* Failed to unzip a buffer of unztomem-10k24"));
long curbuf = (zr==ZR_MORE) ? 1024 : ze.unc_size-totsize;
totsize += curbuf;
}
zr=CloseZip(hz); if (zr!=ZR_OK) msg(_T("* Failed to close unztomem.zip"));
if (totsize!=ze.unc_size) msg(_T("* Failed to unzip all of unztomem-10k24 to mem"));
}
if (p_abort) return;
// unzip a non-round multiple into a smaller buffer
{ hz=OpenZip(_T("\\z\\unztomem.zip"),0); if (hz==0) msg(_T("* Failed to open unztomem.zip"));
GetZipItem(hz,1,&ze); char buf[1024]; zr=ZR_MORE; long totsize=0;
while (zr==ZR_MORE)
{ zr=UnzipItem(hz,1,buf,1024); if (zr!=ZR_MORE && zr!=ZR_OK) msg(_T("* Failed to unzip a buffer of unztomem-10k00.zip"));
long curbuf = (zr==ZR_MORE) ? 1024 : ze.unc_size-totsize;
totsize += curbuf;
}
zr=CloseZip(hz); if (zr!=ZR_OK) msg(_T("* Failed to close unztomem.zip"));
if (totsize!=ze.unc_size) msg(_T("* Failed to unzip all of unztomem-10k00 to mem"));
}
if (p_abort) return;
// unzip a round multiple into a bigger buffer
{ hz=OpenZip(_T("\\z\\unztomem.zip"),0); if (hz==0) msg(_T("* Failed to open unztomem.zip"));
char buf[16384];
zr=UnzipItem(hz,0,buf,16384); if (zr!=ZR_OK) msg(_T("* Failed to unzip the buffer of unztomem-10k24"));
zr=CloseZip(hz); if (zr!=ZR_OK) msg(_T("* Failed to close unztomem.zip"));
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -