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 package org.apache.harmony.security.tests.asn1.der; 19 20 import java.io.ByteArrayInputStream; 21 import java.io.IOException; 22 import java.text.ParseException; 23 import java.text.SimpleDateFormat; 24 import java.util.Arrays; 25 import java.util.Date; 26 import java.util.Locale; 27 import java.util.TimeZone; 28 29 import junit.framework.TestCase; 30 31 import org.apache.harmony.security.asn1.ASN1GeneralizedTime; 32 import org.apache.harmony.security.asn1.DerInputStream; 33 import org.apache.harmony.security.asn1.DerOutputStream; 34 35 /** 36 * ASN.1 DER test for GeneralizedTime type 37 * 38 * @see http://asn1.elibel.tm.fr/en/standards/index.htm 39 */ 40 41 public class GeneralizedTimeTest extends TestCase { 42 43 // decoder/encoder for testing 44 private static final ASN1GeneralizedTime gtime = ASN1GeneralizedTime 45 .getInstance(); 46 47 // data for testing with format: date string/DER encoding/Date object 48 private static final Object[][] validGeneralizedTimes; 49 50 static { 51 SimpleDateFormat sdf = new SimpleDateFormat("dd MMM yyyy HH:mm:ss", Locale.US); 52 sdf.setTimeZone(TimeZone.getTimeZone("GMT")); 53 54 validGeneralizedTimes = new Object[][] { 55 // YYYYMMDD-HHMMSS = "19000101000000Z" 56 { 57 "1 Jan 1900 00:00:00", 58 new byte[] { 0x18, 0x0F, 0x31, 0x39, 0x30, 0x30, 0x30, 59 0x31, 0x30, 0x31, 0x30, 0x30, 0x30, 0x30, 0x30, 60 0x30, 0x5A }, null }, 61 // YYYYMMDD-HHMMSS = "19490203040506Z" 62 { 63 "3 Feb 1949 04:05:06", 64 new byte[] { 0x18, 0x0F, 0x31, 0x39, 0x34, 0x39, 0x30, 65 0x32, 0x30, 0x33, 0x30, 0x34, 0x30, 0x35, 0x30, 66 0x36, 0x5A }, null }, 67 // YYYYMMDD-HHMMSS = "2000708091011Z" 68 { 69 "8 Jul 2000 09:10:11", 70 new byte[] { 0x18, 0x0F, 0x32, 0x30, 0x30, 0x30, 0x30, 71 0x37, 0x30, 0x38, 0x30, 0x39, 0x31, 0x30, 0x31, 72 0x31, 0x5A }, null }, 73 // YYYYMMDD-HHMMSS = "20501213141516Z" 74 { 75 "13 Dec 2050 14:15:16", 76 new byte[] { 0x18, 0x0F, 0x32, 0x30, 0x35, 0x30, 0x31, 77 0x32, 0x31, 0x33, 0x31, 0x34, 0x31, 0x35, 0x31, 78 0x36, 0x5A }, null }, 79 // YYYYMMDD-HHMMSS = "20501213141516Z" 80 { 81 "29 Mar 2332 06:56:40", 82 new byte[] { 0x18, 0x0F, 0x32, 0x33, 0x33, 0x32, 0x30, 83 0x33, 0x32, 0x39, 0x30, 0x36, 0x35, 0x36, 0x34, 84 0x30, 0x5A }, null }, 85 }; 86 87 try { 88 // fill values for Date objects by parsing date string 89 for (int i = 0; i < validGeneralizedTimes.length; i++) { 90 validGeneralizedTimes[i][2] = sdf 91 .parseObject((String) validGeneralizedTimes[i][0]); 92 } 93 } catch (ParseException e) { 94 throw new RuntimeException(e); 95 } 96 } 97 98 /** 99 * Verifies DER decoding/encoding ASN.1 GeneralizedTime. 100 * GeneralizedTime expresses Greenwich Mean Time by 101 * the following pattern: YYYYMMDDHHMMSS'Z' 102 */ 103 public void test_Decode_Encode() throws Exception { 104 105 // decoding byte array 106 for (int i = 0; i < validGeneralizedTimes.length; i++) { 107 DerInputStream in = new DerInputStream( 108 (byte[]) validGeneralizedTimes[i][1]); 109 assertEquals("Decoding array for " + validGeneralizedTimes[i][0], 110 validGeneralizedTimes[i][2], //expected 111 gtime.decode(in)); //decoded 112 } 113 114 // decoding input stream 115 for (int i = 0; i < validGeneralizedTimes.length; i++) { 116 DerInputStream in = new DerInputStream(new ByteArrayInputStream( 117 (byte[]) validGeneralizedTimes[i][1])); 118 assertEquals("Decoding stream for " + validGeneralizedTimes[i][0], 119 validGeneralizedTimes[i][2], //expected 120 gtime.decode(in)); //decoded 121 } 122 123 // encoding date object 124 for (int i = 0; i < validGeneralizedTimes.length; i++) { 125 DerOutputStream out = new DerOutputStream(gtime, 126 validGeneralizedTimes[i][2]); 127 assertTrue("Encoding date for " + validGeneralizedTimes[i][0], 128 Arrays.equals((byte[]) validGeneralizedTimes[i][1], // expected 129 out.encoded)); //encoded 130 } 131 } 132 133 /** 134 * Tests milliseconds result of encoding/decoding on the date after 2050. 135 */ 136 public void test_Milliseconds() throws IOException { 137 // Regression test for HARMONY-1252 138 long old_date = 11431151800000L; 139 long new_date = ((Date) gtime.decode(gtime.encode(new Date(old_date)))) 140 .getTime(); 141 assertEquals(old_date, new_date); 142 } 143 144 public void test_EncodeMilliseconds() throws IOException { 145 //cRegression for HARMONY-2302 146 long old_date = 1164358741071L; 147 long new_date = ((Date) gtime.decode(gtime.encode(new Date(old_date)))) 148 .getTime(); 149 assertEquals(old_date, new_date); 150 } 151 152 } 153