blob: 9080b8a8f425c086e8d89cfaad4c5ce0eba3039c [file] [log] [blame]
Dieter Baron90a8ca12005-01-11 21:18:51 +00001/*
Dieter Baron90a8ca12005-01-11 21:18:51 +00002 fread.c -- test cases for reading from zip archives
Dieter Baron10155932009-03-11 18:07:55 +01003 Copyright (C) 2004-2009 Dieter Baron and Thomas Klausner
Dieter Baron90a8ca12005-01-11 21:18:51 +00004
5 This file is part of libzip, a library to manipulate ZIP archives.
Dieter Baronb86c4332007-11-07 14:35:13 +01006 The authors can be contacted at <libzip@nih.at>
Dieter Baron90a8ca12005-01-11 21:18:51 +00007
8 Redistribution and use in source and binary forms, with or without
9 modification, are permitted provided that the following conditions
10 are met:
11 1. Redistributions of source code must retain the above copyright
12 notice, this list of conditions and the following disclaimer.
13 2. Redistributions in binary form must reproduce the above copyright
14 notice, this list of conditions and the following disclaimer in
15 the documentation and/or other materials provided with the
16 distribution.
17 3. The names of the authors may not be used to endorse or promote
18 products derived from this software without specific prior
19 written permission.
20
21 THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS
22 OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
23 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY
25 DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
27 GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28 INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
29 IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
30 OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
31 IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32*/
33
34
35
36#include <errno.h>
37#include <stdio.h>
38#include <stdlib.h>
39
40#include "zip.h"
41
Dieter Baron90a8ca12005-01-11 21:18:51 +000042enum when {
43 WHEN_NEVER, WHEN_OPEN, WHEN_READ, WHEN_CLOSE
44};
45
46const char *when_name[] = {
47 "no", "zip_fopen", "zip_fread", "zip_fclose"
48};
49
50int do_read(struct zip *, const char *, int, enum when, int, int);
51
52
53
Thomas Klausner2188b742005-07-16 17:14:32 +000054const char *prg;
55
Dieter Baron90a8ca12005-01-11 21:18:51 +000056int
57main(int argc, char *argv[])
58{
59 int fail, ze;
60 struct zip *z;
Dieter Baron71036932005-01-20 21:01:41 +000061 struct zip_source *zs;
Thomas Klausner2188b742005-07-16 17:14:32 +000062 char *archive;
Dieter Baronf7935682006-02-21 10:21:25 +000063 char errstr[1024];
Dieter Baron90a8ca12005-01-11 21:18:51 +000064
65 fail = 0;
66
Thomas Klausner2188b742005-07-16 17:14:32 +000067 prg = argv[0];
68
69 if (argc != 2) {
70 fprintf(stderr, "usage: %s archive\n", prg);
71 return 1;
72 }
73
74 archive = argv[1];
75
Dieter Baron1a6d0b12006-05-05 23:09:00 +000076 if ((z=zip_open(archive, 0, &ze)) == NULL) {
Dieter Baronf7935682006-02-21 10:21:25 +000077 zip_error_to_str(errstr, sizeof(errstr), ze, errno);
78 printf("%s: opening zip archive ``%s'' failed: %s\n",
79 prg, archive, errstr);
Dieter Baron90a8ca12005-01-11 21:18:51 +000080 return 1;
81 }
82
83 fail += do_read(z, "storedok", 0, WHEN_NEVER, 0, 0);
84 fail += do_read(z, "deflateok", 0, WHEN_NEVER, 0, 0);
85 fail += do_read(z, "storedcrcerror", 0, WHEN_READ, ZIP_ER_CRC, 0);
86 fail += do_read(z, "deflatecrcerror", 0, WHEN_READ, ZIP_ER_CRC, 0);
87 fail += do_read(z, "deflatezliberror", 0, WHEN_READ, ZIP_ER_ZLIB, -3);
Dieter Baron1a570ea2005-01-17 10:46:00 +000088 fail += do_read(z, NULL, 0, WHEN_OPEN, ZIP_ER_INVAL, 0);
89 fail += do_read(z, "nosuchfile", 0, WHEN_OPEN, ZIP_ER_NOENT, 0);
90 fail += do_read(z, "deflatezliberror", ZIP_FL_COMPRESSED, WHEN_NEVER, 0,0);
91 fail += do_read(z, "deflatecrcerror", ZIP_FL_COMPRESSED, WHEN_NEVER, 0, 0);
92 fail += do_read(z, "storedcrcerror", ZIP_FL_COMPRESSED,
93 WHEN_READ, ZIP_ER_CRC, 0);
94 fail += do_read(z, "storedok", ZIP_FL_COMPRESSED, WHEN_NEVER, 0, 0);
Dieter Baron90a8ca12005-01-11 21:18:51 +000095
Dieter Baron10155932009-03-11 18:07:55 +010096 fail += do_read(z, "cryptok", 0, WHEN_OPEN, ZIP_ER_NOPASSWD, 0);
97 zip_set_default_password(z, "crypt");
98 fail += do_read(z, "cryptok", 0, WHEN_NEVER, 0, 0);
99 zip_set_default_password(z, "wrong");
100 fail += do_read(z, "cryptok", 0, WHEN_OPEN, ZIP_ER_WRONGPASSWD, 0);
101 zip_set_default_password(z, NULL);
102
Dieter Baron71036932005-01-20 21:01:41 +0000103 zs = zip_source_buffer(z, "asdf", 4, 0);
104 zip_replace(z, zip_name_locate(z, "storedok", 0), zs);
105 fail += do_read(z, "storedok", 0, WHEN_OPEN, ZIP_ER_CHANGED, 0);
106 fail += do_read(z, "storedok", ZIP_FL_UNCHANGED, WHEN_NEVER, 0, 0);
107 zip_delete(z, zip_name_locate(z, "storedok", 0));
108 fail += do_read(z, "storedok", 0, WHEN_OPEN, ZIP_ER_NOENT, 0);
109 fail += do_read(z, "storedok", ZIP_FL_UNCHANGED, WHEN_NEVER, 0, 0);
110 zs = zip_source_buffer(z, "asdf", 4, 0);
111 zip_add(z, "new_file", zs);
112 fail += do_read(z, "new_file", 0, WHEN_OPEN, ZIP_ER_CHANGED, 0);
113 zip_unchange_all(z);
114
Thomas Klausner4b813622008-02-18 16:06:56 +0100115 if (zip_close(z) == -1) {
116 fprintf(stderr, "%s: can't close zip archive `%s'\n", prg, archive);
117 return 1;
118 }
119
Dieter Baron90a8ca12005-01-11 21:18:51 +0000120 exit(fail ? 1 : 0);
121}
122
123
124
125int
126do_read(struct zip *z, const char *name, int flags,
127 enum when when_ex, int ze_ex, int se_ex)
128{
129 struct zip_file *zf;
130 enum when when_got;
131 int ze_got, se_got;
132 char b[8192];
133 int n;
134 char expected[80];
135 char got[80];
136
137 when_got = WHEN_NEVER;
138 ze_got = se_got = 0;
139
140 if ((zf=zip_fopen(z, name, flags)) == NULL) {
141 when_got = WHEN_OPEN;
142 zip_error_get(z, &ze_got, &se_got);
143 }
144 else {
145 while ((n=zip_fread(zf, b, sizeof(b))) > 0)
146 ;
147 if (n < 0) {
148 when_got = WHEN_READ;
149 zip_file_error_get(zf, &ze_got, &se_got);
150 }
151 n = zip_fclose(zf);
152 if (when_got == WHEN_NEVER && n != 0) {
153 when_got = WHEN_CLOSE;
154 ze_got = n;
155 se_got = 0;
156 }
157 }
158
159 if (when_got != when_ex || ze_got != ze_ex || se_got != se_ex) {
160 zip_error_to_str(expected, sizeof(expected), ze_ex, se_ex);
161 zip_error_to_str(got, sizeof(got), ze_got, se_got);
Dieter Baron2f3f11c2009-02-14 18:58:01 +0100162 printf("%s: %s: got %s error (%s), expected %s error (%s)\n",
163 prg, name,
Dieter Baron90a8ca12005-01-11 21:18:51 +0000164 when_name[when_got], got,
165 when_name[when_ex], expected);
166 return 1;
167 }
Dieter Baron2f3f11c2009-02-14 18:58:01 +0100168 else if (getenv("VERBOSE"))
169 printf("%s: %s: passed\n", prg, name);
Dieter Baron90a8ca12005-01-11 21:18:51 +0000170
171 return 0;
172}