1 /*
   2  * Copyright (c) 2019, 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 package jdk.jpackage.test;
  24 
  25 import java.io.ByteArrayInputStream;
  26 import java.io.IOException;
  27 import java.nio.charset.StandardCharsets;
  28 import java.nio.file.Files;
  29 import java.nio.file.Path;
  30 import java.util.List;
  31 import java.util.Set;
  32 import java.util.stream.Collectors;
  33 import java.util.stream.Stream;
  34 import javax.xml.parsers.DocumentBuilder;
  35 import javax.xml.parsers.DocumentBuilderFactory;
  36 import javax.xml.parsers.ParserConfigurationException;
  37 import javax.xml.xpath.XPath;
  38 import javax.xml.xpath.XPathConstants;
  39 import javax.xml.xpath.XPathFactory;
  40 import jdk.jpackage.test.Functional.ThrowingConsumer;
  41 import jdk.jpackage.test.Functional.ThrowingSupplier;
  42 import org.xml.sax.SAXException;
  43 
  44 public class MacHelper {
  45 
  46     public static void withExplodedDmg(JPackageCommand cmd,
  47             ThrowingConsumer<Path> consumer) {
  48         cmd.verifyIsOfType(PackageType.MAC_DMG);
  49 
  50         var plist = readPList(new Executor()
  51                 .setExecutable("/usr/bin/hdiutil")
  52                 .dumpOutput()
  53                 .addArguments("attach", cmd.outputBundle().toString(), "-plist")
  54                 .executeAndGetOutput());
  55 
  56         final Path mountPoint = Path.of(plist.queryValue("mount-point"));
  57         try {
  58             Path dmgImage = mountPoint.resolve(cmd.name() + ".app");
  59             TKit.trace(String.format("Exploded [%s] in [%s] directory",
  60                     cmd.outputBundle(), dmgImage));
  61             ThrowingConsumer.toConsumer(consumer).accept(dmgImage);
  62         } finally {
  63             new Executor()
  64                     .setExecutable("/usr/bin/hdiutil")
  65                     .addArgument("detach").addArgument(mountPoint)
  66                     .execute().assertExitCodeIsZero();
  67         }
  68     }
  69 
  70     public static PListWrapper readPListFromAppImage(Path appImage) {
  71         return readPList(appImage.resolve("Contents/Info.plist"));
  72     }
  73 
  74     public static PListWrapper readPList(Path path) {
  75         TKit.assertReadableFileExists(path);
  76         return ThrowingSupplier.toSupplier(() -> readPList(Files.readAllLines(
  77                 path))).get();
  78     }
  79 
  80     public static PListWrapper readPList(List<String> lines) {
  81         return readPList(lines.stream());
  82     }
  83 
  84     public static PListWrapper readPList(Stream<String> lines) {
  85         return ThrowingSupplier.toSupplier(() -> new PListWrapper(lines.collect(
  86                 Collectors.joining()))).get();
  87     }
  88 
  89     static String getBundleName(JPackageCommand cmd) {
  90         cmd.verifyIsOfType(PackageType.MAC);
  91         return String.format("%s-%s%s", getPackageName(cmd), cmd.version(),
  92                 cmd.packageType().getSuffix());
  93     }
  94 
  95     static Path getInstallationDirectory(JPackageCommand cmd) {
  96         cmd.verifyIsOfType(PackageType.MAC);
  97         return Path.of(cmd.getArgumentValue("--install-dir", () -> "/Applications"))
  98                 .resolve(cmd.name() + ".app");
  99     }
 100 
 101     private static String getPackageName(JPackageCommand cmd) {
 102         return cmd.getArgumentValue("--mac-package-name",
 103                 () -> cmd.name());
 104     }
 105 
 106     public static final class PListWrapper {
 107         public String queryValue(String keyName) {
 108             XPath xPath = XPathFactory.newInstance().newXPath();
 109             // Query for the value of <string> element preceding <key> element
 110             // with value equal to `keyName`
 111             String query = String.format(
 112                     "//string[preceding-sibling::key = \"%s\"][1]", keyName);
 113             return ThrowingSupplier.toSupplier(() -> (String) xPath.evaluate(
 114                     query, doc, XPathConstants.STRING)).get();
 115         }
 116 
 117         PListWrapper(String xml) throws ParserConfigurationException,
 118                 SAXException, IOException {
 119             doc = createDocumentBuilder().parse(new ByteArrayInputStream(
 120                     xml.getBytes(StandardCharsets.UTF_8)));
 121         }
 122 
 123         private static DocumentBuilder createDocumentBuilder() throws
 124                 ParserConfigurationException {
 125             DocumentBuilderFactory dbf = DocumentBuilderFactory.newDefaultInstance();
 126             dbf.setFeature(
 127                     "http://apache.org/xml/features/nonvalidating/load-external-dtd",
 128                     false);
 129             return dbf.newDocumentBuilder();
 130         }
 131 
 132         private final org.w3c.dom.Document doc;
 133     }
 134 
 135     static final Set<Path> CRITICAL_RUNTIME_FILES = Set.of(Path.of(
 136             "Contents/Home/lib/server/libjvm.dylib"));
 137 
 138 }