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