1 /*
2  * @(#) $Id: ASN1ParseTest.java,v 1.3 2003/07/08 08:13:52 pankaj Exp $
3  *
4  * Copyright (c) 2002-03 by Pankaj Kumar (http://www.pankaj-k.net). 
5  * All rights reserved.
6  *
7  * The license governing the use of this file can be found in the 
8  * root directory of the containing software.
9  */
10package org.jstk.asn1;
11
12import java.util.logging.Logger;
13import java.util.Vector;
14import java.io.*;
15import junit.framework.*;
16import org.jstk.pem.*;
17
18public class ASN1ParseTest extends TestCase {
19    public static final Logger logger = ASN1Type.logger;
20    class InputFile {
21        byte[]  bytes;
22        String  file;
23        InputFile(byte[] bytes, String file){
24            this.bytes = bytes;
25            this.file = file;
26        }
27    }
28    protected String[] inputFiles = new String[] {
29        "data/test.csr",
30        "data/test1.cer",
31        "data/test2.pem"
32    };
33    protected Vector inputStreamVec = new Vector();
34
35    public static void main (String[] args) {
36        junit.textui.TestRunner.run (suite());
37    }
38    protected void setUp() {
39        for (int i = 0; i < inputFiles.length; i++){
40            String file = inputFiles[i];
41            byte[] bytes = null;
42            try {                                       // Try PEM format
43                BufferedReader  reader = new BufferedReader(new FileReader(file));
44                PEMData x = new PEMData(reader);
45                bytes = x.decode();
46            } catch (InvalidPEMFormatException exc){    // Assume DER format
47                ByteArrayOutputStream baos = null;
48                try {
49                    FileInputStream is = new FileInputStream(file);
50                    baos = new ByteArrayOutputStream();
51                    byte[] buf = new byte[1024];
52                    int n;
53                    while ((n = is.read(buf)) > 0)
54                        baos.write(buf, 0, n);
55                } catch (IOException ioe){              // Input file has a problem
56                    logger.info("I/O problem with : " + file + ", Exception: " + ioe + ". Skipping ...");
57                    continue;
58                }
59                bytes = baos.toByteArray();
60            } catch (IOException ioe){                  // Input file has a problem
61                logger.info("I/O problem with : " + file + ", Exception: " + ioe + ". Skipping ...");
62                continue;
63            }
64            inputStreamVec.add(new InputFile(bytes, file));
65        }
66    }
67    public static Test suite() {
68        return new TestSuite(ASN1ParseTest.class);
69    }
70    public void testParse() {
71        logger.entering(getClass().getName(), "testParse");
72        for (int i = 0; i < inputStreamVec.size(); i++){
73            DefASN1PullParser parser = new DefASN1PullParser();
74            InputFile inpf = (InputFile)inputStreamVec.elementAt(i);
75            parser.setInput(new ByteArrayInputStream(inpf.bytes));
76            try {
77                while (parser.next() != ASN1PullParser.EOF);
78            } catch (Exception e){
79                fail("parsing failed for file: " + inpf.file + ", Exception: " + e);
80            }
81            logger.info("parsing succeeded for file: " + inpf.file);
82        }
83        logger.exiting(getClass().getName(), "testParse");
84    }
85    public void testDecode() {
86        logger.entering(getClass().getName(), "testDecode");
87        for (int i = 0; i < inputStreamVec.size(); i++){
88            DefASN1PullParser parser = new DefASN1PullParser();
89            InputFile inpf = (InputFile)inputStreamVec.elementAt(i);
90            parser.setInput(new ByteArrayInputStream(inpf.bytes));
91            try {
92                ASN1Any any = new ASN1Any();
93                any.decode(parser);
94            } catch (Exception e){
95                fail("decode failed for file: " + inpf.file + ", Exception: " + e);
96            }
97            logger.info("decode succeeded for file: " + inpf.file);
98        }
99        logger.exiting(getClass().getName(), "testDecode");
00    }
01    public void testRoundTrip() {
02        logger.entering(getClass().getName(), "testRoundTrip");
03        for (int i = 0; i < inputStreamVec.size(); i++){
04            DefASN1PullParser parser = new DefASN1PullParser();
05            InputFile inpf = (InputFile)inputStreamVec.elementAt(i);
06            parser.setInput(new ByteArrayInputStream(inpf.bytes));
07            try {
08                ASN1Any any = new ASN1Any();
09                any.decode(parser);
10                byte[] encoded = any.encode();
11                assertTrue(equalsByteArray(inpf.bytes, encoded));
12            } catch (Exception e){
13                fail("roundtrip test failed for file: " + inpf.file + ", Exception: " + e);
14            }
15            logger.info("roundtrip test succeeded for file: " + inpf.file);
16        }
17        logger.exiting(getClass().getName(), "testRoundTrip");
18    }
19    public boolean equalsByteArray(byte[] aa, byte[] ba){
20        if (aa != null && ba != null && aa.length == ba.length){
21            for (int i = 0; i < aa.length; i++){
22                if (aa[i] != ba[i])
23                    return false;
24            }
25            return true;
26        } else if (aa == null && ba == null){
27            return true;
28        }
29        return true;
30    }
31}