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 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276
|
/* Copyright (c) 2007-2009 Timothy Wall, All Rights Reserved
*
* The contents of this file is dual-licensed under 2
* alternative Open Source/Free licenses: LGPL 2.1 or later and
* Apache License 2.0. (starting with JNA version 4.0.0).
*
* You can freely decide which license you want to apply to
* the project.
*
* You may obtain a copy of the LGPL License at:
*
* http://www.gnu.org/licenses/licenses.html
*
* A copy is also included in the downloadable source code package
* containing JNA, in file "LGPL2.1".
*
* You may obtain a copy of the Apache License at:
*
* http://www.apache.org/licenses/
*
* A copy is also included in the downloadable source code package
* containing JNA, in file "AL2.0".
*/
package com.sun.jna;
import java.io.File;
import java.lang.ref.Reference;
import java.lang.ref.WeakReference;
import java.lang.reflect.Field;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLClassLoader;
import java.util.Properties;
import junit.framework.TestCase;
import org.junit.Assume;
/** Test loading and unloading native support from various locations. Note
* that no JNI classes are directly referenced in these tests.
*/
public class JNALoadTest extends TestCase implements Paths {
private class TestLoader extends URLClassLoader {
public TestLoader(boolean fromJar) throws MalformedURLException {
super(new URL[]{
Platform.isWindowsCE()
? new File("/Storage Card/" + (fromJar ? "jna.jar" : "test.jar")).toURI().toURL()
: new File(BUILDDIR + (fromJar ? "/jna.jar" : "/classes")).toURI().toURL()},
new CloverLoader());
if (fromJar) {
assertJarExists();
}
else {
assertLibraryExists();
}
}
@Override
protected Class<?> findClass(String name) throws ClassNotFoundException {
String boot = System.getProperty("jna.boot.library.path");
if (boot != null) {
System.setProperty("jna.boot.library.path", "");
}
Class<?> cls = super.findClass(name);
if (boot != null) {
System.setProperty("jna.boot.library.path", boot);
}
return cls;
}
}
protected void assertJarExists() {
File jar = new File(JNAJAR);
if (!jar.exists()) {
throw new Error("Expected JNA jar file at " + jar + " is missing");
}
}
protected void assertLibraryExists() {
String osPrefix = Platform.getNativeLibraryResourcePrefix();
String name = System.mapLibraryName("jnidispatch").replace(".dylib", ".jnilib");
if(Platform.isAIX()) {
name = name.replaceAll(".so$", ".a");
}
File lib = new File(CLASSES + "/com/sun/jna/" + osPrefix + "/" + name);
if (!lib.exists()) {
throw new Error("Expected JNA library at " + lib + " is missing");
}
}
public void testAvoidJarUnpacking() throws Exception {
System.setProperty("jna.nounpack", "true");
try {
Class<?> cls = Class.forName("com.sun.jna.Native", true, new TestLoader(true));
fail("Class com.sun.jna.Native should not be loadable if jna.nounpack=true: "
+ cls.getClassLoader());
}
catch(UnsatisfiedLinkError e) {
}
finally {
System.setProperty("jna.nounpack", "false");
}
}
public void testAvoidResourcePathLoading() throws Exception {
System.setProperty("jna.noclasspath", "true");
try {
Class<?> cls = Class.forName("com.sun.jna.Native", true, new TestLoader(false));
fail("Class com.sun.jna.Native should not be loadable if jna.noclasspath=true: "
+ cls.getClassLoader());
}
catch(UnsatisfiedLinkError e) {
}
finally {
System.setProperty("jna.noclasspath", "false");
}
}
public void testLoadAndUnloadFromJar() throws Exception {
if (Platform.isIntel() && (! Platform.is64Bit())) {
System.out.println("Skip " + getName() + " - it is known to be flaky and produces false positives on x86-32bit");
return;
}
ClassLoader loader = new TestLoader(true);
Class<?> cls = Class.forName("com.sun.jna.Native", true, loader);
assertEquals("Wrong class loader", loader, cls.getClassLoader());
assertTrue("System property jna.loaded not set", Boolean.getBoolean("jna.loaded"));
Field field = cls.getDeclaredField("jnidispatchPath");
field.setAccessible(true);
String path = (String)field.get(null);
assertNotNull("Native library path unavailable", path);
assertTrue("Native library not unpacked from jar: " + path,
path.startsWith(Native.getTempDir().getAbsolutePath()));
Reference<Class<?>> ref = new WeakReference<>(cls);
Reference<ClassLoader> clref = new WeakReference<>(loader);
loader = null;
cls = null;
field = null;
System.gc();
for (int i=0;i < GCWaits.GC_WAITS && (ref.get() != null || clref.get() != null);i++) {
GCWaits.gcRun();
}
assertNull("Class not GC'd: " + ref.get(), ref.get());
assertNull("ClassLoader not GC'd: " + clref.get(), clref.get());
// Check for temporary file deletion
File f = new File(path);
for (int i=0;i < GCWaits.GC_WAITS && (f.exists() || Boolean.getBoolean("jna.loaded"));i++) {
GCWaits.gcRun();
}
if (f.exists()) {
assertTrue("Temporary jnidispatch not marked for later deletion: " + f,
new File(f.getAbsolutePath()+".x").exists());
}
assertFalse("System property jna.loaded not cleared", Boolean.getBoolean("jna.loaded"));
// Should be able to load again without complaints about library
// already loaded in another class loader
try {
loader = new TestLoader(true);
cls = Class.forName("com.sun.jna.Native", true, loader);
} catch(Throwable t) {
fail("Couldn't load class again after discarding first load: " + t.getMessage());
} finally {
loader = null;
cls = null;
System.gc();
}
}
// GC Fails under OpenJDK(linux/ppc)
public void testLoadAndUnloadFromResourcePath() throws Exception {
if (Platform.isIntel() && (! Platform.is64Bit())) {
System.out.println("Skip " + getName() + " - it is known to be flaky and produces false positives on x86-32bit");
return;
}
ClassLoader loader = new TestLoader(false);
Class<?> cls = Class.forName("com.sun.jna.Native", true, loader);
assertEquals("Wrong class loader", loader, cls.getClassLoader());
assertTrue("System property jna.loaded not set", Boolean.getBoolean("jna.loaded"));
Field field = cls.getDeclaredField("jnidispatchPath");
field.setAccessible(true);
String path = (String)field.get(null);
assertNotNull("Native library not found", path);
Reference<Class<?>> ref = new WeakReference<>(cls);
Reference<ClassLoader> clref = new WeakReference<>(loader);
loader = null;
cls = null;
field = null;
System.gc();
for (int i=0;i < GCWaits.GC_WAITS && (ref.get() != null || clref.get() != null || Boolean.getBoolean("jna.loaded"));i++) {
GCWaits.gcRun();
}
assertNull("Class not GC'd: " + ref.get(), ref.get());
assertNull("ClassLoader not GC'd: " + clref.get(), clref.get());
assertFalse("System property jna.loaded not cleared", Boolean.getBoolean("jna.loaded"));
Throwable throwable = null;
// NOTE: IBM J9 needs some extra time to unload the native library,
// so try a few times before failing
for (int i=0;i < GCWaits.GC_WAITS;i++) {
GCWaits.gcRun();
try {
loader = new TestLoader(false);
cls = Class.forName("com.sun.jna.Native", true, loader);
break;
}
catch(Throwable t) {
loader = null;
throwable = t;
}
}
try {
if (loader == null) {
fail("Native library not unloaded: " + throwable.getMessage());
}
}
finally {
loader = null;
cls = null;
System.gc();
}
}
public void testLoadFromUnicodePath() throws Exception {
if (Platform.isWindows()) {
String vendor = System.getProperty("java.vendor");
if (vendor != null) {
vendor = vendor.toLowerCase();
if (vendor.contains("oracle") || vendor.contains("sun")) {
System.out.println("Skip " + getName() + " - Fails on Sun JVM windows (32 and 64-bit) (JVM bug), Works with IBM J9 (jdk6) windows");
return;
}
}
}
final String UNICODE = getName() + "-\u0444\u043b\u0441\u0432\u0443";
File tmpdir = new File(System.getProperty("java.io.tmpdir"));
File unicodeDir = new File(tmpdir, UNICODE);
unicodeDir.mkdirs();
Properties props = (Properties)System.getProperties().clone();
try {
System.setProperty("jnidispatch.preserve", "true");
System.setProperty("jna.tmpdir", unicodeDir.getAbsolutePath());
ClassLoader loader = new TestLoader(true);
Class<?> cls = Class.forName("com.sun.jna.Native", true, loader);
assertEquals("Wrong class loader", loader, cls.getClassLoader());
assertTrue("System property jna.loaded not set", Boolean.getBoolean("jna.loaded"));
String path = System.getProperty("jnidispatch.path");
if (path != null) {
File lib = new File(path);
lib.deleteOnExit();
}
}
catch(UnsatisfiedLinkError e) {
throw new Error("JVM error: System.load() failed to load JNA native library from " + System.getProperty("jnidispatch.path") + "): " + e);
}
finally {
System.setProperties(props);
}
}
public static void main(String[] args) {
junit.textui.TestRunner.run(JNALoadTest.class);
}
}
|