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 * @modules jdk.jartool/sun.tools.jar 32 * @summary jar tool fails to convert file separation characters for list and extract 33 * @author Sean Chou 34 */ 35 36 import java.io.File; 37 import java.io.FileOutputStream; 38 import java.io.IOException; 39 import java.io.PipedInputStream; 40 import java.io.PipedOutputStream; 41 import java.io.PrintStream; 42 import java.util.ArrayList; 43 import java.util.List; 44 import java.util.jar.JarEntry; 45 import java.util.jar.JarOutputStream; 46 47 import sun.tools.jar.Main; 48 49 public class JarBackSlash { 50 51 // used construct an entry JarBackSlash/dir/file.txt 52 private static String JARBACKSLASH = "JarBackSlash"; 53 private static String DIR = "dir"; 54 private static String FILENAME = "file.txt"; 55 56 private static File createJarFile() throws IOException { 57 File jarFile = File.createTempFile("JarBackSlashTest", ".jar"); 58 jarFile.deleteOnExit(); 59 60 try (JarOutputStream output = new JarOutputStream(new FileOutputStream(jarFile))) { 61 JarEntry entry = new JarEntry(JARBACKSLASH + "/" + DIR + "/" + FILENAME); 62 output.putNextEntry(entry); 63 } 64 65 return jarFile; 66 } 67 68 private static void testJarList(String jarFile) throws IOException { 69 List<String> argList = new ArrayList<String>(); 70 argList.add("-tvf"); 71 argList.add(jarFile); 72 argList.add(JARBACKSLASH + File.separatorChar + DIR + File.separatorChar + FILENAME); 73 74 String jarArgs[] = new String[argList.size()]; 75 jarArgs = argList.toArray(jarArgs); 76 77 PipedOutputStream pipedOutput = new PipedOutputStream(); 78 PipedInputStream pipedInput = new PipedInputStream(pipedOutput); 79 PrintStream out = new PrintStream(pipedOutput); 80 81 Main jarTool = new Main(out, System.err, "jar"); 82 if (!jarTool.run(jarArgs)) { 83 fail("Could not list jar file."); 84 } 85 86 out.flush(); 87 check(pipedInput.available() > 0); 88 } 89 90 91 private static void testJarExtract(String jarFile) throws IOException { 92 List<String> argList = new ArrayList<String>(); 93 argList.add("-xvf"); 94 argList.add(jarFile); 95 argList.add(JARBACKSLASH + File.separatorChar + DIR + File.separatorChar + FILENAME); 96 97 String jarArgs[] = new String[argList.size()]; 98 jarArgs = argList.toArray(jarArgs); 99 100 PipedOutputStream pipedOutput = new PipedOutputStream(); 101 PipedInputStream pipedInput = new PipedInputStream(pipedOutput); 102 PrintStream out = new PrintStream(pipedOutput); 103 104 Main jarTool = new Main(out, System.err, "jar"); 105 if (!jarTool.run(jarArgs)) { 106 fail("Could not list jar file."); 107 } 108 109 out.flush(); 110 check(pipedInput.available() > 0); 111 } 112 113 public static void realMain(String[] args) throws Throwable { 114 File tmpJarFile = createJarFile(); 115 String tmpJarFilePath = tmpJarFile.getAbsolutePath(); 116 117 testJarList(tmpJarFilePath); 118 testJarExtract(tmpJarFilePath); 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();} | 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 * @modules jdk.jartool 32 * @summary jar tool fails to convert file separation characters for list and extract 33 * @author Sean Chou 34 */ 35 36 import java.io.File; 37 import java.io.FileOutputStream; 38 import java.io.IOException; 39 import java.io.PipedInputStream; 40 import java.io.PipedOutputStream; 41 import java.io.PrintStream; 42 import java.util.ArrayList; 43 import java.util.List; 44 import java.util.jar.JarEntry; 45 import java.util.jar.JarOutputStream; 46 import java.util.spi.ToolProvider; 47 48 public class JarBackSlash { 49 private static final ToolProvider JAR_TOOL = ToolProvider.findFirst("jar") 50 .orElseThrow(() -> 51 new RuntimeException("jar tool not found") 52 ); 53 54 // used construct an entry JarBackSlash/dir/file.txt 55 private static String JARBACKSLASH = "JarBackSlash"; 56 private static String DIR = "dir"; 57 private static String FILENAME = "file.txt"; 58 59 private static File createJarFile() throws IOException { 60 File jarFile = File.createTempFile("JarBackSlashTest", ".jar"); 61 jarFile.deleteOnExit(); 62 63 try (JarOutputStream output = new JarOutputStream(new FileOutputStream(jarFile))) { 64 JarEntry entry = new JarEntry(JARBACKSLASH + "/" + DIR + "/" + FILENAME); 65 output.putNextEntry(entry); 66 } 67 68 return jarFile; 69 } 70 71 private static void testJarList(String jarFile) throws IOException { 72 List<String> argList = new ArrayList<String>(); 73 argList.add("-tvf"); 74 argList.add(jarFile); 75 argList.add(JARBACKSLASH + File.separatorChar + DIR + File.separatorChar + FILENAME); 76 77 String jarArgs[] = new String[argList.size()]; 78 jarArgs = argList.toArray(jarArgs); 79 80 PipedOutputStream pipedOutput = new PipedOutputStream(); 81 PipedInputStream pipedInput = new PipedInputStream(pipedOutput); 82 PrintStream out = new PrintStream(pipedOutput); 83 84 int rc = JAR_TOOL.run(out, System.err, jarArgs); 85 if (rc != 0) { 86 fail("Could not list jar file."); 87 } 88 89 out.flush(); 90 check(pipedInput.available() > 0); 91 } 92 93 94 private static void testJarExtract(String jarFile) throws IOException { 95 List<String> argList = new ArrayList<String>(); 96 argList.add("-xvf"); 97 argList.add(jarFile); 98 argList.add(JARBACKSLASH + File.separatorChar + DIR + File.separatorChar + FILENAME); 99 100 String jarArgs[] = new String[argList.size()]; 101 jarArgs = argList.toArray(jarArgs); 102 103 PipedOutputStream pipedOutput = new PipedOutputStream(); 104 PipedInputStream pipedInput = new PipedInputStream(pipedOutput); 105 PrintStream out = new PrintStream(pipedOutput); 106 107 int rc = JAR_TOOL.run(out, System.err, jarArgs); 108 if (rc != 0) { 109 fail("Could not list jar file."); 110 } 111 112 out.flush(); 113 check(pipedInput.available() > 0); 114 } 115 116 public static void realMain(String[] args) throws Throwable { 117 File tmpJarFile = createJarFile(); 118 String tmpJarFilePath = tmpJarFile.getAbsolutePath(); 119 120 testJarList(tmpJarFilePath); 121 testJarExtract(tmpJarFilePath); 122 } 123 124 125 //--------------------- Infrastructure --------------------------- 126 static volatile int passed = 0, failed = 0; 127 static void pass() {passed++;} 128 static void fail() {failed++; Thread.dumpStack();} |