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