geronimo-scm mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From rickmcgu...@apache.org
Subject svn commit: r398574 - in /geronimo/specs/trunk/geronimo-spec-javamail/src: main/java/org/apache/geronimo/mail/util/ test/java/javax/mail/internet/
Date Mon, 01 May 2006 11:40:32 GMT
Author: rickmcguire
Date: Mon May  1 04:40:28 2006
New Revision: 398574

URL: http://svn.apache.org/viewcvs?rev=398574&view=rev
Log:
GERONIMO-1953 Multiple fixes for uuencode support:

The MimeUtiltiy encode()/decode() method has multiple round trip problems when a uuencode
encoding is specified. Among these problems:

1) not properly recognizing the begin marker
2) incorrect end of data marker is written out
3) incorrect encoded length tests.
4) not properly writing out the second and third bytes of a decoded triplet.
5) a backwards System.arraycopy() munges decoded data. 



Added:
    geronimo/specs/trunk/geronimo-spec-javamail/src/test/java/javax/mail/internet/MimeUtilityTest.java
Modified:
    geronimo/specs/trunk/geronimo-spec-javamail/src/main/java/org/apache/geronimo/mail/util/UUDecoderStream.java
    geronimo/specs/trunk/geronimo-spec-javamail/src/main/java/org/apache/geronimo/mail/util/UUEncoder.java
    geronimo/specs/trunk/geronimo-spec-javamail/src/main/java/org/apache/geronimo/mail/util/UUEncoderStream.java

Modified: geronimo/specs/trunk/geronimo-spec-javamail/src/main/java/org/apache/geronimo/mail/util/UUDecoderStream.java
URL: http://svn.apache.org/viewcvs/geronimo/specs/trunk/geronimo-spec-javamail/src/main/java/org/apache/geronimo/mail/util/UUDecoderStream.java?rev=398574&r1=398573&r2=398574&view=diff
==============================================================================
--- geronimo/specs/trunk/geronimo-spec-javamail/src/main/java/org/apache/geronimo/mail/util/UUDecoderStream.java
(original)
+++ geronimo/specs/trunk/geronimo-spec-javamail/src/main/java/org/apache/geronimo/mail/util/UUDecoderStream.java
Mon May  1 04:40:28 2006
@@ -115,7 +115,7 @@
 
             // now copy some of the data from the decoded buffer to the target buffer
             int copyCount = Math.min(decodedCount, length);
-            System.arraycopy(data, offset, decodedChars, decodedIndex, copyCount);
+            System.arraycopy(decodedChars, decodedIndex, data, offset, copyCount);
             decodedIndex += copyCount;
             decodedCount -= copyCount;
             offset += copyCount;
@@ -125,6 +125,12 @@
         return readCharacters;
     }
 
+    /**
+     * Verify that the first line of the buffer is a valid begin
+     * marker.
+     *
+     * @exception IOException
+     */
     private void checkBegin() throws IOException {
         // we only do this the first time we're requested to read from the stream.
         if (beginRead) {
@@ -149,6 +155,13 @@
     }
 
 
+    /**
+     * Read a line of data.  Returns null if there is an EOF.
+     *
+     * @return The next line read from the stream.  Returns null if we
+     *         hit the end of the stream.
+     * @exception IOException
+     */
     protected String readLine() throws IOException {
         decodedIndex = 0;
         // get an accumulator for the data
@@ -168,7 +181,7 @@
             }
             else {
                 // add this to our buffer
-                buffer.append((byte)ch);
+                buffer.append((char)ch);
             }
             ch = in.read();
         }

Modified: geronimo/specs/trunk/geronimo-spec-javamail/src/main/java/org/apache/geronimo/mail/util/UUEncoder.java
URL: http://svn.apache.org/viewcvs/geronimo/specs/trunk/geronimo-spec-javamail/src/main/java/org/apache/geronimo/mail/util/UUEncoder.java?rev=398574&r1=398573&r2=398574&view=diff
==============================================================================
--- geronimo/specs/trunk/geronimo-spec-javamail/src/main/java/org/apache/geronimo/mail/util/UUEncoder.java
(original)
+++ geronimo/specs/trunk/geronimo-spec-javamail/src/main/java/org/apache/geronimo/mail/util/UUEncoder.java
Mon May  1 04:40:28 2006
@@ -76,9 +76,8 @@
      * @exception IOException
      */
     private int encodeLine(byte[] data, int offset, int length, OutputStream out) throws
IOException {
-        // write out the length of the data in this line, including the length byte.  This
-        // is uuencoded by basing it off of a ' ' character.
-        out.write((byte)(((length + 1) & 0x3F) + ' '));
+        // write out the number of characters encoded in this line.
+        out.write((byte)((length & 0x3F) + ' '));
         byte a;
         byte b;
         byte c;
@@ -160,7 +159,7 @@
      *
      * @param data   The array of byte data to decode.
      * @param off    Starting offset within the array.
-     * @param length The length of data to encode.
+     * @param length The length of data to decode (length does NOT include the terminating
new line).
      * @param out    The output stream used to return the decoded data.
      *
      * @return the number of bytes produced.
@@ -177,8 +176,8 @@
         count = (count - ' ') & 0x3F;
 
         // get the rounded count of characters that should have been used to encode this.
 The + 1 is for the
-        // terminating lineend character.
-        int requiredLength = (((count * 8) + 6) / 6) + 1;
+        // length encoded at the beginning
+        int requiredLength = (((count * 8) + 5) / 6) + 1;
         if (length < requiredLength) {
             throw new IOException("UUEncoded data and length do not match");
         }
@@ -202,14 +201,14 @@
             if (bytesWritten < count) {
                 c = (byte)((data[off++] - ' ') & 0x3F);
                 byte second = (byte)(((b << 4) & 0xF0) | ((c >>> 2) &
0x0F));
-                out.write(first);
+                out.write(second);
                 bytesWritten++;
 
                 // need the third one?
                 if (bytesWritten < count) {
                     d = (byte)((data[off++] - ' ') & 0x3F);
                     byte third = (byte)(((c << 6) & 0xC0) | (d & 0x3F));
-                    out.write(first);
+                    out.write(third);
                     bytesWritten++;
                 }
             }

Modified: geronimo/specs/trunk/geronimo-spec-javamail/src/main/java/org/apache/geronimo/mail/util/UUEncoderStream.java
URL: http://svn.apache.org/viewcvs/geronimo/specs/trunk/geronimo-spec-javamail/src/main/java/org/apache/geronimo/mail/util/UUEncoderStream.java?rev=398574&r1=398573&r2=398574&view=diff
==============================================================================
--- geronimo/specs/trunk/geronimo-spec-javamail/src/main/java/org/apache/geronimo/mail/util/UUEncoderStream.java
(original)
+++ geronimo/specs/trunk/geronimo-spec-javamail/src/main/java/org/apache/geronimo/mail/util/UUEncoderStream.java
Mon May  1 04:40:28 2006
@@ -106,7 +106,7 @@
     private void writeEnd() throws IOException {
         PrintStream writer = new PrintStream(out);
         // write out the stream with a CRLF marker
-        writer.print("\nbegin\r\n");
+        writer.print("\nend\r\n");
         writer.flush();
     }
 

Added: geronimo/specs/trunk/geronimo-spec-javamail/src/test/java/javax/mail/internet/MimeUtilityTest.java
URL: http://svn.apache.org/viewcvs/geronimo/specs/trunk/geronimo-spec-javamail/src/test/java/javax/mail/internet/MimeUtilityTest.java?rev=398574&view=auto
==============================================================================
--- geronimo/specs/trunk/geronimo-spec-javamail/src/test/java/javax/mail/internet/MimeUtilityTest.java
(added)
+++ geronimo/specs/trunk/geronimo-spec-javamail/src/test/java/javax/mail/internet/MimeUtilityTest.java
Mon May  1 04:40:28 2006
@@ -0,0 +1,86 @@
+/**
+ *
+ * Copyright 2006 The Apache Software Foundation
+ *
+ *  Licensed under the Apache License, Version 2.0 (the "License");
+ *  you may not use this file except in compliance with the License.
+ *  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing, software
+ *  distributed under the License is distributed on an "AS IS" BASIS,
+ *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *  See the License for the specific language governing permissions and
+ *  limitations under the License.
+ */
+
+package javax.mail.internet;
+
+import java.io.ByteArrayInputStream;
+import java.io.ByteArrayOutputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.OutputStream;
+import java.util.Properties;
+
+import javax.activation.DataHandler;
+import javax.activation.DataSource;
+import javax.mail.Session;
+
+import junit.framework.TestCase;
+
+public class MimeUtilityTest extends TestCase {
+
+    public void testEncodeDecode() throws Exception {
+
+        byte [] data = new byte[256];
+        for (int i = 0; i < data.length; i++) {
+            data[i] = (byte)i;
+        }
+
+        // different lengths test boundary conditions
+        doEncodingTest(data, 256, "uuencode");
+        doEncodingTest(data, 255, "uuencode");
+        doEncodingTest(data, 254, "uuencode");
+
+        doEncodingTest(data, 256, "binary");
+        doEncodingTest(data, 256, "7bit");
+        doEncodingTest(data, 256, "8bit");
+        doEncodingTest(data, 256, "base64");
+        doEncodingTest(data, 255, "base64");
+        doEncodingTest(data, 254, "base64");
+
+        doEncodingTest(data, 256, "x-uuencode");
+        doEncodingTest(data, 256, "x-uue");
+        doEncodingTest(data, 256, "quoted-printable");
+        doEncodingTest(data, 255, "quoted-printable");
+        doEncodingTest(data, 254, "quoted-printable");
+    }
+
+
+    public void doEncodingTest(byte[] data, int length, String encoding) throws Exception
{
+        ByteArrayOutputStream out = new ByteArrayOutputStream();
+        OutputStream encoder = MimeUtility.encode(out, encoding);
+
+        encoder.write(data, 0, length);
+        encoder.flush();
+
+        byte[] encodedData = out.toByteArray();
+
+        ByteArrayInputStream in = new ByteArrayInputStream(encodedData);
+
+        InputStream decoder = MimeUtility.decode(in, encoding);
+
+        byte[] decodedData = new byte[length];
+
+        int count = decoder.read(decodedData);
+
+        assertEquals(length, count);
+
+        for (int i = 0; i < length; i++) {
+            assertEquals(data[i], decodedData[i]);
+        }
+    }
+
+}



Mime
View raw message