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.text.ParseException;
022: import java.text.SimpleDateFormat;
023: import java.util.Arrays;
024: import java.util.Locale;
025: import java.util.TimeZone;
026:
027: import junit.framework.TestCase;
028:
029: import org.apache.harmony.security.asn1.ASN1UTCTime;
030: import org.apache.harmony.security.asn1.DerInputStream;
031: import org.apache.harmony.security.asn1.DerOutputStream;
032:
033: /**
034: * ASN.1 DER test for UTCTime type
035: *
036: * @see http://asn1.elibel.tm.fr/en/standards/index.htm
037: */
038:
039: public class UTCTimeTest extends TestCase {
040:
041: // UTC time decoder/encoder for testing
042: private static final ASN1UTCTime utime = ASN1UTCTime.getInstance();
043:
044: // data for testing with format: date string/DER encoding/Date object
045: public static final Object[][] validUTCTimes;
046:
047: static {
048: SimpleDateFormat sdf = new SimpleDateFormat(
049: "dd MMM yyyy HH:mm:ss", Locale.US);
050: sdf.setTimeZone(TimeZone.getTimeZone("GMT"));
051:
052: validUTCTimes = new Object[][] {
053: // YYMMDD-HHMMSS = "500708091011Z"
054: {
055: "8 Jul 1950 09:10:11",
056: new byte[] { 0x17, 0x0D, 0x35, 0x30, 0x30,
057: 0x37, 0x30, 0x38, 0x30, 0x39, 0x31,
058: 0x30, 0x31, 0x31, 0x5A }, null },
059: //YYMMDD-HHMMSS = "991213141516Z"
060: {
061: "13 Dec 1999 14:15:16",
062: new byte[] { 0x17, 0x0D, 0x39, 0x39, 0x31,
063: 0x32, 0x31, 0x33, 0x31, 0x34, 0x31,
064: 0x35, 0x31, 0x36, 0x5A }, null },
065: // YYMMDD-HHMMSS = "000101000000Z"
066: {
067: "01 Jan 2000 00:00:00",
068: new byte[] { 0x17, 0x0D, 0x30, 0x30, 0x30,
069: 0x31, 0x30, 0x31, 0x30, 0x30, 0x30,
070: 0x30, 0x30, 0x30, 0x5A }, null },
071: // YYMMDD-HHMMSS = "490203040506Z"
072: {
073: "3 Feb 2049 04:05:06",
074: new byte[] { 0x17, 0x0D, 0x34, 0x39, 0x30,
075: 0x32, 0x30, 0x33, 0x30, 0x34, 0x30,
076: 0x35, 0x30, 0x36, 0x5A }, null }, };
077:
078: try {
079: // fill values for Date objects by parsing date string
080: for (int i = 0; i < validUTCTimes.length; i++) {
081: validUTCTimes[i][2] = sdf
082: .parseObject((String) validUTCTimes[i][0]);
083: }
084: } catch (ParseException e) {
085: throw new RuntimeException(e.getMessage());
086: }
087: }
088:
089: /**
090: * Verifies decoding/encoding ASN.1 UTCTime.
091: * It must interpret the year field (YY) as follows:
092: * - if YY is greater than or equal to 50 then interpreted as 19YY
093: * - and if YY is less than 50 then interpreted as 20YY.
094: */
095: public void testDecodeEncode() throws Exception {
096:
097: // decoding byte array
098: for (int i = 0; i < validUTCTimes.length; i++) {
099: DerInputStream in = new DerInputStream(
100: (byte[]) validUTCTimes[i][1]);
101: assertEquals("Decoding array for " + validUTCTimes[i][0],
102: validUTCTimes[i][2], //expected
103: utime.decode(in)); //decoded
104: }
105:
106: // decoding input stream
107: for (int i = 0; i < validUTCTimes.length; i++) {
108: DerInputStream in = new DerInputStream(
109: new ByteArrayInputStream(
110: (byte[]) validUTCTimes[i][1]));
111: assertEquals("Decoding stream for " + validUTCTimes[i][0],
112: validUTCTimes[i][2], //expected
113: utime.decode(in)); //decoded
114: }
115:
116: // encoding date object
117: for (int i = 0; i < validUTCTimes.length; i++) {
118: DerOutputStream out = new DerOutputStream(utime,
119: validUTCTimes[i][2]);
120: assertTrue("Encoding date for " + validUTCTimes[i][0],
121: Arrays.equals((byte[]) validUTCTimes[i][1], // expected
122: out.encoded)); //encoded
123: }
124: }
125:
126: public static void main(String[] args) {
127: junit.textui.TestRunner.run(UTCTimeTest.class);
128: }
129: }
|