2668N/A/*
4248N/A * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
2668N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
2668N/A *
2668N/A * This code is free software; you can redistribute it and/or modify it
2668N/A * under the terms of the GNU General Public License version 2 only, as
2668N/A * published by the Free Software Foundation.
2668N/A *
2668N/A * This code is distributed in the hope that it will be useful, but WITHOUT
2668N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
2668N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
2668N/A * version 2 for more details (a copy is included in the LICENSE file that
2668N/A * accompanied this code).
2668N/A *
2668N/A * You should have received a copy of the GNU General Public License version
2668N/A * 2 along with this work; if not, write to the Free Software Foundation,
2668N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
2668N/A *
2668N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2668N/A * or visit www.oracle.com if you need additional information or have any
2668N/A * questions.
2668N/A */
2668N/A
3883N/Aimport java.nio.file.Path;
2668N/Aimport java.io.BufferedReader;
2668N/Aimport java.io.ByteArrayOutputStream;
2668N/Aimport java.io.Closeable;
2668N/Aimport java.io.File;
2668N/Aimport java.io.FileFilter;
2668N/Aimport java.io.FileOutputStream;
2668N/Aimport java.io.IOException;
2668N/Aimport java.io.InputStream;
2668N/Aimport java.io.InputStreamReader;
2668N/Aimport java.io.PrintStream;
3883N/Aimport java.nio.file.Files;
2668N/Aimport java.util.ArrayList;
2668N/Aimport java.util.Arrays;
2668N/Aimport java.util.Collections;
2668N/Aimport java.util.List;
2668N/Aimport java.util.Map;
2668N/Aimport java.util.jar.JarFile;
2668N/Aimport java.util.jar.JarOutputStream;
2668N/Aimport java.util.jar.Pack200;
2668N/Aimport java.util.zip.ZipEntry;
2668N/Aimport java.util.zip.ZipFile;
2668N/A
3883N/Aimport static java.nio.file.StandardCopyOption.*;
3883N/A
2668N/A/**
2668N/A *
2668N/A * @author ksrini
2668N/A */
2668N/A
2668N/A/*
2668N/A * This class contains all the commonly used utilities used by various tests
2668N/A * in this directory.
2668N/A */
2668N/Aclass Utils {
2668N/A static final String JavaHome = System.getProperty("test.java",
2668N/A System.getProperty("java.home"));
2668N/A static final boolean IsWindows =
2668N/A System.getProperty("os.name").startsWith("Windows");
2668N/A static final boolean Is64Bit =
2668N/A System.getProperty("sun.arch.data.model", "32").equals("64");
2668N/A static final File JavaSDK = new File(JavaHome).getParentFile();
2668N/A
2668N/A static final String PACK_FILE_EXT = ".pack";
2668N/A static final String JAVA_FILE_EXT = ".java";
2668N/A static final String CLASS_FILE_EXT = ".class";
2668N/A static final String JAR_FILE_EXT = ".jar";
2668N/A
2668N/A static final File TEST_SRC_DIR = new File(System.getProperty("test.src"));
2668N/A static final String VERIFIER_DIR_NAME = "pack200-verifier";
2668N/A static final File VerifierJar = new File(VERIFIER_DIR_NAME + JAR_FILE_EXT);
2668N/A
2668N/A private Utils() {} // all static
2668N/A
2668N/A static {
2668N/A if (!JavaHome.endsWith("jre")) {
2668N/A throw new RuntimeException("Error: requires an SDK to run");
2668N/A }
2668N/A }
2668N/A
2668N/A private static void init() throws IOException {
2668N/A if (VerifierJar.exists()) {
2668N/A return;
2668N/A }
2668N/A File srcDir = new File(TEST_SRC_DIR, VERIFIER_DIR_NAME);
2668N/A List<File> javaFileList = findFiles(srcDir, createFilter(JAVA_FILE_EXT));
2668N/A File tmpFile = File.createTempFile("javac", ".tmp");
2668N/A File classesDir = new File("xclasses");
2668N/A classesDir.mkdirs();
2668N/A FileOutputStream fos = null;
2668N/A PrintStream ps = null;
2668N/A try {
2668N/A fos = new FileOutputStream(tmpFile);
2668N/A ps = new PrintStream(fos);
2668N/A for (File f : javaFileList) {
2668N/A ps.println(f.getAbsolutePath());
2668N/A }
2668N/A } finally {
2668N/A close(ps);
2668N/A close(fos);
2668N/A }
2668N/A
2668N/A compiler("-d",
2668N/A "xclasses",
2668N/A "@" + tmpFile.getAbsolutePath());
2668N/A
2668N/A jar("cvfe",
2668N/A VerifierJar.getName(),
2668N/A "sun.tools.pack.verify.Main",
2668N/A "-C",
2668N/A "xclasses",
2668N/A ".");
2668N/A }
2668N/A
2668N/A static void dirlist(File dir) {
2668N/A File[] files = dir.listFiles();
2668N/A System.out.println("--listing " + dir.getAbsolutePath() + "---");
2668N/A for (File f : files) {
2668N/A StringBuffer sb = new StringBuffer();
2668N/A sb.append(f.isDirectory() ? "d " : "- ");
2668N/A sb.append(f.getName());
2668N/A System.out.println(sb);
2668N/A }
2668N/A }
2668N/A static void doCompareVerify(File reference, File specimen) throws IOException {
2668N/A init();
2668N/A List<String> cmds = new ArrayList<String>();
2668N/A cmds.add(getJavaCmd());
2668N/A cmds.add("-jar");
2668N/A cmds.add(VerifierJar.getName());
2668N/A cmds.add(reference.getAbsolutePath());
2668N/A cmds.add(specimen.getAbsolutePath());
2668N/A cmds.add("-O");
2668N/A runExec(cmds);
2668N/A }
2668N/A
2668N/A static void doCompareBitWise(File reference, File specimen)
2668N/A throws IOException {
2668N/A init();
2668N/A List<String> cmds = new ArrayList<String>();
2668N/A cmds.add(getJavaCmd());
2668N/A cmds.add("-jar");
2668N/A cmds.add(VerifierJar.getName());
2668N/A cmds.add(reference.getName());
2668N/A cmds.add(specimen.getName());
2668N/A cmds.add("-O");
2668N/A cmds.add("-b");
2668N/A runExec(cmds);
2668N/A }
2668N/A
2668N/A static FileFilter createFilter(final String extension) {
2668N/A return new FileFilter() {
2668N/A @Override
2668N/A public boolean accept(File pathname) {
2668N/A String name = pathname.getName();
2668N/A if (name.endsWith(extension)) {
2668N/A return true;
2668N/A }
2668N/A return false;
2668N/A }
2668N/A };
2668N/A }
2668N/A
2668N/A static final FileFilter DIR_FILTER = new FileFilter() {
2668N/A public boolean accept(File pathname) {
2668N/A if (pathname.isDirectory()) {
2668N/A return true;
2668N/A }
2668N/A return false;
2668N/A }
2668N/A };
2668N/A
2668N/A static final FileFilter FILE_FILTER = new FileFilter() {
2668N/A public boolean accept(File pathname) {
2668N/A if (pathname.isFile()) {
2668N/A return true;
2668N/A }
2668N/A return false;
2668N/A }
2668N/A };
2668N/A
2668N/A static void copyFile(File src, File dst) throws IOException {
3883N/A Path parent = dst.toPath().getParent();
3883N/A if (parent != null) {
3883N/A Files.createDirectories(parent);
2668N/A }
3883N/A Files.copy(src.toPath(), dst.toPath(), COPY_ATTRIBUTES, REPLACE_EXISTING);
2668N/A }
2668N/A
2674N/A static String baseName(File file, String extension) {
2674N/A return baseName(file.getAbsolutePath(), extension);
2674N/A }
2674N/A
2674N/A static String baseName(String name, String extension) {
2674N/A int cut = name.length() - extension.length();
2674N/A return name.lastIndexOf(extension) == cut
2674N/A ? name.substring(0, cut)
2674N/A : name;
2674N/A
2674N/A }
2674N/A
2668N/A /*
2668N/A * Suppose a path is provided which consists of a full path
2668N/A * this method returns the sub path for a full path ex: /foo/bar/baz/foobar.z
2668N/A * and the base path is /foo/bar it will will return baz/foobar.z.
2668N/A */
2668N/A private static String getEntryPath(String basePath, String fullPath) {
2668N/A if (!fullPath.startsWith(basePath)) {
2668N/A return null;
2668N/A }
2668N/A return fullPath.substring(basePath.length());
2668N/A }
2668N/A
2668N/A static String getEntryPath(File basePathFile, File fullPathFile) {
2668N/A return getEntryPath(basePathFile.toString(), fullPathFile.toString());
2668N/A }
2668N/A
2668N/A public static void recursiveCopy(File src, File dest) throws IOException {
2668N/A if (!src.exists() || !src.canRead()) {
2668N/A throw new IOException("file not found or readable: " + src);
2668N/A }
2668N/A if (dest.exists() && !dest.isDirectory() && !dest.canWrite()) {
2668N/A throw new IOException("file not found or writeable: " + dest);
2668N/A }
2668N/A if (!dest.exists()) {
2668N/A dest.mkdirs();
2668N/A }
2668N/A List<File> a = directoryList(src);
2668N/A for (File f : a) {
2668N/A copyFile(f, new File(dest, getEntryPath(src, f)));
2668N/A }
2668N/A }
2668N/A
2668N/A static List<File> directoryList(File dirname) {
2668N/A List<File> dirList = new ArrayList<File>();
2668N/A return directoryList(dirname, dirList, null);
2668N/A }
2668N/A
2668N/A private static List<File> directoryList(File dirname, List<File> dirList,
2668N/A File[] dirs) {
2668N/A dirList.addAll(Arrays.asList(dirname.listFiles(FILE_FILTER)));
2668N/A dirs = dirname.listFiles(DIR_FILTER);
2668N/A for (File f : dirs) {
2668N/A if (f.isDirectory() && !f.equals(dirname)) {
2668N/A dirList.add(f);
2668N/A directoryList(f, dirList, dirs);
2668N/A }
2668N/A }
2668N/A return dirList;
2668N/A }
2668N/A
2668N/A static void recursiveDelete(File dir) throws IOException {
2668N/A if (dir.isFile()) {
2668N/A dir.delete();
2668N/A } else if (dir.isDirectory()) {
2668N/A File[] entries = dir.listFiles();
2668N/A for (int i = 0; i < entries.length; i++) {
2668N/A if (entries[i].isDirectory()) {
2668N/A recursiveDelete(entries[i]);
2668N/A }
2668N/A entries[i].delete();
2668N/A }
2668N/A dir.delete();
2668N/A }
2668N/A }
2668N/A
2668N/A static List<File> findFiles(File startDir, FileFilter filter)
2668N/A throws IOException {
2668N/A List<File> list = new ArrayList<File>();
2668N/A findFiles0(startDir, list, filter);
2668N/A return list;
2668N/A }
2668N/A /*
2668N/A * finds files in the start directory using the the filter, appends
2668N/A * the files to the dirList.
2668N/A */
2668N/A private static void findFiles0(File startDir, List<File> list,
2668N/A FileFilter filter) throws IOException {
2668N/A File[] foundFiles = startDir.listFiles(filter);
2668N/A list.addAll(Arrays.asList(foundFiles));
2668N/A File[] dirs = startDir.listFiles(DIR_FILTER);
2668N/A for (File dir : dirs) {
2668N/A findFiles0(dir, list, filter);
2668N/A }
2668N/A }
2668N/A
2668N/A static void close(Closeable c) {
2668N/A if (c == null) {
2668N/A return;
2668N/A }
2668N/A try {
2668N/A c.close();
2668N/A } catch (IOException ignore) {
2668N/A }
2668N/A }
2668N/A
2668N/A static void compiler(String... javacCmds) {
2668N/A if (com.sun.tools.javac.Main.compile(javacCmds) != 0) {
2668N/A throw new RuntimeException("compilation failed");
2668N/A }
2668N/A }
2668N/A
2668N/A static void jar(String... jargs) {
2668N/A sun.tools.jar.Main jarTool =
2668N/A new sun.tools.jar.Main(System.out, System.err, "jartool");
2668N/A if (!jarTool.run(jargs)) {
2668N/A throw new RuntimeException("jar command failed");
2668N/A }
2668N/A }
2668N/A
2668N/A // given a jar file foo.jar will write to foo.pack
2668N/A static void pack(JarFile jarFile, File packFile) throws IOException {
2668N/A Pack200.Packer packer = Pack200.newPacker();
2668N/A Map<String, String> p = packer.properties();
2668N/A // Take the time optimization vs. space
2668N/A p.put(packer.EFFORT, "1"); // CAUTION: do not use 0.
2668N/A // Make the memory consumption as effective as possible
2668N/A p.put(packer.SEGMENT_LIMIT, "10000");
2668N/A // ignore all JAR deflation requests to save time
2668N/A p.put(packer.DEFLATE_HINT, packer.FALSE);
2668N/A // save the file ordering of the original JAR
2668N/A p.put(packer.KEEP_FILE_ORDER, packer.TRUE);
2668N/A FileOutputStream fos = null;
2668N/A try {
2668N/A // Write out to a jtreg scratch area
2668N/A fos = new FileOutputStream(packFile);
2668N/A // Call the packer
2668N/A packer.pack(jarFile, fos);
2668N/A } finally {
2668N/A close(fos);
2668N/A }
2668N/A }
2668N/A
2668N/A // uses java unpacker, slow but useful to discover issues with the packer
2668N/A static void unpackj(File inFile, JarOutputStream jarStream)
2668N/A throws IOException {
2668N/A unpack0(inFile, jarStream, true);
2668N/A
2668N/A }
2668N/A
2668N/A // uses native unpacker using the java APIs
2668N/A static void unpackn(File inFile, JarOutputStream jarStream)
2668N/A throws IOException {
2668N/A unpack0(inFile, jarStream, false);
2668N/A }
2668N/A
2668N/A // given a packed file, create the jar file in the current directory.
2668N/A private static void unpack0(File inFile, JarOutputStream jarStream,
2668N/A boolean useJavaUnpack) throws IOException {
2668N/A // Unpack the files
2668N/A Pack200.Unpacker unpacker = Pack200.newUnpacker();
2668N/A Map<String, String> props = unpacker.properties();
2668N/A if (useJavaUnpack) {
2668N/A props.put("com.sun.java.util.jar.pack.disable.native", "true");
2668N/A }
2668N/A // Call the unpacker
2668N/A unpacker.unpack(inFile, jarStream);
2668N/A }
2668N/A
2668N/A static byte[] getBuffer(ZipFile zf, ZipEntry ze) throws IOException {
2668N/A ByteArrayOutputStream baos = new ByteArrayOutputStream();
2668N/A byte buf[] = new byte[8192];
2668N/A InputStream is = null;
2668N/A try {
2668N/A is = zf.getInputStream(ze);
2668N/A int n = is.read(buf);
2668N/A while (n > 0) {
2668N/A baos.write(buf, 0, n);
2668N/A n = is.read(buf);
2668N/A }
2668N/A return baos.toByteArray();
2668N/A } finally {
2668N/A close(is);
2668N/A }
2668N/A }
2668N/A
2668N/A static ArrayList<String> getZipFileEntryNames(ZipFile z) {
2668N/A ArrayList<String> out = new ArrayList<String>();
2668N/A for (ZipEntry ze : Collections.list(z.entries())) {
2668N/A out.add(ze.getName());
2668N/A }
2668N/A return out;
2668N/A }
2668N/A static List<String> runExec(List<String> cmdsList) {
2677N/A return runExec(cmdsList, null);
2677N/A }
2677N/A static List<String> runExec(List<String> cmdsList, Map<String, String> penv) {
2668N/A ArrayList<String> alist = new ArrayList<String>();
2668N/A ProcessBuilder pb =
2668N/A new ProcessBuilder(cmdsList);
2668N/A Map<String, String> env = pb.environment();
2677N/A if (penv != null && !penv.isEmpty()) {
2677N/A env.putAll(penv);
2677N/A }
2668N/A pb.directory(new File("."));
2668N/A dirlist(new File("."));
2668N/A for (String x : cmdsList) {
2668N/A System.out.print(x + " ");
2668N/A }
2668N/A System.out.println("");
2668N/A int retval = 0;
2668N/A Process p = null;
2668N/A InputStreamReader ir = null;
2668N/A BufferedReader rd = null;
2668N/A InputStream is = null;
2668N/A try {
2668N/A pb.redirectErrorStream(true);
2668N/A p = pb.start();
2668N/A is = p.getInputStream();
2668N/A ir = new InputStreamReader(is);
2668N/A rd = new BufferedReader(ir, 8192);
2668N/A
2668N/A String in = rd.readLine();
2668N/A while (in != null) {
2668N/A alist.add(in);
2668N/A System.out.println(in);
2668N/A in = rd.readLine();
2668N/A }
2668N/A retval = p.waitFor();
2668N/A if (retval != 0) {
2668N/A throw new RuntimeException("process failed with non-zero exit");
2668N/A }
2668N/A } catch (Exception ex) {
2668N/A throw new RuntimeException(ex.getMessage());
2668N/A } finally {
2668N/A close(rd);
2668N/A close(ir);
2668N/A close(is);
2668N/A if (p != null) {
2668N/A p.destroy();
2668N/A }
2668N/A }
2668N/A return alist;
2668N/A }
2668N/A
2668N/A static String getUnpack200Cmd() {
2668N/A return getAjavaCmd("unpack200");
2668N/A }
2668N/A
2668N/A static String getPack200Cmd() {
2668N/A return getAjavaCmd("pack200");
2668N/A }
2668N/A
2668N/A static String getJavaCmd() {
2668N/A return getAjavaCmd("java");
2668N/A }
2668N/A
2668N/A static String getAjavaCmd(String cmdStr) {
2668N/A File binDir = new File(JavaHome, "bin");
2668N/A File unpack200File = IsWindows
2668N/A ? new File(binDir, cmdStr + ".exe")
2668N/A : new File(binDir, cmdStr);
2668N/A
2668N/A String cmd = unpack200File.getAbsolutePath();
2668N/A if (!unpack200File.canExecute()) {
2668N/A throw new RuntimeException("please check" +
2668N/A cmd + " exists and is executable");
2668N/A }
2668N/A return cmd;
2668N/A }
2668N/A
2668N/A private static List<File> locaterCache = null;
2668N/A // search the source dir and jdk dir for requested file and returns
2668N/A // the first location it finds.
2668N/A static File locateJar(String name) {
2668N/A try {
2668N/A if (locaterCache == null) {
2668N/A locaterCache = new ArrayList<File>();
2668N/A locaterCache.addAll(findFiles(TEST_SRC_DIR, createFilter(JAR_FILE_EXT)));
2668N/A locaterCache.addAll(findFiles(JavaSDK, createFilter(JAR_FILE_EXT)));
2668N/A }
2668N/A for (File f : locaterCache) {
2668N/A if (f.getName().equals(name)) {
2668N/A return f;
2668N/A }
2668N/A }
2668N/A throw new IOException("file not found: " + name);
2668N/A } catch (IOException e) {
2668N/A throw new RuntimeException(e);
2668N/A }
2668N/A }
2668N/A}