1 /* 2 * Copyright (c) 2007, 2016, 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 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 import java.util.HashSet; 40 import java.util.Set; 41 42 public class VersionCheck extends TestHelper { 43 44 // tools that do not accept -J-option 45 static final String[] BLACKLIST_JOPTION = { 46 "controlpanel", 47 "jabswitch", 48 "java-rmi", 49 "java-rmi.cgi", 50 "java", 51 "javacpl", 52 "jaccessinspector", 53 "jaccessinspector-32", 54 "jaccesswalker", 55 "jaccesswalker-32", 56 "jaotc", 57 "javaw", 58 "javaws", 59 "jcontrol", 60 "jmc", 61 "jmc.ini", 62 "jweblauncher", 63 "packager", 64 "ssvagent", 65 "unpack200", 66 "wsimport" 67 }; 68 69 // tools that do not accept -version 70 static final String[] BLACKLIST_VERSION = { 71 "appletviewer", 72 "controlpanel", 73 "jaccessinspector", 74 "jaccessinspector-32", 75 "jaccesswalker", 76 "jaccesswalker-32", 77 "jaotc", 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 "jimage", 91 "jinfo", 92 "jlink", 93 "jmap", 94 "jmod", 95 "jmc", 96 "jmc.ini", 97 "jps", 98 "jrunscript", 99 "jjs", 100 "jstack", 101 "jstat", 102 "jstatd", 103 "jweblauncher", 104 "keytool", 105 "kinit", 106 "klist", 107 "ktab", 108 "orbd", 109 "pack200", 110 "packager", 111 "policytool", 112 "rmic", 113 "rmid", 114 "rmiregistry", 115 "schemagen", // returns error code 127 116 "serialver", 117 "servertool", 118 "ssvagent", 119 "tnameserv", 120 "unpack200", 121 "wsgen", 122 "wsimport", 123 "xjc" 124 }; 125 126 // expected reference strings 127 static String refVersion; 128 static String refFullVersion; 129 130 static String getAllVersionLines(String... argv) { 131 return getVersion0(true, argv); 132 } 133 134 static String getVersion(String... argv) { 135 return getVersion0(false, argv); 136 } 137 138 static String getVersion0(boolean allLines, String... argv) { 139 TestHelper.TestResult tr = doExec(argv); 140 StringBuilder out = new StringBuilder(); 141 // remove the HotSpot line 142 for (String x : tr.testOutput) { 143 if (allLines || !x.matches(".*Client.*VM.*|.*Server.*VM.*")) { 144 out = out.append(x + "\n"); 145 } 146 } 147 return out.toString(); 148 } 149 150 /* 151 * Checks if the tools accept "-version" option (exit code is zero). 152 * The output of the tools run with "-version" is not verified. 153 */ 154 static String testToolVersion() { 155 System.out.println("=== testToolVersion === "); 156 Set<String> failed = new HashSet<>(); 157 for (File f : new File(JAVA_BIN).listFiles(new ToolFilter(BLACKLIST_VERSION))) { 158 String x = f.getAbsolutePath(); 159 TestResult tr = doExec(x, "-version"); 160 System.out.println("Testing " + f.getName()); 161 System.out.println("#> " + x + " -version"); 162 tr.testOutput.forEach(System.out::println); 163 System.out.println("#> echo $?"); 164 System.out.println(tr.exitValue); 165 if (!tr.isOK()) { 166 System.out.println("failed"); 167 failed.add(f.getName()); 168 } 169 } 170 if (failed.isEmpty()) { 171 System.out.println("testToolVersion passed"); 172 return ""; 173 } else { 174 System.out.println("testToolVersion failed"); 175 return "testToolVersion: " + failed + "; "; 176 } 177 178 } 179 180 static String testJVersionStrings() { 181 System.out.println("=== testJVersionStrings === "); 182 Set<String> failed = new HashSet<>(); 183 for (File f : new File(JAVA_BIN).listFiles(new ToolFilter(BLACKLIST_JOPTION))) { 184 System.out.println("Testing " + f.getName()); 185 String x = f.getAbsolutePath(); 186 String testStr = getVersion(x, "-J-version"); 187 if (refVersion.compareTo(testStr) != 0) { 188 failed.add(f.getName()); 189 System.out.println("Error: " + x + 190 " fails -J-version comparison"); 191 System.out.println("Expected:"); 192 System.out.print(refVersion); 193 System.out.println("Actual:"); 194 System.out.print(testStr); 195 } 196 197 testStr = getVersion(x, "-J-fullversion"); 198 if (refFullVersion.compareTo(testStr) != 0) { 199 failed.add(f.getName()); 200 System.out.println("Error: " + x + 201 " fails -J-fullversion comparison"); 202 System.out.println("Expected:"); 203 System.out.print(refFullVersion); 204 System.out.println("Actual:"); 205 System.out.print(testStr); 206 } 207 } 208 if (failed.isEmpty()) { 209 System.out.println("testJVersionStrings passed"); 210 return ""; 211 } else { 212 System.out.println("testJVersionStrings failed"); 213 return "testJVersionStrings: " + failed + "; "; 214 } 215 } 216 217 static String testInternalStrings() { 218 System.out.println("=== testInternalStrings === "); 219 String bStr = refVersion.substring(refVersion.indexOf("build") + 220 "build".length() + 1, 221 refVersion.lastIndexOf(")")); 222 223 String expectedFullVersion = "fullversion:" + bStr; 224 225 Map<String, String> envMap = new HashMap<>(); 226 envMap.put(TestHelper.JLDEBUG_KEY, "true"); 227 TestHelper.TestResult tr = doExec(envMap, javaCmd, "-version"); 228 List<String> alist = new ArrayList<>(); 229 tr.testOutput.stream().map(String::trim).forEach(alist::add); 230 231 if (alist.contains(expectedFullVersion)) { 232 System.out.println("testInternalStrings passed"); 233 return ""; 234 } else { 235 System.out.println("Error: could not find " + expectedFullVersion); 236 tr.testOutput.forEach(System.out::println); 237 System.out.println("testInternalStrings failed"); 238 return "testInternalStrings; "; 239 } 240 } 241 242 static String testDebugVersion() { 243 System.out.println("=== testInternalStrings === "); 244 String jdkType = System.getProperty("jdk.debug", "release"); 245 String versionLines = getAllVersionLines(javaCmd, "-version"); 246 if ("release".equals(jdkType)) { 247 jdkType = ""; 248 } else { 249 jdkType = jdkType + " "; 250 } 251 252 String tofind = "(" + jdkType + "build"; 253 254 int idx = versionLines.indexOf(tofind); 255 if (idx < 0) { 256 System.out.println("versionLines " + versionLines); 257 System.out.println("Did not find first instance of " + tofind); 258 return "testDebugVersion; "; 259 } 260 idx = versionLines.indexOf(tofind, idx + 1); 261 if (idx < 0) { 262 System.out.println("versionLines " + versionLines); 263 System.out.println("Did not find second instance of " + tofind); 264 return "testDebugVersion; "; 265 } 266 System.out.println("testDebugVersion passed"); 267 return ""; 268 } 269 270 // Initialize 271 static void init() { 272 refVersion = getVersion(javaCmd, "-version"); 273 refFullVersion = getVersion(javaCmd, "-fullversion"); 274 } 275 276 public static void main(String[] args) { 277 init(); 278 String errorMessage = ""; 279 errorMessage += testJVersionStrings(); 280 errorMessage += testInternalStrings(); 281 errorMessage += testToolVersion(); 282 errorMessage += testDebugVersion(); 283 if (errorMessage.isEmpty()) { 284 System.out.println("All Version string comparisons: PASS"); 285 } else { 286 throw new AssertionError("VersionCheck failed: " + errorMessage); 287 } 288 } 289 290 static class ToolFilter implements FileFilter { 291 final Iterable<String> exclude; 292 protected ToolFilter(String... exclude) { 293 List<String> tlist = new ArrayList<>(); 294 this.exclude = tlist; 295 for (String x : exclude) { 296 String str = x + ((isWindows) ? EXE_FILE_EXT : ""); 297 tlist.add(str.toLowerCase()); 298 } 299 } 300 @Override 301 public boolean accept(File pathname) { 302 if (!pathname.isFile() || !pathname.canExecute()) { 303 return false; 304 } 305 String name = pathname.getName().toLowerCase(); 306 if (isWindows && !name.endsWith(EXE_FILE_EXT)) { 307 return false; 308 } 309 for (String x : exclude) { 310 if (name.endsWith(x)) { 311 return false; 312 } 313 } 314 return true; 315 } 316 } 317 }