Skip to content

Commit db4f348

Browse files
authored
WriteTimeoutSocket::getFileDescriptor$ support for Conscrypt #505 (#507)
WriteTimeoutSocket::getFileDescriptor$ support for Conscrypt #505
1 parent de24775 commit db4f348

File tree

3 files changed

+55
-8
lines changed

3 files changed

+55
-8
lines changed

doc/release/CHANGES.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ E 456 MimeMessage.setFrom(null) fails instead of removing the header
2929
E 461 OAuth2 POP3 Support for Microsoft
3030
E 473 Several modules are not included in build when JDK11 is used
3131
E 493 javamail.providers file missing from provider jars after 1.6.5
32+
E 505 WriteTimeoutSocket::getFileDescriptor$ support for Conscrypt
3233

3334

3435
CHANGES IN THE 2.0.0 RELEASE

mail/src/main/java/com/sun/mail/util/WriteTimeoutSocket.java

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2013, 2019 Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2013, 2021 Oracle and/or its affiliates. All rights reserved.
33
*
44
* This program and the accompanying materials are made available under the
55
* terms of the Eclipse Public License v. 2.0, which is available at
@@ -322,12 +322,20 @@ public Set<SocketOption<?>> supportedOptions() {
322322
* @return the FileDescriptor object
323323
*/
324324
public FileDescriptor getFileDescriptor$() {
325-
try {
326-
Method m = Socket.class.getDeclaredMethod("getFileDescriptor$");
327-
return (FileDescriptor)m.invoke(socket);
328-
} catch (Exception ex) {
329-
return null;
330-
}
325+
//The loop handles issues with non-public classes between
326+
//java.net.Socket and the actual socket type held in this object.
327+
//Must inspect java.net.Socket to ensure compatiblity with old behavior.
328+
for (Class<?> k = socket.getClass(); k != Object.class; k = k.getSuperclass()) {
329+
try {
330+
Method m = k.getDeclaredMethod("getFileDescriptor$");
331+
if (FileDescriptor.class.isAssignableFrom(m.getReturnType())) {
332+
//Skip setAccessible so non-public methods fail to invoke.
333+
return (FileDescriptor) m.invoke(socket);
334+
}
335+
} catch (Exception ignore) {
336+
}
337+
}
338+
return null;
331339
}
332340
}
333341

mail/src/test/java/com/sun/mail/util/WriteTimeoutSocketTest.java

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2009, 2020 Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2009, 2021 Oracle and/or its affiliates. All rights reserved.
33
*
44
* This program and the accompanying materials are made available under the
55
* terms of the Eclipse Public License v. 2.0, which is available at
@@ -18,8 +18,10 @@
1818

1919
import java.lang.reflect.*;
2020

21+
import java.io.FileDescriptor;
2122
import java.io.IOException;
2223
import java.io.InterruptedIOException;
24+
import java.net.Socket;
2325
import java.util.Properties;
2426
import java.util.List;
2527
import java.util.ArrayList;
@@ -42,6 +44,7 @@
4244
import org.junit.rules.Timeout;
4345
import static org.junit.Assert.fail;
4446
import static org.junit.Assert.assertTrue;
47+
import static org.junit.Assert.assertNotNull;
4548

4649
/**
4750
* Test that write timeouts work.
@@ -205,6 +208,41 @@ else if (j == 63)
205208
}
206209
}
207210
}
211+
212+
@Test
213+
public void testFileDescriptor$() throws Exception {
214+
try (PublicFileSocket ps = new PublicFileSocket()) {
215+
assertNotNull(ps.getFileDescriptor$());
216+
}
217+
218+
testFileDescriptor$(new PublicFileSocket());
219+
testFileDescriptor$(new PublicFileSocket1of3());
220+
testFileDescriptor$(new PublicFileSocket2of3());
221+
testFileDescriptor$(new PublicFileSocket3of3());
222+
}
223+
224+
private void testFileDescriptor$(Socket s) throws Exception {
225+
try (WriteTimeoutSocket ws = new WriteTimeoutSocket(s, 1000)) {
226+
assertNotNull(ws.getFileDescriptor$());
227+
} finally {
228+
s.close();
229+
}
230+
}
231+
232+
private static class PublicFileSocket extends Socket {
233+
public FileDescriptor getFileDescriptor$() {
234+
return new FileDescriptor();
235+
}
236+
}
237+
238+
private static class PublicFileSocket1of3 extends PublicFileSocket {
239+
}
240+
241+
private static class PublicFileSocket2of3 extends PublicFileSocket1of3 {
242+
}
243+
244+
private static class PublicFileSocket3of3 extends PublicFileSocket2of3 {
245+
}
208246

209247
/**
210248
* Custom handler.

0 commit comments

Comments
 (0)