1 /*
   2  * Copyright (c) 2012 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  * Portions Copyright (c) 2012 IBM Corporation
  26  */
  27 
  28 /*
  29  * @test
  30  * @bug 7201156
  31  * @summary jar tool fails to convert file separation characters for list and extract
  32  * @author Sean Chou
  33  */
  34 
  35 import java.io.File;
  36 import java.io.FileOutputStream;
  37 import java.io.IOException;
  38 import java.io.PipedInputStream;
  39 import java.io.PipedOutputStream;
  40 import java.io.PrintStream;
  41 import java.nio.file.Files;
  42 import java.nio.file.Paths;
  43 import java.util.ArrayList;
  44 import java.util.List;
  45 import java.util.jar.JarEntry;
  46 import java.util.jar.JarOutputStream;
  47 
  48 import sun.tools.jar.Main;
  49 
  50 public class JarBackSlash {
  51 
  52     // used construct an entry JarBackSlash/dir/file.txt
  53     private static String JARBACKSLASH = "JarBackSlash";
  54     private static String DIR = "dir";
  55     private static String FILENAME = "file.txt";
  56 
  57     private static File createJarFile() throws IOException {
  58         File jarFile = File.createTempFile("JarBackSlashTest", ".jar");
  59         jarFile.deleteOnExit();
  60 
  61         JarOutputStream output = new JarOutputStream(new FileOutputStream(jarFile));
  62         JarEntry entry = new JarEntry(JARBACKSLASH + "/" + DIR + "/" + FILENAME);
  63         output.putNextEntry(entry);
  64         output.close();
  65 
  66         return jarFile;
  67     }
  68 
  69     private static void testJarList(String jarFile) throws IOException {
  70         List<String> argList = new ArrayList<String>();
  71         argList.add("-tvf");
  72         argList.add(jarFile);
  73         argList.add(JARBACKSLASH + File.separatorChar + DIR + File.separatorChar + FILENAME);
  74 
  75         String jarArgs[] = new String[argList.size()];
  76         jarArgs = argList.toArray(jarArgs);
  77 
  78         PipedOutputStream pipedOutput = new PipedOutputStream();
  79         PipedInputStream pipedInput = new PipedInputStream(pipedOutput);
  80         PrintStream out = new PrintStream(pipedOutput);
  81 
  82         Main jarTool = new Main(out, System.err, "jar");
  83         if (!jarTool.run(jarArgs)) {
  84             fail("Could not list jar file.");
  85         }
  86 
  87         out.flush();
  88         check(pipedInput.available() > 0);
  89     }
  90 
  91 
  92     private static void testJarExact(String jarFile) throws IOException {
  93         List<String> argList = new ArrayList<String>();
  94         argList.add("-xvf");
  95         argList.add(jarFile);
  96         argList.add(JARBACKSLASH + File.separatorChar + DIR + File.separatorChar + FILENAME);
  97 
  98         String jarArgs[] = new String[argList.size()];
  99         jarArgs = argList.toArray(jarArgs);
 100 
 101         PipedOutputStream pipedOutput = new PipedOutputStream();
 102         PipedInputStream pipedInput = new PipedInputStream(pipedOutput);
 103         PrintStream out = new PrintStream(pipedOutput);
 104 
 105         Main jarTool = new Main(out, System.err, "jar");
 106         if (!jarTool.run(jarArgs)) {
 107             fail("Could not list jar file.");
 108         }
 109 
 110         out.flush();
 111 
 112         if (pipedInput.available() > 0) {
 113             // remove the created file
 114             Files.deleteIfExists(Paths.get("./", JARBACKSLASH, DIR, FILENAME));
 115             Files.deleteIfExists(Paths.get("./", JARBACKSLASH, DIR));
 116             Files.deleteIfExists(Paths.get("./", JARBACKSLASH));
 117 
 118             return;
 119         } else {
 120             fail("jar failed to extract file with different file separator");
 121         }
 122     }
 123 
 124     public static void realMain(String[] args) throws Throwable {
 125         File tmpJarFile = createJarFile();
 126         String pathRtJar = tmpJarFile.getAbsolutePath();
 127 
 128         testJarList(pathRtJar);
 129         testJarExact(pathRtJar);
 130     }
 131 
 132 
 133     //--------------------- Infrastructure ---------------------------
 134     static volatile int passed = 0, failed = 0;
 135     static void pass() {passed++;}
 136     static void fail() {failed++; Thread.dumpStack();}
 137     static void fail(String msg) {System.out.println(msg); fail();}
 138     static void unexpected(Throwable t) {failed++; t.printStackTrace();}
 139     static void check(boolean cond) {if (cond) pass(); else fail();}
 140     static void equal(Object x, Object y) {
 141         if (x == null ? y == null : x.equals(y)) pass();
 142         else fail(x + " not equal to " + y);}
 143     public static void main(String[] args) throws Throwable {
 144         try {realMain(args);} catch (Throwable t) {unexpected(t);}
 145         System.out.println("\nPassed = " + passed + " failed = " + failed);
 146         if (failed > 0) throw new AssertionError("Some tests failed");}
 147 }