8183040: update jdk/test/lib/Platform.java to use NIO file

Reviewed-by: amenkov, bpb
This commit is contained in:
Igor Ignatyev 2020-06-10 13:15:27 -07:00
parent 120a0d116a
commit b03766fb79
2 changed files with 28 additions and 30 deletions

View File

@ -23,9 +23,9 @@
package jdk.test.lib;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.security.AccessController;
@ -239,7 +239,7 @@ public class Platform {
String jdkPath = System.getProperty("java.home");
Path javaPath = Paths.get(jdkPath + "/bin/java");
String javaFileName = javaPath.toAbsolutePath().toString();
if (!javaPath.toFile().exists()) {
if (Files.notExists(javaPath)) {
throw new FileNotFoundException("Could not find file " + javaFileName);
}

View File

@ -22,13 +22,15 @@
*/
package jdk.test.lib.SA;
import jdk.test.lib.JDKToolLauncher;
import jdk.test.lib.Platform;
import jtreg.SkippedException;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.security.AccessController;
import java.security.PrivilegedAction;
import java.security.PrivilegedActionException;
import java.security.PrivilegedExceptionAction;
import java.util.ArrayList;
@ -37,13 +39,6 @@ import java.util.List;
import java.util.concurrent.TimeUnit;
import java.util.zip.GZIPInputStream;
import jdk.test.lib.Asserts;
import jdk.test.lib.JDKToolLauncher;
import jdk.test.lib.Platform;
import jdk.test.lib.process.OutputAnalyzer;
import jdk.test.lib.process.ProcessTools;
import jtreg.SkippedException;
public class SATestUtils {
/**
* Creates a ProcessBuilder, adding privileges (sudo) if needed.
@ -168,11 +163,15 @@ public class SATestUtils {
*/
private static boolean canPtraceAttachLinux() throws IOException {
// SELinux deny_ptrace:
File deny_ptrace = new File("/sys/fs/selinux/booleans/deny_ptrace");
if (deny_ptrace.exists()) {
try (RandomAccessFile file = AccessController.doPrivileged(
(PrivilegedExceptionAction<RandomAccessFile>) () -> new RandomAccessFile(deny_ptrace, "r"))) {
if (file.readByte() != '0') {
var deny_ptrace = Paths.get("/sys/fs/selinux/booleans/deny_ptrace");
if (Files.exists(deny_ptrace)) {
try {
var bb = AccessController.doPrivileged(
(PrivilegedExceptionAction<byte[]>) () -> Files.readAllBytes(deny_ptrace));
if (bb.length == 0) {
throw new Error("deny_ptrace is empty");
}
if (bb[0] != '0') {
return false;
}
} catch (PrivilegedActionException e) {
@ -186,11 +185,15 @@ public class SATestUtils {
// 1 - restricted ptrace: a process must be a children of the inferior or user is root
// 2 - only processes with CAP_SYS_PTRACE may use ptrace or user is root
// 3 - no attach: no processes may use ptrace with PTRACE_ATTACH
File ptrace_scope = new File("/proc/sys/kernel/yama/ptrace_scope");
if (ptrace_scope.exists()) {
try (RandomAccessFile file = AccessController.doPrivileged(
(PrivilegedExceptionAction<RandomAccessFile>) () -> new RandomAccessFile(ptrace_scope, "r"))) {
byte yama_scope = file.readByte();
var ptrace_scope = Paths.get("/proc/sys/kernel/yama/ptrace_scope");
if (Files.exists(ptrace_scope)) {
try {
var bb = AccessController.doPrivileged(
(PrivilegedExceptionAction<byte[]>) () -> Files.readAllBytes(ptrace_scope));
if (bb.length == 0) {
throw new Error("ptrace_scope is empty");
}
byte yama_scope = bb[0];
if (yama_scope == '3') {
return false;
}
@ -212,13 +215,8 @@ public class SATestUtils {
for (File gzCore : gzCores) {
String coreFileName = gzCore.getName().replace(".gz", "");
System.out.println("Unzipping core into " + coreFileName);
try (GZIPInputStream gzis = new GZIPInputStream(new FileInputStream(gzCore));
FileOutputStream fos = new FileOutputStream(coreFileName)) {
byte[] buffer = new byte[1024];
int length;
while ((length = gzis.read(buffer)) > 0) {
fos.write(buffer, 0, length);
}
try (GZIPInputStream gzis = new GZIPInputStream(Files.newInputStream(gzCore.toPath()))) {
Files.copy(gzis, Paths.get(coreFileName));
} catch (IOException e) {
throw new SkippedException("Not able to unzip file: " + gzCore.getAbsolutePath(), e);
}