zip_open test cases --HG-- branch : HEAD
diff --git a/regress/Makefile.am b/regress/Makefile.am index faa6bcb..6f4ecee 100644 --- a/regress/Makefile.am +++ b/regress/Makefile.am
@@ -1,11 +1,13 @@ -noinst_PROGRAMS=buffadd +noinst_PROGRAMS=buffadd open EXTRA_PROGRAMS=ziptest deltest +EXTRA_DIST=test.zip + deltest_SOURCES=deltest.c buffadd_SOURCES=buffadd.c ziptest_SOURCES=ziptest.c -TESTS=buffadd +TESTS=buffadd open INCLUDES=-I${top_srcdir}/lib LDADD=../lib/libzip.la
diff --git a/regress/buffadd.c b/regress/buffadd.c index a0d32bf..b024a15 100644 --- a/regress/buffadd.c +++ b/regress/buffadd.c
@@ -1,3 +1,41 @@ +/* + $NiH$ + + buffadd.c -- test cases for adding files from buffer + Copyright (C) 1999, 2003 Dieter Baron and Thomas Klausner + + This file is part of libzip, a library to manipulate ZIP archives. + The authors can be contacted at <nih@giga.or.at> + + Redistribution and use in source and binary forms, with or without + modification, are permitted provided that the following conditions + are met: + 1. Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. + 2. Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in + the documentation and/or other materials provided with the + distribution. + 3. The names of the authors may not be used to endorse or promote + products derived from this software without specific prior + written permission. + + THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS + OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY + DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE + GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER + IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR + OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN + IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +*/ + + + +#include <errno.h> #include <stdio.h> #include <stdlib.h> #include <string.h> @@ -13,44 +51,47 @@ { struct zip *z; struct zip_file *ze; + int err; char buf[2000]; remove(testzip); - if ((z=zip_open(testzip, ZIP_CREATE)) == NULL) { + if ((z=zip_open(testzip, ZIP_CREATE, &err)) == NULL) { + zip_error_str(buf, sizeof(buf), err, errno); fprintf(stderr,"%s: can't open zipfile %s: %s\n", argv[0], - testzip, zip_err_str[zip_err]); + testzip, buf); exit(1); } if (zip_add_data(z, testname, NULL, teststr, strlen(teststr), 0)==-1) { fprintf(stderr,"%s: can't add buffer '%s': %s\n", argv[0], - teststr, zip_err_str[zip_err]); + teststr, zip_strerror(z)); exit(1); } if (zip_close(z) == -1) { - fprintf(stderr,"%s: can't close zipfile %s: %s\n", argv[0], - testzip, zip_err_str[zip_err]); + fprintf(stderr,"%s: can't close zipfile %s\n", argv[0], + testzip); exit(1); } - if ((z=zip_open(testzip, ZIP_CHECKCONS))==NULL) { + if ((z=zip_open(testzip, ZIP_CHECKCONS, &err))==NULL) { + zip_error_str(buf, sizeof(buf), err, errno); fprintf(stderr,"%s: can't re-open zipfile %s: %s\n", argv[0], - testzip, zip_err_str[zip_err]); + testzip, buf); exit(1); } if ((ze=zip_fopen(z, testname, 0))==NULL) { fprintf(stderr,"%s: can't fopen file '%s' in '%s': %s\n", argv[0], - testname, testzip, zip_err_str[zip_err]); + testname, testzip, zip_strerror(z)); exit(1); } if (zip_fread(ze, buf, 2000) < 0) { fprintf(stderr,"%s: can't read from '%s' in zipfile '%s': %s\n", - argv[0], testname, testzip, zip_err_str[zip_err]); + argv[0], testname, testzip, zip_file_strerror(ze)); exit(1); } @@ -61,8 +102,8 @@ } if (zip_close(z) == -1) { - fprintf(stderr,"%s: can't close zipfile %s: %s\n", argv[0], - testzip, zip_err_str[zip_err]); + fprintf(stderr,"%s: can't close zipfile %s\n", argv[0], + testzip); exit(1); }
diff --git a/regress/open.c b/regress/open.c new file mode 100644 index 0000000..419e09d --- /dev/null +++ b/regress/open.c
@@ -0,0 +1,116 @@ +/* + $NiH$ + + open.c -- test cases for opening zip archives + Copyright (C) 1999, 2003 Dieter Baron and Thomas Klausner + + This file is part of libzip, a library to manipulate ZIP archives. + The authors can be contacted at <nih@giga.or.at> + + Redistribution and use in source and binary forms, with or without + modification, are permitted provided that the following conditions + are met: + 1. Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. + 2. Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in + the documentation and/or other materials provided with the + distribution. + 3. The names of the authors may not be used to endorse or promote + products derived from this software without specific prior + written permission. + + THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS + OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY + DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE + GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER + IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR + OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN + IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +*/ + + + + +#include <stdio.h> +#include <stdlib.h> + +#include "zip.h" + +int open_fail(const char *fname, int flags, const char *desc, int err); +int open_success(const char *fname, int flags, const char *desc, int nent); + + + +int +main(int argc, char *argv[]) +{ + int fail; + + fail = 0; + + remove("nosuchfile"); + fail += open_fail("nosuchfile", 0, "non-existing", ZERR_NOENT); + fail += open_fail("Makefile", 0, "non-zip", ZERR_NOZIP); + fail += open_fail("test.zip", ZIP_EXCL, "existing-excl", ZERR_EXISTS); + /* ZERR_OPEN */ + /* ZERR_READ */ + /* ZERR_SEEK */ + /* ZERR_INCONS */ + + fail += open_success("test.zip", 0, "existing", 1); + fail += open_success("nosuchfile", ZIP_CREATE, "new", 0); + + exit(fail ? 1 : 0); +} + + + +int +open_fail(const char *fname, int flags, const char *desc, int err) +{ + struct zip *z; + int ze; + + if ((z=zip_open(fname, flags, &ze)) != NULL) { + printf("fail: opening %s succeeded\n", desc); + zip_close(z); + return 1; + } + else if (ze != err) { + printf("fail: opening %s returned wrong error %d, expected %d\n", + desc, ze, err); + return 1; + } + + return 0; +} + + + +int +open_success(const char *fname, int flags, const char *desc, int nent) +{ + struct zip *z; + int ze, num; + + if ((z=zip_open(fname, flags, &ze)) == NULL) { + printf("fail: opening %s failed (%d)\n", desc, ze); + return 1; + } + + num = zip_get_num_files(z); + zip_close(z); + + if (num != nent) { + printf("fail: opening %s got wrong number of files %d, expected %d\n", + desc, num, nent); + return 1; + } + + return 0; +}
diff --git a/regress/test.zip b/regress/test.zip new file mode 100644 index 0000000..69ff08a --- /dev/null +++ b/regress/test.zip Binary files differ