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