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