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 Vladimir N. Molotkov
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.text.ParseException;
026: import java.text.SimpleDateFormat;
027: import java.util.Date;
028: import java.util.TimeZone;
029:
030: import org.apache.harmony.security.asn1.ASN1UTCTime;
031: import org.apache.harmony.security.asn1.DerInputStream;
032: import org.apache.harmony.security.asn1.DerOutputStream;
033:
034: import junit.framework.TestCase;
035:
036: /**
037: * ASN.1 DER test for UTCTime type
038: *
039: * @see http://asn1.elibel.tm.fr/en/standards/index.htm
040: */
041: public class DerUTCTimeEDTest extends TestCase {
042:
043: private ASN1UTCTime uTime = ASN1UTCTime.getInstance();
044:
045: private final int workersNumber = 10;
046: private boolean mtTestPassed;
047:
048: /**
049: * UTC TIME DER Encoder test
050: * @throws ParseException
051: */
052: public final void testUTCEncoder() throws Exception {
053: // no fractional seconds (last 3 0s and "." must be trimmed out)
054: Date myDate = getGmtDate(1101980374187L);
055: byte[] encoded = new DerOutputStream(uTime, myDate).encoded;
056: String rep = new String(encoded, 2, encoded[1] & 0xff);
057: assertEquals("no fraction", "041202093934Z", rep);
058:
059: // midnight
060: SimpleDateFormat sdf = new SimpleDateFormat("dd.MM.yyyy HH:mm");
061: sdf.setTimeZone(TimeZone.getTimeZone("UTC"));
062: myDate = sdf.parse("06.06.2004 00:00");
063: encoded = new DerOutputStream(uTime, myDate).encoded;
064: rep = new String(encoded, 2, encoded[1] & 0xff);
065: assertEquals("midnight", "040606000000Z", rep);
066: }
067:
068: /**
069: * UTC TIME DER Encoder/Decoder test
070: * (byte array case)
071: * @throws ParseException
072: * @throws IOException
073: */
074: public final void testUTCEncoderDecoder01() throws ParseException,
075: IOException {
076: runTest(false);
077: }
078:
079: /**
080: * UTC TIME DER Encoder/Decoder test
081: * (InputStream case)
082: * @throws ParseException
083: * @throws IOException
084: */
085: public final void testUTCEncoderDecoder02() throws ParseException,
086: IOException {
087: runTest(true);
088: }
089:
090: private final void runTest(boolean useInputStream)
091: throws IOException, ParseException {
092: Date myDate = new Date(1101980374187L);
093: byte[] encoded = new DerOutputStream(uTime, myDate).encoded;
094: DerInputStream dis = useInputStream ? new DerInputStream(
095: new ByteArrayInputStream(encoded))
096: : new DerInputStream(encoded);
097: // the difference only fractional-seconds
098: assertEquals(187,
099: (myDate.getTime() - ((Date) uTime.decode(dis))
100: .getTime()));
101:
102: // midnight
103: myDate = new SimpleDateFormat("MM.dd.yyyy HH:mm")
104: .parse("06.06.2004 00:00");
105: encoded = new DerOutputStream(uTime, myDate).encoded;
106: dis = useInputStream ? new DerInputStream(
107: new ByteArrayInputStream(encoded))
108: : new DerInputStream(encoded);
109: assertEquals(myDate, uTime.decode(dis));
110: }
111:
112: public final void testMt() throws InterruptedException {
113: mtTestPassed = true;
114: Thread[] workers = new Thread[workersNumber];
115: for (int i = 0; i < workersNumber; i++) {
116: workers[i] = new TestWorker();
117: }
118: for (int i = 0; i < workersNumber; i++) {
119: workers[i].start();
120: }
121: for (int i = 0; i < workersNumber; i++) {
122: workers[i].join();
123: }
124: assertTrue(mtTestPassed);
125: }
126:
127: private static Date getGmtDate(long mills) {
128: return new Date(mills);
129: }
130:
131: /**
132: * MT Test worker thread
133: *
134: * @author Vladimir Molotkov
135: * @version 0.1
136: */
137: private class TestWorker extends Thread {
138:
139: public void run() {
140: for (int i = 0; i < 100; i++) {
141: try {
142: // Perform DER encoding/decoding:
143: if (i % 2 == 0) {
144: testUTCEncoderDecoder01();
145: } else {
146: testUTCEncoderDecoder02();
147: }
148: } catch (Throwable e) {
149: System.err.println(e);
150: mtTestPassed = false;
151: return;
152: }
153: }
154: }
155: }
156: }
|