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