1 /*
   2  * Copyright (c) 2007, 2014, 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
  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         "javaw",
  50         "javaws",
  51         "jcontrol",
  52         "jmc",
  53         "jmc.ini",
  54         "jvisualvm",
  55         "packager",
  56         "unpack200",
  57         "wsimport"
  58     };
  59 
  60     // tools that do not accept -version
  61     static final String[] BLACKLIST_VERSION = {
  62         "appletviewer",
  63         "controlpanel",
  64         "jar",
  65         "jarsigner",
  66         "java-rmi",
  67         "java-rmi.cgi",
  68         "javadoc",
  69         "javaws",
  70         "jcmd",
  71         "jconsole",
  72         "jcontrol",
  73         "jdeps",
  74         "jinfo",
  75         "jmap",
  76         "jmc",
  77         "jmc.ini",
  78         "jps",
  79         "jrunscript",
  80         "jjs",
  81         "jsadebugd",
  82         "jstack",
  83         "jstat",
  84         "jstatd",
  85         "jvisualvm",
  86         "keytool",
  87         "kinit",
  88         "klist",
  89         "ktab",
  90         "native2ascii",
  91         "orbd",
  92         "pack200",
  93         "packager",
  94         "policytool",
  95         "rmic",
  96         "rmid",
  97         "rmiregistry",
  98         "schemagen", // returns error code 127
  99         "serialver",
 100         "servertool",
 101         "tnameserv",
 102         "unpack200",
 103         "wsgen",
 104         "wsimport",
 105         "xjc"
 106     };
 107 
 108     // expected reference strings
 109     static String refVersion;
 110     static String refFullVersion;
 111 
 112     static String getVersion(String... argv) {
 113         TestHelper.TestResult tr = doExec(argv);
 114         StringBuilder out = new StringBuilder();
 115         // remove the HotSpot line
 116         for (String x : tr.testOutput) {
 117             if (!x.matches(".*Client.*VM.*|.*Server.*VM.*")) {
 118                 out = out.append(x + "\n");
 119             }
 120         }
 121         return out.toString();
 122     }
 123 
 124     /*
 125      * this tests if the tool can take a version string and returns
 126      * a 0 exit code, it is not possible to validate the contents
 127      * of the -version output as they are inconsistent.
 128      */
 129     static boolean testToolVersion() {
 130         TestResult tr = null;
 131         TestHelper.testExitValue = 0;
 132         for (File f : new File(JAVA_BIN).listFiles(new ToolFilter(BLACKLIST_VERSION))) {
 133             String x = f.getAbsolutePath();
 134             System.out.println("Testing (-version): " + x);
 135             tr = doExec(x, "-version");
 136             tr.checkPositive();
 137         }
 138         return TestHelper.testExitValue == 0;
 139     }
 140 
 141     static boolean compareJVersionStrings() {
 142         int failcount = 0;
 143         for (File f : new File(JAVA_BIN).listFiles(new ToolFilter(BLACKLIST_JOPTION))) {
 144             String x = f.getAbsolutePath();
 145             System.out.println("Testing (-J-version): " + x);
 146             String testStr;
 147 
 148             testStr = getVersion(x, "-J-version");
 149             if (refVersion.compareTo(testStr) != 0) {
 150                 failcount++;
 151                 System.out.println("Error: " + x +
 152                                    " fails -J-version comparison");
 153                 System.out.println("Expected:");
 154                 System.out.print(refVersion);
 155                 System.out.println("Actual:");
 156                 System.out.print(testStr);
 157             }
 158 
 159             testStr = getVersion(x, "-J-fullversion");
 160             if (refFullVersion.compareTo(testStr) != 0) {
 161                 failcount++;
 162                 System.out.println("Error: " + x +
 163                                    " fails -J-fullversion comparison");
 164                 System.out.println("Expected:");
 165                 System.out.print(refFullVersion);
 166                 System.out.println("Actual:");
 167                 System.out.print(testStr);
 168             }
 169         }
 170         System.out.println("Version Test: " + failcount);
 171         return failcount == 0;
 172     }
 173 
 174     static boolean compareInternalStrings() {
 175         int failcount = 0;
 176         String bStr = refVersion.substring(refVersion.lastIndexOf("build") +
 177                                            "build".length() + 1,
 178                                            refVersion.lastIndexOf(")"));
 179 
 180         String[] vStr = bStr.split("\\.|-|_");
 181         String jdkMajor = vStr[0];
 182         String jdkMinor = vStr[1];
 183         String jdkMicro = vStr[2];
 184         String jdkBuild = vStr[vStr.length - 1];
 185 
 186         String expectedDotVersion = "dotversion:" + jdkMajor + "." + jdkMinor;
 187         String expectedFullVersion = "fullversion:" + bStr;
 188 
 189         Map<String, String> envMap = new HashMap<>();
 190         envMap.put(TestHelper.JLDEBUG_KEY, "true");
 191         TestHelper.TestResult tr = doExec(envMap, javaCmd, "-version");
 192         List<String> alist = new ArrayList<>();
 193         alist.addAll(tr.testOutput);
 194         for (String x : tr.testOutput) {
 195             alist.add(x.trim());
 196         }
 197         if (!alist.contains(expectedDotVersion)) {
 198             System.out.println("Error: could not find " + expectedDotVersion);
 199             failcount++;
 200         }
 201 
 202         if (!alist.contains(expectedFullVersion)) {
 203             System.out.println("Error: could not find " + expectedFullVersion);
 204             failcount++;
 205         }
 206         System.out.println("Internal Strings Test: " + failcount);
 207         return failcount == 0;
 208     }
 209 
 210     // Initialize
 211     static void init() {
 212         refVersion = getVersion(javaCmd, "-version");
 213         refFullVersion = getVersion(javaCmd, "-fullversion");
 214     }
 215 
 216     public static void main(String[] args) {
 217         init();
 218         if (compareJVersionStrings() &&
 219                 compareInternalStrings() &&
 220                 testToolVersion()) {
 221             System.out.println("All Version string comparisons: PASS");
 222         } else {
 223             throw new AssertionError("Some tests failed");
 224         }
 225     }
 226 
 227     static class ToolFilter implements FileFilter {
 228         final Iterable<String> exclude ;
 229         protected ToolFilter(String... exclude) {
 230             List<String> tlist = new ArrayList<>();
 231             this.exclude = tlist;
 232             for (String x : exclude) {
 233                 String str = x + ((isWindows) ? EXE_FILE_EXT : "");
 234                 tlist.add(str.toLowerCase());
 235             }
 236         }
 237         @Override
 238         public boolean accept(File pathname) {
 239             if (!pathname.isFile() || !pathname.canExecute()) {
 240                 return false;
 241             }
 242             String name = pathname.getName().toLowerCase();
 243             if (isWindows && !name.endsWith(EXE_FILE_EXT)) {
 244                 return false;
 245             }
 246             for (String x : exclude) {
 247                 if (name.endsWith(x)) {
 248                     return false;
 249                 }
 250             }
 251             return true;
 252         }
 253     }
 254 }