Skip to content

Commit

Permalink
8202117: com/sun/jndi/ldap/RemoveNamingListenerTest.java fails interm…
Browse files Browse the repository at this point in the history
…ittently: Connection reset

Reviewed-by: dfuchs, vtewari
  • Loading branch information
Chris Yin committed Mar 19, 2020
1 parent c7c8623 commit 599af2a
Showing 1 changed file with 30 additions and 107 deletions.
137 changes: 30 additions & 107 deletions test/jdk/com/sun/jndi/ldap/RemoveNamingListenerTest.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2011, 2017, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2011, 2020, 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 @@ -20,13 +20,11 @@
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
import java.io.BufferedInputStream;

import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.io.OutputStream;
import java.net.Socket;
import java.nio.charset.StandardCharsets;
import java.net.URI;
import java.util.ConcurrentModificationException;
import java.util.Hashtable;
import javax.naming.Context;
Expand All @@ -37,13 +35,15 @@
import javax.naming.event.NamingExceptionEvent;
import javax.naming.event.NamingListener;
import javax.naming.event.ObjectChangeListener;
import jdk.test.lib.net.URIBuilder;

/**
* @test
* @bug 8176192
* @summary Incorrect usage of Iterator in Java 8 In com.sun.jndi.ldap.
* EventSupport.removeNamingListener
* @modules java.naming
* @library lib/ /test/lib
* @run main RemoveNamingListenerTest
*/
public class RemoveNamingListenerTest {
Expand All @@ -55,10 +55,17 @@ public static void main(String args[]) throws Exception {
TestLDAPServer server = new TestLDAPServer();
server.start();

URI providerURI = URIBuilder.newBuilder()
.scheme("ldap")
.loopback()
.port(server.getPort())
.path("/o=example")
.build();

// Set up environment for creating initial context
Hashtable<String, Object> env = new Hashtable<>(3);
env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
env.put(Context.PROVIDER_URL, "ldap://localhost:" + server.getPort() + "/o=example");
env.put(Context.PROVIDER_URL, providerURI.toString());
env.put("com.sun.jndi.ldap.connect.timeout", "2000");
EventContext ctx = null;

Expand Down Expand Up @@ -89,7 +96,7 @@ public static void main(String args[]) throws Exception {
if (ctx != null) {
ctx.close();
}
server.stopServer();
server.close();
}
}

Expand Down Expand Up @@ -130,112 +137,28 @@ public void namingExceptionThrown(NamingExceptionEvent nee) {
}
}

class TestLDAPServer extends Thread {

private final int LDAP_PORT;
private final ServerSocket serverSocket;
private volatile boolean isRunning;

TestLDAPServer() throws IOException {
serverSocket = new ServerSocket(0);
isRunning = true;
LDAP_PORT = serverSocket.getLocalPort();
setDaemon(true);
}

public int getPort() {
return LDAP_PORT;
}

public void stopServer() {
isRunning = false;
if (serverSocket != null && !serverSocket.isClosed()) {
try {
// this will cause ServerSocket.accept() to throw SocketException.
serverSocket.close();
} catch (IOException ignored) {
}
}
}

@Override
public void run() {
try {
while (isRunning) {
Socket clientSocket = serverSocket.accept();
Thread handler = new Thread(new LDAPServerHandler(clientSocket));
handler.setDaemon(true);
handler.start();
}
} catch (IOException iOException) {
//do not throw exception if server is not running.
if (isRunning) {
throw new RuntimeException(iOException);
}
} finally {
stopServer();
}
}
}

class LDAPServerHandler implements Runnable {
class TestLDAPServer extends BaseLdapServer {

private final Socket clientSocket;
private byte[] bindResponse = {0x30, 0x0C, 0x02, 0x01, 0x01, 0x61, 0x07, 0x0A, 0x01, 0x00, 0x04, 0x00, 0x04, 0x00};
private byte[] searchResponse = {0x30, 0x0C, 0x02, 0x01, 0x02, 0x65, 0x07, 0x0A, 0x01, 0x00, 0x04, 0x00, 0x04, 0x00};

public LDAPServerHandler(final Socket clientSocket) {
this.clientSocket = clientSocket;
public TestLDAPServer() throws IOException {
}

@Override
public void run() {
BufferedInputStream in = null;
PrintWriter out = null;
byte[] bindResponse = {0x30, 0x0C, 0x02, 0x01, 0x01, 0x61, 0x07, 0x0A, 0x01, 0x00, 0x04, 0x00, 0x04, 0x00};
byte[] searchResponse = {0x30, 0x0C, 0x02, 0x01, 0x02, 0x65, 0x07, 0x0A, 0x01, 0x00, 0x04, 0x00, 0x04, 0x00};
try {
in = new BufferedInputStream(clientSocket.getInputStream());
out = new PrintWriter(new OutputStreamWriter(
clientSocket.getOutputStream(), StandardCharsets.UTF_8), true);
while (true) {

// Read the LDAP BindRequest
while (in.read() != -1) {
in.skip(in.available());
break;
}

protected void handleRequest(Socket socket, LdapMessage request,
OutputStream out) throws IOException {
switch (request.getOperation()) {
case BIND_REQUEST:
// Write an LDAP BindResponse
out.write(new String(bindResponse));
out.flush();

// Read the LDAP SearchRequest
while (in.read() != -1) {
in.skip(in.available());
break;
}

out.write(bindResponse);
break;
case SEARCH_REQUEST:
// Write an LDAP SearchResponse
out.write(new String(searchResponse));
out.flush();
}
} catch (IOException iOException) {
throw new RuntimeException(iOException);
} finally {
if (in != null) {
try {
in.close();
} catch (IOException ignored) {
}
}
if (out != null) {
out.close();
}
if (clientSocket != null) {
try {
clientSocket.close();
} catch (IOException ignored) {
}
}
out.write(searchResponse);
break;
default:
break;
}
}
}

0 comments on commit 599af2a

Please sign in to comment.