test/jdk/java/util/zip/ZipFile/Assortment.java
author erikj
Tue, 12 Sep 2017 19:03:39 +0200
changeset 47216 71c04702a3d5
parent 30046 jdk/test/java/util/zip/ZipFile/Assortment.java@cf2c86e1819e
permissions -rw-r--r--
8187443: Forest Consolidation: Move files to unified layout Reviewed-by: darcy, ihse
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
23010
6dadb192ad81 8029235: Update copyright year to match last edit in jdk8 jdk repository for 2013
lana
parents: 17910
diff changeset
     2
 * Copyright (c) 2005, 2013, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
/* @test
25222
5d63cd3696d6 8048990: ZipFile.entries() can't handle empty zip entry names
martin
parents: 23010
diff changeset
    25
 * @bug 4770745 6234507 6303183 8048990
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
 * @summary test a variety of zip file entries
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
 * @author Martin Buchholz
30046
cf2c86e1819e 8078334: Mark regression tests using randomness
darcy
parents: 25222
diff changeset
    28
 * @key randomness
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.util.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.util.zip.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.util.jar.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import java.io.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
public class Assortment {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
    static int passed = 0, failed = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
    static void fail(String msg) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
        failed++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
        new Exception(msg).printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
    static void unexpected(Throwable t) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
        failed++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
        t.printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
    static void check(boolean condition, String msg) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
        if (! condition)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
            fail(msg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
    static void check(boolean condition) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
        check(condition, "Something's wrong");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
17910
82d10099a8a6 4759491: method ZipEntry.setTime(long) works incorrectly
sherman
parents: 9035
diff changeset
    58
    static final int get16(byte b[], int off) {
82d10099a8a6 4759491: method ZipEntry.setTime(long) works incorrectly
sherman
parents: 9035
diff changeset
    59
        return Byte.toUnsignedInt(b[off]) | (Byte.toUnsignedInt(b[off+1]) << 8);
82d10099a8a6 4759491: method ZipEntry.setTime(long) works incorrectly
sherman
parents: 9035
diff changeset
    60
    }
82d10099a8a6 4759491: method ZipEntry.setTime(long) works incorrectly
sherman
parents: 9035
diff changeset
    61
82d10099a8a6 4759491: method ZipEntry.setTime(long) works incorrectly
sherman
parents: 9035
diff changeset
    62
    // check if all "expected" extra fields equal to their
82d10099a8a6 4759491: method ZipEntry.setTime(long) works incorrectly
sherman
parents: 9035
diff changeset
    63
    // corresponding fields in "extra". The "extra" might have
82d10099a8a6 4759491: method ZipEntry.setTime(long) works incorrectly
sherman
parents: 9035
diff changeset
    64
    // timestamp fields added by ZOS.
82d10099a8a6 4759491: method ZipEntry.setTime(long) works incorrectly
sherman
parents: 9035
diff changeset
    65
    static boolean equalsExtraData(byte[] expected, byte[] extra) {
82d10099a8a6 4759491: method ZipEntry.setTime(long) works incorrectly
sherman
parents: 9035
diff changeset
    66
        if (expected == null)
82d10099a8a6 4759491: method ZipEntry.setTime(long) works incorrectly
sherman
parents: 9035
diff changeset
    67
            return true;
82d10099a8a6 4759491: method ZipEntry.setTime(long) works incorrectly
sherman
parents: 9035
diff changeset
    68
        int off = 0;
82d10099a8a6 4759491: method ZipEntry.setTime(long) works incorrectly
sherman
parents: 9035
diff changeset
    69
        int len = expected.length;
82d10099a8a6 4759491: method ZipEntry.setTime(long) works incorrectly
sherman
parents: 9035
diff changeset
    70
        while (off + 4 < len) {
82d10099a8a6 4759491: method ZipEntry.setTime(long) works incorrectly
sherman
parents: 9035
diff changeset
    71
            int tag = get16(expected, off);
82d10099a8a6 4759491: method ZipEntry.setTime(long) works incorrectly
sherman
parents: 9035
diff changeset
    72
            int sz = get16(expected, off + 2);
82d10099a8a6 4759491: method ZipEntry.setTime(long) works incorrectly
sherman
parents: 9035
diff changeset
    73
            int off0 = 0;
82d10099a8a6 4759491: method ZipEntry.setTime(long) works incorrectly
sherman
parents: 9035
diff changeset
    74
            int len0 = extra.length;
82d10099a8a6 4759491: method ZipEntry.setTime(long) works incorrectly
sherman
parents: 9035
diff changeset
    75
            boolean matched = false;
82d10099a8a6 4759491: method ZipEntry.setTime(long) works incorrectly
sherman
parents: 9035
diff changeset
    76
            while (off0 + 4 < len0) {
82d10099a8a6 4759491: method ZipEntry.setTime(long) works incorrectly
sherman
parents: 9035
diff changeset
    77
                int tag0 = get16(extra, off0);
82d10099a8a6 4759491: method ZipEntry.setTime(long) works incorrectly
sherman
parents: 9035
diff changeset
    78
                int sz0 = get16(extra, off0 + 2);
82d10099a8a6 4759491: method ZipEntry.setTime(long) works incorrectly
sherman
parents: 9035
diff changeset
    79
                if (tag == tag0 && sz == sz0) {
82d10099a8a6 4759491: method ZipEntry.setTime(long) works incorrectly
sherman
parents: 9035
diff changeset
    80
                    matched = true;
82d10099a8a6 4759491: method ZipEntry.setTime(long) works incorrectly
sherman
parents: 9035
diff changeset
    81
                    for (int i = 0; i < sz; i++) {
82d10099a8a6 4759491: method ZipEntry.setTime(long) works incorrectly
sherman
parents: 9035
diff changeset
    82
                        if (expected[off + i] != extra[off0 +i])
82d10099a8a6 4759491: method ZipEntry.setTime(long) works incorrectly
sherman
parents: 9035
diff changeset
    83
                            matched = false;
82d10099a8a6 4759491: method ZipEntry.setTime(long) works incorrectly
sherman
parents: 9035
diff changeset
    84
                    }
82d10099a8a6 4759491: method ZipEntry.setTime(long) works incorrectly
sherman
parents: 9035
diff changeset
    85
                    break;
82d10099a8a6 4759491: method ZipEntry.setTime(long) works incorrectly
sherman
parents: 9035
diff changeset
    86
                }
82d10099a8a6 4759491: method ZipEntry.setTime(long) works incorrectly
sherman
parents: 9035
diff changeset
    87
                off0 += (4 + sz0);
82d10099a8a6 4759491: method ZipEntry.setTime(long) works incorrectly
sherman
parents: 9035
diff changeset
    88
            }
82d10099a8a6 4759491: method ZipEntry.setTime(long) works incorrectly
sherman
parents: 9035
diff changeset
    89
            if (!matched)
82d10099a8a6 4759491: method ZipEntry.setTime(long) works incorrectly
sherman
parents: 9035
diff changeset
    90
                return false;
82d10099a8a6 4759491: method ZipEntry.setTime(long) works incorrectly
sherman
parents: 9035
diff changeset
    91
            off += (4 + sz);
82d10099a8a6 4759491: method ZipEntry.setTime(long) works incorrectly
sherman
parents: 9035
diff changeset
    92
        }
82d10099a8a6 4759491: method ZipEntry.setTime(long) works incorrectly
sherman
parents: 9035
diff changeset
    93
        return true;
82d10099a8a6 4759491: method ZipEntry.setTime(long) works incorrectly
sherman
parents: 9035
diff changeset
    94
    }
82d10099a8a6 4759491: method ZipEntry.setTime(long) works incorrectly
sherman
parents: 9035
diff changeset
    95
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
    private static class Entry {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
        private String name;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
        private int    method;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
        private byte[] data;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
        private byte[] extra;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
        private String comment;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
        Entry(String name,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
              int    method,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
              byte[] data,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
              byte[] extra,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
              String comment) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
            this.name    = name;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
            this.method  = method;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
            this.data    = data;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
            this.extra   = extra;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
            this.comment = comment;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
        void write(ZipOutputStream s) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
            ZipEntry e = new ZipEntry(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
            CRC32 crc32 = new CRC32();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
            e.setMethod(method);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
            if (method == ZipEntry.STORED) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
                e.setSize(data == null ? 0 : data.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
                crc32.reset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
                if (data != null) crc32.update(data);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
                e.setCrc(crc32.getValue());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
                e.setSize(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
                e.setCrc(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
            if (comment != null) e.setComment(comment);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
            if (extra   != null) e.setExtra(extra);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
            s.putNextEntry(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
            if (data != null) s.write(data);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
        byte[] getData(ZipFile f, ZipEntry e) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
            byte[] fdata = new byte[(int)e.getSize()];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
            InputStream is = f.getInputStream(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
            is.read(fdata);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
            return fdata;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
25222
5d63cd3696d6 8048990: ZipFile.entries() can't handle empty zip entry names
martin
parents: 23010
diff changeset
   141
        void verify(ZipFile f, ZipEntry e) throws Exception {
5d63cd3696d6 8048990: ZipFile.entries() can't handle empty zip entry names
martin
parents: 23010
diff changeset
   142
            verify(e, getData(f, e));
5d63cd3696d6 8048990: ZipFile.entries() can't handle empty zip entry names
martin
parents: 23010
diff changeset
   143
        }
5d63cd3696d6 8048990: ZipFile.entries() can't handle empty zip entry names
martin
parents: 23010
diff changeset
   144
5d63cd3696d6 8048990: ZipFile.entries() can't handle empty zip entry names
martin
parents: 23010
diff changeset
   145
        void verify(ZipEntry e, byte[] eData) throws Exception {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
            byte[] data  = (this.data == null) ? new byte[]{} : this.data;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
            byte[] extra = (this.extra != null && this.extra.length == 0) ?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
                null : this.extra;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
            check(name.equals(e.getName()));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
            check(method == e.getMethod());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
            check((((comment == null) || comment.equals(""))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
                   && (e.getComment() == null))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
                  || comment.equals(e.getComment()));
17910
82d10099a8a6 4759491: method ZipEntry.setTime(long) works incorrectly
sherman
parents: 9035
diff changeset
   154
            check(equalsExtraData(extra, e.getExtra()));
25222
5d63cd3696d6 8048990: ZipFile.entries() can't handle empty zip entry names
martin
parents: 23010
diff changeset
   155
            check(Arrays.equals(data, eData));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
            check(e.getSize() == data.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
            check((method == ZipEntry.DEFLATED) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
                  (e.getCompressedSize() == data.length));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
25222
5d63cd3696d6 8048990: ZipFile.entries() can't handle empty zip entry names
martin
parents: 23010
diff changeset
   161
        void verify(ZipFile f) throws Exception {
5d63cd3696d6 8048990: ZipFile.entries() can't handle empty zip entry names
martin
parents: 23010
diff changeset
   162
            ZipEntry e = f.getEntry(name);
5d63cd3696d6 8048990: ZipFile.entries() can't handle empty zip entry names
martin
parents: 23010
diff changeset
   163
            verify(e, getData(f, e));
5d63cd3696d6 8048990: ZipFile.entries() can't handle empty zip entry names
martin
parents: 23010
diff changeset
   164
        }
5d63cd3696d6 8048990: ZipFile.entries() can't handle empty zip entry names
martin
parents: 23010
diff changeset
   165
5d63cd3696d6 8048990: ZipFile.entries() can't handle empty zip entry names
martin
parents: 23010
diff changeset
   166
        void verifyZipInputStream(ZipInputStream s) throws Exception {
5d63cd3696d6 8048990: ZipFile.entries() can't handle empty zip entry names
martin
parents: 23010
diff changeset
   167
            ZipEntry e = s.getNextEntry();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
            byte[] data = (this.data == null) ? new byte[]{} : this.data;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
            byte[] otherData = new byte[data.length];
25222
5d63cd3696d6 8048990: ZipFile.entries() can't handle empty zip entry names
martin
parents: 23010
diff changeset
   171
            s.read(otherData);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
            check(Arrays.equals(data, otherData));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
            byte[] extra = (this.extra != null && this.extra.length == 0) ?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
                null : this.extra;
17910
82d10099a8a6 4759491: method ZipEntry.setTime(long) works incorrectly
sherman
parents: 9035
diff changeset
   176
            check(equalsExtraData(extra, e.getExtra()));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
            check(name.equals(e.getName()));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
            check(method == e.getMethod());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
            check(e.getSize() == -1 || e.getSize() == data.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
            check((method == ZipEntry.DEFLATED) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
                  (e.getCompressedSize() == data.length));
25222
5d63cd3696d6 8048990: ZipFile.entries() can't handle empty zip entry names
martin
parents: 23010
diff changeset
   182
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
25222
5d63cd3696d6 8048990: ZipFile.entries() can't handle empty zip entry names
martin
parents: 23010
diff changeset
   184
        void verifyJarInputStream(JarInputStream s) throws Exception {
5d63cd3696d6 8048990: ZipFile.entries() can't handle empty zip entry names
martin
parents: 23010
diff changeset
   185
            // JarInputStream "automatically" reads the manifest
5d63cd3696d6 8048990: ZipFile.entries() can't handle empty zip entry names
martin
parents: 23010
diff changeset
   186
            if (name.equals("meta-iNf/ManIfEst.Mf"))
5d63cd3696d6 8048990: ZipFile.entries() can't handle empty zip entry names
martin
parents: 23010
diff changeset
   187
                return;
5d63cd3696d6 8048990: ZipFile.entries() can't handle empty zip entry names
martin
parents: 23010
diff changeset
   188
5d63cd3696d6 8048990: ZipFile.entries() can't handle empty zip entry names
martin
parents: 23010
diff changeset
   189
            verifyZipInputStream(s);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
    private static int uniquifier = 86;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
    private static String uniquify(String name) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
        return name + (uniquifier++);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
    private static byte[] toBytes(String s) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
        return s.getBytes("UTF-8");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
    private static byte[] toExtra(byte[] bytes) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
        if (bytes == null) return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
        // Construct a fake extra field with valid header length
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
        byte[] v = new byte[bytes.length + 4];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
        v[0] = (byte) 0x47;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
        v[1] = (byte) 0xff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
        v[2] = (byte) bytes.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
        v[3] = (byte) (bytes.length << 8);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
        System.arraycopy(bytes, 0, v, 4, bytes.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
        return v;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
    private static Random random = new Random();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
    private static String makeName(int length) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
        StringBuilder sb = new StringBuilder(length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
        for (int i = 0; i < length; i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
            sb.append((char)(random.nextInt(10000)+1));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
        return sb.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
    public static void main(String[] args) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
        File zipName = new File("x.zip");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
        int[]    methods  = {ZipEntry.STORED, ZipEntry.DEFLATED};
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
        String[] names    = {makeName(1), makeName(160), makeName(9000)};
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
        byte[][] datas    = {null, new byte[]{}, new byte[]{'d'}};
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
        byte[][] extras   = {null, new byte[]{}, new byte[]{'e'}};
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
        String[] comments = {null, "", "c"};
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
        List<Entry> entries = new ArrayList<Entry>();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
        // Highly unusual manifest
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
        entries.add(new Entry("meta-iNf/ManIfEst.Mf",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
                              ZipEntry.STORED,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
                              toBytes("maNiFest-VeRsIon: 1.0\n"),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
                              toExtra(toBytes("Can manifests have extra??")),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
                              "Can manifests have comments??"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
        // The emptiest possible entry
25222
5d63cd3696d6 8048990: ZipFile.entries() can't handle empty zip entry names
martin
parents: 23010
diff changeset
   241
        entries.add(new Entry("", ZipEntry.STORED, null, null, ""));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
        for (String name : names)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
            for (int method : methods)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
                for (byte[] data : datas) // datae??
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
                    for (byte[] extra : extras)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
                        for (String comment : comments)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
                            entries.add(new Entry(uniquify(name), method, data,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
                                                  toExtra(extra), comment));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
        //----------------------------------------------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
        // Write zip file using ZipOutputStream
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
        //----------------------------------------------------------------
8553
46c2babb1e44 7021582: convert jar/zip code and tests to use try-with-resources
smarks
parents: 5506
diff changeset
   254
        try (FileOutputStream fos = new FileOutputStream(zipName);
46c2babb1e44 7021582: convert jar/zip code and tests to use try-with-resources
smarks
parents: 5506
diff changeset
   255
             ZipOutputStream zos = new ZipOutputStream(fos))
46c2babb1e44 7021582: convert jar/zip code and tests to use try-with-resources
smarks
parents: 5506
diff changeset
   256
        {
46c2babb1e44 7021582: convert jar/zip code and tests to use try-with-resources
smarks
parents: 5506
diff changeset
   257
            for (Entry e : entries)
46c2babb1e44 7021582: convert jar/zip code and tests to use try-with-resources
smarks
parents: 5506
diff changeset
   258
                e.write(zos);
46c2babb1e44 7021582: convert jar/zip code and tests to use try-with-resources
smarks
parents: 5506
diff changeset
   259
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
        //----------------------------------------------------------------
25222
5d63cd3696d6 8048990: ZipFile.entries() can't handle empty zip entry names
martin
parents: 23010
diff changeset
   262
        // Verify zip file contents using ZipFile.getEntry()
5d63cd3696d6 8048990: ZipFile.entries() can't handle empty zip entry names
martin
parents: 23010
diff changeset
   263
        //----------------------------------------------------------------
5d63cd3696d6 8048990: ZipFile.entries() can't handle empty zip entry names
martin
parents: 23010
diff changeset
   264
        try (ZipFile f = new ZipFile(zipName)) {
5d63cd3696d6 8048990: ZipFile.entries() can't handle empty zip entry names
martin
parents: 23010
diff changeset
   265
            for (Entry e : entries)
5d63cd3696d6 8048990: ZipFile.entries() can't handle empty zip entry names
martin
parents: 23010
diff changeset
   266
                e.verify(f);
5d63cd3696d6 8048990: ZipFile.entries() can't handle empty zip entry names
martin
parents: 23010
diff changeset
   267
        }
5d63cd3696d6 8048990: ZipFile.entries() can't handle empty zip entry names
martin
parents: 23010
diff changeset
   268
5d63cd3696d6 8048990: ZipFile.entries() can't handle empty zip entry names
martin
parents: 23010
diff changeset
   269
        //----------------------------------------------------------------
5d63cd3696d6 8048990: ZipFile.entries() can't handle empty zip entry names
martin
parents: 23010
diff changeset
   270
        // Verify zip file contents using JarFile.getEntry()
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
        //----------------------------------------------------------------
25222
5d63cd3696d6 8048990: ZipFile.entries() can't handle empty zip entry names
martin
parents: 23010
diff changeset
   272
        try (JarFile f = new JarFile(zipName)) {
5d63cd3696d6 8048990: ZipFile.entries() can't handle empty zip entry names
martin
parents: 23010
diff changeset
   273
            check(f.getManifest() != null);
5d63cd3696d6 8048990: ZipFile.entries() can't handle empty zip entry names
martin
parents: 23010
diff changeset
   274
            for (Entry e : entries)
5d63cd3696d6 8048990: ZipFile.entries() can't handle empty zip entry names
martin
parents: 23010
diff changeset
   275
                e.verify(f);
5d63cd3696d6 8048990: ZipFile.entries() can't handle empty zip entry names
martin
parents: 23010
diff changeset
   276
        }
5d63cd3696d6 8048990: ZipFile.entries() can't handle empty zip entry names
martin
parents: 23010
diff changeset
   277
5d63cd3696d6 8048990: ZipFile.entries() can't handle empty zip entry names
martin
parents: 23010
diff changeset
   278
        //----------------------------------------------------------------
5d63cd3696d6 8048990: ZipFile.entries() can't handle empty zip entry names
martin
parents: 23010
diff changeset
   279
        // Verify zip file contents using ZipFile.entries()
5d63cd3696d6 8048990: ZipFile.entries() can't handle empty zip entry names
martin
parents: 23010
diff changeset
   280
        //----------------------------------------------------------------
5d63cd3696d6 8048990: ZipFile.entries() can't handle empty zip entry names
martin
parents: 23010
diff changeset
   281
        try (ZipFile f = new ZipFile(zipName)) {
5d63cd3696d6 8048990: ZipFile.entries() can't handle empty zip entry names
martin
parents: 23010
diff changeset
   282
            Enumeration<? extends ZipEntry> en = f.entries();
5d63cd3696d6 8048990: ZipFile.entries() can't handle empty zip entry names
martin
parents: 23010
diff changeset
   283
            for (Entry e : entries)
5d63cd3696d6 8048990: ZipFile.entries() can't handle empty zip entry names
martin
parents: 23010
diff changeset
   284
                e.verify(f, en.nextElement());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
25222
5d63cd3696d6 8048990: ZipFile.entries() can't handle empty zip entry names
martin
parents: 23010
diff changeset
   286
            check(!en.hasMoreElements());
5d63cd3696d6 8048990: ZipFile.entries() can't handle empty zip entry names
martin
parents: 23010
diff changeset
   287
        }
5d63cd3696d6 8048990: ZipFile.entries() can't handle empty zip entry names
martin
parents: 23010
diff changeset
   288
5d63cd3696d6 8048990: ZipFile.entries() can't handle empty zip entry names
martin
parents: 23010
diff changeset
   289
        //----------------------------------------------------------------
5d63cd3696d6 8048990: ZipFile.entries() can't handle empty zip entry names
martin
parents: 23010
diff changeset
   290
        // Verify zip file contents using JarFile.entries()
5d63cd3696d6 8048990: ZipFile.entries() can't handle empty zip entry names
martin
parents: 23010
diff changeset
   291
        //----------------------------------------------------------------
5d63cd3696d6 8048990: ZipFile.entries() can't handle empty zip entry names
martin
parents: 23010
diff changeset
   292
        try (JarFile f = new JarFile(zipName)) {
5d63cd3696d6 8048990: ZipFile.entries() can't handle empty zip entry names
martin
parents: 23010
diff changeset
   293
            Enumeration<? extends ZipEntry> en = f.entries();
5d63cd3696d6 8048990: ZipFile.entries() can't handle empty zip entry names
martin
parents: 23010
diff changeset
   294
            for (Entry e : entries)
5d63cd3696d6 8048990: ZipFile.entries() can't handle empty zip entry names
martin
parents: 23010
diff changeset
   295
                e.verify(f, en.nextElement());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
25222
5d63cd3696d6 8048990: ZipFile.entries() can't handle empty zip entry names
martin
parents: 23010
diff changeset
   297
            check(!en.hasMoreElements());
5d63cd3696d6 8048990: ZipFile.entries() can't handle empty zip entry names
martin
parents: 23010
diff changeset
   298
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
25222
5d63cd3696d6 8048990: ZipFile.entries() can't handle empty zip entry names
martin
parents: 23010
diff changeset
   300
        //----------------------------------------------------------------
5d63cd3696d6 8048990: ZipFile.entries() can't handle empty zip entry names
martin
parents: 23010
diff changeset
   301
        // Verify zip file contents using ZipInputStream class
5d63cd3696d6 8048990: ZipFile.entries() can't handle empty zip entry names
martin
parents: 23010
diff changeset
   302
        //----------------------------------------------------------------
5d63cd3696d6 8048990: ZipFile.entries() can't handle empty zip entry names
martin
parents: 23010
diff changeset
   303
        try (FileInputStream fis = new FileInputStream(zipName);
5d63cd3696d6 8048990: ZipFile.entries() can't handle empty zip entry names
martin
parents: 23010
diff changeset
   304
             ZipInputStream s = new ZipInputStream(fis)) {
5d63cd3696d6 8048990: ZipFile.entries() can't handle empty zip entry names
martin
parents: 23010
diff changeset
   305
5d63cd3696d6 8048990: ZipFile.entries() can't handle empty zip entry names
martin
parents: 23010
diff changeset
   306
            for (Entry e : entries)
5d63cd3696d6 8048990: ZipFile.entries() can't handle empty zip entry names
martin
parents: 23010
diff changeset
   307
                e.verifyZipInputStream(s);
5d63cd3696d6 8048990: ZipFile.entries() can't handle empty zip entry names
martin
parents: 23010
diff changeset
   308
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
        //----------------------------------------------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
        // Verify zip file contents using JarInputStream class
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
        //----------------------------------------------------------------
25222
5d63cd3696d6 8048990: ZipFile.entries() can't handle empty zip entry names
martin
parents: 23010
diff changeset
   313
        try (FileInputStream fis = new FileInputStream(zipName);
5d63cd3696d6 8048990: ZipFile.entries() can't handle empty zip entry names
martin
parents: 23010
diff changeset
   314
             JarInputStream s = new JarInputStream(fis)) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
25222
5d63cd3696d6 8048990: ZipFile.entries() can't handle empty zip entry names
martin
parents: 23010
diff changeset
   316
            // JarInputStream "automatically" reads the manifest
5d63cd3696d6 8048990: ZipFile.entries() can't handle empty zip entry names
martin
parents: 23010
diff changeset
   317
            check(s.getManifest() != null);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
25222
5d63cd3696d6 8048990: ZipFile.entries() can't handle empty zip entry names
martin
parents: 23010
diff changeset
   319
            for (Entry e : entries)
5d63cd3696d6 8048990: ZipFile.entries() can't handle empty zip entry names
martin
parents: 23010
diff changeset
   320
                e.verifyJarInputStream(s);
5d63cd3696d6 8048990: ZipFile.entries() can't handle empty zip entry names
martin
parents: 23010
diff changeset
   321
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
//      String cmd = "unzip -t " + zipName.getPath() + " >/dev/tty";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
//      new ProcessBuilder(new String[]{"/bin/sh", "-c", cmd}).start().waitFor();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
        zipName.deleteOnExit();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
        System.out.printf("passed = %d, failed = %d%n", passed, failed);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
        if (failed > 0) throw new Exception("Some tests failed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
}