Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/sun/security/util/DerInputBuffer/TimeParsing.java
38853 views
/*1* Copyright (c) 2002, 2017, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24* @test25* @bug 4558835 4915146 818184126* @summary Verify timezone offset and fractional seconds are correctly parsed27*/2829import java.io.*;30import java.util.Date;3132import sun.security.util.DerInputStream;3334public class TimeParsing {3536// 10 Aug 2001 17:43:51 GMT37private final static long TIME = 997465431000l;38private final static long TIME_FRACT1 = 997465431700l;39private final static long TIME_FRACT2 = 997465431760l;40private final static long TIME_FRACT3 = 997465431765l;4142// 010810174351Z43private final static byte[] UTC_ZULU =44{0x17, 0x0d, 0x30, 0x31, 0x30, 0x38, 0x31, 0x30, 0x31, 0x37, 0x34, 0x33, 0x35, 0x31, 0x5a};4546// 010810184351+010047private final static byte[] UTC_PLUS1 =48{0x17, 0x11, 0x30, 0x31, 0x30, 0x38, 0x31, 0x30, 0x31, 0x38, 0x34, 0x33, 0x35, 0x31, 0x2b, 0x30, 0x31, 0x30, 0x30};4950// 010810164351-010051private final static byte[] UTC_MINUS1 =52{0x17, 0x11, 0x30, 0x31, 0x30, 0x38, 0x31, 0x30, 0x31, 0x36, 0x34, 0x33, 0x35, 0x31, 0x2d, 0x30, 0x31, 0x30, 0x30};5354// 20010810174351Z55private final static byte[] GEN_ZULU =56{0x18, 0x0f, 0x32, 0x30, 0x30, 0x31, 0x30, 0x38, 0x31, 0x30, 0x31, 0x37, 0x34, 0x33, 0x35, 0x31, 0x5a};5758// 20010810184351+010059private final static byte[] GEN_PLUS1 =60{0x18, 0x13, 0x32, 0x30, 0x30, 0x31, 0x30, 0x38, 0x31, 0x30, 0x31, 0x38, 0x34, 0x33, 0x35, 0x31, 0x2b, 0x30, 0x31, 0x30, 0x30};6162// 20010810164351-010063private final static byte[] GEN_MINUS1 =64{0x18, 0x13, 0x32, 0x30, 0x30, 0x31, 0x30, 0x38, 0x31, 0x30, 0x31, 0x36, 0x34, 0x33, 0x35, 0x31, 0x2d, 0x30, 0x31, 0x30, 0x30};6566// 20010810174351.7Z67private final static byte[] GEN_FRACT1_ZULU =68{0x18, 0x11, 0x32, 0x30, 0x30, 0x31, 0x30, 0x38, 0x31, 0x30, 0x31, 0x37, 0x34, 0x33, 0x35, 0x31, 0x2e, 0x37, 0x5a};6970// 20010810174351.76Z71private final static byte[] GEN_FRACT2_ZULU =72{0x18, 0x12, 0x32, 0x30, 0x30, 0x31, 0x30, 0x38, 0x31, 0x30, 0x31, 0x37, 0x34, 0x33, 0x35, 0x31, 0x2e, 0x37, 0x36, 0x5a};7374// 20010810174351.765Z75private final static byte[] GEN_FRACT3_ZULU =76{0x18, 0x13, 0x32, 0x30, 0x30, 0x31, 0x30, 0x38, 0x31, 0x30, 0x31, 0x37, 0x34, 0x33, 0x35, 0x31, 0x2e, 0x37, 0x36, 0x35, 0x5a};7778// 20010810174351.7654Z79private final static byte[] GEN_FRACT4_ZULU =80{0x18, 0x14, 0x32, 0x30, 0x30, 0x31, 0x30, 0x38, 0x31, 0x30, 0x31, 0x37, 0x34, 0x33, 0x35, 0x31, 0x2e, 0x37, 0x36, 0x35, 0x34, 0x5a};8182// 20010810184351.7+010083private final static byte[] GEN_FRACT1_PLUS1 =84{0x18, 0x15, 0x32, 0x30, 0x30, 0x31, 0x30, 0x38, 0x31, 0x30, 0x31, 0x38, 0x34, 0x33, 0x35, 0x31, 0x2e, 0x37, 0x2b, 0x30, 0x31, 0x30, 0x30};8586// 20010810184351.76+010087private final static byte[] GEN_FRACT2_PLUS1 =88{0x18, 0x16, 0x32, 0x30, 0x30, 0x31, 0x30, 0x38, 0x31, 0x30, 0x31, 0x38, 0x34, 0x33, 0x35, 0x31, 0x2e, 0x37, 0x36, 0x2b, 0x30, 0x31, 0x30, 0x30};8990// 20010810184351.765+010091private final static byte[] GEN_FRACT3_PLUS1 =92{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};9394// 20010810184351.7654+010095private final static byte[] GEN_FRACT4_PLUS1 =96{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};9798// 20010810184351,765+010099private final static byte[] GEN_FRACT3_COMMA_PLUS1 =100{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};101102// 20010810184351,7654+0100103private final static byte[] GEN_FRACT4_COMMA_PLUS1 =104{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};105106private static Date decodeUTC(byte[] b) throws IOException {107DerInputStream derin = new DerInputStream(b);108return derin.getUTCTime();109}110111private static Date decodeGeneralized(byte[] b) throws IOException {112DerInputStream derin = new DerInputStream(b);113return derin.getGeneralizedTime();114}115116private static void checkUTC(Date d0, byte[] b, String text) throws Exception {117Date d1 = decodeUTC(b);118if( !d0.equals(d1) ) {119throw new Exception("UTCTime " + text + " failed: " + d1.toGMTString());120} else {121System.out.println("UTCTime " + text + " ok");122}123}124125private static void checkGeneralized(Date d0, byte[] b, String text) throws Exception {126Date d1 = decodeGeneralized(b);127if( !d0.equals(d1) ) {128throw new Exception("GeneralizedTime " + text + " failed: " + d1.toGMTString());129} else {130System.out.println("GeneralizedTime " + text + " ok");131}132}133134public static void main(String args[]) throws Exception {135Date d0 = new Date(TIME);136System.out.println(d0.toGMTString());137138checkUTC(d0, UTC_ZULU, "Zulu");139checkUTC(d0, UTC_PLUS1, "+0100");140checkUTC(d0, UTC_MINUS1, "-0100");141142checkGeneralized(d0, GEN_ZULU, "Zulu");143checkGeneralized(d0, GEN_PLUS1, "+0100");144checkGeneralized(d0, GEN_MINUS1, "-0100");145146Date d1 = new Date(TIME_FRACT1);147checkGeneralized(d1, GEN_FRACT1_ZULU, "fractional seconds (Zulu)");148checkGeneralized(d1, GEN_FRACT1_PLUS1, "fractional seconds (+0100)");149150Date d2 = new Date(TIME_FRACT2);151checkGeneralized(d2, GEN_FRACT2_ZULU, "fractional seconds (Zulu)");152checkGeneralized(d2, GEN_FRACT2_PLUS1, "fractional seconds (+0100)");153154Date d3 = new Date(TIME_FRACT3);155checkGeneralized(d3, GEN_FRACT3_ZULU, "fractional seconds (Zulu)");156checkGeneralized(d3, GEN_FRACT3_PLUS1, "fractional seconds (+0100)");157checkGeneralized(d3, GEN_FRACT3_COMMA_PLUS1, "fractional seconds (+0100)");158checkGeneralized(d3, GEN_FRACT4_ZULU, "fractional seconds (Zulu)");159checkGeneralized(d3, GEN_FRACT4_PLUS1, "fractional seconds (+0100)");160checkGeneralized(d3, GEN_FRACT4_COMMA_PLUS1, "fractional seconds (+0100)");161}162}163164165