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.ASN1GeneralizedTime;
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 GeneralizedTime type
038: *
039: * @see http://asn1.elibel.tm.fr/en/standards/index.htm
040: */
041: public class DerGeneralizedTimeEDTest extends TestCase {
042:
043: private ASN1GeneralizedTime gTime = ASN1GeneralizedTime
044: .getInstance();
045:
046: /**
047: * GENERALIZED TIME DER Encoder test
048: * @throws ParseException
049: */
050: public final void testGeneralizedEncoder() throws Exception {
051: // full fractional seconds
052: Date myDate = getGmtDate(1101980374187L);
053: byte[] encoded = new DerOutputStream(gTime, myDate).encoded;
054: String rep = new String(encoded, 2, encoded[1] & 0xff);
055: assertEquals("full", "20041202093934.187Z", rep);
056:
057: // 2 digit fractional seconds (last 0 must be trimmed out)
058: myDate = getGmtDate(1101980374180L);
059: encoded = new DerOutputStream(gTime, myDate).encoded;
060: rep = new String(encoded, 2, encoded[1] & 0xff);
061: assertEquals("2 fraction", "20041202093934.18Z", rep);
062:
063: // 1 digit fractional seconds (last 2 0s must be trimmed out)
064: myDate = getGmtDate(1101980374100L);
065: encoded = new DerOutputStream(gTime, myDate).encoded;
066: rep = new String(encoded, 2, encoded[1] & 0xff);
067: assertEquals("1 fraction", "20041202093934.1Z", rep);
068:
069: // no fractional seconds (last 3 0s and "." must be trimmed out)
070: myDate = getGmtDate(1101980374000L);
071: encoded = new DerOutputStream(gTime, myDate).encoded;
072: rep = new String(encoded, 2, encoded[1] & 0xff);
073: assertEquals("no fraction", "20041202093934Z", rep);
074:
075: // midnight
076: SimpleDateFormat sdf = new SimpleDateFormat("dd.MM.yyyy HH:mm");
077: sdf.setTimeZone(TimeZone.getTimeZone("UTC"));
078: myDate = sdf.parse("06.06.2004 00:00");
079: encoded = new DerOutputStream(gTime, myDate).encoded;
080: rep = new String(encoded, 2, encoded[1] & 0xff);
081: assertEquals("midnight", "20040606000000Z", rep);
082: }
083:
084: /**
085: * GENERALIZED TIME DER Encoder/Decoder test
086: * (byte array case)
087: * @throws ParseException
088: * @throws IOException
089: */
090: public final void testGeneralizedEncoderDecoder01()
091: throws ParseException, IOException {
092: runTest(false);
093: }
094:
095: /**
096: * GENERALIZED TIME DER Encoder/Decoder test
097: * (InputStream case)
098: * @throws ParseException
099: * @throws IOException
100: */
101: public final void testGeneralizedEncoderDecoder02()
102: throws ParseException, IOException {
103: runTest(true);
104: }
105:
106: private final void runTest(boolean useInputStream)
107: throws IOException, ParseException {
108: // full fractional seconds
109: Date myDate = new Date(1101980374187L);
110: byte[] encoded = new DerOutputStream(gTime, myDate).encoded;
111: DerInputStream dis = useInputStream ? new DerInputStream(
112: new ByteArrayInputStream(encoded))
113: : new DerInputStream(encoded);
114: assertEquals("full", myDate, gTime.decode(dis));
115:
116: // 2 digit fractional seconds (last 0 must be trimmed out)
117: myDate = new Date(1101980374180L);
118: encoded = new DerOutputStream(gTime, myDate).encoded;
119: dis = useInputStream ? new DerInputStream(
120: new ByteArrayInputStream(encoded))
121: : new DerInputStream(encoded);
122: assertEquals("2 fraction", myDate, gTime.decode(dis));
123:
124: // 1 digit fractional seconds (last 2 0s must be trimmed out)
125: myDate = new Date(1101980374100L);
126: encoded = new DerOutputStream(gTime, myDate).encoded;
127: dis = useInputStream ? new DerInputStream(
128: new ByteArrayInputStream(encoded))
129: : new DerInputStream(encoded);
130: assertEquals("1 fraction", myDate, gTime.decode(dis));
131:
132: // no fractional seconds (last 3 0s and "." must be trimmed out)
133: myDate = new Date(1101980374000L);
134: encoded = new DerOutputStream(gTime, myDate).encoded;
135: dis = useInputStream ? new DerInputStream(
136: new ByteArrayInputStream(encoded))
137: : new DerInputStream(encoded);
138: assertEquals("no fraction", myDate, gTime.decode(dis));
139:
140: // midnight
141: myDate = new SimpleDateFormat("MM.dd.yyyy HH:mm")
142: .parse("06.06.2004 00:00");
143: encoded = new DerOutputStream(gTime, myDate).encoded;
144: dis = useInputStream ? new DerInputStream(
145: new ByteArrayInputStream(encoded))
146: : new DerInputStream(encoded);
147: assertEquals("midnight", myDate, gTime.decode(dis));
148:
149: // date 100 ms
150: myDate = new Date(100L);
151: encoded = new DerOutputStream(gTime, myDate).encoded;
152: dis = useInputStream ? new DerInputStream(
153: new ByteArrayInputStream(encoded))
154: : new DerInputStream(encoded);
155: assertEquals("100ms", myDate, gTime.decode(dis));
156: }
157:
158: private static Date getGmtDate(long mills) {
159: return new Date(mills);
160: }
161: }
|