< prev index next >

test/jdk/java/foreign/TestIllegalLink.java

Print this page

 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 /*
 26  * @test
 27  * @requires ((os.arch == "amd64" | os.arch == "x86_64") & sun.arch.data.model == "64") | os.arch == "aarch64"
 28  * @run testng/othervm --enable-native-access=ALL-UNNAMED TestIllegalLink
 29  */
 30 
 31 import jdk.incubator.foreign.CLinker;
 32 import jdk.incubator.foreign.FunctionDescriptor;
 33 import jdk.incubator.foreign.MemoryAddress;
 34 import jdk.incubator.foreign.MemoryLayout;
 35 import jdk.incubator.foreign.MemoryLayouts;
 36 import jdk.incubator.foreign.MemorySegment;
 37 import org.testng.annotations.DataProvider;
 38 import org.testng.annotations.Test;
 39 
 40 import java.lang.invoke.MethodType;
 41 
 42 import static jdk.incubator.foreign.CLinker.C_INT;
 43 import static org.testng.Assert.assertTrue;
 44 import static org.testng.Assert.fail;
 45 
 46 public class TestIllegalLink {
 47 
 48     private static final MemoryAddress DUMMY_TARGET = MemoryAddress.ofLong(1);
 49     private static final CLinker ABI = CLinker.getInstance();
 50 
 51     @Test(dataProvider = "types")
 52     public void testTypeMismatch(MethodType mt, FunctionDescriptor desc, String expectedExceptionMessage) {
 53         try {
 54             ABI.downcallHandle(DUMMY_TARGET, mt, desc);
 55             fail("Expected IllegalArgumentException was not thrown");
 56         } catch (IllegalArgumentException e) {
 57             assertTrue(e.getMessage().contains(expectedExceptionMessage));
 58         }
 59     }
 60 
 61     @DataProvider
 62     public static Object[][] types() {
 63         return new Object[][]{
 64             {
 65                 MethodType.methodType(void.class),
 66                 FunctionDescriptor.of(C_INT),
 67                 "Return type mismatch"
 68             },
 69             {
 70                 MethodType.methodType(void.class),
 71                 FunctionDescriptor.ofVoid(C_INT),
 72                 "Arity mismatch"
 73             },
 74             {
 75                 MethodType.methodType(void.class, int.class),
 76                 FunctionDescriptor.ofVoid(MemoryLayout.paddingLayout(32)),
 77                 "Expected a ValueLayout"
 78             },
 79             {
 80                 MethodType.methodType(void.class, boolean.class),
 81                 FunctionDescriptor.ofVoid(MemoryLayouts.BITS_8_LE),
 82                 "Unsupported carrier"
 83             },
 84             {
 85                 MethodType.methodType(void.class, int.class),
 86                 FunctionDescriptor.ofVoid(MemoryLayouts.BITS_64_LE),
 87                 "Carrier size mismatch"
 88             },
 89             {
 90                 MethodType.methodType(void.class, MemoryAddress.class),
 91                 FunctionDescriptor.ofVoid(MemoryLayout.paddingLayout(64)),
 92                 "Expected a ValueLayout"
 93             },
 94             {
 95                 MethodType.methodType(void.class, MemoryAddress.class),
 96                 FunctionDescriptor.ofVoid(MemoryLayouts.BITS_16_LE),
 97                 "Address size mismatch"
 98             },
 99             {
100                 MethodType.methodType(void.class, MemorySegment.class),
101                 FunctionDescriptor.ofVoid(MemoryLayouts.BITS_64_LE),
102                 "Expected a GroupLayout"
103             },
104             {
105                 MethodType.methodType(void.class, String.class),
106                 FunctionDescriptor.ofVoid(MemoryLayouts.BITS_64_LE),
107                 "Unsupported carrier"
108             },
109         };
110     }
111 
112 }

 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 /*
 26  * @test
 27  * @requires ((os.arch == "amd64" | os.arch == "x86_64") & sun.arch.data.model == "64") | os.arch == "aarch64"
 28  * @run testng/othervm --enable-native-access=ALL-UNNAMED TestIllegalLink
 29  */
 30 
 31 import jdk.incubator.foreign.CLinker;
 32 import jdk.incubator.foreign.FunctionDescriptor;
 33 import jdk.incubator.foreign.MemoryAddress;
 34 import jdk.incubator.foreign.MemoryLayout;
 35 import jdk.incubator.foreign.NativeSymbol;
 36 import jdk.incubator.foreign.ResourceScope;
 37 import org.testng.annotations.DataProvider;
 38 import org.testng.annotations.Test;
 39 



 40 import static org.testng.Assert.assertTrue;
 41 import static org.testng.Assert.fail;
 42 
 43 public class TestIllegalLink extends NativeTestHelper {
 44 
 45     private static final NativeSymbol DUMMY_TARGET = NativeSymbol.ofAddress("dummy", MemoryAddress.ofLong(1), ResourceScope.globalScope());
 46     private static final CLinker ABI = CLinker.systemCLinker();
 47 
 48     @Test(dataProvider = "types")
 49     public void testTypeMismatch(FunctionDescriptor desc, String expectedExceptionMessage) {
 50         try {
 51             ABI.downcallHandle(DUMMY_TARGET, desc);
 52             fail("Expected IllegalArgumentException was not thrown");
 53         } catch (IllegalArgumentException e) {
 54             assertTrue(e.getMessage().contains(expectedExceptionMessage));
 55         }
 56     }
 57 
 58     @DataProvider
 59     public static Object[][] types() {
 60         return new Object[][]{
 61             {
 62                 FunctionDescriptor.of(MemoryLayout.paddingLayout(64)),
 63                 "Unsupported layout: x64"






 64             },
 65             {
















 66                 FunctionDescriptor.ofVoid(MemoryLayout.paddingLayout(64)),
 67                 "Unsupported layout: x64"





 68             },
 69             {
 70                     FunctionDescriptor.of(MemoryLayout.sequenceLayout(C_INT)),
 71                     "Unsupported layout: [:b32]"

 72             },
 73             {
 74                     FunctionDescriptor.ofVoid(MemoryLayout.sequenceLayout(C_INT)),
 75                     "Unsupported layout: [:b32]"

 76             },
 77         };
 78     }
 79 
 80 }
< prev index next >