001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017:
018: /**
019: * @author Stepan M. Mishura
020: * @version $Revision$
021: */package org.apache.harmony.security.tests.asn1.der;
022:
023: import java.io.ByteArrayInputStream;
024: import java.io.IOException;
025: import java.math.BigInteger;
026: import java.util.Arrays;
027:
028: import junit.framework.TestCase;
029:
030: import org.apache.harmony.security.asn1.ASN1Exception;
031: import org.apache.harmony.security.asn1.ASN1Integer;
032: import org.apache.harmony.security.asn1.DerInputStream;
033: import org.apache.harmony.security.asn1.DerOutputStream;
034:
035: /**
036: * ASN.1 DER test for INTEGER type
037: *
038: * @see http://asn1.elibel.tm.fr/en/standards/index.htm
039: */
040:
041: public class IntegerTest extends TestCase {
042:
043: public static void main(String[] args) {
044: junit.textui.TestRunner.run(IntegerTest.class);
045: }
046:
047: public static final Object[][] validTestcase = {
048: // BigInteger / two's-complement representation / encoding
049: { new BigInteger("0"), null, null },
050: { new BigInteger("1"), null, null },
051: { new BigInteger("-1"), null, null },
052: { new BigInteger("127"), null, null },
053: { new BigInteger("-127"), null, null },
054: { new BigInteger("128"), null, null },
055: { new BigInteger("-128"), null, null },
056: { new BigInteger("32767"), null, null },
057: { new BigInteger("-32767"), null, null },
058: { new BigInteger("32768"), null, null },
059: { new BigInteger("-32768"), null, null },
060: { new BigInteger("1234567890"), null, null },
061: { new BigInteger("-1234567890"), null, null }, { // 20 octets
062: new BigInteger(new byte[] { 0x7F, 0x00, 0x00, 0x00,
063: 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
064: 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
065: 0x00, 0x00 }), null, null }, };
066:
067: static {
068: for (int i = 0; i < validTestcase.length; i++) {
069: // get two's-complement representation
070: byte[] array = ((BigInteger) validTestcase[i][0])
071: .toByteArray();
072:
073: // create encoding
074: byte[] encoded = new byte[array.length + 2];
075: encoded[0] = 0x02;
076: encoded[1] = (byte) (encoded.length - 2);
077: System.arraycopy(array, 0, encoded, 2, encoded[1]);
078:
079: // assign is to testcases
080: validTestcase[i][1] = array;
081: validTestcase[i][2] = encoded;
082: }
083: }
084:
085: /**
086: * Tests decoding/encoding integers to/from byte array
087: */
088: public void testDecode_Encode() throws IOException {
089:
090: // oid decoder/encoder for testing
091: ASN1Integer asn1 = ASN1Integer.getInstance();
092:
093: // decode from byte array
094: for (int i = 0; i < validTestcase.length; i++) {
095: DerInputStream in = new DerInputStream(
096: (byte[]) validTestcase[i][2]);
097: assertTrue((validTestcase[i][0]).toString(), // message
098: Arrays.equals((byte[]) validTestcase[i][1], // expected
099: (byte[]) asn1.decode(in))); // returned
100: }
101:
102: // decode from input stream
103: for (int i = 0; i < validTestcase.length; i++) {
104: DerInputStream in = new DerInputStream(
105: new ByteArrayInputStream(
106: (byte[]) validTestcase[i][2]));
107: assertTrue((validTestcase[i][0]).toString(), //message
108: Arrays.equals((byte[]) validTestcase[i][1], //expected
109: (byte[]) asn1.decode(in))); //returned
110: }
111:
112: // encoding
113: for (int i = 0; i < validTestcase.length; i++) {
114: DerOutputStream out = new DerOutputStream(asn1,
115: validTestcase[i][1]);
116: assertTrue((validTestcase[i][0]).toString(), //message
117: Arrays.equals((byte[]) validTestcase[i][2], //expected
118: out.encoded));//returned
119: }
120: }
121:
122: /**
123: * Tests invalid ASN.1 integer encodings
124: */
125: public void testDecode_Invalid() throws IOException {
126: byte[][] invalid = new byte[][] {
127: // wrong tag: tag is not 0x02
128: new byte[] { 0x01, 0x01, 0x00 },
129: // wrong length: length is 0
130: new byte[] { 0x02, 0x00 },
131: // wrong content: is not encoded in minimum number of octets
132: new byte[] { 0x02, 0x02, 0x00, 0x00 },
133: // wrong content: is not encoded in minimum number of octets
134: new byte[] { 0x02, 0x02, (byte) 0xFF, (byte) 0x80 } };
135:
136: for (int i = 0; i < invalid.length; i++) {
137: try {
138: DerInputStream in = new DerInputStream(invalid[i]);
139: ASN1Integer.getInstance().decode(in);
140: fail("No expected ASN1Exception for:" + i);
141: } catch (ASN1Exception e) {
142: }
143: }
144: }
145:
146: public void testConversion() {
147: int[] testcase = new int[] { 0, 1, -1, 127, -127, 128, -128,
148: 32767, -32768, Integer.MAX_VALUE, Integer.MIN_VALUE };
149:
150: for (int i = 0; i < testcase.length; i++) {
151: assertEquals("Testcase: " + i, testcase[i], ASN1Integer
152: .toIntValue(ASN1Integer.fromIntValue(testcase[i])));
153: }
154: }
155: }
|