1 /* 2 * Copyright (c) 2007, 2024, 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 8139986 8162746 8278967 27 * @summary validate and test -version, -fullversion, and internal, as well as 28 * sanity checks if a tool can be launched. 29 * @modules jdk.compiler 30 * jdk.zipfs 31 * @compile VersionCheck.java 32 * @run main VersionCheck 33 */ 34 35 import java.io.File; 36 import java.util.ArrayList; 37 import java.util.HashMap; 38 import java.util.HashSet; 39 import java.util.List; 40 import java.util.Map; 41 import java.util.Set; 42 43 public class VersionCheck extends TestHelper { 44 45 // tools that do not accept -J-option 46 static final String[] BLACKLIST_JOPTION = { 47 "asprof", 48 "controlpanel", 49 "jabswitch", 50 "java-rmi", 51 "java-rmi.cgi", 52 "java", 53 "javacpl", 54 "jaccessinspector", 55 "jaccessinspector-32", 56 "jaccesswalker", 57 "jaccesswalker-32", 58 "javaw", 59 "javaws", 60 "jcontrol", 61 "jfrconv", 62 "jmc", 63 "jmc.ini", 64 "jweblauncher", 65 "jpackage", 66 "ssvagent", 67 "jwebserver" 68 }; 69 70 // tools that do not accept -version 71 static final String[] BLACKLIST_VERSION = { 72 "asprof", 73 "controlpanel", 74 "jaccessinspector", 75 "jaccessinspector-32", 76 "jaccesswalker", 77 "jaccesswalker-32", 78 "jar", 79 "jarsigner", 80 "java-rmi", 81 "java-rmi.cgi", 82 "javadoc", 83 "javacpl", 84 "javaws", 85 "jcmd", 86 "jconsole", 87 "jcontrol", 88 "jdeprscan", 89 "jdeps", 90 "jfr", 91 "jfrconv", 92 "jimage", 93 "jinfo", 94 "jlink", 95 "jmap", 96 "jmod", 97 "jmc", 98 "jmc.ini", 99 "jps", 100 "jrunscript", 101 "jjs", 102 "jstack", 103 "jstat", 104 "jstatd", 105 "jweblauncher", 106 "keytool", 107 "kinit", 108 "klist", 109 "ktab", 110 "jpackage", 111 "rmiregistry", 112 "serialver", 113 "servertool", 114 "ssvagent" 115 }; 116 117 // expected reference strings 118 static String refVersion; 119 static String refFullVersion; 120 121 static String getAllVersionLines(String... argv) { 122 return getVersion0(true, argv); 123 } 124 125 static String getVersion(String... argv) { 126 return getVersion0(false, argv); 127 } 128 129 static String getVersion0(boolean allLines, String... argv) { 130 TestHelper.TestResult tr = doExec(argv); 131 StringBuilder out = new StringBuilder(); 132 // remove the HotSpot line and security manager deprecation warnings 133 for (String x : tr.testOutput) { 134 if (allLines || !x.matches(".*Client.*VM.*|" + 135 ".*Server.*VM.*|" + 136 "WARNING:.*terminally.*deprecated.*|" + 137 "WARNING:.*System::setSecurityManager.*")) { 138 out = out.append(x + "\n"); 139 } 140 } 141 return out.toString(); 142 } 143 144 /* 145 * Checks if the tools accept "-version" option (exit code is zero). 146 * The output of the tools run with "-version" is not verified. 147 */ 148 static String testToolVersion() { 149 System.out.println("=== testToolVersion === "); 150 Set<String> failed = new HashSet<>(); 151 for (File f : new File(JAVA_BIN).listFiles(new ToolFilter(BLACKLIST_VERSION))) { 152 String x = f.getAbsolutePath(); 153 TestResult tr = doExec(x, "-version"); 154 System.out.println("Testing " + f.getName()); 155 System.out.println("#> " + x + " -version"); 156 tr.testOutput.forEach(System.out::println); 157 System.out.println("#> echo $?"); 158 System.out.println(tr.exitValue); 159 if (!tr.isOK()) { 160 System.out.println("failed"); 161 failed.add(f.getName()); 162 } 163 } 164 if (failed.isEmpty()) { 165 System.out.println("testToolVersion passed"); 166 return ""; 167 } else { 168 System.out.println("testToolVersion failed"); 169 return "testToolVersion: " + failed + "; "; 170 } 171 172 } 173 174 static String testJVersionStrings() { 175 System.out.println("=== testJVersionStrings === "); 176 Set<String> failed = new HashSet<>(); 177 for (File f : new File(JAVA_BIN).listFiles(new ToolFilter(BLACKLIST_JOPTION))) { 178 System.out.println("Testing " + f.getName()); 179 String x = f.getAbsolutePath(); 180 String testStr = getVersion(x, "-J-version"); 181 if (refVersion.compareTo(testStr) != 0) { 182 failed.add(f.getName()); 183 System.out.println("Error: " + x + 184 " fails -J-version comparison"); 185 System.out.println("Expected:"); 186 System.out.print(refVersion); 187 System.out.println("Actual:"); 188 System.out.print(testStr); 189 } 190 191 testStr = getVersion(x, "-J-fullversion"); 192 if (refFullVersion.compareTo(testStr) != 0) { 193 failed.add(f.getName()); 194 System.out.println("Error: " + x + 195 " fails -J-fullversion comparison"); 196 System.out.println("Expected:"); 197 System.out.print(refFullVersion); 198 System.out.println("Actual:"); 199 System.out.print(testStr); 200 } 201 } 202 if (failed.isEmpty()) { 203 System.out.println("testJVersionStrings passed"); 204 return ""; 205 } else { 206 System.out.println("testJVersionStrings failed"); 207 return "testJVersionStrings: " + failed + "; "; 208 } 209 } 210 211 static String testInternalStrings() { 212 System.out.println("=== testInternalStrings === "); 213 String bStr = refVersion.substring(refVersion.indexOf("build") + 214 "build".length() + 1, 215 refVersion.lastIndexOf(")")); 216 217 String expectedFullVersion = "fullversion:" + bStr; 218 219 Map<String, String> envMap = new HashMap<>(); 220 envMap.put(TestHelper.JLDEBUG_KEY, "true"); 221 TestHelper.TestResult tr = doExec(envMap, javaCmd, "-version"); 222 List<String> alist = new ArrayList<>(); 223 tr.testOutput.stream().map(String::trim).forEach(alist::add); 224 225 if (alist.contains(expectedFullVersion)) { 226 System.out.println("testInternalStrings passed"); 227 return ""; 228 } else { 229 System.out.println("Error: could not find " + expectedFullVersion); 230 tr.testOutput.forEach(System.out::println); 231 System.out.println("testInternalStrings failed"); 232 return "testInternalStrings; "; 233 } 234 } 235 236 static String testDebugVersion() { 237 System.out.println("=== testInternalStrings === "); 238 String jdkType = System.getProperty("jdk.debug", "release"); 239 String versionLines = getAllVersionLines(javaCmd, "-version"); 240 if ("release".equals(jdkType)) { 241 jdkType = ""; 242 } else { 243 jdkType = jdkType + " "; 244 } 245 246 String tofind = "(" + jdkType + "build"; 247 248 int idx = versionLines.indexOf(tofind); 249 if (idx < 0) { 250 System.out.println("versionLines " + versionLines); 251 System.out.println("Did not find first instance of " + tofind); 252 return "testDebugVersion; "; 253 } 254 idx = versionLines.indexOf(tofind, idx + 1); 255 if (idx < 0) { 256 System.out.println("versionLines " + versionLines); 257 System.out.println("Did not find second instance of " + tofind); 258 return "testDebugVersion; "; 259 } 260 System.out.println("testDebugVersion passed"); 261 return ""; 262 } 263 264 // Initialize 265 static void init() { 266 refVersion = getVersion(javaCmd, "-version"); 267 refFullVersion = getVersion(javaCmd, "-fullversion"); 268 } 269 270 public static void main(String[] args) { 271 init(); 272 String errorMessage = ""; 273 errorMessage += testJVersionStrings(); 274 errorMessage += testInternalStrings(); 275 errorMessage += testToolVersion(); 276 errorMessage += testDebugVersion(); 277 if (errorMessage.isEmpty()) { 278 System.out.println("All Version string comparisons: PASS"); 279 } else { 280 throw new AssertionError("VersionCheck failed: " + errorMessage); 281 } 282 } 283 }