blob: 08a18bd2a286f1fe845e2f658fc80036e11d4b93 [file] [log] [blame]
J. Duke319a3b92007-12-01 00:00:00 +00001/*
Alejandro Murillo20cb4ca2015-11-04 16:02:53 -08002 * Copyright (c) 2007, 2015, Oracle and/or its affiliates. All rights reserved.
J. Duke319a3b92007-12-01 00:00:00 +00003 * 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 *
Kelly O'Hairfe008ae2010-05-25 15:58:33 -070019 * 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.
J. Duke319a3b92007-12-01 00:00:00 +000022 */
23
24/**
25 * @test
Alejandro Murillo20cb4ca2015-11-04 16:02:53 -080026 * @bug 6545058 6611182 8016209 8139986
Steve Sides9d2cd272012-03-29 17:49:34 -070027 * @summary validate and test -version, -fullversion, and internal, as well as
28 * sanity checks if a tool can be launched.
J. Duke319a3b92007-12-01 00:00:00 +000029 * @compile VersionCheck.java
30 * @run main VersionCheck
31 */
32
J. Duke319a3b92007-12-01 00:00:00 +000033import java.io.File;
Steve Sides9d2cd272012-03-29 17:49:34 -070034import java.io.FileFilter;
J. Duke319a3b92007-12-01 00:00:00 +000035import java.util.Map;
36import java.util.ArrayList;
Steve Sides9d2cd272012-03-29 17:49:34 -070037import java.util.HashMap;
J. Duke319a3b92007-12-01 00:00:00 +000038import java.util.List;
J. Duke319a3b92007-12-01 00:00:00 +000039
Steve Sides9d2cd272012-03-29 17:49:34 -070040public class VersionCheck extends TestHelper {
J. Duke319a3b92007-12-01 00:00:00 +000041
Steve Sides9d2cd272012-03-29 17:49:34 -070042 // tools that do not accept -J-option
43 static final String[] BLACKLIST_JOPTION = {
44 "controlpanel",
Ragini Prasade618b152013-02-02 12:08:43 -080045 "jabswitch",
Steve Sides9d2cd272012-03-29 17:49:34 -070046 "java-rmi",
47 "java-rmi.cgi",
48 "java",
Chris Hegarty441905c2014-12-03 14:22:58 +000049 "javacpl",
Kumar Srinivasan429f9e62015-12-03 17:44:18 -080050 "jaccessinspector",
51 "jaccessinspector-32",
52 "jaccesswalker",
53 "jaccesswalker-32",
Steve Sides9d2cd272012-03-29 17:49:34 -070054 "javaw",
55 "javaws",
56 "jcontrol",
Kumar Srinivasan4cc36132013-06-08 09:05:49 -070057 "jmc",
Alexander Zuev8c296952013-10-22 22:18:52 +040058 "jmc.ini",
Chris Hegarty441905c2014-12-03 14:22:58 +000059 "jp2launcher",
Steve Sides9d2cd272012-03-29 17:49:34 -070060 "jvisualvm",
61 "packager",
Chris Hegarty441905c2014-12-03 14:22:58 +000062 "ssvagent",
Steve Sides9d2cd272012-03-29 17:49:34 -070063 "unpack200",
64 "wsimport"
65 };
J. Duke319a3b92007-12-01 00:00:00 +000066
Steve Sides9d2cd272012-03-29 17:49:34 -070067 // tools that do not accept -version
68 static final String[] BLACKLIST_VERSION = {
J. Duke319a3b92007-12-01 00:00:00 +000069 "appletviewer",
Steve Sides9d2cd272012-03-29 17:49:34 -070070 "controlpanel",
Kumar Srinivasan429f9e62015-12-03 17:44:18 -080071 "jaccessinspector",
72 "jaccessinspector-32",
73 "jaccesswalker",
74 "jaccesswalker-32",
J. Duke319a3b92007-12-01 00:00:00 +000075 "jar",
76 "jarsigner",
Steve Sides9d2cd272012-03-29 17:49:34 -070077 "java-rmi",
78 "java-rmi.cgi",
J. Duke319a3b92007-12-01 00:00:00 +000079 "javadoc",
Kumar Srinivasan277a0d22014-12-08 11:54:17 -080080 "javacpl",
Steve Sides9d2cd272012-03-29 17:49:34 -070081 "javaws",
82 "jcmd",
J. Duke319a3b92007-12-01 00:00:00 +000083 "jconsole",
Steve Sides9d2cd272012-03-29 17:49:34 -070084 "jcontrol",
Chris Hegarty83223ad2013-01-03 10:00:55 +000085 "jdeps",
Chris Hegarty441905c2014-12-03 14:22:58 +000086 "jimage",
J. Duke319a3b92007-12-01 00:00:00 +000087 "jinfo",
Alan Batemandb4d3832016-03-17 19:04:16 +000088 "jlink",
J. Duke319a3b92007-12-01 00:00:00 +000089 "jmap",
Alan Batemandb4d3832016-03-17 19:04:16 +000090 "jmod",
Kumar Srinivasan4cc36132013-06-08 09:05:49 -070091 "jmc",
Alexander Zuev8c296952013-10-22 22:18:52 +040092 "jmc.ini",
J. Duke319a3b92007-12-01 00:00:00 +000093 "jps",
Steve Sides9d2cd272012-03-29 17:49:34 -070094 "jrunscript",
Erik Joelssonb3a313f2013-02-25 15:08:11 -080095 "jjs",
Chris Hegarty441905c2014-12-03 14:22:58 +000096 "jp2launcher",
Steve Sides9d2cd272012-03-29 17:49:34 -070097 "jsadebugd",
J. Duke319a3b92007-12-01 00:00:00 +000098 "jstack",
99 "jstat",
100 "jstatd",
Steve Sides9d2cd272012-03-29 17:49:34 -0700101 "jvisualvm",
J. Duke319a3b92007-12-01 00:00:00 +0000102 "keytool",
Steve Sides9d2cd272012-03-29 17:49:34 -0700103 "kinit",
104 "klist",
105 "ktab",
J. Duke319a3b92007-12-01 00:00:00 +0000106 "orbd",
107 "pack200",
Steve Sides9d2cd272012-03-29 17:49:34 -0700108 "packager",
J. Duke319a3b92007-12-01 00:00:00 +0000109 "policytool",
110 "rmic",
111 "rmid",
112 "rmiregistry",
Steve Sides9d2cd272012-03-29 17:49:34 -0700113 "schemagen", // returns error code 127
J. Duke319a3b92007-12-01 00:00:00 +0000114 "serialver",
115 "servertool",
Chris Hegarty441905c2014-12-03 14:22:58 +0000116 "ssvagent",
J. Duke319a3b92007-12-01 00:00:00 +0000117 "tnameserv",
Steve Sides9d2cd272012-03-29 17:49:34 -0700118 "unpack200",
J. Duke319a3b92007-12-01 00:00:00 +0000119 "wsgen",
120 "wsimport",
121 "xjc"
122 };
123
124 // expected reference strings
125 static String refVersion;
126 static String refFullVersion;
127
Alejandro Murillo20cb4ca2015-11-04 16:02:53 -0800128 static String getAllVersionLines(String... argv) {
129 return getVersion0(true, argv);
130 }
131
J. Duke319a3b92007-12-01 00:00:00 +0000132 static String getVersion(String... argv) {
Alejandro Murillo20cb4ca2015-11-04 16:02:53 -0800133 return getVersion0(false, argv);
134 }
135
136 static String getVersion0(boolean allLines, String... argv) {
Steve Sides9d2cd272012-03-29 17:49:34 -0700137 TestHelper.TestResult tr = doExec(argv);
J. Duke319a3b92007-12-01 00:00:00 +0000138 StringBuilder out = new StringBuilder();
139 // remove the HotSpot line
Steve Sides9d2cd272012-03-29 17:49:34 -0700140 for (String x : tr.testOutput) {
Alejandro Murillo20cb4ca2015-11-04 16:02:53 -0800141 if (allLines || !x.matches(".*Client.*VM.*|.*Server.*VM.*")) {
J. Duke319a3b92007-12-01 00:00:00 +0000142 out = out.append(x + "\n");
143 }
144 }
145 return out.toString();
146 }
147
Steve Sides9d2cd272012-03-29 17:49:34 -0700148 /*
149 * this tests if the tool can take a version string and returns
150 * a 0 exit code, it is not possible to validate the contents
151 * of the -version output as they are inconsistent.
152 */
153 static boolean testToolVersion() {
154 TestResult tr = null;
155 TestHelper.testExitValue = 0;
156 for (File f : new File(JAVA_BIN).listFiles(new ToolFilter(BLACKLIST_VERSION))) {
157 String x = f.getAbsolutePath();
158 System.out.println("Testing (-version): " + x);
159 tr = doExec(x, "-version");
160 tr.checkPositive();
161 }
162 return TestHelper.testExitValue == 0;
163 }
164
165 static boolean compareJVersionStrings() {
J. Duke319a3b92007-12-01 00:00:00 +0000166 int failcount = 0;
Steve Sides9d2cd272012-03-29 17:49:34 -0700167 for (File f : new File(JAVA_BIN).listFiles(new ToolFilter(BLACKLIST_JOPTION))) {
168 String x = f.getAbsolutePath();
169 System.out.println("Testing (-J-version): " + x);
J. Duke319a3b92007-12-01 00:00:00 +0000170 String testStr;
171
172 testStr = getVersion(x, "-J-version");
173 if (refVersion.compareTo(testStr) != 0) {
174 failcount++;
175 System.out.println("Error: " + x +
176 " fails -J-version comparison");
177 System.out.println("Expected:");
178 System.out.print(refVersion);
179 System.out.println("Actual:");
180 System.out.print(testStr);
181 }
182
183 testStr = getVersion(x, "-J-fullversion");
184 if (refFullVersion.compareTo(testStr) != 0) {
185 failcount++;
186 System.out.println("Error: " + x +
187 " fails -J-fullversion comparison");
188 System.out.println("Expected:");
189 System.out.print(refFullVersion);
190 System.out.println("Actual:");
191 System.out.print(testStr);
192 }
193 }
194 System.out.println("Version Test: " + failcount);
195 return failcount == 0;
196 }
197
198 static boolean compareInternalStrings() {
199 int failcount = 0;
Kumar Srinivasan92f0f3c2015-12-02 14:31:52 -0800200 String bStr = refVersion.substring(refVersion.indexOf("build") +
J. Duke319a3b92007-12-01 00:00:00 +0000201 "build".length() + 1,
202 refVersion.lastIndexOf(")"));
203
J. Duke319a3b92007-12-01 00:00:00 +0000204 String expectedFullVersion = "fullversion:" + bStr;
205
Steve Sides9d2cd272012-03-29 17:49:34 -0700206 Map<String, String> envMap = new HashMap<>();
207 envMap.put(TestHelper.JLDEBUG_KEY, "true");
208 TestHelper.TestResult tr = doExec(envMap, javaCmd, "-version");
209 List<String> alist = new ArrayList<>();
210 alist.addAll(tr.testOutput);
211 for (String x : tr.testOutput) {
212 alist.add(x.trim());
213 }
J. Duke319a3b92007-12-01 00:00:00 +0000214
215 if (!alist.contains(expectedFullVersion)) {
216 System.out.println("Error: could not find " + expectedFullVersion);
217 failcount++;
218 }
219 System.out.println("Internal Strings Test: " + failcount);
220 return failcount == 0;
221 }
222
Alejandro Murillo20cb4ca2015-11-04 16:02:53 -0800223 static boolean testDebugVersion() {
224 String jdkType = System.getProperty("jdk.debug", "release");
225 String versionLines = getAllVersionLines(javaCmd, "-version");
226 if ("release".equals(jdkType)) {
227 jdkType = "";
228 } else {
229 jdkType = jdkType + " ";
230 }
231 String tofind = "(" + jdkType + "build";
232 int idx = versionLines.indexOf(tofind);
233 if (idx < 0) {
234 System.out.println("Did not find first instance of " + tofind);
235 return false;
236 }
237 idx = versionLines.indexOf(tofind, idx + 1);
238 if (idx < 0) {
239 System.out.println("Did not find first instance of " + tofind);
240 return false;
241 }
242 return true;
243 }
244
J. Duke319a3b92007-12-01 00:00:00 +0000245 // Initialize
246 static void init() {
Steve Sides9d2cd272012-03-29 17:49:34 -0700247 refVersion = getVersion(javaCmd, "-version");
248 refFullVersion = getVersion(javaCmd, "-fullversion");
J. Duke319a3b92007-12-01 00:00:00 +0000249 }
250
251 public static void main(String[] args) {
252 init();
Steve Sides9d2cd272012-03-29 17:49:34 -0700253 if (compareJVersionStrings() &&
254 compareInternalStrings() &&
Alejandro Murillo20cb4ca2015-11-04 16:02:53 -0800255 testToolVersion() &&
256 testDebugVersion()) {
J. Duke319a3b92007-12-01 00:00:00 +0000257 System.out.println("All Version string comparisons: PASS");
258 } else {
259 throw new AssertionError("Some tests failed");
260 }
261 }
Steve Sides9d2cd272012-03-29 17:49:34 -0700262
263 static class ToolFilter implements FileFilter {
Alejandro Murillo20cb4ca2015-11-04 16:02:53 -0800264 final Iterable<String> exclude;
Steve Sides9d2cd272012-03-29 17:49:34 -0700265 protected ToolFilter(String... exclude) {
266 List<String> tlist = new ArrayList<>();
267 this.exclude = tlist;
268 for (String x : exclude) {
269 String str = x + ((isWindows) ? EXE_FILE_EXT : "");
270 tlist.add(str.toLowerCase());
271 }
272 }
273 @Override
274 public boolean accept(File pathname) {
275 if (!pathname.isFile() || !pathname.canExecute()) {
276 return false;
277 }
278 String name = pathname.getName().toLowerCase();
279 if (isWindows && !name.endsWith(EXE_FILE_EXT)) {
280 return false;
281 }
282 for (String x : exclude) {
283 if (name.endsWith(x)) {
284 return false;
285 }
286 }
287 return true;
288 }
289 }
J. Duke319a3b92007-12-01 00:00:00 +0000290}