|
23 | 23 | * questions. |
24 | 24 | */ |
25 | 25 |
|
26 | | -/** |
| 26 | +/* |
27 | 27 | * @test |
28 | 28 | * @bug 8286779 |
29 | 29 | * @summary Test limited/default_local.policy containing inconsistent entries |
30 | | - * @run main/manual InconsistentEntries |
| 30 | + * @library /test/lib |
| 31 | + * @run testng/othervm InconsistentEntries |
31 | 32 | */ |
| 33 | + |
| 34 | +import org.testng.Assert; |
| 35 | +import org.testng.annotations.AfterTest; |
| 36 | +import org.testng.annotations.BeforeTest; |
| 37 | +import org.testng.annotations.Test; |
| 38 | + |
32 | 39 | import javax.crypto.*; |
33 | 40 | import java.io.File; |
| 41 | +import java.io.IOException; |
34 | 42 | import java.nio.file.Files; |
35 | 43 | import java.nio.file.Path; |
| 44 | +import java.nio.file.Paths; |
| 45 | +import java.nio.file.StandardCopyOption; |
36 | 46 | import java.security.Security; |
37 | 47 |
|
38 | 48 | public class InconsistentEntries { |
39 | 49 |
|
40 | | - public static void main(String[] args) throws Exception { |
41 | | - System.out.println("***********************************************************"); |
42 | | - System.out.println("// This is a manual test to test a custom \"default_local.policy\" containing inconsistent entries"); |
43 | | - System.out.println("// under a new subfolder \"$JAVA_HOME/conf/security/policy\" directory."); |
44 | | - System.out.println("// This test fails when the policy directory \"testlimited\" or the policy \"default_local.policy"); |
45 | | - System.out.println("// does not exist or is empty."); |
46 | | - System.out.println("// - Create a new subfolder \"testlimited\" under \"$JAVA_HOME/conf/security/policy\""); |
47 | | - System.out.println("// - Place the custom \"default_local.policy\" under \"testlimited\" directory"); |
48 | | - System.out.println("// - default_local.policy contains:"); |
49 | | - System.out.println("// grant {"); |
50 | | - System.out.println("// permission javax.crypto.CryptoAllPermission;"); |
51 | | - System.out.println("// permission javax.crypto.CryptoPermission \"DES\", 64;"); |
52 | | - System.out.println("// };"); |
53 | | - System.out.println("***********************************************************"); |
| 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 POLICY_DIR = Paths.get(JDK_HOME, "conf", "security", |
| 53 | + "policy", "testlimited"); |
| 54 | + private static final Path POLICY_FILE = Paths.get(TEST_SRC, "default_local.policy"); |
| 55 | + |
| 56 | + Path targetFile = null; |
| 57 | + |
| 58 | + @BeforeTest |
| 59 | + public void setUp() throws IOException { |
| 60 | + if (!POLICY_DIR.toFile().exists()) { |
| 61 | + Files.createDirectory(POLICY_DIR); |
| 62 | + } |
54 | 63 |
|
| 64 | + targetFile = POLICY_DIR.resolve(POLICY_FILE.getFileName()); |
| 65 | + Files.copy(POLICY_FILE, targetFile, StandardCopyOption.REPLACE_EXISTING); |
| 66 | + } |
| 67 | + |
| 68 | + @AfterTest |
| 69 | + public void cleanUp() throws IOException { |
| 70 | + Files.delete(targetFile); |
| 71 | + } |
| 72 | + |
| 73 | + @Test |
| 74 | + public void test() throws Exception { |
55 | 75 | String JAVA_HOME = System.getProperty("java.home"); |
56 | 76 | String FS = System.getProperty("file.separator"); |
57 | 77 | Path testlimited = Path.of(JAVA_HOME + FS + "conf" + FS + "security" + |
58 | 78 | FS + "policy" + FS + "testlimited"); |
59 | 79 | if (!Files.exists(testlimited)) { |
60 | | - throw new RuntimeException("custom policy subdirectory: testlimited does not exist"); |
| 80 | + throw new RuntimeException( |
| 81 | + "custom policy subdirectory: testlimited does not exist"); |
61 | 82 | } |
62 | 83 |
|
63 | 84 | File testpolicy = new File(JAVA_HOME + FS + "conf" + FS + "security" + |
64 | 85 | FS + "policy" + FS + "testlimited" + FS + "default_local.policy"); |
65 | 86 | if (testpolicy.length() == 0) { |
66 | | - throw new RuntimeException("policy: default_local.policy does not exist or is empty"); |
| 87 | + throw new RuntimeException( |
| 88 | + "policy: default_local.policy does not exist or is empty"); |
67 | 89 | } |
68 | 90 |
|
69 | 91 | Security.setProperty("crypto.policy", "testlimited"); |
70 | 92 |
|
71 | | - try { |
72 | | - int maxKeyLen = Cipher.getMaxAllowedKeyLength("AES"); |
73 | | - throw new RuntimeException("Should fail due to inconsistent entries in policy file"); |
74 | | - } catch (ExceptionInInitializerError e) { |
75 | | - e.printStackTrace(); |
76 | | - System.out.println("Test completed successfully"); |
77 | | - } |
| 93 | + Assert.assertThrows(ExceptionInInitializerError.class, |
| 94 | + () -> Cipher.getMaxAllowedKeyLength("AES")); |
78 | 95 | } |
79 | 96 | } |
0 commit comments