DerUTCTimeEDTest.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
18/**
19* @author Vladimir N. Molotkov
20*/
21
22package org.apache.harmony.security.tests.asn1.der;
23
24import java.io.ByteArrayInputStream;
25import java.io.IOException;
26import java.text.ParseException;
27import java.text.SimpleDateFormat;
28import java.util.Date;
29import java.util.TimeZone;
30
31import org.apache.harmony.security.asn1.ASN1UTCTime;
32import org.apache.harmony.security.asn1.DerInputStream;
33import org.apache.harmony.security.asn1.DerOutputStream;
34
35import junit.framework.TestCase;
36
37
38/**
39 * ASN.1 DER test for UTCTime type
40 *
41 * @see http://asn1.elibel.tm.fr/en/standards/index.htm
42 */
43public class DerUTCTimeEDTest extends TestCase {
44
45    private ASN1UTCTime uTime = ASN1UTCTime.getInstance();
46
47    private final int workersNumber = 10;
48    private boolean mtTestPassed;
49    /**
50     * UTC TIME DER Encoder test
51     * @throws ParseException
52     */
53    public final void testUTCEncoder() throws Exception {
54        // no fractional seconds (last 3 0s and "." must be trimmed out)
55        Date myDate = getGmtDate(1101980374187L);
56        byte[] encoded =
57            new DerOutputStream(uTime, myDate).encoded;
58        String rep = new String(encoded, 2, encoded[1] & 0xff, "UTF-8");
59        assertEquals("no fraction", "041202093934Z", rep);
60
61        // midnight
62        SimpleDateFormat sdf =
63            new SimpleDateFormat("dd.MM.yyyy HH:mm");
64        sdf.setTimeZone(TimeZone.getTimeZone("UTC"));
65        myDate = sdf.parse("06.06.2004 00:00");
66        encoded =
67            new DerOutputStream(uTime, myDate).encoded;
68        rep = new String(encoded, 2, encoded[1] & 0xff, "UTF-8");
69        assertEquals("midnight", "040606000000Z", rep);
70    }
71
72    /**
73     * UTC TIME DER Encoder/Decoder test
74     * (byte array case)
75     * @throws ParseException
76     * @throws IOException
77     */
78    public final void testUTCEncoderDecoder01()
79        throws ParseException,
80               IOException {
81        runTest(false);
82    }
83
84    /**
85     * UTC TIME DER Encoder/Decoder test
86     * (InputStream case)
87     * @throws ParseException
88     * @throws IOException
89     */
90    public final void testUTCEncoderDecoder02()
91        throws ParseException,
92               IOException {
93        runTest(true);
94    }
95
96    private final void runTest(boolean useInputStream)
97        throws IOException, ParseException {
98        Date myDate = new Date(1101980374187L);
99        byte[] encoded =
100            new DerOutputStream(uTime, myDate).encoded;
101        DerInputStream dis = useInputStream
102        ? new DerInputStream(new ByteArrayInputStream(encoded))
103        : new DerInputStream(encoded);
104        // the difference only fractional-seconds
105        assertEquals(187, (myDate.getTime()-((Date)uTime.decode(dis)).getTime()));
106
107        // midnight
108        myDate = new SimpleDateFormat("MM.dd.yyyy HH:mm").
109            parse("06.06.2004 00:00");
110        encoded =
111            new DerOutputStream(uTime, myDate).encoded;
112        dis = useInputStream
113        ? new DerInputStream(new ByteArrayInputStream(encoded))
114        : new DerInputStream(encoded);
115        assertEquals(myDate, uTime.decode(dis));
116    }
117
118    public final void testMt() throws InterruptedException {
119        mtTestPassed = true;
120        Thread[] workers = new Thread[workersNumber];
121            for(int i=0; i<workersNumber; i++) {
122                workers[i] = new TestWorker();
123            }
124            for(int i=0; i<workersNumber; i++) {
125                workers[i].start();
126            }
127            for(int i=0; i<workersNumber; i++) {
128                workers[i].join();
129            }
130            assertTrue(mtTestPassed);
131    }
132
133    private static Date getGmtDate(long mills) {
134        return new Date(mills);
135    }
136
137    /**
138     * MT Test worker thread
139     *
140     * @author Vladimir Molotkov
141     * @version 0.1
142     */
143    private class TestWorker extends Thread {
144
145        public void run() {
146            for (int i=0; i<100; i++) {
147                try {
148                    // Perform DER encoding/decoding:
149                    if(i%2==0) {
150                        testUTCEncoderDecoder01();
151                    } else {
152                        testUTCEncoderDecoder02();
153                    }
154                } catch (Throwable e) {
155                    System.err.println(e);
156                    mtTestPassed = false;
157                    return;
158                }
159            }
160        }
161    }
162}
163