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/sun.tools.jar 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.stream.Stream; 36 import sun.tools.jar.Main; 37 38 public class ChangeDir { 39 private final static String jarName = "test.jar"; 40 private final static String fileName = "hello.txt"; 41 42 /** Remove dirs & files needed for test. */ 43 private static void cleanup(Path dir) { 44 try { 45 if (Files.isDirectory(dir)) { 46 try (Stream<Path> s = Files.list(dir)) { 47 s.forEach( p -> cleanup(p)); 48 } 49 } 50 Files.delete(dir); 51 } catch (IOException x) { 52 fail(x.toString()); 53 } 54 } 55 56 public static void realMain(String[] args) throws Throwable { 57 doTest("/"); 58 doTest("//"); 59 doTest("///"); 60 doTest("////"); 61 if (System.getProperty("os.name").startsWith("Windows")) { 62 doTest("\\"); 63 doTest("\\\\"); 64 doTest("\\\\\\"); 65 doTest("\\\\\\\\"); 66 doTest("\\/"); 67 } 68 } 69 70 static void doTest(String sep) throws Throwable { 71 Path topDir = Files.createTempDirectory("delete"); 72 try { 73 Files.deleteIfExists(Paths.get(jarName)); 74 75 // Create a subdirectory "a/b" 76 Path testDir = Files.createDirectories(topDir.resolve("a").resolve("b")); 77 78 // Create file in that subdirectory 79 Path testFile = testDir.resolve(fileName); 80 Files.createFile(testFile); 81 82 // Create a jar file from that subdirectory, but with a // in the 83 // path name. 84 List<String> argList = new ArrayList<String>(); 85 argList.add("cf"); 86 argList.add(jarName); 87 argList.add("-C"); 88 argList.add(topDir.toString() + sep + "a" + sep + sep + "b"); // Note double 'sep' is intentional 89 argList.add(fileName); 90 91 Main jarTool = new Main(System.out, System.err, "jar"); 92 if (!jarTool.run(argList.toArray(new String[argList.size()]))) { 93 fail("Could not create jar file."); 94 } 95 96 // Check that the entry for hello.txt does *not* have a pathname. 97 try (JarFile jf = new JarFile(jarName)) { 98 for (Enumeration<JarEntry> i = jf.entries(); i.hasMoreElements();) { 99 JarEntry je = i.nextElement(); 100 String name = je.getName(); 101 if (name.indexOf(fileName) != -1) { 102 if (name.indexOf(fileName) != 0) { 103 fail(String.format( 104 "Expected '%s' but got '%s'%n", fileName, name)); 105 } else { 106 pass(); 107 } 108 } 109 } 110 } 111 } finally { 112 cleanup(topDir); 113 Files.deleteIfExists(Paths.get(jarName)); 114 } 115 } 116 117 //--------------------- Infrastructure --------------------------- 118 static volatile int passed = 0, failed = 0; 119 static void pass() {passed++;} 120 static void fail() {failed++; Thread.dumpStack();} 121 static void fail(String msg) {System.out.println(msg); fail();} 122 static void unexpected(Throwable t) {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 }