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.nio.file.Path;
  26 import java.util.HashMap;
  27 import java.util.Map;
  28 
  29 public class LinuxHelper {
  30     static String getRelease(JPackageCommand cmd) {
  31         return cmd.getArgumentValue("--linux-app-release", () -> "1");
  32     }
  33 
  34     static String getPackageName(JPackageCommand cmd) {
  35         return cmd.name().toLowerCase();
  36     }
  37 
  38     static String getBundleName(JPackageCommand cmd) {
  39         final String release = getRelease(cmd);
  40         final String version = cmd.version();
  41 
  42         final PackageType packageType = cmd.packageType();
  43         String format = null;
  44         switch (packageType) {
  45             case LINUX_DEB:
  46                 format = "%s_%s-%s_%s";
  47                 break;
  48 
  49             case LINUX_RPM:
  50                 format = "%s-%s-%s.%s";
  51                 break;
  52         }
  53         return String.format(format,
  54                 getPackageName(cmd), version, release, getPackageArch(packageType))
  55                 + packageType.getSuffix();
  56     }
  57 
  58     static Path getLauncherPath(JPackageCommand cmd) {
  59         final String launcherName = cmd.name();
  60         final Path packageFile = cmd.outputBundle();
  61 
  62         Executor exec = new Executor();
  63         exec.saveOutput();
  64         final PackageType packageType = PackageType.fromSuffix(
  65                 packageFile.toString());
  66         switch (packageType) {
  67             case LINUX_DEB:
  68                 exec.setExecutable("dpkg")
  69                         .addArgument("--contents")
  70                         .addArgument(packageFile);
  71                 break;
  72 
  73             case LINUX_RPM:
  74                 exec.setExecutable("rpm")
  75                         .addArgument("-qpl")
  76                         .addArgument(packageFile);
  77                 break;
  78         }
  79 
  80         final String launcherRelativePath = Path.of("/", "bin", launcherName).toString();
  81         for (String line : exec.execute().assertExitCodeIsZero().getOutput()) {
  82             if (line.endsWith(launcherRelativePath)) {
  83                 if (packageType == PackageType.LINUX_DEB) {
  84                     // Typical text lines produced by dpkg look like:
  85                     // drwxr-xr-x root/root         0 2019-08-30 05:30 ./opt/appcategorytest/runtime/lib/
  86                     // -rw-r--r-- root/root    574912 2019-08-30 05:30 ./opt/appcategorytest/runtime/lib/libmlib_image.so
  87                     // Need to skip all fields but absolute path to file.
  88                     line = line.substring(line.indexOf(" ./") + 2);
  89                 }
  90                 return Path.of(line);
  91             }
  92         }
  93 
  94         Test.assertUnexpected(String.format("Failed to find %s in %s package",
  95                 launcherName));
  96         return null;
  97     }
  98 
  99     public static String getDebBundleProperty(Path bundle, String fieldName) {
 100         return new Executor()
 101                 .saveFirstLineOfOutput()
 102                 .setExecutable("dpkg-deb")
 103                 .addArguments("-f", bundle.toString(), fieldName)
 104                 .execute()
 105                 .assertExitCodeIsZero().getFirstLineOfOutput();
 106     }
 107 
 108     public static String geRpmBundleProperty(Path bundle, String fieldName) {
 109         return new Executor()
 110                 .saveFirstLineOfOutput()
 111                 .setExecutable("rpm")
 112                 .addArguments(
 113                         "-qp",
 114                         "--queryformat",
 115                         String.format("%%{%s}", fieldName),
 116                         bundle.toString())
 117                 .execute()
 118                 .assertExitCodeIsZero().getFirstLineOfOutput();
 119     }
 120 
 121     private static String getPackageArch(PackageType type) {
 122         if (archs == null) {
 123             archs = new HashMap<>();
 124         }
 125 
 126         String arch = archs.get(type);
 127         if (arch == null) {
 128             Executor exec = new Executor();
 129             exec.saveFirstLineOfOutput();
 130             switch (type) {
 131                 case LINUX_DEB:
 132                     exec.setExecutable("dpkg").addArgument(
 133                             "--print-architecture");
 134                     break;
 135 
 136                 case LINUX_RPM:
 137                     exec.setExecutable("rpmbuild").addArgument(
 138                             "--eval=%{_target_cpu}");
 139                     break;
 140             }
 141             arch = exec.execute().assertExitCodeIsZero().getFirstLineOfOutput();
 142             archs.put(type, arch);
 143         }
 144         return arch;
 145     }
 146 
 147     static private Map<PackageType, String> archs;
 148 }