Path: blob/master/test/jdk/java/foreign/TestIllegalLink.java
41144 views
/*1* Copyright (c) 2020, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*22*/2324/*25* @test26* @requires ((os.arch == "amd64" | os.arch == "x86_64") & sun.arch.data.model == "64") | os.arch == "aarch64"27* @run testng/othervm --enable-native-access=ALL-UNNAMED TestIllegalLink28*/2930import jdk.incubator.foreign.CLinker;31import jdk.incubator.foreign.FunctionDescriptor;32import jdk.incubator.foreign.MemoryAddress;33import jdk.incubator.foreign.MemoryLayout;34import jdk.incubator.foreign.MemoryLayouts;35import jdk.incubator.foreign.MemorySegment;36import org.testng.annotations.DataProvider;37import org.testng.annotations.Test;3839import java.lang.invoke.MethodType;4041import static jdk.incubator.foreign.CLinker.C_INT;42import static org.testng.Assert.assertTrue;43import static org.testng.Assert.fail;4445public class TestIllegalLink {4647private static final MemoryAddress DUMMY_TARGET = MemoryAddress.ofLong(1);48private static final CLinker ABI = CLinker.getInstance();4950@Test(dataProvider = "types")51public void testTypeMismatch(MethodType mt, FunctionDescriptor desc, String expectedExceptionMessage) {52try {53ABI.downcallHandle(DUMMY_TARGET, mt, desc);54fail("Expected IllegalArgumentException was not thrown");55} catch (IllegalArgumentException e) {56assertTrue(e.getMessage().contains(expectedExceptionMessage));57}58}5960@DataProvider61public static Object[][] types() {62return new Object[][]{63{64MethodType.methodType(void.class),65FunctionDescriptor.of(C_INT),66"Return type mismatch"67},68{69MethodType.methodType(void.class),70FunctionDescriptor.ofVoid(C_INT),71"Arity mismatch"72},73{74MethodType.methodType(void.class, int.class),75FunctionDescriptor.ofVoid(MemoryLayout.paddingLayout(32)),76"Expected a ValueLayout"77},78{79MethodType.methodType(void.class, boolean.class),80FunctionDescriptor.ofVoid(MemoryLayouts.BITS_8_LE),81"Unsupported carrier"82},83{84MethodType.methodType(void.class, int.class),85FunctionDescriptor.ofVoid(MemoryLayouts.BITS_64_LE),86"Carrier size mismatch"87},88{89MethodType.methodType(void.class, MemoryAddress.class),90FunctionDescriptor.ofVoid(MemoryLayout.paddingLayout(64)),91"Expected a ValueLayout"92},93{94MethodType.methodType(void.class, MemoryAddress.class),95FunctionDescriptor.ofVoid(MemoryLayouts.BITS_16_LE),96"Address size mismatch"97},98{99MethodType.methodType(void.class, MemorySegment.class),100FunctionDescriptor.ofVoid(MemoryLayouts.BITS_64_LE),101"Expected a GroupLayout"102},103{104MethodType.methodType(void.class, String.class),105FunctionDescriptor.ofVoid(MemoryLayouts.BITS_64_LE),106"Unsupported carrier"107},108};109}110111}112113114