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