TimeParsing.java revision 17335:8be1b92492d2
1/* 2 * Copyright (c) 2002, 2017, 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 8181841 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 // 20010810174351.7654Z 81 private final static byte[] GEN_FRACT4_ZULU = 82 {0x18, 0x14, 0x32, 0x30, 0x30, 0x31, 0x30, 0x38, 0x31, 0x30, 0x31, 0x37, 0x34, 0x33, 0x35, 0x31, 0x2e, 0x37, 0x36, 0x35, 0x34, 0x5a}; 83 84 // 20010810184351.7+0100 85 private final static byte[] GEN_FRACT1_PLUS1 = 86 {0x18, 0x15, 0x32, 0x30, 0x30, 0x31, 0x30, 0x38, 0x31, 0x30, 0x31, 0x38, 0x34, 0x33, 0x35, 0x31, 0x2e, 0x37, 0x2b, 0x30, 0x31, 0x30, 0x30}; 87 88 // 20010810184351.76+0100 89 private final static byte[] GEN_FRACT2_PLUS1 = 90 {0x18, 0x16, 0x32, 0x30, 0x30, 0x31, 0x30, 0x38, 0x31, 0x30, 0x31, 0x38, 0x34, 0x33, 0x35, 0x31, 0x2e, 0x37, 0x36, 0x2b, 0x30, 0x31, 0x30, 0x30}; 91 92 // 20010810184351.765+0100 93 private final static byte[] GEN_FRACT3_PLUS1 = 94 {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}; 95 96 // 20010810184351.7654+0100 97 private final static byte[] GEN_FRACT4_PLUS1 = 98 {0x18, 0x18, 0x32, 0x30, 0x30, 0x31, 0x30, 0x38, 0x31, 0x30, 0x31, 0x38, 0x34, 0x33, 0x35, 0x31, 0x2e, 0x37, 0x36, 0x35, 0x34, 0x2b, 0x30, 0x31, 0x30, 0x30}; 99 100 // 20010810184351,765+0100 101 private final static byte[] GEN_FRACT3_COMMA_PLUS1 = 102 {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}; 103 104 // 20010810184351,7654+0100 105 private final static byte[] GEN_FRACT4_COMMA_PLUS1 = 106 {0x18, 0x18, 0x32, 0x30, 0x30, 0x31, 0x30, 0x38, 0x31, 0x30, 0x31, 0x38, 0x34, 0x33, 0x35, 0x31, 0x2c, 0x37, 0x36, 0x35, 0x34, 0x2b, 0x30, 0x31, 0x30, 0x30}; 107 108 private static Date decodeUTC(byte[] b) throws IOException { 109 DerInputStream derin = new DerInputStream(b); 110 return derin.getUTCTime(); 111 } 112 113 private static Date decodeGeneralized(byte[] b) throws IOException { 114 DerInputStream derin = new DerInputStream(b); 115 return derin.getGeneralizedTime(); 116 } 117 118 private static void checkUTC(Date d0, byte[] b, String text) throws Exception { 119 Date d1 = decodeUTC(b); 120 if( !d0.equals(d1) ) { 121 throw new Exception("UTCTime " + text + " failed: " + d1.toGMTString()); 122 } else { 123 System.out.println("UTCTime " + text + " ok"); 124 } 125 } 126 127 private static void checkGeneralized(Date d0, byte[] b, String text) throws Exception { 128 Date d1 = decodeGeneralized(b); 129 if( !d0.equals(d1) ) { 130 throw new Exception("GeneralizedTime " + text + " failed: " + d1.toGMTString()); 131 } else { 132 System.out.println("GeneralizedTime " + text + " ok"); 133 } 134 } 135 136 public static void main(String args[]) throws Exception { 137 Date d0 = new Date(TIME); 138 System.out.println(d0.toGMTString()); 139 140 checkUTC(d0, UTC_ZULU, "Zulu"); 141 checkUTC(d0, UTC_PLUS1, "+0100"); 142 checkUTC(d0, UTC_MINUS1, "-0100"); 143 144 checkGeneralized(d0, GEN_ZULU, "Zulu"); 145 checkGeneralized(d0, GEN_PLUS1, "+0100"); 146 checkGeneralized(d0, GEN_MINUS1, "-0100"); 147 148 Date d1 = new Date(TIME_FRACT1); 149 checkGeneralized(d1, GEN_FRACT1_ZULU, "fractional seconds (Zulu)"); 150 checkGeneralized(d1, GEN_FRACT1_PLUS1, "fractional seconds (+0100)"); 151 152 Date d2 = new Date(TIME_FRACT2); 153 checkGeneralized(d2, GEN_FRACT2_ZULU, "fractional seconds (Zulu)"); 154 checkGeneralized(d2, GEN_FRACT2_PLUS1, "fractional seconds (+0100)"); 155 156 Date d3 = new Date(TIME_FRACT3); 157 checkGeneralized(d3, GEN_FRACT3_ZULU, "fractional seconds (Zulu)"); 158 checkGeneralized(d3, GEN_FRACT3_PLUS1, "fractional seconds (+0100)"); 159 checkGeneralized(d3, GEN_FRACT3_COMMA_PLUS1, "fractional seconds (+0100)"); 160 checkGeneralized(d3, GEN_FRACT4_ZULU, "fractional seconds (Zulu)"); 161 checkGeneralized(d3, GEN_FRACT4_PLUS1, "fractional seconds (+0100)"); 162 checkGeneralized(d3, GEN_FRACT4_COMMA_PLUS1, "fractional seconds (+0100)"); 163 } 164} 165