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