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