Skip to content

Commit

Permalink
8257966: Instrument test/jdk/java/nio/channels/spi/SelectorProvider/i…
Browse files Browse the repository at this point in the history
…nheritedChannel/StateTestService.java

Reviewed-by: alanb, dfuchs
  • Loading branch information
Michael-Mc-Mahon committed Dec 10, 2020
1 parent 37043b0 commit b35401d
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 45 deletions.
Expand Up @@ -59,7 +59,8 @@
public class InheritedChannelTest {

private static final String TEST_SRC = System.getProperty("test.src");
private static final String TEST_CLASSES = System.getProperty("test.class.path");
private static final String TEST_CLASSPATH = System.getProperty("test.class.path");
private static final String TEST_CLASSES = System.getProperty("test.classes");
private static final Path POLICY_PASS = Paths.get(TEST_SRC, "java.policy.pass");
private static final Path POLICY_FAIL = Paths.get(TEST_SRC, "java.policy.fail");

Expand All @@ -76,7 +77,7 @@ public Object[][] testCases() {
return new Object[][] {
{ "UnixDomainChannelTest", List.of(UnixDomainChannelTest.class.getName())},
{ "UnixSocketTest", List.of(UnixSocketTest.class.getName())},
{ "StateTest", List.of(StateTest.class.getName()) },
{ "StateTest", List.of(StateTest.class.getName(), "-Dtest.classes="+TEST_CLASSES)},
{ "EchoTest", List.of(EchoTest.class.getName()) },
{ "CloseTest", List.of(CloseTest.class.getName()) },

Expand All @@ -85,15 +86,16 @@ public Object[][] testCases() {
// These system properties are passed to the launched service as options:
// java [-options] class [args...]


{ "StateTest run with " + POLICY_PASS, List.of(StateTest.class.getName(),
"-Djava.security.manager",
"-Dtest.classes=" + TEST_CLASSES,
"-Djava.security.policy="
+ POLICY_PASS)
},
{ "StateTest run with " + POLICY_FAIL, List.of(StateTest.class.getName(),
"-expectFail",
"-Djava.security.manager",
"-Dtest.classes=" + TEST_CLASSES,
"-Djava.security.policy="
+ POLICY_FAIL)
}
Expand All @@ -115,7 +117,7 @@ public void test(String desc, List<String> opts) throws Throwable {
ProcessBuilder pb = new ProcessBuilder(args);

Map<String, String> env = pb.environment();
env.put("CLASSPATH", TEST_CLASSES);
env.put("CLASSPATH", TEST_CLASSPATH);
env.put(pathVar, libraryPath.toString());

ProcessTools.executeCommand(pb).shouldHaveExitValue(0);
Expand Down
Expand Up @@ -38,26 +38,56 @@
* message to indicate the test result.
*/
import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintStream;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.Channel;
import java.nio.channels.DatagramChannel;
import java.nio.channels.ServerSocketChannel;
import java.nio.channels.SocketChannel;
import java.nio.file.Files;
import java.nio.file.Path;

import static java.nio.file.StandardOpenOption.APPEND;
import static java.nio.file.StandardOpenOption.CREATE;

public class StateTestService {

static boolean failed = false;
static int reply_port;

static void check(boolean okay) {
println("check " + okay);
if (!okay) {
failed = true;
}
}

static String logDir;
static PrintStream out;
static boolean initialized = false;

// Opens named log file in ${test.classes}
static void initLogFile() {
if (initialized)
return;

try {
OutputStream f = Files.newOutputStream(Path.of(logDir, "statetest.txt"), APPEND, CREATE);
out = new PrintStream(f);
} catch (Exception e) {}
initialized = true;
}

static void println(String msg) {
initLogFile();
out.println(msg);
}

private static void reply(String msg) throws IOException {
println("REPLYING: " + msg);
InetSocketAddress isa = new InetSocketAddress(InetAddress.getLocalHost(), reply_port);
SocketChannel sc = SocketChannel.open(isa);
byte b[] = msg.getBytes("UTF-8");
Expand All @@ -67,48 +97,53 @@ private static void reply(String msg) throws IOException {
}

public static void main(String args[]) throws IOException {
if (args.length == 0) {
System.err.println("Usage: StateTestService [reply-port]");
return;
}
reply_port = Integer.parseInt(args[0]);

Channel c = null;
try {
c = System.inheritedChannel();
} catch (SecurityException se) {
// ignore
}
if (c == null) {
reply("FAILED");
return;
}
if (args.length == 0) {
System.err.println("Usage: StateTestService [reply-port]");
return;
}
reply_port = Integer.parseInt(args[0]);
logDir = System.getProperty("test.classes");

if (c instanceof SocketChannel) {
SocketChannel sc = (SocketChannel)c;
check( sc.isBlocking() );
check( sc.socket().isBound() );
check( sc.socket().isConnected() );
}
Channel c = null;
try {
c = System.inheritedChannel();
} catch (SecurityException se) {
// ignore
}
if (c == null) {
println("c == null");
reply("FAILED");
return;
}

if (c instanceof ServerSocketChannel) {
ServerSocketChannel ssc = (ServerSocketChannel)c;
check( ssc.isBlocking() );
check( ssc.socket().isBound() );
}
if (c instanceof SocketChannel) {
SocketChannel sc = (SocketChannel)c;
check( sc.isBlocking() );
check( sc.socket().isBound() );
check( sc.socket().isConnected() );
}

if (c instanceof DatagramChannel) {
DatagramChannel dc = (DatagramChannel)c;
check( dc.isBlocking() );
check( dc.socket().isBound() );
}
if (c instanceof ServerSocketChannel) {
ServerSocketChannel ssc = (ServerSocketChannel)c;
check( ssc.isBlocking() );
check( ssc.socket().isBound() );
}

if (failed) {
reply("FAILED");
} else {
reply("PASSED");
}
if (c instanceof DatagramChannel) {
DatagramChannel dc = (DatagramChannel)c;
check( dc.isBlocking() );
check( dc.socket().isBound() );
}

if (failed) {
reply("FAILED");
} else {
reply("PASSED");
}
} catch (Throwable t) {
t.printStackTrace(out);
throw t;
}
}

}
@@ -1,11 +1,11 @@
//
//
//
// Used by unit tests for System.inheritedChannel() method. This policy
// file allows doesn't grant the service the runtime permission needed
// to obtain the inherited channel but does grant the socket permission
// needed to report a test result over a socket connection.
//
grant {
permission java.net.SocketPermission "*:1024-", "resolve,connect";
permission java.io.FilePermission "${test.classes}${/}statetest.txt", "read,write";
permission java.util.PropertyPermission "test.classes", "read";
};
@@ -1,11 +1,11 @@
//
//
//
// Used by unit tests for System.inheritedChannel() method. This policy
// file allows a service to obtain the inherited channel and additionally
// allows the service to report a test result over a socket connection.
//
grant {
permission java.lang.RuntimePermission "inheritedChannel";
permission java.net.SocketPermission "*:1024-", "resolve,connect";
permission java.io.FilePermission "${test.classes}${/}statetest.txt", "read,write";
permission java.util.PropertyPermission "test.classes", "read";
};

1 comment on commit b35401d

@openjdk-notifier
Copy link

Choose a reason for hiding this comment

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

Please sign in to comment.