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.util.Arrays;
026:
027: import junit.framework.TestCase;
028:
029: import org.apache.harmony.security.asn1.ASN1BitString;
030: import org.apache.harmony.security.asn1.ASN1Exception;
031: import org.apache.harmony.security.asn1.BitString;
032: import org.apache.harmony.security.asn1.DerInputStream;
033: import org.apache.harmony.security.asn1.DerOutputStream;
034: import org.apache.harmony.security.asn1.ASN1BitString.ASN1NamedBitList;
035:
036: /**
037: * ASN.1 DER test for Bitstring type
038: *
039: * @see http://asn1.elibel.tm.fr/en/standards/index.htm
040: */
041:
042: public class BitStringTest extends TestCase {
043:
044: public static void main(String[] args) {
045: junit.textui.TestRunner.run(BitStringTest.class);
046: }
047:
048: private static Object[][] validBitstring = new Object[][] {
049: //bitstring array format: bitstring object/ byte array
050: //
051: { new BitString(new byte[] {}, 0), // object
052: new byte[] { 0x03, 0x01, 0x00 } },
053: //
054: { new BitString(new byte[] { 0x05 }, 0), // object
055: new byte[] { 0x03, 0x02, 0x00, 0x05 } },
056: //
057: { new BitString(new byte[] { (byte) 0x80 }, 7), // object
058: new byte[] { 0x03, 0x02, 0x07, (byte) 0x80 } } };
059:
060: public void testDecode_Encode() throws IOException {
061:
062: // decoder/encoder for testing
063: ASN1BitString asn1 = ASN1BitString.getInstance();
064:
065: // decode from byte array
066: for (int i = 0; i < validBitstring.length; i++) {
067: DerInputStream in = new DerInputStream(
068: (byte[]) validBitstring[i][1]);
069:
070: BitString expected = (BitString) validBitstring[i][0];
071: BitString decoded = (BitString) asn1.decode(in);
072:
073: assertEquals("Testcase: " + i, expected.unusedBits,
074: decoded.unusedBits);
075:
076: assertTrue("Testcase: " + i, Arrays.equals(expected.bytes,
077: decoded.bytes));
078: }
079:
080: // decode from input stream
081: for (int i = 0; i < validBitstring.length; i++) {
082: DerInputStream in = new DerInputStream(
083: new ByteArrayInputStream(
084: (byte[]) validBitstring[i][1]));
085:
086: BitString expected = (BitString) validBitstring[i][0];
087: BitString decoded = (BitString) asn1.decode(in);
088:
089: assertEquals("Testcase: " + i, expected.unusedBits,
090: decoded.unusedBits);
091:
092: assertTrue("Testcase: " + i, Arrays.equals(expected.bytes,
093: decoded.bytes));
094: }
095:
096: // encoding
097: for (int i = 0; i < validBitstring.length; i++) {
098: DerOutputStream out = new DerOutputStream(asn1,
099: validBitstring[i][0]);
100: assertTrue("Testcase: " + i, Arrays.equals(
101: (byte[]) validBitstring[i][1], out.encoded));
102: }
103: }
104:
105: public void testDecode_Invalid() throws IOException {
106: byte[][] invalid = new byte[][] {
107: // wrong tag: tag is not 0x03
108: new byte[] { 0x02, 0x01, 0x00 },
109: // wrong length: length is 0
110: new byte[] { 0x03, 0x00 },
111: // wrong content: unused bits value > 7
112: new byte[] { 0x03, 0x03, 0x09, 0x0F, 0x0F },
113: // wrong content: not 0 unused bits for empty string
114: new byte[] { 0x03, 0x01, 0x01 },
115: // wrong content: unused bits in final octet are not 0
116: new byte[] { 0x03, 0x02, 0x01, 0x01 },
117: // wrong content: constructed encoding
118: new byte[] { 0x23, 0x03, 0x03, 0x01, 0x00 } };
119:
120: for (int i = 0; i < invalid.length; i++) {
121: try {
122: DerInputStream in = new DerInputStream(invalid[i]);
123: ASN1BitString.getInstance().decode(in);
124: fail("No expected ASN1Exception for: " + i);
125: } catch (ASN1Exception e) {
126: }
127: }
128: }
129:
130: //
131: //
132: // Named Bit List
133: //
134: //
135:
136: public void testDecodeNamedBitList() throws IOException {
137:
138: Object[][] testcaseBoolean = new Object[][] {
139: // bitstring array format: bitstring object/ byte array
140: //
141: { new boolean[] {}, // object
142: new byte[] { 0x03, 0x01, 0x00 } },
143: //
144: { new boolean[] { true }, // object
145: new byte[] { 0x03, 0x02, 0x07, (byte) 0x80 } },
146: //
147: { new boolean[] { true, false, true }, // object
148: new byte[] { 0x03, 0x02, 0x05, (byte) 0xA0 } },
149: //
150: {
151: new boolean[] { true, true, true, true, true,
152: true, true, true }, // object
153: new byte[] { 0x03, 0x02, 0x00, (byte) 0xFF } },
154: //
155: {
156: new boolean[] { false, false, false, false,
157: false, false, false, false, true }, // object
158: new byte[] { 0x03, 0x03, 0x07, 0x00,
159: (byte) 0x80 } } };
160:
161: ASN1NamedBitList decoder = new ASN1NamedBitList();
162:
163: for (int i = 0; i < testcaseBoolean.length; i++) {
164: DerInputStream in = new DerInputStream(
165: (byte[]) testcaseBoolean[i][1]);
166:
167: assertTrue("Testcase: " + i, Arrays.equals(
168: (boolean[]) testcaseBoolean[i][0],
169: (boolean[]) decoder.decode(in)));
170: }
171: }
172:
173: public void testDecodeNamedBitList_SizeConstraints()
174: throws IOException {
175:
176: Object[][] testcaseBoolean = new Object[][] {
177: //bitstring array format: bitstring object/ byte array
178: //
179: {
180: new boolean[] { false, false, false, false,
181: false, false, false, false }, // object
182: new byte[] { 0x03, 0x01, 0x00 } },
183: //
184: {
185: new boolean[] { true, false, false, false,
186: false, false, false, false }, // object
187: new byte[] { 0x03, 0x02, 0x07, (byte) 0x80 } },
188: //
189: {
190: new boolean[] { true, false, true, false,
191: false, false, false, false }, // object
192: new byte[] { 0x03, 0x02, 0x05, (byte) 0xA0 } },
193: //
194: {
195: new boolean[] { true, true, true, true, true,
196: true, true, true }, // object
197: new byte[] { 0x03, 0x02, 0x00, (byte) 0xFF } },
198: //
199: {
200: new boolean[] { false, false, false, false,
201: false, false, false, false, true }, // object
202: new byte[] { 0x03, 0x03, 0x07, 0x00,
203: (byte) 0x80 } } };
204:
205: ASN1NamedBitList decoder = new ASN1NamedBitList(8);
206:
207: for (int i = 0; i < testcaseBoolean.length; i++) {
208: DerInputStream in = new DerInputStream(
209: (byte[]) testcaseBoolean[i][1]);
210:
211: assertTrue("Testcase: " + i, Arrays.equals(
212: (boolean[]) testcaseBoolean[i][0],
213: (boolean[]) decoder.decode(in)));
214: }
215: }
216:
217: public void testEncodeNamedBitList() throws IOException {
218:
219: Object[][] testcaseBoolean = new Object[][] {
220: //bitstring array format: bitstring object/ byte array
221: //
222: { new boolean[] {}, // object
223: new byte[] { 0x03, 0x01, 0x00 } },
224: //
225: { new boolean[] { false }, // object
226: new byte[] { 0x03, 0x01, 0x00 } },
227: //
228: { new boolean[] { true }, // object
229: new byte[] { 0x03, 0x02, 0x07, (byte) 0x80 } },
230: //
231: { new boolean[] { true, false, true }, // object
232: new byte[] { 0x03, 0x02, 0x05, (byte) 0xA0 } },
233: //
234: {
235: new boolean[] { true, true, true, true, true,
236: true, true, true }, // object
237: new byte[] { 0x03, 0x02, 0x00, (byte) 0xFF } },
238: //
239: {
240: new boolean[] { false, false, false, false,
241: false, false, false, false, true }, // object
242: new byte[] { 0x03, 0x03, 0x07, 0x00,
243: (byte) 0x80 } } };
244:
245: ASN1NamedBitList encoder = new ASN1NamedBitList();
246:
247: for (int i = 0; i < testcaseBoolean.length; i++) {
248: DerOutputStream out = new DerOutputStream(encoder,
249: testcaseBoolean[i][0]);
250: assertTrue("Testcase: " + i, Arrays.equals(
251: (byte[]) testcaseBoolean[i][1], out.encoded));
252: }
253: }
254: }
|