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 =
  40         ToolProvider.findFirst("jar").get();
  41 
  42     private final static String jarName = "test.jar";
  43     private final static String fileName = "hello.txt";
  44 
  45     /** Remove dirs & files needed for test. */
  46     private static void cleanup(Path dir) {
  47         try {
  48             if (Files.isDirectory(dir)) {
  49                 try (Stream<Path> s = Files.list(dir)) {
  50                     s.forEach( p -> cleanup(p));
  51                 }
  52             }
  53             Files.delete(dir);
  54         } catch (IOException x) {
  55             fail(x.toString());
  56         }
  57     }
  58 
  59     public static void realMain(String[] args) throws Throwable {
  60         doTest("/");
  61         doTest("//");
  62         doTest("///");
  63         doTest("////");
  64         if (System.getProperty("os.name").startsWith("Windows")) {
  65             doTest("\\");
  66             doTest("\\\\");
  67             doTest("\\\\\\");
  68             doTest("\\\\\\\\");
  69             doTest("\\/");
  70         }
  71     }
  72 
  73     static void doTest(String sep) throws Throwable {
  74         Path topDir = Files.createTempDirectory("delete");
  75         try {
  76             Files.deleteIfExists(Paths.get(jarName));
  77 
  78             // Create a subdirectory "a/b"
  79             Path testDir = Files.createDirectories(topDir.resolve("a").resolve("b"));
  80 
  81             // Create file in that subdirectory
  82             Path testFile = testDir.resolve(fileName);
  83             Files.createFile(testFile);
  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.toString() + sep + "a" + sep + sep + "b"); // Note double 'sep' is intentional
  92             argList.add(fileName);
  93 
  94             int rc = JAR_TOOL.run(System.out, System.err,
  95                                   argList.toArray(new String[argList.size()]));
  96             if (rc != 0) {
  97                 fail("Could not create jar file.");
  98             }
  99 
 100             // Check that the entry for hello.txt does *not* have a pathname.
 101             try (JarFile jf = new JarFile(jarName)) {
 102                 for (Enumeration<JarEntry> i = jf.entries(); i.hasMoreElements();) {
 103                     JarEntry je = i.nextElement();
 104                     String name = je.getName();
 105                     if (name.indexOf(fileName) != -1) {
 106                         if (name.indexOf(fileName) != 0) {
 107                             fail(String.format(
 108                                      "Expected '%s' but got '%s'%n", fileName, name));
 109                         } else {
 110                             pass();
 111                         }
 112                     }
 113                 }
 114             }
 115         } finally {
 116             cleanup(topDir);
 117             Files.deleteIfExists(Paths.get(jarName));
 118         }
 119     }
 120 
 121     //--------------------- Infrastructure ---------------------------
 122     static volatile int passed = 0, failed = 0;
 123     static void pass() {passed++;}
 124     static void fail() {failed++; Thread.dumpStack();}
 125     static void fail(String msg) {System.out.println(msg); fail();}
 126     static void unexpected(Throwable t) {failed++; t.printStackTrace();}
 127     static void check(boolean cond) {if (cond) pass(); else fail();}
 128     static void equal(Object x, Object y) {
 129         if (x == null ? y == null : x.equals(y)) pass();
 130         else fail(x + " not equal to " + y);}
 131     public static void main(String[] args) throws Throwable {
 132         try {realMain(args);} catch (Throwable t) {unexpected(t);}
 133         System.out.println("\nPassed = " + passed + " failed = " + failed);
 134         if (failed > 0) throw new AssertionError("Some tests failed");}
 135 }