1 /*
   2  * Copyright (c) 2014, 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 8038491
  26  * @summary Crash in ZipFile.read() when ZipFileInputStream is shared between threads
  27  * @library /lib/testlibrary
  28  * @build jdk.testlibrary.FileUtils
  29  * @run main MultiThreadedReadTest
  30  */
  31 
  32 import java.io.File;
  33 import java.io.FileOutputStream;
  34 import java.io.InputStream;
  35 import java.nio.file.Paths;
  36 import java.util.Random;
  37 import java.util.zip.ZipEntry;
  38 import java.util.zip.ZipFile;
  39 import java.util.zip.ZipOutputStream;
  40 import jdk.testlibrary.FileUtils;
  41 
  42 public class MultiThreadedReadTest extends Thread {
  43 
  44     private static final int NUM_THREADS = 10;
  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 
  49     public static void main(String args[]) throws Exception {
  50         createZipFile();        
  51         try (ZipFile zf = new ZipFile(new File(ZIPFILE_NAME))) {
  52             is = zf.getInputStream(zf.getEntry(ZIPENTRY_NAME));
  53             Thread[] threadArray = new Thread[NUM_THREADS];
  54             for (int i = 0; i < threadArray.length; i++) {
  55                 threadArray[i] = new MultiThreadedReadTest();
  56             }
  57             for (int i = 0; i < threadArray.length; i++) {
  58                 threadArray[i].start();
  59             }
  60             for (int i = 0; i < threadArray.length; i++) {
  61                 threadArray[i].join();
  62             }
  63         } finally {
  64             FileUtils.deleteFileIfExistsWithRetry(Paths.get(ZIPFILE_NAME));
  65         }
  66     }
  67 
  68     private static void createZipFile() throws Exception {
  69         try (ZipOutputStream zos =
  70             new ZipOutputStream(new FileOutputStream(ZIPFILE_NAME))) {
  71 
  72             zos.putNextEntry(new ZipEntry(ZIPENTRY_NAME));
  73             StringBuilder sb = new StringBuilder(); 
  74             Random rnd = new Random();
  75             for(int i = 0; i < 1000; i++) {
  76                 // append some random string for ZipEntry
  77                 sb.append(Long.toString(rnd.nextLong()));
  78             }
  79             byte[] b = sb.toString().getBytes();
  80             zos.write(b, 0, b.length);
  81         }
  82     }
  83 
  84     @Override
  85     public void run() {
  86         try {
  87             while (is.read() != -1) { }
  88         } catch (Exception e) {
  89             // Swallow any Exceptions (which are expected) - we're only interested in the crash
  90         }
  91     }
  92 }