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