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
|
/* Copyright (c) 2007-2014 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.win32;
import java.lang.reflect.Method;
import java.util.Collections;
import java.util.List;
import junit.framework.TestCase;
import com.sun.jna.FunctionMapper;
import com.sun.jna.Library;
import com.sun.jna.Native;
import com.sun.jna.NativeLibrary;
import com.sun.jna.NativeLong;
import com.sun.jna.Structure;
/**
* @author twall@users.sf.net
*/
public class W32StdCallTest extends TestCase {
public static interface TestLibrary extends StdCallLibrary {
public static class Inner extends Structure {
public static final List<String> FIELDS = createFieldsOrder("value");
public double value;
@Override
protected List<String> getFieldOrder() {
return FIELDS;
}
}
public static class TestStructure extends Structure {
public static class ByValue extends TestStructure implements Structure.ByValue { }
public static final List<String> FIELDS = createFieldsOrder("c", "s", "i", "j", "inner");
public byte c;
public short s;
public int i;
public long j;
public Inner inner;
@Override
protected List<String> getFieldOrder() {
return FIELDS;
}
}
int returnInt32ArgumentStdCall(int arg);
TestStructure.ByValue returnStructureByValueArgumentStdCall(TestStructure.ByValue arg);
interface Int32Callback extends StdCallCallback {
int callback(int arg, int arg2);
}
int callInt32StdCallCallback(Int32Callback c, int arg, int arg2);
interface ManyArgsStdCallCallback extends StdCallCallback {
void callback(NativeLong arg1, int arg2, double arg3,
String arg4, String arg5,
double arg6, NativeLong arg7,
double arg8, NativeLong arg9,
NativeLong arg10, NativeLong arg11);
}
int callManyArgsStdCallCallback(ManyArgsStdCallCallback c, NativeLong arg1, int arg2,
double arg3, String arg4, String arg5,
double arg6, NativeLong arg7,
double arg8, NativeLong arg9,
NativeLong arg10, NativeLong arg11);
}
public static void main(java.lang.String[] argList) {
junit.textui.TestRunner.run(W32StdCallTest.class);
}
private TestLibrary testlib;
@Override
protected void setUp() {
testlib = Native.load("testlib", TestLibrary.class,
Collections.singletonMap(Library.OPTION_FUNCTION_MAPPER, StdCallLibrary.FUNCTION_MAPPER));
}
@Override
protected void tearDown() {
testlib = null;
}
public void testFunctionMapper() throws Exception {
FunctionMapper mapper = StdCallLibrary.FUNCTION_MAPPER;
NativeLibrary lib = NativeLibrary.getInstance("testlib");
Method[] methods = {
TestLibrary.class.getMethod("returnInt32ArgumentStdCall", int.class),
TestLibrary.class.getMethod("returnStructureByValueArgumentStdCall", TestLibrary.TestStructure.ByValue.class),
TestLibrary.class.getMethod("callInt32StdCallCallback", TestLibrary.Int32Callback.class, int.class, int.class)
};
for (Method m : methods) {
String name = mapper.getFunctionName(lib, m);
assertTrue("Function name not decorated for method "
+ m.getName()
+ ": " + name, name.indexOf("@") != -1);
assertEquals("Wrong name in mapped function",
name, lib.getFunction(name, StdCallLibrary.STDCALL_CONVENTION).getName());
}
}
public void testStdCallReturnInt32Argument() {
final int MAGIC = 0x12345678;
assertEquals("Expect zero return", 0, testlib.returnInt32ArgumentStdCall(0));
assertEquals("Expect magic return", MAGIC, testlib.returnInt32ArgumentStdCall(MAGIC));
}
public void testStdCallReturnStructureByValueArgument() {
TestLibrary.TestStructure.ByValue s = new TestLibrary.TestStructure.ByValue();
assertTrue("Wrong struct value", s.dataEquals(testlib.returnStructureByValueArgumentStdCall(s)));
}
public void testStdCallCallback() {
final int MAGIC = 0x11111111;
final boolean[] called = { false };
TestLibrary.Int32Callback cb = new TestLibrary.Int32Callback() {
@Override
public int callback(int arg, int arg2) {
called[0] = true;
return arg + arg2;
}
};
final int EXPECTED = MAGIC*3;
int value = testlib.callInt32StdCallCallback(cb, MAGIC, MAGIC*2);
assertTrue("stdcall callback not called", called[0]);
if (value == -1) {
fail("stdcall callback did not restore the stack pointer");
}
assertEquals("Wrong stdcall callback value", Integer.toHexString(EXPECTED),
Integer.toHexString(value));
value = testlib.callInt32StdCallCallback(cb, -1, -2);
if (value == -1) {
fail("stdcall callback did not restore the stack pointer");
}
assertEquals("Wrong stdcall callback return", -3, value);
}
public void testStdCallCallbackStackAlignment() {
final boolean[] called = { false };
TestLibrary.ManyArgsStdCallCallback cb = new TestLibrary.ManyArgsStdCallCallback() {
@Override
public void callback(NativeLong arg1, int arg2, double arg3,
String arg4, String arg5,
double arg6, NativeLong arg7,
double arg8, NativeLong arg9,
NativeLong arg10, NativeLong arg11) {
called[0] = true;
}
};
int value = testlib.callManyArgsStdCallCallback(cb, new NativeLong(1),
2, 3, "four", "five",
6, new NativeLong(7),
8, new NativeLong(9),
new NativeLong(10),
new NativeLong(11));
assertTrue("stdcall callback not called", called[0]);
if (value == -1) {
fail("stdcall callback did not restore the stack pointer");
}
}
}
|