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