1 /*
  2  * Copyright (c) 2007, 2021, 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  * @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     };
 66 
 67     // tools that do not accept -version
 68     static final String[] BLACKLIST_VERSION = {
 69         "appletviewer",
 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
129         for (String x : tr.testOutput) {
130             if (allLines || !x.matches(".*Client.*VM.*|.*Server.*VM.*")) {
131                 out = out.append(x + "\n");
132             }
133         }
134         return out.toString();
135     }
136 
137     /*
138      * Checks if the tools accept "-version" option (exit code is zero).
139      * The output of the tools run with "-version" is not verified.
140      */
141     static String testToolVersion() {
142         System.out.println("=== testToolVersion === ");
143         Set<String> failed = new HashSet<>();
144         for (File f : new File(JAVA_BIN).listFiles(new ToolFilter(BLACKLIST_VERSION))) {
145             String x = f.getAbsolutePath();
146             TestResult tr = doExec(x, "-version");
147             System.out.println("Testing " + f.getName());
148             System.out.println("#> " + x + " -version");
149             tr.testOutput.forEach(System.out::println);
150             System.out.println("#> echo $?");
151             System.out.println(tr.exitValue);
152             if (!tr.isOK()) {
153                 System.out.println("failed");
154                 failed.add(f.getName());
155             }
156         }
157         if (failed.isEmpty()) {
158             System.out.println("testToolVersion passed");
159             return "";
160         } else {
161             System.out.println("testToolVersion failed");
162             return "testToolVersion: " + failed + "; ";
163         }
164 
165     }
166 
167     static String testJVersionStrings() {
168         System.out.println("=== testJVersionStrings === ");
169         Set<String> failed = new HashSet<>();
170         for (File f : new File(JAVA_BIN).listFiles(new ToolFilter(BLACKLIST_JOPTION))) {
171             System.out.println("Testing " + f.getName());
172             String x = f.getAbsolutePath();
173             String testStr = getVersion(x, "-J-version");
174             if (refVersion.compareTo(testStr) != 0) {
175                 failed.add(f.getName());
176                 System.out.println("Error: " + x +
177                                    " fails -J-version comparison");
178                 System.out.println("Expected:");
179                 System.out.print(refVersion);
180                 System.out.println("Actual:");
181                 System.out.print(testStr);
182             }
183 
184             testStr = getVersion(x, "-J-fullversion");
185             if (refFullVersion.compareTo(testStr) != 0) {
186                 failed.add(f.getName());
187                 System.out.println("Error: " + x +
188                                    " fails -J-fullversion comparison");
189                 System.out.println("Expected:");
190                 System.out.print(refFullVersion);
191                 System.out.println("Actual:");
192                 System.out.print(testStr);
193             }
194         }
195         if (failed.isEmpty()) {
196             System.out.println("testJVersionStrings passed");
197             return "";
198         } else {
199             System.out.println("testJVersionStrings failed");
200             return "testJVersionStrings: " + failed + "; ";
201         }
202     }
203 
204     static String testInternalStrings() {
205         System.out.println("=== testInternalStrings === ");
206         String bStr = refVersion.substring(refVersion.indexOf("build") +
207                                            "build".length() + 1,
208                                            refVersion.lastIndexOf(")"));
209 
210         String expectedFullVersion = "fullversion:" + bStr;
211 
212         Map<String, String> envMap = new HashMap<>();
213         envMap.put(TestHelper.JLDEBUG_KEY, "true");
214         TestHelper.TestResult tr = doExec(envMap, javaCmd, "-version");
215         List<String> alist = new ArrayList<>();
216         tr.testOutput.stream().map(String::trim).forEach(alist::add);
217 
218         if (alist.contains(expectedFullVersion)) {
219             System.out.println("testInternalStrings passed");
220             return "";
221         } else {
222             System.out.println("Error: could not find " + expectedFullVersion);
223             tr.testOutput.forEach(System.out::println);
224             System.out.println("testInternalStrings failed");
225             return "testInternalStrings; ";
226         }
227     }
228 
229     static String testDebugVersion() {
230         System.out.println("=== testInternalStrings === ");
231         String jdkType = System.getProperty("jdk.debug", "release");
232         String versionLines = getAllVersionLines(javaCmd, "-version");
233         if ("release".equals(jdkType)) {
234             jdkType = "";
235         } else {
236             jdkType = jdkType + " ";
237         }
238 
239         String tofind = "(" + jdkType + "build";
240 
241         int idx = versionLines.indexOf(tofind);
242         if (idx < 0) {
243             System.out.println("versionLines " + versionLines);
244             System.out.println("Did not find first instance of " + tofind);
245             return "testDebugVersion; ";
246         }
247         idx =  versionLines.indexOf(tofind, idx + 1);
248         if (idx < 0) {
249             System.out.println("versionLines " + versionLines);
250             System.out.println("Did not find second instance of " + tofind);
251             return "testDebugVersion; ";
252         }
253         System.out.println("testDebugVersion passed");
254         return "";
255     }
256 
257     // Initialize
258     static void init() {
259         refVersion = getVersion(javaCmd, "-version");
260         refFullVersion = getVersion(javaCmd, "-fullversion");
261     }
262 
263     public static void main(String[] args) {
264         init();
265         String errorMessage = "";
266         errorMessage += testJVersionStrings();
267         errorMessage += testInternalStrings();
268         errorMessage += testToolVersion();
269         errorMessage += testDebugVersion();
270         if (errorMessage.isEmpty()) {
271             System.out.println("All Version string comparisons: PASS");
272         } else {
273             throw new AssertionError("VersionCheck failed: " + errorMessage);
274         }
275     }
276 }