Rizin
unix-like reverse engineering framework and cli tools
fread.c
Go to the documentation of this file.
1 /*
2  fread.c -- test cases for reading from zip archives
3  Copyright (C) 2004-2021 Dieter Baron and Thomas Klausner
4 
5  This file is part of libzip, a library to manipulate ZIP archives.
6  The authors can be contacted at <libzip@nih.at>
7 
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 #include "config.h"
35 
36 #include <stdio.h>
37 #include <stdlib.h>
38 
39 #ifndef HAVE_GETOPT
40 #include "getopt.h"
41 #endif
42 
43 #include "zip.h"
44 
46 
47 const char *when_name[] = {"no", "zip_fopen", "zip_fread", "zip_fclose"};
48 
49 static int do_read(zip_t *z, const char *name, zip_flags_t flags, enum when when_ex, int ze_ex, int se_ex);
50 
51 int verbose;
52 
53 const char *progname;
54 #define USAGE "usage: %s [-v] archive\n"
55 
56 int
57 main(int argc, char *argv[]) {
58  int fail, ze;
59  int c;
60  zip_t *z;
61  zip_source_t *zs;
62  char *archive;
64 
65  verbose = 0;
66  fail = 0;
67 
68  progname = argv[0];
69 
70  while ((c = getopt(argc, argv, "v")) != -1) {
71  switch (c) {
72  case 'v':
73  verbose = 1;
74  break;
75 
76  default:
77  fprintf(stderr, USAGE, progname);
78  return 1;
79  }
80  }
81 
82 
83  if (argc - optind != 1) {
84  fprintf(stderr, USAGE, progname);
85  return 1;
86  }
87 
88  archive = argv[optind];
89 
90  if ((z = zip_open(archive, 0, &ze)) == NULL) {
93  fprintf(stderr, "%s: can't open zip archive '%s': %s\n", progname, archive, zip_error_strerror(&error));
95  return 1;
96  }
97 
98  fail += do_read(z, "storedok", 0, WHEN_NEVER, 0, 0);
99  fail += do_read(z, "deflateok", 0, WHEN_NEVER, 0, 0);
100  fail += do_read(z, "storedcrcerror", 0, WHEN_READ, ZIP_ER_CRC, 0);
101  fail += do_read(z, "deflatecrcerror", 0, WHEN_READ, ZIP_ER_CRC, 0);
102  fail += do_read(z, "deflatezliberror", 0, WHEN_READ, ZIP_ER_ZLIB, -3);
103 #ifndef __clang_analyzer__ /* This test intentionally violates nullability. */
104  fail += do_read(z, NULL, 0, WHEN_OPEN, ZIP_ER_INVAL, 0);
105 #endif
106  fail += do_read(z, "nosuchfile", 0, WHEN_OPEN, ZIP_ER_NOENT, 0);
107  fail += do_read(z, "deflatezliberror", ZIP_FL_COMPRESSED, WHEN_NEVER, 0, 0);
108  fail += do_read(z, "deflatecrcerror", ZIP_FL_COMPRESSED, WHEN_NEVER, 0, 0);
109  fail += do_read(z, "storedcrcerror", ZIP_FL_COMPRESSED, WHEN_READ, ZIP_ER_CRC, 0);
110  fail += do_read(z, "storedok", ZIP_FL_COMPRESSED, WHEN_NEVER, 0, 0);
111 
112  fail += do_read(z, "cryptok", 0, WHEN_OPEN, ZIP_ER_NOPASSWD, 0);
113  zip_set_default_password(z, "crypt");
114  fail += do_read(z, "cryptok", 0, WHEN_NEVER, 0, 0);
115  zip_set_default_password(z, "wrong");
116  fail += do_read(z, "cryptok", 0, WHEN_OPEN, ZIP_ER_WRONGPASSWD, 0);
118 
119  zs = zip_source_buffer(z, "asdf", 4, 0);
120  if ((idx = zip_name_locate(z, "storedok", 0)) < 0) {
121  fprintf(stderr, "%s: can't locate 'storedok' in zip archive '%s': %s\n", progname, archive, zip_strerror(z));
122  fail++;
123  }
124  else {
125  if (zip_replace(z, (zip_uint64_t)idx, zs) < 0) {
126  fprintf(stderr, "%s: can't replace 'storedok' in zip archive '%s': %s\n", progname, archive, zip_strerror(z));
127  fail++;
128  }
129  else {
130  fail += do_read(z, "storedok", 0, WHEN_OPEN, ZIP_ER_CHANGED, 0);
131  fail += do_read(z, "storedok", ZIP_FL_UNCHANGED, WHEN_NEVER, 0, 0);
132  }
133  }
134  if ((idx = zip_name_locate(z, "storedok", 0)) < 0) {
135  fprintf(stderr, "%s: can't locate 'storedok' in zip archive '%s': %s\n", progname, archive, zip_strerror(z));
136  fail++;
137  }
138  else {
139  if (zip_delete(z, (zip_uint64_t)idx) < 0) {
140  fprintf(stderr, "%s: can't replace 'storedok' in zip archive '%s': %s\n", progname, archive, zip_strerror(z));
141  fail++;
142  }
143  else {
144  fail += do_read(z, "storedok", 0, WHEN_OPEN, ZIP_ER_NOENT, 0);
145  fail += do_read(z, "storedok", ZIP_FL_UNCHANGED, WHEN_NEVER, 0, 0);
146  }
147  }
148  zs = zip_source_buffer(z, "asdf", 4, 0);
149  if (zip_file_add(z, "new_file", zs, 0) < 0) {
150  fprintf(stderr, "%s: can't add file to zip archive '%s': %s\n", progname, archive, zip_strerror(z));
151  fail++;
152  }
153  else {
154  fail += do_read(z, "new_file", 0, WHEN_OPEN, ZIP_ER_CHANGED, 0);
155  }
156 
157  zip_unchange_all(z);
158  if (zip_close(z) == -1) {
159  fprintf(stderr, "%s: can't close zip archive '%s': %s\n", progname, archive, zip_strerror(z));
160  return 1;
161  }
162 
163  exit(fail ? 1 : 0);
164 }
165 
166 
167 static int
168 do_read(zip_t *z, const char *name, zip_flags_t flags, enum when when_ex, int ze_ex, int se_ex) {
169  zip_file_t *zf;
170  enum when when_got;
171  zip_error_t error_got, error_ex;
172  zip_error_t *zf_error;
173  int err;
174  char b[8192];
175  zip_int64_t n;
176 
177  when_got = WHEN_NEVER;
178  zip_error_init(&error_got);
179  zip_error_init(&error_ex);
180  zip_error_set(&error_ex, ze_ex, se_ex);
181 
182  if ((zf = zip_fopen(z, name, flags)) == NULL) {
183  when_got = WHEN_OPEN;
184  zf_error = zip_get_error(z);
185  zip_error_set(&error_got, zip_error_code_zip(zf_error), zip_error_code_system(zf_error));
186  }
187  else {
188  while ((n = zip_fread(zf, b, sizeof(b))) > 0)
189  ;
190  if (n < 0) {
191  when_got = WHEN_READ;
192  zf_error = zip_file_get_error(zf);
193  zip_error_set(&error_got, zip_error_code_zip(zf_error), zip_error_code_system(zf_error));
194  }
195  err = zip_fclose(zf);
196  if (when_got == WHEN_NEVER && err != 0) {
197  when_got = WHEN_CLOSE;
198  zip_error_init_with_code(&error_got, err);
199  }
200  }
201 
202  if (when_got != when_ex || zip_error_code_zip(&error_got) != zip_error_code_zip(&error_ex) || zip_error_code_system(&error_got) != zip_error_code_system(&error_ex)) {
203  printf("%s: %s: got %s error (%s), expected %s error (%s)\n", progname, name, when_name[when_got], zip_error_strerror(&error_got), when_name[when_ex], zip_error_strerror(&error_ex));
204  zip_error_fini(&error_got);
205  zip_error_fini(&error_ex);
206  return 1;
207  }
208  else if (verbose)
209  printf("%s: %s: passed\n", progname, name);
210 
211  return 0;
212 }
static bool err
Definition: armass.c:435
int getopt(int nargc, char *const nargv[], const char *ostr)
Definition: getopt.h:20
int optind
Definition: getopt.h:6
#define NULL
Definition: cris-opc.c:27
_Use_decl_annotations_ int __cdecl printf(const char *const _Format,...)
Definition: cs_driver.c:93
int verbose
Definition: fread.c:51
int main(int argc, char *argv[])
Definition: fread.c:57
#define USAGE
Definition: fread.c:54
static int do_read(zip_t *z, const char *name, zip_flags_t flags, enum when when_ex, int ze_ex, int se_ex)
Definition: fread.c:168
const char * when_name[]
Definition: fread.c:47
when
Definition: fread.c:45
@ WHEN_NEVER
Definition: fread.c:45
@ WHEN_READ
Definition: fread.c:45
@ WHEN_CLOSE
Definition: fread.c:45
@ WHEN_OPEN
Definition: fread.c:45
const char * progname
Definition: fread.c:53
ZIP_EXTERN void zip_error_set(zip_error_t *_Nullable, int, int)
Definition: zip_error.c:126
ZIP_EXTERN void zip_error_init_with_code(zip_error_t *_Nonnull, int)
Definition: zip_error.c:66
ZIP_EXTERN zip_int64_t zip_name_locate(zip_t *_Nonnull, const char *_Nonnull, zip_flags_t)
#define ZIP_ER_ZLIB
Definition: zip.h:118
ZIP_EXTERN int zip_fclose(zip_file_t *_Nonnull)
Definition: zip_fclose.c:41
ZIP_EXTERN zip_file_t *_Nullable zip_fopen(zip_t *_Nonnull, const char *_Nonnull, zip_flags_t)
Definition: zip_fopen.c:39
#define ZIP_ER_CHANGED
Definition: zip.h:120
#define ZIP_ER_NOPASSWD
Definition: zip.h:131
ZIP_EXTERN int zip_error_code_system(const zip_error_t *_Nonnull)
Definition: zip_error.c:40
#define ZIP_FL_COMPRESSED
Definition: zip.h:78
#define ZIP_ER_NOENT
Definition: zip.h:114
ZIP_EXTERN int zip_set_default_password(zip_t *_Nonnull, const char *_Nullable)
ZIP_EXTERN void zip_error_init(zip_error_t *_Nonnull)
Definition: zip_error.c:59
#define ZIP_ER_WRONGPASSWD
Definition: zip.h:132
ZIP_EXTERN zip_error_t *_Nonnull zip_get_error(zip_t *_Nonnull)
Definition: zip_error_get.c:46
ZIP_EXTERN zip_int64_t zip_fread(zip_file_t *_Nonnull, void *_Nonnull, zip_uint64_t)
Definition: zip_fread.c:39
#define ZIP_ER_CRC
Definition: zip.h:112
ZIP_EXTERN int zip_close(zip_t *_Nonnull)
Definition: zip_close.c:52
ZIP_EXTERN int zip_delete(zip_t *_Nonnull, zip_uint64_t)
Definition: zip_delete.c:39
ZIP_EXTERN void zip_error_fini(zip_error_t *_Nonnull)
Definition: zip_error.c:52
ZIP_EXTERN int zip_unchange_all(zip_t *_Nonnull)
#define ZIP_ER_INVAL
Definition: zip.h:123
ZIP_EXTERN int zip_error_code_zip(const zip_error_t *_Nonnull)
Definition: zip_error.c:46
zip_uint32_t zip_flags_t
Definition: zip.h:347
ZIP_EXTERN zip_error_t *_Nonnull zip_file_get_error(zip_file_t *_Nonnull)
Definition: zip_error_get.c:52
ZIP_EXTERN zip_t *_Nullable zip_open(const char *_Nonnull, int, int *_Nullable)
Definition: zip_open.c:54
ZIP_EXTERN int zip_replace(zip_t *_Nonnull, zip_uint64_t, zip_source_t *_Nonnull)
Definition: zip_replace.c:40
ZIP_EXTERN const char *_Nonnull zip_error_strerror(zip_error_t *_Nonnull)
#define ZIP_FL_UNCHANGED
Definition: zip.h:79
ZIP_EXTERN zip_int64_t zip_file_add(zip_t *_Nonnull, const char *_Nonnull, zip_source_t *_Nonnull, zip_flags_t)
Definition: zip_file_add.c:45
ZIP_EXTERN zip_source_t *_Nullable zip_source_buffer(zip_t *_Nonnull, const void *_Nullable, zip_uint64_t, int)
ZIP_EXTERN const char *_Nonnull zip_strerror(zip_t *_Nonnull)
Definition: zip_strerror.c:39
static static fork const void static count static fd const char const char static newpath char char argv
Definition: sflib.h:40
int n
Definition: mipsasm.c:19
int idx
Definition: setup.py:197
static struct sockaddr static addrlen static backlog const void static flags void flags
Definition: sfsocketcall.h:123
#define b(i)
Definition: sha256.c:42
#define c(i)
Definition: sha256.c:43
Definition: zipcmp.c:60
Definition: z80asm.h:102
Definition: zip.h:284
Definition: zipint.h:278
#define fail(test)
Definition: tests.h:29
void error(const char *msg)
Definition: untgz.c:593
uint64_t zip_uint64_t
Definition: zipconf.h:39
int64_t zip_int64_t
Definition: zipconf.h:38