You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@harmony.apache.org by sm...@apache.org on 2006/05/26 14:57:08 UTC
svn commit: r409664 - in /incubator/harmony/enhanced/classlib/trunk/modules:
auth/src/main/java/common/org/ietf/jgss/
security/src/main/java/common/org/apache/harmony/security/asn1/
security/src/test/java/common/org/apache/harmony/security/asn1/der/
Author: smishura
Date: Fri May 26 05:57:07 2006
New Revision: 409664
URL: http://svn.apache.org/viewvc?rev=409664&view=rev
Log:
Adding new stream contructor
Modified:
incubator/harmony/enhanced/classlib/trunk/modules/auth/src/main/java/common/org/ietf/jgss/Oid.java (contents, props changed)
incubator/harmony/enhanced/classlib/trunk/modules/security/src/main/java/common/org/apache/harmony/security/asn1/ASN1Type.java
incubator/harmony/enhanced/classlib/trunk/modules/security/src/main/java/common/org/apache/harmony/security/asn1/BerInputStream.java (contents, props changed)
incubator/harmony/enhanced/classlib/trunk/modules/security/src/main/java/common/org/apache/harmony/security/asn1/DerInputStream.java (contents, props changed)
incubator/harmony/enhanced/classlib/trunk/modules/security/src/test/java/common/org/apache/harmony/security/asn1/der/BerInputStreamTest.java (contents, props changed)
Modified: incubator/harmony/enhanced/classlib/trunk/modules/auth/src/main/java/common/org/ietf/jgss/Oid.java
URL: http://svn.apache.org/viewvc/incubator/harmony/enhanced/classlib/trunk/modules/auth/src/main/java/common/org/ietf/jgss/Oid.java?rev=409664&r1=409663&r2=409664&view=diff
==============================================================================
--- incubator/harmony/enhanced/classlib/trunk/modules/auth/src/main/java/common/org/ietf/jgss/Oid.java (original)
+++ incubator/harmony/enhanced/classlib/trunk/modules/auth/src/main/java/common/org/ietf/jgss/Oid.java Fri May 26 05:57:07 2006
@@ -24,7 +24,6 @@
import java.io.InputStream;
import org.apache.harmony.security.asn1.ASN1Oid;
-import org.apache.harmony.security.asn1.DerInputStream;
import org.apache.harmony.security.asn1.ObjectIdentifier;
@@ -52,13 +51,7 @@
*/
public Oid(byte[] data) throws GSSException {
try {
- DerInputStream in = new DerInputStream(data);
-
- if (in.getEndOffset() != data.length) {
- throw new GSSException(GSSException.FAILURE);
- }
-
- oid = new ObjectIdentifier((int[])ASN1.decode(in));
+ oid = new ObjectIdentifier((int[])ASN1.decode(data));
} catch (IOException e) {
GSSException gsse = new GSSException(GSSException.FAILURE);
gsse.initCause(e);
Propchange: incubator/harmony/enhanced/classlib/trunk/modules/auth/src/main/java/common/org/ietf/jgss/Oid.java
------------------------------------------------------------------------------
svn:eol-style = native
Modified: incubator/harmony/enhanced/classlib/trunk/modules/security/src/main/java/common/org/apache/harmony/security/asn1/ASN1Type.java
URL: http://svn.apache.org/viewvc/incubator/harmony/enhanced/classlib/trunk/modules/security/src/main/java/common/org/apache/harmony/security/asn1/ASN1Type.java?rev=409664&r1=409663&r2=409664&view=diff
==============================================================================
--- incubator/harmony/enhanced/classlib/trunk/modules/security/src/main/java/common/org/apache/harmony/security/asn1/ASN1Type.java (original)
+++ incubator/harmony/enhanced/classlib/trunk/modules/security/src/main/java/common/org/apache/harmony/security/asn1/ASN1Type.java Fri May 26 05:57:07 2006
@@ -137,6 +137,11 @@
return decode(new DerInputStream(encoded));
}
+ public final Object decode(byte[] encoded, int offset, int encodingLen)
+ throws IOException {
+ return decode(new DerInputStream(encoded, offset, encodingLen));
+ }
+
public final Object decode(InputStream in) throws IOException {
return decode(new DerInputStream(in));
}
Modified: incubator/harmony/enhanced/classlib/trunk/modules/security/src/main/java/common/org/apache/harmony/security/asn1/BerInputStream.java
URL: http://svn.apache.org/viewvc/incubator/harmony/enhanced/classlib/trunk/modules/security/src/main/java/common/org/apache/harmony/security/asn1/BerInputStream.java?rev=409664&r1=409663&r2=409664&view=diff
==============================================================================
--- incubator/harmony/enhanced/classlib/trunk/modules/security/src/main/java/common/org/apache/harmony/security/asn1/BerInputStream.java (original)
+++ incubator/harmony/enhanced/classlib/trunk/modules/security/src/main/java/common/org/apache/harmony/security/asn1/BerInputStream.java Fri May 26 05:57:07 2006
@@ -69,9 +69,35 @@
* @throws IOException - if an error occurs
*/
public BerInputStream(byte[] encoded) throws IOException {
- buffer = encoded;
+ this(encoded, 0, encoded.length);
+ }
+
+ /**
+ * Creates stream for decoding.
+ *
+ * @param encoded -
+ * bytes array to be decoded
+ * @param offset -
+ * the encoding offset
+ * @param expectedLength -
+ * expeced length of full encoding, this includes identifier,
+ * length an content octets
+ * @throws IOException -
+ * if an error occurs
+ */
+ public BerInputStream(byte[] encoded, int offset, int expectedLength)
+ throws IOException {
+
+ this.buffer = encoded;
+ this.offset = offset;
next();
+
+ // compare expected and decoded length
+ if (length != INDEFINIT_LENGTH
+ && (offset + expectedLength) != (this.offset + this.length)) {
+ throw new ASN1Exception("Wrong content length");
+ }
}
/**
@@ -785,11 +811,14 @@
if (in == null) {
return buffer[offset++] & 0xFF;
} else {
- buffer[offset] = (byte) in.read();
- if (buffer[offset] == -1) {
+ int octet = in.read();
+ if (octet == -1) {
throw new ASN1Exception("Unexpected end of encoding");
}
- return buffer[offset++] & 0xFF;
+
+ buffer[offset++] = (byte) octet;
+
+ return octet;
}
}
Propchange: incubator/harmony/enhanced/classlib/trunk/modules/security/src/main/java/common/org/apache/harmony/security/asn1/BerInputStream.java
------------------------------------------------------------------------------
svn:eol-style = native
Modified: incubator/harmony/enhanced/classlib/trunk/modules/security/src/main/java/common/org/apache/harmony/security/asn1/DerInputStream.java
URL: http://svn.apache.org/viewvc/incubator/harmony/enhanced/classlib/trunk/modules/security/src/main/java/common/org/apache/harmony/security/asn1/DerInputStream.java?rev=409664&r1=409663&r2=409664&view=diff
==============================================================================
--- incubator/harmony/enhanced/classlib/trunk/modules/security/src/main/java/common/org/apache/harmony/security/asn1/DerInputStream.java (original)
+++ incubator/harmony/enhanced/classlib/trunk/modules/security/src/main/java/common/org/apache/harmony/security/asn1/DerInputStream.java Fri May 26 05:57:07 2006
@@ -36,6 +36,11 @@
super(encoded);
}
+ public DerInputStream(byte[] encoded, int offset, int encodingLen)
+ throws IOException {
+ super(encoded, offset, encodingLen);
+ }
+
public DerInputStream(InputStream in) throws IOException {
super(in);
}
Propchange: incubator/harmony/enhanced/classlib/trunk/modules/security/src/main/java/common/org/apache/harmony/security/asn1/DerInputStream.java
------------------------------------------------------------------------------
svn:eol-style = native
Modified: incubator/harmony/enhanced/classlib/trunk/modules/security/src/test/java/common/org/apache/harmony/security/asn1/der/BerInputStreamTest.java
URL: http://svn.apache.org/viewvc/incubator/harmony/enhanced/classlib/trunk/modules/security/src/test/java/common/org/apache/harmony/security/asn1/der/BerInputStreamTest.java?rev=409664&r1=409663&r2=409664&view=diff
==============================================================================
--- incubator/harmony/enhanced/classlib/trunk/modules/security/src/test/java/common/org/apache/harmony/security/asn1/der/BerInputStreamTest.java (original)
+++ incubator/harmony/enhanced/classlib/trunk/modules/security/src/test/java/common/org/apache/harmony/security/asn1/der/BerInputStreamTest.java Fri May 26 05:57:07 2006
@@ -21,14 +21,15 @@
package org.apache.harmony.security.asn1.der;
+import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.math.BigInteger;
-import org.apache.harmony.security.asn1.BerInputStream;
-
-
import junit.framework.TestCase;
+import org.apache.harmony.security.asn1.ASN1Exception;
+import org.apache.harmony.security.asn1.BerInputStream;
+
/**
* Tests BerInputStream implementation
*
@@ -41,67 +42,107 @@
junit.textui.TestRunner.run(BerInputStreamTest.class);
}
- public void test_LengthConstraints() {
+ /**
+ * @tests org.apache.harmony.security.asn1.BerInputStream#BerInputStream(
+ * java.io.ByteArrayInputStream)
+ */
+ public void test_CtorLjava_io_ByteArrayInputStream() throws IOException {
//
- // Positive testcases
+ // tests for decoding initial length of encodings
//
- Object[][] testcase = new Object[][] {
- // length = 0x01
- new Object[] { new byte[] { 0x30, (byte) 0x81, 0x01 },
- BigInteger.valueOf(1) },
+ Object[][] testcase = {
+ // length = 0x01
+ { new byte[] { 0x30, (byte) 0x81, 0x01 }, BigInteger.valueOf(1) },
// length = 0xFF
- new Object[] { new byte[] { 0x30, (byte) 0x81, (byte) 0xFF },
+ { new byte[] { 0x30, (byte) 0x81, (byte) 0xFF },
BigInteger.valueOf(0xFF) },
// length = 0x0101
- new Object[] { new byte[] { 0x30, (byte) 0x82, 0x01, 0x01 },
+ { new byte[] { 0x30, (byte) 0x82, 0x01, 0x01 },
BigInteger.valueOf(0x0101) },
// length = 0xFFFF
- new Object[] {
- new byte[] { 0x30, (byte) 0x82, (byte) 0xFF,
- (byte) 0xFF }, BigInteger.valueOf(0xFFFF) },
+ { new byte[] { 0x30, (byte) 0x82, (byte) 0xFF, (byte) 0xFF },
+ BigInteger.valueOf(0xFFFF) },
// length = 0x0FFFFF
- new Object[] {
+ {
new byte[] { 0x30, (byte) 0x83, 0x0F, (byte) 0xFF,
(byte) 0xFF }, BigInteger.valueOf(0x0FFFFF) },
-
// length = 0xFFFFFF
- new Object[] {
+ {
new byte[] { 0x30, (byte) 0x83, (byte) 0xFF,
(byte) 0xFF, (byte) 0xFF },
BigInteger.valueOf(0xFFFFFF) },
- // length = 0xFFFFFF
- new Object[] {
+ // length = 0xFFFFFF (encoded length has extra byte)
+ {
new byte[] { 0x30, (byte) 0x84, 0x00, (byte) 0xFF,
(byte) 0xFF, (byte) 0xFF },
- BigInteger.valueOf(0xFFFFFF) },
- };
+ BigInteger.valueOf(0xFFFFFF) }, };
+ // positive testcases
for (int i = 0; i < testcase.length; i++) {
try {
- BerInputStream in = new BerInputStream((byte[]) testcase[i][0]);
+ BerInputStream in = new BerInputStream(
+ new ByteArrayInputStream((byte[]) testcase[i][0]));
int expected = ((BigInteger) testcase[i][1]).intValue();
- if (in.getLength() != expected) {
- fail("Testcase: " + i + ". Expected: " + expected
- + ", returned" + in.getLength());
- }
+ assertEquals(expected, in.getLength());
} catch (IOException e) {
+ e.printStackTrace();
fail("Testcase: " + i + "\nUnexpected exception." + e);
}
}
+ // negative testcase
+ try {
+ new BerInputStream(new ByteArrayInputStream(new byte[] { 0x30,
+ (byte) 0x84, 0x01, 0x01, 0x01, 0x01 }));
+ fail("No expected ASN1Exception");
+ } catch (ASN1Exception e) {
+ assertTrue(e.getMessage().startsWith("Too long"));
+ }
+ }
+
+ /**
+ * @tests org.apache.harmony.security.asn1.BerInputStream#BerInputStream(byte[],
+ * int,int)
+ */
+ public void test_Ctor$LbyteLintLint() throws IOException {
+
//
- // Negative testcases
+ // tests for 'expectedLength' parameter
//
+ byte[] encoded = new byte[] { 0x01, 0x01, 0x03, // boolean bytes
+ 0x06, 0x02, 0x01, 0x03, // oid bytes
+ 0x01, 0x00 // just random bytes
+ };
+ // pass boolean encoding
+ BerInputStream in = new BerInputStream(encoded, 0, 3);
+ assertEquals("boolean", 1, in.getLength());
+
+ // pass oid encoding
+ in = new BerInputStream(encoded, 3, 4);
+ assertEquals("boolean", 2, in.getLength());
+
+ // pass random encoding (equals to ANY)
+ in = new BerInputStream(encoded, 7, 2);
+ assertEquals("any", 0, in.getLength());
+
+ // extra bytes for oid
try {
- new BerInputStream(new byte[] { 0x30, (byte) 0x84, 0x01, 0x01,
- 0x01, 0x01 });
- fail("No expected IOException");
- } catch (IOException e) {
- assertTrue(e.getMessage().startsWith("Too long"));
+ new BerInputStream(encoded, 3, 5);
+ fail("No expected ASN1Exception");
+ } catch (ASN1Exception e) {
+ assertEquals("Wrong content length", e.getMessage());
+ }
+
+ // less bytes for oid
+ try {
+ new BerInputStream(encoded, 3, 3);
+ fail("No expected ASN1Exception");
+ } catch (ASN1Exception e) {
+ assertEquals("Wrong content length", e.getMessage());
}
}
}
Propchange: incubator/harmony/enhanced/classlib/trunk/modules/security/src/test/java/common/org/apache/harmony/security/asn1/der/BerInputStreamTest.java
------------------------------------------------------------------------------
svn:eol-style = native