Skip to content

Commit

Permalink
8327474: Review use of java.io.tmpdir in jdk tests
Browse files Browse the repository at this point in the history
Reviewed-by: michaelm, jpai
  • Loading branch information
Bill Huang committed Apr 3, 2024
1 parent 233619b commit 375bfac
Show file tree
Hide file tree
Showing 11 changed files with 180 additions and 127 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2013, 2023, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2013, 2024, 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 @@ -41,6 +41,7 @@
import java.io.InputStream;
import java.io.PrintWriter;
import java.lang.management.ManagementFactory;
import java.nio.file.Path;
import java.util.Map;
import jdk.test.lib.process.ProcessTools;
import sun.tools.attach.HotSpotVirtualMachine;
Expand All @@ -53,7 +54,7 @@ public static void main(String... args) throws Exception {
if (args.length == 0) {
// start a process that has options set in a number of different ways

File flagsFile = File.createTempFile("CheckOriginFlags", null);
File flagsFile = File.createTempFile("CheckOriginFlags", null, Path.of(".").toFile());
try (PrintWriter pw =
new PrintWriter(new FileWriter(flagsFile))) {
pw.println("+PrintCodeCache");
Expand Down
6 changes: 3 additions & 3 deletions test/jdk/java/io/File/CheckPermission.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2013, 2024, 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 @@ -362,13 +362,13 @@ public boolean accept(File file) {
"getFileSystemAttributes");

prepare();
File tmpFile = File.createTempFile(CHECK_PERMISSION_TEST, null);
File tmpFile = File.createTempFile(CHECK_PERMISSION_TEST, null, new File("."));
assertOnlyCheckOperation(tmpFile, EnumSet.of(FileOperation.WRITE));
tmpFile.delete();
assertCheckOperation(tmpFile, EnumSet.of(FileOperation.DELETE));

prepare();
tmpFile = File.createTempFile(CHECK_PERMISSION_TEST, null, null);
tmpFile = File.createTempFile(CHECK_PERMISSION_TEST, null, new File("."));
assertOnlyCheckOperation(tmpFile, EnumSet.of(FileOperation.WRITE));
tmpFile.delete();
assertCheckOperation(tmpFile, EnumSet.of(FileOperation.DELETE));
Expand Down
41 changes: 22 additions & 19 deletions test/jdk/java/io/FileInputStream/NegativeAvailable.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2013, 2024, 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 @@ -46,28 +46,31 @@ public static void main(String[] args) throws IOException {

// Create a temporary file with size of 10 bytes.
Path tmp = Files.createTempFile(null, null);
try (BufferedWriter writer =
Files.newBufferedWriter(tmp, Charset.defaultCharset())) {
for (int i = 0; i < SIZE; i++) {
writer.write('1');
try {
try (BufferedWriter writer =
Files.newBufferedWriter(tmp, Charset.defaultCharset())) {
for (int i = 0; i < SIZE; i++) {
writer.write('1');
}
}
}

File tempFile = tmp.toFile();
try (FileInputStream fis = new FileInputStream(tempFile)) {
if (tempFile.length() != SIZE) {
throw new RuntimeException("unexpected file size = "
+ tempFile.length());
File tempFile = tmp.toFile();
try (FileInputStream fis = new FileInputStream(tempFile)) {
if (tempFile.length() != SIZE) {
throw new RuntimeException("unexpected file size = "
+ tempFile.length());
}
long space = skipBytes(fis, SKIP, SIZE);
space = skipBytes(fis, NEGATIVE_SKIP, space);
space = skipBytes(fis, SKIP, space);
space = skipBytes(fis, SKIP, space);
space = skipBytes(fis, SKIP, space);
space = skipBytes(fis, NEGATIVE_SKIP, space);
space = skipBytes(fis, NEGATIVE_SKIP, space);
}
long space = skipBytes(fis, SKIP, SIZE);
space = skipBytes(fis, NEGATIVE_SKIP, space);
space = skipBytes(fis, SKIP, space);
space = skipBytes(fis, SKIP, space);
space = skipBytes(fis, SKIP, space);
space = skipBytes(fis, NEGATIVE_SKIP, space);
space = skipBytes(fis, NEGATIVE_SKIP, space);
} finally {
Files.deleteIfExists(tmp);
}
Files.deleteIfExists(tmp);
}

/**
Expand Down
72 changes: 46 additions & 26 deletions test/jdk/java/nio/channels/unixdomain/Bind.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2020, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2020, 2024, 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 @@ -159,13 +159,17 @@ public static void runTests() throws IOException {
});
// address with space should work
checkNormal(() -> {
server = ServerSocketChannel.open(StandardProtocolFamily.UNIX);
UnixDomainSocketAddress usa = UnixDomainSocketAddress.of("with space"); // relative to CWD
UnixDomainSocketAddress usa = UnixDomainSocketAddress.of("with space");
Files.deleteIfExists(usa.getPath());
server.bind(usa);
client = SocketChannel.open(usa);
Files.delete(usa.getPath());
assertAddress(client.getRemoteAddress(), usa, "address");
try {
server = ServerSocketChannel.open(StandardProtocolFamily.UNIX);
// relative to CWD
server.bind(usa);
client = SocketChannel.open(usa);
assertAddress(client.getRemoteAddress(), usa, "address");
} finally {
Files.deleteIfExists(usa.getPath());
}
});
// client bind to null: allowed
checkNormal(() -> {
Expand All @@ -185,12 +189,19 @@ public static void runTests() throws IOException {
});
// server bind to null: should bind to a local address
checkNormal(() -> {
server = ServerSocketChannel.open(StandardProtocolFamily.UNIX);
server.bind(null);
UnixDomainSocketAddress usa = (UnixDomainSocketAddress)server.getLocalAddress();
if (usa.getPath().toString().isEmpty())
throw new RuntimeException("expected non zero address length");
System.out.println("Null server address: " + server.getLocalAddress());
UnixDomainSocketAddress usa = null;
try {
server = ServerSocketChannel.open(StandardProtocolFamily.UNIX);
server.bind(null);
usa = (UnixDomainSocketAddress) server.getLocalAddress();
if (usa.getPath().toString().isEmpty())
throw new RuntimeException("expected non zero address length");
System.out.println("Null server address: " + server.getLocalAddress());
} finally {
if (usa != null) {
Files.deleteIfExists(usa.getPath());
}
}
});
// server no bind : not allowed
checkException(
Expand Down Expand Up @@ -307,23 +318,32 @@ public static void runTests() throws IOException {
Arrays.fill(chars, 'x');
String name = new String(chars);
UnixDomainSocketAddress address = UnixDomainSocketAddress.of(name);
ServerSocketChannel server = ServerSocketChannel.open(StandardProtocolFamily.UNIX);
server.bind(address);
SocketChannel client = SocketChannel.open(address);
assertAddress(server.getLocalAddress(), address, "server");
assertAddress(client.getRemoteAddress(), address, "client");
Files.delete(address.getPath());
try {
ServerSocketChannel server = ServerSocketChannel.open(StandardProtocolFamily.UNIX);
server.bind(address);
SocketChannel client = SocketChannel.open(address);
assertAddress(server.getLocalAddress(), address, "server");
assertAddress(client.getRemoteAddress(), address, "client");
} finally {
Files.deleteIfExists(address.getPath());
}
});

// implicit server bind
checkNormal(() -> {
server = ServerSocketChannel.open(StandardProtocolFamily.UNIX);
server.bind(null);
UnixDomainSocketAddress usa = (UnixDomainSocketAddress)server.getLocalAddress();
client = SocketChannel.open(usa);
accept1 = server.accept();
assertAddress(client.getRemoteAddress(), usa, "server");
Files.delete(usa.getPath());
UnixDomainSocketAddress usa = null;
try {
server = ServerSocketChannel.open(StandardProtocolFamily.UNIX);
server.bind(null);
usa = (UnixDomainSocketAddress) server.getLocalAddress();
client = SocketChannel.open(usa);
accept1 = server.accept();
assertAddress(client.getRemoteAddress(), usa, "server");
} finally {
if (usa != null) {
Files.deleteIfExists(usa.getPath());
}
}
});
}
}
11 changes: 10 additions & 1 deletion test/jdk/java/nio/channels/unixdomain/NonBlockingAccept.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2020, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2020, 2024, 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,8 +29,11 @@
*/

import java.net.StandardProtocolFamily;
import java.net.UnixDomainSocketAddress;
import java.nio.channels.ServerSocketChannel;
import java.nio.channels.SocketChannel;
import java.nio.file.Files;

import jtreg.SkippedException;

public class NonBlockingAccept {
Expand All @@ -48,17 +51,23 @@ static void checkSupported() {
public static void main(String[] args) throws Exception {

checkSupported();
UnixDomainSocketAddress addr = null;

try (ServerSocketChannel serverSocketChannel =
ServerSocketChannel.open(StandardProtocolFamily.UNIX)) {
//non blocking mode
serverSocketChannel.configureBlocking(false);
serverSocketChannel.bind(null);
addr = (UnixDomainSocketAddress) serverSocketChannel.getLocalAddress();
SocketChannel socketChannel = serverSocketChannel.accept();
System.out.println("The socketChannel is : expected Null " + socketChannel);
if (socketChannel != null)
throw new RuntimeException("expected null");
// or exception could be thrown otherwise
} finally {
if (addr != null) {
Files.deleteIfExists(addr.getPath());
}
}
}
}
Expand Down
58 changes: 32 additions & 26 deletions test/jdk/java/util/zip/ZipFile/ZeroDate.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2017, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2017, 2024, 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 @@ -40,41 +40,47 @@
import java.util.zip.ZipFile;
import java.util.zip.ZipOutputStream;

import jdk.test.lib.Utils;

/* @test
* @bug 8184940 8188869
* @summary JDK 9 rejects zip files where the modified day or month is 0
* or otherwise represent an invalid date, such as 1980-02-30 24:60:60
* @author Liam Miller-Cushon
* @library /test/lib
*/
public class ZeroDate {

public static void main(String[] args) throws Exception {
// create a zip file, and read it in as a byte array
Path path = Files.createTempFile("bad", ".zip");
try (OutputStream os = Files.newOutputStream(path);
ZipOutputStream zos = new ZipOutputStream(os)) {
ZipEntry e = new ZipEntry("x");
zos.putNextEntry(e);
zos.write((int) 'x');
}
int len = (int) Files.size(path);
byte[] data = new byte[len];
try (InputStream is = Files.newInputStream(path)) {
is.read(data);
}
Files.delete(path);
Path path = Utils.createTempFile("bad", ".zip");
try {
try (OutputStream os = Files.newOutputStream(path);
ZipOutputStream zos = new ZipOutputStream(os)) {
ZipEntry e = new ZipEntry("x");
zos.putNextEntry(e);
zos.write((int) 'x');
}
int len = (int) Files.size(path);
byte[] data = new byte[len];
try (InputStream is = Files.newInputStream(path)) {
is.read(data);
}

// year, month, day are zero
testDate(data.clone(), 0, LocalDate.of(1979, 11, 30).atStartOfDay());
// only year is zero
testDate(data.clone(), 0 << 25 | 4 << 21 | 5 << 16, LocalDate.of(1980, 4, 5).atStartOfDay());
// month is greater than 12
testDate(data.clone(), 0 << 25 | 13 << 21 | 1 << 16, LocalDate.of(1981, 1, 1).atStartOfDay());
// 30th of February
testDate(data.clone(), 0 << 25 | 2 << 21 | 30 << 16, LocalDate.of(1980, 3, 1).atStartOfDay());
// 30th of February, 24:60:60
testDate(data.clone(), 0 << 25 | 2 << 21 | 30 << 16 | 24 << 11 | 60 << 5 | 60 >> 1,
LocalDateTime.of(1980, 3, 2, 1, 1, 0));
// year, month, day are zero
testDate(data.clone(), 0, LocalDate.of(1979, 11, 30).atStartOfDay());
// only year is zero
testDate(data.clone(), 0 << 25 | 4 << 21 | 5 << 16, LocalDate.of(1980, 4, 5).atStartOfDay());
// month is greater than 12
testDate(data.clone(), 0 << 25 | 13 << 21 | 1 << 16, LocalDate.of(1981, 1, 1).atStartOfDay());
// 30th of February
testDate(data.clone(), 0 << 25 | 2 << 21 | 30 << 16, LocalDate.of(1980, 3, 1).atStartOfDay());
// 30th of February, 24:60:60
testDate(data.clone(), 0 << 25 | 2 << 21 | 30 << 16 | 24 << 11 | 60 << 5 | 60 >> 1,
LocalDateTime.of(1980, 3, 2, 1, 1, 0));
} finally {
Files.delete(path);
}
}

private static void testDate(byte[] data, int date, LocalDateTime expected) throws IOException {
Expand All @@ -86,7 +92,7 @@ private static void testDate(byte[] data, int date, LocalDateTime expected) thro
writeU32(data, locpos + LOCTIM, date);

// ensure that the archive is still readable, and the date is 1979-11-30
Path path = Files.createTempFile("out", ".zip");
Path path = Utils.createTempFile("out", ".zip");
try (OutputStream os = Files.newOutputStream(path)) {
os.write(data);
}
Expand Down
6 changes: 3 additions & 3 deletions test/jdk/jdk/jfr/api/consumer/filestream/TestOrdered.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2019, 2024, 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 All @@ -23,7 +23,6 @@

package jdk.jfr.api.consumer.filestream;

import java.nio.file.Files;
import java.nio.file.Path;
import java.time.Instant;
import java.util.ArrayList;
Expand All @@ -35,6 +34,7 @@
import jdk.jfr.Event;
import jdk.jfr.Recording;
import jdk.jfr.consumer.EventStream;
import jdk.test.lib.Utils;

/**
* @test
Expand Down Expand Up @@ -148,7 +148,7 @@ private static Path makeUnorderedRecording() throws Exception {
e.join();
}
r.stop();
Path p = Files.createTempFile("recording", ".jfr");
Path p = Utils.createTempFile("recording", ".jfr");
r.dump(p);
return p;
}
Expand Down
5 changes: 3 additions & 2 deletions test/jdk/jdk/jfr/api/consumer/filestream/TestReuse.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2019, 2024, 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 @@ -34,6 +34,7 @@
import jdk.jfr.Recording;
import jdk.jfr.consumer.EventStream;
import jdk.jfr.consumer.RecordedEvent;
import jdk.test.lib.Utils;

/**
* @test
Expand Down Expand Up @@ -118,7 +119,7 @@ private static Path makeRecording() throws IOException {
}
r.stop();
rotation.close();
Path p = Files.createTempFile("recording", ".jfr");
Path p = Utils.createTempFile("recording", ".jfr");
r.dump(p);
return p;
}
Expand Down
Loading

3 comments on commit 375bfac

@openjdk-notifier
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@GoeLin
Copy link
Member

@GoeLin GoeLin commented on 375bfac Apr 19, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

/backport jdk21u-dev

@openjdk
Copy link

@openjdk openjdk bot commented on 375bfac Apr 19, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@GoeLin the backport was successfully created on the branch backport-GoeLin-375bfac8 in my personal fork of openjdk/jdk21u-dev. To create a pull request with this backport targeting openjdk/jdk21u-dev:master, just click the following link:

➡️ Create pull request

The title of the pull request is automatically filled in correctly and below you find a suggestion for the pull request body:

Hi all,

This pull request contains a backport of commit 375bfac8 from the openjdk/jdk repository.

The commit being backported was authored by Bill Huang on 3 Apr 2024 and was reviewed by Michael McMahon and Jaikiran Pai.

Thanks!

If you need to update the source branch of the pull then run the following commands in a local clone of your personal fork of openjdk/jdk21u-dev:

$ git fetch https://github.com/openjdk-bots/jdk21u-dev.git backport-GoeLin-375bfac8:backport-GoeLin-375bfac8
$ git checkout backport-GoeLin-375bfac8
# make changes
$ git add paths/to/changed/files
$ git commit --message 'Describe additional changes made'
$ git push https://github.com/openjdk-bots/jdk21u-dev.git backport-GoeLin-375bfac8

Please sign in to comment.