commits@javamail.java.net

[javamail~mercurial:763] Increase message size to ensure it doesn't fit in OS networking buffers.

From: <shannon_at_java.net>
Date: Tue, 15 Sep 2015 22:01:17 +0000

Project: javamail
Repository: mercurial
Revision: 763
Author: shannon
Date: 2015-09-15 22:00:38 UTC
Link:

Log Message:
------------
capability() command doesn't properly transform errors - bug 6973
Improve IdleManager log/debug messages to include fully qualified folder name.
Move comment to the right place.
Fix FindBugs error in MailDateFormat
Increase message size to ensure it doesn't fit in OS networking buffers.


Revisions:
----------
759
760
761
762
763


Modified Paths:
---------------
doc/release/CHANGES.txt
mail/src/main/java/com/sun/mail/imap/protocol/IMAPProtocol.java
mail/src/test/java/com/sun/mail/imap/IMAPConnectFailureTest.java
mail/src/test/java/com/sun/mail/imap/IMAPHandler.java
mail/src/main/java/com/sun/mail/imap/IdleManager.java
pom.xml
mail/src/main/java/javax/mail/internet/MailDateFormat.java
mail/src/test/java/com/sun/mail/smtp/SMTPWriteTimeoutTest.java
mail/src/test/java/com/sun/mail/util/WriteTimeoutSocketTest.java


Diffs:
------
diff -r 8bec0af01fb5 -r 8524c5293bb2 doc/release/CHANGES.txt
--- a/doc/release/CHANGES.txt Wed Sep 02 16:34:00 2015 -0700
+++ b/doc/release/CHANGES.txt Fri Sep 04 15:27:18 2015 -0700
@@ -26,6 +26,7 @@
 K 6965 setting mail.<protocol>.auth.mechanisms should override
         mail.<protocol>.auth.<mechanism>.disable
 K 6966 add support for OAuth 2.0 without SASL
+K 6973 capability() command doesn't properly transform errors
 
 
                   CHANGES IN THE 1.5.4 RELEASE

diff -r 8bec0af01fb5 -r 8524c5293bb2 mail/src/main/java/com/sun/mail/imap/protocol/IMAPProtocol.java
--- a/mail/src/main/java/com/sun/mail/imap/protocol/IMAPProtocol.java Wed Sep 02 16:34:00 2015 -0700
+++ b/mail/src/main/java/com/sun/mail/imap/protocol/IMAPProtocol.java Fri Sep 04 15:27:18 2015 -0700
@@ -206,25 +206,26 @@
     public void capability() throws ProtocolException {
         // Check CAPABILITY
         Response[] r = command("CAPABILITY", null);
-
- if (!r[r.length-1].isOK())
- throw new ProtocolException(r[r.length-1].toString());
-
- capabilities = new HashMap(10);
- authmechs = new ArrayList(5);
- for (int i = 0, len = r.length; i < len; i++) {
- if (!(r[i] instanceof IMAPResponse))
- continue;
-
- IMAPResponse ir = (IMAPResponse)r[i];
-
- // Handle *all* untagged CAPABILITY responses.
- // Though the spec seemingly states that only
- // one CAPABILITY response string is allowed (6.1.1),
- // some server vendors claim otherwise.
- if (ir.keyEquals("CAPABILITY"))
- parseCapabilities(ir);
+ Response response = r[r.length-1];
+
+ if (response.isOK()) {
+ capabilities = new HashMap(10);
+ authmechs = new ArrayList(5);
+ for (int i = 0, len = r.length; i < len; i++) {
+ if (!(r[i] instanceof IMAPResponse))
+ continue;
+
+ IMAPResponse ir = (IMAPResponse)r[i];
+
+ // Handle *all* untagged CAPABILITY responses.
+ // Though the spec seemingly states that only
+ // one CAPABILITY response string is allowed (6.1.1),
+ // some server vendors claim otherwise.
+ if (ir.keyEquals("CAPABILITY"))
+ parseCapabilities(ir);
+ }
         }
+ handleResult(response);
     }
 
     /**

diff -r 8bec0af01fb5 -r 8524c5293bb2 mail/src/test/java/com/sun/mail/imap/IMAPConnectFailureTest.java
--- a/mail/src/test/java/com/sun/mail/imap/IMAPConnectFailureTest.java Wed Sep 02 16:34:00 2015 -0700
+++ b/mail/src/test/java/com/sun/mail/imap/IMAPConnectFailureTest.java Fri Sep 04 15:27:18 2015 -0700
@@ -1,7 +1,7 @@
 /*
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
  *
- * Copyright (c) 2009-2014 Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2009-2015 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
@@ -46,11 +46,16 @@
 
 import javax.mail.Session;
 import javax.mail.Store;
+import javax.mail.MessagingException;
 
+import com.sun.mail.iap.ConnectionException;
+
+import com.sun.mail.test.TestServer;
 import com.sun.mail.util.MailConnectException;
 
 import org.junit.Test;
 import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertTrue;
 import static org.junit.Assert.fail;
 
 /**
@@ -100,4 +105,58 @@
             fail(e.getMessage());
         }
     }
+
+ /**
+ * Test that a disconnect after issuing the CAPABILITY command
+ * results in a ConnectionException.
+ */
+ @Test
+ public void testCapabilityDisconnect() {
+ TestServer server = null;
+ try {
+ final IMAPHandler handler = new IMAPHandler() {
+ @Override
+ public void sendGreetings() throws IOException {
+ untagged("OK IMAPHandler");
+ }
+
+ @Override
+ public void capability() throws IOException {
+ exit();
+ }
+ };
+ server = new TestServer(handler);
+ server.start();
+
+ Properties properties = new Properties();
+ properties.setProperty("mail.imap.host", "localhost");
+ properties.setProperty("mail.imap.port", "" + server.getPort());
+ final Session session = Session.getInstance(properties);
+ //session.setDebug(true);
+
+ final Store store = session.getStore("imap");
+ try {
+ store.connect("test", "test");
+ fail("connect did not fail");
+ } catch (MessagingException mex) {
+ // this is what we expect, now check that it was caused by
+ // the right exception
+ assertTrue(mex.getCause() instanceof ConnectionException);
+ } 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();
+ }
+ }
+ }
 }

diff -r 8bec0af01fb5 -r 8524c5293bb2 mail/src/test/java/com/sun/mail/imap/IMAPHandler.java
--- a/mail/src/test/java/com/sun/mail/imap/IMAPHandler.java Wed Sep 02 16:34:00 2015 -0700
+++ b/mail/src/test/java/com/sun/mail/imap/IMAPHandler.java Fri Sep 04 15:27:18 2015 -0700
@@ -218,6 +218,8 @@
             login();
         } else if (commandName.equals("AUTHENTICATE")) {
             authenticate(currentLine);
+ } else if (commandName.equals("CAPABILITY")) {
+ capability();
         } else if (commandName.equals("NOOP")) {
             noop();
         } else if (commandName.equals("SELECT")) {
@@ -279,6 +281,16 @@
     }
 
     /**
+ * CAPABILITY command.
+ *
+ * @throws IOException unable to read/write to socket
+ */
+ public void capability() throws IOException {
+ untagged("CAPABILITY " + capabilities);
+ ok();
+ }
+
+ /**
      * SELECT command.
      *
      * @throws IOException unable to read/write to socket
@@ -473,7 +485,7 @@
                 break;
             }
             char c = s.charAt(i);
- if (c < '_' || c == '\177') {
+ if (c < ' ' || c == '\177') {
                 if (c == '\r')
                     sb.append("\\r");
                 else if (c == '\n')


diff -r 8524c5293bb2 -r d647192f2ef3 mail/src/main/java/com/sun/mail/imap/IdleManager.java
--- a/mail/src/main/java/com/sun/mail/imap/IdleManager.java Fri Sep 04 15:27:18 2015 -0700
+++ b/mail/src/main/java/com/sun/mail/imap/IdleManager.java Mon Sep 14 11:23:25 2015 -0700
@@ -184,7 +184,9 @@
         SocketChannel sc = ifolder.getChannel();
         if (sc == null)
             throw new MessagingException("Folder is not using SocketChannels");
- logger.log(Level.FINEST, "IdleManager watching {0}", ifolder);
+ if (logger.isLoggable(Level.FINEST))
+ logger.log(Level.FINEST, "IdleManager watching {0}",
+ folderName(ifolder));
         // keep trying to start the IDLE command until we're successful.
         // may block if we're in the middle of aborting an IDLE command.
         while (!ifolder.startIdle(this))
@@ -268,8 +270,9 @@
          */
         IMAPFolder folder;
         while ((folder = toWatch.poll()) != null) {
- logger.log(Level.FINEST,
- "IdleManager adding {0} to selector", folder);
+ if (logger.isLoggable(Level.FINEST))
+ logger.log(Level.FINEST,
+ "IdleManager adding {0} to selector", folderName(folder));
             try {
                 SocketChannel sc = folder.getChannel();
                 if (sc == null)
@@ -313,29 +316,34 @@
             // have to cancel so we can switch back to blocking I/O mode
             sk.cancel();
             folder = (IMAPFolder)sk.attachment();
- logger.log(Level.FINE,
- "IdleManager selected folder: {0}", folder);
+ if (logger.isLoggable(Level.FINEST))
+ logger.log(Level.FINE,
+ "IdleManager selected folder: {0}", folderName(folder));
             SelectableChannel sc = sk.channel();
             // switch back to blocking to allow normal I/O
             sc.configureBlocking(true);
             try {
                 if (folder.handleIdle(false)) {
- logger.log(Level.FINE,
- "IdleManager continue watching folder {0}", folder);
+ if (logger.isLoggable(Level.FINEST))
+ logger.log(Level.FINE,
+ "IdleManager continue watching folder {0}",
+ folderName(folder));
                     // more to do with this folder, select on it again
                     // XXX - what if we also added it above?
                     toWatch.add(folder);
                     more = true;
                 } else {
                     // done watching this folder,
- logger.log(Level.FINE,
- "IdleManager done watching folder {0}", folder);
+ if (logger.isLoggable(Level.FINEST))
+ logger.log(Level.FINE,
+ "IdleManager done watching folder {0}",
+ folderName(folder));
                 }
             } catch (MessagingException ex) {
                 // something went wrong, stop watching this folder
                 logger.log(Level.FINE,
- "IdleManager got exception for folder: " + folder,
- ex);
+ "IdleManager got exception for folder: " +
+ folderName(folder), ex);
             }
         }
 
@@ -343,8 +351,10 @@
          * Now, process any folders that we need to abort.
          */
         while ((folder = toAbort.poll()) != null) {
- logger.log(Level.FINE,
- "IdleManager aborting IDLE for folder: {0}", folder);
+ if (logger.isLoggable(Level.FINEST))
+ logger.log(Level.FINE,
+ "IdleManager aborting IDLE for folder: {0}",
+ folderName(folder));
             SocketChannel sc = folder.getChannel();
             if (sc == null)
                 continue;
@@ -392,8 +402,10 @@
             // have to cancel so we can switch back to blocking I/O mode
             sk.cancel();
             folder = (IMAPFolder)sk.attachment();
- logger.log(Level.FINE,
- "IdleManager no longer watching folder: {0}", folder);
+ if (logger.isLoggable(Level.FINEST))
+ logger.log(Level.FINE,
+ "IdleManager no longer watching folder: {0}",
+ folderName(folder));
             SelectableChannel sc = sk.channel();
             // switch back to blocking to allow normal I/O
             try {
@@ -403,7 +415,7 @@
                 // ignore it, channel might be closed
                 logger.log(Level.FINE,
                     "IdleManager exception while aborting idle for folder: " +
- folder, ex);
+ folderName(folder), ex);
             }
         }
 
@@ -411,8 +423,10 @@
          * Finally, process any folders waiting to be watched.
          */
         while ((folder = toWatch.poll()) != null) {
- logger.log(Level.FINE,
- "IdleManager aborting IDLE for unwatched folder: {0}", folder);
+ if (logger.isLoggable(Level.FINEST))
+ logger.log(Level.FINE,
+ "IdleManager aborting IDLE for unwatched folder: {0}",
+ folderName(folder));
             SocketChannel sc = folder.getChannel();
             if (sc == null)
                 continue;
@@ -424,7 +438,7 @@
                 // ignore it, channel might be closed
                 logger.log(Level.FINE,
                     "IdleManager exception while aborting idle for folder: " +
- folder, ex);
+ folderName(folder), ex);
             }
         }
     }
@@ -437,4 +451,18 @@
         logger.finest("IdleManager stopping");
         selector.wakeup();
     }
+
+ /**
+ * Return the fully qualified name of the folder, for use in log messages.
+ * Essentially just the getURLName method, but ignoring the
+ * MessagingException that can never happen.
+ */
+ private static String folderName(Folder folder) {
+ try {
+ return folder.getURLName().toString();
+ } catch (MessagingException mex) {
+ // can't happen
+ return folder.getStore().toString() + "/" + folder.toString();
+ }
+ }
 }


diff -r d647192f2ef3 -r dfda51251b7b pom.xml
--- a/pom.xml Mon Sep 14 11:23:25 2015 -0700
+++ b/pom.xml Mon Sep 14 11:24:35 2015 -0700
@@ -432,7 +432,6 @@
 
             <plugin>
                 <artifactId>maven-jar-plugin</artifactId>
- <!-- need at least this version to make excludes work -->
                 <configuration>
                     <finalName>${project.artifactId}</finalName>
                     <archive>
@@ -569,6 +568,7 @@
                 <plugin>
                     <groupId>org.apache.maven.plugins</groupId>
                     <artifactId>maven-jar-plugin</artifactId>
+ <!-- need at least this version to make excludes work -->
                     <version>2.4</version>
                 </plugin>
                 <plugin>


diff -r dfda51251b7b -r b8453a98ca0f mail/src/main/java/javax/mail/internet/MailDateFormat.java
--- a/mail/src/main/java/javax/mail/internet/MailDateFormat.java Mon Sep 14 11:24:35 2015 -0700
+++ b/mail/src/main/java/javax/mail/internet/MailDateFormat.java Mon Sep 14 18:42:23 2015 +0200
@@ -948,7 +948,7 @@
             int year = parseAsciiDigits(1, MAX_YEAR_DIGITS);
             if (year >= 1000) {
                 return year;
- } else if (year >= 50 && year <= 999) {
+ } else if (year >= 50) {
                 return year + 1900;
             } else {
                 return year + 2000;


diff -r b8453a98ca0f -r ce73a959d6e4 mail/src/test/java/com/sun/mail/smtp/SMTPWriteTimeoutTest.java
--- a/mail/src/test/java/com/sun/mail/smtp/SMTPWriteTimeoutTest.java Mon Sep 14 18:42:23 2015 +0200
+++ b/mail/src/test/java/com/sun/mail/smtp/SMTPWriteTimeoutTest.java Tue Sep 15 15:00:38 2015 -0700
@@ -99,7 +99,7 @@
                 MimeMessage msg = new MimeMessage(session);
                 msg.setRecipients(Message.RecipientType.TO, "joe_at_example.com");
                 msg.setSubject("test");
- byte[] bytes = new byte[1024*1024];
+ byte[] bytes = new byte[16*1024*1024];
                 msg.setDataHandler(
                     new DataHandler(new ByteArrayDataSource(bytes,
                                     "application/octet-stream")));

diff -r b8453a98ca0f -r ce73a959d6e4 mail/src/test/java/com/sun/mail/util/WriteTimeoutSocketTest.java
--- a/mail/src/test/java/com/sun/mail/util/WriteTimeoutSocketTest.java Mon Sep 14 18:42:23 2015 +0200
+++ b/mail/src/test/java/com/sun/mail/util/WriteTimeoutSocketTest.java Tue Sep 15 15:00:38 2015 -0700
@@ -182,7 +182,7 @@
             MimeMessage msg = new MimeMessage(session);
             msg.setFrom("test_at_example.com");
             msg.setSubject("test");
- final int size = 819200; // enough data to fill network buffers
+ 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;