UTCTimeTest.java revision 561ee011997c6c2f1befbfaa9d5f0a99771c1d63
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.asn1.der;
19
20import java.io.ByteArrayInputStream;
21import java.text.ParseException;
22import java.text.SimpleDateFormat;
23import java.util.Arrays;
24import java.util.Locale;
25import java.util.TimeZone;
26
27import junit.framework.TestCase;
28
29import org.apache.harmony.security.asn1.ASN1UTCTime;
30import org.apache.harmony.security.asn1.DerInputStream;
31import org.apache.harmony.security.asn1.DerOutputStream;
32
33/**
34 * ASN.1 DER test for UTCTime type
35 *
36 * @see http://asn1.elibel.tm.fr/en/standards/index.htm
37 */
38
39public class UTCTimeTest extends TestCase {
40
41    // UTC time decoder/encoder for testing
42    private static final ASN1UTCTime utime = ASN1UTCTime.getInstance();
43
44    // data for testing with format: date string/DER encoding/Date object
45    public static final Object[][] validUTCTimes;
46
47    static {
48        SimpleDateFormat sdf = new SimpleDateFormat("dd MMM yyyy HH:mm:ss", Locale.US);
49        sdf.setTimeZone(TimeZone.getTimeZone("GMT"));
50
51        validUTCTimes = new Object[][] {
52                // YYMMDD-HHMMSS = "500708091011Z"
53                {
54                        "8 Jul 1950 09:10:11",
55                        new byte[] { 0x17, 0x0D, 0x35, 0x30, 0x30, 0x37, 0x30,
56                                0x38, 0x30, 0x39, 0x31, 0x30, 0x31, 0x31, 0x5A },
57                        null },
58                //YYMMDD-HHMMSS = "991213141516Z"
59                {
60                        "13 Dec 1999 14:15:16",
61                        new byte[] { 0x17, 0x0D, 0x39, 0x39, 0x31, 0x32, 0x31,
62                                0x33, 0x31, 0x34, 0x31, 0x35, 0x31, 0x36, 0x5A },
63                        null },
64                // YYMMDD-HHMMSS = "000101000000Z"
65                {
66                        "01 Jan 2000 00:00:00",
67                        new byte[] { 0x17, 0x0D, 0x30, 0x30, 0x30, 0x31, 0x30,
68                                0x31, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x5A },
69                        null },
70                // YYMMDD-HHMMSS = "490203040506Z"
71                {
72                        "3 Feb 2049 04:05:06",
73                        new byte[] { 0x17, 0x0D, 0x34, 0x39, 0x30, 0x32, 0x30,
74                                0x33, 0x30, 0x34, 0x30, 0x35, 0x30, 0x36, 0x5A },
75                        null },
76                        };
77
78        try {
79            // fill values for Date objects by parsing date string
80            for (int i = 0; i < validUTCTimes.length; i++) {
81                validUTCTimes[i][2] = sdf
82                        .parseObject((String) validUTCTimes[i][0]);
83            }
84        } catch (ParseException e) {
85            throw new RuntimeException(e.getMessage());
86        }
87    }
88
89    /**
90     * Verifies decoding/encoding ASN.1 UTCTime.
91     * It must interpret the year field (YY) as follows:
92     *  - if YY is greater than or equal to 50 then interpreted as 19YY
93     *  - and if YY is less than 50 then interpreted as 20YY.
94     */
95    public void testDecodeEncode() throws Exception {
96
97        // decoding byte array
98        for (int i = 0; i < validUTCTimes.length; i++) {
99            DerInputStream in = new DerInputStream((byte[]) validUTCTimes[i][1]);
100            assertEquals("Decoding array for " + validUTCTimes[i][0],
101                    validUTCTimes[i][2], //expected
102                    utime.decode(in)); //decoded
103        }
104
105        // decoding input stream
106        for (int i = 0; i < validUTCTimes.length; i++) {
107            DerInputStream in = new DerInputStream(new ByteArrayInputStream(
108                    (byte[]) validUTCTimes[i][1]));
109            assertEquals("Decoding stream for " + validUTCTimes[i][0],
110                    validUTCTimes[i][2], //expected
111                    utime.decode(in)); //decoded
112        }
113
114        // encoding date object
115        for (int i = 0; i < validUTCTimes.length; i++) {
116            DerOutputStream out = new DerOutputStream(utime,
117                    validUTCTimes[i][2]);
118            assertTrue("Encoding date for " + validUTCTimes[i][0], Arrays
119                    .equals((byte[]) validUTCTimes[i][1], // expected
120                            out.encoded)); //encoded
121        }
122    }
123
124    public static void main(String[] args) {
125        junit.textui.TestRunner.run(UTCTimeTest.class);
126    }
127}
128