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