Skip to content

Commit

Permalink
8342238: Test javax/crypto/CryptoPermissions/InconsistentEntries.java…
Browse files Browse the repository at this point in the history
… writes files in tested JDK dir

Reviewed-by: jnimeh, rhalade
  • Loading branch information
fguallini authored and rhalade committed Feb 20, 2025
1 parent f979f72 commit 16033ea
Showing 1 changed file with 35 additions and 28 deletions.
63 changes: 35 additions & 28 deletions test/jdk/javax/crypto/CryptoPermissions/InconsistentEntries.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2022, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2022, 2025, 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
Expand Down Expand Up @@ -29,14 +29,16 @@
* @run testng/othervm InconsistentEntries
*/

import java.util.List;
import jdk.test.lib.Utils;
import jdk.test.lib.cds.CDSTestUtils;
import jdk.test.lib.process.ProcessTools;
import org.testng.Assert;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;

import javax.crypto.*;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
Expand All @@ -45,42 +47,35 @@

public class InconsistentEntries {

private static final String JDK_HOME = System.getProperty("test.jdk");
private static final String TEST_SRC = System.getProperty("test.src");
private static final Path POLICY_DIR = Paths.get(JDK_HOME, "conf", "security",
"policy", "testlimited");
private static final Path POLICY_FILE = Paths.get(TEST_SRC, "default_local.policy");

Path targetFile = null;
private static final String JDK_HOME = System.getProperty("test.jdk", ".");
private static final String TEST_SRC = System.getProperty("test.src", ".");
private static final Path TEMP_JDK_HOME = Path.of("java");
private static final Path POLICY_DIR = TEMP_JDK_HOME.resolve(Path.of("conf", "security",
"policy", "testlimited"));
private static final Path POLICY_FILE_SRC = Paths.get(TEST_SRC, "default_local.policy");
private static final Path POLICY_FILE_TARGET = POLICY_DIR
.resolve(POLICY_FILE_SRC.getFileName());

@BeforeTest
public void setUp() throws IOException {
public void setUp() throws Exception {
// Clone the tested JDK to the scratch directory
CDSTestUtils.clone(new File(JDK_HOME), new File(TEMP_JDK_HOME.toString()));

// create policy directory in the cloned JDK
if (!POLICY_DIR.toFile().exists()) {
Files.createDirectory(POLICY_DIR);
}

targetFile = POLICY_DIR.resolve(POLICY_FILE.getFileName());
Files.copy(POLICY_FILE, targetFile, StandardCopyOption.REPLACE_EXISTING);
}

@AfterTest
public void cleanUp() throws IOException {
Files.delete(targetFile);
// copy policy file into policy directory
Files.copy(POLICY_FILE_SRC, POLICY_FILE_TARGET, StandardCopyOption.REPLACE_EXISTING);
}

@Test
public void test() throws Exception {
String JAVA_HOME = System.getProperty("java.home");
String FS = System.getProperty("file.separator");
Path testlimited = Path.of(JAVA_HOME + FS + "conf" + FS + "security" +
FS + "policy" + FS + "testlimited");
if (!Files.exists(testlimited)) {
public static void main(String[] args) throws Throwable {
if (!Files.exists(POLICY_DIR)) {
throw new RuntimeException(
"custom policy subdirectory: testlimited does not exist");
}

File testpolicy = new File(JAVA_HOME + FS + "conf" + FS + "security" +
FS + "policy" + FS + "testlimited" + FS + "default_local.policy");
File testpolicy = new File(POLICY_FILE_TARGET.toString());
if (testpolicy.length() == 0) {
throw new RuntimeException(
"policy: default_local.policy does not exist or is empty");
Expand All @@ -91,4 +86,16 @@ public void test() throws Exception {
Assert.assertThrows(ExceptionInInitializerError.class,
() -> Cipher.getMaxAllowedKeyLength("AES"));
}

@Test
public void test() throws Exception {
String tmpJava = TEMP_JDK_HOME.resolve("bin").resolve("java").toString();
String[] args = Utils.prependTestJavaOpts(InconsistentEntries.class.getName());
ProcessBuilder pb = new ProcessBuilder(tmpJava);
pb.command().addAll(List.of(args));

ProcessTools
.executeProcess(pb)
.shouldHaveExitValue(0);
}
}

0 comments on commit 16033ea

Please sign in to comment.