aboutsummaryrefslogtreecommitdiffstats
path: root/src/test/java/org/apache/bcel/generic/JDKGenericDumpTestCase.java
blob: 957dfc84e016b6f393ce19f9ccb245a9b36d80ae (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.apache.bcel.generic;

import static com.sun.jna.platform.win32.WinReg.HKEY_LOCAL_MACHINE;
import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.fail;

import java.io.File;
import java.io.FileFilter;
import java.io.InputStream;
import java.util.Collection;
import java.util.Enumeration;
import java.util.HashSet;
import java.util.Set;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;

import org.apache.bcel.classfile.ClassParser;
import org.apache.bcel.classfile.Code;
import org.apache.bcel.classfile.JavaClass;
import org.apache.bcel.classfile.Method;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.SystemUtils;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.junit.runners.Parameterized.Parameters;

import com.sun.jna.platform.win32.Advapi32Util;

/**
 * Test that the generic dump() methods work on the JDK classes Reads each class into an instruction list and then dumps
 * the instructions. The output bytes should be the same as the input.
 */
@RunWith(Parameterized.class)
public class JDKGenericDumpTestCase {

    private static final String KEY_JDK = "SOFTWARE\\JavaSoft\\Java Development Kit";

    private static final String KEY_JDK_9 = "SOFTWARE\\JavaSoft\\JDK";

    private static final String KEY_JRE = "SOFTWARE\\JavaSoft\\Java Runtime Environment";

    private static final String KEY_JRE_9 = "SOFTWARE\\JavaSoft\\JRE";

    @Parameters(name = "{0}")
    public static Collection<String> data() {
        return findJavaHomes();
    }

    private static Set<String> findJavaHomes() {
        if (SystemUtils.IS_OS_WINDOWS) {
            return findJavaHomesOnWindows();
        }
        final Set<String> javaHomes = new HashSet<>(1);
        javaHomes.add(SystemUtils.JAVA_HOME);
        return javaHomes;
    }

    private static Set<String> findJavaHomesOnWindows() {
        Set<String> javaHomes = new HashSet<>();
        addAllJavaHomesOnWindows(KEY_JRE, javaHomes);
        addAllJavaHomesOnWindows(KEY_JRE_9, javaHomes);
        addAllJavaHomesOnWindows(KEY_JDK, javaHomes);
        addAllJavaHomesOnWindows(KEY_JDK_9, javaHomes);
        return javaHomes;
    }

    private static void addAllJavaHomesOnWindows(final String keyJre, Set<String> javaHomes) {
        javaHomes.addAll(findJavaHomesOnWindows(keyJre, Advapi32Util.registryGetKeys(HKEY_LOCAL_MACHINE, keyJre)));
    }

    private static Set<String> findJavaHomesOnWindows(String keyJavaHome, final String[] keys) {
        final Set<String> javaHomes = new HashSet<>(keys.length);
        for (final String key : keys) {
            if (Advapi32Util.registryKeyExists(HKEY_LOCAL_MACHINE, keyJavaHome + "\\" + key)) {
                final String javaHome = Advapi32Util.registryGetStringValue(HKEY_LOCAL_MACHINE,
                        keyJavaHome + "\\" + key, "JavaHome");
                if (StringUtils.isNoneBlank(javaHome)) {
                    if (new File(javaHome).exists()) {
                        javaHomes.add(javaHome);
                    }
                }
            }
        }
        return javaHomes;
    }

    public JDKGenericDumpTestCase(final String javaHome) {
        this.javaHome = javaHome;
    }

    private final String javaHome;

    @Test
    public void testJDKjars() throws Exception {
        final File[] jars = listJDKjars();
        if (jars != null) {
            for (final File file : jars) {
                testJar(file);
            }
        }
    }

    private void testJar(final File file) throws Exception {
        System.out.println(file);
        try (JarFile jar = new JarFile(file)) {
            final Enumeration<JarEntry> en = jar.entries();
            while (en.hasMoreElements()) {
                final JarEntry e = en.nextElement();
                final String name = e.getName();
                if (name.endsWith(".class")) {
                    // System.out.println("- " + name);
                    try (InputStream in = jar.getInputStream(e)) {
                        final ClassParser parser = new ClassParser(in, name);
                        final JavaClass jc = parser.parse();
                        for (final Method m : jc.getMethods()) {
                            compare(name, m);
                        }
                    }
                }
            }
        }
    }

    private void compare(final String name, final Method m) {
        // System.out.println("Method: " + m);
        final Code c = m.getCode();
        if (c == null) {
            return; // e.g. abstract method
        }
        final byte[] src = c.getCode();
        final InstructionList il = new InstructionList(src);
        final byte[] out = il.getByteCode();
        if (src.length == out.length) {
            assertArrayEquals(name + ": " + m.toString(), src, out);
        } else {
            System.out.println(name + ": " + m.toString() + " " + src.length + " " + out.length);
            System.out.println(bytesToHex(src));
            System.out.println(bytesToHex(out));
            for (final InstructionHandle ih : il) {
                System.out.println(ih.toString(false));
            }
            fail("Array comparison failure");
        }
    }

    private File[] listJDKjars() throws Exception {
        final File javaLib = new File(javaHome, "lib");
        return javaLib.listFiles(new FileFilter() {
            @Override
            public boolean accept(final File file) {
                return file.getName().endsWith(".jar");
            }
        });
    }

    private static final char[] hexArray = "0123456789ABCDEF".toCharArray();

    private static String bytesToHex(final byte[] bytes) {
        final char[] hexChars = new char[bytes.length * 3];
        int i = 0;
        for (final byte b : bytes) {
            final int v = b & 0xFF;
            hexChars[i++] = hexArray[v >>> 4];
            hexChars[i++] = hexArray[v & 0x0F];
            hexChars[i++] = ' ';
        }
        return new String(hexChars);
    }
}