Dieter Baron | 90a8ca1 | 2005-01-11 21:18:51 +0000 | [diff] [blame] | 1 | /* |
Dieter Baron | 90a8ca1 | 2005-01-11 21:18:51 +0000 | [diff] [blame] | 2 | fread.c -- test cases for reading from zip archives |
Dieter Baron | 1015593 | 2009-03-11 18:07:55 +0100 | [diff] [blame] | 3 | Copyright (C) 2004-2009 Dieter Baron and Thomas Klausner |
Dieter Baron | 90a8ca1 | 2005-01-11 21:18:51 +0000 | [diff] [blame] | 4 | |
| 5 | This file is part of libzip, a library to manipulate ZIP archives. |
Dieter Baron | b86c433 | 2007-11-07 14:35:13 +0100 | [diff] [blame] | 6 | The authors can be contacted at <libzip@nih.at> |
Dieter Baron | 90a8ca1 | 2005-01-11 21:18:51 +0000 | [diff] [blame] | 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 | |
| 35 | |
| 36 | #include <errno.h> |
| 37 | #include <stdio.h> |
| 38 | #include <stdlib.h> |
| 39 | |
| 40 | #include "zip.h" |
| 41 | |
Dieter Baron | 90a8ca1 | 2005-01-11 21:18:51 +0000 | [diff] [blame] | 42 | enum when { |
| 43 | WHEN_NEVER, WHEN_OPEN, WHEN_READ, WHEN_CLOSE |
| 44 | }; |
| 45 | |
| 46 | const char *when_name[] = { |
| 47 | "no", "zip_fopen", "zip_fread", "zip_fclose" |
| 48 | }; |
| 49 | |
| 50 | int do_read(struct zip *, const char *, int, enum when, int, int); |
| 51 | |
| 52 | |
| 53 | |
Thomas Klausner | 2188b74 | 2005-07-16 17:14:32 +0000 | [diff] [blame] | 54 | const char *prg; |
| 55 | |
Dieter Baron | 90a8ca1 | 2005-01-11 21:18:51 +0000 | [diff] [blame] | 56 | int |
| 57 | main(int argc, char *argv[]) |
| 58 | { |
| 59 | int fail, ze; |
| 60 | struct zip *z; |
Dieter Baron | 7103693 | 2005-01-20 21:01:41 +0000 | [diff] [blame] | 61 | struct zip_source *zs; |
Thomas Klausner | 2188b74 | 2005-07-16 17:14:32 +0000 | [diff] [blame] | 62 | char *archive; |
Dieter Baron | f793568 | 2006-02-21 10:21:25 +0000 | [diff] [blame] | 63 | char errstr[1024]; |
Dieter Baron | 90a8ca1 | 2005-01-11 21:18:51 +0000 | [diff] [blame] | 64 | |
| 65 | fail = 0; |
| 66 | |
Thomas Klausner | 2188b74 | 2005-07-16 17:14:32 +0000 | [diff] [blame] | 67 | 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 Baron | 1a6d0b1 | 2006-05-05 23:09:00 +0000 | [diff] [blame] | 76 | if ((z=zip_open(archive, 0, &ze)) == NULL) { |
Dieter Baron | f793568 | 2006-02-21 10:21:25 +0000 | [diff] [blame] | 77 | zip_error_to_str(errstr, sizeof(errstr), ze, errno); |
| 78 | printf("%s: opening zip archive ``%s'' failed: %s\n", |
| 79 | prg, archive, errstr); |
Dieter Baron | 90a8ca1 | 2005-01-11 21:18:51 +0000 | [diff] [blame] | 80 | 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 Baron | 1a570ea | 2005-01-17 10:46:00 +0000 | [diff] [blame] | 88 | 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 Baron | 90a8ca1 | 2005-01-11 21:18:51 +0000 | [diff] [blame] | 95 | |
Dieter Baron | 1015593 | 2009-03-11 18:07:55 +0100 | [diff] [blame] | 96 | 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 Baron | 7103693 | 2005-01-20 21:01:41 +0000 | [diff] [blame] | 103 | 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 Klausner | 4b81362 | 2008-02-18 16:06:56 +0100 | [diff] [blame] | 115 | if (zip_close(z) == -1) { |
| 116 | fprintf(stderr, "%s: can't close zip archive `%s'\n", prg, archive); |
| 117 | return 1; |
| 118 | } |
| 119 | |
Dieter Baron | 90a8ca1 | 2005-01-11 21:18:51 +0000 | [diff] [blame] | 120 | exit(fail ? 1 : 0); |
| 121 | } |
| 122 | |
| 123 | |
| 124 | |
| 125 | int |
| 126 | do_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 Baron | 2f3f11c | 2009-02-14 18:58:01 +0100 | [diff] [blame] | 162 | printf("%s: %s: got %s error (%s), expected %s error (%s)\n", |
| 163 | prg, name, |
Dieter Baron | 90a8ca1 | 2005-01-11 21:18:51 +0000 | [diff] [blame] | 164 | when_name[when_got], got, |
| 165 | when_name[when_ex], expected); |
| 166 | return 1; |
| 167 | } |
Dieter Baron | 2f3f11c | 2009-02-14 18:58:01 +0100 | [diff] [blame] | 168 | else if (getenv("VERBOSE")) |
| 169 | printf("%s: %s: passed\n", prg, name); |
Dieter Baron | 90a8ca1 | 2005-01-11 21:18:51 +0000 | [diff] [blame] | 170 | |
| 171 | return 0; |
| 172 | } |