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: package org.apache.harmony.security.tests.asn1.der;
019:
020: import java.io.ByteArrayInputStream;
021: import java.io.IOException;
022: import java.text.ParseException;
023: import java.text.SimpleDateFormat;
024: import java.util.Arrays;
025: import java.util.Date;
026: import java.util.Locale;
027: import java.util.TimeZone;
028:
029: import junit.framework.TestCase;
030:
031: import org.apache.harmony.security.asn1.ASN1GeneralizedTime;
032: import org.apache.harmony.security.asn1.DerInputStream;
033: import org.apache.harmony.security.asn1.DerOutputStream;
034:
035: /**
036: * ASN.1 DER test for GeneralizedTime type
037: *
038: * @see http://asn1.elibel.tm.fr/en/standards/index.htm
039: */
040:
041: public class GeneralizedTimeTest extends TestCase {
042:
043: // decoder/encoder for testing
044: private static final ASN1GeneralizedTime gtime = ASN1GeneralizedTime
045: .getInstance();
046:
047: // data for testing with format: date string/DER encoding/Date object
048: private static final Object[][] validGeneralizedTimes;
049:
050: static {
051: SimpleDateFormat sdf = new SimpleDateFormat(
052: "dd MMM yyyy HH:mm:ss", Locale.US);
053: sdf.setTimeZone(TimeZone.getTimeZone("GMT"));
054:
055: validGeneralizedTimes = new Object[][] {
056: // YYYYMMDD-HHMMSS = "19000101000000Z"
057: {
058: "1 Jan 1900 00:00:00",
059: new byte[] { 0x18, 0x0F, 0x31, 0x39, 0x30,
060: 0x30, 0x30, 0x31, 0x30, 0x31, 0x30,
061: 0x30, 0x30, 0x30, 0x30, 0x30, 0x5A },
062: null },
063: // YYYYMMDD-HHMMSS = "19490203040506Z"
064: {
065: "3 Feb 1949 04:05:06",
066: new byte[] { 0x18, 0x0F, 0x31, 0x39, 0x34,
067: 0x39, 0x30, 0x32, 0x30, 0x33, 0x30,
068: 0x34, 0x30, 0x35, 0x30, 0x36, 0x5A },
069: null },
070: // YYYYMMDD-HHMMSS = "2000708091011Z"
071: {
072: "8 Jul 2000 09:10:11",
073: new byte[] { 0x18, 0x0F, 0x32, 0x30, 0x30,
074: 0x30, 0x30, 0x37, 0x30, 0x38, 0x30,
075: 0x39, 0x31, 0x30, 0x31, 0x31, 0x5A },
076: null },
077: // YYYYMMDD-HHMMSS = "20501213141516Z"
078: {
079: "13 Dec 2050 14:15:16",
080: new byte[] { 0x18, 0x0F, 0x32, 0x30, 0x35,
081: 0x30, 0x31, 0x32, 0x31, 0x33, 0x31,
082: 0x34, 0x31, 0x35, 0x31, 0x36, 0x5A },
083: null },
084: // YYYYMMDD-HHMMSS = "20501213141516Z"
085: {
086: "29 Mar 2332 06:56:40",
087: new byte[] { 0x18, 0x0F, 0x32, 0x33, 0x33,
088: 0x32, 0x30, 0x33, 0x32, 0x39, 0x30,
089: 0x36, 0x35, 0x36, 0x34, 0x30, 0x5A },
090: null }, };
091:
092: try {
093: // fill values for Date objects by parsing date string
094: for (int i = 0; i < validGeneralizedTimes.length; i++) {
095: validGeneralizedTimes[i][2] = sdf
096: .parseObject((String) validGeneralizedTimes[i][0]);
097: }
098: } catch (ParseException e) {
099: throw new RuntimeException(e);
100: }
101: }
102:
103: /**
104: * Verifies DER decoding/encoding ASN.1 GeneralizedTime.
105: * GeneralizedTime expresses Greenwich Mean Time by
106: * the following pattern: YYYYMMDDHHMMSS'Z'
107: */
108: public void test_Decode_Encode() throws Exception {
109:
110: // decoding byte array
111: for (int i = 0; i < validGeneralizedTimes.length; i++) {
112: DerInputStream in = new DerInputStream(
113: (byte[]) validGeneralizedTimes[i][1]);
114: assertEquals("Decoding array for "
115: + validGeneralizedTimes[i][0],
116: validGeneralizedTimes[i][2], //expected
117: gtime.decode(in)); //decoded
118: }
119:
120: // decoding input stream
121: for (int i = 0; i < validGeneralizedTimes.length; i++) {
122: DerInputStream in = new DerInputStream(
123: new ByteArrayInputStream(
124: (byte[]) validGeneralizedTimes[i][1]));
125: assertEquals("Decoding stream for "
126: + validGeneralizedTimes[i][0],
127: validGeneralizedTimes[i][2], //expected
128: gtime.decode(in)); //decoded
129: }
130:
131: // encoding date object
132: for (int i = 0; i < validGeneralizedTimes.length; i++) {
133: DerOutputStream out = new DerOutputStream(gtime,
134: validGeneralizedTimes[i][2]);
135: assertTrue("Encoding date for "
136: + validGeneralizedTimes[i][0], Arrays.equals(
137: (byte[]) validGeneralizedTimes[i][1], // expected
138: out.encoded)); //encoded
139: }
140: }
141:
142: /**
143: * Tests milliseconds result of encoding/decoding on the date after 2050.
144: */
145: public void test_Milliseconds() throws IOException {
146: // Regression test for HARMONY-1252
147: long old_date = 11431151800000L;
148: long new_date = ((Date) gtime.decode(gtime.encode(new Date(
149: old_date)))).getTime();
150: assertEquals(old_date, new_date);
151: }
152:
153: public void test_EncodeMilliseconds() throws IOException {
154: //cRegression for HARMONY-2302
155: long old_date = 1164358741071L;
156: long new_date = ((Date) gtime.decode(gtime.encode(new Date(
157: old_date)))).getTime();
158: assertEquals(old_date, new_date);
159: }
160:
161: public static void main(String[] args) {
162: junit.textui.TestRunner.run(GeneralizedTimeTest.class);
163: }
164: }
|