1/* 2 * Copyright (c) 2002, 2003, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 */ 23 24/* 25 * @test 26 * @bug 4558835 4915146 27 * @summary Verify timezone offset and fractional seconds are correctly parsed 28 * @modules java.base/sun.security.util 29 */ 30 31import java.io.*; 32import java.util.Date; 33 34import sun.security.util.DerInputStream; 35 36public class TimeParsing { 37 38 // 10 Aug 2001 17:43:51 GMT 39 private final static long TIME = 997465431000l; 40 private final static long TIME_FRACT1 = 997465431700l; 41 private final static long TIME_FRACT2 = 997465431760l; 42 private final static long TIME_FRACT3 = 997465431765l; 43 44 // 010810174351Z 45 private final static byte[] UTC_ZULU = 46 {0x17, 0x0d, 0x30, 0x31, 0x30, 0x38, 0x31, 0x30, 0x31, 0x37, 0x34, 0x33, 0x35, 0x31, 0x5a}; 47 48 // 010810184351+0100 49 private final static byte[] UTC_PLUS1 = 50 {0x17, 0x11, 0x30, 0x31, 0x30, 0x38, 0x31, 0x30, 0x31, 0x38, 0x34, 0x33, 0x35, 0x31, 0x2b, 0x30, 0x31, 0x30, 0x30}; 51 52 // 010810164351-0100 53 private final static byte[] UTC_MINUS1 = 54 {0x17, 0x11, 0x30, 0x31, 0x30, 0x38, 0x31, 0x30, 0x31, 0x36, 0x34, 0x33, 0x35, 0x31, 0x2d, 0x30, 0x31, 0x30, 0x30}; 55 56 // 20010810174351Z 57 private final static byte[] GEN_ZULU = 58 {0x18, 0x0f, 0x32, 0x30, 0x30, 0x31, 0x30, 0x38, 0x31, 0x30, 0x31, 0x37, 0x34, 0x33, 0x35, 0x31, 0x5a}; 59 60 // 20010810184351+0100 61 private final static byte[] GEN_PLUS1 = 62 {0x18, 0x13, 0x32, 0x30, 0x30, 0x31, 0x30, 0x38, 0x31, 0x30, 0x31, 0x38, 0x34, 0x33, 0x35, 0x31, 0x2b, 0x30, 0x31, 0x30, 0x30}; 63 64 // 20010810164351-0100 65 private final static byte[] GEN_MINUS1 = 66 {0x18, 0x13, 0x32, 0x30, 0x30, 0x31, 0x30, 0x38, 0x31, 0x30, 0x31, 0x36, 0x34, 0x33, 0x35, 0x31, 0x2d, 0x30, 0x31, 0x30, 0x30}; 67 68 // 20010810174351.7Z 69 private final static byte[] GEN_FRACT1_ZULU = 70 {0x18, 0x11, 0x32, 0x30, 0x30, 0x31, 0x30, 0x38, 0x31, 0x30, 0x31, 0x37, 0x34, 0x33, 0x35, 0x31, 0x2e, 0x37, 0x5a}; 71 72 // 20010810174351.76Z 73 private final static byte[] GEN_FRACT2_ZULU = 74 {0x18, 0x12, 0x32, 0x30, 0x30, 0x31, 0x30, 0x38, 0x31, 0x30, 0x31, 0x37, 0x34, 0x33, 0x35, 0x31, 0x2e, 0x37, 0x36, 0x5a}; 75 76 // 20010810174351.765Z 77 private final static byte[] GEN_FRACT3_ZULU = 78 {0x18, 0x13, 0x32, 0x30, 0x30, 0x31, 0x30, 0x38, 0x31, 0x30, 0x31, 0x37, 0x34, 0x33, 0x35, 0x31, 0x2e, 0x37, 0x36, 0x35, 0x5a}; 79 80 // 20010810184351.7+0100 81 private final static byte[] GEN_FRACT1_PLUS1 = 82 {0x18, 0x15, 0x32, 0x30, 0x30, 0x31, 0x30, 0x38, 0x31, 0x30, 0x31, 0x38, 0x34, 0x33, 0x35, 0x31, 0x2e, 0x37, 0x2b, 0x30, 0x31, 0x30, 0x30}; 83 84 // 20010810184351.76+0100 85 private final static byte[] GEN_FRACT2_PLUS1 = 86 {0x18, 0x16, 0x32, 0x30, 0x30, 0x31, 0x30, 0x38, 0x31, 0x30, 0x31, 0x38, 0x34, 0x33, 0x35, 0x31, 0x2e, 0x37, 0x36, 0x2b, 0x30, 0x31, 0x30, 0x30}; 87 88 // 20010810184351.765+0100 89 private final static byte[] GEN_FRACT3_PLUS1 = 90 {0x18, 0x17, 0x32, 0x30, 0x30, 0x31, 0x30, 0x38, 0x31, 0x30, 0x31, 0x38, 0x34, 0x33, 0x35, 0x31, 0x2e, 0x37, 0x36, 0x35, 0x2b, 0x30, 0x31, 0x30, 0x30}; 91 92 // 20010810184351,765+0100 93 private final static byte[] GEN_FRACT3_COMMA_PLUS1 = 94 {0x18, 0x17, 0x32, 0x30, 0x30, 0x31, 0x30, 0x38, 0x31, 0x30, 0x31, 0x38, 0x34, 0x33, 0x35, 0x31, 0x2c, 0x37, 0x36, 0x35, 0x2b, 0x30, 0x31, 0x30, 0x30}; 95 96 97 private static Date decodeUTC(byte[] b) throws IOException { 98 DerInputStream derin = new DerInputStream(b); 99 return derin.getUTCTime(); 100 } 101 102 private static Date decodeGeneralized(byte[] b) throws IOException { 103 DerInputStream derin = new DerInputStream(b); 104 return derin.getGeneralizedTime(); 105 } 106 107 private static void checkUTC(Date d0, byte[] b, String text) throws Exception { 108 Date d1 = decodeUTC(b); 109 if( !d0.equals(d1) ) { 110 throw new Exception("UTCTime " + text + " failed: " + d1.toGMTString()); 111 } else { 112 System.out.println("UTCTime " + text + " ok"); 113 } 114 } 115 116 private static void checkGeneralized(Date d0, byte[] b, String text) throws Exception { 117 Date d1 = decodeGeneralized(b); 118 if( !d0.equals(d1) ) { 119 throw new Exception("GeneralizedTime " + text + " failed: " + d1.toGMTString()); 120 } else { 121 System.out.println("GeneralizedTime " + text + " ok"); 122 } 123 } 124 125 public static void main(String args[]) throws Exception { 126 Date d0 = new Date(TIME); 127 System.out.println(d0.toGMTString()); 128 129 checkUTC(d0, UTC_ZULU, "Zulu"); 130 checkUTC(d0, UTC_PLUS1, "+0100"); 131 checkUTC(d0, UTC_MINUS1, "-0100"); 132 133 checkGeneralized(d0, GEN_ZULU, "Zulu"); 134 checkGeneralized(d0, GEN_PLUS1, "+0100"); 135 checkGeneralized(d0, GEN_MINUS1, "-0100"); 136 137 Date d1 = new Date(TIME_FRACT1); 138 checkGeneralized(d1, GEN_FRACT1_ZULU, "fractional seconds (Zulu)"); 139 checkGeneralized(d1, GEN_FRACT1_PLUS1, "fractional seconds (+0100)"); 140 141 Date d2 = new Date(TIME_FRACT2); 142 checkGeneralized(d2, GEN_FRACT2_ZULU, "fractional seconds (Zulu)"); 143 checkGeneralized(d2, GEN_FRACT2_PLUS1, "fractional seconds (+0100)"); 144 145 Date d3 = new Date(TIME_FRACT3); 146 checkGeneralized(d3, GEN_FRACT3_ZULU, "fractional seconds (Zulu)"); 147 checkGeneralized(d3, GEN_FRACT3_PLUS1, "fractional seconds (+0100)"); 148 checkGeneralized(d3, GEN_FRACT3_COMMA_PLUS1, "fractional seconds (+0100)"); 149 } 150 151} 152