1 /*
   2  * Copyright (c) 2006, 2017, 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 /**
  25  * @test
  26  * @bug 6374379
  27  * @library /test/lib
  28  * @build jdk.test.lib.Platform
  29  *        jdk.test.lib.util.FileUtils
  30  * @run main ReadLongZipFileName
  31  * @summary Verify that we can read zip file names > 255 chars long
  32  */
  33 
  34 import java.io.*;
  35 import java.util.jar.*;
  36 import java.util.Stack;
  37 import jdk.test.lib.util.FileUtils;
  38 
  39 public class ReadLongZipFileName {
  40     private static String entryName = "testFile.txt";;
  41 
  42     public static void realMain(String args[]) {
  43         String longDirName = "abcdefghijklmnopqrstuvwx"; // 24 chars.
  44         String jarFileName = "areallylargejarfilename.jar";    // 27 chars.
  45         File file = null;
  46         File myJarFile = null;
  47         int currentFileLength = 0;
  48         int minRequiredLength = 600; // long enough to definitely fail.
  49         Stack<File> directories = new Stack<File>();
  50 
  51         String filename = "." + File.separator;
  52         try {
  53             // Create a directory structure long enough that the filename will
  54             // put us over the minRequiredLength.
  55             do {
  56                 filename = filename + longDirName + File.separator;
  57                 file = new File(filename);
  58                 file.mkdir();
  59                 currentFileLength = file.getCanonicalPath().length();
  60                 directories.push(file);
  61             } while (currentFileLength < (minRequiredLength - jarFileName.length()));
  62 
  63             // Create a new Jar file: use jar instead of zip to make sure long
  64             // names work for both zip and jar subclass.
  65             filename = filename + jarFileName;
  66             JarOutputStream out = new JarOutputStream(
  67                 new BufferedOutputStream(
  68                     new FileOutputStream(filename.toString())));
  69             out.putNextEntry(new JarEntry(entryName));
  70             out.write(1);
  71             out.close();
  72             myJarFile = new File(filename.toString());
  73             currentFileLength = myJarFile.getCanonicalPath().length();
  74             if (!myJarFile.exists()) {
  75                 fail("Jar file does not exist.");
  76             }
  77         } catch (IOException e) {
  78             unexpected(e, "Problem creating the Jar file.");
  79         }
  80 
  81         try {
  82             JarFile readJarFile = new JarFile(myJarFile);
  83             JarEntry je = readJarFile.getJarEntry(entryName);
  84             check(je != null);
  85             DataInputStream dis = new DataInputStream(
  86                 readJarFile.getInputStream(je));
  87             byte val = dis.readByte();
  88             check(val == 1);
  89             try {
  90                 dis.readByte();
  91                 fail("Read past expected EOF");
  92             } catch (IOException e) {
  93                 pass();
  94             }
  95             readJarFile.close();
  96             pass("Opened Jar file for reading with a name " + currentFileLength
  97                  + " characters long");
  98         } catch (IOException e) {
  99             unexpected(e, "Test failed - problem reading the Jar file back in.");
 100         }
 101 
 102         if (myJarFile != null) {
 103             check(myJarFile.delete());
 104         }
 105 
 106         while (! directories.empty()) {
 107             File f = directories.pop();
 108             try {
 109                 FileUtils.deleteFileWithRetry(f.toPath());
 110             } catch (IOException e) {
 111                 unexpected(e, "Fail to clean up directory, " + f);
 112                 break;
 113             }
 114         }
 115     }
 116 
 117     //--------------------- Infrastructure ---------------------------
 118     static volatile int passed = 0, failed = 0;
 119     static void pass() {passed++;}
 120     static void pass(String msg) {System.out.println(msg); passed++;}
 121     static void fail() {failed++; Thread.dumpStack();}
 122     static void fail(String msg) {System.out.println(msg); fail();}
 123     static void unexpected(Throwable t) {failed++; t.printStackTrace();}
 124     static void unexpected(Throwable t, String msg) {
 125         System.out.println(msg); failed++; t.printStackTrace();}
 126     static void check(boolean cond) {if (cond) pass(); else fail();}
 127     static void equal(Object x, Object y) {
 128         if (x == null ? y == null : x.equals(y)) pass();
 129         else fail(x + " not equal to " + y);}
 130     public static void main(String[] args) throws Throwable {
 131         try {realMain(args);} catch (Throwable t) {unexpected(t);}
 132         System.out.println("\nPassed = " + passed + " failed = " + failed);
 133         if (failed > 0) throw new AssertionError("Some tests failed");}
 134 }