harmony-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From smish...@apache.org
Subject svn commit: r595216 - in /harmony/enhanced/classlib/trunk/modules/security/src: main/java/common/org/apache/harmony/security/asn1/BerInputStream.java test/impl/java/org/apache/harmony/security/tests/asn1/der/BerInputStreamTest.java
Date Thu, 15 Nov 2007 05:46:46 GMT
Author: smishura
Date: Wed Nov 14 21:46:32 2007
New Revision: 595216

URL: http://svn.apache.org/viewvc?rev=595216&view=rev
Log:
Fix and regression test for BerInputStream.readContent() method:
if data can not be red at once try to read it in several blocks

see HARMONY-5054:
[classlib][security] ASN.1: BerInputStream will incorrectly resize buffer when the enveloped
InputStream has lots of bytes

Modified:
    harmony/enhanced/classlib/trunk/modules/security/src/main/java/common/org/apache/harmony/security/asn1/BerInputStream.java
    harmony/enhanced/classlib/trunk/modules/security/src/test/impl/java/org/apache/harmony/security/tests/asn1/der/BerInputStreamTest.java

Modified: harmony/enhanced/classlib/trunk/modules/security/src/main/java/common/org/apache/harmony/security/asn1/BerInputStream.java
URL: http://svn.apache.org/viewvc/harmony/enhanced/classlib/trunk/modules/security/src/main/java/common/org/apache/harmony/security/asn1/BerInputStream.java?rev=595216&r1=595215&r2=595216&view=diff
==============================================================================
--- harmony/enhanced/classlib/trunk/modules/security/src/main/java/common/org/apache/harmony/security/asn1/BerInputStream.java
(original)
+++ harmony/enhanced/classlib/trunk/modules/security/src/main/java/common/org/apache/harmony/security/asn1/BerInputStream.java
Wed Nov 14 21:46:32 2007
@@ -915,9 +915,22 @@
         if (in == null) {
             offset += length;
         } else {
-            if (in.read(buffer, offset, length) != length) {
-                throw new ASN1Exception(Messages.getString("security.13C")); //$NON-NLS-1$
+            int bytesRead = in.read(buffer, offset, length);
+
+            if (bytesRead != length) {
+                // if input stream didn't return all data at once
+                // try to read it in several blocks
+                int c = bytesRead;
+                do {
+                    if (c < 1 || bytesRead > length) {
+                        throw new ASN1Exception(Messages
+                                .getString("security.13C")); //$NON-NLS-1$
+                    }
+                    c = in.read(buffer, offset + bytesRead, length - bytesRead);
+                    bytesRead += c;
+                } while (bytesRead != length);
             }
+
             offset += length;
         }
     }

Modified: harmony/enhanced/classlib/trunk/modules/security/src/test/impl/java/org/apache/harmony/security/tests/asn1/der/BerInputStreamTest.java
URL: http://svn.apache.org/viewvc/harmony/enhanced/classlib/trunk/modules/security/src/test/impl/java/org/apache/harmony/security/tests/asn1/der/BerInputStreamTest.java?rev=595216&r1=595215&r2=595216&view=diff
==============================================================================
--- harmony/enhanced/classlib/trunk/modules/security/src/test/impl/java/org/apache/harmony/security/tests/asn1/der/BerInputStreamTest.java
(original)
+++ harmony/enhanced/classlib/trunk/modules/security/src/test/impl/java/org/apache/harmony/security/tests/asn1/der/BerInputStreamTest.java
Wed Nov 14 21:46:32 2007
@@ -25,6 +25,7 @@
 import java.io.ByteArrayInputStream;
 import java.io.IOException;
 import java.math.BigInteger;
+import java.util.Arrays;
 
 import junit.framework.TestCase;
 
@@ -166,6 +167,57 @@
             fail("No expected ASN1Exception");
         } catch (ASN1Exception e) {
             assertEquals("Wrong content length", e.getMessage());
+        }
+    }
+
+    /**
+     * @tests org.apache.harmony.security.asn1.BerInputStream#readContent()
+     */
+    public void test_readContent() throws IOException {
+
+        byte[] encoding = { ASN1Constants.TAG_OCTETSTRING, 0x0F, 0x01, 0x02,
+                0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C,
+                0x0D, 0x0E, 0x0F };
+
+        // a custom input stream that doesn't return all data at once
+        ByteArrayInputStream in = new ByteArrayInputStream(encoding) {
+            public int read(byte[] b, int off, int len) {
+                if (len < 2) {
+                    return super.read(b, off, len);
+                } else {
+                    return super.read(b, off, 4);
+                }
+
+            }
+        };
+
+        BerInputStream berIn = new BerInputStream(in);
+        berIn.readContent();
+
+        assertTrue(Arrays.equals(encoding, berIn.getEncoded()));
+
+        //
+        // negative test case: the stream returns only 4 bytes of content
+        //
+        in = new ByteArrayInputStream(encoding) {
+
+            int i = 0;
+
+            public int read(byte[] b, int off, int len) {
+                if (i == 0) {
+                    i++;
+                    return super.read(b, off, 4);
+                } else {
+                    return 0;
+                }
+
+            }
+        };
+        berIn = new BerInputStream(in);
+        try {
+            berIn.readContent();
+            fail("No expected ASN1Exception");
+        } catch (ASN1Exception e) {
         }
     }
 }



Mime
View raw message