1 /*
2  * Copyright (c) 2007, 2020, 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         "jaotc",
58         "javaw",
59         "javaws",
60         "jcontrol",
61         "jmc",
62         "jmc.ini",
63         "jweblauncher",
64         "jpackage",
65         "ssvagent"
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         "jaotc",
77         "jar",
78         "jarsigner",
79         "java-rmi",
80         "java-rmi.cgi",
81         "javadoc",
82         "javacpl",
83         "javaws",
84         "jcmd",
85         "jconsole",
86         "jcontrol",
87         "jdeprscan",
88         "jdeps",
89         "jfr",
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         "rmic",
110         "rmid",
111         "rmiregistry",
112         "serialver",
113         "servertool",
114         "ssvagent"
115     };
116 
117     // expected reference strings
118     static String refVersion;
119     static String refFullVersion;
120 
getAllVersionLines(String... argv)121     static String getAllVersionLines(String... argv) {
122         return getVersion0(true, argv);
123     }
124 
getVersion(String... argv)125     static String getVersion(String... argv) {
126         return getVersion0(false, argv);
127     }
128 
getVersion0(boolean allLines, String... argv)129     static String getVersion0(boolean allLines, String... argv) {
130         TestHelper.TestResult tr = doExec(argv);
131         StringBuilder out = new StringBuilder();
132         // remove the HotSpot line
133         for (String x : tr.testOutput) {
134             if (allLines || !x.matches(".*Client.*VM.*|.*Server.*VM.*")) {
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      */
testToolVersion()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 
testJVersionStrings()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 
testInternalStrings()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 
testDebugVersion()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
init()262     static void init() {
263         refVersion = getVersion(javaCmd, "-version");
264         refFullVersion = getVersion(javaCmd, "-fullversion");
265     }
266 
main(String[] args)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 }
281