|
| 1 | +/* |
| 2 | + * Copyright (c) 2022, Red Hat, Inc. |
| 3 | + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
| 4 | + * |
| 5 | + * This code is free software; you can redistribute it and/or modify it |
| 6 | + * under the terms of the GNU General Public License version 2 only, as |
| 7 | + * published by the Free Software Foundation. |
| 8 | + * |
| 9 | + * This code is distributed in the hope that it will be useful, but WITHOUT |
| 10 | + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 11 | + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
| 12 | + * version 2 for more details (a copy is included in the LICENSE file that |
| 13 | + * accompanied this code). |
| 14 | + * |
| 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 | +import jdk.testlibrary.JarUtils; |
| 25 | +import jdk.testlibrary.SecurityTools; |
| 26 | +import jdk.testlibrary.Asserts; |
| 27 | +import jdk.testlibrary.ProcessTools; |
| 28 | +import jdk.testlibrary.OutputAnalyzer; |
| 29 | +import java.util.Calendar; |
| 30 | +import java.util.Locale; |
| 31 | +import java.util.List; |
| 32 | +import java.util.ArrayList; |
| 33 | +import java.nio.file.Paths; |
| 34 | +import java.nio.file.Path; |
| 35 | +import java.nio.file.Files; |
| 36 | +import java.io.File; |
| 37 | +import static java.util.Calendar.WEDNESDAY; |
| 38 | + |
| 39 | +/* |
| 40 | + * @test |
| 41 | + * @bug 8297684 8269039 |
| 42 | + * @summary Checking custom CalendarDataProvider with SPI contained in signed jar does |
| 43 | + * not produce NPE. |
| 44 | + * @library /lib/testlibrary |
| 45 | + * @library provider |
| 46 | + * @build baz.CalendarDataProviderImpl |
| 47 | + * @run main/timeout=600 TestSPISigned |
| 48 | + */ |
| 49 | +public class TestSPISigned { |
| 50 | + |
| 51 | + private static final String TEST_CLASSES = System.getProperty("test.classes", "."); |
| 52 | + private static final String TEST_SRC = System.getProperty("test.src", "."); |
| 53 | + |
| 54 | + private static final Path META_INF_DIR = Paths.get(TEST_SRC, "provider", "meta"); |
| 55 | + private static final Path META_INF_DIR_SOURCE = META_INF_DIR.resolve("META-INF"); |
| 56 | + private static final Path SERVICES_DIR_SOURCE = META_INF_DIR_SOURCE.resolve("services"); |
| 57 | + private static final Path PROVIDER_SOURCE = SERVICES_DIR_SOURCE.resolve("java.util.spi.CalendarDataProvider"); |
| 58 | + private static final Path PROVIDER_PARENT = Paths.get(TEST_CLASSES); |
| 59 | + private static final Path PROVIDER_DIR = PROVIDER_PARENT.resolve("provider"); |
| 60 | + private static final Path META_INF_DIR_TARGET = PROVIDER_DIR.resolve("META-INF"); |
| 61 | + private static final Path SERVICES_DIR_TARGET = META_INF_DIR_TARGET.resolve("services"); |
| 62 | + private static final Path PROVIDER_TARGET = SERVICES_DIR_TARGET.resolve("java.util.spi.CalendarDataProvider"); |
| 63 | + private static final Path MODS_DIR = Paths.get("mods"); |
| 64 | + private static final Path UNSIGNED_JAR = MODS_DIR.resolve("unsigned-with-locale.jar"); |
| 65 | + private static final Path SIGNED_JAR = MODS_DIR.resolve("signed-with-locale.jar"); |
| 66 | + |
| 67 | + public static void main(String[] args) throws Throwable { |
| 68 | + if (args.length == 1) { |
| 69 | + String arg = args[0]; |
| 70 | + if ("run-test".equals(arg)) { |
| 71 | + System.out.println("Debug: Running test"); |
| 72 | + String provProp = System.getProperty("java.ext.dirs"); |
| 73 | + String[] extDirs = provProp.split(File.pathSeparator); |
| 74 | + boolean extDirFound = false; |
| 75 | + for (int i = 0; i < extDirs.length; i++) { |
| 76 | + if (extDirs[i].equals(MODS_DIR.toAbsolutePath().toString())) { |
| 77 | + extDirFound = true; |
| 78 | + break; |
| 79 | + } |
| 80 | + } |
| 81 | + if (!extDirFound) { |
| 82 | + throw new RuntimeException("Test failed! Expected -Djava.ext.dirs to include CalendarDataProvider jar"); |
| 83 | + } |
| 84 | + doRunTest(); |
| 85 | + } else { |
| 86 | + throw new RuntimeException("Test failed! Expected 'run-test' arg for test run"); |
| 87 | + } |
| 88 | + } else { |
| 89 | + // Set up signed jar with custom calendar data provider |
| 90 | + // |
| 91 | + // 1. Create jar with custom CalendarDataProvider |
| 92 | + Files.createDirectory(META_INF_DIR_TARGET); |
| 93 | + Files.createDirectory(SERVICES_DIR_TARGET); |
| 94 | + Files.copy(PROVIDER_SOURCE, PROVIDER_TARGET); |
| 95 | + JarUtils.createJarFile(UNSIGNED_JAR, PROVIDER_DIR); |
| 96 | + |
| 97 | + // create signer's keypair |
| 98 | + SecurityTools.keytool("-genkeypair -keyalg RSA -keystore ks " + |
| 99 | + "-storepass changeit -dname CN=test -alias test") |
| 100 | + .shouldHaveExitValue(0); |
| 101 | + // sign jar |
| 102 | + SecurityTools.jarsigner("-keystore ks -storepass changeit " + |
| 103 | + "-signedjar " + SIGNED_JAR + " " + UNSIGNED_JAR + " test") |
| 104 | + .shouldHaveExitValue(0); |
| 105 | + // run test, which must not throw a NPE |
| 106 | + List<String> testRun = new ArrayList<>(); |
| 107 | + String extDir = System.getProperty("java.home") + File.separator + "lib" + File.separator + "ext"; |
| 108 | + testRun.add("-Djava.ext.dirs=" + extDir + File.pathSeparator + MODS_DIR.toAbsolutePath().toString()); |
| 109 | + testRun.add("-cp"); |
| 110 | + String classPath = System.getProperty("java.class.path"); |
| 111 | + testRun.add(classPath); |
| 112 | + testRun.add(TestSPISigned.class.getSimpleName()); |
| 113 | + testRun.add("run-test"); |
| 114 | + OutputAnalyzer out = ProcessTools.executeTestJvm(testRun.toArray(new String[]{})); |
| 115 | + out.shouldHaveExitValue(0); |
| 116 | + out.shouldContain("DEBUG: Getting xx language"); |
| 117 | + } |
| 118 | + } |
| 119 | + |
| 120 | + private static void doRunTest() { |
| 121 | + Locale locale = new Locale("xx", "YY"); |
| 122 | + Calendar kcal = Calendar.getInstance(locale); |
| 123 | + try { |
| 124 | + check(WEDNESDAY, kcal.getFirstDayOfWeek()); |
| 125 | + check(7, kcal.getMinimalDaysInFirstWeek()); |
| 126 | + } catch (Throwable ex) { |
| 127 | + throw new RuntimeException("Test failed with signed jar and " + |
| 128 | + " argument java.locale.providers=SPI", ex); |
| 129 | + } |
| 130 | + } |
| 131 | + |
| 132 | + private static <T> void check(T expected, T actual) { |
| 133 | + Asserts.assertEquals(expected, actual, "Expected calendar from SPI to be in effect"); |
| 134 | + } |
| 135 | + |
| 136 | +} |
0 commit comments