1 /* 2 * Copyright (c) 2007, 2013, 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 4806786 8023113 27 * @modules jdk.jartool 28 * @summary jar -C doesn't ignore multiple // in path 29 */ 30 31 import java.io.*; 32 import java.nio.file.*; 33 import java.util.*; 34 import java.util.jar.*; 35 import java.util.spi.ToolProvider; 36 import java.util.stream.Stream; 37 38 public class ChangeDir { 39 private static final ToolProvider JAR_TOOL = ToolProvider.findFirst("jar") 40 .orElseThrow(() -> 41 new RuntimeException("jar tool not found") 42 ); 43 44 private final static String jarName = "test.jar"; 45 private final static String fileName = "hello.txt"; 46 47 /** Remove dirs & files needed for test. */ 48 private static void cleanup(Path dir) { 49 try { 50 if (Files.isDirectory(dir)) { 51 try (Stream<Path> s = Files.list(dir)) { 52 s.forEach( p -> cleanup(p)); 53 } 54 } 55 Files.delete(dir); 56 } catch (IOException x) { 57 fail(x.toString()); 58 } 59 } 60 61 public static void realMain(String[] args) throws Throwable { 62 doTest("/"); 63 doTest("//"); 64 doTest("///"); 65 doTest("////"); 66 if (System.getProperty("os.name").startsWith("Windows")) { 67 doTest("\\"); 68 doTest("\\\\"); 69 doTest("\\\\\\"); 70 doTest("\\\\\\\\"); 71 doTest("\\/"); 72 } 73 } 74 75 static void doTest(String sep) throws Throwable { 76 Path topDir = Files.createTempDirectory("delete"); 77 try { 78 Files.deleteIfExists(Paths.get(jarName)); 79 80 // Create a subdirectory "a/b" 81 Path testDir = Files.createDirectories(topDir.resolve("a").resolve("b")); 82 83 // Create file in that subdirectory 84 Path testFile = testDir.resolve(fileName); 85 Files.createFile(testFile); 86 87 // Create a jar file from that subdirectory, but with a // in the 88 // path name. 89 List<String> argList = new ArrayList<String>(); 90 argList.add("cf"); 91 argList.add(jarName); 92 argList.add("-C"); 93 argList.add(topDir.toString() + sep + "a" + sep + sep + "b"); // Note double 'sep' is intentional 94 argList.add(fileName); 95 96 int rc = JAR_TOOL.run(System.out, System.err, 97 argList.toArray(new String[argList.size()])); 98 if (rc != 0) { 99 fail("Could not create jar file."); 100 } 101 102 // Check that the entry for hello.txt does *not* have a pathname. 103 try (JarFile jf = new JarFile(jarName)) { 104 for (Enumeration<JarEntry> i = jf.entries(); i.hasMoreElements();) { 105 JarEntry je = i.nextElement(); 106 String name = je.getName(); 107 if (name.indexOf(fileName) != -1) { 108 if (name.indexOf(fileName) != 0) { 109 fail(String.format( 110 "Expected '%s' but got '%s'%n", fileName, name)); 111 } else { 112 pass(); 113 } 114 } 115 } 116 } 117 } finally { 118 cleanup(topDir); 119 Files.deleteIfExists(Paths.get(jarName)); 120 } 121 } 122 123 //--------------------- Infrastructure --------------------------- 124 static volatile int passed = 0, failed = 0; 125 static void pass() {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 check(boolean cond) {if (cond) pass(); else fail();} 130 static void equal(Object x, Object y) { 131 if (x == null ? y == null : x.equals(y)) pass(); 132 else fail(x + " not equal to " + y);} 133 public static void main(String[] args) throws Throwable { 134 try {realMain(args);} catch (Throwable t) {unexpected(t);} 135 System.out.println("\nPassed = " + passed + " failed = " + failed); 136 if (failed > 0) throw new AssertionError("Some tests failed");} 137 }