1 /*
   2  * Copyright (c) 2007, 2018, 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 /**
  25  * @test
  26  * @bug 6545058 6611182 8016209 8139986 8162746
  27  * @summary validate and test -version, -fullversion, and internal, as well as
  28  *          sanity checks if a tool can be launched.
  29  * @modules jdk.compiler
  30  *          jdk.zipfs
  31  * @compile VersionCheck.java
  32  * @run main VersionCheck
  33  */
  34 
  35 import java.io.File;
  36 import java.io.FileFilter;
  37 import java.util.Map;
  38 import java.util.ArrayList;
  39 import java.util.HashMap;
  40 import java.util.List;
  41 import java.util.HashSet;
  42 import java.util.Set;
  43 
  44 public class VersionCheck extends TestHelper {
  45 
  46     // tools that do not accept -J-option
  47     static final String[] BLACKLIST_JOPTION = {
  48         "controlpanel",
  49         "jabswitch",
  50         "java-rmi",
  51         "java-rmi.cgi",
  52         "java",
  53         "javacpl",
  54         "jaccessinspector",
  55         "jaccessinspector-32",
  56         "jaccesswalker",
  57         "jaccesswalker-32",
  58         "jaotc",
  59         "javaw",
  60         "javaws",
  61         "jcontrol",
  62         "jmc",
  63         "jmc.ini",
  64         "jweblauncher",
  65         "packager",
  66         "ssvagent",
  67         "unpack200",
  68     };
  69 
  70     // tools that do not accept -version
  71     static final String[] BLACKLIST_VERSION = {
  72         "appletviewer",
  73         "controlpanel",
  74         "jaccessinspector",
  75         "jaccessinspector-32",
  76         "jaccesswalker",
  77         "jaccesswalker-32",
  78         "jaotc",
  79         "jar",
  80         "jarsigner",
  81         "java-rmi",
  82         "java-rmi.cgi",
  83         "javadoc",
  84         "javacpl",
  85         "javaws",
  86         "jcmd",
  87         "jconsole",
  88         "jcontrol",
  89         "jdeprscan",
  90         "jdeps",
  91         "jfr",
  92         "jimage",
  93         "jinfo",
  94         "jlink",
  95         "jmap",
  96         "jmod",
  97         "jmc",
  98         "jmc.ini",
  99         "jps",
 100         "jrunscript",
 101         "jjs",
 102         "jstack",
 103         "jstat",
 104         "jstatd",
 105         "jweblauncher",
 106         "keytool",
 107         "kinit",
 108         "klist",
 109         "ktab",
 110         "pack200",
 111         "packager",
 112         "rmic",
 113         "rmid",
 114         "rmiregistry",
 115         "serialver",
 116         "servertool",
 117         "ssvagent",
 118         "unpack200"
 119     };
 120 
 121     // expected reference strings
 122     static String refVersion;
 123     static String refFullVersion;
 124 
 125     static String getAllVersionLines(String... argv) {
 126         return getVersion0(true, argv);
 127     }
 128 
 129     static String getVersion(String... argv) {
 130         return getVersion0(false, argv);
 131     }
 132 
 133     static String getVersion0(boolean allLines, String... argv) {
 134         TestHelper.TestResult tr = doExec(argv);
 135         StringBuilder out = new StringBuilder();
 136         // remove the HotSpot line
 137         for (String x : tr.testOutput) {
 138             if (allLines || !x.matches(".*Client.*VM.*|.*Server.*VM.*")) {
 139                 out = out.append(x + "\n");
 140             }
 141         }
 142         return out.toString();
 143     }
 144 
 145     /*
 146      * Checks if the tools accept "-version" option (exit code is zero).
 147      * The output of the tools run with "-version" is not verified.
 148      */
 149     static String testToolVersion() {
 150         System.out.println("=== testToolVersion === ");
 151         Set<String> failed = new HashSet<>();
 152         for (File f : new File(JAVA_BIN).listFiles(new ToolFilter(BLACKLIST_VERSION))) {
 153             String x = f.getAbsolutePath();
 154             TestResult tr = doExec(x, "-version");
 155             System.out.println("Testing " + f.getName());
 156             System.out.println("#> " + x + " -version");
 157             tr.testOutput.forEach(System.out::println);
 158             System.out.println("#> echo $?");
 159             System.out.println(tr.exitValue);
 160             if (!tr.isOK()) {
 161                 System.out.println("failed");
 162                 failed.add(f.getName());
 163             }
 164         }
 165         if (failed.isEmpty()) {
 166             System.out.println("testToolVersion passed");
 167             return "";
 168         } else {
 169             System.out.println("testToolVersion failed");
 170             return "testToolVersion: " + failed + "; ";
 171         }
 172 
 173     }
 174 
 175     static String testJVersionStrings() {
 176         System.out.println("=== testJVersionStrings === ");
 177         Set<String> failed = new HashSet<>();
 178         for (File f : new File(JAVA_BIN).listFiles(new ToolFilter(BLACKLIST_JOPTION))) {
 179             System.out.println("Testing " + f.getName());
 180             String x = f.getAbsolutePath();
 181             String testStr = getVersion(x, "-J-version");
 182             if (refVersion.compareTo(testStr) != 0) {
 183                 failed.add(f.getName());
 184                 System.out.println("Error: " + x +
 185                                    " fails -J-version comparison");
 186                 System.out.println("Expected:");
 187                 System.out.print(refVersion);
 188                 System.out.println("Actual:");
 189                 System.out.print(testStr);
 190             }
 191 
 192             testStr = getVersion(x, "-J-fullversion");
 193             if (refFullVersion.compareTo(testStr) != 0) {
 194                 failed.add(f.getName());
 195                 System.out.println("Error: " + x +
 196                                    " fails -J-fullversion comparison");
 197                 System.out.println("Expected:");
 198                 System.out.print(refFullVersion);
 199                 System.out.println("Actual:");
 200                 System.out.print(testStr);
 201             }
 202         }
 203         if (failed.isEmpty()) {
 204             System.out.println("testJVersionStrings passed");
 205             return "";
 206         } else {
 207             System.out.println("testJVersionStrings failed");
 208             return "testJVersionStrings: " + failed + "; ";
 209         }
 210     }
 211 
 212     static String testInternalStrings() {
 213         System.out.println("=== testInternalStrings === ");
 214         String bStr = refVersion.substring(refVersion.indexOf("build") +
 215                                            "build".length() + 1,
 216                                            refVersion.lastIndexOf(")"));
 217 
 218         String expectedFullVersion = "fullversion:" + bStr;
 219 
 220         Map<String, String> envMap = new HashMap<>();
 221         envMap.put(TestHelper.JLDEBUG_KEY, "true");
 222         TestHelper.TestResult tr = doExec(envMap, javaCmd, "-version");
 223         List<String> alist = new ArrayList<>();
 224         tr.testOutput.stream().map(String::trim).forEach(alist::add);
 225 
 226         if (alist.contains(expectedFullVersion)) {
 227             System.out.println("testInternalStrings passed");
 228             return "";
 229         } else {
 230             System.out.println("Error: could not find " + expectedFullVersion);
 231             tr.testOutput.forEach(System.out::println);
 232             System.out.println("testInternalStrings failed");
 233             return "testInternalStrings; ";
 234         }
 235     }
 236 
 237     static String testDebugVersion() {
 238         System.out.println("=== testInternalStrings === ");
 239         String jdkType = System.getProperty("jdk.debug", "release");
 240         String versionLines = getAllVersionLines(javaCmd, "-version");
 241         if ("release".equals(jdkType)) {
 242             jdkType = "";
 243         } else {
 244             jdkType = jdkType + " ";
 245         }
 246 
 247         String tofind = "(" + jdkType + "build";
 248 
 249         int idx = versionLines.indexOf(tofind);
 250         if (idx < 0) {
 251             System.out.println("versionLines " + versionLines);
 252             System.out.println("Did not find first instance of " + tofind);
 253             return "testDebugVersion; ";
 254         }
 255         idx =  versionLines.indexOf(tofind, idx + 1);
 256         if (idx < 0) {
 257             System.out.println("versionLines " + versionLines);
 258             System.out.println("Did not find second instance of " + tofind);
 259             return "testDebugVersion; ";
 260         }
 261         System.out.println("testDebugVersion passed");
 262         return "";
 263     }
 264 
 265     // Initialize
 266     static void init() {
 267         refVersion = getVersion(javaCmd, "-version");
 268         refFullVersion = getVersion(javaCmd, "-fullversion");
 269     }
 270 
 271     public static void main(String[] args) {
 272         init();
 273         String errorMessage = "";
 274         errorMessage += testJVersionStrings();
 275         errorMessage += testInternalStrings();
 276         errorMessage += testToolVersion();
 277         errorMessage += testDebugVersion();
 278         if (errorMessage.isEmpty()) {
 279             System.out.println("All Version string comparisons: PASS");
 280         } else {
 281             throw new AssertionError("VersionCheck failed: " + errorMessage);
 282         }
 283     }
 284 
 285     static class ToolFilter implements FileFilter {
 286         final Iterable<String> exclude;
 287         protected ToolFilter(String... exclude) {
 288             List<String> tlist = new ArrayList<>();
 289             this.exclude = tlist;
 290             for (String x : exclude) {
 291                 String str = x + ((isWindows) ? EXE_FILE_EXT : "");
 292                 tlist.add(str.toLowerCase());
 293             }
 294         }
 295         @Override
 296         public boolean accept(File pathname) {
 297             if (!pathname.isFile() || !pathname.canExecute()) {
 298                 return false;
 299             }
 300             String name = pathname.getName().toLowerCase();
 301             if (isWindows && !name.endsWith(EXE_FILE_EXT)) {
 302                 return false;
 303             }
 304             for (String x : exclude) {
 305                 if (name.endsWith(x)) {
 306                     return false;
 307                 }
 308             }
 309             return true;
 310         }
 311     }
 312 }