11/*
2- * Copyright (c) 2022, Oracle and/or its affiliates. All rights reserved.
2+ * Copyright (c) 2022, 2025, Oracle and/or its affiliates. All rights reserved.
33 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44 *
55 * This code is free software; you can redistribute it and/or modify it
2929 * @run testng/othervm InconsistentEntries
3030 */
3131
32+ import java .util .List ;
33+ import jdk .test .lib .Utils ;
34+ import jdk .test .lib .cds .CDSTestUtils ;
35+ import jdk .test .lib .process .ProcessTools ;
3236import org .testng .Assert ;
33- import org .testng .annotations .AfterTest ;
3437import org .testng .annotations .BeforeTest ;
3538import org .testng .annotations .Test ;
3639
3740import javax .crypto .*;
3841import java .io .File ;
39- import java .io .IOException ;
4042import java .nio .file .Files ;
4143import java .nio .file .Path ;
4244import java .nio .file .Paths ;
4547
4648public class InconsistentEntries {
4749
48- private static final String JDK_HOME = System .getProperty ("test.jdk" );
49- private static final String TEST_SRC = System .getProperty ("test.src" );
50- private static final Path POLICY_DIR = Paths .get (JDK_HOME , "conf" , "security" ,
51- "policy" , "testlimited" );
52- private static final Path POLICY_FILE = Paths .get (TEST_SRC , "default_local.policy" );
53-
54- Path targetFile = null ;
50+ private static final String JDK_HOME = System .getProperty ("test.jdk" , "." );
51+ private static final String TEST_SRC = System .getProperty ("test.src" , "." );
52+ private static final Path TEMP_JDK_HOME = Path .of ("java" );
53+ private static final Path POLICY_DIR = TEMP_JDK_HOME .resolve (Path .of ("conf" , "security" ,
54+ "policy" , "testlimited" ));
55+ private static final Path POLICY_FILE_SRC = Paths .get (TEST_SRC , "default_local.policy" );
56+ private static final Path POLICY_FILE_TARGET = POLICY_DIR
57+ .resolve (POLICY_FILE_SRC .getFileName ());
5558
5659 @ BeforeTest
57- public void setUp () throws IOException {
60+ public void setUp () throws Exception {
61+ // Clone the tested JDK to the scratch directory
62+ CDSTestUtils .clone (new File (JDK_HOME ), new File (TEMP_JDK_HOME .toString ()));
63+
64+ // create policy directory in the cloned JDK
5865 if (!POLICY_DIR .toFile ().exists ()) {
5966 Files .createDirectory (POLICY_DIR );
6067 }
6168
62- targetFile = POLICY_DIR .resolve (POLICY_FILE .getFileName ());
63- Files .copy (POLICY_FILE , targetFile , StandardCopyOption .REPLACE_EXISTING );
64- }
65-
66- @ AfterTest
67- public void cleanUp () throws IOException {
68- Files .delete (targetFile );
69+ // copy policy file into policy directory
70+ Files .copy (POLICY_FILE_SRC , POLICY_FILE_TARGET , StandardCopyOption .REPLACE_EXISTING );
6971 }
7072
71- @ Test
72- public void test () throws Exception {
73- String JAVA_HOME = System .getProperty ("java.home" );
74- String FS = System .getProperty ("file.separator" );
75- Path testlimited = Path .of (JAVA_HOME + FS + "conf" + FS + "security" +
76- FS + "policy" + FS + "testlimited" );
77- if (!Files .exists (testlimited )) {
73+ public static void main (String [] args ) throws Throwable {
74+ if (!Files .exists (POLICY_DIR )) {
7875 throw new RuntimeException (
7976 "custom policy subdirectory: testlimited does not exist" );
8077 }
81-
82- File testpolicy = new File (JAVA_HOME + FS + "conf" + FS + "security" +
83- FS + "policy" + FS + "testlimited" + FS + "default_local.policy" );
78+ File testpolicy = new File (POLICY_FILE_TARGET .toString ());
8479 if (testpolicy .length () == 0 ) {
8580 throw new RuntimeException (
8681 "policy: default_local.policy does not exist or is empty" );
@@ -91,4 +86,16 @@ public void test() throws Exception {
9186 Assert .assertThrows (ExceptionInInitializerError .class ,
9287 () -> Cipher .getMaxAllowedKeyLength ("AES" ));
9388 }
89+
90+ @ Test
91+ public void test () throws Exception {
92+ String tmpJava = TEMP_JDK_HOME .resolve ("bin" ).resolve ("java" ).toString ();
93+ String [] args = Utils .prependTestJavaOpts (InconsistentEntries .class .getName ());
94+ ProcessBuilder pb = new ProcessBuilder (tmpJava );
95+ pb .command ().addAll (List .of (args ));
96+
97+ ProcessTools
98+ .executeProcess (pb )
99+ .shouldHaveExitValue (0 );
100+ }
94101}
0 commit comments