1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287
|
/*
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
*
* Copyright (c) 2009-2017 Oracle and/or its affiliates. All rights reserved.
*
* The contents of this file are subject to the terms of either the GNU
* General Public License Version 2 only ("GPL") or the Common Development
* and Distribution License("CDDL") (collectively, the "License"). You
* may not use this file except in compliance with the License. You can
* obtain a copy of the License at
* https://oss.oracle.com/licenses/CDDL+GPL-1.1
* or LICENSE.txt. See the License for the specific
* language governing permissions and limitations under the License.
*
* When distributing the software, include this License Header Notice in each
* file and include the License file at LICENSE.txt.
*
* GPL Classpath Exception:
* Oracle designates this particular file as subject to the "Classpath"
* exception as provided by Oracle in the GPL Version 2 section of the License
* file that accompanied this code.
*
* Modifications:
* If applicable, add the following below the License Header, with the fields
* enclosed by brackets [] replaced by your own identifying information:
* "Portions Copyright [year] [name of copyright owner]"
*
* Contributor(s):
* If you wish your version of this file to be governed by only the CDDL or
* only the GPL Version 2, indicate your decision by adding "[Contributor]
* elects to include this software in this distribution under the [CDDL or GPL
* Version 2] license." If you don't indicate a single choice of license, a
* recipient has the option to distribute your version of this file under
* either the CDDL, the GPL Version 2 or to extend the choice of license to
* its licensees as provided above. However, if you add GPL Version 2 code
* and therefore, elected the GPL Version 2 license, then the option applies
* only if the new code is made subject to such option by the copyright
* holder.
*/
package com.sun.mail.util;
import java.lang.reflect.*;
import java.io.IOException;
import java.io.InterruptedIOException;
import java.util.Properties;
import java.util.List;
import java.util.ArrayList;
import java.util.Set;
import java.util.HashSet;
import javax.mail.*;
import javax.mail.internet.MimeMessage;
import javax.mail.util.ByteArrayDataSource;
import javax.activation.DataHandler;
import javax.net.ssl.*;
import com.sun.mail.imap.IMAPHandler;
import com.sun.mail.test.TestServer;
import com.sun.mail.test.TestSocketFactory;
import com.sun.mail.test.TestSSLSocketFactory;
import org.junit.Ignore;
import org.junit.Test;
import org.junit.Rule;
import org.junit.rules.Timeout;
import static org.junit.Assert.fail;
import static org.junit.Assert.assertTrue;
/**
* Test that write timeouts work.
*/
public final class WriteTimeoutSocketTest {
// timeout the test in case of deadlock
@Rule
public Timeout deadlockTimeout = Timeout.seconds(20);
private static final int TIMEOUT = 200; // ms
private static final String data =
"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
/**
* Test write timeouts with plain sockets.
*/
@Test
@Ignore
public void test() {
final Properties properties = new Properties();
properties.setProperty("mail.imap.host", "localhost");
properties.setProperty("mail.imap.writetimeout", "" + TIMEOUT);
test(properties, false);
}
/**
* Test write timeouts with custom socket factory.
*/
@Test
@Ignore
public void testSocketFactory() {
final Properties properties = new Properties();
properties.setProperty("mail.imap.host", "localhost");
properties.setProperty("mail.imap.writetimeout", "" + TIMEOUT);
TestSocketFactory sf = new TestSocketFactory();
properties.put("mail.imap.socketFactory", sf);
properties.setProperty("mail.imap.socketFactory.fallback", "false");
test(properties, false);
// make sure our socket factory was actually used
assertTrue(sf.getSocketCreated());
}
/**
* Test write timeouts with SSL sockets.
*/
@Test
@Ignore
public void testSSL() {
final Properties properties = new Properties();
properties.setProperty("mail.imap.host", "localhost");
properties.setProperty("mail.imap.writetimeout", "" + TIMEOUT);
properties.setProperty("mail.imap.ssl.enable", "true");
// enable only the anonymous cipher suites since there's no
// server certificate
properties.setProperty("mail.imap.ssl.ciphersuites",
getAnonCipherSuites());
test(properties, true);
}
/**
* Test write timeouts with a custom SSL socket factory.
*/
@Test
@Ignore
public void testSSLSocketFactory() throws Exception {
final Properties properties = new Properties();
properties.setProperty("mail.imap.host", "localhost");
properties.setProperty("mail.imap.writetimeout", "" + TIMEOUT);
properties.setProperty("mail.imap.ssl.enable", "true");
TestSSLSocketFactory sf = new TestSSLSocketFactory();
sf.setDefaultCipherSuites(getAnonCipherSuitesArray());
properties.put("mail.imap.ssl.socketFactory", sf);
// don't fall back to non-SSL
properties.setProperty("mail.imap.socketFactory.fallback", "false");
// enable only the anonymous cipher suites since there's no
// server certificate
properties.setProperty("mail.imap.ssl.ciphersuites",
getAnonCipherSuites());
test(properties, true);
// make sure our socket factory was actually used
assertTrue(sf.getSocketWrapped() || sf.getSocketCreated());
}
/**
* Test that WriteTimeoutSocket overrides all methods from Socket.
* XXX - this is kind of hacky since it depends on Method.toString
*/
@Test
public void testOverrides() throws Exception {
Set<String> socketMethods = new HashSet<>();
Method[] m = java.net.Socket.class.getDeclaredMethods();
String className = java.net.Socket.class.getName() + ".";
for (int i = 0; i < m.length; i++) {
if (Modifier.isPublic(m[i].getModifiers()) &&
!Modifier.isStatic(m[i].getModifiers())) {
String name = m[i].toString().
replace("synchronized ", "").
replace(className, "");
socketMethods.add(name);
}
}
Set<String> wtsocketMethods = new HashSet<>();
m = WriteTimeoutSocket.class.getDeclaredMethods();
className = WriteTimeoutSocket.class.getName() + ".";
for (int i = 0; i < m.length; i++) {
if (Modifier.isPublic(m[i].getModifiers())) {
String name = m[i].toString().
replace("synchronized ", "").
replace(className, "");
socketMethods.remove(name);
}
}
for (String s : socketMethods)
System.out.println("WriteTimeoutSocket did not override: " + s);
assertTrue(socketMethods.isEmpty());
}
private static String[] getAnonCipherSuitesArray() {
SSLSocketFactory sf = (SSLSocketFactory)SSLSocketFactory.getDefault();
List<String> anon = new ArrayList<>();
String[] suites = sf.getSupportedCipherSuites();
for (int i = 0; i < suites.length; i++) {
if (suites[i].indexOf("_anon_") >= 0) {
anon.add(suites[i]);
}
}
return anon.toArray(new String[anon.size()]);
}
private static String getAnonCipherSuites() {
SSLSocketFactory sf = (SSLSocketFactory)SSLSocketFactory.getDefault();
StringBuilder anon = new StringBuilder();
String[] suites = sf.getSupportedCipherSuites();
for (int i = 0; i < suites.length; i++) {
if (suites[i].indexOf("_anon_") >= 0) {
if (anon.length() > 0)
anon.append(" ");
anon.append(suites[i]);
}
}
return anon.toString();
}
private void test(Properties properties, boolean isSSL) {
TestServer server = null;
try {
final TimeoutHandler handler = new TimeoutHandler();
server = new TestServer(handler, isSSL);
server.start();
properties.setProperty("mail.imap.port", "" + server.getPort());
final Session session = Session.getInstance(properties);
//session.setDebug(true);
MimeMessage msg = new MimeMessage(session);
msg.setFrom("test@example.com");
msg.setSubject("test");
final int size = 8192000; // enough data to fill network buffers
byte[] part = new byte[size];
for (int i = 0; i < size; i++) {
int j = i % 64;
if (j == 62)
part[i] = (byte)'\r';
else if (j == 63)
part[i] = (byte)'\n';
else
part[i] = (byte)data.charAt((j + i / 64) % 62);
}
msg.setDataHandler(new DataHandler(
new ByteArrayDataSource(part, "text/plain")));
msg.saveChanges();
final Store store = session.getStore("imap");
try {
store.connect("test", "test");
final Folder f = store.getFolder("test");
f.appendMessages(new Message[] { msg });
fail("No timeout");
} catch (StoreClosedException scex) {
// success!
} catch (Exception ex) {
System.out.println(ex);
//ex.printStackTrace();
fail(ex.toString());
} finally {
store.close();
}
} catch (final Exception e) {
//e.printStackTrace();
fail(e.getMessage());
} finally {
if (server != null) {
server.quit();
}
}
}
/**
* Custom handler.
*/
private static final class TimeoutHandler extends IMAPHandler {
@Override
protected void collectMessage(int bytes) throws IOException {
try {
// allow plenty of time for even slow machines to time out
Thread.sleep(TIMEOUT*20);
} catch (InterruptedException ex) { }
super.collectMessage(bytes);
}
@Override
public void list(String line) throws IOException {
untagged("LIST () \"/\" test");
ok();
}
}
}
|