"Fossies" - the Fresh Open Source Software Archive

Member "libzip-1.6.0/regress/fread.c" (24 Jan 2020, 7211 Bytes) of package /linux/misc/libzip-1.6.0.tar.xz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "fread.c" see the Fossies "Dox" file reference documentation and the latest Fossies "Diffs" side-by-side code changes report: 1.5.2_vs_1.6.0.

    1 /*
    2   fread.c -- test cases for reading from zip archives
    3   Copyright (C) 2004-2019 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 <errno.h>
   37 #include <stdio.h>
   38 #include <stdlib.h>
   39 #ifdef HAVE_UNISTD_H
   40 #include <unistd.h>
   41 #endif
   42 
   43 #ifndef HAVE_GETOPT
   44 #include "getopt.h"
   45 #endif
   46 
   47 #include "zip.h"
   48 
   49 enum when { WHEN_NEVER, WHEN_OPEN, WHEN_READ, WHEN_CLOSE };
   50 
   51 const char *when_name[] = {"no", "zip_fopen", "zip_fread", "zip_fclose"};
   52 
   53 static int do_read(zip_t *z, const char *name, zip_flags_t flags, enum when when_ex, int ze_ex, int se_ex);
   54 
   55 int verbose;
   56 
   57 const char *progname;
   58 #define USAGE "usage: %s [-v] archive\n"
   59 
   60 int
   61 main(int argc, char *argv[]) {
   62     int fail, ze;
   63     int c;
   64     zip_t *z;
   65     zip_source_t *zs;
   66     char *archive;
   67     zip_int64_t idx;
   68 
   69     verbose = 0;
   70     fail = 0;
   71 
   72     progname = argv[0];
   73 
   74     while ((c = getopt(argc, argv, "v")) != -1) {
   75     switch (c) {
   76     case 'v':
   77         verbose = 1;
   78         break;
   79 
   80     default:
   81         fprintf(stderr, USAGE, progname);
   82         return 1;
   83     }
   84     }
   85 
   86 
   87     if (argc - optind != 1) {
   88     fprintf(stderr, USAGE, progname);
   89     return 1;
   90     }
   91 
   92     archive = argv[optind];
   93 
   94     if ((z = zip_open(archive, 0, &ze)) == NULL) {
   95     zip_error_t error;
   96     zip_error_init_with_code(&error, ze);
   97     fprintf(stderr, "%s: can't open zip archive '%s': %s\n", progname, archive, zip_error_strerror(&error));
   98     zip_error_fini(&error);
   99     return 1;
  100     }
  101 
  102     fail += do_read(z, "storedok", 0, WHEN_NEVER, 0, 0);
  103     fail += do_read(z, "deflateok", 0, WHEN_NEVER, 0, 0);
  104     fail += do_read(z, "storedcrcerror", 0, WHEN_READ, ZIP_ER_CRC, 0);
  105     fail += do_read(z, "deflatecrcerror", 0, WHEN_READ, ZIP_ER_CRC, 0);
  106     fail += do_read(z, "deflatezliberror", 0, WHEN_READ, ZIP_ER_ZLIB, -3);
  107 #ifndef __clang_analyzer__ /* This test intentionally violates nullability. */
  108     fail += do_read(z, NULL, 0, WHEN_OPEN, ZIP_ER_INVAL, 0);
  109 #endif
  110     fail += do_read(z, "nosuchfile", 0, WHEN_OPEN, ZIP_ER_NOENT, 0);
  111     fail += do_read(z, "deflatezliberror", ZIP_FL_COMPRESSED, WHEN_NEVER, 0, 0);
  112     fail += do_read(z, "deflatecrcerror", ZIP_FL_COMPRESSED, WHEN_NEVER, 0, 0);
  113     fail += do_read(z, "storedcrcerror", ZIP_FL_COMPRESSED, WHEN_READ, ZIP_ER_CRC, 0);
  114     fail += do_read(z, "storedok", ZIP_FL_COMPRESSED, WHEN_NEVER, 0, 0);
  115 
  116     fail += do_read(z, "cryptok", 0, WHEN_OPEN, ZIP_ER_NOPASSWD, 0);
  117     zip_set_default_password(z, "crypt");
  118     fail += do_read(z, "cryptok", 0, WHEN_NEVER, 0, 0);
  119     zip_set_default_password(z, "wrong");
  120     fail += do_read(z, "cryptok", 0, WHEN_OPEN, ZIP_ER_WRONGPASSWD, 0);
  121     zip_set_default_password(z, NULL);
  122 
  123     zs = zip_source_buffer(z, "asdf", 4, 0);
  124     if ((idx = zip_name_locate(z, "storedok", 0)) < 0) {
  125     fprintf(stderr, "%s: can't locate 'storedok' in zip archive '%s': %s\n", progname, archive, zip_strerror(z));
  126     fail++;
  127     }
  128     else {
  129     if (zip_replace(z, (zip_uint64_t)idx, zs) < 0) {
  130         fprintf(stderr, "%s: can't replace 'storedok' in zip archive '%s': %s\n", progname, archive, zip_strerror(z));
  131         fail++;
  132     }
  133     else {
  134         fail += do_read(z, "storedok", 0, WHEN_OPEN, ZIP_ER_CHANGED, 0);
  135         fail += do_read(z, "storedok", ZIP_FL_UNCHANGED, WHEN_NEVER, 0, 0);
  136     }
  137     }
  138     if ((idx = zip_name_locate(z, "storedok", 0)) < 0) {
  139     fprintf(stderr, "%s: can't locate 'storedok' in zip archive '%s': %s\n", progname, archive, zip_strerror(z));
  140     fail++;
  141     }
  142     else {
  143     if (zip_delete(z, (zip_uint64_t)idx) < 0) {
  144         fprintf(stderr, "%s: can't replace 'storedok' in zip archive '%s': %s\n", progname, archive, zip_strerror(z));
  145         fail++;
  146     }
  147     else {
  148         fail += do_read(z, "storedok", 0, WHEN_OPEN, ZIP_ER_NOENT, 0);
  149         fail += do_read(z, "storedok", ZIP_FL_UNCHANGED, WHEN_NEVER, 0, 0);
  150     }
  151     }
  152     zs = zip_source_buffer(z, "asdf", 4, 0);
  153     if (zip_file_add(z, "new_file", zs, 0) < 0) {
  154     fprintf(stderr, "%s: can't add file to zip archive '%s': %s\n", progname, archive, zip_strerror(z));
  155     fail++;
  156     }
  157     else {
  158     fail += do_read(z, "new_file", 0, WHEN_OPEN, ZIP_ER_CHANGED, 0);
  159     }
  160 
  161     zip_unchange_all(z);
  162     if (zip_close(z) == -1) {
  163     fprintf(stderr, "%s: can't close zip archive '%s': %s\n", progname, archive, zip_strerror(z));
  164     return 1;
  165     }
  166 
  167     exit(fail ? 1 : 0);
  168 }
  169 
  170 
  171 static int
  172 do_read(zip_t *z, const char *name, zip_flags_t flags, enum when when_ex, int ze_ex, int se_ex) {
  173     zip_file_t *zf;
  174     enum when when_got;
  175     zip_error_t error_got, error_ex;
  176     zip_error_t *zf_error;
  177     int err;
  178     char b[8192];
  179     zip_int64_t n;
  180 
  181     when_got = WHEN_NEVER;
  182     zip_error_init(&error_got);
  183     zip_error_init(&error_ex);
  184     zip_error_set(&error_ex, ze_ex, se_ex);
  185 
  186     if ((zf = zip_fopen(z, name, flags)) == NULL) {
  187     when_got = WHEN_OPEN;
  188     zf_error = zip_get_error(z);
  189     zip_error_set(&error_got, zip_error_code_zip(zf_error), zip_error_code_system(zf_error));
  190     }
  191     else {
  192     while ((n = zip_fread(zf, b, sizeof(b))) > 0)
  193         ;
  194     if (n < 0) {
  195         when_got = WHEN_READ;
  196         zf_error = zip_file_get_error(zf);
  197         zip_error_set(&error_got, zip_error_code_zip(zf_error), zip_error_code_system(zf_error));
  198     }
  199     err = zip_fclose(zf);
  200     if (when_got == WHEN_NEVER && err != 0) {
  201         when_got = WHEN_CLOSE;
  202         zip_error_init_with_code(&error_got, err);
  203     }
  204     }
  205 
  206     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)) {
  207     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));
  208     zip_error_fini(&error_got);
  209     zip_error_fini(&error_ex);
  210     return 1;
  211     }
  212     else if (verbose)
  213     printf("%s: %s: passed\n", progname, name);
  214 
  215     return 0;
  216 }