1 /*
   2  * Copyright (c) 2018, 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 
  24 import java.io.File;
  25 import java.nio.file.Files;
  26 import java.nio.file.Path;
  27 import java.util.ArrayList;
  28 import java.util.List;
  29 
  30 public class LicenseBase {
  31 
  32     private static String TEST_NAME;
  33     private static String EXT;
  34     private static String OUTPUT;
  35     private static String[] CMD;
  36 
  37     private static void copyResults() throws Exception {
  38         List<String> files = new ArrayList<>();
  39         files.add(OUTPUT.toLowerCase());
  40         JPackageInstallerHelper.copyTestResults(files);
  41     }
  42 
  43     private static void testCreateInstaller() throws Exception {
  44         JPackageHelper.executeCLI(true, CMD);
  45         JPackageInstallerHelper.validateOutput(OUTPUT);
  46         copyResults();
  47     }
  48 
  49     private static void verifyInstall() throws Exception {
  50         String app = JPackagePath.getLinuxInstalledApp(TEST_NAME);
  51         JPackageInstallerHelper.validateApp(app);
  52 
  53         File licenseFile = null;
  54         if (EXT.equals("rpm")) {
  55             licenseFile = getRpmLicenseFileInstallLocation();
  56         } else if (EXT.equals("deb")) {
  57             licenseFile = getDebLicenseFileInstallLocation();
  58         }
  59         if (!licenseFile.exists()) {
  60             throw new AssertionError(
  61                     "Error: " + licenseFile.getAbsolutePath() + " not found");
  62         }
  63     }
  64 
  65     private static File getRpmLicenseFileInstallLocation() throws Exception {
  66         final String infoResult = "infoResult.txt";
  67         int retVal = JPackageHelper.execute(new File(infoResult), "rpm",
  68                 "--eval", "%{_defaultlicensedir}");
  69         if (retVal != 0) {
  70             throw new AssertionError("rpm exited with error: " + retVal);
  71         }
  72 
  73         final String rootLicenseDir = Files.readString(Path.of(infoResult)).replaceAll(
  74                 "(\\r|\\n)", "");
  75 
  76         retVal = JPackageHelper.execute(new File(infoResult), "rpm",
  77                 "-q", "--queryformat", "%{name}-%{version}",
  78                 TEST_NAME.toLowerCase());
  79         if (retVal != 0) {
  80             throw new AssertionError("rpm exited with error: " + retVal);
  81         }
  82 
  83         final String testPackageName = Files.readString(Path.of(infoResult));
  84 
  85         return Path.of(rootLicenseDir, testPackageName, new File(
  86                 JPackagePath.getLicenseFilePath()).getName()).toFile();
  87     }
  88 
  89     private static File getDebLicenseFileInstallLocation() throws Exception {
  90         return Path.of("/usr", "share", "doc", TEST_NAME.toLowerCase(),
  91                 "copyright").toFile();
  92     }
  93 
  94     private static void verifyUnInstall() throws Exception {
  95         String folderPath = JPackagePath.getLinuxInstallFolder(TEST_NAME);
  96         File folder = new File(folderPath);
  97         if (folder.exists()) {
  98             throw new AssertionError(
  99                     "Error: " + folder.getAbsolutePath() + " exist");
 100         }
 101 
 102         if (EXT.equals("rpm")) {
 103             final File licenseFileFolder = getRpmLicenseFileInstallLocation().getParentFile();
 104             if (folder.exists()) {
 105                 throw new AssertionError(
 106                         "Error: " + licenseFileFolder.getAbsolutePath() + " exist");
 107             }
 108         } else if (EXT.equals("deb")) {
 109             final File licenseFileFolder = getDebLicenseFileInstallLocation().getParentFile();
 110             if (folder.exists()) {
 111                 throw new AssertionError(
 112                         "Error: " + licenseFileFolder.getAbsolutePath() + " exist");
 113             }
 114         }
 115     }
 116 
 117     private static void init(String name, String ext) throws Exception {
 118         TEST_NAME = name;
 119         EXT = ext;
 120         if (EXT.equals("rpm")) {
 121             OUTPUT = "output" + File.separator + TEST_NAME + "-1.0-1." + Base.getRpmArch() + "." + EXT;
 122         } else {
 123             OUTPUT = "output" + File.separator + TEST_NAME + "-1.0." + EXT;
 124         }
 125         CMD = new String [] {
 126         "--package-type", EXT,
 127         "--input", "input",
 128         "--output", "output",
 129         "--name", TEST_NAME,
 130         "--main-jar", "hello.jar",
 131         "--main-class", "Hello",
 132         "--license-file", JPackagePath.getLicenseFilePath()};
 133     }
 134 
 135     public static void run(String name, String ext) throws Exception {
 136         init(name, ext);
 137 
 138         if (JPackageInstallerHelper.isVerifyInstall()) {
 139             verifyInstall();
 140         } else if (JPackageInstallerHelper.isVerifyUnInstall()) {
 141             verifyUnInstall();
 142         } else {
 143             JPackageHelper.createHelloInstallerJar();
 144             testCreateInstaller();
 145         }
 146     }
 147 }