Skip to content

Commit

Permalink
WriteTimeoutSocket::getFileDescriptor$ support for Conscrypt #505 (#507)
Browse files Browse the repository at this point in the history
WriteTimeoutSocket::getFileDescriptor$ support for Conscrypt #505
  • Loading branch information
jmehrens committed Jan 21, 2021
1 parent de24775 commit db4f348
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 8 deletions.
1 change: 1 addition & 0 deletions doc/release/CHANGES.txt
Expand Up @@ -29,6 +29,7 @@ E 456 MimeMessage.setFrom(null) fails instead of removing the header
E 461 OAuth2 POP3 Support for Microsoft
E 473 Several modules are not included in build when JDK11 is used
E 493 javamail.providers file missing from provider jars after 1.6.5
E 505 WriteTimeoutSocket::getFileDescriptor$ support for Conscrypt


CHANGES IN THE 2.0.0 RELEASE
Expand Down
22 changes: 15 additions & 7 deletions mail/src/main/java/com/sun/mail/util/WriteTimeoutSocket.java
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2013, 2019 Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2013, 2021 Oracle and/or its affiliates. All rights reserved.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0, which is available at
Expand Down Expand Up @@ -322,12 +322,20 @@ public Set<SocketOption<?>> supportedOptions() {
* @return the FileDescriptor object
*/
public FileDescriptor getFileDescriptor$() {
try {
Method m = Socket.class.getDeclaredMethod("getFileDescriptor$");
return (FileDescriptor)m.invoke(socket);
} catch (Exception ex) {
return null;
}
//The loop handles issues with non-public classes between
//java.net.Socket and the actual socket type held in this object.
//Must inspect java.net.Socket to ensure compatiblity with old behavior.
for (Class<?> k = socket.getClass(); k != Object.class; k = k.getSuperclass()) {
try {
Method m = k.getDeclaredMethod("getFileDescriptor$");
if (FileDescriptor.class.isAssignableFrom(m.getReturnType())) {
//Skip setAccessible so non-public methods fail to invoke.
return (FileDescriptor) m.invoke(socket);
}
} catch (Exception ignore) {
}
}
return null;
}
}

Expand Down
40 changes: 39 additions & 1 deletion mail/src/test/java/com/sun/mail/util/WriteTimeoutSocketTest.java
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2009, 2020 Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2009, 2021 Oracle and/or its affiliates. All rights reserved.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0, which is available at
Expand All @@ -18,8 +18,10 @@

import java.lang.reflect.*;

import java.io.FileDescriptor;
import java.io.IOException;
import java.io.InterruptedIOException;
import java.net.Socket;
import java.util.Properties;
import java.util.List;
import java.util.ArrayList;
Expand All @@ -42,6 +44,7 @@
import org.junit.rules.Timeout;
import static org.junit.Assert.fail;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.assertNotNull;

/**
* Test that write timeouts work.
Expand Down Expand Up @@ -205,6 +208,41 @@ else if (j == 63)
}
}
}

@Test
public void testFileDescriptor$() throws Exception {
try (PublicFileSocket ps = new PublicFileSocket()) {
assertNotNull(ps.getFileDescriptor$());
}

testFileDescriptor$(new PublicFileSocket());
testFileDescriptor$(new PublicFileSocket1of3());
testFileDescriptor$(new PublicFileSocket2of3());
testFileDescriptor$(new PublicFileSocket3of3());
}

private void testFileDescriptor$(Socket s) throws Exception {
try (WriteTimeoutSocket ws = new WriteTimeoutSocket(s, 1000)) {
assertNotNull(ws.getFileDescriptor$());
} finally {
s.close();
}
}

private static class PublicFileSocket extends Socket {
public FileDescriptor getFileDescriptor$() {
return new FileDescriptor();
}
}

private static class PublicFileSocket1of3 extends PublicFileSocket {
}

private static class PublicFileSocket2of3 extends PublicFileSocket1of3 {
}

private static class PublicFileSocket3of3 extends PublicFileSocket2of3 {
}

/**
* Custom handler.
Expand Down

0 comments on commit db4f348

Please sign in to comment.