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