1 /*
   2  * Copyright (c) 2007, 2016, 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         "wsimport"
  69     };
  70 
  71     // tools that do not accept -version
  72     static final String[] BLACKLIST_VERSION = {
  73         "appletviewer",
  74         "controlpanel",
  75         "jaccessinspector",
  76         "jaccessinspector-32",
  77         "jaccesswalker",
  78         "jaccesswalker-32",
  79         "jaotc",
  80         "jar",
  81         "jarsigner",
  82         "java-rmi",
  83         "java-rmi.cgi",
  84         "javadoc",
  85         "javacpl",
  86         "javaws",
  87         "jcmd",
  88         "jconsole",
  89         "jcontrol",
  90         "jdeprscan",
  91         "jdeps",
  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         "orbd",
 111         "pack200",
 112         "packager",
 113         "policytool",
 114         "rmic",
 115         "rmid",
 116         "rmiregistry",
 117         "schemagen", // returns error code 127
 118         "serialver",
 119         "servertool",
 120         "ssvagent",
 121         "tnameserv",
 122         "unpack200",
 123         "wsgen",
 124         "wsimport",
 125         "xjc"
 126     };
 127 
 128     // expected reference strings
 129     static String refVersion;
 130     static String refFullVersion;
 131 
 132     static String getAllVersionLines(String... argv) {
 133         return getVersion0(true, argv);
 134     }
 135 
 136     static String getVersion(String... argv) {
 137         return getVersion0(false, argv);
 138     }
 139 
 140     static String getVersion0(boolean allLines, String... argv) {
 141         TestHelper.TestResult tr = doExec(argv);
 142         StringBuilder out = new StringBuilder();
 143         // remove the HotSpot line
 144         for (String x : tr.testOutput) {
 145             if (allLines || !x.matches(".*Client.*VM.*|.*Server.*VM.*")) {
 146                 out = out.append(x + "\n");
 147             }
 148         }
 149         return out.toString();
 150     }
 151 
 152     /*
 153      * Checks if the tools accept "-version" option (exit code is zero).
 154      * The output of the tools run with "-version" is not verified.
 155      */
 156     static String testToolVersion() {
 157         System.out.println("=== testToolVersion === ");
 158         Set<String> failed = new HashSet<>();
 159         for (File f : new File(JAVA_BIN).listFiles(new ToolFilter(BLACKLIST_VERSION))) {
 160             String x = f.getAbsolutePath();
 161             TestResult tr = doExec(x, "-version");
 162             System.out.println("Testing " + f.getName());
 163             System.out.println("#> " + x + " -version");
 164             tr.testOutput.forEach(System.out::println);
 165             System.out.println("#> echo $?");
 166             System.out.println(tr.exitValue);
 167             if (!tr.isOK()) {
 168                 System.out.println("failed");
 169                 failed.add(f.getName());
 170             }
 171         }
 172         if (failed.isEmpty()) {
 173             System.out.println("testToolVersion passed");
 174             return "";
 175         } else {
 176             System.out.println("testToolVersion failed");
 177             return "testToolVersion: " + failed + "; ";
 178         }
 179 
 180     }
 181 
 182     static String testJVersionStrings() {
 183         System.out.println("=== testJVersionStrings === ");
 184         Set<String> failed = new HashSet<>();
 185         for (File f : new File(JAVA_BIN).listFiles(new ToolFilter(BLACKLIST_JOPTION))) {
 186             System.out.println("Testing " + f.getName());
 187             String x = f.getAbsolutePath();
 188             String testStr = getVersion(x, "-J-version");
 189             if (refVersion.compareTo(testStr) != 0) {
 190                 failed.add(f.getName());
 191                 System.out.println("Error: " + x +
 192                                    " fails -J-version comparison");
 193                 System.out.println("Expected:");
 194                 System.out.print(refVersion);
 195                 System.out.println("Actual:");
 196                 System.out.print(testStr);
 197             }
 198 
 199             testStr = getVersion(x, "-J-fullversion");
 200             if (refFullVersion.compareTo(testStr) != 0) {
 201                 failed.add(f.getName());
 202                 System.out.println("Error: " + x +
 203                                    " fails -J-fullversion comparison");
 204                 System.out.println("Expected:");
 205                 System.out.print(refFullVersion);
 206                 System.out.println("Actual:");
 207                 System.out.print(testStr);
 208             }
 209         }
 210         if (failed.isEmpty()) {
 211             System.out.println("testJVersionStrings passed");
 212             return "";
 213         } else {
 214             System.out.println("testJVersionStrings failed");
 215             return "testJVersionStrings: " + failed + "; ";
 216         }
 217     }
 218 
 219     static String testInternalStrings() {
 220         System.out.println("=== testInternalStrings === ");
 221         String bStr = refVersion.substring(refVersion.indexOf("build") +
 222                                            "build".length() + 1,
 223                                            refVersion.lastIndexOf(")"));
 224 
 225         String expectedFullVersion = "fullversion:" + bStr;
 226 
 227         Map<String, String> envMap = new HashMap<>();
 228         envMap.put(TestHelper.JLDEBUG_KEY, "true");
 229         TestHelper.TestResult tr = doExec(envMap, javaCmd, "-version");
 230         List<String> alist = new ArrayList<>();
 231         tr.testOutput.stream().map(String::trim).forEach(alist::add);
 232 
 233         if (alist.contains(expectedFullVersion)) {
 234             System.out.println("testInternalStrings passed");
 235             return "";
 236         } else {
 237             System.out.println("Error: could not find " + expectedFullVersion);
 238             tr.testOutput.forEach(System.out::println);
 239             System.out.println("testInternalStrings failed");
 240             return "testInternalStrings; ";
 241         }
 242     }
 243 
 244     static String testDebugVersion() {
 245         System.out.println("=== testInternalStrings === ");
 246         String jdkType = System.getProperty("jdk.debug", "release");
 247         String versionLines = getAllVersionLines(javaCmd, "-version");
 248         if ("release".equals(jdkType)) {
 249             jdkType = "";
 250         } else {
 251             jdkType = jdkType + " ";
 252         }
 253 
 254         String tofind = "(" + jdkType + "build";
 255 
 256         int idx = versionLines.indexOf(tofind);
 257         if (idx < 0) {
 258             System.out.println("versionLines " + versionLines);
 259             System.out.println("Did not find first instance of " + tofind);
 260             return "testDebugVersion; ";
 261         }
 262         idx =  versionLines.indexOf(tofind, idx + 1);
 263         if (idx < 0) {
 264             System.out.println("versionLines " + versionLines);
 265             System.out.println("Did not find second instance of " + tofind);
 266             return "testDebugVersion; ";
 267         }
 268         System.out.println("testDebugVersion passed");
 269         return "";
 270     }
 271 
 272     // Initialize
 273     static void init() {
 274         refVersion = getVersion(javaCmd, "-version");
 275         refFullVersion = getVersion(javaCmd, "-fullversion");
 276     }
 277 
 278     public static void main(String[] args) {
 279         init();
 280         String errorMessage = "";
 281         errorMessage += testJVersionStrings();
 282         errorMessage += testInternalStrings();
 283         errorMessage += testToolVersion();
 284         errorMessage += testDebugVersion();
 285         if (errorMessage.isEmpty()) {
 286             System.out.println("All Version string comparisons: PASS");
 287         } else {
 288             throw new AssertionError("VersionCheck failed: " + errorMessage);
 289         }
 290     }
 291 
 292     static class ToolFilter implements FileFilter {
 293         final Iterable<String> exclude;
 294         protected ToolFilter(String... exclude) {
 295             List<String> tlist = new ArrayList<>();
 296             this.exclude = tlist;
 297             for (String x : exclude) {
 298                 String str = x + ((isWindows) ? EXE_FILE_EXT : "");
 299                 tlist.add(str.toLowerCase());
 300             }
 301         }
 302         @Override
 303         public boolean accept(File pathname) {
 304             if (!pathname.isFile() || !pathname.canExecute()) {
 305                 return false;
 306             }
 307             String name = pathname.getName().toLowerCase();
 308             if (isWindows && !name.endsWith(EXE_FILE_EXT)) {
 309                 return false;
 310             }
 311             for (String x : exclude) {
 312                 if (name.endsWith(x)) {
 313                     return false;
 314                 }
 315             }
 316             return true;
 317         }
 318     }
 319 }