IntegerTest.java revision e98fbf8686c5289bf03fe5c3de7ff82d3a77104d
1/*
2 *  Licensed to the Apache Software Foundation (ASF) under one or more
3 *  contributor license agreements.  See the NOTICE file distributed with
4 *  this work for additional information regarding copyright ownership.
5 *  The ASF licenses this file to You under the Apache License, Version 2.0
6 *  (the "License"); you may not use this file except in compliance with
7 *  the License.  You may obtain a copy of the License at
8 *
9 *     http://www.apache.org/licenses/LICENSE-2.0
10 *
11 *  Unless required by applicable law or agreed to in writing, software
12 *  distributed under the License is distributed on an "AS IS" BASIS,
13 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 *  See the License for the specific language governing permissions and
15 *  limitations under the License.
16 */
17
18/**
19* @author Stepan M. Mishura
20*/
21
22package org.apache.harmony.security.tests.asn1.der;
23
24import java.io.ByteArrayInputStream;
25import java.io.IOException;
26import java.math.BigInteger;
27import java.util.Arrays;
28
29import junit.framework.TestCase;
30
31import org.apache.harmony.security.asn1.ASN1Exception;
32import org.apache.harmony.security.asn1.ASN1Integer;
33import org.apache.harmony.security.asn1.DerInputStream;
34import org.apache.harmony.security.asn1.DerOutputStream;
35
36
37/**
38 * ASN.1 DER test for INTEGER type
39 *
40 * @see http://asn1.elibel.tm.fr/en/standards/index.htm
41 */
42
43public class IntegerTest extends TestCase {
44
45    public static final Object[][] validTestcase = {
46            // BigInteger / two's-complement representation / encoding
47            { new BigInteger("0"), null, null },
48            { new BigInteger("1"), null, null },
49            { new BigInteger("-1"), null, null },
50            { new BigInteger("127"), null, null },
51            { new BigInteger("-127"), null, null },
52            { new BigInteger("128"), null, null },
53            { new BigInteger("-128"), null, null },
54            { new BigInteger("32767"), null, null },
55            { new BigInteger("-32767"), null, null },
56            { new BigInteger("32768"), null, null },
57            { new BigInteger("-32768"), null, null },
58            { new BigInteger("1234567890"), null, null },
59            { new BigInteger("-1234567890"), null, null },
60            { // 20 octets
61                    new BigInteger(new byte[] { 0x7F, 0x00, 0x00, 0x00, 0x00,
62                            0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
63                            0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }), null,
64                    null }, };
65
66    static {
67        for (int i = 0; i < validTestcase.length; i++) {
68            // get two's-complement representation
69            byte[] array = ((BigInteger) validTestcase[i][0]).toByteArray();
70
71            // create encoding
72            byte[] encoded = new byte[array.length + 2];
73            encoded[0] = 0x02;
74            encoded[1] = (byte) (encoded.length - 2);
75            System.arraycopy(array, 0, encoded, 2, encoded[1]);
76
77            // assign is to testcases
78            validTestcase[i][1] = array;
79            validTestcase[i][2] = encoded;
80        }
81    }
82
83    /**
84     * Tests decoding/encoding integers to/from byte array
85     */
86    public void testDecode_Encode() throws IOException {
87
88        // oid decoder/encoder for testing
89        ASN1Integer asn1 = ASN1Integer.getInstance();
90
91        // decode from byte array
92        for (int i = 0; i < validTestcase.length; i++) {
93            DerInputStream in = new DerInputStream((byte[]) validTestcase[i][2]);
94            assertTrue((validTestcase[i][0]).toString(), // message
95                    Arrays.equals((byte[]) validTestcase[i][1], // expected
96                            (byte[]) asn1.decode(in))); // returned
97        }
98
99        // decode from input stream
100        for (int i = 0; i < validTestcase.length; i++) {
101            DerInputStream in = new DerInputStream(new ByteArrayInputStream(
102                    (byte[]) validTestcase[i][2]));
103            assertTrue((validTestcase[i][0]).toString(), //message
104                    Arrays.equals((byte[]) validTestcase[i][1], //expected
105                            (byte[]) asn1.decode(in))); //returned
106        }
107
108        // encoding
109        for (int i = 0; i < validTestcase.length; i++) {
110            DerOutputStream out = new DerOutputStream(asn1, validTestcase[i][1]);
111            assertTrue((validTestcase[i][0]).toString(), //message
112                    Arrays.equals((byte[]) validTestcase[i][2], //expected
113                            out.encoded));//returned
114        }
115    }
116
117    /**
118     * Tests invalid ASN.1 integer encodings
119     */
120    public void testDecode_Invalid() throws IOException {
121        byte[][] invalid = new byte[][] {
122        // wrong tag: tag is not 0x02
123                new byte[] { 0x01, 0x01, 0x00 },
124                // wrong length: length is 0
125                new byte[] { 0x02, 0x00 },
126                // wrong content: is not encoded in minimum number of octets
127                new byte[] { 0x02, 0x02, 0x00, 0x00 },
128                // wrong content: is not encoded in minimum number of octets
129                new byte[] { 0x02, 0x02, (byte) 0xFF, (byte) 0x80 } };
130
131        for (int i = 0; i < invalid.length; i++) {
132            try {
133                DerInputStream in = new DerInputStream(invalid[i]);
134                ASN1Integer.getInstance().decode(in);
135                fail("No expected ASN1Exception for:" + i);
136            } catch (ASN1Exception e) {
137            }
138        }
139    }
140
141    public void testConversion() {
142        int[] testcase = new int[] { 0, 1, -1, 127, -127, 128, -128, 32767,
143                -32768, Integer.MAX_VALUE, Integer.MIN_VALUE };
144
145        for (int i = 0; i < testcase.length; i++) {
146            assertEquals("Testcase: " + i, testcase[i], ASN1Integer
147                    .toIntValue(ASN1Integer.fromIntValue(testcase[i])));
148        }
149    }
150}
151