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
18package org.apache.harmony.security.tests.x509.tsp;
19
20import java.io.IOException;
21import java.math.BigInteger;
22import java.util.Arrays;
23import java.util.Date;
24
25import junit.framework.TestCase;
26
27import org.apache.harmony.security.x501.Name;
28import org.apache.harmony.security.x509.AlgorithmIdentifier;
29import org.apache.harmony.security.x509.Extension;
30import org.apache.harmony.security.x509.Extensions;
31import org.apache.harmony.security.x509.GeneralName;
32import org.apache.harmony.security.x509.tsp.MessageImprint;
33import org.apache.harmony.security.x509.tsp.TSTInfo;
34
35public class TSTInfoTest extends TestCase {
36
37    /**
38     * @throws IOException
39     * @tests 'org.apache.harmony.security.x509.tsp.TSTInfo.getEncoded()'
40     */
41    public void testGetEncoded() throws IOException {
42        // Unizeto CETRUM policy
43        String policy = "1.2.3.4.5";
44        // SHA1 OID as defined in RFC 3161
45        String sha1 = "1.3.14.3.2.26";
46        MessageImprint msgImprint = new MessageImprint(new AlgorithmIdentifier(
47                sha1), new byte[20]);
48        Date genTime = new Date();
49        BigInteger nonce = BigInteger.valueOf(1234567890L);
50        int[] accuracy = new int[] { 1, 0, 0 };
51        GeneralName tsa = new GeneralName(new Name("CN=AnAuthority"));
52        Extensions exts = new Extensions();
53        // Time-Stamping extension OID: as defined in RFC 3161
54        int[] timeStampingExtOID = new int[] { 1, 3, 6, 1, 5, 5, 7, 3, 8 };
55        byte[] timeStampingExtValue = new byte[] { (byte) 1, (byte) 2, (byte) 3 };
56        Extension ext = new Extension(timeStampingExtOID, true,
57                timeStampingExtValue);
58        exts.addExtension(ext);
59
60        TSTInfo info = new TSTInfo(1, policy, msgImprint, BigInteger.TEN,
61                genTime, accuracy, Boolean.FALSE, nonce, tsa, exts);
62
63        byte[] encoding = TSTInfo.ASN1.encode(info);
64        TSTInfo decoded = (TSTInfo) TSTInfo.ASN1.decode(encoding);
65
66        assertEquals("Decoded version is incorrect", info.getVersion(), decoded
67                .getVersion());
68        assertEquals("Decoded policy is incorrect", policy, decoded.getPolicy());
69        assertTrue("Decoded messageImprint is incorrect", Arrays.equals(
70                MessageImprint.ASN1.encode(msgImprint), MessageImprint.ASN1
71                .encode(decoded.getMessageImprint())));
72        assertEquals("Decoded serialNumber is incorrect", BigInteger.TEN,
73                decoded.getSerialNumber());
74        assertEquals("Decoded genTime is incorrect", genTime, decoded
75                .getGenTime());
76        assertTrue("Decoded accuracy is incorrect", Arrays.equals(accuracy,
77                decoded.getAccuracy()));
78        assertFalse("Decoded ordering is incorrect", decoded.getOrdering()
79                .booleanValue());
80        assertEquals("Decoded nonce is incorrect", nonce, decoded.getNonce());
81        assertEquals("Decoded tsa is incorrect", tsa, decoded.getTsa());
82        assertEquals("Decoded extensions is incorrect", exts, decoded
83                .getExtensions());
84    }
85}
86
87