1 /*
2  * Copyright (c) 2004, 2018, 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 4989690 6259855 6706299 8204565
27  * @summary Check that version-related system property invariants hold.
28  * @author Martin Buchholz
29  */
30 
31 import java.io.*;
32 import java.net.URLClassLoader;
33 import java.net.URL;
34 
35 public class Versions {
getProperty(String prop)36     static String getProperty(String prop) throws Exception {
37         String value = System.getProperty(prop);
38         if (value == null)
39             throw new Exception("No such system property: " + prop);
40         System.out.printf("%s=%s%n", prop, value);
41         return value;
42     }
43 
44     static ClassLoader cl;
45 
checkClassVersion(int major, int minor, boolean expectSupported)46     static void checkClassVersion(int major, int minor, boolean expectSupported)
47         throws Exception
48     {
49         final String className  = "ClassVersionTest";
50         final String classFile  = className + ".class";
51 
52         // We create an invalid class file, (only magic and version info),
53         // but the version info must be checked before the body.
54         final DataOutputStream dos =
55             new DataOutputStream(new FileOutputStream(classFile));
56         dos.writeLong((0xCafeBabel << 32) + (minor << 16) + major);
57         dos.close();
58 
59         boolean supported = true;
60         try {
61             Class.forName(className, false, cl);
62         } catch (UnsupportedClassVersionError e) {
63             supported = false;
64         } catch (Throwable t) {
65             // We expect an Exception indicating invalid class file
66         }
67         new File(classFile).delete();
68         if (supported != expectSupported)
69             throw new Exception("Forgot to update java.class.version?");
70     }
71 
main(String [] args)72     public static void main(String [] args) throws Exception {
73         String classVersion   = getProperty("java.class.version");
74         String javaVersion    = getProperty("java.version");
75         String runtimeVersion = getProperty("java.runtime.version");
76         String specVersion    = getProperty("java.specification.version");
77         String vmSpecVersion  = getProperty("java.vm.specification.version");
78         String featureVersion = Integer.toString(Runtime.version().feature());
79 
80         if (! (javaVersion.startsWith(specVersion) &&
81                runtimeVersion.startsWith(specVersion) &&
82                specVersion.equals(featureVersion) &&
83                vmSpecVersion.equals(featureVersion))) {
84             throw new Exception("Invalid version-related system properties");
85         }
86 
87         //----------------------------------------------------------------
88         // Check that java.class.version is correct.
89         // Injecting a larger major or minor version number into a
90         // .class file should result in UnsupportedClassVersionError.
91         //----------------------------------------------------------------
92         String[] versions = classVersion.split("\\.");
93         int majorVersion = Integer.parseInt(versions[0]);
94         int minorVersion = Integer.parseInt(versions[1]);
95         System.out.printf("majorVersion=%s%n",majorVersion);
96         System.out.printf("minorVersion=%s%n",minorVersion);
97 
98         // Look in ".", and *not* in CLASSPATH
99         cl = new URLClassLoader(new URL[]{new File("./").toURL()}, null);
100 
101         checkClassVersion(majorVersion    , minorVersion    , true );
102         checkClassVersion(majorVersion + 1, minorVersion    , false);
103         checkClassVersion(majorVersion    , minorVersion + 1, false);
104     }
105 }
106