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("//"); 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); | 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("//"); 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); |