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