diff -r f1fff0daf868 test/jdk/internal/jimage/JImageFindResourceTest.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/test/jdk/internal/jimage/JImageFindResourceTest.java Wed Nov 25 07:38:39 2015 +0300 @@ -0,0 +1,125 @@ +/* + * Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved. + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * This code is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 2 only, as + * published by the Free Software Foundation. + * + * This code is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * version 2 for more details (a copy is included in the LICENSE file that + * accompanied this code). + * + * You should have received a copy of the GNU General Public License version + * 2 along with this work; if not, write to the Free Software Foundation, + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. + * + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA + * or visit www.oracle.com if you need additional information or have any + * questions. + */ + +/* + * @modules java.base/jdk.internal.jimage + * @summary Additional test scenarios for JIMAGE_FindResource method + * @modules java.base/jdk.internal.jimage + */ + +import java.io.File; + +import jdk.internal.jimage.BasicImageReader; +import jdk.internal.jimage.ImageNativeSubstrate; + +import org.testng.annotations.DataProvider; +import org.testng.annotations.Optional; +import org.testng.annotations.Parameters; +import org.testng.annotations.Test; +import org.testng.Assert; +import org.testng.TestNG; + +@Test +public class JImageFindResourceTest { + + static String JAVA_HOME = System.getProperty("java.home"); + + static String imageFile = JAVA_HOME + File.separator + "lib" + File.separator + + "modules" + File.separator + "bootmodules.jimage"; + + @DataProvider(name="modules") + static Object[][] loadModules() { + return new Object[][] { + { "bad.module" } + }; + } + + @DataProvider(name="classes") + static Object[][] loadClasses() { + return new Object[][] { + { "no/such/Class.class" } + }; + } + + @DataProvider(name="versions") + static Object[][] loadVersions() { + return new Object[][] { + { "noversion" }, + { "8.0" } + }; + } + + @Test(dataProvider="modules") + static void badModuleNameTest(String badModule) throws Exception { + long jimageHandle = ImageNativeSubstrate.JIMAGE_Open(imageFile); + + long[] size = new long[1]; + long result = ImageNativeSubstrate.JIMAGE_FindResource( + jimageHandle, badModule, "9.0", "java/lang/String.class", size); + + System.out.println("FindResource with bad module " + badModule + " returns " + result); + Assert.assertTrue(result == 0, "FindResource returns " + result + " instead of 0"); + + ImageNativeSubstrate.JIMAGE_Close(jimageHandle); + } + + @Test(dataProvider="classes") + static void badClassNameTest(String badClass) throws Exception { + long jimageHandle = ImageNativeSubstrate.JIMAGE_Open(imageFile); + + long[] size = new long[1]; + long result = ImageNativeSubstrate.JIMAGE_FindResource( + jimageHandle, "java.base", "9.0", badClass, size); + + System.out.println("FindResource with bad class " + badClass + " returns " + result); + Assert.assertTrue(result == 0, "FindResource returns " + result + " instead of 0"); + + ImageNativeSubstrate.JIMAGE_Close(jimageHandle); + } + + @Test(dataProvider="versions") + static void badVersionTest(String badVersion) throws Exception { + long jimageHandle = ImageNativeSubstrate.JIMAGE_Open(imageFile); + + long[] size = new long[1]; + long result = ImageNativeSubstrate.JIMAGE_FindResource( + jimageHandle, "java.base", badVersion, "java/lang/String.class", size); + + System.out.println("FindResource with bad version " + badVersion + " returns " + result); + Assert.assertFalse(result == 0, "FindResource returns " + result + " instead of 0"); + + ImageNativeSubstrate.JIMAGE_Close(jimageHandle); + } + + // main method to run standalone from jtreg + + @Test(enabled=false) + @Parameters({"x"}) + @SuppressWarnings("raw_types") + public static void main(@Optional String[] args) { + Class[] testclass = { JImageFindResourceTest.class }; + TestNG testng = new TestNG(); + testng.setTestClasses(testclass); + testng.run(); + } +} diff -r f1fff0daf868 test/jdk/internal/jimage/JImageGetResourceTest.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/test/jdk/internal/jimage/JImageGetResourceTest.java Wed Nov 25 07:38:39 2015 +0300 @@ -0,0 +1,152 @@ +/* + * Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved. + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * This code is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 2 only, as + * published by the Free Software Foundation. + * + * This code is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * version 2 for more details (a copy is included in the LICENSE file that + * accompanied this code). + * + * You should have received a copy of the GNU General Public License version + * 2 along with this work; if not, write to the Free Software Foundation, + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. + * + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA + * or visit www.oracle.com if you need additional information or have any + * questions. + */ + +/* + * @modules java.base/jdk.internal.jimage + * @summary Additional test scenarios for JIMAGE_GetResource method + * @modules java.base/jdk.internal.jimage + */ + +import java.io.File; + +import jdk.internal.jimage.BasicImageReader; +import jdk.internal.jimage.ImageNativeSubstrate; + +import org.testng.annotations.DataProvider; +import org.testng.annotations.Optional; +import org.testng.annotations.Parameters; +import org.testng.annotations.Test; +import org.testng.Assert; +import org.testng.TestNG; + +@Test +public class JImageGetResourceTest { + + static String JAVA_HOME = System.getProperty("java.home"); + + static String imageFile = JAVA_HOME + File.separator + "lib" + File.separator + + "modules" + File.separator + "bootmodules.jimage"; + + static long maxCount = max(); + static long resSize = size(); + + @DataProvider(name="buffers") + static Object[][] loadBuffers() { + return new Object[][] { + { "zero size" }, + { "half size" } + }; + } + + @DataProvider(name="sizes") + static Object[][] loadSizes() { + return new Object[][] { + { new Long(0) } + }; + } + + @Test(dataProvider="buffers") + static void badBufferTest(String badBuffer) throws Exception { + long jimageHandle = ImageNativeSubstrate.JIMAGE_Open(imageFile); + + long[] size = new long[1]; + long location = ImageNativeSubstrate.JIMAGE_FindResource( + jimageHandle, "java.base", "9.0", "java/lang/String.class", size); + + long desiredSize = size[0]; + byte[] buffer = null; + switch (badBuffer) { + case "zero size": + buffer = new byte[] {}; + break; + case "half size": + buffer = new byte[(int)desiredSize / 2]; + break; + } + + long actualSize = ImageNativeSubstrate.JIMAGE_GetResource( + jimageHandle, location, buffer, desiredSize); + + System.out.println("GetResource with bad buffer " + badBuffer + " returns " + actualSize); + Assert.assertTrue(actualSize == 0, "GetResource returns " + actualSize + " instead of 0"); + + ImageNativeSubstrate.JIMAGE_Close(jimageHandle); + } + + @Test(dataProvider="sizes") + static void badSizeTest(Long badSize) throws Exception { + long jimageHandle = ImageNativeSubstrate.JIMAGE_Open(imageFile); + + long[] size = new long[1]; + long location = ImageNativeSubstrate.JIMAGE_FindResource( + jimageHandle, "java.base", "9.0", "java/lang/String.class", size); + + long desiredBadSize = badSize.longValue(); + byte[] buffer = new byte[(int)desiredBadSize]; + long actualSize = ImageNativeSubstrate.JIMAGE_GetResource( + jimageHandle, location, buffer, desiredBadSize); + + System.out.println("GetResource with bad size " + desiredBadSize + + " and real size " + size[0] + " returns " + actualSize); + long refSize = (desiredBadSize <= 0) ? 0 : desiredBadSize; + Assert.assertTrue(actualSize == refSize, "GetResource returns " + desiredBadSize + " instead of 0"); + + ImageNativeSubstrate.JIMAGE_Close(jimageHandle); + } + + static long max() { + try { + long jimageHandle = ImageNativeSubstrate.JIMAGE_Open(imageFile); + String[] names = new String[1]; + int max = ImageNativeSubstrate.JIMAGE_Resources(jimageHandle, names); + return (long)max; + } catch (Exception e) { + return 0; + } + } + + static long size() { + try { + long jimageHandle = ImageNativeSubstrate.JIMAGE_Open(imageFile); + long[] size = new long[1]; + long location = ImageNativeSubstrate.JIMAGE_FindResource( + jimageHandle, "java.base", "9.0", "java/lang/String.class", size); + ImageNativeSubstrate.JIMAGE_Close(jimageHandle); + return size[0]; + } catch (Exception e) { + return 0; + } + } + + // main method to run standalone from jtreg + + @Test(enabled=false) + @Parameters({"x"}) + @SuppressWarnings("raw_types") + public static void main(@Optional String[] args) { + Class[] testclass = { JImageGetResourceTest.class }; + TestNG testng = new TestNG(); + testng.setTestClasses(testclass); + testng.run(); + } +} diff -r f1fff0daf868 test/jdk/internal/jimage/JImageOpenCloseTest.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/test/jdk/internal/jimage/JImageOpenCloseTest.java Wed Nov 25 07:38:39 2015 +0300 @@ -0,0 +1,132 @@ +/* + * Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved. + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * This code is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 2 only, as + * published by the Free Software Foundation. + * + * This code is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * version 2 for more details (a copy is included in the LICENSE file that + * accompanied this code). + * + * You should have received a copy of the GNU General Public License version + * 2 along with this work; if not, write to the Free Software Foundation, + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. + * + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA + * or visit www.oracle.com if you need additional information or have any + * questions. + */ + +/* + * @modules java.base/jdk.internal.jimage + * @summary Additional test scenarios for JIMAGE_Open/JIMAGE_Close methods + * @modules java.base/jdk.internal.jimage + */ + +import java.io.File; +import java.io.FileOutputStream; + +import jdk.internal.jimage.BasicImageReader; +import jdk.internal.jimage.ImageNativeSubstrate; + +import org.testng.annotations.DataProvider; +import org.testng.annotations.Optional; +import org.testng.annotations.Parameters; +import org.testng.annotations.Test; +import org.testng.annotations.BeforeTest; +import org.testng.Assert; +import org.testng.TestNG; + +@Test +public class JImageOpenCloseTest { + + static String JAVA_HOME = System.getProperty("java.home"); + static String TEST_SRC = System.getProperty("test.src"); + static String TEST_DATA; + + static String imageFile = JAVA_HOME + File.separator + "lib" + File.separator + + "modules" + File.separator + "bootmodules.jimage"; + + @BeforeTest + public void createTestData() throws Exception { + File dataDir = new File("data"); + if (!dataDir.exists()) + dataDir.mkdir(); + + TEST_DATA = dataDir.getCanonicalPath(); + + // create empty + File empty = new File(dataDir, "empty.jimage"); + boolean created = empty.createNewFile(); + if (!created) + throw new RuntimeException("Cannot create data file " + empty.getCanonicalPath()); + + // create wrong magic + byte[] magic = new byte[] { (byte)0xb1, (byte)0xb1, (byte)0xfe, (byte)0xca }; + File wrong = new File(dataDir, "wrongmagic.jimage"); + created = wrong.createNewFile(); + if (!created) + throw new RuntimeException("Cannot create data file " + wrong.getCanonicalPath()); + try (FileOutputStream out = new FileOutputStream(wrong)) { + out.write(magic); + out.flush(); + } catch (Exception e) { + throw new RuntimeException("Cannot write data to file " + wrong.getCanonicalPath()); + } + } + + @DataProvider(name="badimages") + static Object[][] getImages() { + return new Object[][] { + { TEST_DATA + File.separator + "nonexisting.jimage" }, + { TEST_DATA + File.separator + "empty.jimage" }, + { TEST_DATA + File.separator + "wrongmagic.jimage" } + }; + } + + @Test + static void multipleOpenTest() throws Exception { + long jimageHandle = ImageNativeSubstrate.JIMAGE_Open(imageFile); + + long jimageHandle1 = ImageNativeSubstrate.JIMAGE_Open(imageFile); + + System.out.println("Multiple open ids first = " + jimageHandle + "; last = " + jimageHandle1); + Assert.assertTrue(jimageHandle == jimageHandle1, "multiple open should have same handle"); + + ImageNativeSubstrate.JIMAGE_Close(jimageHandle); + } + + @Test(dataProvider="badimages") + public static void badJImagesTest(String jimageName) throws Exception { + long jimageHandle = ImageNativeSubstrate.JIMAGE_Open(jimageName); + + System.out.println("Open bad image: " + jimageName + " returns " + jimageHandle); + Assert.assertTrue(jimageHandle == 0, "bad jimage should not be opened"); + } + + @Test + static void multipleCloseTest() throws Exception { + long jimageHandle = ImageNativeSubstrate.JIMAGE_Open(imageFile); + + System.out.println("Close the same jimage several times"); + for (int i = 0; i < 10; i++) { + ImageNativeSubstrate.JIMAGE_Close(jimageHandle); + } + } + + // main method to run standalone from jtreg + + @Test(enabled=false) + @Parameters({"x"}) + @SuppressWarnings("raw_types") + public static void main(@Optional String[] args) { + Class[] testclass = { JImageOpenCloseTest.class }; + TestNG testng = new TestNG(); + testng.setTestClasses(testclass); + testng.run(); + } +} diff -r f1fff0daf868 test/jdk/internal/jimage/JImagePackageToModuleTest.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/test/jdk/internal/jimage/JImagePackageToModuleTest.java Wed Nov 25 07:38:39 2015 +0300 @@ -0,0 +1,81 @@ +/* + * Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved. + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * This code is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 2 only, as + * published by the Free Software Foundation. + * + * This code is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * version 2 for more details (a copy is included in the LICENSE file that + * accompanied this code). + * + * You should have received a copy of the GNU General Public License version + * 2 along with this work; if not, write to the Free Software Foundation, + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. + * + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA + * or visit www.oracle.com if you need additional information or have any + * questions. + */ + +/* + * @modules java.base/jdk.internal.jimage + * @summary Additional test scenarios for JIMAGE_PackageToModule method + * @modules java.base/jdk.internal.jimage + */ + +import java.io.File; + +import jdk.internal.jimage.BasicImageReader; +import jdk.internal.jimage.ImageNativeSubstrate; + +import org.testng.annotations.DataProvider; +import org.testng.annotations.Optional; +import org.testng.annotations.Parameters; +import org.testng.annotations.Test; +import org.testng.Assert; +import org.testng.TestNG; + +@Test +public class JImagePackageToModuleTest { + + static String JAVA_HOME = System.getProperty("java.home"); + + static String imageFile = JAVA_HOME + File.separator + "lib" + File.separator + + "modules" + File.separator + "bootmodules.jimage"; + + @DataProvider(name="packages") + static Object[][] loadPackages() { + return new Object[][] { + { "does/not/exist" } + }; + } + + @Test(dataProvider="packages") + static void incorrectPackageValueTest(String packageName) throws Exception { + long jimageHandle = ImageNativeSubstrate.JIMAGE_Open(imageFile); + + String modName = ImageNativeSubstrate.JIMAGE_PackageToModule(jimageHandle, packageName); + + System.out.println("PackageToModule with package " + packageName + " returns " + modName); + Assert.assertTrue(modName == null, "PackageToModule with package " + packageName + + " returns " + modName + " instead of null"); + + ImageNativeSubstrate.JIMAGE_Close(jimageHandle); + } + + // main method to run standalone from jtreg + + @Test(enabled=false) + @Parameters({"x"}) + @SuppressWarnings("raw_types") + public static void main(@Optional String[] args) { + Class[] testclass = { JImagePackageToModuleTest.class }; + TestNG testng = new TestNG(); + testng.setTestClasses(testclass); + testng.run(); + } +} diff -r f1fff0daf868 test/jdk/internal/jimage/JImageResourcesTest.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/test/jdk/internal/jimage/JImageResourcesTest.java Wed Nov 25 07:38:39 2015 +0300 @@ -0,0 +1,75 @@ +/* + * Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved. + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * This code is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 2 only, as + * published by the Free Software Foundation. + * + * This code is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * version 2 for more details (a copy is included in the LICENSE file that + * accompanied this code). + * + * You should have received a copy of the GNU General Public License version + * 2 along with this work; if not, write to the Free Software Foundation, + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. + * + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA + * or visit www.oracle.com if you need additional information or have any + * questions. + */ + +/* + * @modules java.base/jdk.internal.jimage + * @summary Additional test scenarios for JIMAGE_Resources method + * @modules java.base/jdk.internal.jimage + */ + +import java.io.File; + +import jdk.internal.jimage.BasicImageReader; +import jdk.internal.jimage.ImageNativeSubstrate; + +import org.testng.annotations.DataProvider; +import org.testng.annotations.Optional; +import org.testng.annotations.Parameters; +import org.testng.annotations.Test; +import org.testng.Assert; +import org.testng.TestNG; + +@Test +public class JImageResourcesTest { + + static String JAVA_HOME = System.getProperty("java.home"); + + static String imageFile = JAVA_HOME + File.separator + "lib" + File.separator + + "modules" + File.separator + "bootmodules.jimage"; + + @Test + static void badNamesArrayTest() throws Exception { + long jimageHandle = ImageNativeSubstrate.JIMAGE_Open(imageFile); + + String[] names = new String[1024]; + int max1 = ImageNativeSubstrate.JIMAGE_Resources(jimageHandle, names); + int max2 = ImageNativeSubstrate.JIMAGE_Resources(jimageHandle, null); + + System.out.println("Resources with null array returns " + max2); + Assert.assertTrue(max1 == max2, "GetResource returns " + max2 + " instead of " + max1); + + ImageNativeSubstrate.JIMAGE_Close(jimageHandle); + } + + // main method to run standalone from jtreg + + @Test(enabled=false) + @Parameters({"x"}) + @SuppressWarnings("raw_types") + public static void main(@Optional String[] args) { + Class[] testclass = { JImageResourcesTest.class }; + TestNG testng = new TestNG(); + testng.setTestClasses(testclass); + testng.run(); + } +}