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