1 /*
   2  * Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 /* @test
  25  * @bug 6907252
  26  * @summary ZipFileInputStream Not Thread-Safe
  27  * @library /lib/testlibrary
  28  * @build jdk.testlibrary.*
  29  * @run main ZipEntryFreeTest
  30  */
  31 
  32 import java.io.*;
  33 import java.nio.file.Files;
  34 import java.nio.file.Paths;
  35 import java.util.Random;
  36 import java.util.Timer;
  37 import java.util.TimerTask;
  38 import java.util.zip.*;
  39 import jdk.testlibrary.FileUtils;
  40 
  41 public class ZipEntryFreeTest extends Thread {
  42 
  43     private static final int NUM_THREADS = 5;
  44     private static final int TEST_ITERATIONS = 5;
  45     private static final String ZIPFILE_NAME = "large.zip";
  46     private static final String ZIPENTRY_NAME = "random.txt";
  47     private static InputStream is = null;
  48     final Timer timer = new Timer();
  49 
  50     public static void main(String args[]) throws Exception {
  51         createZipFile();
  52         try {
  53             for (int i = 0; i < TEST_ITERATIONS; i++) {
  54                runTest();
  55             }
  56         } finally {
  57             FileUtils.deleteFileIfExistsWithRetry(Paths.get(ZIPFILE_NAME));
  58         }
  59     }
  60 
  61     private static void runTest() throws Exception {
  62         try (ZipFile zf = new ZipFile(new File(ZIPFILE_NAME))) {
  63             is = zf.getInputStream(zf.getEntry(ZIPENTRY_NAME + "_0"));
  64             Thread[] threadArray = new Thread[NUM_THREADS];
  65             for (int i = 0; i < threadArray.length; i++) {
  66                 threadArray[i] = new ZipEntryFreeTest();
  67             }
  68             for (int i = 0; i < threadArray.length; i++) {
  69                 threadArray[i].start();
  70             }
  71             for (int i = 0; i < threadArray.length; i++) {
  72                 threadArray[i].join();
  73             }
  74         }
  75     }
  76 
  77     private static void createZipFile() throws Exception {
  78         Random rnd = new Random(1000L);
  79         byte[] contents = new byte[2_000_000];
  80         ZipEntry ze = null;
  81 
  82         try (ZipOutputStream zos =
  83             new ZipOutputStream(new FileOutputStream(ZIPFILE_NAME))) {
  84             // uncompressed mode seemed to tickle the crash
  85             zos.setMethod(ZipOutputStream.STORED);
  86             for (int ze_count = 0; ze_count < 10; ze_count++) {
  87                 rnd.nextBytes(contents);
  88                 ze = createZipEntry(contents, ze_count);
  89                 zos.putNextEntry(ze);
  90                 zos.write(contents, 0, contents.length);
  91             }
  92             zos.flush();
  93         }
  94     }
  95 
  96     private static ZipEntry createZipEntry(byte[] b, int i) {
  97         ZipEntry ze = new ZipEntry(ZIPENTRY_NAME + "_" + i);
  98         ze.setCompressedSize(b.length);
  99         ze.setSize(b.length);
 100         CRC32 crc = new CRC32();
 101         crc.update(b);
 102         ze.setCrc(crc.getValue());
 103         return ze;
 104     }
 105 
 106     @Override
 107     public void run() {
 108         try {
 109             int iteration = 0;
 110             TimerTask tt = (new TimerTask() {
 111                 @Override
 112                 public void run() {
 113                     try {
 114                         is.close();
 115                     } catch (Exception ex) {
 116                          ex.printStackTrace(System.out);
 117                     }
 118                 }
 119             });
 120             timer.schedule(tt, 50);
 121             while (is.read() != -1 && iteration++ < 1_000) { }
 122         } catch (Exception e) {
 123             // ZipException now expected instead of ZIP_Read crash
 124             System.out.println(e);
 125         } finally {
 126             timer.cancel();
 127         }
 128     }
 129 }